sp-platform.c 5.3 KB

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