From 12c7ea43b930976f35ce75d11fd3f55438868e13 Mon Sep 17 00:00:00 2001
From: Phil Elwell <phil@raspberrypi.com>
Date: Fri, 4 Aug 2023 11:26:10 +0100
Subject: [PATCH] drm/v3d: Improve MMU support for larger pages

The built-in MMU driver went most of the way towards supporting larger
kernel pages, but dropped the ball when it comes to calculating indexes
into the page table. Fix it.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
---
 drivers/gpu/drm/v3d/v3d_mmu.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

--- a/drivers/gpu/drm/v3d/v3d_mmu.c
+++ b/drivers/gpu/drm/v3d/v3d_mmu.c
@@ -22,6 +22,7 @@
 #include "v3d_regs.h"
 
 #define V3D_MMU_PAGE_SHIFT 12
+#define V3D_PAGE_FACTOR (PAGE_SIZE >> V3D_MMU_PAGE_SHIFT)
 
 /* Note: All PTEs for the 1MB superpage must be filled with the
  * superpage bit set.
@@ -88,7 +89,7 @@ void v3d_mmu_insert_ptes(struct v3d_bo *
 {
 	struct drm_gem_shmem_object *shmem_obj = &bo->base;
 	struct v3d_dev *v3d = to_v3d_dev(shmem_obj->base.dev);
-	u32 page = bo->node.start;
+	u32 page = bo->node.start * V3D_PAGE_FACTOR;
 	u32 page_prot = V3D_PTE_WRITEABLE | V3D_PTE_VALID;
 	struct sg_dma_page_iter dma_iter;
 
@@ -98,13 +99,13 @@ void v3d_mmu_insert_ptes(struct v3d_bo *
 		u32 pte = page_prot | page_address;
 		u32 i;
 
-		BUG_ON(page_address + (PAGE_SIZE >> V3D_MMU_PAGE_SHIFT) >=
+		BUG_ON(page_address + V3D_PAGE_FACTOR >=
 		       BIT(24));
-		for (i = 0; i < PAGE_SIZE >> V3D_MMU_PAGE_SHIFT; i++)
+		for (i = 0; i < V3D_PAGE_FACTOR; i++)
 			v3d->pt[page++] = pte + i;
 	}
 
-	WARN_ON_ONCE(page - bo->node.start !=
+	WARN_ON_ONCE(page - (bo->node.start * V3D_PAGE_FACTOR) !=
 		     shmem_obj->base.size >> V3D_MMU_PAGE_SHIFT);
 
 	if (v3d_mmu_flush_all(v3d))
@@ -115,10 +116,10 @@ void v3d_mmu_remove_ptes(struct v3d_bo *
 {
 	struct v3d_dev *v3d = to_v3d_dev(bo->base.base.dev);
 	u32 npages = bo->base.base.size >> V3D_MMU_PAGE_SHIFT;
-	u32 page;
+	u32 page = bo->node.start * V3D_PAGE_FACTOR;
 
-	for (page = bo->node.start; page < bo->node.start + npages; page++)
-		v3d->pt[page] = 0;
+	while (npages--)
+		v3d->pt[page++] = 0;
 
 	if (v3d_mmu_flush_all(v3d))
 		dev_err(v3d->drm.dev, "MMU flush timeout\n");
