1 /*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef CROCUS_BUFMGR_H
25 #define CROCUS_BUFMGR_H
26
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include "util/macros.h"
32 #include "util/u_atomic.h"
33 #include "util/list.h"
34 #include "pipe/p_defines.h"
35
36 struct crocus_batch;
37 struct intel_device_info;
38 struct util_debug_callback;
39
40 #define CROCUS_BINDER_SIZE (64 * 1024)
41 #define CROCUS_MAX_BINDERS 100
42
43 struct crocus_bo {
44 /**
45 * Size in bytes of the buffer object.
46 *
47 * The size may be larger than the size originally requested for the
48 * allocation, such as being aligned to page size.
49 */
50 uint64_t size;
51
52 /** Buffer manager context associated with this buffer object */
53 struct crocus_bufmgr *bufmgr;
54
55 /** The GEM handle for this buffer object. */
56 uint32_t gem_handle;
57
58 /**
59 * Virtual address of the buffer inside the PPGTT (Per-Process Graphics
60 * Translation Table).
61 *
62 * Although each hardware context has its own VMA, we assign BO's to the
63 * same address in all contexts, for simplicity.
64 */
65 uint64_t gtt_offset;
66
67 /**
68 * The validation list index for this buffer, or -1 when not in a batch.
69 * Note that a single buffer may be in multiple batches (contexts), and
70 * this is a global field, which refers to the last batch using the BO.
71 * It should not be considered authoritative, but can be used to avoid a
72 * linear walk of the validation list in the common case by guessing that
73 * exec_bos[bo->index] == bo and confirming whether that's the case.
74 *
75 * XXX: this is not ideal now that we have more than one batch per context,
76 * XXX: as the index will flop back and forth between the render index and
77 * XXX: compute index...
78 */
79 unsigned index;
80
81 /**
82 * Boolean of whether the GPU is definitely not accessing the buffer.
83 *
84 * This is only valid when reusable, since non-reusable
85 * buffers are those that have been shared with other
86 * processes, so we don't know their state.
87 */
88 bool idle;
89
90 int refcount;
91 const char *name;
92
93 uint64_t kflags;
94
95 /**
96 * Kenel-assigned global name for this object
97 *
98 * List contains both flink named and prime fd'd objects
99 */
100 unsigned global_name;
101
102 /**
103 * Current tiling mode
104 */
105 uint32_t tiling_mode;
106 uint32_t swizzle_mode;
107 uint32_t stride;
108
109 time_t free_time;
110
111 /** Mapped address for the buffer, saved across map/unmap cycles */
112 void *map_cpu;
113 /** GTT virtual address for the buffer, saved across map/unmap cycles */
114 void *map_gtt;
115 /** WC CPU address for the buffer, saved across map/unmap cycles */
116 void *map_wc;
117
118 /** BO cache list */
119 struct list_head head;
120
121 /** List of GEM handle exports of this buffer (bo_export) */
122 struct list_head exports;
123
124 /**
125 * Boolean of whether this buffer can be re-used
126 */
127 bool reusable;
128
129 /**
130 * Boolean of whether this buffer has been shared with an external client.
131 */
132 bool external;
133
134 /**
135 * Boolean of whether this buffer is cache coherent
136 */
137 bool cache_coherent;
138
139 /**
140 * Boolean of whether this buffer points into user memory
141 */
142 bool userptr;
143
144 /**
145 * Boolean of if this is used for scanout.
146 */
147 bool scanout;
148
149 /** Pre-computed hash using _mesa_hash_pointer for cache tracking sets */
150 uint32_t hash;
151 };
152
153 #define BO_ALLOC_ZEROED (1 << 0)
154 #define BO_ALLOC_COHERENT (1 << 1)
155 #define BO_ALLOC_SCANOUT (1 << 2)
156
157 /**
158 * Allocate a buffer object.
159 *
160 * Buffer objects are not necessarily initially mapped into CPU virtual
161 * address space or graphics device aperture. They must be mapped
162 * using crocus_bo_map() to be used by the CPU.
163 */
164 struct crocus_bo *crocus_bo_alloc(struct crocus_bufmgr *bufmgr,
165 const char *name, uint64_t size);
166
167 /**
168 * Allocate a tiled buffer object.
169 *
170 * Alignment for tiled objects is set automatically; the 'flags'
171 * argument provides a hint about how the object will be used initially.
172 *
173 * Valid tiling formats are:
174 * I915_TILING_NONE
175 * I915_TILING_X
176 * I915_TILING_Y
177 */
178 struct crocus_bo *crocus_bo_alloc_tiled(struct crocus_bufmgr *bufmgr,
179 const char *name, uint64_t size,
180 uint32_t alignment,
181 uint32_t tiling_mode, uint32_t pitch,
182 unsigned flags);
183
184 struct crocus_bo *crocus_bo_create_userptr(struct crocus_bufmgr *bufmgr,
185 const char *name, void *ptr,
186 size_t size);
187
188 /** Takes a reference on a buffer object */
189 static inline void
crocus_bo_reference(struct crocus_bo * bo)190 crocus_bo_reference(struct crocus_bo *bo)
191 {
192 p_atomic_inc(&bo->refcount);
193 }
194
195 static inline int
atomic_add_unless(int * v,int add,int unless)196 atomic_add_unless(int *v, int add, int unless)
197 {
198 int c, old;
199 c = p_atomic_read(v);
200 while (c != unless && (old = p_atomic_cmpxchg(v, c, c + add)) != c)
201 c = old;
202 return c == unless;
203 }
204
205 void __crocus_bo_unreference(struct crocus_bo *bo);
206
207 /**
208 * Releases a reference on a buffer object, freeing the data if
209 * no references remain.
210 */
crocus_bo_unreference(struct crocus_bo * bo)211 static inline void crocus_bo_unreference(struct crocus_bo *bo)
212 {
213 if (bo == NULL)
214 return;
215
216 assert(p_atomic_read(&bo->refcount) > 0);
217
218 if (atomic_add_unless(&bo->refcount, -1, 1)) {
219 __crocus_bo_unreference(bo);
220 }
221 }
222
223 #define MAP_READ PIPE_MAP_READ
224 #define MAP_WRITE PIPE_MAP_WRITE
225 #define MAP_ASYNC PIPE_MAP_UNSYNCHRONIZED
226 #define MAP_PERSISTENT PIPE_MAP_PERSISTENT
227 #define MAP_COHERENT PIPE_MAP_COHERENT
228 /* internal */
229 #define MAP_INTERNAL_MASK (0xff << 24)
230 #define MAP_RAW (0x01 << 24)
231
232 #define MAP_FLAGS (MAP_READ | MAP_WRITE | MAP_ASYNC | \
233 MAP_PERSISTENT | MAP_COHERENT | MAP_INTERNAL_MASK)
234
235 /**
236 * Maps the buffer into userspace.
237 *
238 * This function will block waiting for any existing execution on the
239 * buffer to complete, first. The resulting mapping is returned.
240 */
241 MUST_CHECK void *crocus_bo_map(struct util_debug_callback *dbg,
242 struct crocus_bo *bo, unsigned flags);
243
244 /**
245 * Reduces the refcount on the userspace mapping of the buffer
246 * object.
247 */
crocus_bo_unmap(struct crocus_bo * bo)248 static inline int crocus_bo_unmap(struct crocus_bo *bo) { return 0; }
249
250 /**
251 * Waits for rendering to an object by the GPU to have completed.
252 *
253 * This is not required for any access to the BO by bo_map,
254 * bo_subdata, etc. It is merely a way for the driver to implement
255 * glFinish.
256 */
257 void crocus_bo_wait_rendering(struct crocus_bo *bo);
258
259 /**
260 * Unref a buffer manager instance.
261 */
262 void crocus_bufmgr_unref(struct crocus_bufmgr *bufmgr);
263
264 /**
265 * Get the current tiling (and resulting swizzling) mode for the bo.
266 *
267 * \param buf Buffer to get tiling mode for
268 * \param tiling_mode returned tiling mode
269 * \param swizzle_mode returned swizzling mode
270 */
271 int crocus_bo_get_tiling(struct crocus_bo *bo, uint32_t *tiling_mode,
272 uint32_t *swizzle_mode);
273
274 /**
275 * Create a visible name for a buffer which can be used by other apps
276 *
277 * \param buf Buffer to create a name for
278 * \param name Returned name
279 */
280 int crocus_bo_flink(struct crocus_bo *bo, uint32_t *name);
281
282 /**
283 * Is this buffer shared with external clients (exported)?
284 */
285 static inline bool
crocus_bo_is_external(const struct crocus_bo * bo)286 crocus_bo_is_external(const struct crocus_bo *bo)
287 {
288 return bo->external;
289 }
290
291 /**
292 * Returns 1 if mapping the buffer for write could cause the process
293 * to block, due to the object being active in the GPU.
294 */
295 int crocus_bo_busy(struct crocus_bo *bo);
296
297 /**
298 * Specify the volatility of the buffer.
299 * \param bo Buffer to create a name for
300 * \param madv The purgeable status
301 *
302 * Use I915_MADV_DONTNEED to mark the buffer as purgeable, and it will be
303 * reclaimed under memory pressure. If you subsequently require the buffer,
304 * then you must pass I915_MADV_WILLNEED to mark the buffer as required.
305 *
306 * Returns 1 if the buffer was retained, or 0 if it was discarded whilst
307 * marked as I915_MADV_DONTNEED.
308 */
309 int crocus_bo_madvise(struct crocus_bo *bo, int madv);
310
311 struct crocus_bufmgr *
312 crocus_bufmgr_get_for_fd(struct intel_device_info *devinfo, int fd,
313 bool bo_reuse);
314 int crocus_bufmgr_get_fd(struct crocus_bufmgr *bufmgr);
315
316 struct crocus_bo *crocus_bo_gem_create_from_name(struct crocus_bufmgr *bufmgr,
317 const char *name,
318 unsigned handle);
319
320 int crocus_bo_wait(struct crocus_bo *bo, int64_t timeout_ns);
321
322 uint32_t crocus_create_hw_context(struct crocus_bufmgr *bufmgr);
323 uint32_t crocus_clone_hw_context(struct crocus_bufmgr *bufmgr, uint32_t ctx_id);
324
325 #define CROCUS_CONTEXT_LOW_PRIORITY ((I915_CONTEXT_MIN_USER_PRIORITY - 1) / 2)
326 #define CROCUS_CONTEXT_MEDIUM_PRIORITY (I915_CONTEXT_DEFAULT_PRIORITY)
327 #define CROCUS_CONTEXT_HIGH_PRIORITY ((I915_CONTEXT_MAX_USER_PRIORITY + 1) / 2)
328
329 int crocus_hw_context_set_priority(struct crocus_bufmgr *bufmgr,
330 uint32_t ctx_id, int priority);
331
332 void crocus_destroy_hw_context(struct crocus_bufmgr *bufmgr, uint32_t ctx_id);
333
334 int crocus_bo_export_dmabuf(struct crocus_bo *bo, int *prime_fd);
335 struct crocus_bo *crocus_bo_import_dmabuf(struct crocus_bufmgr *bufmgr,
336 int prime_fd, uint64_t modifier);
337 struct crocus_bo *crocus_bo_import_dmabuf_no_mods(struct crocus_bufmgr *bufmgr,
338 int prime_fd);
339
340 /**
341 * Exports a bo as a GEM handle into a given DRM file descriptor
342 * \param bo Buffer to export
343 * \param drm_fd File descriptor where the new handle is created
344 * \param out_handle Pointer to store the new handle
345 *
346 * Returns 0 if the buffer was successfully exported, a non zero error code
347 * otherwise.
348 */
349 int crocus_bo_export_gem_handle_for_device(struct crocus_bo *bo, int drm_fd,
350 uint32_t *out_handle);
351
352 uint32_t crocus_bo_export_gem_handle(struct crocus_bo *bo);
353
354 int crocus_reg_read(struct crocus_bufmgr *bufmgr, uint32_t offset,
355 uint64_t *out);
356
357 int drm_ioctl(int fd, unsigned long request, void *arg);
358
359 #endif /* CROCUS_BUFMGR_H */
360