| 1 |
/* |
| 2 |
* Copyright © 2024 Collabora Ltd. |
| 3 |
* Copyright © 2024 Arm Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
#include "drm-uapi/panthor_drm.h" |
| 7 |
|
| 8 |
#include "panvk_cmd_buffer.h" |
| 9 |
#include "panvk_device.h" |
| 10 |
|
| 11 |
static enum cs_reg_perm |
| 12 |
tiler_oom_reg_perm_cb(struct cs_builder *b, unsigned reg) |
| 13 |
{ |
| 14 |
switch (reg) { |
| 15 |
/* The bbox is set up by the fragment subqueue, we should not modify it. */ |
| 16 |
case 42: |
| 17 |
case 43: |
| 18 |
/* We should only load from the subqueue context. */ |
| 19 |
case PANVK_CS_REG_SUBQUEUE_CTX_START: |
| 20 |
case PANVK_CS_REG_SUBQUEUE_CTX_END: |
| 21 |
return CS_REG_RD; |
| 22 |
} |
| 23 |
return CS_REG_RW; |
| 24 |
} |
| 25 |
|
| 26 |
static void |
| 27 |
copy_fbd(struct cs_builder *b, bool has_zs_ext, uint32_t rt_count, |
| 28 |
struct cs_index src_tiler, struct cs_index src_other, |
| 29 |
struct cs_index dst) |
| 30 |
{ |
| 31 |
/* Copy the FBD from src to dst. Most words come from |
| 32 |
* src_other, but the tiler desc pointer is taken from src_tiler. |
| 33 |
*/ |
| 34 |
cs_load_to(b, cs_scratch_reg_tuple(b, 0, 8), src_other, |
| 35 |
BITFIELD_MASK(8), 0); |
| 36 |
cs_store(b, cs_scratch_reg_tuple(b, 0, 8), dst, |
| 37 |
BITFIELD_MASK(8), 0); |
| 38 |
cs_load_to(b, cs_scratch_reg_tuple(b, 0, 6), src_other, |
| 39 |
BITFIELD_MASK(6), 8 * sizeof(uint32_t)); |
| 40 |
cs_load64_to(b, cs_scratch_reg64(b, 6), src_tiler, |
| 41 |
14 * sizeof(uint32_t)); |
| 42 |
cs_store(b, cs_scratch_reg_tuple(b, 0, 8), dst, BITFIELD_MASK(8), |
| 43 |
8 * sizeof(uint32_t)); |
| 44 |
|
| 45 |
if (has_zs_ext) { |
| 46 |
const uint16_t dbd_offset = sizeof(struct mali_framebuffer_packed); |
| 47 |
|
| 48 |
/* Copy the whole DBD. */ |
| 49 |
cs_load_to(b, cs_scratch_reg_tuple(b, 0, 8), src_other, |
| 50 |
BITFIELD_MASK(8), dbd_offset); |
| 51 |
cs_store(b, cs_scratch_reg_tuple(b, 0, 8), dst, |
| 52 |
BITFIELD_MASK(8), dbd_offset); |
| 53 |
cs_load_to(b, cs_scratch_reg_tuple(b, 0, 8), src_other, |
| 54 |
BITFIELD_MASK(8), dbd_offset + (8 * sizeof(uint32_t))); |
| 55 |
cs_store(b, cs_scratch_reg_tuple(b, 0, 8), dst, |
| 56 |
BITFIELD_MASK(8), dbd_offset + (8 * sizeof(uint32_t))); |
| 57 |
} |
| 58 |
|
| 59 |
const uint16_t rts_offset = |
| 60 |
sizeof(struct mali_framebuffer_packed) + |
| 61 |
(has_zs_ext ? sizeof(struct mali_zs_crc_extension_packed) : 0); |
| 62 |
|
| 63 |
for (uint32_t rt = 0; rt < rt_count; rt++) { |
| 64 |
const uint16_t rt_offset = |
| 65 |
rts_offset + (rt * sizeof(struct mali_render_target_packed)); |
| 66 |
|
| 67 |
/* Copy the whole RTD. */ |
| 68 |
cs_load_to(b, cs_scratch_reg_tuple(b, 0, 8), src_other, |
| 69 |
BITFIELD_MASK(8), rt_offset); |
| 70 |
cs_store(b, cs_scratch_reg_tuple(b, 0, 8), dst, |
| 71 |
BITFIELD_MASK(8), rt_offset); |
| 72 |
cs_load_to(b, cs_scratch_reg_tuple(b, 0, 8), src_other, |
| 73 |
BITFIELD_MASK(8), rt_offset + (8 * sizeof(uint32_t))); |
| 74 |
cs_store(b, cs_scratch_reg_tuple(b, 0, 8), dst, |
| 75 |
BITFIELD_MASK(8), rt_offset + (8 * sizeof(uint32_t))); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static size_t |
| 80 |
generate_tiler_oom_handler(struct panvk_device *dev, |
| 81 |
struct cs_buffer handler_mem, bool has_zs_ext, |
| 82 |
uint32_t rt_count, bool tracing_enabled, |
| 83 |
uint32_t *dump_region_size) |
| 84 |
{ |
| 85 |
assert(rt_count >= 1 && rt_count <= MAX_RTS); |
| 86 |
|
| 87 |
const uint32_t fbd_size = get_fbd_size(has_zs_ext, rt_count); |
| 88 |
|
| 89 |
const struct drm_panthor_csif_info *csif_info = |
| 90 |
panthor_kmod_get_csif_props(dev->kmod.dev); |
| 91 |
|
| 92 |
struct cs_builder b; |
| 93 |
struct cs_builder_conf conf = { |
| 94 |
.nr_registers = csif_info->cs_reg_count, |
| 95 |
.nr_kernel_registers = MAX2(csif_info->unpreserved_cs_reg_count, 4), |
| 96 |
.reg_perm = tiler_oom_reg_perm_cb, |
| 97 |
.ls_sb_slot = SB_ID(LS), |
| 98 |
}; |
| 99 |
cs_builder_init(&b, &conf, handler_mem); |
| 100 |
|
| 101 |
struct cs_function handler; |
| 102 |
struct cs_function_ctx handler_ctx = { |
| 103 |
.ctx_reg = cs_subqueue_ctx_reg(&b), |
| 104 |
.dump_addr_offset = |
| 105 |
offsetof(struct panvk_cs_subqueue_context, reg_dump_addr), |
| 106 |
}; |
| 107 |
struct cs_tracing_ctx tracing_ctx = { |
| 108 |
.enabled = tracing_enabled, |
| 109 |
.ctx_reg = cs_subqueue_ctx_reg(&b), |
| 110 |
.tracebuf_addr_offset = |
| 111 |
offsetof(struct panvk_cs_subqueue_context, debug.tracebuf.cs), |
| 112 |
}; |
| 113 |
struct mali_framebuffer_pointer_packed fb_tag; |
| 114 |
|
| 115 |
pan_pack(&fb_tag, FRAMEBUFFER_POINTER, cfg) { |
| 116 |
cfg.zs_crc_extension_present = has_zs_ext; |
| 117 |
cfg.render_target_count = rt_count; |
| 118 |
} |
| 119 |
|
| 120 |
cs_function_def(&b, &handler, handler_ctx) { |
| 121 |
struct cs_index subqueue_ctx = cs_subqueue_ctx_reg(&b); |
| 122 |
|
| 123 |
struct cs_index zero = cs_scratch_reg64(&b, 0); |
| 124 |
/* Have flush_id read part of the double zero register */ |
| 125 |
struct cs_index flush_id = cs_scratch_reg32(&b, 0); |
| 126 |
|
| 127 |
struct cs_index completed_chunks = cs_scratch_reg_tuple(&b, 2, 4); |
| 128 |
struct cs_index completed_top = cs_scratch_reg64(&b, 2); |
| 129 |
struct cs_index completed_bottom = cs_scratch_reg64(&b, 4); |
| 130 |
struct cs_index td_count = cs_scratch_reg32(&b, 6); |
| 131 |
|
| 132 |
/* FBD patching registers */ |
| 133 |
struct cs_index layer_count = cs_scratch_reg32(&b, 8); |
| 134 |
struct cs_index ir_count = cs_scratch_reg32(&b, 9); |
| 135 |
struct cs_index scratch_fbd_ptr_reg = cs_scratch_reg64(&b, 10); |
| 136 |
struct cs_index current_fbd_ptr_reg = cs_scratch_reg64(&b, 12); |
| 137 |
struct cs_index ir_descs_ptr = cs_scratch_reg64(&b, 14); |
| 138 |
|
| 139 |
/* Run fragment registers will only be used after FBD patching */ |
| 140 |
struct cs_index run_fragment_regs = cs_scratch_reg_tuple(&b, 0, 4); |
| 141 |
|
| 142 |
/* The tiler pointer is pre-filled. */ |
| 143 |
struct cs_index tiler_ptr = cs_reg64(&b, 38); |
| 144 |
|
| 145 |
cs_load64_to(&b, scratch_fbd_ptr_reg, subqueue_ctx, |
| 146 |
TILER_OOM_CTX_FIELD_OFFSET(ir_scratch_fbd_ptr)); |
| 147 |
cs_load32_to(&b, ir_count, subqueue_ctx, |
| 148 |
TILER_OOM_CTX_FIELD_OFFSET(counter)); |
| 149 |
cs_load32_to(&b, layer_count, subqueue_ctx, |
| 150 |
TILER_OOM_CTX_FIELD_OFFSET(layer_count)); |
| 151 |
cs_load64_to(&b, current_fbd_ptr_reg, subqueue_ctx, |
| 152 |
TILER_OOM_CTX_FIELD_OFFSET(layer_fbd_ptr)); |
| 153 |
|
| 154 |
/* Use different framebuffer descriptor depending on whether incremental |
| 155 |
* rendering has already been triggered */ |
| 156 |
cs_if(&b, MALI_CS_CONDITION_GREATER, ir_count) { |
| 157 |
cs_load64_to( |
| 158 |
&b, ir_descs_ptr, subqueue_ctx, |
| 159 |
TILER_OOM_CTX_FIELD_OFFSET(ir_descs[PANVK_IR_MIDDLE_PASS])); |
| 160 |
} |
| 161 |
cs_else(&b) { |
| 162 |
cs_load64_to( |
| 163 |
&b, ir_descs_ptr, subqueue_ctx, |
| 164 |
TILER_OOM_CTX_FIELD_OFFSET(ir_descs[PANVK_IR_FIRST_PASS])); |
| 165 |
} |
| 166 |
|
| 167 |
cs_wait_slot(&b, SB_ID(LS)); |
| 168 |
|
| 169 |
cs_while(&b, MALI_CS_CONDITION_GREATER, layer_count) { |
| 170 |
cs_add32(&b, layer_count, layer_count, -1); |
| 171 |
|
| 172 |
copy_fbd(&b, has_zs_ext, rt_count, current_fbd_ptr_reg, ir_descs_ptr, |
| 173 |
scratch_fbd_ptr_reg); |
| 174 |
|
| 175 |
/* Flush copies before the RUN_FRAGMENT. */ |
| 176 |
cs_wait_slot(&b, SB_ID(LS)); |
| 177 |
|
| 178 |
/* Set FBD pointer to the scratch fbd */ |
| 179 |
cs_add64(&b, cs_sr_reg64(&b, FRAGMENT, FBD_POINTER), |
| 180 |
scratch_fbd_ptr_reg, fb_tag.opaque[0]); |
| 181 |
|
| 182 |
cs_trace_run_fragment(&b, &tracing_ctx, run_fragment_regs, false, |
| 183 |
MALI_TILE_RENDER_ORDER_Z_ORDER); |
| 184 |
|
| 185 |
/* Serialize run fragments since we reuse FBD for the runs */ |
| 186 |
cs_wait_slots(&b, dev->csf.sb.all_iters_mask); |
| 187 |
|
| 188 |
cs_add64(&b, current_fbd_ptr_reg, current_fbd_ptr_reg, fbd_size); |
| 189 |
cs_add64(&b, ir_descs_ptr, ir_descs_ptr, fbd_size); |
| 190 |
} |
| 191 |
|
| 192 |
cs_load32_to(&b, td_count, subqueue_ctx, |
| 193 |
TILER_OOM_CTX_FIELD_OFFSET(td_count)); |
| 194 |
cs_move64_to(&b, zero, 0); |
| 195 |
|
| 196 |
cs_while(&b, MALI_CS_CONDITION_GREATER, td_count) { |
| 197 |
/* Load completed chunks */ |
| 198 |
cs_load_to(&b, completed_chunks, tiler_ptr, BITFIELD_MASK(4), 10 * 4); |
| 199 |
|
| 200 |
cs_finish_fragment(&b, false, completed_top, completed_bottom, |
| 201 |
cs_now()); |
| 202 |
|
| 203 |
/* Zero out polygon list, completed_top and completed_bottom */ |
| 204 |
cs_store64(&b, zero, tiler_ptr, 0); |
| 205 |
cs_store64(&b, zero, tiler_ptr, 10 * 4); |
| 206 |
cs_store64(&b, zero, tiler_ptr, 12 * 4); |
| 207 |
|
| 208 |
cs_add64(&b, tiler_ptr, tiler_ptr, pan_size(TILER_CONTEXT)); |
| 209 |
cs_add32(&b, td_count, td_count, -1); |
| 210 |
} |
| 211 |
|
| 212 |
/* If this is the first IR call, we need to patch the regular FBD |
| 213 |
* to use the last IR config. |
| 214 |
*/ |
| 215 |
cs_if(&b, MALI_CS_CONDITION_EQUAL, ir_count) { |
| 216 |
cs_load64_to(&b, current_fbd_ptr_reg, subqueue_ctx, |
| 217 |
TILER_OOM_CTX_FIELD_OFFSET(layer_fbd_ptr)); |
| 218 |
cs_load64_to(&b, ir_descs_ptr, subqueue_ctx, |
| 219 |
TILER_OOM_CTX_FIELD_OFFSET(ir_descs[PANVK_IR_LAST_PASS])); |
| 220 |
cs_load32_to(&b, layer_count, subqueue_ctx, |
| 221 |
TILER_OOM_CTX_FIELD_OFFSET(layer_count)); |
| 222 |
|
| 223 |
cs_while(&b, MALI_CS_CONDITION_GREATER, layer_count) { |
| 224 |
cs_add32(&b, layer_count, layer_count, -1); |
| 225 |
|
| 226 |
/* Preserve the tiler pointer, take the rest from the |
| 227 |
* last IR config. |
| 228 |
*/ |
| 229 |
copy_fbd(&b, has_zs_ext, rt_count, current_fbd_ptr_reg, |
| 230 |
ir_descs_ptr, current_fbd_ptr_reg); |
| 231 |
|
| 232 |
cs_add64(&b, current_fbd_ptr_reg, current_fbd_ptr_reg, fbd_size); |
| 233 |
cs_add64(&b, ir_descs_ptr, ir_descs_ptr, fbd_size); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/* Increment IR counter */ |
| 238 |
cs_add32(&b, ir_count, ir_count, 1); |
| 239 |
cs_store32(&b, ir_count, subqueue_ctx, |
| 240 |
TILER_OOM_CTX_FIELD_OFFSET(counter)); |
| 241 |
cs_wait_slot(&b, SB_ID(LS)); |
| 242 |
|
| 243 |
/* We need to flush the texture caches so future preloads see the new |
| 244 |
* content. */ |
| 245 |
cs_flush_caches(&b, MALI_CS_FLUSH_MODE_NONE, MALI_CS_FLUSH_MODE_NONE, |
| 246 |
MALI_CS_OTHER_FLUSH_MODE_INVALIDATE, flush_id, |
| 247 |
cs_defer(SB_IMM_MASK, SB_ID(IMM_FLUSH))); |
| 248 |
|
| 249 |
cs_wait_slot(&b, SB_ID(IMM_FLUSH)); |
| 250 |
} |
| 251 |
|
| 252 |
assert(cs_is_valid(&b)); |
| 253 |
cs_end(&b); |
| 254 |
cs_builder_fini(&b); |
| 255 |
*dump_region_size = handler.dump_size; |
| 256 |
|
| 257 |
return handler.length * sizeof(uint64_t); |
| 258 |
} |
| 259 |
|
| 260 |
#define TILER_OOM_HANDLER_MAX_SIZE 2048 |
| 261 |
VkResult |
| 262 |
panvk_per_arch(init_tiler_oom)(struct panvk_device *device) |
| 263 |
{ |
| 264 |
const bool tracing_enabled = PANVK_DEBUG(TRACE); |
| 265 |
VkResult result = panvk_priv_bo_create( |
| 266 |
device, TILER_OOM_HANDLER_MAX_SIZE * 2 * MAX_RTS, |
| 267 |
panvk_device_adjust_bo_flags(device, PAN_KMOD_BO_FLAG_WB_MMAP), |
| 268 |
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE, &device->tiler_oom.handlers_bo); |
| 269 |
if (result != VK_SUCCESS) |
| 270 |
return result; |
| 271 |
|
| 272 |
for (uint32_t zs_ext = 0; zs_ext <= 1; zs_ext++) { |
| 273 |
for (uint32_t rt_count = 1; rt_count <= MAX_RTS; rt_count++) { |
| 274 |
uint32_t idx = get_tiler_oom_handler_idx(zs_ext, rt_count); |
| 275 |
size_t offset = idx * TILER_OOM_HANDLER_MAX_SIZE; |
| 276 |
|
| 277 |
struct cs_buffer handler_mem = { |
| 278 |
.cpu = device->tiler_oom.handlers_bo->addr.host + offset, |
| 279 |
.gpu = device->tiler_oom.handlers_bo->addr.dev + offset, |
| 280 |
.capacity = TILER_OOM_HANDLER_MAX_SIZE / sizeof(uint64_t), |
| 281 |
}; |
| 282 |
|
| 283 |
uint32_t dump_region_size; |
| 284 |
size_t handler_length = |
| 285 |
generate_tiler_oom_handler(device, handler_mem, zs_ext, rt_count, |
| 286 |
tracing_enabled, &dump_region_size); |
| 287 |
|
| 288 |
/* Use memset(0) to make sure the remaining space is filled with NOP |
| 289 |
* instructions. */ |
| 290 |
assert(handler_length <= TILER_OOM_HANDLER_MAX_SIZE); |
| 291 |
memset((uint8_t *)handler_mem.cpu + handler_length, 0, |
| 292 |
TILER_OOM_HANDLER_MAX_SIZE - handler_length); |
| 293 |
device->dump_region_size[PANVK_SUBQUEUE_FRAGMENT] = |
| 294 |
MAX2(device->dump_region_size[PANVK_SUBQUEUE_FRAGMENT], |
| 295 |
dump_region_size); |
| 296 |
} |
| 297 |
device->tiler_oom.handler_stride = TILER_OOM_HANDLER_MAX_SIZE; |
| 298 |
} |
| 299 |
|
| 300 |
panvk_priv_bo_flush(device->tiler_oom.handlers_bo, 0, |
| 301 |
pan_kmod_bo_size(device->tiler_oom.handlers_bo->bo)); |
| 302 |
|
| 303 |
return result; |
| 304 |
} |