Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 1 | /* |
| 2 | * AMD Platform Security Processor (PSP) interface |
| 3 | * |
| 4 | * Copyright (C) 2016-2017 Advanced Micro Devices, Inc. |
| 5 | * |
| 6 | * Author: Brijesh Singh <brijesh.singh@amd.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/kthread.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/spinlock_types.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/hw_random.h> |
| 24 | #include <linux/ccp.h> |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame^] | 25 | #include <linux/firmware.h> |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 26 | |
| 27 | #include "sp-dev.h" |
| 28 | #include "psp-dev.h" |
| 29 | |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame^] | 30 | #define SEV_VERSION_GREATER_OR_EQUAL(_maj, _min) \ |
| 31 | ((psp_master->api_major) >= _maj && \ |
| 32 | (psp_master->api_minor) >= _min) |
| 33 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 34 | #define DEVICE_NAME "sev" |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame^] | 35 | #define SEV_FW_FILE "amd/sev.fw" |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 36 | |
| 37 | static DEFINE_MUTEX(sev_cmd_mutex); |
| 38 | static struct sev_misc_dev *misc_dev; |
| 39 | static struct psp_device *psp_master; |
| 40 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 41 | static struct psp_device *psp_alloc_struct(struct sp_device *sp) |
| 42 | { |
| 43 | struct device *dev = sp->dev; |
| 44 | struct psp_device *psp; |
| 45 | |
| 46 | psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL); |
| 47 | if (!psp) |
| 48 | return NULL; |
| 49 | |
| 50 | psp->dev = dev; |
| 51 | psp->sp = sp; |
| 52 | |
| 53 | snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord); |
| 54 | |
| 55 | return psp; |
| 56 | } |
| 57 | |
| 58 | static irqreturn_t psp_irq_handler(int irq, void *data) |
| 59 | { |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 60 | struct psp_device *psp = data; |
| 61 | unsigned int status; |
| 62 | int reg; |
| 63 | |
| 64 | /* Read the interrupt status: */ |
| 65 | status = ioread32(psp->io_regs + PSP_P2CMSG_INTSTS); |
| 66 | |
| 67 | /* Check if it is command completion: */ |
| 68 | if (!(status & BIT(PSP_CMD_COMPLETE_REG))) |
| 69 | goto done; |
| 70 | |
| 71 | /* Check if it is SEV command completion: */ |
| 72 | reg = ioread32(psp->io_regs + PSP_CMDRESP); |
| 73 | if (reg & PSP_CMDRESP_RESP) { |
| 74 | psp->sev_int_rcvd = 1; |
| 75 | wake_up(&psp->sev_int_queue); |
| 76 | } |
| 77 | |
| 78 | done: |
| 79 | /* Clear the interrupt status by writing the same value we read. */ |
| 80 | iowrite32(status, psp->io_regs + PSP_P2CMSG_INTSTS); |
| 81 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 82 | return IRQ_HANDLED; |
| 83 | } |
| 84 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 85 | static void sev_wait_cmd_ioc(struct psp_device *psp, unsigned int *reg) |
| 86 | { |
| 87 | psp->sev_int_rcvd = 0; |
| 88 | |
| 89 | wait_event(psp->sev_int_queue, psp->sev_int_rcvd); |
| 90 | *reg = ioread32(psp->io_regs + PSP_CMDRESP); |
| 91 | } |
| 92 | |
| 93 | static int sev_cmd_buffer_len(int cmd) |
| 94 | { |
| 95 | switch (cmd) { |
| 96 | case SEV_CMD_INIT: return sizeof(struct sev_data_init); |
| 97 | case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status); |
| 98 | case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr); |
| 99 | case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import); |
| 100 | case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export); |
| 101 | case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start); |
| 102 | case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data); |
| 103 | case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa); |
| 104 | case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish); |
| 105 | case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure); |
| 106 | case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate); |
| 107 | case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate); |
| 108 | case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission); |
| 109 | case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status); |
| 110 | case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg); |
| 111 | case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg); |
| 112 | case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start); |
| 113 | case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data); |
| 114 | case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa); |
| 115 | case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish); |
| 116 | case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start); |
| 117 | case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish); |
| 118 | case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data); |
| 119 | case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa); |
| 120 | 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^] | 121 | case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 122 | default: return 0; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) |
| 129 | { |
| 130 | struct psp_device *psp = psp_master; |
| 131 | unsigned int phys_lsb, phys_msb; |
| 132 | unsigned int reg, ret = 0; |
| 133 | |
| 134 | if (!psp) |
| 135 | return -ENODEV; |
| 136 | |
| 137 | /* Get the physical address of the command buffer */ |
| 138 | phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0; |
| 139 | phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0; |
| 140 | |
| 141 | dev_dbg(psp->dev, "sev command id %#x buffer 0x%08x%08x\n", |
| 142 | cmd, phys_msb, phys_lsb); |
| 143 | |
| 144 | print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data, |
| 145 | sev_cmd_buffer_len(cmd), false); |
| 146 | |
| 147 | iowrite32(phys_lsb, psp->io_regs + PSP_CMDBUFF_ADDR_LO); |
| 148 | iowrite32(phys_msb, psp->io_regs + PSP_CMDBUFF_ADDR_HI); |
| 149 | |
| 150 | reg = cmd; |
| 151 | reg <<= PSP_CMDRESP_CMD_SHIFT; |
| 152 | reg |= PSP_CMDRESP_IOC; |
| 153 | iowrite32(reg, psp->io_regs + PSP_CMDRESP); |
| 154 | |
| 155 | /* wait for command completion */ |
| 156 | sev_wait_cmd_ioc(psp, ®); |
| 157 | |
| 158 | if (psp_ret) |
| 159 | *psp_ret = reg & PSP_CMDRESP_ERR_MASK; |
| 160 | |
| 161 | if (reg & PSP_CMDRESP_ERR_MASK) { |
| 162 | dev_dbg(psp->dev, "sev command %#x failed (%#010x)\n", |
| 163 | cmd, reg & PSP_CMDRESP_ERR_MASK); |
| 164 | ret = -EIO; |
| 165 | } |
| 166 | |
| 167 | print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, |
| 168 | sev_cmd_buffer_len(cmd), false); |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | static int sev_do_cmd(int cmd, void *data, int *psp_ret) |
| 174 | { |
| 175 | int rc; |
| 176 | |
| 177 | mutex_lock(&sev_cmd_mutex); |
| 178 | rc = __sev_do_cmd_locked(cmd, data, psp_ret); |
| 179 | mutex_unlock(&sev_cmd_mutex); |
| 180 | |
| 181 | return rc; |
| 182 | } |
| 183 | |
| 184 | static int __sev_platform_init_locked(int *error) |
| 185 | { |
| 186 | struct psp_device *psp = psp_master; |
| 187 | int rc = 0; |
| 188 | |
| 189 | if (!psp) |
| 190 | return -ENODEV; |
| 191 | |
| 192 | if (psp->sev_state == SEV_STATE_INIT) |
| 193 | return 0; |
| 194 | |
| 195 | rc = __sev_do_cmd_locked(SEV_CMD_INIT, &psp->init_cmd_buf, error); |
| 196 | if (rc) |
| 197 | return rc; |
| 198 | |
| 199 | psp->sev_state = SEV_STATE_INIT; |
| 200 | dev_dbg(psp->dev, "SEV firmware initialized\n"); |
| 201 | |
| 202 | return rc; |
| 203 | } |
| 204 | |
| 205 | int sev_platform_init(int *error) |
| 206 | { |
| 207 | int rc; |
| 208 | |
| 209 | mutex_lock(&sev_cmd_mutex); |
| 210 | rc = __sev_platform_init_locked(error); |
| 211 | mutex_unlock(&sev_cmd_mutex); |
| 212 | |
| 213 | return rc; |
| 214 | } |
| 215 | EXPORT_SYMBOL_GPL(sev_platform_init); |
| 216 | |
| 217 | static int __sev_platform_shutdown_locked(int *error) |
| 218 | { |
| 219 | int ret; |
| 220 | |
Brijesh Singh | e385b5b | 2018-02-15 13:34:44 -0600 | [diff] [blame] | 221 | ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 222 | if (ret) |
| 223 | return ret; |
| 224 | |
| 225 | psp_master->sev_state = SEV_STATE_UNINIT; |
| 226 | dev_dbg(psp_master->dev, "SEV firmware shutdown\n"); |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | static int sev_platform_shutdown(int *error) |
| 232 | { |
| 233 | int rc; |
| 234 | |
| 235 | mutex_lock(&sev_cmd_mutex); |
| 236 | rc = __sev_platform_shutdown_locked(NULL); |
| 237 | mutex_unlock(&sev_cmd_mutex); |
| 238 | |
| 239 | return rc; |
| 240 | } |
| 241 | |
Brijesh Singh | 2960f9a | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 242 | static int sev_get_platform_state(int *state, int *error) |
| 243 | { |
| 244 | int rc; |
| 245 | |
| 246 | rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, |
| 247 | &psp_master->status_cmd_buf, error); |
| 248 | if (rc) |
| 249 | return rc; |
| 250 | |
| 251 | *state = psp_master->status_cmd_buf.state; |
| 252 | return rc; |
| 253 | } |
| 254 | |
| 255 | static int sev_ioctl_do_reset(struct sev_issue_cmd *argp) |
| 256 | { |
| 257 | int state, rc; |
| 258 | |
| 259 | /* |
| 260 | * The SEV spec requires that FACTORY_RESET must be issued in |
| 261 | * UNINIT state. Before we go further lets check if any guest is |
| 262 | * active. |
| 263 | * |
| 264 | * If FW is in WORKING state then deny the request otherwise issue |
| 265 | * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET. |
| 266 | * |
| 267 | */ |
| 268 | rc = sev_get_platform_state(&state, &argp->error); |
| 269 | if (rc) |
| 270 | return rc; |
| 271 | |
| 272 | if (state == SEV_STATE_WORKING) |
| 273 | return -EBUSY; |
| 274 | |
| 275 | if (state == SEV_STATE_INIT) { |
| 276 | rc = __sev_platform_shutdown_locked(&argp->error); |
| 277 | if (rc) |
| 278 | return rc; |
| 279 | } |
| 280 | |
Brijesh Singh | e385b5b | 2018-02-15 13:34:44 -0600 | [diff] [blame] | 281 | return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error); |
Brijesh Singh | 2960f9a | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 282 | } |
| 283 | |
Brijesh Singh | efe1829 | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 284 | static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) |
| 285 | { |
| 286 | struct sev_user_data_status *data = &psp_master->status_cmd_buf; |
| 287 | int ret; |
| 288 | |
| 289 | ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error); |
| 290 | if (ret) |
| 291 | return ret; |
| 292 | |
| 293 | if (copy_to_user((void __user *)argp->data, data, sizeof(*data))) |
| 294 | ret = -EFAULT; |
| 295 | |
| 296 | return ret; |
| 297 | } |
| 298 | |
Brijesh Singh | 4d84b72 | 2017-12-04 10:57:30 -0600 | [diff] [blame] | 299 | static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp) |
| 300 | { |
| 301 | int rc; |
| 302 | |
| 303 | if (psp_master->sev_state == SEV_STATE_UNINIT) { |
| 304 | rc = __sev_platform_init_locked(&argp->error); |
| 305 | if (rc) |
| 306 | return rc; |
| 307 | } |
| 308 | |
Brijesh Singh | e385b5b | 2018-02-15 13:34:44 -0600 | [diff] [blame] | 309 | return __sev_do_cmd_locked(cmd, NULL, &argp->error); |
Brijesh Singh | 4d84b72 | 2017-12-04 10:57:30 -0600 | [diff] [blame] | 310 | } |
| 311 | |
Brijesh Singh | e799035 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 312 | static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp) |
| 313 | { |
| 314 | struct sev_user_data_pek_csr input; |
| 315 | struct sev_data_pek_csr *data; |
| 316 | void *blob = NULL; |
| 317 | int ret; |
| 318 | |
| 319 | if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) |
| 320 | return -EFAULT; |
| 321 | |
| 322 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 323 | if (!data) |
| 324 | return -ENOMEM; |
| 325 | |
| 326 | /* userspace wants to query CSR length */ |
| 327 | if (!input.address || !input.length) |
| 328 | goto cmd; |
| 329 | |
| 330 | /* allocate a physically contiguous buffer to store the CSR blob */ |
| 331 | if (!access_ok(VERIFY_WRITE, input.address, input.length) || |
| 332 | input.length > SEV_FW_BLOB_MAX_SIZE) { |
| 333 | ret = -EFAULT; |
| 334 | goto e_free; |
| 335 | } |
| 336 | |
| 337 | blob = kmalloc(input.length, GFP_KERNEL); |
| 338 | if (!blob) { |
| 339 | ret = -ENOMEM; |
| 340 | goto e_free; |
| 341 | } |
| 342 | |
| 343 | data->address = __psp_pa(blob); |
| 344 | data->len = input.length; |
| 345 | |
| 346 | cmd: |
| 347 | if (psp_master->sev_state == SEV_STATE_UNINIT) { |
| 348 | ret = __sev_platform_init_locked(&argp->error); |
| 349 | if (ret) |
| 350 | goto e_free_blob; |
| 351 | } |
| 352 | |
| 353 | ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error); |
| 354 | |
| 355 | /* If we query the CSR length, FW responded with expected data. */ |
| 356 | input.length = data->len; |
| 357 | |
| 358 | if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { |
| 359 | ret = -EFAULT; |
| 360 | goto e_free_blob; |
| 361 | } |
| 362 | |
| 363 | if (blob) { |
| 364 | if (copy_to_user((void __user *)input.address, blob, input.length)) |
| 365 | ret = -EFAULT; |
| 366 | } |
| 367 | |
| 368 | e_free_blob: |
| 369 | kfree(blob); |
| 370 | e_free: |
| 371 | kfree(data); |
| 372 | return ret; |
| 373 | } |
| 374 | |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 375 | void *psp_copy_user_blob(u64 __user uaddr, u32 len) |
| 376 | { |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 377 | if (!uaddr || !len) |
| 378 | return ERR_PTR(-EINVAL); |
| 379 | |
| 380 | /* verify that blob length does not exceed our limit */ |
| 381 | if (len > SEV_FW_BLOB_MAX_SIZE) |
| 382 | return ERR_PTR(-EINVAL); |
| 383 | |
Markus Elfring | 6c51ddd | 2018-03-05 13:50:13 +0100 | [diff] [blame] | 384 | return memdup_user((void __user *)(uintptr_t)uaddr, len); |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 385 | } |
| 386 | EXPORT_SYMBOL_GPL(psp_copy_user_blob); |
| 387 | |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame^] | 388 | static int sev_get_api_version(void) |
| 389 | { |
| 390 | struct sev_user_data_status *status; |
| 391 | int error, ret; |
| 392 | |
| 393 | status = &psp_master->status_cmd_buf; |
| 394 | ret = sev_platform_status(status, &error); |
| 395 | if (ret) { |
| 396 | dev_err(psp_master->dev, |
| 397 | "SEV: failed to get status. Error: %#x\n", error); |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | psp_master->api_major = status->api_major; |
| 402 | psp_master->api_minor = status->api_minor; |
| 403 | psp_master->build = status->build; |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */ |
| 409 | static int sev_update_firmware(struct device *dev) |
| 410 | { |
| 411 | struct sev_data_download_firmware *data; |
| 412 | const struct firmware *firmware; |
| 413 | int ret, error, order; |
| 414 | struct page *p; |
| 415 | u64 data_size; |
| 416 | |
| 417 | ret = request_firmware(&firmware, SEV_FW_FILE, dev); |
| 418 | if (ret < 0) |
| 419 | return -1; |
| 420 | |
| 421 | /* |
| 422 | * SEV FW expects the physical address given to it to be 32 |
| 423 | * byte aligned. Memory allocated has structure placed at the |
| 424 | * beginning followed by the firmware being passed to the SEV |
| 425 | * FW. Allocate enough memory for data structure + alignment |
| 426 | * padding + SEV FW. |
| 427 | */ |
| 428 | data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32); |
| 429 | |
| 430 | order = get_order(firmware->size + data_size); |
| 431 | p = alloc_pages(GFP_KERNEL, order); |
| 432 | if (!p) { |
| 433 | ret = -1; |
| 434 | goto fw_err; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Copy firmware data to a kernel allocated contiguous |
| 439 | * memory region. |
| 440 | */ |
| 441 | data = page_address(p); |
| 442 | memcpy(page_address(p) + data_size, firmware->data, firmware->size); |
| 443 | |
| 444 | data->address = __psp_pa(page_address(p) + data_size); |
| 445 | data->len = firmware->size; |
| 446 | |
| 447 | ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error); |
| 448 | if (ret) |
| 449 | dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error); |
| 450 | else |
| 451 | dev_info(dev, "SEV firmware update successful\n"); |
| 452 | |
| 453 | __free_pages(p, order); |
| 454 | |
| 455 | fw_err: |
| 456 | release_firmware(firmware); |
| 457 | |
| 458 | return ret; |
| 459 | } |
| 460 | |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 461 | static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp) |
| 462 | { |
| 463 | struct sev_user_data_pek_cert_import input; |
| 464 | struct sev_data_pek_cert_import *data; |
| 465 | void *pek_blob, *oca_blob; |
| 466 | int ret; |
| 467 | |
| 468 | if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) |
| 469 | return -EFAULT; |
| 470 | |
| 471 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 472 | if (!data) |
| 473 | return -ENOMEM; |
| 474 | |
| 475 | /* copy PEK certificate blobs from userspace */ |
| 476 | pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len); |
| 477 | if (IS_ERR(pek_blob)) { |
| 478 | ret = PTR_ERR(pek_blob); |
| 479 | goto e_free; |
| 480 | } |
| 481 | |
| 482 | data->pek_cert_address = __psp_pa(pek_blob); |
| 483 | data->pek_cert_len = input.pek_cert_len; |
| 484 | |
| 485 | /* copy PEK certificate blobs from userspace */ |
| 486 | oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len); |
| 487 | if (IS_ERR(oca_blob)) { |
| 488 | ret = PTR_ERR(oca_blob); |
| 489 | goto e_free_pek; |
| 490 | } |
| 491 | |
| 492 | data->oca_cert_address = __psp_pa(oca_blob); |
| 493 | data->oca_cert_len = input.oca_cert_len; |
| 494 | |
| 495 | /* If platform is not in INIT state then transition it to INIT */ |
| 496 | if (psp_master->sev_state != SEV_STATE_INIT) { |
| 497 | ret = __sev_platform_init_locked(&argp->error); |
| 498 | if (ret) |
| 499 | goto e_free_oca; |
| 500 | } |
| 501 | |
| 502 | ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error); |
| 503 | |
| 504 | e_free_oca: |
| 505 | kfree(oca_blob); |
| 506 | e_free_pek: |
| 507 | kfree(pek_blob); |
| 508 | e_free: |
| 509 | kfree(data); |
| 510 | return ret; |
| 511 | } |
| 512 | |
Brijesh Singh | 76a2b52 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 513 | static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp) |
| 514 | { |
| 515 | struct sev_user_data_pdh_cert_export input; |
| 516 | void *pdh_blob = NULL, *cert_blob = NULL; |
| 517 | struct sev_data_pdh_cert_export *data; |
| 518 | int ret; |
| 519 | |
| 520 | if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) |
| 521 | return -EFAULT; |
| 522 | |
| 523 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 524 | if (!data) |
| 525 | return -ENOMEM; |
| 526 | |
| 527 | /* Userspace wants to query the certificate length. */ |
| 528 | if (!input.pdh_cert_address || |
| 529 | !input.pdh_cert_len || |
| 530 | !input.cert_chain_address) |
| 531 | goto cmd; |
| 532 | |
| 533 | /* Allocate a physically contiguous buffer to store the PDH blob. */ |
| 534 | if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) || |
| 535 | !access_ok(VERIFY_WRITE, input.pdh_cert_address, input.pdh_cert_len)) { |
| 536 | ret = -EFAULT; |
| 537 | goto e_free; |
| 538 | } |
| 539 | |
| 540 | /* Allocate a physically contiguous buffer to store the cert chain blob. */ |
| 541 | if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) || |
| 542 | !access_ok(VERIFY_WRITE, input.cert_chain_address, input.cert_chain_len)) { |
| 543 | ret = -EFAULT; |
| 544 | goto e_free; |
| 545 | } |
| 546 | |
| 547 | pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL); |
| 548 | if (!pdh_blob) { |
| 549 | ret = -ENOMEM; |
| 550 | goto e_free; |
| 551 | } |
| 552 | |
| 553 | data->pdh_cert_address = __psp_pa(pdh_blob); |
| 554 | data->pdh_cert_len = input.pdh_cert_len; |
| 555 | |
| 556 | cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL); |
| 557 | if (!cert_blob) { |
| 558 | ret = -ENOMEM; |
| 559 | goto e_free_pdh; |
| 560 | } |
| 561 | |
| 562 | data->cert_chain_address = __psp_pa(cert_blob); |
| 563 | data->cert_chain_len = input.cert_chain_len; |
| 564 | |
| 565 | cmd: |
| 566 | /* If platform is not in INIT state then transition it to INIT. */ |
| 567 | if (psp_master->sev_state != SEV_STATE_INIT) { |
| 568 | ret = __sev_platform_init_locked(&argp->error); |
| 569 | if (ret) |
| 570 | goto e_free_cert; |
| 571 | } |
| 572 | |
| 573 | ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error); |
| 574 | |
| 575 | /* If we query the length, FW responded with expected data. */ |
| 576 | input.cert_chain_len = data->cert_chain_len; |
| 577 | input.pdh_cert_len = data->pdh_cert_len; |
| 578 | |
| 579 | if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { |
| 580 | ret = -EFAULT; |
| 581 | goto e_free_cert; |
| 582 | } |
| 583 | |
| 584 | if (pdh_blob) { |
| 585 | if (copy_to_user((void __user *)input.pdh_cert_address, |
| 586 | pdh_blob, input.pdh_cert_len)) { |
| 587 | ret = -EFAULT; |
| 588 | goto e_free_cert; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (cert_blob) { |
| 593 | if (copy_to_user((void __user *)input.cert_chain_address, |
| 594 | cert_blob, input.cert_chain_len)) |
| 595 | ret = -EFAULT; |
| 596 | } |
| 597 | |
| 598 | e_free_cert: |
| 599 | kfree(cert_blob); |
| 600 | e_free_pdh: |
| 601 | kfree(pdh_blob); |
| 602 | e_free: |
| 603 | kfree(data); |
| 604 | return ret; |
| 605 | } |
| 606 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 607 | static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) |
| 608 | { |
Brijesh Singh | 2960f9a | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 609 | void __user *argp = (void __user *)arg; |
| 610 | struct sev_issue_cmd input; |
| 611 | int ret = -EFAULT; |
| 612 | |
| 613 | if (!psp_master) |
| 614 | return -ENODEV; |
| 615 | |
| 616 | if (ioctl != SEV_ISSUE_CMD) |
| 617 | return -EINVAL; |
| 618 | |
| 619 | if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd))) |
| 620 | return -EFAULT; |
| 621 | |
| 622 | if (input.cmd > SEV_MAX) |
| 623 | return -EINVAL; |
| 624 | |
| 625 | mutex_lock(&sev_cmd_mutex); |
| 626 | |
| 627 | switch (input.cmd) { |
| 628 | |
| 629 | case SEV_FACTORY_RESET: |
| 630 | ret = sev_ioctl_do_reset(&input); |
| 631 | break; |
Brijesh Singh | efe1829 | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 632 | case SEV_PLATFORM_STATUS: |
| 633 | ret = sev_ioctl_do_platform_status(&input); |
| 634 | break; |
Brijesh Singh | 4d84b72 | 2017-12-04 10:57:30 -0600 | [diff] [blame] | 635 | case SEV_PEK_GEN: |
| 636 | ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input); |
| 637 | break; |
Brijesh Singh | 77f6532 | 2017-12-04 10:57:30 -0600 | [diff] [blame] | 638 | case SEV_PDH_GEN: |
| 639 | ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input); |
| 640 | break; |
Brijesh Singh | e799035 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 641 | case SEV_PEK_CSR: |
| 642 | ret = sev_ioctl_do_pek_csr(&input); |
| 643 | break; |
Brijesh Singh | 7360e4b | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 644 | case SEV_PEK_CERT_IMPORT: |
| 645 | ret = sev_ioctl_do_pek_import(&input); |
| 646 | break; |
Brijesh Singh | 76a2b52 | 2017-12-04 10:57:31 -0600 | [diff] [blame] | 647 | case SEV_PDH_CERT_EXPORT: |
| 648 | ret = sev_ioctl_do_pdh_export(&input); |
| 649 | break; |
Brijesh Singh | 2960f9a | 2017-12-04 10:57:29 -0600 | [diff] [blame] | 650 | default: |
| 651 | ret = -EINVAL; |
| 652 | goto out; |
| 653 | } |
| 654 | |
| 655 | if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd))) |
| 656 | ret = -EFAULT; |
| 657 | out: |
| 658 | mutex_unlock(&sev_cmd_mutex); |
| 659 | |
| 660 | return ret; |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | static const struct file_operations sev_fops = { |
| 664 | .owner = THIS_MODULE, |
| 665 | .unlocked_ioctl = sev_ioctl, |
| 666 | }; |
| 667 | |
| 668 | int sev_platform_status(struct sev_user_data_status *data, int *error) |
| 669 | { |
| 670 | return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error); |
| 671 | } |
| 672 | EXPORT_SYMBOL_GPL(sev_platform_status); |
| 673 | |
| 674 | int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) |
| 675 | { |
| 676 | return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error); |
| 677 | } |
| 678 | EXPORT_SYMBOL_GPL(sev_guest_deactivate); |
| 679 | |
| 680 | int sev_guest_activate(struct sev_data_activate *data, int *error) |
| 681 | { |
| 682 | return sev_do_cmd(SEV_CMD_ACTIVATE, data, error); |
| 683 | } |
| 684 | EXPORT_SYMBOL_GPL(sev_guest_activate); |
| 685 | |
| 686 | int sev_guest_decommission(struct sev_data_decommission *data, int *error) |
| 687 | { |
| 688 | return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error); |
| 689 | } |
| 690 | EXPORT_SYMBOL_GPL(sev_guest_decommission); |
| 691 | |
| 692 | int sev_guest_df_flush(int *error) |
| 693 | { |
Brijesh Singh | e385b5b | 2018-02-15 13:34:44 -0600 | [diff] [blame] | 694 | return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 695 | } |
| 696 | EXPORT_SYMBOL_GPL(sev_guest_df_flush); |
| 697 | |
| 698 | static void sev_exit(struct kref *ref) |
| 699 | { |
| 700 | struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount); |
| 701 | |
| 702 | misc_deregister(&misc_dev->misc); |
| 703 | } |
| 704 | |
| 705 | static int sev_misc_init(struct psp_device *psp) |
| 706 | { |
| 707 | struct device *dev = psp->dev; |
| 708 | int ret; |
| 709 | |
| 710 | /* |
| 711 | * SEV feature support can be detected on multiple devices but the SEV |
| 712 | * FW commands must be issued on the master. During probe, we do not |
| 713 | * know the master hence we create /dev/sev on the first device probe. |
| 714 | * sev_do_cmd() finds the right master device to which to issue the |
| 715 | * command to the firmware. |
| 716 | */ |
| 717 | if (!misc_dev) { |
| 718 | struct miscdevice *misc; |
| 719 | |
| 720 | misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL); |
| 721 | if (!misc_dev) |
| 722 | return -ENOMEM; |
| 723 | |
| 724 | misc = &misc_dev->misc; |
| 725 | misc->minor = MISC_DYNAMIC_MINOR; |
| 726 | misc->name = DEVICE_NAME; |
| 727 | misc->fops = &sev_fops; |
| 728 | |
| 729 | ret = misc_register(misc); |
| 730 | if (ret) |
| 731 | return ret; |
| 732 | |
| 733 | kref_init(&misc_dev->refcount); |
| 734 | } else { |
| 735 | kref_get(&misc_dev->refcount); |
| 736 | } |
| 737 | |
| 738 | init_waitqueue_head(&psp->sev_int_queue); |
| 739 | psp->sev_misc = misc_dev; |
| 740 | dev_dbg(dev, "registered SEV device\n"); |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static int sev_init(struct psp_device *psp) |
| 746 | { |
| 747 | /* Check if device supports SEV feature */ |
| 748 | if (!(ioread32(psp->io_regs + PSP_FEATURE_REG) & 1)) { |
| 749 | dev_dbg(psp->dev, "device does not support SEV\n"); |
| 750 | return 1; |
| 751 | } |
| 752 | |
| 753 | return sev_misc_init(psp); |
| 754 | } |
| 755 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 756 | int psp_dev_init(struct sp_device *sp) |
| 757 | { |
| 758 | struct device *dev = sp->dev; |
| 759 | struct psp_device *psp; |
| 760 | int ret; |
| 761 | |
| 762 | ret = -ENOMEM; |
| 763 | psp = psp_alloc_struct(sp); |
| 764 | if (!psp) |
| 765 | goto e_err; |
| 766 | |
| 767 | sp->psp_data = psp; |
| 768 | |
| 769 | psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata; |
| 770 | if (!psp->vdata) { |
| 771 | ret = -ENODEV; |
| 772 | dev_err(dev, "missing driver data\n"); |
| 773 | goto e_err; |
| 774 | } |
| 775 | |
| 776 | psp->io_regs = sp->io_map + psp->vdata->offset; |
| 777 | |
| 778 | /* Disable and clear interrupts until ready */ |
| 779 | iowrite32(0, psp->io_regs + PSP_P2CMSG_INTEN); |
| 780 | iowrite32(-1, psp->io_regs + PSP_P2CMSG_INTSTS); |
| 781 | |
| 782 | /* Request an irq */ |
| 783 | ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp); |
| 784 | if (ret) { |
| 785 | dev_err(dev, "psp: unable to allocate an IRQ\n"); |
| 786 | goto e_err; |
| 787 | } |
| 788 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 789 | ret = sev_init(psp); |
| 790 | if (ret) |
| 791 | goto e_irq; |
| 792 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 793 | if (sp->set_psp_master_device) |
| 794 | sp->set_psp_master_device(sp); |
| 795 | |
| 796 | /* Enable interrupt */ |
| 797 | iowrite32(-1, psp->io_regs + PSP_P2CMSG_INTEN); |
| 798 | |
| 799 | return 0; |
| 800 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 801 | e_irq: |
| 802 | sp_free_psp_irq(psp->sp, psp); |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 803 | e_err: |
| 804 | sp->psp_data = NULL; |
| 805 | |
| 806 | dev_notice(dev, "psp initialization failed\n"); |
| 807 | |
| 808 | return ret; |
| 809 | } |
| 810 | |
| 811 | void psp_dev_destroy(struct sp_device *sp) |
| 812 | { |
| 813 | struct psp_device *psp = sp->psp_data; |
| 814 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 815 | if (psp->sev_misc) |
| 816 | kref_put(&misc_dev->refcount, sev_exit); |
| 817 | |
Brijesh Singh | 2a6170d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 818 | sp_free_psp_irq(sp, psp); |
| 819 | } |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 820 | |
| 821 | int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, |
| 822 | void *data, int *error) |
| 823 | { |
| 824 | if (!filep || filep->f_op != &sev_fops) |
| 825 | return -EBADF; |
| 826 | |
| 827 | return sev_do_cmd(cmd, data, error); |
| 828 | } |
| 829 | EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); |
| 830 | |
| 831 | void psp_pci_init(void) |
| 832 | { |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 833 | struct sp_device *sp; |
| 834 | int error, rc; |
| 835 | |
| 836 | sp = sp_get_psp_master_device(); |
| 837 | if (!sp) |
| 838 | return; |
| 839 | |
| 840 | psp_master = sp->psp_data; |
| 841 | |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame^] | 842 | if (sev_get_api_version()) |
| 843 | goto err; |
| 844 | |
| 845 | if (SEV_VERSION_GREATER_OR_EQUAL(0, 15) && |
| 846 | sev_update_firmware(psp_master->dev) == 0) |
| 847 | sev_get_api_version(); |
| 848 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 849 | /* Initialize the platform */ |
| 850 | rc = sev_platform_init(&error); |
| 851 | if (rc) { |
| 852 | dev_err(sp->dev, "SEV: failed to INIT error %#x\n", error); |
| 853 | goto err; |
| 854 | } |
| 855 | |
Janakarajan Natarajan | edd303f | 2018-05-25 15:23:29 -0500 | [diff] [blame^] | 856 | dev_info(sp->dev, "SEV API:%d.%d build:%d\n", psp_master->api_major, |
| 857 | psp_master->api_minor, psp_master->build); |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 858 | |
Brijesh Singh | 200664d | 2017-12-04 10:57:28 -0600 | [diff] [blame] | 859 | return; |
| 860 | |
| 861 | err: |
| 862 | psp_master = NULL; |
| 863 | } |
| 864 | |
| 865 | void psp_pci_exit(void) |
| 866 | { |
| 867 | if (!psp_master) |
| 868 | return; |
| 869 | |
| 870 | sev_platform_shutdown(NULL); |
| 871 | } |