1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
5 #include <device/mmio.h>
6 #include <intelblocks/crashlog.h>
7 #include <intelblocks/pmc_ipc.h>
10 int __weak
cl_get_cpu_record_size(void)
15 int __weak
cl_get_pmc_record_size(void)
20 uintptr_t __weak
cl_get_cpu_bar_addr(void)
25 int __weak
cl_get_ioe_record_size(void)
30 uintptr_t __weak
cl_get_cpu_tmp_bar(void)
35 uintptr_t __weak
cl_get_cpu_mb_int_addr(void)
40 int __weak
cl_get_total_data_size(void)
42 return cl_get_cpu_record_size() + cl_get_pmc_record_size();
45 bool __weak
cl_pmc_sram_has_mmio_access(void)
50 bool __weak
cl_ioe_sram_has_mmio_access(void)
55 bool __weak
cpu_crashlog_support(void)
60 bool __weak
pmc_crashlog_support(void)
65 bool __weak
cl_cpu_data_present(void)
70 bool __weak
cl_pmc_data_present(void)
75 bool __weak
cl_ioe_data_present(void)
80 __weak
void reset_discovery_buffers(void) {}
82 __weak
void update_new_pmc_crashlog_size(u32
*pmc_crash_size
) {}
84 __weak
void update_new_cpu_crashlog_size(u32
*cpu_crash_size
) {}
86 __weak
void update_new_ioe_crashlog_size(u32
*ioe_crash_size
) {}
88 pmc_ipc_discovery_buf_t __weak
cl_get_pmc_discovery_buf(void)
90 pmc_ipc_discovery_buf_t discov_buf
;
91 memset(&discov_buf
, 0, sizeof(pmc_ipc_discovery_buf_t
));
95 pmc_crashlog_desc_table_t __weak
cl_get_pmc_descriptor_table(void)
97 pmc_crashlog_desc_table_t desc_tab
;
98 memset(&desc_tab
, 0, sizeof(pmc_crashlog_desc_table_t
));
102 cpu_crashlog_discovery_table_t __weak
cl_get_cpu_discovery_table(void)
104 cpu_crashlog_discovery_table_t cpu_disc_tab
;
105 memset(&cpu_disc_tab
, 0, sizeof(cpu_crashlog_discovery_table_t
));
109 int cpu_cl_poll_mailbox_ready(uintptr_t cl_mailbox_addr
)
111 cpu_crashlog_mailbox_t cl_mailbox_interface
;
115 cl_mailbox_interface
.data
= read32p(cl_mailbox_addr
);
116 udelay(CPU_CRASHLOG_WAIT_STALL
);
118 } while ((cl_mailbox_interface
.fields
.busy
== 1)
119 && stall_cnt
< CPU_CRASHLOG_WAIT_TIMEOUT
);
121 if ((cl_mailbox_interface
.fields
.busy
== 1)
122 && (stall_cnt
>= CPU_CRASHLOG_WAIT_TIMEOUT
)) {
123 printk(BIOS_ERR
, "CPU crashlog mailbox timed out.\n");
130 int cpu_cl_mailbox_cmd(u8 cmd
, u8 param
)
132 cpu_crashlog_mailbox_t cl_mailbox_intf
;
133 uintptr_t cl_base_addr
;
135 memset(&cl_mailbox_intf
, 0, sizeof(cpu_crashlog_mailbox_t
));
137 cl_base_addr
= cl_get_cpu_bar_addr();
139 cl_mailbox_intf
.fields
.command
= cmd
;
140 cl_mailbox_intf
.fields
.param
= param
;
141 cl_mailbox_intf
.fields
.busy
= 1;
143 write32p((cl_base_addr
+ cl_get_cpu_mb_int_addr()),
144 cl_mailbox_intf
.data
);
146 cpu_cl_poll_mailbox_ready(cl_base_addr
+ cl_get_cpu_mb_int_addr());
151 int __weak
cpu_cl_clear_data(void)
153 return cpu_cl_mailbox_cmd(CPU_CRASHLOG_CMD_CLEAR
, 0);
156 void __weak
cpu_cl_rearm(void)
158 /* empty implementation */
161 void __weak
cpu_cl_cleanup(void)
163 /* empty implementation */
166 int pmc_cl_gen_descriptor_table(uintptr_t desc_table_addr
,
167 pmc_crashlog_desc_table_t
*descriptor_table
)
169 int total_data_size
= 0;
170 descriptor_table
->numb_regions
= read32p(desc_table_addr
);
171 printk(BIOS_DEBUG
, "CL PMC desc table: numb of regions is 0x%x at addr 0x%lx\n",
172 descriptor_table
->numb_regions
, desc_table_addr
);
173 for (int i
= 0; i
< descriptor_table
->numb_regions
; i
++) {
174 if (i
>= ARRAY_SIZE(descriptor_table
->regions
)) {
175 printk(BIOS_ERR
, "Maximum number of PMC crashLog descriptor table exceeded (%u/%zu)\n",
176 descriptor_table
->numb_regions
,
177 ARRAY_SIZE(descriptor_table
->regions
));
180 desc_table_addr
+= sizeof(u32
);
181 descriptor_table
->regions
[i
].data
= read32p(desc_table_addr
);
182 total_data_size
+= descriptor_table
->regions
[i
].bits
.size
* sizeof(u32
);
183 printk(BIOS_DEBUG
, "CL PMC desc table: region 0x%x has size 0x%x at offset 0x%x\n",
184 i
, descriptor_table
->regions
[i
].bits
.size
,
185 descriptor_table
->regions
[i
].bits
.offset
);
187 return total_data_size
;
190 bool __weak
pmc_cl_discovery(void)
195 bool __weak
cpu_cl_discovery(void)
200 int cl_pmc_re_arm_after_reset(void)
202 const struct pmc_ipc_buffer req
= { 0 };
203 struct pmc_ipc_buffer res
;
207 cmd_reg
= pmc_make_ipc_cmd(PMC_IPC_CMD_CRASHLOG
,
208 PMC_IPC_CMD_ID_CRASHLOG_RE_ARM_ON_RESET
,
209 PMC_IPC_CMD_SIZE_SHIFT
);
211 r
= pmc_send_ipc_cmd(cmd_reg
, &req
, &res
);
214 printk(BIOS_ERR
, "pmc_send_ipc_cmd failed in %s\n", __func__
);
221 /* Sends PMC IPC to clear CrashLog from PMC SSRAM area */
222 int cl_pmc_clear(void)
224 const struct pmc_ipc_buffer req
= { 0 };
225 struct pmc_ipc_buffer res
;
229 cmd_reg
= pmc_make_ipc_cmd(PMC_IPC_CMD_CRASHLOG
,
230 PMC_IPC_CMD_ID_CRASHLOG_ERASE
,
231 PMC_IPC_CMD_SIZE_SHIFT
);
233 r
= pmc_send_ipc_cmd(cmd_reg
, &req
, &res
);
236 printk(BIOS_ERR
, "pmc_send_ipc_cmd failed in %s\n", __func__
);
244 * Sends PMC IPC to populate CrashLog on all reboot.
245 * The SSRAM area will be cleared on G3 by PMC automatically
247 int cl_pmc_en_gen_on_all_reboot(void)
249 const struct pmc_ipc_buffer req
= { 0 };
250 struct pmc_ipc_buffer res
;
254 cmd_reg
= pmc_make_ipc_cmd(PMC_IPC_CMD_CRASHLOG
,
255 PMC_IPC_CMD_ID_CRASHLOG_ON_RESET
,
256 PMC_IPC_CMD_SIZE_SHIFT
);
258 r
= pmc_send_ipc_cmd(cmd_reg
, &req
, &res
);
261 printk(BIOS_ERR
, "pmc_send_ipc_cmd failed in %s\n", __func__
);
268 bool discover_crashlog(void)
270 bool cpu_cl_discovered
= false, pmc_cl_discovered
= false;
272 reset_discovery_buffers();
274 /* PCH crashLog discovery */
275 pmc_cl_discovered
= pmc_cl_discovery();
277 /* CPU crashLog discovery */
278 cpu_cl_discovered
= cpu_cl_discovery();
280 return (cpu_cl_discovered
|| pmc_cl_discovered
);
283 bool cl_copy_data_from_sram(uintptr_t src_bar
, u32 offset
, size_t size
, u32
*dest_addr
,
284 u32 buffer_index
, bool pmc_sram
)
287 printk(BIOS_ERR
, "Invalid bar 0x%lx and offset 0x%x for %s\n",
288 src_bar
, offset
, __func__
);
292 uintptr_t src_addr
= src_bar
+ offset
;
294 u32 data
= read32p(src_addr
);
296 /* First 32bits of the record must not be 0xdeadbeef */
297 if (data
== INVALID_CRASHLOG_RECORD
) {
298 printk(BIOS_DEBUG
, "Invalid data 0x%x at offset 0x%x from addr 0x%lx\n",
299 data
, offset
, src_bar
);
303 /* PMC: copy if 1st DWORD in buffer is not zero and its 31st bit is not set */
304 if (pmc_sram
&& !(data
&& !(data
& BIT(31)))) {
305 printk(BIOS_DEBUG
, "Invalid data 0x%x at offset 0x%x from addr 0x%lx"
306 " of PMC SRAM.\n", data
, offset
, src_bar
);
309 /*CPU: don't copy if 1st DWORD in first buffer is zero */
310 if (!pmc_sram
&& !data
&& (buffer_index
== 0)) {
311 printk(BIOS_DEBUG
, "Invalid data 0x%x at offset 0x%x from addr 0x%lx"
312 " of telemetry SRAM.\n", data
, offset
, src_bar
);
317 while (copied
< size
) {
318 /* DW by DW copy: byte access to PMC SRAM not allowed */
319 *dest_addr
= read32p(src_addr
);
321 src_addr
+= sizeof(u32
);
327 cl_node_t
*malloc_cl_node(size_t len
)
329 cl_node_t
*node
= malloc(sizeof(cl_node_t
));
333 node
->data
= malloc(len
* sizeof(u32
));
337 node
->size
= len
* sizeof(u32
);
343 void free_cl_node(cl_node_t
*node
)
352 void __weak
cl_get_pmc_sram_data(cl_node_t
*head
)
354 uintptr_t tmp_bar_addr
= cl_get_cpu_tmp_bar();
355 u32 pmc_crashLog_size
= cl_get_pmc_record_size();
356 cl_node_t
*cl_cur
= head
;
358 if (!cl_pmc_sram_has_mmio_access() || !tmp_bar_addr
)
361 pmc_ipc_discovery_buf_t discovery_buf
= cl_get_pmc_discovery_buf();
363 if (discovery_buf
.bits
.supported
!= 1) {
364 printk(BIOS_DEBUG
, "PCH crashlog feature not supported.\n");
365 goto pmc_send_re_arm_after_reset
;
368 /* Get the size of data to copy */
369 if (discovery_buf
.bits
.discov_mechanism
== 1) {
370 if (discovery_buf
.bits
.base_offset
& BIT(31)) {
371 printk(BIOS_DEBUG
, "PCH discovery to be used is disabled.\n");
372 goto pmc_send_re_arm_after_reset
;
374 printk(BIOS_DEBUG
, "PMC crashLog size in discovery mode : 0x%X\n",
377 if (discovery_buf
.bits
.dis
) {
378 printk(BIOS_DEBUG
, "PCH crashlog is disabled in legacy mode.\n");
381 pmc_crashLog_size
= (discovery_buf
.bits
.size
!= 0) ?
382 discovery_buf
.bits
.size
* sizeof(u32
) : 0xC00;
383 printk(BIOS_DEBUG
, "PMC crashLog size in legacy mode : 0x%X\n",
387 bool pmc_sram
= true;
388 pmc_crashlog_desc_table_t descriptor_table
= cl_get_pmc_descriptor_table();
391 while (cl_cur
&& cl_cur
->next
) {
392 cl_cur
= cl_cur
->next
;
395 if (discovery_buf
.bits
.discov_mechanism
== 1) {
396 for (int i
= 0; i
< descriptor_table
.numb_regions
; i
++) {
397 cl_node_t
*cl_node
= malloc_cl_node(descriptor_table
.regions
[i
].bits
.size
);
399 printk(BIOS_DEBUG
, "failed to allocate cl_node [region = %d]\n", i
);
400 goto pmc_send_re_arm_after_reset
;
403 if (cl_copy_data_from_sram(tmp_bar_addr
,
404 descriptor_table
.regions
[i
].bits
.offset
,
405 descriptor_table
.regions
[i
].bits
.size
,
409 cl_cur
->next
= cl_node
;
410 cl_cur
= cl_cur
->next
;
412 pmc_crashLog_size
-= descriptor_table
.regions
[i
].bits
.size
*
414 printk(BIOS_DEBUG
, "discover mode PMC crashlog size adjusted"
415 " to: 0x%x\n", pmc_crashLog_size
);
417 free_cl_node(cl_node
);
421 cl_node_t
*cl_node
= malloc_cl_node(discovery_buf
.bits
.size
);
423 printk(BIOS_DEBUG
, "failed to allocate cl_node\n");
424 goto pmc_send_re_arm_after_reset
;
427 if (cl_copy_data_from_sram(tmp_bar_addr
,
428 discovery_buf
.bits
.base_offset
,
429 discovery_buf
.bits
.size
,
433 cl_cur
->next
= cl_node
;
434 cl_cur
= cl_cur
->next
;
436 pmc_crashLog_size
-= discovery_buf
.bits
.size
* sizeof(u32
);
437 printk(BIOS_DEBUG
, "legacy mode PMC crashlog size adjusted to: 0x%x\n",
440 free_cl_node(cl_node
);
444 update_new_pmc_crashlog_size(&pmc_crashLog_size
);
446 pmc_send_re_arm_after_reset
:
447 /* when bit 7 of discov cmd resp is set -> bit 2 of size field */
448 if (discovery_buf
.bits
.size
& BIT(2))
449 cl_pmc_re_arm_after_reset();
451 /* Clear the SSRAM region after copying the error log */
455 void cl_get_cpu_sram_data(cl_node_t
*head
)
457 cl_node_t
*cl_cur
= head
;
458 u32 m_cpu_crashLog_size
= cl_get_cpu_record_size();
459 cpu_crashlog_discovery_table_t cpu_cl_disc_tab
= cl_get_cpu_discovery_table();
461 if (m_cpu_crashLog_size
< 1) {
462 printk(BIOS_DEBUG
, "%s: no data to collect.\n", __func__
);
466 printk(BIOS_DEBUG
, "CPU crash data size: 0x%X bytes in 0x%X region(s).\n",
467 m_cpu_crashLog_size
, cpu_cl_disc_tab
.header
.fields
.count
);
470 while (cl_cur
&& cl_cur
->next
) {
471 cl_cur
= cl_cur
->next
;
474 for (int i
= 0 ; i
< cpu_cl_disc_tab
.header
.fields
.count
; i
++) {
475 uintptr_t cpu_bar_addr
= cl_get_cpu_bar_addr();
476 bool pmc_sram
= false;
477 if (!cpu_cl_disc_tab
.buffers
[i
].fields
.size
) {
481 cl_node_t
*cl_node
= malloc_cl_node(cpu_cl_disc_tab
.buffers
[i
].fields
.size
);
483 printk(BIOS_DEBUG
, "failed to allocate cl_node [buffer = %d]\n", i
);
487 if (cl_copy_data_from_sram(cpu_bar_addr
,
488 cpu_cl_disc_tab
.buffers
[i
].fields
.offset
,
489 cpu_cl_disc_tab
.buffers
[i
].fields
.size
,
493 cl_cur
->next
= cl_node
;
494 cl_cur
= cl_cur
->next
;
497 m_cpu_crashLog_size
-= cpu_cl_disc_tab
.buffers
[i
].fields
.size
499 free_cl_node(cl_node
);
500 /* for CPU skip all buffers if the 1st one is not valid */
502 m_cpu_crashLog_size
= 0;
508 update_new_cpu_crashlog_size(&m_cpu_crashLog_size
);
510 /* clear telemetry SRAM region */
512 /* perform any SOC specific cleanup */
518 void collect_pmc_and_cpu_crashlog_from_srams(cl_node_t
*head
)
520 if (pmc_crashlog_support() && cl_pmc_data_present()
521 && (cl_get_pmc_record_size() > 0)) {
522 if (CONFIG(SOC_INTEL_CRASHLOG_ON_RESET
)) {
523 cl_pmc_en_gen_on_all_reboot();
524 printk(BIOS_DEBUG
, "Crashlog collection enabled on every reboot.\n");
526 cl_get_pmc_sram_data(head
);
528 printk(BIOS_DEBUG
, "Skipping PMC crashLog collection. Data not present.\n");
531 printk(BIOS_DEBUG
, "m_cpu_crashLog_size : 0x%X bytes\n", cl_get_cpu_record_size());
533 if (cpu_crashlog_support() && cl_cpu_data_present()
534 && (cl_get_cpu_record_size() > 0)) {
535 printk(BIOS_DEBUG
, "CPU crashLog present.\n");
536 cl_get_cpu_sram_data(head
);
538 printk(BIOS_DEBUG
, "Skipping CPU crashLog collection. Data not present.\n");