Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 2 | /* |
| 3 | * AMD Platform Security Processor (PSP) interface |
| 4 | * |
Hook, Gary | fa5cd1c | 2018-12-18 15:48:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 2016,2018 Advanced Micro Devices, Inc. |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 6 | * |
| 7 | * Author: Brijesh Singh <brijesh.singh@amd.com> |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/kthread.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/spinlock_types.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/hw_random.h> |
| 21 | #include <linux/ccp.h> |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 22 | #include <linux/firmware.h> |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 23 | |
Tom Lendacky | 0fc5dea | 2019-10-03 21:17:46 +0000 | [diff] [blame] | 24 | #include <asm/smp.h> |
| 25 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 26 | #include "sp-dev.h" |
| 27 | #include "psp-dev.h" |
| 28 | |
Janakarajan Natarajan | e937206 | 2018-09-14 17:32:04 -0500 | [diff] [blame] | 29 | #define DEVICE_NAME "sev" |
| 30 | #define SEV_FW_FILE "amd/sev.fw" |
| 31 | #define SEV_FW_NAME_SIZE 64 |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 32 | |
| 33 | static DEFINE_MUTEX(sev_cmd_mutex); |
| 34 | static struct sev_misc_dev *misc_dev; |
| 35 | static struct psp_device *psp_master; |
| 36 | |
Brijesh Singh | e82867f | 2018-08-15 16:11:25 -0500 | [diff] [blame] | 37 | static int psp_cmd_timeout = 100; |
| 38 | module_param(psp_cmd_timeout, int, 0644); |
| 39 | MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands"); |
| 40 | |
| 41 | static int psp_probe_timeout = 5; |
| 42 | module_param(psp_probe_timeout, int, 0644); |
| 43 | MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe"); |
| 44 | |
| 45 | static bool psp_dead; |
| 46 | static int psp_timeout; |
| 47 | |
David Rientjes | 83bf425 | 2019-07-12 13:41:58 -0700 | [diff] [blame] | 48 | static inline bool sev_version_greater_or_equal(u8 maj, u8 min) |
| 49 | { |
| 50 | if (psp_master->api_major > maj) |
| 51 | return true; |
| 52 | if (psp_master->api_major == maj && psp_master->api_minor >= min) |
| 53 | return true; |
| 54 | return false; |
| 55 | } |
| 56 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 57 | static struct psp_device *psp_alloc_struct(struct sp_device *sp) |
| 58 | { |
| 59 | struct device *dev = sp->dev; |
| 60 | struct psp_device *psp; |
| 61 | |
| 62 | psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL); |
| 63 | if (!psp) |
| 64 | return NULL; |
| 65 | |
| 66 | psp->dev = dev; |
| 67 | psp->sp = sp; |
| 68 | |
| 69 | snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord); |
| 70 | |
| 71 | return psp; |
| 72 | } |
| 73 | |
| 74 | static irqreturn_t psp_irq_handler(int irq, void *data) |
| 75 | { |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 76 | struct psp_device *psp = data; |
| 77 | unsigned int status; |
| 78 | int reg; |
| 79 | |
| 80 | /* Read the interrupt status: */ |
Tom Lendacky | ad01a98 | 2018-07-03 12:12:03 -0500 | [diff] [blame] | 81 | status = ioread32(psp->io_regs + psp->vdata->intsts_reg); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 82 | |
| 83 | /* Check if it is command completion: */ |
Tom Lendacky | 03af912 | 2018-07-03 12:11:52 -0500 | [diff] [blame] | 84 | if (!(status & PSP_CMD_COMPLETE)) |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 85 | goto done; |
| 86 | |
| 87 | /* Check if it is SEV command completion: */ |
Tom Lendacky | ad01a98 | 2018-07-03 12:12:03 -0500 | [diff] [blame] | 88 | reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 89 | if (reg & PSP_CMDRESP_RESP) { |
| 90 | psp->sev_int_rcvd = 1; |
| 91 | wake_up(&psp->sev_int_queue); |
| 92 | } |
| 93 | |
| 94 | done: |
| 95 | /* Clear the interrupt status by writing the same value we read. */ |
Tom Lendacky | ad01a98 | 2018-07-03 12:12:03 -0500 | [diff] [blame] | 96 | iowrite32(status, psp->io_regs + psp->vdata->intsts_reg); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 97 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 98 | return IRQ_HANDLED; |
| 99 | } |
| 100 | |
Brijesh Singh | e82867f | 2018-08-15 16:11:25 -0500 | [diff] [blame] | 101 | static int sev_wait_cmd_ioc(struct psp_device *psp, |
| 102 | unsigned int *reg, unsigned int timeout) |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 103 | { |
Brijesh Singh | e82867f | 2018-08-15 16:11:25 -0500 | [diff] [blame] | 104 | int ret; |
| 105 | |
| 106 | ret = wait_event_timeout(psp->sev_int_queue, |
| 107 | psp->sev_int_rcvd, timeout * HZ); |
| 108 | if (!ret) |
| 109 | return -ETIMEDOUT; |
| 110 | |
Tom Lendacky | ad01a98 | 2018-07-03 12:12:03 -0500 | [diff] [blame] | 111 | *reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg); |
Brijesh Singh | e82867f | 2018-08-15 16:11:25 -0500 | [diff] [blame] | 112 | |
| 113 | return 0; |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static int sev_cmd_buffer_len(int cmd) |
| 117 | { |
| 118 | switch (cmd) { |
| 119 | case SEV_CMD_INIT: return sizeof(struct sev_data_init); |
| 120 | case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status); |
| 121 | case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr); |
| 122 | case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import); |
| 123 | case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export); |
| 124 | case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start); |
| 125 | case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data); |
| 126 | case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa); |
| 127 | case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish); |
| 128 | case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure); |
| 129 | case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate); |
| 130 | case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate); |
| 131 | case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission); |
| 132 | case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status); |
| 133 | case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg); |
| 134 | case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg); |
| 135 | case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start); |
| 136 | case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data); |
| 137 | case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa); |
| 138 | case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish); |
| 139 | case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start); |
| 140 | case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish); |
| 141 | case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data); |
| 142 | case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa); |
| 143 | case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret); |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 144 | case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware); |
Janakarajan Natarajan | 0b3a830 | 2018-05-25 15:23:30 -0500 | [diff] [blame] | 145 | case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 146 | default: return 0; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) |
| 153 | { |
| 154 | struct psp_device *psp = psp_master; |
| 155 | unsigned int phys_lsb, phys_msb; |
| 156 | unsigned int reg, ret = 0; |
| 157 | |
| 158 | if (!psp) |
| 159 | return -ENODEV; |
| 160 | |
Brijesh Singh | e82867f | 2018-08-15 16:11:25 -0500 | [diff] [blame] | 161 | if (psp_dead) |
| 162 | return -EBUSY; |
| 163 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 164 | /* Get the physical address of the command buffer */ |
| 165 | phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0; |
| 166 | phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0; |
| 167 | |
Brijesh Singh | e82867f | 2018-08-15 16:11:25 -0500 | [diff] [blame] | 168 | dev_dbg(psp->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", |
| 169 | cmd, phys_msb, phys_lsb, psp_timeout); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 170 | |
| 171 | print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data, |
| 172 | sev_cmd_buffer_len(cmd), false); |
| 173 | |
Tom Lendacky | ad01a98 | 2018-07-03 12:12:03 -0500 | [diff] [blame] | 174 | iowrite32(phys_lsb, psp->io_regs + psp->vdata->cmdbuff_addr_lo_reg); |
| 175 | iowrite32(phys_msb, psp->io_regs + psp->vdata->cmdbuff_addr_hi_reg); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 176 | |
Tom Lendacky | f426d2b | 2018-07-03 12:11:33 -0500 | [diff] [blame] | 177 | psp->sev_int_rcvd = 0; |
| 178 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 179 | reg = cmd; |
| 180 | reg <<= PSP_CMDRESP_CMD_SHIFT; |
| 181 | reg |= PSP_CMDRESP_IOC; |
Tom Lendacky | ad01a98 | 2018-07-03 12:12:03 -0500 | [diff] [blame] | 182 | iowrite32(reg, psp->io_regs + psp->vdata->cmdresp_reg); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 183 | |
| 184 | /* wait for command completion */ |
Brijesh Singh | e82867f | 2018-08-15 16:11:25 -0500 | [diff] [blame] | 185 | ret = sev_wait_cmd_ioc(psp, ®, psp_timeout); |
| 186 | if (ret) { |
| 187 | if (psp_ret) |
| 188 | *psp_ret = 0; |
| 189 | |
| 190 | dev_err(psp->dev, "sev command %#x timed out, disabling PSP \n", cmd); |
| 191 | psp_dead = true; |
| 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | psp_timeout = psp_cmd_timeout; |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 197 | |
| 198 | if (psp_ret) |
| 199 | *psp_ret = reg & PSP_CMDRESP_ERR_MASK; |
| 200 | |
| 201 | if (reg & PSP_CMDRESP_ERR_MASK) { |
| 202 | dev_dbg(psp->dev, "sev command %#x failed (%#010x)\n", |
| 203 | cmd, reg & PSP_CMDRESP_ERR_MASK); |
| 204 | ret = -EIO; |
| 205 | } |
| 206 | |
| 207 | print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, |
| 208 | sev_cmd_buffer_len(cmd), false); |
| 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static int sev_do_cmd(int cmd, void *data, int *psp_ret) |
| 214 | { |
| 215 | int rc; |
| 216 | |
| 217 | mutex_lock(&sev_cmd_mutex); |
| 218 | rc = __sev_do_cmd_locked(cmd, data, psp_ret); |
| 219 | mutex_unlock(&sev_cmd_mutex); |
| 220 | |
| 221 | return rc; |
| 222 | } |
| 223 | |
| 224 | static int __sev_platform_init_locked(int *error) |
| 225 | { |
| 226 | struct psp_device *psp = psp_master; |
| 227 | int rc = 0; |
| 228 | |
| 229 | if (!psp) |
| 230 | return -ENODEV; |
| 231 | |
| 232 | if (psp->sev_state == SEV_STATE_INIT) |
| 233 | return 0; |
| 234 | |
| 235 | rc = __sev_do_cmd_locked(SEV_CMD_INIT, &psp->init_cmd_buf, error); |
| 236 | if (rc) |
| 237 | return rc; |
| 238 | |
| 239 | psp->sev_state = SEV_STATE_INIT; |
Tom Lendacky | 0fc5dea | 2019-10-03 21:17:46 +0000 | [diff] [blame] | 240 | |
| 241 | /* Prepare for first SEV guest launch after INIT */ |
| 242 | wbinvd_on_all_cpus(); |
| 243 | rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error); |
| 244 | if (rc) |
| 245 | return rc; |
| 246 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 247 | dev_dbg(psp->dev, "SEV firmware initialized\n"); |
| 248 | |
| 249 | return rc; |
| 250 | } |
| 251 | |
| 252 | int sev_platform_init(int *error) |
| 253 | { |
| 254 | int rc; |
| 255 | |
| 256 | mutex_lock(&sev_cmd_mutex); |
| 257 | rc = __sev_platform_init_locked(error); |
| 258 | mutex_unlock(&sev_cmd_mutex); |
| 259 | |
| 260 | return rc; |
| 261 | } |
| 262 | EXPORT_SYMBOL_GPL(sev_platform_init); |
| 263 | |
| 264 | static int __sev_platform_shutdown_locked(int *error) |
| 265 | { |
| 266 | int ret; |
| 267 | |
Brijesh Singh | e385b5b | 2018-02-15 13:34:44 -0600 | [diff] [blame] | 268 | ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 269 | if (ret) |
| 270 | return ret; |
| 271 | |
| 272 | psp_master->sev_state = SEV_STATE_UNINIT; |
| 273 | dev_dbg(psp_master->dev, "SEV firmware shutdown\n"); |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static int sev_platform_shutdown(int *error) |
| 279 | { |
| 280 | int rc; |
| 281 | |
| 282 | mutex_lock(&sev_cmd_mutex); |
| 283 | rc = __sev_platform_shutdown_locked(NULL); |
| 284 | mutex_unlock(&sev_cmd_mutex); |
| 285 | |
| 286 | return rc; |
| 287 | } |
| 288 | |
Brijesh Singh | 2960f9a | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 289 | static int sev_get_platform_state(int *state, int *error) |
| 290 | { |
| 291 | int rc; |
| 292 | |
| 293 | rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, |
| 294 | &psp_master->status_cmd_buf, error); |
| 295 | if (rc) |
| 296 | return rc; |
| 297 | |
| 298 | *state = psp_master->status_cmd_buf.state; |
| 299 | return rc; |
| 300 | } |
| 301 | |
| 302 | static int sev_ioctl_do_reset(struct sev_issue_cmd *argp) |
| 303 | { |
| 304 | int state, rc; |
| 305 | |
Brijesh Singh | ec310ca | 2019-11-12 13:58:34 -0600 | [diff] [blame] | 306 | if (!capable(CAP_SYS_ADMIN)) |
| 307 | return -EPERM; |
| 308 | |
Brijesh Singh | 2960f9a | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 309 | /* |
| 310 | * The SEV spec requires that FACTORY_RESET must be issued in |
| 311 | * UNINIT state. Before we go further lets check if any guest is |
| 312 | * active. |
| 313 | * |
| 314 | * If FW is in WORKING state then deny the request otherwise issue |
| 315 | * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET. |
| 316 | * |
| 317 | */ |
| 318 | rc = sev_get_platform_state(&state, &argp->error); |
| 319 | if (rc) |
| 320 | return rc; |
| 321 | |
| 322 | if (state == SEV_STATE_WORKING) |
| 323 | return -EBUSY; |
| 324 | |
| 325 | if (state == SEV_STATE_INIT) { |
| 326 | rc = __sev_platform_shutdown_locked(&argp->error); |
| 327 | if (rc) |
| 328 | return rc; |
| 329 | } |
| 330 | |
Brijesh Singh | e385b5b | 2018-02-15 13:34:44 -0600 | [diff] [blame] | 331 | return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error); |
Brijesh Singh | 2960f9a | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 332 | } |
| 333 | |
Brijesh Singh | efe1829 | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 334 | static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) |
| 335 | { |
| 336 | struct sev_user_data_status *data = &psp_master->status_cmd_buf; |
| 337 | int ret; |
| 338 | |
| 339 | ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error); |
| 340 | if (ret) |
| 341 | return ret; |
| 342 | |
| 343 | if (copy_to_user((void __user *)argp->data, data, sizeof(*data))) |
| 344 | ret = -EFAULT; |
| 345 | |
| 346 | return ret; |
| 347 | } |
| 348 | |
Brijesh Singh | 4d84b72 | 2017-12-04 10:57:30 -0600 | [diff] [blame] | 349 | static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp) |
| 350 | { |
| 351 | int rc; |
| 352 | |
Brijesh Singh | ec310ca | 2019-11-12 13:58:34 -0600 | [diff] [blame] | 353 | if (!capable(CAP_SYS_ADMIN)) |
| 354 | return -EPERM; |
| 355 | |
Brijesh Singh | 4d84b72 | 2017-12-04 10:57:30 -0600 | [diff] [blame] | 356 | if (psp_master->sev_state == SEV_STATE_UNINIT) { |
| 357 | rc = __sev_platform_init_locked(&argp->error); |
| 358 | if (rc) |
| 359 | return rc; |
| 360 | } |
| 361 | |
Brijesh Singh | e385b5b | 2018-02-15 13:34:44 -0600 | [diff] [blame] | 362 | return __sev_do_cmd_locked(cmd, NULL, &argp->error); |
Brijesh Singh | 4d84b72 | 2017-12-04 10:57:30 -0600 | [diff] [blame] | 363 | } |
| 364 | |
Brijesh Singh | e799035 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 365 | static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp) |
| 366 | { |
| 367 | struct sev_user_data_pek_csr input; |
| 368 | struct sev_data_pek_csr *data; |
| 369 | void *blob = NULL; |
| 370 | int ret; |
| 371 | |
Brijesh Singh | ec310ca | 2019-11-12 13:58:34 -0600 | [diff] [blame] | 372 | if (!capable(CAP_SYS_ADMIN)) |
| 373 | return -EPERM; |
| 374 | |
Brijesh Singh | e799035 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 375 | if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) |
| 376 | return -EFAULT; |
| 377 | |
| 378 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 379 | if (!data) |
| 380 | return -ENOMEM; |
| 381 | |
| 382 | /* userspace wants to query CSR length */ |
| 383 | if (!input.address || !input.length) |
| 384 | goto cmd; |
| 385 | |
| 386 | /* allocate a physically contiguous buffer to store the CSR blob */ |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 387 | if (!access_ok(input.address, input.length) || |
Brijesh Singh | e799035 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 388 | input.length > SEV_FW_BLOB_MAX_SIZE) { |
| 389 | ret = -EFAULT; |
| 390 | goto e_free; |
| 391 | } |
| 392 | |
| 393 | blob = kmalloc(input.length, GFP_KERNEL); |
| 394 | if (!blob) { |
| 395 | ret = -ENOMEM; |
| 396 | goto e_free; |
| 397 | } |
| 398 | |
| 399 | data->address = __psp_pa(blob); |
| 400 | data->len = input.length; |
| 401 | |
| 402 | cmd: |
| 403 | if (psp_master->sev_state == SEV_STATE_UNINIT) { |
| 404 | ret = __sev_platform_init_locked(&argp->error); |
| 405 | if (ret) |
| 406 | goto e_free_blob; |
| 407 | } |
| 408 | |
| 409 | ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error); |
| 410 | |
| 411 | /* If we query the CSR length, FW responded with expected data. */ |
| 412 | input.length = data->len; |
| 413 | |
| 414 | if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { |
| 415 | ret = -EFAULT; |
| 416 | goto e_free_blob; |
| 417 | } |
| 418 | |
| 419 | if (blob) { |
| 420 | if (copy_to_user((void __user *)input.address, blob, input.length)) |
| 421 | ret = -EFAULT; |
| 422 | } |
| 423 | |
| 424 | e_free_blob: |
| 425 | kfree(blob); |
| 426 | e_free: |
| 427 | kfree(data); |
| 428 | return ret; |
| 429 | } |
| 430 | |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 431 | void *psp_copy_user_blob(u64 __user uaddr, u32 len) |
| 432 | { |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 433 | if (!uaddr || !len) |
| 434 | return ERR_PTR(-EINVAL); |
| 435 | |
| 436 | /* verify that blob length does not exceed our limit */ |
| 437 | if (len > SEV_FW_BLOB_MAX_SIZE) |
| 438 | return ERR_PTR(-EINVAL); |
| 439 | |
Markus Elfring | 6c51ddd | 2018-03-05 13:50:13 +0100 | [diff] [blame] | 440 | return memdup_user((void __user *)(uintptr_t)uaddr, len); |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 441 | } |
| 442 | EXPORT_SYMBOL_GPL(psp_copy_user_blob); |
| 443 | |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 444 | static int sev_get_api_version(void) |
| 445 | { |
| 446 | struct sev_user_data_status *status; |
Janakarajan Natarajan | b78d379 | 2018-09-14 17:32:03 -0500 | [diff] [blame] | 447 | int error = 0, ret; |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 448 | |
| 449 | status = &psp_master->status_cmd_buf; |
| 450 | ret = sev_platform_status(status, &error); |
| 451 | if (ret) { |
| 452 | dev_err(psp_master->dev, |
| 453 | "SEV: failed to get status. Error: %#x\n", error); |
| 454 | return 1; |
| 455 | } |
| 456 | |
| 457 | psp_master->api_major = status->api_major; |
| 458 | psp_master->api_minor = status->api_minor; |
| 459 | psp_master->build = status->build; |
Singh, Brijesh | f8903b3 | 2019-01-30 20:57:52 +0000 | [diff] [blame] | 460 | psp_master->sev_state = status->state; |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
Wei Yongjun | 5182f26 | 2018-09-26 02:09:23 +0000 | [diff] [blame] | 465 | static int sev_get_firmware(struct device *dev, |
| 466 | const struct firmware **firmware) |
Janakarajan Natarajan | e937206 | 2018-09-14 17:32:04 -0500 | [diff] [blame] | 467 | { |
| 468 | char fw_name_specific[SEV_FW_NAME_SIZE]; |
| 469 | char fw_name_subset[SEV_FW_NAME_SIZE]; |
| 470 | |
| 471 | snprintf(fw_name_specific, sizeof(fw_name_specific), |
| 472 | "amd/amd_sev_fam%.2xh_model%.2xh.sbin", |
| 473 | boot_cpu_data.x86, boot_cpu_data.x86_model); |
| 474 | |
| 475 | snprintf(fw_name_subset, sizeof(fw_name_subset), |
| 476 | "amd/amd_sev_fam%.2xh_model%.1xxh.sbin", |
| 477 | boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4); |
| 478 | |
| 479 | /* Check for SEV FW for a particular model. |
| 480 | * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h |
| 481 | * |
| 482 | * or |
| 483 | * |
| 484 | * Check for SEV FW common to a subset of models. |
| 485 | * Ex. amd_sev_fam17h_model0xh.sbin for |
| 486 | * Family 17h Model 00h -- Family 17h Model 0Fh |
| 487 | * |
| 488 | * or |
| 489 | * |
| 490 | * Fall-back to using generic name: sev.fw |
| 491 | */ |
| 492 | if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) || |
| 493 | (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) || |
| 494 | (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0)) |
| 495 | return 0; |
| 496 | |
| 497 | return -ENOENT; |
| 498 | } |
| 499 | |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 500 | /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */ |
| 501 | static int sev_update_firmware(struct device *dev) |
| 502 | { |
| 503 | struct sev_data_download_firmware *data; |
| 504 | const struct firmware *firmware; |
| 505 | int ret, error, order; |
| 506 | struct page *p; |
| 507 | u64 data_size; |
| 508 | |
Janakarajan Natarajan | e937206 | 2018-09-14 17:32:04 -0500 | [diff] [blame] | 509 | if (sev_get_firmware(dev, &firmware) == -ENOENT) { |
| 510 | dev_dbg(dev, "No SEV firmware file present\n"); |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 511 | return -1; |
Janakarajan Natarajan | e937206 | 2018-09-14 17:32:04 -0500 | [diff] [blame] | 512 | } |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * SEV FW expects the physical address given to it to be 32 |
| 516 | * byte aligned. Memory allocated has structure placed at the |
| 517 | * beginning followed by the firmware being passed to the SEV |
| 518 | * FW. Allocate enough memory for data structure + alignment |
| 519 | * padding + SEV FW. |
| 520 | */ |
| 521 | data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32); |
| 522 | |
| 523 | order = get_order(firmware->size + data_size); |
| 524 | p = alloc_pages(GFP_KERNEL, order); |
| 525 | if (!p) { |
| 526 | ret = -1; |
| 527 | goto fw_err; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Copy firmware data to a kernel allocated contiguous |
| 532 | * memory region. |
| 533 | */ |
| 534 | data = page_address(p); |
| 535 | memcpy(page_address(p) + data_size, firmware->data, firmware->size); |
| 536 | |
| 537 | data->address = __psp_pa(page_address(p) + data_size); |
| 538 | data->len = firmware->size; |
| 539 | |
| 540 | ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error); |
| 541 | if (ret) |
| 542 | dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error); |
| 543 | else |
| 544 | dev_info(dev, "SEV firmware update successful\n"); |
| 545 | |
| 546 | __free_pages(p, order); |
| 547 | |
| 548 | fw_err: |
| 549 | release_firmware(firmware); |
| 550 | |
| 551 | return ret; |
| 552 | } |
| 553 | |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 554 | static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp) |
| 555 | { |
| 556 | struct sev_user_data_pek_cert_import input; |
| 557 | struct sev_data_pek_cert_import *data; |
| 558 | void *pek_blob, *oca_blob; |
| 559 | int ret; |
| 560 | |
Brijesh Singh | ec310ca | 2019-11-12 13:58:34 -0600 | [diff] [blame] | 561 | if (!capable(CAP_SYS_ADMIN)) |
| 562 | return -EPERM; |
| 563 | |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 564 | if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) |
| 565 | return -EFAULT; |
| 566 | |
| 567 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 568 | if (!data) |
| 569 | return -ENOMEM; |
| 570 | |
| 571 | /* copy PEK certificate blobs from userspace */ |
| 572 | pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len); |
| 573 | if (IS_ERR(pek_blob)) { |
| 574 | ret = PTR_ERR(pek_blob); |
| 575 | goto e_free; |
| 576 | } |
| 577 | |
| 578 | data->pek_cert_address = __psp_pa(pek_blob); |
| 579 | data->pek_cert_len = input.pek_cert_len; |
| 580 | |
| 581 | /* copy PEK certificate blobs from userspace */ |
| 582 | oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len); |
| 583 | if (IS_ERR(oca_blob)) { |
| 584 | ret = PTR_ERR(oca_blob); |
| 585 | goto e_free_pek; |
| 586 | } |
| 587 | |
| 588 | data->oca_cert_address = __psp_pa(oca_blob); |
| 589 | data->oca_cert_len = input.oca_cert_len; |
| 590 | |
| 591 | /* If platform is not in INIT state then transition it to INIT */ |
| 592 | if (psp_master->sev_state != SEV_STATE_INIT) { |
| 593 | ret = __sev_platform_init_locked(&argp->error); |
| 594 | if (ret) |
| 595 | goto e_free_oca; |
| 596 | } |
| 597 | |
| 598 | ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error); |
| 599 | |
| 600 | e_free_oca: |
| 601 | kfree(oca_blob); |
| 602 | e_free_pek: |
| 603 | kfree(pek_blob); |
| 604 | e_free: |
| 605 | kfree(data); |
| 606 | return ret; |
| 607 | } |
| 608 | |
Singh, Brijesh | d6112ea | 2019-03-28 21:58:52 +0000 | [diff] [blame] | 609 | static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) |
| 610 | { |
| 611 | struct sev_user_data_get_id2 input; |
| 612 | struct sev_data_get_id *data; |
| 613 | void *id_blob = NULL; |
| 614 | int ret; |
| 615 | |
| 616 | /* SEV GET_ID is available from SEV API v0.16 and up */ |
David Rientjes | 83bf425 | 2019-07-12 13:41:58 -0700 | [diff] [blame] | 617 | if (!sev_version_greater_or_equal(0, 16)) |
Singh, Brijesh | d6112ea | 2019-03-28 21:58:52 +0000 | [diff] [blame] | 618 | return -ENOTSUPP; |
| 619 | |
| 620 | if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) |
| 621 | return -EFAULT; |
| 622 | |
| 623 | /* Check if we have write access to the userspace buffer */ |
| 624 | if (input.address && |
| 625 | input.length && |
| 626 | !access_ok(input.address, input.length)) |
| 627 | return -EFAULT; |
| 628 | |
| 629 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 630 | if (!data) |
| 631 | return -ENOMEM; |
| 632 | |
| 633 | if (input.address && input.length) { |
| 634 | id_blob = kmalloc(input.length, GFP_KERNEL); |
| 635 | if (!id_blob) { |
| 636 | kfree(data); |
| 637 | return -ENOMEM; |
| 638 | } |
| 639 | |
| 640 | data->address = __psp_pa(id_blob); |
| 641 | data->len = input.length; |
| 642 | } |
| 643 | |
| 644 | ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); |
| 645 | |
| 646 | /* |
| 647 | * Firmware will return the length of the ID value (either the minimum |
| 648 | * required length or the actual length written), return it to the user. |
| 649 | */ |
| 650 | input.length = data->len; |
| 651 | |
| 652 | if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { |
| 653 | ret = -EFAULT; |
| 654 | goto e_free; |
| 655 | } |
| 656 | |
| 657 | if (id_blob) { |
| 658 | if (copy_to_user((void __user *)input.address, |
| 659 | id_blob, data->len)) { |
| 660 | ret = -EFAULT; |
| 661 | goto e_free; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | e_free: |
| 666 | kfree(id_blob); |
| 667 | kfree(data); |
| 668 | |
| 669 | return ret; |
| 670 | } |
| 671 | |
Janakarajan Natarajan | 0b3a830 | 2018-05-25 15:23:30 -0500 | [diff] [blame] | 672 | static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp) |
| 673 | { |
| 674 | struct sev_data_get_id *data; |
| 675 | u64 data_size, user_size; |
| 676 | void *id_blob, *mem; |
| 677 | int ret; |
| 678 | |
| 679 | /* SEV GET_ID available from SEV API v0.16 and up */ |
David Rientjes | 83bf425 | 2019-07-12 13:41:58 -0700 | [diff] [blame] | 680 | if (!sev_version_greater_or_equal(0, 16)) |
Janakarajan Natarajan | 0b3a830 | 2018-05-25 15:23:30 -0500 | [diff] [blame] | 681 | return -ENOTSUPP; |
| 682 | |
| 683 | /* SEV FW expects the buffer it fills with the ID to be |
| 684 | * 8-byte aligned. Memory allocated should be enough to |
| 685 | * hold data structure + alignment padding + memory |
| 686 | * where SEV FW writes the ID. |
| 687 | */ |
| 688 | data_size = ALIGN(sizeof(struct sev_data_get_id), 8); |
| 689 | user_size = sizeof(struct sev_user_data_get_id); |
| 690 | |
| 691 | mem = kzalloc(data_size + user_size, GFP_KERNEL); |
| 692 | if (!mem) |
| 693 | return -ENOMEM; |
| 694 | |
| 695 | data = mem; |
| 696 | id_blob = mem + data_size; |
| 697 | |
| 698 | data->address = __psp_pa(id_blob); |
| 699 | data->len = user_size; |
| 700 | |
| 701 | ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); |
| 702 | if (!ret) { |
| 703 | if (copy_to_user((void __user *)argp->data, id_blob, data->len)) |
| 704 | ret = -EFAULT; |
| 705 | } |
| 706 | |
| 707 | kfree(mem); |
| 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | |
Brijesh Singh | 76a2b52 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 712 | static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp) |
| 713 | { |
| 714 | struct sev_user_data_pdh_cert_export input; |
| 715 | void *pdh_blob = NULL, *cert_blob = NULL; |
| 716 | struct sev_data_pdh_cert_export *data; |
| 717 | int ret; |
| 718 | |
Brijesh Singh | ec310ca | 2019-11-12 13:58:34 -0600 | [diff] [blame] | 719 | /* If platform is not in INIT state then transition it to INIT. */ |
| 720 | if (psp_master->sev_state != SEV_STATE_INIT) { |
| 721 | if (!capable(CAP_SYS_ADMIN)) |
| 722 | return -EPERM; |
| 723 | |
| 724 | ret = __sev_platform_init_locked(&argp->error); |
| 725 | if (ret) |
| 726 | return ret; |
| 727 | } |
| 728 | |
Brijesh Singh | 76a2b52 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 729 | if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) |
| 730 | return -EFAULT; |
| 731 | |
| 732 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 733 | if (!data) |
| 734 | return -ENOMEM; |
| 735 | |
| 736 | /* Userspace wants to query the certificate length. */ |
| 737 | if (!input.pdh_cert_address || |
| 738 | !input.pdh_cert_len || |
| 739 | !input.cert_chain_address) |
| 740 | goto cmd; |
| 741 | |
| 742 | /* Allocate a physically contiguous buffer to store the PDH blob. */ |
| 743 | if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) || |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 744 | !access_ok(input.pdh_cert_address, input.pdh_cert_len)) { |
Brijesh Singh | 76a2b52 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 745 | ret = -EFAULT; |
| 746 | goto e_free; |
| 747 | } |
| 748 | |
| 749 | /* Allocate a physically contiguous buffer to store the cert chain blob. */ |
| 750 | if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) || |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 751 | !access_ok(input.cert_chain_address, input.cert_chain_len)) { |
Brijesh Singh | 76a2b52 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 752 | ret = -EFAULT; |
| 753 | goto e_free; |
| 754 | } |
| 755 | |
| 756 | pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL); |
| 757 | if (!pdh_blob) { |
| 758 | ret = -ENOMEM; |
| 759 | goto e_free; |
| 760 | } |
| 761 | |
| 762 | data->pdh_cert_address = __psp_pa(pdh_blob); |
| 763 | data->pdh_cert_len = input.pdh_cert_len; |
| 764 | |
| 765 | cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL); |
| 766 | if (!cert_blob) { |
| 767 | ret = -ENOMEM; |
| 768 | goto e_free_pdh; |
| 769 | } |
| 770 | |
| 771 | data->cert_chain_address = __psp_pa(cert_blob); |
| 772 | data->cert_chain_len = input.cert_chain_len; |
| 773 | |
| 774 | cmd: |
Brijesh Singh | 76a2b52 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 775 | ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error); |
| 776 | |
| 777 | /* If we query the length, FW responded with expected data. */ |
| 778 | input.cert_chain_len = data->cert_chain_len; |
| 779 | input.pdh_cert_len = data->pdh_cert_len; |
| 780 | |
| 781 | if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { |
| 782 | ret = -EFAULT; |
| 783 | goto e_free_cert; |
| 784 | } |
| 785 | |
| 786 | if (pdh_blob) { |
| 787 | if (copy_to_user((void __user *)input.pdh_cert_address, |
| 788 | pdh_blob, input.pdh_cert_len)) { |
| 789 | ret = -EFAULT; |
| 790 | goto e_free_cert; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | if (cert_blob) { |
| 795 | if (copy_to_user((void __user *)input.cert_chain_address, |
| 796 | cert_blob, input.cert_chain_len)) |
| 797 | ret = -EFAULT; |
| 798 | } |
| 799 | |
| 800 | e_free_cert: |
| 801 | kfree(cert_blob); |
| 802 | e_free_pdh: |
| 803 | kfree(pdh_blob); |
| 804 | e_free: |
| 805 | kfree(data); |
| 806 | return ret; |
| 807 | } |
| 808 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 809 | static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) |
| 810 | { |
Brijesh Singh | 2960f9a | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 811 | void __user *argp = (void __user *)arg; |
| 812 | struct sev_issue_cmd input; |
| 813 | int ret = -EFAULT; |
| 814 | |
| 815 | if (!psp_master) |
| 816 | return -ENODEV; |
| 817 | |
| 818 | if (ioctl != SEV_ISSUE_CMD) |
| 819 | return -EINVAL; |
| 820 | |
| 821 | if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd))) |
| 822 | return -EFAULT; |
| 823 | |
| 824 | if (input.cmd > SEV_MAX) |
| 825 | return -EINVAL; |
| 826 | |
| 827 | mutex_lock(&sev_cmd_mutex); |
| 828 | |
| 829 | switch (input.cmd) { |
| 830 | |
| 831 | case SEV_FACTORY_RESET: |
| 832 | ret = sev_ioctl_do_reset(&input); |
| 833 | break; |
Brijesh Singh | efe1829 | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 834 | case SEV_PLATFORM_STATUS: |
| 835 | ret = sev_ioctl_do_platform_status(&input); |
| 836 | break; |
Brijesh Singh | 4d84b72 | 2017-12-04 10:57:30 -0600 | [diff] [blame] | 837 | case SEV_PEK_GEN: |
| 838 | ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input); |
| 839 | break; |
Brijesh Singh | 77f6532 | 2017-12-04 10:57:30 -0600 | [diff] [blame] | 840 | case SEV_PDH_GEN: |
| 841 | ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input); |
| 842 | break; |
Brijesh Singh | e799035 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 843 | case SEV_PEK_CSR: |
| 844 | ret = sev_ioctl_do_pek_csr(&input); |
| 845 | break; |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 846 | case SEV_PEK_CERT_IMPORT: |
| 847 | ret = sev_ioctl_do_pek_import(&input); |
| 848 | break; |
Brijesh Singh | 76a2b52 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 849 | case SEV_PDH_CERT_EXPORT: |
| 850 | ret = sev_ioctl_do_pdh_export(&input); |
| 851 | break; |
Janakarajan Natarajan | 0b3a830 | 2018-05-25 15:23:30 -0500 | [diff] [blame] | 852 | case SEV_GET_ID: |
Singh, Brijesh | d6112ea | 2019-03-28 21:58:52 +0000 | [diff] [blame] | 853 | pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n"); |
Janakarajan Natarajan | 0b3a830 | 2018-05-25 15:23:30 -0500 | [diff] [blame] | 854 | ret = sev_ioctl_do_get_id(&input); |
| 855 | break; |
Singh, Brijesh | d6112ea | 2019-03-28 21:58:52 +0000 | [diff] [blame] | 856 | case SEV_GET_ID2: |
| 857 | ret = sev_ioctl_do_get_id2(&input); |
| 858 | break; |
Brijesh Singh | 2960f9a | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 859 | default: |
| 860 | ret = -EINVAL; |
| 861 | goto out; |
| 862 | } |
| 863 | |
| 864 | if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd))) |
| 865 | ret = -EFAULT; |
| 866 | out: |
| 867 | mutex_unlock(&sev_cmd_mutex); |
| 868 | |
| 869 | return ret; |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static const struct file_operations sev_fops = { |
| 873 | .owner = THIS_MODULE, |
| 874 | .unlocked_ioctl = sev_ioctl, |
| 875 | }; |
| 876 | |
| 877 | int sev_platform_status(struct sev_user_data_status *data, int *error) |
| 878 | { |
| 879 | return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error); |
| 880 | } |
| 881 | EXPORT_SYMBOL_GPL(sev_platform_status); |
| 882 | |
| 883 | int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) |
| 884 | { |
| 885 | return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error); |
| 886 | } |
| 887 | EXPORT_SYMBOL_GPL(sev_guest_deactivate); |
| 888 | |
| 889 | int sev_guest_activate(struct sev_data_activate *data, int *error) |
| 890 | { |
| 891 | return sev_do_cmd(SEV_CMD_ACTIVATE, data, error); |
| 892 | } |
| 893 | EXPORT_SYMBOL_GPL(sev_guest_activate); |
| 894 | |
| 895 | int sev_guest_decommission(struct sev_data_decommission *data, int *error) |
| 896 | { |
| 897 | return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error); |
| 898 | } |
| 899 | EXPORT_SYMBOL_GPL(sev_guest_decommission); |
| 900 | |
| 901 | int sev_guest_df_flush(int *error) |
| 902 | { |
Brijesh Singh | e385b5b | 2018-02-15 13:34:44 -0600 | [diff] [blame] | 903 | return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 904 | } |
| 905 | EXPORT_SYMBOL_GPL(sev_guest_df_flush); |
| 906 | |
| 907 | static void sev_exit(struct kref *ref) |
| 908 | { |
| 909 | struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount); |
| 910 | |
| 911 | misc_deregister(&misc_dev->misc); |
| 912 | } |
| 913 | |
| 914 | static int sev_misc_init(struct psp_device *psp) |
| 915 | { |
| 916 | struct device *dev = psp->dev; |
| 917 | int ret; |
| 918 | |
| 919 | /* |
| 920 | * SEV feature support can be detected on multiple devices but the SEV |
| 921 | * FW commands must be issued on the master. During probe, we do not |
| 922 | * know the master hence we create /dev/sev on the first device probe. |
| 923 | * sev_do_cmd() finds the right master device to which to issue the |
| 924 | * command to the firmware. |
| 925 | */ |
| 926 | if (!misc_dev) { |
| 927 | struct miscdevice *misc; |
| 928 | |
| 929 | misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL); |
| 930 | if (!misc_dev) |
| 931 | return -ENOMEM; |
| 932 | |
| 933 | misc = &misc_dev->misc; |
| 934 | misc->minor = MISC_DYNAMIC_MINOR; |
| 935 | misc->name = DEVICE_NAME; |
| 936 | misc->fops = &sev_fops; |
| 937 | |
| 938 | ret = misc_register(misc); |
| 939 | if (ret) |
| 940 | return ret; |
| 941 | |
| 942 | kref_init(&misc_dev->refcount); |
| 943 | } else { |
| 944 | kref_get(&misc_dev->refcount); |
| 945 | } |
| 946 | |
| 947 | init_waitqueue_head(&psp->sev_int_queue); |
| 948 | psp->sev_misc = misc_dev; |
| 949 | dev_dbg(dev, "registered SEV device\n"); |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
Lendacky, Thomas | 7df5218 | 2019-02-15 17:26:33 +0000 | [diff] [blame] | 954 | static int psp_check_sev_support(struct psp_device *psp) |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 955 | { |
Hook, Gary | 03f008c | 2019-10-21 13:44:44 +0000 | [diff] [blame] | 956 | unsigned int val = ioread32(psp->io_regs + psp->vdata->feature_reg); |
| 957 | |
| 958 | /* |
| 959 | * Check for a access to the registers. If this read returns |
| 960 | * 0xffffffff, it's likely that the system is running a broken |
| 961 | * BIOS which disallows access to the device. Stop here and |
| 962 | * fail the PSP initialization (but not the load, as the CCP |
| 963 | * could get properly initialized). |
| 964 | */ |
| 965 | if (val == 0xffffffff) { |
| 966 | dev_notice(psp->dev, "psp: unable to access the device: you might be running a broken BIOS.\n"); |
| 967 | return -ENODEV; |
| 968 | } |
| 969 | |
| 970 | if (!(val & 1)) { |
| 971 | /* Device does not support the SEV feature */ |
Lendacky, Thomas | 7df5218 | 2019-02-15 17:26:33 +0000 | [diff] [blame] | 972 | dev_dbg(psp->dev, "psp does not support SEV\n"); |
| 973 | return -ENODEV; |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 974 | } |
| 975 | |
Lendacky, Thomas | 7df5218 | 2019-02-15 17:26:33 +0000 | [diff] [blame] | 976 | return 0; |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 977 | } |
| 978 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 979 | int psp_dev_init(struct sp_device *sp) |
| 980 | { |
| 981 | struct device *dev = sp->dev; |
| 982 | struct psp_device *psp; |
| 983 | int ret; |
| 984 | |
| 985 | ret = -ENOMEM; |
| 986 | psp = psp_alloc_struct(sp); |
| 987 | if (!psp) |
| 988 | goto e_err; |
| 989 | |
| 990 | sp->psp_data = psp; |
| 991 | |
| 992 | psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata; |
| 993 | if (!psp->vdata) { |
| 994 | ret = -ENODEV; |
| 995 | dev_err(dev, "missing driver data\n"); |
| 996 | goto e_err; |
| 997 | } |
| 998 | |
Tom Lendacky | ad01a98 | 2018-07-03 12:12:03 -0500 | [diff] [blame] | 999 | psp->io_regs = sp->io_map; |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1000 | |
Lendacky, Thomas | 7df5218 | 2019-02-15 17:26:33 +0000 | [diff] [blame] | 1001 | ret = psp_check_sev_support(psp); |
| 1002 | if (ret) |
| 1003 | goto e_disable; |
| 1004 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1005 | /* Disable and clear interrupts until ready */ |
Tom Lendacky | ad01a98 | 2018-07-03 12:12:03 -0500 | [diff] [blame] | 1006 | iowrite32(0, psp->io_regs + psp->vdata->inten_reg); |
| 1007 | iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg); |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1008 | |
| 1009 | /* Request an irq */ |
| 1010 | ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp); |
| 1011 | if (ret) { |
| 1012 | dev_err(dev, "psp: unable to allocate an IRQ\n"); |
| 1013 | goto e_err; |
| 1014 | } |
| 1015 | |
Lendacky, Thomas | 7df5218 | 2019-02-15 17:26:33 +0000 | [diff] [blame] | 1016 | ret = sev_misc_init(psp); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1017 | if (ret) |
| 1018 | goto e_irq; |
| 1019 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1020 | if (sp->set_psp_master_device) |
| 1021 | sp->set_psp_master_device(sp); |
| 1022 | |
| 1023 | /* Enable interrupt */ |
Tom Lendacky | ad01a98 | 2018-07-03 12:12:03 -0500 | [diff] [blame] | 1024 | iowrite32(-1, psp->io_regs + psp->vdata->inten_reg); |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1025 | |
Tom Lendacky | 015c8c8 | 2018-07-03 12:11:42 -0500 | [diff] [blame] | 1026 | dev_notice(dev, "psp enabled\n"); |
| 1027 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1028 | return 0; |
| 1029 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1030 | e_irq: |
| 1031 | sp_free_psp_irq(psp->sp, psp); |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1032 | e_err: |
| 1033 | sp->psp_data = NULL; |
| 1034 | |
| 1035 | dev_notice(dev, "psp initialization failed\n"); |
| 1036 | |
| 1037 | return ret; |
Lendacky, Thomas | 7df5218 | 2019-02-15 17:26:33 +0000 | [diff] [blame] | 1038 | |
| 1039 | e_disable: |
| 1040 | sp->psp_data = NULL; |
| 1041 | |
| 1042 | return ret; |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | void psp_dev_destroy(struct sp_device *sp) |
| 1046 | { |
| 1047 | struct psp_device *psp = sp->psp_data; |
| 1048 | |
Tom Lendacky | afb31cd | 2018-07-26 09:37:59 -0500 | [diff] [blame] | 1049 | if (!psp) |
| 1050 | return; |
| 1051 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1052 | if (psp->sev_misc) |
| 1053 | kref_put(&misc_dev->refcount, sev_exit); |
| 1054 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1055 | sp_free_psp_irq(sp, psp); |
| 1056 | } |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1057 | |
| 1058 | int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, |
| 1059 | void *data, int *error) |
| 1060 | { |
| 1061 | if (!filep || filep->f_op != &sev_fops) |
| 1062 | return -EBADF; |
| 1063 | |
| 1064 | return sev_do_cmd(cmd, data, error); |
| 1065 | } |
| 1066 | EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); |
| 1067 | |
| 1068 | void psp_pci_init(void) |
| 1069 | { |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1070 | struct sp_device *sp; |
| 1071 | int error, rc; |
| 1072 | |
| 1073 | sp = sp_get_psp_master_device(); |
| 1074 | if (!sp) |
| 1075 | return; |
| 1076 | |
| 1077 | psp_master = sp->psp_data; |
| 1078 | |
Brijesh Singh | e82867f | 2018-08-15 16:11:25 -0500 | [diff] [blame] | 1079 | psp_timeout = psp_probe_timeout; |
| 1080 | |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 1081 | if (sev_get_api_version()) |
| 1082 | goto err; |
| 1083 | |
Singh, Brijesh | f8903b3 | 2019-01-30 20:57:52 +0000 | [diff] [blame] | 1084 | /* |
| 1085 | * If platform is not in UNINIT state then firmware upgrade and/or |
| 1086 | * platform INIT command will fail. These command require UNINIT state. |
| 1087 | * |
| 1088 | * In a normal boot we should never run into case where the firmware |
| 1089 | * is not in UNINIT state on boot. But in case of kexec boot, a reboot |
| 1090 | * may not go through a typical shutdown sequence and may leave the |
| 1091 | * firmware in INIT or WORKING state. |
| 1092 | */ |
| 1093 | |
| 1094 | if (psp_master->sev_state != SEV_STATE_UNINIT) { |
| 1095 | sev_platform_shutdown(NULL); |
| 1096 | psp_master->sev_state = SEV_STATE_UNINIT; |
| 1097 | } |
| 1098 | |
David Rientjes | 83bf425 | 2019-07-12 13:41:58 -0700 | [diff] [blame] | 1099 | if (sev_version_greater_or_equal(0, 15) && |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 1100 | sev_update_firmware(psp_master->dev) == 0) |
| 1101 | sev_get_api_version(); |
| 1102 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1103 | /* Initialize the platform */ |
| 1104 | rc = sev_platform_init(&error); |
Ashish Kalra | 1d55fdc | 2019-10-17 22:35:11 +0000 | [diff] [blame] | 1105 | if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) { |
| 1106 | /* |
| 1107 | * INIT command returned an integrity check failure |
| 1108 | * status code, meaning that firmware load and |
| 1109 | * validation of SEV related persistent data has |
| 1110 | * failed and persistent state has been erased. |
| 1111 | * Retrying INIT command here should succeed. |
| 1112 | */ |
| 1113 | dev_dbg(sp->dev, "SEV: retrying INIT command"); |
| 1114 | rc = sev_platform_init(&error); |
| 1115 | } |
| 1116 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1117 | if (rc) { |
| 1118 | dev_err(sp->dev, "SEV: failed to INIT error %#x\n", error); |
Singh, Brijesh | f5a2aeb8 | 2019-04-08 20:42:55 +0000 | [diff] [blame] | 1119 | return; |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1120 | } |
| 1121 | |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame] | 1122 | dev_info(sp->dev, "SEV API:%d.%d build:%d\n", psp_master->api_major, |
| 1123 | psp_master->api_minor, psp_master->build); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1124 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1125 | return; |
| 1126 | |
| 1127 | err: |
| 1128 | psp_master = NULL; |
| 1129 | } |
| 1130 | |
| 1131 | void psp_pci_exit(void) |
| 1132 | { |
| 1133 | if (!psp_master) |
| 1134 | return; |
| 1135 | |
| 1136 | sev_platform_shutdown(NULL); |
| 1137 | } |