freedreno: expose refcnt'ing on ringbuffers
[platform/upstream/libdrm.git] / freedreno / freedreno_ringbuffer.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5  *
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:
12  *
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
15  * Software.
16  *
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
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Rob Clark <robclark@freedesktop.org>
27  */
28
29 #include <assert.h>
30
31 #include "freedreno_drmif.h"
32 #include "freedreno_priv.h"
33 #include "freedreno_ringbuffer.h"
34
35 static struct fd_ringbuffer *
36 ringbuffer_new(struct fd_pipe *pipe, uint32_t size,
37                 enum fd_ringbuffer_flags flags)
38 {
39         struct fd_ringbuffer *ring;
40
41         ring = pipe->funcs->ringbuffer_new(pipe, size, flags);
42         if (!ring)
43                 return NULL;
44
45         ring->flags = flags;
46         ring->pipe = pipe;
47         ring->start = ring->funcs->hostptr(ring);
48         ring->end = &(ring->start[ring->size/4]);
49
50         ring->cur = ring->last_start = ring->start;
51
52         return ring;
53 }
54
55 drm_public struct fd_ringbuffer *
56 fd_ringbuffer_new(struct fd_pipe *pipe, uint32_t size)
57 {
58         return ringbuffer_new(pipe, size, 0);
59 }
60
61 drm_public struct fd_ringbuffer *
62 fd_ringbuffer_new_object(struct fd_pipe *pipe, uint32_t size)
63 {
64         /* we can't really support "growable" rb's in general for
65          * stateobj's since we need a single gpu addr (ie. can't
66          * do the trick of a chain of IB packets):
67          */
68         assert(size);
69         return ringbuffer_new(pipe, size, FD_RINGBUFFER_OBJECT);
70 }
71
72 drm_public void fd_ringbuffer_del(struct fd_ringbuffer *ring)
73 {
74         if (!atomic_dec_and_test(&ring->refcnt))
75                 return;
76
77         fd_ringbuffer_reset(ring);
78         ring->funcs->destroy(ring);
79 }
80
81 drm_public struct fd_ringbuffer *
82 fd_ringbuffer_ref(struct fd_ringbuffer *ring)
83 {
84         STATIC_ASSERT(sizeof(ring->refcnt) <= sizeof(ring->__pad));
85         atomic_inc(&ring->refcnt);
86         return ring;
87 }
88
89 /* ringbuffers which are IB targets should set the toplevel rb (ie.
90  * the IB source) as it's parent before emitting reloc's, to ensure
91  * the bookkeeping works out properly.
92  */
93 drm_public void fd_ringbuffer_set_parent(struct fd_ringbuffer *ring,
94                                          struct fd_ringbuffer *parent)
95 {
96         /* state objects should not be parented! */
97         assert(!(ring->flags & FD_RINGBUFFER_OBJECT));
98         ring->parent = parent;
99 }
100
101 drm_public void fd_ringbuffer_reset(struct fd_ringbuffer *ring)
102 {
103         uint32_t *start = ring->start;
104         if (ring->pipe->id == FD_PIPE_2D)
105                 start = &ring->start[0x140];
106         ring->cur = ring->last_start = start;
107         if (ring->funcs->reset)
108                 ring->funcs->reset(ring);
109 }
110
111 drm_public int fd_ringbuffer_flush(struct fd_ringbuffer *ring)
112 {
113         return ring->funcs->flush(ring, ring->last_start, -1, NULL);
114 }
115
116 drm_public int fd_ringbuffer_flush2(struct fd_ringbuffer *ring, int in_fence_fd,
117                 int *out_fence_fd)
118 {
119         return ring->funcs->flush(ring, ring->last_start, in_fence_fd, out_fence_fd);
120 }
121
122 drm_public void fd_ringbuffer_grow(struct fd_ringbuffer *ring, uint32_t ndwords)
123 {
124         assert(ring->funcs->grow);     /* unsupported on kgsl */
125
126         /* there is an upper bound on IB size, which appears to be 0x100000 */
127         if (ring->size < 0x100000)
128                 ring->size *= 2;
129
130         ring->funcs->grow(ring, ring->size);
131
132         ring->start = ring->funcs->hostptr(ring);
133         ring->end = &(ring->start[ring->size/4]);
134
135         ring->cur = ring->last_start = ring->start;
136 }
137
138 drm_public uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
139 {
140         return ring->last_timestamp;
141 }
142
143 drm_public void fd_ringbuffer_reloc(struct fd_ringbuffer *ring,
144                                     const struct fd_reloc *reloc)
145 {
146         assert(ring->pipe->gpu_id < 500);
147         ring->funcs->emit_reloc(ring, reloc);
148 }
149
150 drm_public void fd_ringbuffer_reloc2(struct fd_ringbuffer *ring,
151                                      const struct fd_reloc *reloc)
152 {
153         ring->funcs->emit_reloc(ring, reloc);
154 }
155
156 drm_public void fd_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
157                 struct fd_ringmarker *target, struct fd_ringmarker *end)
158 {
159         uint32_t submit_offset, size;
160
161         /* This function is deprecated and not supported on 64b devices: */
162         assert(ring->pipe->gpu_id < 500);
163         assert(target->ring == end->ring);
164
165         submit_offset = offset_bytes(target->cur, target->ring->start);
166         size = offset_bytes(end->cur, target->cur);
167
168         ring->funcs->emit_reloc_ring(ring, target->ring, 0, submit_offset, size);
169 }
170
171 drm_public uint32_t fd_ringbuffer_cmd_count(struct fd_ringbuffer *ring)
172 {
173         if (!ring->funcs->cmd_count)
174                 return 1;
175         return ring->funcs->cmd_count(ring);
176 }
177
178 drm_public uint32_t
179 fd_ringbuffer_emit_reloc_ring_full(struct fd_ringbuffer *ring,
180                 struct fd_ringbuffer *target, uint32_t cmd_idx)
181 {
182         uint32_t size = offset_bytes(target->cur, target->start);
183         return ring->funcs->emit_reloc_ring(ring, target, cmd_idx, 0, size);
184 }
185
186 drm_public uint32_t
187 fd_ringbuffer_size(struct fd_ringbuffer *ring)
188 {
189         /* only really needed for stateobj ringbuffers, and won't really
190          * do what you expect for growable rb's.. so lets just restrict
191          * this to stateobj's for now:
192          */
193         assert(ring->flags & FD_RINGBUFFER_OBJECT);
194         return offset_bytes(ring->cur, ring->start);
195 }
196
197 /*
198  * Deprecated ringmarker API:
199  */
200
201 drm_public struct fd_ringmarker * fd_ringmarker_new(struct fd_ringbuffer *ring)
202 {
203         struct fd_ringmarker *marker = NULL;
204
205         marker = calloc(1, sizeof(*marker));
206         if (!marker) {
207                 ERROR_MSG("allocation failed");
208                 return NULL;
209         }
210
211         marker->ring = ring;
212
213         marker->cur = marker->ring->cur;
214
215         return marker;
216 }
217
218 drm_public void fd_ringmarker_del(struct fd_ringmarker *marker)
219 {
220         free(marker);
221 }
222
223 drm_public void fd_ringmarker_mark(struct fd_ringmarker *marker)
224 {
225         marker->cur = marker->ring->cur;
226 }
227
228 drm_public uint32_t fd_ringmarker_dwords(struct fd_ringmarker *start,
229                                          struct fd_ringmarker *end)
230 {
231         return end->cur - start->cur;
232 }
233
234 drm_public int fd_ringmarker_flush(struct fd_ringmarker *marker)
235 {
236         struct fd_ringbuffer *ring = marker->ring;
237         return ring->funcs->flush(ring, marker->cur, -1, NULL);
238 }