]> git.baikalelectronics.ru Git - kernel.git/commitdiff
drm/i915/pxp: Create the arbitrary session after boot
authorHuang, Sean Z <sean.z.huang@intel.com>
Fri, 24 Sep 2021 19:14:42 +0000 (12:14 -0700)
committerRodrigo Vivi <rodrigo.vivi@intel.com>
Mon, 4 Oct 2021 17:10:44 +0000 (13:10 -0400)
Create the arbitrary session, with the fixed session id 0xf, after
system boot, for the case that application allocates the protected
buffer without establishing any protection session. Because the
hardware requires at least one alive session for protected buffer
creation. This arbitrary session will need to be re-created after
teardown or power event because hardware encryption key won't be
valid after such cases.

The session ID is exposed as part of the uapi so it can be used as part
of userspace commands.

v2: use gt->uncore->rpm (Chris)
v3: s/arb_is_in_play/arb_is_valid (Chris), move set-up to the new
    init_hw function
v4: move interface defs to separate header, set arb_is valid to false
    on fini (Rodrigo)
v5: handle async component binding

Signed-off-by: Huang, Sean Z <sean.z.huang@intel.com>
Signed-off-by: Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20210924191452.1539378-8-alan.previn.teres.alexis@intel.com
drivers/gpu/drm/i915/Makefile
drivers/gpu/drm/i915/pxp/intel_pxp.c
drivers/gpu/drm/i915/pxp/intel_pxp.h
drivers/gpu/drm/i915/pxp/intel_pxp_session.c [new file with mode: 0644]
drivers/gpu/drm/i915/pxp/intel_pxp_session.h [new file with mode: 0644]
drivers/gpu/drm/i915/pxp/intel_pxp_tee.c
drivers/gpu/drm/i915/pxp/intel_pxp_tee.h
drivers/gpu/drm/i915/pxp/intel_pxp_tee_interface.h [new file with mode: 0644]
drivers/gpu/drm/i915/pxp/intel_pxp_types.h
include/uapi/drm/i915_drm.h

index d790b603fbdd3aab50c315787297f3649d129dde..1152348c04a91e2a2bcee55ad03bbeb41e09e848 100644 (file)
@@ -281,6 +281,7 @@ i915-y += i915_perf.o
 # Protected execution platform (PXP) support
 i915-$(CONFIG_DRM_I915_PXP) += \
        pxp/intel_pxp.o \
+       pxp/intel_pxp_session.o \
        pxp/intel_pxp_tee.o
 
 # Post-mortem debug and GPU hang state capture
index f7166dd71d8f0f8f1f020a2f6d234417ae72b0df..65598487a7b26f27c022ab04916114d497de0b37 100644 (file)
@@ -3,6 +3,7 @@
  * Copyright(c) 2020 Intel Corporation.
  */
 #include "intel_pxp.h"
+#include "intel_pxp_session.h"
 #include "intel_pxp_tee.h"
 #include "gt/intel_context.h"
 #include "i915_drv.h"
@@ -12,6 +13,11 @@ struct intel_gt *pxp_to_gt(const struct intel_pxp *pxp)
        return container_of(pxp, struct intel_gt, pxp);
 }
 
+bool intel_pxp_is_active(const struct intel_pxp *pxp)
+{
+       return pxp->arb_is_valid;
+}
+
 /* KCR register definitions */
 #define KCR_INIT _MMIO(0x320f0)
 /* Setting KCR Init bit is required after system boot */
@@ -72,6 +78,8 @@ void intel_pxp_init(struct intel_pxp *pxp)
        if (!HAS_PXP(gt->i915))
                return;
 
+       mutex_init(&pxp->tee_mutex);
+
        ret = create_vcs_context(pxp);
        if (ret)
                return;
@@ -93,6 +101,8 @@ void intel_pxp_fini(struct intel_pxp *pxp)
        if (!intel_pxp_is_enabled(pxp))
                return;
 
+       pxp->arb_is_valid = false;
+
        intel_pxp_tee_component_fini(pxp);
 
        destroy_vcs_context(pxp);
@@ -101,6 +111,8 @@ void intel_pxp_fini(struct intel_pxp *pxp)
 void intel_pxp_init_hw(struct intel_pxp *pxp)
 {
        kcr_pxp_enable(pxp_to_gt(pxp));
+
+       intel_pxp_create_arb_session(pxp);
 }
 
 void intel_pxp_fini_hw(struct intel_pxp *pxp)
