1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
29 #ifndef FREEDRENO_PRIV_H_
30 #define FREEDRENO_PRIV_H_
38 #include <sys/ioctl.h>
43 #include "libdrm_macros.h"
45 #include "xf86atomic.h"
47 #include "util_double_list.h"
48 #include "util_math.h"
50 #include "freedreno_drmif.h"
51 #include "freedreno_ringbuffer.h"
61 struct fd_device_funcs {
62 int (*bo_new_handle)(struct fd_device *dev, uint32_t size,
63 uint32_t flags, uint32_t *handle);
64 struct fd_bo * (*bo_from_handle)(struct fd_device *dev,
65 uint32_t size, uint32_t handle);
66 struct fd_pipe * (*pipe_new)(struct fd_device *dev, enum fd_pipe_id id,
68 void (*destroy)(struct fd_device *dev);
73 struct list_head list;
77 struct fd_bo_bucket cache_bucket[14 * 4];
84 enum fd_version version;
87 /* tables to keep track of bo's, to avoid "evil-twin" fd_bo objects:
89 * handle_table: maps handle to fd_bo
90 * name_table: maps flink name to fd_bo
92 * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
93 * returns a new handle. So we need to figure out if the bo is already
94 * open in the process first, before calling gem-open.
96 void *handle_table, *name_table;
98 const struct fd_device_funcs *funcs;
100 struct fd_bo_cache bo_cache;
101 struct fd_bo_cache ring_cache;
103 int closefd; /* call close(fd) upon destruction */
105 /* just for valgrind: */
109 drm_private void fd_bo_cache_init(struct fd_bo_cache *cache, int coarse);
110 drm_private void fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time);
111 drm_private struct fd_bo * fd_bo_cache_alloc(struct fd_bo_cache *cache,
112 uint32_t *size, uint32_t flags);
113 drm_private int fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo);
115 /* for where @table_lock is already held: */
116 drm_private void fd_device_del_locked(struct fd_device *dev);
118 enum fd_ringbuffer_flags {
119 FD_RINGBUFFER_OBJECT = 0x1,
122 struct fd_pipe_funcs {
123 struct fd_ringbuffer * (*ringbuffer_new)(struct fd_pipe *pipe, uint32_t size,
124 enum fd_ringbuffer_flags flags);
125 int (*get_param)(struct fd_pipe *pipe, enum fd_param_id param, uint64_t *value);
126 int (*wait)(struct fd_pipe *pipe, uint32_t timestamp, uint64_t timeout);
127 void (*destroy)(struct fd_pipe *pipe);
131 struct fd_device *dev;
135 const struct fd_pipe_funcs *funcs;
138 struct fd_ringmarker {
139 struct fd_ringbuffer *ring;
143 struct fd_ringbuffer_funcs {
144 void * (*hostptr)(struct fd_ringbuffer *ring);
145 int (*flush)(struct fd_ringbuffer *ring, uint32_t *last_start,
146 int in_fence_fd, int *out_fence_fd);
147 void (*grow)(struct fd_ringbuffer *ring, uint32_t size);
148 void (*reset)(struct fd_ringbuffer *ring);
149 void (*emit_reloc)(struct fd_ringbuffer *ring,
150 const struct fd_reloc *reloc);
151 uint32_t (*emit_reloc_ring)(struct fd_ringbuffer *ring,
152 struct fd_ringbuffer *target, uint32_t cmd_idx,
153 uint32_t submit_offset, uint32_t size);
154 uint32_t (*cmd_count)(struct fd_ringbuffer *ring);
155 void (*destroy)(struct fd_ringbuffer *ring);
159 int (*offset)(struct fd_bo *bo, uint64_t *offset);
160 int (*cpu_prep)(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
161 void (*cpu_fini)(struct fd_bo *bo);
162 int (*madvise)(struct fd_bo *bo, int willneed);
163 uint64_t (*iova)(struct fd_bo *bo);
164 void (*destroy)(struct fd_bo *bo);
168 struct fd_device *dev;
174 const struct fd_bo_funcs *funcs;
182 struct list_head list; /* bucket-list entry */
183 time_t free_time; /* time when added to bucket-list */
186 drm_private struct fd_bo *fd_bo_new_ring(struct fd_device *dev,
187 uint32_t size, uint32_t flags);
189 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
191 #define enable_debug 0 /* TODO make dynamic */
193 #define INFO_MSG(fmt, ...) \
194 do { drmMsg("[I] "fmt " (%s:%d)\n", \
195 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
196 #define DEBUG_MSG(fmt, ...) \
197 do if (enable_debug) { drmMsg("[D] "fmt " (%s:%d)\n", \
198 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
199 #define WARN_MSG(fmt, ...) \
200 do { drmMsg("[W] "fmt " (%s:%d)\n", \
201 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
202 #define ERROR_MSG(fmt, ...) \
203 do { drmMsg("[E] " fmt " (%s:%d)\n", \
204 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
206 #define U642VOID(x) ((void *)(unsigned long)(x))
207 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
209 static inline uint32_t
210 offset_bytes(void *end, void *start)
212 return ((char *)end) - ((char *)start);
216 # include <memcheck.h>
219 * For tracking the backing memory (if valgrind enabled, we force a mmap
220 * for the purposes of tracking)
222 static inline void VG_BO_ALLOC(struct fd_bo *bo)
224 if (bo && RUNNING_ON_VALGRIND) {
225 VALGRIND_MALLOCLIKE_BLOCK(fd_bo_map(bo), bo->size, 0, 1);
229 static inline void VG_BO_FREE(struct fd_bo *bo)
231 VALGRIND_FREELIKE_BLOCK(bo->map, 0);
235 * For tracking bo structs that are in the buffer-cache, so that valgrind
236 * doesn't attribute ownership to the first one to allocate the recycled
239 * Note that the list_head in fd_bo is used to track the buffers in cache
240 * so disable error reporting on the range while they are in cache so
241 * valgrind doesn't squawk about list traversal.
244 static inline void VG_BO_RELEASE(struct fd_bo *bo)
246 if (RUNNING_ON_VALGRIND) {
247 VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);
248 VALGRIND_MAKE_MEM_NOACCESS(bo, bo->dev->bo_size);
249 VALGRIND_FREELIKE_BLOCK(bo->map, 0);
252 static inline void VG_BO_OBTAIN(struct fd_bo *bo)
254 if (RUNNING_ON_VALGRIND) {
255 VALGRIND_MAKE_MEM_DEFINED(bo, bo->dev->bo_size);
256 VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);
257 VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, 1);
261 static inline void VG_BO_ALLOC(struct fd_bo *bo) {}
262 static inline void VG_BO_FREE(struct fd_bo *bo) {}
263 static inline void VG_BO_RELEASE(struct fd_bo *bo) {}
264 static inline void VG_BO_OBTAIN(struct fd_bo *bo) {}
268 #endif /* FREEDRENO_PRIV_H_ */