1*7688df22SAndroid Build Coastguard Worker /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2*7688df22SAndroid Build Coastguard Worker
3*7688df22SAndroid Build Coastguard Worker /*
4*7688df22SAndroid Build Coastguard Worker * Copyright (C) 2012 Rob Clark <[email protected]>
5*7688df22SAndroid Build Coastguard Worker *
6*7688df22SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a
7*7688df22SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"),
8*7688df22SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation
9*7688df22SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10*7688df22SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the
11*7688df22SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions:
12*7688df22SAndroid Build Coastguard Worker *
13*7688df22SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next
14*7688df22SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the
15*7688df22SAndroid Build Coastguard Worker * Software.
16*7688df22SAndroid Build Coastguard Worker *
17*7688df22SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*7688df22SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*7688df22SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20*7688df22SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21*7688df22SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22*7688df22SAndroid Build Coastguard Worker * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23*7688df22SAndroid Build Coastguard Worker * SOFTWARE.
24*7688df22SAndroid Build Coastguard Worker *
25*7688df22SAndroid Build Coastguard Worker * Authors:
26*7688df22SAndroid Build Coastguard Worker * Rob Clark <[email protected]>
27*7688df22SAndroid Build Coastguard Worker */
28*7688df22SAndroid Build Coastguard Worker
29*7688df22SAndroid Build Coastguard Worker #ifndef FREEDRENO_PRIV_H_
30*7688df22SAndroid Build Coastguard Worker #define FREEDRENO_PRIV_H_
31*7688df22SAndroid Build Coastguard Worker
32*7688df22SAndroid Build Coastguard Worker #include <stdlib.h>
33*7688df22SAndroid Build Coastguard Worker #include <errno.h>
34*7688df22SAndroid Build Coastguard Worker #include <string.h>
35*7688df22SAndroid Build Coastguard Worker #include <unistd.h>
36*7688df22SAndroid Build Coastguard Worker #include <errno.h>
37*7688df22SAndroid Build Coastguard Worker #include <fcntl.h>
38*7688df22SAndroid Build Coastguard Worker #include <sys/ioctl.h>
39*7688df22SAndroid Build Coastguard Worker #include <pthread.h>
40*7688df22SAndroid Build Coastguard Worker #include <stdio.h>
41*7688df22SAndroid Build Coastguard Worker #include <assert.h>
42*7688df22SAndroid Build Coastguard Worker
43*7688df22SAndroid Build Coastguard Worker #include "libdrm_macros.h"
44*7688df22SAndroid Build Coastguard Worker #include "xf86drm.h"
45*7688df22SAndroid Build Coastguard Worker #include "xf86atomic.h"
46*7688df22SAndroid Build Coastguard Worker
47*7688df22SAndroid Build Coastguard Worker #include "util_double_list.h"
48*7688df22SAndroid Build Coastguard Worker #include "util_math.h"
49*7688df22SAndroid Build Coastguard Worker
50*7688df22SAndroid Build Coastguard Worker #include "freedreno_drmif.h"
51*7688df22SAndroid Build Coastguard Worker #include "freedreno_ringbuffer.h"
52*7688df22SAndroid Build Coastguard Worker #include "drm.h"
53*7688df22SAndroid Build Coastguard Worker
54*7688df22SAndroid Build Coastguard Worker #ifndef TRUE
55*7688df22SAndroid Build Coastguard Worker # define TRUE 1
56*7688df22SAndroid Build Coastguard Worker #endif
57*7688df22SAndroid Build Coastguard Worker #ifndef FALSE
58*7688df22SAndroid Build Coastguard Worker # define FALSE 0
59*7688df22SAndroid Build Coastguard Worker #endif
60*7688df22SAndroid Build Coastguard Worker
61*7688df22SAndroid Build Coastguard Worker struct fd_device_funcs {
62*7688df22SAndroid Build Coastguard Worker int (*bo_new_handle)(struct fd_device *dev, uint32_t size,
63*7688df22SAndroid Build Coastguard Worker uint32_t flags, uint32_t *handle);
64*7688df22SAndroid Build Coastguard Worker struct fd_bo * (*bo_from_handle)(struct fd_device *dev,
65*7688df22SAndroid Build Coastguard Worker uint32_t size, uint32_t handle);
66*7688df22SAndroid Build Coastguard Worker struct fd_pipe * (*pipe_new)(struct fd_device *dev, enum fd_pipe_id id,
67*7688df22SAndroid Build Coastguard Worker unsigned prio);
68*7688df22SAndroid Build Coastguard Worker void (*destroy)(struct fd_device *dev);
69*7688df22SAndroid Build Coastguard Worker };
70*7688df22SAndroid Build Coastguard Worker
71*7688df22SAndroid Build Coastguard Worker struct fd_bo_bucket {
72*7688df22SAndroid Build Coastguard Worker uint32_t size;
73*7688df22SAndroid Build Coastguard Worker struct list_head list;
74*7688df22SAndroid Build Coastguard Worker };
75*7688df22SAndroid Build Coastguard Worker
76*7688df22SAndroid Build Coastguard Worker struct fd_bo_cache {
77*7688df22SAndroid Build Coastguard Worker struct fd_bo_bucket cache_bucket[14 * 4];
78*7688df22SAndroid Build Coastguard Worker int num_buckets;
79*7688df22SAndroid Build Coastguard Worker time_t time;
80*7688df22SAndroid Build Coastguard Worker };
81*7688df22SAndroid Build Coastguard Worker
82*7688df22SAndroid Build Coastguard Worker struct fd_device {
83*7688df22SAndroid Build Coastguard Worker int fd;
84*7688df22SAndroid Build Coastguard Worker enum fd_version version;
85*7688df22SAndroid Build Coastguard Worker atomic_t refcnt;
86*7688df22SAndroid Build Coastguard Worker
87*7688df22SAndroid Build Coastguard Worker /* tables to keep track of bo's, to avoid "evil-twin" fd_bo objects:
88*7688df22SAndroid Build Coastguard Worker *
89*7688df22SAndroid Build Coastguard Worker * handle_table: maps handle to fd_bo
90*7688df22SAndroid Build Coastguard Worker * name_table: maps flink name to fd_bo
91*7688df22SAndroid Build Coastguard Worker *
92*7688df22SAndroid Build Coastguard Worker * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
93*7688df22SAndroid Build Coastguard Worker * returns a new handle. So we need to figure out if the bo is already
94*7688df22SAndroid Build Coastguard Worker * open in the process first, before calling gem-open.
95*7688df22SAndroid Build Coastguard Worker */
96*7688df22SAndroid Build Coastguard Worker void *handle_table, *name_table;
97*7688df22SAndroid Build Coastguard Worker
98*7688df22SAndroid Build Coastguard Worker const struct fd_device_funcs *funcs;
99*7688df22SAndroid Build Coastguard Worker
100*7688df22SAndroid Build Coastguard Worker struct fd_bo_cache bo_cache;
101*7688df22SAndroid Build Coastguard Worker struct fd_bo_cache ring_cache;
102*7688df22SAndroid Build Coastguard Worker
103*7688df22SAndroid Build Coastguard Worker int closefd; /* call close(fd) upon destruction */
104*7688df22SAndroid Build Coastguard Worker
105*7688df22SAndroid Build Coastguard Worker /* just for valgrind: */
106*7688df22SAndroid Build Coastguard Worker int bo_size;
107*7688df22SAndroid Build Coastguard Worker };
108*7688df22SAndroid Build Coastguard Worker
109*7688df22SAndroid Build Coastguard Worker drm_private void fd_bo_cache_init(struct fd_bo_cache *cache, int coarse);
110*7688df22SAndroid Build Coastguard Worker drm_private void fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time);
111*7688df22SAndroid Build Coastguard Worker drm_private struct fd_bo * fd_bo_cache_alloc(struct fd_bo_cache *cache,
112*7688df22SAndroid Build Coastguard Worker uint32_t *size, uint32_t flags);
113*7688df22SAndroid Build Coastguard Worker drm_private int fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo);
114*7688df22SAndroid Build Coastguard Worker
115*7688df22SAndroid Build Coastguard Worker /* for where @table_lock is already held: */
116*7688df22SAndroid Build Coastguard Worker drm_private void fd_device_del_locked(struct fd_device *dev);
117*7688df22SAndroid Build Coastguard Worker
118*7688df22SAndroid Build Coastguard Worker struct fd_pipe_funcs {
119*7688df22SAndroid Build Coastguard Worker struct fd_ringbuffer * (*ringbuffer_new)(struct fd_pipe *pipe, uint32_t size,
120*7688df22SAndroid Build Coastguard Worker enum fd_ringbuffer_flags flags);
121*7688df22SAndroid Build Coastguard Worker int (*get_param)(struct fd_pipe *pipe, enum fd_param_id param, uint64_t *value);
122*7688df22SAndroid Build Coastguard Worker int (*wait)(struct fd_pipe *pipe, uint32_t timestamp, uint64_t timeout);
123*7688df22SAndroid Build Coastguard Worker void (*destroy)(struct fd_pipe *pipe);
124*7688df22SAndroid Build Coastguard Worker };
125*7688df22SAndroid Build Coastguard Worker
126*7688df22SAndroid Build Coastguard Worker struct fd_pipe {
127*7688df22SAndroid Build Coastguard Worker struct fd_device *dev;
128*7688df22SAndroid Build Coastguard Worker enum fd_pipe_id id;
129*7688df22SAndroid Build Coastguard Worker uint32_t gpu_id;
130*7688df22SAndroid Build Coastguard Worker atomic_t refcnt;
131*7688df22SAndroid Build Coastguard Worker const struct fd_pipe_funcs *funcs;
132*7688df22SAndroid Build Coastguard Worker };
133*7688df22SAndroid Build Coastguard Worker
134*7688df22SAndroid Build Coastguard Worker struct fd_ringbuffer_funcs {
135*7688df22SAndroid Build Coastguard Worker void * (*hostptr)(struct fd_ringbuffer *ring);
136*7688df22SAndroid Build Coastguard Worker int (*flush)(struct fd_ringbuffer *ring, uint32_t *last_start,
137*7688df22SAndroid Build Coastguard Worker int in_fence_fd, int *out_fence_fd);
138*7688df22SAndroid Build Coastguard Worker void (*grow)(struct fd_ringbuffer *ring, uint32_t size);
139*7688df22SAndroid Build Coastguard Worker void (*reset)(struct fd_ringbuffer *ring);
140*7688df22SAndroid Build Coastguard Worker void (*emit_reloc)(struct fd_ringbuffer *ring,
141*7688df22SAndroid Build Coastguard Worker const struct fd_reloc *reloc);
142*7688df22SAndroid Build Coastguard Worker uint32_t (*emit_reloc_ring)(struct fd_ringbuffer *ring,
143*7688df22SAndroid Build Coastguard Worker struct fd_ringbuffer *target, uint32_t cmd_idx);
144*7688df22SAndroid Build Coastguard Worker uint32_t (*cmd_count)(struct fd_ringbuffer *ring);
145*7688df22SAndroid Build Coastguard Worker void (*destroy)(struct fd_ringbuffer *ring);
146*7688df22SAndroid Build Coastguard Worker };
147*7688df22SAndroid Build Coastguard Worker
148*7688df22SAndroid Build Coastguard Worker struct fd_bo_funcs {
149*7688df22SAndroid Build Coastguard Worker int (*offset)(struct fd_bo *bo, uint64_t *offset);
150*7688df22SAndroid Build Coastguard Worker int (*cpu_prep)(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
151*7688df22SAndroid Build Coastguard Worker void (*cpu_fini)(struct fd_bo *bo);
152*7688df22SAndroid Build Coastguard Worker int (*madvise)(struct fd_bo *bo, int willneed);
153*7688df22SAndroid Build Coastguard Worker uint64_t (*iova)(struct fd_bo *bo);
154*7688df22SAndroid Build Coastguard Worker void (*destroy)(struct fd_bo *bo);
155*7688df22SAndroid Build Coastguard Worker };
156*7688df22SAndroid Build Coastguard Worker
157*7688df22SAndroid Build Coastguard Worker struct fd_bo {
158*7688df22SAndroid Build Coastguard Worker struct fd_device *dev;
159*7688df22SAndroid Build Coastguard Worker uint32_t size;
160*7688df22SAndroid Build Coastguard Worker uint32_t handle;
161*7688df22SAndroid Build Coastguard Worker uint32_t name;
162*7688df22SAndroid Build Coastguard Worker void *map;
163*7688df22SAndroid Build Coastguard Worker atomic_t refcnt;
164*7688df22SAndroid Build Coastguard Worker const struct fd_bo_funcs *funcs;
165*7688df22SAndroid Build Coastguard Worker
166*7688df22SAndroid Build Coastguard Worker enum {
167*7688df22SAndroid Build Coastguard Worker NO_CACHE = 0,
168*7688df22SAndroid Build Coastguard Worker BO_CACHE = 1,
169*7688df22SAndroid Build Coastguard Worker RING_CACHE = 2,
170*7688df22SAndroid Build Coastguard Worker } bo_reuse;
171*7688df22SAndroid Build Coastguard Worker
172*7688df22SAndroid Build Coastguard Worker struct list_head list; /* bucket-list entry */
173*7688df22SAndroid Build Coastguard Worker time_t free_time; /* time when added to bucket-list */
174*7688df22SAndroid Build Coastguard Worker };
175*7688df22SAndroid Build Coastguard Worker
176*7688df22SAndroid Build Coastguard Worker drm_private struct fd_bo *fd_bo_new_ring(struct fd_device *dev,
177*7688df22SAndroid Build Coastguard Worker uint32_t size, uint32_t flags);
178*7688df22SAndroid Build Coastguard Worker
179*7688df22SAndroid Build Coastguard Worker #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
180*7688df22SAndroid Build Coastguard Worker
181*7688df22SAndroid Build Coastguard Worker #define enable_debug 0 /* TODO make dynamic */
182*7688df22SAndroid Build Coastguard Worker
183*7688df22SAndroid Build Coastguard Worker #define INFO_MSG(fmt, ...) \
184*7688df22SAndroid Build Coastguard Worker do { drmMsg("[I] "fmt " (%s:%d)\n", \
185*7688df22SAndroid Build Coastguard Worker ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
186*7688df22SAndroid Build Coastguard Worker #define DEBUG_MSG(fmt, ...) \
187*7688df22SAndroid Build Coastguard Worker do if (enable_debug) { drmMsg("[D] "fmt " (%s:%d)\n", \
188*7688df22SAndroid Build Coastguard Worker ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
189*7688df22SAndroid Build Coastguard Worker #define WARN_MSG(fmt, ...) \
190*7688df22SAndroid Build Coastguard Worker do { drmMsg("[W] "fmt " (%s:%d)\n", \
191*7688df22SAndroid Build Coastguard Worker ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
192*7688df22SAndroid Build Coastguard Worker #define ERROR_MSG(fmt, ...) \
193*7688df22SAndroid Build Coastguard Worker do { drmMsg("[E] " fmt " (%s:%d)\n", \
194*7688df22SAndroid Build Coastguard Worker ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
195*7688df22SAndroid Build Coastguard Worker
196*7688df22SAndroid Build Coastguard Worker #define U642VOID(x) ((void *)(unsigned long)(x))
197*7688df22SAndroid Build Coastguard Worker #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
198*7688df22SAndroid Build Coastguard Worker
199*7688df22SAndroid Build Coastguard Worker static inline uint32_t
offset_bytes(void * end,void * start)200*7688df22SAndroid Build Coastguard Worker offset_bytes(void *end, void *start)
201*7688df22SAndroid Build Coastguard Worker {
202*7688df22SAndroid Build Coastguard Worker return ((char *)end) - ((char *)start);
203*7688df22SAndroid Build Coastguard Worker }
204*7688df22SAndroid Build Coastguard Worker
205*7688df22SAndroid Build Coastguard Worker #if HAVE_VALGRIND
206*7688df22SAndroid Build Coastguard Worker # include <memcheck.h>
207*7688df22SAndroid Build Coastguard Worker
208*7688df22SAndroid Build Coastguard Worker /*
209*7688df22SAndroid Build Coastguard Worker * For tracking the backing memory (if valgrind enabled, we force a mmap
210*7688df22SAndroid Build Coastguard Worker * for the purposes of tracking)
211*7688df22SAndroid Build Coastguard Worker */
VG_BO_ALLOC(struct fd_bo * bo)212*7688df22SAndroid Build Coastguard Worker static inline void VG_BO_ALLOC(struct fd_bo *bo)
213*7688df22SAndroid Build Coastguard Worker {
214*7688df22SAndroid Build Coastguard Worker if (bo && RUNNING_ON_VALGRIND) {
215*7688df22SAndroid Build Coastguard Worker VALGRIND_MALLOCLIKE_BLOCK(fd_bo_map(bo), bo->size, 0, 1);
216*7688df22SAndroid Build Coastguard Worker }
217*7688df22SAndroid Build Coastguard Worker }
218*7688df22SAndroid Build Coastguard Worker
VG_BO_FREE(struct fd_bo * bo)219*7688df22SAndroid Build Coastguard Worker static inline void VG_BO_FREE(struct fd_bo *bo)
220*7688df22SAndroid Build Coastguard Worker {
221*7688df22SAndroid Build Coastguard Worker VALGRIND_FREELIKE_BLOCK(bo->map, 0);
222*7688df22SAndroid Build Coastguard Worker }
223*7688df22SAndroid Build Coastguard Worker
224*7688df22SAndroid Build Coastguard Worker /*
225*7688df22SAndroid Build Coastguard Worker * For tracking bo structs that are in the buffer-cache, so that valgrind
226*7688df22SAndroid Build Coastguard Worker * doesn't attribute ownership to the first one to allocate the recycled
227*7688df22SAndroid Build Coastguard Worker * bo.
228*7688df22SAndroid Build Coastguard Worker *
229*7688df22SAndroid Build Coastguard Worker * Note that the list_head in fd_bo is used to track the buffers in cache
230*7688df22SAndroid Build Coastguard Worker * so disable error reporting on the range while they are in cache so
231*7688df22SAndroid Build Coastguard Worker * valgrind doesn't squawk about list traversal.
232*7688df22SAndroid Build Coastguard Worker *
233*7688df22SAndroid Build Coastguard Worker */
VG_BO_RELEASE(struct fd_bo * bo)234*7688df22SAndroid Build Coastguard Worker static inline void VG_BO_RELEASE(struct fd_bo *bo)
235*7688df22SAndroid Build Coastguard Worker {
236*7688df22SAndroid Build Coastguard Worker if (RUNNING_ON_VALGRIND) {
237*7688df22SAndroid Build Coastguard Worker VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);
238*7688df22SAndroid Build Coastguard Worker VALGRIND_MAKE_MEM_NOACCESS(bo, bo->dev->bo_size);
239*7688df22SAndroid Build Coastguard Worker VALGRIND_FREELIKE_BLOCK(bo->map, 0);
240*7688df22SAndroid Build Coastguard Worker }
241*7688df22SAndroid Build Coastguard Worker }
VG_BO_OBTAIN(struct fd_bo * bo)242*7688df22SAndroid Build Coastguard Worker static inline void VG_BO_OBTAIN(struct fd_bo *bo)
243*7688df22SAndroid Build Coastguard Worker {
244*7688df22SAndroid Build Coastguard Worker if (RUNNING_ON_VALGRIND) {
245*7688df22SAndroid Build Coastguard Worker VALGRIND_MAKE_MEM_DEFINED(bo, bo->dev->bo_size);
246*7688df22SAndroid Build Coastguard Worker VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);
247*7688df22SAndroid Build Coastguard Worker VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, 1);
248*7688df22SAndroid Build Coastguard Worker }
249*7688df22SAndroid Build Coastguard Worker }
250*7688df22SAndroid Build Coastguard Worker #else
VG_BO_ALLOC(struct fd_bo * bo)251*7688df22SAndroid Build Coastguard Worker static inline void VG_BO_ALLOC(struct fd_bo *bo) {}
VG_BO_FREE(struct fd_bo * bo)252*7688df22SAndroid Build Coastguard Worker static inline void VG_BO_FREE(struct fd_bo *bo) {}
VG_BO_RELEASE(struct fd_bo * bo)253*7688df22SAndroid Build Coastguard Worker static inline void VG_BO_RELEASE(struct fd_bo *bo) {}
VG_BO_OBTAIN(struct fd_bo * bo)254*7688df22SAndroid Build Coastguard Worker static inline void VG_BO_OBTAIN(struct fd_bo *bo) {}
255*7688df22SAndroid Build Coastguard Worker #endif
256*7688df22SAndroid Build Coastguard Worker
257*7688df22SAndroid Build Coastguard Worker
258*7688df22SAndroid Build Coastguard Worker #endif /* FREEDRENO_PRIV_H_ */
259