Skip to contentSkip to conversation

drm/panthor: Fix firmware initialization on systems with a page size > 4k

PATCH Superseded Boris Brezillon 4 participants 5 messages Oct 8, 2024 – Oct 14, 2024

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Boris Brezillon

author maintainer
#0 lore raw

PATCH 1/1 drm/panthor: Fix firmware initialization on systems with a page size > 4k

The system and GPU MMU page size might differ, which becomes a problem for FW sections that need to be mapped at explicit address since our PAGE_SIZE alignment might cover a VA range that's expected to be used for another section.

Make sure we never map more than we need.

Fixes 2718d91816ee ("drm/panthor: Add the FW logical block")
---
 drivers/gpu/drm/panthor/panthor_fw.c  |  3 +--
 drivers/gpu/drm/panthor/panthor_gem.c | 11 ++++++++---
 drivers/gpu/drm/panthor/panthor_mmu.c |  6 +++---
 3 files changed, 12 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +1 -2
index ef232c0c2049..293846400296 100644
@@ -515,8 +515,7 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,
 		return -EINVAL;
 	}
 
-	if ((hdr.va.start & ~PAGE_MASK) != 0 ||
-	    (hdr.va.end & ~PAGE_MASK) != 0) {
+	if (!IS_ALIGNED(hdr.va.start, SZ_4K) || !IS_ALIGNED(hdr.va.end, SZ_4K)) {
 		drm_err(&ptdev->base, "Firmware corrupted, virtual addresses not page aligned: 0x%x-0x%x\n",
 			hdr.va.start, hdr.va.end);
 		return -EINVAL;
drivers/gpu/drm/panthor/panthor_gem.c +8 -3
index c60b599665d8..2c8d6e2c7232 100644
@@ -44,8 +44,7 @@ void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo)
 			to_panthor_bo(bo->obj)->exclusive_vm_root_gem != panthor_vm_root_gem(vm)))
 		goto out_free_bo;
 
-	ret = panthor_vm_unmap_range(vm, bo->va_node.start,
-				     panthor_kernel_bo_size(bo));
+	ret = panthor_vm_unmap_range(vm, bo->va_node.start, bo->va_node.size);
 	if (ret)
 		goto out_free_bo;
 
@@ -95,10 +94,16 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
 	}
 
 	bo = to_panthor_bo(&obj->base);
-	size = obj->base.size;
 	kbo->obj = &obj->base;
 	bo->flags = bo_flags;
 
+	/* The system and GPU MMU page size might differ, which becomes a
+	 * problem for FW sections that need to be mapped at explicit address
+	 * since our PAGE_SIZE alignment might cover a VA range that's
+	 * expected to be used for another section.
+	 * Make sure we never map more than we need.
+	 */
+	size = ALIGN(size, SZ_4K);
 	ret = panthor_vm_alloc_va(vm, gpu_va, size, &kbo->va_node);
 	if (ret)
 		goto err_put_obj;
