1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2019-2020 Collabora, Ltd.
3bf215546Sopenharmony_ci * Copyright (C) 2019 Alyssa Rosenzweig
4bf215546Sopenharmony_ci * Copyright (C) 2014-2017 Broadcom
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
14bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
15bf215546Sopenharmony_ci * Software.
16bf215546Sopenharmony_ci *
17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23bf215546Sopenharmony_ci * SOFTWARE.
24bf215546Sopenharmony_ci *
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <assert.h>
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "drm-uapi/panfrost_drm.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "pan_bo.h"
32bf215546Sopenharmony_ci#include "pan_context.h"
33bf215546Sopenharmony_ci#include "util/hash_table.h"
34bf215546Sopenharmony_ci#include "util/ralloc.h"
35bf215546Sopenharmony_ci#include "util/format/u_format.h"
36bf215546Sopenharmony_ci#include "util/u_pack_color.h"
37bf215546Sopenharmony_ci#include "util/rounding.h"
38bf215546Sopenharmony_ci#include "util/u_framebuffer.h"
39bf215546Sopenharmony_ci#include "pan_util.h"
40bf215546Sopenharmony_ci#include "decode.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci#define foreach_batch(ctx, idx) \
43bf215546Sopenharmony_ci        BITSET_FOREACH_SET(idx, ctx->batches.active, PAN_MAX_BATCHES)
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cistatic unsigned
46bf215546Sopenharmony_cipanfrost_batch_idx(struct panfrost_batch *batch)
47bf215546Sopenharmony_ci{
48bf215546Sopenharmony_ci        return batch - batch->ctx->batches.slots;
49bf215546Sopenharmony_ci}
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci/* Adds the BO backing surface to a batch if the surface is non-null */
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic void
54bf215546Sopenharmony_cipanfrost_batch_add_surface(struct panfrost_batch *batch, struct pipe_surface *surf)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci        if (surf) {
57bf215546Sopenharmony_ci                struct panfrost_resource *rsrc = pan_resource(surf->texture);
58bf215546Sopenharmony_ci                panfrost_batch_write_rsrc(batch, rsrc, PIPE_SHADER_FRAGMENT);
59bf215546Sopenharmony_ci        }
60bf215546Sopenharmony_ci}
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_cistatic void
63bf215546Sopenharmony_cipanfrost_batch_init(struct panfrost_context *ctx,
64bf215546Sopenharmony_ci                    const struct pipe_framebuffer_state *key,
65bf215546Sopenharmony_ci                    struct panfrost_batch *batch)
66bf215546Sopenharmony_ci{
67bf215546Sopenharmony_ci        struct pipe_screen *pscreen = ctx->base.screen;
68bf215546Sopenharmony_ci        struct panfrost_screen *screen = pan_screen(pscreen);
69bf215546Sopenharmony_ci        struct panfrost_device *dev = &screen->dev;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci        batch->ctx = ctx;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci        batch->seqnum = ++ctx->batches.seqnum;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci        util_dynarray_init(&batch->bos, NULL);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci        batch->minx = batch->miny = ~0;
78bf215546Sopenharmony_ci        batch->maxx = batch->maxy = 0;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci        util_copy_framebuffer_state(&batch->key, key);
81bf215546Sopenharmony_ci        batch->resources =_mesa_set_create(NULL, _mesa_hash_pointer,
82bf215546Sopenharmony_ci                                          _mesa_key_pointer_equal);
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci        /* Preallocate the main pool, since every batch has at least one job
85bf215546Sopenharmony_ci         * structure so it will be used */
86bf215546Sopenharmony_ci        panfrost_pool_init(&batch->pool, NULL, dev, 0, 65536, "Batch pool", true, true);
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci        /* Don't preallocate the invisible pool, since not every batch will use
89bf215546Sopenharmony_ci         * the pre-allocation, particularly if the varyings are larger than the
90bf215546Sopenharmony_ci         * preallocation and a reallocation is needed after anyway. */
91bf215546Sopenharmony_ci        panfrost_pool_init(&batch->invisible_pool, NULL, dev,
92bf215546Sopenharmony_ci                        PAN_BO_INVISIBLE, 65536, "Varyings", false, true);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci        for (unsigned i = 0; i < batch->key.nr_cbufs; ++i)
95bf215546Sopenharmony_ci                panfrost_batch_add_surface(batch, batch->key.cbufs[i]);
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci        panfrost_batch_add_surface(batch, batch->key.zsbuf);
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci        screen->vtbl.init_batch(batch);
100bf215546Sopenharmony_ci}
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_cistatic void
103bf215546Sopenharmony_cipanfrost_batch_cleanup(struct panfrost_context *ctx, struct panfrost_batch *batch)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci        struct panfrost_device *dev = pan_device(ctx->base.screen);
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci        assert(batch->seqnum);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci        if (ctx->batch == batch)
110bf215546Sopenharmony_ci                ctx->batch = NULL;
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci        unsigned batch_idx = panfrost_batch_idx(batch);
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci        pan_bo_access *flags = util_dynarray_begin(&batch->bos);
115bf215546Sopenharmony_ci        unsigned end_bo = util_dynarray_num_elements(&batch->bos, pan_bo_access);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci        for (int i = 0; i < end_bo; ++i) {
118bf215546Sopenharmony_ci                if (!flags[i])
119bf215546Sopenharmony_ci                        continue;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci                struct panfrost_bo *bo = pan_lookup_bo(dev, i);
122bf215546Sopenharmony_ci                panfrost_bo_unreference(bo);
123bf215546Sopenharmony_ci        }
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci        set_foreach(batch->resources, entry) {
126bf215546Sopenharmony_ci                struct panfrost_resource *rsrc = (void *) entry->key;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci                if (_mesa_hash_table_search(ctx->writers, rsrc)) {
129bf215546Sopenharmony_ci                        _mesa_hash_table_remove_key(ctx->writers, rsrc);
130bf215546Sopenharmony_ci                        rsrc->track.nr_writers--;
131bf215546Sopenharmony_ci                }
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci                rsrc->track.nr_users--;
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci                pipe_resource_reference((struct pipe_resource **) &rsrc, NULL);
136bf215546Sopenharmony_ci        }
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci        _mesa_set_destroy(batch->resources, NULL);
139bf215546Sopenharmony_ci        panfrost_pool_cleanup(&batch->pool);
140bf215546Sopenharmony_ci        panfrost_pool_cleanup(&batch->invisible_pool);
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci        util_unreference_framebuffer_state(&batch->key);
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci        util_dynarray_fini(&batch->bos);
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci        memset(batch, 0, sizeof(*batch));
147bf215546Sopenharmony_ci        BITSET_CLEAR(ctx->batches.active, batch_idx);
148bf215546Sopenharmony_ci}
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_cistatic void
151bf215546Sopenharmony_cipanfrost_batch_submit(struct panfrost_context *ctx,
152bf215546Sopenharmony_ci                      struct panfrost_batch *batch);
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_cistatic struct panfrost_batch *
155bf215546Sopenharmony_cipanfrost_get_batch(struct panfrost_context *ctx,
156bf215546Sopenharmony_ci                   const struct pipe_framebuffer_state *key)
157bf215546Sopenharmony_ci{
158bf215546Sopenharmony_ci        struct panfrost_batch *batch = NULL;
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci        for (unsigned i = 0; i < PAN_MAX_BATCHES; i++) {
161bf215546Sopenharmony_ci                if (ctx->batches.slots[i].seqnum &&
162bf215546Sopenharmony_ci                    util_framebuffer_state_equal(&ctx->batches.slots[i].key, key)) {
163bf215546Sopenharmony_ci                        /* We found a match, increase the seqnum for the LRU
164bf215546Sopenharmony_ci                         * eviction logic.
165bf215546Sopenharmony_ci                         */
166bf215546Sopenharmony_ci                        ctx->batches.slots[i].seqnum = ++ctx->batches.seqnum;
167bf215546Sopenharmony_ci                        return &ctx->batches.slots[i];
168bf215546Sopenharmony_ci                }
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci                if (!batch || batch->seqnum > ctx->batches.slots[i].seqnum)
171bf215546Sopenharmony_ci                        batch = &ctx->batches.slots[i];
172bf215546Sopenharmony_ci        }
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci        assert(batch);
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci        /* The selected slot is used, we need to flush the batch */
177bf215546Sopenharmony_ci        if (batch->seqnum)
178bf215546Sopenharmony_ci                panfrost_batch_submit(ctx, batch);
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci        panfrost_batch_init(ctx, key, batch);
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci        unsigned batch_idx = panfrost_batch_idx(batch);
183bf215546Sopenharmony_ci        BITSET_SET(ctx->batches.active, batch_idx);
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci        return batch;
186bf215546Sopenharmony_ci}
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci/* Get the job corresponding to the FBO we're currently rendering into */
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_cistruct panfrost_batch *
191bf215546Sopenharmony_cipanfrost_get_batch_for_fbo(struct panfrost_context *ctx)
192bf215546Sopenharmony_ci{
193bf215546Sopenharmony_ci        /* If we already began rendering, use that */
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci        if (ctx->batch) {
196bf215546Sopenharmony_ci                assert(util_framebuffer_state_equal(&ctx->batch->key,
197bf215546Sopenharmony_ci                                                    &ctx->pipe_framebuffer));
198bf215546Sopenharmony_ci                return ctx->batch;
199bf215546Sopenharmony_ci        }
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci        /* If not, look up the job */
202bf215546Sopenharmony_ci        struct panfrost_batch *batch = panfrost_get_batch(ctx,
203bf215546Sopenharmony_ci                                                          &ctx->pipe_framebuffer);
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci        /* Set this job as the current FBO job. Will be reset when updating the
206bf215546Sopenharmony_ci         * FB state and when submitting or releasing a job.
207bf215546Sopenharmony_ci         */
208bf215546Sopenharmony_ci        ctx->batch = batch;
209bf215546Sopenharmony_ci        panfrost_dirty_state_all(ctx);
210bf215546Sopenharmony_ci        return batch;
211bf215546Sopenharmony_ci}
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_cistruct panfrost_batch *
214bf215546Sopenharmony_cipanfrost_get_fresh_batch_for_fbo(struct panfrost_context *ctx, const char *reason)
215bf215546Sopenharmony_ci{
216bf215546Sopenharmony_ci        struct panfrost_batch *batch;
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci        batch = panfrost_get_batch(ctx, &ctx->pipe_framebuffer);
219bf215546Sopenharmony_ci        panfrost_dirty_state_all(ctx);
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci        /* We only need to submit and get a fresh batch if there is no
222bf215546Sopenharmony_ci         * draw/clear queued. Otherwise we may reuse the batch. */
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci        if (batch->scoreboard.first_job) {
225bf215546Sopenharmony_ci                perf_debug_ctx(ctx, "Flushing the current FBO due to: %s", reason);
226bf215546Sopenharmony_ci                panfrost_batch_submit(ctx, batch);
227bf215546Sopenharmony_ci                batch = panfrost_get_batch(ctx, &ctx->pipe_framebuffer);
228bf215546Sopenharmony_ci        }
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci        ctx->batch = batch;
231bf215546Sopenharmony_ci        return batch;
232bf215546Sopenharmony_ci}
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_cistatic void
235bf215546Sopenharmony_cipanfrost_batch_update_access(struct panfrost_batch *batch,
236bf215546Sopenharmony_ci                             struct panfrost_resource *rsrc, bool writes)
237bf215546Sopenharmony_ci{
238bf215546Sopenharmony_ci        struct panfrost_context *ctx = batch->ctx;
239bf215546Sopenharmony_ci        uint32_t batch_idx = panfrost_batch_idx(batch);
240bf215546Sopenharmony_ci        struct hash_entry *entry = _mesa_hash_table_search(ctx->writers, rsrc);
241bf215546Sopenharmony_ci        struct panfrost_batch *writer = entry ? entry->data : NULL;
242bf215546Sopenharmony_ci        bool found = false;
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_ci        _mesa_set_search_or_add(batch->resources, rsrc, &found);
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci        if (!found) {
247bf215546Sopenharmony_ci                /* Cache number of batches accessing a resource */
248bf215546Sopenharmony_ci                rsrc->track.nr_users++;
249bf215546Sopenharmony_ci
250bf215546Sopenharmony_ci                /* Reference the resource on the batch */
251bf215546Sopenharmony_ci                pipe_reference(NULL, &rsrc->base.reference);
252bf215546Sopenharmony_ci        }
253bf215546Sopenharmony_ci
254bf215546Sopenharmony_ci        /* Flush users if required */
255bf215546Sopenharmony_ci        if (writes || ((writer != NULL) && (writer != batch))) {
256bf215546Sopenharmony_ci                unsigned i;
257bf215546Sopenharmony_ci                foreach_batch(ctx, i) {
258bf215546Sopenharmony_ci                        struct panfrost_batch *batch = &ctx->batches.slots[i];
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci                        /* Skip the entry if this our batch. */
261bf215546Sopenharmony_ci                        if (i == batch_idx)
262bf215546Sopenharmony_ci                                continue;
263bf215546Sopenharmony_ci
264bf215546Sopenharmony_ci                        /* Submit if it's a user */
265bf215546Sopenharmony_ci                        if (_mesa_set_search(batch->resources, rsrc))
266bf215546Sopenharmony_ci                                panfrost_batch_submit(ctx, batch);
267bf215546Sopenharmony_ci                }
268bf215546Sopenharmony_ci        }
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci        if (writes) {
271bf215546Sopenharmony_ci                _mesa_hash_table_insert(ctx->writers, rsrc, batch);
272bf215546Sopenharmony_ci                rsrc->track.nr_writers++;
273bf215546Sopenharmony_ci        }
274bf215546Sopenharmony_ci}
275bf215546Sopenharmony_ci
276bf215546Sopenharmony_cistatic pan_bo_access *
277bf215546Sopenharmony_cipanfrost_batch_get_bo_access(struct panfrost_batch *batch, unsigned handle)
278bf215546Sopenharmony_ci{
279bf215546Sopenharmony_ci        unsigned size = util_dynarray_num_elements(&batch->bos, pan_bo_access);
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci        if (handle >= size) {
282bf215546Sopenharmony_ci                unsigned grow = handle + 1 - size;
283bf215546Sopenharmony_ci
284bf215546Sopenharmony_ci                memset(util_dynarray_grow(&batch->bos, pan_bo_access, grow),
285bf215546Sopenharmony_ci                       0, grow * sizeof(pan_bo_access));
286bf215546Sopenharmony_ci        }
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci        return util_dynarray_element(&batch->bos, pan_bo_access, handle);
289bf215546Sopenharmony_ci}
290bf215546Sopenharmony_ci
291bf215546Sopenharmony_cistatic void
292bf215546Sopenharmony_cipanfrost_batch_add_bo_old(struct panfrost_batch *batch,
293bf215546Sopenharmony_ci                struct panfrost_bo *bo, uint32_t flags)
294bf215546Sopenharmony_ci{
295bf215546Sopenharmony_ci        if (!bo)
296bf215546Sopenharmony_ci                return;
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci        pan_bo_access *entry =
299bf215546Sopenharmony_ci                panfrost_batch_get_bo_access(batch, bo->gem_handle);
300bf215546Sopenharmony_ci        pan_bo_access old_flags = *entry;
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci        if (!old_flags) {
303bf215546Sopenharmony_ci                batch->num_bos++;
304bf215546Sopenharmony_ci                panfrost_bo_reference(bo);
305bf215546Sopenharmony_ci        }
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_ci        if (old_flags == flags)
308bf215546Sopenharmony_ci                return;
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci        flags |= old_flags;
311bf215546Sopenharmony_ci        *entry = flags;
312bf215546Sopenharmony_ci}
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_cistatic uint32_t
315bf215546Sopenharmony_cipanfrost_access_for_stage(enum pipe_shader_type stage)
316bf215546Sopenharmony_ci{
317bf215546Sopenharmony_ci        return (stage == PIPE_SHADER_FRAGMENT) ?
318bf215546Sopenharmony_ci                PAN_BO_ACCESS_FRAGMENT : PAN_BO_ACCESS_VERTEX_TILER;
319bf215546Sopenharmony_ci}
320bf215546Sopenharmony_ci
321bf215546Sopenharmony_civoid
322bf215546Sopenharmony_cipanfrost_batch_add_bo(struct panfrost_batch *batch,
323bf215546Sopenharmony_ci                struct panfrost_bo *bo, enum pipe_shader_type stage)
324bf215546Sopenharmony_ci{
325bf215546Sopenharmony_ci        panfrost_batch_add_bo_old(batch, bo, PAN_BO_ACCESS_READ |
326bf215546Sopenharmony_ci                        panfrost_access_for_stage(stage));
327bf215546Sopenharmony_ci}
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_civoid
330bf215546Sopenharmony_cipanfrost_batch_read_rsrc(struct panfrost_batch *batch,
331bf215546Sopenharmony_ci                         struct panfrost_resource *rsrc,
332bf215546Sopenharmony_ci                         enum pipe_shader_type stage)
333bf215546Sopenharmony_ci{
334bf215546Sopenharmony_ci        uint32_t access = PAN_BO_ACCESS_READ |
335bf215546Sopenharmony_ci                panfrost_access_for_stage(stage);
336bf215546Sopenharmony_ci
337bf215546Sopenharmony_ci        panfrost_batch_add_bo_old(batch, rsrc->image.data.bo, access);
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci        if (rsrc->image.crc.bo)
340bf215546Sopenharmony_ci                panfrost_batch_add_bo_old(batch, rsrc->image.crc.bo, access);
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci        if (rsrc->separate_stencil)
343bf215546Sopenharmony_ci                panfrost_batch_add_bo_old(batch, rsrc->separate_stencil->image.data.bo, access);
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_ci        panfrost_batch_update_access(batch, rsrc, false);
346bf215546Sopenharmony_ci}
347bf215546Sopenharmony_ci
348bf215546Sopenharmony_civoid
349bf215546Sopenharmony_cipanfrost_batch_write_rsrc(struct panfrost_batch *batch,
350bf215546Sopenharmony_ci                         struct panfrost_resource *rsrc,
351bf215546Sopenharmony_ci                         enum pipe_shader_type stage)
352bf215546Sopenharmony_ci{
353bf215546Sopenharmony_ci        uint32_t access = PAN_BO_ACCESS_WRITE |
354bf215546Sopenharmony_ci                panfrost_access_for_stage(stage);
355bf215546Sopenharmony_ci
356bf215546Sopenharmony_ci        panfrost_batch_add_bo_old(batch, rsrc->image.data.bo, access);
357bf215546Sopenharmony_ci
358bf215546Sopenharmony_ci        if (rsrc->image.crc.bo)
359bf215546Sopenharmony_ci                panfrost_batch_add_bo_old(batch, rsrc->image.crc.bo, access);
360bf215546Sopenharmony_ci
361bf215546Sopenharmony_ci        if (rsrc->separate_stencil)
362bf215546Sopenharmony_ci                panfrost_batch_add_bo_old(batch, rsrc->separate_stencil->image.data.bo, access);
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_ci        panfrost_batch_update_access(batch, rsrc, true);
365bf215546Sopenharmony_ci}
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_civoid
368bf215546Sopenharmony_cipanfrost_resource_swap_bo(struct panfrost_context *ctx,
369bf215546Sopenharmony_ci                          struct panfrost_resource *rsrc,
370bf215546Sopenharmony_ci                          struct panfrost_bo *newbo)
371bf215546Sopenharmony_ci{
372bf215546Sopenharmony_ci        /* Any batch writing this resource is writing to the old BO, not the
373bf215546Sopenharmony_ci         * new BO. After swapping the resource's backing BO, there will be no
374bf215546Sopenharmony_ci         * writers of the updated resource. Existing writers still hold a
375bf215546Sopenharmony_ci         * reference to the old BO for reference counting.
376bf215546Sopenharmony_ci         */
377bf215546Sopenharmony_ci        struct hash_entry *writer = _mesa_hash_table_search(ctx->writers, rsrc);
378bf215546Sopenharmony_ci        if (writer) {
379bf215546Sopenharmony_ci                _mesa_hash_table_remove(ctx->writers, writer);
380bf215546Sopenharmony_ci                rsrc->track.nr_writers--;
381bf215546Sopenharmony_ci        }
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ci        /* Likewise, any batch reading this resource is reading the old BO, and
384bf215546Sopenharmony_ci         * after swapping will not be reading this resource.
385bf215546Sopenharmony_ci         */
386bf215546Sopenharmony_ci        unsigned i;
387bf215546Sopenharmony_ci        foreach_batch(ctx, i) {
388bf215546Sopenharmony_ci                struct panfrost_batch *batch = &ctx->batches.slots[i];
389bf215546Sopenharmony_ci                struct set_entry *ent = _mesa_set_search(batch->resources, rsrc);
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci                if (!ent)
392bf215546Sopenharmony_ci                        continue;
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ci                _mesa_set_remove(batch->resources, ent);
395bf215546Sopenharmony_ci                rsrc->track.nr_users--;
396bf215546Sopenharmony_ci        }
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_ci        /* Swap the pointers, dropping a reference to the old BO which is no
399bf215546Sopenharmony_ci         * long referenced from the resource
400bf215546Sopenharmony_ci         */
401bf215546Sopenharmony_ci        panfrost_bo_unreference(rsrc->image.data.bo);
402bf215546Sopenharmony_ci        rsrc->image.data.bo = newbo;
403bf215546Sopenharmony_ci}
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_cistruct panfrost_bo *
406bf215546Sopenharmony_cipanfrost_batch_create_bo(struct panfrost_batch *batch, size_t size,
407bf215546Sopenharmony_ci                         uint32_t create_flags, enum pipe_shader_type stage,
408bf215546Sopenharmony_ci                         const char *label)
409bf215546Sopenharmony_ci{
410bf215546Sopenharmony_ci        struct panfrost_bo *bo;
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ci        bo = panfrost_bo_create(pan_device(batch->ctx->base.screen), size,
413bf215546Sopenharmony_ci                                create_flags, label);
414bf215546Sopenharmony_ci        panfrost_batch_add_bo(batch, bo, stage);
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci        /* panfrost_batch_add_bo() has retained a reference and
417bf215546Sopenharmony_ci         * panfrost_bo_create() initialize the refcnt to 1, so let's
418bf215546Sopenharmony_ci         * unreference the BO here so it gets released when the batch is
419bf215546Sopenharmony_ci         * destroyed (unless it's retained by someone else in the meantime).
420bf215546Sopenharmony_ci         */
421bf215546Sopenharmony_ci        panfrost_bo_unreference(bo);
422bf215546Sopenharmony_ci        return bo;
423bf215546Sopenharmony_ci}
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_cistruct panfrost_bo *
426bf215546Sopenharmony_cipanfrost_batch_get_scratchpad(struct panfrost_batch *batch,
427bf215546Sopenharmony_ci                unsigned size_per_thread,
428bf215546Sopenharmony_ci                unsigned thread_tls_alloc,
429bf215546Sopenharmony_ci                unsigned core_id_range)
430bf215546Sopenharmony_ci{
431bf215546Sopenharmony_ci        unsigned size = panfrost_get_total_stack_size(size_per_thread,
432bf215546Sopenharmony_ci                        thread_tls_alloc,
433bf215546Sopenharmony_ci                        core_id_range);
434bf215546Sopenharmony_ci
435bf215546Sopenharmony_ci        if (batch->scratchpad) {
436bf215546Sopenharmony_ci                assert(batch->scratchpad->size >= size);
437bf215546Sopenharmony_ci        } else {
438bf215546Sopenharmony_ci                batch->scratchpad = panfrost_batch_create_bo(batch, size,
439bf215546Sopenharmony_ci                                             PAN_BO_INVISIBLE,
440bf215546Sopenharmony_ci                                             PIPE_SHADER_VERTEX,
441bf215546Sopenharmony_ci                                             "Thread local storage");
442bf215546Sopenharmony_ci
443bf215546Sopenharmony_ci                panfrost_batch_add_bo(batch, batch->scratchpad,
444bf215546Sopenharmony_ci                                PIPE_SHADER_FRAGMENT);
445bf215546Sopenharmony_ci        }
446bf215546Sopenharmony_ci
447bf215546Sopenharmony_ci        return batch->scratchpad;
448bf215546Sopenharmony_ci}
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_cistruct panfrost_bo *
451bf215546Sopenharmony_cipanfrost_batch_get_shared_memory(struct panfrost_batch *batch,
452bf215546Sopenharmony_ci                unsigned size,
453bf215546Sopenharmony_ci                unsigned workgroup_count)
454bf215546Sopenharmony_ci{
455bf215546Sopenharmony_ci        if (batch->shared_memory) {
456bf215546Sopenharmony_ci                assert(batch->shared_memory->size >= size);
457bf215546Sopenharmony_ci        } else {
458bf215546Sopenharmony_ci                batch->shared_memory = panfrost_batch_create_bo(batch, size,
459bf215546Sopenharmony_ci                                             PAN_BO_INVISIBLE,
460bf215546Sopenharmony_ci                                             PIPE_SHADER_VERTEX,
461bf215546Sopenharmony_ci                                             "Workgroup shared memory");
462bf215546Sopenharmony_ci        }
463bf215546Sopenharmony_ci
464bf215546Sopenharmony_ci        return batch->shared_memory;
465bf215546Sopenharmony_ci}
466bf215546Sopenharmony_ci
467bf215546Sopenharmony_cistatic void
468bf215546Sopenharmony_cipanfrost_batch_to_fb_info(const struct panfrost_batch *batch,
469bf215546Sopenharmony_ci                          struct pan_fb_info *fb,
470bf215546Sopenharmony_ci                          struct pan_image_view *rts,
471bf215546Sopenharmony_ci                          struct pan_image_view *zs,
472bf215546Sopenharmony_ci                          struct pan_image_view *s,
473bf215546Sopenharmony_ci                          bool reserve)
474bf215546Sopenharmony_ci{
475bf215546Sopenharmony_ci        memset(fb, 0, sizeof(*fb));
476bf215546Sopenharmony_ci        memset(rts, 0, sizeof(*rts) * 8);
477bf215546Sopenharmony_ci        memset(zs, 0, sizeof(*zs));
478bf215546Sopenharmony_ci        memset(s, 0, sizeof(*s));
479bf215546Sopenharmony_ci
480bf215546Sopenharmony_ci        fb->width = batch->key.width;
481bf215546Sopenharmony_ci        fb->height = batch->key.height;
482bf215546Sopenharmony_ci        fb->extent.minx = batch->minx;
483bf215546Sopenharmony_ci        fb->extent.miny = batch->miny;
484bf215546Sopenharmony_ci        fb->extent.maxx = batch->maxx - 1;
485bf215546Sopenharmony_ci        fb->extent.maxy = batch->maxy - 1;
486bf215546Sopenharmony_ci        fb->nr_samples = util_framebuffer_get_num_samples(&batch->key);
487bf215546Sopenharmony_ci        fb->rt_count = batch->key.nr_cbufs;
488bf215546Sopenharmony_ci        fb->sprite_coord_origin = pan_tristate_get(batch->sprite_coord_origin);
489bf215546Sopenharmony_ci        fb->first_provoking_vertex = pan_tristate_get(batch->first_provoking_vertex);
490bf215546Sopenharmony_ci
491bf215546Sopenharmony_ci        static const unsigned char id_swz[] = {
492bf215546Sopenharmony_ci                PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y, PIPE_SWIZZLE_Z, PIPE_SWIZZLE_W,
493bf215546Sopenharmony_ci        };
494bf215546Sopenharmony_ci
495bf215546Sopenharmony_ci        for (unsigned i = 0; i < fb->rt_count; i++) {
496bf215546Sopenharmony_ci                struct pipe_surface *surf = batch->key.cbufs[i];
497bf215546Sopenharmony_ci
498bf215546Sopenharmony_ci                if (!surf)
499bf215546Sopenharmony_ci                        continue;
500bf215546Sopenharmony_ci
501bf215546Sopenharmony_ci                struct panfrost_resource *prsrc = pan_resource(surf->texture);
502bf215546Sopenharmony_ci                unsigned mask = PIPE_CLEAR_COLOR0 << i;
503bf215546Sopenharmony_ci
504bf215546Sopenharmony_ci                if (batch->clear & mask) {
505bf215546Sopenharmony_ci                        fb->rts[i].clear = true;
506bf215546Sopenharmony_ci                        memcpy(fb->rts[i].clear_value, batch->clear_color[i],
507bf215546Sopenharmony_ci                               sizeof((fb->rts[i].clear_value)));
508bf215546Sopenharmony_ci                }
509bf215546Sopenharmony_ci
510bf215546Sopenharmony_ci                fb->rts[i].discard = !reserve && !(batch->resolve & mask);
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci                rts[i].format = surf->format;
513bf215546Sopenharmony_ci                rts[i].dim = MALI_TEXTURE_DIMENSION_2D;
514bf215546Sopenharmony_ci                rts[i].last_level = rts[i].first_level = surf->u.tex.level;
515bf215546Sopenharmony_ci                rts[i].first_layer = surf->u.tex.first_layer;
516bf215546Sopenharmony_ci                rts[i].last_layer = surf->u.tex.last_layer;
517bf215546Sopenharmony_ci                rts[i].image = &prsrc->image;
518bf215546Sopenharmony_ci                rts[i].nr_samples = surf->nr_samples ? : MAX2(surf->texture->nr_samples, 1);
519bf215546Sopenharmony_ci                memcpy(rts[i].swizzle, id_swz, sizeof(rts[i].swizzle));
520bf215546Sopenharmony_ci                fb->rts[i].crc_valid = &prsrc->valid.crc;
521bf215546Sopenharmony_ci                fb->rts[i].view = &rts[i];
522bf215546Sopenharmony_ci
523bf215546Sopenharmony_ci                /* Preload if the RT is read or updated */
524bf215546Sopenharmony_ci                if (!(batch->clear & mask) &&
525bf215546Sopenharmony_ci                    ((batch->read & mask) ||
526bf215546Sopenharmony_ci                     ((batch->draws & mask) &&
527bf215546Sopenharmony_ci                      BITSET_TEST(prsrc->valid.data, fb->rts[i].view->first_level))))
528bf215546Sopenharmony_ci                        fb->rts[i].preload = true;
529bf215546Sopenharmony_ci
530bf215546Sopenharmony_ci        }
531bf215546Sopenharmony_ci
532bf215546Sopenharmony_ci        const struct pan_image_view *s_view = NULL, *z_view = NULL;
533bf215546Sopenharmony_ci        struct panfrost_resource *z_rsrc = NULL, *s_rsrc = NULL;
534bf215546Sopenharmony_ci
535bf215546Sopenharmony_ci        if (batch->key.zsbuf) {
536bf215546Sopenharmony_ci                struct pipe_surface *surf = batch->key.zsbuf;
537bf215546Sopenharmony_ci                z_rsrc = pan_resource(surf->texture);
538bf215546Sopenharmony_ci
539bf215546Sopenharmony_ci                zs->format = surf->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ?
540bf215546Sopenharmony_ci                             PIPE_FORMAT_Z32_FLOAT : surf->format;
541bf215546Sopenharmony_ci                zs->dim = MALI_TEXTURE_DIMENSION_2D;
542bf215546Sopenharmony_ci                zs->last_level = zs->first_level = surf->u.tex.level;
543bf215546Sopenharmony_ci                zs->first_layer = surf->u.tex.first_layer;
544bf215546Sopenharmony_ci                zs->last_layer = surf->u.tex.last_layer;
545bf215546Sopenharmony_ci                zs->image = &z_rsrc->image;
546bf215546Sopenharmony_ci                zs->nr_samples = surf->nr_samples ? : MAX2(surf->texture->nr_samples, 1);
547bf215546Sopenharmony_ci                memcpy(zs->swizzle, id_swz, sizeof(zs->swizzle));
548bf215546Sopenharmony_ci                fb->zs.view.zs = zs;
549bf215546Sopenharmony_ci                z_view = zs;
550bf215546Sopenharmony_ci                if (util_format_is_depth_and_stencil(zs->format)) {
551bf215546Sopenharmony_ci                        s_view = zs;
552bf215546Sopenharmony_ci                        s_rsrc = z_rsrc;
553bf215546Sopenharmony_ci                }
554bf215546Sopenharmony_ci
555bf215546Sopenharmony_ci                if (z_rsrc->separate_stencil) {
556bf215546Sopenharmony_ci                        s_rsrc = z_rsrc->separate_stencil;
557bf215546Sopenharmony_ci                        s->format = PIPE_FORMAT_S8_UINT;
558bf215546Sopenharmony_ci                        s->dim = MALI_TEXTURE_DIMENSION_2D;
559bf215546Sopenharmony_ci                        s->last_level = s->first_level = surf->u.tex.level;
560bf215546Sopenharmony_ci                        s->first_layer = surf->u.tex.first_layer;
561bf215546Sopenharmony_ci                        s->last_layer = surf->u.tex.last_layer;
562bf215546Sopenharmony_ci                        s->image = &s_rsrc->image;
563bf215546Sopenharmony_ci                        s->nr_samples = surf->nr_samples ? : MAX2(surf->texture->nr_samples, 1);
564bf215546Sopenharmony_ci                        memcpy(s->swizzle, id_swz, sizeof(s->swizzle));
565bf215546Sopenharmony_ci                        fb->zs.view.s = s;
566bf215546Sopenharmony_ci                        s_view = s;
567bf215546Sopenharmony_ci                }
568bf215546Sopenharmony_ci        }
569bf215546Sopenharmony_ci
570bf215546Sopenharmony_ci        if (batch->clear & PIPE_CLEAR_DEPTH) {
571bf215546Sopenharmony_ci                fb->zs.clear.z = true;
572bf215546Sopenharmony_ci                fb->zs.clear_value.depth = batch->clear_depth;
573bf215546Sopenharmony_ci        }
574bf215546Sopenharmony_ci
575bf215546Sopenharmony_ci        if (batch->clear & PIPE_CLEAR_STENCIL) {
576bf215546Sopenharmony_ci                fb->zs.clear.s = true;
577bf215546Sopenharmony_ci                fb->zs.clear_value.stencil = batch->clear_stencil;
578bf215546Sopenharmony_ci        }
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_ci        fb->zs.discard.z = !reserve && !(batch->resolve & PIPE_CLEAR_DEPTH);
581bf215546Sopenharmony_ci        fb->zs.discard.s = !reserve && !(batch->resolve & PIPE_CLEAR_STENCIL);
582bf215546Sopenharmony_ci
583bf215546Sopenharmony_ci        if (!fb->zs.clear.z &&
584bf215546Sopenharmony_ci            ((batch->read & PIPE_CLEAR_DEPTH) ||
585bf215546Sopenharmony_ci             ((batch->draws & PIPE_CLEAR_DEPTH) &&
586bf215546Sopenharmony_ci              z_rsrc && BITSET_TEST(z_rsrc->valid.data, z_view->first_level))))
587bf215546Sopenharmony_ci                fb->zs.preload.z = true;
588bf215546Sopenharmony_ci
589bf215546Sopenharmony_ci        if (!fb->zs.clear.s &&
590bf215546Sopenharmony_ci            ((batch->read & PIPE_CLEAR_STENCIL) ||
591bf215546Sopenharmony_ci             ((batch->draws & PIPE_CLEAR_STENCIL) &&
592bf215546Sopenharmony_ci              s_rsrc && BITSET_TEST(s_rsrc->valid.data, s_view->first_level))))
593bf215546Sopenharmony_ci                fb->zs.preload.s = true;
594bf215546Sopenharmony_ci
595bf215546Sopenharmony_ci        /* Preserve both component if we have a combined ZS view and
596bf215546Sopenharmony_ci         * one component needs to be preserved.
597bf215546Sopenharmony_ci         */
598bf215546Sopenharmony_ci        if (s_view == z_view && fb->zs.discard.z != fb->zs.discard.s) {
599bf215546Sopenharmony_ci                bool valid = BITSET_TEST(z_rsrc->valid.data, z_view->first_level);
600bf215546Sopenharmony_ci
601bf215546Sopenharmony_ci                fb->zs.discard.z = false;
602bf215546Sopenharmony_ci                fb->zs.discard.s = false;
603bf215546Sopenharmony_ci                fb->zs.preload.z = !fb->zs.clear.z && valid;
604bf215546Sopenharmony_ci                fb->zs.preload.s = !fb->zs.clear.s && valid;
605bf215546Sopenharmony_ci        }
606bf215546Sopenharmony_ci}
607bf215546Sopenharmony_ci
608bf215546Sopenharmony_cistatic int
609bf215546Sopenharmony_cipanfrost_batch_submit_ioctl(struct panfrost_batch *batch,
610bf215546Sopenharmony_ci                            mali_ptr first_job_desc,
611bf215546Sopenharmony_ci                            uint32_t reqs,
612bf215546Sopenharmony_ci                            uint32_t in_sync,
613bf215546Sopenharmony_ci                            uint32_t out_sync)
614bf215546Sopenharmony_ci{
615bf215546Sopenharmony_ci        struct panfrost_context *ctx = batch->ctx;
616bf215546Sopenharmony_ci        struct pipe_context *gallium = (struct pipe_context *) ctx;
617bf215546Sopenharmony_ci        struct panfrost_device *dev = pan_device(gallium->screen);
618bf215546Sopenharmony_ci        struct drm_panfrost_submit submit = {0,};
619bf215546Sopenharmony_ci        uint32_t *bo_handles;
620bf215546Sopenharmony_ci        int ret;
621bf215546Sopenharmony_ci
622bf215546Sopenharmony_ci        /* If we trace, we always need a syncobj, so make one of our own if we
623bf215546Sopenharmony_ci         * weren't given one to use. Remember that we did so, so we can free it
624bf215546Sopenharmony_ci         * after we're done but preventing double-frees if we were given a
625bf215546Sopenharmony_ci         * syncobj */
626bf215546Sopenharmony_ci
627bf215546Sopenharmony_ci        if (!out_sync && dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))
628bf215546Sopenharmony_ci                out_sync = ctx->syncobj;
629bf215546Sopenharmony_ci
630bf215546Sopenharmony_ci        submit.out_sync = out_sync;
631bf215546Sopenharmony_ci        submit.jc = first_job_desc;
632bf215546Sopenharmony_ci        submit.requirements = reqs;
633bf215546Sopenharmony_ci        if (in_sync) {
634bf215546Sopenharmony_ci                submit.in_syncs = (u64)(uintptr_t)(&in_sync);
635bf215546Sopenharmony_ci                submit.in_sync_count = 1;
636bf215546Sopenharmony_ci        }
637bf215546Sopenharmony_ci
638bf215546Sopenharmony_ci        bo_handles = calloc(panfrost_pool_num_bos(&batch->pool) +
639bf215546Sopenharmony_ci                            panfrost_pool_num_bos(&batch->invisible_pool) +
640bf215546Sopenharmony_ci                            batch->num_bos + 2,
641bf215546Sopenharmony_ci                            sizeof(*bo_handles));
642bf215546Sopenharmony_ci        assert(bo_handles);
643bf215546Sopenharmony_ci
644bf215546Sopenharmony_ci        pan_bo_access *flags = util_dynarray_begin(&batch->bos);
645bf215546Sopenharmony_ci        unsigned end_bo = util_dynarray_num_elements(&batch->bos, pan_bo_access);
646bf215546Sopenharmony_ci
647bf215546Sopenharmony_ci        for (int i = 0; i < end_bo; ++i) {
648bf215546Sopenharmony_ci                if (!flags[i])
649bf215546Sopenharmony_ci                        continue;
650bf215546Sopenharmony_ci
651bf215546Sopenharmony_ci                assert(submit.bo_handle_count < batch->num_bos);
652bf215546Sopenharmony_ci                bo_handles[submit.bo_handle_count++] = i;
653bf215546Sopenharmony_ci
654bf215546Sopenharmony_ci                /* Update the BO access flags so that panfrost_bo_wait() knows
655bf215546Sopenharmony_ci                 * about all pending accesses.
656bf215546Sopenharmony_ci                 * We only keep the READ/WRITE info since this is all the BO
657bf215546Sopenharmony_ci                 * wait logic cares about.
658bf215546Sopenharmony_ci                 * We also preserve existing flags as this batch might not
659bf215546Sopenharmony_ci                 * be the first one to access the BO.
660bf215546Sopenharmony_ci                 */
661bf215546Sopenharmony_ci                struct panfrost_bo *bo = pan_lookup_bo(dev, i);
662bf215546Sopenharmony_ci
663bf215546Sopenharmony_ci                bo->gpu_access |= flags[i] & (PAN_BO_ACCESS_RW);
664bf215546Sopenharmony_ci        }
665bf215546Sopenharmony_ci
666bf215546Sopenharmony_ci        panfrost_pool_get_bo_handles(&batch->pool, bo_handles + submit.bo_handle_count);
667bf215546Sopenharmony_ci        submit.bo_handle_count += panfrost_pool_num_bos(&batch->pool);
668bf215546Sopenharmony_ci        panfrost_pool_get_bo_handles(&batch->invisible_pool, bo_handles + submit.bo_handle_count);
669bf215546Sopenharmony_ci        submit.bo_handle_count += panfrost_pool_num_bos(&batch->invisible_pool);
670bf215546Sopenharmony_ci
671bf215546Sopenharmony_ci        /* Add the tiler heap to the list of accessed BOs if the batch has at
672bf215546Sopenharmony_ci         * least one tiler job. Tiler heap is written by tiler jobs and read
673bf215546Sopenharmony_ci         * by fragment jobs (the polygon list is coming from this heap).
674bf215546Sopenharmony_ci         */
675bf215546Sopenharmony_ci        if (batch->scoreboard.first_tiler)
676bf215546Sopenharmony_ci                bo_handles[submit.bo_handle_count++] = dev->tiler_heap->gem_handle;
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci        /* Always used on Bifrost, occassionally used on Midgard */
679bf215546Sopenharmony_ci        bo_handles[submit.bo_handle_count++] = dev->sample_positions->gem_handle;
680bf215546Sopenharmony_ci
681bf215546Sopenharmony_ci        submit.bo_handles = (u64) (uintptr_t) bo_handles;
682bf215546Sopenharmony_ci        if (ctx->is_noop)
683bf215546Sopenharmony_ci                ret = 0;
684bf215546Sopenharmony_ci        else
685bf215546Sopenharmony_ci                ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit);
686bf215546Sopenharmony_ci        free(bo_handles);
687bf215546Sopenharmony_ci
688bf215546Sopenharmony_ci        if (ret)
689bf215546Sopenharmony_ci                return errno;
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_ci        /* Trace the job if we're doing that */
692bf215546Sopenharmony_ci        if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {
693bf215546Sopenharmony_ci                /* Wait so we can get errors reported back */
694bf215546Sopenharmony_ci                drmSyncobjWait(dev->fd, &out_sync, 1,
695bf215546Sopenharmony_ci                               INT64_MAX, 0, NULL);
696bf215546Sopenharmony_ci
697bf215546Sopenharmony_ci                if (dev->debug & PAN_DBG_TRACE)
698bf215546Sopenharmony_ci                        pandecode_jc(submit.jc, dev->gpu_id);
699bf215546Sopenharmony_ci
700bf215546Sopenharmony_ci                if (dev->debug & PAN_DBG_DUMP)
701bf215546Sopenharmony_ci                        pandecode_dump_mappings();
702bf215546Sopenharmony_ci
703bf215546Sopenharmony_ci                /* Jobs won't be complete if blackhole rendering, that's ok */
704bf215546Sopenharmony_ci                if (!ctx->is_noop && dev->debug & PAN_DBG_SYNC)
705bf215546Sopenharmony_ci                        pandecode_abort_on_fault(submit.jc, dev->gpu_id);
706bf215546Sopenharmony_ci        }
707bf215546Sopenharmony_ci
708bf215546Sopenharmony_ci        return 0;
709bf215546Sopenharmony_ci}
710bf215546Sopenharmony_ci
711bf215546Sopenharmony_cistatic bool
712bf215546Sopenharmony_cipanfrost_has_fragment_job(struct panfrost_batch *batch)
713bf215546Sopenharmony_ci{
714bf215546Sopenharmony_ci        return batch->scoreboard.first_tiler || batch->clear;
715bf215546Sopenharmony_ci}
716bf215546Sopenharmony_ci
717bf215546Sopenharmony_ci/* Submit both vertex/tiler and fragment jobs for a batch, possibly with an
718bf215546Sopenharmony_ci * outsync corresponding to the later of the two (since there will be an
719bf215546Sopenharmony_ci * implicit dep between them) */
720bf215546Sopenharmony_ci
721bf215546Sopenharmony_cistatic int
722bf215546Sopenharmony_cipanfrost_batch_submit_jobs(struct panfrost_batch *batch,
723bf215546Sopenharmony_ci                           const struct pan_fb_info *fb,
724bf215546Sopenharmony_ci                           uint32_t in_sync, uint32_t out_sync)
725bf215546Sopenharmony_ci{
726bf215546Sopenharmony_ci        struct pipe_screen *pscreen = batch->ctx->base.screen;
727bf215546Sopenharmony_ci        struct panfrost_screen *screen = pan_screen(pscreen);
728bf215546Sopenharmony_ci        struct panfrost_device *dev = pan_device(pscreen);
729bf215546Sopenharmony_ci        bool has_draws = batch->scoreboard.first_job;
730bf215546Sopenharmony_ci        bool has_tiler = batch->scoreboard.first_tiler;
731bf215546Sopenharmony_ci        bool has_frag = panfrost_has_fragment_job(batch);
732bf215546Sopenharmony_ci        int ret = 0;
733bf215546Sopenharmony_ci
734bf215546Sopenharmony_ci        /* Take the submit lock to make sure no tiler jobs from other context
735bf215546Sopenharmony_ci         * are inserted between our tiler and fragment jobs, failing to do that
736bf215546Sopenharmony_ci         * might result in tiler heap corruption.
737bf215546Sopenharmony_ci         */
738bf215546Sopenharmony_ci        if (has_tiler)
739bf215546Sopenharmony_ci                pthread_mutex_lock(&dev->submit_lock);
740bf215546Sopenharmony_ci
741bf215546Sopenharmony_ci        if (has_draws) {
742bf215546Sopenharmony_ci                ret = panfrost_batch_submit_ioctl(batch, batch->scoreboard.first_job,
743bf215546Sopenharmony_ci                                                  0, in_sync, has_frag ? 0 : out_sync);
744bf215546Sopenharmony_ci
745bf215546Sopenharmony_ci                if (ret)
746bf215546Sopenharmony_ci                        goto done;
747bf215546Sopenharmony_ci        }
748bf215546Sopenharmony_ci
749bf215546Sopenharmony_ci        if (has_frag) {
750bf215546Sopenharmony_ci                mali_ptr fragjob = screen->vtbl.emit_fragment_job(batch, fb);
751bf215546Sopenharmony_ci                ret = panfrost_batch_submit_ioctl(batch, fragjob,
752bf215546Sopenharmony_ci                                                  PANFROST_JD_REQ_FS, 0,
753bf215546Sopenharmony_ci                                                  out_sync);
754bf215546Sopenharmony_ci                if (ret)
755bf215546Sopenharmony_ci                        goto done;
756bf215546Sopenharmony_ci        }
757bf215546Sopenharmony_ci
758bf215546Sopenharmony_cidone:
759bf215546Sopenharmony_ci        if (has_tiler)
760bf215546Sopenharmony_ci                pthread_mutex_unlock(&dev->submit_lock);
761bf215546Sopenharmony_ci
762bf215546Sopenharmony_ci        return ret;
763bf215546Sopenharmony_ci}
764bf215546Sopenharmony_ci
765bf215546Sopenharmony_cistatic void
766bf215546Sopenharmony_cipanfrost_emit_tile_map(struct panfrost_batch *batch, struct pan_fb_info *fb)
767bf215546Sopenharmony_ci{
768bf215546Sopenharmony_ci        if (batch->key.nr_cbufs < 1 || !batch->key.cbufs[0])
769bf215546Sopenharmony_ci                return;
770bf215546Sopenharmony_ci
771bf215546Sopenharmony_ci        struct pipe_surface *surf = batch->key.cbufs[0];
772bf215546Sopenharmony_ci        struct panfrost_resource *pres = surf ? pan_resource(surf->texture) : NULL;
773bf215546Sopenharmony_ci
774bf215546Sopenharmony_ci        if (pres && pres->damage.tile_map.enable) {
775bf215546Sopenharmony_ci                fb->tile_map.base =
776bf215546Sopenharmony_ci                        pan_pool_upload_aligned(&batch->pool.base,
777bf215546Sopenharmony_ci                                                pres->damage.tile_map.data,
778bf215546Sopenharmony_ci                                                pres->damage.tile_map.size,
779bf215546Sopenharmony_ci                                                64);
780bf215546Sopenharmony_ci                fb->tile_map.stride = pres->damage.tile_map.stride;
781bf215546Sopenharmony_ci        }
782bf215546Sopenharmony_ci}
783bf215546Sopenharmony_ci
784bf215546Sopenharmony_cistatic void
785bf215546Sopenharmony_cipanfrost_batch_submit(struct panfrost_context *ctx,
786bf215546Sopenharmony_ci                      struct panfrost_batch *batch)
787bf215546Sopenharmony_ci{
788bf215546Sopenharmony_ci        struct pipe_screen *pscreen = ctx->base.screen;
789bf215546Sopenharmony_ci        struct panfrost_screen *screen = pan_screen(pscreen);
790bf215546Sopenharmony_ci        int ret;
791bf215546Sopenharmony_ci
792bf215546Sopenharmony_ci        /* Nothing to do! */
793bf215546Sopenharmony_ci        if (!batch->scoreboard.first_job && !batch->clear)
794bf215546Sopenharmony_ci                goto out;
795bf215546Sopenharmony_ci
796bf215546Sopenharmony_ci        if (batch->key.zsbuf && panfrost_has_fragment_job(batch)) {
797bf215546Sopenharmony_ci                struct pipe_surface *surf = batch->key.zsbuf;
798bf215546Sopenharmony_ci                struct panfrost_resource *z_rsrc = pan_resource(surf->texture);
799bf215546Sopenharmony_ci
800bf215546Sopenharmony_ci                /* Shared depth/stencil resources are not supported, and would
801bf215546Sopenharmony_ci                 * break this optimisation. */
802bf215546Sopenharmony_ci                assert(!(z_rsrc->base.bind & PAN_BIND_SHARED_MASK));
803bf215546Sopenharmony_ci
804bf215546Sopenharmony_ci                if (batch->clear & PIPE_CLEAR_STENCIL) {
805bf215546Sopenharmony_ci                        z_rsrc->stencil_value = batch->clear_stencil;
806bf215546Sopenharmony_ci                        z_rsrc->constant_stencil = true;
807bf215546Sopenharmony_ci                } else if (z_rsrc->constant_stencil) {
808bf215546Sopenharmony_ci                        batch->clear_stencil = z_rsrc->stencil_value;
809bf215546Sopenharmony_ci                        batch->clear |= PIPE_CLEAR_STENCIL;
810bf215546Sopenharmony_ci                }
811bf215546Sopenharmony_ci
812bf215546Sopenharmony_ci                if (batch->draws & PIPE_CLEAR_STENCIL)
813bf215546Sopenharmony_ci                        z_rsrc->constant_stencil = false;
814bf215546Sopenharmony_ci        }
815bf215546Sopenharmony_ci
816bf215546Sopenharmony_ci        struct pan_fb_info fb;
817bf215546Sopenharmony_ci        struct pan_image_view rts[8], zs, s;
818bf215546Sopenharmony_ci
819bf215546Sopenharmony_ci        panfrost_batch_to_fb_info(batch, &fb, rts, &zs, &s, false);
820bf215546Sopenharmony_ci
821bf215546Sopenharmony_ci        screen->vtbl.preload(batch, &fb);
822bf215546Sopenharmony_ci        screen->vtbl.init_polygon_list(batch);
823bf215546Sopenharmony_ci
824bf215546Sopenharmony_ci        /* Now that all draws are in, we can finally prepare the
825bf215546Sopenharmony_ci         * FBD for the batch (if there is one). */
826bf215546Sopenharmony_ci
827bf215546Sopenharmony_ci        screen->vtbl.emit_tls(batch);
828bf215546Sopenharmony_ci        panfrost_emit_tile_map(batch, &fb);
829bf215546Sopenharmony_ci
830bf215546Sopenharmony_ci        if (batch->scoreboard.first_tiler || batch->clear)
831bf215546Sopenharmony_ci                screen->vtbl.emit_fbd(batch, &fb);
832bf215546Sopenharmony_ci
833bf215546Sopenharmony_ci        ret = panfrost_batch_submit_jobs(batch, &fb, 0, ctx->syncobj);
834bf215546Sopenharmony_ci
835bf215546Sopenharmony_ci        if (ret)
836bf215546Sopenharmony_ci                fprintf(stderr, "panfrost_batch_submit failed: %d\n", ret);
837bf215546Sopenharmony_ci
838bf215546Sopenharmony_ci        /* We must reset the damage info of our render targets here even
839bf215546Sopenharmony_ci         * though a damage reset normally happens when the DRI layer swaps
840bf215546Sopenharmony_ci         * buffers. That's because there can be implicit flushes the GL
841bf215546Sopenharmony_ci         * app is not aware of, and those might impact the damage region: if
842bf215546Sopenharmony_ci         * part of the damaged portion is drawn during those implicit flushes,
843bf215546Sopenharmony_ci         * you have to reload those areas before next draws are pushed, and
844bf215546Sopenharmony_ci         * since the driver can't easily know what's been modified by the draws
845bf215546Sopenharmony_ci         * it flushed, the easiest solution is to reload everything.
846bf215546Sopenharmony_ci         */
847bf215546Sopenharmony_ci        for (unsigned i = 0; i < batch->key.nr_cbufs; i++) {
848bf215546Sopenharmony_ci                if (!batch->key.cbufs[i])
849bf215546Sopenharmony_ci                        continue;
850bf215546Sopenharmony_ci
851bf215546Sopenharmony_ci                panfrost_resource_set_damage_region(ctx->base.screen,
852bf215546Sopenharmony_ci                                                    batch->key.cbufs[i]->texture,
853bf215546Sopenharmony_ci                                                    0, NULL);
854bf215546Sopenharmony_ci        }
855bf215546Sopenharmony_ci
856bf215546Sopenharmony_ciout:
857bf215546Sopenharmony_ci        panfrost_batch_cleanup(ctx, batch);
858bf215546Sopenharmony_ci}
859bf215546Sopenharmony_ci
860bf215546Sopenharmony_ci/* Submit all batches */
861bf215546Sopenharmony_ci
862bf215546Sopenharmony_civoid
863bf215546Sopenharmony_cipanfrost_flush_all_batches(struct panfrost_context *ctx, const char *reason)
864bf215546Sopenharmony_ci{
865bf215546Sopenharmony_ci        struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
866bf215546Sopenharmony_ci        panfrost_batch_submit(ctx, batch);
867bf215546Sopenharmony_ci
868bf215546Sopenharmony_ci        for (unsigned i = 0; i < PAN_MAX_BATCHES; i++) {
869bf215546Sopenharmony_ci                if (ctx->batches.slots[i].seqnum) {
870bf215546Sopenharmony_ci                        if (reason)
871bf215546Sopenharmony_ci                                perf_debug_ctx(ctx, "Flushing everything due to: %s", reason);
872bf215546Sopenharmony_ci
873bf215546Sopenharmony_ci                        panfrost_batch_submit(ctx, &ctx->batches.slots[i]);
874bf215546Sopenharmony_ci                }
875bf215546Sopenharmony_ci        }
876bf215546Sopenharmony_ci}
877bf215546Sopenharmony_ci
878bf215546Sopenharmony_civoid
879bf215546Sopenharmony_cipanfrost_flush_writer(struct panfrost_context *ctx,
880bf215546Sopenharmony_ci                      struct panfrost_resource *rsrc,
881bf215546Sopenharmony_ci                      const char *reason)
882bf215546Sopenharmony_ci{
883bf215546Sopenharmony_ci        struct hash_entry *entry = _mesa_hash_table_search(ctx->writers, rsrc);
884bf215546Sopenharmony_ci
885bf215546Sopenharmony_ci        if (entry) {
886bf215546Sopenharmony_ci                perf_debug_ctx(ctx, "Flushing writer due to: %s", reason);
887bf215546Sopenharmony_ci                panfrost_batch_submit(ctx, entry->data);
888bf215546Sopenharmony_ci        }
889bf215546Sopenharmony_ci}
890bf215546Sopenharmony_ci
891bf215546Sopenharmony_civoid
892bf215546Sopenharmony_cipanfrost_flush_batches_accessing_rsrc(struct panfrost_context *ctx,
893bf215546Sopenharmony_ci                                      struct panfrost_resource *rsrc,
894bf215546Sopenharmony_ci                                      const char *reason)
895bf215546Sopenharmony_ci{
896bf215546Sopenharmony_ci        unsigned i;
897bf215546Sopenharmony_ci        foreach_batch(ctx, i) {
898bf215546Sopenharmony_ci                struct panfrost_batch *batch = &ctx->batches.slots[i];
899bf215546Sopenharmony_ci
900bf215546Sopenharmony_ci                if (!_mesa_set_search(batch->resources, rsrc))
901bf215546Sopenharmony_ci                        continue;
902bf215546Sopenharmony_ci
903bf215546Sopenharmony_ci                perf_debug_ctx(ctx, "Flushing user due to: %s", reason);
904bf215546Sopenharmony_ci                panfrost_batch_submit(ctx, batch);
905bf215546Sopenharmony_ci        }
906bf215546Sopenharmony_ci}
907bf215546Sopenharmony_ci
908bf215546Sopenharmony_civoid
909bf215546Sopenharmony_cipanfrost_batch_adjust_stack_size(struct panfrost_batch *batch)
910bf215546Sopenharmony_ci{
911bf215546Sopenharmony_ci        struct panfrost_context *ctx = batch->ctx;
912bf215546Sopenharmony_ci
913bf215546Sopenharmony_ci        for (unsigned i = 0; i < PIPE_SHADER_TYPES; ++i) {
914bf215546Sopenharmony_ci                struct panfrost_shader_state *ss;
915bf215546Sopenharmony_ci
916bf215546Sopenharmony_ci                ss = panfrost_get_shader_state(ctx, i);
917bf215546Sopenharmony_ci                if (!ss)
918bf215546Sopenharmony_ci                        continue;
919bf215546Sopenharmony_ci
920bf215546Sopenharmony_ci                batch->stack_size = MAX2(batch->stack_size, ss->info.tls_size);
921bf215546Sopenharmony_ci        }
922bf215546Sopenharmony_ci}
923bf215546Sopenharmony_ci
924bf215546Sopenharmony_civoid
925bf215546Sopenharmony_cipanfrost_batch_clear(struct panfrost_batch *batch,
926bf215546Sopenharmony_ci                     unsigned buffers,
927bf215546Sopenharmony_ci                     const union pipe_color_union *color,
928bf215546Sopenharmony_ci                     double depth, unsigned stencil)
929bf215546Sopenharmony_ci{
930bf215546Sopenharmony_ci        struct panfrost_context *ctx = batch->ctx;
931bf215546Sopenharmony_ci
932bf215546Sopenharmony_ci        if (buffers & PIPE_CLEAR_COLOR) {
933bf215546Sopenharmony_ci                for (unsigned i = 0; i < ctx->pipe_framebuffer.nr_cbufs; ++i) {
934bf215546Sopenharmony_ci                        if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
935bf215546Sopenharmony_ci                                continue;
936bf215546Sopenharmony_ci
937bf215546Sopenharmony_ci                        enum pipe_format format = ctx->pipe_framebuffer.cbufs[i]->format;
938bf215546Sopenharmony_ci                        pan_pack_color(batch->clear_color[i], color, format, false);
939bf215546Sopenharmony_ci                }
940bf215546Sopenharmony_ci        }
941bf215546Sopenharmony_ci
942bf215546Sopenharmony_ci        if (buffers & PIPE_CLEAR_DEPTH) {
943bf215546Sopenharmony_ci                batch->clear_depth = depth;
944bf215546Sopenharmony_ci        }
945bf215546Sopenharmony_ci
946bf215546Sopenharmony_ci        if (buffers & PIPE_CLEAR_STENCIL) {
947bf215546Sopenharmony_ci                batch->clear_stencil = stencil;
948bf215546Sopenharmony_ci        }
949bf215546Sopenharmony_ci
950bf215546Sopenharmony_ci        batch->clear |= buffers;
951bf215546Sopenharmony_ci        batch->resolve |= buffers;
952bf215546Sopenharmony_ci
953bf215546Sopenharmony_ci        /* Clearing affects the entire framebuffer (by definition -- this is
954bf215546Sopenharmony_ci         * the Gallium clear callback, which clears the whole framebuffer. If
955bf215546Sopenharmony_ci         * the scissor test were enabled from the GL side, the gallium frontend
956bf215546Sopenharmony_ci         * would emit a quad instead and we wouldn't go down this code path) */
957bf215546Sopenharmony_ci
958bf215546Sopenharmony_ci        panfrost_batch_union_scissor(batch, 0, 0,
959bf215546Sopenharmony_ci                                     ctx->pipe_framebuffer.width,
960bf215546Sopenharmony_ci                                     ctx->pipe_framebuffer.height);
961bf215546Sopenharmony_ci}
962bf215546Sopenharmony_ci
963bf215546Sopenharmony_ci/* Given a new bounding rectangle (scissor), let the job cover the union of the
964bf215546Sopenharmony_ci * new and old bounding rectangles */
965bf215546Sopenharmony_ci
966bf215546Sopenharmony_civoid
967bf215546Sopenharmony_cipanfrost_batch_union_scissor(struct panfrost_batch *batch,
968bf215546Sopenharmony_ci                             unsigned minx, unsigned miny,
969bf215546Sopenharmony_ci                             unsigned maxx, unsigned maxy)
970bf215546Sopenharmony_ci{
971bf215546Sopenharmony_ci        batch->minx = MIN2(batch->minx, minx);
972bf215546Sopenharmony_ci        batch->miny = MIN2(batch->miny, miny);
973bf215546Sopenharmony_ci        batch->maxx = MAX2(batch->maxx, maxx);
974bf215546Sopenharmony_ci        batch->maxy = MAX2(batch->maxy, maxy);
975bf215546Sopenharmony_ci}
976bf215546Sopenharmony_ci
977bf215546Sopenharmony_ci/**
978bf215546Sopenharmony_ci * Checks if rasterization should be skipped. If not, a TILER job must be
979bf215546Sopenharmony_ci * created for each draw, or the IDVS flow must be used.
980bf215546Sopenharmony_ci *
981bf215546Sopenharmony_ci * As a special case, if there is no vertex shader, no primitives are generated,
982bf215546Sopenharmony_ci * meaning the whole pipeline (including rasterization) should be skipped.
983bf215546Sopenharmony_ci */
984bf215546Sopenharmony_cibool
985bf215546Sopenharmony_cipanfrost_batch_skip_rasterization(struct panfrost_batch *batch)
986bf215546Sopenharmony_ci{
987bf215546Sopenharmony_ci        struct panfrost_context *ctx = batch->ctx;
988bf215546Sopenharmony_ci        struct pipe_rasterizer_state *rast = (void *) ctx->rasterizer;
989bf215546Sopenharmony_ci
990bf215546Sopenharmony_ci        return (rast->rasterizer_discard ||
991bf215546Sopenharmony_ci                batch->scissor_culls_everything ||
992bf215546Sopenharmony_ci                !batch->rsd[PIPE_SHADER_VERTEX]);
993bf215546Sopenharmony_ci}
994