Debian Codegraph @

src/soc/amd/common/block/psp/psp_smm.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <device/mmio.h>
4 #include <cpu/x86/msr.h>
5 #include <cpu/amd/msr.h>
6 #include <region_file.h>
7 #include <console/console.h>
8 #include <amdblocks/psp.h>
9 #include <amdblocks/smi.h>
10 #include <soc/iomap.h>
11 #include <spi_flash.h>
12 #include <string.h>
13
14 #include "psp_def.h"
15
16 /*
17 * When sending PSP mailbox commands to the PSP from the SMI handler after the boot done
18 * command was sent, the corresponding data buffer needs to be placed in this core to PSP (C2P)
19 * buffer.
20 */
21 struct {
22 uint8_t buffer[C2P_BUFFER_MAXSIZE];
23 } __aligned(32) c2p_buffer;
24
25 /*
26 * When the PSP sends mailbox commands to the host, it will update the PSP to core (P2C) buffer
27 * and then send an SMI to the host to process the request.
28 */
29 struct {
30 uint8_t buffer[P2C_BUFFER_MAXSIZE];
31 } __aligned(32) p2c_buffer;
32
33 /*
34 * When sending PSP mailbox commands to the PSP from the SMI handler, the SMM flag needs to be
35 * set for the PSP to accept it. Otherwise it should be cleared.
36 */
37 static uint32_t smm_flag;
38
39 static void set_smm_flag(void)
40 {
41 smm_flag = 1;
42 }
43
44 static void clear_smm_flag(void)
45 {
46 smm_flag = 0;
47 }
48
49 static int send_psp_command_smm(uint32_t command, void *buffer)
50 {
51 int cmd_status;
52
53 set_smm_flag();
54 cmd_status = send_psp_command(command, buffer);
55 clear_smm_flag();
56
57 return cmd_status;
58 }
59
60 /*
61 * The MBOX_BIOS_CMD_SMM_INFO PSP mailbox command doesn't necessarily need be sent from SMM,
62 * but doing so allows the linker to sort out the addresses of c2p_buffer, p2c_buffer and
63 * smm_flag without us needing to pass this info between ramstage and smm. In the PSP gen2 case
64 * this will also make sure that the PSP MMIO base will be cached in SMM before the OS takes
65 * over so no SMN accesses will be needed during OS runtime.
66 */
67 int psp_notify_smm(void)
68 {
69 msr_t msr;
70 int cmd_status;
71 struct mbox_cmd_smm_info_buffer buffer = {
72 .header = {
73 .size = sizeof(buffer)
74 },
75 .req = {
76 .psp_smm_data_region = (uintptr_t)p2c_buffer.buffer,
77 .psp_smm_data_length = sizeof(p2c_buffer),
78 .psp_mbox_smm_buffer_address = (uintptr_t)c2p_buffer.buffer,
79 .psp_mbox_smm_flag_address = (uintptr_t)&smm_flag,
80 }
81 };
82
83 msr = rdmsr(SMM_ADDR_MSR);
84 buffer.req.smm_base = msr.raw;
85 msr = rdmsr(SMM_MASK_MSR);
86 msr.lo &= 0xffff0000; /* mask SMM_TSEG_VALID and reserved bits */
87 buffer.req.smm_mask = msr.raw;
88
89 soc_fill_smm_trig_info(&buffer.req.smm_trig_info);
90 #if (CONFIG(SOC_AMD_COMMON_BLOCK_PSP_GEN2))
91 soc_fill_smm_reg_info(&buffer.req.smm_reg_info);
92 #endif
93
94 if (CONFIG(SOC_AMD_COMMON_BLOCK_PSP_SMI)) {
95 configure_psp_smi();
96 enable_psp_smi();
97
98 /* Probe for SPI flash now as it's likely not busy */
99 assert(boot_device_spi_flash());
100 }
101
102 printk(BIOS_DEBUG, "PSP: Notify SMM info... ");
103
104 cmd_status = send_psp_command_smm(MBOX_BIOS_CMD_SMM_INFO, &buffer);
105
106 /* buffer's status shouldn't change but report it if it does */
107 psp_print_cmd_status(cmd_status, &buffer.header);
108
109 return cmd_status;
110 }
111
112 /* Notify PSP the system is going to a sleep state. */
113 void psp_notify_sx_info(uint8_t sleep_type)
114 {
115 int cmd_status;
116 struct mbox_cmd_sx_info_buffer *buffer;
117
118 /* PSP verifies that this buffer is at the address specified in psp_notify_smm() */
119 buffer = (struct mbox_cmd_sx_info_buffer *)c2p_buffer.buffer;
120 memset(buffer, 0, sizeof(*buffer));
121 buffer->header.size = sizeof(*buffer);
122
123 if (sleep_type > MBOX_BIOS_CMD_SX_INFO_SLEEP_TYPE_MAX) {
124 printk(BIOS_ERR, "PSP: BUG: invalid sleep type 0x%x requested\n", sleep_type);
125 return;
126 }
127
128 printk(BIOS_DEBUG, "PSP: Prepare to enter sleep state %d... ", sleep_type);
129
130 buffer->sleep_type = sleep_type;
131
132 cmd_status = send_psp_command_smm(MBOX_BIOS_CMD_SX_INFO, buffer);
133
134 /* buffer's status shouldn't change but report it if it does */
135 psp_print_cmd_status(cmd_status, &buffer->header);
136 }