All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Huang, Sean Z" <sean.z.huang@intel.com>
To: Intel-gfx@lists.freedesktop.org
Subject: [Intel-gfx] [PATCH 08/27] drm/i915/pxp: Read register to check hardware session state
Date: Sun, 15 Nov 2020 13:07:56 -0800	[thread overview]
Message-ID: <20201115210815.5272-8-sean.z.huang@intel.com> (raw)
In-Reply-To: <20201115210815.5272-1-sean.z.huang@intel.com>

Implement the functions to check the hardware protected session
state via reading the hardware register session in play.

Signed-off-by: Huang, Sean Z <sean.z.huang@intel.com>
---
 drivers/gpu/drm/i915/pxp/intel_pxp.h    |   3 +
 drivers/gpu/drm/i915/pxp/intel_pxp_sm.c | 176 ++++++++++++++++++++++++
 drivers/gpu/drm/i915/pxp/intel_pxp_sm.h |  51 +++++++
 3 files changed, 230 insertions(+)

diff --git a/drivers/gpu/drm/i915/pxp/intel_pxp.h b/drivers/gpu/drm/i915/pxp/intel_pxp.h
index 21a6964fc64e..95d3deba7ade 100644
--- a/drivers/gpu/drm/i915/pxp/intel_pxp.h
+++ b/drivers/gpu/drm/i915/pxp/intel_pxp.h
@@ -12,6 +12,9 @@
 #define PXP_IRQ_VECTOR_DISPLAY_APP_TERM_PER_FW_REQ BIT(2)
 #define PXP_IRQ_VECTOR_PXP_DISP_STATE_RESET_COMPLETE BIT(3)
 
+#define pxp_session_list(i915, session_type) (((session_type) == SESSION_TYPE_TYPE0) ? \
+	&(i915)->pxp.r0ctx->active_pxp_type0_sessions : &(i915)->pxp.r0ctx->active_pxp_type1_sessions)
+
 #define MAX_TYPE0_SESSIONS 16
 #define MAX_TYPE1_SESSIONS 6
 
diff --git a/drivers/gpu/drm/i915/pxp/intel_pxp_sm.c b/drivers/gpu/drm/i915/pxp/intel_pxp_sm.c
index 763d194c5f4c..68d421976e33 100644
--- a/drivers/gpu/drm/i915/pxp/intel_pxp_sm.c
+++ b/drivers/gpu/drm/i915/pxp/intel_pxp_sm.c
@@ -10,6 +10,21 @@
 #include "intel_pxp_sm.h"
 #include "intel_pxp_context.h"
 
+static int pxp_sm_reg_read(struct drm_i915_private *i915, u32 offset, u32 *regval)
+{
+	intel_wakeref_t wakeref;
+
+	if (!i915 || !regval)
+		return -EINVAL;
+
+	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
+		i915_reg_t reg_offset = {offset};
+		*regval = intel_uncore_read(&i915->uncore, reg_offset);
+	}
+
+	return 0;
+}
+
 static int pxp_reg_write(struct drm_i915_private *i915, u32 offset, u32 regval)
 {
 	intel_wakeref_t wakeref;
@@ -26,6 +41,167 @@ static int pxp_reg_write(struct drm_i915_private *i915, u32 offset, u32 regval)
 	return 0;
 }
 
