xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/lima/lima_job.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2017-2019 Lima Project
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "xf86drm.h"
28 #include "drm-uapi/lima_drm.h"
29 
30 #include "util/u_math.h"
31 #include "util/ralloc.h"
32 #include "util/os_time.h"
33 #include "util/hash_table.h"
34 #include "util/format/u_format.h"
35 #include "util/u_upload_mgr.h"
36 #include "util/u_inlines.h"
37 #include "util/u_framebuffer.h"
38 
39 #include "lima_screen.h"
40 #include "lima_context.h"
41 #include "lima_job.h"
42 #include "lima_bo.h"
43 #include "lima_util.h"
44 #include "lima_format.h"
45 #include "lima_resource.h"
46 #include "lima_texture.h"
47 #include "lima_fence.h"
48 #include "lima_gpu.h"
49 #include "lima_blit.h"
50 
51 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
52 
53 static void
lima_get_fb_info(struct lima_job * job)54 lima_get_fb_info(struct lima_job *job)
55 {
56    struct lima_context *ctx = job->ctx;
57    struct lima_job_fb_info *fb = &job->fb;
58    struct lima_surface *surf = lima_surface(job->key.cbuf);
59 
60    if (!surf)
61       surf = lima_surface(job->key.zsbuf);
62 
63    if (!surf) {
64       /* We don't have neither cbuf nor zsbuf, use dimensions from ctx */
65       fb->width = ctx->framebuffer.base.width;
66       fb->height =  ctx->framebuffer.base.height;
67    } else {
68       fb->width = surf->base.width;
69       fb->height = surf->base.height;
70    }
71 
72    int width = align(fb->width, 16) >> 4;
73    int height = align(fb->height, 16) >> 4;
74 
75    struct lima_screen *screen = lima_screen(ctx->base.screen);
76 
77    fb->tiled_w = width;
78    fb->tiled_h = height;
79 
80    fb->shift_h = 0;
81    fb->shift_w = 0;
82 
83    int limit = screen->plb_max_blk;
84    while ((width * height) > limit ||
85           width > PLBU_BLOCK_W_MASK || height > PLBU_BLOCK_H_MASK) {
86       if (width >= height || width > PLBU_BLOCK_W_MASK) {
87          width = (width + 1) >> 1;
88          fb->shift_w++;
89       } else {
90          height = (height + 1) >> 1;
91          fb->shift_h++;
92       }
93    }
94 
95    fb->block_w = width;
96    fb->block_h = height;
97 
98    fb->shift_min = MIN3(fb->shift_w, fb->shift_h, 2);
99 }
100 
101 static struct lima_job *
lima_job_create(struct lima_context * ctx,struct pipe_surface * cbuf,struct pipe_surface * zsbuf)102 lima_job_create(struct lima_context *ctx,
103                 struct pipe_surface *cbuf,
104                 struct pipe_surface *zsbuf)
105 {
106    struct lima_job *s;
107 
108    s = rzalloc(ctx, struct lima_job);
109    if (!s)
110       return NULL;
111 
112    s->fd = lima_screen(ctx->base.screen)->fd;
113    s->ctx = ctx;
114 
115    s->damage_rect.minx = s->damage_rect.miny = 0xffff;
116    s->damage_rect.maxx = s->damage_rect.maxy = 0;
117    s->draws = 0;
118 
119    s->clear.depth = 0x00ffffff;
120 
121    for (int i = 0; i < 2; i++) {
122       util_dynarray_init(s->gem_bos + i, s);
123       util_dynarray_init(s->bos + i, s);
124    }
125 
126    util_dynarray_init(&s->vs_cmd_array, s);
127    util_dynarray_init(&s->plbu_cmd_array, s);
128    util_dynarray_init(&s->plbu_cmd_head, s);
129 
130    pipe_surface_reference(&s->key.cbuf, cbuf);
131    pipe_surface_reference(&s->key.zsbuf, zsbuf);
132 
133    lima_get_fb_info(s);
134 
135    s->dump = lima_dump_create();
136 
137    return s;
138 }
139 
140 static void
lima_job_free(struct lima_job * job)141 lima_job_free(struct lima_job *job)
142 {
143    struct lima_context *ctx = job->ctx;
144 
145    _mesa_hash_table_remove_key(ctx->jobs, &job->key);
146 
147    if (job->key.cbuf && (job->resolve & PIPE_CLEAR_COLOR0))
148       _mesa_hash_table_remove_key(ctx->write_jobs, job->key.cbuf->texture);
149    if (job->key.zsbuf && (job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)))
150       _mesa_hash_table_remove_key(ctx->write_jobs, job->key.zsbuf->texture);
151 
152    pipe_surface_reference(&job->key.cbuf, NULL);
153    pipe_surface_reference(&job->key.zsbuf, NULL);
154 
155    lima_dump_free(job->dump);
156    job->dump = NULL;
157 
158    /* TODO: do we need a cache for job? */
159    ralloc_free(job);
160 }
161 
162 struct lima_job *
lima_job_get_with_fb(struct lima_context * ctx,struct pipe_surface * cbuf,struct pipe_surface * zsbuf)163 lima_job_get_with_fb(struct lima_context *ctx,
164                       struct pipe_surface *cbuf,
165                       struct pipe_surface *zsbuf)
166 {
167    struct lima_job_key local_key = {
168       .cbuf = cbuf,
169       .zsbuf = zsbuf,
170    };
171 
172    struct hash_entry *entry = _mesa_hash_table_search(ctx->jobs, &local_key);
173    if (entry)
174       return entry->data;
175 
176    struct lima_job *job = lima_job_create(ctx, cbuf, zsbuf);
177    if (!job)
178       return NULL;
179 
180    _mesa_hash_table_insert(ctx->jobs, &job->key, job);
181 
182    return job;
183 }
184 
185 static struct lima_job *
_lima_job_get(struct lima_context * ctx)186 _lima_job_get(struct lima_context *ctx)
187 {
188    struct lima_context_framebuffer *fb = &ctx->framebuffer;
189 
190    return lima_job_get_with_fb(ctx, fb->base.cbufs[0], fb->base.zsbuf);
191 }
192 
193 /*
194  * Note: this function can only be called in draw code path,
195  * must not exist in flush code path.
196  */
197 struct lima_job *
lima_job_get(struct lima_context * ctx)198 lima_job_get(struct lima_context *ctx)
199 {
200    if (ctx->job)
201       return ctx->job;
202 
203    ctx->job = _lima_job_get(ctx);
204    return ctx->job;
205 }
206 
lima_job_add_bo(struct lima_job * job,int pipe,struct lima_bo * bo,uint32_t flags)207 bool lima_job_add_bo(struct lima_job *job, int pipe,
208                      struct lima_bo *bo, uint32_t flags)
209 {
210    util_dynarray_foreach(job->gem_bos + pipe, struct drm_lima_gem_submit_bo, gem_bo) {
211       if (bo->handle == gem_bo->handle) {
212          gem_bo->flags |= flags;
213          return true;
214       }
215    }
216 
217    struct drm_lima_gem_submit_bo *job_bo =
218       util_dynarray_grow(job->gem_bos + pipe, struct drm_lima_gem_submit_bo, 1);
219    job_bo->handle = bo->handle;
220    job_bo->flags = flags;
221 
222    struct lima_bo **jbo = util_dynarray_grow(job->bos + pipe, struct lima_bo *, 1);
223    *jbo = bo;
224 
225    /* prevent bo from being freed when job start */
226    lima_bo_reference(bo);
227 
228    return true;
229 }
230 
231 static bool
lima_job_start(struct lima_job * job,int pipe,void * frame,uint32_t size)232 lima_job_start(struct lima_job *job, int pipe, void *frame, uint32_t size)
233 {
234    struct lima_context *ctx = job->ctx;
235    struct drm_lima_gem_submit req = {
236       .ctx = ctx->id,
237       .pipe = pipe,
238       .nr_bos = job->gem_bos[pipe].size / sizeof(struct drm_lima_gem_submit_bo),
239       .bos = VOID2U64(util_dynarray_begin(job->gem_bos + pipe)),
240       .frame = VOID2U64(frame),
241       .frame_size = size,
242       .out_sync = ctx->out_sync[pipe],
243    };
244 
245    if (ctx->in_sync_fd >= 0) {
246       int err = drmSyncobjImportSyncFile(job->fd, ctx->in_sync[pipe],
247                                          ctx->in_sync_fd);
248       if (err)
249          return false;
250 
251       req.in_sync[0] = ctx->in_sync[pipe];
252       close(ctx->in_sync_fd);
253       ctx->in_sync_fd = -1;
254    }
255 
256    bool ret = drmIoctl(job->fd, DRM_IOCTL_LIMA_GEM_SUBMIT, &req) == 0;
257 
258    util_dynarray_foreach(job->bos + pipe, struct lima_bo *, bo) {
259       lima_bo_unreference(*bo);
260    }
261 
262    return ret;
263 }
264 
265 static bool
lima_job_wait(struct lima_job * job,int pipe,uint64_t timeout_ns)266 lima_job_wait(struct lima_job *job, int pipe, uint64_t timeout_ns)
267 {
268    int64_t abs_timeout = os_time_get_absolute_timeout(timeout_ns);
269    if (abs_timeout == OS_TIMEOUT_INFINITE)
270       abs_timeout = INT64_MAX;
271 
272    struct lima_context *ctx = job->ctx;
273    return !drmSyncobjWait(job->fd, ctx->out_sync + pipe, 1, abs_timeout, 0, NULL);
274 }
275 
276 static bool
lima_job_has_bo(struct lima_job * job,struct lima_bo * bo,bool all)277 lima_job_has_bo(struct lima_job *job, struct lima_bo *bo, bool all)
278 {
279    for (int i = 0; i < 2; i++) {
280       util_dynarray_foreach(job->gem_bos + i, struct drm_lima_gem_submit_bo, gem_bo) {
281          if (bo->handle == gem_bo->handle) {
282             if (all || gem_bo->flags & LIMA_SUBMIT_BO_WRITE)
283                return true;
284             else
285                break;
286          }
287       }
288    }
289 
290    return false;
291 }
292 
293 void *
lima_job_create_stream_bo(struct lima_job * job,int pipe,unsigned size,uint32_t * va)294 lima_job_create_stream_bo(struct lima_job *job, int pipe,
295                           unsigned size, uint32_t *va)
296 {
297    struct lima_context *ctx = job->ctx;
298 
299    void *cpu;
300    unsigned offset;
301    struct pipe_resource *pres = NULL;
302    u_upload_alloc(ctx->uploader, 0, size, 0x40, &offset, &pres, &cpu);
303 
304    struct lima_resource *res = lima_resource(pres);
305    *va = res->bo->va + offset;
306 
307    lima_job_add_bo(job, pipe, res->bo, LIMA_SUBMIT_BO_READ);
308 
309    pipe_resource_reference(&pres, NULL);
310 
311    return cpu;
312 }
313 
314 static inline struct lima_damage_region *
lima_job_get_damage(struct lima_job * job)315 lima_job_get_damage(struct lima_job *job)
316 {
317    if (!(job->key.cbuf && (job->resolve & PIPE_CLEAR_COLOR0)))
318       return NULL;
319 
320    struct lima_surface *surf = lima_surface(job->key.cbuf);
321    struct lima_resource *res = lima_resource(surf->base.texture);
322    return &res->damage;
323 }
324 
325 static bool
lima_fb_cbuf_needs_reload(struct lima_job * job)326 lima_fb_cbuf_needs_reload(struct lima_job *job)
327 {
328    if (!job->key.cbuf)
329       return false;
330 
331    struct lima_surface *surf = lima_surface(job->key.cbuf);
332    struct lima_resource *res = lima_resource(surf->base.texture);
333    if (res->damage.region) {
334       /* for EGL_KHR_partial_update, when EGL_EXT_buffer_age is enabled,
335        * we need to reload damage region, otherwise just want to reload
336        * the region not aligned to tile boundary */
337       //if (!res->damage.aligned)
338       //   return true;
339       return true;
340    }
341    else if (surf->reload & PIPE_CLEAR_COLOR0)
342          return true;
343 
344    return false;
345 }
346 
347 static bool
lima_fb_zsbuf_needs_reload(struct lima_job * job)348 lima_fb_zsbuf_needs_reload(struct lima_job *job)
349 {
350    if (!job->key.zsbuf)
351       return false;
352 
353    struct lima_surface *surf = lima_surface(job->key.zsbuf);
354    if (surf->reload & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))
355          return true;
356 
357    return false;
358 }
359 
360 static void
lima_pack_reload_plbu_cmd(struct lima_job * job,struct pipe_surface * psurf)361 lima_pack_reload_plbu_cmd(struct lima_job *job, struct pipe_surface *psurf)
362 {
363    struct lima_job_fb_info *fb = &job->fb;
364    struct lima_context *ctx = job->ctx;
365    struct pipe_box src = {
366       .x = 0,
367       .y = 0,
368       .width = fb->width,
369       .height = fb->height,
370    };
371 
372    struct pipe_box dst = {
373       .x = 0,
374       .y = 0,
375       .width = fb->width,
376       .height = fb->height,
377    };
378 
379    if (ctx->framebuffer.base.samples > 1) {
380       for (int i = 0; i < LIMA_MAX_SAMPLES; i++) {
381          lima_pack_blit_cmd(job, &job->plbu_cmd_head,
382                             psurf, &src, &dst,
383                             PIPE_TEX_FILTER_NEAREST, false,
384                             (1 << i), i);
385       }
386    } else {
387       lima_pack_blit_cmd(job, &job->plbu_cmd_head,
388                          psurf, &src, &dst,
389                          PIPE_TEX_FILTER_NEAREST, false,
390                          0xf, 0);
391    }
392 }
393 
394 static void
lima_pack_head_plbu_cmd(struct lima_job * job)395 lima_pack_head_plbu_cmd(struct lima_job *job)
396 {
397    struct lima_context *ctx = job->ctx;
398    struct lima_job_fb_info *fb = &job->fb;
399 
400    PLBU_CMD_BEGIN(&job->plbu_cmd_head, 10);
401 
402    assert((fb->block_w & PLBU_BLOCK_W_MASK) == fb->block_w);
403    assert((fb->block_h & PLBU_BLOCK_H_MASK) == fb->block_h);
404 
405    PLBU_CMD_UNKNOWN2();
406    PLBU_CMD_BLOCK_STEP(fb->shift_min, fb->shift_h, fb->shift_w);
407    PLBU_CMD_TILED_DIMENSIONS(fb->tiled_w, fb->tiled_h);
408    PLBU_CMD_BLOCK_STRIDE(fb->block_w);
409 
410    PLBU_CMD_ARRAY_ADDRESS(
411       ctx->plb_gp_stream->va + ctx->plb_index * ctx->plb_gp_size,
412       fb->block_w * fb->block_h);
413 
414    PLBU_CMD_END();
415 
416    if (lima_fb_cbuf_needs_reload(job)) {
417       lima_pack_reload_plbu_cmd(job, job->key.cbuf);
418    }
419 
420    if (lima_fb_zsbuf_needs_reload(job))
421       lima_pack_reload_plbu_cmd(job, job->key.zsbuf);
422 }
423 
424 static void
hilbert_rotate(int n,int * x,int * y,int rx,int ry)425 hilbert_rotate(int n, int *x, int *y, int rx, int ry)
426 {
427    if (ry == 0) {
428       if (rx == 1) {
429          *x = n-1 - *x;
430          *y = n-1 - *y;
431       }
432 
433       /* Swap x and y */
434       int t  = *x;
435       *x = *y;
436       *y = t;
437    }
438 }
439 
440 static void
hilbert_coords(int n,int d,int * x,int * y)441 hilbert_coords(int n, int d, int *x, int *y)
442 {
443    int rx, ry, i, t=d;
444 
445    *x = *y = 0;
446 
447    for (i = 0; (1 << i) < n; i++) {
448 
449       rx = 1 & (t / 2);
450       ry = 1 & (t ^ rx);
451 
452       hilbert_rotate(1 << i, x, y, rx, ry);
453 
454       *x += rx << i;
455       *y += ry << i;
456 
457       t /= 4;
458    }
459 }
460 
461 static int
lima_get_pp_stream_size(int num_pp,int tiled_w,int tiled_h,uint32_t * off)462 lima_get_pp_stream_size(int num_pp, int tiled_w, int tiled_h, uint32_t *off)
463 {
464    /* carefully calculate each stream start address:
465     * 1. overflow: each stream size may be different due to
466     *    fb->tiled_w * fb->tiled_h can't be divided by num_pp,
467     *    extra size should be added to the preceeding stream
468     * 2. alignment: each stream address should be 0x20 aligned
469     */
470    int delta = tiled_w * tiled_h / num_pp * 16 + 16;
471    int remain = tiled_w * tiled_h % num_pp;
472    int offset = 0;
473 
474    for (int i = 0; i < num_pp; i++) {
475       off[i] = offset;
476 
477       offset += delta;
478       if (remain) {
479          offset += 16;
480          remain--;
481       }
482       offset = align(offset, 0x20);
483    }
484 
485    return offset;
486 }
487 
488 static void
lima_generate_pp_stream(struct lima_job * job,int off_x,int off_y,int tiled_w,int tiled_h)489 lima_generate_pp_stream(struct lima_job *job, int off_x, int off_y,
490                         int tiled_w, int tiled_h)
491 {
492    struct lima_context *ctx = job->ctx;
493    struct lima_pp_stream_state *ps = &ctx->pp_stream;
494    struct lima_job_fb_info *fb = &job->fb;
495    struct lima_screen *screen = lima_screen(ctx->base.screen);
496    int num_pp = screen->num_pp;
497    assert(num_pp > 0);
498 
499    /* use hilbert_coords to generates 1D to 2D relationship.
500     * 1D for pp stream index and 2D for plb block x/y on framebuffer.
501     * if multi pp, interleave the 1D index to make each pp's render target
502     * close enough which should result close workload
503     */
504    int max = MAX2(tiled_w, tiled_h);
505    int index = 0;
506    uint32_t *stream[8];
507    int si[8] = {0};
508    int dim = 0;
509    int count = 0;
510 
511    /* Don't update count if we get zero rect. We'll just generate
512     * PP stream with just terminators in it.
513     */
514    if ((tiled_w * tiled_h) != 0) {
515       dim = util_logbase2_ceil(max);
516       count = 1 << (dim + dim);
517    }
518 
519    for (int i = 0; i < num_pp; i++)
520       stream[i] = ps->map + ps->offset[i];
521 
522    for (int i = 0; i < count; i++) {
523       int x, y;
524       hilbert_coords(max, i, &x, &y);
525       if (x < tiled_w && y < tiled_h) {
526          x += off_x;
527          y += off_y;
528 
529          int pp = index % num_pp;
530          int offset = ((y >> fb->shift_h) * fb->block_w +
531                        (x >> fb->shift_w)) * LIMA_CTX_PLB_BLK_SIZE;
532          int plb_va = ctx->plb[ctx->plb_index]->va + offset;
533 
534          stream[pp][si[pp]++] = 0;
535          stream[pp][si[pp]++] = 0xB8000000 | x | (y << 8);
536          stream[pp][si[pp]++] = 0xE0000002 | ((plb_va >> 3) & ~0xE0000003);
537          stream[pp][si[pp]++] = 0xB0000000;
538 
539          index++;
540       }
541    }
542 
543    for (int i = 0; i < num_pp; i++) {
544       stream[i][si[i]++] = 0;
545       stream[i][si[i]++] = 0xBC000000;
546       stream[i][si[i]++] = 0;
547       stream[i][si[i]++] = 0;
548 
549       lima_dump_command_stream_print(
550          job->dump, stream[i], si[i] * 4,
551          false, "pp plb stream %d at va %x\n",
552          i, ps->va + ps->offset[i]);
553    }
554 }
555 
556 static void
lima_free_stale_pp_stream_bo(struct lima_context * ctx)557 lima_free_stale_pp_stream_bo(struct lima_context *ctx)
558 {
559    list_for_each_entry_safe(struct lima_ctx_plb_pp_stream, entry,
560                             &ctx->plb_pp_stream_lru_list, lru_list) {
561       if (ctx->plb_stream_cache_size <= lima_plb_pp_stream_cache_size)
562          break;
563 
564       struct hash_entry *hash_entry =
565          _mesa_hash_table_search(ctx->plb_pp_stream, &entry->key);
566       if (hash_entry)
567          _mesa_hash_table_remove(ctx->plb_pp_stream, hash_entry);
568       list_del(&entry->lru_list);
569 
570       ctx->plb_stream_cache_size -= entry->bo->size;
571       lima_bo_unreference(entry->bo);
572 
573       ralloc_free(entry);
574    }
575 }
576 
577 static void
lima_update_damage_pp_stream(struct lima_job * job)578 lima_update_damage_pp_stream(struct lima_job *job)
579 {
580    struct lima_context *ctx = job->ctx;
581    struct lima_damage_region *ds = lima_job_get_damage(job);
582    struct lima_job_fb_info *fb = &job->fb;
583    struct pipe_scissor_state bound;
584    struct pipe_scissor_state *dr = &job->damage_rect;
585 
586    if (ds && ds->region) {
587       struct pipe_scissor_state *dbound = &ds->bound;
588       bound.minx = MAX2(dbound->minx, dr->minx >> 4);
589       bound.miny = MAX2(dbound->miny, dr->miny >> 4);
590       bound.maxx = MIN2(dbound->maxx, (dr->maxx + 0xf) >> 4);
591       bound.maxy = MIN2(dbound->maxy, (dr->maxy + 0xf) >> 4);
592    } else {
593       bound.minx = dr->minx >> 4;
594       bound.miny = dr->miny >> 4;
595       bound.maxx = (dr->maxx + 0xf) >> 4;
596       bound.maxy = (dr->maxy + 0xf) >> 4;
597    }
598 
599    /* Clamp to FB size */
600    bound.minx = MIN2(bound.minx, fb->tiled_w);
601    bound.miny = MIN2(bound.miny, fb->tiled_h);
602    bound.maxx = MIN2(bound.maxx, fb->tiled_w);
603    bound.maxy = MIN2(bound.maxy, fb->tiled_h);
604 
605    struct lima_ctx_plb_pp_stream_key key = {
606       .plb_index = ctx->plb_index,
607       .minx = bound.minx,
608       .miny = bound.miny,
609       .maxx = bound.maxx,
610       .maxy = bound.maxy,
611       .shift_w = fb->shift_w,
612       .shift_h = fb->shift_h,
613       .block_w = fb->block_w,
614       .block_h = fb->block_h,
615    };
616 
617    struct hash_entry *entry =
618       _mesa_hash_table_search(ctx->plb_pp_stream, &key);
619    if (entry) {
620       struct lima_ctx_plb_pp_stream *s = entry->data;
621 
622       list_del(&s->lru_list);
623       list_addtail(&s->lru_list, &ctx->plb_pp_stream_lru_list);
624 
625       ctx->pp_stream.map = lima_bo_map(s->bo);
626       ctx->pp_stream.va = s->bo->va;
627       memcpy(ctx->pp_stream.offset, s->offset, sizeof(s->offset));
628 
629       lima_job_add_bo(job, LIMA_PIPE_PP, s->bo, LIMA_SUBMIT_BO_READ);
630 
631       return;
632    }
633 
634    lima_free_stale_pp_stream_bo(ctx);
635 
636    struct lima_screen *screen = lima_screen(ctx->base.screen);
637    struct lima_ctx_plb_pp_stream *s =
638       rzalloc(ctx->plb_pp_stream, struct lima_ctx_plb_pp_stream);
639 
640    list_inithead(&s->lru_list);
641    s->key.plb_index = ctx->plb_index;
642    s->key.minx = bound.minx;
643    s->key.maxx = bound.maxx;
644    s->key.miny = bound.miny;
645    s->key.maxy = bound.maxy;
646    s->key.shift_w = fb->shift_w;
647    s->key.shift_h = fb->shift_h;
648    s->key.block_w = fb->block_w;
649    s->key.block_h = fb->block_h;
650 
651    int tiled_w = bound.maxx - bound.minx;
652    int tiled_h = bound.maxy - bound.miny;
653    int size = lima_get_pp_stream_size(
654       screen->num_pp, tiled_w, tiled_h, s->offset);
655 
656    s->bo = lima_bo_create(screen, size, 0);
657 
658    ctx->pp_stream.map = lima_bo_map(s->bo);
659    ctx->pp_stream.va = s->bo->va;
660    memcpy(ctx->pp_stream.offset, s->offset, sizeof(s->offset));
661 
662    lima_generate_pp_stream(job, bound.minx, bound.miny, tiled_w, tiled_h);
663 
664    ctx->plb_stream_cache_size += size;
665    list_addtail(&s->lru_list, &ctx->plb_pp_stream_lru_list);
666    _mesa_hash_table_insert(ctx->plb_pp_stream, &s->key, s);
667 
668    lima_job_add_bo(job, LIMA_PIPE_PP, s->bo, LIMA_SUBMIT_BO_READ);
669 }
670 
671 static bool
lima_damage_fullscreen(struct lima_job * job)672 lima_damage_fullscreen(struct lima_job *job)
673 {
674    struct pipe_scissor_state *dr = &job->damage_rect;
675 
676    return dr->minx == 0 &&
677           dr->miny == 0 &&
678           dr->maxx == job->fb.width &&
679           dr->maxy == job->fb.height;
680 }
681 
682 static void
lima_update_pp_stream(struct lima_job * job)683 lima_update_pp_stream(struct lima_job *job)
684 {
685    struct lima_context *ctx = job->ctx;
686    struct lima_screen *screen = lima_screen(ctx->base.screen);
687    struct lima_damage_region *damage = lima_job_get_damage(job);
688    if ((screen->gpu_type == DRM_LIMA_PARAM_GPU_ID_MALI400) ||
689        (damage && damage->region) || !lima_damage_fullscreen(job))
690       lima_update_damage_pp_stream(job);
691    else
692       /* Mali450 doesn't need full PP stream */
693       ctx->pp_stream.map = NULL;
694 }
695 
696 static void
lima_update_job_bo(struct lima_job * job)697 lima_update_job_bo(struct lima_job *job)
698 {
699    struct lima_context *ctx = job->ctx;
700 
701    lima_job_add_bo(job, LIMA_PIPE_GP, ctx->plb_gp_stream,
702                       LIMA_SUBMIT_BO_READ);
703    lima_job_add_bo(job, LIMA_PIPE_GP, ctx->plb[ctx->plb_index],
704                       LIMA_SUBMIT_BO_WRITE);
705    lima_job_add_bo(job, LIMA_PIPE_GP, ctx->gp_tile_heap[ctx->plb_index],
706                       LIMA_SUBMIT_BO_WRITE);
707 
708    lima_dump_command_stream_print(
709       job->dump, ctx->plb_gp_stream->map + ctx->plb_index * ctx->plb_gp_size,
710       ctx->plb_gp_size, false, "gp plb stream at va %x\n",
711       ctx->plb_gp_stream->va + ctx->plb_index * ctx->plb_gp_size);
712 
713    lima_job_add_bo(job, LIMA_PIPE_PP, ctx->plb[ctx->plb_index],
714                       LIMA_SUBMIT_BO_READ);
715    lima_job_add_bo(job, LIMA_PIPE_PP, ctx->gp_tile_heap[ctx->plb_index],
716                       LIMA_SUBMIT_BO_READ);
717 
718    struct lima_screen *screen = lima_screen(ctx->base.screen);
719    lima_job_add_bo(job, LIMA_PIPE_PP, screen->pp_buffer, LIMA_SUBMIT_BO_READ);
720 }
721 
722 static void
lima_finish_plbu_cmd(struct util_dynarray * plbu_cmd_array)723 lima_finish_plbu_cmd(struct util_dynarray *plbu_cmd_array)
724 {
725    int i = 0;
726    uint32_t *plbu_cmd = util_dynarray_ensure_cap(plbu_cmd_array, plbu_cmd_array->size + 2 * 4);
727 
728    plbu_cmd[i++] = 0x00000000;
729    plbu_cmd[i++] = 0x50000000; /* END */
730 
731    plbu_cmd_array->size += i * 4;
732 }
733 
734 static void
lima_pack_wb_zsbuf_reg(struct lima_job * job,uint32_t * wb_reg,int wb_idx)735 lima_pack_wb_zsbuf_reg(struct lima_job *job, uint32_t *wb_reg, int wb_idx)
736 {
737    struct lima_job_fb_info *fb = &job->fb;
738    struct pipe_surface *zsbuf = job->key.zsbuf;
739    struct lima_resource *res = lima_resource(zsbuf->texture);
740    int level = zsbuf->u.tex.level;
741    uint32_t format = lima_format_get_pixel(zsbuf->format);
742 
743    struct lima_pp_wb_reg *wb = (void *)wb_reg;
744    wb[wb_idx].type = 0x01; /* 1 for depth, stencil */
745    wb[wb_idx].address = res->bo->va + res->levels[level].offset;
746    wb[wb_idx].pixel_format = format;
747    if (res->tiled) {
748       wb[wb_idx].pixel_layout = 0x2;
749       wb[wb_idx].pitch = fb->tiled_w;
750    } else {
751       wb[wb_idx].pixel_layout = 0x0;
752       wb[wb_idx].pitch = res->levels[level].stride / 8;
753    }
754    wb[wb_idx].flags = 0;
755    unsigned nr_samples = zsbuf->nr_samples ?
756                          zsbuf->nr_samples : MAX2(1, zsbuf->texture->nr_samples);
757    if (nr_samples > 1) {
758       wb[wb_idx].mrt_pitch = res->mrt_pitch;
759       wb[wb_idx].mrt_bits = u_bit_consecutive(0, nr_samples);
760    }
761 }
762 
763 static void
lima_pack_wb_cbuf_reg(struct lima_job * job,uint32_t * frame_reg,uint32_t * wb_reg,int wb_idx)764 lima_pack_wb_cbuf_reg(struct lima_job *job, uint32_t *frame_reg,
765                       uint32_t *wb_reg, int wb_idx)
766 {
767    struct lima_job_fb_info *fb = &job->fb;
768    struct pipe_surface *cbuf = job->key.cbuf;
769    struct lima_resource *res = lima_resource(cbuf->texture);
770    int level = cbuf->u.tex.level;
771    unsigned layer = cbuf->u.tex.first_layer;
772    uint32_t format = lima_format_get_pixel(cbuf->format);
773    bool swap_channels = lima_format_get_pixel_swap_rb(cbuf->format);
774 
775    struct lima_pp_frame_reg *frame = (void *)frame_reg;
776    frame->channel_layout = lima_format_get_channel_layout(cbuf->format);
777 
778    struct lima_pp_wb_reg *wb = (void *)wb_reg;
779    wb[wb_idx].type = 0x02; /* 2 for color buffer */
780    wb[wb_idx].address = res->bo->va + res->levels[level].offset + layer * res->levels[level].layer_stride;
781    wb[wb_idx].pixel_format = format;
782    if (res->tiled) {
783       wb[wb_idx].pixel_layout = 0x2;
784       wb[wb_idx].pitch = fb->tiled_w;
785    } else {
786       wb[wb_idx].pixel_layout = 0x0;
787       wb[wb_idx].pitch = res->levels[level].stride / 8;
788    }
789    wb[wb_idx].flags = swap_channels ? 0x4 : 0x0;
790    unsigned nr_samples = cbuf->nr_samples ?
791                          cbuf->nr_samples : MAX2(1, cbuf->texture->nr_samples);
792    if (nr_samples > 1) {
793       wb[wb_idx].mrt_pitch = res->mrt_pitch;
794       wb[wb_idx].mrt_bits = u_bit_consecutive(0, nr_samples);
795    }
796 }
797 
798 static void
lima_pack_pp_frame_reg(struct lima_job * job,uint32_t * frame_reg,uint32_t * wb_reg)799 lima_pack_pp_frame_reg(struct lima_job *job, uint32_t *frame_reg,
800                        uint32_t *wb_reg)
801 {
802    struct lima_context *ctx = job->ctx;
803    struct lima_job_fb_info *fb = &job->fb;
804    struct pipe_surface *cbuf = job->key.cbuf;
805    struct lima_pp_frame_reg *frame = (void *)frame_reg;
806    struct lima_screen *screen = lima_screen(ctx->base.screen);
807    int wb_idx = 0;
808 
809    frame->render_address = screen->pp_buffer->va + pp_frame_rsw_offset;
810    frame->flags = 0x02;
811    if (cbuf && util_format_is_float(cbuf->format)) {
812       frame->flags |= 0x01; /* enable fp16 */
813       frame->clear_value_color   = (uint32_t)(job->clear.color_16pc & 0xffffffffUL);
814       frame->clear_value_color_1 = (uint32_t)(job->clear.color_16pc >> 32);
815       frame->clear_value_color_2 = 0;
816       frame->clear_value_color_3 = 0;
817    }
818    else {
819       frame->clear_value_color   = job->clear.color_8pc;
820       frame->clear_value_color_1 = job->clear.color_8pc;
821       frame->clear_value_color_2 = job->clear.color_8pc;
822       frame->clear_value_color_3 = job->clear.color_8pc;
823    }
824 
825    frame->clear_value_depth = job->clear.depth;
826    frame->clear_value_stencil = job->clear.stencil;
827    frame->one = 1;
828 
829    frame->width = fb->width - 1;
830    frame->height = fb->height - 1;
831 
832    /* frame->fragment_stack_address is overwritten per-pp in the kernel
833     * by the values of pp_frame.fragment_stack_address[i] */
834 
835    /* These are "stack size" and "stack offset" shifted,
836     * here they are assumed to be always the same. */
837    frame->fragment_stack_size = job->pp_max_stack_size << 16 | job->pp_max_stack_size;
838 
839    /* related with MSAA and different value when r4p0/r7p0 */
840    frame->supersampled_height = fb->height * 2 - 1;
841    frame->scale = 0xE0C;
842 
843    frame->dubya = 0x77;
844    frame->onscreen = 1;
845    frame->blocking = (fb->shift_min << 28) | (fb->shift_h << 16) | fb->shift_w;
846 
847    /* Set default layout to 8888 */
848    frame->channel_layout = 0x8888;
849 
850    if (cbuf && (job->resolve & PIPE_CLEAR_COLOR0))
851       lima_pack_wb_cbuf_reg(job, frame_reg, wb_reg, wb_idx++);
852 
853    if (job->key.zsbuf &&
854        (job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)))
855       lima_pack_wb_zsbuf_reg(job, wb_reg, wb_idx++);
856 }
857 
858 void
lima_do_job(struct lima_job * job)859 lima_do_job(struct lima_job *job)
860 {
861    #define pp_stack_pp_size 0x400
862 
863    struct lima_context *ctx = job->ctx;
864 
865    lima_pack_head_plbu_cmd(job);
866    lima_finish_plbu_cmd(&job->plbu_cmd_array);
867 
868    lima_update_job_bo(job);
869 
870    int vs_cmd_size = job->vs_cmd_array.size;
871    uint32_t vs_cmd_va = 0;
872 
873    if (vs_cmd_size) {
874       void *vs_cmd = lima_job_create_stream_bo(
875          job, LIMA_PIPE_GP, vs_cmd_size, &vs_cmd_va);
876       memcpy(vs_cmd, util_dynarray_begin(&job->vs_cmd_array), vs_cmd_size);
877 
878       lima_dump_command_stream_print(
879          job->dump, vs_cmd, vs_cmd_size, false, "flush vs cmd at va %x\n", vs_cmd_va);
880       lima_dump_vs_command_stream_print(job->dump, vs_cmd, vs_cmd_size, vs_cmd_va);
881    }
882 
883    uint32_t plbu_cmd_va;
884    int plbu_cmd_size = job->plbu_cmd_array.size + job->plbu_cmd_head.size;
885    void *plbu_cmd = lima_job_create_stream_bo(
886       job, LIMA_PIPE_GP, plbu_cmd_size, &plbu_cmd_va);
887    memcpy(plbu_cmd,
888           util_dynarray_begin(&job->plbu_cmd_head),
889           job->plbu_cmd_head.size);
890    memcpy(plbu_cmd + job->plbu_cmd_head.size,
891           util_dynarray_begin(&job->plbu_cmd_array),
892           job->plbu_cmd_array.size);
893 
894    lima_dump_command_stream_print(
895       job->dump, plbu_cmd, plbu_cmd_size, false, "flush plbu cmd at va %x\n", plbu_cmd_va);
896    lima_dump_plbu_command_stream_print(job->dump, plbu_cmd, plbu_cmd_size, plbu_cmd_va);
897 
898    struct lima_screen *screen = lima_screen(ctx->base.screen);
899    struct drm_lima_gp_frame gp_frame;
900    struct lima_gp_frame_reg *gp_frame_reg = (void *)gp_frame.frame;
901    gp_frame_reg->vs_cmd_start = vs_cmd_va;
902    gp_frame_reg->vs_cmd_end = vs_cmd_va + vs_cmd_size;
903    gp_frame_reg->plbu_cmd_start = plbu_cmd_va;
904    gp_frame_reg->plbu_cmd_end = plbu_cmd_va + plbu_cmd_size;
905    gp_frame_reg->tile_heap_start = ctx->gp_tile_heap[ctx->plb_index]->va;
906    gp_frame_reg->tile_heap_end = ctx->gp_tile_heap[ctx->plb_index]->va + ctx->gp_tile_heap_size;
907 
908    lima_dump_command_stream_print(
909       job->dump, &gp_frame, sizeof(gp_frame), false, "add gp frame\n");
910 
911    if (!lima_job_start(job, LIMA_PIPE_GP, &gp_frame, sizeof(gp_frame)))
912       fprintf(stderr, "gp job error\n");
913 
914    if (job->dump) {
915       if (lima_job_wait(job, LIMA_PIPE_GP, OS_TIMEOUT_INFINITE)) {
916          if (ctx->gp_output) {
917             float *pos = lima_bo_map(ctx->gp_output);
918             lima_dump_command_stream_print(
919                job->dump, pos, 4 * 4 * 16, true, "gl_pos dump at va %x\n",
920                ctx->gp_output->va);
921          }
922 
923          uint32_t *plb = lima_bo_map(ctx->plb[ctx->plb_index]);
924          lima_dump_command_stream_print(
925             job->dump, plb, LIMA_CTX_PLB_BLK_SIZE, false, "plb dump at va %x\n",
926             ctx->plb[ctx->plb_index]->va);
927       }
928       else {
929          fprintf(stderr, "gp job wait error\n");
930          exit(1);
931       }
932    }
933 
934    uint32_t pp_stack_va = 0;
935    if (job->pp_max_stack_size) {
936       lima_job_create_stream_bo(
937          job, LIMA_PIPE_PP,
938          screen->num_pp * job->pp_max_stack_size * pp_stack_pp_size,
939          &pp_stack_va);
940    }
941 
942    lima_update_pp_stream(job);
943 
944    struct lima_pp_stream_state *ps = &ctx->pp_stream;
945    if (screen->gpu_type == DRM_LIMA_PARAM_GPU_ID_MALI400) {
946       struct drm_lima_m400_pp_frame pp_frame = {0};
947       lima_pack_pp_frame_reg(job, pp_frame.frame, pp_frame.wb);
948       pp_frame.num_pp = screen->num_pp;
949 
950       for (int i = 0; i < screen->num_pp; i++) {
951          pp_frame.plbu_array_address[i] = ps->va + ps->offset[i];
952          if (job->pp_max_stack_size)
953             pp_frame.fragment_stack_address[i] = pp_stack_va +
954                job->pp_max_stack_size * pp_stack_pp_size * i;
955       }
956 
957       lima_dump_command_stream_print(
958          job->dump, &pp_frame, sizeof(pp_frame), false, "add pp frame\n");
959 
960       if (!lima_job_start(job, LIMA_PIPE_PP, &pp_frame, sizeof(pp_frame)))
961          fprintf(stderr, "pp job error\n");
962    }
963    else {
964       struct drm_lima_m450_pp_frame pp_frame = {0};
965       lima_pack_pp_frame_reg(job, pp_frame.frame, pp_frame.wb);
966       pp_frame.num_pp = screen->num_pp;
967 
968       if (job->pp_max_stack_size)
969          for (int i = 0; i < screen->num_pp; i++)
970             pp_frame.fragment_stack_address[i] = pp_stack_va +
971                job->pp_max_stack_size * pp_stack_pp_size * i;
972 
973       if (ps->map) {
974          for (int i = 0; i < screen->num_pp; i++)
975             pp_frame.plbu_array_address[i] = ps->va + ps->offset[i];
976       }
977       else {
978          pp_frame.use_dlbu = true;
979 
980          struct lima_job_fb_info *fb = &job->fb;
981          pp_frame.dlbu_regs[0] = ctx->plb[ctx->plb_index]->va;
982          pp_frame.dlbu_regs[1] = ((fb->tiled_h - 1) << 16) | (fb->tiled_w - 1);
983          unsigned s = util_logbase2(LIMA_CTX_PLB_BLK_SIZE) - 7;
984          pp_frame.dlbu_regs[2] = (s << 28) | (fb->shift_h << 16) | fb->shift_w;
985          pp_frame.dlbu_regs[3] = ((fb->tiled_h - 1) << 24) | ((fb->tiled_w - 1) << 16);
986       }
987 
988       lima_dump_command_stream_print(
989          job->dump, &pp_frame, sizeof(pp_frame), false, "add pp frame\n");
990 
991       if (!lima_job_start(job, LIMA_PIPE_PP, &pp_frame, sizeof(pp_frame)))
992          fprintf(stderr, "pp job error\n");
993    }
994 
995    if (job->dump) {
996       if (!lima_job_wait(job, LIMA_PIPE_PP, OS_TIMEOUT_INFINITE)) {
997          fprintf(stderr, "pp wait error\n");
998          exit(1);
999       }
1000    }
1001 
1002    ctx->plb_index = (ctx->plb_index + 1) % lima_ctx_num_plb;
1003 
1004    /* Set reload flags for next draw. It'll be unset if buffer is cleared */
1005    if (job->key.cbuf && (job->resolve & PIPE_CLEAR_COLOR0)) {
1006       struct lima_surface *surf = lima_surface(job->key.cbuf);
1007       surf->reload |= PIPE_CLEAR_COLOR0;
1008    }
1009 
1010    if (job->key.zsbuf && (job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
1011       struct lima_surface *surf = lima_surface(job->key.zsbuf);
1012       surf->reload |= (job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL));
1013    }
1014 
1015    if (ctx->job == job)
1016       ctx->job = NULL;
1017 
1018    lima_job_free(job);
1019 }
1020 
1021 void
lima_flush(struct lima_context * ctx)1022 lima_flush(struct lima_context *ctx)
1023 {
1024    hash_table_foreach(ctx->jobs, entry) {
1025       struct lima_job *job = entry->data;
1026       lima_do_job(job);
1027    }
1028 }
1029 
1030 void
lima_flush_job_accessing_bo(struct lima_context * ctx,struct lima_bo * bo,bool write)1031 lima_flush_job_accessing_bo(
1032    struct lima_context *ctx, struct lima_bo *bo, bool write)
1033 {
1034    hash_table_foreach(ctx->jobs, entry) {
1035       struct lima_job *job = entry->data;
1036       if (lima_job_has_bo(job, bo, write))
1037          lima_do_job(job);
1038    }
1039 }
1040 
1041 /*
1042  * This is for current job flush previous job which write to the resource it wants
1043  * to read. Tipical usage is flush the FBO which is used as current task's texture.
1044  */
1045 void
lima_flush_previous_job_writing_resource(struct lima_context * ctx,struct pipe_resource * prsc)1046 lima_flush_previous_job_writing_resource(
1047    struct lima_context *ctx, struct pipe_resource *prsc)
1048 {
1049    struct hash_entry *entry = _mesa_hash_table_search(ctx->write_jobs, prsc);
1050 
1051    if (entry) {
1052       struct lima_job *job = entry->data;
1053 
1054       /* do not flush current job */
1055       if (job != ctx->job)
1056          lima_do_job(job);
1057    }
1058 }
1059 
1060 static void
lima_pipe_flush(struct pipe_context * pctx,struct pipe_fence_handle ** fence,unsigned flags)1061 lima_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
1062                 unsigned flags)
1063 {
1064    struct lima_context *ctx = lima_context(pctx);
1065 
1066    lima_flush(ctx);
1067 
1068    if (fence) {
1069       int drm_fd = lima_screen(ctx->base.screen)->fd;
1070       int fd;
1071 
1072       if (!drmSyncobjExportSyncFile(drm_fd, ctx->out_sync[LIMA_PIPE_PP], &fd))
1073          *fence = lima_fence_create(fd);
1074    }
1075 }
1076 
1077 static void
lima_texture_barrier(struct pipe_context * pctx,unsigned flags)1078 lima_texture_barrier(struct pipe_context *pctx, unsigned flags)
1079 {
1080     struct lima_context *ctx = lima_context(pctx);
1081 
1082     lima_flush(ctx);
1083 }
1084 
1085 static bool
lima_job_compare(const void * s1,const void * s2)1086 lima_job_compare(const void *s1, const void *s2)
1087 {
1088    return memcmp(s1, s2, sizeof(struct lima_job_key)) == 0;
1089 }
1090 
1091 static uint32_t
lima_job_hash(const void * key)1092 lima_job_hash(const void *key)
1093 {
1094    return _mesa_hash_data(key, sizeof(struct lima_job_key));
1095 }
1096 
lima_job_init(struct lima_context * ctx)1097 bool lima_job_init(struct lima_context *ctx)
1098 {
1099    int fd = lima_screen(ctx->base.screen)->fd;
1100 
1101    ctx->jobs = _mesa_hash_table_create(ctx, lima_job_hash, lima_job_compare);
1102    if (!ctx->jobs)
1103       return false;
1104 
1105    ctx->write_jobs = _mesa_hash_table_create(
1106       ctx, _mesa_hash_pointer, _mesa_key_pointer_equal);
1107    if (!ctx->write_jobs)
1108       return false;
1109 
1110    ctx->in_sync_fd = -1;
1111 
1112    for (int i = 0; i < 2; i++) {
1113       if (drmSyncobjCreate(fd, DRM_SYNCOBJ_CREATE_SIGNALED, ctx->in_sync + i) ||
1114           drmSyncobjCreate(fd, DRM_SYNCOBJ_CREATE_SIGNALED, ctx->out_sync + i))
1115          return false;
1116    }
1117 
1118    ctx->base.flush = lima_pipe_flush;
1119    ctx->base.texture_barrier = lima_texture_barrier;
1120 
1121    return true;
1122 }
1123 
lima_job_fini(struct lima_context * ctx)1124 void lima_job_fini(struct lima_context *ctx)
1125 {
1126    int fd = lima_screen(ctx->base.screen)->fd;
1127 
1128    lima_flush(ctx);
1129 
1130    for (int i = 0; i < 2; i++) {
1131       if (ctx->in_sync[i])
1132          drmSyncobjDestroy(fd, ctx->in_sync[i]);
1133       if (ctx->out_sync[i])
1134          drmSyncobjDestroy(fd, ctx->out_sync[i]);
1135    }
1136 
1137    if (ctx->in_sync_fd >= 0)
1138       close(ctx->in_sync_fd);
1139 }
1140