index cd6560b2605ca19b8df4ed8fdf3e4aa8b0e84a39..2960e8338c829dc85c1702de00dae5d967c1b2fc 100644 (file)
@@ -15,6 +15,8 @@ static inline bool intel_pxp_is_enabled(const struct intel_pxp *pxp)
 
 #ifdef CONFIG_DRM_I915_PXP
 struct intel_gt *pxp_to_gt(const struct intel_pxp *pxp);
+bool intel_pxp_is_active(const struct intel_pxp *pxp);
+
 void intel_pxp_init(struct intel_pxp *pxp);
 void intel_pxp_fini(struct intel_pxp *pxp);
 
diff --git a/drivers/gpu/drm/i915/pxp/intel_pxp_session.c b/drivers/gpu/drm/i915/pxp/intel_pxp_session.c
new file mode 100644 (file)
index 0000000..3331868
--- /dev/null
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright(c) 2020, Intel Corporation. All rights reserved.
+ */
+
+#include "drm/i915_drm.h"
+#include "i915_drv.h"
+
+#include "intel_pxp.h"
+#include "intel_pxp_session.h"
+#include "intel_pxp_tee.h"
+#include "intel_pxp_types.h"
+
+#define ARB_SESSION I915_PROTECTED_CONTENT_DEFAULT_SESSION /* shorter define */
+
+#define GEN12_KCR_SIP _MMIO(0x32260) /* KCR hwdrm session in play 0-31 */
+
+static bool intel_pxp_session_is_in_play(struct intel_pxp *pxp, u32 id)
+{
+       struct intel_gt *gt = pxp_to_gt(pxp);
+       intel_wakeref_t wakeref;
+       u32 sip = 0;
+
+       with_intel_runtime_pm(gt->uncore->rpm, wakeref)
+               sip = intel_uncore_read(gt->uncore, GEN12_KCR_SIP);
+
+       return sip & BIT(id);
+}
+
+static int pxp_wait_for_session_state(struct intel_pxp *pxp, u32 id, bool in_play)
+{
+       struct intel_gt *gt = pxp_to_gt(pxp);
+       intel_wakeref_t wakeref;
+       u32 mask = BIT(id);
+       int ret;
+
+       with_intel_runtime_pm(gt->uncore->rpm, wakeref)
+               ret = intel_wait_for_register(gt->uncore,
+                                             GEN12_KCR_SIP,
+                                             mask,
+                                             in_play ? mask : 0,
+                                             100);
+
+       return ret;
+}
+
+int intel_pxp_create_arb_session(struct intel_pxp *pxp)
+{
+       struct intel_gt *gt = pxp_to_gt(pxp);
+       int ret;
+
+       pxp->arb_is_valid = false;
+
+       if (intel_pxp_session_is_in_play(pxp, ARB_SESSION)) {
+               drm_err(&gt->i915->drm, "arb session already in play at creation time\n");
+               return -EEXIST;
+       }
+
+       ret = intel_pxp_tee_cmd_create_arb_session(pxp, ARB_SESSION);
+       if (ret) {
+               drm_err(&gt->i915->drm, "tee cmd for arb session creation failed\n");
+               return ret;
+       }
+
+       ret = pxp_wait_for_session_state(pxp, ARB_SESSION, true);
+       if (ret) {
+               drm_err(&gt->i915->drm, "arb session failed to go in play\n");
+               return ret;
+       }
+
+       pxp->arb_is_valid = true;
+
+       return 0;
+}
diff --git a/drivers/gpu/drm/i915/pxp/intel_pxp_session.h b/drivers/gpu/drm/i915/pxp/intel_pxp_session.h
new file mode 100644 (file)
index 0000000..316c3be
--- /dev/null
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright(c) 2020, Intel Corporation. All rights reserved.
+ */
+
+#ifndef __INTEL_PXP_SESSION_H__
+#define __INTEL_PXP_SESSION_H__
+
+#include <linux/types.h>
+
+struct intel_pxp;
+
+int intel_pxp_create_arb_session(struct intel_pxp *pxp);
+
+#endif /* __INTEL_PXP_SESSION_H__ */
index 0c0c7946e6a0da0d1f4558d1f319e28d849e0f65..accbbf9aa6d1111cf91a137d6c4464f47cc3b80a 100644 (file)
@@ -8,13 +8,63 @@
 #include "drm/i915_component.h"
 #include "i915_drv.h"
 #include "intel_pxp.h"
