sp-platform.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Secure Processor device driver
  4. *
  5. * Copyright (C) 2014,2018 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Tom Lendacky <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/ioport.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/kthread.h>
  16. #include <linux/sched.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/delay.h>
  20. #include <linux/ccp.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/acpi.h>
  24. #include "ccp-dev.h"
  25. struct sp_platform {
  26. int coherent;
  27. unsigned int irq_count;
  28. };
  29. static const struct sp_dev_vdata dev_vdata[] = {
  30. {
  31. .bar = 0,
  32. #ifdef CONFIG_CRYPTO_DEV_SP_CCP
  33. .ccp_vdata = &ccpv3_platform,
  34. #endif
  35. },
  36. };
  37. #ifdef CONFIG_ACPI
  38. static const struct acpi_device_id sp_acpi_match[] = {
  39. { "AMDI0C00", (kernel_ulong_t)&dev_vdata[0] },
  40. { },
  41. };
  42. MODULE_DEVICE_TABLE(acpi, sp_acpi_match);
  43. #endif
  44. #ifdef CONFIG_OF
  45. static const struct of_device_id sp_of_match[] = {
  46. { .compatible = "amd,ccp-seattle-v1a",
  47. .data = (const void *)&dev_vdata[0] },
  48. { },
  49. };
  50. MODULE_DEVICE_TABLE(of, sp_of_match);
  51. #endif
  52. static struct sp_dev_vdata *sp_get_of_version(struct platform_device *pdev)
  53. {
  54. #ifdef CONFIG_OF
  55. const struct of_device_id *match;
  56. match = of_match_node(sp_of_match, pdev->dev.of_node);
  57. if (match && match->data)
  58. return (struct sp_dev_vdata *)match->data;
  59. #endif
  60. return NULL;
  61. }
  62. static struct sp_dev_vdata *sp_get_acpi_version(struct platform_device *pdev)
  63. {
  64. #ifdef CONFIG_ACPI
  65. const struct acpi_device_id *match;
  66. match = acpi_match_device(sp_acpi_match, &pdev->dev);
  67. if (match && match->driver_data)
  68. return (struct sp_dev_vdata *)match->driver_data;
  69. #endif
  70. return NULL;
  71. }
  72. static int sp_get_irqs(struct sp_device *sp)
  73. {
  74. struct sp_platform *sp_platform = sp->dev_specific;
  75. struct device *dev = sp->dev;
  76. struct platform_device *pdev = to_platform_device(dev);
  77. int ret;
  78. sp_platform->irq_count = platform_irq_count(pdev);
  79. ret = platform_get_irq(pdev, 0);
  80. if (ret < 0) {
  81. dev_notice(dev, "unable to get IRQ (%d)\n", ret);
  82. return ret;
  83. }
  84. sp->psp_irq = ret;
  85. if (sp_platform->irq_count == 1) {
  86. sp->ccp_irq = ret;
  87. } else {
  88. ret = platform_get_irq(pdev, 1);
  89. if (ret < 0) {
  90. dev_notice(dev, "unable to get IRQ (%d)\n", ret);
  91. return ret;
  92. }
  93. sp->ccp_irq = ret;
  94. }
  95. return 0;
  96. }
  97. static int sp_platform_probe(struct platform_device *pdev)
  98. {
  99. struct sp_device *sp;
  100. struct sp_platform *sp_platform;
  101. struct device *dev = &pdev->dev;
  102. enum dev_dma_attr attr;
  103. int ret;
  104. ret = -ENOMEM;
  105. sp = sp_alloc_struct(dev);
  106. if (!sp)
  107. goto e_err;
  108. sp_platform = devm_kzalloc(dev, sizeof(*sp_platform), GFP_KERNEL);
  109. if (!sp_platform)
  110. goto e_err;
  111. sp->dev_specific = sp_platform;
  112. sp->dev_vdata = pdev->dev.of_node ? sp_get_of_version(pdev)
  113. : sp_get_acpi_version(pdev);
  114. if (!sp->dev_vdata) {
  115. ret = -ENODEV;
  116. dev_err(dev, "missing driver data\n");
  117. goto e_err;
  118. }
  119. sp->io_map = devm_platform_ioremap_resource(pdev, 0);
  120. if (IS_ERR(sp->io_map)) {
  121. ret = PTR_ERR(sp->io_map);
  122. goto e_err;
  123. }
  124. attr = device_get_dma_attr(dev);
  125. if (attr == DEV_DMA_NOT_SUPPORTED) {
  126. dev_err(dev, "DMA is not supported");
  127. goto e_err;
  128. }
  129. sp_platform->coherent = (attr == DEV_DMA_COHERENT);
  130. if (sp_platform->coherent)
  131. sp->axcache = CACHE_WB_NO_ALLOC;
  132. else
  133. sp->axcache = CACHE_NONE;
  134. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
  135. if (ret) {
  136. dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
  137. goto e_err;
  138. }
  139. ret = sp_get_irqs(sp);
  140. if (ret)
  141. goto e_err;
  142. dev_set_drvdata(dev, sp);
  143. ret = sp_init(sp);
  144. if (ret)
  145. goto e_err;
  146. dev_notice(dev, "enabled\n");
  147. return 0;
  148. e_err:
  149. dev_notice(dev, "initialization failed\n");
  150. return ret;
  151. }
  152. static int sp_platform_remove(struct platform_device *pdev)
  153. {
  154. struct device *dev = &pdev->dev;
  155. struct sp_device *sp = dev_get_drvdata(dev);
  156. sp_destroy(sp);
  157. dev_notice(dev, "disabled\n");
  158. return 0;
  159. }
  160. #ifdef CONFIG_PM
  161. static int sp_platform_suspend(struct platform_device *pdev,
  162. pm_message_t state)
  163. {
  164. struct device *dev = &pdev->dev;
  165. struct sp_device *sp = dev_get_drvdata(dev);
  166. return sp_suspend(sp);
  167. }
  168. static int sp_platform_resume(struct platform_device *pdev)
  169. {
  170. struct device *dev = &pdev->dev;
  171. struct sp_device *sp = dev_get_drvdata(dev);
  172. return sp_resume(sp);
  173. }
  174. #endif
  175. static struct platform_driver sp_platform_driver = {
  176. .driver = {
  177. .name = "ccp",
  178. #ifdef CONFIG_ACPI
  179. .acpi_match_table = sp_acpi_match,
  180. #endif
  181. #ifdef CONFIG_OF
  182. .of_match_table = sp_of_match,
  183. #endif
  184. },
  185. .probe = sp_platform_probe,
  186. .remove = sp_platform_remove,
  187. #ifdef CONFIG_PM
  188. .suspend = sp_platform_suspend,
  189. .resume = sp_platform_resume,
  190. #endif
  191. };
  192. int sp_platform_init(void)
  193. {
  194. return platform_driver_register(&sp_platform_driver);
  195. }
  196. void sp_platform_exit(void)
  197. {
  198. platform_driver_unregister(&sp_platform_driver);
  199. }