iris: remove unused iris_bo->swizzle_mode
[mesa.git] / src / gallium / drivers / iris / iris_bufmgr.h
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 IRIS_BUFMGR_H
25 #define IRIS_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 iris_batch;
37 struct gen_device_info;
38 struct pipe_debug_callback;
39
40 /**
41 * Memory zones. When allocating a buffer, you can request that it is
42 * placed into a specific region of the virtual address space (PPGTT).
43 *
44 * Most buffers can go anywhere (IRIS_MEMZONE_OTHER). Some buffers are
45 * accessed via an offset from a base address. STATE_BASE_ADDRESS has
46 * a maximum 4GB size for each region, so we need to restrict those
47 * buffers to be within 4GB of the base. Each memory zone corresponds
48 * to a particular base address.
49 *
50 * We lay out the virtual address space as follows:
51 *
52 * - [0, 4K): Nothing (empty page for null address)
53 * - [4K, 4G): Shaders (Instruction Base Address)
54 * - [4G, 8G): Surfaces & Binders (Surface State Base Address, Bindless ...)
55 * - [8G, 12G): Dynamic (Dynamic State Base Address)
56 * - [12G, *): Other (everything else in the full 48-bit VMA)
57 *
58 * A special buffer for border color lives at the start of the dynamic state
59 * memory zone. This unfortunately has to be handled specially because the
60 * SAMPLER_STATE "Indirect State Pointer" field is only a 24-bit pointer.
61 *
62 * Each GL context uses a separate GEM context, which technically gives them
63 * each a separate VMA. However, we assign address globally, so buffers will
64 * have the same address in all GEM contexts. This lets us have a single BO
65 * field for the address, which is easy and cheap.
66 */
67 enum iris_memory_zone {
68 IRIS_MEMZONE_SHADER,
69 IRIS_MEMZONE_BINDER,
70 IRIS_MEMZONE_SURFACE,
71 IRIS_MEMZONE_DYNAMIC,
72 IRIS_MEMZONE_OTHER,
73
74 IRIS_MEMZONE_BORDER_COLOR_POOL,
75 };
76
77 /* Intentionally exclude single buffer "zones" */
78 #define IRIS_MEMZONE_COUNT (IRIS_MEMZONE_OTHER + 1)
79
80 #define IRIS_BINDER_SIZE (64 * 1024)
81 #define IRIS_MAX_BINDERS 100
82
83 #define IRIS_MEMZONE_SHADER_START (0ull * (1ull << 32))
84 #define IRIS_MEMZONE_BINDER_START (1ull * (1ull << 32))
85 #define IRIS_MEMZONE_SURFACE_START (IRIS_MEMZONE_BINDER_START + IRIS_MAX_BINDERS * IRIS_BINDER_SIZE)
86 #define IRIS_MEMZONE_DYNAMIC_START (2ull * (1ull << 32))
87 #define IRIS_MEMZONE_OTHER_START (3ull * (1ull << 32))
88
89 #define IRIS_BORDER_COLOR_POOL_ADDRESS IRIS_MEMZONE_DYNAMIC_START
90 #define IRIS_BORDER_COLOR_POOL_SIZE (64 * 1024)
91
92 struct iris_bo {
93 /**
94 * Size in bytes of the buffer object.
95 *
96 * The size may be larger than the size originally requested for the
97 * allocation, such as being aligned to page size.
98 */
99 uint64_t size;
100
101 /** Buffer manager context associated with this buffer object */
102 struct iris_bufmgr *bufmgr;
103
104 /** Pre-computed hash using _mesa_hash_pointer for cache tracking sets */
105 uint32_t hash;
106
107 /** The GEM handle for this buffer object. */
108 uint32_t gem_handle;
109
110 /**
111 * Virtual address of the buffer inside the PPGTT (Per-Process Graphics
112 * Translation Table).
113 *
114 * Although each hardware context has its own VMA, we assign BO's to the
115 * same address in all contexts, for simplicity.
116 */
117 uint64_t gtt_offset;
118
119 /**
120 * If non-zero, then this bo has an aux-map translation to this address.
121 */
122 uint64_t aux_map_address;
123
124 /**
125 * The validation list index for this buffer, or -1 when not in a batch.
126 * Note that a single buffer may be in multiple batches (contexts), and
127 * this is a global field, which refers to the last batch using the BO.
128 * It should not be considered authoritative, but can be used to avoid a
129 * linear walk of the validation list in the common case by guessing that
130 * exec_bos[bo->index] == bo and confirming whether that's the case.
131 *
132 * XXX: this is not ideal now that we have more than one batch per context,
133 * XXX: as the index will flop back and forth between the render index and
134 * XXX: compute index...
135 */
136 unsigned index;
137
138 int refcount;
139 const char *name;
140
141 uint64_t kflags;
142
143 /**
144 * Kenel-assigned global name for this object
145 *
146 * List contains both flink named and prime fd'd objects
147 */
148 unsigned global_name;
149
150 /**
151 * Current tiling mode
152 */
153 uint32_t tiling_mode;
154 uint32_t stride;
155
156 time_t free_time;
157
158 /** Mapped address for the buffer, saved across map/unmap cycles */
159 void *map_cpu;
160 /** GTT virtual address for the buffer, saved across map/unmap cycles */
161 void *map_gtt;
162 /** WC CPU address for the buffer, saved across map/unmap cycles */
163 void *map_wc;
164
165 /** BO cache list */
166 struct list_head head;
167
168 /**
169 * Boolean of whether the GPU is definitely not accessing the buffer.
170 *
171 * This is only valid when reusable, since non-reusable
172 * buffers are those that have been shared with other
173 * processes, so we don't know their state.
174 */
175 bool idle;
176
177 /**
178 * Boolean of whether this buffer can be re-used
179 */
180 bool reusable;
181
182 /**
183 * Boolean of whether this buffer has been shared with an external client.
184 */
185 bool external;
186
187 /**
188 * Boolean of whether this buffer is cache coherent
189 */
190 bool cache_coherent;
191
192 /**
193 * Boolean of whether this buffer points into user memory
194 */
195 bool userptr;
196 };
197
198 #define BO_ALLOC_ZEROED (1<<0)
199 #define BO_ALLOC_COHERENT (1<<1)
200
201 /**
202 * Allocate a buffer object.
203 *
204 * Buffer objects are not necessarily initially mapped into CPU virtual
205 * address space or graphics device aperture. They must be mapped
206 * using iris_bo_map() to be used by the CPU.
207 */
208 struct iris_bo *iris_bo_alloc(struct iris_bufmgr *bufmgr,
209 const char *name,
210 uint64_t size,
211 enum iris_memory_zone memzone);
212
213 /**
214 * Allocate a tiled buffer object.
215 *
216 * Alignment for tiled objects is set automatically; the 'flags'
217 * argument provides a hint about how the object will be used initially.
218 *
219 * Valid tiling formats are:
220 * I915_TILING_NONE
221 * I915_TILING_X
222 * I915_TILING_Y
223 */
224 struct iris_bo *iris_bo_alloc_tiled(struct iris_bufmgr *bufmgr,
225 const char *name,
226 uint64_t size,
227 uint32_t alignment,
228 enum iris_memory_zone memzone,
229 uint32_t tiling_mode,
230 uint32_t pitch,
231 unsigned flags);
232
233 struct iris_bo *
234 iris_bo_create_userptr(struct iris_bufmgr *bufmgr, const char *name,
235 void *ptr, size_t size,
236 enum iris_memory_zone memzone);
237
238 /** Takes a reference on a buffer object */
239 static inline void
240 iris_bo_reference(struct iris_bo *bo)
241 {
242 p_atomic_inc(&bo->refcount);
243 }
244
245 /**
246 * Releases a reference on a buffer object, freeing the data if
247 * no references remain.
248 */
249 void iris_bo_unreference(struct iris_bo *bo);
250
251 #define MAP_READ PIPE_TRANSFER_READ
252 #define MAP_WRITE PIPE_TRANSFER_WRITE
253 #define MAP_ASYNC PIPE_TRANSFER_UNSYNCHRONIZED
254 #define MAP_PERSISTENT PIPE_TRANSFER_PERSISTENT
255 #define MAP_COHERENT PIPE_TRANSFER_COHERENT
256 /* internal */
257 #define MAP_INTERNAL_MASK (0xffu << 24)
258 #define MAP_RAW (0x01 << 24)
259
260 #define MAP_FLAGS (MAP_READ | MAP_WRITE | MAP_ASYNC | \
261 MAP_PERSISTENT | MAP_COHERENT | MAP_INTERNAL_MASK)
262
263 /**
264 * Maps the buffer into userspace.
265 *
266 * This function will block waiting for any existing execution on the
267 * buffer to complete, first. The resulting mapping is returned.
268 */
269 MUST_CHECK void *iris_bo_map(struct pipe_debug_callback *dbg,
270 struct iris_bo *bo, unsigned flags);
271
272 /**
273 * Reduces the refcount on the userspace mapping of the buffer
274 * object.
275 */
276 static inline int iris_bo_unmap(struct iris_bo *bo) { return 0; }
277
278 /**
279 * Waits for rendering to an object by the GPU to have completed.
280 *
281 * This is not required for any access to the BO by bo_map,
282 * bo_subdata, etc. It is merely a way for the driver to implement
283 * glFinish.
284 */
285 void iris_bo_wait_rendering(struct iris_bo *bo);
286
287
288 /**
289 * Unref a buffer manager instance.
290 */
291 void iris_bufmgr_unref(struct iris_bufmgr *bufmgr);
292
293 /**
294 * Create a visible name for a buffer which can be used by other apps
295 *
296 * \param buf Buffer to create a name for
297 * \param name Returned name
298 */
299 int iris_bo_flink(struct iris_bo *bo, uint32_t *name);
300
301 /**
302 * Make a BO externally accessible.
303 *
304 * \param bo Buffer to make external
305 */
306 void iris_bo_make_external(struct iris_bo *bo);
307
308 /**
309 * Returns 1 if mapping the buffer for write could cause the process
310 * to block, due to the object being active in the GPU.
311 */
312 int iris_bo_busy(struct iris_bo *bo);
313
314 /**
315 * Specify the volatility of the buffer.
316 * \param bo Buffer to create a name for
317 * \param madv The purgeable status
318 *
319 * Use I915_MADV_DONTNEED to mark the buffer as purgeable, and it will be
320 * reclaimed under memory pressure. If you subsequently require the buffer,
321 * then you must pass I915_MADV_WILLNEED to mark the buffer as required.
322 *
323 * Returns 1 if the buffer was retained, or 0 if it was discarded whilst
324 * marked as I915_MADV_DONTNEED.
325 */
326 int iris_bo_madvise(struct iris_bo *bo, int madv);
327
328 /* drm_bacon_bufmgr_gem.c */
329 struct iris_bufmgr *iris_bufmgr_get_for_fd(struct gen_device_info *devinfo, int fd,
330 bool bo_reuse);
331 int iris_bufmgr_get_fd(struct iris_bufmgr *bufmgr);
332
333 struct iris_bo *iris_bo_gem_create_from_name(struct iris_bufmgr *bufmgr,
334 const char *name,
335 unsigned handle);
336
337 void* iris_bufmgr_get_aux_map_context(struct iris_bufmgr *bufmgr);
338
339 int iris_bo_wait(struct iris_bo *bo, int64_t timeout_ns);
340
341 uint32_t iris_create_hw_context(struct iris_bufmgr *bufmgr);
342 uint32_t iris_clone_hw_context(struct iris_bufmgr *bufmgr, uint32_t ctx_id);
343
344 #define IRIS_CONTEXT_LOW_PRIORITY ((I915_CONTEXT_MIN_USER_PRIORITY-1)/2)
345 #define IRIS_CONTEXT_MEDIUM_PRIORITY (I915_CONTEXT_DEFAULT_PRIORITY)
346 #define IRIS_CONTEXT_HIGH_PRIORITY ((I915_CONTEXT_MAX_USER_PRIORITY+1)/2)
347
348 int iris_hw_context_set_priority(struct iris_bufmgr *bufmgr,
349 uint32_t ctx_id, int priority);
350
351 void iris_destroy_hw_context(struct iris_bufmgr *bufmgr, uint32_t ctx_id);
352
353 int iris_bo_export_dmabuf(struct iris_bo *bo, int *prime_fd);
354 struct iris_bo *iris_bo_import_dmabuf(struct iris_bufmgr *bufmgr, int prime_fd,
355 uint32_t tiling, uint32_t stride);
356
357 uint32_t iris_bo_export_gem_handle(struct iris_bo *bo);
358
359 int iris_reg_read(struct iris_bufmgr *bufmgr, uint32_t offset, uint64_t *out);
360
361 int drm_ioctl(int fd, unsigned long request, void *arg);
362
363 /**
364 * Returns the BO's address relative to the appropriate base address.
365 *
366 * All of our base addresses are programmed to the start of a 4GB region,
367 * so simply returning the bottom 32 bits of the BO address will give us
368 * the offset from whatever base address corresponds to that memory region.
369 */
370 static inline uint32_t
371 iris_bo_offset_from_base_address(struct iris_bo *bo)
372 {
373 /* This only works for buffers in the memory zones corresponding to a
374 * base address - the top, unbounded memory zone doesn't have a base.
375 */
376 assert(bo->gtt_offset < IRIS_MEMZONE_OTHER_START);
377 return bo->gtt_offset;
378 }
379
380 enum iris_memory_zone iris_memzone_for_address(uint64_t address);
381
382 #endif /* IRIS_BUFMGR_H */