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>
43 #include "util/hash_table.h"
44 #include "util/list.h"
45 #include "util/u_debug.h"
46 #include "util/u_atomic.h"
47 #include "util/u_math.h"
48 #include "util/u_debug.h"
50 #include "freedreno_drmif.h"
51 #include "freedreno_ringbuffer.h"
53 #define atomic_dec_and_test(x) (__sync_add_and_fetch (x, -1) == 0)
55 struct fd_device_funcs
{
56 int (*bo_new_handle
)(struct fd_device
*dev
, uint32_t size
,
57 uint32_t flags
, uint32_t *handle
);
58 struct fd_bo
* (*bo_from_handle
)(struct fd_device
*dev
,
59 uint32_t size
, uint32_t handle
);
60 struct fd_pipe
* (*pipe_new
)(struct fd_device
*dev
, enum fd_pipe_id id
,
62 void (*destroy
)(struct fd_device
*dev
);
67 struct list_head list
;
71 struct fd_bo_bucket cache_bucket
[14 * 4];
78 enum fd_version version
;
81 /* tables to keep track of bo's, to avoid "evil-twin" fd_bo objects:
83 * handle_table: maps handle to fd_bo
84 * name_table: maps flink name to fd_bo
86 * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
87 * returns a new handle. So we need to figure out if the bo is already
88 * open in the process first, before calling gem-open.
90 struct hash_table
*handle_table
, *name_table
;
92 const struct fd_device_funcs
*funcs
;
94 struct fd_bo_cache bo_cache
;
95 struct fd_bo_cache ring_cache
;
97 int closefd
; /* call close(fd) upon destruction */
99 /* just for valgrind: */
103 void fd_bo_cache_init(struct fd_bo_cache
*cache
, int coarse
);
104 void fd_bo_cache_cleanup(struct fd_bo_cache
*cache
, time_t time
);
105 struct fd_bo
* fd_bo_cache_alloc(struct fd_bo_cache
*cache
,
106 uint32_t *size
, uint32_t flags
);
107 int fd_bo_cache_free(struct fd_bo_cache
*cache
, struct fd_bo
*bo
);
109 /* for where @table_lock is already held: */
110 void fd_device_del_locked(struct fd_device
*dev
);
112 struct fd_pipe_funcs
{
113 struct fd_ringbuffer
* (*ringbuffer_new_object
)(struct fd_pipe
*pipe
, uint32_t size
);
114 struct fd_submit
* (*submit_new
)(struct fd_pipe
*pipe
);
115 int (*get_param
)(struct fd_pipe
*pipe
, enum fd_param_id param
, uint64_t *value
);
116 int (*wait
)(struct fd_pipe
*pipe
, uint32_t timestamp
, uint64_t timeout
);
117 void (*destroy
)(struct fd_pipe
*pipe
);
121 struct fd_device
*dev
;
125 const struct fd_pipe_funcs
*funcs
;
128 struct fd_submit_funcs
{
129 struct fd_ringbuffer
* (*new_ringbuffer
)(struct fd_submit
*submit
,
130 uint32_t size
, enum fd_ringbuffer_flags flags
);
131 int (*flush
)(struct fd_submit
*submit
, int in_fence_fd
,
132 int *out_fence_fd
, uint32_t *out_fence
);
133 void (*destroy
)(struct fd_submit
*submit
);
137 struct fd_pipe
*pipe
;
138 const struct fd_submit_funcs
*funcs
;
142 int (*offset
)(struct fd_bo
*bo
, uint64_t *offset
);
143 int (*cpu_prep
)(struct fd_bo
*bo
, struct fd_pipe
*pipe
, uint32_t op
);
144 void (*cpu_fini
)(struct fd_bo
*bo
);
145 int (*madvise
)(struct fd_bo
*bo
, int willneed
);
146 uint64_t (*iova
)(struct fd_bo
*bo
);
147 void (*set_name
)(struct fd_bo
*bo
, const char *fmt
, va_list ap
);
148 void (*destroy
)(struct fd_bo
*bo
);
152 struct fd_device
*dev
;
157 uint32_t flags
; /* flags like FD_RELOC_DUMP to use for relocs to this BO */
160 const struct fd_bo_funcs
*funcs
;
168 struct list_head list
; /* bucket-list entry */
169 time_t free_time
; /* time when added to bucket-list */
172 struct fd_bo
*fd_bo_new_ring(struct fd_device
*dev
, uint32_t size
);
174 #define enable_debug 0 /* TODO make dynamic */
178 #define INFO_MSG(fmt, ...) \
179 do { if (fd_dbg()) debug_printf("[I] "fmt " (%s:%d)\n", \
180 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
181 #define DEBUG_MSG(fmt, ...) \
182 do if (enable_debug) { debug_printf("[D] "fmt " (%s:%d)\n", \
183 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
184 #define WARN_MSG(fmt, ...) \
185 do { debug_printf("[W] "fmt " (%s:%d)\n", \
186 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
187 #define ERROR_MSG(fmt, ...) \
188 do { debug_printf("[E] " fmt " (%s:%d)\n", \
189 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
191 #define U642VOID(x) ((void *)(unsigned long)(x))
192 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
195 # include <memcheck.h>
198 * For tracking the backing memory (if valgrind enabled, we force a mmap
199 * for the purposes of tracking)
201 static inline void VG_BO_ALLOC(struct fd_bo
*bo
)
203 if (bo
&& RUNNING_ON_VALGRIND
) {
204 VALGRIND_MALLOCLIKE_BLOCK(fd_bo_map(bo
), bo
->size
, 0, 1);
208 static inline void VG_BO_FREE(struct fd_bo
*bo
)
210 VALGRIND_FREELIKE_BLOCK(bo
->map
, 0);
214 * For tracking bo structs that are in the buffer-cache, so that valgrind
215 * doesn't attribute ownership to the first one to allocate the recycled
218 * Note that the list_head in fd_bo is used to track the buffers in cache
219 * so disable error reporting on the range while they are in cache so
220 * valgrind doesn't squawk about list traversal.
223 static inline void VG_BO_RELEASE(struct fd_bo
*bo
)
225 if (RUNNING_ON_VALGRIND
) {
226 VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo
, bo
->dev
->bo_size
);
227 VALGRIND_MAKE_MEM_NOACCESS(bo
, bo
->dev
->bo_size
);
228 VALGRIND_FREELIKE_BLOCK(bo
->map
, 0);
231 static inline void VG_BO_OBTAIN(struct fd_bo
*bo
)
233 if (RUNNING_ON_VALGRIND
) {
234 VALGRIND_MAKE_MEM_DEFINED(bo
, bo
->dev
->bo_size
);
235 VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo
, bo
->dev
->bo_size
);
236 VALGRIND_MALLOCLIKE_BLOCK(bo
->map
, bo
->size
, 0, 1);
240 static inline void VG_BO_ALLOC(struct fd_bo
*bo
) {}
241 static inline void VG_BO_FREE(struct fd_bo
*bo
) {}
242 static inline void VG_BO_RELEASE(struct fd_bo
*bo
) {}
243 static inline void VG_BO_OBTAIN(struct fd_bo
*bo
) {}
246 #define FD_DEFINE_CAST(parent, child) \
247 static inline struct child * to_ ## child (struct parent *x) \
248 { return (struct child *)x; }
251 #endif /* FREEDRENO_PRIV_H_ */