sp-dev.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <[email protected]>
  8. * Author: Gary R Hook <[email protected]>
  9. * Author: Brijesh Singh <[email protected]>
  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 <[email protected]>");
  23. MODULE_AUTHOR("Gary R Hook <[email protected]>");
  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. int sp_suspend(struct sp_device *sp)
  171. {
  172. if (sp->dev_vdata->ccp_vdata) {
  173. ccp_dev_suspend(sp);
  174. }
  175. return 0;
  176. }
  177. int sp_resume(struct sp_device *sp)
  178. {
  179. if (sp->dev_vdata->ccp_vdata) {
  180. ccp_dev_resume(sp);
  181. }
  182. return 0;
  183. }
  184. struct sp_device *sp_get_psp_master_device(void)
  185. {
  186. struct sp_device *i, *ret = NULL;
  187. unsigned long flags;
  188. write_lock_irqsave(&sp_unit_lock, flags);
  189. if (list_empty(&sp_units))
  190. goto unlock;
  191. list_for_each_entry(i, &sp_units, entry) {
  192. if (i->psp_data && i->get_psp_master_device) {
  193. ret = i->get_psp_master_device();
  194. break;
  195. }
  196. }
  197. unlock:
  198. write_unlock_irqrestore(&sp_unit_lock, flags);
  199. return ret;
  200. }
  201. static int __init sp_mod_init(void)
  202. {
  203. #ifdef CONFIG_X86
  204. int ret;
  205. ret = sp_pci_init();
  206. if (ret)
  207. return ret;
  208. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  209. psp_pci_init();
  210. #endif
  211. return 0;
  212. #endif
  213. #ifdef CONFIG_ARM64
  214. int ret;
  215. ret = sp_platform_init();
  216. if (ret)
  217. return ret;
  218. return 0;
  219. #endif
  220. return -ENODEV;
  221. }
  222. static void __exit sp_mod_exit(void)
  223. {
  224. #ifdef CONFIG_X86
  225. #ifdef CONFIG_CRYPTO_DEV_SP_PSP
  226. psp_pci_exit();
  227. #endif
  228. sp_pci_exit();
  229. #endif
  230. #ifdef CONFIG_ARM64
  231. sp_platform_exit();
  232. #endif
  233. }
  234. module_init(sp_mod_init);
  235. module_exit(sp_mod_exit);