blob: c5808038290185a77f2a39b53088f53713c061c1 [file] [log] [blame]
Marshall Dawson68243a52017-06-15 16:59:20 -06001/*
2 * This file is part of the coreboot project.
3 *
Marshall Dawson68243a52017-06-15 16:59:20 -06004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
Kyösti Mälkki13f66502019-03-03 08:01:05 +020015#include <device/mmio.h>
Charles Marslett81655832018-07-24 10:43:33 -050016#include <cpu/x86/msr.h>
Marshall Dawson596ecec2017-10-12 16:04:08 -060017#include <cbfs.h>
18#include <region_file.h>
Marshall Dawson68243a52017-06-15 16:59:20 -060019#include <timer.h>
20#include <device/pci_def.h>
Marshall Dawsond1cc3c22017-12-08 12:46:11 -070021#include <bootstate.h>
Marshall Dawson68243a52017-06-15 16:59:20 -060022#include <console/console.h>
Charles Marslett81655832018-07-24 10:43:33 -050023#include <device/pci_ops.h>
Marshall Dawson68243a52017-06-15 16:59:20 -060024#include <amdblocks/psp.h>
Charles Marslett81655832018-07-24 10:43:33 -050025#include <soc/iomap.h>
26#include <soc/northbridge.h>
Marshall Dawson3c578192020-01-19 17:16:01 -070027#include "psp_def.h"
Marshall Dawson68243a52017-06-15 16:59:20 -060028
29static const char *psp_status_nobase = "error: PSP BAR3 not assigned";
30static const char *psp_status_halted = "error: PSP in halted state";
31static const char *psp_status_recovery = "error: PSP recovery required";
32static const char *psp_status_errcmd = "error sending command";
33static const char *psp_status_init_timeout = "error: PSP init timeout";
34static const char *psp_status_cmd_timeout = "error: PSP command timeout";
35static const char *psp_status_noerror = "";
36
37static const char *status_to_string(int err)
38{
39 switch (err) {
40 case -PSPSTS_NOBASE:
41 return psp_status_nobase;
42 case -PSPSTS_HALTED:
43 return psp_status_halted;
44 case -PSPSTS_RECOVERY:
45 return psp_status_recovery;
46 case -PSPSTS_SEND_ERROR:
47 return psp_status_errcmd;
48 case -PSPSTS_INIT_TIMEOUT:
49 return psp_status_init_timeout;
50 case -PSPSTS_CMD_TIMEOUT:
51 return psp_status_cmd_timeout;
52 default:
53 return psp_status_noerror;
54 }
55}
56
Marshall Dawson68243a52017-06-15 16:59:20 -060057static u32 rd_mbox_sts(struct psp_mbox *mbox)
58{
59 return read32(&mbox->mbox_status);
60}
61
62static void wr_mbox_cmd(struct psp_mbox *mbox, u32 cmd)
63{
64 write32(&mbox->mbox_command, cmd);
65}
66
67static u32 rd_mbox_cmd(struct psp_mbox *mbox)
68{
69 return read32(&mbox->mbox_command);
70}
71
72static void wr_mbox_cmd_resp(struct psp_mbox *mbox, void *buffer)
73{
74 write64(&mbox->cmd_response, (uintptr_t)buffer);
75}
76
77static u32 rd_resp_sts(struct mbox_default_buffer *buffer)
78{
79 return read32(&buffer->header.status);
80}
81
82static int wait_initialized(struct psp_mbox *mbox)
83{
84 struct stopwatch sw;
85
86 stopwatch_init_msecs_expire(&sw, PSP_INIT_TIMEOUT);
87
88 do {
89 if (rd_mbox_sts(mbox) & STATUS_INITIALIZED)
90 return 0;
91 } while (!stopwatch_expired(&sw));
92
93 return -PSPSTS_INIT_TIMEOUT;
94}
95
96static int wait_command(struct psp_mbox *mbox)
97{
98 struct stopwatch sw;
99
100 stopwatch_init_msecs_expire(&sw, PSP_CMD_TIMEOUT);
101
102 do {
103 if (!rd_mbox_cmd(mbox))
104 return 0;
105 } while (!stopwatch_expired(&sw));
106
107 return -PSPSTS_CMD_TIMEOUT;
108}
109
110static int send_psp_command(u32 command, void *buffer)
111{
Felix Helddba32292020-03-31 23:54:44 +0200112 struct psp_mbox *mbox = soc_get_mbox_address();
Marshall Dawson68243a52017-06-15 16:59:20 -0600113 if (!mbox)
114 return -PSPSTS_NOBASE;
115
Marshall Dawson68243a52017-06-15 16:59:20 -0600116 /* check for PSP error conditions */
Marshall Dawson33c87732017-12-13 10:24:41 -0700117 if (rd_mbox_sts(mbox) & STATUS_HALT)
118 return -PSPSTS_HALTED;
119
120 if (rd_mbox_sts(mbox) & STATUS_RECOVERY)
121 return -PSPSTS_RECOVERY;
Marshall Dawson68243a52017-06-15 16:59:20 -0600122
123 /* PSP must be finished with init and ready to accept a command */
Marshall Dawson33c87732017-12-13 10:24:41 -0700124 if (wait_initialized(mbox))
125 return -PSPSTS_INIT_TIMEOUT;
126
127 if (wait_command(mbox))
128 return -PSPSTS_CMD_TIMEOUT;
Marshall Dawson68243a52017-06-15 16:59:20 -0600129
130 /* set address of command-response buffer and write command register */
131 wr_mbox_cmd_resp(mbox, buffer);
132 wr_mbox_cmd(mbox, command);
133
134 /* PSP clears command register when complete */
Marshall Dawson33c87732017-12-13 10:24:41 -0700135 if (wait_command(mbox))
136 return -PSPSTS_CMD_TIMEOUT;
Marshall Dawson68243a52017-06-15 16:59:20 -0600137
138 /* check delivery status */
Marshall Dawson33c87732017-12-13 10:24:41 -0700139 if (rd_mbox_sts(mbox) & (STATUS_ERROR | STATUS_TERMINATED))
140 return -PSPSTS_SEND_ERROR;
141
142 return 0;
Marshall Dawson68243a52017-06-15 16:59:20 -0600143}
144
145/*
Marshall Dawson5646a642020-01-19 16:12:25 -0700146 * Print meaningful status to the console. Caller only passes a pointer to a
147 * buffer if it's expected to contain its own status.
148 */
149static void print_cmd_status(int cmd_status, struct mbox_default_buffer *buffer)
150{
151 if (buffer && rd_resp_sts(buffer))
152 printk(BIOS_DEBUG, "buffer status=0x%x ", rd_resp_sts(buffer));
153
154 if (cmd_status)
155 printk(BIOS_DEBUG, "%s\n", status_to_string(cmd_status));
156 else
157 printk(BIOS_DEBUG, "OK\n");
158}
159
160/*
Marshall Dawson68243a52017-06-15 16:59:20 -0600161 * Notify the PSP that DRAM is present. Upon receiving this command, the PSP
162 * will load its OS into fenced DRAM that is not accessible to the x86 cores.
163 */
164int psp_notify_dram(void)
165{
Marshall Dawson68243a52017-06-15 16:59:20 -0600166 int cmd_status;
Marshall Dawson66dd3992017-12-13 10:51:13 -0700167 struct mbox_default_buffer buffer = {
168 .header = {
169 .size = sizeof(buffer)
170 }
171 };
Marshall Dawson68243a52017-06-15 16:59:20 -0600172
173 printk(BIOS_DEBUG, "PSP: Notify that DRAM is available... ");
174
Marshall Dawson68243a52017-06-15 16:59:20 -0600175 cmd_status = send_psp_command(MBOX_BIOS_CMD_DRAM_INFO, &buffer);
176
177 /* buffer's status shouldn't change but report it if it does */
Marshall Dawson5646a642020-01-19 16:12:25 -0700178 print_cmd_status(cmd_status, &buffer);
Marshall Dawson68243a52017-06-15 16:59:20 -0600179
180 return cmd_status;
181}
Marshall Dawson596ecec2017-10-12 16:04:08 -0600182
183/*
Marshall Dawsond1cc3c22017-12-08 12:46:11 -0700184 * Notify the PSP that the system is completing the boot process. Upon
185 * receiving this command, the PSP will only honor commands where the buffer
186 * is in SMM space.
187 */
188static void psp_notify_boot_done(void *unused)
189{
190 int cmd_status;
191 struct mbox_default_buffer buffer = {
192 .header = {
193 .size = sizeof(buffer)
194 }
195 };
196
197 printk(BIOS_DEBUG, "PSP: Notify that POST is finishing... ");
198
199 cmd_status = send_psp_command(MBOX_BIOS_CMD_BOOT_DONE, &buffer);
200
201 /* buffer's status shouldn't change but report it if it does */
Marshall Dawson5646a642020-01-19 16:12:25 -0700202 print_cmd_status(cmd_status, &buffer);
Marshall Dawsond1cc3c22017-12-08 12:46:11 -0700203}
204
205/*
Marshall Dawson596ecec2017-10-12 16:04:08 -0600206 * Tell the PSP to load a firmware blob from a location in the BIOS image.
207 */
Marshall Dawson737e56a2020-01-19 16:32:08 -0700208int psp_load_named_blob(enum psp_blob_type type, const char *name)
Marshall Dawson596ecec2017-10-12 16:04:08 -0600209{
210 int cmd_status;
Marshall Dawson737e56a2020-01-19 16:32:08 -0700211 u32 command;
Marshall Dawson596ecec2017-10-12 16:04:08 -0600212 void *blob;
213 struct cbfsf cbfs_file;
214 struct region_device rdev;
Marshall Dawson737e56a2020-01-19 16:32:08 -0700215
216 switch (type) {
217 case BLOB_SMU_FW:
218 command = MBOX_BIOS_CMD_SMU_FW;
219 break;
220 case BLOB_SMU_FW2:
221 command = MBOX_BIOS_CMD_SMU_FW2;
222 break;
223 default:
224 printk(BIOS_ERR, "BUG: Invalid PSP blob type %x\n", type);
225 return -PSPSTS_INVALID_BLOB;
226 }
227
228 /* type can only be BLOB_SMU_FW or BLOB_SMU_FW2 here, so don't re-check for this */
229 if (!CONFIG(SOC_AMD_PSP_SELECTABLE_SMU_FW)) {
230 printk(BIOS_ERR, "BUG: Selectable firmware is not supported\n");
231 return -PSPSTS_UNSUPPORTED;
232 }
Marshall Dawson596ecec2017-10-12 16:04:08 -0600233
234 if (cbfs_boot_locate(&cbfs_file, name, NULL)) {
235 printk(BIOS_ERR, "BUG: Cannot locate blob for PSP loading\n");
Marshall Dawson737e56a2020-01-19 16:32:08 -0700236 return -PSPSTS_INVALID_NAME;
Marshall Dawson596ecec2017-10-12 16:04:08 -0600237 }
238
239 cbfs_file_data(&rdev, &cbfs_file);
240 blob = rdev_mmap_full(&rdev);
Marshall Dawson737e56a2020-01-19 16:32:08 -0700241 if (!blob) {
Marshall Dawson596ecec2017-10-12 16:04:08 -0600242 printk(BIOS_ERR, "BUG: Cannot map blob for PSP loading\n");
Marshall Dawson737e56a2020-01-19 16:32:08 -0700243 return -PSPSTS_INVALID_NAME;
Marshall Dawson596ecec2017-10-12 16:04:08 -0600244 }
Marshall Dawson737e56a2020-01-19 16:32:08 -0700245
246 printk(BIOS_DEBUG, "PSP: Load blob type %x from @%p... ", type, blob);
247
248 /* Blob commands use the buffer registers as data, not pointer to buf */
249 cmd_status = send_psp_command(command, blob);
250 print_cmd_status(cmd_status, NULL);
251
252 rdev_munmap(&rdev, blob);
253 return cmd_status;
Marshall Dawson596ecec2017-10-12 16:04:08 -0600254}
Marshall Dawsond1cc3c22017-12-08 12:46:11 -0700255
256BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY,
257 psp_notify_boot_done, NULL);