Mention branches and keyring.
[releases.git] / ccp / sev-dev.c
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 #include <linux/cpufeature.h>
25
26 #include <asm/smp.h>
27
28 #include "psp-dev.h"
29 #include "sev-dev.h"
30
31 #define DEVICE_NAME             "sev"
32 #define SEV_FW_FILE             "/*(DEBLOBBED)*/"
33 #define SEV_FW_NAME_SIZE        64
34
35 static DEFINE_MUTEX(sev_cmd_mutex);
36 static struct sev_misc_dev *misc_dev;
37
38 static int psp_cmd_timeout = 100;
39 module_param(psp_cmd_timeout, int, 0644);
40 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
41
42 static int psp_probe_timeout = 5;
43 module_param(psp_probe_timeout, int, 0644);
44 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
45
46 /*(DEBLOBBED)*/ /* 1st gen EPYC */
47 /*(DEBLOBBED)*/ /* 2nd gen EPYC */
48 /*(DEBLOBBED)*/ /* 3rd gen EPYC */
49
50 static bool psp_dead;
51 static int psp_timeout;
52
53 /* Trusted Memory Region (TMR):
54  *   The TMR is a 1MB area that must be 1MB aligned.  Use the page allocator
55  *   to allocate the memory, which will return aligned memory for the specified
56  *   allocation order.
57  */
58 #define SEV_ES_TMR_SIZE         (1024 * 1024)
59 static void *sev_es_tmr;
60
61 static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
62 {
63         struct sev_device *sev = psp_master->sev_data;
64
65         if (sev->api_major > maj)
66                 return true;
67
68         if (sev->api_major == maj && sev->api_minor >= min)
69                 return true;
70
71         return false;
72 }
73
74 static void sev_irq_handler(int irq, void *data, unsigned int status)
75 {
76         struct sev_device *sev = data;
77         int reg;
78
79         /* Check if it is command completion: */
80         if (!(status & SEV_CMD_COMPLETE))
81                 return;
82
83         /* Check if it is SEV command completion: */
84         reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
85         if (reg & PSP_CMDRESP_RESP) {
86                 sev->int_rcvd = 1;
87                 wake_up(&sev->int_queue);
88         }
89 }
90
91 static int sev_wait_cmd_ioc(struct sev_device *sev,
92                             unsigned int *reg, unsigned int timeout)
93 {
94         int ret;
95
96         ret = wait_event_timeout(sev->int_queue,
97                         sev->int_rcvd, timeout * HZ);
98         if (!ret)
99                 return -ETIMEDOUT;
100
101         *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
102
103         return 0;
104 }
105
106 static int sev_cmd_buffer_len(int cmd)
107 {
108         switch (cmd) {
109         case SEV_CMD_INIT:                      return sizeof(struct sev_data_init);
110         case SEV_CMD_PLATFORM_STATUS:           return sizeof(struct sev_user_data_status);
111         case SEV_CMD_PEK_CSR:                   return sizeof(struct sev_data_pek_csr);
112         case SEV_CMD_PEK_CERT_IMPORT:           return sizeof(struct sev_data_pek_cert_import);
113         case SEV_CMD_PDH_CERT_EXPORT:           return sizeof(struct sev_data_pdh_cert_export);
114         case SEV_CMD_LAUNCH_START:              return sizeof(struct sev_data_launch_start);
115         case SEV_CMD_LAUNCH_UPDATE_DATA:        return sizeof(struct sev_data_launch_update_data);
116         case SEV_CMD_LAUNCH_UPDATE_VMSA:        return sizeof(struct sev_data_launch_update_vmsa);
117         case SEV_CMD_LAUNCH_FINISH:             return sizeof(struct sev_data_launch_finish);
118         case SEV_CMD_LAUNCH_MEASURE:            return sizeof(struct sev_data_launch_measure);
119         case SEV_CMD_ACTIVATE:                  return sizeof(struct sev_data_activate);
120         case SEV_CMD_DEACTIVATE:                return sizeof(struct sev_data_deactivate);
121         case SEV_CMD_DECOMMISSION:              return sizeof(struct sev_data_decommission);
122         case SEV_CMD_GUEST_STATUS:              return sizeof(struct sev_data_guest_status);
123         case SEV_CMD_DBG_DECRYPT:               return sizeof(struct sev_data_dbg);
124         case SEV_CMD_DBG_ENCRYPT:               return sizeof(struct sev_data_dbg);
125         case SEV_CMD_SEND_START:                return sizeof(struct sev_data_send_start);
126         case SEV_CMD_SEND_UPDATE_DATA:          return sizeof(struct sev_data_send_update_data);
127         case SEV_CMD_SEND_UPDATE_VMSA:          return sizeof(struct sev_data_send_update_vmsa);
128         case SEV_CMD_SEND_FINISH:               return sizeof(struct sev_data_send_finish);
129         case SEV_CMD_RECEIVE_START:             return sizeof(struct sev_data_receive_start);
130         case SEV_CMD_RECEIVE_FINISH:            return sizeof(struct sev_data_receive_finish);
131         case SEV_CMD_RECEIVE_UPDATE_DATA:       return sizeof(struct sev_data_receive_update_data);
132         case SEV_CMD_RECEIVE_UPDATE_VMSA:       return sizeof(struct sev_data_receive_update_vmsa);
133         case SEV_CMD_LAUNCH_UPDATE_SECRET:      return sizeof(struct sev_data_launch_secret);
134         case SEV_CMD_DOWNLOAD_FIRMWARE:         return sizeof(struct sev_data_download_firmware);
135         case SEV_CMD_GET_ID:                    return sizeof(struct sev_data_get_id);
136         case SEV_CMD_ATTESTATION_REPORT:        return sizeof(struct sev_data_attestation_report);
137         case SEV_CMD_SEND_CANCEL:               return sizeof(struct sev_data_send_cancel);
138         default:                                return 0;
139         }
140
141         return 0;
142 }
143
144 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
145 {
146         struct psp_device *psp = psp_master;
147         struct sev_device *sev;
148         unsigned int phys_lsb, phys_msb;
149         unsigned int reg, ret = 0;
150         int buf_len;
151
152         if (!psp || !psp->sev_data)
153                 return -ENODEV;
154
155         if (psp_dead)
156                 return -EBUSY;
157
158         sev = psp->sev_data;
159
160         buf_len = sev_cmd_buffer_len(cmd);
161         if (WARN_ON_ONCE(!data != !buf_len))
162                 return -EINVAL;
163
164         /*
165          * Copy the incoming data to driver's scratch buffer as __pa() will not
166          * work for some memory, e.g. vmalloc'd addresses, and @data may not be
167          * physically contiguous.
168          */
169         if (data)
170                 memcpy(sev->cmd_buf, data, buf_len);
171
172         /* Get the physical address of the command buffer */
173         phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0;
174         phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0;
175
176         dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
177                 cmd, phys_msb, phys_lsb, psp_timeout);
178
179         print_hex_dump_debug("(in):  ", DUMP_PREFIX_OFFSET, 16, 2, data,
180                              buf_len, false);
181
182         iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
183         iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
184
185         sev->int_rcvd = 0;
186
187         reg = cmd;
188         reg <<= SEV_CMDRESP_CMD_SHIFT;
189         reg |= SEV_CMDRESP_IOC;
190         iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
191
192         /* wait for command completion */
193         ret = sev_wait_cmd_ioc(sev, &reg, psp_timeout);
194         if (ret) {
195                 if (psp_ret)
196                         *psp_ret = 0;
197
198                 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
199                 psp_dead = true;
200
201                 return ret;
202         }
203
204         psp_timeout = psp_cmd_timeout;
205
206         if (psp_ret)
207                 *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
208
209         if (reg & PSP_CMDRESP_ERR_MASK) {
210                 dev_dbg(sev->dev, "sev command %#x failed (%#010x)\n",
211                         cmd, reg & PSP_CMDRESP_ERR_MASK);
212                 ret = -EIO;
213         }
214
215         print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
216                              buf_len, false);
217
218         /*
219          * Copy potential output from the PSP back to data.  Do this even on
220          * failure in case the caller wants to glean something from the error.
221          */
222         if (data)
223                 memcpy(data, sev->cmd_buf, buf_len);
224
225         return ret;
226 }
227
228 static int sev_do_cmd(int cmd, void *data, int *psp_ret)
229 {
230         int rc;
231
232         mutex_lock(&sev_cmd_mutex);
233         rc = __sev_do_cmd_locked(cmd, data, psp_ret);
234         mutex_unlock(&sev_cmd_mutex);
235
236         return rc;
237 }
238
239 static int __sev_platform_init_locked(int *error)
240 {
241         struct psp_device *psp = psp_master;
242         struct sev_data_init data;
243         struct sev_device *sev;
244         int psp_ret = -1, rc = 0;
245
246         if (!psp || !psp->sev_data)
247                 return -ENODEV;
248
249         sev = psp->sev_data;
250
251         if (sev->state == SEV_STATE_INIT)
252                 return 0;
253
254         memset(&data, 0, sizeof(data));
255         if (sev_es_tmr) {
256                 u64 tmr_pa;
257
258                 /*
259                  * Do not include the encryption mask on the physical
260                  * address of the TMR (firmware should clear it anyway).
261                  */
262                 tmr_pa = __pa(sev_es_tmr);
263
264                 data.flags |= SEV_INIT_FLAGS_SEV_ES;
265                 data.tmr_address = tmr_pa;
266                 data.tmr_len = SEV_ES_TMR_SIZE;
267         }
268
269         rc = __sev_do_cmd_locked(SEV_CMD_INIT, &data, &psp_ret);
270         if (rc && psp_ret == SEV_RET_SECURE_DATA_INVALID) {
271                 /*
272                  * Initialization command returned an integrity check failure
273                  * status code, meaning that firmware load and validation of SEV
274                  * related persistent data has failed. Retrying the
275                  * initialization function should succeed by replacing the state
276                  * with a reset state.
277                  */
278                 dev_dbg(sev->dev, "SEV: retrying INIT command");
279                 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &data, &psp_ret);
280         }
281         if (error)
282                 *error = psp_ret;
283
284         if (rc)
285                 return rc;
286
287         sev->state = SEV_STATE_INIT;
288
289         /* Prepare for first SEV guest launch after INIT */
290         wbinvd_on_all_cpus();
291         rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
292         if (rc)
293                 return rc;
294
295         dev_dbg(sev->dev, "SEV firmware initialized\n");
296
297         return rc;
298 }
299
300 int sev_platform_init(int *error)
301 {
302         int rc;
303
304         mutex_lock(&sev_cmd_mutex);
305         rc = __sev_platform_init_locked(error);
306         mutex_unlock(&sev_cmd_mutex);
307
308         return rc;
309 }
310 EXPORT_SYMBOL_GPL(sev_platform_init);
311
312 static int __sev_platform_shutdown_locked(int *error)
313 {
314         struct sev_device *sev = psp_master->sev_data;
315         int ret;
316
317         if (sev->state == SEV_STATE_UNINIT)
318                 return 0;
319
320         ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
321         if (ret)
322                 return ret;
323
324         sev->state = SEV_STATE_UNINIT;
325         dev_dbg(sev->dev, "SEV firmware shutdown\n");
326
327         return ret;
328 }
329
330 static int sev_platform_shutdown(int *error)
331 {
332         int rc;
333
334         mutex_lock(&sev_cmd_mutex);
335         rc = __sev_platform_shutdown_locked(NULL);
336         mutex_unlock(&sev_cmd_mutex);
337
338         return rc;
339 }
340
341 static int sev_get_platform_state(int *state, int *error)
342 {
343         struct sev_user_data_status data;
344         int rc;
345
346         rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, error);
347         if (rc)
348                 return rc;
349
350         *state = data.state;
351         return rc;
352 }
353
354 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
355 {
356         int state, rc;
357
358         if (!writable)
359                 return -EPERM;
360
361         /*
362          * The SEV spec requires that FACTORY_RESET must be issued in
363          * UNINIT state. Before we go further lets check if any guest is
364          * active.
365          *
366          * If FW is in WORKING state then deny the request otherwise issue
367          * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
368          *
369          */
370         rc = sev_get_platform_state(&state, &argp->error);
371         if (rc)
372                 return rc;
373
374         if (state == SEV_STATE_WORKING)
375                 return -EBUSY;
376
377         if (state == SEV_STATE_INIT) {
378                 rc = __sev_platform_shutdown_locked(&argp->error);
379                 if (rc)
380                         return rc;
381         }
382
383         return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
384 }
385
386 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
387 {
388         struct sev_user_data_status data;
389         int ret;
390
391         ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, &argp->error);
392         if (ret)
393                 return ret;
394
395         if (copy_to_user((void __user *)argp->data, &data, sizeof(data)))
396                 ret = -EFAULT;
397
398         return ret;
399 }
400
401 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
402 {
403         struct sev_device *sev = psp_master->sev_data;
404         int rc;
405
406         if (!writable)
407                 return -EPERM;
408
409         if (sev->state == SEV_STATE_UNINIT) {
410                 rc = __sev_platform_init_locked(&argp->error);
411                 if (rc)
412                         return rc;
413         }
414
415         return __sev_do_cmd_locked(cmd, NULL, &argp->error);
416 }
417
418 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
419 {
420         struct sev_device *sev = psp_master->sev_data;
421         struct sev_user_data_pek_csr input;
422         struct sev_data_pek_csr data;
423         void __user *input_address;
424         void *blob = NULL;
425         int ret;
426
427         if (!writable)
428                 return -EPERM;
429
430         if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
431                 return -EFAULT;
432
433         memset(&data, 0, sizeof(data));
434
435         /* userspace wants to query CSR length */
436         if (!input.address || !input.length)
437                 goto cmd;
438
439         /* allocate a physically contiguous buffer to store the CSR blob */
440         input_address = (void __user *)input.address;
441         if (input.length > SEV_FW_BLOB_MAX_SIZE)
442                 return -EFAULT;
443
444         blob = kmalloc(input.length, GFP_KERNEL);
445         if (!blob)
446                 return -ENOMEM;
447
448         data.address = __psp_pa(blob);
449         data.len = input.length;
450
451 cmd:
452         if (sev->state == SEV_STATE_UNINIT) {
453                 ret = __sev_platform_init_locked(&argp->error);
454                 if (ret)
455                         goto e_free_blob;
456         }
457
458         ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error);
459
460          /* If we query the CSR length, FW responded with expected data. */
461         input.length = data.len;
462
463         if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
464                 ret = -EFAULT;
465                 goto e_free_blob;
466         }
467
468         if (blob) {
469                 if (copy_to_user(input_address, blob, input.length))
470                         ret = -EFAULT;
471         }
472
473 e_free_blob:
474         kfree(blob);
475         return ret;
476 }
477
478 void *psp_copy_user_blob(u64 uaddr, u32 len)
479 {
480         if (!uaddr || !len)
481                 return ERR_PTR(-EINVAL);
482
483         /* verify that blob length does not exceed our limit */
484         if (len > SEV_FW_BLOB_MAX_SIZE)
485                 return ERR_PTR(-EINVAL);
486
487         return memdup_user((void __user *)uaddr, len);
488 }
489 EXPORT_SYMBOL_GPL(psp_copy_user_blob);
490
491 static int sev_get_api_version(void)
492 {
493         struct sev_device *sev = psp_master->sev_data;
494         struct sev_user_data_status status;
495         int error = 0, ret;
496
497         ret = sev_platform_status(&status, &error);
498         if (ret) {
499                 dev_err(sev->dev,
500                         "SEV: failed to get status. Error: %#x\n", error);
501                 return 1;
502         }
503
504         sev->api_major = status.api_major;
505         sev->api_minor = status.api_minor;
506         sev->build = status.build;
507         sev->state = status.state;
508
509         return 0;
510 }
511
512 static int sev_get_firmware(struct device *dev,
513                             const struct firmware **firmware)
514 {
515         char fw_name_specific[SEV_FW_NAME_SIZE];
516         char fw_name_subset[SEV_FW_NAME_SIZE];
517
518         snprintf(fw_name_specific, sizeof(fw_name_specific),
519                  "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
520                  boot_cpu_data.x86, boot_cpu_data.x86_model);
521
522         snprintf(fw_name_subset, sizeof(fw_name_subset),
523                  "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
524                  boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
525
526         /* Check for SEV FW for a particular model.
527          * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
528          *
529          * or
530          *
531          * Check for SEV FW common to a subset of models.
532          * Ex. amd_sev_fam17h_model0xh.sbin for
533          *     Family 17h Model 00h -- Family 17h Model 0Fh
534          *
535          * or
536          *
537          * Fall-back to using generic name: sev.fw
538          */
539         if ((firmware_reject_nowarn(firmware, fw_name_specific, dev) >= 0) ||
540             (firmware_reject_nowarn(firmware, fw_name_subset, dev) >= 0) ||
541             (firmware_reject_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
542                 return 0;
543
544         return -ENOENT;
545 }
546
547 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
548 static int sev_update_firmware(struct device *dev)
549 {
550         struct sev_data_download_firmware *data;
551         const struct firmware *firmware;
552         int ret, error, order;
553         struct page *p;
554         u64 data_size;
555
556         if (sev_get_firmware(dev, &firmware) == -ENOENT) {
557                 dev_dbg(dev, "No SEV firmware file present\n");
558                 return -1;
559         }
560
561         /*
562          * SEV FW expects the physical address given to it to be 32
563          * byte aligned. Memory allocated has structure placed at the
564          * beginning followed by the firmware being passed to the SEV
565          * FW. Allocate enough memory for data structure + alignment
566          * padding + SEV FW.
567          */
568         data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
569
570         order = get_order(firmware->size + data_size);
571         p = alloc_pages(GFP_KERNEL, order);
572         if (!p) {
573                 ret = -1;
574                 goto fw_err;
575         }
576
577         /*
578          * Copy firmware data to a kernel allocated contiguous
579          * memory region.
580          */
581         data = page_address(p);
582         memcpy(page_address(p) + data_size, firmware->data, firmware->size);
583
584         data->address = __psp_pa(page_address(p) + data_size);
585         data->len = firmware->size;
586
587         ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
588         if (ret)
589                 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
590         else
591                 dev_info(dev, "SEV firmware update successful\n");
592
593         __free_pages(p, order);
594
595 fw_err:
596         release_firmware(firmware);
597
598         return ret;
599 }
600
601 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
602 {
603         struct sev_device *sev = psp_master->sev_data;
604         struct sev_user_data_pek_cert_import input;
605         struct sev_data_pek_cert_import data;
606         void *pek_blob, *oca_blob;
607         int ret;
608
609         if (!writable)
610                 return -EPERM;
611
612         if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
613                 return -EFAULT;
614
615         /* copy PEK certificate blobs from userspace */
616         pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
617         if (IS_ERR(pek_blob))
618                 return PTR_ERR(pek_blob);
619
620         data.reserved = 0;
621         data.pek_cert_address = __psp_pa(pek_blob);
622         data.pek_cert_len = input.pek_cert_len;
623
624         /* copy PEK certificate blobs from userspace */
625         oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
626         if (IS_ERR(oca_blob)) {
627                 ret = PTR_ERR(oca_blob);
628                 goto e_free_pek;
629         }
630
631         data.oca_cert_address = __psp_pa(oca_blob);
632         data.oca_cert_len = input.oca_cert_len;
633
634         /* If platform is not in INIT state then transition it to INIT */
635         if (sev->state != SEV_STATE_INIT) {
636                 ret = __sev_platform_init_locked(&argp->error);
637                 if (ret)
638                         goto e_free_oca;
639         }
640
641         ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error);
642
643 e_free_oca:
644         kfree(oca_blob);
645 e_free_pek:
646         kfree(pek_blob);
647         return ret;
648 }
649
650 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
651 {
652         struct sev_user_data_get_id2 input;
653         struct sev_data_get_id data;
654         void __user *input_address;
655         void *id_blob = NULL;
656         int ret;
657
658         /* SEV GET_ID is available from SEV API v0.16 and up */
659         if (!sev_version_greater_or_equal(0, 16))
660                 return -ENOTSUPP;
661
662         if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
663                 return -EFAULT;
664
665         input_address = (void __user *)input.address;
666
667         if (input.address && input.length) {
668                 id_blob = kmalloc(input.length, GFP_KERNEL);
669                 if (!id_blob)
670                         return -ENOMEM;
671
672                 data.address = __psp_pa(id_blob);
673                 data.len = input.length;
674         } else {
675                 data.address = 0;
676                 data.len = 0;
677         }
678
679         ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, &data, &argp->error);
680
681         /*
682          * Firmware will return the length of the ID value (either the minimum
683          * required length or the actual length written), return it to the user.
684          */
685         input.length = data.len;
686
687         if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
688                 ret = -EFAULT;
689                 goto e_free;
690         }
691
692         if (id_blob) {
693                 if (copy_to_user(input_address, id_blob, data.len)) {
694                         ret = -EFAULT;
695                         goto e_free;
696                 }
697         }
698
699 e_free:
700         kfree(id_blob);
701
702         return ret;
703 }
704
705 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
706 {
707         struct sev_data_get_id *data;
708         u64 data_size, user_size;
709         void *id_blob, *mem;
710         int ret;
711
712         /* SEV GET_ID available from SEV API v0.16 and up */
713         if (!sev_version_greater_or_equal(0, 16))
714                 return -ENOTSUPP;
715
716         /* SEV FW expects the buffer it fills with the ID to be
717          * 8-byte aligned. Memory allocated should be enough to
718          * hold data structure + alignment padding + memory
719          * where SEV FW writes the ID.
720          */
721         data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
722         user_size = sizeof(struct sev_user_data_get_id);
723
724         mem = kzalloc(data_size + user_size, GFP_KERNEL);
725         if (!mem)
726                 return -ENOMEM;
727
728         data = mem;
729         id_blob = mem + data_size;
730
731         data->address = __psp_pa(id_blob);
732         data->len = user_size;
733
734         ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
735         if (!ret) {
736                 if (copy_to_user((void __user *)argp->data, id_blob, data->len))
737                         ret = -EFAULT;
738         }
739
740         kfree(mem);
741
742         return ret;
743 }
744
745 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
746 {
747         struct sev_device *sev = psp_master->sev_data;
748         struct sev_user_data_pdh_cert_export input;
749         void *pdh_blob = NULL, *cert_blob = NULL;
750         struct sev_data_pdh_cert_export data;
751         void __user *input_cert_chain_address;
752         void __user *input_pdh_cert_address;
753         int ret;
754
755         /* If platform is not in INIT state then transition it to INIT. */
756         if (sev->state != SEV_STATE_INIT) {
757                 if (!writable)
758                         return -EPERM;
759
760                 ret = __sev_platform_init_locked(&argp->error);
761                 if (ret)
762                         return ret;
763         }
764
765         if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
766                 return -EFAULT;
767
768         memset(&data, 0, sizeof(data));
769
770         /* Userspace wants to query the certificate length. */
771         if (!input.pdh_cert_address ||
772             !input.pdh_cert_len ||
773             !input.cert_chain_address)
774                 goto cmd;
775
776         input_pdh_cert_address = (void __user *)input.pdh_cert_address;
777         input_cert_chain_address = (void __user *)input.cert_chain_address;
778
779         /* Allocate a physically contiguous buffer to store the PDH blob. */
780         if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE)
781                 return -EFAULT;
782
783         /* Allocate a physically contiguous buffer to store the cert chain blob. */
784         if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE)
785                 return -EFAULT;
786
787         pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
788         if (!pdh_blob)
789                 return -ENOMEM;
790
791         data.pdh_cert_address = __psp_pa(pdh_blob);
792         data.pdh_cert_len = input.pdh_cert_len;
793
794         cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
795         if (!cert_blob) {
796                 ret = -ENOMEM;
797                 goto e_free_pdh;
798         }
799
800         data.cert_chain_address = __psp_pa(cert_blob);
801         data.cert_chain_len = input.cert_chain_len;
802
803 cmd:
804         ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error);
805
806         /* If we query the length, FW responded with expected data. */
807         input.cert_chain_len = data.cert_chain_len;
808         input.pdh_cert_len = data.pdh_cert_len;
809
810         if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
811                 ret = -EFAULT;
812                 goto e_free_cert;
813         }
814
815         if (pdh_blob) {
816                 if (copy_to_user(input_pdh_cert_address,
817                                  pdh_blob, input.pdh_cert_len)) {
818                         ret = -EFAULT;
819                         goto e_free_cert;
820                 }
821         }
822
823         if (cert_blob) {
824                 if (copy_to_user(input_cert_chain_address,
825                                  cert_blob, input.cert_chain_len))
826                         ret = -EFAULT;
827         }
828
829 e_free_cert:
830         kfree(cert_blob);
831 e_free_pdh:
832         kfree(pdh_blob);
833         return ret;
834 }
835
836 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
837 {
838         void __user *argp = (void __user *)arg;
839         struct sev_issue_cmd input;
840         int ret = -EFAULT;
841         bool writable = file->f_mode & FMODE_WRITE;
842
843         if (!psp_master || !psp_master->sev_data)
844                 return -ENODEV;
845
846         if (ioctl != SEV_ISSUE_CMD)
847                 return -EINVAL;
848
849         if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
850                 return -EFAULT;
851
852         if (input.cmd > SEV_MAX)
853                 return -EINVAL;
854
855         mutex_lock(&sev_cmd_mutex);
856
857         switch (input.cmd) {
858
859         case SEV_FACTORY_RESET:
860                 ret = sev_ioctl_do_reset(&input, writable);
861                 break;
862         case SEV_PLATFORM_STATUS:
863                 ret = sev_ioctl_do_platform_status(&input);
864                 break;
865         case SEV_PEK_GEN:
866                 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable);
867                 break;
868         case SEV_PDH_GEN:
869                 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable);
870                 break;
871         case SEV_PEK_CSR:
872                 ret = sev_ioctl_do_pek_csr(&input, writable);
873                 break;
874         case SEV_PEK_CERT_IMPORT:
875                 ret = sev_ioctl_do_pek_import(&input, writable);
876                 break;
877         case SEV_PDH_CERT_EXPORT:
878                 ret = sev_ioctl_do_pdh_export(&input, writable);
879                 break;
880         case SEV_GET_ID:
881                 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
882                 ret = sev_ioctl_do_get_id(&input);
883                 break;
884         case SEV_GET_ID2:
885                 ret = sev_ioctl_do_get_id2(&input);
886                 break;
887         default:
888                 ret = -EINVAL;
889                 goto out;
890         }
891
892         if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
893                 ret = -EFAULT;
894 out:
895         mutex_unlock(&sev_cmd_mutex);
896
897         return ret;
898 }
899
900 static const struct file_operations sev_fops = {
901         .owner  = THIS_MODULE,
902         .unlocked_ioctl = sev_ioctl,
903 };
904
905 int sev_platform_status(struct sev_user_data_status *data, int *error)
906 {
907         return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
908 }
909 EXPORT_SYMBOL_GPL(sev_platform_status);
910
911 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
912 {
913         return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
914 }
915 EXPORT_SYMBOL_GPL(sev_guest_deactivate);
916
917 int sev_guest_activate(struct sev_data_activate *data, int *error)
918 {
919         return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
920 }
921 EXPORT_SYMBOL_GPL(sev_guest_activate);
922
923 int sev_guest_decommission(struct sev_data_decommission *data, int *error)
924 {
925         return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
926 }
927 EXPORT_SYMBOL_GPL(sev_guest_decommission);
928
929 int sev_guest_df_flush(int *error)
930 {
931         return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
932 }
933 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
934
935 static void sev_exit(struct kref *ref)
936 {
937         misc_deregister(&misc_dev->misc);
938         kfree(misc_dev);
939         misc_dev = NULL;
940 }
941
942 static int sev_misc_init(struct sev_device *sev)
943 {
944         struct device *dev = sev->dev;
945         int ret;
946
947         /*
948          * SEV feature support can be detected on multiple devices but the SEV
949          * FW commands must be issued on the master. During probe, we do not
950          * know the master hence we create /dev/sev on the first device probe.
951          * sev_do_cmd() finds the right master device to which to issue the
952          * command to the firmware.
953          */
954         if (!misc_dev) {
955                 struct miscdevice *misc;
956
957                 misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
958                 if (!misc_dev)
959                         return -ENOMEM;
960
961                 misc = &misc_dev->misc;
962                 misc->minor = MISC_DYNAMIC_MINOR;
963                 misc->name = DEVICE_NAME;
964                 misc->fops = &sev_fops;
965
966                 ret = misc_register(misc);
967                 if (ret)
968                         return ret;
969
970                 kref_init(&misc_dev->refcount);
971         } else {
972                 kref_get(&misc_dev->refcount);
973         }
974
975         init_waitqueue_head(&sev->int_queue);
976         sev->misc = misc_dev;
977         dev_dbg(dev, "registered SEV device\n");
978
979         return 0;
980 }
981
982 int sev_dev_init(struct psp_device *psp)
983 {
984         struct device *dev = psp->dev;
985         struct sev_device *sev;
986         int ret = -ENOMEM;
987
988         if (!boot_cpu_has(X86_FEATURE_SEV)) {
989                 dev_info_once(dev, "SEV: memory encryption not enabled by BIOS\n");
990                 return 0;
991         }
992
993         sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
994         if (!sev)
995                 goto e_err;
996
997         sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
998         if (!sev->cmd_buf)
999                 goto e_sev;
1000
1001         psp->sev_data = sev;
1002
1003         sev->dev = dev;
1004         sev->psp = psp;
1005
1006         sev->io_regs = psp->io_regs;
1007
1008         sev->vdata = (struct sev_vdata *)psp->vdata->sev;
1009         if (!sev->vdata) {
1010                 ret = -ENODEV;
1011                 dev_err(dev, "sev: missing driver data\n");
1012                 goto e_buf;
1013         }
1014
1015         psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
1016
1017         ret = sev_misc_init(sev);
1018         if (ret)
1019                 goto e_irq;
1020
1021         dev_notice(dev, "sev enabled\n");
1022
1023         return 0;
1024
1025 e_irq:
1026         psp_clear_sev_irq_handler(psp);
1027 e_buf:
1028         devm_free_pages(dev, (unsigned long)sev->cmd_buf);
1029 e_sev:
1030         devm_kfree(dev, sev);
1031 e_err:
1032         psp->sev_data = NULL;
1033
1034         dev_notice(dev, "sev initialization failed\n");
1035
1036         return ret;
1037 }
1038
1039 static void sev_firmware_shutdown(struct sev_device *sev)
1040 {
1041         sev_platform_shutdown(NULL);
1042
1043         if (sev_es_tmr) {
1044                 /* The TMR area was encrypted, flush it from the cache */
1045                 wbinvd_on_all_cpus();
1046
1047                 free_pages((unsigned long)sev_es_tmr,
1048                            get_order(SEV_ES_TMR_SIZE));
1049                 sev_es_tmr = NULL;
1050         }
1051 }
1052
1053 void sev_dev_destroy(struct psp_device *psp)
1054 {
1055         struct sev_device *sev = psp->sev_data;
1056
1057         if (!sev)
1058                 return;
1059
1060         sev_firmware_shutdown(sev);
1061
1062         if (sev->misc)
1063                 kref_put(&misc_dev->refcount, sev_exit);
1064
1065         psp_clear_sev_irq_handler(psp);
1066 }
1067
1068 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1069                                 void *data, int *error)
1070 {
1071         if (!filep || filep->f_op != &sev_fops)
1072                 return -EBADF;
1073
1074         return sev_do_cmd(cmd, data, error);
1075 }
1076 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1077
1078 void sev_pci_init(void)
1079 {
1080         struct sev_device *sev = psp_master->sev_data;
1081         struct page *tmr_page;
1082         int error, rc;
1083
1084         if (!sev)
1085                 return;
1086
1087         psp_timeout = psp_probe_timeout;
1088
1089         if (sev_get_api_version())
1090                 goto err;
1091
1092         if (sev_version_greater_or_equal(0, 15) &&
1093             sev_update_firmware(sev->dev) == 0)
1094                 sev_get_api_version();
1095
1096         /* Obtain the TMR memory area for SEV-ES use */
1097         tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE));
1098         if (tmr_page) {
1099                 sev_es_tmr = page_address(tmr_page);
1100         } else {
1101                 sev_es_tmr = NULL;
1102                 dev_warn(sev->dev,
1103                          "SEV: TMR allocation failed, SEV-ES support unavailable\n");
1104         }
1105
1106         /* Initialize the platform */
1107         rc = sev_platform_init(&error);
1108         if (rc) {
1109                 dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error);
1110                 return;
1111         }
1112
1113         dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
1114                  sev->api_minor, sev->build);
1115
1116         return;
1117
1118 err:
1119         psp_master->sev_data = NULL;
1120 }
1121
1122 void sev_pci_exit(void)
1123 {
1124         struct sev_device *sev = psp_master->sev_data;
1125
1126         if (!sev)
1127                 return;
1128
1129         sev_firmware_shutdown(sev);
1130 }