psp-dev.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Platform Security Processor (PSP) interface
  4. *
  5. * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Brijesh Singh <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/irqreturn.h>
  11. #include "sp-dev.h"
  12. #include "psp-dev.h"
  13. #include "sev-dev.h"
  14. #include "tee-dev.h"
  15. struct psp_device *psp_master;
  16. static struct psp_device *psp_alloc_struct(struct sp_device *sp)
  17. {
  18. struct device *dev = sp->dev;
  19. struct psp_device *psp;
  20. psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL);
  21. if (!psp)
  22. return NULL;
  23. psp->dev = dev;
  24. psp->sp = sp;
  25. snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord);
  26. return psp;
  27. }
  28. static irqreturn_t psp_irq_handler(int irq, void *data)
  29. {
  30. struct psp_device *psp = data;
  31. unsigned int status;
  32. /* Read the interrupt status: */
  33. status = ioread32(psp->io_regs + psp->vdata->intsts_reg);
  34. /* Clear the interrupt status by writing the same value we read. */
  35. iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
  36. /* invoke subdevice interrupt handlers */
  37. if (status) {
  38. if (psp->sev_irq_handler)
  39. psp->sev_irq_handler(irq, psp->sev_irq_data, status);
  40. if (psp->tee_irq_handler)
  41. psp->tee_irq_handler(irq, psp->tee_irq_data, status);
  42. }
  43. return IRQ_HANDLED;
  44. }
  45. static unsigned int psp_get_capability(struct psp_device *psp)
  46. {
  47. unsigned int val = ioread32(psp->io_regs + psp->vdata->feature_reg);
  48. /*
  49. * Check for a access to the registers. If this read returns
  50. * 0xffffffff, it's likely that the system is running a broken
  51. * BIOS which disallows access to the device. Stop here and
  52. * fail the PSP initialization (but not the load, as the CCP
  53. * could get properly initialized).
  54. */
  55. if (val == 0xffffffff) {
  56. dev_notice(psp->dev, "psp: unable to access the device: you might be running a broken BIOS.\n");
  57. return -ENODEV;
  58. }
  59. psp->capability = val;
  60. /* Detect if TSME and SME are both enabled */
  61. if (psp->capability & PSP_CAPABILITY_PSP_SECURITY_REPORTING &&
  62. psp->capability & (PSP_SECURITY_TSME_STATUS << PSP_CAPABILITY_PSP_SECURITY_OFFSET) &&
  63. cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
  64. dev_notice(psp->dev, "psp: Both TSME and SME are active, SME is unnecessary when TSME is active.\n");
  65. return 0;
  66. }
  67. static int psp_check_sev_support(struct psp_device *psp)
  68. {
  69. /* Check if device supports SEV feature */
  70. if (!(psp->capability & PSP_CAPABILITY_SEV)) {
  71. dev_dbg(psp->dev, "psp does not support SEV\n");
  72. return -ENODEV;
  73. }
  74. return 0;
  75. }
  76. static int psp_check_tee_support(struct psp_device *psp)
  77. {
  78. /* Check if device supports TEE feature */
  79. if (!(psp->capability & PSP_CAPABILITY_TEE)) {
  80. dev_dbg(psp->dev, "psp does not support TEE\n");
  81. return -ENODEV;
  82. }
  83. return 0;
  84. }
  85. static int psp_init(struct psp_device *psp)
  86. {
  87. int ret;
  88. if (!psp_check_sev_support(psp)) {
  89. ret = sev_dev_init(psp);
  90. if (ret)
  91. return ret;
  92. }
  93. if (!psp_check_tee_support(psp)) {
  94. ret = tee_dev_init(psp);
  95. if (ret)
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. int psp_dev_init(struct sp_device *sp)
  101. {
  102. struct device *dev = sp->dev;
  103. struct psp_device *psp;
  104. int ret;
  105. ret = -ENOMEM;
  106. psp = psp_alloc_struct(sp);
  107. if (!psp)
  108. goto e_err;
  109. sp->psp_data = psp;
  110. psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
  111. if (!psp->vdata) {
  112. ret = -ENODEV;
  113. dev_err(dev, "missing driver data\n");
  114. goto e_err;
  115. }
  116. psp->io_regs = sp->io_map;
  117. ret = psp_get_capability(psp);
  118. if (ret)
  119. goto e_disable;
  120. /* Disable and clear interrupts until ready */
  121. iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
  122. iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
  123. /* Request an irq */
  124. ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
  125. if (ret) {
  126. dev_err(dev, "psp: unable to allocate an IRQ\n");
  127. goto e_err;
  128. }
  129. ret = psp_init(psp);
  130. if (ret)
  131. goto e_irq;
  132. if (sp->set_psp_master_device)
  133. sp->set_psp_master_device(sp);
  134. /* Enable interrupt */
  135. iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
  136. dev_notice(dev, "psp enabled\n");
  137. return 0;
  138. e_irq:
  139. sp_free_psp_irq(psp->sp, psp);
  140. e_err:
  141. sp->psp_data = NULL;
  142. dev_notice(dev, "psp initialization failed\n");
  143. return ret;
  144. e_disable:
  145. sp->psp_data = NULL;
  146. return ret;
  147. }
  148. void psp_dev_destroy(struct sp_device *sp)
  149. {
  150. struct psp_device *psp = sp->psp_data;
  151. if (!psp)
  152. return;
  153. sev_dev_destroy(psp);
  154. tee_dev_destroy(psp);
  155. sp_free_psp_irq(sp, psp);
  156. if (sp->clear_psp_master_device)
  157. sp->clear_psp_master_device(sp);
  158. }
  159. void psp_set_sev_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
  160. void *data)
  161. {
  162. psp->sev_irq_data = data;
  163. psp->sev_irq_handler = handler;
  164. }
  165. void psp_clear_sev_irq_handler(struct psp_device *psp)
  166. {
  167. psp_set_sev_irq_handler(psp, NULL, NULL);
  168. }
  169. void psp_set_tee_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
  170. void *data)
  171. {
  172. psp->tee_irq_data = data;
  173. psp->tee_irq_handler = handler;
  174. }
  175. void psp_clear_tee_irq_handler(struct psp_device *psp)
  176. {
  177. psp_set_tee_irq_handler(psp, NULL, NULL);
  178. }
  179. struct psp_device *psp_get_master_device(void)
  180. {
  181. struct sp_device *sp = sp_get_psp_master_device();
  182. return sp ? sp->psp_data : NULL;
  183. }
  184. void psp_pci_init(void)
  185. {
  186. psp_master = psp_get_master_device();
  187. if (!psp_master)
  188. return;
  189. sev_pci_init();
  190. }
  191. void psp_pci_exit(void)
  192. {
  193. if (!psp_master)
  194. return;
  195. sev_pci_exit();
  196. }