Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/iris/iris_bufmgr.h
4565 views
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 "c11/threads.h"
32
#include "util/macros.h"
33
#include "util/u_atomic.h"
34
#include "util/list.h"
35
#include "pipe/p_defines.h"
36
37
struct iris_batch;
38
struct intel_device_info;
39
struct pipe_debug_callback;
40
struct isl_surf;
41
42
/**
43
* Memory zones. When allocating a buffer, you can request that it is
44
* placed into a specific region of the virtual address space (PPGTT).
45
*
46
* Most buffers can go anywhere (IRIS_MEMZONE_OTHER). Some buffers are
47
* accessed via an offset from a base address. STATE_BASE_ADDRESS has
48
* a maximum 4GB size for each region, so we need to restrict those
49
* buffers to be within 4GB of the base. Each memory zone corresponds
50
* to a particular base address.
51
*
52
* We lay out the virtual address space as follows:
53
*
54
* - [0, 4K): Nothing (empty page for null address)
55
* - [4K, 4G): Shaders (Instruction Base Address)
56
* - [4G, 8G): Surfaces & Binders (Surface State Base Address, Bindless ...)
57
* - [8G, 12G): Dynamic (Dynamic State Base Address)
58
* - [12G, *): Other (everything else in the full 48-bit VMA)
59
*
60
* A special buffer for border color lives at the start of the dynamic state
61
* memory zone. This unfortunately has to be handled specially because the
62
* SAMPLER_STATE "Indirect State Pointer" field is only a 24-bit pointer.
63
*
64
* Each GL context uses a separate GEM context, which technically gives them
65
* each a separate VMA. However, we assign address globally, so buffers will
66
* have the same address in all GEM contexts. This lets us have a single BO
67
* field for the address, which is easy and cheap.
68
*/
69
enum iris_memory_zone {
70
IRIS_MEMZONE_SHADER,
71
IRIS_MEMZONE_BINDER,
72
IRIS_MEMZONE_BINDLESS,
73
IRIS_MEMZONE_SURFACE,
74
IRIS_MEMZONE_DYNAMIC,
75
IRIS_MEMZONE_OTHER,
76
77
IRIS_MEMZONE_BORDER_COLOR_POOL,
78
};
79
80
/* Intentionally exclude single buffer "zones" */
81
#define IRIS_MEMZONE_COUNT (IRIS_MEMZONE_OTHER + 1)
82
83
#define IRIS_BINDER_SIZE (64 * 1024)
84
#define IRIS_MAX_BINDERS 100
85
#define IRIS_BINDLESS_SIZE (8 * 1024 * 1024)
86
87
#define IRIS_MEMZONE_SHADER_START (0ull * (1ull << 32))
88
#define IRIS_MEMZONE_BINDER_START (1ull * (1ull << 32))
89
#define IRIS_MEMZONE_BINDLESS_START (IRIS_MEMZONE_BINDER_START + IRIS_MAX_BINDERS * IRIS_BINDER_SIZE)
90
#define IRIS_MEMZONE_SURFACE_START (IRIS_MEMZONE_BINDLESS_START + IRIS_BINDLESS_SIZE)
91
#define IRIS_MEMZONE_DYNAMIC_START (2ull * (1ull << 32))
92
#define IRIS_MEMZONE_OTHER_START (3ull * (1ull << 32))
93
94
#define IRIS_BORDER_COLOR_POOL_ADDRESS IRIS_MEMZONE_DYNAMIC_START
95
#define IRIS_BORDER_COLOR_POOL_SIZE (64 * 1024)
96
97
/**
98
* Classification of the various incoherent caches of the GPU into a number of
99
* caching domains.
100
*/
101
enum iris_domain {
102
/** Render color cache. */
103
IRIS_DOMAIN_RENDER_WRITE = 0,
104
/** (Hi)Z/stencil cache. */
105
IRIS_DOMAIN_DEPTH_WRITE,
106
/** Any other read-write cache. */
107
IRIS_DOMAIN_OTHER_WRITE,
108
/** Any other read-only cache. */
109
IRIS_DOMAIN_OTHER_READ,
110
/** Number of caching domains. */
111
NUM_IRIS_DOMAINS,
112
/** Not a real cache, use to opt out of the cache tracking mechanism. */
113
IRIS_DOMAIN_NONE = NUM_IRIS_DOMAINS
114
};
115
116
/**
117
* Whether a caching domain is guaranteed not to write any data to memory.
118
*/
119
static inline bool
120
iris_domain_is_read_only(enum iris_domain access)
121
{
122
return access == IRIS_DOMAIN_OTHER_READ;
123
}
124
125
enum iris_mmap_mode {
126
IRIS_MMAP_UC, /**< Fully uncached memory map */
127
IRIS_MMAP_WC, /**< Write-combining map with no caching of reads */
128
IRIS_MMAP_WB, /**< Write-back mapping with CPU caches enabled */
129
};
130
131
struct iris_bo {
132
/**
133
* Size in bytes of the buffer object.
134
*
135
* The size may be larger than the size originally requested for the
136
* allocation, such as being aligned to page size.
137
*/
138
uint64_t size;
139
140
/** Buffer manager context associated with this buffer object */
141
struct iris_bufmgr *bufmgr;
142
143
/** Pre-computed hash using _mesa_hash_pointer for cache tracking sets */
144
uint32_t hash;
145
146
/** The GEM handle for this buffer object. */
147
uint32_t gem_handle;
148
149
/**
150
* Virtual address of the buffer inside the PPGTT (Per-Process Graphics
151
* Translation Table).
152
*
153
* Although each hardware context has its own VMA, we assign BO's to the
154
* same address in all contexts, for simplicity.
155
*/
156
uint64_t gtt_offset;
157
158
/**
159
* If non-zero, then this bo has an aux-map translation to this address.
160
*/
161
uint64_t aux_map_address;
162
163
/**
164
* The validation list index for this buffer, or -1 when not in a batch.
165
* Note that a single buffer may be in multiple batches (contexts), and
166
* this is a global field, which refers to the last batch using the BO.
167
* It should not be considered authoritative, but can be used to avoid a
168
* linear walk of the validation list in the common case by guessing that
169
* exec_bos[bo->index] == bo and confirming whether that's the case.
170
*
171
* XXX: this is not ideal now that we have more than one batch per context,
172
* XXX: as the index will flop back and forth between the render index and
173
* XXX: compute index...
174
*/
175
unsigned index;
176
177
int refcount;
178
const char *name;
179
180
uint64_t kflags;
181
182
/**
183
* Kernel-assigned global name for this object
184
*
185
* List contains both flink named and prime fd'd objects
186
*/
187
unsigned global_name;
188
189
time_t free_time;
190
191
/** Mapped address for the buffer, saved across map/unmap cycles */
192
void *map;
193
194
/** BO cache list */
195
struct list_head head;
196
197
/** List of GEM handle exports of this buffer (bo_export) */
198
struct list_head exports;
199
200
/**
201
* Synchronization sequence number of most recent access of this BO from
202
* each caching domain.
203
*
204
* Although this is a global field, use in multiple contexts should be
205
* safe, see iris_emit_buffer_barrier_for() for details.
206
*
207
* Also align it to 64 bits. This will make atomic operations faster on 32
208
* bit platforms.
209
*/
210
uint64_t last_seqnos[NUM_IRIS_DOMAINS] __attribute__ ((aligned (8)));
211
212
/**
213
* Boolean of whether the GPU is definitely not accessing the buffer.
214
*
215
* This is only valid when reusable, since non-reusable
216
* buffers are those that have been shared with other
217
* processes, so we don't know their state.
218
*/
219
bool idle;
220
221
/**
222
* Boolean of whether this buffer can be re-used
223
*/
224
bool reusable;
225
226
/** Was this buffer imported from an external client? */
227
bool imported;
228
229
/** Has this buffer been exported to external clients? */
230
bool exported;
231
232
/**
233
* Boolean of whether this buffer points into user memory
234
*/
235
bool userptr;
236
237
/** The mmap coherency mode selected at BO allocation time */
238
enum iris_mmap_mode mmap_mode;
239
240
/**
241
* Boolean of whether this was allocated from local memory
242
*/
243
bool local;
244
};
245
246
#define BO_ALLOC_ZEROED (1<<0)
247
#define BO_ALLOC_COHERENT (1<<1)
248
#define BO_ALLOC_SMEM (1<<2)
249
250
/**
251
* Allocate a buffer object.
252
*
253
* Buffer objects are not necessarily initially mapped into CPU virtual
254
* address space or graphics device aperture. They must be mapped
255
* using iris_bo_map() to be used by the CPU.
256
*/
257
struct iris_bo *iris_bo_alloc(struct iris_bufmgr *bufmgr,
258
const char *name,
259
uint64_t size,
260
uint32_t alignment,
261
enum iris_memory_zone memzone,
262
unsigned flags);
263
264
struct iris_bo *
265
iris_bo_create_userptr(struct iris_bufmgr *bufmgr, const char *name,
266
void *ptr, size_t size,
267
enum iris_memory_zone memzone);
268
269
/** Takes a reference on a buffer object */
270
static inline void
271
iris_bo_reference(struct iris_bo *bo)
272
{
273
p_atomic_inc(&bo->refcount);
274
}
275
276
/**
277
* Releases a reference on a buffer object, freeing the data if
278
* no references remain.
279
*/
280
void iris_bo_unreference(struct iris_bo *bo);
281
282
#define MAP_READ PIPE_MAP_READ
283
#define MAP_WRITE PIPE_MAP_WRITE
284
#define MAP_ASYNC PIPE_MAP_UNSYNCHRONIZED
285
#define MAP_PERSISTENT PIPE_MAP_PERSISTENT
286
#define MAP_COHERENT PIPE_MAP_COHERENT
287
/* internal */
288
#define MAP_RAW (PIPE_MAP_DRV_PRV << 0)
289
#define MAP_INTERNAL_MASK (MAP_RAW)
290
291
#define MAP_FLAGS (MAP_READ | MAP_WRITE | MAP_ASYNC | \
292
MAP_PERSISTENT | MAP_COHERENT | MAP_INTERNAL_MASK)
293
294
/**
295
* Maps the buffer into userspace.
296
*
297
* This function will block waiting for any existing execution on the
298
* buffer to complete, first. The resulting mapping is returned.
299
*/
300
MUST_CHECK void *iris_bo_map(struct pipe_debug_callback *dbg,
301
struct iris_bo *bo, unsigned flags);
302
303
/**
304
* Reduces the refcount on the userspace mapping of the buffer
305
* object.
306
*/
307
static inline int iris_bo_unmap(struct iris_bo *bo) { return 0; }
308
309
/**
310
* Waits for rendering to an object by the GPU to have completed.
311
*
312
* This is not required for any access to the BO by bo_map,
313
* bo_subdata, etc. It is merely a way for the driver to implement
314
* glFinish.
315
*/
316
void iris_bo_wait_rendering(struct iris_bo *bo);
317
318
319
/**
320
* Unref a buffer manager instance.
321
*/
322
void iris_bufmgr_unref(struct iris_bufmgr *bufmgr);
323
324
/**
325
* Create a visible name for a buffer which can be used by other apps
326
*
327
* \param buf Buffer to create a name for
328
* \param name Returned name
329
*/
330
int iris_bo_flink(struct iris_bo *bo, uint32_t *name);
331
332
/**
333
* Is this buffer shared with external clients (imported or exported)?
334
*/
335
static inline bool
336
iris_bo_is_external(const struct iris_bo *bo)
337
{
338
return bo->exported || bo->imported;
339
}
340
341
/**
342
* Mark a buffer as being shared with other external clients.
343
*/
344
void iris_bo_mark_exported(struct iris_bo *bo);
345
346
/**
347
* Returns 1 if mapping the buffer for write could cause the process
348
* to block, due to the object being active in the GPU.
349
*/
350
int iris_bo_busy(struct iris_bo *bo);
351
352
/**
353
* Specify the volatility of the buffer.
354
* \param bo Buffer to create a name for
355
* \param madv The purgeable status
356
*
357
* Use I915_MADV_DONTNEED to mark the buffer as purgeable, and it will be
358
* reclaimed under memory pressure. If you subsequently require the buffer,
359
* then you must pass I915_MADV_WILLNEED to mark the buffer as required.
360
*
361
* Returns 1 if the buffer was retained, or 0 if it was discarded whilst
362
* marked as I915_MADV_DONTNEED.
363
*/
364
int iris_bo_madvise(struct iris_bo *bo, int madv);
365
366
/* drm_bacon_bufmgr_gem.c */
367
struct iris_bufmgr *iris_bufmgr_get_for_fd(struct intel_device_info *devinfo,
368
int fd, bool bo_reuse);
369
int iris_bufmgr_get_fd(struct iris_bufmgr *bufmgr);
370
371
struct iris_bo *iris_bo_gem_create_from_name(struct iris_bufmgr *bufmgr,
372
const char *name,
373
unsigned handle);
374
375
void* iris_bufmgr_get_aux_map_context(struct iris_bufmgr *bufmgr);
376
377
int iris_bo_wait(struct iris_bo *bo, int64_t timeout_ns);
378
379
uint32_t iris_create_hw_context(struct iris_bufmgr *bufmgr);
380
uint32_t iris_clone_hw_context(struct iris_bufmgr *bufmgr, uint32_t ctx_id);
381
382
#define IRIS_CONTEXT_LOW_PRIORITY ((I915_CONTEXT_MIN_USER_PRIORITY-1)/2)
383
#define IRIS_CONTEXT_MEDIUM_PRIORITY (I915_CONTEXT_DEFAULT_PRIORITY)
384
#define IRIS_CONTEXT_HIGH_PRIORITY ((I915_CONTEXT_MAX_USER_PRIORITY+1)/2)
385
386
int iris_hw_context_set_priority(struct iris_bufmgr *bufmgr,
387
uint32_t ctx_id, int priority);
388
389
void iris_destroy_hw_context(struct iris_bufmgr *bufmgr, uint32_t ctx_id);
390
391
int iris_gem_get_tiling(struct iris_bo *bo, uint32_t *tiling);
392
int iris_gem_set_tiling(struct iris_bo *bo, const struct isl_surf *surf);
393
394
int iris_bo_export_dmabuf(struct iris_bo *bo, int *prime_fd);
395
struct iris_bo *iris_bo_import_dmabuf(struct iris_bufmgr *bufmgr, int prime_fd);
396
397
/**
398
* Exports a bo as a GEM handle into a given DRM file descriptor
399
* \param bo Buffer to export
400
* \param drm_fd File descriptor where the new handle is created
401
* \param out_handle Pointer to store the new handle
402
*
403
* Returns 0 if the buffer was successfully exported, a non zero error code
404
* otherwise.
405
*/
406
int iris_bo_export_gem_handle_for_device(struct iris_bo *bo, int drm_fd,
407
uint32_t *out_handle);
408
409
uint32_t iris_bo_export_gem_handle(struct iris_bo *bo);
410
411
int iris_reg_read(struct iris_bufmgr *bufmgr, uint32_t offset, uint64_t *out);
412
413
int drm_ioctl(int fd, unsigned long request, void *arg);
414
415
/**
416
* Returns the BO's address relative to the appropriate base address.
417
*
418
* All of our base addresses are programmed to the start of a 4GB region,
419
* so simply returning the bottom 32 bits of the BO address will give us
420
* the offset from whatever base address corresponds to that memory region.
421
*/
422
static inline uint32_t
423
iris_bo_offset_from_base_address(struct iris_bo *bo)
424
{
425
/* This only works for buffers in the memory zones corresponding to a
426
* base address - the top, unbounded memory zone doesn't have a base.
427
*/
428
assert(bo->gtt_offset < IRIS_MEMZONE_OTHER_START);
429
return bo->gtt_offset;
430
}
431
432
/**
433
* Track access of a BO from the specified caching domain and sequence number.
434
*
435
* Can be used without locking. Only the most recent access (i.e. highest
436
* seqno) is tracked.
437
*/
438
static inline void
439
iris_bo_bump_seqno(struct iris_bo *bo, uint64_t seqno,
440
enum iris_domain type)
441
{
442
uint64_t *const last_seqno = &bo->last_seqnos[type];
443
uint64_t tmp, prev_seqno = p_atomic_read(last_seqno);
444
445
while (prev_seqno < seqno &&
446
prev_seqno != (tmp = p_atomic_cmpxchg(last_seqno, prev_seqno, seqno)))
447
prev_seqno = tmp;
448
}
449
450
enum iris_memory_zone iris_memzone_for_address(uint64_t address);
451
452
#endif /* IRIS_BUFMGR_H */
453
454