blob: 5541b6bd7ac6e9bd5da92008c8cbfad0a8e35ecd [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
19
dongqing.li3d3670e2023-10-17 11:15:45 +080020static void wait_usb_dev(void);
21
22#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
168static void wait_usb_dev(void)
169{
170 int print_cnt = 0, ret;
171
172 while (1) {
173 run_command("usb start", 1);
174 mdelay(2000);
175 run_command("usb reset", 1);
176 ret = usb_stor_scan(1);
177 if (ret) {
178 if (!(print_cnt & 0x3f)) {
179 print_cnt++;
180 printf("ramdump: can't find usb device, please insert Udisk.\n\n");
181 }
182 mdelay(10000);
183 continue;
184 } else {
185 run_command("usb dev", 1);
186 break;
187 }
188 }
189}
190
191/*
192 * NOTE: this is a default implementation for writing compressed ramdump data
193 * to /data/ partition for Android platform. You can read out dumpfile in
194 * path /data/crashdump-1.bin when enter Android for crash analyze.
195 * by default, /data/ partition for android is EXT4 fs.
196 *
197 * TODO:
198 * If you are using different fs or OS on your platform, implement compress
199 * data save command for your fs and OS in your board.c with same function
200 * name "ramdump_save_compress_data".
201 */
202__weak int ramdump_save_compress_data(void)
203{
204 char cmd[128] = {0};
205 char *env;
206 int ret = 0;
207
208 env = env_get("ramdump_location");
209 if (!env)
210 return 0;
211
212 printf("ramdump_location:%s\n", env);
213 /* currently we only support write to usb disk */
214 if (strncmp(env, "usb", 3)) {
215 printf("not supported location\n");
216 return 0;
217 }
218
219 wait_usb_dev();
220
dongqing.li3d3670e2023-10-17 11:15:45 +0800221 printf("Prepare to save crash file: base=0x%08lx, size=0x%08lx\n\n",
222 ramdump_base, ramdump_size);
Bo Lv72d0e902023-01-02 14:27:34 +0000223 sprintf(cmd, "fatwrite usb 0 %lx crashdump-1.bin %lx\n",
224 ramdump_base, ramdump_size);
225 printf("CMD:%s\n", cmd);
226 ret = run_command(cmd, 0);
dongqing.li3d3670e2023-10-17 11:15:45 +0800227 if (ret != 0) {
228 printf("run fatwrite usb ERROR!\n");
229 return -1;
230 }
231 printf("run fatwrite usb OK!\n");
232 mdelay(5000);
233 //run_command("reset", 1);
Bo Lv72d0e902023-01-02 14:27:34 +0000234 return 0;
235}
236
237static void ramdump_env_setup(unsigned long addr, unsigned long size)
238{
239 unsigned int data[10] = {
240 0x8E9C929F, 0x9E9C9791,
241 0xD28C9191, 0x97949B8D,
242 0x888B92, 0xCEBB97,
243 0x938E9B90, 0x978D8D97,
244 0xC8009B8A, 0xB99CDB
245 };
246 char *line, *p1, *p2, *o;
247 unsigned char *p;
248 int i;
249
250 p = (unsigned char *)data;
251 for (i = 0; i < 40; i++)
252 p[i] = ~(p[i] - 1);
253
254 /*
255 * TODO: Make sure address for fdt_high and initrd_high
256 * are suitable for all boards
257 *
258 * usually kernel load address is 0x010800000
259 * Make sure:
260 * (kernel image size + ramdisk size) <
261 * (initrd_high - 0x010800000)
262 * dts file size < (fdt_high - initrd_high)
263 */
dongqing.li3d3670e2023-10-17 11:15:45 +0800264 //env_set("initrd_high", "0x0BB00000");
265 //env_set("fdt_high", "0x0BE00000");
Bo Lv72d0e902023-01-02 14:27:34 +0000266 line = env_get("bootargs");
267 if (!line)
268 return;
269
270 i = strlen(line);
271 o = malloc(i + 128);
272 if (!o)
273 return;
274
275 memset(o, 0, i + 128);
276 memcpy(o, line, i);
277 line = o + i + 128;
278 p1 = strstr(o, (const char *)p);
279 if (p1) {
280 p2 = strchr(p1, ' ');
281 if (!p2)
282 p2 = p1;
283 memmove(p1, p2, line - p2);
284 }
285 i = strlen(o);
286 p1 = o + i;
287 p1[0] = ' ';
288 sprintf(p1 + 1, "%s=%s ramdump=%lx,%lx",
289 (char *)data, (char *)(data + 6), addr, size);
290 env_set("bootargs", o);
291
292#if DEBUG_RAMDUMP
293 run_command("printenv bootargs", 1);
294#endif
295 free(o);
296}
297
dongqing.li3d3670e2023-10-17 11:15:45 +0800298static int overwrite_bl33z_rsvmem_info(unsigned long addr, unsigned long size)
299{
300 int address_cells = 0;
301 unsigned long align_size = PAGE_ALIGN(size);
302 char cmd[0x80];
303
304 address_cells = fdt_address_cells(gd->fdt_blob, 0);
305 if (address_cells < 1) {
306 printf("%s, bad #address-cells !\n", __func__);
307 return -1;
308 }
309 printf("%s, get fdt #address-cells = %d\n", __func__, address_cells);
310
311 memset(cmd, 0, sizeof(cmd));
312 if (address_cells == 2)
313 sprintf(cmd,
314 "fdt set /reserved-memory/ramdump_bl33z reg '<0x0 0x%08lx 0x0 0x%08lx>'",
315 addr, align_size);
316 else
317 sprintf(cmd, "fdt set /reserved-memory/ramdump_bl33z reg '<0x%08lx 0x%08lx>'",
318 addr, align_size);
319
320 printf("%s\n", cmd);
321 run_command(cmd, 0);
322
323 printf("fdt set /reserved-memory/ramdump_bl33z status okay\n");
324 run_command("fdt set /reserved-memory/ramdump_bl33z status okay", 0);
325
326 return 0;
327}
328
329static int reduce_dts_reserved_memory(void)
330{
331 int address_cells = 0;
332
333 address_cells = fdt_address_cells(gd->fdt_blob, 0);
334 if (address_cells < 1) {
335 printf("%s, bad #address-cells !\n", __func__);
336 return -1;
337 }
338 printf("%s, get fdt #address-cells = %d\n", __func__, address_cells);
339 if (address_cells == 2) {
340 printf("\nReduce dts reserved memory:\n");
341 printf(" * disabled logo_reserved\n");
342 run_command("fdt set /reserved-memory/linux,meson-fb reg '<0x0 0x0 0x0 0x0>'", 0);
343 printf(" * disabled vdin1_cma_reserved\n");
344 run_command("fdt set /reserved-memory/linux,vdin1_cma reg '<0x0 0x0 0x0 0x0>'", 0);
345 printf(" * disabled ion_cma_reserved\n");
346 run_command("fdt set /reserved-memory/linux,ion-dev size '<0 0>'", 0);
347 printf(" * disabled dsp_fw_reserved\n");
348 run_command("fdt set /reserved-memory/linux,dsp_fw size '<0 0>'", 0);
349 printf(" * disabled lcd_tcon_reserved\n");
350 run_command("fdt set /reserved-memory/linux,lcd_tcon reg '<0x0 0x0 0x0 0x0>'", 0);
351 printf(" * reduce codec_mm_cma_reserved\n");
352 run_command("fdt set /reserved-memory/linux,codec_mm_cma size '<0x0 0x5000000>'", 0);
353 printf(" * reduce ion_fb_reserved, display\n");
354 run_command("fdt set /reserved-memory/linux,ion-fb size '<0x0 0x1000000>'", 0);
355 } else {
356 printf("\nReduce dts reserved memory:\n");
357 printf(" * disabled logo_reserved\n");
358 run_command("fdt set /reserved-memory/linux,meson-fb reg '<0x0 0x0>'", 0);
359 printf(" * disabled vdin1_cma_reserved\n");
360 run_command("fdt set /reserved-memory/linux,vdin1_cma reg '<0x0 0x0>'", 0);
361 printf(" * disabled ion_cma_reserved\n");
362 run_command("fdt set /reserved-memory/linux,ion-dev size '<0>'", 0);
363 printf(" * disabled dsp_fw_reserved\n");
364 run_command("fdt set /reserved-memory/linux,dsp_fw size '<0>'", 0);
365 printf(" * disabled lcd_tcon_reserved\n");
366 run_command("fdt set /reserved-memory/linux,lcd_tcon reg '<0x0 0x0>'", 0);
367 printf(" * reduce codec_mm_cma_reserved\n");
368 run_command("fdt set /reserved-memory/linux,codec_mm_cma size '<0x5000000>'", 0);
369 printf(" * reduce ion_fb_reserved, display\n");
370 run_command("fdt set /reserved-memory/linux,ion-fb size '<0x1000000>'", 0);
371 }
372
373 //run_command("fdt print /reserved-memory", 0);
374 return 0;
375}
376
Bo Lv72d0e902023-01-02 14:27:34 +0000377void check_ramdump(void)
378{
379 unsigned long size = 0;
380 unsigned long addr = 0;
381 char *env;
382 int reboot_mode;
dongqing.li3d3670e2023-10-17 11:15:45 +0800383 char str[128] = "";
384 unsigned int ddr_scramble_reg = 0xfe02e030;
Bo Lv72d0e902023-01-02 14:27:34 +0000385
386 env = env_get("ramdump_enable");
387 printf("%s, ramdump_enable = %s\n", __func__, env);
388 if (env) {
389 if (!strcmp(env, "1")) {
390 reboot_mode = get_reboot_mode();
391 if ((reboot_mode == AMLOGIC_WATCHDOG_REBOOT ||
392 reboot_mode == AMLOGIC_KERNEL_PANIC)) {
393 addr = ramdump_base;
394 size = ramdump_size;
395 printf("%s, addr:%lx, size:%lx\n",
396 __func__, addr, size);
397 if (addr && size) {
398 ramdump_env_setup(addr, size);
399#ifdef CONFIG_DUMP_COMPRESS_HEAD
400 dump_info((unsigned int)ramdump_base, 0x80, "bl33 check COMPRESS DATA 2");
401#endif
dongqing.li3d3670e2023-10-17 11:15:45 +0800402 //ramdump_save_compress_data();
403 env = env_get("ramdump_location");
404 if (!strncmp(env, "usb", 3)) {
405 printf("Crash saved Udisk already. reboot ...\n\n\n");
406 mdelay(3000);
407 run_command("reset", 1);
408 } else if (!strncmp(env, "data", 4)) {
409 printf("Crash file will save to Android /data.\n");
410 reduce_dts_reserved_memory();
411 overwrite_bl33z_rsvmem_info(addr, size);
412 env_set("initrd_high", "0x0BB00000");
413 env_set("fdt_high", "0x0BE00000");
414 }
415 } else {
416 ramdump_env_setup(0, 0);
Bo Lv72d0e902023-01-02 14:27:34 +0000417 }
418 } else {
419 ramdump_env_setup(0, 0);
420 }
dongqing.li3d3670e2023-10-17 11:15:45 +0800421#ifndef CONFIG_MDUMP_COMPRESS
422 //ramdump bl33z
Bo Lv72d0e902023-01-02 14:27:34 +0000423 printf("%s, fdt: rsvmem ramdump_bl33z enable.\n", __func__);
424 run_command("fdt set /reserved-memory/ramdump_bl33z status okay", 0);
425#endif
dongqing.li3d3670e2023-10-17 11:15:45 +0800426 //ddr scramble
427 sprintf(str, "setenv initargs ${initargs} scramble_reg=0x%08x",
428 ddr_scramble_reg);
429 printf("set_scramble: %s\n", str);
430 run_command(str, 0);
Bo Lv72d0e902023-01-02 14:27:34 +0000431 }
432 }
433}