ccp-platform.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2014 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/device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/ioport.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/kthread.h>
  19. #include <linux/sched.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/delay.h>
  23. #include <linux/ccp.h>
  24. #include <linux/of.h>
  25. #include "ccp-dev.h"
  26. static int ccp_get_irq(struct ccp_device *ccp)
  27. {
  28. struct device *dev = ccp->dev;
  29. struct platform_device *pdev = container_of(dev,
  30. struct platform_device, dev);
  31. int ret;
  32. ret = platform_get_irq(pdev, 0);
  33. if (ret < 0)
  34. return ret;
  35. ccp->irq = ret;
  36. ret = request_irq(ccp->irq, ccp_irq_handler, 0, "ccp", dev);
  37. if (ret) {
  38. dev_notice(dev, "unable to allocate IRQ (%d)\n", ret);
  39. return ret;
  40. }
  41. return 0;
  42. }
  43. static int ccp_get_irqs(struct ccp_device *ccp)
  44. {
  45. struct device *dev = ccp->dev;
  46. int ret;
  47. ret = ccp_get_irq(ccp);
  48. if (!ret)
  49. return 0;
  50. /* Couldn't get an interrupt */
  51. dev_notice(dev, "could not enable interrupts (%d)\n", ret);
  52. return ret;
  53. }
  54. static void ccp_free_irqs(struct ccp_device *ccp)
  55. {
  56. struct device *dev = ccp->dev;
  57. free_irq(ccp->irq, dev);
  58. }
  59. static struct resource *ccp_find_mmio_area(struct ccp_device *ccp)
  60. {
  61. struct device *dev = ccp->dev;
  62. struct platform_device *pdev = container_of(dev,
  63. struct platform_device, dev);
  64. struct resource *ior;
  65. ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  66. if (ior && (resource_size(ior) >= 0x800))
  67. return ior;
  68. return NULL;
  69. }
  70. static int ccp_platform_probe(struct platform_device *pdev)
  71. {
  72. struct ccp_device *ccp;
  73. struct device *dev = &pdev->dev;
  74. struct resource *ior;
  75. int ret;
  76. ret = -ENOMEM;
  77. ccp = ccp_alloc_struct(dev);
  78. if (!ccp)
  79. goto e_err;
  80. ccp->dev_specific = NULL;
  81. ccp->get_irq = ccp_get_irqs;
  82. ccp->free_irq = ccp_free_irqs;
  83. ior = ccp_find_mmio_area(ccp);
  84. ccp->io_map = devm_ioremap_resource(dev, ior);
  85. if (IS_ERR(ccp->io_map)) {
  86. ret = PTR_ERR(ccp->io_map);
  87. goto e_free;
  88. }
  89. ccp->io_regs = ccp->io_map;
  90. if (!dev->dma_mask)
  91. dev->dma_mask = &dev->coherent_dma_mask;
  92. *(dev->dma_mask) = DMA_BIT_MASK(48);
  93. dev->coherent_dma_mask = DMA_BIT_MASK(48);
  94. if (of_property_read_bool(dev->of_node, "dma-coherent"))
  95. ccp->axcache = CACHE_WB_NO_ALLOC;
  96. else
  97. ccp->axcache = CACHE_NONE;
  98. dev_set_drvdata(dev, ccp);
  99. ret = ccp_init(ccp);
  100. if (ret)
  101. goto e_free;
  102. dev_notice(dev, "enabled\n");
  103. return 0;
  104. e_free:
  105. kfree(ccp);
  106. e_err:
  107. dev_notice(dev, "initialization failed\n");
  108. return ret;
  109. }
  110. static int ccp_platform_remove(struct platform_device *pdev)
  111. {
  112. struct device *dev = &pdev->dev;
  113. struct ccp_device *ccp = dev_get_drvdata(dev);
  114. ccp_destroy(ccp);
  115. kfree(ccp);
  116. dev_notice(dev, "disabled\n");
  117. return 0;
  118. }
  119. #ifdef CONFIG_PM
  120. static int ccp_platform_suspend(struct platform_device *pdev,
  121. pm_message_t state)
  122. {
  123. struct device *dev = &pdev->dev;
  124. struct ccp_device *ccp = dev_get_drvdata(dev);
  125. unsigned long flags;
  126. unsigned int i;
  127. spin_lock_irqsave(&ccp->cmd_lock, flags);
  128. ccp->suspending = 1;
  129. /* Wake all the queue kthreads to prepare for suspend */
  130. for (i = 0; i < ccp->cmd_q_count; i++)
  131. wake_up_process(ccp->cmd_q[i].kthread);
  132. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  133. /* Wait for all queue kthreads to say they're done */
  134. while (!ccp_queues_suspended(ccp))
  135. wait_event_interruptible(ccp->suspend_queue,
  136. ccp_queues_suspended(ccp));
  137. return 0;
  138. }
  139. static int ccp_platform_resume(struct platform_device *pdev)
  140. {
  141. struct device *dev = &pdev->dev;
  142. struct ccp_device *ccp = dev_get_drvdata(dev);
  143. unsigned long flags;
  144. unsigned int i;
  145. spin_lock_irqsave(&ccp->cmd_lock, flags);
  146. ccp->suspending = 0;
  147. /* Wake up all the kthreads */
  148. for (i = 0; i < ccp->cmd_q_count; i++) {
  149. ccp->cmd_q[i].suspended = 0;
  150. wake_up_process(ccp->cmd_q[i].kthread);
  151. }
  152. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  153. return 0;
  154. }
  155. #endif
  156. static const struct of_device_id ccp_platform_ids[] = {
  157. { .compatible = "amd,ccp-seattle-v1a" },
  158. { },
  159. };
  160. static struct platform_driver ccp_platform_driver = {
  161. .driver = {
  162. .name = "AMD Cryptographic Coprocessor",
  163. .of_match_table = ccp_platform_ids,
  164. },
  165. .probe = ccp_platform_probe,
  166. .remove = ccp_platform_remove,
  167. #ifdef CONFIG_PM
  168. .suspend = ccp_platform_suspend,
  169. .resume = ccp_platform_resume,
  170. #endif
  171. };
  172. int ccp_platform_init(void)
  173. {
  174. return platform_driver_register(&ccp_platform_driver);
  175. }
  176. void ccp_platform_exit(void)
  177. {
  178. platform_driver_unregister(&ccp_platform_driver);
  179. }