ccp-pci.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@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/pci.h>
  15. #include <linux/pci_ids.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. #define IO_BAR 2
  24. #define MSIX_VECTORS 2
  25. struct ccp_msix {
  26. u32 vector;
  27. char name[16];
  28. };
  29. struct ccp_pci {
  30. int msix_count;
  31. struct ccp_msix msix[MSIX_VECTORS];
  32. };
  33. static int ccp_get_msix_irqs(struct ccp_device *ccp)
  34. {
  35. struct ccp_pci *ccp_pci = ccp->dev_specific;
  36. struct device *dev = ccp->dev;
  37. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  38. struct msix_entry msix_entry[MSIX_VECTORS];
  39. unsigned int name_len = sizeof(ccp_pci->msix[0].name) - 1;
  40. int v, ret;
  41. for (v = 0; v < ARRAY_SIZE(msix_entry); v++)
  42. msix_entry[v].entry = v;
  43. ret = pci_enable_msix_range(pdev, msix_entry, 1, v);
  44. if (ret < 0)
  45. return ret;
  46. ccp_pci->msix_count = ret;
  47. for (v = 0; v < ccp_pci->msix_count; v++) {
  48. /* Set the interrupt names and request the irqs */
  49. snprintf(ccp_pci->msix[v].name, name_len, "ccp-%u", v);
  50. ccp_pci->msix[v].vector = msix_entry[v].vector;
  51. ret = request_irq(ccp_pci->msix[v].vector, ccp_irq_handler,
  52. 0, ccp_pci->msix[v].name, dev);
  53. if (ret) {
  54. dev_notice(dev, "unable to allocate MSI-X IRQ (%d)\n",
  55. ret);
  56. goto e_irq;
  57. }
  58. }
  59. return 0;
  60. e_irq:
  61. while (v--)
  62. free_irq(ccp_pci->msix[v].vector, dev);
  63. pci_disable_msix(pdev);
  64. ccp_pci->msix_count = 0;
  65. return ret;
  66. }
  67. static int ccp_get_msi_irq(struct ccp_device *ccp)
  68. {
  69. struct device *dev = ccp->dev;
  70. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  71. int ret;
  72. ret = pci_enable_msi(pdev);
  73. if (ret)
  74. return ret;
  75. ret = request_irq(pdev->irq, ccp_irq_handler, 0, "ccp", dev);
  76. if (ret) {
  77. dev_notice(dev, "unable to allocate MSI IRQ (%d)\n", ret);
  78. goto e_msi;
  79. }
  80. return 0;
  81. e_msi:
  82. pci_disable_msi(pdev);
  83. return ret;
  84. }
  85. static int ccp_get_irqs(struct ccp_device *ccp)
  86. {
  87. struct device *dev = ccp->dev;
  88. int ret;
  89. ret = ccp_get_msix_irqs(ccp);
  90. if (!ret)
  91. return 0;
  92. /* Couldn't get MSI-X vectors, try MSI */
  93. dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
  94. ret = ccp_get_msi_irq(ccp);
  95. if (!ret)
  96. return 0;
  97. /* Couldn't get MSI interrupt */
  98. dev_notice(dev, "could not enable MSI (%d)\n", ret);
  99. return ret;
  100. }
  101. static void ccp_free_irqs(struct ccp_device *ccp)
  102. {
  103. struct ccp_pci *ccp_pci = ccp->dev_specific;
  104. struct device *dev = ccp->dev;
  105. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  106. if (ccp_pci->msix_count) {
  107. while (ccp_pci->msix_count--)
  108. free_irq(ccp_pci->msix[ccp_pci->msix_count].vector,
  109. dev);
  110. pci_disable_msix(pdev);
  111. } else {
  112. free_irq(pdev->irq, dev);
  113. pci_disable_msi(pdev);
  114. }
  115. }
  116. static int ccp_find_mmio_area(struct ccp_device *ccp)
  117. {
  118. struct device *dev = ccp->dev;
  119. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  120. resource_size_t io_len;
  121. unsigned long io_flags;
  122. int bar;
  123. io_flags = pci_resource_flags(pdev, IO_BAR);
  124. io_len = pci_resource_len(pdev, IO_BAR);
  125. if ((io_flags & IORESOURCE_MEM) && (io_len >= (IO_OFFSET + 0x800)))
  126. return IO_BAR;
  127. for (bar = 0; bar < PCI_STD_RESOURCE_END; bar++) {
  128. io_flags = pci_resource_flags(pdev, bar);
  129. io_len = pci_resource_len(pdev, bar);
  130. if ((io_flags & IORESOURCE_MEM) &&
  131. (io_len >= (IO_OFFSET + 0x800)))
  132. return bar;
  133. }
  134. return -EIO;
  135. }
  136. static int ccp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  137. {
  138. struct ccp_device *ccp;
  139. struct ccp_pci *ccp_pci;
  140. struct device *dev = &pdev->dev;
  141. unsigned int bar;
  142. int ret;
  143. ret = -ENOMEM;
  144. ccp = ccp_alloc_struct(dev);
  145. if (!ccp)
  146. goto e_err;
  147. ccp_pci = kzalloc(sizeof(*ccp_pci), GFP_KERNEL);
  148. if (!ccp_pci) {
  149. ret = -ENOMEM;
  150. goto e_free1;
  151. }
  152. ccp->dev_specific = ccp_pci;
  153. ccp->get_irq = ccp_get_irqs;
  154. ccp->free_irq = ccp_free_irqs;
  155. ret = pci_request_regions(pdev, "ccp");
  156. if (ret) {
  157. dev_err(dev, "pci_request_regions failed (%d)\n", ret);
  158. goto e_free2;
  159. }
  160. ret = pci_enable_device(pdev);
  161. if (ret) {
  162. dev_err(dev, "pci_enable_device failed (%d)\n", ret);
  163. goto e_regions;
  164. }
  165. pci_set_master(pdev);
  166. ret = ccp_find_mmio_area(ccp);
  167. if (ret < 0)
  168. goto e_device;
  169. bar = ret;
  170. ret = -EIO;
  171. ccp->io_map = pci_iomap(pdev, bar, 0);
  172. if (ccp->io_map == NULL) {
  173. dev_err(dev, "pci_iomap failed\n");
  174. goto e_device;
  175. }
  176. ccp->io_regs = ccp->io_map + IO_OFFSET;
  177. ret = dma_set_mask(dev, DMA_BIT_MASK(48));
  178. if (ret == 0) {
  179. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(48));
  180. if (ret) {
  181. dev_err(dev,
  182. "pci_set_consistent_dma_mask failed (%d)\n",
  183. ret);
  184. goto e_bar0;
  185. }
  186. } else {
  187. ret = dma_set_mask(dev, DMA_BIT_MASK(32));
  188. if (ret) {
  189. dev_err(dev, "pci_set_dma_mask failed (%d)\n", ret);
  190. goto e_bar0;
  191. }
  192. }
  193. dev_set_drvdata(dev, ccp);
  194. ret = ccp_init(ccp);
  195. if (ret)
  196. goto e_bar0;
  197. dev_notice(dev, "enabled\n");
  198. return 0;
  199. e_bar0:
  200. pci_iounmap(pdev, ccp->io_map);
  201. e_device:
  202. pci_disable_device(pdev);
  203. e_regions:
  204. pci_release_regions(pdev);
  205. e_free2:
  206. kfree(ccp_pci);
  207. e_free1:
  208. kfree(ccp);
  209. e_err:
  210. dev_notice(dev, "initialization failed\n");
  211. return ret;
  212. }
  213. static void ccp_pci_remove(struct pci_dev *pdev)
  214. {
  215. struct device *dev = &pdev->dev;
  216. struct ccp_device *ccp = dev_get_drvdata(dev);
  217. if (!ccp)
  218. return;
  219. ccp_destroy(ccp);
  220. pci_iounmap(pdev, ccp->io_map);
  221. pci_disable_device(pdev);
  222. pci_release_regions(pdev);
  223. kfree(ccp);
  224. dev_notice(dev, "disabled\n");
  225. }
  226. #ifdef CONFIG_PM
  227. static int ccp_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  228. {
  229. struct device *dev = &pdev->dev;
  230. struct ccp_device *ccp = dev_get_drvdata(dev);
  231. unsigned long flags;
  232. unsigned int i;
  233. spin_lock_irqsave(&ccp->cmd_lock, flags);
  234. ccp->suspending = 1;
  235. /* Wake all the queue kthreads to prepare for suspend */
  236. for (i = 0; i < ccp->cmd_q_count; i++)
  237. wake_up_process(ccp->cmd_q[i].kthread);
  238. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  239. /* Wait for all queue kthreads to say they're done */
  240. while (!ccp_queues_suspended(ccp))
  241. wait_event_interruptible(ccp->suspend_queue,
  242. ccp_queues_suspended(ccp));
  243. return 0;
  244. }
  245. static int ccp_pci_resume(struct pci_dev *pdev)
  246. {
  247. struct device *dev = &pdev->dev;
  248. struct ccp_device *ccp = dev_get_drvdata(dev);
  249. unsigned long flags;
  250. unsigned int i;
  251. spin_lock_irqsave(&ccp->cmd_lock, flags);
  252. ccp->suspending = 0;
  253. /* Wake up all the kthreads */
  254. for (i = 0; i < ccp->cmd_q_count; i++) {
  255. ccp->cmd_q[i].suspended = 0;
  256. wake_up_process(ccp->cmd_q[i].kthread);
  257. }
  258. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  259. return 0;
  260. }
  261. #endif
  262. static DEFINE_PCI_DEVICE_TABLE(ccp_pci_table) = {
  263. { PCI_VDEVICE(AMD, 0x1537), },
  264. /* Last entry must be zero */
  265. { 0, }
  266. };
  267. MODULE_DEVICE_TABLE(pci, ccp_pci_table);
  268. static struct pci_driver ccp_pci_driver = {
  269. .name = "AMD Cryptographic Coprocessor",
  270. .id_table = ccp_pci_table,
  271. .probe = ccp_pci_probe,
  272. .remove = ccp_pci_remove,
  273. #ifdef CONFIG_PM
  274. .suspend = ccp_pci_suspend,
  275. .resume = ccp_pci_resume,
  276. #endif
  277. };
  278. int ccp_pci_init(void)
  279. {
  280. return pci_register_driver(&ccp_pci_driver);
  281. }
  282. void ccp_pci_exit(void)
  283. {
  284. pci_unregister_driver(&ccp_pci_driver);
  285. }