Skip to contentSkip to conversation

iommu/sun50i: Convert to map_pages/unmap_pages

PATCH In review Jernej Skrabec 2 participants 4 messages Sep 12, 2023 – Sep 25, 2023

Subsystems (2)
lore
Share & tools
lore

Jernej Skrabec

author maintainer
#0 lore raw

PATCH 1/1 iommu/sun50i: Convert to map_pages/unmap_pages

Convert driver to use map_pages and unmap_pages. Since functions operate on page table, extend them to be able to operate on whole table, not just one entry.

---
Hi!

I'm not sure if it makes sense to check validity of page table entry when unmaping pages makes sense. What do you think?

Best regards,
Jernej

 drivers/iommu/sun50i-iommu.c | 55 +++++++++++++++++++++---------------
 1 file changed, 33 insertions(+), 22 deletions(-)
drivers/iommu/sun50i-iommu.c +33 -22
index 74c5cb93e900..be6102730a56 100644
@@ -589,11 +589,12 @@ static u32 *sun50i_dte_get_page_table(struct sun50i_iommu_domain *sun50i_domain,
 }
 
 static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,
-			    phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
+			    phys_addr_t paddr, size_t pgsize, size_t pgcount,
+			    int prot, gfp_t gfp, size_t *mapped)
 {
 	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
 	struct sun50i_iommu *iommu = sun50i_domain->iommu;
-	u32 pte_index;
+	u32 pte_index, count, i;
 	u32 *page_table, *pte_addr;
 	int ret = 0;
 
@@ -604,45 +605,55 @@ static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,
 	}
 
 	pte_index = sun50i_iova_get_pte_index(iova);
-	pte_addr = &page_table[pte_index];
-	if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) {
-		phys_addr_t page_phys = sun50i_pte_get_page_address(*pte_addr);
-		dev_err(iommu->dev,
-			"iova %pad already mapped to %pa cannot remap to %pa prot: %#x\n",
-			&iova, &page_phys, &paddr, prot);
-		ret = -EBUSY;
-		goto out;
+	count = min(pgcount, (size_t)NUM_PT_ENTRIES - pte_index);
+	for (i = 0; i < count; i++) {
+		pte_addr = &page_table[pte_index + i];
+		if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) {
+			phys_addr_t page_phys = sun50i_pte_get_page_address(*pte_addr);
+
+			dev_err(iommu->dev,
+				"iova %pad already mapped to %pa cannot remap to %pa prot: %#x\n",
+				&iova, &page_phys, &paddr, prot);
+			ret = -EBUSY;
+			goto out;
+		}
+		*pte_addr = sun50i_mk_pte(paddr, prot);
+		paddr += SPAGE_SIZE;
 	}
 
-	*pte_addr = sun50i_mk_pte(paddr, prot);
-	sun50i_table_flush(sun50i_domain, pte_addr, 1);
+	sun50i_table_flush(sun50i_domain, &page_table[pte_index], i);
+	*mapped = i * SPAGE_SIZE;
 
 out:
 	return ret;
 }
 
 static size_t sun50i_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
