|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Copyright (C) 2025 Etnaviv Project |
| 4 | + */ |
| 5 | + |
| 6 | +#include <linux/errno.h> |
| 7 | +#include <linux/dev_printk.h> |
| 8 | +#include <linux/string.h> |
| 9 | +#include <linux/types.h> |
| 10 | + |
| 11 | +#include "etnaviv_buffer.h" |
| 12 | +#include "etnaviv_cmdbuf.h" |
| 13 | +#include "etnaviv_gpu.h" |
| 14 | +#include "state_3d.xml.h" |
| 15 | + |
| 16 | +#include "etnaviv_flop_reset.h" |
| 17 | + |
| 18 | +#define PPU_IMAGE_STRIDE 64 |
| 19 | +#define PPU_IMAGE_XSIZE 64 |
| 20 | +#define PPU_IMAGE_YSIZE 6 |
| 21 | + |
| 22 | +#define PPU_FLOP_RESET_INSTR_DWORD_COUNT 16 |
| 23 | + |
| 24 | +static void etnaviv_emit_flop_reset_state_ppu(struct etnaviv_cmdbuf *cmdbuf, |
| 25 | + u32 buffer_base, u32 input_offset, |
| 26 | + u32 output_offset, |
| 27 | + u32 shader_offset, |
| 28 | + u32 shader_size, |
| 29 | + u32 shader_register_count) |
| 30 | +{ |
| 31 | + CMD_LOAD_STATE(cmdbuf, VIVS_GL_API_MODE, VIVS_GL_API_MODE_OPENCL); |
| 32 | + CMD_SEM(cmdbuf, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE); |
| 33 | + CMD_STALL(cmdbuf, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE); |
| 34 | + |
| 35 | + CMD_LOAD_STATES_START(cmdbuf, VIVS_SH_HALTI5_UNIFORMS(0), 4); |
| 36 | + |
| 37 | + OUT(cmdbuf, buffer_base + input_offset); |
| 38 | + OUT(cmdbuf, PPU_IMAGE_STRIDE); |
| 39 | + OUT(cmdbuf, PPU_IMAGE_XSIZE | (PPU_IMAGE_YSIZE << 16)); |
| 40 | + OUT(cmdbuf, 0x444051f0); |
| 41 | + OUT(cmdbuf, 0xffffffff); |
| 42 | + |
| 43 | + CMD_LOAD_STATES_START(cmdbuf, VIVS_SH_HALTI5_UNIFORMS(4), 4); |
| 44 | + OUT(cmdbuf, buffer_base + output_offset); |
| 45 | + OUT(cmdbuf, PPU_IMAGE_STRIDE); |
| 46 | + OUT(cmdbuf, PPU_IMAGE_XSIZE | (PPU_IMAGE_YSIZE << 16)); |
| 47 | + OUT(cmdbuf, 0x444051f0); |
| 48 | + OUT(cmdbuf, 0xffffffff); |
| 49 | + |
| 50 | + CMD_LOAD_STATE(cmdbuf, VIVS_CL_CONFIG, |
| 51 | + VIVS_CL_CONFIG_DIMENSIONS(2) | |
| 52 | + VIVS_CL_CONFIG_VALUE_ORDER(3)); |
| 53 | + CMD_LOAD_STATE(cmdbuf, VIVS_VS_ICACHE_INVALIDATE, 0x1f); |
| 54 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_VARYING_NUM_COMPONENTS(0), 0); |
| 55 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_TEMP_REGISTER_CONTROL, |
| 56 | + shader_register_count); |
| 57 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_SAMPLER_BASE, 0x0); |
| 58 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_UNIFORM_BASE, 0x0); |
| 59 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_NEWRANGE_LOW, 0x0); |
| 60 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_NEWRANGE_HIGH, shader_size / 16); |
| 61 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_INST_ADDR, buffer_base + shader_offset); |
| 62 | + CMD_LOAD_STATE(cmdbuf, VIVS_SH_CONFIG, VIVS_SH_CONFIG_RTNE_ROUNDING); |
| 63 | + CMD_LOAD_STATE(cmdbuf, VIVS_VS_ICACHE_CONTROL, |
| 64 | + VIVS_VS_ICACHE_CONTROL_ENABLE); |
| 65 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_ICACHE_COUNT, shader_size / 16 - 1); |
| 66 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_INPUT_COUNT, 0x1f01); |
| 67 | + CMD_LOAD_STATE(cmdbuf, VIVS_VS_HALTI5_UNK008A0, 0x0); |
| 68 | + CMD_LOAD_STATE(cmdbuf, VIVS_PA_VS_OUTPUT_COUNT, 0x0); |
| 69 | + CMD_LOAD_STATE(cmdbuf, VIVS_GL_VARYING_TOTAL_COMPONENTS, 0x0); |
| 70 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_CONTROL_EXT, 0x0); |
| 71 | + CMD_LOAD_STATE(cmdbuf, VIVS_VS_OUTPUT_COUNT, 0x1); |
| 72 | + CMD_LOAD_STATE(cmdbuf, VIVS_GL_HALTI5_SH_SPECIALS, 0x0); |
| 73 | + CMD_LOAD_STATE(cmdbuf, VIVS_PS_ICACHE_PREFETCH, 0x0); |
| 74 | + CMD_LOAD_STATE(cmdbuf, VIVS_CL_UNK00924, 0x0); |
| 75 | + CMD_LOAD_STATE(cmdbuf, VIVS_CL_THREAD_ALLOCATION, 0x1); |
| 76 | + |
| 77 | + CMD_LOAD_STATE(cmdbuf, VIVS_CL_GLOBAL_WORK_OFFSET_X, 0x0); |
| 78 | + CMD_LOAD_STATE(cmdbuf, VIVS_CL_GLOBAL_WORK_OFFSET_Y, 0x0); |
| 79 | + CMD_LOAD_STATE(cmdbuf, VIVS_CL_GLOBAL_WORK_OFFSET_Z, 0x0); |
| 80 | + |
| 81 | + CMD_LOAD_STATES_START(cmdbuf, VIVS_CL_WORKGROUP_COUNT_X, 9); |
| 82 | + OUT(cmdbuf, 0xf); |
| 83 | + OUT(cmdbuf, 0x5); |
| 84 | + OUT(cmdbuf, 0xffffffff); |
| 85 | + OUT(cmdbuf, 0x0); |
| 86 | + OUT(cmdbuf, 0x0); |
| 87 | + OUT(cmdbuf, 0x3ff); |
| 88 | + OUT(cmdbuf, 0x0); |
| 89 | + OUT(cmdbuf, 0x4); |
| 90 | + OUT(cmdbuf, 0x1); |
| 91 | + OUT(cmdbuf, 0x0); |
| 92 | + |
| 93 | + CMD_LOAD_STATE(cmdbuf, VIVS_CL_KICKER, 0xbadabeeb); |
| 94 | + CMD_LOAD_STATE(cmdbuf, VIVS_GL_FLUSH_CACHE, |
| 95 | + VIVS_GL_FLUSH_CACHE_SHADER_L1 | |
| 96 | + VIVS_GL_FLUSH_CACHE_UNK10 | |
| 97 | + VIVS_GL_FLUSH_CACHE_UNK11); |
| 98 | +} |
| 99 | + |
| 100 | +static void etnaviv_flop_reset_ppu_fill_input(u32 *buffer, u32 size) |
| 101 | +{ |
| 102 | + memset32(buffer, 0x01010101, size / 4); |
| 103 | +} |
| 104 | + |
| 105 | +static void etnaviv_flop_reset_ppu_set_shader(u8 *dest) |
| 106 | +{ |
| 107 | + static const u32 inst[PPU_FLOP_RESET_INSTR_DWORD_COUNT] = { |
| 108 | + /* img_load.u8 r1, c0, r0.xy */ |
| 109 | + 0x78011779, |
| 110 | + 0x39000804, |
| 111 | + 0x00A90050, |
| 112 | + 0x00000000, |
| 113 | + /* img_load.u8 r2, c0, r0.xy */ |
| 114 | + 0x78021779, |
| 115 | + 0x39000804, |
| 116 | + 0x00A90050, |
| 117 | + 0x00000000, |
| 118 | + /* dp2x8 r1, r1, r2, c3_512 */ |
| 119 | + 0xB8017145, |
| 120 | + 0x390018FC, |
| 121 | + 0x01C90140, |
| 122 | + 0x40390028, |
| 123 | + /* img_store.u8 r1, c2, r0.xy, r1 */ |
| 124 | + 0x380007BA, |
| 125 | + 0x39001804, |
| 126 | + 0x00A90050, |
| 127 | + 0x00390018, |
| 128 | + }; |
| 129 | + memcpy(dest, inst, sizeof(inst)); |
| 130 | +} |
| 131 | + |
| 132 | +static const struct etnaviv_flop_reset_entry { |
| 133 | + u16 chip_model; |
| 134 | + u16 revision; |
| 135 | + u32 flags; |
| 136 | +} etnaviv_flop_reset_db[] = { |
| 137 | + { |
| 138 | + .chip_model = 0x8000, |
| 139 | + .revision = 0x6205, |
| 140 | + }, |
| 141 | +}; |
| 142 | + |
| 143 | +bool etnaviv_flop_reset_ppu_require(const struct etnaviv_chip_identity *chip_id) |
| 144 | +{ |
| 145 | + const struct etnaviv_flop_reset_entry *e = etnaviv_flop_reset_db; |
| 146 | + |
| 147 | + for (int i = 0; i < ARRAY_SIZE(etnaviv_flop_reset_db); ++i, ++e) { |
| 148 | + if (chip_id->model == e->chip_model && |
| 149 | + chip_id->revision == e->revision) |
| 150 | + return true; |
| 151 | + } |
| 152 | + |
| 153 | + return false; |
| 154 | +} |
| 155 | + |
| 156 | +static const u32 image_data_size = PPU_IMAGE_STRIDE * PPU_IMAGE_YSIZE; |
| 157 | +static const u32 output_offset = ALIGN(image_data_size, 64); |
| 158 | +static const u32 shader_offset = ALIGN(output_offset + image_data_size, 64); |
| 159 | +static const u32 shader_size = PPU_FLOP_RESET_INSTR_DWORD_COUNT * sizeof(u32); |
| 160 | +static const u32 shader_register_count = 3; |
| 161 | +static const u32 buffer_size = shader_offset + shader_size; |
| 162 | + |
| 163 | +int etnaviv_flop_reset_ppu_init(struct etnaviv_drm_private *priv) |
| 164 | +{ |
| 165 | + /* Get some space from the ring buffer to put the payload |
| 166 | + * (input and output image, and shader), we keep this buffer |
| 167 | + * for the whole life time the driver is bound |
| 168 | + */ |
| 169 | + priv->flop_reset_data_ppu = |
| 170 | + kzalloc(sizeof(*priv->flop_reset_data_ppu), GFP_KERNEL); |
| 171 | + |
| 172 | + if (!priv->flop_reset_data_ppu) |
| 173 | + return -ENOMEM; |
| 174 | + |
| 175 | + int ret = etnaviv_cmdbuf_init(priv->cmdbuf_suballoc, |
| 176 | + priv->flop_reset_data_ppu, buffer_size); |
| 177 | + if (ret) { |
| 178 | + kfree(priv->flop_reset_data_ppu); |
| 179 | + return ret; |
| 180 | + } |
| 181 | + |
| 182 | + void *buffer_base = priv->flop_reset_data_ppu->vaddr; |
| 183 | + u32 *input_data = (u32 *)buffer_base; |
| 184 | + u8 *shader_data = (u8 *)buffer_base + shader_offset; |
| 185 | + |
| 186 | + etnaviv_flop_reset_ppu_fill_input(input_data, image_data_size); |
| 187 | + etnaviv_flop_reset_ppu_set_shader(shader_data); |
| 188 | + |
| 189 | + return 0; |
| 190 | +} |
| 191 | + |
| 192 | +void etnaviv_flop_reset_ppu_run(struct etnaviv_gpu *gpu) |
| 193 | +{ |
| 194 | + struct etnaviv_drm_private *priv = gpu->drm->dev_private; |
| 195 | + |
| 196 | + if (!priv->flop_reset_data_ppu) { |
| 197 | + dev_err(gpu->dev, |
| 198 | + "Oops: Flop reset data was not initialized, skipping\n"); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + u32 buffer_base = etnaviv_cmdbuf_get_va(priv->flop_reset_data_ppu, |
| 203 | + &gpu->mmu_context->cmdbuf_mapping); |
| 204 | + |
| 205 | + etnaviv_emit_flop_reset_state_ppu(&gpu->buffer, buffer_base, 0, |
| 206 | + output_offset, shader_offset, |
| 207 | + shader_size, shader_register_count); |
| 208 | +} |
0 commit comments