freedreno/drm: remove dependency on gallium driver
[mesa.git] / src / gallium / drivers / freedreno / drm / freedreno_ringbuffer.h
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_RINGBUFFER_H_
28 #define FREEDRENO_RINGBUFFER_H_
29
30 #include "util/u_debug.h"
31
32 #include "freedreno_drmif.h"
33
34 struct fd_submit;
35 struct fd_ringbuffer;
36
37 enum fd_ringbuffer_flags {
38
39 /* Primary ringbuffer for a submit, ie. an IB1 level rb
40 * which kernel must setup RB->IB1 CP_INDIRECT_BRANCH
41 * packets.
42 */
43 FD_RINGBUFFER_PRIMARY = 0x1,
44
45 /* Hint that the stateobj will be used for streaming state
46 * that is used once or a few times and then discarded.
47 *
48 * For sub-allocation, non streaming stateobj's should be
49 * sub-allocated from a page size buffer, so one long lived
50 * state obj doesn't prevent other pages from being freed.
51 * (Ie. it would be no worse than allocating a page sized
52 * bo for each small non-streaming stateobj).
53 *
54 * But streaming stateobj's could be sub-allocated from a
55 * larger buffer to reduce the alloc/del overhead.
56 */
57 FD_RINGBUFFER_STREAMING = 0x2,
58
59 /* Indicates that "growable" cmdstream can be used,
60 * consisting of multiple physical cmdstream buffers
61 */
62 FD_RINGBUFFER_GROWABLE = 0x4,
63
64 /* Internal use only: */
65 _FD_RINGBUFFER_OBJECT = 0x8,
66 };
67
68 /* A submit object manages/tracks all the state buildup for a "submit"
69 * ioctl to the kernel. Additionally, with the exception of long-lived
70 * non-STREAMING stateobj rb's, rb's are allocated from the submit.
71 */
72 struct fd_submit * fd_submit_new(struct fd_pipe *pipe);
73
74 /* NOTE: all ringbuffer's create from the submit should be unref'd
75 * before destroying the submit.
76 */
77 void fd_submit_del(struct fd_submit *submit);
78
79 /* Allocate a new rb from the submit. */
80 struct fd_ringbuffer * fd_submit_new_ringbuffer(struct fd_submit *submit,
81 uint32_t size, enum fd_ringbuffer_flags flags);
82
83 /* in_fence_fd: -1 for no in-fence, else fence fd
84 * out_fence_fd: NULL for no output-fence requested, else ptr to return out-fence
85 */
86 int fd_submit_flush(struct fd_submit *submit,
87 int in_fence_fd, int *out_fence_fd,
88 uint32_t *out_fence);
89
90 struct fd_ringbuffer_funcs;
91
92 /* the ringbuffer object is not opaque so that OUT_RING() type stuff
93 * can be inlined. Note that users should not make assumptions about
94 * the size of this struct.
95 */
96 struct fd_ringbuffer {
97 uint32_t *cur, *end, *start;
98 const struct fd_ringbuffer_funcs *funcs;
99
100 // size or end coudl probably go away
101 int size;
102 int32_t refcnt;
103 enum fd_ringbuffer_flags flags;
104 };
105
106 /* Allocate a new long-lived state object, not associated with
107 * a submit:
108 */
109 struct fd_ringbuffer * fd_ringbuffer_new_object(struct fd_pipe *pipe,
110 uint32_t size);
111
112 struct fd_ringbuffer *fd_ringbuffer_ref(struct fd_ringbuffer *ring);
113 void fd_ringbuffer_del(struct fd_ringbuffer *ring);
114
115 void fd_ringbuffer_grow(struct fd_ringbuffer *ring, uint32_t ndwords);
116
117 static inline void fd_ringbuffer_emit(struct fd_ringbuffer *ring,
118 uint32_t data)
119 {
120 (*ring->cur++) = data;
121 }
122
123 struct fd_reloc {
124 struct fd_bo *bo;
125 #define FD_RELOC_READ 0x0001
126 #define FD_RELOC_WRITE 0x0002
127 uint32_t flags;
128 uint32_t offset;
129 uint32_t or;
130 int32_t shift;
131 uint32_t orhi; /* used for a5xx+ */
132 };
133
134 /* NOTE: relocs are 2 dwords on a5xx+ */
135
136 void fd_ringbuffer_reloc(struct fd_ringbuffer *ring, const struct fd_reloc *reloc);
137 uint32_t fd_ringbuffer_cmd_count(struct fd_ringbuffer *ring);
138 uint32_t fd_ringbuffer_emit_reloc_ring_full(struct fd_ringbuffer *ring,
139 struct fd_ringbuffer *target, uint32_t cmd_idx);
140
141 static inline uint32_t
142 offset_bytes(void *end, void *start)
143 {
144 return ((char *)end) - ((char *)start);
145 }
146
147 static inline uint32_t
148 fd_ringbuffer_size(struct fd_ringbuffer *ring)
149 {
150 /* only really needed for stateobj ringbuffers, and won't really
151 * do what you expect for growable rb's.. so lets just restrict
152 * this to stateobj's for now:
153 */
154 debug_assert(!(ring->flags & FD_RINGBUFFER_GROWABLE));
155 return offset_bytes(ring->cur, ring->start);
156 }
157
158
159 #endif /* FREEDRENO_RINGBUFFER_H_ */