sp-dev.c 6.0 KB

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