sp-platform.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #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. unsigned int i, count;
  78. int ret;
  79. for (i = 0, count = 0; i < pdev->num_resources; i++) {
  80. struct resource *res = &pdev->resource[i];
  81. if (resource_type(res) == IORESOURCE_IRQ)
  82. count++;
  83. }
  84. sp_platform->irq_count = count;
  85. ret = platform_get_irq(pdev, 0);
  86. if (ret < 0) {
  87. dev_notice(dev, "unable to get IRQ (%d)\n", ret);
  88. return ret;
  89. }
  90. sp->psp_irq = ret;
  91. if (count == 1) {
  92. sp->ccp_irq = ret;
  93. } else {
  94. ret = platform_get_irq(pdev, 1);
  95. if (ret < 0) {
  96. dev_notice(dev, "unable to get IRQ (%d)\n", ret);
  97. return ret;
  98. }
  99. sp->ccp_irq = ret;
  100. }
  101. return 0;
  102. }
  103. static int sp_platform_probe(struct platform_device *pdev)
  104. {
  105. struct sp_device *sp;
  106. struct sp_platform *sp_platform;
  107. struct device *dev = &pdev->dev;
  108. enum dev_dma_attr attr;
  109. int ret;
  110. ret = -ENOMEM;
  111. sp = sp_alloc_struct(dev);
  112. if (!sp)
  113. goto e_err;
  114. sp_platform = devm_kzalloc(dev, sizeof(*sp_platform), GFP_KERNEL);
  115. if (!sp_platform)
  116. goto e_err;
  117. sp->dev_specific = sp_platform;
  118. sp->dev_vdata = pdev->dev.of_node ? sp_get_of_version(pdev)
  119. : sp_get_acpi_version(pdev);
  120. if (!sp->dev_vdata) {
  121. ret = -ENODEV;
  122. dev_err(dev, "missing driver data\n");
  123. goto e_err;
  124. }
  125. sp->io_map = devm_platform_ioremap_resource(pdev, 0);
  126. if (IS_ERR(sp->io_map)) {
  127. ret = PTR_ERR(sp->io_map);
  128. goto e_err;
  129. }
  130. attr = device_get_dma_attr(dev);
  131. if (attr == DEV_DMA_NOT_SUPPORTED) {
  132. dev_err(dev, "DMA is not supported");
  133. goto e_err;
  134. }
  135. sp_platform->coherent = (attr == DEV_DMA_COHERENT);
  136. if (sp_platform->coherent)
  137. sp->axcache = CACHE_WB_NO_ALLOC;
  138. else
  139. sp->axcache = CACHE_NONE;
  140. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
  141. if (ret) {
  142. dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
  143. goto e_err;
  144. }
  145. ret = sp_get_irqs(sp);
  146. if (ret)
  147. goto e_err;
  148. dev_set_drvdata(dev, sp);
  149. ret = sp_init(sp);
  150. if (ret)
  151. goto e_err;
  152. dev_notice(dev, "enabled\n");
  153. return 0;
  154. e_err:
  155. dev_notice(dev, "initialization failed\n");
  156. return ret;
  157. }
  158. static int sp_platform_remove(struct platform_device *pdev)
  159. {
  160. struct device *dev = &pdev->dev;
  161. struct sp_device *sp = dev_get_drvdata(dev);
  162. sp_destroy(sp);
  163. dev_notice(dev, "disabled\n");
  164. return 0;
  165. }
  166. #ifdef CONFIG_PM
  167. static int sp_platform_suspend(struct platform_device *pdev,
  168. pm_message_t state)
  169. {
  170. struct device *dev = &pdev->dev;
  171. struct sp_device *sp = dev_get_drvdata(dev);
  172. return sp_suspend(sp, state);
  173. }
  174. static int sp_platform_resume(struct platform_device *pdev)
  175. {
  176. struct device *dev = &pdev->dev;
  177. struct sp_device *sp = dev_get_drvdata(dev);
  178. return sp_resume(sp);
  179. }
  180. #endif
  181. static struct platform_driver sp_platform_driver = {
  182. .driver = {
  183. .name = "ccp",
  184. #ifdef CONFIG_ACPI
  185. .acpi_match_table = sp_acpi_match,
  186. #endif
  187. #ifdef CONFIG_OF
  188. .of_match_table = sp_of_match,
  189. #endif
  190. },
  191. .probe = sp_platform_probe,
  192. .remove = sp_platform_remove,
  193. #ifdef CONFIG_PM
  194. .suspend = sp_platform_suspend,
  195. .resume = sp_platform_resume,
  196. #endif
  197. };
  198. int sp_platform_init(void)
  199. {
  200. return platform_driver_register(&sp_platform_driver);
  201. }
  202. void sp_platform_exit(void)
  203. {
  204. platform_driver_unregister(&sp_platform_driver);
  205. }