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