2
* Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
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:
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
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 FROM,
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
* Rob Clark <robclark@freedesktop.org>
27
#ifndef FREEDRENO_PRIV_H_
28
#define FREEDRENO_PRIV_H_
36
#include <sys/ioctl.h>
41
#include "util/hash_table.h"
42
#include "util/list.h"
44
#include "util/simple_mtx.h"
45
#include "util/slab.h"
46
#include "util/u_atomic.h"
47
#include "util/u_debug.h"
48
#include "util/u_math.h"
50
#include "freedreno_dev_info.h"
51
#include "freedreno_drmif.h"
52
#include "freedreno_ringbuffer.h"
54
extern simple_mtx_t table_lock;
57
* Stupid/simple growable array implementation:
60
#define MAX_ARRAY_SIZE ((unsigned short)~0)
63
grow(void **ptr, uint16_t nr, uint16_t *max, uint16_t sz)
65
assert((nr + 1) < MAX_ARRAY_SIZE);
66
if ((nr + 1) > *max) {
67
if (*max > MAX_ARRAY_SIZE/2)
68
*max = MAX_ARRAY_SIZE;
69
else if ((*max * 2) < (nr + 1))
73
*ptr = realloc(*ptr, *max * sz);
77
#define DECLARE_ARRAY(type, name) \
78
unsigned short nr_##name, max_##name; \
81
#define APPEND(x, name, ...) \
83
grow((void **)&(x)->name, (x)->nr_##name, &(x)->max_##name, \
84
sizeof((x)->name[0])); \
85
(x)->name[(x)->nr_##name] = __VA_ARGS__; \
89
#define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
92
struct fd_device_funcs {
93
/* Create a new buffer object:
95
struct fd_bo *(*bo_new)(struct fd_device *dev, uint32_t size, uint32_t flags);
97
/* Create a new buffer object from existing handle (ie. dma-buf or
100
struct fd_bo *(*bo_from_handle)(struct fd_device *dev, uint32_t size,
103
struct fd_pipe *(*pipe_new)(struct fd_device *dev, enum fd_pipe_id id,
105
void (*destroy)(struct fd_device *dev);
108
struct fd_bo_bucket {
110
struct list_head list;
114
struct fd_bo_bucket cache_bucket[14 * 4];
121
enum fd_version version;
124
/* tables to keep track of bo's, to avoid "evil-twin" fd_bo objects:
126
* handle_table: maps handle to fd_bo
127
* name_table: maps flink name to fd_bo
129
* We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
130
* returns a new handle. So we need to figure out if the bo is already
131
* open in the process first, before calling gem-open.
133
struct hash_table *handle_table, *name_table;
135
const struct fd_device_funcs *funcs;
137
struct fd_bo_cache bo_cache;
138
struct fd_bo_cache ring_cache;
140
bool has_cached_coherent;
142
bool closefd; /* call close(fd) upon destruction */
144
/* just for valgrind: */
148
* List of deferred submits, protected by submit_lock. The deferred
149
* submits are tracked globally per-device, even if they execute in
150
* different order on the kernel side (ie. due to different priority
151
* submitqueues, etc) to preserve the order that they are passed off
152
* to the kernel. Once the kernel has them, it is the fences' job
153
* to preserve correct order of execution.
155
struct list_head deferred_submits;
156
unsigned deferred_cmds;
157
simple_mtx_t submit_lock;
160
* BO for suballocating long-lived state objects.
162
* Note: one would be tempted to put this in fd_pipe to avoid locking.
163
* But that is a bad idea for a couple of reasons:
165
* 1) With TC, stateobj allocation can happen in either frontend thread
166
* (ie. most CSOs), and also driver thread (a6xx cached tex state)
167
* 2) It is best for fd_pipe to not hold a reference to a BO that can
168
* be free'd to bo cache, as that can cause unexpected re-entrancy
169
* (fd_bo_cache_alloc() -> find_in_bucket() -> fd_bo_state() ->
170
* cleanup_fences() -> drop pipe ref which free's bo's).
172
struct fd_bo *suballoc_bo;
173
uint32_t suballoc_offset;
174
simple_mtx_t suballoc_lock;
176
struct util_queue submit_queue;
179
#define foreach_submit(name, list) \
180
list_for_each_entry(struct fd_submit, name, list, node)
181
#define foreach_submit_safe(name, list) \
182
list_for_each_entry_safe(struct fd_submit, name, list, node)
183
#define last_submit(list) \
184
list_last_entry(list, struct fd_submit, node)
186
void fd_bo_cache_init(struct fd_bo_cache *cache, int coarse);
187
void fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time);
188
struct fd_bo *fd_bo_cache_alloc(struct fd_bo_cache *cache, uint32_t *size,
190
int fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo);
192
/* for where @table_lock is already held: */
193
void fd_bo_del_locked(struct fd_bo *bo);
194
void fd_device_del_locked(struct fd_device *dev);
195
void fd_pipe_del_locked(struct fd_pipe *pipe);
197
struct fd_pipe_funcs {
198
struct fd_ringbuffer *(*ringbuffer_new_object)(struct fd_pipe *pipe,
200
struct fd_submit *(*submit_new)(struct fd_pipe *pipe);
203
* Flush any deferred submits (if deferred submits are supported by
204
* the pipe implementation)
206
void (*flush)(struct fd_pipe *pipe, uint32_t fence);
208
int (*get_param)(struct fd_pipe *pipe, enum fd_param_id param,
210
int (*set_param)(struct fd_pipe *pipe, enum fd_param_id param,
212
int (*wait)(struct fd_pipe *pipe, const struct fd_fence *fence,
214
void (*destroy)(struct fd_pipe *pipe);
217
struct fd_pipe_control {
220
#define control_ptr(pipe, member) \
221
(pipe)->control_mem, offsetof(struct fd_pipe_control, member), 0, 0
224
struct fd_device *dev;
226
struct fd_dev_id dev_id;
229
* Note refcnt is *not* atomic, but protected by table_lock, since the
230
* table_lock is held in fd_bo_add_fence(), which is the hotpath.
235
* Previous fence seqno allocated for this pipe. The fd_pipe represents
236
* a single timeline, fences allocated by this pipe can be compared to
237
* each other, but fences from different pipes are not comparable (as
238
* there could be preemption of multiple priority level submitqueues at
244
* The last fence seqno that was flushed to kernel (doesn't mean that it
245
* is complete, just that the kernel knows about it)
247
uint32_t last_submit_fence;
249
uint32_t last_enqueue_fence; /* just for debugging */
251
struct fd_bo *control_mem;
252
volatile struct fd_pipe_control *control;
254
struct slab_parent_pool ring_pool;
256
const struct fd_pipe_funcs *funcs;
259
uint32_t fd_pipe_emit_fence(struct fd_pipe *pipe, struct fd_ringbuffer *ring);
262
fd_pipe_flush(struct fd_pipe *pipe, uint32_t fence)
264
if (!pipe->funcs->flush)
266
pipe->funcs->flush(pipe, fence);
269
struct fd_submit_funcs {
270
struct fd_ringbuffer *(*new_ringbuffer)(struct fd_submit *submit,
272
enum fd_ringbuffer_flags flags);
273
int (*flush)(struct fd_submit *submit, int in_fence_fd,
274
struct fd_submit_fence *out_fence);
275
void (*destroy)(struct fd_submit *submit);
280
struct fd_pipe *pipe;
281
const struct fd_submit_funcs *funcs;
283
struct fd_ringbuffer *primary;
285
struct list_head node; /* node in fd_pipe::deferred_submits */
288
static inline unsigned
289
fd_dev_count_deferred_cmds(struct fd_device *dev)
293
simple_mtx_assert_locked(&dev->submit_lock);
295
list_for_each_entry (struct fd_submit, submit, &dev->deferred_submits, node) {
296
nr += fd_ringbuffer_cmd_count(submit->primary);
303
int (*offset)(struct fd_bo *bo, uint64_t *offset);
304
int (*cpu_prep)(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
305
void (*cpu_fini)(struct fd_bo *bo);
306
int (*madvise)(struct fd_bo *bo, int willneed);
307
uint64_t (*iova)(struct fd_bo *bo);
308
void (*set_name)(struct fd_bo *bo, const char *fmt, va_list ap);
309
void (*destroy)(struct fd_bo *bo);
312
* Optional, copy data into bo, falls back to mmap+memcpy. If not
313
* implemented, it must be possible to mmap all buffers
315
void (*upload)(struct fd_bo *bo, void *src, unsigned len);
319
/* For non-shared buffers, track the last pipe the buffer was active
320
* on, and the per-pipe fence value that indicates when the buffer is
324
struct fd_pipe *pipe;
328
struct fd_device *dev;
333
uint32_t reloc_flags; /* flags like FD_RELOC_DUMP to use for relocs to this BO */
334
uint32_t alloc_flags; /* flags that control allocation/mapping, ie. FD_BO_x */
337
const struct fd_bo_funcs *funcs;
345
/* Buffers that are shared (imported or exported) may be used in
346
* other processes, so we need to fallback to kernel to determine
351
/* We need to be able to disable userspace fence synchronization for
352
* special internal buffers, namely the pipe->control buffer, to avoid
353
* a circular reference loop.
357
/* Most recent index in submit's bo table, used to optimize the common
358
* case where a bo is used many times in the same submit.
362
struct list_head list; /* bucket-list entry */
363
time_t free_time; /* time when added to bucket-list */
365
DECLARE_ARRAY(struct fd_bo_fence, fences);
367
/* In the common case, there is no more than one fence attached.
368
* This provides storage for the fences table until it grows to
369
* be larger than a single element.
371
struct fd_bo_fence _inline_fence;
374
void fd_bo_add_fence(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t fence);
381
enum fd_bo_state fd_bo_state(struct fd_bo *bo);
383
struct fd_bo *fd_bo_new_ring(struct fd_device *dev, uint32_t size);
385
#define enable_debug 0 /* TODO make dynamic */
389
#define INFO_MSG(fmt, ...) \
392
mesa_logi("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
394
#define DEBUG_MSG(fmt, ...) \
396
if (enable_debug) { \
397
mesa_logd("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
400
#define WARN_MSG(fmt, ...) \
402
mesa_logw("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
404
#define ERROR_MSG(fmt, ...) \
406
mesa_loge("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
409
#define U642VOID(x) ((void *)(unsigned long)(x))
410
#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
413
#include <memcheck.h>
416
* For tracking the backing memory (if valgrind enabled, we force a mmap
417
* for the purposes of tracking)
420
VG_BO_ALLOC(struct fd_bo *bo)
422
if (bo && RUNNING_ON_VALGRIND) {
423
VALGRIND_MALLOCLIKE_BLOCK(fd_bo_map(bo), bo->size, 0, 1);
428
VG_BO_FREE(struct fd_bo *bo)
430
VALGRIND_FREELIKE_BLOCK(bo->map, 0);
434
* For tracking bo structs that are in the buffer-cache, so that valgrind
435
* doesn't attribute ownership to the first one to allocate the recycled
438
* Note that the list_head in fd_bo is used to track the buffers in cache
439
* so disable error reporting on the range while they are in cache so
440
* valgrind doesn't squawk about list traversal.
444
VG_BO_RELEASE(struct fd_bo *bo)
446
if (RUNNING_ON_VALGRIND) {
447
VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);
448
VALGRIND_MAKE_MEM_NOACCESS(bo, bo->dev->bo_size);
449
VALGRIND_FREELIKE_BLOCK(bo->map, 0);
453
VG_BO_OBTAIN(struct fd_bo *bo)
455
if (RUNNING_ON_VALGRIND) {
456
VALGRIND_MAKE_MEM_DEFINED(bo, bo->dev->bo_size);
457
VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);
458
VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, 1);
463
VG_BO_ALLOC(struct fd_bo *bo)
467
VG_BO_FREE(struct fd_bo *bo)
471
VG_BO_RELEASE(struct fd_bo *bo)
475
VG_BO_OBTAIN(struct fd_bo *bo)
480
#define FD_DEFINE_CAST(parent, child) \
481
static inline struct child *to_##child(struct parent *x) \
483
return (struct child *)x; \
486
#endif /* FREEDRENO_PRIV_H_ */