+/**
+ * is_sw_session_active - Check if the given sw session id is active.
+ * @i915: i915 device handle.
+ * @session_type: Specified session type
+ * @session_index: Numeric session identifier.
+ * @is_in_play: Set false to return true if the specified session is active.
+ *              Set true to also check if the session is active and in_play.
+ * @protection_mode: get the protection mode of specified session.
+ *
+ * The caller needs to use ctx_mutex lock to protect the session_list
+ * inside this function.
+ *
+ * Return : true if session with the same identifier is active (and in_play).
+ */
+static bool is_sw_session_active(struct drm_i915_private *i915, int session_type,
+				 int session_index, bool is_in_play, int *protection_mode)
+{
+	struct pxp_protected_session *current_session;
+
+	lockdep_assert_held(&i915->pxp.r0ctx->ctx_mutex);
+
+	list_for_each_entry(current_session, pxp_session_list(i915, session_type), session_list) {
+		if (current_session->session_index == session_index) {
+			if (protection_mode)
+				*protection_mode = current_session->protection_mode;
+
+			if (is_in_play && !current_session->session_is_in_play)
+				return false;
+
+			return true;
+		}
+	}
+
+	/* session id not found. return false */
+	return false;
+}
+
+static bool is_hw_type0_session_in_play(struct drm_i915_private *i915, int session_index)
+{
+	u32 regval_sip = 0;
+	u32 reg_session_id_mask;
+	bool hw_session_is_in_play = false;
+	int ret = 0;
+
+	if (!i915 || session_index < 0 || session_index >= MAX_TYPE0_SESSIONS)
+		goto end;
+
+	ret = pxp_sm_reg_read(i915, GEN12_KCR_SIP.reg, &regval_sip);
+	if (ret) {
+		drm_dbg(&i915->drm, "Failed to read()\n");
+		goto end;
+	}
+
+	reg_session_id_mask = (1 << session_index);
+	hw_session_is_in_play = (bool)(regval_sip & reg_session_id_mask);
+end:
+	return hw_session_is_in_play;
+}
+
+static bool is_hw_type1_session_in_play(struct drm_i915_private *i915, int session_index)
+{
+	int ret = 0;
+	u32 regval_tsip_low = 0;
+	u32 regval_tsip_high = 0;
+	u64 reg_session_id_mask;
+	u64 regval_tsip;
+	bool hw_session_is_in_play = false;
+
+	if (!i915 || session_index < 0 || session_index >= MAX_TYPE1_SESSIONS)
+		goto end;
+
+	ret = pxp_sm_reg_read(i915, GEN12_KCR_TSIP_LOW.reg, &regval_tsip_low);
+	if (ret) {
+		drm_dbg(&i915->drm, "Failed to pxp_sm_reg_read()\n");
+		goto end;
+	}
+
+	ret = pxp_sm_reg_read(i915, GEN12_KCR_TSIP_HIGH.reg, &regval_tsip_high);
+	if (ret) {
+		drm_dbg(&i915->drm, "Failed to pxp_sm_reg_read()\n");
+		goto end;
+	}
+
+	reg_session_id_mask = (1 << session_index);
+	regval_tsip = ((u64)regval_tsip_high << 32) | regval_tsip_low;
+	hw_session_is_in_play = (bool)(regval_tsip & reg_session_id_mask);
+end:
+	return hw_session_is_in_play;
+}
+
+static bool is_hw_session_in_play(struct drm_i915_private *i915,
+				  int session_type, int session_index)
+{
+	bool is_in_play = false;
+
+	if (session_type == SESSION_TYPE_TYPE0)
+		is_in_play = is_hw_type0_session_in_play(i915, session_index);
+	else if (session_type == SESSION_TYPE_TYPE1)
+		is_in_play = is_hw_type1_session_in_play(i915, session_index);
+	else
+		drm_dbg(&i915->drm, "Failed to %s invalid session_type=[%d]\n", __func__, session_type);
+
+	return is_in_play;
+}
+
+/* check hw session in play reg if match the current sw state */
+static int sync_hw_sw_state(struct drm_i915_private *i915, int session_index, int session_type)
+{
+	const int max_retry = 10;
+	const int ms_delay = 10;
+	int retry = 0;
+	int ret;
+
+	if (!i915 || session_type >= SESSION_TYPE_MAX)
+		return -EINVAL;
+
+	ret = -EINVAL;
+	for (retry = 0; retry < max_retry; retry++) {
+		if (is_hw_session_in_play(i915, session_type, session_index) ==
+		    is_sw_session_active(i915, session_type, session_index, true, NULL)) {
+			ret = 0;
+			break;
+		}
+
+		msleep(ms_delay);
+	}
+
+	return ret;
+}
+
+/**
+ * check_if_protected_type0_sessions_are_attacked - To check if type0 active sessions are attacked.
+ * @i915: i915 device handle.
+ *
+ * Return: true if HW shows protected sessions are attacked, false otherwise.
+ */
+static bool check_if_protected_type0_sessions_are_attacked(struct drm_i915_private *i915)
+{
+	i915_reg_t kcr_status_reg = KCR_STATUS_1;
+	u32 reg_value = 0;
+	u32 mask = 0x80000000;
+	int ret;
+
+	if (!i915)
+		return false;
+
+	if (i915->pxp.r0ctx->global_state_attacked)
+		return true;
+
+	ret = pxp_sm_reg_read(i915, kcr_status_reg.reg, &reg_value);
+	if (ret) {
+		drm_dbg(&i915->drm, "Failed to pxp_sm_reg_read\n");
+		goto end;
+	}
+
+	if (reg_value & mask)
+		return true;
+end:
+	return false;
+}
+
 int pxp_sm_set_kcr_init_reg(struct drm_i915_private *i915)
 {
 	int ret;
diff --git a/drivers/gpu/drm/i915/pxp/intel_pxp_sm.h b/drivers/gpu/drm/i915/pxp/intel_pxp_sm.h
index d061f395aa16..222a879be96d 100644
--- a/drivers/gpu/drm/i915/pxp/intel_pxp_sm.h
+++ b/drivers/gpu/drm/i915/pxp/intel_pxp_sm.h
@@ -15,6 +15,57 @@
 /* Setting KCR Init bit is required after system boot */
 #define KCR_INIT_ALLOW_DISPLAY_ME_WRITES (BIT(14) | (BIT(14) << KCR_INIT_MASK_SHIFT))
 
+#define KCR_STATUS_1        _MMIO(0x320f4)
+#define GEN12_KCR_SIP       _MMIO(0x32260)   /* KCR type0 session in play 0-31 */
+#define GEN12_KCR_TSIP_LOW  _MMIO(0x32264)   /* KCR type1 session in play 0-31 */
+#define GEN12_KCR_TSIP_HIGH _MMIO(0x32268)   /* KCR type1 session in play 32-63 */
+
+enum pxp_session_types {
+	SESSION_TYPE_TYPE0 = 0,
+	SESSION_TYPE_TYPE1 = 1,
+
+	SESSION_TYPE_MAX
+};
+
+enum pxp_protection_modes {
+	PROTECTION_MODE_NONE = 0,
+	PROTECTION_MODE_LM   = 2,
+	PROTECTION_MODE_HM   = 3,
+	PROTECTION_MODE_SM   = 6,
+
+	PROTECTION_MODE_ALL
+};
+
+/**
+ * struct pxp_protected_session - linked list to track all active sessions.
+ */
+struct pxp_protected_session {
+	/** @session_list: linked list infrastructure, do not change its order. */
+	struct list_head session_list;
+
+	/** @session_index: Numeric identifier for this protected session */
+	int session_index;
+	/** @session_type: Type of session */
+	int session_type;
+	/** @protection_mode: mode of protection requested */
+	int protection_mode;
+	/** @context_id: context identifier of the protected session requestor */
+	int context_id;
+	/** @pid: pid of this session's creator */
+	int pid;
+	/** @drmfile: pointer to drm_file, which is allocated on device file open() call */
+	struct drm_file *drmfile;
+
+	/**
+	 * @session_is_in_play: indicates whether the session has been established
+	 *                      in the HW root of trust if this flag is false, it
+	 *                      indicates an application has reserved this session,
+	 *                      but has not * established the session in the
+	 *                      hardware yet.
+	 */
+	bool session_is_in_play;
+};
+
 int pxp_sm_set_kcr_init_reg(struct drm_i915_private *i915);
 
 #endif /* __INTEL_PXP_SM_H__ */
-- 
2.17.1

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

  parent reply	other threads:[~2020-11-15 21:08 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-15 21:07 [Intel-gfx] [PATCH 01/27] drm/i915/pxp: Introduce Intel PXP component Huang, Sean Z
2020-11-15 21:07 ` [Intel-gfx] [PATCH 02/27] drm/i915/pxp: Enable PXP irq worker and callback stub Huang, Sean Z
2020-11-15 21:07 ` [Intel-gfx] [PATCH 03/27] drm/i915/pxp: Add PXP context for logical hardware states Huang, Sean Z
2020-11-15 21:07 ` [Intel-gfx] [PATCH 04/27] drm/i915/pxp: set KCR reg init during the boot time Huang, Sean Z
2020-11-15 21:07 ` [Intel-gfx] [PATCH 05/27] drm/i915/pxp: Enable ioctl action to set the ring3 context Huang, Sean Z
2020-11-16 10:22   ` Joonas Lahtinen
2020-11-16 10:53     ` Chris Wilson
2020-11-15 21:07 ` [Intel-gfx] [PATCH 06/27] drm/i915: Rename the whitelist to allowlist Huang, Sean Z
2020-11-16 10:26   ` Joonas Lahtinen
2020-11-16 11:33     ` Chris Wilson
2020-11-15 21:07 ` [Intel-gfx] [PATCH 07/27] drm/i915/pxp: Add PXP-related registers into allowlist Huang, Sean Z
2020-11-16 10:33   ` Joonas Lahtinen
2020-11-16 11:10     ` Chris Wilson
2020-11-15 21:07 ` Huang, Sean Z [this message]
2020-11-15 21:07 ` [Intel-gfx] [PATCH 09/27] drm/i915/pxp: Implement funcs to get/set PXP tag Huang, Sean Z
2020-11-15 21:07 ` [Intel-gfx] [PATCH 10/27] drm/i915/pxp: Enable ioctl action to reserve session slot Huang, Sean Z
2020-11-16 11:01   ` Joonas Lahtinen
2020-11-15 21:07 ` [Intel-gfx] [PATCH 11/27] drm/i915/pxp: Enable ioctl action to set session in play Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 12/27] drm/i915/pxp: Func to send hardware session termination Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 13/27] drm/i915/pxp: Enable ioctl action to terminate the session Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 14/27] drm/i915/pxp: Enable ioctl action to query PXP tag Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 15/27] drm/i915/pxp: Destroy all type0 sessions upon teardown Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 16/27] drm/i915/pxp: Termiante the session upon app crash Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 17/27] drm/i915/pxp: Enable PXP power management Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 18/27] drm/i915/pxp: Implement funcs to create the TEE channel Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 19/27] drm/i915/pxp: Enable ioctl action to send TEE commands Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 20/27] drm/i915/pxp: Create the arbitrary session after boot Huang, Sean Z
2020-11-16 10:54   ` Joonas Lahtinen
2020-11-15 21:08 ` [Intel-gfx] [PATCH 21/27] drm/i915/pxp: Add i915 trace logs for PXP operations Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 22/27] drm/i915/pxp: Expose session state for display protection flip Huang, Sean Z
2020-11-16 10:49   ` Joonas Lahtinen
2020-11-16 23:37     ` Huang, Sean Z
2020-11-17  8:39   ` Anshuman Gupta
2020-11-17 19:09     ` Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 23/27] mei: bus: enable pavp device Huang, Sean Z
2020-11-16 10:46   ` Joonas Lahtinen
2020-11-16 10:49     ` Winkler, Tomas
2020-11-16 11:08       ` Joonas Lahtinen
2020-11-15 21:08 ` [Intel-gfx] [PATCH 24/27] mei: pxp: export pavp client to me client bus Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 25/27] drm/i915/uapi: introduce drm_i915_gem_create_ext for TGL Huang, Sean Z
2020-11-16 10:38   ` Joonas Lahtinen
2020-11-16 10:39     ` Joonas Lahtinen
2020-11-17  8:42   ` Lionel Landwerlin
2020-11-15 21:08 ` [Intel-gfx] [PATCH 26/27] drm/i915/pavp: User interface for Protected buffer Huang, Sean Z
2020-11-15 21:08 ` [Intel-gfx] [PATCH 27/27] drm/i915/pxp: Add plane decryption support Huang, Sean Z
2020-11-15 21:30 ` [Intel-gfx] ✗ Fi.CI.CHECKPATCH: warning for series starting with [01/27] drm/i915/pxp: Introduce Intel PXP component Patchwork
2020-11-15 22:00 ` [Intel-gfx] ✗ Fi.CI.BAT: failure " Patchwork
2020-11-16 10:09 ` [Intel-gfx] [PATCH 01/27] " Joonas Lahtinen
  -- strict thread matches above, loose matches on Subject: below --
2020-11-15 20:23 Huang, Sean Z
2020-11-15 20:23 ` [Intel-gfx] [PATCH 08/27] drm/i915/pxp: Read register to check hardware session state Huang, Sean Z
2020-11-14 17:12 kernel test robot
2020-11-14  1:45 [Intel-gfx] [PATCH 01/27] drm/i915/pxp: Introduce Intel PXP component Sean Z Huang
2020-11-14  1:45 ` [Intel-gfx] [PATCH 08/27] drm/i915/pxp: Read register to check hardware session state Sean Z Huang
2020-11-14  5:04   ` kernel test robot
2020-11-14  5:04     ` kernel test robot
2020-11-14  5:14   ` kernel test robot
2020-11-14  5:14     ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201115210815.5272-8-sean.z.huang@intel.com \
    --to=sean.z.huang@intel.com \
    --cc=Intel-gfx@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.