Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * AMD Secure Encrypted Virtualization (SEV) interface
4 *
5 * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
6 *
7 * Author: Brijesh Singh <brijesh.singh@amd.com>
8 */
9
10#include <linux/bitfield.h>
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/kthread.h>
14#include <linux/sched.h>
15#include <linux/interrupt.h>
16#include <linux/spinlock.h>
17#include <linux/spinlock_types.h>
18#include <linux/types.h>
19#include <linux/mutex.h>
20#include <linux/delay.h>
21#include <linux/hw_random.h>
22#include <linux/ccp.h>
23#include <linux/firmware.h>
24#include <linux/gfp.h>
25#include <linux/cpufeature.h>
26#include <linux/fs.h>
27#include <linux/fs_struct.h>
28#include <linux/psp.h>
29
30#include <asm/smp.h>
31#include <asm/cacheflush.h>
32
33#include "psp-dev.h"
34#include "sev-dev.h"
35
36#define DEVICE_NAME "sev"
37#define SEV_FW_FILE "amd/sev.fw"
38#define SEV_FW_NAME_SIZE 64
39
40static DEFINE_MUTEX(sev_cmd_mutex);
41static struct sev_misc_dev *misc_dev;
42
43static int psp_cmd_timeout = 100;
44module_param(psp_cmd_timeout, int, 0644);
45MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
46
47static int psp_probe_timeout = 5;
48module_param(psp_probe_timeout, int, 0644);
49MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
50
51static char *init_ex_path;
52module_param(init_ex_path, charp, 0444);
53MODULE_PARM_DESC(init_ex_path, " Path for INIT_EX data; if set try INIT_EX");
54
55static bool psp_init_on_probe = true;
56module_param(psp_init_on_probe, bool, 0444);
57MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
58
59MODULE_FIRMWARE("amd/amd_sev_fam17h_model0xh.sbin"); /* 1st gen EPYC */
60MODULE_FIRMWARE("amd/amd_sev_fam17h_model3xh.sbin"); /* 2nd gen EPYC */
61MODULE_FIRMWARE("amd/amd_sev_fam19h_model0xh.sbin"); /* 3rd gen EPYC */
62MODULE_FIRMWARE("amd/amd_sev_fam19h_model1xh.sbin"); /* 4th gen EPYC */
63
64static bool psp_dead;
65static int psp_timeout;
66
67/* Trusted Memory Region (TMR):
68 * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator
69 * to allocate the memory, which will return aligned memory for the specified
70 * allocation order.
71 */
72#define SEV_ES_TMR_SIZE (1024 * 1024)
73static void *sev_es_tmr;
74
75/* INIT_EX NV Storage:
76 * The NV Storage is a 32Kb area and must be 4Kb page aligned. Use the page
77 * allocator to allocate the memory, which will return aligned memory for the
78 * specified allocation order.
79 */
80#define NV_LENGTH (32 * 1024)
81static void *sev_init_ex_buffer;
82
83static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
84{
85 struct sev_device *sev = psp_master->sev_data;
86
87 if (sev->api_major > maj)
88 return true;
89
90 if (sev->api_major == maj && sev->api_minor >= min)
91 return true;
92
93 return false;
94}
95
96static void sev_irq_handler(int irq, void *data, unsigned int status)
97{
98 struct sev_device *sev = data;
99 int reg;
100
101 /* Check if it is command completion: */
102 if (!(status & SEV_CMD_COMPLETE))
103 return;
104
105 /* Check if it is SEV command completion: */
106 reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
107 if (FIELD_GET(PSP_CMDRESP_RESP, reg)) {
108 sev->int_rcvd = 1;
109 wake_up(&sev->int_queue);
110 }
111}
112
113static int sev_wait_cmd_ioc(struct sev_device *sev,
114 unsigned int *reg, unsigned int timeout)
115{
116 int ret;
117
118 ret = wait_event_timeout(sev->int_queue,
119 sev->int_rcvd, timeout * HZ);
120 if (!ret)
121 return -ETIMEDOUT;
122
123 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
124
125 return 0;
126}
127
128static int sev_cmd_buffer_len(int cmd)
129{
130 switch (cmd) {
131 case SEV_CMD_INIT: return sizeof(struct sev_data_init);
132 case SEV_CMD_INIT_EX: return sizeof(struct sev_data_init_ex);
133 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
134 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
135 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
136 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
137 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
138 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
139 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
140 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
141 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
142 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
143 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
144 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
145 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
146 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
147 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
148 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
149 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
150 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
151 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
152 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
153 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
154 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
155 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
156 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
157 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
158 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
159 case SEV_CMD_ATTESTATION_REPORT: return sizeof(struct sev_data_attestation_report);
160 case SEV_CMD_SEND_CANCEL: return sizeof(struct sev_data_send_cancel);
161 default: return 0;
162 }
163
164 return 0;
165}
166
167static void *sev_fw_alloc(unsigned long len)
168{
169 struct page *page;
170
171 page = alloc_pages(GFP_KERNEL, get_order(len));
172 if (!page)
173 return NULL;
174
175 return page_address(page);
176}
177
178static struct file *open_file_as_root(const char *filename, int flags, umode_t mode)
179{
180 struct file *fp;
181 struct path root;
182 struct cred *cred;
183 const struct cred *old_cred;
184
185 task_lock(&init_task);
186 get_fs_root(init_task.fs, &root);
187 task_unlock(&init_task);
188
189 cred = prepare_creds();
190 if (!cred)
191 return ERR_PTR(-ENOMEM);
192 cred->fsuid = GLOBAL_ROOT_UID;
193 old_cred = override_creds(cred);
194
195 fp = file_open_root(&root, filename, flags, mode);
196 path_put(&root);
197
198 revert_creds(old_cred);
199
200 return fp;
201}
202
203static int sev_read_init_ex_file(void)
204{
205 struct sev_device *sev = psp_master->sev_data;
206 struct file *fp;
207 ssize_t nread;
208
209 lockdep_assert_held(&sev_cmd_mutex);
210
211 if (!sev_init_ex_buffer)
212 return -EOPNOTSUPP;
213
214 fp = open_file_as_root(init_ex_path, O_RDONLY, 0);
215 if (IS_ERR(fp)) {
216 int ret = PTR_ERR(fp);
217
218 if (ret == -ENOENT) {
219 dev_info(sev->dev,
220 "SEV: %s does not exist and will be created later.\n",
221 init_ex_path);
222 ret = 0;
223 } else {
224 dev_err(sev->dev,
225 "SEV: could not open %s for read, error %d\n",
226 init_ex_path, ret);
227 }
228 return ret;
229 }
230
231 nread = kernel_read(fp, sev_init_ex_buffer, NV_LENGTH, NULL);
232 if (nread != NV_LENGTH) {
233 dev_info(sev->dev,
234 "SEV: could not read %u bytes to non volatile memory area, ret %ld\n",
235 NV_LENGTH, nread);
236 }
237
238 dev_dbg(sev->dev, "SEV: read %ld bytes from NV file\n", nread);
239 filp_close(fp, NULL);
240
241 return 0;
242}
243
244static int sev_write_init_ex_file(void)
245{
246 struct sev_device *sev = psp_master->sev_data;
247 struct file *fp;
248 loff_t offset = 0;
249 ssize_t nwrite;
250
251 lockdep_assert_held(&sev_cmd_mutex);
252
253 if (!sev_init_ex_buffer)
254 return 0;
255
256 fp = open_file_as_root(init_ex_path, O_CREAT | O_WRONLY, 0600);
257 if (IS_ERR(fp)) {
258 int ret = PTR_ERR(fp);
259
260 dev_err(sev->dev,
261 "SEV: could not open file for write, error %d\n",
262 ret);
263 return ret;
264 }
265
266 nwrite = kernel_write(fp, sev_init_ex_buffer, NV_LENGTH, &offset);
267 vfs_fsync(fp, 0);
268 filp_close(fp, NULL);
269
270 if (nwrite != NV_LENGTH) {
271 dev_err(sev->dev,
272 "SEV: failed to write %u bytes to non volatile memory area, ret %ld\n",
273 NV_LENGTH, nwrite);
274 return -EIO;
275 }
276
277 dev_dbg(sev->dev, "SEV: write successful to NV file\n");
278
279 return 0;
280}
281
282static int sev_write_init_ex_file_if_required(int cmd_id)
283{
284 lockdep_assert_held(&sev_cmd_mutex);
285
286 if (!sev_init_ex_buffer)
287 return 0;
288
289 /*
290 * Only a few platform commands modify the SPI/NV area, but none of the
291 * non-platform commands do. Only INIT(_EX), PLATFORM_RESET, PEK_GEN,
292 * PEK_CERT_IMPORT, and PDH_GEN do.
293 */
294 switch (cmd_id) {
295 case SEV_CMD_FACTORY_RESET:
296 case SEV_CMD_INIT_EX:
297 case SEV_CMD_PDH_GEN:
298 case SEV_CMD_PEK_CERT_IMPORT:
299 case SEV_CMD_PEK_GEN:
300 break;
301 default:
302 return 0;
303 }
304
305 return sev_write_init_ex_file();
306}
307
308static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
309{
310 struct psp_device *psp = psp_master;
311 struct sev_device *sev;
312 unsigned int cmdbuff_hi, cmdbuff_lo;
313 unsigned int phys_lsb, phys_msb;
314 unsigned int reg, ret = 0;
315 int buf_len;
316
317 if (!psp || !psp->sev_data)
318 return -ENODEV;
319
320 if (psp_dead)
321 return -EBUSY;
322
323 sev = psp->sev_data;
324
325 buf_len = sev_cmd_buffer_len(cmd);
326 if (WARN_ON_ONCE(!data != !buf_len))
327 return -EINVAL;
328
329 /*
330 * Copy the incoming data to driver's scratch buffer as __pa() will not
331 * work for some memory, e.g. vmalloc'd addresses, and @data may not be
332 * physically contiguous.
333 */
334 if (data)
335 memcpy(sev->cmd_buf, data, buf_len);
336
337 /* Get the physical address of the command buffer */
338 phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0;
339 phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0;
340
341 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
342 cmd, phys_msb, phys_lsb, psp_timeout);
343
344 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
345 buf_len, false);
346
347 iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
348 iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
349
350 sev->int_rcvd = 0;
351
352 reg = FIELD_PREP(SEV_CMDRESP_CMD, cmd) | SEV_CMDRESP_IOC;
353 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
354
355 /* wait for command completion */
356 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout);
357 if (ret) {
358 if (psp_ret)
359 *psp_ret = 0;
360
361 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
362 psp_dead = true;
363
364 return ret;
365 }
366
367 psp_timeout = psp_cmd_timeout;
368
369 if (psp_ret)
370 *psp_ret = FIELD_GET(PSP_CMDRESP_STS, reg);
371
372 if (FIELD_GET(PSP_CMDRESP_STS, reg)) {
373 dev_dbg(sev->dev, "sev command %#x failed (%#010lx)\n",
374 cmd, FIELD_GET(PSP_CMDRESP_STS, reg));
375
376 /*
377 * PSP firmware may report additional error information in the
378 * command buffer registers on error. Print contents of command
379 * buffer registers if they changed.
380 */
381 cmdbuff_hi = ioread32(sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
382 cmdbuff_lo = ioread32(sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
383 if (cmdbuff_hi != phys_msb || cmdbuff_lo != phys_lsb) {
384 dev_dbg(sev->dev, "Additional error information reported in cmdbuff:");
385 dev_dbg(sev->dev, " cmdbuff hi: %#010x\n", cmdbuff_hi);
386 dev_dbg(sev->dev, " cmdbuff lo: %#010x\n", cmdbuff_lo);
387 }
388 ret = -EIO;
389 } else {
390 ret = sev_write_init_ex_file_if_required(cmd);
391 }
392
393 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
394 buf_len, false);
395
396 /*
397 * Copy potential output from the PSP back to data. Do this even on
398 * failure in case the caller wants to glean something from the error.
399 */
400 if (data)
401 memcpy(data, sev->cmd_buf, buf_len);
402
403 return ret;
404}
405
406static int sev_do_cmd(int cmd, void *data, int *psp_ret)
407{
408 int rc;
409
410 mutex_lock(&sev_cmd_mutex);
411 rc = __sev_do_cmd_locked(cmd, data, psp_ret);
412 mutex_unlock(&sev_cmd_mutex);
413
414 return rc;
415}
416
417static int __sev_init_locked(int *error)
418{
419 struct sev_data_init data;
420
421 memset(&data, 0, sizeof(data));
422 if (sev_es_tmr) {
423 /*
424 * Do not include the encryption mask on the physical
425 * address of the TMR (firmware should clear it anyway).
426 */
427 data.tmr_address = __pa(sev_es_tmr);
428
429 data.flags |= SEV_INIT_FLAGS_SEV_ES;
430 data.tmr_len = SEV_ES_TMR_SIZE;
431 }
432
433 return __sev_do_cmd_locked(SEV_CMD_INIT, &data, error);
434}
435
436static int __sev_init_ex_locked(int *error)
437{
438 struct sev_data_init_ex data;
439
440 memset(&data, 0, sizeof(data));
441 data.length = sizeof(data);
442 data.nv_address = __psp_pa(sev_init_ex_buffer);
443 data.nv_len = NV_LENGTH;
444
445 if (sev_es_tmr) {
446 /*
447 * Do not include the encryption mask on the physical
448 * address of the TMR (firmware should clear it anyway).
449 */
450 data.tmr_address = __pa(sev_es_tmr);
451
452 data.flags |= SEV_INIT_FLAGS_SEV_ES;
453 data.tmr_len = SEV_ES_TMR_SIZE;
454 }
455
456 return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error);
457}
458
459static inline int __sev_do_init_locked(int *psp_ret)
460{
461 if (sev_init_ex_buffer)
462 return __sev_init_ex_locked(psp_ret);
463 else
464 return __sev_init_locked(psp_ret);
465}
466
467static int __sev_platform_init_locked(int *error)
468{
469 int rc = 0, psp_ret = SEV_RET_NO_FW_CALL;
470 struct psp_device *psp = psp_master;
471 struct sev_device *sev;
472
473 if (!psp || !psp->sev_data)
474 return -ENODEV;
475
476 sev = psp->sev_data;
477
478 if (sev->state == SEV_STATE_INIT)
479 return 0;
480
481 if (sev_init_ex_buffer) {
482 rc = sev_read_init_ex_file();
483 if (rc)
484 return rc;
485 }
486
487 rc = __sev_do_init_locked(&psp_ret);
488 if (rc && psp_ret == SEV_RET_SECURE_DATA_INVALID) {
489 /*
490 * Initialization command returned an integrity check failure
491 * status code, meaning that firmware load and validation of SEV
492 * related persistent data has failed. Retrying the
493 * initialization function should succeed by replacing the state
494 * with a reset state.
495 */
496 dev_err(sev->dev,
497"SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state.");
498 rc = __sev_do_init_locked(&psp_ret);
499 }
500
501 if (error)
502 *error = psp_ret;
503
504 if (rc)
505 return rc;
506
507 sev->state = SEV_STATE_INIT;
508
509 /* Prepare for first SEV guest launch after INIT */
510 wbinvd_on_all_cpus();
511 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
512 if (rc)
513 return rc;
514
515 dev_dbg(sev->dev, "SEV firmware initialized\n");
516
517 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
518 sev->api_minor, sev->build);
519
520 return 0;
521}
522
523int sev_platform_init(int *error)
524{
525 int rc;
526
527 mutex_lock(&sev_cmd_mutex);
528 rc = __sev_platform_init_locked(error);
529 mutex_unlock(&sev_cmd_mutex);
530
531 return rc;
532}
533EXPORT_SYMBOL_GPL(sev_platform_init);
534
535static int __sev_platform_shutdown_locked(int *error)
536{
537 struct psp_device *psp = psp_master;
538 struct sev_device *sev;
539 int ret;
540
541 if (!psp || !psp->sev_data)
542 return 0;
543
544 sev = psp->sev_data;
545
546 if (sev->state == SEV_STATE_UNINIT)
547 return 0;
548
549 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
550 if (ret)
551 return ret;
552
553 sev->state = SEV_STATE_UNINIT;
554 dev_dbg(sev->dev, "SEV firmware shutdown\n");
555
556 return ret;
557}
558
559static int sev_platform_shutdown(int *error)
560{
561 int rc;
562
563 mutex_lock(&sev_cmd_mutex);
564 rc = __sev_platform_shutdown_locked(NULL);
565 mutex_unlock(&sev_cmd_mutex);
566
567 return rc;
568}
569
570static int sev_get_platform_state(int *state, int *error)
571{
572 struct sev_user_data_status data;
573 int rc;
574
575 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, error);
576 if (rc)
577 return rc;
578
579 *state = data.state;
580 return rc;
581}
582
583static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
584{
585 int state, rc;
586
587 if (!writable)
588 return -EPERM;
589
590 /*
591 * The SEV spec requires that FACTORY_RESET must be issued in
592 * UNINIT state. Before we go further lets check if any guest is
593 * active.
594 *
595 * If FW is in WORKING state then deny the request otherwise issue
596 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
597 *
598 */
599 rc = sev_get_platform_state(&state, &argp->error);
600 if (rc)
601 return rc;
602
603 if (state == SEV_STATE_WORKING)
604 return -EBUSY;
605
606 if (state == SEV_STATE_INIT) {
607 rc = __sev_platform_shutdown_locked(&argp->error);
608 if (rc)
609 return rc;
610 }
611
612 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
613}
614
615static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
616{
617 struct sev_user_data_status data;
618 int ret;
619
620 memset(&data, 0, sizeof(data));
621
622 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, &argp->error);
623 if (ret)
624 return ret;
625
626 if (copy_to_user((void __user *)argp->data, &data, sizeof(data)))
627 ret = -EFAULT;
628
629 return ret;
630}
631
632static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
633{
634 struct sev_device *sev = psp_master->sev_data;
635 int rc;
636
637 if (!writable)
638 return -EPERM;
639
640 if (sev->state == SEV_STATE_UNINIT) {
641 rc = __sev_platform_init_locked(&argp->error);
642 if (rc)
643 return rc;
644 }
645
646 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
647}
648
649static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
650{
651 struct sev_device *sev = psp_master->sev_data;
652 struct sev_user_data_pek_csr input;
653 struct sev_data_pek_csr data;
654 void __user *input_address;
655 void *blob = NULL;
656 int ret;
657
658 if (!writable)
659 return -EPERM;
660
661 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
662 return -EFAULT;
663
664 memset(&data, 0, sizeof(data));
665
666 /* userspace wants to query CSR length */
667 if (!input.address || !input.length)
668 goto cmd;
669
670 /* allocate a physically contiguous buffer to store the CSR blob */
671 input_address = (void __user *)input.address;
672 if (input.length > SEV_FW_BLOB_MAX_SIZE)
673 return -EFAULT;
674
675 blob = kzalloc(input.length, GFP_KERNEL);
676 if (!blob)
677 return -ENOMEM;
678
679 data.address = __psp_pa(blob);
680 data.len = input.length;
681
682cmd:
683 if (sev->state == SEV_STATE_UNINIT) {
684 ret = __sev_platform_init_locked(&argp->error);
685 if (ret)
686 goto e_free_blob;
687 }
688
689 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error);
690
691 /* If we query the CSR length, FW responded with expected data. */
692 input.length = data.len;
693
694 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
695 ret = -EFAULT;
696 goto e_free_blob;
697 }
698
699 if (blob) {
700 if (copy_to_user(input_address, blob, input.length))
701 ret = -EFAULT;
702 }
703
704e_free_blob:
705 kfree(blob);
706 return ret;
707}
708
709void *psp_copy_user_blob(u64 uaddr, u32 len)
710{
711 if (!uaddr || !len)
712 return ERR_PTR(-EINVAL);
713
714 /* verify that blob length does not exceed our limit */
715 if (len > SEV_FW_BLOB_MAX_SIZE)
716 return ERR_PTR(-EINVAL);
717
718 return memdup_user((void __user *)uaddr, len);
719}
720EXPORT_SYMBOL_GPL(psp_copy_user_blob);
721
722static int sev_get_api_version(void)
723{
724 struct sev_device *sev = psp_master->sev_data;
725 struct sev_user_data_status status;
726 int error = 0, ret;
727
728 ret = sev_platform_status(&status, &error);
729 if (ret) {
730 dev_err(sev->dev,
731 "SEV: failed to get status. Error: %#x\n", error);
732 return 1;
733 }
734
735 sev->api_major = status.api_major;
736 sev->api_minor = status.api_minor;
737 sev->build = status.build;
738 sev->state = status.state;
739
740 return 0;
741}
742
743static int sev_get_firmware(struct device *dev,
744 const struct firmware **firmware)
745{
746 char fw_name_specific[SEV_FW_NAME_SIZE];
747 char fw_name_subset[SEV_FW_NAME_SIZE];
748
749 snprintf(fw_name_specific, sizeof(fw_name_specific),
750 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
751 boot_cpu_data.x86, boot_cpu_data.x86_model);
752
753 snprintf(fw_name_subset, sizeof(fw_name_subset),
754 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
755 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
756
757 /* Check for SEV FW for a particular model.
758 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
759 *
760 * or
761 *
762 * Check for SEV FW common to a subset of models.
763 * Ex. amd_sev_fam17h_model0xh.sbin for
764 * Family 17h Model 00h -- Family 17h Model 0Fh
765 *
766 * or
767 *
768 * Fall-back to using generic name: sev.fw
769 */
770 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
771 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
772 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
773 return 0;
774
775 return -ENOENT;
776}
777
778/* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
779static int sev_update_firmware(struct device *dev)
780{
781 struct sev_data_download_firmware *data;
782 const struct firmware *firmware;
783 int ret, error, order;
784 struct page *p;
785 u64 data_size;
786
787 if (!sev_version_greater_or_equal(0, 15)) {
788 dev_dbg(dev, "DOWNLOAD_FIRMWARE not supported\n");
789 return -1;
790 }
791
792 if (sev_get_firmware(dev, &firmware) == -ENOENT) {
793 dev_dbg(dev, "No SEV firmware file present\n");
794 return -1;
795 }
796
797 /*
798 * SEV FW expects the physical address given to it to be 32
799 * byte aligned. Memory allocated has structure placed at the
800 * beginning followed by the firmware being passed to the SEV
801 * FW. Allocate enough memory for data structure + alignment
802 * padding + SEV FW.
803 */
804 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
805
806 order = get_order(firmware->size + data_size);
807 p = alloc_pages(GFP_KERNEL, order);
808 if (!p) {
809 ret = -1;
810 goto fw_err;
811 }
812
813 /*
814 * Copy firmware data to a kernel allocated contiguous
815 * memory region.
816 */
817 data = page_address(p);
818 memcpy(page_address(p) + data_size, firmware->data, firmware->size);
819
820 data->address = __psp_pa(page_address(p) + data_size);
821 data->len = firmware->size;
822
823 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
824
825 /*
826 * A quirk for fixing the committed TCB version, when upgrading from
827 * earlier firmware version than 1.50.
828 */
829 if (!ret && !sev_version_greater_or_equal(1, 50))
830 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
831
832 if (ret)
833 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
834 else
835 dev_info(dev, "SEV firmware update successful\n");
836
837 __free_pages(p, order);
838
839fw_err:
840 release_firmware(firmware);
841
842 return ret;
843}
844
845static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
846{
847 struct sev_device *sev = psp_master->sev_data;
848 struct sev_user_data_pek_cert_import input;
849 struct sev_data_pek_cert_import data;
850 void *pek_blob, *oca_blob;
851 int ret;
852
853 if (!writable)
854 return -EPERM;
855
856 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
857 return -EFAULT;
858
859 /* copy PEK certificate blobs from userspace */
860 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
861 if (IS_ERR(pek_blob))
862 return PTR_ERR(pek_blob);
863
864 data.reserved = 0;
865 data.pek_cert_address = __psp_pa(pek_blob);
866 data.pek_cert_len = input.pek_cert_len;
867
868 /* copy PEK certificate blobs from userspace */
869 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
870 if (IS_ERR(oca_blob)) {
871 ret = PTR_ERR(oca_blob);
872 goto e_free_pek;
873 }
874
875 data.oca_cert_address = __psp_pa(oca_blob);
876 data.oca_cert_len = input.oca_cert_len;
877
878 /* If platform is not in INIT state then transition it to INIT */
879 if (sev->state != SEV_STATE_INIT) {
880 ret = __sev_platform_init_locked(&argp->error);
881 if (ret)
882 goto e_free_oca;
883 }
884
885 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error);
886
887e_free_oca:
888 kfree(oca_blob);
889e_free_pek:
890 kfree(pek_blob);
891 return ret;
892}
893
894static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
895{
896 struct sev_user_data_get_id2 input;
897 struct sev_data_get_id data;
898 void __user *input_address;
899 void *id_blob = NULL;
900 int ret;
901
902 /* SEV GET_ID is available from SEV API v0.16 and up */
903 if (!sev_version_greater_or_equal(0, 16))
904 return -ENOTSUPP;
905
906 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
907 return -EFAULT;
908
909 input_address = (void __user *)input.address;
910
911 if (input.address && input.length) {
912 /*
913 * The length of the ID shouldn't be assumed by software since
914 * it may change in the future. The allocation size is limited
915 * to 1 << (PAGE_SHIFT + MAX_PAGE_ORDER) by the page allocator.
916 * If the allocation fails, simply return ENOMEM rather than
917 * warning in the kernel log.
918 */
919 id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN);
920 if (!id_blob)
921 return -ENOMEM;
922
923 data.address = __psp_pa(id_blob);
924 data.len = input.length;
925 } else {
926 data.address = 0;
927 data.len = 0;
928 }
929
930 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, &data, &argp->error);
931
932 /*
933 * Firmware will return the length of the ID value (either the minimum
934 * required length or the actual length written), return it to the user.
935 */
936 input.length = data.len;
937
938 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
939 ret = -EFAULT;
940 goto e_free;
941 }
942
943 if (id_blob) {
944 if (copy_to_user(input_address, id_blob, data.len)) {
945 ret = -EFAULT;
946 goto e_free;
947 }
948 }
949
950e_free:
951 kfree(id_blob);
952
953 return ret;
954}
955
956static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
957{
958 struct sev_data_get_id *data;
959 u64 data_size, user_size;
960 void *id_blob, *mem;
961 int ret;
962
963 /* SEV GET_ID available from SEV API v0.16 and up */
964 if (!sev_version_greater_or_equal(0, 16))
965 return -ENOTSUPP;
966
967 /* SEV FW expects the buffer it fills with the ID to be
968 * 8-byte aligned. Memory allocated should be enough to
969 * hold data structure + alignment padding + memory
970 * where SEV FW writes the ID.
971 */
972 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
973 user_size = sizeof(struct sev_user_data_get_id);
974
975 mem = kzalloc(data_size + user_size, GFP_KERNEL);
976 if (!mem)
977 return -ENOMEM;
978
979 data = mem;
980 id_blob = mem + data_size;
981
982 data->address = __psp_pa(id_blob);
983 data->len = user_size;
984
985 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
986 if (!ret) {
987 if (copy_to_user((void __user *)argp->data, id_blob, data->len))
988 ret = -EFAULT;
989 }
990
991 kfree(mem);
992
993 return ret;
994}
995
996static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
997{
998 struct sev_device *sev = psp_master->sev_data;
999 struct sev_user_data_pdh_cert_export input;
1000 void *pdh_blob = NULL, *cert_blob = NULL;
1001 struct sev_data_pdh_cert_export data;
1002 void __user *input_cert_chain_address;
1003 void __user *input_pdh_cert_address;
1004 int ret;
1005
1006 /* If platform is not in INIT state then transition it to INIT. */
1007 if (sev->state != SEV_STATE_INIT) {
1008 if (!writable)
1009 return -EPERM;
1010
1011 ret = __sev_platform_init_locked(&argp->error);
1012 if (ret)
1013 return ret;
1014 }
1015
1016 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
1017 return -EFAULT;
1018
1019 memset(&data, 0, sizeof(data));
1020
1021 /* Userspace wants to query the certificate length. */
1022 if (!input.pdh_cert_address ||
1023 !input.pdh_cert_len ||
1024 !input.cert_chain_address)
1025 goto cmd;
1026
1027 input_pdh_cert_address = (void __user *)input.pdh_cert_address;
1028 input_cert_chain_address = (void __user *)input.cert_chain_address;
1029
1030 /* Allocate a physically contiguous buffer to store the PDH blob. */
1031 if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE)
1032 return -EFAULT;
1033
1034 /* Allocate a physically contiguous buffer to store the cert chain blob. */
1035 if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE)
1036 return -EFAULT;
1037
1038 pdh_blob = kzalloc(input.pdh_cert_len, GFP_KERNEL);
1039 if (!pdh_blob)
1040 return -ENOMEM;
1041
1042 data.pdh_cert_address = __psp_pa(pdh_blob);
1043 data.pdh_cert_len = input.pdh_cert_len;
1044
1045 cert_blob = kzalloc(input.cert_chain_len, GFP_KERNEL);
1046 if (!cert_blob) {
1047 ret = -ENOMEM;
1048 goto e_free_pdh;
1049 }
1050
1051 data.cert_chain_address = __psp_pa(cert_blob);
1052 data.cert_chain_len = input.cert_chain_len;
1053
1054cmd:
1055 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error);
1056
1057 /* If we query the length, FW responded with expected data. */
1058 input.cert_chain_len = data.cert_chain_len;
1059 input.pdh_cert_len = data.pdh_cert_len;
1060
1061 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
1062 ret = -EFAULT;
1063 goto e_free_cert;
1064 }
1065
1066 if (pdh_blob) {
1067 if (copy_to_user(input_pdh_cert_address,
1068 pdh_blob, input.pdh_cert_len)) {
1069 ret = -EFAULT;
1070 goto e_free_cert;
1071 }
1072 }
1073
1074 if (cert_blob) {
1075 if (copy_to_user(input_cert_chain_address,
1076 cert_blob, input.cert_chain_len))
1077 ret = -EFAULT;
1078 }
1079
1080e_free_cert:
1081 kfree(cert_blob);
1082e_free_pdh:
1083 kfree(pdh_blob);
1084 return ret;
1085}
1086
1087static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
1088{
1089 void __user *argp = (void __user *)arg;
1090 struct sev_issue_cmd input;
1091 int ret = -EFAULT;
1092 bool writable = file->f_mode & FMODE_WRITE;
1093
1094 if (!psp_master || !psp_master->sev_data)
1095 return -ENODEV;
1096
1097 if (ioctl != SEV_ISSUE_CMD)
1098 return -EINVAL;
1099
1100 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
1101 return -EFAULT;
1102
1103 if (input.cmd > SEV_MAX)
1104 return -EINVAL;
1105
1106 mutex_lock(&sev_cmd_mutex);
1107
1108 switch (input.cmd) {
1109
1110 case SEV_FACTORY_RESET:
1111 ret = sev_ioctl_do_reset(&input, writable);
1112 break;
1113 case SEV_PLATFORM_STATUS:
1114 ret = sev_ioctl_do_platform_status(&input);
1115 break;
1116 case SEV_PEK_GEN:
1117 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable);
1118 break;
1119 case SEV_PDH_GEN:
1120 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable);
1121 break;
1122 case SEV_PEK_CSR:
1123 ret = sev_ioctl_do_pek_csr(&input, writable);
1124 break;
1125 case SEV_PEK_CERT_IMPORT:
1126 ret = sev_ioctl_do_pek_import(&input, writable);
1127 break;
1128 case SEV_PDH_CERT_EXPORT:
1129 ret = sev_ioctl_do_pdh_export(&input, writable);
1130 break;
1131 case SEV_GET_ID:
1132 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
1133 ret = sev_ioctl_do_get_id(&input);
1134 break;
1135 case SEV_GET_ID2:
1136 ret = sev_ioctl_do_get_id2(&input);
1137 break;
1138 default:
1139 ret = -EINVAL;
1140 goto out;
1141 }
1142
1143 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
1144 ret = -EFAULT;
1145out:
1146 mutex_unlock(&sev_cmd_mutex);
1147
1148 return ret;
1149}
1150
1151static const struct file_operations sev_fops = {
1152 .owner = THIS_MODULE,
1153 .unlocked_ioctl = sev_ioctl,
1154};
1155
1156int sev_platform_status(struct sev_user_data_status *data, int *error)
1157{
1158 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
1159}
1160EXPORT_SYMBOL_GPL(sev_platform_status);
1161
1162int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
1163{
1164 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
1165}
1166EXPORT_SYMBOL_GPL(sev_guest_deactivate);
1167
1168int sev_guest_activate(struct sev_data_activate *data, int *error)
1169{
1170 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
1171}
1172EXPORT_SYMBOL_GPL(sev_guest_activate);
1173
1174int sev_guest_decommission(struct sev_data_decommission *data, int *error)
1175{
1176 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
1177}
1178EXPORT_SYMBOL_GPL(sev_guest_decommission);
1179
1180int sev_guest_df_flush(int *error)
1181{
1182 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
1183}
1184EXPORT_SYMBOL_GPL(sev_guest_df_flush);
1185
1186static void sev_exit(struct kref *ref)
1187{
1188 misc_deregister(&misc_dev->misc);
1189 kfree(misc_dev);
1190 misc_dev = NULL;
1191}
1192
1193static int sev_misc_init(struct sev_device *sev)
1194{
1195 struct device *dev = sev->dev;
1196 int ret;
1197
1198 /*
1199 * SEV feature support can be detected on multiple devices but the SEV
1200 * FW commands must be issued on the master. During probe, we do not
1201 * know the master hence we create /dev/sev on the first device probe.
1202 * sev_do_cmd() finds the right master device to which to issue the
1203 * command to the firmware.
1204 */
1205 if (!misc_dev) {
1206 struct miscdevice *misc;
1207
1208 misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
1209 if (!misc_dev)
1210 return -ENOMEM;
1211
1212 misc = &misc_dev->misc;
1213 misc->minor = MISC_DYNAMIC_MINOR;
1214 misc->name = DEVICE_NAME;
1215 misc->fops = &sev_fops;
1216
1217 ret = misc_register(misc);
1218 if (ret)
1219 return ret;
1220
1221 kref_init(&misc_dev->refcount);
1222 } else {
1223 kref_get(&misc_dev->refcount);
1224 }
1225
1226 init_waitqueue_head(&sev->int_queue);
1227 sev->misc = misc_dev;
1228 dev_dbg(dev, "registered SEV device\n");
1229
1230 return 0;
1231}
1232
1233int sev_dev_init(struct psp_device *psp)
1234{
1235 struct device *dev = psp->dev;
1236 struct sev_device *sev;
1237 int ret = -ENOMEM;
1238
1239 if (!boot_cpu_has(X86_FEATURE_SEV)) {
1240 dev_info_once(dev, "SEV: memory encryption not enabled by BIOS\n");
1241 return 0;
1242 }
1243
1244 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
1245 if (!sev)
1246 goto e_err;
1247
1248 sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
1249 if (!sev->cmd_buf)
1250 goto e_sev;
1251
1252 psp->sev_data = sev;
1253
1254 sev->dev = dev;
1255 sev->psp = psp;
1256
1257 sev->io_regs = psp->io_regs;
1258
1259 sev->vdata = (struct sev_vdata *)psp->vdata->sev;
1260 if (!sev->vdata) {
1261 ret = -ENODEV;
1262 dev_err(dev, "sev: missing driver data\n");
1263 goto e_buf;
1264 }
1265
1266 psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
1267
1268 ret = sev_misc_init(sev);
1269 if (ret)
1270 goto e_irq;
1271
1272 dev_notice(dev, "sev enabled\n");
1273
1274 return 0;
1275
1276e_irq:
1277 psp_clear_sev_irq_handler(psp);
1278e_buf:
1279 devm_free_pages(dev, (unsigned long)sev->cmd_buf);
1280e_sev:
1281 devm_kfree(dev, sev);
1282e_err:
1283 psp->sev_data = NULL;
1284
1285 dev_notice(dev, "sev initialization failed\n");
1286
1287 return ret;
1288}
1289
1290static void sev_firmware_shutdown(struct sev_device *sev)
1291{
1292 sev_platform_shutdown(NULL);
1293
1294 if (sev_es_tmr) {
1295 /* The TMR area was encrypted, flush it from the cache */
1296 wbinvd_on_all_cpus();
1297
1298 free_pages((unsigned long)sev_es_tmr,
1299 get_order(SEV_ES_TMR_SIZE));
1300 sev_es_tmr = NULL;
1301 }
1302
1303 if (sev_init_ex_buffer) {
1304 free_pages((unsigned long)sev_init_ex_buffer,
1305 get_order(NV_LENGTH));
1306 sev_init_ex_buffer = NULL;
1307 }
1308}
1309
1310void sev_dev_destroy(struct psp_device *psp)
1311{
1312 struct sev_device *sev = psp->sev_data;
1313
1314 if (!sev)
1315 return;
1316
1317 sev_firmware_shutdown(sev);
1318
1319 if (sev->misc)
1320 kref_put(&misc_dev->refcount, sev_exit);
1321
1322 psp_clear_sev_irq_handler(psp);
1323}
1324
1325int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1326 void *data, int *error)
1327{
1328 if (!filep || filep->f_op != &sev_fops)
1329 return -EBADF;
1330
1331 return sev_do_cmd(cmd, data, error);
1332}
1333EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1334
1335void sev_pci_init(void)
1336{
1337 struct sev_device *sev = psp_master->sev_data;
1338 int error, rc;
1339
1340 if (!sev)
1341 return;
1342
1343 psp_timeout = psp_probe_timeout;
1344
1345 if (sev_get_api_version())
1346 goto err;
1347
1348 if (sev_update_firmware(sev->dev) == 0)
1349 sev_get_api_version();
1350
1351 /* If an init_ex_path is provided rely on INIT_EX for PSP initialization
1352 * instead of INIT.
1353 */
1354 if (init_ex_path) {
1355 sev_init_ex_buffer = sev_fw_alloc(NV_LENGTH);
1356 if (!sev_init_ex_buffer) {
1357 dev_err(sev->dev,
1358 "SEV: INIT_EX NV memory allocation failed\n");
1359 goto err;
1360 }
1361 }
1362
1363 /* Obtain the TMR memory area for SEV-ES use */
1364 sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE);
1365 if (sev_es_tmr)
1366 /* Must flush the cache before giving it to the firmware */
1367 clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE);
1368 else
1369 dev_warn(sev->dev,
1370 "SEV: TMR allocation failed, SEV-ES support unavailable\n");
1371
1372 if (!psp_init_on_probe)
1373 return;
1374
1375 /* Initialize the platform */
1376 rc = sev_platform_init(&error);
1377 if (rc)
1378 dev_err(sev->dev, "SEV: failed to INIT error %#x, rc %d\n",
1379 error, rc);
1380
1381 return;
1382
1383err:
1384 psp_master->sev_data = NULL;
1385}
1386
1387void sev_pci_exit(void)
1388{
1389 struct sev_device *sev = psp_master->sev_data;
1390
1391 if (!sev)
1392 return;
1393
1394 sev_firmware_shutdown(sev);
1395}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * AMD Secure Encrypted Virtualization (SEV) interface
4 *
5 * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
6 *
7 * Author: Brijesh Singh <brijesh.singh@amd.com>
8 */
9
10#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/kthread.h>
13#include <linux/sched.h>
14#include <linux/interrupt.h>
15#include <linux/spinlock.h>
16#include <linux/spinlock_types.h>
17#include <linux/types.h>
18#include <linux/mutex.h>
19#include <linux/delay.h>
20#include <linux/hw_random.h>
21#include <linux/ccp.h>
22#include <linux/firmware.h>
23#include <linux/gfp.h>
24
25#include <asm/smp.h>
26
27#include "psp-dev.h"
28#include "sev-dev.h"
29
30#define DEVICE_NAME "sev"
31#define SEV_FW_FILE "amd/sev.fw"
32#define SEV_FW_NAME_SIZE 64
33
34static DEFINE_MUTEX(sev_cmd_mutex);
35static struct sev_misc_dev *misc_dev;
36
37static 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
48/* Trusted Memory Region (TMR):
49 * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator
50 * to allocate the memory, which will return aligned memory for the specified
51 * allocation order.
52 */
53#define SEV_ES_TMR_SIZE (1024 * 1024)
54static void *sev_es_tmr;
55
56static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
57{
58 struct sev_device *sev = psp_master->sev_data;
59
60 if (sev->api_major > maj)
61 return true;
62
63 if (sev->api_major == maj && sev->api_minor >= min)
64 return true;
65
66 return false;
67}
68
69static void sev_irq_handler(int irq, void *data, unsigned int status)
70{
71 struct sev_device *sev = data;
72 int reg;
73
74 /* Check if it is command completion: */
75 if (!(status & SEV_CMD_COMPLETE))
76 return;
77
78 /* Check if it is SEV command completion: */
79 reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
80 if (reg & PSP_CMDRESP_RESP) {
81 sev->int_rcvd = 1;
82 wake_up(&sev->int_queue);
83 }
84}
85
86static int sev_wait_cmd_ioc(struct sev_device *sev,
87 unsigned int *reg, unsigned int timeout)
88{
89 int ret;
90
91 ret = wait_event_timeout(sev->int_queue,
92 sev->int_rcvd, timeout * HZ);
93 if (!ret)
94 return -ETIMEDOUT;
95
96 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
97
98 return 0;
99}
100
101static int sev_cmd_buffer_len(int cmd)
102{
103 switch (cmd) {
104 case SEV_CMD_INIT: return sizeof(struct sev_data_init);
105 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
106 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
107 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
108 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
109 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
110 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
111 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
112 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
113 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
114 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
115 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
116 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
117 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
118 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
119 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
120 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
121 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
122 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
123 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
124 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
125 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
126 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
127 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
128 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
129 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
130 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
131 default: return 0;
132 }
133
134 return 0;
135}
136
137static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
138{
139 struct psp_device *psp = psp_master;
140 struct sev_device *sev;
141 unsigned int phys_lsb, phys_msb;
142 unsigned int reg, ret = 0;
143
144 if (!psp || !psp->sev_data)
145 return -ENODEV;
146
147 if (psp_dead)
148 return -EBUSY;
149
150 sev = psp->sev_data;
151
152 /* Get the physical address of the command buffer */
153 phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0;
154 phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0;
155
156 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
157 cmd, phys_msb, phys_lsb, psp_timeout);
158
159 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
160 sev_cmd_buffer_len(cmd), false);
161
162 iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
163 iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
164
165 sev->int_rcvd = 0;
166
167 reg = cmd;
168 reg <<= SEV_CMDRESP_CMD_SHIFT;
169 reg |= SEV_CMDRESP_IOC;
170 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
171
172 /* wait for command completion */
173 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout);
174 if (ret) {
175 if (psp_ret)
176 *psp_ret = 0;
177
178 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
179 psp_dead = true;
180
181 return ret;
182 }
183
184 psp_timeout = psp_cmd_timeout;
185
186 if (psp_ret)
187 *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
188
189 if (reg & PSP_CMDRESP_ERR_MASK) {
190 dev_dbg(sev->dev, "sev command %#x failed (%#010x)\n",
191 cmd, reg & PSP_CMDRESP_ERR_MASK);
192 ret = -EIO;
193 }
194
195 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
196 sev_cmd_buffer_len(cmd), false);
197
198 return ret;
199}
200
201static int sev_do_cmd(int cmd, void *data, int *psp_ret)
202{
203 int rc;
204
205 mutex_lock(&sev_cmd_mutex);
206 rc = __sev_do_cmd_locked(cmd, data, psp_ret);
207 mutex_unlock(&sev_cmd_mutex);
208
209 return rc;
210}
211
212static int __sev_platform_init_locked(int *error)
213{
214 struct psp_device *psp = psp_master;
215 struct sev_device *sev;
216 int rc = 0;
217
218 if (!psp || !psp->sev_data)
219 return -ENODEV;
220
221 sev = psp->sev_data;
222
223 if (sev->state == SEV_STATE_INIT)
224 return 0;
225
226 if (sev_es_tmr) {
227 u64 tmr_pa;
228
229 /*
230 * Do not include the encryption mask on the physical
231 * address of the TMR (firmware should clear it anyway).
232 */
233 tmr_pa = __pa(sev_es_tmr);
234
235 sev->init_cmd_buf.flags |= SEV_INIT_FLAGS_SEV_ES;
236 sev->init_cmd_buf.tmr_address = tmr_pa;
237 sev->init_cmd_buf.tmr_len = SEV_ES_TMR_SIZE;
238 }
239
240 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &sev->init_cmd_buf, error);
241 if (rc)
242 return rc;
243
244 sev->state = SEV_STATE_INIT;
245
246 /* Prepare for first SEV guest launch after INIT */
247 wbinvd_on_all_cpus();
248 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
249 if (rc)
250 return rc;
251
252 dev_dbg(sev->dev, "SEV firmware initialized\n");
253
254 return rc;
255}
256
257int sev_platform_init(int *error)
258{
259 int rc;
260
261 mutex_lock(&sev_cmd_mutex);
262 rc = __sev_platform_init_locked(error);
263 mutex_unlock(&sev_cmd_mutex);
264
265 return rc;
266}
267EXPORT_SYMBOL_GPL(sev_platform_init);
268
269static int __sev_platform_shutdown_locked(int *error)
270{
271 struct sev_device *sev = psp_master->sev_data;
272 int ret;
273
274 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
275 if (ret)
276 return ret;
277
278 sev->state = SEV_STATE_UNINIT;
279 dev_dbg(sev->dev, "SEV firmware shutdown\n");
280
281 return ret;
282}
283
284static int sev_platform_shutdown(int *error)
285{
286 int rc;
287
288 mutex_lock(&sev_cmd_mutex);
289 rc = __sev_platform_shutdown_locked(NULL);
290 mutex_unlock(&sev_cmd_mutex);
291
292 return rc;
293}
294
295static int sev_get_platform_state(int *state, int *error)
296{
297 struct sev_device *sev = psp_master->sev_data;
298 int rc;
299
300 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
301 &sev->status_cmd_buf, error);
302 if (rc)
303 return rc;
304
305 *state = sev->status_cmd_buf.state;
306 return rc;
307}
308
309static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
310{
311 int state, rc;
312
313 if (!writable)
314 return -EPERM;
315
316 /*
317 * The SEV spec requires that FACTORY_RESET must be issued in
318 * UNINIT state. Before we go further lets check if any guest is
319 * active.
320 *
321 * If FW is in WORKING state then deny the request otherwise issue
322 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
323 *
324 */
325 rc = sev_get_platform_state(&state, &argp->error);
326 if (rc)
327 return rc;
328
329 if (state == SEV_STATE_WORKING)
330 return -EBUSY;
331
332 if (state == SEV_STATE_INIT) {
333 rc = __sev_platform_shutdown_locked(&argp->error);
334 if (rc)
335 return rc;
336 }
337
338 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
339}
340
341static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
342{
343 struct sev_device *sev = psp_master->sev_data;
344 struct sev_user_data_status *data = &sev->status_cmd_buf;
345 int ret;
346
347 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
348 if (ret)
349 return ret;
350
351 if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
352 ret = -EFAULT;
353
354 return ret;
355}
356
357static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
358{
359 struct sev_device *sev = psp_master->sev_data;
360 int rc;
361
362 if (!writable)
363 return -EPERM;
364
365 if (sev->state == SEV_STATE_UNINIT) {
366 rc = __sev_platform_init_locked(&argp->error);
367 if (rc)
368 return rc;
369 }
370
371 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
372}
373
374static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
375{
376 struct sev_device *sev = psp_master->sev_data;
377 struct sev_user_data_pek_csr input;
378 struct sev_data_pek_csr *data;
379 void __user *input_address;
380 void *blob = NULL;
381 int ret;
382
383 if (!writable)
384 return -EPERM;
385
386 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
387 return -EFAULT;
388
389 data = kzalloc(sizeof(*data), GFP_KERNEL);
390 if (!data)
391 return -ENOMEM;
392
393 /* userspace wants to query CSR length */
394 if (!input.address || !input.length)
395 goto cmd;
396
397 /* allocate a physically contiguous buffer to store the CSR blob */
398 input_address = (void __user *)input.address;
399 if (input.length > SEV_FW_BLOB_MAX_SIZE) {
400 ret = -EFAULT;
401 goto e_free;
402 }
403
404 blob = kmalloc(input.length, GFP_KERNEL);
405 if (!blob) {
406 ret = -ENOMEM;
407 goto e_free;
408 }
409
410 data->address = __psp_pa(blob);
411 data->len = input.length;
412
413cmd:
414 if (sev->state == SEV_STATE_UNINIT) {
415 ret = __sev_platform_init_locked(&argp->error);
416 if (ret)
417 goto e_free_blob;
418 }
419
420 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
421
422 /* If we query the CSR length, FW responded with expected data. */
423 input.length = data->len;
424
425 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
426 ret = -EFAULT;
427 goto e_free_blob;
428 }
429
430 if (blob) {
431 if (copy_to_user(input_address, blob, input.length))
432 ret = -EFAULT;
433 }
434
435e_free_blob:
436 kfree(blob);
437e_free:
438 kfree(data);
439 return ret;
440}
441
442void *psp_copy_user_blob(u64 uaddr, u32 len)
443{
444 if (!uaddr || !len)
445 return ERR_PTR(-EINVAL);
446
447 /* verify that blob length does not exceed our limit */
448 if (len > SEV_FW_BLOB_MAX_SIZE)
449 return ERR_PTR(-EINVAL);
450
451 return memdup_user((void __user *)uaddr, len);
452}
453EXPORT_SYMBOL_GPL(psp_copy_user_blob);
454
455static int sev_get_api_version(void)
456{
457 struct sev_device *sev = psp_master->sev_data;
458 struct sev_user_data_status *status;
459 int error = 0, ret;
460
461 status = &sev->status_cmd_buf;
462 ret = sev_platform_status(status, &error);
463 if (ret) {
464 dev_err(sev->dev,
465 "SEV: failed to get status. Error: %#x\n", error);
466 return 1;
467 }
468
469 sev->api_major = status->api_major;
470 sev->api_minor = status->api_minor;
471 sev->build = status->build;
472 sev->state = status->state;
473
474 return 0;
475}
476
477static int sev_get_firmware(struct device *dev,
478 const struct firmware **firmware)
479{
480 char fw_name_specific[SEV_FW_NAME_SIZE];
481 char fw_name_subset[SEV_FW_NAME_SIZE];
482
483 snprintf(fw_name_specific, sizeof(fw_name_specific),
484 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
485 boot_cpu_data.x86, boot_cpu_data.x86_model);
486
487 snprintf(fw_name_subset, sizeof(fw_name_subset),
488 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
489 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
490
491 /* Check for SEV FW for a particular model.
492 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
493 *
494 * or
495 *
496 * Check for SEV FW common to a subset of models.
497 * Ex. amd_sev_fam17h_model0xh.sbin for
498 * Family 17h Model 00h -- Family 17h Model 0Fh
499 *
500 * or
501 *
502 * Fall-back to using generic name: sev.fw
503 */
504 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
505 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
506 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
507 return 0;
508
509 return -ENOENT;
510}
511
512/* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
513static int sev_update_firmware(struct device *dev)
514{
515 struct sev_data_download_firmware *data;
516 const struct firmware *firmware;
517 int ret, error, order;
518 struct page *p;
519 u64 data_size;
520
521 if (sev_get_firmware(dev, &firmware) == -ENOENT) {
522 dev_dbg(dev, "No SEV firmware file present\n");
523 return -1;
524 }
525
526 /*
527 * SEV FW expects the physical address given to it to be 32
528 * byte aligned. Memory allocated has structure placed at the
529 * beginning followed by the firmware being passed to the SEV
530 * FW. Allocate enough memory for data structure + alignment
531 * padding + SEV FW.
532 */
533 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
534
535 order = get_order(firmware->size + data_size);
536 p = alloc_pages(GFP_KERNEL, order);
537 if (!p) {
538 ret = -1;
539 goto fw_err;
540 }
541
542 /*
543 * Copy firmware data to a kernel allocated contiguous
544 * memory region.
545 */
546 data = page_address(p);
547 memcpy(page_address(p) + data_size, firmware->data, firmware->size);
548
549 data->address = __psp_pa(page_address(p) + data_size);
550 data->len = firmware->size;
551
552 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
553 if (ret)
554 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
555 else
556 dev_info(dev, "SEV firmware update successful\n");
557
558 __free_pages(p, order);
559
560fw_err:
561 release_firmware(firmware);
562
563 return ret;
564}
565
566static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
567{
568 struct sev_device *sev = psp_master->sev_data;
569 struct sev_user_data_pek_cert_import input;
570 struct sev_data_pek_cert_import *data;
571 void *pek_blob, *oca_blob;
572 int ret;
573
574 if (!writable)
575 return -EPERM;
576
577 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
578 return -EFAULT;
579
580 data = kzalloc(sizeof(*data), GFP_KERNEL);
581 if (!data)
582 return -ENOMEM;
583
584 /* copy PEK certificate blobs from userspace */
585 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
586 if (IS_ERR(pek_blob)) {
587 ret = PTR_ERR(pek_blob);
588 goto e_free;
589 }
590
591 data->pek_cert_address = __psp_pa(pek_blob);
592 data->pek_cert_len = input.pek_cert_len;
593
594 /* copy PEK certificate blobs from userspace */
595 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
596 if (IS_ERR(oca_blob)) {
597 ret = PTR_ERR(oca_blob);
598 goto e_free_pek;
599 }
600
601 data->oca_cert_address = __psp_pa(oca_blob);
602 data->oca_cert_len = input.oca_cert_len;
603
604 /* If platform is not in INIT state then transition it to INIT */
605 if (sev->state != SEV_STATE_INIT) {
606 ret = __sev_platform_init_locked(&argp->error);
607 if (ret)
608 goto e_free_oca;
609 }
610
611 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
612
613e_free_oca:
614 kfree(oca_blob);
615e_free_pek:
616 kfree(pek_blob);
617e_free:
618 kfree(data);
619 return ret;
620}
621
622static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
623{
624 struct sev_user_data_get_id2 input;
625 struct sev_data_get_id *data;
626 void __user *input_address;
627 void *id_blob = NULL;
628 int ret;
629
630 /* SEV GET_ID is available from SEV API v0.16 and up */
631 if (!sev_version_greater_or_equal(0, 16))
632 return -ENOTSUPP;
633
634 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
635 return -EFAULT;
636
637 input_address = (void __user *)input.address;
638
639 data = kzalloc(sizeof(*data), GFP_KERNEL);
640 if (!data)
641 return -ENOMEM;
642
643 if (input.address && input.length) {
644 id_blob = kmalloc(input.length, GFP_KERNEL);
645 if (!id_blob) {
646 kfree(data);
647 return -ENOMEM;
648 }
649
650 data->address = __psp_pa(id_blob);
651 data->len = input.length;
652 }
653
654 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
655
656 /*
657 * Firmware will return the length of the ID value (either the minimum
658 * required length or the actual length written), return it to the user.
659 */
660 input.length = data->len;
661
662 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
663 ret = -EFAULT;
664 goto e_free;
665 }
666
667 if (id_blob) {
668 if (copy_to_user(input_address, id_blob, data->len)) {
669 ret = -EFAULT;
670 goto e_free;
671 }
672 }
673
674e_free:
675 kfree(id_blob);
676 kfree(data);
677
678 return ret;
679}
680
681static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
682{
683 struct sev_data_get_id *data;
684 u64 data_size, user_size;
685 void *id_blob, *mem;
686 int ret;
687
688 /* SEV GET_ID available from SEV API v0.16 and up */
689 if (!sev_version_greater_or_equal(0, 16))
690 return -ENOTSUPP;
691
692 /* SEV FW expects the buffer it fills with the ID to be
693 * 8-byte aligned. Memory allocated should be enough to
694 * hold data structure + alignment padding + memory
695 * where SEV FW writes the ID.
696 */
697 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
698 user_size = sizeof(struct sev_user_data_get_id);
699
700 mem = kzalloc(data_size + user_size, GFP_KERNEL);
701 if (!mem)
702 return -ENOMEM;
703
704 data = mem;
705 id_blob = mem + data_size;
706
707 data->address = __psp_pa(id_blob);
708 data->len = user_size;
709
710 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
711 if (!ret) {
712 if (copy_to_user((void __user *)argp->data, id_blob, data->len))
713 ret = -EFAULT;
714 }
715
716 kfree(mem);
717
718 return ret;
719}
720
721static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
722{
723 struct sev_device *sev = psp_master->sev_data;
724 struct sev_user_data_pdh_cert_export input;
725 void *pdh_blob = NULL, *cert_blob = NULL;
726 struct sev_data_pdh_cert_export *data;
727 void __user *input_cert_chain_address;
728 void __user *input_pdh_cert_address;
729 int ret;
730
731 /* If platform is not in INIT state then transition it to INIT. */
732 if (sev->state != SEV_STATE_INIT) {
733 if (!writable)
734 return -EPERM;
735
736 ret = __sev_platform_init_locked(&argp->error);
737 if (ret)
738 return ret;
739 }
740
741 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
742 return -EFAULT;
743
744 data = kzalloc(sizeof(*data), GFP_KERNEL);
745 if (!data)
746 return -ENOMEM;
747
748 /* Userspace wants to query the certificate length. */
749 if (!input.pdh_cert_address ||
750 !input.pdh_cert_len ||
751 !input.cert_chain_address)
752 goto cmd;
753
754 input_pdh_cert_address = (void __user *)input.pdh_cert_address;
755 input_cert_chain_address = (void __user *)input.cert_chain_address;
756
757 /* Allocate a physically contiguous buffer to store the PDH blob. */
758 if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) {
759 ret = -EFAULT;
760 goto e_free;
761 }
762
763 /* Allocate a physically contiguous buffer to store the cert chain blob. */
764 if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) {
765 ret = -EFAULT;
766 goto e_free;
767 }
768
769 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
770 if (!pdh_blob) {
771 ret = -ENOMEM;
772 goto e_free;
773 }
774
775 data->pdh_cert_address = __psp_pa(pdh_blob);
776 data->pdh_cert_len = input.pdh_cert_len;
777
778 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
779 if (!cert_blob) {
780 ret = -ENOMEM;
781 goto e_free_pdh;
782 }
783
784 data->cert_chain_address = __psp_pa(cert_blob);
785 data->cert_chain_len = input.cert_chain_len;
786
787cmd:
788 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
789
790 /* If we query the length, FW responded with expected data. */
791 input.cert_chain_len = data->cert_chain_len;
792 input.pdh_cert_len = data->pdh_cert_len;
793
794 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
795 ret = -EFAULT;
796 goto e_free_cert;
797 }
798
799 if (pdh_blob) {
800 if (copy_to_user(input_pdh_cert_address,
801 pdh_blob, input.pdh_cert_len)) {
802 ret = -EFAULT;
803 goto e_free_cert;
804 }
805 }
806
807 if (cert_blob) {
808 if (copy_to_user(input_cert_chain_address,
809 cert_blob, input.cert_chain_len))
810 ret = -EFAULT;
811 }
812
813e_free_cert:
814 kfree(cert_blob);
815e_free_pdh:
816 kfree(pdh_blob);
817e_free:
818 kfree(data);
819 return ret;
820}
821
822static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
823{
824 void __user *argp = (void __user *)arg;
825 struct sev_issue_cmd input;
826 int ret = -EFAULT;
827 bool writable = file->f_mode & FMODE_WRITE;
828
829 if (!psp_master || !psp_master->sev_data)
830 return -ENODEV;
831
832 if (ioctl != SEV_ISSUE_CMD)
833 return -EINVAL;
834
835 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
836 return -EFAULT;
837
838 if (input.cmd > SEV_MAX)
839 return -EINVAL;
840
841 mutex_lock(&sev_cmd_mutex);
842
843 switch (input.cmd) {
844
845 case SEV_FACTORY_RESET:
846 ret = sev_ioctl_do_reset(&input, writable);
847 break;
848 case SEV_PLATFORM_STATUS:
849 ret = sev_ioctl_do_platform_status(&input);
850 break;
851 case SEV_PEK_GEN:
852 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable);
853 break;
854 case SEV_PDH_GEN:
855 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable);
856 break;
857 case SEV_PEK_CSR:
858 ret = sev_ioctl_do_pek_csr(&input, writable);
859 break;
860 case SEV_PEK_CERT_IMPORT:
861 ret = sev_ioctl_do_pek_import(&input, writable);
862 break;
863 case SEV_PDH_CERT_EXPORT:
864 ret = sev_ioctl_do_pdh_export(&input, writable);
865 break;
866 case SEV_GET_ID:
867 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
868 ret = sev_ioctl_do_get_id(&input);
869 break;
870 case SEV_GET_ID2:
871 ret = sev_ioctl_do_get_id2(&input);
872 break;
873 default:
874 ret = -EINVAL;
875 goto out;
876 }
877
878 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
879 ret = -EFAULT;
880out:
881 mutex_unlock(&sev_cmd_mutex);
882
883 return ret;
884}
885
886static const struct file_operations sev_fops = {
887 .owner = THIS_MODULE,
888 .unlocked_ioctl = sev_ioctl,
889};
890
891int sev_platform_status(struct sev_user_data_status *data, int *error)
892{
893 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
894}
895EXPORT_SYMBOL_GPL(sev_platform_status);
896
897int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
898{
899 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
900}
901EXPORT_SYMBOL_GPL(sev_guest_deactivate);
902
903int sev_guest_activate(struct sev_data_activate *data, int *error)
904{
905 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
906}
907EXPORT_SYMBOL_GPL(sev_guest_activate);
908
909int sev_guest_decommission(struct sev_data_decommission *data, int *error)
910{
911 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
912}
913EXPORT_SYMBOL_GPL(sev_guest_decommission);
914
915int sev_guest_df_flush(int *error)
916{
917 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
918}
919EXPORT_SYMBOL_GPL(sev_guest_df_flush);
920
921static void sev_exit(struct kref *ref)
922{
923 misc_deregister(&misc_dev->misc);
924 kfree(misc_dev);
925 misc_dev = NULL;
926}
927
928static int sev_misc_init(struct sev_device *sev)
929{
930 struct device *dev = sev->dev;
931 int ret;
932
933 /*
934 * SEV feature support can be detected on multiple devices but the SEV
935 * FW commands must be issued on the master. During probe, we do not
936 * know the master hence we create /dev/sev on the first device probe.
937 * sev_do_cmd() finds the right master device to which to issue the
938 * command to the firmware.
939 */
940 if (!misc_dev) {
941 struct miscdevice *misc;
942
943 misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
944 if (!misc_dev)
945 return -ENOMEM;
946
947 misc = &misc_dev->misc;
948 misc->minor = MISC_DYNAMIC_MINOR;
949 misc->name = DEVICE_NAME;
950 misc->fops = &sev_fops;
951
952 ret = misc_register(misc);
953 if (ret)
954 return ret;
955
956 kref_init(&misc_dev->refcount);
957 } else {
958 kref_get(&misc_dev->refcount);
959 }
960
961 init_waitqueue_head(&sev->int_queue);
962 sev->misc = misc_dev;
963 dev_dbg(dev, "registered SEV device\n");
964
965 return 0;
966}
967
968int sev_dev_init(struct psp_device *psp)
969{
970 struct device *dev = psp->dev;
971 struct sev_device *sev;
972 int ret = -ENOMEM;
973
974 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
975 if (!sev)
976 goto e_err;
977
978 psp->sev_data = sev;
979
980 sev->dev = dev;
981 sev->psp = psp;
982
983 sev->io_regs = psp->io_regs;
984
985 sev->vdata = (struct sev_vdata *)psp->vdata->sev;
986 if (!sev->vdata) {
987 ret = -ENODEV;
988 dev_err(dev, "sev: missing driver data\n");
989 goto e_err;
990 }
991
992 psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
993
994 ret = sev_misc_init(sev);
995 if (ret)
996 goto e_irq;
997
998 dev_notice(dev, "sev enabled\n");
999
1000 return 0;
1001
1002e_irq:
1003 psp_clear_sev_irq_handler(psp);
1004e_err:
1005 psp->sev_data = NULL;
1006
1007 dev_notice(dev, "sev initialization failed\n");
1008
1009 return ret;
1010}
1011
1012void sev_dev_destroy(struct psp_device *psp)
1013{
1014 struct sev_device *sev = psp->sev_data;
1015
1016 if (!sev)
1017 return;
1018
1019 if (sev->misc)
1020 kref_put(&misc_dev->refcount, sev_exit);
1021
1022 psp_clear_sev_irq_handler(psp);
1023}
1024
1025int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1026 void *data, int *error)
1027{
1028 if (!filep || filep->f_op != &sev_fops)
1029 return -EBADF;
1030
1031 return sev_do_cmd(cmd, data, error);
1032}
1033EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1034
1035void sev_pci_init(void)
1036{
1037 struct sev_device *sev = psp_master->sev_data;
1038 struct page *tmr_page;
1039 int error, rc;
1040
1041 if (!sev)
1042 return;
1043
1044 psp_timeout = psp_probe_timeout;
1045
1046 if (sev_get_api_version())
1047 goto err;
1048
1049 /*
1050 * If platform is not in UNINIT state then firmware upgrade and/or
1051 * platform INIT command will fail. These command require UNINIT state.
1052 *
1053 * In a normal boot we should never run into case where the firmware
1054 * is not in UNINIT state on boot. But in case of kexec boot, a reboot
1055 * may not go through a typical shutdown sequence and may leave the
1056 * firmware in INIT or WORKING state.
1057 */
1058
1059 if (sev->state != SEV_STATE_UNINIT) {
1060 sev_platform_shutdown(NULL);
1061 sev->state = SEV_STATE_UNINIT;
1062 }
1063
1064 if (sev_version_greater_or_equal(0, 15) &&
1065 sev_update_firmware(sev->dev) == 0)
1066 sev_get_api_version();
1067
1068 /* Obtain the TMR memory area for SEV-ES use */
1069 tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE));
1070 if (tmr_page) {
1071 sev_es_tmr = page_address(tmr_page);
1072 } else {
1073 sev_es_tmr = NULL;
1074 dev_warn(sev->dev,
1075 "SEV: TMR allocation failed, SEV-ES support unavailable\n");
1076 }
1077
1078 /* Initialize the platform */
1079 rc = sev_platform_init(&error);
1080 if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) {
1081 /*
1082 * INIT command returned an integrity check failure
1083 * status code, meaning that firmware load and
1084 * validation of SEV related persistent data has
1085 * failed and persistent state has been erased.
1086 * Retrying INIT command here should succeed.
1087 */
1088 dev_dbg(sev->dev, "SEV: retrying INIT command");
1089 rc = sev_platform_init(&error);
1090 }
1091
1092 if (rc) {
1093 dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error);
1094 return;
1095 }
1096
1097 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
1098 sev->api_minor, sev->build);
1099
1100 return;
1101
1102err:
1103 psp_master->sev_data = NULL;
1104}
1105
1106void sev_pci_exit(void)
1107{
1108 if (!psp_master->sev_data)
1109 return;
1110
1111 sev_platform_shutdown(NULL);
1112
1113 if (sev_es_tmr) {
1114 /* The TMR area was encrypted, flush it from the cache */
1115 wbinvd_on_all_cpus();
1116
1117 free_pages((unsigned long)sev_es_tmr,
1118 get_order(SEV_ES_TMR_SIZE));
1119 sev_es_tmr = NULL;
1120 }
1121}