Skip to content

Commit c7fc123

Browse files
hegdevasantjoergroedel
authored andcommitted
iommu/amd/pgtbl_v2: Invalidate updated page ranges only
Enhance __domain_flush_pages() to detect domain page table mode and use that info to build invalidation commands. So that we can use amd_iommu_domain_flush_pages() to invalidate v2 page table. Also pass PASID, gn variable to device_flush_iotlb() so that it can build IOTLB invalidation command for both v1 and v2 page table. Signed-off-by: Vasant Hegde <vasant.hegde@amd.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/20231122090215.6191-10-vasant.hegde@amd.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 2c535dd commit c7fc123

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

drivers/iommu/amd/io_pgtable_v2.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ static int iommu_v2_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
244244
unsigned long mapped_size = 0;
245245
unsigned long o_iova = iova;
246246
size_t size = pgcount << __ffs(pgsize);
247-
int count = 0;
248247
int ret = 0;
249248
bool updated = false;
250249

@@ -265,19 +264,14 @@ static int iommu_v2_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
265264

266265
*pte = set_pte_attr(paddr, map_size, prot);
267266

268-
count++;
269267
iova += map_size;
270268
paddr += map_size;
271269
mapped_size += map_size;
272270
}
273271

274272
out:
275-
if (updated) {
276-
if (count > 1)
277-
amd_iommu_flush_tlb(&pdom->domain, 0);
278-
else
279-
amd_iommu_flush_page(&pdom->domain, 0, o_iova);
280-
}
273+
if (updated)
274+
amd_iommu_domain_flush_pages(pdom, o_iova, size);
281275

282276
if (mapped)
283277
*mapped += mapped_size;

drivers/iommu/amd/iommu.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ static void detach_device(struct device *dev);
8585
*
8686
****************************************************************************/
8787

88+
static inline bool pdom_is_v2_pgtbl_mode(struct protection_domain *pdom)
89+
{
90+
return (pdom && (pdom->flags & PD_IOMMUV2_MASK));
91+
}
92+
8893
static inline int get_acpihid_device_id(struct device *dev,
8994
struct acpihid_map_entry **entry)
9095
{
@@ -1382,8 +1387,8 @@ void amd_iommu_flush_all_caches(struct amd_iommu *iommu)
13821387
/*
13831388
* Command send function for flushing on-device TLB
13841389
*/
1385-
static int device_flush_iotlb(struct iommu_dev_data *dev_data,
1386-
u64 address, size_t size)
1390+
static int device_flush_iotlb(struct iommu_dev_data *dev_data, u64 address,
1391+
size_t size, ioasid_t pasid, bool gn)
13871392
{
13881393
struct amd_iommu *iommu;
13891394
struct iommu_cmd cmd;
@@ -1395,7 +1400,7 @@ static int device_flush_iotlb(struct iommu_dev_data *dev_data,
13951400
return -EINVAL;
13961401

13971402
build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address,
1398-
size, IOMMU_NO_PASID, false);
1403+
size, pasid, gn);
13991404

14001405
return iommu_queue_command(iommu, &cmd);
14011406
}
@@ -1441,8 +1446,11 @@ static int device_flush_dte(struct iommu_dev_data *dev_data)
14411446
return ret;
14421447
}
14431448

1444-
if (dev_data->ats_enabled)
1445-
ret = device_flush_iotlb(dev_data, 0, ~0UL);
1449+
if (dev_data->ats_enabled) {
1450+
/* Invalidate the entire contents of an IOTLB */
1451+
ret = device_flush_iotlb(dev_data, 0, ~0UL,
1452+
IOMMU_NO_PASID, false);
1453+
}
14461454

14471455
return ret;
14481456
}
@@ -1458,9 +1466,13 @@ static void __domain_flush_pages(struct protection_domain *domain,
14581466
struct iommu_dev_data *dev_data;
14591467
struct iommu_cmd cmd;
14601468
int ret = 0, i;
1469+
ioasid_t pasid = IOMMU_NO_PASID;
1470+
bool gn = false;
1471+
1472+
if (pdom_is_v2_pgtbl_mode(domain))
1473+
gn = true;
14611474

1462-
build_inv_iommu_pages(&cmd, address, size, domain->id,
1463-
IOMMU_NO_PASID, false);
1475+
build_inv_iommu_pages(&cmd, address, size, domain->id, pasid, gn);
14641476

14651477
for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
14661478
if (!domain->dev_iommu[i])
@@ -1478,7 +1490,7 @@ static void __domain_flush_pages(struct protection_domain *domain,
14781490
if (!dev_data->ats_enabled)
14791491
continue;
14801492

1481-
ret |= device_flush_iotlb(dev_data, address, size);
1493+
ret |= device_flush_iotlb(dev_data, address, size, pasid, gn);
14821494
}
14831495

14841496
WARN_ON(ret);

0 commit comments

Comments
 (0)