blob: a1a0e5baec8fd23b8fc67b6d637cc0c7df0da1f3 [file] [log] [blame]
Marshall Dawson68243a52017-06-15 16:59:20 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2017 Advanced Micro Devices, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <arch/io.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>
23#include <amdblocks/psp.h>
24
25static const char *psp_status_nobase = "error: PSP BAR3 not assigned";
26static const char *psp_status_halted = "error: PSP in halted state";
27static const char *psp_status_recovery = "error: PSP recovery required";
28static const char *psp_status_errcmd = "error sending command";
29static const char *psp_status_init_timeout = "error: PSP init timeout";
30static const char *psp_status_cmd_timeout = "error: PSP command timeout";
31static const char *psp_status_noerror = "";
32
33static const char *status_to_string(int err)
34{
35 switch (err) {
36 case -PSPSTS_NOBASE:
37 return psp_status_nobase;
38 case -PSPSTS_HALTED:
39 return psp_status_halted;
40 case -PSPSTS_RECOVERY:
41 return psp_status_recovery;
42 case -PSPSTS_SEND_ERROR:
43 return psp_status_errcmd;
44 case -PSPSTS_INIT_TIMEOUT:
45 return psp_status_init_timeout;
46 case -PSPSTS_CMD_TIMEOUT:
47 return psp_status_cmd_timeout;
48 default:
49 return psp_status_noerror;
50 }
51}
52
53static struct psp_mbox *get_mbox_address(void)
54{
55 UINT32 base; /* UINT32 for compatibility with PspBaseLib */
56 BOOLEAN bar3_status;
57 uintptr_t baseptr;
58
59 bar3_status = GetPspBar3Addr(&base);
60 if (!bar3_status) {
61 PspBarInitEarly();
62 bar3_status = GetPspBar3Addr(&base);
63 }
64 if (!bar3_status)
65 return NULL;
66
67 baseptr = base;
68 return (struct psp_mbox *)(baseptr + PSP_MAILBOX_BASE);
69}
70
71static u32 rd_mbox_sts(struct psp_mbox *mbox)
72{
73 return read32(&mbox->mbox_status);
74}
75
76static void wr_mbox_cmd(struct psp_mbox *mbox, u32 cmd)
77{
78 write32(&mbox->mbox_command, cmd);
79}
80
81static u32 rd_mbox_cmd(struct psp_mbox *mbox)
82{
83 return read32(&mbox->mbox_command);
84}
85
86static void wr_mbox_cmd_resp(struct psp_mbox *mbox, void *buffer)
87{
88 write64(&mbox->cmd_response, (uintptr_t)buffer);
89}
90
91static u32 rd_resp_sts(struct mbox_default_buffer *buffer)
92{
93 return read32(&buffer->header.status);
94}
95
96static int wait_initialized(struct psp_mbox *mbox)
97{
98 struct stopwatch sw;
99
100 stopwatch_init_msecs_expire(&sw, PSP_INIT_TIMEOUT);
101
102 do {
103 if (rd_mbox_sts(mbox) & STATUS_INITIALIZED)
104 return 0;
105 } while (!stopwatch_expired(&sw));
106
107 return -PSPSTS_INIT_TIMEOUT;
108}
109
110static int wait_command(struct psp_mbox *mbox)
111{
112 struct stopwatch sw;
113
114 stopwatch_init_msecs_expire(&sw, PSP_CMD_TIMEOUT);
115
116 do {
117 if (!rd_mbox_cmd(mbox))
118 return 0;
119 } while (!stopwatch_expired(&sw));
120
121 return -PSPSTS_CMD_TIMEOUT;
122}
123
124static int send_psp_command(u32 command, void *buffer)
125{
Marshall Dawson68243a52017-06-15 16:59:20 -0600126 struct psp_mbox *mbox = get_mbox_address();
127 if (!mbox)
128 return -PSPSTS_NOBASE;
129
Marshall Dawson68243a52017-06-15 16:59:20 -0600130 /* check for PSP error conditions */
Marshall Dawson33c87732017-12-13 10:24:41 -0700131 if (rd_mbox_sts(mbox) & STATUS_HALT)
132 return -PSPSTS_HALTED;
133
134 if (rd_mbox_sts(mbox) & STATUS_RECOVERY)
135 return -PSPSTS_RECOVERY;
Marshall Dawson68243a52017-06-15 16:59:20 -0600136
137 /* PSP must be finished with init and ready to accept a command */
Marshall Dawson33c87732017-12-13 10:24:41 -0700138 if (wait_initialized(mbox))
139 return -PSPSTS_INIT_TIMEOUT;
140
141 if (wait_command(mbox))
142 return -PSPSTS_CMD_TIMEOUT;
Marshall Dawson68243a52017-06-15 16:59:20 -0600143
144 /* set address of command-response buffer and write command register */
145 wr_mbox_cmd_resp(mbox, buffer);
146 wr_mbox_cmd(mbox, command);
147
148 /* PSP clears command register when complete */
Marshall Dawson33c87732017-12-13 10:24:41 -0700149 if (wait_command(mbox))
150 return -PSPSTS_CMD_TIMEOUT;
Marshall Dawson68243a52017-06-15 16:59:20 -0600151
152 /* check delivery status */
Marshall Dawson33c87732017-12-13 10:24:41 -0700153 if (rd_mbox_sts(mbox) & (STATUS_ERROR | STATUS_TERMINATED))
154 return -PSPSTS_SEND_ERROR;
155
156 return 0;
Marshall Dawson68243a52017-06-15 16:59:20 -0600157}
158
159/*
160 * Notify the PSP that DRAM is present. Upon receiving this command, the PSP
161 * will load its OS into fenced DRAM that is not accessible to the x86 cores.
162 */
163int psp_notify_dram(void)
164{
Marshall Dawson68243a52017-06-15 16:59:20 -0600165 int cmd_status;
Marshall Dawson66dd3992017-12-13 10:51:13 -0700166 struct mbox_default_buffer buffer = {
167 .header = {
168 .size = sizeof(buffer)
169 }
170 };
Marshall Dawson68243a52017-06-15 16:59:20 -0600171
172 printk(BIOS_DEBUG, "PSP: Notify that DRAM is available... ");
173
Marshall Dawson68243a52017-06-15 16:59:20 -0600174 cmd_status = send_psp_command(MBOX_BIOS_CMD_DRAM_INFO, &buffer);
175
176 /* buffer's status shouldn't change but report it if it does */
177 if (rd_resp_sts(&buffer))
178 printk(BIOS_DEBUG, "buffer status=0x%x ",
179 rd_resp_sts(&buffer));
180 if (cmd_status)
181 printk(BIOS_DEBUG, "%s\n", status_to_string(cmd_status));
182 else
183 printk(BIOS_DEBUG, "OK\n");
184
185 return cmd_status;
186}
Marshall Dawson596ecec2017-10-12 16:04:08 -0600187
188/*
Marshall Dawsond1cc3c22017-12-08 12:46:11 -0700189 * Notify the PSP that the system is completing the boot process. Upon
190 * receiving this command, the PSP will only honor commands where the buffer
191 * is in SMM space.
192 */
193static void psp_notify_boot_done(void *unused)
194{
195 int cmd_status;
196 struct mbox_default_buffer buffer = {
197 .header = {
198 .size = sizeof(buffer)
199 }
200 };
201
202 printk(BIOS_DEBUG, "PSP: Notify that POST is finishing... ");
203
204 cmd_status = send_psp_command(MBOX_BIOS_CMD_BOOT_DONE, &buffer);
205
206 /* buffer's status shouldn't change but report it if it does */
207 if (rd_resp_sts(&buffer))
208 printk(BIOS_DEBUG, "buffer status=0x%x ",
209 rd_resp_sts(&buffer));
210 if (cmd_status)
211 printk(BIOS_DEBUG, "%s\n", status_to_string(cmd_status));
212 else
213 printk(BIOS_DEBUG, "OK\n");
214}
215
216/*
Marshall Dawson596ecec2017-10-12 16:04:08 -0600217 * Tell the PSP to load a firmware blob from a location in the BIOS image.
218 */
219static int psp_load_blob(int type, void *addr)
220{
221 int cmd_status;
222
223 if (!IS_ENABLED(CONFIG_SOC_AMD_PSP_SELECTABLE_SMU_FW)) {
224 printk(BIOS_ERR, "BUG: Selectable firmware is not supported\n");
225 return PSPSTS_UNSUPPORTED;
226 }
227
228 /* only two types currently supported */
229 if (type != MBOX_BIOS_CMD_SMU_FW && type != MBOX_BIOS_CMD_SMU_FW2) {
230 printk(BIOS_ERR, "BUG: Invalid PSP blob type %x\n", type);
231 return PSPSTS_INVALID_BLOB;
232 }
233
234 printk(BIOS_DEBUG, "PSP: Load blob type %x from @%p... ", type, addr);
235
236 /* Blob commands use the buffer registers as data, not pointer to buf */
237 cmd_status = send_psp_command(type, addr);
238
239 if (cmd_status)
240 printk(BIOS_DEBUG, "%s\n", status_to_string(cmd_status));
241 else
242 printk(BIOS_DEBUG, "OK\n");
243
244 return cmd_status;
245}
246
247int psp_load_named_blob(int type, const char *name)
248{
249 void *blob;
250 struct cbfsf cbfs_file;
251 struct region_device rdev;
252 int r;
253
254 if (cbfs_boot_locate(&cbfs_file, name, NULL)) {
255 printk(BIOS_ERR, "BUG: Cannot locate blob for PSP loading\n");
256 return PSPSTS_INVALID_NAME;
257 }
258
259 cbfs_file_data(&rdev, &cbfs_file);
260 blob = rdev_mmap_full(&rdev);
261 if (blob) {
262 r = psp_load_blob(type, blob);
263 rdev_munmap(&rdev, blob);
264 } else {
265 printk(BIOS_ERR, "BUG: Cannot map blob for PSP loading\n");
266 return PSPSTS_INVALID_NAME;
267 }
268 return r;
269}
Marshall Dawsond1cc3c22017-12-08 12:46:11 -0700270
271BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY,
272 psp_notify_boot_done, NULL);