Skip to content

Commit 4f0a600

Browse files
hegdevasantjoergroedel
authored andcommitted
iommu/amd: Refactor IOMMU tlb invalidation code
build_inv_iommu_pages() and build_inv_iommu_pasid() pretty much duplicates the code. Hence enhance build_inv_iommu_pages() to invalidate guest pages as well. And remove build_inv_iommu_pasid(). Suggested-by: Kishon Vijay Abraham I <kvijayab@amd.com> Signed-off-by: Vasant Hegde <vasant.hegde@amd.com> Reviewed-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/20231122090215.6191-6-vasant.hegde@amd.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent cf62924 commit 4f0a600

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

drivers/iommu/amd/iommu.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,17 +1124,23 @@ static inline u64 build_inv_address(u64 address, size_t size)
11241124
}
11251125

11261126
static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
1127-
size_t size, u16 domid)
1127+
size_t size, u16 domid,
1128+
ioasid_t pasid, bool gn)
11281129
{
11291130
u64 inv_address = build_inv_address(address, size);
11301131

11311132
memset(cmd, 0, sizeof(*cmd));
1133+
11321134
cmd->data[1] |= domid;
11331135
cmd->data[2] = lower_32_bits(inv_address);
11341136
cmd->data[3] = upper_32_bits(inv_address);
1135-
CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
11361137
/* PDE bit - we want to flush everything, not only the PTEs */
11371138
cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
1139+
if (gn) {
1140+
cmd->data[0] |= pasid;
1141+
cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1142+
}
1143+
CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
11381144
}
11391145

11401146
static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
@@ -1151,22 +1157,6 @@ static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
11511157
CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
11521158
}
11531159

1154-
static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid,
1155-
u64 address, size_t size)
1156-
{
1157-
u64 inv_address = build_inv_address(address, size);
1158-
1159-
memset(cmd, 0, sizeof(*cmd));
1160-
1161-
cmd->data[0] = pasid;
1162-
cmd->data[1] = domid;
1163-
cmd->data[2] = lower_32_bits(inv_address);
1164-
cmd->data[3] = upper_32_bits(inv_address);
1165-
cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
1166-
cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
1167-
CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
1168-
}
1169-
11701160
static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, u32 pasid,
11711161
int qdep, u64 address, size_t size)
11721162
{
@@ -1337,7 +1327,7 @@ static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
13371327
for (dom_id = 0; dom_id <= last_bdf; ++dom_id) {
13381328
struct iommu_cmd cmd;
13391329
build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1340-
dom_id);
1330+
dom_id, IOMMU_NO_PASID, false);
13411331
iommu_queue_command(iommu, &cmd);
13421332
}
13431333

@@ -1348,7 +1338,8 @@ static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
13481338
{
13491339
struct iommu_cmd cmd;
13501340

1351-
build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, dom_id);
1341+
build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
1342+
dom_id, IOMMU_NO_PASID, false);
13521343
iommu_queue_command(iommu, &cmd);
13531344

13541345
iommu_completion_wait(iommu);
@@ -1477,7 +1468,8 @@ static void __domain_flush_pages(struct protection_domain *domain,
14771468
struct iommu_cmd cmd;
14781469
int ret = 0, i;
14791470

1480-
build_inv_iommu_pages(&cmd, address, size, domain->id);
1471+
build_inv_iommu_pages(&cmd, address, size, domain->id,
1472+
IOMMU_NO_PASID, false);
14811473

14821474
for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
14831475
if (!domain->dev_iommu[i])
@@ -2661,7 +2653,7 @@ static int __flush_pasid(struct protection_domain *domain, u32 pasid,
26612653
if (!(domain->flags & PD_IOMMUV2_MASK))
26622654
return -EINVAL;
26632655

2664-
build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
2656+
build_inv_iommu_pages(&cmd, address, size, domain->id, pasid, true);
26652657

26662658
/*
26672659
* IOMMU TLB needs to be flushed before Device TLB to

0 commit comments

Comments
 (0)