intel_pxp_session.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright(c) 2020, Intel Corporation. All rights reserved.
  4. */
  5. #include "i915_drv.h"
  6. #include "intel_pxp.h"
  7. #include "intel_pxp_cmd.h"
  8. #include "intel_pxp_gsccs.h"
  9. #include "intel_pxp_session.h"
  10. #include "intel_pxp_tee.h"
  11. #include "intel_pxp_types.h"
  12. #include "intel_pxp_regs.h"
  13. #define ARB_SESSION I915_PROTECTED_CONTENT_DEFAULT_SESSION /* shorter define */
  14. static bool intel_pxp_session_is_in_play(struct intel_pxp *pxp, u32 id)
  15. {
  16. struct intel_uncore *uncore = pxp->ctrl_gt->uncore;
  17. intel_wakeref_t wakeref;
  18. u32 sip = 0;
  19. /* if we're suspended the session is considered off */
  20. with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref)
  21. sip = intel_uncore_read(uncore, KCR_SIP(pxp->kcr_base));
  22. return sip & BIT(id);
  23. }
  24. static int pxp_wait_for_session_state(struct intel_pxp *pxp, u32 id, bool in_play)
  25. {
  26. struct intel_uncore *uncore = pxp->ctrl_gt->uncore;
  27. intel_wakeref_t wakeref;
  28. u32 mask = BIT(id);
  29. int ret;
  30. /* if we're suspended the session is considered off */
  31. wakeref = intel_runtime_pm_get_if_in_use(uncore->rpm);
  32. if (!wakeref)
  33. return in_play ? -ENODEV : 0;
  34. ret = intel_wait_for_register(uncore,
  35. KCR_SIP(pxp->kcr_base),
  36. mask,
  37. in_play ? mask : 0,
  38. 250);
  39. intel_runtime_pm_put(uncore->rpm, wakeref);
  40. return ret;
  41. }
  42. static int pxp_create_arb_session(struct intel_pxp *pxp)
  43. {
  44. struct intel_gt *gt = pxp->ctrl_gt;
  45. int ret;
  46. pxp->arb_is_valid = false;
  47. if (intel_pxp_session_is_in_play(pxp, ARB_SESSION)) {
  48. drm_err(&gt->i915->drm, "arb session already in play at creation time\n");
  49. return -EEXIST;
  50. }
  51. if (HAS_ENGINE(pxp->ctrl_gt, GSC0))
  52. ret = intel_pxp_gsccs_create_session(pxp, ARB_SESSION);
  53. else
  54. ret = intel_pxp_tee_cmd_create_arb_session(pxp, ARB_SESSION);
  55. if (ret) {
  56. drm_err(&gt->i915->drm, "tee cmd for arb session creation failed\n");
  57. return ret;
  58. }
  59. ret = pxp_wait_for_session_state(pxp, ARB_SESSION, true);
  60. if (ret) {
  61. drm_dbg(&gt->i915->drm, "arb session failed to go in play\n");
  62. return ret;
  63. }
  64. drm_dbg(&gt->i915->drm, "PXP ARB session is alive\n");
  65. if (!++pxp->key_instance)
  66. ++pxp->key_instance;
  67. pxp->arb_is_valid = true;
  68. return 0;
  69. }
  70. static int pxp_terminate_arb_session_and_global(struct intel_pxp *pxp)
  71. {
  72. int ret;
  73. struct intel_gt *gt = pxp->ctrl_gt;
  74. /* must mark termination in progress calling this function */
  75. GEM_WARN_ON(pxp->arb_is_valid);
  76. /* terminate the hw sessions */
  77. ret = intel_pxp_terminate_session(pxp, ARB_SESSION);
  78. if (ret) {
  79. drm_err(&gt->i915->drm, "Failed to submit session termination\n");
  80. return ret;
  81. }
  82. ret = pxp_wait_for_session_state(pxp, ARB_SESSION, false);
  83. if (ret) {
  84. drm_err(&gt->i915->drm, "Session state did not clear\n");
  85. return ret;
  86. }
  87. intel_uncore_write(gt->uncore, KCR_GLOBAL_TERMINATE(pxp->kcr_base), 1);
  88. if (HAS_ENGINE(gt, GSC0))
  89. intel_pxp_gsccs_end_arb_fw_session(pxp, ARB_SESSION);
  90. else
  91. intel_pxp_tee_end_arb_fw_session(pxp, ARB_SESSION);
  92. return ret;
  93. }
  94. void intel_pxp_terminate(struct intel_pxp *pxp, bool post_invalidation_needs_restart)
  95. {
  96. int ret;
  97. pxp->hw_state_invalidated = post_invalidation_needs_restart;
  98. /*
  99. * if we fail to submit the termination there is no point in waiting for
  100. * it to complete. PXP will be marked as non-active until the next
  101. * termination is issued.
  102. */
  103. ret = pxp_terminate_arb_session_and_global(pxp);
  104. if (ret)
  105. complete_all(&pxp->termination);
  106. }
  107. static void pxp_terminate_complete(struct intel_pxp *pxp)
  108. {
  109. /* Re-create the arb session after teardown handle complete */
  110. if (fetch_and_zero(&pxp->hw_state_invalidated)) {
  111. drm_dbg(&pxp->ctrl_gt->i915->drm, "PXP: creating arb_session after invalidation");
  112. pxp_create_arb_session(pxp);
  113. }
  114. complete_all(&pxp->termination);
  115. }
  116. static void pxp_session_work(struct work_struct *work)
  117. {
  118. struct intel_pxp *pxp = container_of(work, typeof(*pxp), session_work);
  119. struct intel_gt *gt = pxp->ctrl_gt;
  120. intel_wakeref_t wakeref;
  121. u32 events = 0;
  122. spin_lock_irq(gt->irq_lock);
  123. events = fetch_and_zero(&pxp->session_events);
  124. spin_unlock_irq(gt->irq_lock);
  125. if (!events)
  126. return;
  127. drm_dbg(&gt->i915->drm, "PXP: processing event-flags 0x%08x", events);
  128. if (events & PXP_INVAL_REQUIRED)
  129. intel_pxp_invalidate(pxp);
  130. /*
  131. * If we're processing an event while suspending then don't bother,
  132. * we're going to re-init everything on resume anyway.
  133. */
  134. wakeref = intel_runtime_pm_get_if_in_use(gt->uncore->rpm);
  135. if (!wakeref)
  136. return;
  137. if (events & PXP_TERMINATION_REQUEST) {
  138. events &= ~PXP_TERMINATION_COMPLETE;
  139. intel_pxp_terminate(pxp, true);
  140. }
  141. if (events & PXP_TERMINATION_COMPLETE)
  142. pxp_terminate_complete(pxp);
  143. intel_runtime_pm_put(gt->uncore->rpm, wakeref);
  144. }
  145. void intel_pxp_session_management_init(struct intel_pxp *pxp)
  146. {
  147. mutex_init(&pxp->arb_mutex);
  148. INIT_WORK(&pxp->session_work, pxp_session_work);
  149. }