sp-dev.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Secure Processor driver
  4. *
  5. * Copyright (C) 2017-2018 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  8. * Author: Gary R Hook <gary.hook@amd.com>
  9. * Author: Brijesh Singh <brijesh.singh@amd.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kthread.h>
  14. #include <linux/sched.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/spinlock_types.h>
  18. #include <linux/types.h>
  19. #include <linux/ccp.h>
  20. #include "ccp-dev.h"
  21. #include "sp-dev.h"
  22. MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
  23. MODULE_AUTHOR("Gary R Hook <gary.hook@amd.com>");
  24. MODULE_LICENSE("GPL");
  25. MODULE_VERSION("1.1.0");
  26. MODULE_DESCRIPTION("AMD Secure Processor driver");
  27. /* List of SPs, SP count, read-write access lock, and access functions
  28. *
  29. * Lock structure: get sp_unit_lock for reading whenever we need to
  30. * examine the SP list.
  31. */
  32. static DEFINE_RWLOCK(sp_unit_lock);
  33. static LIST_HEAD(sp_units);
  34. /* Ever-increasing value to produce unique unit numbers */
  35. static atomic_t sp_ordinal;
  36. static void sp_add_device(struct sp_device *sp)
  37. {
  38. unsigned long flags;
  39. write_lock_irqsave(&sp_unit_lock, flags);
  40. list_add_tail(&sp->entry, &sp_units);
  41. write_unlock_irqrestore(&sp_unit_lock, flags);
  42. }
  43. static void sp_del_device(struct sp_device *sp)
  44. {
  45. unsigned long flags;
  46. write_lock_irqsave(&sp_unit_lock, flags);
  47. list_del(&sp->entry);
  48. write_unlock_irqrestore(&sp_unit_lock, flags);
  49. }
  50. static irqreturn_t sp_irq_handler(int irq, void *data)
  51. {
  52. struct sp_device *sp = data;
  53. if (sp->ccp_irq_handler)
  54. sp->ccp_irq_handler(irq, sp->ccp_irq_data);
  55. if (sp->psp_irq_handler)
  56. sp->psp_irq_handler(irq, sp->psp_irq_data);
  57. return IRQ_HANDLED;
  58. }
  59. int sp_request_ccp_irq(struct sp_device *sp, irq_handler_t handler,
  60. const char *name, void *data)
  61. {
  62. int ret;
  63. if ((sp->psp_irq == sp->ccp_irq) && sp->dev_vdata->psp_vdata) {
  64. /* Need a common routine to manage all interrupts */
  65. sp->ccp_irq_data = data;
  66. sp->ccp_irq_handler = handler;
  67. if (!sp->irq_registered) {
  68. ret = request_irq(sp->ccp_irq, sp_irq_handler, 0,
  69. sp->name, sp);
  70. if (ret)
  71. return ret;
  72. sp->irq_registered = true;
  73. }
  74. } else {
  75. /* Each sub-device can manage it's own interrupt */
  76. ret = request_irq(sp->ccp_irq, handler, 0, name, data);
  77. if (ret)
  78. return ret;
  79. }
  80. return 0;
  81. }
  82. int sp_request_psp_irq(struct sp_device *sp, irq_handler_t handler,
  83. const char *name, void *data)
  84. {
  85. int ret;
  86. if ((sp->psp_irq == sp->ccp_irq) && sp->dev_vdata->ccp_vdata) {
  87. /* Need a common routine to manage all interrupts */
  88. sp->psp_irq_data = data;
  89. sp->psp_irq_handler = handler;
  90. if (!sp->irq_registered) {
  91. ret = request_irq(sp->psp_irq, sp_irq_handler, 0,
  92. sp->name, sp);
  93. if (ret)
  94. return ret;
  95. sp->irq_registered = true;
  96. }
  97. } else {
  98. /* Each sub-device can manage it's own interrupt */
  99. ret = request_irq(sp->psp_irq, handler, 0, name, data);
  100. if (ret)
  101. return ret;
  102. }
  103. return 0;
  104. }
  105. void sp_free_ccp_irq(struct sp_device *sp, void *data)
  106. {
  107. if ((sp->psp_irq == sp->ccp_irq) && sp->dev_vdata->psp_vdata) {
  108. /* Using common routine to manage all interrupts */
  109. if (!sp->psp_irq_handler) {
  110. /* Nothing else using it, so free it */
  111. free_irq(sp->ccp_irq, sp);
  112. sp->irq_registered = false;
  113. }
  114. sp->ccp_irq_handler = NULL;
  115. sp->ccp_irq_data = NULL;
  116. } else {
  117. /* Each sub-device can manage it's own interrupt */
  118. free_irq(sp->ccp_irq, data);
  119. }
  120. }
  121. void sp_free_psp_irq(struct sp_device *sp, void *data)
  122. {
  123. if ((sp->psp_irq == sp->ccp_irq) && sp->dev_vdata->ccp_vdata) {
  124. /* Using common routine to manage all interrupts */
  125. if (!sp->ccp_irq_handler) {
  126. /* Nothing else using it, so free it */
  127. free_irq(sp->psp_irq, sp);
  128. sp->irq_registered = false;
  129. }
  130. sp->psp_irq_handler = NULL;
  131. sp->psp_irq_data = NULL;
  132. } else {
  133. /* Each sub-device can manage it's own interrupt */
  134. free_irq(sp->psp_irq, data);
  135. }
  136. }
  137. /**
  138. * sp_alloc_struct - allocate and initialize the sp_device struct
  139. *
  140. * @dev: device struct of the SP
  141. */
  142. struct sp_device *sp_alloc_struct(struct device *dev)
  143. {
  144. struct sp_device *sp;
  145. sp = devm_kzalloc(dev, sizeof(*sp), GFP_KERNEL);
  146. if (!sp)
  147. return NULL;
  148. sp->dev = dev;
  149. sp->ord = atomic_inc_return(&sp_ordinal);
  150. snprintf(sp->name, SP_MAX_NAME_LEN, "sp-%u", sp->ord);
  151. return sp;
  152. }
  153. int sp_init(struct sp_device *sp)
  154. {
  155. sp_add_device(sp);
  156. if (sp->dev_vdata->ccp_vdata)
  157. ccp_dev_init(sp);
  158. if (sp->dev_vdata->psp_vdata)
  159. psp_dev_init(sp);
  160. return 0;
  161. }
  162. void sp_destroy(struct sp_device *sp)
  163. {
  164. if (sp->dev_vdata->ccp_vdata)
  165. ccp_dev_destroy(sp);
  166. if (sp->dev_vdata->psp_vdata)
  167. psp_dev_destroy(sp);
  168. sp_del_device(sp);
  169. }
  170. #ifdef CONFIG_PM
  171. int sp_suspend(struct sp_device *sp, pm_message_t state)
  172. {
  173. int ret;
  174. if (sp->dev_vdata->ccp_vdata) {
  175. ret = ccp_dev_suspend(sp, state);
  176. if (ret)
  177. return ret;
  178. }
  179. return 0;
  180. }
  181. int sp_resume(struct sp_device *sp)
  182. {
  183. int ret;
  184. if (sp->dev_vdata->ccp_vdata) {
  185. ret = ccp_dev_resume(sp);
  186. if (ret)
  187. return ret;
  188. }
  189. return 0;
  190. }
  191. #endif
  192. struct sp_device *sp_get_psp_master_device(void)
  193. {
  194. struct sp_device *i, *ret = NULL;
  195. unsigned long flags;
  196. write_lock_irqsave(&sp_unit_lock, flags);
  197. if (list_empty(&sp_units))
  198. goto unlock;
  199. list_for_each_entry(i, &sp_units, entry) {
  200. if (i->psp_data && i->get_psp_master_device) {
  201. ret = i->get_psp_master_device();
  202. break;
  203. }
  204. }
  205. unlock:
  206. write_unlock_irqrestore(&sp_unit_lock, flags);
  207. return ret;
  208. }
  209. static int __init sp_mod_init(void)
  210. {
  211. #ifdef CONFIG_X86
  212. int ret;
  213. ret = sp_pci_init();
  214. if (ret)
  215. return ret;
  216. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  217. psp_pci_init();
  218. #endif
  219. return 0;
  220. #endif
  221. #ifdef CONFIG_ARM64
  222. int ret;
  223. ret = sp_platform_init();
  224. if (ret)
  225. return ret;
  226. return 0;
  227. #endif
  228. return -ENODEV;
  229. }
  230. static void __exit sp_mod_exit(void)
  231. {
  232. #ifdef CONFIG_X86
  233. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  234. psp_pci_exit();
  235. #endif
  236. sp_pci_exit();
  237. #endif
  238. #ifdef CONFIG_ARM64
  239. sp_platform_exit();
  240. #endif
  241. }
  242. module_init(sp_mod_init);
  243. module_exit(sp_mod_exit);