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