RK3368 GPU version Rogue M 1.28
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / rogue_m / services / shared / common / 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
45 #if defined(__KERNEL__)
46 #include "osfunc.h"
47 #include <linux/string.h>
48 #else
49 #include "services.h"
50 #include <string.h>
51 #endif
52
53 #if (defined(__arm64__) || defined(__aarch64__) || defined (PVRSRV_DEVMEM_SAFE_MEMSETCPY)) && !defined(__QNXNTO__)
54
55 #define ZERO_BUF_SIZE 1024
56
57 #if defined(__GNUC__)
58 #define PVRSRV_MEM_ALIGN __attribute__ ((aligned (0x8)))
59 #define PVRSRV_MEM_64BIT_ALIGN_MASK (0x7)
60 #define PVRSRV_MEM_32BIT_ALIGN_MASK (0x3)
61 #else
62 #error "PVRSRV Alignment macros need to be defined for this compiler"
63 #endif
64
65 /******************************************************************************
66  Function Name      : OSDeviceMemCopy / PVRSRVDeviceMemCopy
67  Inputs             : pvDst - pointer to the destination memory region
68                       pvSrc - pointer to the source memory region
69                       uiSize - size of the memory region that will be copied
70  Outputs            :
71  Returns            :
72  Description        : This is a counterpart of standard memcpy function for
73                       uncached memory. The reason for this function is that
74                       when uncached memory is used and the pointers are not
75                       64-bit aligned on Aarch64 architecture a memory
76                       exception will be thrown. This applies both to user and
77                       kernel space.
78 ******************************************************************************/
79 IMG_EXPORT IMG_VOID PVRSRVDeviceMemCopy(
80         IMG_VOID* pvDst,
81         const IMG_VOID* pvSrc,
82         IMG_SIZE_T uiSize)
83 {
84         /* Use volatile to avoid compiler optimisations */
85         volatile IMG_BYTE * pbySrc = (IMG_BYTE*)pvSrc;
86         volatile IMG_BYTE * pbyDst = (IMG_BYTE*)pvDst;
87         IMG_SIZE_T uiTailSize = uiSize;
88
89         /* If both pointers have same alignment we can optimise. */
90         if (((IMG_UINTPTR_T)pbySrc & PVRSRV_MEM_64BIT_ALIGN_MASK)
91                 == ((IMG_UINTPTR_T)pbyDst & PVRSRV_MEM_64BIT_ALIGN_MASK))
92         {
93                 IMG_SIZE_T uiAlignedSize;
94                 IMG_UINT uiHeadSize;
95
96                 uiHeadSize = (sizeof(void *)
97                         - ((IMG_UINTPTR_T)pbySrc & PVRSRV_MEM_64BIT_ALIGN_MASK))
98                         & PVRSRV_MEM_64BIT_ALIGN_MASK;
99
100                 /* For 64bit aligned pointers we will almost always (not if uiSize is 0)
101                  * go in and use memcpy if the size is large enough. For other aligned
102                  * pointers if size is large enough we will copy first few bytes to
103                  * align those pointers to 64bit then use memcpy and after that copy
104                  * remaining bytes.
105                  * If uiSize is less then uiHeadSize we will skip to byte-by-byte
106                  * copy since we can't use memcpy in such case. */
107                 if (uiSize > uiHeadSize)
108                 {
109                         uiSize -= uiHeadSize;
110                         uiTailSize = uiSize & PVRSRV_MEM_64BIT_ALIGN_MASK;
111                         uiAlignedSize = uiSize - uiTailSize;
112
113                         /* Copy few leading bytes to align pointer to 64bit boundary. */
114                         while (uiHeadSize--)
115                         {
116                                 *pbyDst++ = *pbySrc++;
117                         }
118
119                         /* here pointers are already 64bit aligned so we can use memcpy. */
120                         memcpy((IMG_VOID*)pbyDst, (IMG_VOID*)pbySrc, uiAlignedSize);
121
122                         /* skip over copied data */
123                         pbyDst += uiAlignedSize;
124                         pbySrc += uiAlignedSize;
125                 }
126         }
127         /* If pointers are 32bit aligned but not aligned in relation to each
128          * other.*/
129         else if ((((IMG_UINTPTR_T)pbySrc | (IMG_UINTPTR_T)pbyDst)
130                 & PVRSRV_MEM_32BIT_ALIGN_MASK) == 0)
131         {
132                 volatile IMG_UINT32 * pui32Src = (IMG_UINT32*)pbySrc;
133                 volatile IMG_UINT32 * pui32Dst = (IMG_UINT32*)pbyDst;
134                 IMG_SIZE_T uiAlignedSize;
135
136                 uiTailSize = uiSize & PVRSRV_MEM_32BIT_ALIGN_MASK;
137                 uiAlignedSize = uiSize - uiTailSize;
138
139                 /* do the 4 byte copy */
140                 uiSize = uiSize >> 2;
141                 while (uiSize--)
142                 {
143                         *pui32Dst++ = *pui32Src++;
144                 }
145
146                 pbyDst += uiAlignedSize;
147                 pbySrc += uiAlignedSize;
148         }
149
150         /* Copy either remaining memory if optimisation was performed but
151          * size was not aligned or all memory if we need to. */
152         while (uiTailSize--)
153         {
154                 *pbyDst++ = *pbySrc++;
155         }
156
157 }
158
159 /******************************************************************************
160  Function Name      : OSDeviceMemSet / PVRSRVDeviceMemSet
161  Inputs             : pvDest - pointer to destination memory
162                       ui8Value - the 'set' value
163                       uiSize - size of the memory block
164  Outputs            :
165  Returns            :
166  Description        : This is a counterpart of standard memset function for
167                       uncached memory. The reason for this function is that
168                       when uncached memory is used and the pointer is not
169                       64-bit aligned on Aarch64 architecture an memory
170                       exception will be thrown. This applies both to user and
171                       kernel space.
172 ******************************************************************************/
173 IMG_EXPORT IMG_VOID PVRSRVDeviceMemSet(
174         IMG_VOID *pvDest,
175         IMG_UINT8 ui8Value,
176         IMG_SIZE_T uiSize)
177 {
178         /* Use volatile to avoid compiler optimisations */
179         volatile IMG_BYTE * pbyDst = (IMG_BYTE*)pvDest;
180         static IMG_BYTE gZeroBuf[ZERO_BUF_SIZE] PVRSRV_MEM_ALIGN = { 0 };
181
182         /* Run workaround if one of the address or size is not aligned, or
183          * we are zeroing */
184         if ((ui8Value == 0) || ((((IMG_SIZE_T)pbyDst | uiSize) & PVRSRV_MEM_64BIT_ALIGN_MASK) != 0))
185         {
186                 IMG_UINT32 uiTailSize;
187
188                 /* Buffer address unaligned */
189                 if ((IMG_SIZE_T)pbyDst & PVRSRV_MEM_64BIT_ALIGN_MASK)
190                 {
191                         /* Increment the buffer pointer */
192                         for (; uiSize > 0 && ((IMG_SIZE_T)pbyDst & PVRSRV_MEM_64BIT_ALIGN_MASK); uiSize--)
193                         {
194                                 *pbyDst++ = ui8Value;
195                         }
196                         /* Did loop stop because size is zero? */
197                         if (uiSize == 0) return;
198                 }
199
200                 /* Set the remaining part of the buffer */
201                 if (ui8Value)
202                 {
203                         /* Non-zero set */
204                         uiTailSize = (uiSize & PVRSRV_MEM_64BIT_ALIGN_MASK);
205
206                         memset((IMG_VOID*) pbyDst, (IMG_INT) ui8Value, (size_t) uiSize-uiTailSize);
207                         pbyDst += uiSize-uiTailSize;
208                 }
209                 else
210                 {
211                         /* Zero set */
212                         uiTailSize = (uiSize & PVRSRV_MEM_64BIT_ALIGN_MASK);
213                         uiSize -= uiTailSize;
214
215                         while (uiSize > 1024)
216                         {
217                                 memcpy((IMG_VOID*) pbyDst, gZeroBuf, (size_t) ZERO_BUF_SIZE);
218                                 pbyDst +=ZERO_BUF_SIZE;
219                                 uiSize -= ZERO_BUF_SIZE;
220                         }
221                         memcpy((IMG_VOID*) pbyDst, gZeroBuf, (size_t) uiSize);
222                         pbyDst += uiSize;
223                 }
224
225                 /* Handle any tail bytes, loop skipped in tail is 0 */
226                 for (; uiTailSize > 0; uiTailSize--)
227                 {
228                         *pbyDst++ = ui8Value;
229                 }
230         }
231         /* Alignment fine, non-zero set, no need to work around device memory
232          * use with ARM64 libc */
233         else
234         {
235                 memset(pvDest, (IMG_INT) ui8Value, (size_t) uiSize);
236         }
237 }
238
239 #else /* (defined(__arm64__) || defined(__aarch64__) || defined (PVRSRV_DEVMEM_SAFE_MEMSETCPY)) && !defined(__QNXNTO__) */
240
241 IMG_EXPORT IMG_VOID PVRSRVDeviceMemCopy(
242         IMG_VOID*       pvDst,
243         const IMG_VOID* pvSrc,
244         IMG_SIZE_T      uiSize)
245 {
246         memcpy(pvDst, pvSrc, uiSize);
247 }
248
249 IMG_EXPORT IMG_VOID PVRSRVDeviceMemSet(
250         IMG_VOID *pvDest,
251         IMG_UINT8 ui8Value,
252         IMG_SIZE_T uiSize)
253 {
254         memset(pvDest, ui8Value, uiSize);
255 }
256
257 #endif /* (defined(__arm64__) || defined(__aarch64__) || defined (PVRSRV_DEVMEM_SAFE_MEMSETCPY)) && !defined(__QNXNTO__) */