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
40 static DEFINE_MUTEX(sev_cmd_mutex);
41 static struct sev_misc_dev *misc_dev;
42
43 static int psp_cmd_timeout = 100;
44 module_param(psp_cmd_timeout, int, 0644);
45 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
46
47 static int psp_probe_timeout = 5;
48 module_param(psp_probe_timeout, int, 0644);
49 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
50
51 static char *init_ex_path;
52 module_param(init_ex_path, charp, 0444);
53 MODULE_PARM_DESC(init_ex_path, " Path for INIT_EX data; if set try INIT_EX");
54
55 static bool psp_init_on_probe = true;
56 module_param(psp_init_on_probe, bool, 0444);
57 MODULE_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
59 MODULE_FIRMWARE("amd/amd_sev_fam17h_model0xh.sbin"); /* 1st gen EPYC */
60 MODULE_FIRMWARE("amd/amd_sev_fam17h_model3xh.sbin"); /* 2nd gen EPYC */
61 MODULE_FIRMWARE("amd/amd_sev_fam19h_model0xh.sbin"); /* 3rd gen EPYC */
62 MODULE_FIRMWARE("amd/amd_sev_fam19h_model1xh.sbin"); /* 4th gen EPYC */
63
64 static bool psp_dead;
65 static 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)
73 static 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)
81 static void *sev_init_ex_buffer;
82
sev_version_greater_or_equal(u8 maj,u8 min)83 static 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
sev_irq_handler(int irq,void * data,unsigned int status)96 static 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
sev_wait_cmd_ioc(struct sev_device * sev,unsigned int * reg,unsigned int timeout)113 static 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
sev_cmd_buffer_len(int cmd)128 static 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
sev_fw_alloc(unsigned long len)167 static 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
open_file_as_root(const char * filename,int flags,umode_t mode)178 static 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
sev_read_init_ex_file(void)203 static 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
sev_write_init_ex_file(void)244 static 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
sev_write_init_ex_file_if_required(int cmd_id)282 static 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
__sev_do_cmd_locked(int cmd,void * data,int * psp_ret)308 static 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 phys_lsb, phys_msb;
313 unsigned int reg, ret = 0;
314 int buf_len;
315
316 if (!psp || !psp->sev_data)
317 return -ENODEV;
318
319 if (psp_dead)
320 return -EBUSY;
321
322 sev = psp->sev_data;
323
324 buf_len = sev_cmd_buffer_len(cmd);
325 if (WARN_ON_ONCE(!data != !buf_len))
326 return -EINVAL;
327
328 /*
329 * Copy the incoming data to driver's scratch buffer as __pa() will not
330 * work for some memory, e.g. vmalloc'd addresses, and @data may not be
331 * physically contiguous.
332 */
333 if (data)
334 memcpy(sev->cmd_buf, data, buf_len);
335
336 /* Get the physical address of the command buffer */
337 phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0;
338 phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0;
339
340 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
341 cmd, phys_msb, phys_lsb, psp_timeout);
342
343 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
344 buf_len, false);
345
346 iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
347 iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
348
349 sev->int_rcvd = 0;
350
351 reg = FIELD_PREP(SEV_CMDRESP_CMD, cmd) | SEV_CMDRESP_IOC;
352 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
353
354 /* wait for command completion */
355 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout);
356 if (ret) {
357 if (psp_ret)
358 *psp_ret = 0;
359
360 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
361 psp_dead = true;
362
363 return ret;
364 }
365
366 psp_timeout = psp_cmd_timeout;
367
368 if (psp_ret)
369 *psp_ret = FIELD_GET(PSP_CMDRESP_STS, reg);
370
371 if (FIELD_GET(PSP_CMDRESP_STS, reg)) {
372 dev_dbg(sev->dev, "sev command %#x failed (%#010lx)\n",
373 cmd, FIELD_GET(PSP_CMDRESP_STS, reg));
374 ret = -EIO;
375 } else {
376 ret = sev_write_init_ex_file_if_required(cmd);
377 }
378
379 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
380 buf_len, false);
381
382 /*
383 * Copy potential output from the PSP back to data. Do this even on
384 * failure in case the caller wants to glean something from the error.
385 */
386 if (data)
387 memcpy(data, sev->cmd_buf, buf_len);
388
389 return ret;
390 }
391
sev_do_cmd(int cmd,void * data,int * psp_ret)392 static int sev_do_cmd(int cmd, void *data, int *psp_ret)
393 {
394 int rc;
395
396 mutex_lock(&sev_cmd_mutex);
397 rc = __sev_do_cmd_locked(cmd, data, psp_ret);
398 mutex_unlock(&sev_cmd_mutex);
399
400 return rc;
401 }
402
__sev_init_locked(int * error)403 static int __sev_init_locked(int *error)
404 {
405 struct sev_data_init data;
406
407 memset(&data, 0, sizeof(data));
408 if (sev_es_tmr) {
409 /*
410 * Do not include the encryption mask on the physical
411 * address of the TMR (firmware should clear it anyway).
412 */
413 data.tmr_address = __pa(sev_es_tmr);
414
415 data.flags |= SEV_INIT_FLAGS_SEV_ES;
416 data.tmr_len = SEV_ES_TMR_SIZE;
417 }
418
419 return __sev_do_cmd_locked(SEV_CMD_INIT, &data, error);
420 }
421
__sev_init_ex_locked(int * error)422 static int __sev_init_ex_locked(int *error)
423 {
424 struct sev_data_init_ex data;
425
426 memset(&data, 0, sizeof(data));
427 data.length = sizeof(data);
428 data.nv_address = __psp_pa(sev_init_ex_buffer);
429 data.nv_len = NV_LENGTH;
430
431 if (sev_es_tmr) {
432 /*
433 * Do not include the encryption mask on the physical
434 * address of the TMR (firmware should clear it anyway).
435 */
436 data.tmr_address = __pa(sev_es_tmr);
437
438 data.flags |= SEV_INIT_FLAGS_SEV_ES;
439 data.tmr_len = SEV_ES_TMR_SIZE;
440 }
441
442 return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error);
443 }
444
__sev_do_init_locked(int * psp_ret)445 static inline int __sev_do_init_locked(int *psp_ret)
446 {
447 if (sev_init_ex_buffer)
448 return __sev_init_ex_locked(psp_ret);
449 else
450 return __sev_init_locked(psp_ret);
451 }
452
__sev_platform_init_locked(int * error)453 static int __sev_platform_init_locked(int *error)
454 {
455 int rc = 0, psp_ret = SEV_RET_NO_FW_CALL;
456 struct psp_device *psp = psp_master;
457 struct sev_device *sev;
458
459 if (!psp || !psp->sev_data)
460 return -ENODEV;
461
462 sev = psp->sev_data;
463
464 if (sev->state == SEV_STATE_INIT)
465 return 0;
466
467 if (sev_init_ex_buffer) {
468 rc = sev_read_init_ex_file();
469 if (rc)
470 return rc;
471 }
472
473 rc = __sev_do_init_locked(&psp_ret);
474 if (rc && psp_ret == SEV_RET_SECURE_DATA_INVALID) {
475 /*
476 * Initialization command returned an integrity check failure
477 * status code, meaning that firmware load and validation of SEV
478 * related persistent data has failed. Retrying the
479 * initialization function should succeed by replacing the state
480 * with a reset state.
481 */
482 dev_err(sev->dev,
483 "SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state.");
484 rc = __sev_do_init_locked(&psp_ret);
485 }
486
487 if (error)
488 *error = psp_ret;
489
490 if (rc)
491 return rc;
492
493 sev->state = SEV_STATE_INIT;
494
495 /* Prepare for first SEV guest launch after INIT */
496 wbinvd_on_all_cpus();
497 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
498 if (rc)
499 return rc;
500
501 dev_dbg(sev->dev, "SEV firmware initialized\n");
502
503 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
504 sev->api_minor, sev->build);
505
506 return 0;
507 }
508
sev_platform_init(int * error)509 int sev_platform_init(int *error)
510 {
511 int rc;
512
513 mutex_lock(&sev_cmd_mutex);
514 rc = __sev_platform_init_locked(error);
515 mutex_unlock(&sev_cmd_mutex);
516
517 return rc;
518 }
519 EXPORT_SYMBOL_GPL(sev_platform_init);
520
__sev_platform_shutdown_locked(int * error)521 static int __sev_platform_shutdown_locked(int *error)
522 {
523 struct sev_device *sev = psp_master->sev_data;
524 int ret;
525
526 if (!sev || sev->state == SEV_STATE_UNINIT)
527 return 0;
528
529 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
530 if (ret)
531 return ret;
532
533 sev->state = SEV_STATE_UNINIT;
534 dev_dbg(sev->dev, "SEV firmware shutdown\n");
535
536 return ret;
537 }
538
sev_platform_shutdown(int * error)539 static int sev_platform_shutdown(int *error)
540 {
541 int rc;
542
543 mutex_lock(&sev_cmd_mutex);
544 rc = __sev_platform_shutdown_locked(NULL);
545 mutex_unlock(&sev_cmd_mutex);
546
547 return rc;
548 }
549
sev_get_platform_state(int * state,int * error)550 static int sev_get_platform_state(int *state, int *error)
551 {
552 struct sev_user_data_status data;
553 int rc;
554
555 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, error);
556 if (rc)
557 return rc;
558
559 *state = data.state;
560 return rc;
561 }
562
sev_ioctl_do_reset(struct sev_issue_cmd * argp,bool writable)563 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
564 {
565 int state, rc;
566
567 if (!writable)
568 return -EPERM;
569
570 /*
571 * The SEV spec requires that FACTORY_RESET must be issued in
572 * UNINIT state. Before we go further lets check if any guest is
573 * active.
574 *
575 * If FW is in WORKING state then deny the request otherwise issue
576 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
577 *
578 */
579 rc = sev_get_platform_state(&state, &argp->error);
580 if (rc)
581 return rc;
582
583 if (state == SEV_STATE_WORKING)
584 return -EBUSY;
585
586 if (state == SEV_STATE_INIT) {
587 rc = __sev_platform_shutdown_locked(&argp->error);
588 if (rc)
589 return rc;
590 }
591
592 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
593 }
594
sev_ioctl_do_platform_status(struct sev_issue_cmd * argp)595 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
596 {
597 struct sev_user_data_status data;
598 int ret;
599
600 memset(&data, 0, sizeof(data));
601
602 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, &argp->error);
603 if (ret)
604 return ret;
605
606 if (copy_to_user((void __user *)argp->data, &data, sizeof(data)))
607 ret = -EFAULT;
608
609 return ret;
610 }
611
sev_ioctl_do_pek_pdh_gen(int cmd,struct sev_issue_cmd * argp,bool writable)612 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
613 {
614 struct sev_device *sev = psp_master->sev_data;
615 int rc;
616
617 if (!writable)
618 return -EPERM;
619
620 if (sev->state == SEV_STATE_UNINIT) {
621 rc = __sev_platform_init_locked(&argp->error);
622 if (rc)
623 return rc;
624 }
625
626 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
627 }
628
sev_ioctl_do_pek_csr(struct sev_issue_cmd * argp,bool writable)629 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
630 {
631 struct sev_device *sev = psp_master->sev_data;
632 struct sev_user_data_pek_csr input;
633 struct sev_data_pek_csr data;
634 void __user *input_address;
635 void *blob = NULL;
636 int ret;
637
638 if (!writable)
639 return -EPERM;
640
641 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
642 return -EFAULT;
643
644 memset(&data, 0, sizeof(data));
645
646 /* userspace wants to query CSR length */
647 if (!input.address || !input.length)
648 goto cmd;
649
650 /* allocate a physically contiguous buffer to store the CSR blob */
651 input_address = (void __user *)input.address;
652 if (input.length > SEV_FW_BLOB_MAX_SIZE)
653 return -EFAULT;
654
655 blob = kzalloc(input.length, GFP_KERNEL);
656 if (!blob)
657 return -ENOMEM;
658
659 data.address = __psp_pa(blob);
660 data.len = input.length;
661
662 cmd:
663 if (sev->state == SEV_STATE_UNINIT) {
664 ret = __sev_platform_init_locked(&argp->error);
665 if (ret)
666 goto e_free_blob;
667 }
668
669 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error);
670
671 /* If we query the CSR length, FW responded with expected data. */
672 input.length = data.len;
673
674 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
675 ret = -EFAULT;
676 goto e_free_blob;
677 }
678
679 if (blob) {
680 if (copy_to_user(input_address, blob, input.length))
681 ret = -EFAULT;
682 }
683
684 e_free_blob:
685 kfree(blob);
686 return ret;
687 }
688
psp_copy_user_blob(u64 uaddr,u32 len)689 void *psp_copy_user_blob(u64 uaddr, u32 len)
690 {
691 if (!uaddr || !len)
692 return ERR_PTR(-EINVAL);
693
694 /* verify that blob length does not exceed our limit */
695 if (len > SEV_FW_BLOB_MAX_SIZE)
696 return ERR_PTR(-EINVAL);
697
698 return memdup_user((void __user *)uaddr, len);
699 }
700 EXPORT_SYMBOL_GPL(psp_copy_user_blob);
701
sev_get_api_version(void)702 static int sev_get_api_version(void)
703 {
704 struct sev_device *sev = psp_master->sev_data;
705 struct sev_user_data_status status;
706 int error = 0, ret;
707
708 ret = sev_platform_status(&status, &error);
709 if (ret) {
710 dev_err(sev->dev,
711 "SEV: failed to get status. Error: %#x\n", error);
712 return 1;
713 }
714
715 sev->api_major = status.api_major;
716 sev->api_minor = status.api_minor;
717 sev->build = status.build;
718 sev->state = status.state;
719
720 return 0;
721 }
722
sev_get_firmware(struct device * dev,const struct firmware ** firmware)723 static int sev_get_firmware(struct device *dev,
724 const struct firmware **firmware)
725 {
726 char fw_name_specific[SEV_FW_NAME_SIZE];
727 char fw_name_subset[SEV_FW_NAME_SIZE];
728
729 snprintf(fw_name_specific, sizeof(fw_name_specific),
730 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
731 boot_cpu_data.x86, boot_cpu_data.x86_model);
732
733 snprintf(fw_name_subset, sizeof(fw_name_subset),
734 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
735 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
736
737 /* Check for SEV FW for a particular model.
738 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
739 *
740 * or
741 *
742 * Check for SEV FW common to a subset of models.
743 * Ex. amd_sev_fam17h_model0xh.sbin for
744 * Family 17h Model 00h -- Family 17h Model 0Fh
745 *
746 * or
747 *
748 * Fall-back to using generic name: sev.fw
749 */
750 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
751 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
752 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
753 return 0;
754
755 return -ENOENT;
756 }
757
758 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
sev_update_firmware(struct device * dev)759 static int sev_update_firmware(struct device *dev)
760 {
761 struct sev_data_download_firmware *data;
762 const struct firmware *firmware;
763 int ret, error, order;
764 struct page *p;
765 u64 data_size;
766
767 if (!sev_version_greater_or_equal(0, 15)) {
768 dev_dbg(dev, "DOWNLOAD_FIRMWARE not supported\n");
769 return -1;
770 }
771
772 if (sev_get_firmware(dev, &firmware) == -ENOENT) {
773 dev_dbg(dev, "No SEV firmware file present\n");
774 return -1;
775 }
776
777 /*
778 * SEV FW expects the physical address given to it to be 32
779 * byte aligned. Memory allocated has structure placed at the
780 * beginning followed by the firmware being passed to the SEV
781 * FW. Allocate enough memory for data structure + alignment
782 * padding + SEV FW.
783 */
784 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
785
786 order = get_order(firmware->size + data_size);
787 p = alloc_pages(GFP_KERNEL, order);
788 if (!p) {
789 ret = -1;
790 goto fw_err;
791 }
792
793 /*
794 * Copy firmware data to a kernel allocated contiguous
795 * memory region.
796 */
797 data = page_address(p);
798 memcpy(page_address(p) + data_size, firmware->data, firmware->size);
799
800 data->address = __psp_pa(page_address(p) + data_size);
801 data->len = firmware->size;
802
803 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
804
805 /*
806 * A quirk for fixing the committed TCB version, when upgrading from
807 * earlier firmware version than 1.50.
808 */
809 if (!ret && !sev_version_greater_or_equal(1, 50))
810 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
811
812 if (ret)
813 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
814 else
815 dev_info(dev, "SEV firmware update successful\n");
816
817 __free_pages(p, order);
818
819 fw_err:
820 release_firmware(firmware);
821
822 return ret;
823 }
824
sev_ioctl_do_pek_import(struct sev_issue_cmd * argp,bool writable)825 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
826 {
827 struct sev_device *sev = psp_master->sev_data;
828 struct sev_user_data_pek_cert_import input;
829 struct sev_data_pek_cert_import data;
830 void *pek_blob, *oca_blob;
831 int ret;
832
833 if (!writable)
834 return -EPERM;
835
836 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
837 return -EFAULT;
838
839 /* copy PEK certificate blobs from userspace */
840 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
841 if (IS_ERR(pek_blob))
842 return PTR_ERR(pek_blob);
843
844 data.reserved = 0;
845 data.pek_cert_address = __psp_pa(pek_blob);
846 data.pek_cert_len = input.pek_cert_len;
847
848 /* copy PEK certificate blobs from userspace */
849 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
850 if (IS_ERR(oca_blob)) {
851 ret = PTR_ERR(oca_blob);
852 goto e_free_pek;
853 }
854
855 data.oca_cert_address = __psp_pa(oca_blob);
856 data.oca_cert_len = input.oca_cert_len;
857
858 /* If platform is not in INIT state then transition it to INIT */
859 if (sev->state != SEV_STATE_INIT) {
860 ret = __sev_platform_init_locked(&argp->error);
861 if (ret)
862 goto e_free_oca;
863 }
864
865 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error);
866
867 e_free_oca:
868 kfree(oca_blob);
869 e_free_pek:
870 kfree(pek_blob);
871 return ret;
872 }
873
sev_ioctl_do_get_id2(struct sev_issue_cmd * argp)874 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
875 {
876 struct sev_user_data_get_id2 input;
877 struct sev_data_get_id data;
878 void __user *input_address;
879 void *id_blob = NULL;
880 int ret;
881
882 /* SEV GET_ID is available from SEV API v0.16 and up */
883 if (!sev_version_greater_or_equal(0, 16))
884 return -ENOTSUPP;
885
886 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
887 return -EFAULT;
888
889 input_address = (void __user *)input.address;
890
891 if (input.address && input.length) {
892 /*
893 * The length of the ID shouldn't be assumed by software since
894 * it may change in the future. The allocation size is limited
895 * to 1 << (PAGE_SHIFT + MAX_ORDER) by the page allocator.
896 * If the allocation fails, simply return ENOMEM rather than
897 * warning in the kernel log.
898 */
899 id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN);
900 if (!id_blob)
901 return -ENOMEM;
902
903 data.address = __psp_pa(id_blob);
904 data.len = input.length;
905 } else {
906 data.address = 0;
907 data.len = 0;
908 }
909
910 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, &data, &argp->error);
911
912 /*
913 * Firmware will return the length of the ID value (either the minimum
914 * required length or the actual length written), return it to the user.
915 */
916 input.length = data.len;
917
918 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
919 ret = -EFAULT;
920 goto e_free;
921 }
922
923 if (id_blob) {
924 if (copy_to_user(input_address, id_blob, data.len)) {
925 ret = -EFAULT;
926 goto e_free;
927 }
928 }
929
930 e_free:
931 kfree(id_blob);
932
933 return ret;
934 }
935
sev_ioctl_do_get_id(struct sev_issue_cmd * argp)936 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
937 {
938 struct sev_data_get_id *data;
939 u64 data_size, user_size;
940 void *id_blob, *mem;
941 int ret;
942
943 /* SEV GET_ID available from SEV API v0.16 and up */
944 if (!sev_version_greater_or_equal(0, 16))
945 return -ENOTSUPP;
946
947 /* SEV FW expects the buffer it fills with the ID to be
948 * 8-byte aligned. Memory allocated should be enough to
949 * hold data structure + alignment padding + memory
950 * where SEV FW writes the ID.
951 */
952 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
953 user_size = sizeof(struct sev_user_data_get_id);
954
955 mem = kzalloc(data_size + user_size, GFP_KERNEL);
956 if (!mem)
957 return -ENOMEM;
958
959 data = mem;
960 id_blob = mem + data_size;
961
962 data->address = __psp_pa(id_blob);
963 data->len = user_size;
964
965 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
966 if (!ret) {
967 if (copy_to_user((void __user *)argp->data, id_blob, data->len))
968 ret = -EFAULT;
969 }
970
971 kfree(mem);
972
973 return ret;
974 }
975
sev_ioctl_do_pdh_export(struct sev_issue_cmd * argp,bool writable)976 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
977 {
978 struct sev_device *sev = psp_master->sev_data;
979 struct sev_user_data_pdh_cert_export input;
980 void *pdh_blob = NULL, *cert_blob = NULL;
981 struct sev_data_pdh_cert_export data;
982 void __user *input_cert_chain_address;
983 void __user *input_pdh_cert_address;
984 int ret;
985
986 /* If platform is not in INIT state then transition it to INIT. */
987 if (sev->state != SEV_STATE_INIT) {
988 if (!writable)
989 return -EPERM;
990
991 ret = __sev_platform_init_locked(&argp->error);
992 if (ret)
993 return ret;
994 }
995
996 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
997 return -EFAULT;
998
999 memset(&data, 0, sizeof(data));
1000
1001 /* Userspace wants to query the certificate length. */
1002 if (!input.pdh_cert_address ||
1003 !input.pdh_cert_len ||
1004 !input.cert_chain_address)
1005 goto cmd;
1006
1007 input_pdh_cert_address = (void __user *)input.pdh_cert_address;
1008 input_cert_chain_address = (void __user *)input.cert_chain_address;
1009
1010 /* Allocate a physically contiguous buffer to store the PDH blob. */
1011 if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE)
1012 return -EFAULT;
1013
1014 /* Allocate a physically contiguous buffer to store the cert chain blob. */
1015 if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE)
1016 return -EFAULT;
1017
1018 pdh_blob = kzalloc(input.pdh_cert_len, GFP_KERNEL);
1019 if (!pdh_blob)
1020 return -ENOMEM;
1021
1022 data.pdh_cert_address = __psp_pa(pdh_blob);
1023 data.pdh_cert_len = input.pdh_cert_len;
1024
1025 cert_blob = kzalloc(input.cert_chain_len, GFP_KERNEL);
1026 if (!cert_blob) {
1027 ret = -ENOMEM;
1028 goto e_free_pdh;
1029 }
1030
1031 data.cert_chain_address = __psp_pa(cert_blob);
1032 data.cert_chain_len = input.cert_chain_len;
1033
1034 cmd:
1035 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error);
1036
1037 /* If we query the length, FW responded with expected data. */
1038 input.cert_chain_len = data.cert_chain_len;
1039 input.pdh_cert_len = data.pdh_cert_len;
1040
1041 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
1042 ret = -EFAULT;
1043 goto e_free_cert;
1044 }
1045
1046 if (pdh_blob) {
1047 if (copy_to_user(input_pdh_cert_address,
1048 pdh_blob, input.pdh_cert_len)) {
1049 ret = -EFAULT;
1050 goto e_free_cert;
1051 }
1052 }
1053
1054 if (cert_blob) {
1055 if (copy_to_user(input_cert_chain_address,
1056 cert_blob, input.cert_chain_len))
1057 ret = -EFAULT;
1058 }
1059
1060 e_free_cert:
1061 kfree(cert_blob);
1062 e_free_pdh:
1063 kfree(pdh_blob);
1064 return ret;
1065 }
1066
sev_ioctl(struct file * file,unsigned int ioctl,unsigned long arg)1067 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
1068 {
1069 void __user *argp = (void __user *)arg;
1070 struct sev_issue_cmd input;
1071 int ret = -EFAULT;
1072 bool writable = file->f_mode & FMODE_WRITE;
1073
1074 if (!psp_master || !psp_master->sev_data)
1075 return -ENODEV;
1076
1077 if (ioctl != SEV_ISSUE_CMD)
1078 return -EINVAL;
1079
1080 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
1081 return -EFAULT;
1082
1083 if (input.cmd > SEV_MAX)
1084 return -EINVAL;
1085
1086 mutex_lock(&sev_cmd_mutex);
1087
1088 switch (input.cmd) {
1089
1090 case SEV_FACTORY_RESET:
1091 ret = sev_ioctl_do_reset(&input, writable);
1092 break;
1093 case SEV_PLATFORM_STATUS:
1094 ret = sev_ioctl_do_platform_status(&input);
1095 break;
1096 case SEV_PEK_GEN:
1097 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable);
1098 break;
1099 case SEV_PDH_GEN:
1100 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable);
1101 break;
1102 case SEV_PEK_CSR:
1103 ret = sev_ioctl_do_pek_csr(&input, writable);
1104 break;
1105 case SEV_PEK_CERT_IMPORT:
1106 ret = sev_ioctl_do_pek_import(&input, writable);
1107 break;
1108 case SEV_PDH_CERT_EXPORT:
1109 ret = sev_ioctl_do_pdh_export(&input, writable);
1110 break;
1111 case SEV_GET_ID:
1112 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
1113 ret = sev_ioctl_do_get_id(&input);
1114 break;
1115 case SEV_GET_ID2:
1116 ret = sev_ioctl_do_get_id2(&input);
1117 break;
1118 default:
1119 ret = -EINVAL;
1120 goto out;
1121 }
1122
1123 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
1124 ret = -EFAULT;
1125 out:
1126 mutex_unlock(&sev_cmd_mutex);
1127
1128 return ret;
1129 }
1130
1131 static const struct file_operations sev_fops = {
1132 .owner = THIS_MODULE,
1133 .unlocked_ioctl = sev_ioctl,
1134 };
1135
sev_platform_status(struct sev_user_data_status * data,int * error)1136 int sev_platform_status(struct sev_user_data_status *data, int *error)
1137 {
1138 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
1139 }
1140 EXPORT_SYMBOL_GPL(sev_platform_status);
1141
sev_guest_deactivate(struct sev_data_deactivate * data,int * error)1142 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
1143 {
1144 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
1145 }
1146 EXPORT_SYMBOL_GPL(sev_guest_deactivate);
1147
sev_guest_activate(struct sev_data_activate * data,int * error)1148 int sev_guest_activate(struct sev_data_activate *data, int *error)
1149 {
1150 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
1151 }
1152 EXPORT_SYMBOL_GPL(sev_guest_activate);
1153
sev_guest_decommission(struct sev_data_decommission * data,int * error)1154 int sev_guest_decommission(struct sev_data_decommission *data, int *error)
1155 {
1156 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
1157 }
1158 EXPORT_SYMBOL_GPL(sev_guest_decommission);
1159
sev_guest_df_flush(int * error)1160 int sev_guest_df_flush(int *error)
1161 {
1162 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
1163 }
1164 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
1165
sev_exit(struct kref * ref)1166 static void sev_exit(struct kref *ref)
1167 {
1168 misc_deregister(&misc_dev->misc);
1169 kfree(misc_dev);
1170 misc_dev = NULL;
1171 }
1172
sev_misc_init(struct sev_device * sev)1173 static int sev_misc_init(struct sev_device *sev)
1174 {
1175 struct device *dev = sev->dev;
1176 int ret;
1177
1178 /*
1179 * SEV feature support can be detected on multiple devices but the SEV
1180 * FW commands must be issued on the master. During probe, we do not
1181 * know the master hence we create /dev/sev on the first device probe.
1182 * sev_do_cmd() finds the right master device to which to issue the
1183 * command to the firmware.
1184 */
1185 if (!misc_dev) {
1186 struct miscdevice *misc;
1187
1188 misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
1189 if (!misc_dev)
1190 return -ENOMEM;
1191
1192 misc = &misc_dev->misc;
1193 misc->minor = MISC_DYNAMIC_MINOR;
1194 misc->name = DEVICE_NAME;
1195 misc->fops = &sev_fops;
1196
1197 ret = misc_register(misc);
1198 if (ret)
1199 return ret;
1200
1201 kref_init(&misc_dev->refcount);
1202 } else {
1203 kref_get(&misc_dev->refcount);
1204 }
1205
1206 init_waitqueue_head(&sev->int_queue);
1207 sev->misc = misc_dev;
1208 dev_dbg(dev, "registered SEV device\n");
1209
1210 return 0;
1211 }
1212
sev_dev_init(struct psp_device * psp)1213 int sev_dev_init(struct psp_device *psp)
1214 {
1215 struct device *dev = psp->dev;
1216 struct sev_device *sev;
1217 int ret = -ENOMEM;
1218
1219 if (!boot_cpu_has(X86_FEATURE_SEV)) {
1220 dev_info_once(dev, "SEV: memory encryption not enabled by BIOS\n");
1221 return 0;
1222 }
1223
1224 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
1225 if (!sev)
1226 goto e_err;
1227
1228 sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
1229 if (!sev->cmd_buf)
1230 goto e_sev;
1231
1232 psp->sev_data = sev;
1233
1234 sev->dev = dev;
1235 sev->psp = psp;
1236
1237 sev->io_regs = psp->io_regs;
1238
1239 sev->vdata = (struct sev_vdata *)psp->vdata->sev;
1240 if (!sev->vdata) {
1241 ret = -ENODEV;
1242 dev_err(dev, "sev: missing driver data\n");
1243 goto e_buf;
1244 }
1245
1246 psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
1247
1248 ret = sev_misc_init(sev);
1249 if (ret)
1250 goto e_irq;
1251
1252 dev_notice(dev, "sev enabled\n");
1253
1254 return 0;
1255
1256 e_irq:
1257 psp_clear_sev_irq_handler(psp);
1258 e_buf:
1259 devm_free_pages(dev, (unsigned long)sev->cmd_buf);
1260 e_sev:
1261 devm_kfree(dev, sev);
1262 e_err:
1263 psp->sev_data = NULL;
1264
1265 dev_notice(dev, "sev initialization failed\n");
1266
1267 return ret;
1268 }
1269
sev_firmware_shutdown(struct sev_device * sev)1270 static void sev_firmware_shutdown(struct sev_device *sev)
1271 {
1272 sev_platform_shutdown(NULL);
1273
1274 if (sev_es_tmr) {
1275 /* The TMR area was encrypted, flush it from the cache */
1276 wbinvd_on_all_cpus();
1277
1278 free_pages((unsigned long)sev_es_tmr,
1279 get_order(SEV_ES_TMR_SIZE));
1280 sev_es_tmr = NULL;
1281 }
1282
1283 if (sev_init_ex_buffer) {
1284 free_pages((unsigned long)sev_init_ex_buffer,
1285 get_order(NV_LENGTH));
1286 sev_init_ex_buffer = NULL;
1287 }
1288 }
1289
sev_dev_destroy(struct psp_device * psp)1290 void sev_dev_destroy(struct psp_device *psp)
1291 {
1292 struct sev_device *sev = psp->sev_data;
1293
1294 if (!sev)
1295 return;
1296
1297 sev_firmware_shutdown(sev);
1298
1299 if (sev->misc)
1300 kref_put(&misc_dev->refcount, sev_exit);
1301
1302 psp_clear_sev_irq_handler(psp);
1303 }
1304
sev_issue_cmd_external_user(struct file * filep,unsigned int cmd,void * data,int * error)1305 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1306 void *data, int *error)
1307 {
1308 if (!filep || filep->f_op != &sev_fops)
1309 return -EBADF;
1310
1311 return sev_do_cmd(cmd, data, error);
1312 }
1313 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1314
sev_pci_init(void)1315 void sev_pci_init(void)
1316 {
1317 struct sev_device *sev = psp_master->sev_data;
1318 int error, rc;
1319
1320 if (!sev)
1321 return;
1322
1323 psp_timeout = psp_probe_timeout;
1324
1325 if (sev_get_api_version())
1326 goto err;
1327
1328 if (sev_update_firmware(sev->dev) == 0)
1329 sev_get_api_version();
1330
1331 /* If an init_ex_path is provided rely on INIT_EX for PSP initialization
1332 * instead of INIT.
1333 */
1334 if (init_ex_path) {
1335 sev_init_ex_buffer = sev_fw_alloc(NV_LENGTH);
1336 if (!sev_init_ex_buffer) {
1337 dev_err(sev->dev,
1338 "SEV: INIT_EX NV memory allocation failed\n");
1339 goto err;
1340 }
1341 }
1342
1343 /* Obtain the TMR memory area for SEV-ES use */
1344 sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE);
1345 if (sev_es_tmr)
1346 /* Must flush the cache before giving it to the firmware */
1347 clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE);
1348 else
1349 dev_warn(sev->dev,
1350 "SEV: TMR allocation failed, SEV-ES support unavailable\n");
1351
1352 if (!psp_init_on_probe)
1353 return;
1354
1355 /* Initialize the platform */
1356 rc = sev_platform_init(&error);
1357 if (rc)
1358 dev_err(sev->dev, "SEV: failed to INIT error %#x, rc %d\n",
1359 error, rc);
1360
1361 return;
1362
1363 err:
1364 psp_master->sev_data = NULL;
1365 }
1366
sev_pci_exit(void)1367 void sev_pci_exit(void)
1368 {
1369 struct sev_device *sev = psp_master->sev_data;
1370
1371 if (!sev)
1372 return;
1373
1374 sev_firmware_shutdown(sev);
1375 }
1376