sp-platform.c 5.2 KB

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