osfunc_common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*************************************************************************/ /*!
  2. @File
  3. @Title OS functions header
  4. @Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
  5. @Description OS specific API definitions
  6. @License Dual MIT/GPLv2
  7. The contents of this file are subject to the MIT license as set out below.
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. Alternatively, the contents of this file may be used under the terms of
  17. the GNU General Public License Version 2 ("GPL") in which case the provisions
  18. of GPL are applicable instead of those above.
  19. If you wish to allow use of your version of this file only under the terms of
  20. GPL, and not to allow others to use your version of this file under the terms
  21. of the MIT license, indicate your decision by deleting the provisions above
  22. and replace them with the notice and other provisions required by GPL as set
  23. out in the file called "GPL-COPYING" included in this distribution. If you do
  24. not delete the provisions above, a recipient may use your version of this file
  25. under the terms of either the MIT license or GPL.
  26. This License is also included in this distribution in the file called
  27. "MIT-COPYING".
  28. EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
  29. PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  30. BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  31. PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
  32. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  33. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  34. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. */ /**************************************************************************/
  36. #ifndef OSFUNC_COMMON_H
  37. /*! @cond Doxygen_Suppress */
  38. #define OSFUNC_COMMON_H
  39. /*! @endcond */
  40. #if defined(__KERNEL__) && defined(__linux__)
  41. #include <linux/string.h>
  42. #else
  43. #include <string.h>
  44. #endif
  45. #include "img_types.h"
  46. #ifdef __cplusplus
  47. extern "C"
  48. {
  49. #endif
  50. /**************************************************************************/ /*!
  51. @Function DeviceMemSet
  52. @Description Set memory, whose mapping may be uncached, to a given value.
  53. Safe implementation for all architectures for uncached mapping,
  54. optimised for speed where supported by tool chains.
  55. In such cases, OSDeviceMemSet() is defined as a call to this
  56. function.
  57. @Input pvDest void pointer to the memory to be set
  58. @Input ui8Value byte containing the value to be set
  59. @Input ui32Size the number of bytes to be set to the given value
  60. @Return None
  61. */ /**************************************************************************/
  62. void DeviceMemSet(void *pvDest, IMG_UINT8 ui8Value, size_t ui32Size);
  63. /**************************************************************************/ /*!
  64. @Function DeviceMemCopy
  65. @Description Copy values from one area of memory. Safe implementation for
  66. all architectures for uncached mapping, of either the source
  67. or destination, optimised for speed where supported by tool
  68. chains. In such cases, OSDeviceMemCopy() is defined as a call
  69. to this function.
  70. @Input pvDst void pointer to the destination memory
  71. @Input pvSrc void pointer to the source memory
  72. @Input ui32Size the number of bytes to be copied
  73. @Return None
  74. */ /**************************************************************************/
  75. void DeviceMemCopy(void *pvDst, const void *pvSrc, size_t ui32Size);
  76. /**************************************************************************/ /*!
  77. @Function DeviceMemSetBytes
  78. @Description Potentially very slow (but safe) memset fallback for non-GNU C
  79. compilers for arm64/aarch64
  80. @Input pvDest void pointer to the memory to be set
  81. @Input ui8Value byte containing the value to be set
  82. @Input ui32Size the number of bytes to be set to the given value
  83. @Return None
  84. */ /**************************************************************************/
  85. void DeviceMemSetBytes(void *pvDest, IMG_UINT8 ui8Value, size_t ui32Size);
  86. /**************************************************************************/ /*!
  87. @Function DeviceMemCopyBytes
  88. @Description Potentially very slow (but safe) memcpy fallback for non-GNU C
  89. compilers for arm64/aarch64
  90. @Input pvDst void pointer to the destination memory
  91. @Input pvSrc void pointer to the source memory
  92. @Input ui32Size the number of bytes to be copied
  93. @Return None
  94. */ /**************************************************************************/
  95. void DeviceMemCopyBytes(void *pvDst, const void *pvSrc, size_t ui32Size);
  96. /**************************************************************************/ /*!
  97. @Function StringLCopy
  98. @Description Copy at most uDataSize-1 bytes from pszSrc to pszDest.
  99. If no null byte ('\0') is contained within the first uDataSize-1
  100. characters of the source string, the destination string will be
  101. truncated. If the length of the source string is less than uDataSize
  102. an additional NUL byte will be copied to the destination string
  103. to ensure that the string is NUL-terminated.
  104. @Input pszDest char pointer to the destination string
  105. @Input pszSrc const char pointer to the source string
  106. @Input uDataSize the maximum number of bytes to be copied
  107. @Return Size of the source string
  108. */ /**************************************************************************/
  109. size_t StringLCopy(IMG_CHAR *pszDest, const IMG_CHAR *pszSrc, size_t uDataSize);
  110. #if defined(__arm64__) || defined(__aarch64__) || defined(PVRSRV_DEVMEM_TEST_SAFE_MEMSETCPY)
  111. #if defined(__GNUC__)
  112. /* Workarounds for assumptions made that memory will not be mapped uncached
  113. * in kernel or user address spaces on arm64 platforms (or other testing).
  114. */
  115. #define OSDeviceMemSet(a,b,c) DeviceMemSet((a), (b), (c))
  116. #define OSDeviceMemCopy(a,b,c) DeviceMemCopy((a), (b), (c))
  117. #else /* defined __GNUC__ */
  118. #define OSDeviceMemSet(a,b,c) DeviceMemSetBytes((a), (b), (c))
  119. #define OSDeviceMemCopy(a,b,c) DeviceMemCopyBytes((a), (b), (c))
  120. #endif /* defined __GNUC__ */
  121. #else /* (defined(__arm64__) || defined(__aarch64__) || defined(PVRSRV_DEVMEM_TEST_SAFE_MEMSETCPY)) */
  122. /* Everything else */
  123. /**************************************************************************/ /*!
  124. @Function OSDeviceMemSet
  125. @Description Set memory, whose mapping may be uncached, to a given value.
  126. On some architectures, additional processing may be needed
  127. if the mapping is uncached.
  128. @Input a void pointer to the memory to be set
  129. @Input b byte containing the value to be set
  130. @Input c the number of bytes to be set to the given value
  131. @Return Pointer to the destination memory.
  132. */ /**************************************************************************/
  133. #define OSDeviceMemSet(a,b,c) memset((a), (b), (c))
  134. /**************************************************************************/ /*!
  135. @Function OSDeviceMemCopy
  136. @Description Copy values from one area of memory, to another, when one
  137. or both mappings may be uncached.
  138. On some architectures, additional processing may be needed
  139. if mappings are uncached.
  140. @Input a void pointer to the destination memory
  141. @Input b void pointer to the source memory
  142. @Input c the number of bytes to be copied
  143. @Return Pointer to the destination memory.
  144. */ /**************************************************************************/
  145. #define OSDeviceMemCopy(a,b,c) memcpy((a), (b), (c))
  146. #endif /* (defined(__arm64__) || defined(__aarch64__) || defined(PVRSRV_DEVMEM_TEST_SAFE_MEMSETCPY)) */
  147. /**************************************************************************/ /*!
  148. @Function OSCachedMemSet
  149. @Description Set memory, where the mapping is known to be cached, to a
  150. given value. This function exists to allow an optimal memset
  151. to be performed when memory is known to be cached.
  152. @Input a void pointer to the memory to be set
  153. @Input b byte containing the value to be set
  154. @Input c the number of bytes to be set to the given value
  155. @Return Pointer to the destination memory.
  156. */ /**************************************************************************/
  157. #define OSCachedMemSet(a,b,c) memset((a), (b), (c))
  158. /**************************************************************************/ /*!
  159. @Function OSCachedMemCopy
  160. @Description Copy values from one area of memory, to another, when both
  161. mappings are known to be cached.
  162. This function exists to allow an optimal memcpy to be
  163. performed when memory is known to be cached.
  164. @Input a void pointer to the destination memory
  165. @Input b void pointer to the source memory
  166. @Input c the number of bytes to be copied
  167. @Return Pointer to the destination memory.
  168. */ /**************************************************************************/
  169. #define OSCachedMemCopy(a,b,c) memcpy((a), (b), (c))
  170. #if defined(__KERNEL__)
  171. /**************************************************************************/ /*!
  172. @Function OSCachedMemSetWMB
  173. @Description Set memory, where the mapping is known to be cached or
  174. write-combine, to a given value and issue a write memory barrier
  175. after. This
  176. function exists to allow an optimal memset to be performed when
  177. memory is known to be cached or write-combine.
  178. @Input a void pointer to the memory to be set
  179. @Input b byte containing the value to be set
  180. @Input c the number of bytes to be set to the given value
  181. @Return Pointer to the destination memory.
  182. */ /**************************************************************************/
  183. #define OSCachedMemSetWMB(a,b,c) \
  184. do { \
  185. (void) memset((a), (b), (c)); \
  186. OSWriteMemoryBarrier(); \
  187. } while (0)
  188. /**************************************************************************/ /*!
  189. @Function OSCachedMemCopy
  190. @Description Copy values from one area of memory, to another, when both
  191. mappings are known to be cached or write-combine and issue
  192. a write memory barrier after.
  193. This function exists to allow an optimal memcpy to be
  194. performed when memory is known to be cached or write-combine.
  195. @Input a void pointer to the destination memory
  196. @Input b void pointer to the source memory
  197. @Input c the number of bytes to be copied
  198. @Return Pointer to the destination memory.
  199. */ /**************************************************************************/
  200. #define OSCachedMemCopyWMB(a,b,c) \
  201. do { \
  202. (void) memcpy((a), (b), (c)); \
  203. OSWriteMemoryBarrier(); \
  204. } while (0)
  205. #endif /* defined(__KERNEL__) */
  206. /**************************************************************************/ /*!
  207. @Function OSStringLCopy
  208. @Description Copy at most uDataSize-1 bytes from pszSrc to pszDest.
  209. If no null byte ('\0') is contained within the first uDataSize-1
  210. characters of the source string, the destination string will be
  211. truncated. If the length of the source string is less than uDataSize
  212. an additional NUL byte will be copied to the destination string
  213. to ensure that the string is NUL-terminated.
  214. @Input a char pointer to the destination string
  215. @Input b const char pointer to the source string
  216. @Input c the maximum number of bytes to be copied
  217. @Return Size of the source string
  218. */ /**************************************************************************/
  219. #if defined(__QNXNTO__) || (defined(__linux__) && defined(__KERNEL__) && !defined(DEBUG))
  220. #define OSStringLCopy(a,b,c) strlcpy((a), (b), (c))
  221. #else /* defined(__QNXNTO__) ... */
  222. #define OSStringLCopy(a,b,c) StringLCopy((a), (b), (c))
  223. #endif /* defined(__QNXNTO__) ... */
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #endif /* OSFUNC_COMMON_H */
  228. /******************************************************************************
  229. End of file (osfunc_common.h)
  230. ******************************************************************************/