psp-dev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * AMD Platform Security Processor (PSP) interface
  3. *
  4. * Copyright (C) 2016-2017 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Brijesh Singh <brijesh.singh@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kthread.h>
  15. #include <linux/sched.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/spinlock_types.h>
  19. #include <linux/types.h>
  20. #include <linux/mutex.h>
  21. #include <linux/delay.h>
  22. #include <linux/hw_random.h>
  23. #include <linux/ccp.h>
  24. #include <linux/firmware.h>
  25. #include "sp-dev.h"
  26. #include "psp-dev.h"
  27. #define SEV_VERSION_GREATER_OR_EQUAL(_maj, _min) \
  28. ((psp_master->api_major) >= _maj && \
  29. (psp_master->api_minor) >= _min)
  30. #define DEVICE_NAME "sev"
  31. #define SEV_FW_FILE "amd/sev.fw"
  32. static DEFINE_MUTEX(sev_cmd_mutex);
  33. static struct sev_misc_dev *misc_dev;
  34. static struct psp_device *psp_master;
  35. static int psp_cmd_timeout = 100;
  36. module_param(psp_cmd_timeout, int, 0644);
  37. MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
  38. static int psp_probe_timeout = 5;
  39. module_param(psp_probe_timeout, int, 0644);
  40. MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
  41. static bool psp_dead;
  42. static int psp_timeout;
  43. static struct psp_device *psp_alloc_struct(struct sp_device *sp)
  44. {
  45. struct device *dev = sp->dev;
  46. struct psp_device *psp;
  47. psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL);
  48. if (!psp)
  49. return NULL;
  50. psp->dev = dev;
  51. psp->sp = sp;
  52. snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord);
  53. return psp;
  54. }
  55. static irqreturn_t psp_irq_handler(int irq, void *data)
  56. {
  57. struct psp_device *psp = data;
  58. unsigned int status;
  59. int reg;
  60. /* Read the interrupt status: */
  61. status = ioread32(psp->io_regs + psp->vdata->intsts_reg);
  62. /* Check if it is command completion: */
  63. if (!(status & PSP_CMD_COMPLETE))
  64. goto done;
  65. /* Check if it is SEV command completion: */
  66. reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg);
  67. if (reg & PSP_CMDRESP_RESP) {
  68. psp->sev_int_rcvd = 1;
  69. wake_up(&psp->sev_int_queue);
  70. }
  71. done:
  72. /* Clear the interrupt status by writing the same value we read. */
  73. iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
  74. return IRQ_HANDLED;
  75. }
  76. static int sev_wait_cmd_ioc(struct psp_device *psp,
  77. unsigned int *reg, unsigned int timeout)
  78. {
  79. int ret;
  80. ret = wait_event_timeout(psp->sev_int_queue,
  81. psp->sev_int_rcvd, timeout * HZ);
  82. if (!ret)
  83. return -ETIMEDOUT;
  84. *reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg);
  85. return 0;
  86. }
  87. static int sev_cmd_buffer_len(int cmd)
  88. {
  89. switch (cmd) {
  90. case SEV_CMD_INIT: return sizeof(struct sev_data_init);
  91. case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
  92. case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
  93. case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
  94. case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
  95. case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
  96. case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
  97. case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
  98. case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
  99. case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
  100. case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
  101. case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
  102. case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
  103. case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
  104. case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
  105. case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
  106. case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
  107. case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
  108. case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
  109. case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
  110. case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
  111. case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
  112. case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
  113. case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
  114. case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
  115. case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
  116. case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
  117. default: return 0;
  118. }
  119. return 0;
  120. }
  121. static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
  122. {
  123. struct psp_device *psp = psp_master;
  124. unsigned int phys_lsb, phys_msb;
  125. unsigned int reg, ret = 0;
  126. if (!psp)
  127. return -ENODEV;
  128. if (psp_dead)
  129. return -EBUSY;
  130. /* Get the physical address of the command buffer */
  131. phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0;
  132. phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0;
  133. dev_dbg(psp->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
  134. cmd, phys_msb, phys_lsb, psp_timeout);
  135. print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
  136. sev_cmd_buffer_len(cmd), false);
  137. iowrite32(phys_lsb, psp->io_regs + psp->vdata->cmdbuff_addr_lo_reg);
  138. iowrite32(phys_msb, psp->io_regs + psp->vdata->cmdbuff_addr_hi_reg);
  139. psp->sev_int_rcvd = 0;
  140. reg = cmd;
  141. reg <<= PSP_CMDRESP_CMD_SHIFT;
  142. reg |= PSP_CMDRESP_IOC;
  143. iowrite32(reg, psp->io_regs + psp->vdata->cmdresp_reg);
  144. /* wait for command completion */
  145. ret = sev_wait_cmd_ioc(psp, &reg, psp_timeout);
  146. if (ret) {
  147. if (psp_ret)
  148. *psp_ret = 0;
  149. dev_err(psp->dev, "sev command %#x timed out, disabling PSP \n", cmd);
  150. psp_dead = true;
  151. return ret;
  152. }
  153. psp_timeout = psp_cmd_timeout;
  154. if (psp_ret)
  155. *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
  156. if (reg & PSP_CMDRESP_ERR_MASK) {
  157. dev_dbg(psp->dev, "sev command %#x failed (%#010x)\n",
  158. cmd, reg & PSP_CMDRESP_ERR_MASK);
  159. ret = -EIO;
  160. }
  161. print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
  162. sev_cmd_buffer_len(cmd), false);
  163. return ret;
  164. }
  165. static int sev_do_cmd(int cmd, void *data, int *psp_ret)
  166. {
  167. int rc;
  168. mutex_lock(&sev_cmd_mutex);
  169. rc = __sev_do_cmd_locked(cmd, data, psp_ret);
  170. mutex_unlock(&sev_cmd_mutex);
  171. return rc;
  172. }
  173. static int __sev_platform_init_locked(int *error)
  174. {
  175. struct psp_device *psp = psp_master;
  176. int rc = 0;
  177. if (!psp)
  178. return -ENODEV;
  179. if (psp->sev_state == SEV_STATE_INIT)
  180. return 0;
  181. rc = __sev_do_cmd_locked(SEV_CMD_INIT, &psp->init_cmd_buf, error);
  182. if (rc)
  183. return rc;
  184. psp->sev_state = SEV_STATE_INIT;
  185. dev_dbg(psp->dev, "SEV firmware initialized\n");
  186. return rc;
  187. }
  188. int sev_platform_init(int *error)
  189. {
  190. int rc;
  191. mutex_lock(&sev_cmd_mutex);
  192. rc = __sev_platform_init_locked(error);
  193. mutex_unlock(&sev_cmd_mutex);
  194. return rc;
  195. }
  196. EXPORT_SYMBOL_GPL(sev_platform_init);
  197. static int __sev_platform_shutdown_locked(int *error)
  198. {
  199. int ret;
  200. ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
  201. if (ret)
  202. return ret;
  203. psp_master->sev_state = SEV_STATE_UNINIT;
  204. dev_dbg(psp_master->dev, "SEV firmware shutdown\n");
  205. return ret;
  206. }
  207. static int sev_platform_shutdown(int *error)
  208. {
  209. int rc;
  210. mutex_lock(&sev_cmd_mutex);
  211. rc = __sev_platform_shutdown_locked(NULL);
  212. mutex_unlock(&sev_cmd_mutex);
  213. return rc;
  214. }
  215. static int sev_get_platform_state(int *state, int *error)
  216. {
  217. int rc;
  218. rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
  219. &psp_master->status_cmd_buf, error);
  220. if (rc)
  221. return rc;
  222. *state = psp_master->status_cmd_buf.state;
  223. return rc;
  224. }
  225. static int sev_ioctl_do_reset(struct sev_issue_cmd *argp)
  226. {
  227. int state, rc;
  228. /*
  229. * The SEV spec requires that FACTORY_RESET must be issued in
  230. * UNINIT state. Before we go further lets check if any guest is
  231. * active.
  232. *
  233. * If FW is in WORKING state then deny the request otherwise issue
  234. * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
  235. *
  236. */
  237. rc = sev_get_platform_state(&state, &argp->error);
  238. if (rc)
  239. return rc;
  240. if (state == SEV_STATE_WORKING)
  241. return -EBUSY;
  242. if (state == SEV_STATE_INIT) {
  243. rc = __sev_platform_shutdown_locked(&argp->error);
  244. if (rc)
  245. return rc;
  246. }
  247. return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
  248. }
  249. static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
  250. {
  251. struct sev_user_data_status *data = &psp_master->status_cmd_buf;
  252. int ret;
  253. ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
  254. if (ret)
  255. return ret;
  256. if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
  257. ret = -EFAULT;
  258. return ret;
  259. }
  260. static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)
  261. {
  262. int rc;
  263. if (psp_master->sev_state == SEV_STATE_UNINIT) {
  264. rc = __sev_platform_init_locked(&argp->error);
  265. if (rc)
  266. return rc;
  267. }
  268. return __sev_do_cmd_locked(cmd, NULL, &argp->error);
  269. }
  270. static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)
  271. {
  272. struct sev_user_data_pek_csr input;
  273. struct sev_data_pek_csr *data;
  274. void *blob = NULL;
  275. int ret;
  276. if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
  277. return -EFAULT;
  278. data = kzalloc(sizeof(*data), GFP_KERNEL);
  279. if (!data)
  280. return -ENOMEM;
  281. /* userspace wants to query CSR length */
  282. if (!input.address || !input.length)
  283. goto cmd;
  284. /* allocate a physically contiguous buffer to store the CSR blob */
  285. if (!access_ok(VERIFY_WRITE, input.address, input.length) ||
  286. input.length > SEV_FW_BLOB_MAX_SIZE) {
  287. ret = -EFAULT;
  288. goto e_free;
  289. }
  290. blob = kmalloc(input.length, GFP_KERNEL);
  291. if (!blob) {
  292. ret = -ENOMEM;
  293. goto e_free;
  294. }
  295. data->address = __psp_pa(blob);
  296. data->len = input.length;
  297. cmd:
  298. if (psp_master->sev_state == SEV_STATE_UNINIT) {
  299. ret = __sev_platform_init_locked(&argp->error);
  300. if (ret)
  301. goto e_free_blob;
  302. }
  303. ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
  304. /* If we query the CSR length, FW responded with expected data. */
  305. input.length = data->len;
  306. if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
  307. ret = -EFAULT;
  308. goto e_free_blob;
  309. }
  310. if (blob) {
  311. if (copy_to_user((void __user *)input.address, blob, input.length))
  312. ret = -EFAULT;
  313. }
  314. e_free_blob:
  315. kfree(blob);
  316. e_free:
  317. kfree(data);
  318. return ret;
  319. }
  320. void *psp_copy_user_blob(u64 __user uaddr, u32 len)
  321. {
  322. if (!uaddr || !len)
  323. return ERR_PTR(-EINVAL);
  324. /* verify that blob length does not exceed our limit */
  325. if (len > SEV_FW_BLOB_MAX_SIZE)
  326. return ERR_PTR(-EINVAL);
  327. return memdup_user((void __user *)(uintptr_t)uaddr, len);
  328. }
  329. EXPORT_SYMBOL_GPL(psp_copy_user_blob);
  330. static int sev_get_api_version(void)
  331. {
  332. struct sev_user_data_status *status;
  333. int error, ret;
  334. status = &psp_master->status_cmd_buf;
  335. ret = sev_platform_status(status, &error);
  336. if (ret) {
  337. dev_err(psp_master->dev,
  338. "SEV: failed to get status. Error: %#x\n", error);
  339. return 1;
  340. }
  341. psp_master->api_major = status->api_major;
  342. psp_master->api_minor = status->api_minor;
  343. psp_master->build = status->build;
  344. return 0;
  345. }
  346. /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
  347. static int sev_update_firmware(struct device *dev)
  348. {
  349. struct sev_data_download_firmware *data;
  350. const struct firmware *firmware;
  351. int ret, error, order;
  352. struct page *p;
  353. u64 data_size;
  354. ret = request_firmware(&firmware, SEV_FW_FILE, dev);
  355. if (ret < 0)
  356. return -1;
  357. /*
  358. * SEV FW expects the physical address given to it to be 32
  359. * byte aligned. Memory allocated has structure placed at the
  360. * beginning followed by the firmware being passed to the SEV
  361. * FW. Allocate enough memory for data structure + alignment
  362. * padding + SEV FW.
  363. */
  364. data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
  365. order = get_order(firmware->size + data_size);
  366. p = alloc_pages(GFP_KERNEL, order);
  367. if (!p) {
  368. ret = -1;
  369. goto fw_err;
  370. }
  371. /*
  372. * Copy firmware data to a kernel allocated contiguous
  373. * memory region.
  374. */
  375. data = page_address(p);
  376. memcpy(page_address(p) + data_size, firmware->data, firmware->size);
  377. data->address = __psp_pa(page_address(p) + data_size);
  378. data->len = firmware->size;
  379. ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
  380. if (ret)
  381. dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
  382. else
  383. dev_info(dev, "SEV firmware update successful\n");
  384. __free_pages(p, order);
  385. fw_err:
  386. release_firmware(firmware);
  387. return ret;
  388. }
  389. static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp)
  390. {
  391. struct sev_user_data_pek_cert_import input;
  392. struct sev_data_pek_cert_import *data;
  393. void *pek_blob, *oca_blob;
  394. int ret;
  395. if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
  396. return -EFAULT;
  397. data = kzalloc(sizeof(*data), GFP_KERNEL);
  398. if (!data)
  399. return -ENOMEM;
  400. /* copy PEK certificate blobs from userspace */
  401. pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
  402. if (IS_ERR(pek_blob)) {
  403. ret = PTR_ERR(pek_blob);
  404. goto e_free;
  405. }
  406. data->pek_cert_address = __psp_pa(pek_blob);
  407. data->pek_cert_len = input.pek_cert_len;
  408. /* copy PEK certificate blobs from userspace */
  409. oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
  410. if (IS_ERR(oca_blob)) {
  411. ret = PTR_ERR(oca_blob);
  412. goto e_free_pek;
  413. }
  414. data->oca_cert_address = __psp_pa(oca_blob);
  415. data->oca_cert_len = input.oca_cert_len;
  416. /* If platform is not in INIT state then transition it to INIT */
  417. if (psp_master->sev_state != SEV_STATE_INIT) {
  418. ret = __sev_platform_init_locked(&argp->error);
  419. if (ret)
  420. goto e_free_oca;
  421. }
  422. ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
  423. e_free_oca:
  424. kfree(oca_blob);
  425. e_free_pek:
  426. kfree(pek_blob);
  427. e_free:
  428. kfree(data);
  429. return ret;
  430. }
  431. static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
  432. {
  433. struct sev_data_get_id *data;
  434. u64 data_size, user_size;
  435. void *id_blob, *mem;
  436. int ret;
  437. /* SEV GET_ID available from SEV API v0.16 and up */
  438. if (!SEV_VERSION_GREATER_OR_EQUAL(0, 16))
  439. return -ENOTSUPP;
  440. /* SEV FW expects the buffer it fills with the ID to be
  441. * 8-byte aligned. Memory allocated should be enough to
  442. * hold data structure + alignment padding + memory
  443. * where SEV FW writes the ID.
  444. */
  445. data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
  446. user_size = sizeof(struct sev_user_data_get_id);
  447. mem = kzalloc(data_size + user_size, GFP_KERNEL);
  448. if (!mem)
  449. return -ENOMEM;
  450. data = mem;
  451. id_blob = mem + data_size;
  452. data->address = __psp_pa(id_blob);
  453. data->len = user_size;
  454. ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
  455. if (!ret) {
  456. if (copy_to_user((void __user *)argp->data, id_blob, data->len))
  457. ret = -EFAULT;
  458. }
  459. kfree(mem);
  460. return ret;
  461. }
  462. static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp)
  463. {
  464. struct sev_user_data_pdh_cert_export input;
  465. void *pdh_blob = NULL, *cert_blob = NULL;
  466. struct sev_data_pdh_cert_export *data;
  467. int ret;
  468. if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
  469. return -EFAULT;
  470. data = kzalloc(sizeof(*data), GFP_KERNEL);
  471. if (!data)
  472. return -ENOMEM;
  473. /* Userspace wants to query the certificate length. */
  474. if (!input.pdh_cert_address ||
  475. !input.pdh_cert_len ||
  476. !input.cert_chain_address)
  477. goto cmd;
  478. /* Allocate a physically contiguous buffer to store the PDH blob. */
  479. if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) ||
  480. !access_ok(VERIFY_WRITE, input.pdh_cert_address, input.pdh_cert_len)) {
  481. ret = -EFAULT;
  482. goto e_free;
  483. }
  484. /* Allocate a physically contiguous buffer to store the cert chain blob. */
  485. if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) ||
  486. !access_ok(VERIFY_WRITE, input.cert_chain_address, input.cert_chain_len)) {
  487. ret = -EFAULT;
  488. goto e_free;
  489. }
  490. pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
  491. if (!pdh_blob) {
  492. ret = -ENOMEM;
  493. goto e_free;
  494. }
  495. data->pdh_cert_address = __psp_pa(pdh_blob);
  496. data->pdh_cert_len = input.pdh_cert_len;
  497. cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
  498. if (!cert_blob) {
  499. ret = -ENOMEM;
  500. goto e_free_pdh;
  501. }
  502. data->cert_chain_address = __psp_pa(cert_blob);
  503. data->cert_chain_len = input.cert_chain_len;
  504. cmd:
  505. /* If platform is not in INIT state then transition it to INIT. */
  506. if (psp_master->sev_state != SEV_STATE_INIT) {
  507. ret = __sev_platform_init_locked(&argp->error);
  508. if (ret)
  509. goto e_free_cert;
  510. }
  511. ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
  512. /* If we query the length, FW responded with expected data. */
  513. input.cert_chain_len = data->cert_chain_len;
  514. input.pdh_cert_len = data->pdh_cert_len;
  515. if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
  516. ret = -EFAULT;
  517. goto e_free_cert;
  518. }
  519. if (pdh_blob) {
  520. if (copy_to_user((void __user *)input.pdh_cert_address,
  521. pdh_blob, input.pdh_cert_len)) {
  522. ret = -EFAULT;
  523. goto e_free_cert;
  524. }
  525. }
  526. if (cert_blob) {
  527. if (copy_to_user((void __user *)input.cert_chain_address,
  528. cert_blob, input.cert_chain_len))
  529. ret = -EFAULT;
  530. }
  531. e_free_cert:
  532. kfree(cert_blob);
  533. e_free_pdh:
  534. kfree(pdh_blob);
  535. e_free:
  536. kfree(data);
  537. return ret;
  538. }
  539. static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
  540. {
  541. void __user *argp = (void __user *)arg;
  542. struct sev_issue_cmd input;
  543. int ret = -EFAULT;
  544. if (!psp_master)
  545. return -ENODEV;
  546. if (ioctl != SEV_ISSUE_CMD)
  547. return -EINVAL;
  548. if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
  549. return -EFAULT;
  550. if (input.cmd > SEV_MAX)
  551. return -EINVAL;
  552. mutex_lock(&sev_cmd_mutex);
  553. switch (input.cmd) {
  554. case SEV_FACTORY_RESET:
  555. ret = sev_ioctl_do_reset(&input);
  556. break;
  557. case SEV_PLATFORM_STATUS:
  558. ret = sev_ioctl_do_platform_status(&input);
  559. break;
  560. case SEV_PEK_GEN:
  561. ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input);
  562. break;
  563. case SEV_PDH_GEN:
  564. ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input);
  565. break;
  566. case SEV_PEK_CSR:
  567. ret = sev_ioctl_do_pek_csr(&input);
  568. break;
  569. case SEV_PEK_CERT_IMPORT:
  570. ret = sev_ioctl_do_pek_import(&input);
  571. break;
  572. case SEV_PDH_CERT_EXPORT:
  573. ret = sev_ioctl_do_pdh_export(&input);
  574. break;
  575. case SEV_GET_ID:
  576. ret = sev_ioctl_do_get_id(&input);
  577. break;
  578. default:
  579. ret = -EINVAL;
  580. goto out;
  581. }
  582. if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
  583. ret = -EFAULT;
  584. out:
  585. mutex_unlock(&sev_cmd_mutex);
  586. return ret;
  587. }
  588. static const struct file_operations sev_fops = {
  589. .owner = THIS_MODULE,
  590. .unlocked_ioctl = sev_ioctl,
  591. };
  592. int sev_platform_status(struct sev_user_data_status *data, int *error)
  593. {
  594. return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
  595. }
  596. EXPORT_SYMBOL_GPL(sev_platform_status);
  597. int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
  598. {
  599. return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
  600. }
  601. EXPORT_SYMBOL_GPL(sev_guest_deactivate);
  602. int sev_guest_activate(struct sev_data_activate *data, int *error)
  603. {
  604. return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
  605. }
  606. EXPORT_SYMBOL_GPL(sev_guest_activate);
  607. int sev_guest_decommission(struct sev_data_decommission *data, int *error)
  608. {
  609. return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
  610. }
  611. EXPORT_SYMBOL_GPL(sev_guest_decommission);
  612. int sev_guest_df_flush(int *error)
  613. {
  614. return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
  615. }
  616. EXPORT_SYMBOL_GPL(sev_guest_df_flush);
  617. static void sev_exit(struct kref *ref)
  618. {
  619. struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount);
  620. misc_deregister(&misc_dev->misc);
  621. }
  622. static int sev_misc_init(struct psp_device *psp)
  623. {
  624. struct device *dev = psp->dev;
  625. int ret;
  626. /*
  627. * SEV feature support can be detected on multiple devices but the SEV
  628. * FW commands must be issued on the master. During probe, we do not
  629. * know the master hence we create /dev/sev on the first device probe.
  630. * sev_do_cmd() finds the right master device to which to issue the
  631. * command to the firmware.
  632. */
  633. if (!misc_dev) {
  634. struct miscdevice *misc;
  635. misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL);
  636. if (!misc_dev)
  637. return -ENOMEM;
  638. misc = &misc_dev->misc;
  639. misc->minor = MISC_DYNAMIC_MINOR;
  640. misc->name = DEVICE_NAME;
  641. misc->fops = &sev_fops;
  642. ret = misc_register(misc);
  643. if (ret)
  644. return ret;
  645. kref_init(&misc_dev->refcount);
  646. } else {
  647. kref_get(&misc_dev->refcount);
  648. }
  649. init_waitqueue_head(&psp->sev_int_queue);
  650. psp->sev_misc = misc_dev;
  651. dev_dbg(dev, "registered SEV device\n");
  652. return 0;
  653. }
  654. static int sev_init(struct psp_device *psp)
  655. {
  656. /* Check if device supports SEV feature */
  657. if (!(ioread32(psp->io_regs + psp->vdata->feature_reg) & 1)) {
  658. dev_dbg(psp->dev, "device does not support SEV\n");
  659. return 1;
  660. }
  661. return sev_misc_init(psp);
  662. }
  663. int psp_dev_init(struct sp_device *sp)
  664. {
  665. struct device *dev = sp->dev;
  666. struct psp_device *psp;
  667. int ret;
  668. ret = -ENOMEM;
  669. psp = psp_alloc_struct(sp);
  670. if (!psp)
  671. goto e_err;
  672. sp->psp_data = psp;
  673. psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
  674. if (!psp->vdata) {
  675. ret = -ENODEV;
  676. dev_err(dev, "missing driver data\n");
  677. goto e_err;
  678. }
  679. psp->io_regs = sp->io_map;
  680. /* Disable and clear interrupts until ready */
  681. iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
  682. iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
  683. /* Request an irq */
  684. ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
  685. if (ret) {
  686. dev_err(dev, "psp: unable to allocate an IRQ\n");
  687. goto e_err;
  688. }
  689. ret = sev_init(psp);
  690. if (ret)
  691. goto e_irq;
  692. if (sp->set_psp_master_device)
  693. sp->set_psp_master_device(sp);
  694. /* Enable interrupt */
  695. iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
  696. dev_notice(dev, "psp enabled\n");
  697. return 0;
  698. e_irq:
  699. sp_free_psp_irq(psp->sp, psp);
  700. e_err:
  701. sp->psp_data = NULL;
  702. dev_notice(dev, "psp initialization failed\n");
  703. return ret;
  704. }
  705. void psp_dev_destroy(struct sp_device *sp)
  706. {
  707. struct psp_device *psp = sp->psp_data;
  708. if (!psp)
  709. return;
  710. if (psp->sev_misc)
  711. kref_put(&misc_dev->refcount, sev_exit);
  712. sp_free_psp_irq(sp, psp);
  713. }
  714. int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
  715. void *data, int *error)
  716. {
  717. if (!filep || filep->f_op != &sev_fops)
  718. return -EBADF;
  719. return sev_do_cmd(cmd, data, error);
  720. }
  721. EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
  722. void psp_pci_init(void)
  723. {
  724. struct sp_device *sp;
  725. int error, rc;
  726. sp = sp_get_psp_master_device();
  727. if (!sp)
  728. return;
  729. psp_master = sp->psp_data;
  730. psp_timeout = psp_probe_timeout;
  731. if (sev_get_api_version())
  732. goto err;
  733. if (SEV_VERSION_GREATER_OR_EQUAL(0, 15) &&
  734. sev_update_firmware(psp_master->dev) == 0)
  735. sev_get_api_version();
  736. /* Initialize the platform */
  737. rc = sev_platform_init(&error);
  738. if (rc) {
  739. dev_err(sp->dev, "SEV: failed to INIT error %#x\n", error);
  740. return;
  741. }
  742. dev_info(sp->dev, "SEV API:%d.%d build:%d\n", psp_master->api_major,
  743. psp_master->api_minor, psp_master->build);
  744. return;
  745. err:
  746. psp_master = NULL;
  747. }
  748. void psp_pci_exit(void)
  749. {
  750. if (!psp_master)
  751. return;
  752. sev_platform_shutdown(NULL);
  753. }