-				 size_t size, struct iommu_iotlb_gather *gather)
+				 size_t pgsize, size_t pgcount,
+				 struct iommu_iotlb_gather *gather)
 {
 	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
+	u32 dte, count, i, pte_index;
 	phys_addr_t pt_phys;
 	u32 *pte_addr;
-	u32 dte;
 
 	dte = sun50i_domain->dt[sun50i_iova_get_dte_index(iova)];
 	if (!sun50i_dte_is_pt_valid(dte))
 		return 0;
 
 	pt_phys = sun50i_dte_get_pt_address(dte);
-	pte_addr = (u32 *)phys_to_virt(pt_phys) + sun50i_iova_get_pte_index(iova);
+	pte_index = sun50i_iova_get_pte_index(iova);
+	pte_addr = (u32 *)phys_to_virt(pt_phys) + pte_index;
 
-	if (!sun50i_pte_is_page_valid(*pte_addr))
-		return 0;
+	count = min(pgcount, (size_t)NUM_PT_ENTRIES - pte_index);
+	for (i = 0; i < count; i++)
+		if (!sun50i_pte_is_page_valid(pte_addr[i]))
+			break;
 
-	memset(pte_addr, 0, sizeof(*pte_addr));
-	sun50i_table_flush(sun50i_domain, pte_addr, 1);
+	memset(pte_addr, 0, sizeof(*pte_addr) * i);
+	sun50i_table_flush(sun50i_domain, pte_addr, i);
 
-	return SZ_4K;
+	return i * SPAGE_SIZE;
 }
 
 static phys_addr_t sun50i_iommu_iova_to_phys(struct iommu_domain *domain,
@@ -838,8 +849,8 @@ static const struct iommu_ops sun50i_iommu_ops = {
 		.iotlb_sync_map = sun50i_iommu_iotlb_sync_map,
 		.iotlb_sync	= sun50i_iommu_iotlb_sync,
 		.iova_to_phys	= sun50i_iommu_iova_to_phys,
-		.map		= sun50i_iommu_map,
-		.unmap		= sun50i_iommu_unmap,
+		.map_pages	= sun50i_iommu_map,
+		.unmap_pages	= sun50i_iommu_unmap,
 		.free		= sun50i_iommu_domain_free,
 	}
 };
Signature · 1 line
2.42.0
reviewer
#1 lore raw
Quoted 10 lines · Jernej Skrabec

Convert driver to use map_pages and unmap_pages. Since functions operate on page table, extend them to be able to operate on whole table, not just one entry.

---
Hi!

I'm not sure if it makes sense to check validity of page table entry when unmaping pages makes sense. What do you think?

As things currently stand it's largely a matter of opinion - some drivers consider that unmapping something which hasn't been mapped is nonsensical, and almost always represents the caller having gone wrong, thus report it as an error; others take the view that as long as they can achieve the requested end result, they're not going to think too hard about how they got there. The same arguments apply to whether you'd quietly replace an existing PTE or not when mapping, so I'd say the main thing is to at least be self-consistent one way or the other.

Cheers,
Robin.

Quoted 111 lines

Best regards,
Jernej

  drivers/iommu/sun50i-iommu.c | 55 +++++++++++++++++++++---------------
  1 file changed, 33 insertions(+), 22 deletions(-)
drivers/iommu/sun50i-iommu.c +33 -22
index 74c5cb93e900..be6102730a56 100644
@@ -589,11 +589,12 @@ static u32 *sun50i_dte_get_page_table(struct sun50i_iommu_domain *sun50i_domain,
  }
 
  static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,
-			    phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
+			    phys_addr_t paddr, size_t pgsize, size_t pgcount,
+			    int prot, gfp_t gfp, size_t *mapped)
  {
  	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
  	struct sun50i_iommu *iommu = sun50i_domain->iommu;
-	u32 pte_index;
+	u32 pte_index, count, i;
  	u32 *page_table, *pte_addr;
  	int ret = 0;
 
@@ -604,45 +605,55 @@ static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,
  	}
 
  	pte_index = sun50i_iova_get_pte_index(iova);
-	pte_addr = &page_table[pte_index];
-	if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) {
-		phys_addr_t page_phys = sun50i_pte_get_page_address(*pte_addr);
-		dev_err(iommu->dev,
-			"iova %pad already mapped to %pa cannot remap to %pa prot: %#x\n",
-			&iova, &page_phys, &paddr, prot);
-		ret = -EBUSY;
-		goto out;
+	count = min(pgcount, (size_t)NUM_PT_ENTRIES - pte_index);
+	for (i = 0; i < count; i++) {
+		pte_addr = &page_table[pte_index + i];
+		if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) {
+			phys_addr_t page_phys = sun50i_pte_get_page_address(*pte_addr);
+
+			dev_err(iommu->dev,
+				"iova %pad already mapped to %pa cannot remap to %pa prot: %#x\n",
+				&iova, &page_phys, &paddr, prot);
+			ret = -EBUSY;
+			goto out;
+		}
+		*pte_addr = sun50i_mk_pte(paddr, prot);
+		paddr += SPAGE_SIZE;
  	}
 
