1 /*************************************************************************/ /*!
3 @Title Memory manipulation functions
4 @Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @Description Memory related functions
6 @License Dual MIT/GPLv2
8 The contents of this file are subject to the MIT license as set out below.
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
20 Alternatively, the contents of this file may be used under the terms of
21 the GNU General Public License Version 2 ("GPL") in which case the provisions
22 of GPL are applicable instead of those above.
24 If you wish to allow use of your version of this file only under the terms of
25 GPL, and not to allow others to use your version of this file under the terms
26 of the MIT license, indicate your decision by deleting the provisions above
27 and replace them with the notice and other provisions required by GPL as set
28 out in the file called "GPL-COPYING" included in this distribution. If you do
29 not delete the provisions above, a recipient may use your version of this file
30 under the terms of either the MIT license or GPL.
32 This License is also included in this distribution in the file called
35 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
36 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
37 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
38 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
39 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
40 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
41 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 */ /**************************************************************************/
44 /* This workaround is only *required* on ARM64. Avoid building or including
45 * it by default on other architectures, unless the 'safe memcpy' test flag
46 * is enabled. (The code should work on other architectures.)
49 #if defined(__arm64__) || defined(__aarch64__) || defined (PVRSRV_DEVMEM_TEST_SAFE_MEMSETCPY)
51 /* NOTE: This C file is compiled with -ffreestanding to avoid pattern matching
52 * by the compiler to stdlib functions, and it must only use the below
53 * headers. Do not include any IMG or services headers in this file.
57 /* Prototypes to suppress warnings in -ffreestanding mode */
58 void DeviceMemCopy(void *pvDst, const void *pvSrc, size_t uSize);
59 void DeviceMemSet(void *pvDst, unsigned char ui8Value, size_t uSize);
61 /* This file is only intended to be used on platforms which use GCC or Clang,
62 * due to its requirement on __attribute__((vector_size(n))), typeof() and
68 ({__typeof(a) _a = (a); __typeof(b) _b = (b); _a > _b ? _b : _a;})
70 #if !defined(DEVICE_MEMSETCPY_ALIGN_IN_BYTES)
71 #define DEVICE_MEMSETCPY_ALIGN_IN_BYTES __SIZEOF_LONG__
73 #if DEVICE_MEMSETCPY_ALIGN_IN_BYTES % 2 != 0
74 #error "DEVICE_MEMSETCPY_ALIGN_IN_BYTES must be a power of 2"
76 #if DEVICE_MEMSETCPY_ALIGN_IN_BYTES < 4
77 #error "DEVICE_MEMSETCPY_ALIGN_IN_BYTES must be equal or greater than 4"
80 #if __SIZEOF_POINTER__ != __SIZEOF_LONG__
81 #error No support for architectures where void* and long are sized differently
84 #if __SIZEOF_LONG__ > DEVICE_MEMSETCPY_ALIGN_IN_BYTES
85 /* Meaningless, and harder to do correctly */
86 # error Cannot handle DEVICE_MEMSETCPY_ALIGN_IN_BYTES < sizeof(long)
87 typedef unsigned long block_t;
88 #elif __SIZEOF_LONG__ <= DEVICE_MEMSETCPY_ALIGN_IN_BYTES
89 typedef unsigned int block_t
90 __attribute__((vector_size(DEVICE_MEMSETCPY_ALIGN_IN_BYTES)));
91 # if defined(__arm64__) || defined(__aarch64__)
92 # if DEVICE_MEMSETCPY_ALIGN_IN_BYTES == 8
93 # define DEVICE_MEMSETCPY_ARM64
97 # elif DEVICE_MEMSETCPY_ALIGN_IN_BYTES == 16
98 # define DEVICE_MEMSETCPY_ARM64
102 # elif DEVICE_MEMSETCPY_ALIGN_IN_BYTES == 32
103 # if defined(__ARM_NEON_FP)
104 # define DEVICE_MEMSETCPY_ARM64
110 # if defined(DEVICE_MEMSETCPY_ARM64)
111 # if defined(DEVICE_MEMSETCPY_ARM64_NON_TEMPORAL)
112 # define NSHLD() __asm__ ("dmb nshld")
113 # define NSHST() __asm__ ("dmb nshst")
122 typedef unsigned int block_half_t
123 __attribute__((vector_size(DEVICE_MEMSETCPY_ALIGN_IN_BYTES / 2)));
128 __attribute__((visibility("hidden")))
129 void DeviceMemCopy(void *pvDst, const void *pvSrc, size_t uSize)
131 volatile const char *pcSrc = pvSrc;
132 volatile char *pcDst = pvDst;
133 size_t uPreambleBytes;
136 size_t uSrcUnaligned = (size_t)pcSrc % sizeof(block_t);
137 size_t uDstUnaligned = (size_t)pcDst % sizeof(block_t);
139 if (!uSrcUnaligned && !uDstUnaligned)
141 /* Neither pointer is unaligned. Optimal case. */
146 if (uSrcUnaligned == sizeof(int) && uDstUnaligned == sizeof(int))
148 /* Both pointers are at least 32-bit aligned, and we assume that
149 * the processor must handle all kinds of 32-bit load-stores.
150 * NOTE: Could we optimize this with a non-temporal version?
152 if (uSize >= sizeof(int))
154 volatile int *piSrc = (int *)pcSrc;
155 volatile int *piDst = (int *)pcDst;
157 while (uSize >= sizeof(int))
160 uSize -= sizeof(int);
163 pcSrc = (char *)piSrc;
164 pcDst = (char *)piDst;
167 else if (uSrcUnaligned == uDstUnaligned)
169 /* Neither pointer is usefully aligned, but they are misaligned in
170 * the same way, so we can copy a preamble in a slow way, then
173 uPreambleBytes = MIN(sizeof(block_t) - uDstUnaligned, uSize);
174 uSize -= uPreambleBytes;
175 while (uPreambleBytes)
185 if (bBlockCopy && uSize >= sizeof(block_t))
187 volatile block_t *pSrc = (block_t *)pcSrc;
188 volatile block_t *pDst = (block_t *)pcDst;
192 while (uSize >= sizeof(block_t))
194 #if defined(DEVICE_MEMSETCPY_ARM64)
195 __asm__ (LDP " " REGSZ "0, " REGSZ "1, [%[pSrc]]\n\t"
196 STP " " REGSZ "0, " REGSZ "1, [%[pDst]]"
198 : [pSrc] "r" (pSrc), [pDst] "r" (pDst)
199 : "memory", REGCL "0", REGCL "1");
205 uSize -= sizeof(block_t);
210 pcSrc = (char *)pSrc;
211 pcDst = (char *)pDst;
221 __attribute__((visibility("hidden")))
222 void DeviceMemSet(void *pvDst, unsigned char ui8Value, size_t uSize)
224 volatile char *pcDst = pvDst;
225 size_t uPreambleBytes;
227 size_t uDstUnaligned = (size_t)pcDst % sizeof(block_t);
231 uPreambleBytes = MIN(sizeof(block_t) - uDstUnaligned, uSize);
232 uSize -= uPreambleBytes;
233 while (uPreambleBytes)
240 if (uSize >= sizeof(block_t))
242 volatile block_t *pDst = (block_t *)pcDst;
243 #if defined(DEVICE_MEMSETCPY_ARM64)
250 for (i = 0; i < sizeof(bValue) / sizeof(unsigned int); i++)
251 bValue[i] = ui8Value << 24U |
258 while (uSize >= sizeof(block_t))
260 #if defined(DEVICE_MEMSETCPY_ARM64)
261 __asm__ (STP " %" REGSZ "[bValue], %" REGSZ "[bValue], [%[pDst]]"
263 : [bValue] BVCLB (bValue), [pDst] "r" (pDst)
269 uSize -= sizeof(block_t);
274 pcDst = (char *)pDst;
284 #else /* !defined(__GNUC__) */
286 /* Potentially very slow (but safe) fallbacks for non-GNU C compilers */
288 void DeviceMemCopy(void *pvDst, const void *pvSrc, size_t uSize)
290 volatile const char *pcSrc = pvSrc;
291 volatile char *pcDst = pvDst;
300 void DeviceMemSet(void *pvDst, unsigned char ui8Value, size_t uSize)
302 volatile char *pcDst = pvDst;
311 #endif /* !defined(__GNUC__) */
313 #endif /* defined(__arm64__) || defined(__aarch64__) || defined (PVRSRV_DEVMEM_TEST_SAFE_MEMSETCPY) */