1 /*
2 * Copyright © 2021 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef PANVK_MACROS_H
7 #define PANVK_MACROS_H
8
9 #include <assert.h>
10
11 #ifdef HAVE_VALGRIND
12 #include <memcheck.h>
13 #include <valgrind.h>
14 #define VG(x) x
15 #else
16 #define VG(x)
17 #endif
18
19 #include "vk_log.h"
20
21 static inline VkResult
panvk_catch_indirect_alloc_failure(VkResult error)22 panvk_catch_indirect_alloc_failure(VkResult error)
23 {
24 /* errno is set to -ENOMEM in the kmod allocator callback when an allocation
25 * fails. When that's the case, the allocation failure takes precedence on
26 * the original error code. We also reset errno before leaving so we don't
27 * end up reporting the same allocation failure twice. */
28 if (errno == -ENOMEM) {
29 errno = 0;
30 return VK_ERROR_OUT_OF_HOST_MEMORY;
31 }
32
33 return error;
34 }
35
36 #define panvk_error(obj, error) \
37 vk_error(obj, panvk_catch_indirect_alloc_failure(error))
38
39 #define panvk_errorf(obj, error, ...) \
40 vk_errorf(obj, panvk_catch_indirect_alloc_failure(error), __VA_ARGS__)
41
42 #define panvk_stub() assert(!"stub")
43
44 #define panvk_arch_name(name, version) panvk_##version##_##name
45
46 #define panvk_arch_dispatch(arch, name, ...) \
47 do { \
48 switch (arch) { \
49 case 6: \
50 panvk_arch_name(name, v6)(__VA_ARGS__); \
51 break; \
52 case 7: \
53 panvk_arch_name(name, v7)(__VA_ARGS__); \
54 break; \
55 case 10: \
56 panvk_arch_name(name, v10)(__VA_ARGS__); \
57 break; \
58 default: \
59 unreachable("Unsupported architecture"); \
60 } \
61 } while (0)
62
63 #define panvk_arch_dispatch_ret(arch, name, ret, ...) \
64 do { \
65 switch (arch) { \
66 case 6: \
67 ret = panvk_arch_name(name, v6)(__VA_ARGS__); \
68 break; \
69 case 7: \
70 ret = panvk_arch_name(name, v7)(__VA_ARGS__); \
71 break; \
72 case 10: \
73 ret = panvk_arch_name(name, v10)(__VA_ARGS__); \
74 break; \
75 default: \
76 unreachable("Unsupported architecture"); \
77 } \
78 } while (0)
79
80 #ifdef PAN_ARCH
81 #if PAN_ARCH == 6
82 #define panvk_per_arch(name) panvk_arch_name(name, v6)
83 #elif PAN_ARCH == 7
84 #define panvk_per_arch(name) panvk_arch_name(name, v7)
85 #elif PAN_ARCH == 9
86 #define panvk_per_arch(name) panvk_arch_name(name, v9)
87 #elif PAN_ARCH == 10
88 #define panvk_per_arch(name) panvk_arch_name(name, v10)
89 #else
90 #error "Unsupported arch"
91 #endif
92 #endif
93
94 #endif
95