sp-pci.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Secure Processor device driver
  4. *
  5. * Copyright (C) 2013,2018 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  8. * Author: Gary R Hook <gary.hook@amd.com>
  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. static int sp_get_msix_irqs(struct sp_device *sp)
  31. {
  32. struct sp_pci *sp_pci = sp->dev_specific;
  33. struct device *dev = sp->dev;
  34. struct pci_dev *pdev = to_pci_dev(dev);
  35. int v, ret;
  36. for (v = 0; v < ARRAY_SIZE(sp_pci->msix_entry); v++)
  37. sp_pci->msix_entry[v].entry = v;
  38. ret = pci_enable_msix_range(pdev, sp_pci->msix_entry, 1, v);
  39. if (ret < 0)
  40. return ret;
  41. sp_pci->msix_count = ret;
  42. sp->use_tasklet = true;
  43. sp->psp_irq = sp_pci->msix_entry[0].vector;
  44. sp->ccp_irq = (sp_pci->msix_count > 1) ? sp_pci->msix_entry[1].vector
  45. : sp_pci->msix_entry[0].vector;
  46. return 0;
  47. }
  48. static int sp_get_msi_irq(struct sp_device *sp)
  49. {
  50. struct device *dev = sp->dev;
  51. struct pci_dev *pdev = to_pci_dev(dev);
  52. int ret;
  53. ret = pci_enable_msi(pdev);
  54. if (ret)
  55. return ret;
  56. sp->ccp_irq = pdev->irq;
  57. sp->psp_irq = pdev->irq;
  58. return 0;
  59. }
  60. static int sp_get_irqs(struct sp_device *sp)
  61. {
  62. struct device *dev = sp->dev;
  63. int ret;
  64. ret = sp_get_msix_irqs(sp);
  65. if (!ret)
  66. return 0;
  67. /* Couldn't get MSI-X vectors, try MSI */
  68. dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
  69. ret = sp_get_msi_irq(sp);
  70. if (!ret)
  71. return 0;
  72. /* Couldn't get MSI interrupt */
  73. dev_notice(dev, "could not enable MSI (%d)\n", ret);
  74. return ret;
  75. }
  76. static void sp_free_irqs(struct sp_device *sp)
  77. {
  78. struct sp_pci *sp_pci = sp->dev_specific;
  79. struct device *dev = sp->dev;
  80. struct pci_dev *pdev = to_pci_dev(dev);
  81. if (sp_pci->msix_count)
  82. pci_disable_msix(pdev);
  83. else if (sp->psp_irq)
  84. pci_disable_msi(pdev);
  85. sp->ccp_irq = 0;
  86. sp->psp_irq = 0;
  87. }
  88. static bool sp_pci_is_master(struct sp_device *sp)
  89. {
  90. struct device *dev_cur, *dev_new;
  91. struct pci_dev *pdev_cur, *pdev_new;
  92. dev_new = sp->dev;
  93. dev_cur = sp_dev_master->dev;
  94. pdev_new = to_pci_dev(dev_new);
  95. pdev_cur = to_pci_dev(dev_cur);
  96. if (pdev_new->bus->number < pdev_cur->bus->number)
  97. return true;
  98. if (PCI_SLOT(pdev_new->devfn) < PCI_SLOT(pdev_cur->devfn))
  99. return true;
  100. if (PCI_FUNC(pdev_new->devfn) < PCI_FUNC(pdev_cur->devfn))
  101. return true;
  102. return false;
  103. }
  104. static void psp_set_master(struct sp_device *sp)
  105. {
  106. if (!sp_dev_master) {
  107. sp_dev_master = sp;
  108. return;
  109. }
  110. if (sp_pci_is_master(sp))
  111. sp_dev_master = sp;
  112. }
  113. static struct sp_device *psp_get_master(void)
  114. {
  115. return sp_dev_master;
  116. }
  117. static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  118. {
  119. struct sp_device *sp;
  120. struct sp_pci *sp_pci;
  121. struct device *dev = &pdev->dev;
  122. void __iomem * const *iomap_table;
  123. int bar_mask;
  124. int ret;
  125. ret = -ENOMEM;
  126. sp = sp_alloc_struct(dev);
  127. if (!sp)
  128. goto e_err;
  129. sp_pci = devm_kzalloc(dev, sizeof(*sp_pci), GFP_KERNEL);
  130. if (!sp_pci)
  131. goto e_err;
  132. sp->dev_specific = sp_pci;
  133. sp->dev_vdata = (struct sp_dev_vdata *)id->driver_data;
  134. if (!sp->dev_vdata) {
  135. ret = -ENODEV;
  136. dev_err(dev, "missing driver data\n");
  137. goto e_err;
  138. }
  139. ret = pcim_enable_device(pdev);
  140. if (ret) {
  141. dev_err(dev, "pcim_enable_device failed (%d)\n", ret);
  142. goto e_err;
  143. }
  144. bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
  145. ret = pcim_iomap_regions(pdev, bar_mask, "ccp");
  146. if (ret) {
  147. dev_err(dev, "pcim_iomap_regions failed (%d)\n", ret);
  148. goto e_err;
  149. }
  150. iomap_table = pcim_iomap_table(pdev);
  151. if (!iomap_table) {
  152. dev_err(dev, "pcim_iomap_table failed\n");
  153. ret = -ENOMEM;
  154. goto e_err;
  155. }
  156. sp->io_map = iomap_table[sp->dev_vdata->bar];
  157. if (!sp->io_map) {
  158. dev_err(dev, "ioremap failed\n");
  159. ret = -ENOMEM;
  160. goto e_err;
  161. }
  162. ret = sp_get_irqs(sp);
  163. if (ret)
  164. goto e_err;
  165. pci_set_master(pdev);
  166. sp->set_psp_master_device = psp_set_master;
  167. sp->get_psp_master_device = psp_get_master;
  168. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
  169. if (ret) {
  170. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  171. if (ret) {
  172. dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n",
  173. ret);
  174. goto free_irqs;
  175. }
  176. }
  177. dev_set_drvdata(dev, sp);
  178. ret = sp_init(sp);
  179. if (ret)
  180. goto free_irqs;
  181. return 0;
  182. free_irqs:
  183. sp_free_irqs(sp);
  184. e_err:
  185. dev_notice(dev, "initialization failed\n");
  186. return ret;
  187. }
  188. static void sp_pci_remove(struct pci_dev *pdev)
  189. {
  190. struct device *dev = &pdev->dev;
  191. struct sp_device *sp = dev_get_drvdata(dev);
  192. if (!sp)
  193. return;
  194. sp_destroy(sp);
  195. sp_free_irqs(sp);
  196. }
  197. #ifdef CONFIG_PM
  198. static int sp_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  199. {
  200. struct device *dev = &pdev->dev;
  201. struct sp_device *sp = dev_get_drvdata(dev);
  202. return sp_suspend(sp, state);
  203. }
  204. static int sp_pci_resume(struct pci_dev *pdev)
  205. {
  206. struct device *dev = &pdev->dev;
  207. struct sp_device *sp = dev_get_drvdata(dev);
  208. return sp_resume(sp);
  209. }
  210. #endif
  211. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  212. static const struct psp_vdata pspv1 = {
  213. .cmdresp_reg = 0x10580,
  214. .cmdbuff_addr_lo_reg = 0x105e0,
  215. .cmdbuff_addr_hi_reg = 0x105e4,
  216. .feature_reg = 0x105fc,
  217. .inten_reg = 0x10610,
  218. .intsts_reg = 0x10614,
  219. };
  220. static const struct psp_vdata pspv2 = {
  221. .cmdresp_reg = 0x10980,
  222. .cmdbuff_addr_lo_reg = 0x109e0,
  223. .cmdbuff_addr_hi_reg = 0x109e4,
  224. .feature_reg = 0x109fc,
  225. .inten_reg = 0x10690,
  226. .intsts_reg = 0x10694,
  227. };
  228. #endif
  229. static const struct sp_dev_vdata dev_vdata[] = {
  230. { /* 0 */
  231. .bar = 2,
  232. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  233. .ccp_vdata = &ccpv3,
  234. #endif
  235. },
  236. { /* 1 */
  237. .bar = 2,
  238. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  239. .ccp_vdata = &ccpv5a,
  240. #endif
  241. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  242. .psp_vdata = &pspv1,
  243. #endif
  244. },
  245. { /* 2 */
  246. .bar = 2,
  247. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  248. .ccp_vdata = &ccpv5b,
  249. #endif
  250. },
  251. { /* 3 */
  252. .bar = 2,
  253. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  254. .ccp_vdata = &ccpv5a,
  255. #endif
  256. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  257. .psp_vdata = &pspv2,
  258. #endif
  259. },
  260. };
  261. static const struct pci_device_id sp_pci_table[] = {
  262. { PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&dev_vdata[0] },
  263. { PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&dev_vdata[1] },
  264. { PCI_VDEVICE(AMD, 0x1468), (kernel_ulong_t)&dev_vdata[2] },
  265. { PCI_VDEVICE(AMD, 0x1486), (kernel_ulong_t)&dev_vdata[3] },
  266. /* Last entry must be zero */
  267. { 0, }
  268. };
  269. MODULE_DEVICE_TABLE(pci, sp_pci_table);
  270. static struct pci_driver sp_pci_driver = {
  271. .name = "ccp",
  272. .id_table = sp_pci_table,
  273. .probe = sp_pci_probe,
  274. .remove = sp_pci_remove,
  275. #ifdef CONFIG_PM
  276. .suspend = sp_pci_suspend,
  277. .resume = sp_pci_resume,
  278. #endif
  279. };
  280. int sp_pci_init(void)
  281. {
  282. return pci_register_driver(&sp_pci_driver);
  283. }
  284. void sp_pci_exit(void)
  285. {
  286. pci_unregister_driver(&sp_pci_driver);
  287. }