blob: 5aedfc138f58ea6c9413847c97114cdc2042ef1f [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>
21#include <console/console.h>
22#include <amdblocks/psp.h>
23
24static const char *psp_status_nobase = "error: PSP BAR3 not assigned";
25static const char *psp_status_halted = "error: PSP in halted state";
26static const char *psp_status_recovery = "error: PSP recovery required";
27static const char *psp_status_errcmd = "error sending command";
28static const char *psp_status_init_timeout = "error: PSP init timeout";
29static const char *psp_status_cmd_timeout = "error: PSP command timeout";
30static const char *psp_status_noerror = "";
31
32static const char *status_to_string(int err)
33{
34 switch (err) {
35 case -PSPSTS_NOBASE:
36 return psp_status_nobase;
37 case -PSPSTS_HALTED:
38 return psp_status_halted;
39 case -PSPSTS_RECOVERY:
40 return psp_status_recovery;
41 case -PSPSTS_SEND_ERROR:
42 return psp_status_errcmd;
43 case -PSPSTS_INIT_TIMEOUT:
44 return psp_status_init_timeout;
45 case -PSPSTS_CMD_TIMEOUT:
46 return psp_status_cmd_timeout;
47 default:
48 return psp_status_noerror;
49 }
50}
51
52static struct psp_mbox *get_mbox_address(void)
53{
54 UINT32 base; /* UINT32 for compatibility with PspBaseLib */
55 BOOLEAN bar3_status;
56 uintptr_t baseptr;
57
58 bar3_status = GetPspBar3Addr(&base);
59 if (!bar3_status) {
60 PspBarInitEarly();
61 bar3_status = GetPspBar3Addr(&base);
62 }
63 if (!bar3_status)
64 return NULL;
65
66 baseptr = base;
67 return (struct psp_mbox *)(baseptr + PSP_MAILBOX_BASE);
68}
69
70static u32 rd_mbox_sts(struct psp_mbox *mbox)
71{
72 return read32(&mbox->mbox_status);
73}
74
75static void wr_mbox_cmd(struct psp_mbox *mbox, u32 cmd)
76{
77 write32(&mbox->mbox_command, cmd);
78}
79
80static u32 rd_mbox_cmd(struct psp_mbox *mbox)
81{
82 return read32(&mbox->mbox_command);
83}
84
85static void wr_mbox_cmd_resp(struct psp_mbox *mbox, void *buffer)
86{
87 write64(&mbox->cmd_response, (uintptr_t)buffer);
88}
89
90static u32 rd_resp_sts(struct mbox_default_buffer *buffer)
91{
92 return read32(&buffer->header.status);
93}
94
95static int wait_initialized(struct psp_mbox *mbox)
96{
97 struct stopwatch sw;
98
99 stopwatch_init_msecs_expire(&sw, PSP_INIT_TIMEOUT);
100
101 do {
102 if (rd_mbox_sts(mbox) & STATUS_INITIALIZED)
103 return 0;
104 } while (!stopwatch_expired(&sw));
105
106 return -PSPSTS_INIT_TIMEOUT;
107}
108
109static int wait_command(struct psp_mbox *mbox)
110{
111 struct stopwatch sw;
112
113 stopwatch_init_msecs_expire(&sw, PSP_CMD_TIMEOUT);
114
115 do {
116 if (!rd_mbox_cmd(mbox))
117 return 0;
118 } while (!stopwatch_expired(&sw));
119
120 return -PSPSTS_CMD_TIMEOUT;
121}
122
123static int send_psp_command(u32 command, void *buffer)
124{
125 u32 command_reg;
126 int status = 0;
127
128 struct psp_mbox *mbox = get_mbox_address();
129 if (!mbox)
130 return -PSPSTS_NOBASE;
131
Marshall Dawson37277082017-10-27 20:12:46 -0600132 command_reg = pci_read_config32(SOC_PSP_DEV, PCI_COMMAND);
133 pci_write_config32(SOC_PSP_DEV, PCI_COMMAND, command_reg |
Marshall Dawson68243a52017-06-15 16:59:20 -0600134 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
135
136 /* check for PSP error conditions */
137 if (rd_mbox_sts(mbox) & STATUS_HALT) {
138 status = -PSPSTS_HALTED;
139 goto exit;
140 }
141 if (rd_mbox_sts(mbox) & STATUS_RECOVERY) {
142 status = -PSPSTS_RECOVERY;
143 goto exit;
144 }
145
146 /* PSP must be finished with init and ready to accept a command */
147 if (wait_initialized(mbox)) {
148 status = -PSPSTS_INIT_TIMEOUT;
149 goto exit;
150 }
151 if (wait_command(mbox)) {
152 status = -PSPSTS_CMD_TIMEOUT;
153 goto exit;
154 }
155
156 /* set address of command-response buffer and write command register */
157 wr_mbox_cmd_resp(mbox, buffer);
158 wr_mbox_cmd(mbox, command);
159
160 /* PSP clears command register when complete */
161 if (wait_command(mbox)) {
162 status = -PSPSTS_CMD_TIMEOUT;
163 goto exit;
164 }
165
166 /* check delivery status */
167 if (rd_mbox_sts(mbox) & (STATUS_ERROR | STATUS_TERMINATED)) {
168 status = -PSPSTS_SEND_ERROR;
169 goto exit;
170 }
171exit:
172 /* restore command register to original value */
Marshall Dawson37277082017-10-27 20:12:46 -0600173 pci_write_config32(SOC_PSP_DEV, PCI_COMMAND, command_reg);
Marshall Dawson68243a52017-06-15 16:59:20 -0600174 return status;
175}
176
177/*
178 * Notify the PSP that DRAM is present. Upon receiving this command, the PSP
179 * will load its OS into fenced DRAM that is not accessible to the x86 cores.
180 */
181int psp_notify_dram(void)
182{
183 struct mbox_default_buffer buffer;
184 int cmd_status;
185
186 printk(BIOS_DEBUG, "PSP: Notify that DRAM is available... ");
187
188 buffer.header.size = sizeof(struct mbox_default_buffer);
189 buffer.header.status = 0; /* PSP does not report status for this cmd */
190
191 cmd_status = send_psp_command(MBOX_BIOS_CMD_DRAM_INFO, &buffer);
192
193 /* buffer's status shouldn't change but report it if it does */
194 if (rd_resp_sts(&buffer))
195 printk(BIOS_DEBUG, "buffer status=0x%x ",
196 rd_resp_sts(&buffer));
197 if (cmd_status)
198 printk(BIOS_DEBUG, "%s\n", status_to_string(cmd_status));
199 else
200 printk(BIOS_DEBUG, "OK\n");
201
202 return cmd_status;
203}
Marshall Dawson596ecec2017-10-12 16:04:08 -0600204
205/*
206 * Tell the PSP to load a firmware blob from a location in the BIOS image.
207 */
208static int psp_load_blob(int type, void *addr)
209{
210 int cmd_status;
211
212 if (!IS_ENABLED(CONFIG_SOC_AMD_PSP_SELECTABLE_SMU_FW)) {
213 printk(BIOS_ERR, "BUG: Selectable firmware is not supported\n");
214 return PSPSTS_UNSUPPORTED;
215 }
216
217 /* only two types currently supported */
218 if (type != MBOX_BIOS_CMD_SMU_FW && type != MBOX_BIOS_CMD_SMU_FW2) {
219 printk(BIOS_ERR, "BUG: Invalid PSP blob type %x\n", type);
220 return PSPSTS_INVALID_BLOB;
221 }
222
223 printk(BIOS_DEBUG, "PSP: Load blob type %x from @%p... ", type, addr);
224
225 /* Blob commands use the buffer registers as data, not pointer to buf */
226 cmd_status = send_psp_command(type, addr);
227
228 if (cmd_status)
229 printk(BIOS_DEBUG, "%s\n", status_to_string(cmd_status));
230 else
231 printk(BIOS_DEBUG, "OK\n");
232
233 return cmd_status;
234}
235
236int psp_load_named_blob(int type, const char *name)
237{
238 void *blob;
239 struct cbfsf cbfs_file;
240 struct region_device rdev;
241 int r;
242
243 if (cbfs_boot_locate(&cbfs_file, name, NULL)) {
244 printk(BIOS_ERR, "BUG: Cannot locate blob for PSP loading\n");
245 return PSPSTS_INVALID_NAME;
246 }
247
248 cbfs_file_data(&rdev, &cbfs_file);
249 blob = rdev_mmap_full(&rdev);
250 if (blob) {
251 r = psp_load_blob(type, blob);
252 rdev_munmap(&rdev, blob);
253 } else {
254 printk(BIOS_ERR, "BUG: Cannot map blob for PSP loading\n");
255 return PSPSTS_INVALID_NAME;
256 }
257 return r;
258}