Skip to content

Commit

Permalink
drm/i915/pxp: Implement PXP irq handler
Browse files Browse the repository at this point in the history
The HW will generate a teardown interrupt when session termination is
required, which requires i915 to submit a terminating batch. Once the HW
is done with the termination it will generate another interrupt, at
which point it is safe to re-create the session.

Since the termination and re-creation flow is something we want to
trigger from the driver as well, use a common work function that can be
called both from the irq handler and from the driver set-up flows, which
has the addded benefit of allowing us to skip any extra locks because
the work itself serializes the operations.

v2: use struct completion instead of bool (Chris)
v3: drop locks, clean up functions and improve comments (Chris),
    move to common work function.
v4: improve comments, simplify wait logic (Rodrigo)
v5: unconditionally set interrupts, rename state_attacked var (Rodrigo)
v10: remove inclusion of intel_gt_types.h from intel_pxp.h (Jani)

Signed-off-by: Alan Previn <alan.previn.teres.alexis@intel.com>
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-10-alan.previn.teres.alexis@intel.com
  • Loading branch information
Huang, Sean Z authored and Rodrigo Vivi committed Oct 4, 2021
1 parent 95c9e12 commit 2ae0968
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 16 deletions.
1 change: 1 addition & 0 deletions drivers/gpu/drm/i915/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ i915-y += i915_perf.o
i915-$(CONFIG_DRM_I915_PXP) += \
pxp/intel_pxp.o \
pxp/intel_pxp_cmd.o \
pxp/intel_pxp_irq.o \
pxp/intel_pxp_session.o \
pxp/intel_pxp_tee.o

Expand Down
7 changes: 7 additions & 0 deletions drivers/gpu/drm/i915/gt/intel_gt_irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "intel_lrc_reg.h"
#include "intel_uncore.h"
#include "intel_rps.h"
#include "pxp/intel_pxp_irq.h"

static void guc_irq_handler(struct intel_guc *guc, u16 iir)
{
Expand Down Expand Up @@ -64,6 +65,9 @@ gen11_other_irq_handler(struct intel_gt *gt, const u8 instance,
if (instance == OTHER_GTPM_INSTANCE)
return gen11_rps_irq_handler(&gt->rps, iir);

if (instance == OTHER_KCR_INSTANCE)
return intel_pxp_irq_handler(&gt->pxp, iir);

WARN_ONCE(1, "unhandled other interrupt instance=0x%x, iir=0x%x\n",
instance, iir);
}
Expand Down Expand Up @@ -196,6 +200,9 @@ void gen11_gt_irq_reset(struct intel_gt *gt)
intel_uncore_write(uncore, GEN11_GPM_WGBOXPERF_INTR_MASK, ~0);
intel_uncore_write(uncore, GEN11_GUC_SG_INTR_ENABLE, 0);
intel_uncore_write(uncore, GEN11_GUC_SG_INTR_MASK, ~0);

intel_uncore_write(uncore, GEN11_CRYPTO_RSVD_INTR_ENABLE, 0);
intel_uncore_write(uncore, GEN11_CRYPTO_RSVD_INTR_MASK, ~0);
}

void gen11_gt_irq_postinstall(struct intel_gt *gt)
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/i915/i915_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -8124,6 +8124,7 @@ enum {
/* irq instances for OTHER_CLASS */
#define OTHER_GUC_INSTANCE 0
#define OTHER_GTPM_INSTANCE 1
#define OTHER_KCR_INSTANCE 4

#define GEN11_INTR_IDENTITY_REG(x) _MMIO(0x190060 + ((x) * 4))

Expand Down
66 changes: 60 additions & 6 deletions drivers/gpu/drm/i915/pxp/intel_pxp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
/*
* Copyright(c) 2020 Intel Corporation.
*/
#include <linux/workqueue.h>
#include "intel_pxp.h"
#include "intel_pxp_irq.h"
#include "intel_pxp_session.h"
#include "intel_pxp_tee.h"
#include "gt/intel_context.h"
Expand Down Expand Up @@ -80,6 +82,16 @@ void intel_pxp_init(struct intel_pxp *pxp)

mutex_init(&pxp->tee_mutex);

/*
* we'll use the completion to check if there is a termination pending,
* so we start it as completed and we reinit it when a termination
* is triggered.
*/
init_completion(&pxp->termination);
complete_all(&pxp->termination);

INIT_WORK(&pxp->session_work, intel_pxp_session_work);

ret = create_vcs_context(pxp);
if (ret)
return;
Expand Down Expand Up @@ -108,19 +120,61 @@ void intel_pxp_fini(struct intel_pxp *pxp)
destroy_vcs_context(pxp);
}