-	*pte_addr = sun50i_mk_pte(paddr, prot);
-	sun50i_table_flush(sun50i_domain, pte_addr, 1);
+	sun50i_table_flush(sun50i_domain, &page_table[pte_index], i);
+	*mapped = i * SPAGE_SIZE;
 
  out:
  	return ret;
  }
 
  static size_t sun50i_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
-				 size_t size, struct iommu_iotlb_gather *gather)
+				 size_t pgsize, size_t pgcount,
+				 struct iommu_iotlb_gather *gather)
  {
  	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
+	u32 dte, count, i, pte_index;
  	phys_addr_t pt_phys;
  	u32 *pte_addr;
-	u32 dte;
 
  	dte = sun50i_domain->dt[sun50i_iova_get_dte_index(iova)];
  	if (!sun50i_dte_is_pt_valid(dte))
  		return 0;
 
  	pt_phys = sun50i_dte_get_pt_address(dte);
-	pte_addr = (u32 *)phys_to_virt(pt_phys) + sun50i_iova_get_pte_index(iova);
+	pte_index = sun50i_iova_get_pte_index(iova);
+	pte_addr = (u32 *)phys_to_virt(pt_phys) + pte_index;
 
-	if (!sun50i_pte_is_page_valid(*pte_addr))
-		return 0;
+	count = min(pgcount, (size_t)NUM_PT_ENTRIES - pte_index);
+	for (i = 0; i < count; i++)
+		if (!sun50i_pte_is_page_valid(pte_addr[i]))
+			break;
 
-	memset(pte_addr, 0, sizeof(*pte_addr));
-	sun50i_table_flush(sun50i_domain, pte_addr, 1);
+	memset(pte_addr, 0, sizeof(*pte_addr) * i);
+	sun50i_table_flush(sun50i_domain, pte_addr, i);
 
-	return SZ_4K;
+	return i * SPAGE_SIZE;
  }
 
  static phys_addr_t sun50i_iommu_iova_to_phys(struct iommu_domain *domain,
@@ -838,8 +849,8 @@ static const struct iommu_ops sun50i_iommu_ops = {
  		.iotlb_sync_map = sun50i_iommu_iotlb_sync_map,
  		.iotlb_sync	= sun50i_iommu_iotlb_sync,
  		.iova_to_phys	= sun50i_iommu_iova_to_phys,
-		.map		= sun50i_iommu_map,
-		.unmap		= sun50i_iommu_unmap,
+		.map_pages	= sun50i_iommu_map,
+		.unmap_pages	= sun50i_iommu_unmap,
  		.free		= sun50i_iommu_domain_free,
  	}
  };
author maintainer
#2 lore raw

Dne sreda, 13. september 2023 ob 22:20:15 CEST je Robin Murphy napisal(a):

Quoted 12 lines · Jernej Skrabec
Quoted 10 lines

Convert driver to use map_pages and unmap_pages. Since functions operate on page table, extend them to be able to operate on whole table, not just one entry.

---
Hi!

I'm not sure if it makes sense to check validity of page table entry when unmaping pages makes sense. What do you think?

Quoted 8 lines

As things currently stand it's largely a matter of opinion - some drivers consider that unmapping something which hasn't been mapped is nonsensical, and almost always represents the caller having gone wrong, thus report it as an error; others take the view that as long as they can achieve the requested end result, they're not going to think too hard about how they got there. The same arguments apply to whether you'd quietly replace an existing PTE or not when mapping, so I'd say the main thing is to at least be self-consistent one way or the other.

Ok, I preserved strict checks in this update, which is consistent and same as before.

Best regards,
Jernej

Quoted 18 lines

Cheers,
Robin.

Quoted 14 lines

Best regards,
Jernej

  drivers/iommu/sun50i-iommu.c | 55 +++++++++++++++++++++---------------
  1 file changed, 33 insertions(+), 22 deletions(-)
drivers/iommu/sun50i-iommu.c +0 -0
index 74c5cb93e900..be6102730a56 100644
@@ -589,11 +589,12 @@ static u32 *sun50i_dte_get_page_table(struct

sun50i_iommu_domain *sun50i_domain,>
}

Quoted 3 lines

drivers/iommu/sun50i-iommu.c

