sp-platform.c 4.7 KB

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