void intel_pxp_init_hw(struct intel_pxp *pxp)
void intel_pxp_mark_termination_in_progress(struct intel_pxp *pxp)
{
int ret;
pxp->arb_is_valid = false;
reinit_completion(&pxp->termination);
}

static void intel_pxp_queue_termination(struct intel_pxp *pxp)
{
struct intel_gt *gt = pxp_to_gt(pxp);

/*
* We want to get the same effect as if we received a termination
* interrupt, so just pretend that we did.
*/
spin_lock_irq(&gt->irq_lock);
intel_pxp_mark_termination_in_progress(pxp);
pxp->session_events |= PXP_TERMINATION_REQUEST;
queue_work(system_unbound_wq, &pxp->session_work);
spin_unlock_irq(&gt->irq_lock);
}

/*
* the arb session is restarted from the irq work when we receive the
* termination completion interrupt
*/
int intel_pxp_wait_for_arb_start(struct intel_pxp *pxp)
{
if (!intel_pxp_is_enabled(pxp))
return 0;

if (!wait_for_completion_timeout(&pxp->termination,
msecs_to_jiffies(100)))
return -ETIMEDOUT;

if (!pxp->arb_is_valid)
return -EIO;

return 0;
}

void intel_pxp_init_hw(struct intel_pxp *pxp)
{
kcr_pxp_enable(pxp_to_gt(pxp));
intel_pxp_irq_enable(pxp);

/* always emit a full termination to clean the state */
ret = intel_pxp_terminate_arb_session_and_global(pxp);
if (!ret)
intel_pxp_create_arb_session(pxp);
/*
* the session could've been attacked while we weren't loaded, so
* handle it as if it was and re-create it.
*/
intel_pxp_queue_termination(pxp);
}

void intel_pxp_fini_hw(struct intel_pxp *pxp)
{
kcr_pxp_disable(pxp_to_gt(pxp));

intel_pxp_irq_disable(pxp);
}
8 changes: 8 additions & 0 deletions drivers/gpu/drm/i915/pxp/intel_pxp.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ void intel_pxp_fini(struct intel_pxp *pxp);

void intel_pxp_init_hw(struct intel_pxp *pxp);
void intel_pxp_fini_hw(struct intel_pxp *pxp);

