blob: 7ca2d3408e7aa53aa2f0afe995841cd4c5d9b53f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Brijesh Singh2a6170d2017-12-04 10:57:28 -06002/*
3 * AMD Platform Security Processor (PSP) interface
4 *
Hook, Garyfa5cd1c2018-12-18 15:48:29 +00005 * Copyright (C) 2016,2018 Advanced Micro Devices, Inc.
Brijesh Singh2a6170d2017-12-04 10:57:28 -06006 *
7 * Author: Brijesh Singh <brijesh.singh@amd.com>
Brijesh Singh2a6170d2017-12-04 10:57:28 -06008 */
9
10#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/kthread.h>
13#include <linux/sched.h>
14#include <linux/interrupt.h>
15#include <linux/spinlock.h>
16#include <linux/spinlock_types.h>
17#include <linux/types.h>
18#include <linux/mutex.h>
19#include <linux/delay.h>
20#include <linux/hw_random.h>
21#include <linux/ccp.h>
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -050022#include <linux/firmware.h>
Brijesh Singh2a6170d2017-12-04 10:57:28 -060023
Tom Lendacky0fc5dea2019-10-03 21:17:46 +000024#include <asm/smp.h>
25
Brijesh Singh2a6170d2017-12-04 10:57:28 -060026#include "sp-dev.h"
27#include "psp-dev.h"
28
Janakarajan Natarajane9372062018-09-14 17:32:04 -050029#define DEVICE_NAME "sev"
30#define SEV_FW_FILE "amd/sev.fw"
31#define SEV_FW_NAME_SIZE 64
Brijesh Singh200664d2017-12-04 10:57:28 -060032
33static DEFINE_MUTEX(sev_cmd_mutex);
34static struct sev_misc_dev *misc_dev;
35static struct psp_device *psp_master;
36
Brijesh Singhe82867f2018-08-15 16:11:25 -050037static int psp_cmd_timeout = 100;
38module_param(psp_cmd_timeout, int, 0644);
39MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
40
41static int psp_probe_timeout = 5;
42module_param(psp_probe_timeout, int, 0644);
43MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
44
45static bool psp_dead;
46static int psp_timeout;
47
David Rientjes83bf4252019-07-12 13:41:58 -070048static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
49{
50 if (psp_master->api_major > maj)
51 return true;
52 if (psp_master->api_major == maj && psp_master->api_minor >= min)
53 return true;
54 return false;
55}
56
Brijesh Singh2a6170d2017-12-04 10:57:28 -060057static struct psp_device *psp_alloc_struct(struct sp_device *sp)
58{
59 struct device *dev = sp->dev;
60 struct psp_device *psp;
61
62 psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL);
63 if (!psp)
64 return NULL;
65
66 psp->dev = dev;
67 psp->sp = sp;
68
69 snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord);
70
71 return psp;
72}
73
74static irqreturn_t psp_irq_handler(int irq, void *data)
75{
Brijesh Singh200664d2017-12-04 10:57:28 -060076 struct psp_device *psp = data;
77 unsigned int status;
78 int reg;
79
80 /* Read the interrupt status: */
Tom Lendackyad01a982018-07-03 12:12:03 -050081 status = ioread32(psp->io_regs + psp->vdata->intsts_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -060082
83 /* Check if it is command completion: */
Tom Lendacky03af9122018-07-03 12:11:52 -050084 if (!(status & PSP_CMD_COMPLETE))
Brijesh Singh200664d2017-12-04 10:57:28 -060085 goto done;
86
87 /* Check if it is SEV command completion: */
Tom Lendackyad01a982018-07-03 12:12:03 -050088 reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -060089 if (reg & PSP_CMDRESP_RESP) {
90 psp->sev_int_rcvd = 1;
91 wake_up(&psp->sev_int_queue);
92 }
93
94done:
95 /* Clear the interrupt status by writing the same value we read. */
Tom Lendackyad01a982018-07-03 12:12:03 -050096 iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -060097
Brijesh Singh2a6170d2017-12-04 10:57:28 -060098 return IRQ_HANDLED;
99}
100
Brijesh Singhe82867f2018-08-15 16:11:25 -0500101static int sev_wait_cmd_ioc(struct psp_device *psp,
102 unsigned int *reg, unsigned int timeout)
Brijesh Singh200664d2017-12-04 10:57:28 -0600103{
Brijesh Singhe82867f2018-08-15 16:11:25 -0500104 int ret;
105
106 ret = wait_event_timeout(psp->sev_int_queue,
107 psp->sev_int_rcvd, timeout * HZ);
108 if (!ret)
109 return -ETIMEDOUT;
110
Tom Lendackyad01a982018-07-03 12:12:03 -0500111 *reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg);
Brijesh Singhe82867f2018-08-15 16:11:25 -0500112
113 return 0;
Brijesh Singh200664d2017-12-04 10:57:28 -0600114}
115
116static int sev_cmd_buffer_len(int cmd)
117{
118 switch (cmd) {
119 case SEV_CMD_INIT: return sizeof(struct sev_data_init);
120 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
121 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
122 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
123 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
124 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
125 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
126 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
127 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
128 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
129 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
130 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
131 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
132 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
133 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
134 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
135 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
136 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
137 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
138 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
139 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
140 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
141 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
142 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
143 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500144 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500145 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
Brijesh Singh200664d2017-12-04 10:57:28 -0600146 default: return 0;
147 }
148
149 return 0;
150}
151
152static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
153{
154 struct psp_device *psp = psp_master;
155 unsigned int phys_lsb, phys_msb;
156 unsigned int reg, ret = 0;
157
158 if (!psp)
159 return -ENODEV;
160
Brijesh Singhe82867f2018-08-15 16:11:25 -0500161 if (psp_dead)
162 return -EBUSY;
163
Brijesh Singh200664d2017-12-04 10:57:28 -0600164 /* Get the physical address of the command buffer */
165 phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0;
166 phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0;
167
Brijesh Singhe82867f2018-08-15 16:11:25 -0500168 dev_dbg(psp->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
169 cmd, phys_msb, phys_lsb, psp_timeout);
Brijesh Singh200664d2017-12-04 10:57:28 -0600170
171 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
172 sev_cmd_buffer_len(cmd), false);
173
Tom Lendackyad01a982018-07-03 12:12:03 -0500174 iowrite32(phys_lsb, psp->io_regs + psp->vdata->cmdbuff_addr_lo_reg);
175 iowrite32(phys_msb, psp->io_regs + psp->vdata->cmdbuff_addr_hi_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -0600176
Tom Lendackyf426d2b2018-07-03 12:11:33 -0500177 psp->sev_int_rcvd = 0;
178
Brijesh Singh200664d2017-12-04 10:57:28 -0600179 reg = cmd;
180 reg <<= PSP_CMDRESP_CMD_SHIFT;
181 reg |= PSP_CMDRESP_IOC;
Tom Lendackyad01a982018-07-03 12:12:03 -0500182 iowrite32(reg, psp->io_regs + psp->vdata->cmdresp_reg);
Brijesh Singh200664d2017-12-04 10:57:28 -0600183
184 /* wait for command completion */
Brijesh Singhe82867f2018-08-15 16:11:25 -0500185 ret = sev_wait_cmd_ioc(psp, &reg, psp_timeout);
186 if (ret) {
187 if (psp_ret)
188 *psp_ret = 0;
189
190 dev_err(psp->dev, "sev command %#x timed out, disabling PSP \n", cmd);
191 psp_dead = true;
192
193 return ret;
194 }
195
196 psp_timeout = psp_cmd_timeout;
Brijesh Singh200664d2017-12-04 10:57:28 -0600197
198 if (psp_ret)
199 *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
200
201 if (reg & PSP_CMDRESP_ERR_MASK) {
202 dev_dbg(psp->dev, "sev command %#x failed (%#010x)\n",
203 cmd, reg & PSP_CMDRESP_ERR_MASK);
204 ret = -EIO;
205 }
206
207 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
208 sev_cmd_buffer_len(cmd), false);
209
210 return ret;
211}
212
213static int sev_do_cmd(int cmd, void *data, int *psp_ret)
214{
215 int rc;
216
217 mutex_lock(&sev_cmd_mutex);
218 rc = __sev_do_cmd_locked(cmd, data, psp_ret);
219 mutex_unlock(&sev_cmd_mutex);
220
221 return rc;
222}
223
224static int __sev_platform_init_locked(int *error)
225{
226 struct psp_device *psp = psp_master;
227 int rc = 0;
228
229 if (!psp)
230 return -ENODEV;
231
232 if (psp->sev_state == SEV_STATE_INIT)
233 return 0;
234
235 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &psp->init_cmd_buf, error);
236 if (rc)
237 return rc;
238
239 psp->sev_state = SEV_STATE_INIT;
Tom Lendacky0fc5dea2019-10-03 21:17:46 +0000240
241 /* Prepare for first SEV guest launch after INIT */
242 wbinvd_on_all_cpus();
243 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
244 if (rc)
245 return rc;
246
Brijesh Singh200664d2017-12-04 10:57:28 -0600247 dev_dbg(psp->dev, "SEV firmware initialized\n");
248
249 return rc;
250}
251
252int sev_platform_init(int *error)
253{
254 int rc;
255
256 mutex_lock(&sev_cmd_mutex);
257 rc = __sev_platform_init_locked(error);
258 mutex_unlock(&sev_cmd_mutex);
259
260 return rc;
261}
262EXPORT_SYMBOL_GPL(sev_platform_init);
263
264static int __sev_platform_shutdown_locked(int *error)
265{
266 int ret;
267
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600268 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
Brijesh Singh200664d2017-12-04 10:57:28 -0600269 if (ret)
270 return ret;
271
272 psp_master->sev_state = SEV_STATE_UNINIT;
273 dev_dbg(psp_master->dev, "SEV firmware shutdown\n");
274
275 return ret;
276}
277
278static int sev_platform_shutdown(int *error)
279{
280 int rc;
281
282 mutex_lock(&sev_cmd_mutex);
283 rc = __sev_platform_shutdown_locked(NULL);
284 mutex_unlock(&sev_cmd_mutex);
285
286 return rc;
287}
288
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600289static int sev_get_platform_state(int *state, int *error)
290{
291 int rc;
292
293 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
294 &psp_master->status_cmd_buf, error);
295 if (rc)
296 return rc;
297
298 *state = psp_master->status_cmd_buf.state;
299 return rc;
300}
301
302static int sev_ioctl_do_reset(struct sev_issue_cmd *argp)
303{
304 int state, rc;
305
Brijesh Singhec310ca2019-11-12 13:58:34 -0600306 if (!capable(CAP_SYS_ADMIN))
307 return -EPERM;
308
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600309 /*
310 * The SEV spec requires that FACTORY_RESET must be issued in
311 * UNINIT state. Before we go further lets check if any guest is
312 * active.
313 *
314 * If FW is in WORKING state then deny the request otherwise issue
315 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
316 *
317 */
318 rc = sev_get_platform_state(&state, &argp->error);
319 if (rc)
320 return rc;
321
322 if (state == SEV_STATE_WORKING)
323 return -EBUSY;
324
325 if (state == SEV_STATE_INIT) {
326 rc = __sev_platform_shutdown_locked(&argp->error);
327 if (rc)
328 return rc;
329 }
330
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600331 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600332}
333
Brijesh Singhefe18292017-12-04 10:57:29 -0600334static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
335{
336 struct sev_user_data_status *data = &psp_master->status_cmd_buf;
337 int ret;
338
339 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
340 if (ret)
341 return ret;
342
343 if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
344 ret = -EFAULT;
345
346 return ret;
347}
348
Brijesh Singh4d84b722017-12-04 10:57:30 -0600349static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)
350{
351 int rc;
352
Brijesh Singhec310ca2019-11-12 13:58:34 -0600353 if (!capable(CAP_SYS_ADMIN))
354 return -EPERM;
355
Brijesh Singh4d84b722017-12-04 10:57:30 -0600356 if (psp_master->sev_state == SEV_STATE_UNINIT) {
357 rc = __sev_platform_init_locked(&argp->error);
358 if (rc)
359 return rc;
360 }
361
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600362 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
Brijesh Singh4d84b722017-12-04 10:57:30 -0600363}
364
Brijesh Singhe7990352017-12-04 10:57:31 -0600365static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)
366{
367 struct sev_user_data_pek_csr input;
368 struct sev_data_pek_csr *data;
369 void *blob = NULL;
370 int ret;
371
Brijesh Singhec310ca2019-11-12 13:58:34 -0600372 if (!capable(CAP_SYS_ADMIN))
373 return -EPERM;
374
Brijesh Singhe7990352017-12-04 10:57:31 -0600375 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
376 return -EFAULT;
377
378 data = kzalloc(sizeof(*data), GFP_KERNEL);
379 if (!data)
380 return -ENOMEM;
381
382 /* userspace wants to query CSR length */
383 if (!input.address || !input.length)
384 goto cmd;
385
386 /* allocate a physically contiguous buffer to store the CSR blob */
Linus Torvalds96d4f262019-01-03 18:57:57 -0800387 if (!access_ok(input.address, input.length) ||
Brijesh Singhe7990352017-12-04 10:57:31 -0600388 input.length > SEV_FW_BLOB_MAX_SIZE) {
389 ret = -EFAULT;
390 goto e_free;
391 }
392
393 blob = kmalloc(input.length, GFP_KERNEL);
394 if (!blob) {
395 ret = -ENOMEM;
396 goto e_free;
397 }
398
399 data->address = __psp_pa(blob);
400 data->len = input.length;
401
402cmd:
403 if (psp_master->sev_state == SEV_STATE_UNINIT) {
404 ret = __sev_platform_init_locked(&argp->error);
405 if (ret)
406 goto e_free_blob;
407 }
408
409 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
410
411 /* If we query the CSR length, FW responded with expected data. */
412 input.length = data->len;
413
414 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
415 ret = -EFAULT;
416 goto e_free_blob;
417 }
418
419 if (blob) {
420 if (copy_to_user((void __user *)input.address, blob, input.length))
421 ret = -EFAULT;
422 }
423
424e_free_blob:
425 kfree(blob);
426e_free:
427 kfree(data);
428 return ret;
429}
430
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600431void *psp_copy_user_blob(u64 __user uaddr, u32 len)
432{
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600433 if (!uaddr || !len)
434 return ERR_PTR(-EINVAL);
435
436 /* verify that blob length does not exceed our limit */
437 if (len > SEV_FW_BLOB_MAX_SIZE)
438 return ERR_PTR(-EINVAL);
439
Markus Elfring6c51ddd2018-03-05 13:50:13 +0100440 return memdup_user((void __user *)(uintptr_t)uaddr, len);
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600441}
442EXPORT_SYMBOL_GPL(psp_copy_user_blob);
443
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500444static int sev_get_api_version(void)
445{
446 struct sev_user_data_status *status;
Janakarajan Natarajanb78d3792018-09-14 17:32:03 -0500447 int error = 0, ret;
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500448
449 status = &psp_master->status_cmd_buf;
450 ret = sev_platform_status(status, &error);
451 if (ret) {
452 dev_err(psp_master->dev,
453 "SEV: failed to get status. Error: %#x\n", error);
454 return 1;
455 }
456
457 psp_master->api_major = status->api_major;
458 psp_master->api_minor = status->api_minor;
459 psp_master->build = status->build;
Singh, Brijeshf8903b32019-01-30 20:57:52 +0000460 psp_master->sev_state = status->state;
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500461
462 return 0;
463}
464
Wei Yongjun5182f262018-09-26 02:09:23 +0000465static int sev_get_firmware(struct device *dev,
466 const struct firmware **firmware)
Janakarajan Natarajane9372062018-09-14 17:32:04 -0500467{
468 char fw_name_specific[SEV_FW_NAME_SIZE];
469 char fw_name_subset[SEV_FW_NAME_SIZE];
470
471 snprintf(fw_name_specific, sizeof(fw_name_specific),
472 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
473 boot_cpu_data.x86, boot_cpu_data.x86_model);
474
475 snprintf(fw_name_subset, sizeof(fw_name_subset),
476 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
477 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
478
479 /* Check for SEV FW for a particular model.
480 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
481 *
482 * or
483 *
484 * Check for SEV FW common to a subset of models.
485 * Ex. amd_sev_fam17h_model0xh.sbin for
486 * Family 17h Model 00h -- Family 17h Model 0Fh
487 *
488 * or
489 *
490 * Fall-back to using generic name: sev.fw
491 */
492 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
493 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
494 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
495 return 0;
496
497 return -ENOENT;
498}
499
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500500/* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
501static int sev_update_firmware(struct device *dev)
502{
503 struct sev_data_download_firmware *data;
504 const struct firmware *firmware;
505 int ret, error, order;
506 struct page *p;
507 u64 data_size;
508
Janakarajan Natarajane9372062018-09-14 17:32:04 -0500509 if (sev_get_firmware(dev, &firmware) == -ENOENT) {
510 dev_dbg(dev, "No SEV firmware file present\n");
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500511 return -1;
Janakarajan Natarajane9372062018-09-14 17:32:04 -0500512 }
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -0500513
514 /*
515 * SEV FW expects the physical address given to it to be 32
516 * byte aligned. Memory allocated has structure placed at the
517 * beginning followed by the firmware being passed to the SEV
518 * FW. Allocate enough memory for data structure + alignment
519 * padding + SEV FW.
520 */
521 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
522
523 order = get_order(firmware->size + data_size);
524 p = alloc_pages(GFP_KERNEL, order);
525 if (!p) {
526 ret = -1;
527 goto fw_err;
528 }
529
530 /*
531 * Copy firmware data to a kernel allocated contiguous
532 * memory region.
533 */
534 data = page_address(p);
535 memcpy(page_address(p) + data_size, firmware->data, firmware->size);
536
537 data->address = __psp_pa(page_address(p) + data_size);
538 data->len = firmware->size;
539
540 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
541 if (ret)
542 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
543 else
544 dev_info(dev, "SEV firmware update successful\n");
545
546 __free_pages(p, order);
547
548fw_err:
549 release_firmware(firmware);
550
551 return ret;
552}
553
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600554static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp)
555{
556 struct sev_user_data_pek_cert_import input;
557 struct sev_data_pek_cert_import *data;
558 void *pek_blob, *oca_blob;
559 int ret;
560
Brijesh Singhec310ca2019-11-12 13:58:34 -0600561 if (!capable(CAP_SYS_ADMIN))
562 return -EPERM;
563
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600564 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
565 return -EFAULT;
566
567 data = kzalloc(sizeof(*data), GFP_KERNEL);
568 if (!data)
569 return -ENOMEM;
570
571 /* copy PEK certificate blobs from userspace */
572 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
573 if (IS_ERR(pek_blob)) {
574 ret = PTR_ERR(pek_blob);
575 goto e_free;
576 }
577
578 data->pek_cert_address = __psp_pa(pek_blob);
579 data->pek_cert_len = input.pek_cert_len;
580
581 /* copy PEK certificate blobs from userspace */
582 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
583 if (IS_ERR(oca_blob)) {
584 ret = PTR_ERR(oca_blob);
585 goto e_free_pek;
586 }
587
588 data->oca_cert_address = __psp_pa(oca_blob);
589 data->oca_cert_len = input.oca_cert_len;
590
591 /* If platform is not in INIT state then transition it to INIT */
592 if (psp_master->sev_state != SEV_STATE_INIT) {
593 ret = __sev_platform_init_locked(&argp->error);
594 if (ret)
595 goto e_free_oca;
596 }
597
598 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
599
600e_free_oca:
601 kfree(oca_blob);
602e_free_pek:
603 kfree(pek_blob);
604e_free:
605 kfree(data);
606 return ret;
607}
608
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000609static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
610{
611 struct sev_user_data_get_id2 input;
612 struct sev_data_get_id *data;
613 void *id_blob = NULL;
614 int ret;
615
616 /* SEV GET_ID is available from SEV API v0.16 and up */
David Rientjes83bf4252019-07-12 13:41:58 -0700617 if (!sev_version_greater_or_equal(0, 16))
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000618 return -ENOTSUPP;
619
620 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
621 return -EFAULT;
622
623 /* Check if we have write access to the userspace buffer */
624 if (input.address &&
625 input.length &&
626 !access_ok(input.address, input.length))
627 return -EFAULT;
628
629 data = kzalloc(sizeof(*data), GFP_KERNEL);
630 if (!data)
631 return -ENOMEM;
632
633 if (input.address && input.length) {
634 id_blob = kmalloc(input.length, GFP_KERNEL);
635 if (!id_blob) {
636 kfree(data);
637 return -ENOMEM;
638 }
639
640 data->address = __psp_pa(id_blob);
641 data->len = input.length;
642 }
643
644 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
645
646 /*
647 * Firmware will return the length of the ID value (either the minimum
648 * required length or the actual length written), return it to the user.
649 */
650 input.length = data->len;
651
652 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
653 ret = -EFAULT;
654 goto e_free;
655 }
656
657 if (id_blob) {
658 if (copy_to_user((void __user *)input.address,
659 id_blob, data->len)) {
660 ret = -EFAULT;
661 goto e_free;
662 }
663 }
664
665e_free:
666 kfree(id_blob);
667 kfree(data);
668
669 return ret;
670}
671
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500672static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
673{
674 struct sev_data_get_id *data;
675 u64 data_size, user_size;
676 void *id_blob, *mem;
677 int ret;
678
679 /* SEV GET_ID available from SEV API v0.16 and up */
David Rientjes83bf4252019-07-12 13:41:58 -0700680 if (!sev_version_greater_or_equal(0, 16))
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500681 return -ENOTSUPP;
682
683 /* SEV FW expects the buffer it fills with the ID to be
684 * 8-byte aligned. Memory allocated should be enough to
685 * hold data structure + alignment padding + memory
686 * where SEV FW writes the ID.
687 */
688 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
689 user_size = sizeof(struct sev_user_data_get_id);
690
691 mem = kzalloc(data_size + user_size, GFP_KERNEL);
692 if (!mem)
693 return -ENOMEM;
694
695 data = mem;
696 id_blob = mem + data_size;
697
698 data->address = __psp_pa(id_blob);
699 data->len = user_size;
700
701 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
702 if (!ret) {
703 if (copy_to_user((void __user *)argp->data, id_blob, data->len))
704 ret = -EFAULT;
705 }
706
707 kfree(mem);
708
709 return ret;
710}
711
Brijesh Singh76a2b522017-12-04 10:57:31 -0600712static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp)
713{
714 struct sev_user_data_pdh_cert_export input;
715 void *pdh_blob = NULL, *cert_blob = NULL;
716 struct sev_data_pdh_cert_export *data;
717 int ret;
718
Brijesh Singhec310ca2019-11-12 13:58:34 -0600719 /* If platform is not in INIT state then transition it to INIT. */
720 if (psp_master->sev_state != SEV_STATE_INIT) {
721 if (!capable(CAP_SYS_ADMIN))
722 return -EPERM;
723
724 ret = __sev_platform_init_locked(&argp->error);
725 if (ret)
726 return ret;
727 }
728
Brijesh Singh76a2b522017-12-04 10:57:31 -0600729 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
730 return -EFAULT;
731
732 data = kzalloc(sizeof(*data), GFP_KERNEL);
733 if (!data)
734 return -ENOMEM;
735
736 /* Userspace wants to query the certificate length. */
737 if (!input.pdh_cert_address ||
738 !input.pdh_cert_len ||
739 !input.cert_chain_address)
740 goto cmd;
741
742 /* Allocate a physically contiguous buffer to store the PDH blob. */
743 if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) ||
Linus Torvalds96d4f262019-01-03 18:57:57 -0800744 !access_ok(input.pdh_cert_address, input.pdh_cert_len)) {
Brijesh Singh76a2b522017-12-04 10:57:31 -0600745 ret = -EFAULT;
746 goto e_free;
747 }
748
749 /* Allocate a physically contiguous buffer to store the cert chain blob. */
750 if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) ||
Linus Torvalds96d4f262019-01-03 18:57:57 -0800751 !access_ok(input.cert_chain_address, input.cert_chain_len)) {
Brijesh Singh76a2b522017-12-04 10:57:31 -0600752 ret = -EFAULT;
753 goto e_free;
754 }
755
756 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
757 if (!pdh_blob) {
758 ret = -ENOMEM;
759 goto e_free;
760 }
761
762 data->pdh_cert_address = __psp_pa(pdh_blob);
763 data->pdh_cert_len = input.pdh_cert_len;
764
765 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
766 if (!cert_blob) {
767 ret = -ENOMEM;
768 goto e_free_pdh;
769 }
770
771 data->cert_chain_address = __psp_pa(cert_blob);
772 data->cert_chain_len = input.cert_chain_len;
773
774cmd:
Brijesh Singh76a2b522017-12-04 10:57:31 -0600775 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
776
777 /* If we query the length, FW responded with expected data. */
778 input.cert_chain_len = data->cert_chain_len;
779 input.pdh_cert_len = data->pdh_cert_len;
780
781 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
782 ret = -EFAULT;
783 goto e_free_cert;
784 }
785
786 if (pdh_blob) {
787 if (copy_to_user((void __user *)input.pdh_cert_address,
788 pdh_blob, input.pdh_cert_len)) {
789 ret = -EFAULT;
790 goto e_free_cert;
791 }
792 }
793
794 if (cert_blob) {
795 if (copy_to_user((void __user *)input.cert_chain_address,
796 cert_blob, input.cert_chain_len))
797 ret = -EFAULT;
798 }
799
800e_free_cert:
801 kfree(cert_blob);
802e_free_pdh:
803 kfree(pdh_blob);
804e_free:
805 kfree(data);
806 return ret;
807}
808
Brijesh Singh200664d2017-12-04 10:57:28 -0600809static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
810{
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600811 void __user *argp = (void __user *)arg;
812 struct sev_issue_cmd input;
813 int ret = -EFAULT;
814
815 if (!psp_master)
816 return -ENODEV;
817
818 if (ioctl != SEV_ISSUE_CMD)
819 return -EINVAL;
820
821 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
822 return -EFAULT;
823
824 if (input.cmd > SEV_MAX)
825 return -EINVAL;
826
827 mutex_lock(&sev_cmd_mutex);
828
829 switch (input.cmd) {
830
831 case SEV_FACTORY_RESET:
832 ret = sev_ioctl_do_reset(&input);
833 break;
Brijesh Singhefe18292017-12-04 10:57:29 -0600834 case SEV_PLATFORM_STATUS:
835 ret = sev_ioctl_do_platform_status(&input);
836 break;
Brijesh Singh4d84b722017-12-04 10:57:30 -0600837 case SEV_PEK_GEN:
838 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input);
839 break;
Brijesh Singh77f65322017-12-04 10:57:30 -0600840 case SEV_PDH_GEN:
841 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input);
842 break;
Brijesh Singhe7990352017-12-04 10:57:31 -0600843 case SEV_PEK_CSR:
844 ret = sev_ioctl_do_pek_csr(&input);
845 break;
Brijesh Singh7360e4b2017-12-04 10:57:31 -0600846 case SEV_PEK_CERT_IMPORT:
847 ret = sev_ioctl_do_pek_import(&input);
848 break;
Brijesh Singh76a2b522017-12-04 10:57:31 -0600849 case SEV_PDH_CERT_EXPORT:
850 ret = sev_ioctl_do_pdh_export(&input);
851 break;
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500852 case SEV_GET_ID:
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000853 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
Janakarajan Natarajan0b3a8302018-05-25 15:23:30 -0500854 ret = sev_ioctl_do_get_id(&input);
855 break;
Singh, Brijeshd6112ea2019-03-28 21:58:52 +0000856 case SEV_GET_ID2:
857 ret = sev_ioctl_do_get_id2(&input);
858 break;
Brijesh Singh2960f9a2017-12-04 10:57:29 -0600859 default:
860 ret = -EINVAL;
861 goto out;
862 }
863
864 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
865 ret = -EFAULT;
866out:
867 mutex_unlock(&sev_cmd_mutex);
868
869 return ret;
Brijesh Singh200664d2017-12-04 10:57:28 -0600870}
871
872static const struct file_operations sev_fops = {
873 .owner = THIS_MODULE,
874 .unlocked_ioctl = sev_ioctl,
875};
876
877int sev_platform_status(struct sev_user_data_status *data, int *error)
878{
879 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
880}
881EXPORT_SYMBOL_GPL(sev_platform_status);
882
883int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
884{
885 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
886}
887EXPORT_SYMBOL_GPL(sev_guest_deactivate);
888
889int sev_guest_activate(struct sev_data_activate *data, int *error)
890{
891 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
892}
893EXPORT_SYMBOL_GPL(sev_guest_activate);
894
895int sev_guest_decommission(struct sev_data_decommission *data, int *error)
896{
897 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
898}
899EXPORT_SYMBOL_GPL(sev_guest_decommission);
900
901int sev_guest_df_flush(int *error)
902{
Brijesh Singhe385b5b2018-02-15 13:34:44 -0600903 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
Brijesh Singh200664d2017-12-04 10:57:28 -0600904}
905EXPORT_SYMBOL_GPL(sev_guest_df_flush);
906
907static void sev_exit(struct kref *ref)
908{
909 struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount);
910
911 misc_deregister(&misc_dev->misc);
912}
913
914static int sev_misc_init(struct psp_device *psp)
915{
916 struct device *dev = psp->dev;
917 int ret;
918
919 /*
920 * SEV feature support can be detected on multiple devices but the SEV
921 * FW commands must be issued on the master. During probe, we do not
922 * know the master hence we create /dev/sev on the first device probe.
923 * sev_do_cmd() finds the right master device to which to issue the
924 * command to the firmware.
925 */
926 if (!misc_dev) {
927 struct miscdevice *misc;
928
929 misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL);
930 if (!misc_dev)
931 return -ENOMEM;
932
933 misc = &misc_dev->misc;
934 misc->minor = MISC_DYNAMIC_MINOR;
935 misc->name = DEVICE_NAME;
936 misc->fops = &sev_fops;
937
938 ret = misc_register(misc);
939 if (ret)
940 return ret;
941
942 kref_init(&misc_dev->refcount);
943 } else {
944 kref_get(&misc_dev->refcount);
945 }
946
947 init_waitqueue_head(&psp->sev_int_queue);
948 psp->sev_misc = misc_dev;
949 dev_dbg(dev, "registered SEV device\n");
950
951 return 0;
952}
953
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000954static int psp_check_sev_support(struct psp_device *psp)
Brijesh Singh200664d2017-12-04 10:57:28 -0600955{
Hook, Gary03f008c2019-10-21 13:44:44 +0000956 unsigned int val = ioread32(psp->io_regs + psp->vdata->feature_reg);
957
958 /*
959 * Check for a access to the registers. If this read returns
960 * 0xffffffff, it's likely that the system is running a broken
961 * BIOS which disallows access to the device. Stop here and
962 * fail the PSP initialization (but not the load, as the CCP
963 * could get properly initialized).
964 */
965 if (val == 0xffffffff) {
966 dev_notice(psp->dev, "psp: unable to access the device: you might be running a broken BIOS.\n");
967 return -ENODEV;
968 }
969
970 if (!(val & 1)) {
971 /* Device does not support the SEV feature */
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000972 dev_dbg(psp->dev, "psp does not support SEV\n");
973 return -ENODEV;
Brijesh Singh200664d2017-12-04 10:57:28 -0600974 }
975
Lendacky, Thomas7df52182019-02-15 17:26:33 +0000976 return 0;
Brijesh Singh200664d2017-12-04 10:57:28 -0600977}
978
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600979int psp_dev_init(struct sp_device *sp)
980{
981 struct device *dev = sp->dev;
982 struct psp_device *psp;
983 int ret;
984
985 ret = -ENOMEM;
986 psp = psp_alloc_struct(sp);
987 if (!psp)
988 goto e_err;
989
990 sp->psp_data = psp;
991
992 psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
993 if (!psp->vdata) {
994 ret = -ENODEV;
995 dev_err(dev, "missing driver data\n");
996 goto e_err;
997 }
998
Tom Lendackyad01a982018-07-03 12:12:03 -0500999 psp->io_regs = sp->io_map;
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001000
Lendacky, Thomas7df52182019-02-15 17:26:33 +00001001 ret = psp_check_sev_support(psp);
1002 if (ret)
1003 goto e_disable;
1004
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001005 /* Disable and clear interrupts until ready */
Tom Lendackyad01a982018-07-03 12:12:03 -05001006 iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
1007 iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001008
1009 /* Request an irq */
1010 ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
1011 if (ret) {
1012 dev_err(dev, "psp: unable to allocate an IRQ\n");
1013 goto e_err;
1014 }
1015
Lendacky, Thomas7df52182019-02-15 17:26:33 +00001016 ret = sev_misc_init(psp);
Brijesh Singh200664d2017-12-04 10:57:28 -06001017 if (ret)
1018 goto e_irq;
1019
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001020 if (sp->set_psp_master_device)
1021 sp->set_psp_master_device(sp);
1022
1023 /* Enable interrupt */
Tom Lendackyad01a982018-07-03 12:12:03 -05001024 iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001025
Tom Lendacky015c8c82018-07-03 12:11:42 -05001026 dev_notice(dev, "psp enabled\n");
1027
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001028 return 0;
1029
Brijesh Singh200664d2017-12-04 10:57:28 -06001030e_irq:
1031 sp_free_psp_irq(psp->sp, psp);
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001032e_err:
1033 sp->psp_data = NULL;
1034
1035 dev_notice(dev, "psp initialization failed\n");
1036
1037 return ret;
Lendacky, Thomas7df52182019-02-15 17:26:33 +00001038
1039e_disable:
1040 sp->psp_data = NULL;
1041
1042 return ret;
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001043}
1044
1045void psp_dev_destroy(struct sp_device *sp)
1046{
1047 struct psp_device *psp = sp->psp_data;
1048
Tom Lendackyafb31cd2018-07-26 09:37:59 -05001049 if (!psp)
1050 return;
1051
Brijesh Singh200664d2017-12-04 10:57:28 -06001052 if (psp->sev_misc)
1053 kref_put(&misc_dev->refcount, sev_exit);
1054
Brijesh Singh2a6170d2017-12-04 10:57:28 -06001055 sp_free_psp_irq(sp, psp);
1056}
Brijesh Singh200664d2017-12-04 10:57:28 -06001057
1058int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1059 void *data, int *error)
1060{
1061 if (!filep || filep->f_op != &sev_fops)
1062 return -EBADF;
1063
1064 return sev_do_cmd(cmd, data, error);
1065}
1066EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1067
1068void psp_pci_init(void)
1069{
Brijesh Singh200664d2017-12-04 10:57:28 -06001070 struct sp_device *sp;
1071 int error, rc;
1072
1073 sp = sp_get_psp_master_device();
1074 if (!sp)
1075 return;
1076
1077 psp_master = sp->psp_data;
1078
Brijesh Singhe82867f2018-08-15 16:11:25 -05001079 psp_timeout = psp_probe_timeout;
1080
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -05001081 if (sev_get_api_version())
1082 goto err;
1083
Singh, Brijeshf8903b32019-01-30 20:57:52 +00001084 /*
1085 * If platform is not in UNINIT state then firmware upgrade and/or
1086 * platform INIT command will fail. These command require UNINIT state.
1087 *
1088 * In a normal boot we should never run into case where the firmware
1089 * is not in UNINIT state on boot. But in case of kexec boot, a reboot
1090 * may not go through a typical shutdown sequence and may leave the
1091 * firmware in INIT or WORKING state.
1092 */
1093
1094 if (psp_master->sev_state != SEV_STATE_UNINIT) {
1095 sev_platform_shutdown(NULL);
1096 psp_master->sev_state = SEV_STATE_UNINIT;
1097 }
1098
David Rientjes83bf4252019-07-12 13:41:58 -07001099 if (sev_version_greater_or_equal(0, 15) &&
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -05001100 sev_update_firmware(psp_master->dev) == 0)
1101 sev_get_api_version();
1102
Brijesh Singh200664d2017-12-04 10:57:28 -06001103 /* Initialize the platform */
1104 rc = sev_platform_init(&error);
Ashish Kalra1d55fdc2019-10-17 22:35:11 +00001105 if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) {
1106 /*
1107 * INIT command returned an integrity check failure
1108 * status code, meaning that firmware load and
1109 * validation of SEV related persistent data has
1110 * failed and persistent state has been erased.
1111 * Retrying INIT command here should succeed.
1112 */
1113 dev_dbg(sp->dev, "SEV: retrying INIT command");
1114 rc = sev_platform_init(&error);
1115 }
1116
Brijesh Singh200664d2017-12-04 10:57:28 -06001117 if (rc) {
1118 dev_err(sp->dev, "SEV: failed to INIT error %#x\n", error);
Singh, Brijeshf5a2aeb82019-04-08 20:42:55 +00001119 return;
Brijesh Singh200664d2017-12-04 10:57:28 -06001120 }
1121
Janakarajan Natarajanedd303f2018-05-25 15:23:29 -05001122 dev_info(sp->dev, "SEV API:%d.%d build:%d\n", psp_master->api_major,
1123 psp_master->api_minor, psp_master->build);
Brijesh Singh200664d2017-12-04 10:57:28 -06001124
Brijesh Singh200664d2017-12-04 10:57:28 -06001125 return;
1126
1127err:
1128 psp_master = NULL;
1129}
1130
1131void psp_pci_exit(void)
1132{
1133 if (!psp_master)
1134 return;
1135
1136 sev_platform_shutdown(NULL);
1137}