xref: /aosp_15_r20/external/mesa3d/src/freedreno/drm/freedreno_pipe.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker  * Copyright © 2012-2018 Rob Clark <[email protected]>
3*61046927SAndroid Build Coastguard Worker  * SPDX-License-Identifier: MIT
4*61046927SAndroid Build Coastguard Worker  *
5*61046927SAndroid Build Coastguard Worker  * Authors:
6*61046927SAndroid Build Coastguard Worker  *    Rob Clark <[email protected]>
7*61046927SAndroid Build Coastguard Worker  */
8*61046927SAndroid Build Coastguard Worker 
9*61046927SAndroid Build Coastguard Worker #include "freedreno_drmif.h"
10*61046927SAndroid Build Coastguard Worker #include "freedreno_priv.h"
11*61046927SAndroid Build Coastguard Worker 
12*61046927SAndroid Build Coastguard Worker /**
13*61046927SAndroid Build Coastguard Worker  * priority of zero is highest priority, and higher numeric values are
14*61046927SAndroid Build Coastguard Worker  * lower priorities
15*61046927SAndroid Build Coastguard Worker  */
16*61046927SAndroid Build Coastguard Worker struct fd_pipe *
fd_pipe_new2(struct fd_device * dev,enum fd_pipe_id id,uint32_t prio)17*61046927SAndroid Build Coastguard Worker fd_pipe_new2(struct fd_device *dev, enum fd_pipe_id id, uint32_t prio)
18*61046927SAndroid Build Coastguard Worker {
19*61046927SAndroid Build Coastguard Worker    struct fd_pipe *pipe;
20*61046927SAndroid Build Coastguard Worker    uint64_t val;
21*61046927SAndroid Build Coastguard Worker 
22*61046927SAndroid Build Coastguard Worker    if (id > FD_PIPE_MAX) {
23*61046927SAndroid Build Coastguard Worker       ERROR_MSG("invalid pipe id: %d", id);
24*61046927SAndroid Build Coastguard Worker       return NULL;
25*61046927SAndroid Build Coastguard Worker    }
26*61046927SAndroid Build Coastguard Worker 
27*61046927SAndroid Build Coastguard Worker    if ((prio != 1) && (fd_device_version(dev) < FD_VERSION_SUBMIT_QUEUES)) {
28*61046927SAndroid Build Coastguard Worker       ERROR_MSG("invalid priority!");
29*61046927SAndroid Build Coastguard Worker       return NULL;
30*61046927SAndroid Build Coastguard Worker    }
31*61046927SAndroid Build Coastguard Worker 
32*61046927SAndroid Build Coastguard Worker    pipe = dev->funcs->pipe_new(dev, id, prio);
33*61046927SAndroid Build Coastguard Worker    if (!pipe) {
34*61046927SAndroid Build Coastguard Worker       ERROR_MSG("allocation failed");
35*61046927SAndroid Build Coastguard Worker       return NULL;
36*61046927SAndroid Build Coastguard Worker    }
37*61046927SAndroid Build Coastguard Worker 
38*61046927SAndroid Build Coastguard Worker    pipe->dev = dev;
39*61046927SAndroid Build Coastguard Worker    pipe->id = id;
40*61046927SAndroid Build Coastguard Worker    p_atomic_set(&pipe->refcnt, 1);
41*61046927SAndroid Build Coastguard Worker 
42*61046927SAndroid Build Coastguard Worker    fd_pipe_get_param(pipe, FD_GPU_ID, &val);
43*61046927SAndroid Build Coastguard Worker    pipe->dev_id.gpu_id = val;
44*61046927SAndroid Build Coastguard Worker 
45*61046927SAndroid Build Coastguard Worker    fd_pipe_get_param(pipe, FD_CHIP_ID, &val);
46*61046927SAndroid Build Coastguard Worker    pipe->dev_id.chip_id = val;
47*61046927SAndroid Build Coastguard Worker 
48*61046927SAndroid Build Coastguard Worker    pipe->is_64bit = fd_dev_64b(&pipe->dev_id);
49*61046927SAndroid Build Coastguard Worker 
50*61046927SAndroid Build Coastguard Worker    /* Use the _NOSYNC flags because we don't want the control_mem bo to hold
51*61046927SAndroid Build Coastguard Worker     * a reference to the ourself.  This also means that we won't be able
52*61046927SAndroid Build Coastguard Worker     * to determine if the buffer is idle which is needed by bo-cache.  But
53*61046927SAndroid Build Coastguard Worker     * pipe creation/destroy is not a high frequency event.
54*61046927SAndroid Build Coastguard Worker     */
55*61046927SAndroid Build Coastguard Worker    pipe->control_mem = fd_bo_new(dev, sizeof(*pipe->control),
56*61046927SAndroid Build Coastguard Worker                                  FD_BO_CACHED_COHERENT | _FD_BO_NOSYNC,
57*61046927SAndroid Build Coastguard Worker                                  "pipe-control");
58*61046927SAndroid Build Coastguard Worker    pipe->control = fd_bo_map(pipe->control_mem);
59*61046927SAndroid Build Coastguard Worker 
60*61046927SAndroid Build Coastguard Worker    /* We could be getting a bo from the bo-cache, make sure the fence value
61*61046927SAndroid Build Coastguard Worker     * is not garbage:
62*61046927SAndroid Build Coastguard Worker     */
63*61046927SAndroid Build Coastguard Worker    pipe->control->fence = 0;
64*61046927SAndroid Build Coastguard Worker    pipe->control_mem->bo_reuse = NO_CACHE;
65*61046927SAndroid Build Coastguard Worker 
66*61046927SAndroid Build Coastguard Worker    return pipe;
67*61046927SAndroid Build Coastguard Worker }
68*61046927SAndroid Build Coastguard Worker 
69*61046927SAndroid Build Coastguard Worker struct fd_pipe *
fd_pipe_new(struct fd_device * dev,enum fd_pipe_id id)70*61046927SAndroid Build Coastguard Worker fd_pipe_new(struct fd_device *dev, enum fd_pipe_id id)
71*61046927SAndroid Build Coastguard Worker {
72*61046927SAndroid Build Coastguard Worker    return fd_pipe_new2(dev, id, 1);
73*61046927SAndroid Build Coastguard Worker }
74*61046927SAndroid Build Coastguard Worker 
75*61046927SAndroid Build Coastguard Worker struct fd_pipe *
fd_pipe_ref(struct fd_pipe * pipe)76*61046927SAndroid Build Coastguard Worker fd_pipe_ref(struct fd_pipe *pipe)
77*61046927SAndroid Build Coastguard Worker {
78*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&fence_lock);
79*61046927SAndroid Build Coastguard Worker    fd_pipe_ref_locked(pipe);
80*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&fence_lock);
81*61046927SAndroid Build Coastguard Worker    return pipe;
82*61046927SAndroid Build Coastguard Worker }
83*61046927SAndroid Build Coastguard Worker 
84*61046927SAndroid Build Coastguard Worker struct fd_pipe *
fd_pipe_ref_locked(struct fd_pipe * pipe)85*61046927SAndroid Build Coastguard Worker fd_pipe_ref_locked(struct fd_pipe *pipe)
86*61046927SAndroid Build Coastguard Worker {
87*61046927SAndroid Build Coastguard Worker    simple_mtx_assert_locked(&fence_lock);
88*61046927SAndroid Build Coastguard Worker    pipe->refcnt++;
89*61046927SAndroid Build Coastguard Worker    return pipe;
90*61046927SAndroid Build Coastguard Worker }
91*61046927SAndroid Build Coastguard Worker 
92*61046927SAndroid Build Coastguard Worker void
fd_pipe_del(struct fd_pipe * pipe)93*61046927SAndroid Build Coastguard Worker fd_pipe_del(struct fd_pipe *pipe)
94*61046927SAndroid Build Coastguard Worker {
95*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&fence_lock);
96*61046927SAndroid Build Coastguard Worker    fd_pipe_del_locked(pipe);
97*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&fence_lock);
98*61046927SAndroid Build Coastguard Worker }
99*61046927SAndroid Build Coastguard Worker 
100*61046927SAndroid Build Coastguard Worker void
fd_pipe_del_locked(struct fd_pipe * pipe)101*61046927SAndroid Build Coastguard Worker fd_pipe_del_locked(struct fd_pipe *pipe)
102*61046927SAndroid Build Coastguard Worker {
103*61046927SAndroid Build Coastguard Worker    simple_mtx_assert_locked(&fence_lock);
104*61046927SAndroid Build Coastguard Worker    if (--pipe->refcnt)
105*61046927SAndroid Build Coastguard Worker       return;
106*61046927SAndroid Build Coastguard Worker 
107*61046927SAndroid Build Coastguard Worker    fd_bo_del(pipe->control_mem);
108*61046927SAndroid Build Coastguard Worker    pipe->funcs->destroy(pipe);
109*61046927SAndroid Build Coastguard Worker }
110*61046927SAndroid Build Coastguard Worker 
111*61046927SAndroid Build Coastguard Worker /**
112*61046927SAndroid Build Coastguard Worker  * Flush any unflushed deferred submits.  This is called at context-
113*61046927SAndroid Build Coastguard Worker  * destroy to make sure we don't leak unflushed submits.
114*61046927SAndroid Build Coastguard Worker  */
115*61046927SAndroid Build Coastguard Worker void
fd_pipe_purge(struct fd_pipe * pipe)116*61046927SAndroid Build Coastguard Worker fd_pipe_purge(struct fd_pipe *pipe)
117*61046927SAndroid Build Coastguard Worker {
118*61046927SAndroid Build Coastguard Worker    struct fd_device *dev = pipe->dev;
119*61046927SAndroid Build Coastguard Worker    struct fd_fence *unflushed_fence = NULL;
120*61046927SAndroid Build Coastguard Worker 
121*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&dev->submit_lock);
122*61046927SAndroid Build Coastguard Worker 
123*61046927SAndroid Build Coastguard Worker    /* We only queue up deferred submits for a single pipe at a time, so
124*61046927SAndroid Build Coastguard Worker     * if there is a deferred_submits_fence on the same pipe as us, we
125*61046927SAndroid Build Coastguard Worker     * know we have deferred_submits queued, which need to be flushed:
126*61046927SAndroid Build Coastguard Worker     */
127*61046927SAndroid Build Coastguard Worker    if (dev->deferred_submits_fence && dev->deferred_submits_fence->pipe == pipe) {
128*61046927SAndroid Build Coastguard Worker       unflushed_fence = fd_fence_ref(dev->deferred_submits_fence);
129*61046927SAndroid Build Coastguard Worker    }
130*61046927SAndroid Build Coastguard Worker 
131*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&dev->submit_lock);
132*61046927SAndroid Build Coastguard Worker 
133*61046927SAndroid Build Coastguard Worker    if (unflushed_fence) {
134*61046927SAndroid Build Coastguard Worker       fd_fence_flush(unflushed_fence);
135*61046927SAndroid Build Coastguard Worker       fd_fence_del(unflushed_fence);
136*61046927SAndroid Build Coastguard Worker    }
137*61046927SAndroid Build Coastguard Worker 
138*61046927SAndroid Build Coastguard Worker    if (pipe->funcs->finish)
139*61046927SAndroid Build Coastguard Worker       pipe->funcs->finish(pipe);
140*61046927SAndroid Build Coastguard Worker }
141*61046927SAndroid Build Coastguard Worker 
142*61046927SAndroid Build Coastguard Worker int
fd_pipe_get_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t * value)143*61046927SAndroid Build Coastguard Worker fd_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param, uint64_t *value)
144*61046927SAndroid Build Coastguard Worker {
145*61046927SAndroid Build Coastguard Worker    return pipe->funcs->get_param(pipe, param, value);
146*61046927SAndroid Build Coastguard Worker }
147*61046927SAndroid Build Coastguard Worker 
148*61046927SAndroid Build Coastguard Worker int
fd_pipe_set_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t value)149*61046927SAndroid Build Coastguard Worker fd_pipe_set_param(struct fd_pipe *pipe, enum fd_param_id param, uint64_t value)
150*61046927SAndroid Build Coastguard Worker {
151*61046927SAndroid Build Coastguard Worker    return pipe->funcs->set_param(pipe, param, value);
152*61046927SAndroid Build Coastguard Worker }
153*61046927SAndroid Build Coastguard Worker 
154*61046927SAndroid Build Coastguard Worker const struct fd_dev_id *
fd_pipe_dev_id(struct fd_pipe * pipe)155*61046927SAndroid Build Coastguard Worker fd_pipe_dev_id(struct fd_pipe *pipe)
156*61046927SAndroid Build Coastguard Worker {
157*61046927SAndroid Build Coastguard Worker    return &pipe->dev_id;
158*61046927SAndroid Build Coastguard Worker }
159*61046927SAndroid Build Coastguard Worker 
160*61046927SAndroid Build Coastguard Worker int
fd_pipe_wait(struct fd_pipe * pipe,const struct fd_fence * fence)161*61046927SAndroid Build Coastguard Worker fd_pipe_wait(struct fd_pipe *pipe, const struct fd_fence *fence)
162*61046927SAndroid Build Coastguard Worker {
163*61046927SAndroid Build Coastguard Worker    return fd_pipe_wait_timeout(pipe, fence, ~0);
164*61046927SAndroid Build Coastguard Worker }
165*61046927SAndroid Build Coastguard Worker 
166*61046927SAndroid Build Coastguard Worker int
fd_pipe_wait_timeout(struct fd_pipe * pipe,const struct fd_fence * fence,uint64_t timeout)167*61046927SAndroid Build Coastguard Worker fd_pipe_wait_timeout(struct fd_pipe *pipe, const struct fd_fence *fence,
168*61046927SAndroid Build Coastguard Worker                      uint64_t timeout)
169*61046927SAndroid Build Coastguard Worker {
170*61046927SAndroid Build Coastguard Worker    if (!fd_fence_after(fence->ufence, pipe->control->fence))
171*61046927SAndroid Build Coastguard Worker       return 0;
172*61046927SAndroid Build Coastguard Worker 
173*61046927SAndroid Build Coastguard Worker    if (!timeout)
174*61046927SAndroid Build Coastguard Worker       return -ETIMEDOUT;
175*61046927SAndroid Build Coastguard Worker 
176*61046927SAndroid Build Coastguard Worker    fd_pipe_flush(pipe, fence->ufence);
177*61046927SAndroid Build Coastguard Worker 
178*61046927SAndroid Build Coastguard Worker    return pipe->funcs->wait(pipe, fence, timeout);
179*61046927SAndroid Build Coastguard Worker }
180*61046927SAndroid Build Coastguard Worker 
181*61046927SAndroid Build Coastguard Worker uint32_t
fd_pipe_emit_fence(struct fd_pipe * pipe,struct fd_ringbuffer * ring)182*61046927SAndroid Build Coastguard Worker fd_pipe_emit_fence(struct fd_pipe *pipe, struct fd_ringbuffer *ring)
183*61046927SAndroid Build Coastguard Worker {
184*61046927SAndroid Build Coastguard Worker    uint32_t fence = ++pipe->last_fence;
185*61046927SAndroid Build Coastguard Worker    unsigned gen = fd_dev_gen(&pipe->dev_id);
186*61046927SAndroid Build Coastguard Worker 
187*61046927SAndroid Build Coastguard Worker    if (gen >= A7XX) {
188*61046927SAndroid Build Coastguard Worker       OUT_PKT7(ring, CP_EVENT_WRITE7, 4);
189*61046927SAndroid Build Coastguard Worker       OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_TS) |
190*61046927SAndroid Build Coastguard Worker                CP_EVENT_WRITE7_0_WRITE_SRC(EV_WRITE_USER_32B) |
191*61046927SAndroid Build Coastguard Worker                CP_EVENT_WRITE7_0_WRITE_DST(EV_DST_RAM) |
192*61046927SAndroid Build Coastguard Worker                CP_EVENT_WRITE7_0_WRITE_ENABLED);
193*61046927SAndroid Build Coastguard Worker       OUT_RELOC(ring, control_ptr(pipe, fence));   /* ADDR_LO/HI */
194*61046927SAndroid Build Coastguard Worker       OUT_RING(ring, fence);
195*61046927SAndroid Build Coastguard Worker    } else if (gen >= A5XX) {
196*61046927SAndroid Build Coastguard Worker       OUT_PKT7(ring, CP_EVENT_WRITE, 4);
197*61046927SAndroid Build Coastguard Worker       OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_TS));
198*61046927SAndroid Build Coastguard Worker       OUT_RELOC(ring, control_ptr(pipe, fence));   /* ADDR_LO/HI */
199*61046927SAndroid Build Coastguard Worker       OUT_RING(ring, fence);
200*61046927SAndroid Build Coastguard Worker    } else {
201*61046927SAndroid Build Coastguard Worker       OUT_PKT3(ring, CP_EVENT_WRITE, 3);
202*61046927SAndroid Build Coastguard Worker       OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_TS));
203*61046927SAndroid Build Coastguard Worker       OUT_RELOC(ring, control_ptr(pipe, fence));   /* ADDR */
204*61046927SAndroid Build Coastguard Worker       OUT_RING(ring, fence);
205*61046927SAndroid Build Coastguard Worker    }
206*61046927SAndroid Build Coastguard Worker 
207*61046927SAndroid Build Coastguard Worker    return fence;
208*61046927SAndroid Build Coastguard Worker }
209*61046927SAndroid Build Coastguard Worker 
210*61046927SAndroid Build Coastguard Worker struct fd_fence *
fd_fence_new(struct fd_pipe * pipe,bool use_fence_fd)211*61046927SAndroid Build Coastguard Worker fd_fence_new(struct fd_pipe *pipe, bool use_fence_fd)
212*61046927SAndroid Build Coastguard Worker {
213*61046927SAndroid Build Coastguard Worker    struct fd_fence *f = calloc(1, sizeof(*f));
214*61046927SAndroid Build Coastguard Worker 
215*61046927SAndroid Build Coastguard Worker    f->refcnt = 1;
216*61046927SAndroid Build Coastguard Worker    f->pipe = fd_pipe_ref(pipe);
217*61046927SAndroid Build Coastguard Worker    util_queue_fence_init(&f->ready);
218*61046927SAndroid Build Coastguard Worker    f->use_fence_fd = use_fence_fd;
219*61046927SAndroid Build Coastguard Worker    f->fence_fd = -1;
220*61046927SAndroid Build Coastguard Worker 
221*61046927SAndroid Build Coastguard Worker    return f;
222*61046927SAndroid Build Coastguard Worker }
223*61046927SAndroid Build Coastguard Worker 
224*61046927SAndroid Build Coastguard Worker struct fd_fence *
fd_fence_ref(struct fd_fence * f)225*61046927SAndroid Build Coastguard Worker fd_fence_ref(struct fd_fence *f)
226*61046927SAndroid Build Coastguard Worker {
227*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&fence_lock);
228*61046927SAndroid Build Coastguard Worker    fd_fence_ref_locked(f);
229*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&fence_lock);
230*61046927SAndroid Build Coastguard Worker 
231*61046927SAndroid Build Coastguard Worker    return f;
232*61046927SAndroid Build Coastguard Worker }
233*61046927SAndroid Build Coastguard Worker 
234*61046927SAndroid Build Coastguard Worker struct fd_fence *
fd_fence_ref_locked(struct fd_fence * f)235*61046927SAndroid Build Coastguard Worker fd_fence_ref_locked(struct fd_fence *f)
236*61046927SAndroid Build Coastguard Worker {
237*61046927SAndroid Build Coastguard Worker    simple_mtx_assert_locked(&fence_lock);
238*61046927SAndroid Build Coastguard Worker    f->refcnt++;
239*61046927SAndroid Build Coastguard Worker    return f;
240*61046927SAndroid Build Coastguard Worker }
241*61046927SAndroid Build Coastguard Worker 
242*61046927SAndroid Build Coastguard Worker void
fd_fence_del(struct fd_fence * f)243*61046927SAndroid Build Coastguard Worker fd_fence_del(struct fd_fence *f)
244*61046927SAndroid Build Coastguard Worker {
245*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&fence_lock);
246*61046927SAndroid Build Coastguard Worker    fd_fence_del_locked(f);
247*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&fence_lock);
248*61046927SAndroid Build Coastguard Worker }
249*61046927SAndroid Build Coastguard Worker 
250*61046927SAndroid Build Coastguard Worker void
fd_fence_del_locked(struct fd_fence * f)251*61046927SAndroid Build Coastguard Worker fd_fence_del_locked(struct fd_fence *f)
252*61046927SAndroid Build Coastguard Worker {
253*61046927SAndroid Build Coastguard Worker    simple_mtx_assert_locked(&fence_lock);
254*61046927SAndroid Build Coastguard Worker 
255*61046927SAndroid Build Coastguard Worker    if (--f->refcnt)
256*61046927SAndroid Build Coastguard Worker       return;
257*61046927SAndroid Build Coastguard Worker 
258*61046927SAndroid Build Coastguard Worker    fd_pipe_del_locked(f->pipe);
259*61046927SAndroid Build Coastguard Worker 
260*61046927SAndroid Build Coastguard Worker    if (f->use_fence_fd && (f->fence_fd != -1))
261*61046927SAndroid Build Coastguard Worker       close(f->fence_fd);
262*61046927SAndroid Build Coastguard Worker 
263*61046927SAndroid Build Coastguard Worker    free(f);
264*61046927SAndroid Build Coastguard Worker }
265*61046927SAndroid Build Coastguard Worker 
266*61046927SAndroid Build Coastguard Worker /**
267*61046927SAndroid Build Coastguard Worker  * Wait until corresponding submit is flushed to kernel
268*61046927SAndroid Build Coastguard Worker  */
269*61046927SAndroid Build Coastguard Worker void
fd_fence_flush(struct fd_fence * f)270*61046927SAndroid Build Coastguard Worker fd_fence_flush(struct fd_fence *f)
271*61046927SAndroid Build Coastguard Worker {
272*61046927SAndroid Build Coastguard Worker    MESA_TRACE_FUNC();
273*61046927SAndroid Build Coastguard Worker    /*
274*61046927SAndroid Build Coastguard Worker     * TODO we could simplify this to remove the flush_sync part of
275*61046927SAndroid Build Coastguard Worker     * fd_pipe_sp_flush() and just rely on the util_queue_fence_wait()
276*61046927SAndroid Build Coastguard Worker     */
277*61046927SAndroid Build Coastguard Worker    fd_pipe_flush(f->pipe, f->ufence);
278*61046927SAndroid Build Coastguard Worker    util_queue_fence_wait(&f->ready);
279*61046927SAndroid Build Coastguard Worker }
280*61046927SAndroid Build Coastguard Worker 
281*61046927SAndroid Build Coastguard Worker int
fd_fence_wait(struct fd_fence * f)282*61046927SAndroid Build Coastguard Worker fd_fence_wait(struct fd_fence *f)
283*61046927SAndroid Build Coastguard Worker {
284*61046927SAndroid Build Coastguard Worker    MESA_TRACE_FUNC();
285*61046927SAndroid Build Coastguard Worker    return fd_pipe_wait(f->pipe, f);
286*61046927SAndroid Build Coastguard Worker }
287