drivers/gpu/drm/panthor/panthor_mmu.c +3 -3
index 3cd2bce59edc..e53d131c53cc 100644
@@ -1027,10 +1027,10 @@ panthor_vm_alloc_va(struct panthor_vm *vm, u64 va, u64 size,
 {
 	int ret;
 
-	if (!size || (size & ~PAGE_MASK))
+	if (!size || !IS_ALIGNED(size, SZ_4K))
 		return -EINVAL;
 
-	if (va != PANTHOR_VM_KERNEL_AUTO_VA && (va & ~PAGE_MASK))
+	if (va != PANTHOR_VM_KERNEL_AUTO_VA && !IS_ALIGNED(va, SZ_4K))
 		return -EINVAL;
 
 	mutex_lock(&vm->mm_lock);
@@ -2370,7 +2370,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,
 	int ret;
 
 	/* Aligned on page size. */
-	if ((op->va | op->size) & ~PAGE_MASK)
+	if (!IS_ALIGNED(op->va | op->size, SZ_4K))
 		return -EINVAL;
 
 	switch (op->flags & DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) {
Signature · 1 line
2.46.2
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Boris Brezillon

The system and GPU MMU page size might differ, which becomes a problem for FW sections that need to be mapped at explicit address since our PAGE_SIZE alignment might cover a VA range that's expected to be used for another section.

Make sure we never map more than we need.

Fixes 2718d91816ee ("drm/panthor: Add the FW logical block")
Quoted 78 lines
---
 drivers/gpu/drm/panthor/panthor_fw.c  |  3 +--
 drivers/gpu/drm/panthor/panthor_gem.c | 11 ++++++++---
 drivers/gpu/drm/panthor/panthor_mmu.c |  6 +++---
 3 files changed, 12 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +1 -2
index ef232c0c2049..293846400296 100644
@@ -515,8 +515,7 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,
 		return -EINVAL;
 	}
 
-	if ((hdr.va.start & ~PAGE_MASK) != 0 ||
-	    (hdr.va.end & ~PAGE_MASK) != 0) {
+	if (!IS_ALIGNED(hdr.va.start, SZ_4K) || !IS_ALIGNED(hdr.va.end, SZ_4K)) {
 		drm_err(&ptdev->base, "Firmware corrupted, virtual addresses not page aligned: 0x%x-0x%x\n",
 			hdr.va.start, hdr.va.end);
 		return -EINVAL;
drivers/gpu/drm/panthor/panthor_gem.c +8 -3
index c60b599665d8..2c8d6e2c7232 100644
@@ -44,8 +44,7 @@ void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo)
 			to_panthor_bo(bo->obj)->exclusive_vm_root_gem != panthor_vm_root_gem(vm)))
 		goto out_free_bo;
 
-	ret = panthor_vm_unmap_range(vm, bo->va_node.start,
-				     panthor_kernel_bo_size(bo));
+	ret = panthor_vm_unmap_range(vm, bo->va_node.start, bo->va_node.size);
 	if (ret)
 		goto out_free_bo;
 
@@ -95,10 +94,16 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
 	}
 
 	bo = to_panthor_bo(&obj->base);
-	size = obj->base.size;
 	kbo->obj = &obj->base;
 	bo->flags = bo_flags;
 
+	/* The system and GPU MMU page size might differ, which becomes a
+	 * problem for FW sections that need to be mapped at explicit address
+	 * since our PAGE_SIZE alignment might cover a VA range that's
+	 * expected to be used for another section.
+	 * Make sure we never map more than we need.
+	 */
+	size = ALIGN(size, SZ_4K);
 	ret = panthor_vm_alloc_va(vm, gpu_va, size, &kbo->va_node);
 	if (ret)
 		goto err_put_obj;
drivers/gpu/drm/panthor/panthor_mmu.c +3 -3
index 3cd2bce59edc..e53d131c53cc 100644
@@ -1027,10 +1027,10 @@ panthor_vm_alloc_va(struct panthor_vm *vm, u64 va, u64 size,
 {
 	int ret;
 
-	if (!size || (size & ~PAGE_MASK))
+	if (!size || !IS_ALIGNED(size, SZ_4K))
 		return -EINVAL;
 
-	if (va != PANTHOR_VM_KERNEL_AUTO_VA && (va & ~PAGE_MASK))
+	if (va != PANTHOR_VM_KERNEL_AUTO_VA && !IS_ALIGNED(va, SZ_4K))
 		return -EINVAL;
 
 	mutex_lock(&vm->mm_lock);
@@ -2370,7 +2370,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,
 	int ret;
 
 	/* Aligned on page size. */
-	if ((op->va | op->size) & ~PAGE_MASK)
+	if (!IS_ALIGNED(op->va | op->size, SZ_4K))
 		return -EINVAL;
 
 	switch (op->flags & DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) {
Reviewed-by
#2 lore raw

Hi Boris,

Quoted 54 lines · Steven Price
Quoted 9 lines · Boris Brezillon

The system and GPU MMU page size might differ, which becomes a problem for FW sections that need to be mapped at explicit address since our PAGE_SIZE alignment might cover a VA range that's expected to be used for another section.

Make sure we never map more than we need.

Fixes 2718d91816ee ("drm/panthor: Add the FW logical block")
Quoted 41 lines
---
 drivers/gpu/drm/panthor/panthor_fw.c  |  3 +--
 drivers/gpu/drm/panthor/panthor_gem.c | 11 ++++++++---
 drivers/gpu/drm/panthor/panthor_mmu.c |  6 +++---
 3 files changed, 12 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +1 -2
index ef232c0c2049..293846400296 100644
@@ -515,8 +515,7 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,
 		return -EINVAL;
 	}
 
-	if ((hdr.va.start & ~PAGE_MASK) != 0 ||
-	    (hdr.va.end & ~PAGE_MASK) != 0) {
+	if (!IS_ALIGNED(hdr.va.start, SZ_4K) || !IS_ALIGNED(hdr.va.end, SZ_4K)) {
 		drm_err(&ptdev->base, "Firmware corrupted, virtual addresses not page aligned: 0x%x-0x%x\n",
 			hdr.va.start, hdr.va.end);
 		return -EINVAL;
drivers/gpu/drm/panthor/panthor_gem.c +1 -3
index c60b599665d8..2c8d6e2c7232 100644
@@ -44,8 +44,7 @@ void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo)
 			to_panthor_bo(bo->obj)->exclusive_vm_root_gem != panthor_vm_root_gem(vm)))
 		goto out_free_bo;
 
-	ret = panthor_vm_unmap_range(vm, bo->va_node.start,
-				     panthor_kernel_bo_size(bo));
+	ret = panthor_vm_unmap_range(vm, bo->va_node.start, bo->va_node.size);
 	if (ret)
 		goto out_free_bo;
 
@@ -95,10 +94,16 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
 	}
 	bo = to_panthor_bo(&obj->base);
