blob: 5ff842c03a70ce45e1c330ad8542be44351e28d9 [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
24#include "sp-dev.h"
25#include "psp-dev.h"
26
Janakarajan Natarajane9372062018-09-14 17:32:04 -050027#define DEVICE_NAME "sev"
28#define SEV_FW_FILE "amd/sev.fw"
29#define SEV_FW_NAME_SIZE 64
Brijesh Singh200664d2017-12-04 10:57:28 -060030
31static DEFINE_MUTEX(sev_cmd_mutex);
32static struct sev_misc_dev *misc_dev;
33static struct psp_device *psp_master;
34
Brijesh Singhe82867f2018-08-15 16:11:25 -050035static int psp_cmd_timeout = 100;
36module_param(psp_cmd_timeout, int, 0644);
37MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
38
39static int psp_probe_timeout = 5;
40module_param(psp_probe_timeout, int, 0644);
41MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
42
43static bool psp_dead;
44static int psp_timeout;
45
David Rientjes83bf4252019-07-12 13:41:58 -070046static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
47{
48 if (psp_master->api_major > maj)
49 return true;
50 if (psp_master->api_major == maj && psp_master->api_minor >= min)
51 return true;
52 return false;
53}
54
Brijesh Singh2a6170d2017-12-04 10:57:28 -060055static struct psp_device *psp_alloc_struct(struct sp_device *sp)
56{
57 struct device *dev = sp->dev;
58 struct psp_device *psp;
59
60 psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL);
61 if (!psp)
62 return NULL;
63
64 psp->dev = dev;
65 psp->sp = sp;
66
67 snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord);
68
69 return psp;
70}
71
72static irqreturn_t psp_irq_handler(int irq, void *data)
73{
Brijesh Singh200664d2017-12-04 10:57:28 -060074 struct psp_device *psp = data;
75 unsigned int status;
76 int reg;
77
78 /* Read the interrupt status: */
Tom Lendackyad01a982018-07-03 12:12:03 -050079 status = ioread32(psp->io_regs + psp->vdata->intsts_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -060080
81 /* Check if it is command completion: */
Tom Lendacky03af9122018-07-03 12:11:52 -050082 if (!(status & PSP_CMD_COMPLETE))
Brijesh Singh200664d2017-12-04 10:57:28 -060083 goto done;
84
85 /* Check if it is SEV command completion: */
Tom Lendackyad01a982018-07-03 12:12:03 -050086 reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -060087 if (reg & PSP_CMDRESP_RESP) {
88 psp->sev_int_rcvd = 1;
89 wake_up(&psp->sev_int_queue);
90 }
91
92done:
93 /* Clear the interrupt status by writing the same value we read. */
Tom Lendackyad01a982018-07-03 12:12:03 -050094 iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -060095
Brijesh Singh2a6170d2017-12-04 10:57:28 -060096 return IRQ_HANDLED;
97}
98
Brijesh Singhe82867f2018-08-15 16:11:25 -050099static int sev_wait_cmd_ioc(struct psp_device *psp,
100 unsigned int *reg, unsigned int timeout)
Brijesh Singh200664d2017-12-04 10:57:28 -0600101{
Brijesh Singhe82867f2018-08-15 16:11:25 -0500102 int ret;
103
104 ret = wait_event_timeout(psp->sev_int_queue,
105 psp->sev_int_rcvd, timeout * HZ);
106 if (!ret)
107 return -ETIMEDOUT;
108
Tom Lendackyad01a982018-07-03 12:12:03 -0500109 *reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg);
Brijesh Singhe82867f2018-08-15 16:11:25 -0500110
111 return 0;
Brijesh Singh200664d2017-12-04 10:57:28 -0600112}
113
114static int sev_cmd_buffer_len(int cmd)
115{
116 switch (cmd) {
117 case SEV_CMD_INIT: return sizeof(struct sev_data_init);
118 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
119 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
120 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
121 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
122 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
123 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
124 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
125 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
126 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
127 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
128 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
129 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
130 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
131 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
132 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
133 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
134 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
135 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
136 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
137 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
138 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
139 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
140 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
141 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500142 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500143 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
Brijesh Singh200664d2017-12-04 10:57:28 -0600144 default: return 0;
145 }
146
147 return 0;
148}
149
150static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
151{
152 struct psp_device *psp = psp_master;
153 unsigned int phys_lsb, phys_msb;
154 unsigned int reg, ret = 0;
155
156 if (!psp)
157 return -ENODEV;
158
Brijesh Singhe82867f2018-08-15 16:11:25 -0500159 if (psp_dead)
160 return -EBUSY;
161
Brijesh Singh200664d2017-12-04 10:57:28 -0600162 /* Get the physical address of the command buffer */
163 phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0;
164 phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0;
165
Brijesh Singhe82867f2018-08-15 16:11:25 -0500166 dev_dbg(psp->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
167 cmd, phys_msb, phys_lsb, psp_timeout);
Brijesh Singh200664d2017-12-04 10:57:28 -0600168
169 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
170 sev_cmd_buffer_len(cmd), false);
171
Tom Lendackyad01a982018-07-03 12:12:03 -0500172 iowrite32(phys_lsb, psp->io_regs + psp->vdata->cmdbuff_addr_lo_reg);
173 iowrite32(phys_msb, psp->io_regs + psp->vdata->cmdbuff_addr_hi_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -0600174
Tom Lendackyf426d2b2018-07-03 12:11:33 -0500175 psp->sev_int_rcvd = 0;
176
Brijesh Singh200664d2017-12-04 10:57:28 -0600177 reg = cmd;
178 reg <<= PSP_CMDRESP_CMD_SHIFT;
179 reg |= PSP_CMDRESP_IOC;
Tom Lendackyad01a982018-07-03 12:12:03 -0500180 iowrite32(reg, psp->io_regs + psp->vdata->cmdresp_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -0600181
182 /* wait for command completion */
Brijesh Singhe82867f2018-08-15 16:11:25 -0500183 ret = sev_wait_cmd_ioc(psp, &reg, psp_timeout);
184 if (ret) {
185 if (psp_ret)
186 *psp_ret = 0;
187
188 dev_err(psp->dev, "sev command %#x timed out, disabling PSP \n", cmd);
189 psp_dead = true;
190
191 return ret;
192 }
193
194 psp_timeout = psp_cmd_timeout;
Brijesh Singh200664d2017-12-04 10:57:28 -0600195
196 if (psp_ret)
197 *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
198
199 if (reg & PSP_CMDRESP_ERR_MASK) {
200 dev_dbg(psp->dev, "sev command %#x failed (%#010x)\n",
201 cmd, reg & PSP_CMDRESP_ERR_MASK);
202 ret = -EIO;
203 }
204
205 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
206 sev_cmd_buffer_len(cmd), false);
207
208 return ret;
209}
210
211static int sev_do_cmd(int cmd, void *data, int *psp_ret)
212{
213 int rc;
214
215 mutex_lock(&sev_cmd_mutex);
216 rc = __sev_do_cmd_locked(cmd, data, psp_ret);
217 mutex_unlock(&sev_cmd_mutex);
218
219 return rc;
220}
221
222static int __sev_platform_init_locked(int *error)
223{
224 struct psp_device *psp = psp_master;
225 int rc = 0;
226
227 if (!psp)
228 return -ENODEV;
229
230 if (psp->sev_state == SEV_STATE_INIT)
231 return 0;
232
233 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &psp->init_cmd_buf, error);
234 if (rc)
235 return rc;
236
237 psp->sev_state = SEV_STATE_INIT;
238 dev_dbg(psp->dev, "SEV firmware initialized\n");
239
240 return rc;
241}
242
243int sev_platform_init(int *error)
244{
245 int rc;
246
247 mutex_lock(&sev_cmd_mutex);
248 rc = __sev_platform_init_locked(error);
249 mutex_unlock(&sev_cmd_mutex);
250
251 return rc;
252}
253EXPORT_SYMBOL_GPL(sev_platform_init);
254
255static int __sev_platform_shutdown_locked(int *error)
256{
257 int ret;
258
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600259 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
Brijesh Singh200664d2017-12-04 10:57:28 -0600260 if (ret)
261 return ret;
262
263 psp_master->sev_state = SEV_STATE_UNINIT;
264 dev_dbg(psp_master->dev, "SEV firmware shutdown\n");
265
266 return ret;
267}
268
269static int sev_platform_shutdown(int *error)
270{
271 int rc;
272
273 mutex_lock(&sev_cmd_mutex);
274 rc = __sev_platform_shutdown_locked(NULL);
275 mutex_unlock(&sev_cmd_mutex);
276
277 return rc;
278}
279
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600280static int sev_get_platform_state(int *state, int *error)
281{
282 int rc;
283
284 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
285 &psp_master->status_cmd_buf, error);
286 if (rc)
287 return rc;
288
289 *state = psp_master->status_cmd_buf.state;
290 return rc;
291}
292
293static int sev_ioctl_do_reset(struct sev_issue_cmd *argp)
294{
295 int state, rc;
296
Brijesh Singhec310ca2019-11-12 13:58:34 -0600297 if (!capable(CAP_SYS_ADMIN))
298 return -EPERM;
299
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600300 /*
301 * The SEV spec requires that FACTORY_RESET must be issued in
302 * UNINIT state. Before we go further lets check if any guest is
303 * active.
304 *
305 * If FW is in WORKING state then deny the request otherwise issue
306 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
307 *
308 */
309 rc = sev_get_platform_state(&state, &argp->error);
310 if (rc)
311 return rc;
312
313 if (state == SEV_STATE_WORKING)
314 return -EBUSY;
315
316 if (state == SEV_STATE_INIT) {
317 rc = __sev_platform_shutdown_locked(&argp->error);
318 if (rc)
319 return rc;
320 }
321
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600322 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600323}
324
Brijesh Singhefe18292017-12-04 10:57:29 -0600325static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
326{
327 struct sev_user_data_status *data = &psp_master->status_cmd_buf;
328 int ret;
329
330 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
331 if (ret)
332 return ret;
333
334 if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
335 ret = -EFAULT;
336
337 return ret;
338}
339
Brijesh Singh4d84b722017-12-04 10:57:30 -0600340static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)
341{
342 int rc;
343
Brijesh Singhec310ca2019-11-12 13:58:34 -0600344 if (!capable(CAP_SYS_ADMIN))
345 return -EPERM;
346
Brijesh Singh4d84b722017-12-04 10:57:30 -0600347 if (psp_master->sev_state == SEV_STATE_UNINIT) {
348 rc = __sev_platform_init_locked(&argp->error);
349 if (rc)
350 return rc;
351 }
352
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600353 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
Brijesh Singh4d84b722017-12-04 10:57:30 -0600354}
355
Brijesh Singhe7990352017-12-04 10:57:31 -0600356static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)
357{
358 struct sev_user_data_pek_csr input;
359 struct sev_data_pek_csr *data;
360 void *blob = NULL;
361 int ret;
362
Brijesh Singhec310ca2019-11-12 13:58:34 -0600363 if (!capable(CAP_SYS_ADMIN))
364 return -EPERM;
365
Brijesh Singhe7990352017-12-04 10:57:31 -0600366 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
Brijesh Singhec310ca2019-11-12 13:58:34 -0600552 if (!capable(CAP_SYS_ADMIN))
553 return -EPERM;
554
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600555 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
556 return -EFAULT;
557
558 data = kzalloc(sizeof(*data), GFP_KERNEL);
559 if (!data)
560 return -ENOMEM;
561
562 /* copy PEK certificate blobs from userspace */
563 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
564 if (IS_ERR(pek_blob)) {
565 ret = PTR_ERR(pek_blob);
566 goto e_free;
567 }
568
569 data->pek_cert_address = __psp_pa(pek_blob);
570 data->pek_cert_len = input.pek_cert_len;
571
572 /* copy PEK certificate blobs from userspace */
573 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
574 if (IS_ERR(oca_blob)) {
575 ret = PTR_ERR(oca_blob);
576 goto e_free_pek;
577 }
578
579 data->oca_cert_address = __psp_pa(oca_blob);
580 data->oca_cert_len = input.oca_cert_len;
581
582 /* If platform is not in INIT state then transition it to INIT */
583 if (psp_master->sev_state != SEV_STATE_INIT) {
584 ret = __sev_platform_init_locked(&argp->error);
585 if (ret)
586 goto e_free_oca;
587 }
588
589 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
590
591e_free_oca:
592 kfree(oca_blob);
593e_free_pek:
594 kfree(pek_blob);
595e_free:
596 kfree(data);
597 return ret;
598}
599
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000600static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
601{
602 struct sev_user_data_get_id2 input;
603 struct sev_data_get_id *data;
604 void *id_blob = NULL;
605 int ret;
606
607 /* SEV GET_ID is available from SEV API v0.16 and up */
David Rientjes83bf4252019-07-12 13:41:58 -0700608 if (!sev_version_greater_or_equal(0, 16))
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000609 return -ENOTSUPP;
610
611 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
612 return -EFAULT;
613
614 /* Check if we have write access to the userspace buffer */
615 if (input.address &&
616 input.length &&
617 !access_ok(input.address, input.length))
618 return -EFAULT;
619
620 data = kzalloc(sizeof(*data), GFP_KERNEL);
621 if (!data)
622 return -ENOMEM;
623
624 if (input.address && input.length) {
625 id_blob = kmalloc(input.length, GFP_KERNEL);
626 if (!id_blob) {
627 kfree(data);
628 return -ENOMEM;
629 }
630
631 data->address = __psp_pa(id_blob);
632 data->len = input.length;
633 }
634
635 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
636
637 /*
638 * Firmware will return the length of the ID value (either the minimum
639 * required length or the actual length written), return it to the user.
640 */
641 input.length = data->len;
642
643 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
644 ret = -EFAULT;
645 goto e_free;
646 }
647
648 if (id_blob) {
649 if (copy_to_user((void __user *)input.address,
650 id_blob, data->len)) {
651 ret = -EFAULT;
652 goto e_free;
653 }
654 }
655
656e_free:
657 kfree(id_blob);
658 kfree(data);
659
660 return ret;
661}
662
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500663static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
664{
665 struct sev_data_get_id *data;
666 u64 data_size, user_size;
667 void *id_blob, *mem;
668 int ret;
669
670 /* SEV GET_ID available from SEV API v0.16 and up */
David Rientjes83bf4252019-07-12 13:41:58 -0700671 if (!sev_version_greater_or_equal(0, 16))
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500672 return -ENOTSUPP;
673
674 /* SEV FW expects the buffer it fills with the ID to be
675 * 8-byte aligned. Memory allocated should be enough to
676 * hold data structure + alignment padding + memory
677 * where SEV FW writes the ID.
678 */
679 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
680 user_size = sizeof(struct sev_user_data_get_id);
681
682 mem = kzalloc(data_size + user_size, GFP_KERNEL);
683 if (!mem)
684 return -ENOMEM;
685
686 data = mem;
687 id_blob = mem + data_size;
688
689 data->address = __psp_pa(id_blob);
690 data->len = user_size;
691
692 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
693 if (!ret) {
694 if (copy_to_user((void __user *)argp->data, id_blob, data->len))
695 ret = -EFAULT;
696 }
697
698 kfree(mem);
699
700 return ret;
701}
702
Brijesh Singh76a2b522017-12-04 10:57:31 -0600703static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp)
704{
705 struct sev_user_data_pdh_cert_export input;
706 void *pdh_blob = NULL, *cert_blob = NULL;
707 struct sev_data_pdh_cert_export *data;
708 int ret;
709
Brijesh Singhec310ca2019-11-12 13:58:34 -0600710 /* If platform is not in INIT state then transition it to INIT. */
711 if (psp_master->sev_state != SEV_STATE_INIT) {
712 if (!capable(CAP_SYS_ADMIN))
713 return -EPERM;
714
715 ret = __sev_platform_init_locked(&argp->error);
716 if (ret)
717 return ret;
718 }
719
Brijesh Singh76a2b522017-12-04 10:57:31 -0600720 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
721 return -EFAULT;
722
723 data = kzalloc(sizeof(*data), GFP_KERNEL);
724 if (!data)
725 return -ENOMEM;
726
727 /* Userspace wants to query the certificate length. */
728 if (!input.pdh_cert_address ||
729 !input.pdh_cert_len ||
730 !input.cert_chain_address)
731 goto cmd;
732
733 /* Allocate a physically contiguous buffer to store the PDH blob. */
734 if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) ||
Linus Torvalds96d4f262019-01-03 18:57:57 -0800735 !access_ok(input.pdh_cert_address, input.pdh_cert_len)) {
Brijesh Singh76a2b522017-12-04 10:57:31 -0600736 ret = -EFAULT;
737 goto e_free;
738 }
739
740 /* Allocate a physically contiguous buffer to store the cert chain blob. */
741 if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) ||
Linus Torvalds96d4f262019-01-03 18:57:57 -0800742 !access_ok(input.cert_chain_address, input.cert_chain_len)) {
Brijesh Singh76a2b522017-12-04 10:57:31 -0600743 ret = -EFAULT;
744 goto e_free;
745 }
746
747 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
748 if (!pdh_blob) {
749 ret = -ENOMEM;
750 goto e_free;
751 }
752
753 data->pdh_cert_address = __psp_pa(pdh_blob);
754 data->pdh_cert_len = input.pdh_cert_len;
755
756 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
757 if (!cert_blob) {
758 ret = -ENOMEM;
759 goto e_free_pdh;
760 }
761
762 data->cert_chain_address = __psp_pa(cert_blob);
763 data->cert_chain_len = input.cert_chain_len;
764
765cmd:
Brijesh Singh76a2b522017-12-04 10:57:31 -0600766 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
767
768 /* If we query the length, FW responded with expected data. */
769 input.cert_chain_len = data->cert_chain_len;
770 input.pdh_cert_len = data->pdh_cert_len;
771
772 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
773 ret = -EFAULT;
774 goto e_free_cert;
775 }
776
777 if (pdh_blob) {
778 if (copy_to_user((void __user *)input.pdh_cert_address,
779 pdh_blob, input.pdh_cert_len)) {
780 ret = -EFAULT;
781 goto e_free_cert;
782 }
783 }
784
785 if (cert_blob) {
786 if (copy_to_user((void __user *)input.cert_chain_address,
787 cert_blob, input.cert_chain_len))
788 ret = -EFAULT;
789 }
790
791e_free_cert:
792 kfree(cert_blob);
793e_free_pdh:
794 kfree(pdh_blob);
795e_free:
796 kfree(data);
797 return ret;
798}
799
Brijesh Singh200664d2017-12-04 10:57:28 -0600800static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
801{
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600802 void __user *argp = (void __user *)arg;
803 struct sev_issue_cmd input;
804 int ret = -EFAULT;
805
806 if (!psp_master)
807 return -ENODEV;
808
809 if (ioctl != SEV_ISSUE_CMD)
810 return -EINVAL;
811
812 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
813 return -EFAULT;
814
815 if (input.cmd > SEV_MAX)
816 return -EINVAL;
817
818 mutex_lock(&sev_cmd_mutex);
819
820 switch (input.cmd) {
821
822 case SEV_FACTORY_RESET:
823 ret = sev_ioctl_do_reset(&input);
824 break;
Brijesh Singhefe18292017-12-04 10:57:29 -0600825 case SEV_PLATFORM_STATUS:
826 ret = sev_ioctl_do_platform_status(&input);
827 break;
Brijesh Singh4d84b722017-12-04 10:57:30 -0600828 case SEV_PEK_GEN:
829 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input);
830 break;
Brijesh Singh77f65322017-12-04 10:57:30 -0600831 case SEV_PDH_GEN:
832 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input);
833 break;
Brijesh Singhe7990352017-12-04 10:57:31 -0600834 case SEV_PEK_CSR:
835 ret = sev_ioctl_do_pek_csr(&input);
836 break;
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600837 case SEV_PEK_CERT_IMPORT:
838 ret = sev_ioctl_do_pek_import(&input);
839 break;
Brijesh Singh76a2b522017-12-04 10:57:31 -0600840 case SEV_PDH_CERT_EXPORT:
841 ret = sev_ioctl_do_pdh_export(&input);
842 break;
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500843 case SEV_GET_ID:
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000844 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500845 ret = sev_ioctl_do_get_id(&input);
846 break;
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000847 case SEV_GET_ID2:
848 ret = sev_ioctl_do_get_id2(&input);
849 break;
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600850 default:
851 ret = -EINVAL;
852 goto out;
853 }
854
855 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
856 ret = -EFAULT;
857out:
858 mutex_unlock(&sev_cmd_mutex);
859
860 return ret;
Brijesh Singh200664d2017-12-04 10:57:28 -0600861}
862
863static const struct file_operations sev_fops = {
864 .owner = THIS_MODULE,
865 .unlocked_ioctl = sev_ioctl,
866};
867
868int sev_platform_status(struct sev_user_data_status *data, int *error)
869{
870 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
871}
872EXPORT_SYMBOL_GPL(sev_platform_status);
873
874int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
875{
876 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
877}
878EXPORT_SYMBOL_GPL(sev_guest_deactivate);
879
880int sev_guest_activate(struct sev_data_activate *data, int *error)
881{
882 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
883}
884EXPORT_SYMBOL_GPL(sev_guest_activate);
885
886int sev_guest_decommission(struct sev_data_decommission *data, int *error)
887{
888 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
889}
890EXPORT_SYMBOL_GPL(sev_guest_decommission);
891
892int sev_guest_df_flush(int *error)
893{
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600894 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
Brijesh Singh200664d2017-12-04 10:57:28 -0600895}
896EXPORT_SYMBOL_GPL(sev_guest_df_flush);
897
898static void sev_exit(struct kref *ref)
899{
900 struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount);
901
902 misc_deregister(&misc_dev->misc);
903}
904
905static int sev_misc_init(struct psp_device *psp)
906{
907 struct device *dev = psp->dev;
908 int ret;
909
910 /*
911 * SEV feature support can be detected on multiple devices but the SEV
912 * FW commands must be issued on the master. During probe, we do not
913 * know the master hence we create /dev/sev on the first device probe.
914 * sev_do_cmd() finds the right master device to which to issue the
915 * command to the firmware.
916 */
917 if (!misc_dev) {
918 struct miscdevice *misc;
919
920 misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL);
921 if (!misc_dev)
922 return -ENOMEM;
923
924 misc = &misc_dev->misc;
925 misc->minor = MISC_DYNAMIC_MINOR;
926 misc->name = DEVICE_NAME;
927 misc->fops = &sev_fops;
928
929 ret = misc_register(misc);
930 if (ret)
931 return ret;
932
933 kref_init(&misc_dev->refcount);
934 } else {
935 kref_get(&misc_dev->refcount);
936 }
937
938 init_waitqueue_head(&psp->sev_int_queue);
939 psp->sev_misc = misc_dev;
940 dev_dbg(dev, "registered SEV device\n");
941
942 return 0;
943}
944
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000945static int psp_check_sev_support(struct psp_device *psp)
Brijesh Singh200664d2017-12-04 10:57:28 -0600946{
Hook, Gary03f008c2019-10-21 13:44:44 +0000947 unsigned int val = ioread32(psp->io_regs + psp->vdata->feature_reg);
948
949 /*
950 * Check for a access to the registers. If this read returns
951 * 0xffffffff, it's likely that the system is running a broken
952 * BIOS which disallows access to the device. Stop here and
953 * fail the PSP initialization (but not the load, as the CCP
954 * could get properly initialized).
955 */
956 if (val == 0xffffffff) {
957 dev_notice(psp->dev, "psp: unable to access the device: you might be running a broken BIOS.\n");
958 return -ENODEV;
959 }
960
961 if (!(val & 1)) {
962 /* Device does not support the SEV feature */
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000963 dev_dbg(psp->dev, "psp does not support SEV\n");
964 return -ENODEV;
Brijesh Singh200664d2017-12-04 10:57:28 -0600965 }
966
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000967 return 0;
Brijesh Singh200664d2017-12-04 10:57:28 -0600968}
969
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600970int psp_dev_init(struct sp_device *sp)
971{
972 struct device *dev = sp->dev;
973 struct psp_device *psp;
974 int ret;
975
976 ret = -ENOMEM;
977 psp = psp_alloc_struct(sp);
978 if (!psp)
979 goto e_err;
980
981 sp->psp_data = psp;
982
983 psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
984 if (!psp->vdata) {
985 ret = -ENODEV;
986 dev_err(dev, "missing driver data\n");
987 goto e_err;
988 }
989
Tom Lendackyad01a982018-07-03 12:12:03 -0500990 psp->io_regs = sp->io_map;
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600991
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000992 ret = psp_check_sev_support(psp);
993 if (ret)
994 goto e_disable;
995
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600996 /* Disable and clear interrupts until ready */
Tom Lendackyad01a982018-07-03 12:12:03 -0500997 iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
998 iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600999
1000 /* Request an irq */
1001 ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
1002 if (ret) {
1003 dev_err(dev, "psp: unable to allocate an IRQ\n");
1004 goto e_err;
1005 }
1006
Lendacky, Thomas7df52182019-02-15 17:26:33 +00001007 ret = sev_misc_init(psp);
Brijesh Singh200664d2017-12-04 10:57:28 -06001008 if (ret)
1009 goto e_irq;
1010
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001011 if (sp->set_psp_master_device)
1012 sp->set_psp_master_device(sp);
1013
1014 /* Enable interrupt */
Tom Lendackyad01a982018-07-03 12:12:03 -05001015 iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001016
Tom Lendacky015c8c82018-07-03 12:11:42 -05001017 dev_notice(dev, "psp enabled\n");
1018
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001019 return 0;
1020
Brijesh Singh200664d2017-12-04 10:57:28 -06001021e_irq:
1022 sp_free_psp_irq(psp->sp, psp);
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001023e_err:
1024 sp->psp_data = NULL;
1025
1026 dev_notice(dev, "psp initialization failed\n");
1027
1028 return ret;
Lendacky, Thomas7df52182019-02-15 17:26:33 +00001029
1030e_disable:
1031 sp->psp_data = NULL;
1032
1033 return ret;
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001034}
1035
1036void psp_dev_destroy(struct sp_device *sp)
1037{
1038 struct psp_device *psp = sp->psp_data;
1039
Tom Lendackyafb31cd2018-07-26 09:37:59 -05001040 if (!psp)
1041 return;
1042
Brijesh Singh200664d2017-12-04 10:57:28 -06001043 if (psp->sev_misc)
1044 kref_put(&misc_dev->refcount, sev_exit);
1045
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001046 sp_free_psp_irq(sp, psp);
1047}
Brijesh Singh200664d2017-12-04 10:57:28 -06001048
1049int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1050 void *data, int *error)
1051{
1052 if (!filep || filep->f_op != &sev_fops)
1053 return -EBADF;
1054
1055 return sev_do_cmd(cmd, data, error);
1056}
1057EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1058
1059void psp_pci_init(void)
1060{
Brijesh Singh200664d2017-12-04 10:57:28 -06001061 struct sp_device *sp;
1062 int error, rc;
1063
1064 sp = sp_get_psp_master_device();
1065 if (!sp)
1066 return;
1067
1068 psp_master = sp->psp_data;
1069
Brijesh Singhe82867f2018-08-15 16:11:25 -05001070 psp_timeout = psp_probe_timeout;
1071
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -05001072 if (sev_get_api_version())
1073 goto err;
1074
Singh, Brijeshf8903b32019-01-30 20:57:52 +00001075 /*
1076 * If platform is not in UNINIT state then firmware upgrade and/or
1077 * platform INIT command will fail. These command require UNINIT state.
1078 *
1079 * In a normal boot we should never run into case where the firmware
1080 * is not in UNINIT state on boot. But in case of kexec boot, a reboot
1081 * may not go through a typical shutdown sequence and may leave the
1082 * firmware in INIT or WORKING state.
1083 */
1084
1085 if (psp_master->sev_state != SEV_STATE_UNINIT) {
1086 sev_platform_shutdown(NULL);
1087 psp_master->sev_state = SEV_STATE_UNINIT;
1088 }
1089
David Rientjes83bf4252019-07-12 13:41:58 -07001090 if (sev_version_greater_or_equal(0, 15) &&
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -05001091 sev_update_firmware(psp_master->dev) == 0)
1092 sev_get_api_version();
1093
Brijesh Singh200664d2017-12-04 10:57:28 -06001094 /* Initialize the platform */
1095 rc = sev_platform_init(&error);
Ashish Kalra1d55fdc2019-10-17 22:35:11 +00001096 if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) {
1097 /*
1098 * INIT command returned an integrity check failure
1099 * status code, meaning that firmware load and
1100 * validation of SEV related persistent data has
1101 * failed and persistent state has been erased.
1102 * Retrying INIT command here should succeed.
1103 */
1104 dev_dbg(sp->dev, "SEV: retrying INIT command");
1105 rc = sev_platform_init(&error);
1106 }
1107
Brijesh Singh200664d2017-12-04 10:57:28 -06001108 if (rc) {
1109 dev_err(sp->dev, "SEV: failed to INIT error %#x\n", error);
Singh, Brijeshf5a2aeb82019-04-08 20:42:55 +00001110 return;
Brijesh Singh200664d2017-12-04 10:57:28 -06001111 }
1112
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -05001113 dev_info(sp->dev, "SEV API:%d.%d build:%d\n", psp_master->api_major,
1114 psp_master->api_minor, psp_master->build);
Brijesh Singh200664d2017-12-04 10:57:28 -06001115
Brijesh Singh200664d2017-12-04 10:57:28 -06001116 return;
1117
1118err:
1119 psp_master = NULL;
1120}
1121
1122void psp_pci_exit(void)
1123{
1124 if (!psp_master)
1125 return;
1126
1127 sev_platform_shutdown(NULL);
1128}