Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 4 | * |
| 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älkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 15 | #include <device/mmio.h> |
Charles Marslett | 8165583 | 2018-07-24 10:43:33 -0500 | [diff] [blame] | 16 | #include <cpu/x86/msr.h> |
Marshall Dawson | 596ecec | 2017-10-12 16:04:08 -0600 | [diff] [blame] | 17 | #include <cbfs.h> |
| 18 | #include <region_file.h> |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 19 | #include <timer.h> |
| 20 | #include <device/pci_def.h> |
Marshall Dawson | d1cc3c2 | 2017-12-08 12:46:11 -0700 | [diff] [blame] | 21 | #include <bootstate.h> |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 22 | #include <console/console.h> |
Charles Marslett | 8165583 | 2018-07-24 10:43:33 -0500 | [diff] [blame] | 23 | #include <device/pci_ops.h> |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 24 | #include <amdblocks/psp.h> |
Charles Marslett | 8165583 | 2018-07-24 10:43:33 -0500 | [diff] [blame] | 25 | #include <soc/iomap.h> |
| 26 | #include <soc/northbridge.h> |
Marshall Dawson | 3c57819 | 2020-01-19 17:16:01 -0700 | [diff] [blame^] | 27 | #include "psp_def.h" |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 28 | |
| 29 | static const char *psp_status_nobase = "error: PSP BAR3 not assigned"; |
| 30 | static const char *psp_status_halted = "error: PSP in halted state"; |
| 31 | static const char *psp_status_recovery = "error: PSP recovery required"; |
| 32 | static const char *psp_status_errcmd = "error sending command"; |
| 33 | static const char *psp_status_init_timeout = "error: PSP init timeout"; |
| 34 | static const char *psp_status_cmd_timeout = "error: PSP command timeout"; |
| 35 | static const char *psp_status_noerror = ""; |
| 36 | |
| 37 | static 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 Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 57 | static u32 rd_mbox_sts(struct psp_mbox *mbox) |
| 58 | { |
| 59 | return read32(&mbox->mbox_status); |
| 60 | } |
| 61 | |
| 62 | static void wr_mbox_cmd(struct psp_mbox *mbox, u32 cmd) |
| 63 | { |
| 64 | write32(&mbox->mbox_command, cmd); |
| 65 | } |
| 66 | |
| 67 | static u32 rd_mbox_cmd(struct psp_mbox *mbox) |
| 68 | { |
| 69 | return read32(&mbox->mbox_command); |
| 70 | } |
| 71 | |
| 72 | static void wr_mbox_cmd_resp(struct psp_mbox *mbox, void *buffer) |
| 73 | { |
| 74 | write64(&mbox->cmd_response, (uintptr_t)buffer); |
| 75 | } |
| 76 | |
| 77 | static u32 rd_resp_sts(struct mbox_default_buffer *buffer) |
| 78 | { |
| 79 | return read32(&buffer->header.status); |
| 80 | } |
| 81 | |
| 82 | static 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 | |
| 96 | static 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 | |
| 110 | static int send_psp_command(u32 command, void *buffer) |
| 111 | { |
Felix Held | dba3229 | 2020-03-31 23:54:44 +0200 | [diff] [blame] | 112 | struct psp_mbox *mbox = soc_get_mbox_address(); |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 113 | if (!mbox) |
| 114 | return -PSPSTS_NOBASE; |
| 115 | |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 116 | /* check for PSP error conditions */ |
Marshall Dawson | 33c8773 | 2017-12-13 10:24:41 -0700 | [diff] [blame] | 117 | 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 Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 122 | |
| 123 | /* PSP must be finished with init and ready to accept a command */ |
Marshall Dawson | 33c8773 | 2017-12-13 10:24:41 -0700 | [diff] [blame] | 124 | if (wait_initialized(mbox)) |
| 125 | return -PSPSTS_INIT_TIMEOUT; |
| 126 | |
| 127 | if (wait_command(mbox)) |
| 128 | return -PSPSTS_CMD_TIMEOUT; |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 129 | |
| 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 Dawson | 33c8773 | 2017-12-13 10:24:41 -0700 | [diff] [blame] | 135 | if (wait_command(mbox)) |
| 136 | return -PSPSTS_CMD_TIMEOUT; |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 137 | |
| 138 | /* check delivery status */ |
Marshall Dawson | 33c8773 | 2017-12-13 10:24:41 -0700 | [diff] [blame] | 139 | if (rd_mbox_sts(mbox) & (STATUS_ERROR | STATUS_TERMINATED)) |
| 140 | return -PSPSTS_SEND_ERROR; |
| 141 | |
| 142 | return 0; |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
Marshall Dawson | 5646a64 | 2020-01-19 16:12:25 -0700 | [diff] [blame] | 146 | * 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 | */ |
| 149 | static 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 Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 161 | * 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 | */ |
| 164 | int psp_notify_dram(void) |
| 165 | { |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 166 | int cmd_status; |
Marshall Dawson | 66dd399 | 2017-12-13 10:51:13 -0700 | [diff] [blame] | 167 | struct mbox_default_buffer buffer = { |
| 168 | .header = { |
| 169 | .size = sizeof(buffer) |
| 170 | } |
| 171 | }; |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 172 | |
| 173 | printk(BIOS_DEBUG, "PSP: Notify that DRAM is available... "); |
| 174 | |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 175 | 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 Dawson | 5646a64 | 2020-01-19 16:12:25 -0700 | [diff] [blame] | 178 | print_cmd_status(cmd_status, &buffer); |
Marshall Dawson | 68243a5 | 2017-06-15 16:59:20 -0600 | [diff] [blame] | 179 | |
| 180 | return cmd_status; |
| 181 | } |
Marshall Dawson | 596ecec | 2017-10-12 16:04:08 -0600 | [diff] [blame] | 182 | |
| 183 | /* |
Marshall Dawson | d1cc3c2 | 2017-12-08 12:46:11 -0700 | [diff] [blame] | 184 | * 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 | */ |
| 188 | static 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 Dawson | 5646a64 | 2020-01-19 16:12:25 -0700 | [diff] [blame] | 202 | print_cmd_status(cmd_status, &buffer); |
Marshall Dawson | d1cc3c2 | 2017-12-08 12:46:11 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
Marshall Dawson | 596ecec | 2017-10-12 16:04:08 -0600 | [diff] [blame] | 206 | * Tell the PSP to load a firmware blob from a location in the BIOS image. |
| 207 | */ |
Marshall Dawson | 737e56a | 2020-01-19 16:32:08 -0700 | [diff] [blame] | 208 | int psp_load_named_blob(enum psp_blob_type type, const char *name) |
Marshall Dawson | 596ecec | 2017-10-12 16:04:08 -0600 | [diff] [blame] | 209 | { |
| 210 | int cmd_status; |
Marshall Dawson | 737e56a | 2020-01-19 16:32:08 -0700 | [diff] [blame] | 211 | u32 command; |
Marshall Dawson | 596ecec | 2017-10-12 16:04:08 -0600 | [diff] [blame] | 212 | void *blob; |
| 213 | struct cbfsf cbfs_file; |
| 214 | struct region_device rdev; |
Marshall Dawson | 737e56a | 2020-01-19 16:32:08 -0700 | [diff] [blame] | 215 | |
| 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 Dawson | 596ecec | 2017-10-12 16:04:08 -0600 | [diff] [blame] | 233 | |
| 234 | if (cbfs_boot_locate(&cbfs_file, name, NULL)) { |
| 235 | printk(BIOS_ERR, "BUG: Cannot locate blob for PSP loading\n"); |
Marshall Dawson | 737e56a | 2020-01-19 16:32:08 -0700 | [diff] [blame] | 236 | return -PSPSTS_INVALID_NAME; |
Marshall Dawson | 596ecec | 2017-10-12 16:04:08 -0600 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | cbfs_file_data(&rdev, &cbfs_file); |
| 240 | blob = rdev_mmap_full(&rdev); |
Marshall Dawson | 737e56a | 2020-01-19 16:32:08 -0700 | [diff] [blame] | 241 | if (!blob) { |
Marshall Dawson | 596ecec | 2017-10-12 16:04:08 -0600 | [diff] [blame] | 242 | printk(BIOS_ERR, "BUG: Cannot map blob for PSP loading\n"); |
Marshall Dawson | 737e56a | 2020-01-19 16:32:08 -0700 | [diff] [blame] | 243 | return -PSPSTS_INVALID_NAME; |
Marshall Dawson | 596ecec | 2017-10-12 16:04:08 -0600 | [diff] [blame] | 244 | } |
Marshall Dawson | 737e56a | 2020-01-19 16:32:08 -0700 | [diff] [blame] | 245 | |
| 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 Dawson | 596ecec | 2017-10-12 16:04:08 -0600 | [diff] [blame] | 254 | } |
Marshall Dawson | d1cc3c2 | 2017-12-08 12:46:11 -0700 | [diff] [blame] | 255 | |
| 256 | BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, |
| 257 | psp_notify_boot_done, NULL); |