-	size = obj->base.size;
 	kbo->obj = &obj->base;
 	bo->flags = bo_flags;
 
Quoted 6 lines

drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 105

Quoted 6 lines
drivers/gpu/drm/panthor/panthor_gem.c +6 -0
+	/* The system and GPU MMU page size might differ, which becomes a
+	 * problem for FW sections that need to be mapped at explicit address
+	 * since our PAGE_SIZE alignment might cover a VA range that's
+	 * expected to be used for another section.
+	 * Make sure we never map more than we need.
+	 */

I was wondering, this must be a relatively common situation for other DRM drivers, so maybe the DRM shmem core should have a BO creation function that lets you specify an exact page alignment?

Quoted 1 line

drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 106

Quoted 1 line
drivers/gpu/drm/panthor/panthor_gem.c +1 -0
+	size = ALIGN(size, SZ_4K);

I was thinking, if we know that the FW's page size is always 4KiB, maybe we could do

#define CSF_FW_PAGESIZE SZ_4K

somewhere in panthor_fw.h to make things clearer?

Cheers,
Adrian

Quoted 29 lines
Quoted 29 lines
drivers/gpu/drm/panthor/panthor_gem.c +0 -0
 	ret = panthor_vm_alloc_va(vm, gpu_va, size, &kbo->va_node);
 	if (ret)
 		goto err_put_obj;
