source: rtems/bsps/powerpc/shared/start/memcpy.c @ e560ee85

Last change on this file since e560ee85 was e560ee85, checked in by Joel Sherrill <joel@…>, on 03/01/22 at 21:38:55

bsps/powerpc/: Scripted embedded brains header file clean up

Updates #4625.

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2 * Copyright (c) 2011 embedded brains GmbH.  All rights reserved.
3 *
4 * The license and distribution terms for this file may be
5 * found in the file LICENSE in this distribution or at
6 * http://www.rtems.org/license/LICENSE.
7 */
8
9#include <bspopts.h>
10#include <rtems/powerpc/powerpc.h>
11
12#if BSP_DATA_CACHE_ENABLED \
13  && PPC_CACHE_ALIGNMENT == 32 \
14  && !defined(BSP_DATA_CACHE_USE_WRITE_THROUGH)
15
16#include <string.h>
17#include <stdint.h>
18#include <stdbool.h>
19
20#include <libcpu/powerpc-utility.h>
21
22#define WORD_SIZE 4
23
24#define WORD_MASK (WORD_SIZE - 1)
25
26static bool aligned(const void *a, const void *b)
27{
28  return ((((uintptr_t) a) | ((uintptr_t) b)) & WORD_MASK) == 0;
29}
30
31void *memcpy(void *dst_ptr, const void *src_ptr, size_t n)
32{
33  uint8_t *dst = dst_ptr;
34  const uint8_t *src = src_ptr;
35
36  ppc_data_cache_block_touch(src);
37
38  if (__builtin_expect(n >= WORD_SIZE && aligned(src, dst), 1)) {
39    uint32_t *word_dst = (uint32_t *) dst - 1;
40    const uint32_t *word_src = (const uint32_t *) src - 1;
41
42    if (n >= 2 * PPC_CACHE_ALIGNMENT - WORD_SIZE) {
43      while ((uintptr_t) (word_dst + 1) % PPC_CACHE_ALIGNMENT != 0) {
44        uint32_t tmp;
45        __asm__ volatile (
46          "lwzu %[tmp], 0x4(%[src])\n"
47          "stwu %[tmp], 0x4(%[dst])\n"
48          : [src] "+b" (word_src),
49            [dst] "+b" (word_dst),
50            [tmp] "=&r" (tmp)
51        );
52        n -= WORD_SIZE;
53      }
54
55      while (n >= PPC_CACHE_ALIGNMENT) {
56        uint32_t dst_offset = 4;
57        uint32_t src_offset = 32 + 4;
58        uint32_t tmp0;
59        uint32_t tmp1;
60        uint32_t tmp2;
61        uint32_t tmp3;
62        __asm__ volatile (
63          "dcbz %[dst],  %[dst_offset]\n"
64          "lwz  %[tmp0], 0x04(%[src])\n"
65          "dcbt %[src],  %[src_offset]\n"
66          "lwz  %[tmp1], 0x08(%[src])\n"
67          "lwz  %[tmp2], 0x0c(%[src])\n"
68          "lwz  %[tmp3], 0x10(%[src])\n"
69          "stw  %[tmp0], 0x04(%[dst])\n"
70          "stw  %[tmp1], 0x08(%[dst])\n"
71          "stw  %[tmp2], 0x0c(%[dst])\n"
72          "stw  %[tmp3], 0x10(%[dst])\n"
73          "lwz  %[tmp0], 0x14(%[src])\n"
74          "lwz  %[tmp1], 0x18(%[src])\n"
75          "lwz  %[tmp2], 0x1c(%[src])\n"
76          "lwzu %[tmp3], 0x20(%[src])\n"
77          "stw  %[tmp0], 0x14(%[dst])\n"
78          "stw  %[tmp1], 0x18(%[dst])\n"
79          "stw  %[tmp2], 0x1c(%[dst])\n"
80          "stwu %[tmp3], 0x20(%[dst])\n"
81          : [src] "+b" (word_src),
82            [dst] "+b" (word_dst),
83            [tmp0] "=&r" (tmp0),
84            [tmp1] "=&r" (tmp1),
85            [tmp2] "=&r" (tmp2),
86            [tmp3] "=&r" (tmp3)
87          : [src_offset] "r" (src_offset),
88            [dst_offset] "r" (dst_offset)
89        );
90        n -= PPC_CACHE_ALIGNMENT;
91      }
92    }
93
94    while (n >= WORD_SIZE) {
95      uint32_t tmp;
96      __asm__ volatile (
97        "lwzu %[tmp], 0x4(%[src])\n"
98        "stwu %[tmp], 0x4(%[dst])\n"
99        : [src] "+b" (word_src),
100          [dst] "+b" (word_dst),
101          [tmp] "=&r" (tmp)
102      );
103      n -= WORD_SIZE;
104    }
105
106    dst = (uint8_t *) word_dst + 4;
107    src = (const uint8_t *) word_src + 4;
108  }
109
110  while (n > 0) {
111    *dst = *src;
112    ++src;
113    ++dst;
114    --n;
115  }
116
117  return dst_ptr;
118}
119
120#endif /* BSP_DATA_CACHE_ENABLED && PPC_CACHE_ALIGNMENT == 32 */
Note: See TracBrowser for help on using the repository browser.