void intel_pxp_mark_termination_in_progress(struct intel_pxp *pxp);
int intel_pxp_wait_for_arb_start(struct intel_pxp *pxp);
#else
static inline void intel_pxp_init(struct intel_pxp *pxp)
{
Expand All @@ -30,6 +33,11 @@ static inline void intel_pxp_init(struct intel_pxp *pxp)
static inline void intel_pxp_fini(struct intel_pxp *pxp)
{
}

static inline int intel_pxp_wait_for_arb_start(struct intel_pxp *pxp)
{
return -ENODEV;
}
#endif

#endif /* __INTEL_PXP_H__ */
100 changes: 100 additions & 0 deletions drivers/gpu/drm/i915/pxp/intel_pxp_irq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// SPDX-License-Identifier: MIT
/*
* Copyright(c) 2020 Intel Corporation.
*/
#include <linux/workqueue.h>
#include "intel_pxp.h"
#include "intel_pxp_irq.h"
#include "intel_pxp_session.h"
#include "gt/intel_gt_irq.h"
#include "gt/intel_gt_types.h"
#include "i915_irq.h"
#include "i915_reg.h"

/**
* intel_pxp_irq_handler - Handles PXP interrupts.
* @pxp: pointer to pxp struct
* @iir: interrupt vector
*/
void intel_pxp_irq_handler(struct intel_pxp *pxp, u16 iir)
{
struct intel_gt *gt = pxp_to_gt(pxp);

if (GEM_WARN_ON(!intel_pxp_is_enabled(pxp)))
return;

lockdep_assert_held(&gt->irq_lock);

if (unlikely(!iir))
return;

if (iir & (GEN12_DISPLAY_PXP_STATE_TERMINATED_INTERRUPT |
GEN12_DISPLAY_APP_TERMINATED_PER_FW_REQ_INTERRUPT)) {
/* immediately mark PXP as inactive on termination */
intel_pxp_mark_termination_in_progress(pxp);
pxp->session_events |= PXP_TERMINATION_REQUEST;
}

if (iir & GEN12_DISPLAY_STATE_RESET_COMPLETE_INTERRUPT)
pxp->session_events |= PXP_TERMINATION_COMPLETE;

if (pxp->session_events)
queue_work(system_unbound_wq, &pxp->session_work);
}

static inline void __pxp_set_interrupts(struct intel_gt *gt, u32 interrupts)
{
struct intel_uncore *uncore = gt->uncore;
const u32 mask = interrupts << 16;

intel_uncore_write(uncore, GEN11_CRYPTO_RSVD_INTR_ENABLE, mask);
intel_uncore_write(uncore, GEN11_CRYPTO_RSVD_INTR_MASK, ~mask);
}

static inline void pxp_irq_reset(struct intel_gt *gt)
{
spin_lock_irq(&gt->irq_lock);
gen11_gt_reset_one_iir(gt, 0, GEN11_KCR);
spin_unlock_irq(&gt->irq_lock);
}

void intel_pxp_irq_enable(struct intel_pxp *pxp)
{
struct intel_gt *gt = pxp_to_gt(pxp);

spin_lock_irq(&gt->irq_lock);

if (!pxp->irq_enabled)
WARN_ON_ONCE(gen11_gt_reset_one_iir(gt, 0, GEN11_KCR));

__pxp_set_interrupts(gt, GEN12_PXP_INTERRUPTS);
pxp->irq_enabled = true;

spin_unlock_irq(&gt->irq_lock);
}

void intel_pxp_irq_disable(struct intel_pxp *pxp)
{
struct intel_gt *gt = pxp_to_gt(pxp);

/*
* We always need to submit a global termination when we re-enable the
* interrupts, so there is no need to make sure that the session state
* makes sense at the end of this function. Just make sure this is not
* called in a path were the driver consider the session as valid and
* doesn't call a termination on restart.
*/
GEM_WARN_ON(intel_pxp_is_active(pxp));

spin_lock_irq(&gt->irq_lock);

pxp->irq_enabled = false;
__pxp_set_interrupts(gt, 0);

spin_unlock_irq(&gt->irq_lock);
intel_synchronize_irq(gt->i915);

pxp_irq_reset(gt);

flush_work(&pxp->session_work);
}
32 changes: 32 additions & 0 deletions drivers/gpu/drm/i915/pxp/intel_pxp_irq.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: MIT */
/*
* Copyright(c) 2020, Intel Corporation. All rights reserved.
*/

#ifndef __INTEL_PXP_IRQ_H__
#define __INTEL_PXP_IRQ_H__

#include <linux/types.h>

struct intel_pxp;

#define GEN12_DISPLAY_PXP_STATE_TERMINATED_INTERRUPT BIT(1)
#define GEN12_DISPLAY_APP_TERMINATED_PER_FW_REQ_INTERRUPT BIT(2)
#define GEN12_DISPLAY_STATE_RESET_COMPLETE_INTERRUPT BIT(3)

#define GEN12_PXP_INTERRUPTS \
(GEN12_DISPLAY_PXP_STATE_TERMINATED_INTERRUPT | \
GEN12_DISPLAY_APP_TERMINATED_PER_FW_REQ_INTERRUPT | \
GEN12_DISPLAY_STATE_RESET_COMPLETE_INTERRUPT)

#ifdef CONFIG_DRM_I915_PXP
void intel_pxp_irq_enable(struct intel_pxp *pxp);
void intel_pxp_irq_disable(struct intel_pxp *pxp);
void intel_pxp_irq_handler(struct intel_pxp *pxp, u16 iir);
#else
static inline void intel_pxp_irq_handler(struct intel_pxp *pxp, u16 iir)
{
}
#endif

#endif /* __INTEL_PXP_IRQ_H__ */
54 changes: 51 additions & 3 deletions drivers/gpu/drm/i915/pxp/intel_pxp_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static int pxp_wait_for_session_state(struct intel_pxp *pxp, u32 id, bool in_pla
return ret;
}

int intel_pxp_create_arb_session(struct intel_pxp *pxp)
static int pxp_create_arb_session(struct intel_pxp *pxp)
{
struct intel_gt *gt = pxp_to_gt(pxp);
int ret;
Expand Down Expand Up @@ -77,12 +77,13 @@ int intel_pxp_create_arb_session(struct intel_pxp *pxp)
return 0;
}

int intel_pxp_terminate_arb_session_and_global(struct intel_pxp *pxp)
static int pxp_terminate_arb_session_and_global(struct intel_pxp *pxp)
{
int ret;
struct intel_gt *gt = pxp_to_gt(pxp);

pxp->arb_is_valid = false;
/* must mark termination in progress calling this function */
GEM_WARN_ON(pxp->arb_is_valid);

/* terminate the hw sessions */
ret = intel_pxp_terminate_session(pxp, ARB_SESSION);
Expand All @@ -101,3 +102,50 @@ int intel_pxp_terminate_arb_session_and_global(struct intel_pxp *pxp)

return ret;
}

static void pxp_terminate(struct intel_pxp *pxp)
{
int ret;

pxp->hw_state_invalidated = true;

/*
* if we fail to submit the termination there is no point in waiting for
* it to complete. PXP will be marked as non-active until the next
* termination is issued.
*/
ret = pxp_terminate_arb_session_and_global(pxp);
if (ret)
complete_all(&pxp->termination);
}

static void pxp_terminate_complete(struct intel_pxp *pxp)
{
/* Re-create the arb session after teardown handle complete */
if (fetch_and_zero(&pxp->hw_state_invalidated))
pxp_create_arb_session(pxp);

complete_all(&pxp->termination);
}

void intel_pxp_session_work(struct work_struct *work)
{
struct intel_pxp *pxp = container_of(work, typeof(*pxp), session_work);
struct intel_gt *gt = pxp_to_gt(pxp);
u32 events = 0;

spin_lock_irq(&gt->irq_lock);
events = fetch_and_zero(&pxp->session_events);
spin_unlock_irq(&gt->irq_lock);

if (!events)
return;

if (events & PXP_TERMINATION_REQUEST) {
events &= ~PXP_TERMINATION_COMPLETE;
pxp_terminate(pxp);
}

if (events & PXP_TERMINATION_COMPLETE)
pxp_terminate_complete(pxp);
}
5 changes: 2 additions & 3 deletions drivers/gpu/drm/i915/pxp/intel_pxp_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

#include <linux/types.h>

struct intel_pxp;
struct work_struct;

int intel_pxp_create_arb_session(struct intel_pxp *pxp);
int intel_pxp_terminate_arb_session_and_global(struct intel_pxp *pxp);
void intel_pxp_session_work(struct work_struct *work);

#endif /* __INTEL_PXP_SESSION_H__ */
Loading

0 comments on commit 2ae0968

Please sign in to comment.