sp-pci.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Secure Processor device driver
  4. *
  5. * Copyright (C) 2013,2019 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Tom Lendacky <[email protected]>
  8. * Author: Gary R Hook <[email protected]>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/device.h>
  13. #include <linux/pci.h>
  14. #include <linux/pci_ids.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/kthread.h>
  17. #include <linux/sched.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/delay.h>
  21. #include <linux/ccp.h>
  22. #include "ccp-dev.h"
  23. #include "psp-dev.h"
  24. #define MSIX_VECTORS 2
  25. struct sp_pci {
  26. int msix_count;
  27. struct msix_entry msix_entry[MSIX_VECTORS];
  28. };
  29. static struct sp_device *sp_dev_master;
  30. #define attribute_show(name, def) \
  31. static ssize_t name##_show(struct device *d, struct device_attribute *attr, \
  32. char *buf) \
  33. { \
  34. struct sp_device *sp = dev_get_drvdata(d); \
  35. struct psp_device *psp = sp->psp_data; \
  36. int bit = PSP_SECURITY_##def << PSP_CAPABILITY_PSP_SECURITY_OFFSET; \
  37. return sysfs_emit(buf, "%d\n", (psp->capability & bit) > 0); \
  38. }
  39. attribute_show(fused_part, FUSED_PART)
  40. static DEVICE_ATTR_RO(fused_part);
  41. attribute_show(debug_lock_on, DEBUG_LOCK_ON)
  42. static DEVICE_ATTR_RO(debug_lock_on);
  43. attribute_show(tsme_status, TSME_STATUS)
  44. static DEVICE_ATTR_RO(tsme_status);
  45. attribute_show(anti_rollback_status, ANTI_ROLLBACK_STATUS)
  46. static DEVICE_ATTR_RO(anti_rollback_status);
  47. attribute_show(rpmc_production_enabled, RPMC_PRODUCTION_ENABLED)
  48. static DEVICE_ATTR_RO(rpmc_production_enabled);
  49. attribute_show(rpmc_spirom_available, RPMC_SPIROM_AVAILABLE)
  50. static DEVICE_ATTR_RO(rpmc_spirom_available);
  51. attribute_show(hsp_tpm_available, HSP_TPM_AVAILABLE)
  52. static DEVICE_ATTR_RO(hsp_tpm_available);
  53. attribute_show(rom_armor_enforced, ROM_ARMOR_ENFORCED)
  54. static DEVICE_ATTR_RO(rom_armor_enforced);
  55. static struct attribute *psp_attrs[] = {
  56. &dev_attr_fused_part.attr,
  57. &dev_attr_debug_lock_on.attr,
  58. &dev_attr_tsme_status.attr,
  59. &dev_attr_anti_rollback_status.attr,
  60. &dev_attr_rpmc_production_enabled.attr,
  61. &dev_attr_rpmc_spirom_available.attr,
  62. &dev_attr_hsp_tpm_available.attr,
  63. &dev_attr_rom_armor_enforced.attr,
  64. NULL
  65. };
  66. static umode_t psp_security_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
  67. {
  68. struct device *dev = kobj_to_dev(kobj);
  69. struct sp_device *sp = dev_get_drvdata(dev);
  70. struct psp_device *psp = sp->psp_data;
  71. if (psp && (psp->capability & PSP_CAPABILITY_PSP_SECURITY_REPORTING))
  72. return 0444;
  73. return 0;
  74. }
  75. static struct attribute_group psp_attr_group = {
  76. .attrs = psp_attrs,
  77. .is_visible = psp_security_is_visible,
  78. };
  79. static const struct attribute_group *psp_groups[] = {
  80. &psp_attr_group,
  81. NULL,
  82. };
  83. static int sp_get_msix_irqs(struct sp_device *sp)
  84. {
  85. struct sp_pci *sp_pci = sp->dev_specific;
  86. struct device *dev = sp->dev;
  87. struct pci_dev *pdev = to_pci_dev(dev);
  88. int v, ret;
  89. for (v = 0; v < ARRAY_SIZE(sp_pci->msix_entry); v++)
  90. sp_pci->msix_entry[v].entry = v;
  91. ret = pci_enable_msix_range(pdev, sp_pci->msix_entry, 1, v);
  92. if (ret < 0)
  93. return ret;
  94. sp_pci->msix_count = ret;
  95. sp->use_tasklet = true;
  96. sp->psp_irq = sp_pci->msix_entry[0].vector;
  97. sp->ccp_irq = (sp_pci->msix_count > 1) ? sp_pci->msix_entry[1].vector
  98. : sp_pci->msix_entry[0].vector;
  99. return 0;
  100. }
  101. static int sp_get_msi_irq(struct sp_device *sp)
  102. {
  103. struct device *dev = sp->dev;
  104. struct pci_dev *pdev = to_pci_dev(dev);
  105. int ret;
  106. ret = pci_enable_msi(pdev);
  107. if (ret)
  108. return ret;
  109. sp->ccp_irq = pdev->irq;
  110. sp->psp_irq = pdev->irq;
  111. return 0;
  112. }
  113. static int sp_get_irqs(struct sp_device *sp)
  114. {
  115. struct device *dev = sp->dev;
  116. int ret;
  117. ret = sp_get_msix_irqs(sp);
  118. if (!ret)
  119. return 0;
  120. /* Couldn't get MSI-X vectors, try MSI */
  121. dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
  122. ret = sp_get_msi_irq(sp);
  123. if (!ret)
  124. return 0;
  125. /* Couldn't get MSI interrupt */
  126. dev_notice(dev, "could not enable MSI (%d)\n", ret);
  127. return ret;
  128. }
  129. static void sp_free_irqs(struct sp_device *sp)
  130. {
  131. struct sp_pci *sp_pci = sp->dev_specific;
  132. struct device *dev = sp->dev;
  133. struct pci_dev *pdev = to_pci_dev(dev);
  134. if (sp_pci->msix_count)
  135. pci_disable_msix(pdev);
  136. else if (sp->psp_irq)
  137. pci_disable_msi(pdev);
  138. sp->ccp_irq = 0;
  139. sp->psp_irq = 0;
  140. }
  141. static bool sp_pci_is_master(struct sp_device *sp)
  142. {
  143. struct device *dev_cur, *dev_new;
  144. struct pci_dev *pdev_cur, *pdev_new;
  145. dev_new = sp->dev;
  146. dev_cur = sp_dev_master->dev;
  147. pdev_new = to_pci_dev(dev_new);
  148. pdev_cur = to_pci_dev(dev_cur);
  149. if (pdev_new->bus->number < pdev_cur->bus->number)
  150. return true;
  151. if (PCI_SLOT(pdev_new->devfn) < PCI_SLOT(pdev_cur->devfn))
  152. return true;
  153. if (PCI_FUNC(pdev_new->devfn) < PCI_FUNC(pdev_cur->devfn))
  154. return true;
  155. return false;
  156. }
  157. static void psp_set_master(struct sp_device *sp)
  158. {
  159. if (!sp_dev_master) {
  160. sp_dev_master = sp;
  161. return;
  162. }
  163. if (sp_pci_is_master(sp))
  164. sp_dev_master = sp;
  165. }
  166. static struct sp_device *psp_get_master(void)
  167. {
  168. return sp_dev_master;
  169. }
  170. static void psp_clear_master(struct sp_device *sp)
  171. {
  172. if (sp == sp_dev_master) {
  173. sp_dev_master = NULL;
  174. dev_dbg(sp->dev, "Cleared sp_dev_master\n");
  175. }
  176. }
  177. static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  178. {
  179. struct sp_device *sp;
  180. struct sp_pci *sp_pci;
  181. struct device *dev = &pdev->dev;
  182. void __iomem * const *iomap_table;
  183. int bar_mask;
  184. int ret;
  185. ret = -ENOMEM;
  186. sp = sp_alloc_struct(dev);
  187. if (!sp)
  188. goto e_err;
  189. sp_pci = devm_kzalloc(dev, sizeof(*sp_pci), GFP_KERNEL);
  190. if (!sp_pci)
  191. goto e_err;
  192. sp->dev_specific = sp_pci;
  193. sp->dev_vdata = (struct sp_dev_vdata *)id->driver_data;
  194. if (!sp->dev_vdata) {
  195. ret = -ENODEV;
  196. dev_err(dev, "missing driver data\n");
  197. goto e_err;
  198. }
  199. ret = pcim_enable_device(pdev);
  200. if (ret) {
  201. dev_err(dev, "pcim_enable_device failed (%d)\n", ret);
  202. goto e_err;
  203. }
  204. bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
  205. ret = pcim_iomap_regions(pdev, bar_mask, "ccp");
  206. if (ret) {
  207. dev_err(dev, "pcim_iomap_regions failed (%d)\n", ret);
  208. goto e_err;
  209. }
  210. iomap_table = pcim_iomap_table(pdev);
  211. if (!iomap_table) {
  212. dev_err(dev, "pcim_iomap_table failed\n");
  213. ret = -ENOMEM;
  214. goto e_err;
  215. }
  216. sp->io_map = iomap_table[sp->dev_vdata->bar];
  217. if (!sp->io_map) {
  218. dev_err(dev, "ioremap failed\n");
  219. ret = -ENOMEM;
  220. goto e_err;
  221. }
  222. ret = sp_get_irqs(sp);
  223. if (ret)
  224. goto e_err;
  225. pci_set_master(pdev);
  226. sp->set_psp_master_device = psp_set_master;
  227. sp->get_psp_master_device = psp_get_master;
  228. sp->clear_psp_master_device = psp_clear_master;
  229. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
  230. if (ret) {
  231. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  232. if (ret) {
  233. dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n",
  234. ret);
  235. goto free_irqs;
  236. }
  237. }
  238. dev_set_drvdata(dev, sp);
  239. ret = sp_init(sp);
  240. if (ret)
  241. goto free_irqs;
  242. return 0;
  243. free_irqs:
  244. sp_free_irqs(sp);
  245. e_err:
  246. dev_notice(dev, "initialization failed\n");
  247. return ret;
  248. }
  249. static void sp_pci_shutdown(struct pci_dev *pdev)
  250. {
  251. struct device *dev = &pdev->dev;
  252. struct sp_device *sp = dev_get_drvdata(dev);
  253. if (!sp)
  254. return;
  255. sp_destroy(sp);
  256. }
  257. static void sp_pci_remove(struct pci_dev *pdev)
  258. {
  259. struct device *dev = &pdev->dev;
  260. struct sp_device *sp = dev_get_drvdata(dev);
  261. if (!sp)
  262. return;
  263. sp_destroy(sp);
  264. sp_free_irqs(sp);
  265. }
  266. static int __maybe_unused sp_pci_suspend(struct device *dev)
  267. {
  268. struct sp_device *sp = dev_get_drvdata(dev);
  269. return sp_suspend(sp);
  270. }
  271. static int __maybe_unused sp_pci_resume(struct device *dev)
  272. {
  273. struct sp_device *sp = dev_get_drvdata(dev);
  274. return sp_resume(sp);
  275. }
  276. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  277. static const struct sev_vdata sevv1 = {
  278. .cmdresp_reg = 0x10580,
  279. .cmdbuff_addr_lo_reg = 0x105e0,
  280. .cmdbuff_addr_hi_reg = 0x105e4,
  281. };
  282. static const struct sev_vdata sevv2 = {
  283. .cmdresp_reg = 0x10980,
  284. .cmdbuff_addr_lo_reg = 0x109e0,
  285. .cmdbuff_addr_hi_reg = 0x109e4,
  286. };
  287. static const struct tee_vdata teev1 = {
  288. .cmdresp_reg = 0x10544,
  289. .cmdbuff_addr_lo_reg = 0x10548,
  290. .cmdbuff_addr_hi_reg = 0x1054c,
  291. .ring_wptr_reg = 0x10550,
  292. .ring_rptr_reg = 0x10554,
  293. };
  294. static const struct psp_vdata pspv1 = {
  295. .sev = &sevv1,
  296. .feature_reg = 0x105fc,
  297. .inten_reg = 0x10610,
  298. .intsts_reg = 0x10614,
  299. };
  300. static const struct psp_vdata pspv2 = {
  301. .sev = &sevv2,
  302. .feature_reg = 0x109fc,
  303. .inten_reg = 0x10690,
  304. .intsts_reg = 0x10694,
  305. };
  306. static const struct psp_vdata pspv3 = {
  307. .tee = &teev1,
  308. .feature_reg = 0x109fc,
  309. .inten_reg = 0x10690,
  310. .intsts_reg = 0x10694,
  311. };
  312. static const struct psp_vdata pspv4 = {
  313. .sev = &sevv2,
  314. .tee = &teev1,
  315. .feature_reg = 0x109fc,
  316. .inten_reg = 0x10690,
  317. .intsts_reg = 0x10694,
  318. };
  319. #endif
  320. static const struct sp_dev_vdata dev_vdata[] = {
  321. { /* 0 */
  322. .bar = 2,
  323. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  324. .ccp_vdata = &ccpv3,
  325. #endif
  326. },
  327. { /* 1 */
  328. .bar = 2,
  329. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  330. .ccp_vdata = &ccpv5a,
  331. #endif
  332. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  333. .psp_vdata = &pspv1,
  334. #endif
  335. },
  336. { /* 2 */
  337. .bar = 2,
  338. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  339. .ccp_vdata = &ccpv5b,
  340. #endif
  341. },
  342. { /* 3 */
  343. .bar = 2,
  344. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  345. .ccp_vdata = &ccpv5a,
  346. #endif
  347. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  348. .psp_vdata = &pspv2,
  349. #endif
  350. },
  351. { /* 4 */
  352. .bar = 2,
  353. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  354. .ccp_vdata = &ccpv5a,
  355. #endif
  356. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  357. .psp_vdata = &pspv3,
  358. #endif
  359. },
  360. { /* 5 */
  361. .bar = 2,
  362. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  363. .psp_vdata = &pspv4,
  364. #endif
  365. },
  366. { /* 6 */
  367. .bar = 2,
  368. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  369. .psp_vdata = &pspv3,
  370. #endif
  371. },
  372. };
  373. static const struct pci_device_id sp_pci_table[] = {
  374. { PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&dev_vdata[0] },
  375. { PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&dev_vdata[1] },
  376. { PCI_VDEVICE(AMD, 0x1468), (kernel_ulong_t)&dev_vdata[2] },
  377. { PCI_VDEVICE(AMD, 0x1486), (kernel_ulong_t)&dev_vdata[3] },
  378. { PCI_VDEVICE(AMD, 0x15DF), (kernel_ulong_t)&dev_vdata[4] },
  379. { PCI_VDEVICE(AMD, 0x14CA), (kernel_ulong_t)&dev_vdata[5] },
  380. { PCI_VDEVICE(AMD, 0x15C7), (kernel_ulong_t)&dev_vdata[6] },
  381. { PCI_VDEVICE(AMD, 0x1649), (kernel_ulong_t)&dev_vdata[6] },
  382. /* Last entry must be zero */
  383. { 0, }
  384. };
  385. MODULE_DEVICE_TABLE(pci, sp_pci_table);
  386. static SIMPLE_DEV_PM_OPS(sp_pci_pm_ops, sp_pci_suspend, sp_pci_resume);
  387. static struct pci_driver sp_pci_driver = {
  388. .name = "ccp",
  389. .id_table = sp_pci_table,
  390. .probe = sp_pci_probe,
  391. .remove = sp_pci_remove,
  392. .shutdown = sp_pci_shutdown,
  393. .driver.pm = &sp_pci_pm_ops,
  394. .dev_groups = psp_groups,
  395. };
  396. int sp_pci_init(void)
  397. {
  398. return pci_register_driver(&sp_pci_driver);
  399. }
  400. void sp_pci_exit(void)
  401. {
  402. pci_unregister_driver(&sp_pci_driver);
  403. }