• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Rob Clark <robclark@freedesktop.org>
25  */
26 
27 #ifndef FREEDRENO_DRMIF_H_
28 #define FREEDRENO_DRMIF_H_
29 
30 #include <stdint.h>
31 
32 #include "util/bitset.h"
33 #include "util/list.h"
34 #include "util/u_debug.h"
35 #include "util/u_queue.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 struct fd_bo;
42 struct fd_pipe;
43 struct fd_device;
44 
45 enum fd_pipe_id {
46    FD_PIPE_3D = 1,
47    FD_PIPE_2D = 2,
48    /* some devices have two 2d blocks.. not really sure how to
49     * use that yet, so just ignoring the 2nd 2d pipe for now
50     */
51    FD_PIPE_MAX
52 };
53 
54 enum fd_param_id {
55    FD_DEVICE_ID,
56    FD_GMEM_SIZE,
57    FD_GMEM_BASE,     /* 64b */
58    FD_GPU_ID,
59    FD_CHIP_ID,       /* 64b */
60    FD_MAX_FREQ,
61    FD_TIMESTAMP,
62    FD_NR_PRIORITIES,      /* # of rings == # of distinct priority levels */
63    FD_CTX_FAULTS,    /* # of per context faults */
64    FD_GLOBAL_FAULTS, /* # of global (all context) faults */
65    FD_SUSPEND_COUNT, /* # of times the GPU has suspended, and potentially lost state */
66    FD_SYSPROF,       /* Settable (for CAP_SYS_ADMIN) param for system profiling */
67    FD_VA_SIZE,       /* GPU virtual address size */
68 };
69 
70 /**
71  * Helper for fence/seqno comparisions which deals properly with rollover.
72  * Returns true if fence 'a' is before fence 'b'
73  */
74 static inline bool
fd_fence_before(uint32_t a,uint32_t b)75 fd_fence_before(uint32_t a, uint32_t b)
76 {
77    return (int32_t)(a - b) < 0;
78 }
79 
80 static inline bool
fd_fence_after(uint32_t a,uint32_t b)81 fd_fence_after(uint32_t a, uint32_t b)
82 {
83    return (int32_t)(a - b) > 0;
84 }
85 
86 /**
87  * Encapsulates submit out-fence(s), which consist of a 'timestamp' (per-
88  * pipe (submitqueue) sequence number) and optionally, if requested, an
89  * out-fence-fd
90  *
91  * Per submit, there are actually two fences:
92  *  1) The userspace maintained fence, which is used to optimistically
93  *     avoid kernel ioctls to query if specific rendering is completed
94  *  2) The kernel maintained fence, which we cannot directly do anything
95  *     with, other than pass it back to the kernel
96  *
97  * The userspace fence is mostly internal to the drm layer, but we want
98  * the gallium layer to be able to pass it back to us for things like
99  * fd_pipe_wait().  So this struct encapsulates the two.
100  */
101 struct fd_fence {
102    /**
103     * Note refcnt is *not* atomic, but protected by fence_lock, since the
104     * fence_lock is held in fd_bo_add_fence(), which is the hotpath.
105     */
106    int32_t refcnt;
107 
108    struct fd_pipe *pipe;
109 
110    /**
111     * The ready fence is signaled once the submit is actually flushed down
112     * to the kernel, and fence/fence_fd are populated.  You must wait for
113     * this fence to be signaled before reading fence/fence_fd.
114     */
115    struct util_queue_fence ready;
116 
117    uint32_t kfence;     /* kernel fence */
118    uint32_t ufence;     /* userspace fence */
119 
120    /**
121     * Optional dma_fence fd, returned by submit if use_fence_fd is true
122     */
123    int fence_fd;
124    bool use_fence_fd;
125 };
126 
127 struct fd_fence *fd_fence_new(struct fd_pipe *pipe, bool use_fence_fd);
128 struct fd_fence *fd_fence_ref(struct fd_fence *f);
129 struct fd_fence *fd_fence_ref_locked(struct fd_fence *f);
130 void fd_fence_del(struct fd_fence *f);
131 void fd_fence_del_locked(struct fd_fence *f);
132 void fd_fence_flush(struct fd_fence *f);
133 int fd_fence_wait(struct fd_fence *f);
134 
135 /*
136  * bo flags:
137  */
138 
139 #define FD_BO_CACHED_COHERENT     BITSET_BIT(0) /* Default caching is WRITECOMBINE */
140 #define FD_BO_GPUREADONLY         BITSET_BIT(1)
141 #define FD_BO_NOMAP               BITSET_BIT(2) /* Hint that the bo will not be mmap'd */
142 
143 /* Hint that the bo will be exported/shared: */
144 #define FD_BO_SHARED              BITSET_BIT(4)
145 #define FD_BO_SCANOUT             BITSET_BIT(5)
146 
147 /* internal bo flags: */
148 #define _FD_BO_NOSYNC             BITSET_BIT(7) /* Avoid userspace fencing on control buffers */
149 
150 /*
151  * bo access flags: (keep aligned to MSM_PREP_x)
152  */
153 #define FD_BO_PREP_READ   BITSET_BIT(0)
154 #define FD_BO_PREP_WRITE  BITSET_BIT(1)
155 #define FD_BO_PREP_NOSYNC BITSET_BIT(2)
156 #define FD_BO_PREP_FLUSH  BITSET_BIT(3)
157 
158 
159 /* device functions:
160  */
161 
162 struct fd_device *fd_device_new(int fd);
163 struct fd_device *fd_device_new_dup(int fd);
164 struct fd_device *fd_device_open(void);
165 struct fd_device *fd_device_ref(struct fd_device *dev);
166 void fd_device_purge(struct fd_device *dev);
167 void fd_device_del(struct fd_device *dev);
168 int fd_device_fd(struct fd_device *dev);
169 
170 enum fd_version {
171    FD_VERSION_MADVISE = 1,             /* kernel supports madvise */
172    FD_VERSION_UNLIMITED_CMDS = 1,      /* submits w/ >4 cmd buffers (growable ringbuffer) */
173    FD_VERSION_FENCE_FD = 2,            /* submit command supports in/out fences */
174    FD_VERSION_GMEM_BASE = 3,           /* supports querying GMEM base address */
175    FD_VERSION_SUBMIT_QUEUES = 3,       /* submit queues and multiple priority levels */
176    FD_VERSION_BO_IOVA = 3,             /* supports fd_bo_get/put_iova() */
177    FD_VERSION_SOFTPIN = 4,             /* adds softpin, bo name, and dump flag */
178    FD_VERSION_ROBUSTNESS = 5,          /* adds FD_NR_FAULTS and FD_PP_PGTABLE */
179    FD_VERSION_MEMORY_FD = 2,           /* supports shared memory objects */
180    FD_VERSION_SUSPENDS = 7,            /* Adds MSM_PARAM_SUSPENDS to detect device suspend */
181    FD_VERSION_CACHED_COHERENT = 8,     /* Adds cached-coherent support (a6xx+) */
182    FD_VERSION_VA_SIZE = 9,
183 };
184 enum fd_version fd_device_version(struct fd_device *dev);
185 
186 bool fd_has_syncobj(struct fd_device *dev);
187 
188 /* pipe functions:
189  */
190 
191 struct fd_pipe *fd_pipe_new(struct fd_device *dev, enum fd_pipe_id id);
192 struct fd_pipe *fd_pipe_new2(struct fd_device *dev, enum fd_pipe_id id,
193                              uint32_t prio);
194 struct fd_pipe *fd_pipe_ref(struct fd_pipe *pipe);
195 struct fd_pipe *fd_pipe_ref_locked(struct fd_pipe *pipe);
196 void fd_pipe_del(struct fd_pipe *pipe);
197 void fd_pipe_purge(struct fd_pipe *pipe);
198 const struct fd_dev_id * fd_pipe_dev_id(struct fd_pipe *pipe);
199 int fd_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
200                       uint64_t *value);
201 int fd_pipe_set_param(struct fd_pipe *pipe, enum fd_param_id param,
202                       uint64_t value);
203 int fd_pipe_wait(struct fd_pipe *pipe, const struct fd_fence *fence);
204 /* timeout in nanosec */
205 int fd_pipe_wait_timeout(struct fd_pipe *pipe, const struct fd_fence *fence,
206                          uint64_t timeout);
207 
208 /* buffer-object functions:
209  */
210 
211 struct fd_bo {
212    struct fd_device *dev;
213    uint32_t size;
214    uint32_t handle;
215    uint32_t name;
216    int32_t refcnt;
217    uint32_t reloc_flags; /* flags like FD_RELOC_DUMP to use for relocs to this BO */
218    uint32_t alloc_flags; /* flags that control allocation/mapping, ie. FD_BO_x */
219    uint64_t iova;
220    void *map;
221    const struct fd_bo_funcs *funcs;
222 
223    enum {
224       NO_CACHE = 0,
225       BO_CACHE = 1,
226       RING_CACHE = 2,
227    } bo_reuse : 2;
228 
229    /* Most recent index in submit's bo table, used to optimize the common
230     * case where a bo is used many times in the same submit.
231     */
232    uint32_t idx;
233 
234    struct list_head node; /* bucket-list entry */
235    time_t free_time;      /* time when added to bucket-list */
236 
237    unsigned short nr_fences, max_fences;
238    struct fd_fence **fences;
239 
240    /* In the common case, there is no more than one fence attached.
241     * This provides storage for the fences table until it grows to
242     * be larger than a single element.
243     */
244    struct fd_fence *_inline_fence;
245 };
246 
247 struct fd_bo *_fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags);
248 void _fd_bo_set_name(struct fd_bo *bo, const char *fmt, va_list ap);
249 
250 static inline void fd_bo_set_name(struct fd_bo *bo, const char *fmt, ...)
251    _util_printf_format(2, 3);
252 
253 static inline void
fd_bo_set_name(struct fd_bo * bo,const char * fmt,...)254 fd_bo_set_name(struct fd_bo *bo, const char *fmt, ...)
255 {
256 #ifndef NDEBUG
257    va_list ap;
258    va_start(ap, fmt);
259    _fd_bo_set_name(bo, fmt, ap);
260    va_end(ap);
261 #endif
262 }
263 
264 static inline struct fd_bo *fd_bo_new(struct fd_device *dev, uint32_t size,
265                                       uint32_t flags, const char *fmt, ...)
266    _util_printf_format(4, 5);
267 
268 static inline struct fd_bo *
fd_bo_new(struct fd_device * dev,uint32_t size,uint32_t flags,const char * fmt,...)269 fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags, const char *fmt,
270           ...)
271 {
272    struct fd_bo *bo = _fd_bo_new(dev, size, flags);
273 #ifndef NDEBUG
274    if (fmt) {
275       va_list ap;
276       va_start(ap, fmt);
277       _fd_bo_set_name(bo, fmt, ap);
278       va_end(ap);
279    }
280 #endif
281    return bo;
282 }
283 
284 struct fd_bo *fd_bo_from_handle(struct fd_device *dev, uint32_t handle,
285                                 uint32_t size);
286 struct fd_bo *fd_bo_from_name(struct fd_device *dev, uint32_t name);
287 struct fd_bo *fd_bo_from_dmabuf(struct fd_device *dev, int fd);
288 void fd_bo_mark_for_dump(struct fd_bo *bo);
289 
290 static inline uint64_t
fd_bo_get_iova(struct fd_bo * bo)291 fd_bo_get_iova(struct fd_bo *bo)
292 {
293    /* ancient kernels did not support this */
294    assert(bo->iova != 0);
295    return bo->iova;
296 }
297 
298 struct fd_bo *fd_bo_ref(struct fd_bo *bo);
299 void fd_bo_del(struct fd_bo *bo);
300 void fd_bo_del_array(struct fd_bo **bos, int count);
301 void fd_bo_del_list_nocache(struct list_head *list);
302 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name);
303 uint32_t fd_bo_handle(struct fd_bo *bo);
304 int fd_bo_dmabuf_drm(struct fd_bo *bo);
305 int fd_bo_dmabuf(struct fd_bo *bo);
306 uint32_t fd_bo_size(struct fd_bo *bo);
307 void *fd_bo_map(struct fd_bo *bo);
308 void fd_bo_upload(struct fd_bo *bo, void *src, unsigned off, unsigned len);
309 bool fd_bo_prefer_upload(struct fd_bo *bo, unsigned len);
310 int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
311 bool fd_bo_is_cached(struct fd_bo *bo);
312 void fd_bo_set_metadata(struct fd_bo *bo, void *metadata, uint32_t metadata_size);
313 int fd_bo_get_metadata(struct fd_bo *bo, void *metadata, uint32_t metadata_size);
314 
315 #ifdef __cplusplus
316 } /* end of extern "C" */
317 #endif
318 
319 #endif /* FREEDRENO_DRMIF_H_ */
320