From nobody Sun Dec 14 05:53:41 2025 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0517F2DEA68 for ; Sat, 13 Dec 2025 19:09:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765652955; cv=pass; b=cuKAjqB1/L9UM9z4hgOzgaF/Bk4JyJ6Wblibb2EwLtnCDvPOdTzC8WSKV3Pb74zn7BjOQl3jxPayWE7MK/PPnD02fc0L1LL39njAzJF2Bpunp+uVtAMO+N8QXeLQSt1g2dNqEmpkRQ/dA0WoTL6wG3jsrJT6YyeUFjywAluPYnY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765652955; c=relaxed/simple; bh=IOazdogK+ws+XNmdWj2kDHs6x/jUuDjXMWTgZzFOyis=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ui2sw1giRlIkzJJW/ywZRLzL5r+xabdIlgmvDZD2zYQdG4D+D1yJvid5BZsCyu3kg59PxUBlw+7o2vg5Y2M+Sp0Mh4dsvAepgMRWlWntlW0HqGLKKfZy2Pmw9j7dU+Z03uBmEA5tvHZvDcwvByLFRFGYe4ry0ekb0lTc2qHrDY0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=adrian.larumbe@collabora.com header.b=PyZAH2ks; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=adrian.larumbe@collabora.com header.b="PyZAH2ks" ARC-Seal: i=1; a=rsa-sha256; t=1765652937; cv=none; d=zohomail.com; s=zohoarc; b=akx4heNj/tJ6xJD/D9eKx31D4o5LPWfhcwwKtU6zWa3WQ7vNl05xY9LfgykpwQjOktvslDNJUNs3ZMm4GUDTqmSX2c7C/Oy7q87mQZMy4ld/kBSjdJXlWreFgm5+jbyXaJBWuIrs7Y1OX3SOkVklQxf0wX+bItuEpPfv8HQHkrk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1765652937; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=qvsf70Oo8X+gG3m4aGKePHSJuRxWkWqNmnZYvw6cWk8=; b=VgCjSfPhBVXNwMspbmo3/5otHtcQY2fCdFFKepJI7tA0ZGd+u6P1cgZZRcjm7UT9uAy2AB66BvhtTJ8lWv06puyaG+ydh9EIfk75JNeCXUJORb41AzjJfDnGJhwUdqIisxUvRKFGTrqBemvvsJA8NXUrTx0gX0u1RgvKKpWYXnQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=adrian.larumbe@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1765652937; s=zohomail; d=collabora.com; i=adrian.larumbe@collabora.com; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To; bh=qvsf70Oo8X+gG3m4aGKePHSJuRxWkWqNmnZYvw6cWk8=; b=PyZAH2ksqk53XRwWFQk7PZcwrToQZ4Xst9lnbsyn2TaA7GaMqDrf0zJin3LAC08Z TfF52gMeD4VbXV9iw6/MMhla4LwVo1oU3IZMSvjFQx2CUy2fxcLlIhLcCEfsVvrFjpf LleHjsqjMbKa83i0248326ztE5cVSHfINYXb1XGI= Received: by mx.zohomail.com with SMTPS id 176565293632532.665479372826894; Sat, 13 Dec 2025 11:08:56 -0800 (PST) From: =?UTF-8?q?Adri=C3=A1n=20Larumbe?= To: linux-kernel@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, Steven Price , Boris Brezillon , kernel@collabora.com, =?UTF-8?q?Adri=C3=A1n=20Larumbe?= , Liviu Dudau , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter Subject: [PATCH v3 1/1] drm/panthor: Support partial unmaps of huge pages Date: Sat, 13 Dec 2025 19:08:33 +0000 Message-ID: <20251213190835.2444075-2-adrian.larumbe@collabora.com> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20251213190835.2444075-1-adrian.larumbe@collabora.com> References: <20251213190835.2444075-1-adrian.larumbe@collabora.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Commit 33729a5fc0ca ("iommu/io-pgtable-arm: Remove split on unmap behavior") did away with the treatment of partial unmaps of huge IOPTEs. In the case of Panthor, that means an attempt to run a VM_BIND unmap operation on a memory region whose start address and size aren't 2MiB aligned, in the event it intersects with a huge page, would lead to ARM IOMMU management code to fail and a warning being raised. Presently, and for lack of a better alternative, it's best to have Panthor handle partial unmaps at the driver level, by unmapping entire huge pages and remapping the difference between them and the requested unmap region. This could change in the future when the VM_BIND uAPI is expanded to enforce huge page alignment and map/unmap operational constraints that render this code unnecessary. Signed-off-by: Adri=C3=A1n Larumbe --- drivers/gpu/drm/panthor/panthor_mmu.c | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/drivers/gpu/drm/panthor/panthor_mmu.c b/drivers/gpu/drm/pantho= r/panthor_mmu.c index 183da30fa500..f11340a7f59e 100644 --- a/drivers/gpu/drm/panthor/panthor_mmu.c +++ b/drivers/gpu/drm/panthor/panthor_mmu.c @@ -2110,6 +2110,44 @@ static int panthor_gpuva_sm_step_map(struct drm_gpuv= a_op *op, void *priv) return 0; } =20 +static bool +iova_mapped_as_huge_page(const struct panthor_vma *vma, u64 addr) +{ + const struct page *pg; + pgoff_t bo_offset; + + bo_offset =3D addr - vma->base.va.addr + vma->base.gem.offset; + pg =3D to_panthor_bo(vma->base.gem.obj)->base.pages[bo_offset >> PAGE_SHI= FT]; + + return (folio_size(page_folio(pg)) >=3D SZ_2M); +} + +static void +get_map_unmap_intervals(const struct drm_gpuva_op_remap *op, + const struct panthor_vma *unmap_vma, + u64 *unmap_start, u64 *unmap_range) +{ + u64 aligned_unmap_start, aligned_unmap_end, unmap_end; + + drm_gpuva_op_remap_to_unmap_range(op, unmap_start, unmap_range); + unmap_end =3D *unmap_start + *unmap_range; + + aligned_unmap_start =3D ALIGN_DOWN(*unmap_start, SZ_2M); + if (op->prev && aligned_unmap_start < *unmap_start && + op->prev->va.addr <=3D aligned_unmap_start && + iova_mapped_as_huge_page(unmap_vma, *unmap_start)) { + *unmap_range +=3D *unmap_start - aligned_unmap_start; + *unmap_start =3D aligned_unmap_start; + } + + aligned_unmap_end =3D ALIGN(unmap_end, SZ_2M); + if (op->next && aligned_unmap_end > unmap_end && + op->next->va.addr + op->next->va.range >=3D aligned_unmap_end && + iova_mapped_as_huge_page(unmap_vma, unmap_end - 1)) { + *unmap_range +=3D aligned_unmap_end - unmap_end; + } +} + static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op, void *priv) { @@ -2121,16 +2159,44 @@ static int panthor_gpuva_sm_step_remap(struct drm_g= puva_op *op, int ret; =20 drm_gpuva_op_remap_to_unmap_range(&op->remap, &unmap_start, &unmap_range); + + /* + * ARM IOMMU page table management code disallows partial unmaps of huge = pages, + * so when a partial unmap is requested, we must first unmap the entire h= uge + * page and then remap the difference between the huge page minus the req= uested + * unmap region. Calculating the right offsets and ranges for the differe= nt unmap + * and map operations is the responsibility of the following function. + */ + get_map_unmap_intervals(&op->remap, unmap_vma, &unmap_start, &unmap_range= ); + ret =3D panthor_vm_unmap_pages(vm, unmap_start, unmap_range); if (ret) return ret; =20 if (op->remap.prev) { + ret =3D panthor_vm_map_pages(vm, unmap_start, + flags_to_prot(unmap_vma->flags), + to_drm_gem_shmem_obj(op->remap.prev->gem.obj)->sgt, + op->remap.prev->gem.offset + + (unmap_start - op->remap.prev->va.addr), + op->remap.prev->va.addr + op->remap.prev->va.range - + unmap_start); + if (ret) + return ret; + prev_vma =3D panthor_vm_op_ctx_get_vma(op_ctx); panthor_vma_init(prev_vma, unmap_vma->flags); } =20 if (op->remap.next) { + ret =3D panthor_vm_map_pages(vm, op->remap.next->va.addr, + flags_to_prot(unmap_vma->flags), + to_drm_gem_shmem_obj(op->remap.next->gem.obj)->sgt, + op->remap.next->gem.offset, + unmap_start + unmap_range - op->remap.next->va.addr); + if (ret) + return ret; + next_vma =3D panthor_vm_op_ctx_get_vma(op_ctx); panthor_vma_init(next_vma, unmap_vma->flags); } --=20 2.51.2