RK3368 GPU: Rogue N Init.
[firefly-linux-kernel-4.4.55.git] / drivers / staging / imgtec / rogue / mem_utils.c
1 /*************************************************************************/ /*!
2 @File
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
7
8 The contents of this file are subject to the MIT license as set out below.
9
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:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
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.
23
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.
31
32 This License is also included in this distribution in the file called
33 "MIT-COPYING".
34
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 */ /**************************************************************************/
43
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.)
47  */
48
49 #if defined(__arm64__) || defined(__aarch64__) || defined (PVRSRV_DEVMEM_TEST_SAFE_MEMSETCPY)
50
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.
54  */
55 #include <stddef.h>
56
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);
60
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
63  * __SIZEOF__ macros.
64  */
65 #if defined(__GNUC__)
66
67 #define MIN(a, b) \
68  ({__typeof(a) _a = (a); __typeof(b) _b = (b); _a > _b ? _b : _a;})
69
70 #if !defined(DEVICE_MEMSETCPY_ALIGN_IN_BYTES)
71 #define DEVICE_MEMSETCPY_ALIGN_IN_BYTES __SIZEOF_LONG__
72 #endif
73 #if DEVICE_MEMSETCPY_ALIGN_IN_BYTES % 2 != 0
74 #error "DEVICE_MEMSETCPY_ALIGN_IN_BYTES must be a power of 2"
75 #endif
76 #if DEVICE_MEMSETCPY_ALIGN_IN_BYTES < 4
77 #error "DEVICE_MEMSETCPY_ALIGN_IN_BYTES must be equal or greater than 4"
78 #endif
79
80 #if __SIZEOF_POINTER__ != __SIZEOF_LONG__
81 #error No support for architectures where void* and long are sized differently
82 #endif
83
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
94 #   define REGSZ "w"
95 #   define REGCL "w"
96 #   define BVCLB "r"
97 #  elif DEVICE_MEMSETCPY_ALIGN_IN_BYTES == 16
98 #   define DEVICE_MEMSETCPY_ARM64
99 #   define REGSZ "x"
100 #   define REGCL "x"
101 #   define BVCLB "r"
102 #  elif DEVICE_MEMSETCPY_ALIGN_IN_BYTES == 32
103 #   if defined(__ARM_NEON_FP)
104 #    define DEVICE_MEMSETCPY_ARM64
105 #    define REGSZ "q"
106 #    define REGCL "v"
107 #    define BVCLB "w"
108 #   endif
109 #  endif
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")
114 #    define LDP "ldnp"
115 #    define STP "stnp"
116 #   else
117 #    define NSHLD()
118 #    define NSHST()
119 #    define LDP "ldp"
120 #    define STP "stp"
121 #   endif
122  typedef unsigned int block_half_t
123         __attribute__((vector_size(DEVICE_MEMSETCPY_ALIGN_IN_BYTES / 2)));
124 #  endif
125 # endif
126 #endif
127
128 __attribute__((visibility("hidden")))
129 void DeviceMemCopy(void *pvDst, const void *pvSrc, size_t uSize)
130 {
131         volatile const char *pcSrc = pvSrc;
132         volatile char *pcDst = pvDst;
133         size_t uPreambleBytes;
134         int bBlockCopy = 0;
135
136         size_t uSrcUnaligned = (size_t)pcSrc % sizeof(block_t);
137         size_t uDstUnaligned = (size_t)pcDst % sizeof(block_t);
138
139         if (!uSrcUnaligned && !uDstUnaligned)
140         {
141                 /* Neither pointer is unaligned. Optimal case. */
142                 bBlockCopy = 1;
143         }
144         else
145         {
146                 if (uSrcUnaligned == sizeof(int) && uDstUnaligned == sizeof(int))
147                 {
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?
151                          */
152                         if (uSize >= sizeof(int))
153                         {
154                                 volatile int *piSrc = (int *)pcSrc;
155                                 volatile int *piDst = (int *)pcDst;
156
157                                 while (uSize >= sizeof(int))
158                                 {
159                                         *piDst++ = *piSrc++;
160                                         uSize -= sizeof(int);
161                                 }
162
163                                 pcSrc = (char *)piSrc;
164                                 pcDst = (char *)piDst;
165                         }
166                 }
167                 else if (uSrcUnaligned == uDstUnaligned)
168                 {
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
171                          * optimize the rest.
172                          */
173                         uPreambleBytes = MIN(sizeof(block_t) - uDstUnaligned, uSize);
174                         uSize -= uPreambleBytes;
175                         while (uPreambleBytes)
176                         {
177                                 *pcDst++ = *pcSrc++;
178                                 uPreambleBytes--;
179                         }
180
181                         bBlockCopy = 1;
182                 }
183         }
184
185         if (bBlockCopy && uSize >= sizeof(block_t))
186         {
187                 volatile block_t *pSrc = (block_t *)pcSrc;
188                 volatile block_t *pDst = (block_t *)pcDst;
189
190                 NSHLD();
191
192                 while (uSize >= sizeof(block_t))
193                 {
194 #if defined(DEVICE_MEMSETCPY_ARM64)
195                         __asm__ (LDP " " REGSZ "0, " REGSZ "1, [%[pSrc]]\n\t"
196                                  STP " " REGSZ "0, " REGSZ "1, [%[pDst]]"
197                                                 :
198                                                 : [pSrc] "r" (pSrc), [pDst] "r" (pDst)
199                                                 : "memory", REGCL "0", REGCL "1");
200 #else
201                         *pDst = *pSrc;
202 #endif
203                         pDst++;
204                         pSrc++;
205                         uSize -= sizeof(block_t);
206                 }
207
208                 NSHST();
209
210                 pcSrc = (char *)pSrc;
211                 pcDst = (char *)pDst;
212         }
213
214         while (uSize)
215         {
216                 *pcDst++ = *pcSrc++;
217                 uSize--;
218         }
219 }
220
221 __attribute__((visibility("hidden")))
222 void DeviceMemSet(void *pvDst, unsigned char ui8Value, size_t uSize)
223 {
224         volatile char *pcDst = pvDst;
225         size_t uPreambleBytes;
226
227         size_t uDstUnaligned = (size_t)pcDst % sizeof(block_t);
228
229         if (uDstUnaligned)
230         {
231                 uPreambleBytes = MIN(sizeof(block_t) - uDstUnaligned, uSize);
232                 uSize -= uPreambleBytes;
233                 while (uPreambleBytes)
234                 {
235                         *pcDst++ = ui8Value;
236                         uPreambleBytes--;
237                 }
238         }
239
240         if (uSize >= sizeof(block_t))
241         {
242                 volatile block_t *pDst = (block_t *)pcDst;
243 #if defined(DEVICE_MEMSETCPY_ARM64)
244                 block_half_t bValue;
245 #else
246                 block_t bValue;
247 #endif
248                 size_t i;
249
250                 for (i = 0; i < sizeof(bValue) / sizeof(unsigned int); i++)
251                         bValue[i] = ui8Value << 24U |
252                                     ui8Value << 16U |
253                                     ui8Value <<  8U |
254                                     ui8Value;
255
256                 NSHLD();
257
258                 while (uSize >= sizeof(block_t))
259                 {
260 #if defined(DEVICE_MEMSETCPY_ARM64)
261                         __asm__ (STP " %" REGSZ "[bValue], %" REGSZ "[bValue], [%[pDst]]"
262                                                 :
263                                                 : [bValue] BVCLB (bValue), [pDst] "r" (pDst)
264                                                 : "memory");
265 #else
266                         *pDst = bValue;
267 #endif
268                         pDst++;
269                         uSize -= sizeof(block_t);
270                 }
271
272                 NSHST();
273
274                 pcDst = (char *)pDst;
275         }
276
277         while (uSize)
278         {
279                 *pcDst++ = ui8Value;
280                 uSize--;
281         }
282 }
283
284 #else /* !defined(__GNUC__) */
285
286 /* Potentially very slow (but safe) fallbacks for non-GNU C compilers */
287
288 void DeviceMemCopy(void *pvDst, const void *pvSrc, size_t uSize)
289 {
290         volatile const char *pcSrc = pvSrc;
291         volatile char *pcDst = pvDst;
292
293         while (uSize)
294         {
295                 *pcDst++ = *pcSrc++;
296                 uSize--;
297         }
298 }
299
300 void DeviceMemSet(void *pvDst, unsigned char ui8Value, size_t uSize)
301 {
302         volatile char *pcDst = pvDst;
303
304         while (uSize)
305         {
306                 *pcDst++ = ui8Value;
307                 uSize--;
308         }
309 }
310
311 #endif /* !defined(__GNUC__) */
312
313 #endif /* defined(__arm64__) || defined(__aarch64__) || defined (PVRSRV_DEVMEM_TEST_SAFE_MEMSETCPY) */