+#include "intel_pxp_session.h"
 #include "intel_pxp_tee.h"
+#include "intel_pxp_tee_interface.h"
 
 static inline struct intel_pxp *i915_dev_to_pxp(struct device *i915_kdev)
 {
        return &kdev_to_i915(i915_kdev)->gt.pxp;
 }
 
+static int intel_pxp_tee_io_message(struct intel_pxp *pxp,
+                                   void *msg_in, u32 msg_in_size,
+                                   void *msg_out, u32 msg_out_max_size,
+                                   u32 *msg_out_rcv_size)
+{
+       struct drm_i915_private *i915 = pxp_to_gt(pxp)->i915;
+       struct i915_pxp_component *pxp_component = pxp->pxp_component;
+       int ret = 0;
+
+       mutex_lock(&pxp->tee_mutex);
+
+       /*
+        * The binding of the component is asynchronous from i915 probe, so we
+        * can't be sure it has happened.
+        */
+       if (!pxp_component) {
+               ret = -ENODEV;
+               goto unlock;
+       }
+
+       ret = pxp_component->ops->send(pxp_component->tee_dev, msg_in, msg_in_size);
+       if (ret) {
+               drm_err(&i915->drm, "Failed to send PXP TEE message\n");
+               goto unlock;
+       }
+
+       ret = pxp_component->ops->recv(pxp_component->tee_dev, msg_out, msg_out_max_size);
+       if (ret < 0) {
+               drm_err(&i915->drm, "Failed to receive PXP TEE message\n");
+               goto unlock;
+       }
+
+       if (ret > msg_out_max_size) {
+               drm_err(&i915->drm,
+                       "Failed to receive PXP TEE message due to unexpected output size\n");
+               ret = -ENOSPC;
+               goto unlock;
+       }
+
+       if (msg_out_rcv_size)
+               *msg_out_rcv_size = ret;
+
+       ret = 0;
+unlock:
+       mutex_unlock(&pxp->tee_mutex);
+       return ret;
+}
+
 /**
  * i915_pxp_tee_component_bind - bind function to pass the function pointers to pxp_tee
  * @i915_kdev: pointer to i915 kernel device
@@ -28,14 +78,24 @@ static inline struct intel_pxp *i915_dev_to_pxp(struct device *i915_kdev)
 static int i915_pxp_tee_component_bind(struct device *i915_kdev,
                                       struct device *tee_kdev, void *data)
 {
+       struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
        struct intel_pxp *pxp = i915_dev_to_pxp(i915_kdev);
 
+       mutex_lock(&pxp->tee_mutex);
        pxp->pxp_component = data;
        pxp->pxp_component->tee_dev = tee_kdev;
+       mutex_unlock(&pxp->tee_mutex);
 
        /* the component is required to fully start the PXP HW */
        intel_pxp_init_hw(pxp);
 
+       if (!pxp->arb_is_valid) {
+               drm_err(&i915->drm, "Failed to create arb session during bind\n");
+               intel_pxp_fini_hw(pxp);
+               pxp->pxp_component = NULL;
+               return -EIO;
+       }
+
        return 0;
 }
 
@@ -46,7 +106,9 @@ static void i915_pxp_tee_component_unbind(struct device *i915_kdev,
 
        intel_pxp_fini_hw(pxp);
 
+       mutex_lock(&pxp->tee_mutex);
        pxp->pxp_component = NULL;
+       mutex_unlock(&pxp->tee_mutex);
 }
 
 static const struct component_ops i915_pxp_tee_component_ops = {
@@ -82,3 +144,28 @@ void intel_pxp_tee_component_fini(struct intel_pxp *pxp)
        component_del(i915->drm.dev, &i915_pxp_tee_component_ops);
        pxp->pxp_component_added = false;
 }
