Debian Codegraph @

src/soc/mediatek/mt8196/gpueb.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
2
3 /*
4 * These values are used by MediaTek internally.
5 * We can find these registers in "CODA_GPU_RPC_202306081135".
6 * The setting values are provided by MediaTek designers.
7 */
8
9 #include <arch/barrier.h>
10 #include <cbfs.h>
11 #include <console/console.h>
12 #include <delay.h>
13 #include <device/mmio.h>
14 #include <soc/gpueb.h>
15 #include <soc/mcu_common.h>
16 #include <soc/symbols.h>
17 #include <timer.h>
18
19 #define DUMP_REG(reg) printk(BIOS_DEBUG, #reg "(%#x) = %#x\n", (reg), read32p((reg)))
20
21 static struct st_auto_dma_fw {
22 const void *fw;
23 size_t fw_size;
24 uint64_t fw_addr;
25 } auto_dma_fw[PT_FW_NUM];
26
27 static void gpu_reset(void)
28 {
29 /* Reset MFG before normal boot */
30 write32p(MFG_RPC_MFG36_PWR_CON, MFG_PWR_OFF_VALUE0);
31 write32p(MFG_RPC_MFG35_PWR_CON, MFG_PWR_OFF_VALUE0);
32 write32p(MFG_RPC_MFG34_PWR_CON, MFG_PWR_OFF_VALUE0);
33 write32p(MFG_RPC_MFG33_PWR_CON, MFG_PWR_OFF_VALUE0);
34 write32p(MFG_RPC_MFG32_PWR_CON, MFG_PWR_OFF_VALUE0);
35 write32p(MFG_RPC_MFG31_PWR_CON, MFG_PWR_OFF_VALUE0);
36 write32p(MFG_RPC_MFG30_PWR_CON, MFG_PWR_OFF_VALUE0);
37 write32p(MFG_RPC_MFG29_PWR_CON, MFG_PWR_OFF_VALUE0);
38 write32p(MFG_RPC_MFG28_PWR_CON, MFG_PWR_OFF_VALUE0);
39 write32p(MFG_RPC_MFG27_PWR_CON, MFG_PWR_OFF_VALUE0);
40 write32p(MFG_RPC_MFG26_PWR_CON, MFG_PWR_OFF_VALUE0);
41 write32p(MFG_RPC_MFG25_PWR_CON, MFG_PWR_OFF_VALUE0);
42 write32p(MFG_RPC_MFG20_PWR_CON, MFG_PWR_OFF_VALUE0);
43 write32p(MFG_RPC_MFG19_PWR_CON, MFG_PWR_OFF_VALUE0);
44 write32p(MFG_RPC_MFG18_PWR_CON, MFG_PWR_OFF_VALUE0);
45 write32p(MFG_RPC_MFG17_PWR_CON, MFG_PWR_OFF_VALUE0);
46 write32p(MFG_RPC_MFG16_PWR_CON, MFG_PWR_OFF_VALUE0);
47 write32p(MFG_RPC_MFG15_PWR_CON, MFG_PWR_OFF_VALUE0);
48 write32p(MFG_RPC_MFG14_PWR_CON, MFG_PWR_OFF_VALUE0);
49 write32p(MFG_RPC_MFG13_PWR_CON, MFG_PWR_OFF_VALUE0);
50 write32p(MFG_RPC_MFG12_PWR_CON, MFG_PWR_OFF_VALUE0);
51 write32p(MFG_RPC_MFG11_PWR_CON, MFG_PWR_OFF_VALUE0);
52 write32p(MFG_RPC_MFG10_PWR_CON, MFG_PWR_OFF_VALUE0);
53 write32p(MFG_RPC_MFG9_PWR_CON, MFG_PWR_OFF_VALUE0);
54 write32p(MFG_RPC_MFG23_PWR_CON, MFG_PWR_OFF_VALUE0);
55 write32p(MFG_RPC_MFG8_PWR_CON, MFG_PWR_OFF_VALUE0);
56 write32p(MFG_RPC_MFG7_PWR_CON, MFG_PWR_OFF_VALUE0);
57 write32p(MFG_RPC_MFG5_PWR_CON, MFG_PWR_OFF_VALUE0);
58 write32p(MFG_RPC_MFG4_PWR_CON, MFG_PWR_OFF_VALUE0);
59 write32p(MFG_RPC_MFG3_PWR_CON, MFG_PWR_OFF_VALUE0);
60 write32p(MFG_RPC_MFG2_PWR_CON, MFG_PWR_OFF_VALUE0);
61 write32p(MFG_RPC_MFG37_PWR_CON, MFG_PWR_OFF_VALUE1);
62 write32p(MFG_RPC_MFG1_PWR_CON, MFG_PWR_OFF_VALUE2);
63 write32p(MFG_RPC_MFG0_PWR_CON, MFG_PWR_OFF_VALUE3);
64 write32p(MFG_GHPM_CFG0_CON, 0x0);
65 write32p(MFG_RPC_GHPM_CFG13_CON, 0x0);
66 write32p(MFG_RPCTOP_DUMMY_REG_0, 0x0);
67 }
68
69 static bool is_mfg0_power_on(void)
70 {
71 return (read32p(MFG0_PWR_CON) & MFG0_PWR_ACK_BIT) == MFG0_PWR_ACK_BIT;
72 }
73
74 static u32 get_mfg0_pwr_con(void)
75 {
76 return read32p(MFG_RPC_MFG0_PWR_CON);
77 }
78
79 static void gpueb_dump_register(void)
80 {
81 DUMP_REG(MFG_RPC_MFG0_PWR_CON);
82 DUMP_REG(MFG_RPC_SLV_SLP_PROT_RDY_STA);
83 }
84
85 static void gpueb_print_exp_status(void)
86 {
87 printk(BIOS_ERR, "%s: Request mainpll polling ack: failed\n", __func__);
88 printk(BIOS_ERR, "%s: MFG0_PWR_CON = %#x\n", __func__, get_mfg0_pwr_con());
89 DUMP_REG(MFG_RPC_AO_CLK_CFG);
90 DUMP_REG(MAINPLL_SEL);
91 DUMP_REG(MFG_SODI_VRF18);
92 DUMP_REG(GPUEB_BUSPLL_RES_STA_GPR);
93 DUMP_REG(MFG_RPC_GHPM_CFG13_CON);
94 DUMP_REG(MFG_GHPM_RO2_CON);
95 }
96
97 static bool wait_status_ready(uintptr_t status_reg, uint32_t mask,
98 uint32_t expect, uint32_t count)
99 {
100 uint32_t i = 0;
101
102 do {
103 udelay(MTCMOS_DELAY_TIME_US);
104 if ((read32p(status_reg) & mask) == expect)
105 return true;
106 i++;
107 } while (i < count);
108
109 gpueb_dump_register();
110 return false;
111 }
112
113 static void gpueb_request_resource(void)
114 {
115 setbits32p(MFG_RPC_GHPM_CFG13_CON, RESOURCE_BITS);
116 if (!wait_status_ready(MFG_GHPM_RO2_CON, RESOURCE_ACK_BITS, RESOURCE_ACK_BITS,
117 ACK_SYNC_COUNT)) {
118 gpueb_print_exp_status();
119 die("%s: Failed to wait for resource ready\n", __func__);
120 }
121 }
122
123 static void mfg0_power_on(void)
124 {
125 /* Check if the power of MFG0 is OFF; if it is ON, then return */
126 if (read32p(MFG_RPC_MFG0_PWR_CON) == MFG0_PWR_ON_VALUE)
127 return;
128
129 /* Check if the power condition of MFG0 SRAM is ACK; if not, then return */
130 if (!(read32p(MFG_RPC_MFG0_PWR_CON) & MFG0_PWR_SRAM_PDN))
131 return;
132
133 setbits32p(MFG_RPC_MFG0_PWR_CON, BIT(28));
134 setbits32p(MFG_RPC_MFG0_PWR_CON, BIT(2));
135 if (!wait_status_ready(MFG_RPC_MFG0_PWR_CON, BIT(30), BIT(30), POWER_ACK_DELAY_COUNT))
136 return;
137
138 udelay(MFG0_PWR_ON_DELAY_US);
139
140 setbits32p(MFG_RPC_MFG0_PWR_CON, BIT(3));
141 if (!wait_status_ready(MFG_RPC_MFG0_PWR_CON, GENMASK(31, 30), GENMASK(31, 30),
142 POWER_2ND_ACK_DELAY_COUNT))
143 return;
144
145 clrbits32p(MFG_RPC_MFG0_PWR_CON, BIT(4));
146 clrbits32p(MFG_RPC_MFG0_PWR_CON, BIT(1));
147 setbits32p(MFG_RPC_MFG0_PWR_CON, BIT(0));
148 clrbits32p(MFG_RPC_MFG0_PWR_CON, BIT(8));
149 if (!wait_status_ready(MFG_RPC_MFG0_PWR_CON, BIT(12), 0, POWER_2ND_ACK_DELAY_COUNT))
150 return;
151
152 setbits32p(MFG_RPC_MFG0_PWR_CON, BIT(21));
153 clrbits32p(MFG_RPC_MFG0_PWR_CON, BIT(22));
154 write32p(MFGSYS_PROTECT_EN_CLR_0, GENMASK(31, 25) | GENMASK(19, 16) | GENMASK(3, 0));
155 if (!wait_status_ready(MFGSYS_PROTECT_EN_STA_0,
156 GENMASK(31, 26) | BIT(7) | GENMASK(3, 0), 0,
157 POWER_2ND_ACK_DELAY_COUNT))
158 return;
159
160 write32p(MFG_RPC_SLV_SLP_PROT_EN_CLR, BIT(0));
161 if (!wait_status_ready(MFG_RPC_SLV_SLP_PROT_RDY_STA, BIT(0), 0,
162 POWER_2ND_ACK_DELAY_COUNT))
163 return;
164
165 write32p(MFG_RPC_SLV_SLP_PROT_EN_CLR, BIT(1));
166 if (!wait_status_ready(MFG_RPC_SLV_SLP_PROT_RDY_STA, BIT(1), 0,
167 POWER_2ND_ACK_DELAY_COUNT))
168 return;
169
170 write32p(MFG_RPC_SLV_WAY_EN_SET, BIT(0));
171 wait_status_ready(MFG_RPC_SLV_CTRL_UPDATE, BIT(0), BIT(0), POWER_2ND_ACK_DELAY_COUNT);
172 }
173
174 static void switch_mux_pll(void)
175 {
176 gpueb_request_resource();
177 setbits32p(MFG_RPC_AO_CLK_CFG, GPUEB_CKMUX_SEL_26M_BIT);
178 udelay(SWITCH_MUX_WAIT_US);
179 }
180
181 static void gpueb_assert_init_success(void)
182 {
183 u32 gpueb_mux;
184
185 gpueb_mux = read32p(MFG_RPC_AO_CLK_CFG);
186 if (is_mfg0_power_on() &&
187 ((gpueb_mux & GPUEB_CKMUX_SEL_26M_BIT) == GPUEB_CKMUX_SEL_26M_BIT)) {
188 printk(BIOS_INFO, "%s: Done\n", __func__);
189 return;
190 }
191 gpueb_print_exp_status();
192 die("%s: Failed to assert init\n", __func__);
193 }
194
195 static void parse_fw(const void *buf_addr, size_t buf_size, const void **img, size_t *img_size)
196 {
197 int i;
198 void *addr;
199 struct gpueb_pt_hdr *hdr;
200
201 hdr = (struct gpueb_pt_hdr *)buf_addr;
202
203 if (buf_size <= sizeof(*hdr))
204 die("%s: Buffer size too small\n", __func__);
205
206 while ((uintptr_t)hdr + sizeof(*hdr) <= (uintptr_t)buf_addr + buf_size &&
207 hdr->magic == GPUEB_PT_MAGIC) {
208 addr = (void *)hdr + hdr->hdr_size;
209
210 if (hdr->id == 0 || hdr->id >= PT_FW_NUM) {
211 printk(BIOS_WARNING, "%s: unexpected hdr (%d)\n", __func__, hdr->id);
212 addr += ALIGN_UP(hdr->img_size, hdr->align);
213 hdr = (struct gpueb_pt_hdr *)addr;
214 continue;
215 }
216
217 if ((uintptr_t)addr + hdr->img_size > (uintptr_t)buf_addr + buf_size)
218 break;
219
220 if (hdr->id == PT_FW_IDX_GPUEB_FW) {
221 *img = addr;
222 *img_size = hdr->img_size;
223 } else {
224 auto_dma_fw[hdr->id].fw = addr;
225 auto_dma_fw[hdr->id].fw_size = hdr->img_size;
226 auto_dma_fw[hdr->id].fw_addr = hdr->addr;
227 }
228
229 addr += ALIGN_UP(hdr->img_size, hdr->align);
230 hdr = (struct gpueb_pt_hdr *)addr;
231 }
232
233 if (!*img || !*img_size)
234 die("%s: img:%p, img_size:%ld\n", __func__, *img, *img_size);
235
236 for (i = PT_FW_IDX_AUTO_DMA_START; i <= PT_FW_IDX_AUTO_DMA_END; i++) {
237 if (!auto_dma_fw[i].fw || !auto_dma_fw[i].fw_size) {
238 die("%s: Partition missing (@%p)\n"
239 "fw_id[%d]:%p, size:%ld\n", __func__, buf_addr, i,
240 auto_dma_fw[i].fw, auto_dma_fw[i].fw_size);
241 }
242 }
243 }
244
245 static void load_fw(enum pt_fw_idx pt_id)
246 {
247 assert(auto_dma_fw[pt_id].fw_addr + auto_dma_fw[pt_id].fw_size <= GPUEB_SRAM_SIZE);
248 memcpy((void *)(MFG_GPUEB_BASE + auto_dma_fw[pt_id].fw_addr),
249 auto_dma_fw[pt_id].fw, auto_dma_fw[pt_id].fw_size);
250 }
251
252 static void load_auto_dma_fw(void)
253 {
254 load_fw(PT_FW_IDX_AUTO_DMA_CS_BRCAST_PART1_FW);
255 load_fw(PT_FW_IDX_AUTO_DMA_CS_BRCAST_PART2_FW);
256 load_fw(PT_FW_IDX_AUTO_DMA_CS_BRCAST_PART3_FW);
257 load_fw(PT_FW_IDX_AUTO_DMA_CS_HBVC_LOGGING_FW);
258 load_fw(PT_FW_IDX_AUTO_DMA_CS_TOP_SESV8_FW);
259 load_fw(PT_FW_IDX_AUTO_DMA_CS_STK_SESV8_FW);
260 load_fw(PT_FW_IDX_AUTO_DMA_SMMU_FW);
261 load_fw(PT_FW_IDX_AUTO_DMA_PREOC_FW);
262 load_fw(PT_FW_IDX_AUTO_DMA_CCMD_CH1_FW);
263 load_fw(PT_FW_IDX_AUTO_DMA_CCMD_CH2_FW);
264 load_fw(PT_FW_IDX_AUTO_DMA_CCMD_CH0_FW);
265 load_fw(PT_FW_IDX_AUTO_DMA_HBVC_CONFIG_GRP0_FW);
266 load_fw(PT_FW_IDX_AUTO_DMA_HBVC_CONFIG_GRP1_FW);
267 load_fw(PT_FW_IDX_AUTO_DMA_GPM_OUT_OF_RATIO_FW);
268 load_fw(PT_FW_IDX_AUTO_DMA_GPM_OUT_OF_SUM_MAX_FW);
269 }
270
271 static bool gpueb_is_sram_access(void)
272 {
273 u32 mfg0_pwr_con;
274
275 mfg0_pwr_con = read32p(MFG0_PWR_CON);
276
277 if (mfg0_pwr_con & MFG0_PWR_SRAM_PDN_ACK) {
278 printk(BIOS_ERR, "%s: SRAM is power-down(%#x)\n", __func__, mfg0_pwr_con);
279 return false;
280 }
281
282 if ((mfg0_pwr_con & MFG0_PWR_SRAM_SLEEP_B_ACK) != MFG0_PWR_SRAM_SLEEP_B_ACK) {
283 printk(BIOS_ERR, "%s: SRAM is under sleep mode(%#x)\n", __func__, mfg0_pwr_con);
284 return false;
285 }
286
287 return true;
288 }
289
290 static void gpueb_power_init(void)
291 {
292 mfg0_power_on();
293 switch_mux_pll();
294 gpueb_assert_init_success();
295 }
296
297 static void gpueb_reset(struct mtk_mcu *mcu)
298 {
299 const void *gpueb_img = NULL;
300 size_t gpueb_img_size = 0;
301 u32 tcm_lock_info;
302
303 parse_fw(mcu->load_buffer, mcu->buffer_size, &gpueb_img, &gpueb_img_size);
304 if (!gpueb_img || !gpueb_img_size)
305 die("%s: Failed to parse gpueb FW", __func__);
306
307 tcm_lock_info = (TCM_LOCK_ADDR >> 12) & GPUEB_TCM_LOCK_MASK;
308 tcm_lock_info = tcm_lock_info << 24;
309
310 write32p(GPUEB_CFGREG_SW_RSTN, 0);
311 memset((void *)MFG_GPUEB_BASE, 0, GPUEB_SRAM_SIZE);
312 assert(gpueb_img_size <= GPUEB_SRAM_SIZE);
313 memcpy((void *)MFG_GPUEB_BASE, gpueb_img, gpueb_img_size);
314
315 load_auto_dma_fw();
316
317 write32p(GPUEB_ABNORMAL_BOOT, 0);
318 write32p(GPUEB_WARM_BOOT, 0);
319 write32p(GPUMPU_RSV_ADDR, (uintptr_t)_resv_mem_gpueb >> PAGE_SHIFT);
320
321 /* Trigger GPUEB */
322 write32p(GPUEB_CFGREG_SW_RSTN, 0);
323 write32p(GPUEB_CFGREG_SW_RSTN,
324 GPUEB_CFGREG_SW_RSTN_SW_RSTN | GPUEB_CFGREG_SW_RSTN_DMA_BUSY_MASK |
325 tcm_lock_info | GPUEB_CFGREG_PTCM_LOCK);
326 }
327
328 static bool is_boot_up_complete(void)
329 {
330 return read32p(GPUEB_INIT_FOOTPRINT) == GPUEB_BOOT_UP_COMPLETE;
331 }
332
333 static bool gpueb_wait_boot_up_done(void)
334 {
335 if (!wait_us(BOOT_TIMEOUT_US, is_boot_up_complete())) {
336 printk(BIOS_CRIT, "%s: GPUEB boot-up timed out!!!\n", __func__);
337 DUMP_REG(GPUEB_MUX);
338 DUMP_REG(MFG0_PWR_CON);
339 DUMP_REG(MFG_RPC_GPUEB_CFG);
340 DUMP_REG(GPUEB_CFGREG_SW_RSTN);
341 DUMP_REG(GPUEB_CFGREG_DBG_APB_PC);
342 DUMP_REG(GPUEB_CFGREG_DBG_APB_LR);
343 DUMP_REG(GPUEB_CFGREG_DBG_APB_SP);
344 DUMP_REG(GPUEB_INIT_FOOTPRINT);
345 DUMP_REG(GPUFREQ_FOOTPRINT_GPR);
346 DUMP_REG(GPUPTP3_BAD_SAMPLE);
347 DUMP_REG(GPUEB_DIAGNOSIS_GPR);
348
349 write32p(GPUEB_INFO, 0x5A5A5A5A);
350 DUMP_REG(GPUEB_INFO);
351 return false;
352 }
353
354 return true;
355 }
356
357 static struct mtk_mcu gpueb = {
358 .firmware_name = CONFIG_GPUEB_FIRMWARE,
359 .load_buffer = _dram_dma,
360 .buffer_size = REGION_SIZE(dram_dma),
361 .reset = gpueb_reset,
362 };
363
364 static void gpueb_load(void)
365 {
366 if (!is_mfg0_power_on() || !gpueb_is_sram_access())
367 die("%s: GPUEB(MFG0) NOT power-on!!!", __func__);
368
369 /* Load FW image and trigger GPUEB */
370 if (mtk_init_mcu(&gpueb))
371 die("%s: Failed to load FW image", __func__);
372
373 if (!gpueb_wait_boot_up_done())
374 die("%s: Failed to wait for boot up", __func__);
375
376 printk(BIOS_INFO, "%s: GPUEB boot-up complete (magic: %#x)\n", __func__,
377 read32p(GPUEB_INIT_FOOTPRINT));
378 }
379
380 void gpueb_init(void)
381 {
382 gpu_reset();
383 gpueb_power_init();
384 gpueb_load();
385 printk(BIOS_INFO, "%s: Init done\n", __func__);
386 }