img_defs.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*************************************************************************/ /*!
  2. @File
  3. @Title Common header containing type definitions for portability
  4. @Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
  5. @Description Contains variable and structure definitions. Any platform
  6. specific types should be defined in this file.
  7. @License Strictly Confidential.
  8. */ /**************************************************************************/
  9. #ifndef IMG_DEFS_H
  10. #define IMG_DEFS_H
  11. #if defined(__linux__) && defined(__KERNEL__)
  12. #include <linux/types.h>
  13. #else
  14. #include <stddef.h>
  15. #endif
  16. #if !(defined(__linux__) && defined(__KERNEL__))
  17. #include <assert.h>
  18. #endif
  19. #include "img_types.h"
  20. #if defined(NO_INLINE_FUNCS)
  21. #define INLINE
  22. #define FORCE_INLINE
  23. #else
  24. #if defined(__cplusplus) || defined(INTEGRITY_OS)
  25. #if !defined(INLINE)
  26. #define INLINE inline
  27. #endif
  28. #define FORCE_INLINE static inline
  29. #else
  30. #if !defined(INLINE)
  31. #define INLINE __inline
  32. #endif
  33. #if (defined(UNDER_WDDM) || defined(WINDOWS_WDF)) && defined(_X86_)
  34. #define FORCE_INLINE __forceinline
  35. #else
  36. #define FORCE_INLINE static __inline
  37. #endif
  38. #endif
  39. #endif
  40. /* True if the GCC version is at least the given version. False for older
  41. * versions of GCC, or other compilers.
  42. */
  43. #if defined(__GNUC__)
  44. #define GCC_VERSION_AT_LEAST(major, minor) \
  45. (__GNUC__ > (major) || \
  46. (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
  47. #else
  48. #define GCC_VERSION_AT_LEAST(major, minor) 0
  49. #endif
  50. /* Use Clang's __has_extension and __has_builtin macros if available. */
  51. #if defined(__has_extension)
  52. #define has_clang_extension(e) __has_extension(e)
  53. #else
  54. #define has_clang_extension(e) 0
  55. #endif
  56. #if defined(__has_builtin)
  57. #define has_clang_builtin(e) __has_builtin(e)
  58. #else
  59. #define has_clang_builtin(e) 0
  60. #endif
  61. /* Use this in any file, or use attributes under GCC - see below */
  62. #ifndef PVR_UNREFERENCED_PARAMETER
  63. #define PVR_UNREFERENCED_PARAMETER(param) ((void)(param))
  64. #endif
  65. /* static_assert(condition, "message to print if it fails");
  66. *
  67. * Assert something at compile time. If the assertion fails, try to print
  68. * the message, otherwise do nothing. static_assert is available if:
  69. *
  70. * - It's already defined as a macro (e.g. by <assert.h> in C11)
  71. * - We're using MSVC which exposes static_assert unconditionally
  72. * - We're using a C++ compiler that supports C++11
  73. * - We're using GCC 4.6 and up in C mode (in which case it's available as
  74. * _Static_assert)
  75. *
  76. * In all other cases, fall back to an equivalent that makes an invalid
  77. * declaration.
  78. */
  79. #if !defined(static_assert) && !defined(_MSC_VER) && \
  80. (!defined(__cplusplus) || __cplusplus < 201103L) || defined(__KLOCWORK__)
  81. /* static_assert isn't already available */
  82. #if !defined(__cplusplus) && (GCC_VERSION_AT_LEAST(4, 6) || \
  83. (defined(__clang__) && has_clang_extension(c_static_assert)))
  84. #define static_assert _Static_assert
  85. #else
  86. #define static_assert(expr, message) \
  87. extern int static_assert_failed[(expr) ? 1 : -1] __attribute__((unused))
  88. #endif
  89. #endif
  90. /*
  91. * unreachable("explanation") can be used to indicate to the compiler that
  92. * some parts of the code can never be reached, like the default branch
  93. * of a switch that covers all real-world possibilities, even though there
  94. * are other ints that exist for instance.
  95. *
  96. * The message will be printed as an assert() when debugging.
  97. *
  98. * Note: there is no need to add a 'return' or any error handling after
  99. * calling unreachable(), as this call will never return.
  100. */
  101. #if defined(__linux__) && defined(__KERNEL__)
  102. /* Kernel has its own unreachable(), which is a simple infinite loop */
  103. #elif GCC_VERSION_AT_LEAST(4, 5) || has_clang_builtin(__builtin_unreachable)
  104. #define unreachable(msg) \
  105. do { \
  106. assert(!(msg)); \
  107. __builtin_unreachable(); \
  108. } while (false)
  109. #elif defined(_MSC_VER)
  110. #define unreachable(msg) \
  111. do { \
  112. assert(!(msg)); \
  113. __assume(0); \
  114. } while (false)
  115. #else
  116. #define unreachable(msg) \
  117. do { \
  118. assert(!(msg)); \
  119. while (1); \
  120. } while (false)
  121. #endif
  122. /*
  123. * assume(x > 2 && x <= 7) works like an assert(), except it hints to the
  124. * compiler what it can assume to optimise the code, like a limited range
  125. * of parameter values.
  126. */
  127. #if has_clang_builtin(__builtin_assume)
  128. #define assume(expr) \
  129. do { \
  130. assert(expr); \
  131. __builtin_assume(expr); \
  132. } while (false)
  133. #elif defined(_MSC_VER)
  134. #define assume(expr) \
  135. do { \
  136. assert(expr); \
  137. __assume(expr); \
  138. } while (false)
  139. #elif defined(__linux__) && defined(__KERNEL__)
  140. #define assume(expr) ((void)(expr))
  141. #elif GCC_VERSION_AT_LEAST(4, 5) || has_clang_builtin(__builtin_unreachable)
  142. #define assume(expr) \
  143. do { \
  144. if (unlikely(!(expr))) \
  145. unreachable("Assumption isn't true: " # expr); \
  146. } while (false)
  147. #else
  148. #define assume(expr) assert(expr)
  149. #endif
  150. /*! Macro to calculate the n-byte aligned value from that supplied rounding up.
  151. * n must be a power of two.
  152. *
  153. * Both arguments should be of a type with the same size otherwise the macro may
  154. * cut off digits, e.g. imagine a 64 bit address in _x and a 32 bit value in _n.
  155. */
  156. #define PVR_ALIGN(_x, _n) (((_x)+((_n)-1U)) & ~((_n)-1U))
  157. #if defined(_WIN32)
  158. #if defined(WINDOWS_WDF)
  159. /*
  160. * For WINDOWS_WDF drivers we don't want these defines to overwrite calling conventions propagated through the build system.
  161. * This 'empty' choice helps to resolve all the calling conv issues.
  162. *
  163. */
  164. #define IMG_CALLCONV
  165. #define C_CALLCONV
  166. #define IMG_INTERNAL
  167. #define IMG_RESTRICT __restrict
  168. /*
  169. * The proper way of dll linking under MS compilers is made of two things:
  170. * - decorate implementation with __declspec(dllexport)
  171. * this decoration helps compiler with making the so called
  172. * 'export library'
  173. * - decorate forward-declaration (in a source dependent on a dll) with
  174. * __declspec(dllimport), this decoration helps the compiler to make
  175. * faster and smaller code in terms of calling dll-imported functions
  176. *
  177. * Usually these decorations are performed by having a single macro define
  178. * making that expands to a proper __declspec() depending on the
  179. * translation unit, dllexport inside the dll source and dllimport outside
  180. * the dll source. Having IMG_EXPORT and IMG_IMPORT resolving to the same
  181. * __declspec() makes no sense, but at least works.
  182. */
  183. #define IMG_IMPORT __declspec(dllexport)
  184. #define IMG_EXPORT __declspec(dllexport)
  185. #else
  186. #define IMG_CALLCONV __stdcall
  187. #define IMG_INTERNAL
  188. #define IMG_EXPORT __declspec(dllexport)
  189. #define IMG_RESTRICT __restrict
  190. #define C_CALLCONV __cdecl
  191. /*
  192. * IMG_IMPORT is defined as IMG_EXPORT so that headers and implementations
  193. * match. Some compilers require the header to be declared IMPORT, while
  194. * the implementation is declared EXPORT.
  195. */
  196. #define IMG_IMPORT IMG_EXPORT
  197. #endif
  198. #if defined(UNDER_WDDM)
  199. #ifndef _INC_STDLIB
  200. #if defined(__mips)
  201. /* do nothing */
  202. #elif defined(UNDER_MSBUILD)
  203. /* do nothing */
  204. #else
  205. _CRTIMP void __cdecl abort(void);
  206. #endif
  207. #endif
  208. #endif /* UNDER_WDDM */
  209. #else
  210. #if (defined(__linux__) || defined(__QNXNTO__)) && defined(__KERNEL__)
  211. #define IMG_INTERNAL
  212. #define IMG_EXPORT
  213. #define IMG_CALLCONV
  214. #elif defined(__linux__) || defined(__METAG) || defined(__mips) || defined(__QNXNTO__) || defined(__riscv)
  215. #define IMG_CALLCONV
  216. #define C_CALLCONV
  217. #if defined(__METAG)
  218. #define IMG_INTERNAL
  219. #else
  220. #define IMG_INTERNAL __attribute__((visibility("hidden")))
  221. #endif
  222. #define IMG_EXPORT __attribute__((visibility("default")))
  223. #define IMG_RESTRICT __restrict__
  224. #elif defined(INTEGRITY_OS)
  225. #define IMG_CALLCONV
  226. #define IMG_INTERNAL
  227. #define IMG_EXPORT
  228. #define IMG_RESTRICT
  229. #define C_CALLCONV
  230. #define __cdecl
  231. #ifndef USE_CODE
  232. #define IMG_ABORT() printf("IMG_ABORT was called.\n")
  233. #endif
  234. #else
  235. #error("define an OS")
  236. #endif
  237. #endif
  238. /* Use default definition if not overridden */
  239. #ifndef IMG_ABORT
  240. #if defined(EXIT_ON_ABORT)
  241. #define IMG_ABORT() exit(1)
  242. #else
  243. #define IMG_ABORT() abort()
  244. #endif
  245. #endif
  246. /* The best way to suppress unused parameter warnings using GCC is to use a
  247. * variable attribute. Place the __maybe_unused between the type and name of an
  248. * unused parameter in a function parameter list e.g. 'int __maybe_unused var'.
  249. * This should only be used in GCC build environments, for example, in files
  250. * that compile only on Linux.
  251. * Other files should use PVR_UNREFERENCED_PARAMETER
  252. */
  253. /* Kernel macros for compiler attributes */
  254. /* Note: param positions start at 1 */
  255. #if defined(__linux__) && defined(__KERNEL__)
  256. #include <linux/compiler.h>
  257. #if !defined(__fallthrough)
  258. #if GCC_VERSION_AT_LEAST(7, 0)
  259. #define __fallthrough __attribute__((__fallthrough__))
  260. #else
  261. #define __fallthrough
  262. #endif
  263. #endif
  264. #elif defined(__GNUC__) || defined(HAS_GNUC_ATTRIBUTES)
  265. #define __must_check __attribute__((warn_unused_result))
  266. #define __maybe_unused __attribute__((unused))
  267. #define __malloc __attribute__((malloc))
  268. /* Bionic's <sys/cdefs.h> might have defined these already */
  269. /* See https://android.googlesource.com/platform/bionic.git/+/master/libc/include/sys/cdefs.h */
  270. #if !defined(__packed)
  271. #define __packed __attribute__((packed))
  272. #endif
  273. #if !defined(__aligned)
  274. #define __aligned(n) __attribute__((aligned(n)))
  275. #endif
  276. #if !defined(__noreturn)
  277. #define __noreturn __attribute__((noreturn))
  278. #endif
  279. /* That one compiler that supports attributes but doesn't support
  280. * the printf attribute... */
  281. #if defined(__GNUC__)
  282. #define __printf(fmt, va) __attribute__((format(printf, (fmt), (va))))
  283. #else
  284. #define __printf(fmt, va)
  285. #endif /* defined(__GNUC__) */
  286. #if defined(__cplusplus) && (__cplusplus >= 201703L)
  287. #define __fallthrough [[fallthrough]]
  288. #elif GCC_VERSION_AT_LEAST(7, 0)
  289. #define __fallthrough __attribute__((__fallthrough__))
  290. #else
  291. #define __fallthrough
  292. #endif
  293. #define __user
  294. #define __force
  295. #define __iomem
  296. #else
  297. /* Silently ignore those attributes */
  298. #define __printf(fmt, va)
  299. #define __packed
  300. #define __aligned(n)
  301. #define __must_check
  302. #define __maybe_unused
  303. #define __malloc
  304. #if defined(_MSC_VER) || defined(CC_ARM)
  305. #define __noreturn __declspec(noreturn)
  306. #else
  307. #define __noreturn
  308. #endif
  309. /* This may already been defined, e.g. by SAL (Source Annotation Language) */
  310. #if !defined(__fallthrough)
  311. #define __fallthrough
  312. #endif
  313. #define __user
  314. #define __force
  315. #define __iomem
  316. #endif
  317. /* Other attributes, following the same style */
  318. #if defined(__GNUC__) || defined(HAS_GNUC_ATTRIBUTES)
  319. #define __const_function __attribute__((const))
  320. #else
  321. #define __const_function
  322. #endif
  323. /* GCC builtins */
  324. #if defined(__linux__) && defined(__KERNEL__)
  325. #include <linux/compiler.h>
  326. #elif defined(__GNUC__) || defined(INTEGRITY_OS)
  327. /* Klocwork does not support __builtin_expect, which makes the actual condition
  328. * expressions hidden during analysis, affecting it negatively. */
  329. #if !defined(__KLOCWORK__) && !defined(INTEGRITY_OS) && !defined(DEBUG)
  330. #define likely(x) __builtin_expect(!!(x), 1)
  331. #define unlikely(x) __builtin_expect(!!(x), 0)
  332. #endif
  333. /* Compiler memory barrier to prevent reordering */
  334. #define barrier() __asm__ __volatile__("": : :"memory")
  335. #else
  336. #define barrier() static_assert(0, "barrier() isn't supported by your compiler");
  337. #endif
  338. /* That one OS that defines one but not the other... */
  339. #ifndef likely
  340. #define likely(x) (x)
  341. #endif
  342. #ifndef unlikely
  343. #define unlikely(x) (x)
  344. #endif
  345. /* These two macros are also provided by the kernel */
  346. #ifndef BIT
  347. #define BIT(b) (1UL << (b))
  348. #endif
  349. #ifndef BIT_ULL
  350. #define BIT_ULL(b) (1ULL << (b))
  351. #endif
  352. #define BIT_SET(f, b) BITMASK_SET((f), BIT(b))
  353. #define BIT_UNSET(f, b) BITMASK_UNSET((f), BIT(b))
  354. #define BIT_TOGGLE(f, b) BITMASK_TOGGLE((f), BIT(b))
  355. #define BIT_ISSET(f, b) BITMASK_HAS((f), BIT(b))
  356. #define BITMASK_SET(f, m) do { ((f) |= (m)); } while (false)
  357. #define BITMASK_UNSET(f, m) do { ((f) &= ~(m)); } while (false)
  358. #define BITMASK_TOGGLE(f, m) do { ((f) ^= (m)); } while (false)
  359. #define BITMASK_HAS(f, m) (((f) & (m)) == (m)) /* the bits from the mask are all set */
  360. #define BITMASK_ANY(f, m) (((f) & (m)) != 0U) /* any bit from the mask is set */
  361. #ifndef MAX
  362. #define MAX(a ,b) (((a) > (b)) ? (a) : (b))
  363. #endif
  364. #ifndef MIN
  365. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  366. #endif
  367. #ifndef CLAMP
  368. #define CLAMP(min, max, n) ((n) < (min) ? (min) : ((n) > (max) ? (max) : (n)))
  369. #endif
  370. #define SWAP(X, Y) (X) ^= (Y); (Y) ^= (X); (X) ^= (Y);
  371. #if defined(__linux__) && defined(__KERNEL__)
  372. #include <linux/kernel.h>
  373. #include <linux/bug.h>
  374. #endif
  375. /* Get a structure's address from the address of a member */
  376. #define IMG_CONTAINER_OF(ptr, type, member) \
  377. (type *) ((uintptr_t) (ptr) - offsetof(type, member))
  378. /* Get a new pointer with an offset (in bytes) from a base address, useful
  379. * when traversing byte buffers and accessing data in buffers through struct
  380. * pointers.
  381. * Note, this macro is not equivalent to or replacing offsetof() */
  382. #define IMG_OFFSET_ADDR(addr, offset_in_bytes) \
  383. (void*)&(((IMG_UINT8*)(void*)(addr))[offset_in_bytes])
  384. /* Get a new pointer with an offset (in dwords) from a base address, useful
  385. * when traversing byte buffers and accessing data in buffers through struct
  386. * pointers.
  387. * Note, this macro is not equivalent to or replacing offsetof() */
  388. #define IMG_OFFSET_ADDR_DW(addr, offset_in_dwords) \
  389. (void*)(((IMG_UINT32*)(void*)(addr)) + (offset_in_dwords))
  390. /* The number of elements in a fixed-sized array */
  391. #ifndef ARRAY_SIZE
  392. #define ARRAY_SIZE(ARR) (sizeof(ARR) / sizeof((ARR)[0]))
  393. #endif
  394. /* To guarantee that __func__ can be used, define it as a macro here if it
  395. isn't already provided by the compiler. */
  396. #if defined(_MSC_VER) || (defined(__cplusplus) && __cplusplus < 201103L)
  397. #define __func__ __FUNCTION__
  398. #endif
  399. #if defined(__cplusplus)
  400. /* C++ Specific:
  401. * Disallow use of copy and assignment operator within a class.
  402. * Should be placed under private. */
  403. #define IMG_DISALLOW_COPY_AND_ASSIGN(C) \
  404. C(const C&); \
  405. void operator=(const C&)
  406. #endif
  407. #if defined(SUPPORT_PVR_VALGRIND) && !defined(__METAG) && !defined(__mips) && !defined(__riscv)
  408. #include "/usr/include/valgrind/memcheck.h"
  409. #define VG_MARK_INITIALIZED(pvData,ui32Size) VALGRIND_MAKE_MEM_DEFINED(pvData,ui32Size)
  410. #define VG_MARK_NOACCESS(pvData,ui32Size) VALGRIND_MAKE_MEM_NOACCESS(pvData,ui32Size)
  411. #define VG_MARK_ACCESS(pvData,ui32Size) VALGRIND_MAKE_MEM_UNDEFINED(pvData,ui32Size)
  412. #define VG_ASSERT_DEFINED(pvData,ui32Size) VALGRIND_CHECK_MEM_IS_DEFINED(pvData,ui32Size)
  413. #else
  414. #if defined(_MSC_VER)
  415. # define PVR_MSC_SUPPRESS_4127 __pragma(warning(suppress:4127))
  416. #else
  417. # define PVR_MSC_SUPPRESS_4127
  418. #endif
  419. #define VG_MARK_INITIALIZED(pvData,ui32Size) PVR_MSC_SUPPRESS_4127 do { } while (false)
  420. #define VG_MARK_NOACCESS(pvData,ui32Size) PVR_MSC_SUPPRESS_4127 do { } while (false)
  421. #define VG_MARK_ACCESS(pvData,ui32Size) PVR_MSC_SUPPRESS_4127 do { } while (false)
  422. #define VG_ASSERT_DEFINED(pvData,ui32Size) PVR_MSC_SUPPRESS_4127 do { } while (false)
  423. #endif
  424. #define IMG_STRINGIFY_IMPL(x) # x
  425. #define IMG_STRINGIFY(x) IMG_STRINGIFY_IMPL(x)
  426. #if defined(INTEGRITY_OS)
  427. /* Definitions not present in INTEGRITY. */
  428. #define PATH_MAX 200
  429. #endif
  430. #if defined(__clang__) || defined(__GNUC__)
  431. /* __SIZEOF_POINTER__ is defined already by these compilers */
  432. #elif defined(INTEGRITY_OS)
  433. #if defined(__Ptr_Is_64)
  434. #define __SIZEOF_POINTER__ 8
  435. #else
  436. #define __SIZEOF_POINTER__ 4
  437. #endif
  438. #elif defined(_WIN32)
  439. #define __SIZEOF_POINTER__ sizeof(char *)
  440. #else
  441. #warning Unknown OS - using default method to determine whether CPU arch is 64-bit.
  442. #define __SIZEOF_POINTER__ sizeof(char *)
  443. #endif
  444. /* RDI8567: gcc/clang/llvm load/store optimisations may cause issues with
  445. * uncached device memory allocations. Some pointers are made 'volatile'
  446. * to prevent those optimisations being applied to writes through those
  447. * pointers.
  448. */
  449. #if (GCC_VERSION_AT_LEAST(7, 0) || defined(__clang__)) && (defined(__arm64__) || defined(__aarch64__))
  450. #define NOLDSTOPT volatile
  451. /* after applying 'volatile' to a pointer, we may need to cast it to 'void *'
  452. * to keep it compatible with its existing uses.
  453. */
  454. #define NOLDSTOPT_VOID (void *)
  455. #define NOLDSTOPT_REQUIRED 1
  456. #else
  457. #define NOLDSTOPT
  458. #define NOLDSTOPT_VOID
  459. #endif
  460. #endif /* IMG_DEFS_H */
  461. /*****************************************************************************
  462. End of file (img_defs.h)
  463. *****************************************************************************/