+
+int intel_pxp_tee_cmd_create_arb_session(struct intel_pxp *pxp,
+                                        int arb_session_id)
+{
+       struct drm_i915_private *i915 = pxp_to_gt(pxp)->i915;
+       struct pxp_tee_create_arb_in msg_in = {0};
+       struct pxp_tee_create_arb_out msg_out = {0};
+       int ret;
+
+       msg_in.header.api_version = PXP_TEE_APIVER;
+       msg_in.header.command_id = PXP_TEE_ARB_CMDID;
+       msg_in.header.buffer_len = sizeof(msg_in) - sizeof(msg_in.header);
+       msg_in.protection_mode = PXP_TEE_ARB_PROTECTION_MODE;
+       msg_in.session_id = arb_session_id;
+
+       ret = intel_pxp_tee_io_message(pxp,
+                                      &msg_in, sizeof(msg_in),
+                                      &msg_out, sizeof(msg_out),
+                                      NULL);
+
+       if (ret)
+               drm_err(&i915->drm, "Failed to send tee msg ret=[%d]\n", ret);
+
+       return ret;
+}
index 23d050a5d3e7b44e5ac815828d71b2ad519ac90d..c136053ce3406535761e4e9448dc6eef292e1fba 100644 (file)
@@ -11,4 +11,7 @@
 int intel_pxp_tee_component_init(struct intel_pxp *pxp);
 void intel_pxp_tee_component_fini(struct intel_pxp *pxp);
 
+int intel_pxp_tee_cmd_create_arb_session(struct intel_pxp *pxp,
+                                        int arb_session_id);
+
 #endif /* __INTEL_PXP_TEE_H__ */
diff --git a/drivers/gpu/drm/i915/pxp/intel_pxp_tee_interface.h b/drivers/gpu/drm/i915/pxp/intel_pxp_tee_interface.h
new file mode 100644 (file)
index 0000000..36e9b08
--- /dev/null
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright(c) 2020, Intel Corporation. All rights reserved.
+ */
+
+#ifndef __INTEL_PXP_TEE_INTERFACE_H__
+#define __INTEL_PXP_TEE_INTERFACE_H__
+
+#include <linux/types.h>
+
+#define PXP_TEE_APIVER 0x40002
+#define PXP_TEE_ARB_CMDID 0x1e
+#define PXP_TEE_ARB_PROTECTION_MODE 0x2
+
+/* PXP TEE message header */
+struct pxp_tee_cmd_header {
+       u32 api_version;
+       u32 command_id;
+       u32 status;
+       /* Length of the message (excluding the header) */
+       u32 buffer_len;
+} __packed;
+
+/* PXP TEE message input to create a arbitrary session */
+struct pxp_tee_create_arb_in {
+       struct pxp_tee_cmd_header header;
+       u32 protection_mode;
+       u32 session_id;
+} __packed;
+
+/* PXP TEE message output to create a arbitrary session */
+struct pxp_tee_create_arb_out {
+       struct pxp_tee_cmd_header header;
+} __packed;
+
+#endif /* __INTEL_PXP_TEE_INTERFACE_H__ */
index 3a8e17e591bd0bfbd21ef7f16ad99408f1ba9eb7..d393e46a7d98a261794a6ab9d7bd06dcd6970188 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef __INTEL_PXP_TYPES_H__
 #define __INTEL_PXP_TYPES_H__
 
+#include <linux/mutex.h>
 #include <linux/types.h>
 
 struct intel_context;
@@ -16,6 +17,15 @@ struct intel_pxp {
        bool pxp_component_added;
 
        struct intel_context *ce;
+
+       /*
+        * After a teardown, the arb session can still be in play on the HW
+        * even if the keys are gone, so we can't rely on the HW state of the
+        * session to know if it's valid and need to track the status in SW.
+        */
+       bool arb_is_valid;
+
+       struct mutex tee_mutex; /* protects the tee channel binding */
 };
 
 #endif /* __INTEL_PXP_TYPES_H__ */
index bde5860b3686264b17d120e01f93272495bf4068..920e9e852e5a57d545f0bb64fd7a09129ea503cf 100644 (file)
@@ -3038,6 +3038,9 @@ struct drm_i915_gem_create_ext_memory_regions {
        __u64 regions;
 };
 
+/* ID of the protected content session managed by i915 when PXP is active */
+#define I915_PROTECTED_CONTENT_DEFAULT_SESSION 0xf
+
 #if defined(__cplusplus)
 }
 #endif