sp-pci.c 7.1 KB

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