blob: f94bf4db68a0677cc03099651c15d5418d7e9e55 [file] [log] [blame]
Bo Lv72d0e902023-01-02 14:27:34 +00001// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2/*
3 * Copyright (c) 2019 Amlogic, Inc. All rights reserved.
4 */
5
6#include <common.h>
dongqing.li3d3670e2023-10-17 11:15:45 +08007#include <asm/amlogic/arch/bl31_apis.h>
8#include <asm/amlogic/arch/secure_apb.h>
9#include <amlogic/ramdump.h>
10#include <amlogic/emmc_partitions.h>
Bo Lv72d0e902023-01-02 14:27:34 +000011#include <usb.h>
dongqing.li3d3670e2023-10-17 11:15:45 +080012#include <asm/amlogic/arch/regs.h>
13#include <asm/global_data.h>
14#include <syscon.h>
Bo Lv72d0e902023-01-02 14:27:34 +000015
16#define DEBUG_RAMDUMP 0
17#define AMLOGIC_KERNEL_PANIC 0x0c
18#define AMLOGIC_WATCHDOG_REBOOT 0x0d
Zhongfu Luobef22a72023-12-07 16:30:21 +080019#ifdef CONFIG_USB_STORAGE
dongqing.li3d3670e2023-10-17 11:15:45 +080020static void wait_usb_dev(void);
Zhongfu Luobef22a72023-12-07 16:30:21 +080021#endif
dongqing.li3d3670e2023-10-17 11:15:45 +080022#undef CONFIG_DUMP_COMPRESS_HEAD
Bo Lv72d0e902023-01-02 14:27:34 +000023#ifdef CONFIG_DUMP_COMPRESS_HEAD
24static void dump_info(unsigned int addr, unsigned int size, const char *info)
25{
26 int i = 0;
27
28 printf("\nDUMP %s 0X%08x :", info, addr);
29 for (i = 0; i < size; i += 4) {
30 if (0 == (i % 32))
31 printf("\n[0x%08x] ", addr + i);
32 printf("%08x ", readl(addr + i));
33 }
34 printf("\n\n");
35}
dongqing.li3d3670e2023-10-17 11:15:45 +080036
37int parse_dump_file_v2(const char *base, unsigned long size)
38{
39 struct ram_compress_file_header_v2 *fhead;
40 struct ram_compress_section_header_v2 csh;
41 struct section_info *seg_tbl;
42 unsigned long seg_offset;
43 unsigned int idx;
44 int cnt = 0, csh_size = 0;
45
46 // check ramdump version >= v2.0
47 if ((!strncmp(base, COMPRESS_TAG_HEAD, COMPRESS_TAG_SIZE - 1)) &&
48 (!strncmp(base, COMPRESS_TAG_BL3Z, COMPRESS_TAG_SIZE - 1))) {
49 printf("# Check the crash file head error, exit.\n");
50 return -1;
51 }
52
53 // dump crash section head
54 fhead = (struct ram_compress_file_header_v2 *)base;
55 printf("# total number of segment is %d\n", fhead->section_count);
56 if (fhead->section_count > 10) {
57 printf("# crash file head error. exit.\n");
58 return -1;
59 }
60
61 seg_tbl = malloc(sizeof(struct section_info) * fhead->section_count);
62 printf("# total compress file size is 0x%08lx\n", fhead->file_size);
63 csh_size = sizeof(struct ram_compress_section_header_v2);
64 seg_offset = (unsigned long)fhead + sizeof(struct ram_compress_file_header_v2);
65 printf("# crash file fhead offset is 0x%08lx\n", seg_offset);
66
67 for (cnt = 0; cnt < fhead->section_count; cnt++) {
68 memset(&csh, 0, sizeof(csh));
69 if (seg_offset > ((unsigned long)base + size))
70 continue;
71
72 memcpy((void *)&csh, (char *)seg_offset, sizeof(csh));
73 if (csh.section_index > fhead->section_count) {
74 printf("csh.section_index(%d) > fhead->section_count(%d), error, exit.\n",
75 csh.section_index, fhead->section_count);
76 return -1;
77 }
78
79 if (csh.compress_type == RAM_COMPRESS_ERROR ||
80 csh.compress_type > RAM_COMPRESS_MAX)
81 continue;
82
83 idx = csh.section_index - 1;
84 seg_tbl[idx].offset = seg_offset + sizeof(csh);
85 seg_tbl[idx].type = csh.compress_type;
86 seg_tbl[idx].raw_size = csh.raw_size;
87 seg_tbl[idx].zip_size = csh.zip_size;
88 seg_tbl[idx].val = csh.set_value;
89 seg_offset += csh.zip_size;
90 }
91
92 // dump section table
93 printf("\n-----------------------------------------------------------\n");
94 printf("idx offset type orig size zip size val\n");
95 printf("-----------------------------------------------------------\n");
96 for (cnt = 0; cnt < fhead->section_count; cnt++) {
97 if (seg_tbl[cnt].type == RAM_COMPRESS_ERROR ||
98 seg_tbl[cnt].type > RAM_COMPRESS_MAX)
99 continue;
100 printf(" %2d, 0x%08lx, %2u, 0x%08lx, 0x%08lx, 0x%02x\n",
101 cnt + 1, seg_tbl[cnt].offset,
102 seg_tbl[cnt].type, seg_tbl[cnt].raw_size,
103 seg_tbl[cnt].zip_size, seg_tbl[cnt].val);
104 }
105 printf("-----------------------------------------------------------\n");
106
107 // printf section table
108 for (cnt = 0; cnt < fhead->section_count; cnt++) {
109 if (seg_tbl[cnt].type == RAM_COMPRESS_ERROR ||
110 seg_tbl[cnt].type > RAM_COMPRESS_MAX)
111 continue;
112 dump_info((unsigned int)seg_tbl[cnt].offset - csh_size, csh_size, "crash section head");
113 }
114
115 return 0;
116}
Bo Lv72d0e902023-01-02 14:27:34 +0000117#endif
118
119unsigned long ramdump_base = 0;
120unsigned long ramdump_size = 0;
121unsigned int get_reboot_mode(void)
122{
123 uint32_t reboot_mode_val = ((readl(REG_MDUMP_REBOOT_MODE) >> 12) & 0xf);
124 return reboot_mode_val;
125}
126
127void ramdump_init(void)
128{
dongqing.li3d3670e2023-10-17 11:15:45 +0800129 unsigned int data, reboot_mode;
130
Bo Lv72d0e902023-01-02 14:27:34 +0000131 printf("%s, base reg:0x%08x, size reg:0x%08x\n", __func__,
132 REG_MDUMP_COMPRESS_BASE, REG_MDUMP_COMPRESS_SIZE);
133 ramdump_base = readl(REG_MDUMP_COMPRESS_BASE);
134 ramdump_size = readl(REG_MDUMP_COMPRESS_SIZE);
dongqing.li3d3670e2023-10-17 11:15:45 +0800135#if defined(CONFIG_MDUMP_COMPRESS) && defined(CONFIG_COMPRESSED_ADDR)
136 printf("%s, ramdump_base(0x%08lx) is overwritten as 0x%08x\n",
137 __func__, ramdump_base, CONFIG_COMPRESSED_ADDR);
138 ramdump_base = (unsigned long)CONFIG_COMPRESSED_ADDR;
139#endif
Bo Lv72d0e902023-01-02 14:27:34 +0000140 if (ramdump_base & 0x80) {
141 /* 0x80: The flag indicates that the addr exceeds 4G. */
142 /* real size = size[31:0] + addr[6:0]<<32 */
143 ramdump_size += (ramdump_base & 0x7f) << 32;
144 /* real addr = addr[31:8] << 8 */
145 ramdump_base = (ramdump_base & 0xffffff00) << 8;
146 }
147
dongqing.li3d3670e2023-10-17 11:15:45 +0800148 data = readl(REG_MDUMP_CPUBOOT_STATUS);
149 writel(data & ~RAMDUMP_STICKY_DATA_MASK, REG_MDUMP_CPUBOOT_STATUS);
Bo Lv72d0e902023-01-02 14:27:34 +0000150 printf("%s, add:%lx, size:%lx\n", __func__, ramdump_base, ramdump_size);
151
152#ifdef CONFIG_DUMP_COMPRESS_HEAD
153 dump_info((unsigned int)ramdump_base, 0x80, "bl33 check COMPRESS DATA 1");
154#endif
dongqing.li3d3670e2023-10-17 11:15:45 +0800155
156 reboot_mode = get_reboot_mode();
157 if ((reboot_mode == AMLOGIC_WATCHDOG_REBOOT ||
158 reboot_mode == AMLOGIC_KERNEL_PANIC)) {
159 if (ramdump_base && ramdump_size) {
160#ifdef CONFIG_DUMP_COMPRESS_HEAD
161 parse_dump_file_v2((char *)ramdump_base, ramdump_size);
162#endif
163 ramdump_save_compress_data();
164 }
165 }
Bo Lv72d0e902023-01-02 14:27:34 +0000166}
167
Zhongfu Luobef22a72023-12-07 16:30:21 +0800168#ifdef CONFIG_USB_STORAGE
Bo Lv72d0e902023-01-02 14:27:34 +0000169static void wait_usb_dev(void)
170{
171 int print_cnt = 0, ret;
172
173 while (1) {
174 run_command("usb start", 1);
175 mdelay(2000);
176 run_command("usb reset", 1);
177 ret = usb_stor_scan(1);
178 if (ret) {
179 if (!(print_cnt & 0x3f)) {
180 print_cnt++;
181 printf("ramdump: can't find usb device, please insert Udisk.\n\n");
182 }
183 mdelay(10000);
184 continue;
185 } else {
186 run_command("usb dev", 1);
187 break;
188 }
189 }
190}
Zhongfu Luobef22a72023-12-07 16:30:21 +0800191#endif
Bo Lv72d0e902023-01-02 14:27:34 +0000192/*
193 * NOTE: this is a default implementation for writing compressed ramdump data
194 * to /data/ partition for Android platform. You can read out dumpfile in
195 * path /data/crashdump-1.bin when enter Android for crash analyze.
196 * by default, /data/ partition for android is EXT4 fs.
197 *
198 * TODO:
199 * If you are using different fs or OS on your platform, implement compress
200 * data save command for your fs and OS in your board.c with same function
201 * name "ramdump_save_compress_data".
202 */
203__weak int ramdump_save_compress_data(void)
204{
205 char cmd[128] = {0};
206 char *env;
207 int ret = 0;
208
209 env = env_get("ramdump_location");
210 if (!env)
211 return 0;
212
213 printf("ramdump_location:%s\n", env);
214 /* currently we only support write to usb disk */
215 if (strncmp(env, "usb", 3)) {
216 printf("not supported location\n");
217 return 0;
218 }
Zhongfu Luobef22a72023-12-07 16:30:21 +0800219#ifdef CONFIG_USB_STORAGE
Bo Lv72d0e902023-01-02 14:27:34 +0000220 wait_usb_dev();
Zhongfu Luobef22a72023-12-07 16:30:21 +0800221#endif
dongqing.li3d3670e2023-10-17 11:15:45 +0800222 printf("Prepare to save crash file: base=0x%08lx, size=0x%08lx\n\n",
223 ramdump_base, ramdump_size);
Bo Lv72d0e902023-01-02 14:27:34 +0000224 sprintf(cmd, "fatwrite usb 0 %lx crashdump-1.bin %lx\n",
225 ramdump_base, ramdump_size);
226 printf("CMD:%s\n", cmd);
227 ret = run_command(cmd, 0);
dongqing.li3d3670e2023-10-17 11:15:45 +0800228 if (ret != 0) {
229 printf("run fatwrite usb ERROR!\n");
230 return -1;
231 }
232 printf("run fatwrite usb OK!\n");
233 mdelay(5000);
234 //run_command("reset", 1);
Bo Lv72d0e902023-01-02 14:27:34 +0000235 return 0;
236}
237
238static void ramdump_env_setup(unsigned long addr, unsigned long size)
239{
240 unsigned int data[10] = {
241 0x8E9C929F, 0x9E9C9791,
242 0xD28C9191, 0x97949B8D,
243 0x888B92, 0xCEBB97,
244 0x938E9B90, 0x978D8D97,
245 0xC8009B8A, 0xB99CDB
246 };
247 char *line, *p1, *p2, *o;
248 unsigned char *p;
249 int i;
250
251 p = (unsigned char *)data;
252 for (i = 0; i < 40; i++)
253 p[i] = ~(p[i] - 1);
254
255 /*
256 * TODO: Make sure address for fdt_high and initrd_high
257 * are suitable for all boards
258 *
259 * usually kernel load address is 0x010800000
260 * Make sure:
261 * (kernel image size + ramdisk size) <
262 * (initrd_high - 0x010800000)
263 * dts file size < (fdt_high - initrd_high)
264 */
dongqing.li3d3670e2023-10-17 11:15:45 +0800265 //env_set("initrd_high", "0x0BB00000");
266 //env_set("fdt_high", "0x0BE00000");
Bo Lv72d0e902023-01-02 14:27:34 +0000267 line = env_get("bootargs");
268 if (!line)
269 return;
270
271 i = strlen(line);
272 o = malloc(i + 128);
273 if (!o)
274 return;
275
276 memset(o, 0, i + 128);
277 memcpy(o, line, i);
278 line = o + i + 128;
279 p1 = strstr(o, (const char *)p);
280 if (p1) {
281 p2 = strchr(p1, ' ');
282 if (!p2)
283 p2 = p1;
284 memmove(p1, p2, line - p2);
285 }
286 i = strlen(o);
287 p1 = o + i;
288 p1[0] = ' ';
289 sprintf(p1 + 1, "%s=%s ramdump=%lx,%lx",
290 (char *)data, (char *)(data + 6), addr, size);
291 env_set("bootargs", o);
292
293#if DEBUG_RAMDUMP
294 run_command("printenv bootargs", 1);
295#endif
296 free(o);
297}
298
dongqing.li3d3670e2023-10-17 11:15:45 +0800299static int overwrite_bl33z_rsvmem_info(unsigned long addr, unsigned long size)
300{
301 int address_cells = 0;
302 unsigned long align_size = PAGE_ALIGN(size);
303 char cmd[0x80];
304
305 address_cells = fdt_address_cells(gd->fdt_blob, 0);
306 if (address_cells < 1) {
307 printf("%s, bad #address-cells !\n", __func__);
308 return -1;
309 }
310 printf("%s, get fdt #address-cells = %d\n", __func__, address_cells);
311
312 memset(cmd, 0, sizeof(cmd));
313 if (address_cells == 2)
314 sprintf(cmd,
315 "fdt set /reserved-memory/ramdump_bl33z reg '<0x0 0x%08lx 0x0 0x%08lx>'",
316 addr, align_size);
317 else
318 sprintf(cmd, "fdt set /reserved-memory/ramdump_bl33z reg '<0x%08lx 0x%08lx>'",
319 addr, align_size);
320
321 printf("%s\n", cmd);
322 run_command(cmd, 0);
323
324 printf("fdt set /reserved-memory/ramdump_bl33z status okay\n");
325 run_command("fdt set /reserved-memory/ramdump_bl33z status okay", 0);
326
327 return 0;
328}
329
330static int reduce_dts_reserved_memory(void)
331{
332 int address_cells = 0;
333
334 address_cells = fdt_address_cells(gd->fdt_blob, 0);
335 if (address_cells < 1) {
336 printf("%s, bad #address-cells !\n", __func__);
337 return -1;
338 }
339 printf("%s, get fdt #address-cells = %d\n", __func__, address_cells);
340 if (address_cells == 2) {
341 printf("\nReduce dts reserved memory:\n");
342 printf(" * disabled logo_reserved\n");
343 run_command("fdt set /reserved-memory/linux,meson-fb reg '<0x0 0x0 0x0 0x0>'", 0);
344 printf(" * disabled vdin1_cma_reserved\n");
345 run_command("fdt set /reserved-memory/linux,vdin1_cma reg '<0x0 0x0 0x0 0x0>'", 0);
346 printf(" * disabled ion_cma_reserved\n");
347 run_command("fdt set /reserved-memory/linux,ion-dev size '<0 0>'", 0);
348 printf(" * disabled dsp_fw_reserved\n");
349 run_command("fdt set /reserved-memory/linux,dsp_fw size '<0 0>'", 0);
350 printf(" * disabled lcd_tcon_reserved\n");
351 run_command("fdt set /reserved-memory/linux,lcd_tcon reg '<0x0 0x0 0x0 0x0>'", 0);
352 printf(" * reduce codec_mm_cma_reserved\n");
353 run_command("fdt set /reserved-memory/linux,codec_mm_cma size '<0x0 0x5000000>'", 0);
354 printf(" * reduce ion_fb_reserved, display\n");
355 run_command("fdt set /reserved-memory/linux,ion-fb size '<0x0 0x1000000>'", 0);
356 } else {
357 printf("\nReduce dts reserved memory:\n");
358 printf(" * disabled logo_reserved\n");
359 run_command("fdt set /reserved-memory/linux,meson-fb reg '<0x0 0x0>'", 0);
360 printf(" * disabled vdin1_cma_reserved\n");
361 run_command("fdt set /reserved-memory/linux,vdin1_cma reg '<0x0 0x0>'", 0);
362 printf(" * disabled ion_cma_reserved\n");
363 run_command("fdt set /reserved-memory/linux,ion-dev size '<0>'", 0);
364 printf(" * disabled dsp_fw_reserved\n");
365 run_command("fdt set /reserved-memory/linux,dsp_fw size '<0>'", 0);
366 printf(" * disabled lcd_tcon_reserved\n");
367 run_command("fdt set /reserved-memory/linux,lcd_tcon reg '<0x0 0x0>'", 0);
368 printf(" * reduce codec_mm_cma_reserved\n");
369 run_command("fdt set /reserved-memory/linux,codec_mm_cma size '<0x5000000>'", 0);
370 printf(" * reduce ion_fb_reserved, display\n");
371 run_command("fdt set /reserved-memory/linux,ion-fb size '<0x1000000>'", 0);
372 }
373
374 //run_command("fdt print /reserved-memory", 0);
375 return 0;
376}
377
Bo Lv72d0e902023-01-02 14:27:34 +0000378void check_ramdump(void)
379{
380 unsigned long size = 0;
381 unsigned long addr = 0;
382 char *env;
383 int reboot_mode;
dongqing.li3d3670e2023-10-17 11:15:45 +0800384 char str[128] = "";
385 unsigned int ddr_scramble_reg = 0xfe02e030;
Bo Lv72d0e902023-01-02 14:27:34 +0000386
387 env = env_get("ramdump_enable");
388 printf("%s, ramdump_enable = %s\n", __func__, env);
389 if (env) {
390 if (!strcmp(env, "1")) {
391 reboot_mode = get_reboot_mode();
392 if ((reboot_mode == AMLOGIC_WATCHDOG_REBOOT ||
393 reboot_mode == AMLOGIC_KERNEL_PANIC)) {
394 addr = ramdump_base;
395 size = ramdump_size;
396 printf("%s, addr:%lx, size:%lx\n",
397 __func__, addr, size);
398 if (addr && size) {
399 ramdump_env_setup(addr, size);
400#ifdef CONFIG_DUMP_COMPRESS_HEAD
401 dump_info((unsigned int)ramdump_base, 0x80, "bl33 check COMPRESS DATA 2");
402#endif
dongqing.li3d3670e2023-10-17 11:15:45 +0800403 //ramdump_save_compress_data();
404 env = env_get("ramdump_location");
405 if (!strncmp(env, "usb", 3)) {
406 printf("Crash saved Udisk already. reboot ...\n\n\n");
407 mdelay(3000);
408 run_command("reset", 1);
409 } else if (!strncmp(env, "data", 4)) {
410 printf("Crash file will save to Android /data.\n");
411 reduce_dts_reserved_memory();
412 overwrite_bl33z_rsvmem_info(addr, size);
413 env_set("initrd_high", "0x0BB00000");
414 env_set("fdt_high", "0x0BE00000");
415 }
416 } else {
417 ramdump_env_setup(0, 0);
Bo Lv72d0e902023-01-02 14:27:34 +0000418 }
419 } else {
420 ramdump_env_setup(0, 0);
421 }
dongqing.li3d3670e2023-10-17 11:15:45 +0800422#ifndef CONFIG_MDUMP_COMPRESS
423 //ramdump bl33z
Bo Lv72d0e902023-01-02 14:27:34 +0000424 printf("%s, fdt: rsvmem ramdump_bl33z enable.\n", __func__);
425 run_command("fdt set /reserved-memory/ramdump_bl33z status okay", 0);
426#endif
dongqing.li3d3670e2023-10-17 11:15:45 +0800427 //ddr scramble
428 sprintf(str, "setenv initargs ${initargs} scramble_reg=0x%08x",
429 ddr_scramble_reg);
430 printf("set_scramble: %s\n", str);
431 run_command(str, 0);
Bo Lv72d0e902023-01-02 14:27:34 +0000432 }
433 }
434}