drivers/gpu/drm/panthor/panthor_mmu.c +3 -3
index 3cd2bce59edc..e53d131c53cc 100644
@@ -1027,10 +1027,10 @@ panthor_vm_alloc_va(struct panthor_vm *vm, u64 va, u64 size,
 {
 	int ret;
 
-	if (!size || (size & ~PAGE_MASK))
+	if (!size || !IS_ALIGNED(size, SZ_4K))
 		return -EINVAL;
 
-	if (va != PANTHOR_VM_KERNEL_AUTO_VA && (va & ~PAGE_MASK))
+	if (va != PANTHOR_VM_KERNEL_AUTO_VA && !IS_ALIGNED(va, SZ_4K))
 		return -EINVAL;
 
 	mutex_lock(&vm->mm_lock);
@@ -2370,7 +2370,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,
 	int ret;
 
 	/* Aligned on page size. */
-	if ((op->va | op->size) & ~PAGE_MASK)
+	if (!IS_ALIGNED(op->va | op->size, SZ_4K))
 		return -EINVAL;
 
 	switch (op->flags & DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) {
maintainer Reviewed-by
#3 lore raw
Quoted 15 lines · Adrián Larumbe

Hi Boris,

Quoted 12 lines · Steven Price
Quoted 9 lines · Boris Brezillon

The system and GPU MMU page size might differ, which becomes a problem for FW sections that need to be mapped at explicit address since our PAGE_SIZE alignment might cover a VA range that's expected to be used for another section.

Make sure we never map more than we need.

Fixes 2718d91816ee ("drm/panthor: Add the FW logical block")
Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
---
 drivers/gpu/drm/panthor/panthor_fw.c  |  3 +--
 drivers/gpu/drm/panthor/panthor_gem.c | 11 ++++++++---
 drivers/gpu/drm/panthor/panthor_mmu.c |  6 +++---
 3 files changed, 12 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +1 -2
index ef232c0c2049..293846400296 100644
@@ -515,8 +515,7 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,
 		return -EINVAL;
 	}
 
-	if ((hdr.va.start & ~PAGE_MASK) != 0 ||
-	    (hdr.va.end & ~PAGE_MASK) != 0) {
+	if (!IS_ALIGNED(hdr.va.start, SZ_4K) || !IS_ALIGNED(hdr.va.end, SZ_4K)) {
 		drm_err(&ptdev->base, "Firmware corrupted, virtual addresses not page aligned: 0x%x-0x%x\n",
 			hdr.va.start, hdr.va.end);
 		return -EINVAL;
drivers/gpu/drm/panthor/panthor_gem.c +1 -3
index c60b599665d8..2c8d6e2c7232 100644
@@ -44,8 +44,7 @@ void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo)
 			to_panthor_bo(bo->obj)->exclusive_vm_root_gem != panthor_vm_root_gem(vm)))
 		goto out_free_bo;
 
-	ret = panthor_vm_unmap_range(vm, bo->va_node.start,
-				     panthor_kernel_bo_size(bo));
+	ret = panthor_vm_unmap_range(vm, bo->va_node.start, bo->va_node.size);
 	if (ret)
 		goto out_free_bo;
 
@@ -95,10 +94,16 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
 	}
 	bo = to_panthor_bo(&obj->base);
-	size = obj->base.size;
 	kbo->obj = &obj->base;
 	bo->flags = bo_flags;
 
Quoted 10 lines
Quoted 6 lines

drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 105

Quoted 6 lines
drivers/gpu/drm/panthor/panthor_gem.c +6 -0
+	/* The system and GPU MMU page size might differ, which becomes a
+	 * problem for FW sections that need to be mapped at explicit address
+	 * since our PAGE_SIZE alignment might cover a VA range that's
+	 * expected to be used for another section.
+	 * Make sure we never map more than we need.
+	 */

I was wondering, this must be a relatively common situation for other DRM drivers, so maybe the DRM shmem core should have a BO creation function that lets you specify an exact page alignment?

There are probably not that many DRM drivers that need specific alignment, but I think it's not a bad idea to add a function like that in a separate patch.

Quoted 8 lines
Quoted 2 lines

drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 106

Quoted 2 lines
drivers/gpu/drm/panthor/panthor_gem.c +1 -0
+	size = ALIGN(size, SZ_4K);

I was thinking, if we know that the FW's page size is always 4KiB, maybe we could do

#define CSF_FW_PAGESIZE SZ_4K

somewhere in panthor_fw.h to make things clearer?

Until future comes in and the firmware page size changes :)
But yeah, it could help in the future to search for firmware's page size when we will need to update it.

Best regards,
Liviu

Quoted 36 lines

Cheers,
Adrian

Quoted 29 lines
Quoted 29 lines
drivers/gpu/drm/panthor/panthor_gem.c +0 -0
 	ret = panthor_vm_alloc_va(vm, gpu_va, size, &kbo->va_node);
 	if (ret)
 		goto err_put_obj;
