* [PATCH 0/2] amd/iommu: Clean up DTE programming
@ 2024-08-16 15:43 Suravee Suthikulpanit
2024-08-16 15:43 ` [PATCH 1/2] iommu/amd: Introduce struct gcr3_tbl_info.giov Suravee Suthikulpanit
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Suravee Suthikulpanit @ 2024-08-16 15:43 UTC (permalink / raw)
To: linux-kernel, iommu
Cc: joro, robin.murphy, vasant.hegde, jon.grimm, santosh.shukla,
pandoh, kumaranand, Suravee Suthikulpanit
Hi,
This series cleans up set_dte_entry () in AMD IOMMU driver.
This is also a preparation for subsequent nested translation
support series.
Thanks,
Suravee
Suravee Suthikulpanit (2):
iommu/amd: Introduce struct gcr3_tbl_info.giov
iommu/amd: Refactor set_dte_entry to simplify GCR3 Table Root Pointer
programming
drivers/iommu/amd/amd_iommu_types.h | 1 +
drivers/iommu/amd/iommu.c | 131 +++++++++++++++++-----------
2 files changed, 79 insertions(+), 53 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] iommu/amd: Introduce struct gcr3_tbl_info.giov
2024-08-16 15:43 [PATCH 0/2] amd/iommu: Clean up DTE programming Suravee Suthikulpanit
@ 2024-08-16 15:43 ` Suravee Suthikulpanit
2024-08-16 15:43 ` [PATCH 2/2] iommu/amd: Refactor set_dte_entry to simplify GCR3 Table Root Pointer programming Suravee Suthikulpanit
2024-08-16 16:58 ` [PATCH 0/2] amd/iommu: Clean up DTE programming Suthikulpanit, Suravee
2 siblings, 0 replies; 4+ messages in thread
From: Suravee Suthikulpanit @ 2024-08-16 15:43 UTC (permalink / raw)
To: linux-kernel, iommu
Cc: joro, robin.murphy, vasant.hegde, jon.grimm, santosh.shukla,
pandoh, kumaranand, Suravee Suthikulpanit
To track DTE[GIOV] programming during IOMMU domain attach, also add logic
to determine if the GIOV is required, and set the variable accordinglly.
Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
drivers/iommu/amd/amd_iommu_types.h | 1 +
drivers/iommu/amd/iommu.c | 10 ++++++++++
2 files changed, 11 insertions(+)
diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h
index 2b76b5dedc1d..9be761ba0214 100644
--- a/drivers/iommu/amd/amd_iommu_types.h
+++ b/drivers/iommu/amd/amd_iommu_types.h
@@ -544,6 +544,7 @@ struct gcr3_tbl_info {
int glx; /* Number of levels for GCR3 table */
u32 pasid_cnt; /* Track attached PASIDs */
u16 domid; /* Per device domain ID */
+ bool giov; /* Track DTE[GIOV] */
};
struct amd_io_pgtable {
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 87c5385ce3f2..4667b6c4906f 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2011,6 +2011,16 @@ static int init_gcr3_table(struct iommu_dev_data *dev_data,
if (ret)
free_gcr3_table(&dev_data->gcr3_info);
+ if (!ret) {
+ /*
+ * GIOV is required for PD_MODE_V2 because we need
+ * to support the case where the end-point device
+ * does not have PASID in the TLP prefix when setting
+ * up to use the v2 table.
+ */
+ dev_data->gcr3_info.giov = true;
+ }
+
return ret;
}
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/2] iommu/amd: Refactor set_dte_entry to simplify GCR3 Table Root Pointer programming
2024-08-16 15:43 [PATCH 0/2] amd/iommu: Clean up DTE programming Suravee Suthikulpanit
2024-08-16 15:43 ` [PATCH 1/2] iommu/amd: Introduce struct gcr3_tbl_info.giov Suravee Suthikulpanit
@ 2024-08-16 15:43 ` Suravee Suthikulpanit
2024-08-16 16:58 ` [PATCH 0/2] amd/iommu: Clean up DTE programming Suthikulpanit, Suravee
2 siblings, 0 replies; 4+ messages in thread
From: Suravee Suthikulpanit @ 2024-08-16 15:43 UTC (permalink / raw)
To: linux-kernel, iommu
Cc: joro, robin.murphy, vasant.hegde, jon.grimm, santosh.shukla,
pandoh, kumaranand, Suravee Suthikulpanit
Separate logic for setting DTE[GCR3 Table Root Pointer] into a helper
function set_dte_gcr3_table().
Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
drivers/iommu/amd/iommu.c | 121 +++++++++++++++++++++-----------------
1 file changed, 68 insertions(+), 53 deletions(-)
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 4667b6c4906f..8fc496db5560 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -1851,90 +1851,105 @@ int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid)
return ret;
}
+static void set_dte_gcr3_table(struct amd_iommu *iommu,
+ struct iommu_dev_data *dev_data,
+ struct dev_table_entry *target)
+{
+ struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
+ u64 tmp, gcr3;
+
+ if (!gcr3_info->gcr3_tbl)
+ return;
+
+ pr_debug("%s: devid=%#x, glx=%#x, giov=%#x, gcr3_tbl=%#llx\n",
+ __func__, dev_data->devid, gcr3_info->glx, gcr3_info->giov,
+ (unsigned long long)gcr3_info->gcr3_tbl);
+
+ tmp = gcr3_info->glx;
+ target->data[0] |= (tmp & DTE_GLX_MASK) << DTE_GLX_SHIFT;
+ if (gcr3_info->giov)
+ target->data[0] |= DTE_FLAG_GIOV;
+ target->data[0] |= DTE_FLAG_GV;
+
+ /* First mask out possible old values for GCR3 table */
+ tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
+ target->data[0] &= ~tmp;
+ tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
+ tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
+ target->data[1] &= ~tmp;
+
+ gcr3 = iommu_virt_to_phys(gcr3_info->gcr3_tbl);
+
+ /* Encode GCR3 table into DTE */
+ tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
+ target->data[0] |= tmp;
+ tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
+ tmp |= DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
+ target->data[1] |= tmp;
+
+ /* Use system default */
+ tmp = amd_iommu_gpt_level;
+
+ /* Mask out old values for GuestPagingMode */
+ target->data[2] &= ~(0x3ULL << DTE_GPT_LEVEL_SHIFT);
+ target->data[2] |= (tmp << DTE_GPT_LEVEL_SHIFT);
+}
+
static void set_dte_entry(struct amd_iommu *iommu,
struct iommu_dev_data *dev_data)
{
- u64 pte_root = 0;
- u64 flags = 0;
- u32 old_domid;
- u16 devid = dev_data->devid;
u16 domid;
+ u16 devid = dev_data->devid;
struct protection_domain *domain = dev_data->domain;
+ struct dev_table_entry target = {.data = {0, 0, 0, 0}};
struct dev_table_entry *dev_table = get_dev_table(iommu);
struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
+ u32 old_domid = dev_table[devid].data[1] & DEV_DOMID_MASK;
- if (gcr3_info && gcr3_info->gcr3_tbl)
+ if (gcr3_info->gcr3_tbl)
domid = dev_data->gcr3_info.domid;
else
domid = domain->id;
+ /*
+ * Need to get the current value in dte[1,2] because they contain
+ * interrupt-remapping settings, which has been programmed earlier.
+ */
+ target.data[1] = dev_table[devid].data[1];
+ target.data[2] = dev_table[devid].data[2];
+
if (domain->iop.mode != PAGE_MODE_NONE)
- pte_root = iommu_virt_to_phys(domain->iop.root);
+ target.data[0] = iommu_virt_to_phys(domain->iop.root);
- pte_root |= (domain->iop.mode & DEV_ENTRY_MODE_MASK)
+ target.data[0] |= (domain->iop.mode & DEV_ENTRY_MODE_MASK)
<< DEV_ENTRY_MODE_SHIFT;
- pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V;
+ target.data[0] |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V;
/*
* When SNP is enabled, Only set TV bit when IOMMU
* page translation is in use.
*/
if (!amd_iommu_snp_en || (domid != 0))
- pte_root |= DTE_FLAG_TV;
-
- flags = dev_table[devid].data[1];
+ target.data[0] |= DTE_FLAG_TV;
if (dev_data->ats_enabled)
- flags |= DTE_FLAG_IOTLB;
+ target.data[1] |= DTE_FLAG_IOTLB;
if (dev_data->ppr)
- pte_root |= 1ULL << DEV_ENTRY_PPR;
+ target.data[0] |= 1ULL << DEV_ENTRY_PPR;
if (domain->dirty_tracking)
- pte_root |= DTE_FLAG_HAD;
-
- if (gcr3_info && gcr3_info->gcr3_tbl) {
- u64 gcr3 = iommu_virt_to_phys(gcr3_info->gcr3_tbl);
- u64 glx = gcr3_info->glx;
- u64 tmp;
-
- pte_root |= DTE_FLAG_GV;
- pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
-
- /* First mask out possible old values for GCR3 table */
- tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
- flags &= ~tmp;
+ target.data[0] |= DTE_FLAG_HAD;
- tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
- flags &= ~tmp;
-
- /* Encode GCR3 table into DTE */
- tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
- pte_root |= tmp;
-
- tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
- flags |= tmp;
-
- tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
- flags |= tmp;
-
- if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL) {
- dev_table[devid].data[2] |=
- ((u64)GUEST_PGTABLE_5_LEVEL << DTE_GPT_LEVEL_SHIFT);
- }
-
- /* GIOV is supported with V2 page table mode only */
- if (pdom_is_v2_pgtbl_mode(domain))
- pte_root |= DTE_FLAG_GIOV;
- }
+ target.data[1] &= ~DEV_DOMID_MASK;
+ target.data[1] |= domid;
- flags &= ~DEV_DOMID_MASK;
- flags |= domid;
+ set_dte_gcr3_table(iommu, dev_data, &target);
- old_domid = dev_table[devid].data[1] & DEV_DOMID_MASK;
- dev_table[devid].data[1] = flags;
- dev_table[devid].data[0] = pte_root;
+ dev_table[devid].data[0] = target.data[0];
+ dev_table[devid].data[1] = target.data[1];
+ dev_table[devid].data[2] = target.data[2];
/*
* A kdump kernel might be replacing a domain ID that was copied from
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 0/2] amd/iommu: Clean up DTE programming
2024-08-16 15:43 [PATCH 0/2] amd/iommu: Clean up DTE programming Suravee Suthikulpanit
2024-08-16 15:43 ` [PATCH 1/2] iommu/amd: Introduce struct gcr3_tbl_info.giov Suravee Suthikulpanit
2024-08-16 15:43 ` [PATCH 2/2] iommu/amd: Refactor set_dte_entry to simplify GCR3 Table Root Pointer programming Suravee Suthikulpanit
@ 2024-08-16 16:58 ` Suthikulpanit, Suravee
2 siblings, 0 replies; 4+ messages in thread
From: Suthikulpanit, Suravee @ 2024-08-16 16:58 UTC (permalink / raw)
To: linux-kernel, iommu
Cc: joro, robin.murphy, vasant.hegde, jon.grimm, santosh.shukla,
pandoh, kumaranand
Please ignore this thread. Sorry for the spam.
Suravee
On 8/16/2024 10:43 PM, Suravee Suthikulpanit wrote:
> Hi,
>
> This series cleans up set_dte_entry () in AMD IOMMU driver.
> This is also a preparation for subsequent nested translation
> support series.
>
> Thanks,
> Suravee
>
>
> Suravee Suthikulpanit (2):
> iommu/amd: Introduce struct gcr3_tbl_info.giov
> iommu/amd: Refactor set_dte_entry to simplify GCR3 Table Root Pointer
> programming
>
> drivers/iommu/amd/amd_iommu_types.h | 1 +
> drivers/iommu/amd/iommu.c | 131 +++++++++++++++++-----------
> 2 files changed, 79 insertions(+), 53 deletions(-)
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-08-16 16:58 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-16 15:43 [PATCH 0/2] amd/iommu: Clean up DTE programming Suravee Suthikulpanit
2024-08-16 15:43 ` [PATCH 1/2] iommu/amd: Introduce struct gcr3_tbl_info.giov Suravee Suthikulpanit
2024-08-16 15:43 ` [PATCH 2/2] iommu/amd: Refactor set_dte_entry to simplify GCR3 Table Root Pointer programming Suravee Suthikulpanit
2024-08-16 16:58 ` [PATCH 0/2] amd/iommu: Clean up DTE programming Suthikulpanit, Suravee
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®