blob: 39fdd0641637f590cb6f328953ffbb1eaf810f17 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Brijesh Singh2a6170d2017-12-04 10:57:28 -06002/*
3 * AMD Platform Security Processor (PSP) interface
4 *
Hook, Garyfa5cd1c2018-12-18 15:48:29 +00005 * Copyright (C) 2016,2018 Advanced Micro Devices, Inc.
Brijesh Singh2a6170d2017-12-04 10:57:28 -06006 *
7 * Author: Brijesh Singh <brijesh.singh@amd.com>
Brijesh Singh2a6170d2017-12-04 10:57:28 -06008 */
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 Natarajanedd303f2018-05-25 15:23:29 -050022#include <linux/firmware.h>
Brijesh Singh2a6170d2017-12-04 10:57:28 -060023
Tom Lendacky0fc5dea2019-10-03 21:17:46 +000024#include <asm/smp.h>
25
Brijesh Singh2a6170d2017-12-04 10:57:28 -060026#include "sp-dev.h"
27#include "psp-dev.h"
28
Janakarajan Natarajane9372062018-09-14 17:32:04 -050029#define DEVICE_NAME "sev"
30#define SEV_FW_FILE "amd/sev.fw"
31#define SEV_FW_NAME_SIZE 64
Brijesh Singh200664d2017-12-04 10:57:28 -060032
33static DEFINE_MUTEX(sev_cmd_mutex);
34static struct sev_misc_dev *misc_dev;
35static struct psp_device *psp_master;
36
Brijesh Singhe82867f2018-08-15 16:11:25 -050037static int psp_cmd_timeout = 100;
38module_param(psp_cmd_timeout, int, 0644);
39MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
40
41static int psp_probe_timeout = 5;
42module_param(psp_probe_timeout, int, 0644);
43MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
44
45static bool psp_dead;
46static int psp_timeout;
47
David Rientjes83bf4252019-07-12 13:41:58 -070048static 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 Singh2a6170d2017-12-04 10:57:28 -060057static 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
74static irqreturn_t psp_irq_handler(int irq, void *data)
75{
Brijesh Singh200664d2017-12-04 10:57:28 -060076 struct psp_device *psp = data;
77 unsigned int status;
78 int reg;
79
80 /* Read the interrupt status: */
Tom Lendackyad01a982018-07-03 12:12:03 -050081 status = ioread32(psp->io_regs + psp->vdata->intsts_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -060082
83 /* Check if it is command completion: */
Tom Lendacky03af9122018-07-03 12:11:52 -050084 if (!(status & PSP_CMD_COMPLETE))
Brijesh Singh200664d2017-12-04 10:57:28 -060085 goto done;
86
87 /* Check if it is SEV command completion: */
Tom Lendackyad01a982018-07-03 12:12:03 -050088 reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -060089 if (reg & PSP_CMDRESP_RESP) {
90 psp->sev_int_rcvd = 1;
91 wake_up(&psp->sev_int_queue);
92 }
93
94done:
95 /* Clear the interrupt status by writing the same value we read. */
Tom Lendackyad01a982018-07-03 12:12:03 -050096 iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -060097
Brijesh Singh2a6170d2017-12-04 10:57:28 -060098 return IRQ_HANDLED;
99}
100
Brijesh Singhe82867f2018-08-15 16:11:25 -0500101static int sev_wait_cmd_ioc(struct psp_device *psp,
102 unsigned int *reg, unsigned int timeout)
Brijesh Singh200664d2017-12-04 10:57:28 -0600103{
Brijesh Singhe82867f2018-08-15 16:11:25 -0500104 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 Lendackyad01a982018-07-03 12:12:03 -0500111 *reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg);
Brijesh Singhe82867f2018-08-15 16:11:25 -0500112
113 return 0;
Brijesh Singh200664d2017-12-04 10:57:28 -0600114}
115
116static 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 Natarajanedd303f2018-05-25 15:23:29 -0500144 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500145 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
Brijesh Singh200664d2017-12-04 10:57:28 -0600146 default: return 0;
147 }
148
149 return 0;
150}
151
152static 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 Singhe82867f2018-08-15 16:11:25 -0500161 if (psp_dead)
162 return -EBUSY;
163
Brijesh Singh200664d2017-12-04 10:57:28 -0600164 /* 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 Singhe82867f2018-08-15 16:11:25 -0500168 dev_dbg(psp->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
169 cmd, phys_msb, phys_lsb, psp_timeout);
Brijesh Singh200664d2017-12-04 10:57:28 -0600170
171 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
172 sev_cmd_buffer_len(cmd), false);
173
Tom Lendackyad01a982018-07-03 12:12:03 -0500174 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 Singh200664d2017-12-04 10:57:28 -0600176
Tom Lendackyf426d2b2018-07-03 12:11:33 -0500177 psp->sev_int_rcvd = 0;
178
Brijesh Singh200664d2017-12-04 10:57:28 -0600179 reg = cmd;
180 reg <<= PSP_CMDRESP_CMD_SHIFT;
181 reg |= PSP_CMDRESP_IOC;
Tom Lendackyad01a982018-07-03 12:12:03 -0500182 iowrite32(reg, psp->io_regs + psp->vdata->cmdresp_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -0600183
184 /* wait for command completion */
Brijesh Singhe82867f2018-08-15 16:11:25 -0500185 ret = sev_wait_cmd_ioc(psp, &reg, 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 Singh200664d2017-12-04 10:57:28 -0600197
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
213static 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
224static 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 Lendacky0fc5dea2019-10-03 21:17:46 +0000240
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 Singh200664d2017-12-04 10:57:28 -0600247 dev_dbg(psp->dev, "SEV firmware initialized\n");
248
249 return rc;
250}
251
252int 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}
262EXPORT_SYMBOL_GPL(sev_platform_init);
263
264static int __sev_platform_shutdown_locked(int *error)
265{
266 int ret;
267
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600268 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
Brijesh Singh200664d2017-12-04 10:57:28 -0600269 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
278static 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 Singh2960f9a2017-12-04 10:57:29 -0600289static 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
302static int sev_ioctl_do_reset(struct sev_issue_cmd *argp)
303{
304 int state, rc;
305
306 /*
307 * The SEV spec requires that FACTORY_RESET must be issued in
308 * UNINIT state. Before we go further lets check if any guest is
309 * active.
310 *
311 * If FW is in WORKING state then deny the request otherwise issue
312 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
313 *
314 */
315 rc = sev_get_platform_state(&state, &argp->error);
316 if (rc)
317 return rc;
318
319 if (state == SEV_STATE_WORKING)
320 return -EBUSY;
321
322 if (state == SEV_STATE_INIT) {
323 rc = __sev_platform_shutdown_locked(&argp->error);
324 if (rc)
325 return rc;
326 }
327
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600328 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600329}
330
Brijesh Singhefe18292017-12-04 10:57:29 -0600331static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
332{
333 struct sev_user_data_status *data = &psp_master->status_cmd_buf;
334 int ret;
335
336 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
337 if (ret)
338 return ret;
339
340 if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
341 ret = -EFAULT;
342
343 return ret;
344}
345
Brijesh Singh4d84b722017-12-04 10:57:30 -0600346static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)
347{
348 int rc;
349
350 if (psp_master->sev_state == SEV_STATE_UNINIT) {
351 rc = __sev_platform_init_locked(&argp->error);
352 if (rc)
353 return rc;
354 }
355
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600356 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
Brijesh Singh4d84b722017-12-04 10:57:30 -0600357}
358
Brijesh Singhe7990352017-12-04 10:57:31 -0600359static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)
360{
361 struct sev_user_data_pek_csr input;
362 struct sev_data_pek_csr *data;
363 void *blob = NULL;
364 int ret;
365
366 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
367 return -EFAULT;
368
369 data = kzalloc(sizeof(*data), GFP_KERNEL);
370 if (!data)
371 return -ENOMEM;
372
373 /* userspace wants to query CSR length */
374 if (!input.address || !input.length)
375 goto cmd;
376
377 /* allocate a physically contiguous buffer to store the CSR blob */
Linus Torvalds96d4f262019-01-03 18:57:57 -0800378 if (!access_ok(input.address, input.length) ||
Brijesh Singhe7990352017-12-04 10:57:31 -0600379 input.length > SEV_FW_BLOB_MAX_SIZE) {
380 ret = -EFAULT;
381 goto e_free;
382 }
383
384 blob = kmalloc(input.length, GFP_KERNEL);
385 if (!blob) {
386 ret = -ENOMEM;
387 goto e_free;
388 }
389
390 data->address = __psp_pa(blob);
391 data->len = input.length;
392
393cmd:
394 if (psp_master->sev_state == SEV_STATE_UNINIT) {
395 ret = __sev_platform_init_locked(&argp->error);
396 if (ret)
397 goto e_free_blob;
398 }
399
400 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
401
402 /* If we query the CSR length, FW responded with expected data. */
403 input.length = data->len;
404
405 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
406 ret = -EFAULT;
407 goto e_free_blob;
408 }
409
410 if (blob) {
411 if (copy_to_user((void __user *)input.address, blob, input.length))
412 ret = -EFAULT;
413 }
414
415e_free_blob:
416 kfree(blob);
417e_free:
418 kfree(data);
419 return ret;
420}
421
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600422void *psp_copy_user_blob(u64 __user uaddr, u32 len)
423{
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600424 if (!uaddr || !len)
425 return ERR_PTR(-EINVAL);
426
427 /* verify that blob length does not exceed our limit */
428 if (len > SEV_FW_BLOB_MAX_SIZE)
429 return ERR_PTR(-EINVAL);
430
Markus Elfring6c51ddd2018-03-05 13:50:13 +0100431 return memdup_user((void __user *)(uintptr_t)uaddr, len);
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600432}
433EXPORT_SYMBOL_GPL(psp_copy_user_blob);
434
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500435static int sev_get_api_version(void)
436{
437 struct sev_user_data_status *status;
Janakarajan Natarajanb78d3792018-09-14 17:32:03 -0500438 int error = 0, ret;
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500439
440 status = &psp_master->status_cmd_buf;
441 ret = sev_platform_status(status, &error);
442 if (ret) {
443 dev_err(psp_master->dev,
444 "SEV: failed to get status. Error: %#x\n", error);
445 return 1;
446 }
447
448 psp_master->api_major = status->api_major;
449 psp_master->api_minor = status->api_minor;
450 psp_master->build = status->build;
Singh, Brijeshf8903b32019-01-30 20:57:52 +0000451 psp_master->sev_state = status->state;
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500452
453 return 0;
454}
455
Wei Yongjun5182f262018-09-26 02:09:23 +0000456static int sev_get_firmware(struct device *dev,
457 const struct firmware **firmware)
Janakarajan Natarajane9372062018-09-14 17:32:04 -0500458{
459 char fw_name_specific[SEV_FW_NAME_SIZE];
460 char fw_name_subset[SEV_FW_NAME_SIZE];
461
462 snprintf(fw_name_specific, sizeof(fw_name_specific),
463 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
464 boot_cpu_data.x86, boot_cpu_data.x86_model);
465
466 snprintf(fw_name_subset, sizeof(fw_name_subset),
467 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
468 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
469
470 /* Check for SEV FW for a particular model.
471 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
472 *
473 * or
474 *
475 * Check for SEV FW common to a subset of models.
476 * Ex. amd_sev_fam17h_model0xh.sbin for
477 * Family 17h Model 00h -- Family 17h Model 0Fh
478 *
479 * or
480 *
481 * Fall-back to using generic name: sev.fw
482 */
483 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
484 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
485 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
486 return 0;
487
488 return -ENOENT;
489}
490
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500491/* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
492static int sev_update_firmware(struct device *dev)
493{
494 struct sev_data_download_firmware *data;
495 const struct firmware *firmware;
496 int ret, error, order;
497 struct page *p;
498 u64 data_size;
499
Janakarajan Natarajane9372062018-09-14 17:32:04 -0500500 if (sev_get_firmware(dev, &firmware) == -ENOENT) {
501 dev_dbg(dev, "No SEV firmware file present\n");
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500502 return -1;
Janakarajan Natarajane9372062018-09-14 17:32:04 -0500503 }
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500504
505 /*
506 * SEV FW expects the physical address given to it to be 32
507 * byte aligned. Memory allocated has structure placed at the
508 * beginning followed by the firmware being passed to the SEV
509 * FW. Allocate enough memory for data structure + alignment
510 * padding + SEV FW.
511 */
512 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
513
514 order = get_order(firmware->size + data_size);
515 p = alloc_pages(GFP_KERNEL, order);
516 if (!p) {
517 ret = -1;
518 goto fw_err;
519 }
520
521 /*
522 * Copy firmware data to a kernel allocated contiguous
523 * memory region.
524 */
525 data = page_address(p);
526 memcpy(page_address(p) + data_size, firmware->data, firmware->size);
527
528 data->address = __psp_pa(page_address(p) + data_size);
529 data->len = firmware->size;
530
531 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
532 if (ret)
533 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
534 else
535 dev_info(dev, "SEV firmware update successful\n");
536
537 __free_pages(p, order);
538
539fw_err:
540 release_firmware(firmware);
541
542 return ret;
543}
544
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600545static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp)
546{
547 struct sev_user_data_pek_cert_import input;
548 struct sev_data_pek_cert_import *data;
549 void *pek_blob, *oca_blob;
550 int ret;
551
552 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
553 return -EFAULT;
554
555 data = kzalloc(sizeof(*data), GFP_KERNEL);
556 if (!data)
557 return -ENOMEM;
558
559 /* copy PEK certificate blobs from userspace */
560 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
561 if (IS_ERR(pek_blob)) {
562 ret = PTR_ERR(pek_blob);
563 goto e_free;
564 }
565
566 data->pek_cert_address = __psp_pa(pek_blob);
567 data->pek_cert_len = input.pek_cert_len;
568
569 /* copy PEK certificate blobs from userspace */
570 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
571 if (IS_ERR(oca_blob)) {
572 ret = PTR_ERR(oca_blob);
573 goto e_free_pek;
574 }
575
576 data->oca_cert_address = __psp_pa(oca_blob);
577 data->oca_cert_len = input.oca_cert_len;
578
579 /* If platform is not in INIT state then transition it to INIT */
580 if (psp_master->sev_state != SEV_STATE_INIT) {
581 ret = __sev_platform_init_locked(&argp->error);
582 if (ret)
583 goto e_free_oca;
584 }
585
586 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
587
588e_free_oca:
589 kfree(oca_blob);
590e_free_pek:
591 kfree(pek_blob);
592e_free:
593 kfree(data);
594 return ret;
595}
596
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000597static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
598{
599 struct sev_user_data_get_id2 input;
600 struct sev_data_get_id *data;
601 void *id_blob = NULL;
602 int ret;
603
604 /* SEV GET_ID is available from SEV API v0.16 and up */
David Rientjes83bf4252019-07-12 13:41:58 -0700605 if (!sev_version_greater_or_equal(0, 16))
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000606 return -ENOTSUPP;
607
608 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
609 return -EFAULT;
610
611 /* Check if we have write access to the userspace buffer */
612 if (input.address &&
613 input.length &&
614 !access_ok(input.address, input.length))
615 return -EFAULT;
616
617 data = kzalloc(sizeof(*data), GFP_KERNEL);
618 if (!data)
619 return -ENOMEM;
620
621 if (input.address && input.length) {
622 id_blob = kmalloc(input.length, GFP_KERNEL);
623 if (!id_blob) {
624 kfree(data);
625 return -ENOMEM;
626 }
627
628 data->address = __psp_pa(id_blob);
629 data->len = input.length;
630 }
631
632 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
633
634 /*
635 * Firmware will return the length of the ID value (either the minimum
636 * required length or the actual length written), return it to the user.
637 */
638 input.length = data->len;
639
640 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
641 ret = -EFAULT;
642 goto e_free;
643 }
644
645 if (id_blob) {
646 if (copy_to_user((void __user *)input.address,
647 id_blob, data->len)) {
648 ret = -EFAULT;
649 goto e_free;
650 }
651 }
652
653e_free:
654 kfree(id_blob);
655 kfree(data);
656
657 return ret;
658}
659
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500660static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
661{
662 struct sev_data_get_id *data;
663 u64 data_size, user_size;
664 void *id_blob, *mem;
665 int ret;
666
667 /* SEV GET_ID available from SEV API v0.16 and up */
David Rientjes83bf4252019-07-12 13:41:58 -0700668 if (!sev_version_greater_or_equal(0, 16))
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500669 return -ENOTSUPP;
670
671 /* SEV FW expects the buffer it fills with the ID to be
672 * 8-byte aligned. Memory allocated should be enough to
673 * hold data structure + alignment padding + memory
674 * where SEV FW writes the ID.
675 */
676 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
677 user_size = sizeof(struct sev_user_data_get_id);
678
679 mem = kzalloc(data_size + user_size, GFP_KERNEL);
680 if (!mem)
681 return -ENOMEM;
682
683 data = mem;
684 id_blob = mem + data_size;
685
686 data->address = __psp_pa(id_blob);
687 data->len = user_size;
688
689 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
690 if (!ret) {
691 if (copy_to_user((void __user *)argp->data, id_blob, data->len))
692 ret = -EFAULT;
693 }
694
695 kfree(mem);
696
697 return ret;
698}
699
Brijesh Singh76a2b522017-12-04 10:57:31 -0600700static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp)
701{
702 struct sev_user_data_pdh_cert_export input;
703 void *pdh_blob = NULL, *cert_blob = NULL;
704 struct sev_data_pdh_cert_export *data;
705 int ret;
706
707 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
708 return -EFAULT;
709
710 data = kzalloc(sizeof(*data), GFP_KERNEL);
711 if (!data)
712 return -ENOMEM;
713
714 /* Userspace wants to query the certificate length. */
715 if (!input.pdh_cert_address ||
716 !input.pdh_cert_len ||
717 !input.cert_chain_address)
718 goto cmd;
719
720 /* Allocate a physically contiguous buffer to store the PDH blob. */
721 if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) ||
Linus Torvalds96d4f262019-01-03 18:57:57 -0800722 !access_ok(input.pdh_cert_address, input.pdh_cert_len)) {
Brijesh Singh76a2b522017-12-04 10:57:31 -0600723 ret = -EFAULT;
724 goto e_free;
725 }
726
727 /* Allocate a physically contiguous buffer to store the cert chain blob. */
728 if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) ||
Linus Torvalds96d4f262019-01-03 18:57:57 -0800729 !access_ok(input.cert_chain_address, input.cert_chain_len)) {
Brijesh Singh76a2b522017-12-04 10:57:31 -0600730 ret = -EFAULT;
731 goto e_free;
732 }
733
734 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
735 if (!pdh_blob) {
736 ret = -ENOMEM;
737 goto e_free;
738 }
739
740 data->pdh_cert_address = __psp_pa(pdh_blob);
741 data->pdh_cert_len = input.pdh_cert_len;
742
743 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
744 if (!cert_blob) {
745 ret = -ENOMEM;
746 goto e_free_pdh;
747 }
748
749 data->cert_chain_address = __psp_pa(cert_blob);
750 data->cert_chain_len = input.cert_chain_len;
751
752cmd:
753 /* If platform is not in INIT state then transition it to INIT. */
754 if (psp_master->sev_state != SEV_STATE_INIT) {
755 ret = __sev_platform_init_locked(&argp->error);
756 if (ret)
757 goto e_free_cert;
758 }
759
760 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
761
762 /* If we query the length, FW responded with expected data. */
763 input.cert_chain_len = data->cert_chain_len;
764 input.pdh_cert_len = data->pdh_cert_len;
765
766 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
767 ret = -EFAULT;
768 goto e_free_cert;
769 }
770
771 if (pdh_blob) {
772 if (copy_to_user((void __user *)input.pdh_cert_address,
773 pdh_blob, input.pdh_cert_len)) {
774 ret = -EFAULT;
775 goto e_free_cert;
776 }
777 }
778
779 if (cert_blob) {
780 if (copy_to_user((void __user *)input.cert_chain_address,
781 cert_blob, input.cert_chain_len))
782 ret = -EFAULT;
783 }
784
785e_free_cert:
786 kfree(cert_blob);
787e_free_pdh:
788 kfree(pdh_blob);
789e_free:
790 kfree(data);
791 return ret;
792}
793
Brijesh Singh200664d2017-12-04 10:57:28 -0600794static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
795{
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600796 void __user *argp = (void __user *)arg;
797 struct sev_issue_cmd input;
798 int ret = -EFAULT;
799
800 if (!psp_master)
801 return -ENODEV;
802
803 if (ioctl != SEV_ISSUE_CMD)
804 return -EINVAL;
805
806 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
807 return -EFAULT;
808
809 if (input.cmd > SEV_MAX)
810 return -EINVAL;
811
812 mutex_lock(&sev_cmd_mutex);
813
814 switch (input.cmd) {
815
816 case SEV_FACTORY_RESET:
817 ret = sev_ioctl_do_reset(&input);
818 break;
Brijesh Singhefe18292017-12-04 10:57:29 -0600819 case SEV_PLATFORM_STATUS:
820 ret = sev_ioctl_do_platform_status(&input);
821 break;
Brijesh Singh4d84b722017-12-04 10:57:30 -0600822 case SEV_PEK_GEN:
823 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input);
824 break;
Brijesh Singh77f65322017-12-04 10:57:30 -0600825 case SEV_PDH_GEN:
826 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input);
827 break;
Brijesh Singhe7990352017-12-04 10:57:31 -0600828 case SEV_PEK_CSR:
829 ret = sev_ioctl_do_pek_csr(&input);
830 break;
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600831 case SEV_PEK_CERT_IMPORT:
832 ret = sev_ioctl_do_pek_import(&input);
833 break;
Brijesh Singh76a2b522017-12-04 10:57:31 -0600834 case SEV_PDH_CERT_EXPORT:
835 ret = sev_ioctl_do_pdh_export(&input);
836 break;
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500837 case SEV_GET_ID:
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000838 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500839 ret = sev_ioctl_do_get_id(&input);
840 break;
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000841 case SEV_GET_ID2:
842 ret = sev_ioctl_do_get_id2(&input);
843 break;
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600844 default:
845 ret = -EINVAL;
846 goto out;
847 }
848
849 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
850 ret = -EFAULT;
851out:
852 mutex_unlock(&sev_cmd_mutex);
853
854 return ret;
Brijesh Singh200664d2017-12-04 10:57:28 -0600855}
856
857static const struct file_operations sev_fops = {
858 .owner = THIS_MODULE,
859 .unlocked_ioctl = sev_ioctl,
860};
861
862int sev_platform_status(struct sev_user_data_status *data, int *error)
863{
864 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
865}
866EXPORT_SYMBOL_GPL(sev_platform_status);
867
868int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
869{
870 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
871}
872EXPORT_SYMBOL_GPL(sev_guest_deactivate);
873
874int sev_guest_activate(struct sev_data_activate *data, int *error)
875{
876 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
877}
878EXPORT_SYMBOL_GPL(sev_guest_activate);
879
880int sev_guest_decommission(struct sev_data_decommission *data, int *error)
881{
882 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
883}
884EXPORT_SYMBOL_GPL(sev_guest_decommission);
885
886int sev_guest_df_flush(int *error)
887{
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600888 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
Brijesh Singh200664d2017-12-04 10:57:28 -0600889}
890EXPORT_SYMBOL_GPL(sev_guest_df_flush);
891
892static void sev_exit(struct kref *ref)
893{
894 struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount);
895
896 misc_deregister(&misc_dev->misc);
897}
898
899static int sev_misc_init(struct psp_device *psp)
900{
901 struct device *dev = psp->dev;
902 int ret;
903
904 /*
905 * SEV feature support can be detected on multiple devices but the SEV
906 * FW commands must be issued on the master. During probe, we do not
907 * know the master hence we create /dev/sev on the first device probe.
908 * sev_do_cmd() finds the right master device to which to issue the
909 * command to the firmware.
910 */
911 if (!misc_dev) {
912 struct miscdevice *misc;
913
914 misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL);
915 if (!misc_dev)
916 return -ENOMEM;
917
918 misc = &misc_dev->misc;
919 misc->minor = MISC_DYNAMIC_MINOR;
920 misc->name = DEVICE_NAME;
921 misc->fops = &sev_fops;
922
923 ret = misc_register(misc);
924 if (ret)
925 return ret;
926
927 kref_init(&misc_dev->refcount);
928 } else {
929 kref_get(&misc_dev->refcount);
930 }
931
932 init_waitqueue_head(&psp->sev_int_queue);
933 psp->sev_misc = misc_dev;
934 dev_dbg(dev, "registered SEV device\n");
935
936 return 0;
937}
938
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000939static int psp_check_sev_support(struct psp_device *psp)
Brijesh Singh200664d2017-12-04 10:57:28 -0600940{
941 /* Check if device supports SEV feature */
Tom Lendackyad01a982018-07-03 12:12:03 -0500942 if (!(ioread32(psp->io_regs + psp->vdata->feature_reg) & 1)) {
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000943 dev_dbg(psp->dev, "psp does not support SEV\n");
944 return -ENODEV;
Brijesh Singh200664d2017-12-04 10:57:28 -0600945 }
946
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000947 return 0;
Brijesh Singh200664d2017-12-04 10:57:28 -0600948}
949
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600950int psp_dev_init(struct sp_device *sp)
951{
952 struct device *dev = sp->dev;
953 struct psp_device *psp;
954 int ret;
955
956 ret = -ENOMEM;
957 psp = psp_alloc_struct(sp);
958 if (!psp)
959 goto e_err;
960
961 sp->psp_data = psp;
962
963 psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
964 if (!psp->vdata) {
965 ret = -ENODEV;
966 dev_err(dev, "missing driver data\n");
967 goto e_err;
968 }
969
Tom Lendackyad01a982018-07-03 12:12:03 -0500970 psp->io_regs = sp->io_map;
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600971
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000972 ret = psp_check_sev_support(psp);
973 if (ret)
974 goto e_disable;
975
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600976 /* Disable and clear interrupts until ready */
Tom Lendackyad01a982018-07-03 12:12:03 -0500977 iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
978 iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600979
980 /* Request an irq */
981 ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
982 if (ret) {
983 dev_err(dev, "psp: unable to allocate an IRQ\n");
984 goto e_err;
985 }
986
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000987 ret = sev_misc_init(psp);
Brijesh Singh200664d2017-12-04 10:57:28 -0600988 if (ret)
989 goto e_irq;
990
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600991 if (sp->set_psp_master_device)
992 sp->set_psp_master_device(sp);
993
994 /* Enable interrupt */
Tom Lendackyad01a982018-07-03 12:12:03 -0500995 iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600996
Tom Lendacky015c8c82018-07-03 12:11:42 -0500997 dev_notice(dev, "psp enabled\n");
998
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600999 return 0;
1000
Brijesh Singh200664d2017-12-04 10:57:28 -06001001e_irq:
1002 sp_free_psp_irq(psp->sp, psp);
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001003e_err:
1004 sp->psp_data = NULL;
1005
1006 dev_notice(dev, "psp initialization failed\n");
1007
1008 return ret;
Lendacky, Thomas7df52182019-02-15 17:26:33 +00001009
1010e_disable:
1011 sp->psp_data = NULL;
1012
1013 return ret;
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001014}
1015
1016void psp_dev_destroy(struct sp_device *sp)
1017{
1018 struct psp_device *psp = sp->psp_data;
1019
Tom Lendackyafb31cd2018-07-26 09:37:59 -05001020 if (!psp)
1021 return;
1022
Brijesh Singh200664d2017-12-04 10:57:28 -06001023 if (psp->sev_misc)
1024 kref_put(&misc_dev->refcount, sev_exit);
1025
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001026 sp_free_psp_irq(sp, psp);
1027}
Brijesh Singh200664d2017-12-04 10:57:28 -06001028
1029int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1030 void *data, int *error)
1031{
1032 if (!filep || filep->f_op != &sev_fops)
1033 return -EBADF;
1034
1035 return sev_do_cmd(cmd, data, error);
1036}
1037EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1038
1039void psp_pci_init(void)
1040{
Brijesh Singh200664d2017-12-04 10:57:28 -06001041 struct sp_device *sp;
1042 int error, rc;
1043
1044 sp = sp_get_psp_master_device();
1045 if (!sp)
1046 return;
1047
1048 psp_master = sp->psp_data;
1049
Brijesh Singhe82867f2018-08-15 16:11:25 -05001050 psp_timeout = psp_probe_timeout;
1051
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -05001052 if (sev_get_api_version())
1053 goto err;
1054
Singh, Brijeshf8903b32019-01-30 20:57:52 +00001055 /*
1056 * If platform is not in UNINIT state then firmware upgrade and/or
1057 * platform INIT command will fail. These command require UNINIT state.
1058 *
1059 * In a normal boot we should never run into case where the firmware
1060 * is not in UNINIT state on boot. But in case of kexec boot, a reboot
1061 * may not go through a typical shutdown sequence and may leave the
1062 * firmware in INIT or WORKING state.
1063 */
1064
1065 if (psp_master->sev_state != SEV_STATE_UNINIT) {
1066 sev_platform_shutdown(NULL);
1067 psp_master->sev_state = SEV_STATE_UNINIT;
1068 }
1069
David Rientjes83bf4252019-07-12 13:41:58 -07001070 if (sev_version_greater_or_equal(0, 15) &&
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -05001071 sev_update_firmware(psp_master->dev) == 0)
1072 sev_get_api_version();
1073
Brijesh Singh200664d2017-12-04 10:57:28 -06001074 /* Initialize the platform */
1075 rc = sev_platform_init(&error);
1076 if (rc) {
1077 dev_err(sp->dev, "SEV: failed to INIT error %#x\n", error);
Singh, Brijeshf5a2aeb82019-04-08 20:42:55 +00001078 return;
Brijesh Singh200664d2017-12-04 10:57:28 -06001079 }
1080
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -05001081 dev_info(sp->dev, "SEV API:%d.%d build:%d\n", psp_master->api_major,
1082 psp_master->api_minor, psp_master->build);
Brijesh Singh200664d2017-12-04 10:57:28 -06001083
Brijesh Singh200664d2017-12-04 10:57:28 -06001084 return;
1085
1086err:
1087 psp_master = NULL;
1088}
1089
1090void psp_pci_exit(void)
1091{
1092 if (!psp_master)
1093 return;
1094
1095 sev_platform_shutdown(NULL);
1096}