sp-pci.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #define MSIX_VECTORS 2
  27. struct sp_pci {
  28. int msix_count;
  29. struct msix_entry msix_entry[MSIX_VECTORS];
  30. };
  31. static int sp_get_msix_irqs(struct sp_device *sp)
  32. {
  33. struct sp_pci *sp_pci = sp->dev_specific;
  34. struct device *dev = sp->dev;
  35. struct pci_dev *pdev = to_pci_dev(dev);
  36. int v, ret;
  37. for (v = 0; v < ARRAY_SIZE(sp_pci->msix_entry); v++)
  38. sp_pci->msix_entry[v].entry = v;
  39. ret = pci_enable_msix_range(pdev, sp_pci->msix_entry, 1, v);
  40. if (ret < 0)
  41. return ret;
  42. sp_pci->msix_count = ret;
  43. sp->use_tasklet = true;
  44. sp->psp_irq = sp_pci->msix_entry[0].vector;
  45. sp->ccp_irq = (sp_pci->msix_count > 1) ? sp_pci->msix_entry[1].vector
  46. : sp_pci->msix_entry[0].vector;
  47. return 0;
  48. }
  49. static int sp_get_msi_irq(struct sp_device *sp)
  50. {
  51. struct device *dev = sp->dev;
  52. struct pci_dev *pdev = to_pci_dev(dev);
  53. int ret;
  54. ret = pci_enable_msi(pdev);
  55. if (ret)
  56. return ret;
  57. sp->ccp_irq = pdev->irq;
  58. sp->psp_irq = pdev->irq;
  59. return 0;
  60. }
  61. static int sp_get_irqs(struct sp_device *sp)
  62. {
  63. struct device *dev = sp->dev;
  64. int ret;
  65. ret = sp_get_msix_irqs(sp);
  66. if (!ret)
  67. return 0;
  68. /* Couldn't get MSI-X vectors, try MSI */
  69. dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
  70. ret = sp_get_msi_irq(sp);
  71. if (!ret)
  72. return 0;
  73. /* Couldn't get MSI interrupt */
  74. dev_notice(dev, "could not enable MSI (%d)\n", ret);
  75. return ret;
  76. }
  77. static void sp_free_irqs(struct sp_device *sp)
  78. {
  79. struct sp_pci *sp_pci = sp->dev_specific;
  80. struct device *dev = sp->dev;
  81. struct pci_dev *pdev = to_pci_dev(dev);
  82. if (sp_pci->msix_count)
  83. pci_disable_msix(pdev);
  84. else if (sp->psp_irq)
  85. pci_disable_msi(pdev);
  86. sp->ccp_irq = 0;
  87. sp->psp_irq = 0;
  88. }
  89. static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  90. {
  91. struct sp_device *sp;
  92. struct sp_pci *sp_pci;
  93. struct device *dev = &pdev->dev;
  94. void __iomem * const *iomap_table;
  95. int bar_mask;
  96. int ret;
  97. ret = -ENOMEM;
  98. sp = sp_alloc_struct(dev);
  99. if (!sp)
  100. goto e_err;
  101. sp_pci = devm_kzalloc(dev, sizeof(*sp_pci), GFP_KERNEL);
  102. if (!sp_pci)
  103. goto e_err;
  104. sp->dev_specific = sp_pci;
  105. sp->dev_vdata = (struct sp_dev_vdata *)id->driver_data;
  106. if (!sp->dev_vdata) {
  107. ret = -ENODEV;
  108. dev_err(dev, "missing driver data\n");
  109. goto e_err;
  110. }
  111. ret = pcim_enable_device(pdev);
  112. if (ret) {
  113. dev_err(dev, "pcim_enable_device failed (%d)\n", ret);
  114. goto e_err;
  115. }
  116. bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
  117. ret = pcim_iomap_regions(pdev, bar_mask, "ccp");
  118. if (ret) {
  119. dev_err(dev, "pcim_iomap_regions failed (%d)\n", ret);
  120. goto e_err;
  121. }
  122. iomap_table = pcim_iomap_table(pdev);
  123. if (!iomap_table) {
  124. dev_err(dev, "pcim_iomap_table failed\n");
  125. ret = -ENOMEM;
  126. goto e_err;
  127. }
  128. sp->io_map = iomap_table[sp->dev_vdata->bar];
  129. if (!sp->io_map) {
  130. dev_err(dev, "ioremap failed\n");
  131. ret = -ENOMEM;
  132. goto e_err;
  133. }
  134. ret = sp_get_irqs(sp);
  135. if (ret)
  136. goto e_err;
  137. pci_set_master(pdev);
  138. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
  139. if (ret) {
  140. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  141. if (ret) {
  142. dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n",
  143. ret);
  144. goto e_err;
  145. }
  146. }
  147. dev_set_drvdata(dev, sp);
  148. ret = sp_init(sp);
  149. if (ret)
  150. goto e_err;
  151. dev_notice(dev, "enabled\n");
  152. return 0;
  153. e_err:
  154. dev_notice(dev, "initialization failed\n");
  155. return ret;
  156. }
  157. static void sp_pci_remove(struct pci_dev *pdev)
  158. {
  159. struct device *dev = &pdev->dev;
  160. struct sp_device *sp = dev_get_drvdata(dev);
  161. if (!sp)
  162. return;
  163. sp_destroy(sp);
  164. sp_free_irqs(sp);
  165. dev_notice(dev, "disabled\n");
  166. }
  167. #ifdef CONFIG_PM
  168. static int sp_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  169. {
  170. struct device *dev = &pdev->dev;
  171. struct sp_device *sp = dev_get_drvdata(dev);
  172. return sp_suspend(sp, state);
  173. }
  174. static int sp_pci_resume(struct pci_dev *pdev)
  175. {
  176. struct device *dev = &pdev->dev;
  177. struct sp_device *sp = dev_get_drvdata(dev);
  178. return sp_resume(sp);
  179. }
  180. #endif
  181. static const struct sp_dev_vdata dev_vdata[] = {
  182. {
  183. .bar = 2,
  184. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  185. .ccp_vdata = &ccpv3,
  186. #endif
  187. },
  188. {
  189. .bar = 2,
  190. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  191. .ccp_vdata = &ccpv5a,
  192. #endif
  193. },
  194. {
  195. .bar = 2,
  196. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  197. .ccp_vdata = &ccpv5b,
  198. #endif
  199. },
  200. };
  201. static const struct pci_device_id sp_pci_table[] = {
  202. { PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&dev_vdata[0] },
  203. { PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&dev_vdata[1] },
  204. { PCI_VDEVICE(AMD, 0x1468), (kernel_ulong_t)&dev_vdata[2] },
  205. /* Last entry must be zero */
  206. { 0, }
  207. };
  208. MODULE_DEVICE_TABLE(pci, sp_pci_table);
  209. static struct pci_driver sp_pci_driver = {
  210. .name = "ccp",
  211. .id_table = sp_pci_table,
  212. .probe = sp_pci_probe,
  213. .remove = sp_pci_remove,
  214. #ifdef CONFIG_PM
  215. .suspend = sp_pci_suspend,
  216. .resume = sp_pci_resume,
  217. #endif
  218. };
  219. int sp_pci_init(void)
  220. {
  221. return pci_register_driver(&sp_pci_driver);
  222. }
  223. void sp_pci_exit(void)
  224. {
  225. pci_unregister_driver(&sp_pci_driver);
  226. }