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