Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: MIT
0002 /*
0003  * Copyright(c) 2020, Intel Corporation. All rights reserved.
0004  */
0005 
0006 #include "i915_drv.h"
0007 
0008 #include "intel_pxp.h"
0009 #include "intel_pxp_cmd.h"
0010 #include "intel_pxp_session.h"
0011 #include "intel_pxp_tee.h"
0012 #include "intel_pxp_types.h"
0013 
0014 #define ARB_SESSION I915_PROTECTED_CONTENT_DEFAULT_SESSION /* shorter define */
0015 
0016 #define GEN12_KCR_SIP _MMIO(0x32260) /* KCR hwdrm session in play 0-31 */
0017 
0018 /* PXP global terminate register for session termination */
0019 #define PXP_GLOBAL_TERMINATE _MMIO(0x320f8)
0020 
0021 static bool intel_pxp_session_is_in_play(struct intel_pxp *pxp, u32 id)
0022 {
0023     struct intel_uncore *uncore = pxp_to_gt(pxp)->uncore;
0024     intel_wakeref_t wakeref;
0025     u32 sip = 0;
0026 
0027     /* if we're suspended the session is considered off */
0028     with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref)
0029         sip = intel_uncore_read(uncore, GEN12_KCR_SIP);
0030 
0031     return sip & BIT(id);
0032 }
0033 
0034 static int pxp_wait_for_session_state(struct intel_pxp *pxp, u32 id, bool in_play)
0035 {
0036     struct intel_uncore *uncore = pxp_to_gt(pxp)->uncore;
0037     intel_wakeref_t wakeref;
0038     u32 mask = BIT(id);
0039     int ret;
0040 
0041     /* if we're suspended the session is considered off */
0042     wakeref = intel_runtime_pm_get_if_in_use(uncore->rpm);
0043     if (!wakeref)
0044         return in_play ? -ENODEV : 0;
0045 
0046     ret = intel_wait_for_register(uncore,
0047                       GEN12_KCR_SIP,
0048                       mask,
0049                       in_play ? mask : 0,
0050                       100);
0051 
0052     intel_runtime_pm_put(uncore->rpm, wakeref);
0053 
0054     return ret;
0055 }
0056 
0057 static int pxp_create_arb_session(struct intel_pxp *pxp)
0058 {
0059     struct intel_gt *gt = pxp_to_gt(pxp);
0060     int ret;
0061 
0062     pxp->arb_is_valid = false;
0063 
0064     if (intel_pxp_session_is_in_play(pxp, ARB_SESSION)) {
0065         drm_err(&gt->i915->drm, "arb session already in play at creation time\n");
0066         return -EEXIST;
0067     }
0068 
0069     ret = intel_pxp_tee_cmd_create_arb_session(pxp, ARB_SESSION);
0070     if (ret) {
0071         drm_err(&gt->i915->drm, "tee cmd for arb session creation failed\n");
0072         return ret;
0073     }
0074 
0075     ret = pxp_wait_for_session_state(pxp, ARB_SESSION, true);
0076     if (ret) {
0077         drm_err(&gt->i915->drm, "arb session failed to go in play\n");
0078         return ret;
0079     }
0080 
0081     if (!++pxp->key_instance)
0082         ++pxp->key_instance;
0083 
0084     pxp->arb_is_valid = true;
0085 
0086     return 0;
0087 }
0088 
0089 static int pxp_terminate_arb_session_and_global(struct intel_pxp *pxp)
0090 {
0091     int ret;
0092     struct intel_gt *gt = pxp_to_gt(pxp);
0093 
0094     /* must mark termination in progress calling this function */
0095     GEM_WARN_ON(pxp->arb_is_valid);
0096 
0097     /* terminate the hw sessions */
0098     ret = intel_pxp_terminate_session(pxp, ARB_SESSION);
0099     if (ret) {
0100         drm_err(&gt->i915->drm, "Failed to submit session termination\n");
0101         return ret;
0102     }
0103 
0104     ret = pxp_wait_for_session_state(pxp, ARB_SESSION, false);
0105     if (ret) {
0106         drm_err(&gt->i915->drm, "Session state did not clear\n");
0107         return ret;
0108     }
0109 
0110     intel_uncore_write(gt->uncore, PXP_GLOBAL_TERMINATE, 1);
0111 
0112     return ret;
0113 }
0114 
0115 static void pxp_terminate(struct intel_pxp *pxp)
0116 {
0117     int ret;
0118 
0119     pxp->hw_state_invalidated = true;
0120 
0121     /*
0122      * if we fail to submit the termination there is no point in waiting for
0123      * it to complete. PXP will be marked as non-active until the next
0124      * termination is issued.
0125      */
0126     ret = pxp_terminate_arb_session_and_global(pxp);
0127     if (ret)
0128         complete_all(&pxp->termination);
0129 }
0130 
0131 static void pxp_terminate_complete(struct intel_pxp *pxp)
0132 {
0133     /* Re-create the arb session after teardown handle complete */
0134     if (fetch_and_zero(&pxp->hw_state_invalidated))
0135         pxp_create_arb_session(pxp);
0136 
0137     complete_all(&pxp->termination);
0138 }
0139 
0140 void intel_pxp_session_work(struct work_struct *work)
0141 {
0142     struct intel_pxp *pxp = container_of(work, typeof(*pxp), session_work);
0143     struct intel_gt *gt = pxp_to_gt(pxp);
0144     intel_wakeref_t wakeref;
0145     u32 events = 0;
0146 
0147     spin_lock_irq(&gt->irq_lock);
0148     events = fetch_and_zero(&pxp->session_events);
0149     spin_unlock_irq(&gt->irq_lock);
0150 
0151     if (!events)
0152         return;
0153 
0154     if (events & PXP_INVAL_REQUIRED)
0155         intel_pxp_invalidate(pxp);
0156 
0157     /*
0158      * If we're processing an event while suspending then don't bother,
0159      * we're going to re-init everything on resume anyway.
0160      */
0161     wakeref = intel_runtime_pm_get_if_in_use(gt->uncore->rpm);
0162     if (!wakeref)
0163         return;
0164 
0165     if (events & PXP_TERMINATION_REQUEST) {
0166         events &= ~PXP_TERMINATION_COMPLETE;
0167         pxp_terminate(pxp);
0168     }
0169 
0170     if (events & PXP_TERMINATION_COMPLETE)
0171         pxp_terminate_complete(pxp);
0172 
0173     intel_runtime_pm_put(gt->uncore->rpm, wakeref);
0174 }