Quoted 3 lines

static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,>

drivers/iommu/sun50i-iommu.c +0 -1
-			    phys_addr_t paddr, size_t size, int 

prot, gfp_t gfp)

Quoted 1 line

drivers/iommu/sun50i-iommu.c

Quoted 1 line
drivers/iommu/sun50i-iommu.c +1 -0
+			    phys_addr_t paddr, size_t pgsize, size_t 

pgcount,

Quoted 5 lines

drivers/iommu/sun50i-iommu.c

Quoted 5 lines
drivers/iommu/sun50i-iommu.c +1 -0
+			    int prot, gfp_t gfp, size_t *mapped)
 
  {
 
  	struct sun50i_iommu_domain *sun50i_domain = 

to_sun50i_domain(domain);

Quoted 12 lines
Quoted 12 lines
  	struct sun50i_iommu *iommu = sun50i_domain->iommu;
drivers/iommu/sun50i-iommu.c +1 -1
-	u32 pte_index;
+	u32 pte_index, count, i;
 
  	u32 *page_table, *pte_addr;
  	int ret = 0;
@@ -604,45 +605,55 @@ static int sun50i_iommu_map(struct iommu_domain
 *domain, unsigned long iova,> 
  	}
 
Quoted 5 lines

drivers/iommu/sun50i-iommu.c static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,

Quoted 5 lines
drivers/iommu/sun50i-iommu.c +0 -3
  	pte_index = sun50i_iova_get_pte_index(iova);
 
-	pte_addr = &page_table[pte_index];
-	if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) {
-		phys_addr_t page_phys = 

sun50i_pte_get_page_address(*pte_addr);

Quoted 2 lines

drivers/iommu/sun50i-iommu.c static int sun50i_iommu_map(struct iommu_domain · line 613

Quoted 2 lines
drivers/iommu/sun50i-iommu.c +0 -2
-		dev_err(iommu->dev,
-			"iova %pad already mapped to %pa cannot 

remap to %pa prot: %#x\n",

Quoted 8 lines

drivers/iommu/sun50i-iommu.c static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,

Quoted 8 lines
drivers/iommu/sun50i-iommu.c +5 -3
-			&iova, &page_phys, &paddr, prot);
-		ret = -EBUSY;
-		goto out;
+	count = min(pgcount, (size_t)NUM_PT_ENTRIES - pte_index);
+	for (i = 0; i < count; i++) {
+		pte_addr = &page_table[pte_index + i];
+		if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) {
+			phys_addr_t page_phys = 

sun50i_pte_get_page_address(*pte_addr);

Quoted 3 lines

drivers/iommu/sun50i-iommu.c static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,

Quoted 3 lines
drivers/iommu/sun50i-iommu.c +3 -0
+
+			dev_err(iommu->dev,
+				"iova %pad already mapped to %pa 

cannot remap to %pa prot: %#x\n",

Quoted 14 lines
Quoted 14 lines
drivers/iommu/sun50i-iommu.c +8 -2
+				&iova, &page_phys, &paddr, prot);
+			ret = -EBUSY;
+			goto out;
+		}
+		*pte_addr = sun50i_mk_pte(paddr, prot);
+		paddr += SPAGE_SIZE;
 
  	}
 
-	*pte_addr = sun50i_mk_pte(paddr, prot);
-	sun50i_table_flush(sun50i_domain, pte_addr, 1);
+	sun50i_table_flush(sun50i_domain, &page_table[pte_index], i);
+	*mapped = i * SPAGE_SIZE;
 
Quoted 8 lines

drivers/iommu/sun50i-iommu.c static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,

Quoted 8 lines
drivers/iommu/sun50i-iommu.c +0 -1
  out:
  	return ret;
 
  }
 
  static size_t sun50i_iommu_unmap(struct iommu_domain *domain, unsigned
  long iova,> 
-				 size_t size, struct 

iommu_iotlb_gather *gather)

Quoted 2 lines

drivers/iommu/sun50i-iommu.c static int sun50i_iommu_map(struct iommu_domain · line 638

Quoted 2 lines
drivers/iommu/sun50i-iommu.c +2 -0
+				 size_t pgsize, size_t pgcount,
+				 struct iommu_iotlb_gather 

*gather)

