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 */ /**************************************************************************/
45 #if defined(__KERNEL__)
47 #include <linux/string.h>
53 #if (defined(__arm64__) || defined(__aarch64__) || defined (PVRSRV_DEVMEM_SAFE_MEMSETCPY)) && !defined(__QNXNTO__)
55 #define ZERO_BUF_SIZE 1024
58 #define PVRSRV_MEM_ALIGN __attribute__ ((aligned (0x8)))
59 #define PVRSRV_MEM_64BIT_ALIGN_MASK (0x7)
60 #define PVRSRV_MEM_32BIT_ALIGN_MASK (0x3)
62 #error "PVRSRV Alignment macros need to be defined for this compiler"
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
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
78 ******************************************************************************/
79 IMG_EXPORT IMG_VOID PVRSRVDeviceMemCopy(
81 const IMG_VOID* pvSrc,
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;
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))
93 IMG_SIZE_T uiAlignedSize;
96 uiHeadSize = (sizeof(void *)
97 - ((IMG_UINTPTR_T)pbySrc & PVRSRV_MEM_64BIT_ALIGN_MASK))
98 & PVRSRV_MEM_64BIT_ALIGN_MASK;
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
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)
109 uiSize -= uiHeadSize;
110 uiTailSize = uiSize & PVRSRV_MEM_64BIT_ALIGN_MASK;
111 uiAlignedSize = uiSize - uiTailSize;
113 /* Copy few leading bytes to align pointer to 64bit boundary. */
116 *pbyDst++ = *pbySrc++;
119 /* here pointers are already 64bit aligned so we can use memcpy. */
120 memcpy((IMG_VOID*)pbyDst, (IMG_VOID*)pbySrc, uiAlignedSize);
122 /* skip over copied data */
123 pbyDst += uiAlignedSize;
124 pbySrc += uiAlignedSize;
127 /* If pointers are 32bit aligned but not aligned in relation to each
129 else if ((((IMG_UINTPTR_T)pbySrc | (IMG_UINTPTR_T)pbyDst)
130 & PVRSRV_MEM_32BIT_ALIGN_MASK) == 0)
132 volatile IMG_UINT32 * pui32Src = (IMG_UINT32*)pbySrc;
133 volatile IMG_UINT32 * pui32Dst = (IMG_UINT32*)pbyDst;
134 IMG_SIZE_T uiAlignedSize;
136 uiTailSize = uiSize & PVRSRV_MEM_32BIT_ALIGN_MASK;
137 uiAlignedSize = uiSize - uiTailSize;
139 /* do the 4 byte copy */
140 uiSize = uiSize >> 2;
143 *pui32Dst++ = *pui32Src++;
146 pbyDst += uiAlignedSize;
147 pbySrc += uiAlignedSize;
150 /* Copy either remaining memory if optimisation was performed but
151 * size was not aligned or all memory if we need to. */
154 *pbyDst++ = *pbySrc++;
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
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
172 ******************************************************************************/
173 IMG_EXPORT IMG_VOID PVRSRVDeviceMemSet(
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 };
182 /* Run workaround if one of the address or size is not aligned, or
184 if ((ui8Value == 0) || ((((IMG_SIZE_T)pbyDst | uiSize) & PVRSRV_MEM_64BIT_ALIGN_MASK) != 0))
186 IMG_UINT32 uiTailSize;
188 /* Buffer address unaligned */
189 if ((IMG_SIZE_T)pbyDst & PVRSRV_MEM_64BIT_ALIGN_MASK)
191 /* Increment the buffer pointer */
192 for (; uiSize > 0 && ((IMG_SIZE_T)pbyDst & PVRSRV_MEM_64BIT_ALIGN_MASK); uiSize--)
194 *pbyDst++ = ui8Value;
196 /* Did loop stop because size is zero? */
197 if (uiSize == 0) return;
200 /* Set the remaining part of the buffer */
204 uiTailSize = (uiSize & PVRSRV_MEM_64BIT_ALIGN_MASK);
206 memset((IMG_VOID*) pbyDst, (IMG_INT) ui8Value, (size_t) uiSize-uiTailSize);
207 pbyDst += uiSize-uiTailSize;
212 uiTailSize = (uiSize & PVRSRV_MEM_64BIT_ALIGN_MASK);
213 uiSize -= uiTailSize;
215 while (uiSize > 1024)
217 memcpy((IMG_VOID*) pbyDst, gZeroBuf, (size_t) ZERO_BUF_SIZE);
218 pbyDst +=ZERO_BUF_SIZE;
219 uiSize -= ZERO_BUF_SIZE;
221 memcpy((IMG_VOID*) pbyDst, gZeroBuf, (size_t) uiSize);
225 /* Handle any tail bytes, loop skipped in tail is 0 */
226 for (; uiTailSize > 0; uiTailSize--)
228 *pbyDst++ = ui8Value;
231 /* Alignment fine, non-zero set, no need to work around device memory
232 * use with ARM64 libc */
235 memset(pvDest, (IMG_INT) ui8Value, (size_t) uiSize);
239 #else /* (defined(__arm64__) || defined(__aarch64__) || defined (PVRSRV_DEVMEM_SAFE_MEMSETCPY)) && !defined(__QNXNTO__) */
241 IMG_EXPORT IMG_VOID PVRSRVDeviceMemCopy(
243 const IMG_VOID* pvSrc,
246 memcpy(pvDst, pvSrc, uiSize);
249 IMG_EXPORT IMG_VOID PVRSRVDeviceMemSet(
254 memset(pvDest, ui8Value, uiSize);
257 #endif /* (defined(__arm64__) || defined(__aarch64__) || defined (PVRSRV_DEVMEM_SAFE_MEMSETCPY)) && !defined(__QNXNTO__) */