sp-platform.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <thomas.lendacky@amd.com>
  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. static const struct acpi_device_id sp_acpi_match[] = {
  38. { "AMDI0C00", (kernel_ulong_t)&dev_vdata[0] },
  39. { },
  40. };
  41. MODULE_DEVICE_TABLE(acpi, sp_acpi_match);
  42. static const struct of_device_id sp_of_match[] = {
  43. { .compatible = "amd,ccp-seattle-v1a",
  44. .data = (const void *)&dev_vdata[0] },
  45. { },
  46. };
  47. MODULE_DEVICE_TABLE(of, sp_of_match);
  48. static const struct sp_dev_vdata *sp_get_acpi_version(struct platform_device *pdev)
  49. {
  50. const struct acpi_device_id *match;
  51. match = acpi_match_device(sp_acpi_match, &pdev->dev);
  52. if (match && match->driver_data)
  53. return (const struct sp_dev_vdata *)match->driver_data;
  54. return NULL;
  55. }
  56. static int sp_get_irqs(struct sp_device *sp)
  57. {
  58. struct sp_platform *sp_platform = sp->dev_specific;
  59. struct device *dev = sp->dev;
  60. struct platform_device *pdev = to_platform_device(dev);
  61. int ret;
  62. sp_platform->irq_count = platform_irq_count(pdev);
  63. ret = platform_get_irq(pdev, 0);
  64. if (ret < 0) {
  65. dev_notice(dev, "unable to get IRQ (%d)\n", ret);
  66. return ret;
  67. }
  68. sp->psp_irq = ret;
  69. if (sp_platform->irq_count == 1) {
  70. sp->ccp_irq = ret;
  71. } else {
  72. ret = platform_get_irq(pdev, 1);
  73. if (ret < 0) {
  74. dev_notice(dev, "unable to get IRQ (%d)\n", ret);
  75. return ret;
  76. }
  77. sp->ccp_irq = ret;
  78. }
  79. return 0;
  80. }
  81. static int sp_platform_probe(struct platform_device *pdev)
  82. {
  83. struct sp_device *sp;
  84. struct sp_platform *sp_platform;
  85. struct device *dev = &pdev->dev;
  86. enum dev_dma_attr attr;
  87. int ret;
  88. ret = -ENOMEM;
  89. sp = sp_alloc_struct(dev);
  90. if (!sp)
  91. goto e_err;
  92. sp_platform = devm_kzalloc(dev, sizeof(*sp_platform), GFP_KERNEL);
  93. if (!sp_platform)
  94. goto e_err;
  95. sp->dev_specific = sp_platform;
  96. sp->dev_vdata = pdev->dev.of_node ? of_device_get_match_data(&pdev->dev)
  97. : sp_get_acpi_version(pdev);
  98. if (!sp->dev_vdata) {
  99. ret = -ENODEV;
  100. dev_err(dev, "missing driver data\n");
  101. goto e_err;
  102. }
  103. sp->io_map = devm_platform_ioremap_resource(pdev, 0);
  104. if (IS_ERR(sp->io_map)) {
  105. ret = PTR_ERR(sp->io_map);
  106. goto e_err;
  107. }
  108. attr = device_get_dma_attr(dev);
  109. if (attr == DEV_DMA_NOT_SUPPORTED) {
  110. dev_err(dev, "DMA is not supported");
  111. goto e_err;
  112. }
  113. sp_platform->coherent = (attr == DEV_DMA_COHERENT);
  114. if (sp_platform->coherent)
  115. sp->axcache = CACHE_WB_NO_ALLOC;
  116. else
  117. sp->axcache = CACHE_NONE;
  118. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
  119. if (ret) {
  120. dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
  121. goto e_err;
  122. }
  123. ret = sp_get_irqs(sp);
  124. if (ret)
  125. goto e_err;
  126. dev_set_drvdata(dev, sp);
  127. ret = sp_init(sp);
  128. if (ret)
  129. goto e_err;
  130. dev_notice(dev, "enabled\n");
  131. return 0;
  132. e_err:
  133. dev_notice(dev, "initialization failed\n");
  134. return ret;
  135. }
  136. static void sp_platform_remove(struct platform_device *pdev)
  137. {
  138. struct device *dev = &pdev->dev;
  139. struct sp_device *sp = dev_get_drvdata(dev);
  140. sp_destroy(sp);
  141. dev_notice(dev, "disabled\n");
  142. }
  143. #ifdef CONFIG_PM
  144. static int sp_platform_suspend(struct platform_device *pdev,
  145. pm_message_t state)
  146. {
  147. struct device *dev = &pdev->dev;
  148. struct sp_device *sp = dev_get_drvdata(dev);
  149. return sp_suspend(sp);
  150. }
  151. static int sp_platform_resume(struct platform_device *pdev)
  152. {
  153. struct device *dev = &pdev->dev;
  154. struct sp_device *sp = dev_get_drvdata(dev);
  155. return sp_resume(sp);
  156. }
  157. #endif
  158. static struct platform_driver sp_platform_driver = {
  159. .driver = {
  160. .name = "ccp",
  161. .acpi_match_table = sp_acpi_match,
  162. .of_match_table = sp_of_match,
  163. },
  164. .probe = sp_platform_probe,
  165. .remove = sp_platform_remove,
  166. #ifdef CONFIG_PM
  167. .suspend = sp_platform_suspend,
  168. .resume = sp_platform_resume,
  169. #endif
  170. };
  171. int sp_platform_init(void)
  172. {
  173. return platform_driver_register(&sp_platform_driver);
  174. }
  175. void sp_platform_exit(void)
  176. {
  177. platform_driver_unregister(&sp_platform_driver);
  178. }