From patchwork Tue Apr 30 11:28:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Brezillon X-Patchwork-Id: 13648788 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B942CC4345F for ; Tue, 30 Apr 2024 11:29:01 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4768410E846; Tue, 30 Apr 2024 11:28:58 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.b="qQNRuJS/"; dkim-atps=neutral Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) by gabe.freedesktop.org (Postfix) with ESMTPS id 64FD910E846 for ; Tue, 30 Apr 2024 11:28:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1714476535; bh=2BBN0FjOpkGJdj50e4sPnXZ5GDuaIEz7hTNIiBr4oPA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qQNRuJS/cKlQAD+Ifi+7/H0tocfCHec9nqECS9/1twGcoNkHTjbh2tOaKGxn8Fvmy oyu0qbJsp/4/ZJvm/mZ+RdckuYoYIvSMFCJdzz+Lh8R2kFaDFFahi9dqf5fkLA2pOg IXL3wfdgY+90suvK20KNDAjekgJ77TPI6x49T9y4jzsBp2RSAxWUNEqfZDKE0CTpm5 E8grTdESqNbkb0QbsXb6GCEkxrzVxXNI317RuvEJwdkexYSz2iswJ5W74Sm5gtF1EC WedKcEQd9XzKZGgpueO3oUSJblrp6jZg8JSuis+oow4vEU9SpQ+V0TE2oMgmq11mEI 1clVMjSXon8lw== Received: from localhost.localdomain (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bbrezillon) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 8B0EF3781F9A; Tue, 30 Apr 2024 11:28:54 +0000 (UTC) From: Boris Brezillon To: Boris Brezillon , Steven Price , Liviu Dudau , =?utf-8?q?Adri=C3=A1n_Larumbe?= Cc: dri-devel@lists.freedesktop.org, Antonino Maniscalco , kernel@collabora.com Subject: [PATCH v2 1/4] drm/panthor: Fix tiler OOM handling to allow incremental rendering Date: Tue, 30 Apr 2024 13:28:49 +0200 Message-ID: <20240430112852.486424-2-boris.brezillon@collabora.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240430112852.486424-1-boris.brezillon@collabora.com> References: <20240430112852.486424-1-boris.brezillon@collabora.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Antonino Maniscalco If the kernel couldn't allocate memory because we reached the maximum number of chunks but no render passes are in flight (panthor_heap_grow() returning -ENOMEM), we should defer the OOM handling to the FW by returning a NULL chunk. The FW will then call the tiler OOM exception handler, which is supposed to implement incremental rendering (execute an intermediate fragment job to flush the pending primitives, release the tiler memory that was used to store those primitives, and start over from where it stopped). Instead of checking for both ENOMEM and EBUSY, make panthor_heap_grow() return ENOMEM no matter the reason of this allocation failure, the FW doesn't care anyway. v2: - Make panthor_heap_grow() return -ENOMEM for all kind of allocation failures - Document the panthor_heap_grow() semantics Fixes: de8548813824 ("drm/panthor: Add the scheduler logical block") Signed-off-by: Antonino Maniscalco Signed-off-by: Boris Brezillon Reviewed-by: Liviu Dudau Reviewed-by: Steven Price --- drivers/gpu/drm/panthor/panthor_heap.c | 12 ++++++++---- drivers/gpu/drm/panthor/panthor_sched.c | 7 ++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/panthor/panthor_heap.c b/drivers/gpu/drm/panthor/panthor_heap.c index 143fa35f2e74..c3c0ba744937 100644 --- a/drivers/gpu/drm/panthor/panthor_heap.c +++ b/drivers/gpu/drm/panthor/panthor_heap.c @@ -410,6 +410,13 @@ int panthor_heap_return_chunk(struct panthor_heap_pool *pool, * @renderpasses_in_flight: Number of render passes currently in-flight. * @pending_frag_count: Number of fragment jobs waiting for execution/completion. * @new_chunk_gpu_va: Pointer used to return the chunk VA. + * + * Return: + * - 0 if a new heap was allocated + * - -ENOMEM if the tiler context reached the maximum number of chunks + * or if too many render passes are in-flight + * or if the allocation failed + * - -EINVAL if any of the arguments passed to panthor_heap_grow() is invalid */ int panthor_heap_grow(struct panthor_heap_pool *pool, u64 heap_gpu_va, @@ -439,10 +446,7 @@ int panthor_heap_grow(struct panthor_heap_pool *pool, * handler provided by the userspace driver, if any). */ if (renderpasses_in_flight > heap->target_in_flight || - (pending_frag_count > 0 && heap->chunk_count >= heap->max_chunks)) { - ret = -EBUSY; - goto out_unlock; - } else if (heap->chunk_count >= heap->max_chunks) { + heap->chunk_count >= heap->max_chunks) { ret = -ENOMEM; goto out_unlock; } diff --git a/drivers/gpu/drm/panthor/panthor_sched.c b/drivers/gpu/drm/panthor/panthor_sched.c index b3a51a6de523..fd928362d45e 100644 --- a/drivers/gpu/drm/panthor/panthor_sched.c +++ b/drivers/gpu/drm/panthor/panthor_sched.c @@ -1354,7 +1354,12 @@ static int group_process_tiler_oom(struct panthor_group *group, u32 cs_id) pending_frag_count, &new_chunk_va); } - if (ret && ret != -EBUSY) { + /* If the heap context doesn't have memory for us, we want to let the + * FW try to reclaim memory by waiting for fragment jobs to land or by + * executing the tiler OOM exception handler, which is supposed to + * implement incremental rendering. + */ + if (ret && ret != -ENOMEM) { drm_warn(&ptdev->base, "Failed to extend the tiler heap\n"); group->fatal_queues |= BIT(cs_id); sched_queue_delayed_work(sched, tick, 0); From patchwork Tue Apr 30 11:28:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Brezillon X-Patchwork-Id: 13648789 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3137CC4345F for ; Tue, 30 Apr 2024 11:29:04 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4CF2210E9C7; Tue, 30 Apr 2024 11:28:58 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.b="gmucLLKP"; dkim-atps=neutral Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) by gabe.freedesktop.org (Postfix) with ESMTPS id F121510E353 for ; Tue, 30 Apr 2024 11:28:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1714476535; bh=iLnB0Ladq3bH0pE7VAWidZZhvtzbEl+ZBZ9XjPG75X8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gmucLLKPmJ2PkhylUf/xtYauW6jUVZrb/FFYj86NOxNPOgSsgTvh38vhRZ4OjgErN gDJ2A9TVDvwiirr9Bqau0uZ2UpTkxq1f6t2iYmWQxmAS6XLPS5dZwLN9HWtR1loF24 sT0sL08RLuKKSglDvoV7v6wgeflpyQdGCQ7tG7jQp48zAn7sm09NcApGMvA+iJk9mk oYGUxqMfqyhY78baBqnpUpxdv0GBr7hyNsYXxuFXqMXG3sybhbqGnuxUscpl945vCa nYHu4D9+XF7Y51ZiUN2SE4momRLk5ijkQp7NuA7cZ/2i5ZN/taLb/YndBHPMTTK0xN wohMzgjiU7OzA== Received: from localhost.localdomain (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bbrezillon) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 2D969378143B; Tue, 30 Apr 2024 11:28:55 +0000 (UTC) From: Boris Brezillon To: Boris Brezillon , Steven Price , Liviu Dudau , =?utf-8?q?Adri=C3=A1n_Larumbe?= Cc: dri-devel@lists.freedesktop.org, kernel@collabora.com Subject: [PATCH v2 2/4] drm/panthor: Make sure the tiler initial/max chunks are consistent Date: Tue, 30 Apr 2024 13:28:50 +0200 Message-ID: <20240430112852.486424-3-boris.brezillon@collabora.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240430112852.486424-1-boris.brezillon@collabora.com> References: <20240430112852.486424-1-boris.brezillon@collabora.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" It doesn't make sense to have a maximum number of chunks smaller than the initial number of chunks attached to the context. Fix the uAPI header to reflect the new constraint, and mention the undocumented "initial_chunk_count > 0" constraint while at it. v2: - Fix the check Fixes: 9cca48fa4f89 ("drm/panthor: Add the heap logical block") Signed-off-by: Boris Brezillon Reviewed-by: Steven Price --- drivers/gpu/drm/panthor/panthor_heap.c | 3 +++ include/uapi/drm/panthor_drm.h | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/panthor/panthor_heap.c b/drivers/gpu/drm/panthor/panthor_heap.c index c3c0ba744937..3be86ec383d6 100644 --- a/drivers/gpu/drm/panthor/panthor_heap.c +++ b/drivers/gpu/drm/panthor/panthor_heap.c @@ -281,6 +281,9 @@ int panthor_heap_create(struct panthor_heap_pool *pool, if (initial_chunk_count == 0) return -EINVAL; + if (initial_chunk_count > max_chunks) + return -EINVAL; + if (hweight32(chunk_size) != 1 || chunk_size < SZ_256K || chunk_size > SZ_2M) return -EINVAL; diff --git a/include/uapi/drm/panthor_drm.h b/include/uapi/drm/panthor_drm.h index dadb05ab1235..5db80a0682d5 100644 --- a/include/uapi/drm/panthor_drm.h +++ b/include/uapi/drm/panthor_drm.h @@ -895,13 +895,17 @@ struct drm_panthor_tiler_heap_create { /** @vm_id: VM ID the tiler heap should be mapped to */ __u32 vm_id; - /** @initial_chunk_count: Initial number of chunks to allocate. */ + /** @initial_chunk_count: Initial number of chunks to allocate. Must be at least one. */ __u32 initial_chunk_count; /** @chunk_size: Chunk size. Must be a power of two at least 256KB large. */ __u32 chunk_size; - /** @max_chunks: Maximum number of chunks that can be allocated. */ + /** + * @max_chunks: Maximum number of chunks that can be allocated. + * + * Must be at least @initial_chunk_count. + */ __u32 max_chunks; /** From patchwork Tue Apr 30 11:28:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Brezillon X-Patchwork-Id: 13648791 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 10E23C19F53 for ; Tue, 30 Apr 2024 11:29:11 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2C96110E9CB; Tue, 30 Apr 2024 11:29:10 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.b="jQtQ1b3y"; dkim-atps=neutral Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) by gabe.freedesktop.org (Postfix) with ESMTPS id ABC9010E353 for ; Tue, 30 Apr 2024 11:28:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1714476536; bh=vriAHE0ifENhiAgPB/UblO3j8EK8STP2YX7MCZDoXgc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jQtQ1b3ynuxs1pHFUiyJ5PSwBdWt8MHShhzTjp4sYyhiSpWuWu826epSPIFjm+X18 EicfpXlkX0rv98z7xDn1LXrFabiF+iqMleZ6i05blwq1nDvTKo7wBr+2ZXJ6xblmBM Ya8CUifl1rKt0oRXIA8GE4H0CusqkQy3maLxN7jPbyfvNxWEQzqGHkfqNF1HIi9tZM EwqZS3PMd7sILepqnZo2yUwbkvmrAMNRUJLUNuZMccsix+ciNfznGvoXhuhFRNvtB2 Q2k5a1eqtenaqxHbzdpEoGFYh5wwhFQHn+5hyc26djrbgA1I1iKUjr4BNIuhBLqYbj fKWvuyCukscnA== Received: from localhost.localdomain (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bbrezillon) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 0ABB2378205B; Tue, 30 Apr 2024 11:28:55 +0000 (UTC) From: Boris Brezillon To: Boris Brezillon , Steven Price , Liviu Dudau , =?utf-8?q?Adri=C3=A1n_Larumbe?= Cc: dri-devel@lists.freedesktop.org, kernel@collabora.com Subject: [PATCH v2 3/4] drm/panthor: Relax the constraints on the tiler chunk size Date: Tue, 30 Apr 2024 13:28:51 +0200 Message-ID: <20240430112852.486424-4-boris.brezillon@collabora.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240430112852.486424-1-boris.brezillon@collabora.com> References: <20240430112852.486424-1-boris.brezillon@collabora.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The field used to store the chunk size if 12 bits wide, and the encoding is chunk_size = chunk_header.chunk_size << 12, which gives us a theoretical [4k:8M] range. This range is further limited by implementation constraints, and all known implementations seem to impose a [128k:8M] range, so do the same here. We also relax the power-of-two constraint, which doesn't seem to exist on v10. This will allow userspace to fine-tune initial/max tiler memory on memory-constrained devices. v2: - Turn the power-of-two constraint into a page-aligned constraint to allow fine-tune of the initial/max heap memory size - Fix the panthor_heap_create() kerneldoc Fixes: 9cca48fa4f89 ("drm/panthor: Add the heap logical block") Signed-off-by: Boris Brezillon Reviewed-by: Liviu Dudau Reviewed-by: Steven Price --- drivers/gpu/drm/panthor/panthor_heap.c | 8 ++++---- include/uapi/drm/panthor_drm.h | 6 +++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/panthor/panthor_heap.c b/drivers/gpu/drm/panthor/panthor_heap.c index 3be86ec383d6..683bb94761bc 100644 --- a/drivers/gpu/drm/panthor/panthor_heap.c +++ b/drivers/gpu/drm/panthor/panthor_heap.c @@ -253,8 +253,8 @@ int panthor_heap_destroy(struct panthor_heap_pool *pool, u32 handle) * @pool: Pool to instantiate the heap context from. * @initial_chunk_count: Number of chunk allocated at initialization time. * Must be at least 1. - * @chunk_size: The size of each chunk. Must be a power of two between 256k - * and 2M. + * @chunk_size: The size of each chunk. Must be page-aligned and lie in the + * [128k:2M] range. * @max_chunks: Maximum number of chunks that can be allocated. * @target_in_flight: Maximum number of in-flight render passes. * @heap_ctx_gpu_va: Pointer holding the GPU address of the allocated heap @@ -284,8 +284,8 @@ int panthor_heap_create(struct panthor_heap_pool *pool, if (initial_chunk_count > max_chunks) return -EINVAL; - if (hweight32(chunk_size) != 1 || - chunk_size < SZ_256K || chunk_size > SZ_2M) + if (!IS_ALIGNED(chunk_size, PAGE_SIZE) || + chunk_size < SZ_128K || chunk_size > SZ_8M) return -EINVAL; down_read(&pool->lock); diff --git a/include/uapi/drm/panthor_drm.h b/include/uapi/drm/panthor_drm.h index 5db80a0682d5..b8220d2e698f 100644 --- a/include/uapi/drm/panthor_drm.h +++ b/include/uapi/drm/panthor_drm.h @@ -898,7 +898,11 @@ struct drm_panthor_tiler_heap_create { /** @initial_chunk_count: Initial number of chunks to allocate. Must be at least one. */ __u32 initial_chunk_count; - /** @chunk_size: Chunk size. Must be a power of two at least 256KB large. */ + /** + * @chunk_size: Chunk size. + * + * Must be page-aligned and lie in the [128k:8M] range. + */ __u32 chunk_size; /** From patchwork Tue Apr 30 11:28:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Brezillon X-Patchwork-Id: 13648790 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4566FC4345F for ; Tue, 30 Apr 2024 11:29:10 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9FD4E10E353; Tue, 30 Apr 2024 11:29:09 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.b="n0wISxzU"; dkim-atps=neutral Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4635D10E353 for ; Tue, 30 Apr 2024 11:28:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1714476537; bh=g3JsBisKyUaCAC7iPEVJvY3tE1oCEy1ivGeqwezys/E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n0wISxzUKEe69VXDnRAwiaCMatcyFZtZ7Av8SjO2emUaiYmLakZbOJOWqhQfoVl4y UQh84TSfMbg/GrQXLahu/NxLP5CXgDodo5/88a/UCZJpzy+Ixut8oUOTfImeGdCM2T TWF7VvXuq4BBVj0AsttyNxud5+mIE1ja8RV1/sB74MGKHRBcN2eCRuZmR03JhFdieW FQdNK3/4PmveUdELHxNQRUZYboeWBv2R436Ppt5SGIvITFwpWioOKOx7bedSEVdtW6 IIOxGaJcl5A9XtsXXUJsF5TT+eYBUq6ibfortUteqsDon0+GHz/f0gaKlykanJXhpi uq94BbnKdgmCg== Received: from localhost.localdomain (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bbrezillon) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 9FECC37813C4; Tue, 30 Apr 2024 11:28:56 +0000 (UTC) From: Boris Brezillon To: Boris Brezillon , Steven Price , Liviu Dudau , =?utf-8?q?Adri=C3=A1n_Larumbe?= Cc: dri-devel@lists.freedesktop.org, kernel@collabora.com, Eric Smith Subject: [PATCH v2 4/4] drm/panthor: Fix an off-by-one in the heap context retrieval logic Date: Tue, 30 Apr 2024 13:28:52 +0200 Message-ID: <20240430112852.486424-5-boris.brezillon@collabora.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240430112852.486424-1-boris.brezillon@collabora.com> References: <20240430112852.486424-1-boris.brezillon@collabora.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" ID 0 is reserved to encode 'no-tiler-heap', the heap ID range is [1:MAX_HEAPS_PER_POOL], which we occasionally need to turn into an index in the [0:MAX_HEAPS_PER_POOL-1] when we want to access the context object. v2: - New patch Fixes: 9cca48fa4f89 ("drm/panthor: Add the heap logical block") Reported-by: Eric Smith Signed-off-by: Boris Brezillon Tested-by: Eric Smith --- drivers/gpu/drm/panthor/panthor_heap.c | 35 +++++++++++++++++++------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/panthor/panthor_heap.c b/drivers/gpu/drm/panthor/panthor_heap.c index 683bb94761bc..b1a7dbf25fb2 100644 --- a/drivers/gpu/drm/panthor/panthor_heap.c +++ b/drivers/gpu/drm/panthor/panthor_heap.c @@ -109,7 +109,11 @@ static int panthor_heap_ctx_stride(struct panthor_device *ptdev) static int panthor_get_heap_ctx_offset(struct panthor_heap_pool *pool, int id) { - return panthor_heap_ctx_stride(pool->ptdev) * id; + /* ID 0 is reserved to encode 'no-tiler-heap', the valid range + * is [1:MAX_HEAPS_PER_POOL], which we need to turn into a + * [0:MAX_HEAPS_PER_POOL-1] context index, hence the minus one here. + */ + return panthor_heap_ctx_stride(pool->ptdev) * (id - 1); } static void *panthor_get_heap_ctx(struct panthor_heap_pool *pool, int id) @@ -118,6 +122,21 @@ static void *panthor_get_heap_ctx(struct panthor_heap_pool *pool, int id) panthor_get_heap_ctx_offset(pool, id); } +static int panthor_get_heap_ctx_id(struct panthor_heap_pool *pool, + u64 heap_ctx_gpu_va) +{ + u64 offset = heap_ctx_gpu_va - panthor_kernel_bo_gpuva(pool->gpu_contexts); + u32 heap_idx = (u32)offset / panthor_heap_ctx_stride(pool->ptdev); + + if (offset > U32_MAX || heap_idx >= MAX_HEAPS_PER_POOL) + return -EINVAL; + + /* ID 0 is reserved to encode 'no-tiler-heap', the valid range + * is [1:MAX_HEAPS_PER_POOL], hence the plus one here. + */ + return heap_idx + 1; +} + static void panthor_free_heap_chunk(struct panthor_vm *vm, struct panthor_heap *heap, struct panthor_heap_chunk *chunk) @@ -364,14 +383,13 @@ int panthor_heap_return_chunk(struct panthor_heap_pool *pool, u64 heap_gpu_va, u64 chunk_gpu_va) { - u64 offset = heap_gpu_va - panthor_kernel_bo_gpuva(pool->gpu_contexts); - u32 heap_id = (u32)offset / panthor_heap_ctx_stride(pool->ptdev); + int heap_id = panthor_get_heap_ctx_id(pool, heap_gpu_va); struct panthor_heap_chunk *chunk, *tmp, *removed = NULL; struct panthor_heap *heap; int ret; - if (offset > U32_MAX || heap_id >= MAX_HEAPS_PER_POOL) - return -EINVAL; + if (heap_id < 0) + return heap_id; down_read(&pool->lock); heap = xa_load(&pool->xa, heap_id); @@ -427,14 +445,13 @@ int panthor_heap_grow(struct panthor_heap_pool *pool, u32 pending_frag_count, u64 *new_chunk_gpu_va) { - u64 offset = heap_gpu_va - panthor_kernel_bo_gpuva(pool->gpu_contexts); - u32 heap_id = (u32)offset / panthor_heap_ctx_stride(pool->ptdev); + int heap_id = panthor_get_heap_ctx_id(pool, heap_gpu_va); struct panthor_heap_chunk *chunk; struct panthor_heap *heap; int ret; - if (offset > U32_MAX || heap_id >= MAX_HEAPS_PER_POOL) - return -EINVAL; + if (heap_id < 0) + return heap_id; down_read(&pool->lock); heap = xa_load(&pool->xa, heap_id);