• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 The ChromiumOS Authors
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 
7 #include <stdarg.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 
12 #if defined(__WIN32__)
13 struct iovec {
14     void *iov_base; /* Starting address */
15     size_t iov_len; /* Length in bytes */
16 };
17 #else
18 #include <sys/uio.h>
19 #endif
20 
21 #ifndef RUTABAGA_GFX_FFI_H
22 #define RUTABAGA_GFX_FFI_H
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /**
29  * Versioning
30  */
31 #define RUTABAGA_VERSION_MAJOR 0
32 #define RUTABAGA_VERSION_MINOR 1
33 #define RUTABAGA_VERSION_PATCH 1
34 
35 /**
36  * Blob resource creation parameters.
37  */
38 #define RUTABAGA_BLOB_MEM_GUEST 1
39 #define RUTABAGA_BLOB_MEM_HOST3D 2
40 #define RUTABAGA_BLOB_MEM_HOST3D_GUEST 3
41 
42 #define RUTABAGA_BLOB_FLAG_USE_MAPPABLE 1
43 #define RUTABAGA_BLOB_FLAG_USE_SHAREABLE 2
44 #define RUTABAGA_BLOB_FLAG_USE_CROSS_DEVICE 4
45 
46 /**
47  * Rutabaga capsets.
48  */
49 #define RUTABAGA_CAPSET_VIRGL 1
50 #define RUTABAGA_CAPSET_VIRGL2 2
51 #define RUTABAGA_CAPSET_GFXSTREAM 3
52 #define RUTABAGA_CAPSET_VENUS 4
53 #define RUTABAGA_CAPSET_CROSS_DOMAIN 5
54 #define RUTABAGA_CAPSET_DRM 6
55 
56 /**
57  * Mapped memory caching flags (see virtio_gpu spec)
58  */
59 #define RUTABAGA_MAP_CACHE_CACHED 1
60 #define RUTABAGA_MAP_CACHE_UNCACHED 2
61 #define RUTABAGA_MAP_CACHE_WC 3
62 
63 /**
64  * Rutabaga flags for creating fences.
65  */
66 #define RUTABAGA_FLAG_FENCE (1 << 0)
67 #define RUTABAGA_FLAG_INFO_RING_IDX (1 << 1)
68 
69 /**
70  * Rutabaga channel types
71  */
72 #define RUTABAGA_CHANNEL_TYPE_WAYLAND 1
73 
74 /**
75  * Rutabaga handle types
76  */
77 #define RUTABAGA_MEM_HANDLE_TYPE_OPAQUE_FD 0x1
78 #define RUTABAGA_MEM_HANDLE_TYPE_DMABUF 0x2
79 #define RUTABAGA_MEM_HANDLE_TYPE_OPAQUE_WIN32 0x3
80 #define RUTABAGA_MEM_HANDLE_TYPE_SHM 0x4
81 
82 #define RUTABAGA_FENCE_HANDLE_TYPE_OPAQUE_FD 0x10
83 #define RUTABAGA_FENCE_HANDLE_TYPE_SYNC_FD 0x20
84 #define RUTABAGA_FENCE_HANDLE_TYPE_OPAQUE_WIN32 0x40
85 
86 struct rutabaga;
87 
88 struct rutabaga_fence {
89     uint32_t flags;
90     uint64_t fence_id;
91     uint32_t ctx_id;
92     uint32_t ring_idx;
93 };
94 
95 struct rutabaga_create_blob {
96     uint32_t blob_mem;
97     uint32_t blob_flags;
98     uint64_t blob_id;
99     uint64_t size;
100 };
101 
102 struct rutabaga_create_3d {
103     uint32_t target;
104     uint32_t format;
105     uint32_t bind;
106     uint32_t width;
107     uint32_t height;
108     uint32_t depth;
109     uint32_t array_size;
110     uint32_t last_level;
111     uint32_t nr_samples;
112     uint32_t flags;
113 };
114 
115 struct rutabaga_transfer {
116     uint32_t x;
117     uint32_t y;
118     uint32_t z;
119     uint32_t w;
120     uint32_t h;
121     uint32_t d;
122     uint32_t level;
123     uint32_t stride;
124     uint32_t layer_stride;
125     uint64_t offset;
126 };
127 
128 struct rutabaga_iovecs {
129     struct iovec *iovecs;
130     size_t num_iovecs;
131 };
132 
133 struct rutabaga_handle {
134     int64_t os_handle;
135     uint32_t handle_type;
136 };
137 
138 struct rutabaga_mapping {
139     uint64_t ptr;
140     uint64_t size;
141 };
142 
143 /**
144  * Assumes null-terminated C-string.
145  */
146 struct rutabaga_channel {
147     const char *channel_name;
148     uint32_t channel_type;
149 };
150 
151 struct rutabaga_channels {
152     struct rutabaga_channel *channels;
153     size_t num_channels;
154 };
155 
156 /**
157  * Throwing an exception inside this callback is not allowed.
158  */
159 typedef void (*write_fence_cb)(uint64_t user_data, struct rutabaga_fence fence_data);
160 
161 struct rutabaga_builder {
162     // Required for correct functioning
163     uint64_t user_data;
164     uint64_t capset_mask;
165     write_fence_cb fence_cb;
166 
167     // Optional and platform specific
168     struct rutabaga_channels *channels;
169 };
170 
171 /**
172  * # Safety
173  * - If `(*builder).channels` is not null, the caller must ensure `(*channels).channels` points to
174  *   a valid array of `struct rutabaga_channel` of size `(*channels).num_channels`.
175  * - The `channel_name` field of `struct rutabaga_channel` must be a null-terminated C-string.
176  */
177 int32_t rutabaga_init(const struct rutabaga_builder *builder, struct rutabaga **ptr);
178 
179 /**
180  * # Safety
181  * - `ptr` must have been created by `rutabaga_init`.
182  */
183 int32_t rutabaga_finish(struct rutabaga **ptr);
184 
185 int32_t rutabaga_get_num_capsets(struct rutabaga *ptr, uint32_t *num_capsets);
186 
187 int32_t rutabaga_get_capset_info(struct rutabaga *ptr, uint32_t capset_index, uint32_t *capset_id,
188                                  uint32_t *capset_version, uint32_t *capset_size);
189 
190 /**
191  * # Safety
192  * - `capset` must point an array of bytes of size `capset_size`.
193  */
194 int32_t rutabaga_get_capset(struct rutabaga *ptr, uint32_t capset_id, uint32_t version,
195                             uint8_t *capset, uint32_t capset_size);
196 
197 /**
198  * # Safety
199  * - `context_name` must either be NULL or a valid pointer to an array of at least
200  *   `context_name_len` bytes encoding a UTF-8 string.
201  */
202 int32_t rutabaga_context_create(struct rutabaga *ptr, uint32_t ctx_id, uint32_t context_init,
203                                 const char *context_name, uint32_t context_name_len);
204 
205 int32_t rutabaga_context_destroy(struct rutabaga *ptr, uint32_t ctx_id);
206 
207 int32_t rutabaga_context_attach_resource(struct rutabaga *ptr, uint32_t ctx_id,
208                                          uint32_t resource_id);
209 
210 int32_t rutabaga_context_detach_resource(struct rutabaga *ptr, uint32_t ctx_id,
211                                          uint32_t resource_id);
212 
213 int32_t rutabaga_resource_create_3d(struct rutabaga *ptr, uint32_t resource_id,
214                                     const struct rutabaga_create_3d *create_3d);
215 
216 /**
217  * # Safety
218  * - If `iovecs` is not null, the caller must ensure `(*iovecs).iovecs` points to a valid array of
219  *   iovecs of size `(*iovecs).num_iovecs`.
220  * - Each iovec must point to valid memory starting at `iov_base` with length `iov_len`.
221  * - Each iovec must valid until the resource's backing is explictly detached or the resource is
222  *   is unreferenced.
223  */
224 int32_t rutabaga_resource_attach_backing(struct rutabaga *ptr, uint32_t resource_id,
225                                          const struct rutabaga_iovecs *iovecs);
226 
227 int32_t rutabaga_resource_detach_backing(struct rutabaga *ptr, uint32_t resource_id);
228 
229 /**
230  * # Safety
231  * - If `iovecs` is not null, the caller must ensure `(*iovecs).iovecs` points to a valid array of
232  *   iovecs of size `(*iovecs).num_iovecs`.
233  */
234 int32_t rutabaga_resource_transfer_read(struct rutabaga *ptr, uint32_t ctx_id, uint32_t resource_id,
235                                         const struct rutabaga_transfer *transfer,
236                                         const struct iovec *iovec);
237 
238 int32_t rutabaga_resource_transfer_write(struct rutabaga *ptr, uint32_t ctx_id,
239                                          uint32_t resource_id,
240                                          const struct rutabaga_transfer *transfer);
241 
242 /**
243  * # Safety
244  * - If `iovecs` is not null, the caller must ensure `(*iovecs).iovecs` points to a valid array of
245  *   iovecs of size `(*iovecs).num_iovecs`.
246  * - If `handle` is not null, the caller must ensure it is a valid OS-descriptor.  Ownership is
247  *   transfered to rutabaga.
248  * - Each iovec must valid until the resource's backing is explictly detached or the resource is
249  *   is unreferenced.
250  */
251 int32_t rutabaga_resource_create_blob(struct rutabaga *ptr, uint32_t ctx_id, uint32_t resource_id,
252                                       const struct rutabaga_create_blob *rutabaga_create_blob,
253                                       const struct rutabaga_iovecs *iovecs,
254                                       const struct rutabaga_handle *handle);
255 
256 int32_t rutabaga_resource_unref(struct rutabaga *ptr, uint32_t resource_id);
257 
258 /**
259  * # Safety
260  * Caller owns raw descriptor on success and is responsible for closing it.
261  */
262 int32_t rutabaga_resource_export_blob(struct rutabaga *ptr, uint32_t resource_id,
263                                       struct rutabaga_handle *handle);
264 
265 int32_t rutabaga_resource_map(struct rutabaga *ptr, uint32_t resource_id,
266                               struct rutabaga_mapping *mapping);
267 
268 int32_t rutabaga_resource_unmap(struct rutabaga *ptr, uint32_t resource_id);
269 
270 int32_t rutabaga_resource_map_info(struct rutabaga *ptr, uint32_t resource_id, uint32_t *map_info);
271 
272 /**
273  * # Safety
274  * - `commands` must point to a contiguous memory region of `size` bytes.
275  */
276 int32_t rutabaga_submit_command(struct rutabaga *ptr, uint32_t ctx_id, uint8_t *commands,
277                                 size_t size);
278 
279 int32_t rutabaga_create_fence(struct rutabaga *ptr, const struct rutabaga_fence *fence);
280 
281 #ifdef __cplusplus
282 }
283 #endif
284 
285 #endif
286