1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <console/console.h>
6 #include <device/mmio.h>
7 #include <intelblocks/crashlog.h>
8 #include <intelblocks/pmc_ipc.h>
11 int __weak
cl_get_cpu_record_size(void)
16 int __weak
cl_get_pmc_record_size(void)
21 u32 __weak
cl_get_cpu_bar_addr(void)
26 int __weak
cl_get_ioe_record_size(void)
31 u32 __weak
cl_get_cpu_tmp_bar(void)
36 u32 __weak
cl_get_cpu_mb_int_addr(void)
41 int __weak
cl_get_total_data_size(void)
43 return cl_get_cpu_record_size() + cl_get_pmc_record_size();
46 bool __weak
cl_pmc_sram_has_mmio_access(void)
51 bool __weak
cl_ioe_sram_has_mmio_access(void)
56 bool __weak
cpu_crashlog_support(void)
61 bool __weak
pmc_crashlog_support(void)
66 bool __weak
cl_cpu_data_present(void)
71 bool __weak
cl_pmc_data_present(void)
76 bool __weak
cl_ioe_data_present(void)
81 __weak
void reset_discovery_buffers(void) {}
83 __weak
void update_new_pmc_crashlog_size(u32
*pmc_crash_size
) {}
85 __weak
void update_new_cpu_crashlog_size(u32
*cpu_crash_size
) {}
87 __weak
void update_new_ioe_crashlog_size(u32
*ioe_crash_size
) {}
89 pmc_ipc_discovery_buf_t __weak
cl_get_pmc_discovery_buf(void)
91 pmc_ipc_discovery_buf_t discov_buf
;
92 memset(&discov_buf
, 0, sizeof(pmc_ipc_discovery_buf_t
));
96 pmc_crashlog_desc_table_t __weak
cl_get_pmc_descriptor_table(void)
98 pmc_crashlog_desc_table_t desc_tab
;
99 memset(&desc_tab
, 0, sizeof(pmc_crashlog_desc_table_t
));
103 cpu_crashlog_discovery_table_t __weak
cl_get_cpu_discovery_table(void)
105 cpu_crashlog_discovery_table_t cpu_disc_tab
;
106 memset(&cpu_disc_tab
, 0, sizeof(cpu_crashlog_discovery_table_t
));
110 int cpu_cl_poll_mailbox_ready(u32 cl_mailbox_addr
)
112 cpu_crashlog_mailbox_t cl_mailbox_interface
;
116 cl_mailbox_interface
.data
= read32((u32
*)cl_mailbox_addr
);
117 udelay(CPU_CRASHLOG_WAIT_STALL
);
119 } while ((cl_mailbox_interface
.fields
.busy
== 1)
120 && stall_cnt
< CPU_CRASHLOG_WAIT_TIMEOUT
);
122 if ((cl_mailbox_interface
.fields
.busy
== 1)
123 && (stall_cnt
>= CPU_CRASHLOG_WAIT_TIMEOUT
)) {
124 printk(BIOS_ERR
, "CPU crashlog mailbox timed out.\n");
131 int cpu_cl_mailbox_cmd(u8 cmd
, u8 param
)
133 cpu_crashlog_mailbox_t cl_mailbox_intf
;
136 memset(&cl_mailbox_intf
, 0, sizeof(cpu_crashlog_mailbox_t
));
138 cl_base_addr
= cl_get_cpu_bar_addr();
140 cl_mailbox_intf
.fields
.command
= cmd
;
141 cl_mailbox_intf
.fields
.param
= param
;
142 cl_mailbox_intf
.fields
.busy
= 1;
144 write32((u32
*)(cl_base_addr
+ cl_get_cpu_mb_int_addr()),
145 cl_mailbox_intf
.data
);
147 cpu_cl_poll_mailbox_ready(cl_base_addr
+ cl_get_cpu_mb_int_addr());
152 int __weak
cpu_cl_clear_data(void)
154 return cpu_cl_mailbox_cmd(CPU_CRASHLOG_CMD_CLEAR
, 0);
157 void __weak
cpu_cl_rearm(void)
159 /* empty implementation */
162 void __weak
cpu_cl_cleanup(void)
164 /* empty implementation */
167 int pmc_cl_gen_descriptor_table(u32 desc_table_addr
,
168 pmc_crashlog_desc_table_t
*descriptor_table
)
170 int total_data_size
= 0;
171 descriptor_table
->numb_regions
= read32((u32
*)desc_table_addr
);
172 printk(BIOS_DEBUG
, "CL PMC desc table: numb of regions is 0x%x at addr 0x%x\n",
173 descriptor_table
->numb_regions
, desc_table_addr
);
174 for (int i
= 0; i
< descriptor_table
->numb_regions
; i
++) {
175 if (i
>= ARRAY_SIZE(descriptor_table
->regions
)) {
176 printk(BIOS_ERR
, "Maximum number of PMC crashLog descriptor table exceeded (%u/%zu)\n",
177 descriptor_table
->numb_regions
,
178 ARRAY_SIZE(descriptor_table
->regions
));
181 desc_table_addr
+= 4;
182 descriptor_table
->regions
[i
].data
= read32((u32
*)(desc_table_addr
));
183 total_data_size
+= descriptor_table
->regions
[i
].bits
.size
* sizeof(u32
);
184 printk(BIOS_DEBUG
, "CL PMC desc table: region 0x%x has size 0x%x at offset 0x%x\n",
185 i
, descriptor_table
->regions
[i
].bits
.size
,
186 descriptor_table
->regions
[i
].bits
.offset
);
188 return total_data_size
;
191 bool __weak
pmc_cl_discovery(void)
196 bool __weak
cpu_cl_discovery(void)
201 int cl_pmc_re_arm_after_reset(void)
203 const struct pmc_ipc_buffer req
= { 0 };
204 struct pmc_ipc_buffer res
;
208 cmd_reg
= pmc_make_ipc_cmd(PMC_IPC_CMD_CRASHLOG
,
209 PMC_IPC_CMD_ID_CRASHLOG_RE_ARM_ON_RESET
,
210 PMC_IPC_CMD_SIZE_SHIFT
);
212 r
= pmc_send_ipc_cmd(cmd_reg
, &req
, &res
);
215 printk(BIOS_ERR
, "pmc_send_ipc_cmd failed in %s\n", __func__
);
222 /* Sends PMC IPC to clear CrashLog from PMC SSRAM area */
223 int cl_pmc_clear(void)
225 const struct pmc_ipc_buffer req
= { 0 };
226 struct pmc_ipc_buffer res
;
230 cmd_reg
= pmc_make_ipc_cmd(PMC_IPC_CMD_CRASHLOG
,
231 PMC_IPC_CMD_ID_CRASHLOG_ERASE
,
232 PMC_IPC_CMD_SIZE_SHIFT
);
234 r
= pmc_send_ipc_cmd(cmd_reg
, &req
, &res
);
237 printk(BIOS_ERR
, "pmc_send_ipc_cmd failed in %s\n", __func__
);
245 * Sends PMC IPC to populate CrashLog on all reboot.
246 * The SSRAM area will be cleared on G3 by PMC automatically
248 int cl_pmc_en_gen_on_all_reboot(void)
250 const struct pmc_ipc_buffer req
= { 0 };
251 struct pmc_ipc_buffer res
;
255 cmd_reg
= pmc_make_ipc_cmd(PMC_IPC_CMD_CRASHLOG
,
256 PMC_IPC_CMD_ID_CRASHLOG_ON_RESET
,
257 PMC_IPC_CMD_SIZE_SHIFT
);
259 r
= pmc_send_ipc_cmd(cmd_reg
, &req
, &res
);
262 printk(BIOS_ERR
, "pmc_send_ipc_cmd failed in %s\n", __func__
);
269 bool discover_crashlog(void)
271 bool cpu_cl_discovered
= false, pmc_cl_discovered
= false;
273 reset_discovery_buffers();
275 /* PCH crashLog discovery */
276 pmc_cl_discovered
= pmc_cl_discovery();
278 /* CPU crashLog discovery */
279 cpu_cl_discovered
= cpu_cl_discovery();
281 return (cpu_cl_discovered
|| pmc_cl_discovered
);
284 bool cl_copy_data_from_sram(u32 src_bar
,
292 printk(BIOS_ERR
, "Invalid bar 0x%x and offset 0x%x for %s\n",
293 src_bar
, offset
, __func__
);
297 u32 src_addr
= src_bar
+ offset
;
299 u32 data
= read32((u32
*)src_addr
);
301 /* First 32bits of the record must not be 0xdeadbeef */
302 if (data
== INVALID_CRASHLOG_RECORD
) {
303 printk(BIOS_DEBUG
, "Invalid data 0x%x at offset 0x%x from addr 0x%x\n",
304 data
, offset
, src_bar
);
308 /* PMC: copy if 1st DWORD in buffer is not zero and its 31st bit is not set */
309 if (pmc_sram
&& !(data
&& !(data
& BIT(31)))) {
310 printk(BIOS_DEBUG
, "Invalid data 0x%x at offset 0x%x from addr 0x%x"
311 " of PMC SRAM.\n", data
, offset
, src_bar
);
314 /*CPU: don't copy if 1st DWORD in first buffer is zero */
315 if (!pmc_sram
&& !data
&& (buffer_index
== 0)) {
316 printk(BIOS_DEBUG
, "Invalid data 0x%x at offset 0x%x from addr 0x%x"
317 " of telemetry SRAM.\n", data
, offset
, src_bar
);
322 while (copied
< size
) {
323 /* DW by DW copy: byte access to PMC SRAM not allowed */
324 *dest_addr
= read32((u32
*)src_addr
);
332 cl_node_t
*malloc_cl_node(size_t len
)
334 cl_node_t
*node
= malloc(sizeof(cl_node_t
));
338 node
->data
= malloc(len
* sizeof(u32
));
342 node
->size
= len
* sizeof(u32
);
348 void free_cl_node(cl_node_t
*node
)
357 void __weak
cl_get_pmc_sram_data(cl_node_t
*head
)
359 u32 tmp_bar_addr
= cl_get_cpu_tmp_bar();
360 u32 pmc_crashLog_size
= cl_get_pmc_record_size();
361 cl_node_t
*cl_cur
= head
;
363 if (!cl_pmc_sram_has_mmio_access() || !tmp_bar_addr
)
366 pmc_ipc_discovery_buf_t discovery_buf
= cl_get_pmc_discovery_buf();
368 if (discovery_buf
.bits
.supported
!= 1) {
369 printk(BIOS_DEBUG
, "PCH crashlog feature not supported.\n");
370 goto pmc_send_re_arm_after_reset
;
373 /* Get the size of data to copy */
374 if (discovery_buf
.bits
.discov_mechanism
== 1) {
375 if (discovery_buf
.bits
.base_offset
& BIT(31)) {
376 printk(BIOS_DEBUG
, "PCH discovery to be used is disabled.\n");
377 goto pmc_send_re_arm_after_reset
;
379 printk(BIOS_DEBUG
, "PMC crashLog size in discovery mode : 0x%X\n",
382 if (discovery_buf
.bits
.dis
) {
383 printk(BIOS_DEBUG
, "PCH crashlog is disabled in legacy mode.\n");
386 pmc_crashLog_size
= (discovery_buf
.bits
.size
!= 0) ?
387 discovery_buf
.bits
.size
: 0xC00;
388 printk(BIOS_DEBUG
, "PMC crashLog size in legacy mode : 0x%X\n",
392 bool pmc_sram
= true;
393 pmc_crashlog_desc_table_t descriptor_table
= cl_get_pmc_descriptor_table();
396 while (cl_cur
&& cl_cur
->next
) {
397 cl_cur
= cl_cur
->next
;
400 if (discovery_buf
.bits
.discov_mechanism
== 1) {
401 for (int i
= 0; i
< descriptor_table
.numb_regions
; i
++) {
402 cl_node_t
*cl_node
= malloc_cl_node(descriptor_table
.regions
[i
].bits
.size
);
404 printk(BIOS_DEBUG
, "failed to allocate cl_node [region = %d]\n", i
);
405 goto pmc_send_re_arm_after_reset
;
408 if (cl_copy_data_from_sram(tmp_bar_addr
,
409 descriptor_table
.regions
[i
].bits
.offset
,
410 descriptor_table
.regions
[i
].bits
.size
,
414 cl_cur
->next
= cl_node
;
415 cl_cur
= cl_cur
->next
;
417 pmc_crashLog_size
-= descriptor_table
.regions
[i
].bits
.size
*
419 printk(BIOS_DEBUG
, "discover mode PMC crashlog size adjusted"
420 " to: 0x%x\n", pmc_crashLog_size
);
422 free_cl_node(cl_node
);
426 cl_node_t
*cl_node
= malloc_cl_node(discovery_buf
.bits
.size
);
428 printk(BIOS_DEBUG
, "failed to allocate cl_node\n");
429 goto pmc_send_re_arm_after_reset
;
432 if (cl_copy_data_from_sram(tmp_bar_addr
,
433 discovery_buf
.bits
.base_offset
,
434 discovery_buf
.bits
.size
,
438 cl_cur
->next
= cl_node
;
439 cl_cur
= cl_cur
->next
;
441 pmc_crashLog_size
-= discovery_buf
.bits
.size
* sizeof(u32
);
442 printk(BIOS_DEBUG
, "legacy mode PMC crashlog size adjusted to: 0x%x\n",
445 free_cl_node(cl_node
);
449 update_new_pmc_crashlog_size(&pmc_crashLog_size
);
451 pmc_send_re_arm_after_reset
:
452 /* when bit 7 of discov cmd resp is set -> bit 2 of size field */
453 if (discovery_buf
.bits
.size
& BIT(2))
454 cl_pmc_re_arm_after_reset();
456 /* Clear the SSRAM region after copying the error log */
460 void cl_get_cpu_sram_data(cl_node_t
*head
)
462 cl_node_t
*cl_cur
= head
;
463 u32 m_cpu_crashLog_size
= cl_get_cpu_record_size();
464 cpu_crashlog_discovery_table_t cpu_cl_disc_tab
= cl_get_cpu_discovery_table();
466 if (m_cpu_crashLog_size
< 1) {
467 printk(BIOS_DEBUG
, "%s: no data to collect.\n", __func__
);
471 printk(BIOS_DEBUG
, "CPU crash data size: 0x%X bytes in 0x%X region(s).\n",
472 m_cpu_crashLog_size
, cpu_cl_disc_tab
.header
.fields
.count
);
475 while (cl_cur
&& cl_cur
->next
) {
476 cl_cur
= cl_cur
->next
;
479 for (int i
= 0 ; i
< cpu_cl_disc_tab
.header
.fields
.count
; i
++) {
480 u32 cpu_bar_addr
= cl_get_cpu_bar_addr();
481 bool pmc_sram
= false;
483 if (!cpu_cl_disc_tab
.buffers
[i
].fields
.size
) {
487 cl_node_t
*cl_node
= malloc_cl_node(cpu_cl_disc_tab
.buffers
[i
].fields
.size
);
489 printk(BIOS_DEBUG
, "failed to allocate cl_node [buffer = %d]\n", i
);
493 if (cl_copy_data_from_sram(cpu_bar_addr
,
494 cpu_cl_disc_tab
.buffers
[i
].fields
.offset
,
495 cpu_cl_disc_tab
.buffers
[i
].fields
.size
,
499 cl_cur
->next
= cl_node
;
500 cl_cur
= cl_cur
->next
;
503 m_cpu_crashLog_size
-= cpu_cl_disc_tab
.buffers
[i
].fields
.size
505 free_cl_node(cl_node
);
506 /* for CPU skip all buffers if the 1st one is not valid */
508 m_cpu_crashLog_size
= 0;
514 update_new_cpu_crashlog_size(&m_cpu_crashLog_size
);
516 /* clear telemetry SRAM region */
518 /* perform any SOC specific cleanup */
524 void collect_pmc_and_cpu_crashlog_from_srams(cl_node_t
*head
)
526 if (pmc_crashlog_support() && cl_pmc_data_present()
527 && (cl_get_pmc_record_size() > 0)) {
528 if (CONFIG(SOC_INTEL_CRASHLOG_ON_RESET
)) {
529 cl_pmc_en_gen_on_all_reboot();
530 printk(BIOS_DEBUG
, "Crashlog collection enabled on every reboot.\n");
532 cl_get_pmc_sram_data(head
);
534 printk(BIOS_DEBUG
, "Skipping PMC crashLog collection. Data not present.\n");
537 printk(BIOS_DEBUG
, "m_cpu_crashLog_size : 0x%X bytes\n", cl_get_cpu_record_size());
539 if (cpu_crashlog_support() && cl_cpu_data_present()
540 && (cl_get_cpu_record_size() > 0)) {
541 printk(BIOS_DEBUG
, "CPU crashLog present.\n");
542 cl_get_cpu_sram_data(head
);
544 printk(BIOS_DEBUG
, "Skipping CPU crashLog collection. Data not present.\n");