sp-dev.c 6.1 KB

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