Quoted 4 lines

drivers/iommu/sun50i-iommu.c static int sun50i_iommu_map(struct iommu_domain · line 641

Quoted 4 lines
drivers/iommu/sun50i-iommu.c +0 -0
  {
 
  	struct sun50i_iommu_domain *sun50i_domain = 

to_sun50i_domain(domain);

Quoted 56 lines
Quoted 56 lines
drivers/iommu/sun50i-iommu.c +1 -2
+	u32 dte, count, i, pte_index;
 
  	phys_addr_t pt_phys;
  	u32 *pte_addr;
 
-	u32 dte;
 
  	dte = sun50i_domain->dt[sun50i_iova_get_dte_index(iova)];
  	if (!sun50i_dte_is_pt_valid(dte))
 
  		return 0;
 
  	pt_phys = sun50i_dte_get_pt_address(dte);
 
-	pte_addr = (u32 *)phys_to_virt(pt_phys) +

sun50i_iova_get_pte_index(iova); + pte_index =
sun50i_iova_get_pte_index(iova);

drivers/iommu/sun50i-iommu.c +10 -7
+	pte_addr = (u32 *)phys_to_virt(pt_phys) + pte_index;
 
-	if (!sun50i_pte_is_page_valid(*pte_addr))
-		return 0;
+	count = min(pgcount, (size_t)NUM_PT_ENTRIES - pte_index);
+	for (i = 0; i < count; i++)
+		if (!sun50i_pte_is_page_valid(pte_addr[i]))
+			break;
 
-	memset(pte_addr, 0, sizeof(*pte_addr));
-	sun50i_table_flush(sun50i_domain, pte_addr, 1);
+	memset(pte_addr, 0, sizeof(*pte_addr) * i);
+	sun50i_table_flush(sun50i_domain, pte_addr, i);
 
-	return SZ_4K;
+	return i * SPAGE_SIZE;
 
  }
 
  static phys_addr_t sun50i_iommu_iova_to_phys(struct iommu_domain
  *domain,
@@ -838,8 +849,8 @@ static const struct iommu_ops sun50i_iommu_ops = {
 
  		.iotlb_sync_map = sun50i_iommu_iotlb_sync_map,
  		.iotlb_sync	= sun50i_iommu_iotlb_sync,
  		.iova_to_phys	= sun50i_iommu_iova_to_phys,
 
-		.map		= sun50i_iommu_map,
-		.unmap		= sun50i_iommu_unmap,
+		.map_pages	= sun50i_iommu_map,
+		.unmap_pages	= sun50i_iommu_unmap,
 

.free = sun50i_iommu_domain_free,

  	}
  
  };
reviewer
#3 lore raw
In reply to Jernej Skrabec
Quoted 57 lines · Jernej Skrabec

Convert driver to use map_pages and unmap_pages. Since functions operate on page table, extend them to be able to operate on whole table, not just one entry.

---
Hi!

I'm not sure if it makes sense to check validity of page table entry when unmaping pages makes sense. What do you think?

Best regards,
Jernej

  drivers/iommu/sun50i-iommu.c | 55 +++++++++++++++++++++---------------
  1 file changed, 33 insertions(+), 22 deletions(-)
drivers/iommu/sun50i-iommu.c +12 -10
index 74c5cb93e900..be6102730a56 100644
@@ -589,11 +589,12 @@ static u32 *sun50i_dte_get_page_table(struct sun50i_iommu_domain *sun50i_domain,
  }
 
  static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,
-			    phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
+			    phys_addr_t paddr, size_t pgsize, size_t pgcount,
+			    int prot, gfp_t gfp, size_t *mapped)
  {
  	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
  	struct sun50i_iommu *iommu = sun50i_domain->iommu;
-	u32 pte_index;
+	u32 pte_index, count, i;
  	u32 *page_table, *pte_addr;
  	int ret = 0;
 
@@ -604,45 +605,55 @@ static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,
  	}
 
  	pte_index = sun50i_iova_get_pte_index(iova);
