psp-dev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 "sp-dev.h"
  25. #include "psp-dev.h"
  26. #define DEVICE_NAME "sev"
  27. static DEFINE_MUTEX(sev_cmd_mutex);
  28. static struct sev_misc_dev *misc_dev;
  29. static struct psp_device *psp_master;
  30. static struct psp_device *psp_alloc_struct(struct sp_device *sp)
  31. {
  32. struct device *dev = sp->dev;
  33. struct psp_device *psp;
  34. psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL);
  35. if (!psp)
  36. return NULL;
  37. psp->dev = dev;
  38. psp->sp = sp;
  39. snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord);
  40. return psp;
  41. }
  42. static irqreturn_t psp_irq_handler(int irq, void *data)
  43. {
  44. struct psp_device *psp = data;
  45. unsigned int status;
  46. int reg;
  47. /* Read the interrupt status: */
  48. status = ioread32(psp->io_regs + PSP_P2CMSG_INTSTS);
  49. /* Check if it is command completion: */
  50. if (!(status & BIT(PSP_CMD_COMPLETE_REG)))
  51. goto done;
  52. /* Check if it is SEV command completion: */
  53. reg = ioread32(psp->io_regs + PSP_CMDRESP);
  54. if (reg & PSP_CMDRESP_RESP) {
  55. psp->sev_int_rcvd = 1;
  56. wake_up(&psp->sev_int_queue);
  57. }
  58. done:
  59. /* Clear the interrupt status by writing the same value we read. */
  60. iowrite32(status, psp->io_regs + PSP_P2CMSG_INTSTS);
  61. return IRQ_HANDLED;
  62. }
  63. static void sev_wait_cmd_ioc(struct psp_device *psp, unsigned int *reg)
  64. {
  65. psp->sev_int_rcvd = 0;
  66. wait_event(psp->sev_int_queue, psp->sev_int_rcvd);
  67. *reg = ioread32(psp->io_regs + PSP_CMDRESP);
  68. }
  69. static int sev_cmd_buffer_len(int cmd)
  70. {
  71. switch (cmd) {
  72. case SEV_CMD_INIT: return sizeof(struct sev_data_init);
  73. case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
  74. case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
  75. case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
  76. case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
  77. case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
  78. case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
  79. case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
  80. case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
  81. case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
  82. case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
  83. case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
  84. case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
  85. case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
  86. case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
  87. case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
  88. case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
  89. case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
  90. case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
  91. case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
  92. case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
  93. case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
  94. case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
  95. case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
  96. case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
  97. default: return 0;
  98. }
  99. return 0;
  100. }
  101. static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
  102. {
  103. struct psp_device *psp = psp_master;
  104. unsigned int phys_lsb, phys_msb;
  105. unsigned int reg, ret = 0;
  106. if (!psp)
  107. return -ENODEV;
  108. /* Get the physical address of the command buffer */
  109. phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0;
  110. phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0;
  111. dev_dbg(psp->dev, "sev command id %#x buffer 0x%08x%08x\n",
  112. cmd, phys_msb, phys_lsb);
  113. print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
  114. sev_cmd_buffer_len(cmd), false);
  115. iowrite32(phys_lsb, psp->io_regs + PSP_CMDBUFF_ADDR_LO);
  116. iowrite32(phys_msb, psp->io_regs + PSP_CMDBUFF_ADDR_HI);
  117. reg = cmd;
  118. reg <<= PSP_CMDRESP_CMD_SHIFT;
  119. reg |= PSP_CMDRESP_IOC;
  120. iowrite32(reg, psp->io_regs + PSP_CMDRESP);
  121. /* wait for command completion */
  122. sev_wait_cmd_ioc(psp, &reg);
  123. if (psp_ret)
  124. *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
  125. if (reg & PSP_CMDRESP_ERR_MASK) {
  126. dev_dbg(psp->dev, "sev command %#x failed (%#010x)\n",
  127. cmd, reg & PSP_CMDRESP_ERR_MASK);
  128. ret = -EIO;
  129. }
  130. print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
  131. sev_cmd_buffer_len(cmd), false);
  132. return ret;
  133. }
  134. static int sev_do_cmd(int cmd, void *data, int *psp_ret)
  135. {
  136. int rc;
  137. mutex_lock(&sev_cmd_mutex);
  138. rc = __sev_do_cmd_locked(cmd, data, psp_ret);
  139. mutex_unlock(&sev_cmd_mutex);
  140. return rc;
  141. }
  142. static int __sev_platform_init_locked(int *error)
  143. {
  144. struct psp_device *psp = psp_master;
  145. int rc = 0;
  146. if (!psp)
  147. return -ENODEV;
  148. if (psp->sev_state == SEV_STATE_INIT)
  149. return 0;
  150. rc = __sev_do_cmd_locked(SEV_CMD_INIT, &psp->init_cmd_buf, error);
  151. if (rc)
  152. return rc;
  153. psp->sev_state = SEV_STATE_INIT;
  154. dev_dbg(psp->dev, "SEV firmware initialized\n");
  155. return rc;
  156. }
  157. int sev_platform_init(int *error)
  158. {
  159. int rc;
  160. mutex_lock(&sev_cmd_mutex);
  161. rc = __sev_platform_init_locked(error);
  162. mutex_unlock(&sev_cmd_mutex);
  163. return rc;
  164. }
  165. EXPORT_SYMBOL_GPL(sev_platform_init);
  166. static int __sev_platform_shutdown_locked(int *error)
  167. {
  168. int ret;
  169. ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
  170. if (ret)
  171. return ret;
  172. psp_master->sev_state = SEV_STATE_UNINIT;
  173. dev_dbg(psp_master->dev, "SEV firmware shutdown\n");
  174. return ret;
  175. }
  176. static int sev_platform_shutdown(int *error)
  177. {
  178. int rc;
  179. mutex_lock(&sev_cmd_mutex);
  180. rc = __sev_platform_shutdown_locked(NULL);
  181. mutex_unlock(&sev_cmd_mutex);
  182. return rc;
  183. }
  184. static int sev_get_platform_state(int *state, int *error)
  185. {
  186. int rc;
  187. rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
  188. &psp_master->status_cmd_buf, error);
  189. if (rc)
  190. return rc;
  191. *state = psp_master->status_cmd_buf.state;
  192. return rc;
  193. }
  194. static int sev_ioctl_do_reset(struct sev_issue_cmd *argp)
  195. {
  196. int state, rc;
  197. /*
  198. * The SEV spec requires that FACTORY_RESET must be issued in
  199. * UNINIT state. Before we go further lets check if any guest is
  200. * active.
  201. *
  202. * If FW is in WORKING state then deny the request otherwise issue
  203. * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
  204. *
  205. */
  206. rc = sev_get_platform_state(&state, &argp->error);
  207. if (rc)
  208. return rc;
  209. if (state == SEV_STATE_WORKING)
  210. return -EBUSY;
  211. if (state == SEV_STATE_INIT) {
  212. rc = __sev_platform_shutdown_locked(&argp->error);
  213. if (rc)
  214. return rc;
  215. }
  216. return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
  217. }
  218. static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
  219. {
  220. struct sev_user_data_status *data = &psp_master->status_cmd_buf;
  221. int ret;
  222. ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
  223. if (ret)
  224. return ret;
  225. if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
  226. ret = -EFAULT;
  227. return ret;
  228. }
  229. static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)
  230. {
  231. int rc;
  232. if (psp_master->sev_state == SEV_STATE_UNINIT) {
  233. rc = __sev_platform_init_locked(&argp->error);
  234. if (rc)
  235. return rc;
  236. }
  237. return __sev_do_cmd_locked(cmd, NULL, &argp->error);
  238. }
  239. static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)
  240. {
  241. struct sev_user_data_pek_csr input;
  242. struct sev_data_pek_csr *data;
  243. void *blob = NULL;
  244. int ret;
  245. if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
  246. return -EFAULT;
  247. data = kzalloc(sizeof(*data), GFP_KERNEL);
  248. if (!data)
  249. return -ENOMEM;
  250. /* userspace wants to query CSR length */
  251. if (!input.address || !input.length)
  252. goto cmd;
  253. /* allocate a physically contiguous buffer to store the CSR blob */
  254. if (!access_ok(VERIFY_WRITE, input.address, input.length) ||
  255. input.length > SEV_FW_BLOB_MAX_SIZE) {
  256. ret = -EFAULT;
  257. goto e_free;
  258. }
  259. blob = kmalloc(input.length, GFP_KERNEL);
  260. if (!blob) {
  261. ret = -ENOMEM;
  262. goto e_free;
  263. }
  264. data->address = __psp_pa(blob);
  265. data->len = input.length;
  266. cmd:
  267. if (psp_master->sev_state == SEV_STATE_UNINIT) {
  268. ret = __sev_platform_init_locked(&argp->error);
  269. if (ret)
  270. goto e_free_blob;
  271. }
  272. ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
  273. /* If we query the CSR length, FW responded with expected data. */
  274. input.length = data->len;
  275. if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
  276. ret = -EFAULT;
  277. goto e_free_blob;
  278. }
  279. if (blob) {
  280. if (copy_to_user((void __user *)input.address, blob, input.length))
  281. ret = -EFAULT;
  282. }
  283. e_free_blob:
  284. kfree(blob);
  285. e_free:
  286. kfree(data);
  287. return ret;
  288. }
  289. void *psp_copy_user_blob(u64 __user uaddr, u32 len)
  290. {
  291. if (!uaddr || !len)
  292. return ERR_PTR(-EINVAL);
  293. /* verify that blob length does not exceed our limit */
  294. if (len > SEV_FW_BLOB_MAX_SIZE)
  295. return ERR_PTR(-EINVAL);
  296. return memdup_user((void __user *)(uintptr_t)uaddr, len);
  297. }
  298. EXPORT_SYMBOL_GPL(psp_copy_user_blob);
  299. static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp)
  300. {
  301. struct sev_user_data_pek_cert_import input;
  302. struct sev_data_pek_cert_import *data;
  303. void *pek_blob, *oca_blob;
  304. int ret;
  305. if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
  306. return -EFAULT;
  307. data = kzalloc(sizeof(*data), GFP_KERNEL);
  308. if (!data)
  309. return -ENOMEM;
  310. /* copy PEK certificate blobs from userspace */
  311. pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
  312. if (IS_ERR(pek_blob)) {
  313. ret = PTR_ERR(pek_blob);
  314. goto e_free;
  315. }
  316. data->pek_cert_address = __psp_pa(pek_blob);
  317. data->pek_cert_len = input.pek_cert_len;
  318. /* copy PEK certificate blobs from userspace */
  319. oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
  320. if (IS_ERR(oca_blob)) {
  321. ret = PTR_ERR(oca_blob);
  322. goto e_free_pek;
  323. }
  324. data->oca_cert_address = __psp_pa(oca_blob);
  325. data->oca_cert_len = input.oca_cert_len;
  326. /* If platform is not in INIT state then transition it to INIT */
  327. if (psp_master->sev_state != SEV_STATE_INIT) {
  328. ret = __sev_platform_init_locked(&argp->error);
  329. if (ret)
  330. goto e_free_oca;
  331. }
  332. ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
  333. e_free_oca:
  334. kfree(oca_blob);
  335. e_free_pek:
  336. kfree(pek_blob);
  337. e_free:
  338. kfree(data);
  339. return ret;
  340. }
  341. static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp)
  342. {
  343. struct sev_user_data_pdh_cert_export input;
  344. void *pdh_blob = NULL, *cert_blob = NULL;
  345. struct sev_data_pdh_cert_export *data;
  346. int ret;
  347. if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
  348. return -EFAULT;
  349. data = kzalloc(sizeof(*data), GFP_KERNEL);
  350. if (!data)
  351. return -ENOMEM;
  352. /* Userspace wants to query the certificate length. */
  353. if (!input.pdh_cert_address ||
  354. !input.pdh_cert_len ||
  355. !input.cert_chain_address)
  356. goto cmd;
  357. /* Allocate a physically contiguous buffer to store the PDH blob. */
  358. if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) ||
  359. !access_ok(VERIFY_WRITE, input.pdh_cert_address, input.pdh_cert_len)) {
  360. ret = -EFAULT;
  361. goto e_free;
  362. }
  363. /* Allocate a physically contiguous buffer to store the cert chain blob. */
  364. if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) ||
  365. !access_ok(VERIFY_WRITE, input.cert_chain_address, input.cert_chain_len)) {
  366. ret = -EFAULT;
  367. goto e_free;
  368. }
  369. pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
  370. if (!pdh_blob) {
  371. ret = -ENOMEM;
  372. goto e_free;
  373. }
  374. data->pdh_cert_address = __psp_pa(pdh_blob);
  375. data->pdh_cert_len = input.pdh_cert_len;
  376. cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
  377. if (!cert_blob) {
  378. ret = -ENOMEM;
  379. goto e_free_pdh;
  380. }
  381. data->cert_chain_address = __psp_pa(cert_blob);
  382. data->cert_chain_len = input.cert_chain_len;
  383. cmd:
  384. /* If platform is not in INIT state then transition it to INIT. */
  385. if (psp_master->sev_state != SEV_STATE_INIT) {
  386. ret = __sev_platform_init_locked(&argp->error);
  387. if (ret)
  388. goto e_free_cert;
  389. }
  390. ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
  391. /* If we query the length, FW responded with expected data. */
  392. input.cert_chain_len = data->cert_chain_len;
  393. input.pdh_cert_len = data->pdh_cert_len;
  394. if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
  395. ret = -EFAULT;
  396. goto e_free_cert;
  397. }
  398. if (pdh_blob) {
  399. if (copy_to_user((void __user *)input.pdh_cert_address,
  400. pdh_blob, input.pdh_cert_len)) {
  401. ret = -EFAULT;
  402. goto e_free_cert;
  403. }
  404. }
  405. if (cert_blob) {
  406. if (copy_to_user((void __user *)input.cert_chain_address,
  407. cert_blob, input.cert_chain_len))
  408. ret = -EFAULT;
  409. }
  410. e_free_cert:
  411. kfree(cert_blob);
  412. e_free_pdh:
  413. kfree(pdh_blob);
  414. e_free:
  415. kfree(data);
  416. return ret;
  417. }
  418. static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
  419. {
  420. void __user *argp = (void __user *)arg;
  421. struct sev_issue_cmd input;
  422. int ret = -EFAULT;
  423. if (!psp_master)
  424. return -ENODEV;
  425. if (ioctl != SEV_ISSUE_CMD)
  426. return -EINVAL;
  427. if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
  428. return -EFAULT;
  429. if (input.cmd > SEV_MAX)
  430. return -EINVAL;
  431. mutex_lock(&sev_cmd_mutex);
  432. switch (input.cmd) {
  433. case SEV_FACTORY_RESET:
  434. ret = sev_ioctl_do_reset(&input);
  435. break;
  436. case SEV_PLATFORM_STATUS:
  437. ret = sev_ioctl_do_platform_status(&input);
  438. break;
  439. case SEV_PEK_GEN:
  440. ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input);
  441. break;
  442. case SEV_PDH_GEN:
  443. ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input);
  444. break;
  445. case SEV_PEK_CSR:
  446. ret = sev_ioctl_do_pek_csr(&input);
  447. break;
  448. case SEV_PEK_CERT_IMPORT:
  449. ret = sev_ioctl_do_pek_import(&input);
  450. break;
  451. case SEV_PDH_CERT_EXPORT:
  452. ret = sev_ioctl_do_pdh_export(&input);
  453. break;
  454. default:
  455. ret = -EINVAL;
  456. goto out;
  457. }
  458. if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
  459. ret = -EFAULT;
  460. out:
  461. mutex_unlock(&sev_cmd_mutex);
  462. return ret;
  463. }
  464. static const struct file_operations sev_fops = {
  465. .owner = THIS_MODULE,
  466. .unlocked_ioctl = sev_ioctl,
  467. };
  468. int sev_platform_status(struct sev_user_data_status *data, int *error)
  469. {
  470. return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
  471. }
  472. EXPORT_SYMBOL_GPL(sev_platform_status);
  473. int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
  474. {
  475. return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
  476. }
  477. EXPORT_SYMBOL_GPL(sev_guest_deactivate);
  478. int sev_guest_activate(struct sev_data_activate *data, int *error)
  479. {
  480. return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
  481. }
  482. EXPORT_SYMBOL_GPL(sev_guest_activate);
  483. int sev_guest_decommission(struct sev_data_decommission *data, int *error)
  484. {
  485. return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
  486. }
  487. EXPORT_SYMBOL_GPL(sev_guest_decommission);
  488. int sev_guest_df_flush(int *error)
  489. {
  490. return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
  491. }
  492. EXPORT_SYMBOL_GPL(sev_guest_df_flush);
  493. static void sev_exit(struct kref *ref)
  494. {
  495. struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount);
  496. misc_deregister(&misc_dev->misc);
  497. }
  498. static int sev_misc_init(struct psp_device *psp)
  499. {
  500. struct device *dev = psp->dev;
  501. int ret;
  502. /*
  503. * SEV feature support can be detected on multiple devices but the SEV
  504. * FW commands must be issued on the master. During probe, we do not
  505. * know the master hence we create /dev/sev on the first device probe.
  506. * sev_do_cmd() finds the right master device to which to issue the
  507. * command to the firmware.
  508. */
  509. if (!misc_dev) {
  510. struct miscdevice *misc;
  511. misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL);
  512. if (!misc_dev)
  513. return -ENOMEM;
  514. misc = &misc_dev->misc;
  515. misc->minor = MISC_DYNAMIC_MINOR;
  516. misc->name = DEVICE_NAME;
  517. misc->fops = &sev_fops;
  518. ret = misc_register(misc);
  519. if (ret)
  520. return ret;
  521. kref_init(&misc_dev->refcount);
  522. } else {
  523. kref_get(&misc_dev->refcount);
  524. }
  525. init_waitqueue_head(&psp->sev_int_queue);
  526. psp->sev_misc = misc_dev;
  527. dev_dbg(dev, "registered SEV device\n");
  528. return 0;
  529. }
  530. static int sev_init(struct psp_device *psp)
  531. {
  532. /* Check if device supports SEV feature */
  533. if (!(ioread32(psp->io_regs + PSP_FEATURE_REG) & 1)) {
  534. dev_dbg(psp->dev, "device does not support SEV\n");
  535. return 1;
  536. }
  537. return sev_misc_init(psp);
  538. }
  539. int psp_dev_init(struct sp_device *sp)
  540. {
  541. struct device *dev = sp->dev;
  542. struct psp_device *psp;
  543. int ret;
  544. ret = -ENOMEM;
  545. psp = psp_alloc_struct(sp);
  546. if (!psp)
  547. goto e_err;
  548. sp->psp_data = psp;
  549. psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
  550. if (!psp->vdata) {
  551. ret = -ENODEV;
  552. dev_err(dev, "missing driver data\n");
  553. goto e_err;
  554. }
  555. psp->io_regs = sp->io_map + psp->vdata->offset;
  556. /* Disable and clear interrupts until ready */
  557. iowrite32(0, psp->io_regs + PSP_P2CMSG_INTEN);
  558. iowrite32(-1, psp->io_regs + PSP_P2CMSG_INTSTS);
  559. /* Request an irq */
  560. ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
  561. if (ret) {
  562. dev_err(dev, "psp: unable to allocate an IRQ\n");
  563. goto e_err;
  564. }
  565. ret = sev_init(psp);
  566. if (ret)
  567. goto e_irq;
  568. if (sp->set_psp_master_device)
  569. sp->set_psp_master_device(sp);
  570. /* Enable interrupt */
  571. iowrite32(-1, psp->io_regs + PSP_P2CMSG_INTEN);
  572. return 0;
  573. e_irq:
  574. sp_free_psp_irq(psp->sp, psp);
  575. e_err:
  576. sp->psp_data = NULL;
  577. dev_notice(dev, "psp initialization failed\n");
  578. return ret;
  579. }
  580. void psp_dev_destroy(struct sp_device *sp)
  581. {
  582. struct psp_device *psp = sp->psp_data;
  583. if (psp->sev_misc)
  584. kref_put(&misc_dev->refcount, sev_exit);
  585. sp_free_psp_irq(sp, psp);
  586. }
  587. int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
  588. void *data, int *error)
  589. {
  590. if (!filep || filep->f_op != &sev_fops)
  591. return -EBADF;
  592. return sev_do_cmd(cmd, data, error);
  593. }
  594. EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
  595. void psp_pci_init(void)
  596. {
  597. struct sev_user_data_status *status;
  598. struct sp_device *sp;
  599. int error, rc;
  600. sp = sp_get_psp_master_device();
  601. if (!sp)
  602. return;
  603. psp_master = sp->psp_data;
  604. /* Initialize the platform */
  605. rc = sev_platform_init(&error);
  606. if (rc) {
  607. dev_err(sp->dev, "SEV: failed to INIT error %#x\n", error);
  608. goto err;
  609. }
  610. /* Display SEV firmware version */
  611. status = &psp_master->status_cmd_buf;
  612. rc = sev_platform_status(status, &error);
  613. if (rc) {
  614. dev_err(sp->dev, "SEV: failed to get status error %#x\n", error);
  615. goto err;
  616. }
  617. dev_info(sp->dev, "SEV API:%d.%d build:%d\n", status->api_major,
  618. status->api_minor, status->build);
  619. return;
  620. err:
  621. psp_master = NULL;
  622. }
  623. void psp_pci_exit(void)
  624. {
  625. if (!psp_master)
  626. return;
  627. sev_platform_shutdown(NULL);
  628. }