1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef LIB_MAGMA_CLIENT_INCLUDE_LIB_MAGMA_MAGMA_COMMON_DEFS_H_
6 #define LIB_MAGMA_CLIENT_INCLUDE_LIB_MAGMA_MAGMA_COMMON_DEFS_H_
7 
8 #include <stdint.h>
9 
10 #if defined(__cplusplus)
11 extern "C" {
12 #endif
13 
14 // This header is C, so clang-tidy shouldn't recommend using C++ features.
15 // NOLINTBEGIN(modernize-use-using)
16 
17 // LINT.IfChange(version)
18 // This version should be incremented whenever the Magma API changes.
19 #define MAGMA_API_VERSION 1
20 // LINT.ThenChange()
21 
22 // LINT.IfChange
23 #if defined(__Fuchsia__)
24 // The entrypoints should be exported from libmagma_client.a. ICDs should use a
25 // version script to re-exporting magma entrypoints.
26 #define MAGMA_EXPORT __attribute__((visibility("default")))
27 #else
28 #define MAGMA_EXPORT
29 #endif
30 
31 typedef uint64_t magma_query_t;
32 
33 // This is a list of vendor-neutral queries that can be passed to magma_query.
34 // Returns the hardware vendor ID (simple result) - should be the PCI ID of the GPU vendor
35 // if possible, or the Khronos vendor ID otherwise.
36 #define MAGMA_QUERY_VENDOR_ID ((magma_query_t)0)
37 // Returns the hardware device ID (simple result)
38 #define MAGMA_QUERY_DEVICE_ID ((magma_query_t)1)
39 // Returns the version of the vendor interfaces supported by the system driver (simple result).
40 #define MAGMA_QUERY_VENDOR_VERSION ((magma_query_t)2)
41 // Returns true if MAGMA_QUERY_TOTAL_TIME is supported (simple result)
42 #define MAGMA_QUERY_IS_TOTAL_TIME_SUPPORTED ((magma_query_t)3)
43 // 4 was MAGMA_QUERY_MINIMUM_MAPPABLE_ADDRESS
44 // Upper 32bits: max inflight messages, lower 32bits: max inflight memory (MB) (simple result)
45 #define MAGMA_QUERY_MAXIMUM_INFLIGHT_PARAMS ((magma_query_t)5)
46 
47 // All vendor-specific queries IDs that can be passed to magma_query must be >=
48 // MAGMA_QUERY_VENDOR_PARAM_0.
49 #define MAGMA_QUERY_VENDOR_PARAM_0 ((magma_query_t)10000)
50 
51 // This is a list of vendor-neutral queries that can be passed to magma_query.
52 // Returns a struct magma_total_time_query_result (buffer result)
53 #define MAGMA_QUERY_TOTAL_TIME ((magma_query_t)500)
54 
55 // reserved ID to represent an invalid object
56 #define MAGMA_INVALID_OBJECT_ID ((uint64_t)0ull)
57 
58 // All vendor-specific command buffer flags must be >=
59 // MAGMA_COMMAND_BUFFER_VENDOR_FLAGS_0.
60 #define MAGMA_COMMAND_BUFFER_VENDOR_FLAGS_0 ((uint64_t)(1ull << 16))
61 
62 typedef int32_t magma_status_t;
63 
64 // possible values for magma_status_t
65 #define MAGMA_STATUS_OK ((magma_status_t)(0))
66 #define MAGMA_STATUS_INTERNAL_ERROR ((magma_status_t)(-1))
67 #define MAGMA_STATUS_INVALID_ARGS ((magma_status_t)(-2))
68 #define MAGMA_STATUS_ACCESS_DENIED ((magma_status_t)(-3))
69 #define MAGMA_STATUS_MEMORY_ERROR ((magma_status_t)(-4))
70 #define MAGMA_STATUS_CONTEXT_KILLED ((magma_status_t)(-5))
71 #define MAGMA_STATUS_CONNECTION_LOST ((magma_status_t)(-6))
72 #define MAGMA_STATUS_TIMED_OUT ((magma_status_t)(-7))
73 #define MAGMA_STATUS_UNIMPLEMENTED ((magma_status_t)(-8))
74 // This error means that an object was not in the right state for an operation on it.
75 #define MAGMA_STATUS_BAD_STATE ((magma_status_t)(-9))
76 #define MAGMA_STATUS_ALIAS_FOR_LAST MAGMA_STATUS_BAD_STATE
77 
78 typedef uint32_t magma_cache_operation_t;
79 // possible values for magma_cache_operation_t
80 #define MAGMA_CACHE_OPERATION_CLEAN ((magma_cache_operation_t)0)
81 #define MAGMA_CACHE_OPERATION_CLEAN_INVALIDATE ((magma_cache_operation_t)1)
82 
83 typedef uint32_t magma_cache_policy_t;
84 // possible values for magma_cache_policy_t
85 #define MAGMA_CACHE_POLICY_CACHED ((magma_cache_policy_t)0)
86 #define MAGMA_CACHE_POLICY_WRITE_COMBINING ((magma_cache_policy_t)1)
87 #define MAGMA_CACHE_POLICY_UNCACHED ((magma_cache_policy_t)2)
88 
89 #define MAGMA_DUMP_TYPE_NORMAL ((uint32_t)(1 << 0))
90 
91 #define MAGMA_PERF_COUNTER_RESULT_DISCONTINUITY ((uint32_t)(1 << 0))
92 
93 typedef uint32_t magma_format_t;
94 
95 // Values must match fuchsia.sysmem.PixelFormatType
96 #define MAGMA_FORMAT_INVALID ((magma_format_t)0)
97 #define MAGMA_FORMAT_R8G8B8A8 ((magma_format_t)1)
98 #define MAGMA_FORMAT_BGRA32 ((magma_format_t)101)
99 #define MAGMA_FORMAT_I420 ((magma_format_t)102)
100 #define MAGMA_FORMAT_M420 ((magma_format_t)103)
101 #define MAGMA_FORMAT_NV12 ((magma_format_t)104)
102 #define MAGMA_FORMAT_YUY2 ((magma_format_t)105)
103 #define MAGMA_FORMAT_MJPEG ((magma_format_t)106)
104 #define MAGMA_FORMAT_YV12 ((magma_format_t)107)
105 #define MAGMA_FORMAT_BGR24 ((magma_format_t)108)
106 #define MAGMA_FORMAT_RGB565 ((magma_format_t)109)
107 #define MAGMA_FORMAT_RGB332 ((magma_format_t)110)
108 #define MAGMA_FORMAT_RGB2220 ((magma_format_t)111)
109 #define MAGMA_FORMAT_L8 ((magma_format_t)112)
110 #define MAGMA_FORMAT_R8 ((magma_format_t)113)
111 #define MAGMA_FORMAT_R8G8 ((magma_format_t)114)
112 
113 typedef uint64_t magma_format_modifier_t;
114 // These must match the fuchsia.sysmem format modifier values.
115 #define MAGMA_FORMAT_MODIFIER_LINEAR ((magma_format_modifier_t)0x0000000000000000)
116 
117 #define MAGMA_FORMAT_MODIFIER_INTEL_X_TILED ((magma_format_modifier_t)0x0100000000000001)
118 #define MAGMA_FORMAT_MODIFIER_INTEL_Y_TILED ((magma_format_modifier_t)0x0100000000000002)
119 #define MAGMA_FORMAT_MODIFIER_INTEL_YF_TILED ((magma_format_modifier_t)0x0100000000000003)
120 
121 #define MAGMA_FORMAT_MODIFIER_INTEL_Y_TILED_CCS ((magma_format_modifier_t)0x0100000001000002)
122 #define MAGMA_FORMAT_MODIFIER_INTEL_YF_TILED_CCS ((magma_format_modifier_t)0x0100000001000003)
123 
124 #define MAGMA_FORMAT_MODIFIER_ARM_YUV_BIT ((magma_format_modifier_t)0x10)
125 #define MAGMA_FORMAT_MODIFIER_ARM_SPLIT_BLOCK_BIT ((magma_format_modifier_t)0x20)
126 #define MAGMA_FORMAT_MODIFIER_ARM_SPARSE_BIT ((magma_format_modifier_t)0x40)
127 #define MAGMA_FORMAT_MODIFIER_ARM_BCH_BIT ((magma_format_modifier_t)0x800)
128 #define MAGMA_FORMAT_MODIFIER_ARM_TE_BIT ((magma_format_modifier_t)0x1000)
129 #define MAGMA_FORMAT_MODIFIER_ARM_TILED_HEADER_BIT ((magma_format_modifier_t)0x2000)
130 
131 #define MAGMA_FORMAT_MODIFIER_ARM ((magma_format_modifier_t)0x0800000000000000)
132 #define MAGMA_FORMAT_MODIFIER_ARM_AFBC_16X16 ((magma_format_modifier_t)0x0800000000000001)
133 #define MAGMA_FORMAT_MODIFIER_ARM_AFBC_32X8 ((magma_format_modifier_t)0x0800000000000002)
134 #define MAGMA_FORMAT_MODIFIER_ARM_LINEAR_TE ((magma_format_modifier_t)0x0800000000001000)
135 #define MAGMA_FORMAT_MODIFIER_ARM_AFBC_16X16_TE ((magma_format_modifier_t)0x0800000000001001)
136 #define MAGMA_FORMAT_MODIFIER_ARM_AFBC_32X8_TE ((magma_format_modifier_t)0x0800000000001002)
137 
138 #define MAGMA_FORMAT_MODIFIER_ARM_AFBC_16X16_YUV_TILED_HEADER \
139   ((magma_format_modifier_t)0x0800000000002011)
140 
141 #define MAGMA_FORMAT_MODIFIER_ARM_AFBC_16X16_SPLIT_BLOCK_SPARSE_YUV \
142   ((magma_format_modifier_t)0x0800000000000071)
143 #define MAGMA_FORMAT_MODIFIER_ARM_AFBC_16X16_SPLIT_BLOCK_SPARSE_YUV_TE \
144   ((magma_format_modifier_t)0x0800000000001071)
145 
146 #define MAGMA_FORMAT_MODIFIER_ARM_AFBC_16X16_SPLIT_BLOCK_SPARSE_YUV_TILED_HEADER \
147   ((magma_format_modifier_t)0x0800000000002071)
148 #define MAGMA_FORMAT_MODIFIER_ARM_AFBC_16X16_SPLIT_BLOCK_SPARSE_YUV_TE_TILED_HEADER \
149   ((magma_format_modifier_t)0x0800000000003071)
150 
151 typedef uint32_t magma_colorspace_t;
152 
153 // Must match fuchsia.sysmem.ColorSpaceType values.
154 #define MAGMA_COLORSPACE_INVALID ((magma_colorspace_t)0)
155 #define MAGMA_COLORSPACE_SRGB ((magma_colorspace_t)1)
156 #define MAGMA_COLORSPACE_REC601_NTSC ((magma_colorspace_t)2)
157 #define MAGMA_COLORSPACE_REC601_NTSC_FULL_RANGE ((magma_colorspace_t)3)
158 #define MAGMA_COLORSPACE_REC601_PAL ((magma_colorspace_t)4)
159 #define MAGMA_COLORSPACE_REC601_PAL_FULL_RANGE ((magma_colorspace_t)5)
160 #define MAGMA_COLORSPACE_REC709 ((magma_colorspace_t)6)
161 #define MAGMA_COLORSPACE_REC2020 ((magma_colorspace_t)7)
162 #define MAGMA_COLORSPACE_REC2100 ((magma_colorspace_t)8)
163 
164 typedef uint32_t magma_coherency_domain_t;
165 
166 #define MAGMA_COHERENCY_DOMAIN_CPU ((magma_coherency_domain_t)0)
167 #define MAGMA_COHERENCY_DOMAIN_RAM ((magma_coherency_domain_t)1)
168 #define MAGMA_COHERENCY_DOMAIN_INACCESSIBLE ((magma_coherency_domain_t)2)
169 
170 #define MAGMA_POLL_TYPE_SEMAPHORE ((uint32_t)1)
171 #define MAGMA_POLL_TYPE_HANDLE ((uint32_t)2)
172 
173 #define MAGMA_POLL_CONDITION_READABLE ((uint32_t)1)
174 #define MAGMA_POLL_CONDITION_SIGNALED ((uint32_t)3)
175 
176 typedef uint32_t magma_buffer_range_op_t;
177 
178 // Eagerly populate hardware page tables with the pages mapping in this range, committing pages as
179 // needed. This is not needed for MAGMA_MAP_FLAG_GROWABLE allocations, since the page tables
180 // will be populated on demand.
181 #define MAGMA_BUFFER_RANGE_OP_POPULATE_TABLES ((magma_buffer_range_op_t)1)
182 // Commit memory on the client thread. Hardware page tables may not be populated. This should be
183 // used
184 // before POPULATE_TABLES to ensure the expensive work of committing pages happens with the
185 // correct priority and without blocking the processing in the MSD of commands from other threads
186 // from the same connection.
187 #define MAGMA_BUFFER_RANGE_OP_COMMIT ((magma_buffer_range_op_t)2)
188 // Depopulate hardware page table mappings for this range. This prevents the hardware from
189 // accessing
190 // pages in that range, but the pages retain their contents.
191 #define MAGMA_BUFFER_RANGE_OP_DEPOPULATE_TABLES ((magma_buffer_range_op_t)3)
192 // Decommit memory wholy on the client thread. This may fail if the MSD currently has the page
193 // tables populated.
194 #define MAGMA_BUFFER_RANGE_OP_DECOMMIT ((magma_buffer_range_op_t)4)
195 
196 // Set `is_secure` flag in the `BufferMemorySettings` so protected memory is allocated.
197 #define MAGMA_SYSMEM_FLAG_PROTECTED ((uint32_t)(1 << 0))
198 // This flag is only used to modify the name of the buffer to signal that the client requested it
199 // using vkAllocateMemory or similar.
200 #define MAGMA_SYSMEM_FLAG_FOR_CLIENT ((uint32_t)(1 << 2))
201 
202 #define MAGMA_MAX_IMAGE_PLANES ((uint32_t)4)
203 
204 #define MAGMA_MAX_DRM_FORMAT_MODIFIERS ((uint32_t)16)
205 
206 // Normal bool doesn't have to be a particular size.
207 typedef uint8_t magma_bool_t;
208 
209 typedef uint64_t magma_device_t;
210 
211 typedef uint64_t magma_buffer_t;
212 
213 typedef uint64_t magma_semaphore_t;
214 
215 typedef uint64_t magma_perf_count_pool_t;
216 
217 typedef uint64_t magma_connection_t;
218 
219 // An opaque handle that corresponds to a fuchsia.sysmem.Allocator connection to sysmem.
220 typedef uint64_t magma_sysmem_connection_t;
221 
222 // Corresponds to a zx_handle_t on Fuchsia.
223 typedef uint32_t magma_handle_t;
224 
225 // An ID for a buffer that can be used to refer to it when submitting command buffers. Only valid
226 // within a single connection.
227 typedef uint64_t magma_buffer_id_t;
228 
229 typedef uint64_t magma_semaphore_id_t;
230 
231 typedef struct magma_poll_item {
232   union {
233     magma_semaphore_t semaphore;
234     magma_handle_t handle;
235   };
236   uint32_t type;
237   uint32_t condition;
238   uint32_t result;
239   uint32_t unused;
240 } magma_poll_item_t;
241 
242 // A buffer referenced by a command buffer descriptor
243 typedef struct magma_exec_resource {
244   magma_buffer_id_t buffer_id;
245   uint64_t offset;
246   uint64_t length;
247 } magma_exec_resource_t;
248 
249 // A resource to be executed by a command buffer descriptor
250 typedef struct magma_exec_command_buffer {
251   uint32_t resource_index;
252   uint32_t unused;
253   uint64_t start_offset;
254 } __attribute__((__aligned__(8))) magma_exec_command_buffer_t;
255 
256 typedef struct magma_command_descriptor {
257   // The count of `resources` that may be referenced by the hardware. These must have been
258   // mapped to the hardware previously.
259   uint32_t resource_count;
260   // The count of `command_buffers` to be executed as a unit.
261   uint32_t command_buffer_count;
262   // The count of `semaphore_ids` to be waited upon before beginning execution; these
263   // will be reset after all have been signaled.
264   uint32_t wait_semaphore_count;
265   // The count of `semaphore_ids` to be signaled after execution is complete.
266   uint32_t signal_semaphore_count;
267 
268   struct magma_exec_resource* resources;
269   struct magma_exec_command_buffer* command_buffers;
270   // List of semaphore IDs: first the wait semaphore IDs, followed by signal semaphore IDs.
271   uint64_t* semaphore_ids;
272 
273   uint64_t flags;
274 
275 } __attribute__((__aligned__(8))) magma_command_descriptor_t;
276 
277 typedef struct magma_inline_command_buffer {
278   void* data;
279   uint64_t size;
280   uint64_t* semaphore_ids;
281   uint32_t semaphore_count;
282 } magma_inline_command_buffer_t;
283 
284 typedef struct magma_total_time_query_result {
285   uint64_t gpu_time_ns;        // GPU time in ns since driver start.
286   uint64_t monotonic_time_ns;  // monotonic clock time measured at same time CPU time was.
287 } magma_total_time_query_result_t;
288 
289 typedef struct magma_buffer_offset {
290   uint64_t buffer_id;
291   uint64_t offset;
292   uint64_t length;
293 } magma_buffer_offset_t;
294 
295 // The top 16 bits are reserved for vendor-specific flags.
296 #define MAGMA_MAP_FLAG_VENDOR_SHIFT ((uint64_t)16)
297 
298 #define MAGMA_MAP_FLAG_READ ((uint64_t)(1 << 0))
299 #define MAGMA_MAP_FLAG_WRITE ((uint64_t)(1 << 1))
300 #define MAGMA_MAP_FLAG_EXECUTE ((uint64_t)(1 << 2))
301 #define MAGMA_MAP_FLAG_GROWABLE ((uint64_t)(1 << 3))
302 #define MAGMA_MAP_FLAG_VENDOR_0 ((uint64_t)(1 << MAGMA_MAP_FLAG_VENDOR_SHIFT))
303 
304 typedef struct magma_buffer_info {
305   uint64_t committed_byte_count;
306   uint64_t size;
307 } magma_buffer_info_t;
308 
309 #define MAGMA_IMAGE_CREATE_FLAGS_PRESENTABLE ((uint32_t)1)
310 #define MAGMA_IMAGE_CREATE_FLAGS_VULKAN_USAGE ((uint32_t)(1 << 1))
311 
312 typedef struct magma_image_create_info {
313   // A format specified by DRM (Linux Direct Rendering Manager)
314   uint64_t drm_format;
315   // The last modifier must be DRM_FORMAT_MOD_INVALID
316   uint64_t drm_format_modifiers[MAGMA_MAX_DRM_FORMAT_MODIFIERS];
317   uint32_t width;
318   uint32_t height;
319   // If MAGMA_IMAGE_CREATE_FLAGS_VULKAN_USAGE is provided, Vulkan usage flags
320   // should be provided in the upper 32 bits.
321   uint64_t flags;
322 } magma_image_create_info_t;
323 
324 typedef struct magma_image_info {
325   uint64_t plane_strides[MAGMA_MAX_IMAGE_PLANES];
326   uint32_t plane_offsets[MAGMA_MAX_IMAGE_PLANES];
327   uint64_t drm_format_modifier;
328   uint32_t coherency_domain;
329   uint32_t unused;
330 } magma_image_info_t;
331 
332 // LINT.ThenChange(version)
333 // NOLINTEND(modernize-use-using)
334 
335 #if defined(__cplusplus)
336 }
337 #endif
338 
339 #endif  // LIB_MAGMA_CLIENT_INCLUDE_LIB_MAGMA_MAGMA_COMMON_DEFS_H_
340