Path: blob/21.2-virgl/src/gallium/drivers/lima/lima_job.c
4565 views
/*1* Copyright (C) 2017-2019 Lima Project2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*21*/2223#include <stdlib.h>24#include <string.h>2526#include "xf86drm.h"27#include "drm-uapi/lima_drm.h"2829#include "util/u_math.h"30#include "util/ralloc.h"31#include "util/os_time.h"32#include "util/hash_table.h"33#include "util/format/u_format.h"34#include "util/u_upload_mgr.h"35#include "util/u_inlines.h"3637#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"4748#define VOID2U64(x) ((uint64_t)(unsigned long)(x))4950static void51lima_get_fb_info(struct lima_job *job)52{53struct lima_context *ctx = job->ctx;54struct lima_job_fb_info *fb = &job->fb;5556fb->width = ctx->framebuffer.base.width;57fb->height = ctx->framebuffer.base.height;5859int width = align(fb->width, 16) >> 4;60int height = align(fb->height, 16) >> 4;6162struct lima_screen *screen = lima_screen(ctx->base.screen);6364fb->tiled_w = width;65fb->tiled_h = height;6667fb->shift_h = 0;68fb->shift_w = 0;6970int limit = screen->plb_max_blk;71while ((width * height) > limit) {72if (width >= height) {73width = (width + 1) >> 1;74fb->shift_w++;75} else {76height = (height + 1) >> 1;77fb->shift_h++;78}79}8081fb->block_w = width;82fb->block_h = height;8384fb->shift_min = MIN3(fb->shift_w, fb->shift_h, 2);85}8687static struct lima_job *88lima_job_create(struct lima_context *ctx)89{90struct lima_job *s;9192s = rzalloc(ctx, struct lima_job);93if (!s)94return NULL;9596s->fd = lima_screen(ctx->base.screen)->fd;97s->ctx = ctx;9899s->damage_rect.minx = s->damage_rect.miny = 0xffff;100s->damage_rect.maxx = s->damage_rect.maxy = 0;101s->draws = 0;102103s->clear.depth = 0x00ffffff;104105for (int i = 0; i < 2; i++) {106util_dynarray_init(s->gem_bos + i, s);107util_dynarray_init(s->bos + i, s);108}109110util_dynarray_init(&s->vs_cmd_array, s);111util_dynarray_init(&s->plbu_cmd_array, s);112util_dynarray_init(&s->plbu_cmd_head, s);113114struct lima_context_framebuffer *fb = &ctx->framebuffer;115pipe_surface_reference(&s->key.cbuf, fb->base.cbufs[0]);116pipe_surface_reference(&s->key.zsbuf, fb->base.zsbuf);117118lima_get_fb_info(s);119120s->dump = lima_dump_create();121122return s;123}124125static void126lima_job_free(struct lima_job *job)127{128struct lima_context *ctx = job->ctx;129130_mesa_hash_table_remove_key(ctx->jobs, &job->key);131132if (job->key.cbuf && (job->resolve & PIPE_CLEAR_COLOR0))133_mesa_hash_table_remove_key(ctx->write_jobs, job->key.cbuf->texture);134if (job->key.zsbuf && (job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)))135_mesa_hash_table_remove_key(ctx->write_jobs, job->key.zsbuf->texture);136137pipe_surface_reference(&job->key.cbuf, NULL);138pipe_surface_reference(&job->key.zsbuf, NULL);139140lima_dump_free(job->dump);141job->dump = NULL;142143/* TODO: do we need a cache for job? */144ralloc_free(job);145}146147static struct lima_job *148_lima_job_get(struct lima_context *ctx)149{150struct lima_context_framebuffer *fb = &ctx->framebuffer;151struct lima_job_key local_key = {152.cbuf = fb->base.cbufs[0],153.zsbuf = fb->base.zsbuf,154};155156struct hash_entry *entry = _mesa_hash_table_search(ctx->jobs, &local_key);157if (entry)158return entry->data;159160struct lima_job *job = lima_job_create(ctx);161if (!job)162return NULL;163164_mesa_hash_table_insert(ctx->jobs, &job->key, job);165166return job;167}168169/*170* Note: this function can only be called in draw code path,171* must not exist in flush code path.172*/173struct lima_job *174lima_job_get(struct lima_context *ctx)175{176if (ctx->job)177return ctx->job;178179ctx->job = _lima_job_get(ctx);180return ctx->job;181}182183bool lima_job_add_bo(struct lima_job *job, int pipe,184struct lima_bo *bo, uint32_t flags)185{186util_dynarray_foreach(job->gem_bos + pipe, struct drm_lima_gem_submit_bo, gem_bo) {187if (bo->handle == gem_bo->handle) {188gem_bo->flags |= flags;189return true;190}191}192193struct drm_lima_gem_submit_bo *job_bo =194util_dynarray_grow(job->gem_bos + pipe, struct drm_lima_gem_submit_bo, 1);195job_bo->handle = bo->handle;196job_bo->flags = flags;197198struct lima_bo **jbo = util_dynarray_grow(job->bos + pipe, struct lima_bo *, 1);199*jbo = bo;200201/* prevent bo from being freed when job start */202lima_bo_reference(bo);203204return true;205}206207static bool208lima_job_start(struct lima_job *job, int pipe, void *frame, uint32_t size)209{210struct lima_context *ctx = job->ctx;211struct 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};220221if (ctx->in_sync_fd >= 0) {222int err = drmSyncobjImportSyncFile(job->fd, ctx->in_sync[pipe],223ctx->in_sync_fd);224if (err)225return false;226227req.in_sync[0] = ctx->in_sync[pipe];228close(ctx->in_sync_fd);229ctx->in_sync_fd = -1;230}231232bool ret = drmIoctl(job->fd, DRM_IOCTL_LIMA_GEM_SUBMIT, &req) == 0;233234util_dynarray_foreach(job->bos + pipe, struct lima_bo *, bo) {235lima_bo_unreference(*bo);236}237238return ret;239}240241static bool242lima_job_wait(struct lima_job *job, int pipe, uint64_t timeout_ns)243{244int64_t abs_timeout = os_time_get_absolute_timeout(timeout_ns);245if (abs_timeout == OS_TIMEOUT_INFINITE)246abs_timeout = INT64_MAX;247248struct lima_context *ctx = job->ctx;249return !drmSyncobjWait(job->fd, ctx->out_sync + pipe, 1, abs_timeout, 0, NULL);250}251252static bool253lima_job_has_bo(struct lima_job *job, struct lima_bo *bo, bool all)254{255for (int i = 0; i < 2; i++) {256util_dynarray_foreach(job->gem_bos + i, struct drm_lima_gem_submit_bo, gem_bo) {257if (bo->handle == gem_bo->handle) {258if (all || gem_bo->flags & LIMA_SUBMIT_BO_WRITE)259return true;260else261break;262}263}264}265266return false;267}268269void *270lima_job_create_stream_bo(struct lima_job *job, int pipe,271unsigned size, uint32_t *va)272{273struct lima_context *ctx = job->ctx;274275void *cpu;276unsigned offset;277struct pipe_resource *pres = NULL;278u_upload_alloc(ctx->uploader, 0, size, 0x40, &offset, &pres, &cpu);279280struct lima_resource *res = lima_resource(pres);281*va = res->bo->va + offset;282283lima_job_add_bo(job, pipe, res->bo, LIMA_SUBMIT_BO_READ);284285pipe_resource_reference(&pres, NULL);286287return cpu;288}289290static inline struct lima_damage_region *291lima_job_get_damage(struct lima_job *job)292{293if (!(job->key.cbuf && (job->resolve & PIPE_CLEAR_COLOR0)))294return NULL;295296struct lima_surface *surf = lima_surface(job->key.cbuf);297struct lima_resource *res = lima_resource(surf->base.texture);298return &res->damage;299}300301static bool302lima_fb_cbuf_needs_reload(struct lima_job *job)303{304if (!job->key.cbuf)305return false;306307struct lima_surface *surf = lima_surface(job->key.cbuf);308struct lima_resource *res = lima_resource(surf->base.texture);309if (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 reload312* the region not aligned to tile boundary */313//if (!res->damage.aligned)314// return true;315return true;316}317else if (surf->reload & PIPE_CLEAR_COLOR0)318return true;319320return false;321}322323static bool324lima_fb_zsbuf_needs_reload(struct lima_job *job)325{326if (!job->key.zsbuf)327return false;328329struct lima_surface *surf = lima_surface(job->key.zsbuf);330if (surf->reload & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))331return true;332333return false;334}335336static void337lima_pack_reload_plbu_cmd(struct lima_job *job, struct pipe_surface *psurf)338{339#define lima_reload_render_state_offset 0x0000340#define lima_reload_gl_pos_offset 0x0040341#define lima_reload_varying_offset 0x0080342#define lima_reload_tex_desc_offset 0x00c0343#define lima_reload_tex_array_offset 0x0100344#define lima_reload_buffer_size 0x0140345346struct lima_context *ctx = job->ctx;347struct lima_surface *surf = lima_surface(psurf);348int level = psurf->u.tex.level;349unsigned first_layer = psurf->u.tex.first_layer;350351uint32_t va;352void *cpu = lima_job_create_stream_bo(353job, LIMA_PIPE_PP, lima_reload_buffer_size, &va);354355struct lima_screen *screen = lima_screen(ctx->base.screen);356357uint32_t reload_shader_first_instr_size =358((uint32_t *)(screen->pp_buffer->map + pp_reload_program_offset))[0] & 0x1f;359uint32_t reload_shader_va = screen->pp_buffer->va + pp_reload_program_offset;360361struct lima_render_state reload_render_state = {362.alpha_blend = 0xf03b1ad2,363.depth_test = 0x0000000e,364.depth_range = 0xffff0000,365.stencil_front = 0x00000007,366.stencil_back = 0x00000007,367.multi_sample = 0x0000f007,368.shader_address = reload_shader_va | reload_shader_first_instr_size,369.varying_types = 0x00000001,370.textures_address = va + lima_reload_tex_array_offset,371.aux0 = 0x00004021,372.varyings_address = va + lima_reload_varying_offset,373};374375if (util_format_is_depth_or_stencil(psurf->format)) {376reload_render_state.alpha_blend &= 0x0fffffff;377if (psurf->format != PIPE_FORMAT_Z16_UNORM)378reload_render_state.depth_test |= 0x400;379if (surf->reload & PIPE_CLEAR_DEPTH)380reload_render_state.depth_test |= 0x801;381if (surf->reload & PIPE_CLEAR_STENCIL) {382reload_render_state.depth_test |= 0x1000;383reload_render_state.stencil_front = 0x0000024f;384reload_render_state.stencil_back = 0x0000024f;385reload_render_state.stencil_test = 0x0000ffff;386}387}388389memcpy(cpu + lima_reload_render_state_offset, &reload_render_state,390sizeof(reload_render_state));391392lima_tex_desc *td = cpu + lima_reload_tex_desc_offset;393memset(td, 0, lima_min_tex_desc_size);394lima_texture_desc_set_res(ctx, td, psurf->texture, level, level, first_layer);395td->format = lima_format_get_texel_reload(psurf->format);396td->unnorm_coords = 1;397td->texture_type = LIMA_TEXTURE_TYPE_2D;398td->min_img_filter_nearest = 1;399td->mag_img_filter_nearest = 1;400td->wrap_s_clamp_to_edge = 1;401td->wrap_t_clamp_to_edge = 1;402td->unknown_2_2 = 0x1;403404uint32_t *ta = cpu + lima_reload_tex_array_offset;405ta[0] = va + lima_reload_tex_desc_offset;406407struct lima_job_fb_info *fb = &job->fb;408float reload_gl_pos[] = {409fb->width, 0, 0, 1,4100, 0, 0, 1,4110, fb->height, 0, 1,412};413memcpy(cpu + lima_reload_gl_pos_offset, reload_gl_pos,414sizeof(reload_gl_pos));415416float reload_varying[] = {417fb->width, 0, 0, 0,4180, fb->height, 0, 0,419};420memcpy(cpu + lima_reload_varying_offset, reload_varying,421sizeof(reload_varying));422423PLBU_CMD_BEGIN(&job->plbu_cmd_head, 20);424425PLBU_CMD_VIEWPORT_LEFT(0);426PLBU_CMD_VIEWPORT_RIGHT(fui(fb->width));427PLBU_CMD_VIEWPORT_BOTTOM(0);428PLBU_CMD_VIEWPORT_TOP(fui(fb->height));429430PLBU_CMD_RSW_VERTEX_ARRAY(431va + lima_reload_render_state_offset,432va + lima_reload_gl_pos_offset);433434PLBU_CMD_UNKNOWN2();435PLBU_CMD_UNKNOWN1();436437PLBU_CMD_INDICES(screen->pp_buffer->va + pp_shared_index_offset);438PLBU_CMD_INDEXED_DEST(va + lima_reload_gl_pos_offset);439PLBU_CMD_DRAW_ELEMENTS(0xf, 0, 3);440441PLBU_CMD_END();442443lima_dump_command_stream_print(job->dump, cpu, lima_reload_buffer_size,444false, "reload plbu cmd at va %x\n", va);445}446447static void448lima_pack_head_plbu_cmd(struct lima_job *job)449{450struct lima_context *ctx = job->ctx;451struct lima_job_fb_info *fb = &job->fb;452453PLBU_CMD_BEGIN(&job->plbu_cmd_head, 10);454455PLBU_CMD_UNKNOWN2();456PLBU_CMD_BLOCK_STEP(fb->shift_min, fb->shift_h, fb->shift_w);457PLBU_CMD_TILED_DIMENSIONS(fb->tiled_w, fb->tiled_h);458PLBU_CMD_BLOCK_STRIDE(fb->block_w);459460PLBU_CMD_ARRAY_ADDRESS(461ctx->plb_gp_stream->va + ctx->plb_index * ctx->plb_gp_size,462fb->block_w * fb->block_h);463464PLBU_CMD_END();465466if (lima_fb_cbuf_needs_reload(job))467lima_pack_reload_plbu_cmd(job, job->key.cbuf);468469if (lima_fb_zsbuf_needs_reload(job))470lima_pack_reload_plbu_cmd(job, job->key.zsbuf);471}472473static void474hilbert_rotate(int n, int *x, int *y, int rx, int ry)475{476if (ry == 0) {477if (rx == 1) {478*x = n-1 - *x;479*y = n-1 - *y;480}481482/* Swap x and y */483int t = *x;484*x = *y;485*y = t;486}487}488489static void490hilbert_coords(int n, int d, int *x, int *y)491{492int rx, ry, i, t=d;493494*x = *y = 0;495496for (i = 0; (1 << i) < n; i++) {497498rx = 1 & (t / 2);499ry = 1 & (t ^ rx);500501hilbert_rotate(1 << i, x, y, rx, ry);502503*x += rx << i;504*y += ry << i;505506t /= 4;507}508}509510static int511lima_get_pp_stream_size(int num_pp, int tiled_w, int tiled_h, uint32_t *off)512{513/* carefully calculate each stream start address:514* 1. overflow: each stream size may be different due to515* fb->tiled_w * fb->tiled_h can't be divided by num_pp,516* extra size should be added to the preceeding stream517* 2. alignment: each stream address should be 0x20 aligned518*/519int delta = tiled_w * tiled_h / num_pp * 16 + 16;520int remain = tiled_w * tiled_h % num_pp;521int offset = 0;522523for (int i = 0; i < num_pp; i++) {524off[i] = offset;525526offset += delta;527if (remain) {528offset += 16;529remain--;530}531offset = align(offset, 0x20);532}533534return offset;535}536537static void538lima_generate_pp_stream(struct lima_job *job, int off_x, int off_y,539int tiled_w, int tiled_h)540{541struct lima_context *ctx = job->ctx;542struct lima_pp_stream_state *ps = &ctx->pp_stream;543struct lima_job_fb_info *fb = &job->fb;544struct lima_screen *screen = lima_screen(ctx->base.screen);545int i, num_pp = screen->num_pp;546547/* use hilbert_coords to generates 1D to 2D relationship.548* 1D for pp stream index and 2D for plb block x/y on framebuffer.549* if multi pp, interleave the 1D index to make each pp's render target550* close enough which should result close workload551*/552int max = MAX2(tiled_w, tiled_h);553int index = 0;554uint32_t *stream[4];555int si[4] = {0};556int dim = 0;557int count = 0;558559/* Don't update count if we get zero rect. We'll just generate560* PP stream with just terminators in it.561*/562if ((tiled_w * tiled_h) != 0) {563dim = util_logbase2_ceil(max);564count = 1 << (dim + dim);565}566567for (i = 0; i < num_pp; i++)568stream[i] = ps->map + ps->offset[i];569570for (i = 0; i < count; i++) {571int x, y;572hilbert_coords(max, i, &x, &y);573if (x < tiled_w && y < tiled_h) {574x += off_x;575y += off_y;576577int pp = index % num_pp;578int offset = ((y >> fb->shift_h) * fb->block_w +579(x >> fb->shift_w)) * LIMA_CTX_PLB_BLK_SIZE;580int plb_va = ctx->plb[ctx->plb_index]->va + offset;581582stream[pp][si[pp]++] = 0;583stream[pp][si[pp]++] = 0xB8000000 | x | (y << 8);584stream[pp][si[pp]++] = 0xE0000002 | ((plb_va >> 3) & ~0xE0000003);585stream[pp][si[pp]++] = 0xB0000000;586587index++;588}589}590591for (i = 0; i < num_pp; i++) {592stream[i][si[i]++] = 0;593stream[i][si[i]++] = 0xBC000000;594stream[i][si[i]++] = 0;595stream[i][si[i]++] = 0;596597lima_dump_command_stream_print(598job->dump, stream[i], si[i] * 4,599false, "pp plb stream %d at va %x\n",600i, ps->va + ps->offset[i]);601}602}603604static void605lima_free_stale_pp_stream_bo(struct lima_context *ctx)606{607list_for_each_entry_safe(struct lima_ctx_plb_pp_stream, entry,608&ctx->plb_pp_stream_lru_list, lru_list) {609if (ctx->plb_stream_cache_size <= lima_plb_pp_stream_cache_size)610break;611612struct hash_entry *hash_entry =613_mesa_hash_table_search(ctx->plb_pp_stream, &entry->key);614if (hash_entry)615_mesa_hash_table_remove(ctx->plb_pp_stream, hash_entry);616list_del(&entry->lru_list);617618ctx->plb_stream_cache_size -= entry->bo->size;619lima_bo_unreference(entry->bo);620621ralloc_free(entry);622}623}624625static void626lima_update_damage_pp_stream(struct lima_job *job)627{628struct lima_context *ctx = job->ctx;629struct lima_damage_region *ds = lima_job_get_damage(job);630struct lima_job_fb_info *fb = &job->fb;631struct pipe_scissor_state bound;632struct pipe_scissor_state *dr = &job->damage_rect;633634if (ds && ds->region) {635struct pipe_scissor_state *dbound = &ds->bound;636bound.minx = MAX2(dbound->minx, dr->minx >> 4);637bound.miny = MAX2(dbound->miny, dr->miny >> 4);638bound.maxx = MIN2(dbound->maxx, (dr->maxx + 0xf) >> 4);639bound.maxy = MIN2(dbound->maxy, (dr->maxy + 0xf) >> 4);640} else {641bound.minx = dr->minx >> 4;642bound.miny = dr->miny >> 4;643bound.maxx = (dr->maxx + 0xf) >> 4;644bound.maxy = (dr->maxy + 0xf) >> 4;645}646647/* Clamp to FB size */648bound.minx = MIN2(bound.minx, fb->tiled_w);649bound.miny = MIN2(bound.miny, fb->tiled_h);650bound.maxx = MIN2(bound.maxx, fb->tiled_w);651bound.maxy = MIN2(bound.maxy, fb->tiled_h);652653struct lima_ctx_plb_pp_stream_key key = {654.plb_index = ctx->plb_index,655.minx = bound.minx,656.miny = bound.miny,657.maxx = bound.maxx,658.maxy = bound.maxy,659.shift_w = fb->shift_w,660.shift_h = fb->shift_h,661.block_w = fb->block_w,662.block_h = fb->block_h,663};664665struct hash_entry *entry =666_mesa_hash_table_search(ctx->plb_pp_stream, &key);667if (entry) {668struct lima_ctx_plb_pp_stream *s = entry->data;669670list_del(&s->lru_list);671list_addtail(&s->lru_list, &ctx->plb_pp_stream_lru_list);672673ctx->pp_stream.map = lima_bo_map(s->bo);674ctx->pp_stream.va = s->bo->va;675memcpy(ctx->pp_stream.offset, s->offset, sizeof(s->offset));676677lima_job_add_bo(job, LIMA_PIPE_PP, s->bo, LIMA_SUBMIT_BO_READ);678679return;680}681682lima_free_stale_pp_stream_bo(ctx);683684struct lima_screen *screen = lima_screen(ctx->base.screen);685struct lima_ctx_plb_pp_stream *s =686rzalloc(ctx->plb_pp_stream, struct lima_ctx_plb_pp_stream);687688list_inithead(&s->lru_list);689s->key.plb_index = ctx->plb_index;690s->key.minx = bound.minx;691s->key.maxx = bound.maxx;692s->key.miny = bound.miny;693s->key.maxy = bound.maxy;694s->key.shift_w = fb->shift_w;695s->key.shift_h = fb->shift_h;696s->key.block_w = fb->block_w;697s->key.block_h = fb->block_h;698699int tiled_w = bound.maxx - bound.minx;700int tiled_h = bound.maxy - bound.miny;701int size = lima_get_pp_stream_size(702screen->num_pp, tiled_w, tiled_h, s->offset);703704s->bo = lima_bo_create(screen, size, 0);705706ctx->pp_stream.map = lima_bo_map(s->bo);707ctx->pp_stream.va = s->bo->va;708memcpy(ctx->pp_stream.offset, s->offset, sizeof(s->offset));709710lima_generate_pp_stream(job, bound.minx, bound.miny, tiled_w, tiled_h);711712ctx->plb_stream_cache_size += size;713list_addtail(&s->lru_list, &ctx->plb_pp_stream_lru_list);714_mesa_hash_table_insert(ctx->plb_pp_stream, &s->key, s);715716lima_job_add_bo(job, LIMA_PIPE_PP, s->bo, LIMA_SUBMIT_BO_READ);717}718719static bool720lima_damage_fullscreen(struct lima_job *job)721{722struct pipe_scissor_state *dr = &job->damage_rect;723724return dr->minx == 0 &&725dr->miny == 0 &&726dr->maxx == job->fb.width &&727dr->maxy == job->fb.height;728}729730static void731lima_update_pp_stream(struct lima_job *job)732{733struct lima_context *ctx = job->ctx;734struct lima_screen *screen = lima_screen(ctx->base.screen);735struct lima_damage_region *damage = lima_job_get_damage(job);736if ((screen->gpu_type == DRM_LIMA_PARAM_GPU_ID_MALI400) ||737(damage && damage->region) || !lima_damage_fullscreen(job))738lima_update_damage_pp_stream(job);739else740/* Mali450 doesn't need full PP stream */741ctx->pp_stream.map = NULL;742}743744static void745lima_update_job_bo(struct lima_job *job)746{747struct lima_context *ctx = job->ctx;748749lima_job_add_bo(job, LIMA_PIPE_GP, ctx->plb_gp_stream,750LIMA_SUBMIT_BO_READ);751lima_job_add_bo(job, LIMA_PIPE_GP, ctx->plb[ctx->plb_index],752LIMA_SUBMIT_BO_WRITE);753lima_job_add_bo(job, LIMA_PIPE_GP, ctx->gp_tile_heap[ctx->plb_index],754LIMA_SUBMIT_BO_WRITE);755756lima_dump_command_stream_print(757job->dump, ctx->plb_gp_stream->map + ctx->plb_index * ctx->plb_gp_size,758ctx->plb_gp_size, false, "gp plb stream at va %x\n",759ctx->plb_gp_stream->va + ctx->plb_index * ctx->plb_gp_size);760761lima_job_add_bo(job, LIMA_PIPE_PP, ctx->plb[ctx->plb_index],762LIMA_SUBMIT_BO_READ);763lima_job_add_bo(job, LIMA_PIPE_PP, ctx->gp_tile_heap[ctx->plb_index],764LIMA_SUBMIT_BO_READ);765766struct lima_screen *screen = lima_screen(ctx->base.screen);767lima_job_add_bo(job, LIMA_PIPE_PP, screen->pp_buffer, LIMA_SUBMIT_BO_READ);768}769770static void771lima_finish_plbu_cmd(struct util_dynarray *plbu_cmd_array)772{773int i = 0;774uint32_t *plbu_cmd = util_dynarray_ensure_cap(plbu_cmd_array, plbu_cmd_array->size + 2 * 4);775776plbu_cmd[i++] = 0x00000000;777plbu_cmd[i++] = 0x50000000; /* END */778779plbu_cmd_array->size += i * 4;780}781782static void783lima_pack_wb_zsbuf_reg(struct lima_job *job, uint32_t *wb_reg, int wb_idx)784{785struct lima_job_fb_info *fb = &job->fb;786struct pipe_surface *zsbuf = job->key.zsbuf;787struct lima_resource *res = lima_resource(zsbuf->texture);788int level = zsbuf->u.tex.level;789uint32_t format = lima_format_get_pixel(zsbuf->format);790791struct lima_pp_wb_reg *wb = (void *)wb_reg;792wb[wb_idx].type = 0x01; /* 1 for depth, stencil */793wb[wb_idx].address = res->bo->va + res->levels[level].offset;794wb[wb_idx].pixel_format = format;795if (res->tiled) {796wb[wb_idx].pixel_layout = 0x2;797wb[wb_idx].pitch = fb->tiled_w;798} else {799wb[wb_idx].pixel_layout = 0x0;800wb[wb_idx].pitch = res->levels[level].stride / 8;801}802wb[wb_idx].mrt_bits = 0;803}804805static void806lima_pack_wb_cbuf_reg(struct lima_job *job, uint32_t *frame_reg,807uint32_t *wb_reg, int wb_idx)808{809struct lima_job_fb_info *fb = &job->fb;810struct pipe_surface *cbuf = job->key.cbuf;811struct lima_resource *res = lima_resource(cbuf->texture);812int level = cbuf->u.tex.level;813unsigned layer = cbuf->u.tex.first_layer;814uint32_t format = lima_format_get_pixel(cbuf->format);815bool swap_channels = lima_format_get_pixel_swap_rb(cbuf->format);816817struct lima_pp_frame_reg *frame = (void *)frame_reg;818frame->channel_layout = lima_format_get_channel_layout(cbuf->format);819820struct lima_pp_wb_reg *wb = (void *)wb_reg;821wb[wb_idx].type = 0x02; /* 2 for color buffer */822wb[wb_idx].address = res->bo->va + res->levels[level].offset + layer * res->levels[level].layer_stride;823wb[wb_idx].pixel_format = format;824if (res->tiled) {825wb[wb_idx].pixel_layout = 0x2;826wb[wb_idx].pitch = fb->tiled_w;827} else {828wb[wb_idx].pixel_layout = 0x0;829wb[wb_idx].pitch = res->levels[level].stride / 8;830}831wb[wb_idx].mrt_bits = swap_channels ? 0x4 : 0x0;832}833834static void835lima_pack_pp_frame_reg(struct lima_job *job, uint32_t *frame_reg,836uint32_t *wb_reg)837{838struct lima_context *ctx = job->ctx;839struct lima_job_fb_info *fb = &job->fb;840struct pipe_surface *cbuf = job->key.cbuf;841struct lima_pp_frame_reg *frame = (void *)frame_reg;842struct lima_screen *screen = lima_screen(ctx->base.screen);843int wb_idx = 0;844845frame->render_address = screen->pp_buffer->va + pp_frame_rsw_offset;846frame->flags = 0x02;847if (cbuf && util_format_is_float(cbuf->format)) {848frame->flags |= 0x01; /* enable fp16 */849frame->clear_value_color = (uint32_t)(job->clear.color_16pc & 0xffffffffUL);850frame->clear_value_color_1 = (uint32_t)(job->clear.color_16pc >> 32);851frame->clear_value_color_2 = 0;852frame->clear_value_color_3 = 0;853}854else {855frame->clear_value_color = job->clear.color_8pc;856frame->clear_value_color_1 = job->clear.color_8pc;857frame->clear_value_color_2 = job->clear.color_8pc;858frame->clear_value_color_3 = job->clear.color_8pc;859}860861frame->clear_value_depth = job->clear.depth;862frame->clear_value_stencil = job->clear.stencil;863frame->one = 1;864865frame->width = fb->width - 1;866frame->height = fb->height - 1;867868/* frame->fragment_stack_address is overwritten per-pp in the kernel869* by the values of pp_frame.fragment_stack_address[i] */870871/* These are "stack size" and "stack offset" shifted,872* here they are assumed to be always the same. */873frame->fragment_stack_size = job->pp_max_stack_size << 16 | job->pp_max_stack_size;874875/* related with MSAA and different value when r4p0/r7p0 */876frame->supersampled_height = fb->height * 2 - 1;877frame->scale = 0xE0C;878879frame->dubya = 0x77;880frame->onscreen = 1;881frame->blocking = (fb->shift_min << 28) | (fb->shift_h << 16) | fb->shift_w;882883/* Set default layout to 8888 */884frame->channel_layout = 0x8888;885886if (cbuf && (job->resolve & PIPE_CLEAR_COLOR0))887lima_pack_wb_cbuf_reg(job, frame_reg, wb_reg, wb_idx++);888889if (job->key.zsbuf &&890(job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)))891lima_pack_wb_zsbuf_reg(job, wb_reg, wb_idx++);892}893894void895lima_do_job(struct lima_job *job)896{897#define pp_stack_pp_size 0x400898899struct lima_context *ctx = job->ctx;900901lima_pack_head_plbu_cmd(job);902lima_finish_plbu_cmd(&job->plbu_cmd_array);903904lima_update_job_bo(job);905906int vs_cmd_size = job->vs_cmd_array.size;907uint32_t vs_cmd_va = 0;908909if (vs_cmd_size) {910void *vs_cmd = lima_job_create_stream_bo(911job, LIMA_PIPE_GP, vs_cmd_size, &vs_cmd_va);912memcpy(vs_cmd, util_dynarray_begin(&job->vs_cmd_array), vs_cmd_size);913914lima_dump_command_stream_print(915job->dump, vs_cmd, vs_cmd_size, false, "flush vs cmd at va %x\n", vs_cmd_va);916lima_dump_vs_command_stream_print(job->dump, vs_cmd, vs_cmd_size, vs_cmd_va);917}918919uint32_t plbu_cmd_va;920int plbu_cmd_size = job->plbu_cmd_array.size + job->plbu_cmd_head.size;921void *plbu_cmd = lima_job_create_stream_bo(922job, LIMA_PIPE_GP, plbu_cmd_size, &plbu_cmd_va);923memcpy(plbu_cmd,924util_dynarray_begin(&job->plbu_cmd_head),925job->plbu_cmd_head.size);926memcpy(plbu_cmd + job->plbu_cmd_head.size,927util_dynarray_begin(&job->plbu_cmd_array),928job->plbu_cmd_array.size);929930lima_dump_command_stream_print(931job->dump, plbu_cmd, plbu_cmd_size, false, "flush plbu cmd at va %x\n", plbu_cmd_va);932lima_dump_plbu_command_stream_print(job->dump, plbu_cmd, plbu_cmd_size, plbu_cmd_va);933934struct lima_screen *screen = lima_screen(ctx->base.screen);935struct drm_lima_gp_frame gp_frame;936struct lima_gp_frame_reg *gp_frame_reg = (void *)gp_frame.frame;937gp_frame_reg->vs_cmd_start = vs_cmd_va;938gp_frame_reg->vs_cmd_end = vs_cmd_va + vs_cmd_size;939gp_frame_reg->plbu_cmd_start = plbu_cmd_va;940gp_frame_reg->plbu_cmd_end = plbu_cmd_va + plbu_cmd_size;941gp_frame_reg->tile_heap_start = ctx->gp_tile_heap[ctx->plb_index]->va;942gp_frame_reg->tile_heap_end = ctx->gp_tile_heap[ctx->plb_index]->va + ctx->gp_tile_heap_size;943944lima_dump_command_stream_print(945job->dump, &gp_frame, sizeof(gp_frame), false, "add gp frame\n");946947if (!lima_job_start(job, LIMA_PIPE_GP, &gp_frame, sizeof(gp_frame)))948fprintf(stderr, "gp job error\n");949950if (job->dump) {951if (lima_job_wait(job, LIMA_PIPE_GP, PIPE_TIMEOUT_INFINITE)) {952if (ctx->gp_output) {953float *pos = lima_bo_map(ctx->gp_output);954lima_dump_command_stream_print(955job->dump, pos, 4 * 4 * 16, true, "gl_pos dump at va %x\n",956ctx->gp_output->va);957}958959uint32_t *plb = lima_bo_map(ctx->plb[ctx->plb_index]);960lima_dump_command_stream_print(961job->dump, plb, LIMA_CTX_PLB_BLK_SIZE, false, "plb dump at va %x\n",962ctx->plb[ctx->plb_index]->va);963}964else {965fprintf(stderr, "gp job wait error\n");966exit(1);967}968}969970uint32_t pp_stack_va = 0;971if (job->pp_max_stack_size) {972lima_job_create_stream_bo(973job, LIMA_PIPE_PP,974screen->num_pp * job->pp_max_stack_size * pp_stack_pp_size,975&pp_stack_va);976}977978lima_update_pp_stream(job);979980struct lima_pp_stream_state *ps = &ctx->pp_stream;981if (screen->gpu_type == DRM_LIMA_PARAM_GPU_ID_MALI400) {982struct drm_lima_m400_pp_frame pp_frame = {0};983lima_pack_pp_frame_reg(job, pp_frame.frame, pp_frame.wb);984pp_frame.num_pp = screen->num_pp;985986for (int i = 0; i < screen->num_pp; i++) {987pp_frame.plbu_array_address[i] = ps->va + ps->offset[i];988if (job->pp_max_stack_size)989pp_frame.fragment_stack_address[i] = pp_stack_va +990job->pp_max_stack_size * pp_stack_pp_size * i;991}992993lima_dump_command_stream_print(994job->dump, &pp_frame, sizeof(pp_frame), false, "add pp frame\n");995996if (!lima_job_start(job, LIMA_PIPE_PP, &pp_frame, sizeof(pp_frame)))997fprintf(stderr, "pp job error\n");998}999else {1000struct drm_lima_m450_pp_frame pp_frame = {0};1001lima_pack_pp_frame_reg(job, pp_frame.frame, pp_frame.wb);1002pp_frame.num_pp = screen->num_pp;10031004if (job->pp_max_stack_size)1005for (int i = 0; i < screen->num_pp; i++)1006pp_frame.fragment_stack_address[i] = pp_stack_va +1007job->pp_max_stack_size * pp_stack_pp_size * i;10081009if (ps->map) {1010for (int i = 0; i < screen->num_pp; i++)1011pp_frame.plbu_array_address[i] = ps->va + ps->offset[i];1012}1013else {1014pp_frame.use_dlbu = true;10151016struct lima_job_fb_info *fb = &job->fb;1017pp_frame.dlbu_regs[0] = ctx->plb[ctx->plb_index]->va;1018pp_frame.dlbu_regs[1] = ((fb->tiled_h - 1) << 16) | (fb->tiled_w - 1);1019unsigned s = util_logbase2(LIMA_CTX_PLB_BLK_SIZE) - 7;1020pp_frame.dlbu_regs[2] = (s << 28) | (fb->shift_h << 16) | fb->shift_w;1021pp_frame.dlbu_regs[3] = ((fb->tiled_h - 1) << 24) | ((fb->tiled_w - 1) << 16);1022}10231024lima_dump_command_stream_print(1025job->dump, &pp_frame, sizeof(pp_frame), false, "add pp frame\n");10261027if (!lima_job_start(job, LIMA_PIPE_PP, &pp_frame, sizeof(pp_frame)))1028fprintf(stderr, "pp job error\n");1029}10301031if (job->dump) {1032if (!lima_job_wait(job, LIMA_PIPE_PP, PIPE_TIMEOUT_INFINITE)) {1033fprintf(stderr, "pp wait error\n");1034exit(1);1035}1036}10371038ctx->plb_index = (ctx->plb_index + 1) % lima_ctx_num_plb;10391040/* Set reload flags for next draw. It'll be unset if buffer is cleared */1041if (job->key.cbuf && (job->resolve & PIPE_CLEAR_COLOR0)) {1042struct lima_surface *surf = lima_surface(job->key.cbuf);1043surf->reload = PIPE_CLEAR_COLOR0;1044}10451046if (job->key.zsbuf && (job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {1047struct lima_surface *surf = lima_surface(job->key.zsbuf);1048surf->reload = (job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL));1049}10501051if (ctx->job == job)1052ctx->job = NULL;10531054lima_job_free(job);1055}10561057void1058lima_flush(struct lima_context *ctx)1059{1060hash_table_foreach(ctx->jobs, entry) {1061struct lima_job *job = entry->data;1062lima_do_job(job);1063}1064}10651066void1067lima_flush_job_accessing_bo(1068struct lima_context *ctx, struct lima_bo *bo, bool write)1069{1070hash_table_foreach(ctx->jobs, entry) {1071struct lima_job *job = entry->data;1072if (lima_job_has_bo(job, bo, write))1073lima_do_job(job);1074}1075}10761077/*1078* This is for current job flush previous job which write to the resource it wants1079* to read. Tipical usage is flush the FBO which is used as current task's texture.1080*/1081void1082lima_flush_previous_job_writing_resource(1083struct lima_context *ctx, struct pipe_resource *prsc)1084{1085struct hash_entry *entry = _mesa_hash_table_search(ctx->write_jobs, prsc);10861087if (entry) {1088struct lima_job *job = entry->data;10891090/* do not flush current job */1091if (job != ctx->job)1092lima_do_job(job);1093}1094}10951096static void1097lima_pipe_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,1098unsigned flags)1099{1100struct lima_context *ctx = lima_context(pctx);11011102lima_flush(ctx);11031104if (fence) {1105int drm_fd = lima_screen(ctx->base.screen)->fd;1106int fd;11071108if (!drmSyncobjExportSyncFile(drm_fd, ctx->out_sync[LIMA_PIPE_PP], &fd))1109*fence = lima_fence_create(fd);1110}1111}11121113static bool1114lima_job_compare(const void *s1, const void *s2)1115{1116return memcmp(s1, s2, sizeof(struct lima_job_key)) == 0;1117}11181119static uint32_t1120lima_job_hash(const void *key)1121{1122return _mesa_hash_data(key, sizeof(struct lima_job_key));1123}11241125bool lima_job_init(struct lima_context *ctx)1126{1127int fd = lima_screen(ctx->base.screen)->fd;11281129ctx->jobs = _mesa_hash_table_create(ctx, lima_job_hash, lima_job_compare);1130if (!ctx->jobs)1131return false;11321133ctx->write_jobs = _mesa_hash_table_create(1134ctx, _mesa_hash_pointer, _mesa_key_pointer_equal);1135if (!ctx->write_jobs)1136return false;11371138ctx->in_sync_fd = -1;11391140for (int i = 0; i < 2; i++) {1141if (drmSyncobjCreate(fd, DRM_SYNCOBJ_CREATE_SIGNALED, ctx->in_sync + i) ||1142drmSyncobjCreate(fd, DRM_SYNCOBJ_CREATE_SIGNALED, ctx->out_sync + i))1143return false;1144}11451146ctx->base.flush = lima_pipe_flush;11471148return true;1149}11501151void lima_job_fini(struct lima_context *ctx)1152{1153int fd = lima_screen(ctx->base.screen)->fd;11541155lima_flush(ctx);11561157for (int i = 0; i < 2; i++) {1158if (ctx->in_sync[i])1159drmSyncobjDestroy(fd, ctx->in_sync[i]);1160if (ctx->out_sync[i])1161drmSyncobjDestroy(fd, ctx->out_sync[i]);1162}11631164if (ctx->in_sync_fd >= 0)1165close(ctx->in_sync_fd);1166}116711681169