-	pte_addr = &page_table[pte_index];
-	if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) {
-		phys_addr_t page_phys = sun50i_pte_get_page_address(*pte_addr);
-		dev_err(iommu->dev,
-			"iova %pad already mapped to %pa cannot remap to %pa prot: %#x\n",
-			&iova, &page_phys, &paddr, prot);
-		ret = -EBUSY;
-		goto out;
+	count = min(pgcount, (size_t)NUM_PT_ENTRIES - pte_index);
+	for (i = 0; i < count; i++) {
+		pte_addr = &page_table[pte_index + i];
+		if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) {
+			phys_addr_t page_phys = sun50i_pte_get_page_address(*pte_addr);
+
+			dev_err(iommu->dev,
+				"iova %pad already mapped to %pa cannot remap to %pa prot: %#x\n",
+				&iova, &page_phys, &paddr, prot);

drivers/iommu/sun50i-iommu.c static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova, · line 624

Quoted 10 lines
drivers/iommu/sun50i-iommu.c +6 -2
+			ret = -EBUSY;
+			goto out;
+		}
+		*pte_addr = sun50i_mk_pte(paddr, prot);
+		paddr += SPAGE_SIZE;
  	}
 
-	*pte_addr = sun50i_mk_pte(paddr, prot);
-	sun50i_table_flush(sun50i_domain, pte_addr, 1);
+	sun50i_table_flush(sun50i_domain, &page_table[pte_index], i);

I think you want the "out" label here, so that "mapped" is still updated and the core knows how much to undo on failure. I guess one could argue that the pagetables are already messed up if that happens, but it still doesn't seem like a good reason to deliberately compound the problem by leaving behind even more "unexpected" entries.

Thanks,
Robin.

Quoted 54 lines
drivers/iommu/sun50i-iommu.c +15 -10
+	*mapped = i * SPAGE_SIZE;
 
  out:
  	return ret;
  }
 
  static size_t sun50i_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
-				 size_t size, struct iommu_iotlb_gather *gather)
+				 size_t pgsize, size_t pgcount,
+				 struct iommu_iotlb_gather *gather)
  {
  	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
+	u32 dte, count, i, pte_index;
  	phys_addr_t pt_phys;
  	u32 *pte_addr;
-	u32 dte;
 
  	dte = sun50i_domain->dt[sun50i_iova_get_dte_index(iova)];
  	if (!sun50i_dte_is_pt_valid(dte))
  		return 0;
 
  	pt_phys = sun50i_dte_get_pt_address(dte);
-	pte_addr = (u32 *)phys_to_virt(pt_phys) + sun50i_iova_get_pte_index(iova);
+	pte_index = sun50i_iova_get_pte_index(iova);
+	pte_addr = (u32 *)phys_to_virt(pt_phys) + pte_index;
 
-	if (!sun50i_pte_is_page_valid(*pte_addr))
-		return 0;
+	count = min(pgcount, (size_t)NUM_PT_ENTRIES - pte_index);
+	for (i = 0; i < count; i++)
+		if (!sun50i_pte_is_page_valid(pte_addr[i]))
+			break;
 
-	memset(pte_addr, 0, sizeof(*pte_addr));
-	sun50i_table_flush(sun50i_domain, pte_addr, 1);
+	memset(pte_addr, 0, sizeof(*pte_addr) * i);
+	sun50i_table_flush(sun50i_domain, pte_addr, i);
 
-	return SZ_4K;
+	return i * SPAGE_SIZE;
  }
 
  static phys_addr_t sun50i_iommu_iova_to_phys(struct iommu_domain *domain,
@@ -838,8 +849,8 @@ static const struct iommu_ops sun50i_iommu_ops = {
  		.iotlb_sync_map = sun50i_iommu_iotlb_sync_map,
  		.iotlb_sync	= sun50i_iommu_iotlb_sync,
  		.iova_to_phys	= sun50i_iommu_iova_to_phys,
-		.map		= sun50i_iommu_map,
-		.unmap		= sun50i_iommu_unmap,
+		.map_pages	= sun50i_iommu_map,
+		.unmap_pages	= sun50i_iommu_unmap,
  		.free		= sun50i_iommu_domain_free,
  	}
  };