psp-dev.c 26 KB

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