drivers/gpu/drm/panthor/panthor_mmu.c +3 -3
index 3cd2bce59edc..e53d131c53cc 100644
@@ -1027,10 +1027,10 @@ panthor_vm_alloc_va(struct panthor_vm *vm, u64 va, u64 size,
 {
 	int ret;
 
-	if (!size || (size & ~PAGE_MASK))
+	if (!size || !IS_ALIGNED(size, SZ_4K))
 		return -EINVAL;
 
-	if (va != PANTHOR_VM_KERNEL_AUTO_VA && (va & ~PAGE_MASK))
+	if (va != PANTHOR_VM_KERNEL_AUTO_VA && !IS_ALIGNED(va, SZ_4K))
 		return -EINVAL;
 
 	mutex_lock(&vm->mm_lock);
@@ -2370,7 +2370,7 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,
 	int ret;
 
 	/* Aligned on page size. */
-	if ((op->va | op->size) & ~PAGE_MASK)
+	if (!IS_ALIGNED(op->va | op->size, SZ_4K))
 		return -EINVAL;
 
 	switch (op->flags & DRM_PANTHOR_VM_BIND_OP_TYPE_MASK) {
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
author maintainer
#4 lore raw
In reply to Adrián Larumbe
Quoted 57 lines · On Fri, 11 Oct 2024 18:58:45 +0100 Adrián Larumbe…

Hi Boris,

Quoted 54 lines · Steven Price
Quoted 9 lines · Boris Brezillon

The system and GPU MMU page size might differ, which becomes a problem for FW sections that need to be mapped at explicit address since our PAGE_SIZE alignment might cover a VA range that's expected to be used for another section.

Make sure we never map more than we need.

Fixes 2718d91816ee ("drm/panthor: Add the FW logical block")
Quoted 41 lines
---
 drivers/gpu/drm/panthor/panthor_fw.c  |  3 +--
 drivers/gpu/drm/panthor/panthor_gem.c | 11 ++++++++---
 drivers/gpu/drm/panthor/panthor_mmu.c |  6 +++---
 3 files changed, 12 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +1 -2
index ef232c0c2049..293846400296 100644
@@ -515,8 +515,7 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,
 		return -EINVAL;
 	}
 
-	if ((hdr.va.start & ~PAGE_MASK) != 0 ||
-	    (hdr.va.end & ~PAGE_MASK) != 0) {
+	if (!IS_ALIGNED(hdr.va.start, SZ_4K) || !IS_ALIGNED(hdr.va.end, SZ_4K)) {
 		drm_err(&ptdev->base, "Firmware corrupted, virtual addresses not page aligned: 0x%x-0x%x\n",
 			hdr.va.start, hdr.va.end);
 		return -EINVAL;
drivers/gpu/drm/panthor/panthor_gem.c +1 -3
index c60b599665d8..2c8d6e2c7232 100644
@@ -44,8 +44,7 @@ void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo)
 			to_panthor_bo(bo->obj)->exclusive_vm_root_gem != panthor_vm_root_gem(vm)))
 		goto out_free_bo;
 
-	ret = panthor_vm_unmap_range(vm, bo->va_node.start,
-				     panthor_kernel_bo_size(bo));
+	ret = panthor_vm_unmap_range(vm, bo->va_node.start, bo->va_node.size);
 	if (ret)
 		goto out_free_bo;
 
@@ -95,10 +94,16 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
 	}
 	bo = to_panthor_bo(&obj->base);
-	size = obj->base.size;
 	kbo->obj = &obj->base;
 	bo->flags = bo_flags;
 
Quoted 10 lines
Quoted 6 lines

drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 105

Quoted 6 lines
drivers/gpu/drm/panthor/panthor_gem.c +6 -0
+	/* The system and GPU MMU page size might differ, which becomes a
+	 * problem for FW sections that need to be mapped at explicit address
+	 * since our PAGE_SIZE alignment might cover a VA range that's
+	 * expected to be used for another section.
+	 * Make sure we never map more than we need.
+	 */  

I was wondering, this must be a relatively common situation for other DRM drivers, so maybe the DRM shmem core should have a BO creation function that lets you specify an exact page alignment?

So, there's a difference between the MMU page granularity and the system page size. The BO allocation granularity will always be the system page size, because gem_shmem can't allocate partial pages.

Quoted 8 lines
Quoted 2 lines

drivers/gpu/drm/panthor/panthor_gem.c panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, · line 106

Quoted 2 lines
drivers/gpu/drm/panthor/panthor_gem.c +1 -0
+	size = ALIGN(size, SZ_4K);  

I was thinking, if we know that the FW's page size is always 4KiB, maybe we could do

#define CSF_FW_PAGESIZE SZ_4K

somewhere in panthor_fw.h to make things clearer?

That I can do. If we want it to be fully dynamic, we can even make it a panthor_mmu_get_page_size() helper which queries the pgsize_bitmap of the io_pgtable to get the minimum granularity.