From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01507C64ED8 for ; Fri, 17 Feb 2023 13:45:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229669AbjBQNpn (ORCPT ); Fri, 17 Feb 2023 08:45:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229652AbjBQNpY (ORCPT ); Fri, 17 Feb 2023 08:45:24 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D00CC1114A for ; Fri, 17 Feb 2023 05:44:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641476; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B0IjfZRZB9FdjtHsb79gae4VEMZHyq9PsFfSTQw35tA=; b=gJRTrdPHCOfj66vMD/QXKaJ50Ab2T4k6VP1KUedAEuysrzkwbGI18e+KJtMCGcdpa86lBF hZHMKBbnRtyn7nbZLSiozc7yc5uStpxJgtAM/v/7JK1zj/QuGHXCJucKvFxlHi5eV6s/lw ZhQrn1VODBqJm9fB7BnIequ9o8YilaE= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-460-PHVNJNoxOau90cifGGKKxg-1; Fri, 17 Feb 2023 08:44:34 -0500 X-MC-Unique: PHVNJNoxOau90cifGGKKxg-1 Received: by mail-ed1-f70.google.com with SMTP id j10-20020a05640211ca00b004acbf564d75so1081814edw.5 for ; Fri, 17 Feb 2023 05:44:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=B0IjfZRZB9FdjtHsb79gae4VEMZHyq9PsFfSTQw35tA=; b=uWehPcR7a/qjn1U6a3Pwh3Yw1eq0mktYId17lAJQNOLauHf8CzdtMJB9vB91onMrpn 7WOvXb9WjNw+/voOpq+v4wrDD+/l8DPXcX78jig3V4d9gXg87RS96ppoe1asXp3XjXJF CXK9YdRkjByNrRSaygWdYK/DwAIgx7BbvBu7KmYEOAVH02cYy/0ZM1nZJ4gZs4q4tNyp RJqipBUI7Q7i03mRX2zkuIvag9HDGGt7TDcTyyMWEb3GAXUSU1UC8lorGWbWLAow+w+N EJQJ2h7Gv4kMuDwZ1n5Yd7KVxlyDJ7DRAz7Hj7fzPEyB+Sb93fsAuSu+9WHzS/rUgrBM brnw== X-Gm-Message-State: AO0yUKV1fX7UYy559c5z9czgtSG8/8FynHyA+sqnnjVKMr00aGHEqb33 HkqNBqVYw6lFmnn8Y8DaOi09knePCUGns0RUtI+g8adiajejXiZs1dl+j/hhkkh8kX7Jtb6sYbO ymoxcegp44CNs0AgNaQ3IWxqN X-Received: by 2002:a05:6402:410:b0:4ab:4949:32e9 with SMTP id q16-20020a056402041000b004ab494932e9mr984190edv.18.1676641473580; Fri, 17 Feb 2023 05:44:33 -0800 (PST) X-Google-Smtp-Source: AK7set/4N76GkzJnpJcleEpK2JhSWu+e9Z1uDn6VZOI21cMjRppL2k5UR2eDffFSzxOUmRo01p4xQA== X-Received: by 2002:a05:6402:410:b0:4ab:4949:32e9 with SMTP id q16-20020a056402041000b004ab494932e9mr984164edv.18.1676641473202; Fri, 17 Feb 2023 05:44:33 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id z61-20020a509e43000000b004ad03b18ae3sm2300111ede.62.2023.02.17.05.44.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:44:32 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH drm-next v2 01/16] drm: execution context for GEM buffers Date: Fri, 17 Feb 2023 14:44:07 +0100 Message-Id: <20230217134422.14116-2-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Christian K=C3=B6nig This adds the infrastructure for an execution context for GEM buffers which is similar to the existinc TTMs execbuf util and intended to replace it in the long term. The basic functionality is that we abstracts the necessary loop to lock many different GEM buffers with automated deadlock and duplicate handling. v2: drop xarray and use dynamic resized array instead, the locking overhead is unecessary and measureable. Signed-off-by: Christian K=C3=B6nig --- Documentation/gpu/drm-mm.rst | 12 ++ drivers/gpu/drm/Kconfig | 6 + drivers/gpu/drm/Makefile | 2 + drivers/gpu/drm/amd/amdgpu/Kconfig | 1 + drivers/gpu/drm/drm_exec.c | 295 +++++++++++++++++++++++++++++ include/drm/drm_exec.h | 144 ++++++++++++++ 6 files changed, 460 insertions(+) create mode 100644 drivers/gpu/drm/drm_exec.c create mode 100644 include/drm/drm_exec.h diff --git a/Documentation/gpu/drm-mm.rst b/Documentation/gpu/drm-mm.rst index a79fd3549ff8..a52e6f4117d6 100644 --- a/Documentation/gpu/drm-mm.rst +++ b/Documentation/gpu/drm-mm.rst @@ -493,6 +493,18 @@ DRM Sync Objects .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c :export: =20 +DRM Execution context +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +.. kernel-doc:: drivers/gpu/drm/drm_exec.c + :doc: Overview + +.. kernel-doc:: include/drm/drm_exec.h + :internal: + +.. kernel-doc:: drivers/gpu/drm/drm_exec.c + :export: + GPU Scheduler =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig index f42d4c6a19f2..1573d658fbb5 100644 --- a/drivers/gpu/drm/Kconfig +++ b/drivers/gpu/drm/Kconfig @@ -200,6 +200,12 @@ config DRM_TTM GPU memory types. Will be enabled automatically if a device driver uses it. =20 +config DRM_EXEC + tristate + depends on DRM + help + Execution context for command submissions + config DRM_BUDDY tristate depends on DRM diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile index ab4460fcd63f..d40defbb0347 100644 --- a/drivers/gpu/drm/Makefile +++ b/drivers/gpu/drm/Makefile @@ -78,6 +78,8 @@ obj-$(CONFIG_DRM_PANEL_ORIENTATION_QUIRKS) +=3D drm_panel= _orientation_quirks.o # # Memory-management helpers # +# +obj-$(CONFIG_DRM_EXEC) +=3D drm_exec.o =20 obj-$(CONFIG_DRM_BUDDY) +=3D drm_buddy.o =20 diff --git a/drivers/gpu/drm/amd/amdgpu/Kconfig b/drivers/gpu/drm/amd/amdgp= u/Kconfig index 5341b6b242c3..279fb3bba810 100644 --- a/drivers/gpu/drm/amd/amdgpu/Kconfig +++ b/drivers/gpu/drm/amd/amdgpu/Kconfig @@ -11,6 +11,7 @@ config DRM_AMDGPU select DRM_SCHED select DRM_TTM select DRM_TTM_HELPER + select DRM_EXEC select POWER_SUPPLY select HWMON select I2C diff --git a/drivers/gpu/drm/drm_exec.c b/drivers/gpu/drm/drm_exec.c new file mode 100644 index 000000000000..ed2106c22786 --- /dev/null +++ b/drivers/gpu/drm/drm_exec.c @@ -0,0 +1,295 @@ +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ + +#include +#include +#include + +/** + * DOC: Overview + * + * This component mainly abstracts the retry loop necessary for locking + * multiple GEM objects while preparing hardware operations (e.g. command + * submissions, page table updates etc..). + * + * If a contention is detected while locking a GEM object the cleanup proc= edure + * unlocks all previously locked GEM objects and locks the contended one f= irst + * before locking any further objects. + * + * After an object is locked fences slots can optionally be reserved on the + * dma_resv object inside the GEM object. + * + * A typical usage pattern should look like this:: + * + * struct drm_gem_object *obj; + * struct drm_exec exec; + * unsigned long index; + * int ret; + * + * drm_exec_init(&exec, true); + * drm_exec_while_not_all_locked(&exec) { + * ret =3D drm_exec_prepare_obj(&exec, boA, 1); + * drm_exec_continue_on_contention(&exec); + * if (ret) + * goto error; + * + * ret =3D drm_exec_lock(&exec, boB, 1); + * drm_exec_continue_on_contention(&exec); + * if (ret) + * goto error; + * } + * + * drm_exec_for_each_locked_object(&exec, index, obj) { + * dma_resv_add_fence(obj->resv, fence, DMA_RESV_USAGE_READ); + * ... + * } + * drm_exec_fini(&exec); + * + * See struct dma_exec for more details. + */ + +/* Dummy value used to initially enter the retry loop */ +#define DRM_EXEC_DUMMY (void*)~0 + +/* Initialize the drm_exec_objects container */ +static void drm_exec_objects_init(struct drm_exec_objects *container) +{ + container->objects =3D kmalloc(PAGE_SIZE, GFP_KERNEL); + + /* If allocation here fails, just delay that till the first use */ + container->max_objects =3D container->objects ? + PAGE_SIZE / sizeof(void *) : 0; + container->num_objects =3D 0; +} + +/* Cleanup the drm_exec_objects container */ +static void drm_exec_objects_fini(struct drm_exec_objects *container) +{ + kvfree(container->objects); +} + +/* Make sure we have enough room and add an object the container */ +static int drm_exec_objects_add(struct drm_exec_objects *container, + struct drm_gem_object *obj) +{ + if (unlikely(container->num_objects =3D=3D container->max_objects)) { + size_t size =3D container->max_objects * sizeof(void *); + void *tmp; + + tmp =3D kvrealloc(container->objects, size, size + PAGE_SIZE, + GFP_KERNEL); + if (!tmp) + return -ENOMEM; + + container->objects =3D tmp; + container->max_objects +=3D PAGE_SIZE / sizeof(void *); + } + drm_gem_object_get(obj); + container->objects[container->num_objects++] =3D obj; + return 0; +} + +/* Unlock all objects and drop references */ +static void drm_exec_unlock_all(struct drm_exec *exec) +{ + struct drm_gem_object *obj; + unsigned long index; + + drm_exec_for_each_duplicate_object(exec, index, obj) + drm_gem_object_put(obj); + + drm_exec_for_each_locked_object(exec, index, obj) { + dma_resv_unlock(obj->resv); + drm_gem_object_put(obj); + } +} + +/** + * drm_exec_init - initialize a drm_exec object + * @exec: the drm_exec object to initialize + * @interruptible: if locks should be acquired interruptible + * + * Initialize the object and make sure that we can track locked and duplic= ate + * objects. + */ +void drm_exec_init(struct drm_exec *exec, bool interruptible) +{ + exec->interruptible =3D interruptible; + drm_exec_objects_init(&exec->locked); + drm_exec_objects_init(&exec->duplicates); + exec->contended =3D DRM_EXEC_DUMMY; +} +EXPORT_SYMBOL(drm_exec_init); + +/** + * drm_exec_fini - finalize a drm_exec object + * @exec: the drm_exec object to finilize + * + * Unlock all locked objects, drop the references to objects and free all = memory + * used for tracking the state. + */ +void drm_exec_fini(struct drm_exec *exec) +{ + drm_exec_unlock_all(exec); + drm_exec_objects_fini(&exec->locked); + drm_exec_objects_fini(&exec->duplicates); + if (exec->contended !=3D DRM_EXEC_DUMMY) { + drm_gem_object_put(exec->contended); + ww_acquire_fini(&exec->ticket); + } +} +EXPORT_SYMBOL(drm_exec_fini); + +/** + * drm_exec_cleanup - cleanup when contention is detected + * @exec: the drm_exec object to cleanup + * + * Cleanup the current state and return true if we should stay inside the = retry + * loop, false if there wasn't any contention detected and we can keep the + * objects locked. + */ +bool drm_exec_cleanup(struct drm_exec *exec) +{ + if (likely(!exec->contended)) { + ww_acquire_done(&exec->ticket); + return false; + } + + if (likely(exec->contended =3D=3D DRM_EXEC_DUMMY)) { + exec->contended =3D NULL; + ww_acquire_init(&exec->ticket, &reservation_ww_class); + return true; + } + + drm_exec_unlock_all(exec); + exec->locked.num_objects =3D 0; + exec->duplicates.num_objects =3D 0; + return true; +} +EXPORT_SYMBOL(drm_exec_cleanup); + +/* Track the locked object in the xa and reserve fences */ +static int drm_exec_obj_locked(struct drm_exec_objects *container, + struct drm_gem_object *obj, + unsigned int num_fences) +{ + int ret; + + if (container) { + ret =3D drm_exec_objects_add(container, obj); + if (ret) + return ret; + } + + if (num_fences) { + ret =3D dma_resv_reserve_fences(obj->resv, num_fences); + if (ret) + goto error_erase; + } + + return 0; + +error_erase: + if (container) { + --container->num_objects; + drm_gem_object_put(obj); + } + return ret; +} + +/* Make sure the contended object is locked first */ +static int drm_exec_lock_contended(struct drm_exec *exec) +{ + struct drm_gem_object *obj =3D exec->contended; + int ret; + + if (likely(!obj)) + return 0; + + if (exec->interruptible) { + ret =3D dma_resv_lock_slow_interruptible(obj->resv, + &exec->ticket); + if (unlikely(ret)) + goto error_dropref; + } else { + dma_resv_lock_slow(obj->resv, &exec->ticket); + } + + ret =3D drm_exec_obj_locked(&exec->locked, obj, 0); + if (unlikely(ret)) + dma_resv_unlock(obj->resv); + +error_dropref: + /* Always cleanup the contention so that error handling can kick in */ + drm_gem_object_put(obj); + exec->contended =3D NULL; + return ret; +} + +/** + * drm_exec_prepare_obj - prepare a GEM object for use + * @exec: the drm_exec object with the state + * @obj: the GEM object to prepare + * @num_fences: how many fences to reserve + * + * Prepare a GEM object for use by locking it and reserving fence slots. A= ll + * succesfully locked objects are put into the locked container. Duplicates + * detected as well and automatically moved into the duplicates container. + * + * Returns: -EDEADLK if a contention is detected, -ENOMEM when memory + * allocation failed and zero for success. + */ +int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj, + unsigned int num_fences) +{ + int ret; + + ret =3D drm_exec_lock_contended(exec); + if (unlikely(ret)) + return ret; + + if (exec->interruptible) + ret =3D dma_resv_lock_interruptible(obj->resv, &exec->ticket); + else + ret =3D dma_resv_lock(obj->resv, &exec->ticket); + + if (unlikely(ret =3D=3D -EDEADLK)) { + drm_gem_object_get(obj); + exec->contended =3D obj; + return -EDEADLK; + } + + if (unlikely(ret =3D=3D -EALREADY)) { + struct drm_exec_objects *container =3D &exec->duplicates; + + /* + * If this is the first locked GEM object it was most likely + * just contended. So don't add it to the duplicates, just + * reserve the fence slots. + */ + if (exec->locked.num_objects && exec->locked.objects[0] =3D=3D obj) + container =3D NULL; + + ret =3D drm_exec_obj_locked(container, obj, num_fences); + if (ret) + return ret; + + } else if (unlikely(ret)) { + return ret; + + } else { + ret =3D drm_exec_obj_locked(&exec->locked, obj, num_fences); + if (ret) + goto error_unlock; + } + + drm_gem_object_get(obj); + return 0; + +error_unlock: + dma_resv_unlock(obj->resv); + return ret; +} +EXPORT_SYMBOL(drm_exec_prepare_obj); + +MODULE_DESCRIPTION("DRM execution context"); +MODULE_LICENSE("Dual MIT/GPL"); diff --git a/include/drm/drm_exec.h b/include/drm/drm_exec.h new file mode 100644 index 000000000000..f73981c6292e --- /dev/null +++ b/include/drm/drm_exec.h @@ -0,0 +1,144 @@ +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ + +#ifndef __DRM_EXEC_H__ +#define __DRM_EXEC_H__ + +#include + +struct drm_gem_object; + +/** + * struct drm_exec_objects - Container for GEM objects in a drm_exec + */ +struct drm_exec_objects { + unsigned int num_objects; + unsigned int max_objects; + struct drm_gem_object **objects; +}; + +/** + * drm_exec_objects_for_each - iterate over all the objects inside the con= tainer + */ +#define drm_exec_objects_for_each(array, index, obj) \ + for (index =3D 0, obj =3D (array)->objects[0]; \ + index < (array)->num_objects; \ + ++index, obj =3D (array)->objects[index]) + +/** + * struct drm_exec - Execution context + */ +struct drm_exec { + /** + * @interruptible: If locks should be taken interruptible + */ + bool interruptible; + + /** + * @ticket: WW ticket used for acquiring locks + */ + struct ww_acquire_ctx ticket; + + /** + * @locked: container for the locked GEM objects + */ + struct drm_exec_objects locked; + + /** + * @duplicates: container for the duplicated GEM objects + */ + struct drm_exec_objects duplicates; + + /** + * @contended: contended GEM object we backet of for. + */ + struct drm_gem_object *contended; +}; + +/** + * drm_exec_for_each_locked_object - iterate over all the locked objects + * @exec: drm_exec object + * @index: unsigned long index for the iteration + * @obj: the current GEM object + * + * Iterate over all the locked GEM objects inside the drm_exec object. + */ +#define drm_exec_for_each_locked_object(exec, index, obj) \ + drm_exec_objects_for_each(&(exec)->locked, index, obj) + +/** + * drm_exec_for_each_duplicate_object - iterate over all the duplicate obj= ects + * @exec: drm_exec object + * @index: unsigned long index for the iteration + * @obj: the current GEM object + * + * Iterate over all the duplicate GEM objects inside the drm_exec object. + */ +#define drm_exec_for_each_duplicate_object(exec, index, obj) \ + drm_exec_objects_for_each(&(exec)->duplicates, index, obj) + +/** + * drm_exec_while_not_all_locked - loop until all GEM objects are prepared + * @exec: drm_exec object + * + * Core functionality of the drm_exec object. Loops until all GEM objects = are + * prepared and no more contention exists. + * + * At the beginning of the loop it is guaranteed that no GEM object is loc= ked. + */ +#define drm_exec_while_not_all_locked(exec) \ + while (drm_exec_cleanup(exec)) + +/** + * drm_exec_continue_on_contention - continue the loop when we need to cle= anup + * @exec: drm_exec object + * + * Control flow helper to continue when a contention was detected and we n= eed to + * clean up and re-start the loop to prepare all GEM objects. + */ +#define drm_exec_continue_on_contention(exec) \ + if (unlikely(drm_exec_is_contended(exec))) \ + continue + +/** + * drm_exec_break_on_contention - break a subordinal loop on contention + * @exec: drm_exec object + * + * Control flow helper to break a subordinal loop when a contention was de= tected + * and we need to clean up and re-start the loop to prepare all GEM object= s. + */ +#define drm_exec_break_on_contention(exec) \ + if (unlikely(drm_exec_is_contended(exec))) \ + break + +/** + * drm_exec_is_contended - check for contention + * @exec: drm_exec object + * + * Returns true if the drm_exec object has run into some contention while + * locking a GEM object and needs to clean up. + */ +static inline bool drm_exec_is_contended(struct drm_exec *exec) +{ + return !!exec->contended; +} + +/** + * drm_exec_has_duplicates - check for duplicated GEM object + * @exec: drm_exec object + * + * Return true if the drm_exec object has encountered some already locked = GEM + * objects while trying to lock them. This can happen if multiple GEM obje= cts + * share the same underlying resv object. + */ +static inline bool drm_exec_has_duplicates(struct drm_exec *exec) +{ + return exec->duplicates.num_objects > 0; +} + +void drm_exec_init(struct drm_exec *exec, bool interruptible); +void drm_exec_fini(struct drm_exec *exec); +bool drm_exec_cleanup(struct drm_exec *exec); +int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj, + unsigned int num_fences); + +#endif --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B342C05027 for ; Fri, 17 Feb 2023 13:46:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229854AbjBQNpv (ORCPT ); Fri, 17 Feb 2023 08:45:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229761AbjBQNp1 (ORCPT ); Fri, 17 Feb 2023 08:45:27 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 975A9126D1 for ; Fri, 17 Feb 2023 05:44:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641479; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7uKZrgGFJpURUNNqYPRQvVp3RBdWsUReZR9Qa+W+o5s=; b=BNUcM6POrJG74JL6p38XolGt+ycNnXKd0YIX+BflxiyUJ3YHnbPI8+pLq6T2z0bmii5OSu T+hYxR683U+lcnetgEsI2gROjRggvDErFRMaT0KALxjGqjvqV/BrIfQ2Ig4oNxlASf04HY xwaXCUgL9iYyPSl4ZN1CfA/4A3ITdcY= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-623-2u7igbg5Mqqh0vxl0AQhWw-1; Fri, 17 Feb 2023 08:44:38 -0500 X-MC-Unique: 2u7igbg5Mqqh0vxl0AQhWw-1 Received: by mail-ed1-f71.google.com with SMTP id cy15-20020a0564021c8f00b004aaa054d189so1571814edb.11 for ; Fri, 17 Feb 2023 05:44:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7uKZrgGFJpURUNNqYPRQvVp3RBdWsUReZR9Qa+W+o5s=; b=CVDvmwVPcdr4oWfU4whltEFaoIN1GLGs6g3l7zo6yqmH2ERzM63AmdLxTR4i9Pc7Yb JjFo225v/mC/Mp7hvD748xGo6rXcOiZSeG7sKDBHVHlMAm/vgpo/zRpsg1Rl7uM5bpKZ UGoZ35FUDlOLKXEbsPVZzBvA3w4ogPBiiyB6bf1YjJ/5Tf0tTiGrEljCsfwReQQqFjf9 ICrFjJF/AQDQ0OlQth0momkHhM1MbsD9YPcsyDG0L2lTOb/ZwjEAZCjKYsqDLvSvAyWe Dc9KBDFx1chWi5ySgGrFjgnWdPUoCV/lTC7EgofR2yOxzQbUtJLlOtP9uWMGB/FZ1m3R 3jvA== X-Gm-Message-State: AO0yUKU92qBjIUjiGs87HI+Fo/E6hWmja1Fl5i+/483HjbW2yAJMZHcr 39pAxzLEIDvJWJKMeFD2HxIhtc1WaDCPA6FpkDadg75C30IOfYvaVqs7QkViFPMCjUI7bdjqFQn Pr/zj3kFVqNJSTFZOfIBnDAsD X-Received: by 2002:aa7:c9c6:0:b0:4ae:e51e:9e3e with SMTP id i6-20020aa7c9c6000000b004aee51e9e3emr348781edt.24.1676641477312; Fri, 17 Feb 2023 05:44:37 -0800 (PST) X-Google-Smtp-Source: AK7set9IRUJksGLLW/RDiXSfuPpHSO6kl3p0Lri0OcDeobF/5J+q0ZakgTJZKlO0HFr6pr+zDCdPAg== X-Received: by 2002:aa7:c9c6:0:b0:4ae:e51e:9e3e with SMTP id i6-20020aa7c9c6000000b004aee51e9e3emr348775edt.24.1676641477173; Fri, 17 Feb 2023 05:44:37 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id u8-20020a509508000000b004ad61135698sm1948004eda.13.2023.02.17.05.44.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:44:36 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 02/16] drm/exec: fix memory leak in drm_exec_prepare_obj() Date: Fri, 17 Feb 2023 14:44:08 +0100 Message-Id: <20230217134422.14116-3-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Don't call drm_gem_object_get() unconditionally. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/drm_exec.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/gpu/drm/drm_exec.c b/drivers/gpu/drm/drm_exec.c index ed2106c22786..5713a589a6a3 100644 --- a/drivers/gpu/drm/drm_exec.c +++ b/drivers/gpu/drm/drm_exec.c @@ -282,7 +282,6 @@ int drm_exec_prepare_obj(struct drm_exec *exec, struct = drm_gem_object *obj, goto error_unlock; } =20 - drm_gem_object_get(obj); return 0; =20 error_unlock: --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4264FC64EC4 for ; Fri, 17 Feb 2023 13:46:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229904AbjBQNpy (ORCPT ); Fri, 17 Feb 2023 08:45:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229768AbjBQNpa (ORCPT ); Fri, 17 Feb 2023 08:45:30 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6607C13D65 for ; Fri, 17 Feb 2023 05:44:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641483; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ysk0aOdzp2igGOtHeZoTd2E71aY9qQ/NU2yUGbV3q7I=; b=PMCDPjjTA/rrNGC4Vylab7IVxFwRiyFhD8G72minNrGtbNkC3/7L1zKz41lIALSi7RaCJF V88vnFD2EJdvIo8nsmfj4TXP4dj9BytB9FjNLzSHF+s/hS6X0Hkcxs+b/RdH0JFTxySc+e ZufSTYlUEtWXTZeOcSVQAZOC0N1ui7o= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-673-M4VkDKjFNnmdZfWubZMmgA-1; Fri, 17 Feb 2023 08:44:42 -0500 X-MC-Unique: M4VkDKjFNnmdZfWubZMmgA-1 Received: by mail-ed1-f69.google.com with SMTP id d9-20020a056402400900b004ad062fee5eso1727798eda.17 for ; Fri, 17 Feb 2023 05:44:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ysk0aOdzp2igGOtHeZoTd2E71aY9qQ/NU2yUGbV3q7I=; b=7Ly/Xh5XfWS/eu8q+6erM7HnTDp0DMa5vtjijm32CEanJu/8Tc45Z0i4jRI+glUe+g zOoEvGfkgXckCei4DOiyk/VpDJ/H0yQD5kTSZxLn6+K2GlqqaFgrek84p7urLV+S+jGX LkY4NZG+7Zeo+csVzmhsz+TeYwjWeXMaeM7TmHOfgr3SYIIm1RGnLVp/OSGyVTDpHxv8 mXIWdjtG+AGlUhxakLeaMFjSkK85wkPGL6klbEna3nU0WRe2wYPPa0XpHmL3OCNdoldl /YrinjtkHEz3c+12ZIMn68w2hOZ/sTJSGjskk9gRX/H31MqUp71ua2MtuaFGLWZC6PxC 7i5A== X-Gm-Message-State: AO0yUKX1JIbxNPRb9i3XpYFEe2cnFrO1SBDEVsbD1tOHLvR67p2NBHTB pJjEnRcWnHTKq8iiU/9MjrRlGY2el0wlZOZy0dzGMWApnUoCAKj6vxaDLdCp0Qapmx4RABayQHP OHmZJf/Tg3yTiHrx60i7kxdgs X-Received: by 2002:a17:907:d309:b0:8b1:2dda:b60d with SMTP id vg9-20020a170907d30900b008b12ddab60dmr5401603ejc.20.1676641481149; Fri, 17 Feb 2023 05:44:41 -0800 (PST) X-Google-Smtp-Source: AK7set+xPQs4iBYRE3EkYeswx89ncoFpP13u/wl+vaRpLe0EqlVsAKT/Yi78czFv33gb2ohWXHgOKA== X-Received: by 2002:a17:907:d309:b0:8b1:2dda:b60d with SMTP id vg9-20020a170907d30900b008b12ddab60dmr5401592ejc.20.1676641481019; Fri, 17 Feb 2023 05:44:41 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id r10-20020a1709063d6a00b00882f9130eafsm2167138ejf.26.2023.02.17.05.44.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:44:40 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 03/16] maple_tree: split up MA_STATE() macro Date: Fri, 17 Feb 2023 14:44:09 +0100 Message-Id: <20230217134422.14116-4-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Split up the MA_STATE() macro such that components using the maple tree can easily inherit from struct ma_state and build custom tree walk macros to hide their internals from users. Example: struct sample_iter { struct ma_state mas; struct sample_mgr *mgr; struct sample_entry *entry; }; \#define SAMPLE_ITER(name, __mgr) \ struct sample_iter name =3D { \ .mas =3D __MA_STATE(&(__mgr)->mt, 0, 0), .mgr =3D __mgr, .entry =3D NULL, } \#define sample_iter_for_each_range(it__, start__, end__) \ for ((it__).mas.index =3D start__, (it__).entry =3D mas_find(&(it__).mas, = end__ - 1); \ (it__).entry; (it__).entry =3D mas_find(&(it__).mas, end__ - 1)) Signed-off-by: Danilo Krummrich --- include/linux/maple_tree.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h index e594db58a0f1..ca04c900e51a 100644 --- a/include/linux/maple_tree.h +++ b/include/linux/maple_tree.h @@ -424,8 +424,8 @@ struct ma_wr_state { #define MA_ERROR(err) \ ((struct maple_enode *)(((unsigned long)err << 2) | 2UL)) =20 -#define MA_STATE(name, mt, first, end) \ - struct ma_state name =3D { \ +#define __MA_STATE(mt, first, end) \ + { \ .tree =3D mt, \ .index =3D first, \ .last =3D end, \ @@ -435,6 +435,9 @@ struct ma_wr_state { .alloc =3D NULL, \ } =20 +#define MA_STATE(name, mt, first, end) \ + struct ma_state name =3D __MA_STATE(mt, first, end) + #define MA_WR_STATE(name, ma_state, wr_entry) \ struct ma_wr_state name =3D { \ .mas =3D ma_state, \ --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B00E3C64ED6 for ; Fri, 17 Feb 2023 13:45:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229801AbjBQNpr (ORCPT ); Fri, 17 Feb 2023 08:45:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229660AbjBQNpZ (ORCPT ); Fri, 17 Feb 2023 08:45:25 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76D6B14206 for ; Fri, 17 Feb 2023 05:44:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641487; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Hw5y2doCl00T3ibUla5BtLSkevMMipuwdAIlo8ABos8=; b=Rs5dMdFQ52LKShWZU+ePiya26iBa2WqoaKfi5ZQLro53g5Da4yPlRgn5+X9THI/OF6ULkc Bbd+z9rJTGQa9ZVzAex9PSM1hLaEQBKqKU4fLF0OvXKQ3RKb2xACi6VQxGaCt4COt3nYRR wf4l80EcTocD1UKAG1OsbXGiUO7+9zE= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-232-jOesfNOCPL-E-9I4aGCuYQ-1; Fri, 17 Feb 2023 08:44:46 -0500 X-MC-Unique: jOesfNOCPL-E-9I4aGCuYQ-1 Received: by mail-ed1-f69.google.com with SMTP id b16-20020a056402351000b004ace77022ebso1900123edd.8 for ; Fri, 17 Feb 2023 05:44:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Hw5y2doCl00T3ibUla5BtLSkevMMipuwdAIlo8ABos8=; b=KZp+N4pNB9B/oSAL/hA96+vxFpXj2zBRlqGHRiyl0lDievOYCTYtk9Kh+ZCzGHaMJX 4cqrCOAP1VI5kR5VLAviDgBGhaAGfwFJ6aQ92hMt60E7RCIM3VgCaWuMKHiUcFPnpHp0 QanjRntdCjG9QxZTwTBx0VpgRATluzySm5U/pj4jfb2HJZrwz/3xfnydBhitXHQWAzHn coz40Q3sx0PowPbiyyvSz0YsOchW2fHUgsZ3S7O+LAUO1CoMOpEW4dXMqU3Dm2KxWuLE D0zDWh+2h+wObtX975gBU1YDgLard1F5S+QKuzd3eua/ryI9zc1PgRx6NEZ/PQ4iQ8mc V6kw== X-Gm-Message-State: AO0yUKWeVLZ4Rg3x0yCw5EBjhnY9Z5cViZjdhdXdY6Rtq3I578JgCSLX nvcbdmlAXNIR+IYq4u4A7Cig5DHndvM9cOSj0nB6/T0zsLy2ry1AqewZp0FQWidnhQDBTGP6iAT kn52xEINW+6JZZ1IUCusZfP6N X-Received: by 2002:a17:907:7204:b0:8ab:a378:5f96 with SMTP id dr4-20020a170907720400b008aba3785f96mr6877789ejc.3.1676641485227; Fri, 17 Feb 2023 05:44:45 -0800 (PST) X-Google-Smtp-Source: AK7set/TZG7ec9qZlnOFOe4bPzyNu9aCVszGIuq0bCjLphIoqf/IkkxWSOngWo7N98WoGLtfENjN0g== X-Received: by 2002:a17:907:7204:b0:8ab:a378:5f96 with SMTP id dr4-20020a170907720400b008aba3785f96mr6877771ejc.3.1676641484996; Fri, 17 Feb 2023 05:44:44 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id t6-20020a1709066bc600b008b159612b2dsm2164061ejs.106.2023.02.17.05.44.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:44:44 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 04/16] maple_tree: add flag MT_FLAGS_LOCK_NONE Date: Fri, 17 Feb 2023 14:44:10 +0100 Message-Id: <20230217134422.14116-5-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Generic components making use of the maple tree (such as the DRM GPUVA Manager) delegate the responsibility of ensuring mutual exclusion to their users. While such components could inherit the concept of an external lock, some users might just serialize the access to the component and hence to the internal maple tree. In order to allow such use cases, add a new flag MT_FLAGS_LOCK_NONE to indicate not to do any internal lockdep checks. Signed-off-by: Danilo Krummrich --- include/linux/maple_tree.h | 20 +++++++++++++++----- lib/maple_tree.c | 7 ++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h index ca04c900e51a..f795e5def8d0 100644 --- a/include/linux/maple_tree.h +++ b/include/linux/maple_tree.h @@ -170,10 +170,11 @@ enum maple_type { #define MT_FLAGS_USE_RCU 0x02 #define MT_FLAGS_HEIGHT_OFFSET 0x02 #define MT_FLAGS_HEIGHT_MASK 0x7C -#define MT_FLAGS_LOCK_MASK 0x300 +#define MT_FLAGS_LOCK_MASK 0x700 #define MT_FLAGS_LOCK_IRQ 0x100 #define MT_FLAGS_LOCK_BH 0x200 #define MT_FLAGS_LOCK_EXTERN 0x300 +#define MT_FLAGS_LOCK_NONE 0x400 =20 #define MAPLE_HEIGHT_MAX 31 =20 @@ -559,11 +560,16 @@ static inline void mas_set(struct ma_state *mas, unsi= gned long index) mas_set_range(mas, index, index); } =20 -static inline bool mt_external_lock(const struct maple_tree *mt) +static inline bool mt_lock_external(const struct maple_tree *mt) { return (mt->ma_flags & MT_FLAGS_LOCK_MASK) =3D=3D MT_FLAGS_LOCK_EXTERN; } =20 +static inline bool mt_lock_none(const struct maple_tree *mt) +{ + return (mt->ma_flags & MT_FLAGS_LOCK_MASK) =3D=3D MT_FLAGS_LOCK_NONE; +} + /** * mt_init_flags() - Initialise an empty maple tree with flags. * @mt: Maple Tree @@ -577,7 +583,7 @@ static inline bool mt_external_lock(const struct maple_= tree *mt) static inline void mt_init_flags(struct maple_tree *mt, unsigned int flags) { mt->ma_flags =3D flags; - if (!mt_external_lock(mt)) + if (!mt_lock_external(mt) && !mt_lock_none(mt)) spin_lock_init(&mt->ma_lock); rcu_assign_pointer(mt->ma_root, NULL); } @@ -612,9 +618,11 @@ static inline void mt_clear_in_rcu(struct maple_tree *= mt) if (!mt_in_rcu(mt)) return; =20 - if (mt_external_lock(mt)) { + if (mt_lock_external(mt)) { BUG_ON(!mt_lock_is_held(mt)); mt->ma_flags &=3D ~MT_FLAGS_USE_RCU; + } else if (mt_lock_none(mt)) { + mt->ma_flags &=3D ~MT_FLAGS_USE_RCU; } else { mtree_lock(mt); mt->ma_flags &=3D ~MT_FLAGS_USE_RCU; @@ -631,9 +639,11 @@ static inline void mt_set_in_rcu(struct maple_tree *mt) if (mt_in_rcu(mt)) return; =20 - if (mt_external_lock(mt)) { + if (mt_lock_external(mt)) { BUG_ON(!mt_lock_is_held(mt)); mt->ma_flags |=3D MT_FLAGS_USE_RCU; + } else if (mt_lock_none(mt)) { + mt->ma_flags |=3D MT_FLAGS_USE_RCU; } else { mtree_lock(mt); mt->ma_flags |=3D MT_FLAGS_USE_RCU; diff --git a/lib/maple_tree.c b/lib/maple_tree.c index 26e2045d3cda..f51c0fd4eaad 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -802,8 +802,8 @@ static inline void __rcu **ma_slots(struct maple_node *= mn, enum maple_type mt) =20 static inline bool mt_locked(const struct maple_tree *mt) { - return mt_external_lock(mt) ? mt_lock_is_held(mt) : - lockdep_is_held(&mt->ma_lock); + return mt_lock_external(mt) ? mt_lock_is_held(mt) : + mt_lock_none(mt) ? true : lockdep_is_held(&mt->ma_lock); } =20 static inline void *mt_slot(const struct maple_tree *mt, @@ -6120,7 +6120,8 @@ bool mas_nomem(struct ma_state *mas, gfp_t gfp) return false; } =20 - if (gfpflags_allow_blocking(gfp) && !mt_external_lock(mas->tree)) { + if (gfpflags_allow_blocking(gfp) && + !mt_lock_external(mas->tree) && !mt_lock_none(mas->tree)) { mtree_unlock(mas->tree); mas_alloc_nodes(mas, gfp); mtree_lock(mas->tree); --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 907C4C6379F for ; Fri, 17 Feb 2023 13:46:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229949AbjBQNp6 (ORCPT ); Fri, 17 Feb 2023 08:45:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229642AbjBQNpp (ORCPT ); Fri, 17 Feb 2023 08:45:45 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB3FD14237 for ; Fri, 17 Feb 2023 05:44:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641495; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2dQGSqJNfvjw1TcjewHtrd025tzeuBg4RcAC7yA56II=; b=iZLN3kCw+HSK0IMlUGhNA0lhAcf3aG0ZkbYXXEoXIO1aoajBOIdbevLHUKLLst0OMVoCx0 NZITjuwZ40IKz12zAuXjfweJbrcAfXx3lng3I4GRQ9JVPXROyE8w+y7/hsj1KL3RA5Iykj OpDwqgtsDNf5tp90wnerj0NE4EAPnJ0= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-615-3tZeeuToO3-sigekFyJw_A-1; Fri, 17 Feb 2023 08:44:54 -0500 X-MC-Unique: 3tZeeuToO3-sigekFyJw_A-1 Received: by mail-ed1-f72.google.com with SMTP id ee29-20020a056402291d00b0049ec3a108beso1640690edb.7 for ; Fri, 17 Feb 2023 05:44:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2dQGSqJNfvjw1TcjewHtrd025tzeuBg4RcAC7yA56II=; b=DcMM3Tz7Td5n94hTfayBIIdRHbWwmq+Z4encW5bAF7zopXZkKITBaHsiM+uMrNJ8Fh NYAdWEyVO4/siVsWgFiQ+DHreL/ttEd8AQw8Aaydm/UGJf5nNEPJ7/XqIl7VjMGs19Uc Oh8pOmsm7m6Cioz7fPhCvNro6Y+BbZEMHNY7B7KefrvzrDBsG+ti0oexPx8RRW/DOAWH xh+/G2S4siteLX2f76OhMQv+beFk1qo95ivM9WBQEG0NFnpXlxtMaQW9866HDd/CoEaQ A6IaMuxQWqBYLNMSfA47nAtSSq6tLIGc5zPLfDMcDZRTABaQvoT43H6+TowkMx6JYak9 u6pA== X-Gm-Message-State: AO0yUKVE8fHbS8GiYPCsdkoYPRv5/BxlsO7w6HId8yCBb6zKZDTfm/Jw 2LH8VQQ7CYkvEG4VRUT5QkbFgdsGdqBmRtQaLp4Z7Tz6R2qfC7SYFkj8xVNk6QgT7F1cKFC/7jb IxmnavE9xyJ0Fmz3Y++TjtL/C X-Received: by 2002:a17:906:7fc8:b0:88d:5043:2299 with SMTP id r8-20020a1709067fc800b0088d50432299mr1149586ejs.51.1676641490029; Fri, 17 Feb 2023 05:44:50 -0800 (PST) X-Google-Smtp-Source: AK7set++eVCkVSaBsVy3HluNIwtpO+iefhrnRic5Dxqpkeqd1LTe3YIcKh6NFSrT26vDj4/e0gZbFg== X-Received: by 2002:a17:906:7fc8:b0:88d:5043:2299 with SMTP id r8-20020a1709067fc800b0088d50432299mr1149541ejs.51.1676641489204; Fri, 17 Feb 2023 05:44:49 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id j23-20020a17090643d700b008838b040454sm2158069ejn.95.2023.02.17.05.44.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:44:48 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich , Dave Airlie Subject: [PATCH drm-next v2 05/16] drm: manager to keep track of GPUs VA mappings Date: Fri, 17 Feb 2023 14:44:11 +0100 Message-Id: <20230217134422.14116-6-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add infrastructure to keep track of GPU virtual address (VA) mappings with a decicated VA space manager implementation. New UAPIs, motivated by Vulkan sparse memory bindings graphics drivers start implementing, allow userspace applications to request multiple and arbitrary GPU VA mappings of buffer objects. The DRM GPU VA manager is intended to serve the following purposes in this context. 1) Provide infrastructure to track GPU VA allocations and mappings, making use of the maple_tree. 2) Generically connect GPU VA mappings to their backing buffers, in particular DRM GEM objects. 3) Provide a common implementation to perform more complex mapping operations on the GPU VA space. In particular splitting and merging of GPU VA mappings, e.g. for intersecting mapping requests or partial unmap requests. Suggested-by: Dave Airlie Signed-off-by: Danilo Krummrich --- Documentation/gpu/drm-mm.rst | 31 + drivers/gpu/drm/Makefile | 1 + drivers/gpu/drm/drm_gem.c | 3 + drivers/gpu/drm/drm_gpuva_mgr.c | 1704 +++++++++++++++++++++++++++++++ include/drm/drm_drv.h | 6 + include/drm/drm_gem.h | 75 ++ include/drm/drm_gpuva_mgr.h | 714 +++++++++++++ 7 files changed, 2534 insertions(+) create mode 100644 drivers/gpu/drm/drm_gpuva_mgr.c create mode 100644 include/drm/drm_gpuva_mgr.h diff --git a/Documentation/gpu/drm-mm.rst b/Documentation/gpu/drm-mm.rst index a52e6f4117d6..c9f120cfe730 100644 --- a/Documentation/gpu/drm-mm.rst +++ b/Documentation/gpu/drm-mm.rst @@ -466,6 +466,37 @@ DRM MM Range Allocator Function References .. kernel-doc:: drivers/gpu/drm/drm_mm.c :export: =20 +DRM GPU VA Manager +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Overview +-------- + +.. kernel-doc:: drivers/gpu/drm/drm_gpuva_mgr.c + :doc: Overview + +Split and Merge +--------------- + +.. kernel-doc:: drivers/gpu/drm/drm_gpuva_mgr.c + :doc: Split and Merge + +Locking +------- + +.. kernel-doc:: drivers/gpu/drm/drm_gpuva_mgr.c + :doc: Locking + + +DRM GPU VA Manager Function References +-------------------------------------- + +.. kernel-doc:: include/drm/drm_gpuva_mgr.h + :internal: + +.. kernel-doc:: drivers/gpu/drm/drm_gpuva_mgr.c + :export: + DRM Buddy Allocator =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile index d40defbb0347..4d098efffb98 100644 --- a/drivers/gpu/drm/Makefile +++ b/drivers/gpu/drm/Makefile @@ -45,6 +45,7 @@ drm-y :=3D \ drm_vblank.o \ drm_vblank_work.o \ drm_vma_manager.o \ + drm_gpuva_mgr.o \ drm_writeback.o drm-$(CONFIG_DRM_LEGACY) +=3D \ drm_agpsupport.o \ diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c index 59a0bb5ebd85..65115fe88627 100644 --- a/drivers/gpu/drm/drm_gem.c +++ b/drivers/gpu/drm/drm_gem.c @@ -164,6 +164,9 @@ void drm_gem_private_object_init(struct drm_device *dev, if (!obj->resv) obj->resv =3D &obj->_resv; =20 + if (drm_core_check_feature(dev, DRIVER_GEM_GPUVA)) + drm_gem_gpuva_init(obj); + drm_vma_node_reset(&obj->vma_node); INIT_LIST_HEAD(&obj->lru_node); } diff --git a/drivers/gpu/drm/drm_gpuva_mgr.c b/drivers/gpu/drm/drm_gpuva_mg= r.c new file mode 100644 index 000000000000..19f583704562 --- /dev/null +++ b/drivers/gpu/drm/drm_gpuva_mgr.c @@ -0,0 +1,1704 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2022 Red Hat. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software= "), + * to deal in the Software without restriction, including without limitati= on + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Danilo Krummrich + * + */ + +#include +#include + +/** + * DOC: Overview + * + * The DRM GPU VA Manager, represented by struct drm_gpuva_manager keeps t= rack + * of a GPU's virtual address (VA) space and manages the corresponding vir= tual + * mappings represented by &drm_gpuva objects. It also keeps track of the + * mapping's backing &drm_gem_object buffers. + * + * &drm_gem_object buffers maintain a list (and a corresponding list lock)= of + * &drm_gpuva objects representing all existent GPU VA mappings using this + * &drm_gem_object as backing buffer. + * + * If the &DRM_GPUVA_MANAGER_REGIONS feature is enabled, a GPU VA mapping = can + * only be created within a previously allocated &drm_gpuva_region, which + * represents a reserved portion of the GPU VA space. GPU VA mappings are = not + * allowed to span over a &drm_gpuva_region's boundary. + * + * GPU VA regions can also be flagged as sparse, which allows drivers to c= reate + * sparse mappings for a whole GPU VA region in order to support Vulkan + * 'Sparse Resources'. + * + * The GPU VA manager internally uses &maple_tree structures to manage the + * &drm_gpuva mappings and the &drm_gpuva_regions within a GPU's virtual a= ddress + * space. + * + * Besides the GPU VA space regions (&drm_gpuva_region) allocated by a dri= ver + * the &drm_gpuva_manager contains a special region representing the porti= on of + * VA space reserved by the kernel. This node is initialized together with= the + * GPU VA manager instance and removed when the GPU VA manager is destroye= d. + * + * In a typical application drivers would embed struct drm_gpuva_manager, + * struct drm_gpuva_region and struct drm_gpuva within their own driver + * specific structures, there won't be any memory allocations of it's own = nor + * memory allocations of &drm_gpuva or &drm_gpuva_region entries. + */ + +/** + * DOC: Split and Merge + * + * The DRM GPU VA manager also provides an algorithm implementing splittin= g and + * merging of existent GPU VA mappings with the ones that are requested to= be + * mapped or unmapped. This feature is required by the Vulkan API to imple= ment + * Vulkan 'Sparse Memory Bindings' - drivers UAPIs often refer to this as + * VM BIND. + * + * Drivers can call drm_gpuva_sm_map() to receive a sequence of callbacks + * containing map, unmap and remap operations for a given newly requested + * mapping. The sequence of callbacks represents the set of operations to + * execute in order to integrate the new mapping cleanly into the current = state + * of the GPU VA space. + * + * Depending on how the new GPU VA mapping intersects with the existent ma= ppings + * of the GPU VA space the &drm_gpuva_fn_ops callbacks contain an arbitrary + * amount of unmap operations, a maximum of two remap operations and a sin= gle + * map operation. The caller might receive no callback at all if no operat= ion is + * required, e.g. if the requested mapping already exists in the exact sam= e way. + * + * The single map operation, if existent, represents the original map oper= ation + * requested by the caller. Please note that this operation might be alter= ed + * comparing it with the original map operation, e.g. because it was merge= d with + * an already existent mapping. Hence, drivers must execute this map oper= ation + * instead of the original one passed to drm_gpuva_sm_map(). + * + * &drm_gpuva_op_unmap contains a 'keep' field, which indicates whether the + * &drm_gpuva to unmap is physically contiguous with the original mapping + * request. Optionally, if 'keep' is set, drivers may keep the actual page= table + * entries for this &drm_gpuva, adding the missing page table entries only= and + * update the &drm_gpuva_manager's view of things accordingly. + * + * Drivers may do the same optimization, namely delta page table updates, = also + * for remap operations. This is possible since &drm_gpuva_op_remap consis= ts of + * one unmap operation and one or two map operations, such that drivers can + * derive the page table update delta accordingly. + * + * Note that there can't be more than two existent mappings to split up, o= ne at + * the beginning and one at the end of the new mapping, hence there is a + * maximum of two remap operations. + * + * Generally, the DRM GPU VA manager never merges mappings across the + * boundaries of &drm_gpuva_regions. This is the case since merging between + * GPU VA regions would result into unmap and map operations to be issued = for + * both regions involved although the original mapping request was referre= d to + * one specific GPU VA region only. Since the other GPU VA region, the one= not + * explicitly requested to be altered, might be in use by the GPU, we are = not + * allowed to issue any map/unmap operations for this region. + * + * To update the &drm_gpuva_manager's view of the GPU VA space + * drm_gpuva_insert() and drm_gpuva_remove() should be used. + * + * Analogous to drm_gpuva_sm_map() drm_gpuva_sm_unmap() uses &drm_gpuva_fn= _ops + * to call back into the driver in order to unmap a range of GPU VA space.= The + * logic behind this function is way simpler though: For all existent mapp= ings + * enclosed by the given range unmap operations are created. For mappings = which + * are only partically located within the given range, remap operations are + * created such that those mappings are split up and re-mapped partically. + * + * The following diagram depicts the basic relationships of existent GPU VA + * mappings, a newly requested mapping and the resulting mappings as imple= mented + * by drm_gpuva_sm_map() - it doesn't cover any arbitrary combinations of = these. + * + * 1) Requested mapping is identical, hence noop. + * + * :: + * + * 0 a 1 + * old: |-----------| (bo_offset=3Dn) + * + * 0 a 1 + * req: |-----------| (bo_offset=3Dn) + * + * 0 a 1 + * new: |-----------| (bo_offset=3Dn) + * + * + * 2) Requested mapping is identical, except for the BO offset, hence repl= ace + * the mapping. + * + * :: + * + * 0 a 1 + * old: |-----------| (bo_offset=3Dn) + * + * 0 a 1 + * req: |-----------| (bo_offset=3Dm) + * + * 0 a 1 + * new: |-----------| (bo_offset=3Dm) + * + * + * 3) Requested mapping is identical, except for the backing BO, hence rep= lace + * the mapping. + * + * :: + * + * 0 a 1 + * old: |-----------| (bo_offset=3Dn) + * + * 0 b 1 + * req: |-----------| (bo_offset=3Dn) + * + * 0 b 1 + * new: |-----------| (bo_offset=3Dn) + * + * + * 4) Existent mapping is a left aligned subset of the requested one, hence + * replace the existent one. + * + * :: + * + * 0 a 1 + * old: |-----| (bo_offset=3Dn) + * + * 0 a 2 + * req: |-----------| (bo_offset=3Dn) + * + * 0 a 2 + * new: |-----------| (bo_offset=3Dn) + * + * .. note:: + * We expect to see the same result for a request with a different BO + * and/or non-contiguous BO offset. + * + * + * 5) Requested mapping's range is a left aligned subset of the existent o= ne, + * but backed by a different BO. Hence, map the requested mapping and s= plit + * the existent one adjusting it's BO offset. + * + * :: + * + * 0 a 2 + * old: |-----------| (bo_offset=3Dn) + * + * 0 b 1 + * req: |-----| (bo_offset=3Dn) + * + * 0 b 1 a' 2 + * new: |-----|-----| (b.bo_offset=3Dn, a.bo_offset=3Dn+1) + * + * .. note:: + * We expect to see the same result for a request with a different BO + * and/or non-contiguous BO offset. + * + * + * 6) Existent mapping is a superset of the requested mapping, hence noop. + * + * :: + * + * 0 a 2 + * old: |-----------| (bo_offset=3Dn) + * + * 0 a 1 + * req: |-----| (bo_offset=3Dn) + * + * 0 a 2 + * new: |-----------| (bo_offset=3Dn) + * + * + * 7) Requested mapping's range is a right aligned subset of the existent = one, + * but backed by a different BO. Hence, map the requested mapping and s= plit + * the existent one, without adjusting the BO offset. + * + * :: + * + * 0 a 2 + * old: |-----------| (bo_offset=3Dn) + * + * 1 b 2 + * req: |-----| (bo_offset=3Dm) + * + * 0 a 1 b 2 + * new: |-----|-----| (a.bo_offset=3Dn,b.bo_offset=3Dm) + * + * + * 8) Existent mapping is a superset of the requested mapping, hence noop. + * + * :: + * + * 0 a 2 + * old: |-----------| (bo_offset=3Dn) + * + * 1 a 2 + * req: |-----| (bo_offset=3Dn+1) + * + * 0 a 2 + * new: |-----------| (bo_offset=3Dn) + * + * + * 9) Existent mapping is overlapped at the end by the requested mapping b= acked + * by a different BO. Hence, map the requested mapping and split up the + * existent one, without adjusting the BO offset. + * + * :: + * + * 0 a 2 + * old: |-----------| (bo_offset=3Dn) + * + * 1 b 3 + * req: |-----------| (bo_offset=3Dm) + * + * 0 a 1 b 3 + * new: |-----|-----------| (a.bo_offset=3Dn,b.bo_offset=3Dm) + * + * + * 10) Existent mapping is overlapped by the requested mapping, both havin= g the + * same backing BO with a contiguous offset. Hence, merge both mapping= s. + * + * :: + * + * 0 a 2 + * old: |-----------| (bo_offset=3Dn) + * + * 1 a 3 + * req: |-----------| (bo_offset=3Dn+1) + * + * 0 a 3 + * new: |-----------------| (bo_offset=3Dn) + * + * + * 11) Requested mapping's range is a centered subset of the existent one + * having a different backing BO. Hence, map the requested mapping and= split + * up the existent one in two mappings, adjusting the BO offset of the= right + * one accordingly. + * + * :: + * + * 0 a 3 + * old: |-----------------| (bo_offset=3Dn) + * + * 1 b 2 + * req: |-----| (bo_offset=3Dm) + * + * 0 a 1 b 2 a' 3 + * new: |-----|-----|-----| (a.bo_offset=3Dn,b.bo_offset=3Dm,a'.bo_offset= =3Dn+2) + * + * + * 12) Requested mapping is a contiguous subset of the existent one, hence= noop. + * + * :: + * + * 0 a 3 + * old: |-----------------| (bo_offset=3Dn) + * + * 1 a 2 + * req: |-----| (bo_offset=3Dn+1) + * + * 0 a 3 + * old: |-----------------| (bo_offset=3Dn) + * + * + * 13) Existent mapping is a right aligned subset of the requested one, he= nce + * replace the existent one. + * + * :: + * + * 1 a 2 + * old: |-----| (bo_offset=3Dn+1) + * + * 0 a 2 + * req: |-----------| (bo_offset=3Dn) + * + * 0 a 2 + * new: |-----------| (bo_offset=3Dn) + * + * .. note:: + * We expect to see the same result for a request with a different = bo + * and/or non-contiguous bo_offset. + * + * + * 14) Existent mapping is a centered subset of the requested one, hence + * replace the existent one. + * + * :: + * + * 1 a 2 + * old: |-----| (bo_offset=3Dn+1) + * + * 0 a 3 + * req: |----------------| (bo_offset=3Dn) + * + * 0 a 3 + * new: |----------------| (bo_offset=3Dn) + * + * .. note:: + * We expect to see the same result for a request with a different = bo + * and/or non-contiguous bo_offset. + * + * + * 15) Existent mappings is overlapped at the beginning by the requested m= apping + * backed by a different BO. Hence, map the requested mapping and spli= t up + * the existent one, adjusting it's BO offset accordingly. + * + * :: + * + * 1 a 3 + * old: |-----------| (bo_offset=3Dn) + * + * 0 b 2 + * req: |-----------| (bo_offset=3Dm) + * + * 0 b 2 a' 3 + * new: |-----------|-----| (b.bo_offset=3Dm,a.bo_offset=3Dn+2) + * + * + * 16) Requested mapping fills the gap between two existent mappings all h= aving + * the same backing BO, such that all three have a contiguous BO offse= t. + * Hence, merge all mappings. + * + * :: + * + * 0 a 1 + * old: |-----------| (bo_offset=3Dn) + * + * 2 a 3 + * old': |-----------| (bo_offset=3Dn+2) + * + * 1 a 2 + * req: |-----------| (bo_offset=3Dn+1) + * + * a + * new: |----------------------------------| (bo_offset=3Dn) + */ + +/** + * DOC: Locking + * + * Generally, the GPU VA manager does not take care of locking itself, it = is + * the drivers responsibility to take care about locking. Drivers might wa= nt to + * protect the following operations: inserting, removing and iterating + * &drm_gpuva and &drm_gpuva_region objects as well as generating all kind= s of + * operations, such as split / merge or prefetch. + * + * The GPU VA manager also does not take care of the locking of the backing + * &drm_gem_object buffers GPU VA lists by itself; drivers are responsible= to + * enforce mutual exclusion. + */ + + +static int __drm_gpuva_region_insert(struct drm_gpuva_manager *mgr, + struct drm_gpuva_region *reg); +static void __drm_gpuva_region_remove(struct drm_gpuva_region *reg); + +/** + * drm_gpuva_manager_init - initialize a &drm_gpuva_manager + * @mgr: pointer to the &drm_gpuva_manager to initialize + * @name: the name of the GPU VA space + * @start_offset: the start offset of the GPU VA space + * @range: the size of the GPU VA space + * @reserve_offset: the start of the kernel reserved GPU VA area + * @reserve_range: the size of the kernel reserved GPU VA area + * @ops: &drm_gpuva_fn_ops called on &drm_gpuva_sm_map / &drm_gpuva_sm_unm= ap + * @flags: the feature flags of the &drm_gpuva_manager + * + * The &drm_gpuva_manager must be initialized with this function before us= e. + * + * Note that @mgr must be cleared to 0 before calling this function. The g= iven + * &name is expected to be managed by the surrounding driver structures. + */ +void +drm_gpuva_manager_init(struct drm_gpuva_manager *mgr, + const char *name, + u64 start_offset, u64 range, + u64 reserve_offset, u64 reserve_range, + struct drm_gpuva_fn_ops *ops, + enum drm_gpuva_mgr_flags flags) +{ + mt_init_flags(&mgr->region_mt, MT_FLAGS_LOCK_NONE); + mt_init_flags(&mgr->va_mt, MT_FLAGS_LOCK_NONE); + + mgr->mm_start =3D start_offset; + mgr->mm_range =3D range; + + mgr->name =3D name ? name : "unknown"; + mgr->ops =3D ops; + mgr->flags =3D flags; + + memset(&mgr->kernel_alloc_region, 0, sizeof(struct drm_gpuva_region)); + mgr->kernel_alloc_region.va.addr =3D reserve_offset; + mgr->kernel_alloc_region.va.range =3D reserve_range; + + __drm_gpuva_region_insert(mgr, &mgr->kernel_alloc_region); +} +EXPORT_SYMBOL(drm_gpuva_manager_init); + +/** + * drm_gpuva_manager_destroy - cleanup a &drm_gpuva_manager + * @mgr: pointer to the &drm_gpuva_manager to clean up + * + * Note that it is a bug to call this function on a manager that still + * holds GPU VA mappings. + */ +void +drm_gpuva_manager_destroy(struct drm_gpuva_manager *mgr) +{ + mgr->name =3D NULL; + __drm_gpuva_region_remove(&mgr->kernel_alloc_region); + + WARN(!mtree_empty(&mgr->va_mt), + "GPUVA tree is not empty, potentially leaking memory."); + __mt_destroy(&mgr->va_mt); + + WARN(!mtree_empty(&mgr->region_mt), + "GPUVA region tree is not empty, potentially leaking memory."); + __mt_destroy(&mgr->region_mt); +} +EXPORT_SYMBOL(drm_gpuva_manager_destroy); + +static inline bool +drm_gpuva_in_mm_range(struct drm_gpuva_manager *mgr, u64 addr, u64 range) +{ + u64 end =3D addr + range; + u64 mm_start =3D mgr->mm_start; + u64 mm_end =3D mm_start + mgr->mm_range; + + return addr < mm_end && mm_start < end; +} + +static inline bool +drm_gpuva_in_kernel_region(struct drm_gpuva_manager *mgr, u64 addr, u64 ra= nge) +{ + u64 end =3D addr + range; + u64 kstart =3D mgr->kernel_alloc_region.va.addr; + u64 kend =3D kstart + mgr->kernel_alloc_region.va.range; + + return addr < kend && kstart < end; +} + +static struct drm_gpuva_region * +drm_gpuva_in_region(struct drm_gpuva_manager *mgr, u64 addr, u64 range) +{ + DRM_GPUVA_REGION_ITER(it, mgr); + + /* Find the VA region the requested range is strictly enclosed by. */ + drm_gpuva_iter_for_each_range(it, addr, addr + range) { + struct drm_gpuva_region *reg =3D it.reg; + + if (reg->va.addr <=3D addr && + reg->va.addr + reg->va.range >=3D addr + range && + reg !=3D &mgr->kernel_alloc_region) + return reg; + } + + return NULL; +} + +static bool +drm_gpuva_in_any_region(struct drm_gpuva_manager *mgr, u64 addr, u64 range) +{ + return !!drm_gpuva_in_region(mgr, addr, range); +} + +/** + * drm_gpuva_remove_iter - removes the iterators current element + * @it: the &drm_gpuva_iterator + * + * This removes the element the iterator currently points to. + */ +void +drm_gpuva_iter_remove(struct drm_gpuva_iterator *it) +{ + mas_erase(&it->mas); +} +EXPORT_SYMBOL(drm_gpuva_iter_remove); + +/** + * drm_gpuva_insert - insert a &drm_gpuva + * @mgr: the &drm_gpuva_manager to insert the &drm_gpuva in + * @va: the &drm_gpuva to insert + * @addr: the start address of the GPU VA + * @range: the range of the GPU VA + * + * Insert a &drm_gpuva with a given address and range into a + * &drm_gpuva_manager. + * + * Returns: 0 on success, negative error code on failure. + */ +int +drm_gpuva_insert(struct drm_gpuva_manager *mgr, + struct drm_gpuva *va) +{ + u64 addr =3D va->va.addr; + u64 range =3D va->va.range; + MA_STATE(mas, &mgr->va_mt, addr, addr + range - 1); + struct drm_gpuva_region *reg =3D NULL; + int ret; + + if (unlikely(!drm_gpuva_in_mm_range(mgr, addr, range))) + return -EINVAL; + + if (unlikely(drm_gpuva_in_kernel_region(mgr, addr, range))) + return -EINVAL; + + if (mgr->flags & DRM_GPUVA_MANAGER_REGIONS) { + reg =3D drm_gpuva_in_region(mgr, addr, range); + if (unlikely(!reg)) + return -EINVAL; + } + + if (unlikely(drm_gpuva_find_first(mgr, addr, range))) + return -EEXIST; + + ret =3D mas_store_gfp(&mas, va, GFP_KERNEL); + if (unlikely(ret)) + return ret; + + va->mgr =3D mgr; + va->region =3D reg; + + return 0; +} +EXPORT_SYMBOL(drm_gpuva_insert); + +/** + * drm_gpuva_remove - remove a &drm_gpuva + * @va: the &drm_gpuva to remove + * + * This removes the given &va from the underlaying tree. + */ +void +drm_gpuva_remove(struct drm_gpuva *va) +{ + MA_STATE(mas, &va->mgr->va_mt, va->va.addr, 0); + + mas_erase(&mas); +} +EXPORT_SYMBOL(drm_gpuva_remove); + +/** + * drm_gpuva_link - link a &drm_gpuva + * @va: the &drm_gpuva to link + * + * This adds the given &va to the GPU VA list of the &drm_gem_object it is + * associated with. + * + * This function expects the caller to protect the GEM's GPUVA list against + * concurrent access. + */ +void +drm_gpuva_link(struct drm_gpuva *va) +{ + if (likely(va->gem.obj)) + list_add_tail(&va->head, &va->gem.obj->gpuva.list); +} +EXPORT_SYMBOL(drm_gpuva_link); + +/** + * drm_gpuva_unlink - unlink a &drm_gpuva + * @va: the &drm_gpuva to unlink + * + * This removes the given &va from the GPU VA list of the &drm_gem_object = it is + * associated with. + * + * This function expects the caller to protect the GEM's GPUVA list against + * concurrent access. + */ +void +drm_gpuva_unlink(struct drm_gpuva *va) +{ + if (likely(va->gem.obj)) + list_del_init(&va->head); +} +EXPORT_SYMBOL(drm_gpuva_unlink); + +/** + * drm_gpuva_find_first - find the first &drm_gpuva in the given range + * @mgr: the &drm_gpuva_manager to search in + * @addr: the &drm_gpuvas address + * @range: the &drm_gpuvas range + * + * Returns: the first &drm_gpuva within the given range + */ +struct drm_gpuva * +drm_gpuva_find_first(struct drm_gpuva_manager *mgr, + u64 addr, u64 range) +{ + MA_STATE(mas, &mgr->va_mt, addr, 0); + + return mas_find(&mas, addr + range - 1); +} +EXPORT_SYMBOL(drm_gpuva_find_first); + +/** + * drm_gpuva_find - find a &drm_gpuva + * @mgr: the &drm_gpuva_manager to search in + * @addr: the &drm_gpuvas address + * @range: the &drm_gpuvas range + * + * Returns: the &drm_gpuva at a given &addr and with a given &range + */ +struct drm_gpuva * +drm_gpuva_find(struct drm_gpuva_manager *mgr, + u64 addr, u64 range) +{ + struct drm_gpuva *va; + + va =3D drm_gpuva_find_first(mgr, addr, range); + if (!va) + goto out; + + if (va->va.range !=3D range) + goto out; + + return va; + +out: + return NULL; +} +EXPORT_SYMBOL(drm_gpuva_find); + +/** + * drm_gpuva_find_prev - find the &drm_gpuva before the given address + * @mgr: the &drm_gpuva_manager to search in + * @start: the given GPU VA's start address + * + * Find the adjacent &drm_gpuva before the GPU VA with given &start addres= s. + * + * Note that if there is any free space between the GPU VA mappings no map= ping + * is returned. + * + * Returns: a pointer to the found &drm_gpuva or NULL if none was found + */ +struct drm_gpuva * +drm_gpuva_find_prev(struct drm_gpuva_manager *mgr, u64 start) +{ + MA_STATE(mas, &mgr->va_mt, start, 0); + + if (start <=3D mgr->mm_start || + start > (mgr->mm_start + mgr->mm_range)) + return NULL; + + return mas_prev(&mas, start - 1); +} +EXPORT_SYMBOL(drm_gpuva_find_prev); + +/** + * drm_gpuva_find_next - find the &drm_gpuva after the given address + * @mgr: the &drm_gpuva_manager to search in + * @end: the given GPU VA's end address + * + * Find the adjacent &drm_gpuva after the GPU VA with given &end address. + * + * Note that if there is any free space between the GPU VA mappings no map= ping + * is returned. + * + * Returns: a pointer to the found &drm_gpuva or NULL if none was found + */ +struct drm_gpuva * +drm_gpuva_find_next(struct drm_gpuva_manager *mgr, u64 end) +{ + MA_STATE(mas, &mgr->va_mt, end - 1, 0); + + if (end < mgr->mm_start || + end >=3D (mgr->mm_start + mgr->mm_range)) + return NULL; + + return mas_next(&mas, end); +} +EXPORT_SYMBOL(drm_gpuva_find_next); + +static int +__drm_gpuva_region_insert(struct drm_gpuva_manager *mgr, + struct drm_gpuva_region *reg) +{ + u64 addr =3D reg->va.addr; + u64 range =3D reg->va.range; + MA_STATE(mas, &mgr->region_mt, addr, addr + range - 1); + int ret; + + if (unlikely(!drm_gpuva_in_mm_range(mgr, addr, range))) + return -EINVAL; + + ret =3D mas_store_gfp(&mas, reg, GFP_KERNEL); + if (unlikely(ret)) + return ret; + + reg->mgr =3D mgr; + + return 0; +} + +/** + * drm_gpuva_region_insert - insert a &drm_gpuva_region + * @mgr: the &drm_gpuva_manager to insert the &drm_gpuva in + * @reg: the &drm_gpuva_region to insert + * @addr: the start address of the GPU VA + * @range: the range of the GPU VA + * + * Insert a &drm_gpuva_region with a given address and range into a + * &drm_gpuva_manager. + * + * Returns: 0 on success, negative error code on failure. + */ +int +drm_gpuva_region_insert(struct drm_gpuva_manager *mgr, + struct drm_gpuva_region *reg) +{ + if (unlikely(!(mgr->flags & DRM_GPUVA_MANAGER_REGIONS))) + return -EINVAL; + + return __drm_gpuva_region_insert(mgr, reg); +} +EXPORT_SYMBOL(drm_gpuva_region_insert); + +static void +__drm_gpuva_region_remove(struct drm_gpuva_region *reg) +{ + struct drm_gpuva_manager *mgr =3D reg->mgr; + MA_STATE(mas, &mgr->region_mt, reg->va.addr, 0); + + mas_erase(&mas); +} + +/** + * drm_gpuva_region_remove - remove a &drm_gpuva_region + * @reg: the &drm_gpuva to remove + * + * This removes the given ® from the underlaying tree. + */ +void +drm_gpuva_region_remove(struct drm_gpuva_region *reg) +{ + struct drm_gpuva_manager *mgr =3D reg->mgr; + + if (unlikely(!(mgr->flags & DRM_GPUVA_MANAGER_REGIONS))) + return; + + if (unlikely(reg =3D=3D &mgr->kernel_alloc_region)) { + WARN(1, "Can't destroy kernel reserved region.\n"); + return; + } + + if (unlikely(!drm_gpuva_region_empty(reg))) + WARN(1, "GPU VA region should be empty on destroy.\n"); + + __drm_gpuva_region_remove(reg); +} +EXPORT_SYMBOL(drm_gpuva_region_remove); + +/** + * drm_gpuva_region_empty - indicate whether a &drm_gpuva_region is empty + * @reg: the &drm_gpuva to destroy + * + * Returns: true if the &drm_gpuva_region is empty, false otherwise + */ +bool +drm_gpuva_region_empty(struct drm_gpuva_region *reg) +{ + DRM_GPUVA_ITER(it, reg->mgr); + + drm_gpuva_iter_for_each_range(it, reg->va.addr, + reg->va.addr + + reg->va.range) + return false; + + return true; +} +EXPORT_SYMBOL(drm_gpuva_region_empty); + +/** + * drm_gpuva_region_find_first - find the first &drm_gpuva_region in the g= iven + * range + * @mgr: the &drm_gpuva_manager to search in + * @addr: the &drm_gpuva_regions address + * @range: the &drm_gpuva_regions range + * + * Returns: the first &drm_gpuva_region within the given range + */ +struct drm_gpuva_region * +drm_gpuva_region_find_first(struct drm_gpuva_manager *mgr, + u64 addr, u64 range) +{ + MA_STATE(mas, &mgr->region_mt, addr, 0); + + return mas_find(&mas, addr + range - 1); +} +EXPORT_SYMBOL(drm_gpuva_region_find_first); + +/** + * drm_gpuva_region_find - find a &drm_gpuva_region + * @mgr: the &drm_gpuva_manager to search in + * @addr: the &drm_gpuva_regions address + * @range: the &drm_gpuva_regions range + * + * Returns: the &drm_gpuva_region at a given &addr and with a given &range + */ +struct drm_gpuva_region * +drm_gpuva_region_find(struct drm_gpuva_manager *mgr, + u64 addr, u64 range) +{ + struct drm_gpuva_region *reg; + + reg =3D drm_gpuva_region_find_first(mgr, addr, range); + if (!reg) + goto out; + + if (reg->va.range !=3D range) + goto out; + + return reg; + +out: + return NULL; +} +EXPORT_SYMBOL(drm_gpuva_region_find); + +static int +op_map_cb(int (*step)(struct drm_gpuva_op *, void *), + void *priv, + u64 addr, u64 range, + struct drm_gem_object *obj, u64 offset) +{ + struct drm_gpuva_op op =3D {}; + + op.op =3D DRM_GPUVA_OP_MAP; + op.map.va.addr =3D addr; + op.map.va.range =3D range; + op.map.gem.obj =3D obj; + op.map.gem.offset =3D offset; + + return step(&op, priv); +} + +static int +op_remap_cb(int (*step)(struct drm_gpuva_op *, void *), + void *priv, + struct drm_gpuva_op_map *prev, + struct drm_gpuva_op_map *next, + struct drm_gpuva_op_unmap *unmap) +{ + struct drm_gpuva_op op =3D {}; + struct drm_gpuva_op_remap *r; + + op.op =3D DRM_GPUVA_OP_REMAP; + r =3D &op.remap; + r->prev =3D prev; + r->next =3D next; + r->unmap =3D unmap; + + return step(&op, priv); +} + +static int +op_unmap_cb(int (*step)(struct drm_gpuva_op *, void *), + void *priv, + struct drm_gpuva *va, bool merge) +{ + struct drm_gpuva_op op =3D {}; + + op.op =3D DRM_GPUVA_OP_UNMAP; + op.unmap.va =3D va; + op.unmap.keep =3D merge; + + return step(&op, priv); +} + +static inline bool +gpuva_should_merge(struct drm_gpuva *va) +{ + /* Never merge mappings with NULL GEMs. */ + return !!va->gem.obj; +} + +static int +__drm_gpuva_sm_map(struct drm_gpuva_manager *mgr, + struct drm_gpuva_fn_ops *ops, void *priv, + u64 req_addr, u64 req_range, + struct drm_gem_object *req_obj, u64 req_offset) +{ + DRM_GPUVA_ITER(it, mgr); + int (*step)(struct drm_gpuva_op *, void *); + struct drm_gpuva *va, *prev =3D NULL; + u64 req_end =3D req_addr + req_range; + bool skip_pmerge =3D false, skip_nmerge =3D false; + int ret; + + step =3D ops->sm_map_step; + + if (unlikely(!drm_gpuva_in_mm_range(mgr, req_addr, req_range))) + return -EINVAL; + + if (unlikely(drm_gpuva_in_kernel_region(mgr, req_addr, req_range))) + return -EINVAL; + + if ((mgr->flags & DRM_GPUVA_MANAGER_REGIONS) && + !drm_gpuva_in_any_region(mgr, req_addr, req_range)) + return -EINVAL; + + drm_gpuva_iter_for_each_range(it, req_addr, req_end) { + struct drm_gpuva *va =3D it.va; + struct drm_gem_object *obj =3D va->gem.obj; + u64 offset =3D va->gem.offset; + u64 addr =3D va->va.addr; + u64 range =3D va->va.range; + u64 end =3D addr + range; + bool merge =3D gpuva_should_merge(va); + + /* Generally, we want to skip merging with potential mappings + * left and right of the requested one when we found a + * collision, since merging happens in this loop already. + * + * However, there is one exception when the requested mapping + * spans into a free VM area. If this is the case we might + * still hit the boundary of another mapping before and/or + * after the free VM area. + */ + skip_pmerge =3D true; + skip_nmerge =3D true; + + if (addr =3D=3D req_addr) { + merge &=3D obj =3D=3D req_obj && + offset =3D=3D req_offset; + + if (end =3D=3D req_end) { + if (merge) + goto done; + + ret =3D op_unmap_cb(step, priv, va, false); + if (ret) + return ret; + break; + } + + if (end < req_end) { + skip_nmerge =3D false; + ret =3D op_unmap_cb(step, priv, va, merge); + if (ret) + return ret; + goto next; + } + + if (end > req_end) { + struct drm_gpuva_op_map n =3D { + .va.addr =3D req_end, + .va.range =3D range - req_range, + .gem.obj =3D obj, + .gem.offset =3D offset + req_range, + }; + struct drm_gpuva_op_unmap u =3D { .va =3D va }; + + if (merge) + goto done; + + ret =3D op_remap_cb(step, priv, NULL, &n, &u); + if (ret) + return ret; + break; + } + } else if (addr < req_addr) { + u64 ls_range =3D req_addr - addr; + struct drm_gpuva_op_map p =3D { + .va.addr =3D addr, + .va.range =3D ls_range, + .gem.obj =3D obj, + .gem.offset =3D offset, + }; + struct drm_gpuva_op_unmap u =3D { .va =3D va }; + + merge &=3D obj =3D=3D req_obj && + offset + ls_range =3D=3D req_offset; + + if (end =3D=3D req_end) { + if (merge) + goto done; + + ret =3D op_remap_cb(step, priv, &p, NULL, &u); + if (ret) + return ret; + break; + } + + if (end < req_end) { + u64 new_addr =3D addr; + u64 new_range =3D req_range + ls_range; + u64 new_offset =3D offset; + + /* We validated that the requested mapping is + * within a single VA region already. + * Since it overlaps the current mapping (which + * can't cross a VA region boundary) we can be + * sure that we're still within the boundaries + * of the same VA region after merging. + */ + if (merge) { + req_offset =3D new_offset; + req_addr =3D new_addr; + req_range =3D new_range; + ret =3D op_unmap_cb(step, priv, va, true); + if (ret) + return ret; + goto next; + } + + ret =3D op_remap_cb(step, priv, &p, NULL, &u); + if (ret) + return ret; + goto next; + } + + if (end > req_end) { + struct drm_gpuva_op_map n =3D { + .va.addr =3D req_end, + .va.range =3D end - req_end, + .gem.obj =3D obj, + .gem.offset =3D offset + ls_range + + req_range, + }; + + if (merge) + goto done; + + ret =3D op_remap_cb(step, priv, &p, &n, &u); + if (ret) + return ret; + break; + } + } else if (addr > req_addr) { + merge &=3D obj =3D=3D req_obj && + offset =3D=3D req_offset + + (addr - req_addr); + + if (!prev) + skip_pmerge =3D false; + + if (end =3D=3D req_end) { + ret =3D op_unmap_cb(step, priv, va, merge); + if (ret) + return ret; + break; + } + + if (end < req_end) { + skip_nmerge =3D false; + ret =3D op_unmap_cb(step, priv, va, merge); + if (ret) + return ret; + goto next; + } + + if (end > req_end) { + struct drm_gpuva_op_map n =3D { + .va.addr =3D req_end, + .va.range =3D end - req_end, + .gem.obj =3D obj, + .gem.offset =3D offset + req_end - addr, + }; + struct drm_gpuva_op_unmap u =3D { .va =3D va }; + u64 new_end =3D end; + u64 new_range =3D new_end - req_addr; + + /* We validated that the requested mapping is + * within a single VA region already. + * Since it overlaps the current mapping (which + * can't cross a VA region boundary) we can be + * sure that we're still within the boundaries + * of the same VA region after merging. + */ + if (merge) { + req_end =3D new_end; + req_range =3D new_range; + ret =3D op_unmap_cb(step, priv, va, true); + if (ret) + return ret; + break; + } + + ret =3D op_remap_cb(step, priv, NULL, &n, &u); + if (ret) + return ret; + break; + } + } +next: + prev =3D va; + } + + va =3D skip_pmerge ? NULL : drm_gpuva_find_prev(mgr, req_addr); + if (va) { + struct drm_gem_object *obj =3D va->gem.obj; + u64 offset =3D va->gem.offset; + u64 addr =3D va->va.addr; + u64 range =3D va->va.range; + u64 new_offset =3D offset; + u64 new_addr =3D addr; + u64 new_range =3D req_range + range; + bool merge =3D gpuva_should_merge(va) && + obj =3D=3D req_obj && + offset + range =3D=3D req_offset; + + if (mgr->flags & DRM_GPUVA_MANAGER_REGIONS) + merge &=3D drm_gpuva_in_any_region(mgr, new_addr, + new_range); + + if (merge) { + ret =3D op_unmap_cb(step, priv, va, true); + if (ret) + return ret; + + req_offset =3D new_offset; + req_addr =3D new_addr; + req_range =3D new_range; + } + } + + va =3D skip_nmerge ? NULL : drm_gpuva_find_next(mgr, req_end); + if (va) { + struct drm_gem_object *obj =3D va->gem.obj; + u64 offset =3D va->gem.offset; + u64 addr =3D va->va.addr; + u64 range =3D va->va.range; + u64 end =3D addr + range; + u64 new_range =3D req_range + range; + u64 new_end =3D end; + bool merge =3D gpuva_should_merge(va) && + obj =3D=3D req_obj && + offset =3D=3D req_offset + req_range; + + if (mgr->flags & DRM_GPUVA_MANAGER_REGIONS) + merge &=3D drm_gpuva_in_any_region(mgr, req_addr, + new_range); + + if (merge) { + ret =3D op_unmap_cb(step, priv, va, true); + if (ret) + return ret; + + req_range =3D new_range; + req_end =3D new_end; + } + } + + ret =3D op_map_cb(step, priv, + req_addr, req_range, + req_obj, req_offset); + if (ret) + return ret; + +done: + return 0; +} + +static int +__drm_gpuva_sm_unmap(struct drm_gpuva_manager *mgr, + struct drm_gpuva_fn_ops *ops, void *priv, + u64 req_addr, u64 req_range) +{ + DRM_GPUVA_ITER(it, mgr); + int (*step)(struct drm_gpuva_op *, void *); + u64 req_end =3D req_addr + req_range; + int ret; + + step =3D ops->sm_unmap_step; + + drm_gpuva_iter_for_each_range(it, req_addr, req_end) { + struct drm_gpuva *va =3D it.va; + struct drm_gpuva_op_map prev =3D {}, next =3D {}; + bool prev_split =3D false, next_split =3D false; + struct drm_gem_object *obj =3D va->gem.obj; + u64 offset =3D va->gem.offset; + u64 addr =3D va->va.addr; + u64 range =3D va->va.range; + u64 end =3D addr + range; + + if (addr < req_addr) { + prev.va.addr =3D addr; + prev.va.range =3D req_addr - addr; + prev.gem.obj =3D obj; + prev.gem.offset =3D offset; + + prev_split =3D true; + } + + if (end > req_end) { + next.va.addr =3D req_end; + next.va.range =3D end - req_end; + next.gem.obj =3D obj; + next.gem.offset =3D offset + (req_end - addr); + + next_split =3D true; + } + + if (prev_split || next_split) { + struct drm_gpuva_op_unmap unmap =3D { .va =3D va }; + + ret =3D op_remap_cb(step, priv, &prev, &next, &unmap); + if (ret) + return ret; + } else { + ret =3D op_unmap_cb(step, priv, va, false); + if (ret) + return ret; + } + } + + return 0; +} + +/** + * drm_gpuva_sm_map - creates the &drm_gpuva_op split/merge steps + * @mgr: the &drm_gpuva_manager representing the GPU VA space + * @req_addr: the start address of the new mapping + * @req_range: the range of the new mapping + * @req_obj: the &drm_gem_object to map + * @req_offset: the offset within the &drm_gem_object + * @priv: pointer to a driver private data structure + * + * This function iterates the given range of the GPU VA space. It utilizes= the + * &drm_gpuva_fn_ops to call back into the driver providing the split and = merge + * steps. + * + * Drivers may use these callbacks to update the GPU VA space right away w= ithin + * the callback. In case the driver decides to copy and store the operatio= ns for + * later processing neither this function nor &drm_gpuva_sm_unmap is allow= ed to + * be called before the &drm_gpuva_manager's view of the GPU VA space was + * updated with the previous set of operations. To update the + * &drm_gpuva_manager's view of the GPU VA space drm_gpuva_insert(), + * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be + * used. + * + * A sequence of callbacks can contain map, unmap and remap operations, but + * the sequence of callbacks might also be empty if no operation is requir= ed, + * e.g. if the requested mapping already exists in the exact same way. + * + * There can be an arbitrary amount of unmap operations, a maximum of two = remap + * operations and a single map operation. The latter one, if existent, + * represents the original map operation requested by the caller. Please n= ote + * that the map operation might has been modified, e.g. if it was merged w= ith + * an existent mapping. + * + * Returns: 0 on success or a negative error code + */ +int +drm_gpuva_sm_map(struct drm_gpuva_manager *mgr, void *priv, + u64 req_addr, u64 req_range, + struct drm_gem_object *req_obj, u64 req_offset) +{ + if (!mgr->ops || !mgr->ops->sm_map_step) + return -EINVAL; + + return __drm_gpuva_sm_map(mgr, mgr->ops, priv, + req_addr, req_range, + req_obj, req_offset); +} +EXPORT_SYMBOL(drm_gpuva_sm_map); + +/** + * drm_gpuva_sm_unmap - creates the &drm_gpuva_ops to split on unmap + * @mgr: the &drm_gpuva_manager representing the GPU VA space + * @req_addr: the start address of the range to unmap + * @req_range: the range of the mappings to unmap + * @ops: the &drm_gpuva_fn_ops callbacks to provide the split/merge steps + * @priv: pointer to a driver private data structure + * + * This function iterates the given range of the GPU VA space. It utilizes= the + * &drm_gpuva_fn_ops to call back into the driver providing the operations= to + * unmap and, if required, split existent mappings. + * + * Drivers may use these callbacks to update the GPU VA space right away w= ithin + * the callback. In case the driver decides to copy and store the operatio= ns for + * later processing neither this function nor &drm_gpuva_sm_map is allowed= to be + * called before the &drm_gpuva_manager's view of the GPU VA space was upd= ated + * with the previous set of operations. To update the &drm_gpuva_manager's= view + * of the GPU VA space drm_gpuva_insert(), drm_gpuva_destroy_locked() and/= or + * drm_gpuva_destroy_unlocked() should be used. + * + * A sequence of callbacks can contain unmap and remap operations, dependi= ng on + * whether there are actual overlapping mappings to split. + * + * There can be an arbitrary amount of unmap operations and a maximum of t= wo + * remap operations. + * + * Returns: 0 on success or a negative error code + */ +int +drm_gpuva_sm_unmap(struct drm_gpuva_manager *mgr, void *priv, + u64 req_addr, u64 req_range) +{ + if (!mgr->ops || !mgr->ops->sm_unmap_step) + return -EINVAL; + + return __drm_gpuva_sm_unmap(mgr, mgr->ops, priv, + req_addr, req_range); +} +EXPORT_SYMBOL(drm_gpuva_sm_unmap); + +static struct drm_gpuva_op * +gpuva_op_alloc(struct drm_gpuva_manager *mgr) +{ + struct drm_gpuva_fn_ops *fn =3D mgr->ops; + struct drm_gpuva_op *op; + + if (fn && fn->op_alloc) + op =3D fn->op_alloc(); + else + op =3D kzalloc(sizeof(*op), GFP_KERNEL); + + if (unlikely(!op)) + return NULL; + + return op; +} + +static void +gpuva_op_free(struct drm_gpuva_manager *mgr, + struct drm_gpuva_op *op) +{ + struct drm_gpuva_fn_ops *fn =3D mgr->ops; + + if (fn && fn->op_free) + fn->op_free(op); + else + kfree(op); +} + +int drm_gpuva_sm_step(struct drm_gpuva_op *__op, void *priv) +{ + struct { + struct drm_gpuva_manager *mgr; + struct drm_gpuva_ops *ops; + } *args =3D priv; + struct drm_gpuva_manager *mgr =3D args->mgr; + struct drm_gpuva_ops *ops =3D args->ops; + struct drm_gpuva_op *op; + + op =3D gpuva_op_alloc(mgr); + if (unlikely(!op)) + goto err; + + memcpy(op, __op, sizeof(*op)); + + if (op->op =3D=3D DRM_GPUVA_OP_REMAP) { + struct drm_gpuva_op_remap *__r =3D &__op->remap; + struct drm_gpuva_op_remap *r =3D &op->remap; + + r->unmap =3D kmemdup(__r->unmap, sizeof(*r->unmap), + GFP_KERNEL); + if (unlikely(!r->unmap)) + goto err_free_op; + + if (__r->prev) { + r->prev =3D kmemdup(__r->prev, sizeof(*r->prev), + GFP_KERNEL); + if (unlikely(!r->prev)) + goto err_free_unmap; + } + + if (__r->next) { + r->next =3D kmemdup(__r->next, sizeof(*r->next), + GFP_KERNEL); + if (unlikely(!r->next)) + goto err_free_prev; + } + } + + list_add_tail(&op->entry, &ops->list); + + return 0; + +err_free_unmap: + kfree(op->remap.unmap); +err_free_prev: + kfree(op->remap.prev); +err_free_op: + gpuva_op_free(mgr, op); +err: + return -ENOMEM; +} + +static struct drm_gpuva_fn_ops gpuva_list_ops =3D { + .sm_map_step =3D drm_gpuva_sm_step, + .sm_unmap_step =3D drm_gpuva_sm_step, +}; + +/** + * drm_gpuva_sm_map_ops_create - creates the &drm_gpuva_ops to split and m= erge + * @mgr: the &drm_gpuva_manager representing the GPU VA space + * @req_addr: the start address of the new mapping + * @req_range: the range of the new mapping + * @req_obj: the &drm_gem_object to map + * @req_offset: the offset within the &drm_gem_object + * + * This function creates a list of operations to perform splitting and mer= ging + * of existent mapping(s) with the newly requested one. + * + * The list can be iterated with &drm_gpuva_for_each_op and must be proces= sed + * in the given order. It can contain map, unmap and remap operations, but= it + * also can be empty if no operation is required, e.g. if the requested ma= pping + * already exists is the exact same way. + * + * There can be an arbitrary amount of unmap operations, a maximum of two = remap + * operations and a single map operation. The latter one, if existent, + * represents the original map operation requested by the caller. Please n= ote + * that the map operation might has been modified, e.g. if it was merged w= ith an + * existent mapping. + * + * Note that before calling this function again with another mapping reque= st it + * is necessary to update the &drm_gpuva_manager's view of the GPU VA spac= e. The + * previously obtained operations must be either processed or abandoned. To + * update the &drm_gpuva_manager's view of the GPU VA space drm_gpuva_inse= rt(), + * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be + * used. + * + * After the caller finished processing the returned &drm_gpuva_ops, they = must + * be freed with &drm_gpuva_ops_free. + * + * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on fail= ure + */ +struct drm_gpuva_ops * +drm_gpuva_sm_map_ops_create(struct drm_gpuva_manager *mgr, + u64 req_addr, u64 req_range, + struct drm_gem_object *req_obj, u64 req_offset) +{ + struct drm_gpuva_ops *ops; + struct { + struct drm_gpuva_manager *mgr; + struct drm_gpuva_ops *ops; + } args; + int ret; + + ops =3D kzalloc(sizeof(*ops), GFP_KERNEL); + if (unlikely(!ops)) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&ops->list); + + args.mgr =3D mgr; + args.ops =3D ops; + + ret =3D __drm_gpuva_sm_map(mgr, &gpuva_list_ops, &args, + req_addr, req_range, + req_obj, req_offset); + if (ret) { + kfree(ops); + return ERR_PTR(ret); + } + + return ops; +} +EXPORT_SYMBOL(drm_gpuva_sm_map_ops_create); + +/** + * drm_gpuva_sm_unmap_ops_create - creates the &drm_gpuva_ops to split on = unmap + * @mgr: the &drm_gpuva_manager representing the GPU VA space + * @req_addr: the start address of the range to unmap + * @req_range: the range of the mappings to unmap + * + * This function creates a list of operations to perform unmapping and, if + * required, splitting of the mappings overlapping the unmap range. + * + * The list can be iterated with &drm_gpuva_for_each_op and must be proces= sed + * in the given order. It can contain unmap and remap operations, dependin= g on + * whether there are actual overlapping mappings to split. + * + * There can be an arbitrary amount of unmap operations and a maximum of t= wo + * remap operations. + * + * Note that before calling this function again with another range to unma= p it + * is necessary to update the &drm_gpuva_manager's view of the GPU VA spac= e. The + * previously obtained operations must be processed or abandoned. To updat= e the + * &drm_gpuva_manager's view of the GPU VA space drm_gpuva_insert(), + * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be + * used. + * + * After the caller finished processing the returned &drm_gpuva_ops, they = must + * be freed with &drm_gpuva_ops_free. + * + * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on fail= ure + */ +struct drm_gpuva_ops * +drm_gpuva_sm_unmap_ops_create(struct drm_gpuva_manager *mgr, + u64 req_addr, u64 req_range) +{ + struct drm_gpuva_ops *ops; + struct { + struct drm_gpuva_manager *mgr; + struct drm_gpuva_ops *ops; + } args; + int ret; + + ops =3D kzalloc(sizeof(*ops), GFP_KERNEL); + if (unlikely(!ops)) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&ops->list); + + args.mgr =3D mgr; + args.ops =3D ops; + + ret =3D __drm_gpuva_sm_unmap(mgr, &gpuva_list_ops, &args, + req_addr, req_range); + if (ret) { + kfree(ops); + return ERR_PTR(ret); + } + + return ops; +} +EXPORT_SYMBOL(drm_gpuva_sm_unmap_ops_create); + +/** + * drm_gpuva_prefetch_ops_create - creates the &drm_gpuva_ops to prefetch + * @mgr: the &drm_gpuva_manager representing the GPU VA space + * @req_addr: the start address of the range to prefetch + * @req_range: the range of the mappings to prefetch + * + * This function creates a list of operations to perform prefetching. + * + * The list can be iterated with &drm_gpuva_for_each_op and must be proces= sed + * in the given order. It can contain prefetch operations. + * + * There can be an arbitrary amount of prefetch operations. + * + * After the caller finished processing the returned &drm_gpuva_ops, they = must + * be freed with &drm_gpuva_ops_free. + * + * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on fail= ure + */ +struct drm_gpuva_ops * +drm_gpuva_prefetch_ops_create(struct drm_gpuva_manager *mgr, + u64 addr, u64 range) +{ + DRM_GPUVA_ITER(it, mgr); + struct drm_gpuva_ops *ops; + struct drm_gpuva_op *op; + int ret; + + ops =3D kzalloc(sizeof(*ops), GFP_KERNEL); + if (!ops) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&ops->list); + + drm_gpuva_iter_for_each_range(it, addr, addr + range) { + op =3D gpuva_op_alloc(mgr); + if (!op) { + ret =3D -ENOMEM; + goto err_free_ops; + } + + op->op =3D DRM_GPUVA_OP_PREFETCH; + op->prefetch.va =3D it.va; + list_add_tail(&op->entry, &ops->list); + } + + return ops; + +err_free_ops: + drm_gpuva_ops_free(mgr, ops); + return ERR_PTR(ret); +} +EXPORT_SYMBOL(drm_gpuva_prefetch_ops_create); + +/** + * drm_gpuva_gem_unmap_ops_create - creates the &drm_gpuva_ops to unmap a = GEM + * @mgr: the &drm_gpuva_manager representing the GPU VA space + * @obj: the &drm_gem_object to unmap + * + * This function creates a list of operations to perform unmapping for eve= ry + * GPUVA attached to a GEM. + * + * The list can be iterated with &drm_gpuva_for_each_op and consists out o= f an + * arbitrary amount of unmap operations. + * + * After the caller finished processing the returned &drm_gpuva_ops, they = must + * be freed with &drm_gpuva_ops_free. + * + * It is the callers responsibility to protect the GEMs GPUVA list against + * concurrent access. + * + * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on fail= ure + */ +struct drm_gpuva_ops * +drm_gpuva_gem_unmap_ops_create(struct drm_gpuva_manager *mgr, + struct drm_gem_object *obj) +{ + struct drm_gpuva_ops *ops; + struct drm_gpuva_op *op; + struct drm_gpuva *va; + int ret; + + ops =3D kzalloc(sizeof(*ops), GFP_KERNEL); + if (!ops) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&ops->list); + + drm_gem_for_each_gpuva(va, obj) { + op =3D gpuva_op_alloc(mgr); + if (!op) { + ret =3D -ENOMEM; + goto err_free_ops; + } + + op->op =3D DRM_GPUVA_OP_UNMAP; + op->unmap.va =3D va; + list_add_tail(&op->entry, &ops->list); + } + + return ops; + +err_free_ops: + drm_gpuva_ops_free(mgr, ops); + return ERR_PTR(ret); +} +EXPORT_SYMBOL(drm_gpuva_gem_unmap_ops_create); + + +/** + * drm_gpuva_ops_free - free the given &drm_gpuva_ops + * @mgr: the &drm_gpuva_manager the ops were created for + * @ops: the &drm_gpuva_ops to free + * + * Frees the given &drm_gpuva_ops structure including all the ops associat= ed + * with it. + */ +void +drm_gpuva_ops_free(struct drm_gpuva_manager *mgr, + struct drm_gpuva_ops *ops) +{ + struct drm_gpuva_op *op, *next; + + drm_gpuva_for_each_op_safe(op, next, ops) { + list_del(&op->entry); + + if (op->op =3D=3D DRM_GPUVA_OP_REMAP) { + kfree(op->remap.prev); + kfree(op->remap.next); + kfree(op->remap.unmap); + } + + gpuva_op_free(mgr, op); + } + + kfree(ops); +} +EXPORT_SYMBOL(drm_gpuva_ops_free); diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h index 1d76d0686b03..4fe4a1552948 100644 --- a/include/drm/drm_drv.h +++ b/include/drm/drm_drv.h @@ -104,6 +104,12 @@ enum drm_driver_feature { * acceleration should be handled by two drivers that are connected using= auxiliary bus. */ DRIVER_COMPUTE_ACCEL =3D BIT(7), + /** + * @DRIVER_GEM_GPUVA: + * + * Driver supports user defined GPU VA bindings for GEM objects. + */ + DRIVER_GEM_GPUVA =3D BIT(8), =20 /* IMPORTANT: Below are all the legacy flags, add new ones above. */ =20 diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index 772a4adf5287..4a3679034966 100644 --- a/include/drm/drm_gem.h +++ b/include/drm/drm_gem.h @@ -36,6 +36,8 @@ =20 #include #include +#include +#include =20 #include =20 @@ -337,6 +339,17 @@ struct drm_gem_object { */ struct dma_resv _resv; =20 + /** + * @gpuva: + * + * Provides the list and list mutex of GPU VAs attached to this + * GEM object. + */ + struct { + struct list_head list; + struct mutex mutex; + } gpuva; + /** * @funcs: * @@ -479,4 +492,66 @@ void drm_gem_lru_move_tail(struct drm_gem_lru *lru, st= ruct drm_gem_object *obj); unsigned long drm_gem_lru_scan(struct drm_gem_lru *lru, unsigned nr_to_sca= n, bool (*shrink)(struct drm_gem_object *obj)); =20 +/** + * drm_gem_gpuva_init - initialize the gpuva list of a GEM object + * @obj: the &drm_gem_object + * + * This initializes the &drm_gem_object's &drm_gpuva list and the mutex + * protecting it. + * + * Calling this function is only necessary for drivers intending to suppor= t the + * &drm_driver_feature DRIVER_GEM_GPUVA. + */ +static inline void drm_gem_gpuva_init(struct drm_gem_object *obj) +{ + INIT_LIST_HEAD(&obj->gpuva.list); + mutex_init(&obj->gpuva.mutex); +} + +/** + * drm_gem_gpuva_lock - lock the GEM's gpuva list mutex + * @obj: the &drm_gem_object + * + * This unlocks the mutex protecting the &drm_gem_object's &drm_gpuva list. + */ +static inline void drm_gem_gpuva_lock(struct drm_gem_object *obj) +{ + mutex_lock(&obj->gpuva.mutex); +} + +/** + * drm_gem_gpuva_unlock - unlock the GEM's gpuva list mutex + * @obj: the &drm_gem_object + * + * This unlocks the mutex protecting the &drm_gem_object's &drm_gpuva list. + */ +static inline void drm_gem_gpuva_unlock(struct drm_gem_object *obj) +{ + mutex_unlock(&obj->gpuva.mutex); +} + +/** + * drm_gem_for_each_gpuva - iternator to walk over a list of gpuvas + * @entry: &drm_gpuva structure to assign to in each iteration step + * @obj: the &drm_gem_object the &drm_gpuvas to walk are associated with + * + * This iterator walks over all &drm_gpuva structures associated with the + * &drm_gpuva_manager. + */ +#define drm_gem_for_each_gpuva(entry, obj) \ + list_for_each_entry(entry, &obj->gpuva.list, head) + +/** + * drm_gem_for_each_gpuva_safe - iternator to safely walk over a list of g= puvas + * @entry: &drm_gpuva structure to assign to in each iteration step + * @next: &next &drm_gpuva to store the next step + * @obj: the &drm_gem_object the &drm_gpuvas to walk are associated with + * + * This iterator walks over all &drm_gpuva structures associated with the + * &drm_gem_object. It is implemented with list_for_each_entry_safe(), hen= ce + * it is save against removal of elements. + */ +#define drm_gem_for_each_gpuva_safe(entry, next, obj) \ + list_for_each_entry_safe(entry, next, &obj->gpuva.list, head) + #endif /* __DRM_GEM_H__ */ diff --git a/include/drm/drm_gpuva_mgr.h b/include/drm/drm_gpuva_mgr.h new file mode 100644 index 000000000000..d245d01e37a9 --- /dev/null +++ b/include/drm/drm_gpuva_mgr.h @@ -0,0 +1,714 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __DRM_GPUVA_MGR_H__ +#define __DRM_GPUVA_MGR_H__ + +/* + * Copyright (c) 2022 Red Hat. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software= "), + * to deal in the Software without restriction, including without limitati= on + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include + +struct drm_gpuva_manager; +struct drm_gpuva_fn_ops; + +/** + * struct drm_gpuva_region - structure to track a portion of GPU VA space + * + * This structure represents a portion of a GPUs VA space and is associated + * with a &drm_gpuva_manager. + * + * GPU VA mappings, represented by &drm_gpuva objects, are restricted to be + * placed within a &drm_gpuva_region. + */ +struct drm_gpuva_region { + /** + * @mgr: the &drm_gpuva_manager this object is associated with + */ + struct drm_gpuva_manager *mgr; + + /** + * @va: structure containing the address and range of the &drm_gpuva_regi= on + */ + struct { + /** + * @addr: the start address + */ + u64 addr; + + /* + * @range: the range + */ + u64 range; + } va; + + /** + * @sparse: indicates whether this region is sparse + */ + bool sparse; +}; + +int drm_gpuva_region_insert(struct drm_gpuva_manager *mgr, + struct drm_gpuva_region *reg); +void drm_gpuva_region_remove(struct drm_gpuva_region *reg); + +bool +drm_gpuva_region_empty(struct drm_gpuva_region *reg); + +struct drm_gpuva_region * +drm_gpuva_region_find(struct drm_gpuva_manager *mgr, + u64 addr, u64 range); +struct drm_gpuva_region * +drm_gpuva_region_find_first(struct drm_gpuva_manager *mgr, + u64 addr, u64 range); + +/** + * enum drm_gpuva_flags - flags for struct drm_gpuva + */ +enum drm_gpuva_flags { + /** + * @DRM_GPUVA_EVICTED: + * + * Flag indicating that the &drm_gpuva's backing GEM is evicted. + */ + DRM_GPUVA_EVICTED =3D (1 << 0), + + /** + * @DRM_GPUVA_USERBITS: user defined bits + */ + DRM_GPUVA_USERBITS =3D (1 << 1), +}; + +/** + * struct drm_gpuva - structure to track a GPU VA mapping + * + * This structure represents a GPU VA mapping and is associated with a + * &drm_gpuva_manager. + * + * Typically, this structure is embedded in bigger driver structures. + */ +struct drm_gpuva { + /** + * @mgr: the &drm_gpuva_manager this object is associated with + */ + struct drm_gpuva_manager *mgr; + + /** + * @region: the &drm_gpuva_region the &drm_gpuva is mapped in + */ + struct drm_gpuva_region *region; + + /** + * @head: the &list_head to attach this object to a &drm_gem_object + */ + struct list_head head; + + /** + * @flags: the &drm_gpuva_flags for this mapping + */ + enum drm_gpuva_flags flags; + + /** + * @va: structure containing the address and range of the &drm_gpuva + */ + struct { + /** + * @addr: the start address + */ + u64 addr; + + /* + * @range: the range + */ + u64 range; + } va; + + /** + * @gem: structure containing the &drm_gem_object and it's offset + */ + struct { + /** + * @offset: the offset within the &drm_gem_object + */ + u64 offset; + + /** + * @obj: the mapped &drm_gem_object + */ + struct drm_gem_object *obj; + } gem; +}; + +void drm_gpuva_link(struct drm_gpuva *va); +void drm_gpuva_unlink(struct drm_gpuva *va); + +int drm_gpuva_insert(struct drm_gpuva_manager *mgr, + struct drm_gpuva *va); +void drm_gpuva_remove(struct drm_gpuva *va); + +struct drm_gpuva *drm_gpuva_find(struct drm_gpuva_manager *mgr, + u64 addr, u64 range); +struct drm_gpuva *drm_gpuva_find_first(struct drm_gpuva_manager *mgr, + u64 addr, u64 range); +struct drm_gpuva *drm_gpuva_find_prev(struct drm_gpuva_manager *mgr, u64 s= tart); +struct drm_gpuva *drm_gpuva_find_next(struct drm_gpuva_manager *mgr, u64 e= nd); + +/** + * drm_gpuva_evict - sets whether the backing GEM of this &drm_gpuva is ev= icted + * @va: the &drm_gpuva to set the evict flag for + * @evict: indicates whether the &drm_gpuva is evicted + */ +static inline void drm_gpuva_evict(struct drm_gpuva *va, bool evict) +{ + if (evict) + va->flags |=3D DRM_GPUVA_EVICTED; + else + va->flags &=3D ~DRM_GPUVA_EVICTED; +} + +/** + * drm_gpuva_evicted - indicates whether the backing BO of this &drm_gpuva + * is evicted + * @va: the &drm_gpuva to check + */ +static inline bool drm_gpuva_evicted(struct drm_gpuva *va) +{ + return va->flags & DRM_GPUVA_EVICTED; +} + +/** + * enum drm_gpuva_mgr_flags - the feature flags for the &drm_gpuva_manager + */ +enum drm_gpuva_mgr_flags { + /** + * @DRM_GPUVA_MANAGER_REGIONS: + * + * Enable the &drm_gpuva_manager to separately track &drm_gpuva_regions. + * + * &drm_gpuva_regions represent a reserved portion of VA space drivers + * can create mappings in. If regions are enabled, &drm_gpuvas can be + * created within an existing &drm_gpuva_region only and merge + * operations never indicate merging over region boundaries. + */ + DRM_GPUVA_MANAGER_REGIONS =3D (1 << 0), +}; + +/** + * struct drm_gpuva_manager - DRM GPU VA Manager + * + * The DRM GPU VA Manager keeps track of a GPU's virtual address space by = using + * &maple_tree structures. Typically, this structure is embedded in bigger + * driver structures. + * + * Drivers can pass addresses and ranges in an arbitrary unit, e.g. bytes = or + * pages. + * + * There should be one manager instance per GPU virtual address space. + */ +struct drm_gpuva_manager { + /** + * @name: the name of the DRM GPU VA space + */ + const char *name; + + /** + * @mm_start: start of the VA space + */ + u64 mm_start; + + /** + * @mm_range: length of the VA space + */ + u64 mm_range; + + /** + * @region_mt: the &maple_tree to track GPU VA regions + */ + struct maple_tree region_mt; + + /** + * @va_mt: the &maple_tree to track GPU VA mappings + */ + struct maple_tree va_mt; + + /** + * @kernel_alloc_region: + * + * &drm_gpuva_region representing the address space cutout reserved for + * the kernel + */ + struct drm_gpuva_region kernel_alloc_region; + + /** + * @ops: &drm_gpuva_fn_ops providing the split/merge steps to drivers + */ + struct drm_gpuva_fn_ops *ops; + + /** + * @flags: the feature flags of the &drm_gpuva_manager + */ + enum drm_gpuva_mgr_flags flags; +}; + +void drm_gpuva_manager_init(struct drm_gpuva_manager *mgr, + const char *name, + u64 start_offset, u64 range, + u64 reserve_offset, u64 reserve_range, + struct drm_gpuva_fn_ops *ops, + enum drm_gpuva_mgr_flags flags); +void drm_gpuva_manager_destroy(struct drm_gpuva_manager *mgr); + +/** + * struct drm_gpuva_iterator - iterator for walking the internal (maple) t= ree + */ +struct drm_gpuva_iterator { + /** + * @mas: the maple tree iterator (maple advanced state) + */ + struct ma_state mas; + + /** + * @mgr: the &drm_gpuva_manager to iterate + */ + struct drm_gpuva_manager *mgr; + + union { + /** + * @va: the current &drm_gpuva entry + */ + struct drm_gpuva *va; + + /** + * @reg: the current &drm_gpuva_region entry + */ + struct drm_gpuva_region *reg; + + /** + * @entry: the current entry + */ + void *entry; + }; +}; + +void drm_gpuva_iter_remove(struct drm_gpuva_iterator *it); + +/** + * DRM_GPUVA_ITER - create an iterator structure to iterate the &drm_gpuva= tree + * @name: the name of the &drm_gpuva_iterator to create + * @mgr: the &drm_gpuva_manager to iterate + */ +#define DRM_GPUVA_ITER(name, mgr__) \ + struct drm_gpuva_iterator name =3D { \ + .mas =3D __MA_STATE(&(mgr__)->va_mt, 0, 0), \ + .mgr =3D mgr__, \ + .va =3D NULL, \ + } + +/** + * DRM_GPUVA_REGION_ITER - create an iterator structure to iterate the + * &drm_gpuva_region tree + * @name: the name of the &drm_gpuva_iterator to create + * @mgr: the &drm_gpuva_manager to iterate + */ +#define DRM_GPUVA_REGION_ITER(name, mgr__) \ + struct drm_gpuva_iterator name =3D { \ + .mas =3D __MA_STATE(&(mgr__)->region_mt, 0, 0), \ + .mgr =3D mgr__, \ + .reg =3D NULL, \ + } + +/** + * drm_gpuva_iter_for_each_range - iternator to walk over a range of entri= es + * @it__: &drm_gpuva_iterator structure to assign to in each iteration step + * @start__: starting offset, the first entry will overlap this + * @end__: ending offset, the last entry will start before this (but may o= verlap) + * + * This function can be used to iterate both &drm_gpuva objects and + * &drm_gpuva_region objects. + * + * It is safe against the removal of elements using &drm_gpuva_iter_remove, + * however it is not safe against the removal of elements using + * &drm_gpuva_remove and &drm_gpuva_region_remove. + */ +#define drm_gpuva_iter_for_each_range(it__, start__, end__) \ + for ((it__).mas.index =3D start__, (it__).entry =3D mas_find(&(it__).mas,= end__ - 1); \ + (it__).entry; (it__).entry =3D mas_find(&(it__).mas, end__ - 1)) + +/** + * drm_gpuva_iter_for_each - iternator to walk over all existing entries + * @it__: &drm_gpuva_iterator structure to assign to in each iteration step + * + * This function can be used to iterate both &drm_gpuva objects and + * &drm_gpuva_region objects. + * + * It is safe against the removal of elements using &drm_gpuva_iter_remove, + * however it is not safe against the removal of elements using + * &drm_gpuva_remove and &drm_gpuva_region_remove. + */ +#define drm_gpuva_iter_for_each(it__) \ + drm_gpuva_iter_for_each_range(it__, (it__).mgr->mm_start, \ + (it__).mgr->mm_start + (it__).mgr->mm_range) + +/** + * enum drm_gpuva_op_type - GPU VA operation type + * + * Operations to alter the GPU VA mappings tracked by the &drm_gpuva_manag= er. + */ +enum drm_gpuva_op_type { + /** + * @DRM_GPUVA_OP_MAP: the map op type + */ + DRM_GPUVA_OP_MAP, + + /** + * @DRM_GPUVA_OP_REMAP: the remap op type + */ + DRM_GPUVA_OP_REMAP, + + /** + * @DRM_GPUVA_OP_UNMAP: the unmap op type + */ + DRM_GPUVA_OP_UNMAP, + + /** + * @DRM_GPUVA_OP_PREFETCH: the prefetch op type + */ + DRM_GPUVA_OP_PREFETCH, +}; + +/** + * struct drm_gpuva_op_map - GPU VA map operation + * + * This structure represents a single map operation generated by the + * DRM GPU VA manager. + */ +struct drm_gpuva_op_map { + /** + * @va: structure containing address and range of a map + * operation + */ + struct { + /** + * @addr: the base address of the new mapping + */ + u64 addr; + + /** + * @range: the range of the new mapping + */ + u64 range; + } va; + + /** + * @gem: structure containing the &drm_gem_object and it's offset + */ + struct { + /** + * @offset: the offset within the &drm_gem_object + */ + u64 offset; + + /** + * @obj: the &drm_gem_object to map + */ + struct drm_gem_object *obj; + } gem; +}; + +/** + * struct drm_gpuva_op_unmap - GPU VA unmap operation + * + * This structure represents a single unmap operation generated by the + * DRM GPU VA manager. + */ +struct drm_gpuva_op_unmap { + /** + * @va: the &drm_gpuva to unmap + */ + struct drm_gpuva *va; + + /** + * @keep: + * + * Indicates whether this &drm_gpuva is physically contiguous with the + * original mapping request. + * + * Optionally, if &keep is set, drivers may keep the actual page table + * mappings for this &drm_gpuva, adding the missing page table entries + * only and update the &drm_gpuva_manager accordingly. + */ + bool keep; +}; + +/** + * struct drm_gpuva_op_remap - GPU VA remap operation + * + * This represents a single remap operation generated by the DRM GPU VA ma= nager. + * + * A remap operation is generated when an existing GPU VA mmapping is spli= t up + * by inserting a new GPU VA mapping or by partially unmapping existent + * mapping(s), hence it consists of a maximum of two map and one unmap + * operation. + * + * The @unmap operation takes care of removing the original existing mappi= ng. + * @prev is used to remap the preceding part, @next the subsequent part. + * + * If either a new mapping's start address is aligned with the start addre= ss + * of the old mapping or the new mapping's end address is aligned with the + * end address of the old mapping, either @prev or @next is NULL. + * + * Note, the reason for a dedicated remap operation, rather than arbitrary + * unmap and map operations, is to give drivers the chance of extracting d= river + * specific data for creating the new mappings from the unmap operations's + * &drm_gpuva structure which typically is embedded in larger driver speci= fic + * structures. + */ +struct drm_gpuva_op_remap { + /** + * @prev: the preceding part of a split mapping + */ + struct drm_gpuva_op_map *prev; + + /** + * @next: the subsequent part of a split mapping + */ + struct drm_gpuva_op_map *next; + + /** + * @unmap: the unmap operation for the original existing mapping + */ + struct drm_gpuva_op_unmap *unmap; +}; + +/** + * struct drm_gpuva_op_prefetch - GPU VA prefetch operation + * + * This structure represents a single prefetch operation generated by the + * DRM GPU VA manager. + */ +struct drm_gpuva_op_prefetch { + /** + * @va: the &drm_gpuva to prefetch + */ + struct drm_gpuva *va; +}; + +/** + * struct drm_gpuva_op - GPU VA operation + * + * This structure represents a single generic operation. + * + * The particular type of the operation is defined by @op. + */ +struct drm_gpuva_op { + /** + * @entry: + * + * The &list_head used to distribute instances of this struct within + * &drm_gpuva_ops. + */ + struct list_head entry; + + /** + * @op: the type of the operation + */ + enum drm_gpuva_op_type op; + + union { + /** + * @map: the map operation + */ + struct drm_gpuva_op_map map; + + /** + * @remap: the remap operation + */ + struct drm_gpuva_op_remap remap; + + /** + * @unmap: the unmap operation + */ + struct drm_gpuva_op_unmap unmap; + + /** + * @prefetch: the prefetch operation + */ + struct drm_gpuva_op_prefetch prefetch; + }; +}; + +/** + * struct drm_gpuva_ops - wraps a list of &drm_gpuva_op + */ +struct drm_gpuva_ops { + /** + * @list: the &list_head + */ + struct list_head list; +}; + +/** + * drm_gpuva_for_each_op - iterator to walk over &drm_gpuva_ops + * @op: &drm_gpuva_op to assign in each iteration step + * @ops: &drm_gpuva_ops to walk + * + * This iterator walks over all ops within a given list of operations. + */ +#define drm_gpuva_for_each_op(op, ops) list_for_each_entry(op, &(ops)->lis= t, entry) + +/** + * drm_gpuva_for_each_op_safe - iterator to safely walk over &drm_gpuva_ops + * @op: &drm_gpuva_op to assign in each iteration step + * @next: &next &drm_gpuva_op to store the next step + * @ops: &drm_gpuva_ops to walk + * + * This iterator walks over all ops within a given list of operations. It = is + * implemented with list_for_each_safe(), so save against removal of eleme= nts. + */ +#define drm_gpuva_for_each_op_safe(op, next, ops) \ + list_for_each_entry_safe(op, next, &(ops)->list, entry) + +/** + * drm_gpuva_for_each_op_from_reverse - iterate backwards from the given p= oint + * @op: &drm_gpuva_op to assign in each iteration step + * @ops: &drm_gpuva_ops to walk + * + * This iterator walks over all ops within a given list of operations begi= nning + * from the given operation in reverse order. + */ +#define drm_gpuva_for_each_op_from_reverse(op, ops) \ + list_for_each_entry_from_reverse(op, &(ops)->list, entry) + +/** + * drm_gpuva_first_op - returns the first &drm_gpuva_op from &drm_gpuva_ops + * @ops: the &drm_gpuva_ops to get the fist &drm_gpuva_op from + */ +#define drm_gpuva_first_op(ops) \ + list_first_entry(&(ops)->list, struct drm_gpuva_op, entry) + +/** + * drm_gpuva_last_op - returns the last &drm_gpuva_op from &drm_gpuva_ops + * @ops: the &drm_gpuva_ops to get the last &drm_gpuva_op from + */ +#define drm_gpuva_last_op(ops) \ + list_last_entry(&(ops)->list, struct drm_gpuva_op, entry) + +/** + * drm_gpuva_prev_op - previous &drm_gpuva_op in the list + * @op: the current &drm_gpuva_op + */ +#define drm_gpuva_prev_op(op) list_prev_entry(op, entry) + +/** + * drm_gpuva_next_op - next &drm_gpuva_op in the list + * @op: the current &drm_gpuva_op + */ +#define drm_gpuva_next_op(op) list_next_entry(op, entry) + +struct drm_gpuva_ops * +drm_gpuva_sm_map_ops_create(struct drm_gpuva_manager *mgr, + u64 addr, u64 range, + struct drm_gem_object *obj, u64 offset); +struct drm_gpuva_ops * +drm_gpuva_sm_unmap_ops_create(struct drm_gpuva_manager *mgr, + u64 addr, u64 range); + +struct drm_gpuva_ops * +drm_gpuva_prefetch_ops_create(struct drm_gpuva_manager *mgr, + u64 addr, u64 range); + +struct drm_gpuva_ops * +drm_gpuva_gem_unmap_ops_create(struct drm_gpuva_manager *mgr, + struct drm_gem_object *obj); + +void drm_gpuva_ops_free(struct drm_gpuva_manager *mgr, + struct drm_gpuva_ops *ops); + +/** + * struct drm_gpuva_fn_ops - callbacks for split/merge steps + * + * This structure defines the callbacks used by &drm_gpuva_sm_map and + * &drm_gpuva_sm_unmap to provide the split/merge steps for map and unmap + * operations to drivers. + */ +struct drm_gpuva_fn_ops { + /** + * @op_alloc: called when the &drm_gpuva_manager allocates + * a struct drm_gpuva_op + * + * Some drivers may want to embed struct drm_gpuva_op into driver + * specific structures. By implementing this callback drivers can + * allocate memory accordingly. + * + * This callback is optional. + */ + struct drm_gpuva_op *(*op_alloc)(void); + + /** + * @op_free: called when the &drm_gpuva_manager frees a + * struct drm_gpuva_op + * + * Some drivers may want to embed struct drm_gpuva_op into driver + * specific structures. By implementing this callback drivers can + * free the previously allocated memory accordingly. + * + * This callback is optional. + */ + void (*op_free)(struct drm_gpuva_op *op); + + /** + * @sm_map_step: called from &drm_gpuva_sm_map providing the split and + * merge steps + * + * This callback provides a single split / merge step or, if no split + * and merge is indicated, the original map operation. + * + * The &priv pointer is equal to the one drivers pass to + * &drm_gpuva_sm_map. + */ + int (*sm_map_step)(struct drm_gpuva_op *op, void *priv); + + /** + * @sm_unmap_step: called from &drm_gpuva_sm_map providing the split and + * merge steps + * + * This callback provides a single split step or, if no split is + * indicated, the plain unmap operations of the corresponding unmap + * range originally passed to &drm_gpuva_sm_unmap. + * + * The &priv pointer is equal to the one drivers pass to + * &drm_gpuva_sm_unmap. + */ + int (*sm_unmap_step)(struct drm_gpuva_op *op, void *priv); +}; + +int drm_gpuva_sm_map(struct drm_gpuva_manager *mgr, void *priv, + u64 addr, u64 range, + struct drm_gem_object *obj, u64 offset); + +int drm_gpuva_sm_unmap(struct drm_gpuva_manager *mgr, void *priv, + u64 addr, u64 range); + +#endif /* __DRM_GPUVA_MGR_H__ */ --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17B58C6379F for ; Fri, 17 Feb 2023 13:49:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229985AbjBQNts (ORCPT ); Fri, 17 Feb 2023 08:49:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229881AbjBQNtl (ORCPT ); Fri, 17 Feb 2023 08:49:41 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC0486BDDC for ; Fri, 17 Feb 2023 05:48:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641706; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Px4ciz6by/X66jGqheLS55GJ7hc35ETECtAGNQXcKuY=; b=Uv6iEdtiyq4fhwjOLTC0aCsIZPVpieMNSJ8rbwfmC0xi82ALcZnZvoM7nrwLQlOrHS1YTF DNswwVL4pKMYOgR01E8FMZwH4QQWKhx4ViRi1kOff8T08tO9cdKrZNJL4jrzuCH/Sp1wOR E0rq3BBiZsgQEhTG+57CwiP5FWCaF/c= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-424-PeeNG1uNPfulKsXaUSEanw-1; Fri, 17 Feb 2023 08:48:25 -0500 X-MC-Unique: PeeNG1uNPfulKsXaUSEanw-1 Received: by mail-ed1-f72.google.com with SMTP id bd13-20020a056402206d00b004acd97105ffso2028923edb.19 for ; Fri, 17 Feb 2023 05:48:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Px4ciz6by/X66jGqheLS55GJ7hc35ETECtAGNQXcKuY=; b=N5vhx0bk6+0W6BcnzQ7XJjdQxLO4P4khwuA7Rt9ArEUwYgpJrxfh/NuR9ZZ7pCf41z haSSpxH240BZ8aykCfFTpKC13V6uSBxXbAwXLdsUKJNfO2HmDQev9OEZKSib58pSA3qT Lwjq7XrtnQWUPQjrXgU2aoblSY3A7H5Xa/uM8YUwV+8FcSmTDGH2pDEOzxS9WRnXCu5F ZYBd0QwK/3xxgZ9cKP8oci7cLJpK17VEpJWalIRErlvUjM65wxaqZ6oZxkWX9E0AMXZr 8/D5JMO86J1AzNkqVKiShKB4CVCtlJ/bl/iiKEJaFVCxVDyuV7kVPw5UuwzdkUSCwB6r /uew== X-Gm-Message-State: AO0yUKWaX6A1mvYYRbL7oaC+jnVuHOEw+0HOPaKphcrv3h7vMR/Xg5iS Ldcjf69ziTr5EXH+y/hhUp892kMNbCqeb4oXRZA1N2rFby38vvzBz6agFykCM1QRF5+9vH6EcII MZVD595jdovVPt1zoPxdejg7W X-Received: by 2002:a17:906:2c4c:b0:8b1:16b3:303b with SMTP id f12-20020a1709062c4c00b008b116b3303bmr8902646ejh.43.1676641704488; Fri, 17 Feb 2023 05:48:24 -0800 (PST) X-Google-Smtp-Source: AK7set99KelezAcLfESG3NW3a/8bhkPGfJ35zGwm2UnKEgc6MsbYnErOdjepQYokeUptlLv4OQllEg== X-Received: by 2002:a17:906:2c4c:b0:8b1:16b3:303b with SMTP id f12-20020a1709062c4c00b008b116b3303bmr8902627ejh.43.1676641704235; Fri, 17 Feb 2023 05:48:24 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id cf25-20020a170906b2d900b008b132c5ea47sm2144884ejb.153.2023.02.17.05.48.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:48:23 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 06/16] drm: debugfs: provide infrastructure to dump a DRM GPU VA space Date: Fri, 17 Feb 2023 14:48:10 +0100 Message-Id: <20230217134820.14672-1-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This commit adds a function to dump a DRM GPU VA space and a macro for drivers to register the struct drm_info_list 'gpuvas' entry. Most likely, most drivers might maintain one DRM GPU VA space per struct drm_file, but there might also be drivers not having a fixed relation between DRM GPU VA spaces and a DRM core infrastructure, hence we need the indirection via the driver iterating it's maintained DRM GPU VA spaces. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/drm_debugfs.c | 56 +++++++++++++++++++++++++++++++++++ include/drm/drm_debugfs.h | 25 ++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/drivers/gpu/drm/drm_debugfs.c b/drivers/gpu/drm/drm_debugfs.c index 4f643a490dc3..0a8e3fdd5f6f 100644 --- a/drivers/gpu/drm/drm_debugfs.c +++ b/drivers/gpu/drm/drm_debugfs.c @@ -39,6 +39,7 @@ #include #include #include +#include =20 #include "drm_crtc_internal.h" #include "drm_internal.h" @@ -175,6 +176,61 @@ static const struct file_operations drm_debugfs_fops = =3D { .release =3D single_release, }; =20 +/** + * drm_debugfs_gpuva_info - dump the given DRM GPU VA space + * @m: pointer to the &seq_file to write + * @mgr: the &drm_gpuva_manager representing the GPU VA space + * + * Dumps the GPU VA regions and mappings of a given DRM GPU VA manager. + * + * For each DRM GPU VA space drivers should call this function from their + * &drm_info_list's show callback. + * + * Returns: 0 on success, -ENODEV if the &mgr is not initialized + */ +int drm_debugfs_gpuva_info(struct seq_file *m, + struct drm_gpuva_manager *mgr) +{ + DRM_GPUVA_ITER(it, mgr); + DRM_GPUVA_REGION_ITER(__it, mgr); + + if (!mgr->name) + return -ENODEV; + + seq_printf(m, "DRM GPU VA space (%s)\n", mgr->name); + seq_puts (m, "\n"); + seq_puts (m, " VA regions | start | range | e= nd | sparse\n"); + seq_puts (m, "----------------------------------------------------------= --------------------------\n"); + seq_printf(m, " VA space | 0x%016llx | 0x%016llx | 0x%016llx | -\n", + mgr->mm_start, mgr->mm_range, mgr->mm_start + mgr->mm_range); + seq_puts (m, "----------------------------------------------------------= -------------------------\n"); + drm_gpuva_iter_for_each(__it) { + struct drm_gpuva_region *reg =3D __it.reg; + + if (reg =3D=3D &mgr->kernel_alloc_region) { + seq_printf(m, " kernel node | 0x%016llx | 0x%016llx | 0x%016llx | -\n= ", + reg->va.addr, reg->va.range, reg->va.addr + reg->va.range); + continue; + } + + seq_printf(m, " | 0x%016llx | 0x%016llx | 0x%016llx | %s\n", + reg->va.addr, reg->va.range, reg->va.addr + reg->va.range, + reg->sparse ? "true" : "false"); + } + seq_puts(m, "\n"); + seq_puts(m, " VAs | start | range | end = | object | object offset\n"); + seq_puts(m, "------------------------------------------------------------= -------------------------------------------------\n"); + drm_gpuva_iter_for_each(it) { + struct drm_gpuva *va =3D it.va; + + seq_printf(m, " | 0x%016llx | 0x%016llx | 0x%016llx | 0x%016llx | 0x= %016llx\n", + va->va.addr, va->va.range, va->va.addr + va->va.range, + (u64)va->gem.obj, va->gem.offset); + } + + return 0; +} +EXPORT_SYMBOL(drm_debugfs_gpuva_info); =20 /** * drm_debugfs_create_files - Initialize a given set of debugfs files for = DRM diff --git a/include/drm/drm_debugfs.h b/include/drm/drm_debugfs.h index 7616f457ce70..cb2c1956a214 100644 --- a/include/drm/drm_debugfs.h +++ b/include/drm/drm_debugfs.h @@ -34,6 +34,22 @@ =20 #include #include + +#include + +/** + * DRM_DEBUGFS_GPUVA_INFO - &drm_info_list entry to dump a GPU VA space + * @show: the &drm_info_list's show callback + * @data: driver private data + * + * Drivers should use this macro to define a &drm_info_list entry to provi= de a + * debugfs file for dumping the GPU VA space regions and mappings. + * + * For each DRM GPU VA space drivers should call drm_debugfs_gpuva_info() = from + * their @show callback. + */ +#define DRM_DEBUGFS_GPUVA_INFO(show, data) {"gpuvas", show, DRIVER_GEM_GPU= VA, data} + /** * struct drm_info_list - debugfs info list entry * @@ -134,6 +150,9 @@ void drm_debugfs_add_file(struct drm_device *dev, const= char *name, =20 void drm_debugfs_add_files(struct drm_device *dev, const struct drm_debugfs_info *files, int count); + +int drm_debugfs_gpuva_info(struct seq_file *m, + struct drm_gpuva_manager *mgr); #else static inline void drm_debugfs_create_files(const struct drm_info_list *fi= les, int count, struct dentry *root, @@ -155,6 +174,12 @@ static inline void drm_debugfs_add_files(struct drm_de= vice *dev, const struct drm_debugfs_info *files, int count) {} + +static inline int drm_debugfs_gpuva_info(struct seq_file *m, + struct drm_gpuva_manager *mgr) +{ + return 0; +} #endif =20 #endif /* _DRM_DEBUGFS_H_ */ --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18B70C05027 for ; Fri, 17 Feb 2023 13:50:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230005AbjBQNtv (ORCPT ); Fri, 17 Feb 2023 08:49:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229882AbjBQNtl (ORCPT ); Fri, 17 Feb 2023 08:49:41 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 863AB6B33F for ; Fri, 17 Feb 2023 05:48:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641710; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eEmQDOQyU6gF5gMBW9RNpV/N/n3J9qaRJsJk6Xv6fHM=; b=TD3snQ7QJ6TuuUURI4fZPzWLwZe+IHFdnlDU+8MYRMkgITubPXu8/HujDjYpXhz7dcnGMy Z6xqahmnI/+jPUF9O5aS6A0fxhu6O/QjsW9qGyP7qh6nBERz+Ww7Ci1JrK7UZhFDVTOoIG SHYjhgGPQk4sKtXtelqS3VcVXKDCA1w= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-443-H4XjKId2NAaQOKmKcnrlvw-1; Fri, 17 Feb 2023 08:48:29 -0500 X-MC-Unique: H4XjKId2NAaQOKmKcnrlvw-1 Received: by mail-ed1-f71.google.com with SMTP id cz5-20020a0564021ca500b004a6c2f6a226so1245436edb.15 for ; Fri, 17 Feb 2023 05:48:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eEmQDOQyU6gF5gMBW9RNpV/N/n3J9qaRJsJk6Xv6fHM=; b=fuP0nPMw1GVrnziC2e/Yjtb+wpc6T148MCDKTet6kk7EOPrz8yWlWVhNL4Q3KDPPsN 7OjJfivfTpdppxtgj/CBc3v84xdG7Ds/NkqHJwJMyVBPZettlnoK1LaUbQ8D41P6Itn8 z4LtvJOAZYe623nbsLp4SuamfY6Se1ZKM1fp0yh8KCYJwclEvdndH9NBuOiD/5K9ZDqX z1HxKRfro8V+ApXSBfrQb+BvUPZf/jawsW28BaJqjZD1LH9xZsd4sxu/qv2rQ5o4ZRiX Mo+21kfIz1xM8JTJIDfbOQzjJZn3q/40a6ESF6wOIdV2vHJQYWh9w8BWd+HrFGrhXY4R +azA== X-Gm-Message-State: AO0yUKUelRoOEbWOwqI/CeNSfvpfp31bK24IK+VtNIJxXEn0wky9Yn4P pCm9wCooF3U3ezVR73Ih/pgBMrqfk5gA2HeCPT8ss4OEcTm0QCHEsneLOCwvrhTgzibVg1XB9Wk sOtVzcNfU1jEpYc1JOV0UYodQ X-Received: by 2002:a17:906:5e45:b0:861:7a02:1046 with SMTP id b5-20020a1709065e4500b008617a021046mr9000413eju.37.1676641708467; Fri, 17 Feb 2023 05:48:28 -0800 (PST) X-Google-Smtp-Source: AK7set8cnsFQ2/2nbfdSQlY0qUbNsdNsHcvEXcTDgO7kDDv/fQR13lIJp0sfctJpGRYxNRgg25e5MA== X-Received: by 2002:a17:906:5e45:b0:861:7a02:1046 with SMTP id b5-20020a1709065e4500b008617a021046mr9000388eju.37.1676641708174; Fri, 17 Feb 2023 05:48:28 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id bt21-20020a170906b15500b008b13259093csm2165027ejb.107.2023.02.17.05.48.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:48:27 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich , Dave Airlie Subject: [PATCH drm-next v2 07/16] drm/nouveau: new VM_BIND uapi interfaces Date: Fri, 17 Feb 2023 14:48:11 +0100 Message-Id: <20230217134820.14672-2-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This commit provides the interfaces for the new UAPI motivated by the Vulkan API. It allows user mode drivers (UMDs) to: 1) Initialize a GPU virtual address (VA) space via the new DRM_IOCTL_NOUVEAU_VM_INIT ioctl. UMDs can provide a kernel reserved VA area. 2) Bind and unbind GPU VA space mappings via the new DRM_IOCTL_NOUVEAU_VM_BIND ioctl. 3) Execute push buffers with the new DRM_IOCTL_NOUVEAU_EXEC ioctl. Both, DRM_IOCTL_NOUVEAU_VM_BIND and DRM_IOCTL_NOUVEAU_EXEC support asynchronous processing with DRM syncobjs as synchronization mechanism. The default DRM_IOCTL_NOUVEAU_VM_BIND is synchronous processing, DRM_IOCTL_NOUVEAU_EXEC supports asynchronous processing only. Co-authored-by: Dave Airlie Signed-off-by: Danilo Krummrich --- Documentation/gpu/driver-uapi.rst | 8 ++ include/uapi/drm/nouveau_drm.h | 220 ++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+) diff --git a/Documentation/gpu/driver-uapi.rst b/Documentation/gpu/driver-u= api.rst index 4411e6919a3d..9c7ca6e33a68 100644 --- a/Documentation/gpu/driver-uapi.rst +++ b/Documentation/gpu/driver-uapi.rst @@ -6,3 +6,11 @@ drm/i915 uAPI =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 .. kernel-doc:: include/uapi/drm/i915_drm.h + +drm/nouveau uAPI +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +VM_BIND / EXEC uAPI +------------------- + +.. kernel-doc:: include/uapi/drm/nouveau_drm.h diff --git a/include/uapi/drm/nouveau_drm.h b/include/uapi/drm/nouveau_drm.h index 853a327433d3..dbe0afbd858a 100644 --- a/include/uapi/drm/nouveau_drm.h +++ b/include/uapi/drm/nouveau_drm.h @@ -126,6 +126,220 @@ struct drm_nouveau_gem_cpu_fini { __u32 handle; }; =20 +/** + * struct drm_nouveau_sync - sync object + * + * This structure serves as synchronization mechanism for (potentially) + * asynchronous operations such as EXEC or VM_BIND. + */ +struct drm_nouveau_sync { + /** + * @flags: the flags for a sync object + * + * The first 8 bits are used to determine the type of the sync object. + */ + __u32 flags; +#define DRM_NOUVEAU_SYNC_SYNCOBJ 0x0 +#define DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ 0x1 +#define DRM_NOUVEAU_SYNC_TYPE_MASK 0xf + /** + * @handle: the handle of the sync object + */ + __u32 handle; + /** + * @timeline_value: + * + * The timeline point of the sync object in case the syncobj is of + * type DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ. + */ + __u64 timeline_value; +}; + +/** + * struct drm_nouveau_vm_init - GPU VA space init structure + * + * Used to initialize the GPU's VA space for a user client, telling the ke= rnel + * which portion of the VA space is managed by the UMD and kernel respecti= vely. + */ +struct drm_nouveau_vm_init { + /** + * @unmanaged_addr: start address of the kernel managed VA space region + */ + __u64 unmanaged_addr; + /** + * @unmanaged_size: size of the kernel managed VA space region in bytes + */ + __u64 unmanaged_size; +}; + +/** + * struct drm_nouveau_vm_bind_op - VM_BIND operation + * + * This structure represents a single VM_BIND operation. UMDs should pass + * an array of this structure via struct drm_nouveau_vm_bind's &op_ptr fie= ld. + */ +struct drm_nouveau_vm_bind_op { + /** + * @op: the operation type + */ + __u32 op; +/** + * @DRM_NOUVEAU_VM_BIND_OP_ALLOC: + * + * The alloc operation is used to reserve a VA space region within the GPU= 's VA + * space. Optionally, the &DRM_NOUVEAU_VM_BIND_SPARSE flag can be passed to + * instruct the kernel to create sparse mappings for the given region. + */ +#define DRM_NOUVEAU_VM_BIND_OP_ALLOC 0x0 +/** + * @DRM_NOUVEAU_VM_BIND_OP_FREE: Free a reserved VA space region. + */ +#define DRM_NOUVEAU_VM_BIND_OP_FREE 0x1 +/** + * @DRM_NOUVEAU_VM_BIND_OP_MAP: + * + * Map a GEM object to the GPU's VA space. The mapping must be fully enclo= sed by + * a previously allocated VA space region. If the region is sparse, existi= ng + * sparse mappings are overwritten. + */ +#define DRM_NOUVEAU_VM_BIND_OP_MAP 0x2 +/** + * @DRM_NOUVEAU_VM_BIND_OP_UNMAP: + * + * Unmap an existing mapping in the GPU's VA space. If the region the mapp= ing + * is located in is a sparse region, new sparse mappings are created where= the + * unmapped (memory backed) mapping was mapped previously. + */ +#define DRM_NOUVEAU_VM_BIND_OP_UNMAP 0x3 + /** + * @flags: the flags for a &drm_nouveau_vm_bind_op + */ + __u32 flags; +/** + * @DRM_NOUVEAU_VM_BIND_SPARSE: + * + * Indicates that an allocated VA space region should be sparse. + */ +#define DRM_NOUVEAU_VM_BIND_SPARSE (1 << 8) + /** + * @handle: the handle of the DRM GEM object to map + */ + __u32 handle; + /** + * @pad: 32 bit padding, should be 0 + */ + __u32 pad; + /** + * @addr: + * + * the address the VA space region or (memory backed) mapping should be m= apped to + */ + __u64 addr; + /** + * @bo_offset: the offset within the BO backing the mapping + */ + __u64 bo_offset; + /** + * @range: the size of the requested mapping in bytes + */ + __u64 range; +}; + +/** + * struct drm_nouveau_vm_bind - structure for DRM_IOCTL_NOUVEAU_VM_BIND + */ +struct drm_nouveau_vm_bind { + /** + * @op_count: the number of &drm_nouveau_vm_bind_op + */ + __u32 op_count; + /** + * @flags: the flags for a &drm_nouveau_vm_bind ioctl + */ + __u32 flags; +/** + * @DRM_NOUVEAU_VM_BIND_RUN_ASYNC: + * + * Indicates that the given VM_BIND operation should be executed asynchron= ously + * by the kernel. + * + * If this flag is not supplied the kernel executes the associated operati= ons + * synchronously and doesn't accept any &drm_nouveau_sync objects. + */ +#define DRM_NOUVEAU_VM_BIND_RUN_ASYNC 0x1 + /** + * @wait_count: the number of wait &drm_nouveau_syncs + */ + __u32 wait_count; + /** + * @sig_count: the number of &drm_nouveau_syncs to signal when finished + */ + __u32 sig_count; + /** + * @wait_ptr: pointer to &drm_nouveau_syncs to wait for + */ + __u64 wait_ptr; + /** + * @sig_ptr: pointer to &drm_nouveau_syncs to signal when finished + */ + __u64 sig_ptr; + /** + * @op_ptr: pointer to the &drm_nouveau_vm_bind_ops to execute + */ + __u64 op_ptr; +}; + +/** + * struct drm_nouveau_exec_push - EXEC push operation + * + * This structure represents a single EXEC push operation. UMDs should pas= s an + * array of this structure via struct drm_nouveau_exec's &push_ptr field. + */ +struct drm_nouveau_exec_push { + /** + * @va: the virtual address of the push buffer mapping + */ + __u64 va; + /** + * @va_len: the length of the push buffer mapping + */ + __u64 va_len; +}; + +/** + * struct drm_nouveau_exec - structure for DRM_IOCTL_NOUVEAU_EXEC + */ +struct drm_nouveau_exec { + /** + * @channel: the channel to execute the push buffer in + */ + __u32 channel; + /** + * @push_count: the number of &drm_nouveau_exec_push ops + */ + __u32 push_count; + /** + * @wait_count: the number of wait &drm_nouveau_syncs + */ + __u32 wait_count; + /** + * @sig_count: the number of &drm_nouveau_syncs to signal when finished + */ + __u32 sig_count; + /** + * @wait_ptr: pointer to &drm_nouveau_syncs to wait for + */ + __u64 wait_ptr; + /** + * @sig_ptr: pointer to &drm_nouveau_syncs to signal when finished + */ + __u64 sig_ptr; + /** + * @push_ptr: pointer to &drm_nouveau_exec_push ops + */ + __u64 push_ptr; +}; + #define DRM_NOUVEAU_GETPARAM 0x00 /* deprecated */ #define DRM_NOUVEAU_SETPARAM 0x01 /* deprecated */ #define DRM_NOUVEAU_CHANNEL_ALLOC 0x02 /* deprecated */ @@ -136,6 +350,9 @@ struct drm_nouveau_gem_cpu_fini { #define DRM_NOUVEAU_NVIF 0x07 #define DRM_NOUVEAU_SVM_INIT 0x08 #define DRM_NOUVEAU_SVM_BIND 0x09 +#define DRM_NOUVEAU_VM_INIT 0x10 +#define DRM_NOUVEAU_VM_BIND 0x11 +#define DRM_NOUVEAU_EXEC 0x12 #define DRM_NOUVEAU_GEM_NEW 0x40 #define DRM_NOUVEAU_GEM_PUSHBUF 0x41 #define DRM_NOUVEAU_GEM_CPU_PREP 0x42 @@ -197,6 +414,9 @@ struct drm_nouveau_svm_bind { #define DRM_IOCTL_NOUVEAU_GEM_CPU_FINI DRM_IOW (DRM_COMMAND_BASE + D= RM_NOUVEAU_GEM_CPU_FINI, struct drm_nouveau_gem_cpu_fini) #define DRM_IOCTL_NOUVEAU_GEM_INFO DRM_IOWR(DRM_COMMAND_BASE + D= RM_NOUVEAU_GEM_INFO, struct drm_nouveau_gem_info) =20 +#define DRM_IOCTL_NOUVEAU_VM_INIT DRM_IOWR(DRM_COMMAND_BASE + D= RM_NOUVEAU_VM_INIT, struct drm_nouveau_vm_init) +#define DRM_IOCTL_NOUVEAU_VM_BIND DRM_IOWR(DRM_COMMAND_BASE + D= RM_NOUVEAU_VM_BIND, struct drm_nouveau_vm_bind) +#define DRM_IOCTL_NOUVEAU_EXEC DRM_IOWR(DRM_COMMAND_BASE + D= RM_NOUVEAU_EXEC, struct drm_nouveau_exec) #if defined(__cplusplus) } #endif --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 005EEC64ED6 for ; Fri, 17 Feb 2023 13:50:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229957AbjBQNuF (ORCPT ); Fri, 17 Feb 2023 08:50:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229952AbjBQNtn (ORCPT ); Fri, 17 Feb 2023 08:49:43 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7B736BDF9 for ; Fri, 17 Feb 2023 05:48:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641714; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cfAnd4OS8K7FlDcC4KqE1N+eZSmvaMlTDnF99DGXOtY=; b=GpSXW1z8hugOXE7CU/IzKYUotWPWSII1X3LScVpHZz5O2rAJcT7oIcHU5APj+7ZOrGatkC CU9JeT/qKLG8knjrVs505f6wFCPdnVD/bLEQm7G2DezHPacWxyrIveFV4UZSeOYCB8Nooo Wd17KAWYpbL7BcBWPpoMqa581y/RPj4= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-250-NApthtffPT2PrHhBYo3DOg-1; Fri, 17 Feb 2023 08:48:33 -0500 X-MC-Unique: NApthtffPT2PrHhBYo3DOg-1 Received: by mail-ed1-f71.google.com with SMTP id j10-20020a05640211ca00b004acbf564d75so1094861edw.5 for ; Fri, 17 Feb 2023 05:48:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cfAnd4OS8K7FlDcC4KqE1N+eZSmvaMlTDnF99DGXOtY=; b=X2ZjsZpBte1P0OmV/VPsdoe0aZH7Mw+UHkUfzngBJGoBWwls/394DoyP0wuHXDUt3j R9t+joqRI5vUpeAZ0VFJSO4Qu/QT2VrEhnXsgFuvWz5JD2qW+o9n9tvRg5rVP6mNSzeV f9bIrZirrPzaxlw9Nd00TfqAPuiIsJFZS8Fkme/fevtL2zzG2T289setAVmKrXOeLvuD 1kADzYeLNHT5WdwCYwtKRzSfbYl3wflwIHnEHa7iiZWGiKeNvTNt8xBDaxDyokyc8QjR 9hbBdXULLf7lPHmfV3fgWOnWeLA3nllEzzBZ0Aowc/gsaLmnhOIkF1oKJMj5HIMys0Lh Dc7A== X-Gm-Message-State: AO0yUKVJD4qnmfiD9Ldb8TjjBYBGAC/qPTtKxD71jqlywsgJPyEbYp7f MWoDuhM4U6cnxQeI6tB+RaNvkLC9DVFJwAhgSiCqtugTUAExaneCOgu5mBIUA2Z67a3aw0RKe33 8JFIzc6qADQJMgjCx/EfV0R0J X-Received: by 2002:a17:907:212d:b0:882:2027:c8e2 with SMTP id qo13-20020a170907212d00b008822027c8e2mr103434ejb.56.1676641712433; Fri, 17 Feb 2023 05:48:32 -0800 (PST) X-Google-Smtp-Source: AK7set9pimoemSkJ2kQZQvRKaPf0MC6QFTSypVsludSNFNxOV7/3olSpQeL9Z0Z58W/+NW3BXT3ovA== X-Received: by 2002:a17:907:212d:b0:882:2027:c8e2 with SMTP id qo13-20020a170907212d00b008822027c8e2mr103423ejb.56.1676641712215; Fri, 17 Feb 2023 05:48:32 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id qn20-20020a170907211400b007a9c3831409sm2145357ejb.137.2023.02.17.05.48.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:48:31 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 08/16] drm/nouveau: get vmm via nouveau_cli_vmm() Date: Fri, 17 Feb 2023 14:48:12 +0100 Message-Id: <20230217134820.14672-3-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Provide a getter function for the client's current vmm context. Since we'll add a new (u)vmm context for UMD bindings in subsequent commits, this will keep the code clean. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nouveau/nouveau_bo.c | 2 +- drivers/gpu/drm/nouveau/nouveau_chan.c | 2 +- drivers/gpu/drm/nouveau/nouveau_drv.h | 9 +++++++++ drivers/gpu/drm/nouveau/nouveau_gem.c | 6 +++--- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau= /nouveau_bo.c index 288eebc70a67..f3039c1f87c9 100644 --- a/drivers/gpu/drm/nouveau/nouveau_bo.c +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c @@ -204,7 +204,7 @@ nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size, in= t *align, u32 domain, struct nouveau_drm *drm =3D cli->drm; struct nouveau_bo *nvbo; struct nvif_mmu *mmu =3D &cli->mmu; - struct nvif_vmm *vmm =3D cli->svm.cli ? &cli->svm.vmm : &cli->vmm.vmm; + struct nvif_vmm *vmm =3D &nouveau_cli_vmm(cli)->vmm; int i, pi =3D -1; =20 if (!*size) { diff --git a/drivers/gpu/drm/nouveau/nouveau_chan.c b/drivers/gpu/drm/nouve= au/nouveau_chan.c index e648ecd0c1a0..1068abe41024 100644 --- a/drivers/gpu/drm/nouveau/nouveau_chan.c +++ b/drivers/gpu/drm/nouveau/nouveau_chan.c @@ -148,7 +148,7 @@ nouveau_channel_prep(struct nouveau_drm *drm, struct nv= if_device *device, =20 chan->device =3D device; chan->drm =3D drm; - chan->vmm =3D cli->svm.cli ? &cli->svm : &cli->vmm; + chan->vmm =3D nouveau_cli_vmm(cli); atomic_set(&chan->killed, 0); =20 /* allocate memory for dma push buffer */ diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouvea= u/nouveau_drv.h index b5de312a523f..81350e685b50 100644 --- a/drivers/gpu/drm/nouveau/nouveau_drv.h +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h @@ -112,6 +112,15 @@ struct nouveau_cli_work { struct dma_fence_cb cb; }; =20 +static inline struct nouveau_vmm * +nouveau_cli_vmm(struct nouveau_cli *cli) +{ + if (cli->svm.cli) + return &cli->svm; + + return &cli->vmm; +} + void nouveau_cli_work_queue(struct nouveau_cli *, struct dma_fence *, struct nouveau_cli_work *); =20 diff --git a/drivers/gpu/drm/nouveau/nouveau_gem.c b/drivers/gpu/drm/nouvea= u/nouveau_gem.c index f77e44958037..08689ced4f6a 100644 --- a/drivers/gpu/drm/nouveau/nouveau_gem.c +++ b/drivers/gpu/drm/nouveau/nouveau_gem.c @@ -103,7 +103,7 @@ nouveau_gem_object_open(struct drm_gem_object *gem, str= uct drm_file *file_priv) struct nouveau_bo *nvbo =3D nouveau_gem_object(gem); struct nouveau_drm *drm =3D nouveau_bdev(nvbo->bo.bdev); struct device *dev =3D drm->dev->dev; - struct nouveau_vmm *vmm =3D cli->svm.cli ? &cli->svm : &cli->vmm; + struct nouveau_vmm *vmm =3D nouveau_cli_vmm(cli); struct nouveau_vma *vma; int ret; =20 @@ -180,7 +180,7 @@ nouveau_gem_object_close(struct drm_gem_object *gem, st= ruct drm_file *file_priv) struct nouveau_bo *nvbo =3D nouveau_gem_object(gem); struct nouveau_drm *drm =3D nouveau_bdev(nvbo->bo.bdev); struct device *dev =3D drm->dev->dev; - struct nouveau_vmm *vmm =3D cli->svm.cli ? &cli->svm : & cli->vmm; + struct nouveau_vmm *vmm =3D nouveau_cli_vmm(cli); struct nouveau_vma *vma; int ret; =20 @@ -269,7 +269,7 @@ nouveau_gem_info(struct drm_file *file_priv, struct drm= _gem_object *gem, { struct nouveau_cli *cli =3D nouveau_cli(file_priv); struct nouveau_bo *nvbo =3D nouveau_gem_object(gem); - struct nouveau_vmm *vmm =3D cli->svm.cli ? &cli->svm : &cli->vmm; + struct nouveau_vmm *vmm =3D nouveau_cli_vmm(cli); struct nouveau_vma *vma; =20 if (is_power_of_2(nvbo->valid_domains)) --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66695C6379F for ; Fri, 17 Feb 2023 13:50:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230033AbjBQNuO (ORCPT ); Fri, 17 Feb 2023 08:50:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230021AbjBQNtx (ORCPT ); Fri, 17 Feb 2023 08:49:53 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53F2C6C013 for ; Fri, 17 Feb 2023 05:48:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s63r6ewLpHy8dX6pfqzPL7Kskgx+EPYod2Ty9Ts4erk=; b=gmOGNouJuIR1YvO5H/bsds32Cl4TgmIpjJr9527PefFMq+tVL8E+aPEcFp+nYNPpfCNNWS CJesaA223aI958D2ldpURde1ibn+PyFW0pqJwS/AiZPKJRsqcw3YuVqPfb8Iyx/RB4+BEv UCdrntiXBRPvN560dEohEPh7RUkLS4U= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-175-Wg97RsO2OtSzfbJ7IEte-w-1; Fri, 17 Feb 2023 08:48:38 -0500 X-MC-Unique: Wg97RsO2OtSzfbJ7IEte-w-1 Received: by mail-ed1-f72.google.com with SMTP id q12-20020a056402518c00b004ace822b750so1950481edd.20 for ; Fri, 17 Feb 2023 05:48:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=s63r6ewLpHy8dX6pfqzPL7Kskgx+EPYod2Ty9Ts4erk=; b=8OTD+DDSu1Orva9ZKwX0DpnZTMXf3RLljvB61fuVupA3DMTkxIF1pcMf5p1euLmG+Z RyqTaCsBlr+bQ80QOnzma4fugY93BrUc5tgXj4wZRXEFOfTn0zNSW182ouzzkIMK9Lyt rZxGNUJ2ZqCJMs59EbzFaTERSm4ytfGKLDjeUCuojXc6hqN1MbQS6DamBqOrQro42J21 J0nI7kc7js6M02sNIG6EpjSfGf5AGuaokLStIriT1JCsAb476+DwgDA5cS5gNIZ69C94 GJz9PQ7/NNQEX8SykxS2WdtJDmPCtmf0apxH6+Vouh76adqv3QOmFAZFEXjMiRraaZMe Icmg== X-Gm-Message-State: AO0yUKVI0XDwmkRyqn4HvL1ELKwTrhCqcddes23Y1Zj4M7K5c/iAXPuP /iZpSAcT5rzXTjXMOJ/uGPi5wrbxdku/ViS6bU1r77BOmm4vI2WAW0SSHNFArzxk1oI1xWLazTt tXGLbasHZPFZDCkygStqlfHWF X-Received: by 2002:a17:906:9750:b0:8b3:946d:51c8 with SMTP id o16-20020a170906975000b008b3946d51c8mr1973103ejy.29.1676641717143; Fri, 17 Feb 2023 05:48:37 -0800 (PST) X-Google-Smtp-Source: AK7set8Vb+Eh70SMrWH0gbi9pDuylvuAxYJRJaM1j4o+Rm2VHpNfcVf+Ka0ZZS+k/kpmT6aZ0ke53A== X-Received: by 2002:a17:906:9750:b0:8b3:946d:51c8 with SMTP id o16-20020a170906975000b008b3946d51c8mr1973082ejy.29.1676641717002; Fri, 17 Feb 2023 05:48:37 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id a15-20020a509b4f000000b004acc7202074sm2288777edj.16.2023.02.17.05.48.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:48:36 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 09/16] drm/nouveau: bo: initialize GEM GPU VA interface Date: Fri, 17 Feb 2023 14:48:13 +0100 Message-Id: <20230217134820.14672-4-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Initialize the GEM's DRM GPU VA manager interface in preparation for the (u)vmm implementation, provided by subsequent commits, to make use of it. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nouveau/nouveau_bo.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau= /nouveau_bo.c index f3039c1f87c9..bf6984c8754c 100644 --- a/drivers/gpu/drm/nouveau/nouveau_bo.c +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c @@ -215,11 +215,14 @@ nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size, = int *align, u32 domain, nvbo =3D kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL); if (!nvbo) return ERR_PTR(-ENOMEM); + INIT_LIST_HEAD(&nvbo->head); INIT_LIST_HEAD(&nvbo->entry); INIT_LIST_HEAD(&nvbo->vma_list); nvbo->bo.bdev =3D &drm->ttm.bdev; =20 + drm_gem_gpuva_init(&nvbo->bo.base); + /* This is confusing, and doesn't actually mean we want an uncached * mapping, but is what NOUVEAU_GEM_DOMAIN_COHERENT gets translated * into in nouveau_gem_new(). --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF3E1C6379F for ; Fri, 17 Feb 2023 13:50:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229966AbjBQNuU (ORCPT ); Fri, 17 Feb 2023 08:50:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229649AbjBQNuC (ORCPT ); Fri, 17 Feb 2023 08:50:02 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06E0E6C026 for ; Fri, 17 Feb 2023 05:48:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641724; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qV+blR6Tkr/sKsOr7dEvAgWdCXkrbGqs9gehh/nqNIE=; b=PVafClIyLLIR4LgNXgrgs4JEjSaambexZ7quQE+X5NcyJIjUt1g5Tjz3U5YDYr0Ti8AW8Q Q5phyfqqn4pEzFUb5qKypZ6Uwn1eDwlMvs9Qwm0wQ4jtLl5Vn/3fGt15IUnGxUUVOQVaGs 9eU0Sb85e5ATkEoeSslQFVmVbnOfvBg= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-628-oVArtSvdPSKs_bnFnoVC_g-1; Fri, 17 Feb 2023 08:48:42 -0500 X-MC-Unique: oVArtSvdPSKs_bnFnoVC_g-1 Received: by mail-ed1-f71.google.com with SMTP id ee29-20020a056402291d00b0049ec3a108beso1653275edb.7 for ; Fri, 17 Feb 2023 05:48:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qV+blR6Tkr/sKsOr7dEvAgWdCXkrbGqs9gehh/nqNIE=; b=okzy/bMvZmt0dDMBbrb4owOadwjWEFZqkLxfSQ31gZuCHe8m0/dcQ6zmUw6/pj4sBG 7DgTKjPoA88FjlvZKT3UC9BMzHx0FsjhcrFW3Cksx0H4Jj4el4KWn0xqU+Zo8W9mjnL7 eQbx6m2BfDdA4ZGJRLFIZnj9HZeo5PukL4jAt1GUusDOagi8IaPbu0J6Nf0CY40FAl4V /8biGajAENuYAlTjf1UcgpZ/184HZI0rfNYiMhrzpjmdiuH5ihNIaJ/chhF0XOC/N0gF akSt667jfYhsp76ReXRGX9QV7dMiyqAElMeecFsg9p3Nkjra+X2D3AmmXXOBCB5YLi73 +m0Q== X-Gm-Message-State: AO0yUKWtu+CliNe3GAEh7FMVe1RGAKfGZY7XEdqB48U8eoxWpu+AYvbM RPvGm6rgs+J5eu0eWqSA3R7454nKPe3r+5VL7V1foEV8Z3I1QsRglSRj+LqZVZSVvRgb/mRdwGa Oy6RwqnUlNqqQFtUrdC2d75P/ X-Received: by 2002:a05:6402:457:b0:4ad:7640:ff47 with SMTP id p23-20020a056402045700b004ad7640ff47mr3317196edw.13.1676641721926; Fri, 17 Feb 2023 05:48:41 -0800 (PST) X-Google-Smtp-Source: AK7set8TQtlKkchdYFDpX6esHsmxmbuVnZmBvwTb0vwG2PSeRr7Qepshcx1saI0cWGGhV/p6T+nLUA== X-Received: by 2002:a05:6402:457:b0:4ad:7640:ff47 with SMTP id p23-20020a056402045700b004ad7640ff47mr3317181edw.13.1676641721700; Fri, 17 Feb 2023 05:48:41 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id v22-20020a509556000000b004a23558f01fsm2348675eda.43.2023.02.17.05.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:48:40 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 10/16] drm/nouveau: move usercopy helpers to nouveau_drv.h Date: Fri, 17 Feb 2023 14:48:14 +0100 Message-Id: <20230217134820.14672-5-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move the usercopy helpers to a common driver header file to make it usable for the new API added in subsequent commits. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nouveau/nouveau_drv.h | 26 ++++++++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_gem.c | 26 -------------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouvea= u/nouveau_drv.h index 81350e685b50..20a7f31b9082 100644 --- a/drivers/gpu/drm/nouveau/nouveau_drv.h +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h @@ -130,6 +130,32 @@ nouveau_cli(struct drm_file *fpriv) return fpriv ? fpriv->driver_priv : NULL; } =20 +static inline void +u_free(void *addr) +{ + kvfree(addr); +} + +static inline void * +u_memcpya(uint64_t user, unsigned nmemb, unsigned size) +{ + void *mem; + void __user *userptr =3D (void __force __user *)(uintptr_t)user; + + size *=3D nmemb; + + mem =3D kvmalloc(size, GFP_KERNEL); + if (!mem) + return ERR_PTR(-ENOMEM); + + if (copy_from_user(mem, userptr, size)) { + u_free(mem); + return ERR_PTR(-EFAULT); + } + + return mem; +} + #include #include =20 diff --git a/drivers/gpu/drm/nouveau/nouveau_gem.c b/drivers/gpu/drm/nouvea= u/nouveau_gem.c index 08689ced4f6a..4369c8dc8b5b 100644 --- a/drivers/gpu/drm/nouveau/nouveau_gem.c +++ b/drivers/gpu/drm/nouveau/nouveau_gem.c @@ -613,32 +613,6 @@ nouveau_gem_pushbuf_validate(struct nouveau_channel *c= han, return 0; } =20 -static inline void -u_free(void *addr) -{ - kvfree(addr); -} - -static inline void * -u_memcpya(uint64_t user, unsigned nmemb, unsigned size) -{ - void *mem; - void __user *userptr =3D (void __force __user *)(uintptr_t)user; - - size *=3D nmemb; - - mem =3D kvmalloc(size, GFP_KERNEL); - if (!mem) - return ERR_PTR(-ENOMEM); - - if (copy_from_user(mem, userptr, size)) { - u_free(mem); - return ERR_PTR(-EFAULT); - } - - return mem; -} - static int nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli, struct drm_nouveau_gem_pushbuf *req, --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 176FBC05027 for ; Fri, 17 Feb 2023 13:50:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230060AbjBQNuR (ORCPT ); Fri, 17 Feb 2023 08:50:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230059AbjBQNuA (ORCPT ); Fri, 17 Feb 2023 08:50:00 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 803355ECBB for ; Fri, 17 Feb 2023 05:48:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641728; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d5/2RnRF2JWHGpr9RK4XcaUSwl3Atw4selg5mVgZ4/g=; b=FPTy6rOidvU//z9jhStEcDnYX/OXv5VpLvcEi7lg+IvNTBg9wKtlsZkJ+a3pqPKvu/oLDA 6CT1H+S65eKrJw1hvUHRbP2ePMPbOqHGOL7pehW+TxvxE3kOEFpjfz4hSn0TeK8InUgUPM PESuhjfkCENjDXyZV3CwZcYxvC+xJZc= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-439-Bp_7Y2vFMmmbhmIrc9CNFA-1; Fri, 17 Feb 2023 08:48:47 -0500 X-MC-Unique: Bp_7Y2vFMmmbhmIrc9CNFA-1 Received: by mail-ed1-f69.google.com with SMTP id v3-20020a056402174300b004aab6614de9so1104559edx.6 for ; Fri, 17 Feb 2023 05:48:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d5/2RnRF2JWHGpr9RK4XcaUSwl3Atw4selg5mVgZ4/g=; b=jhRcX3wCif0B+R2gbCrvBwGsCXzfbPM4Aj5/GmGiWWmQEsi75zT+9eyqmGw/w3APsA Ir+2EZJfvRop4PAAg7nMDXQDYri+H+np6WHfyIM/Z+MZ++yNxw+8Ub8CnUXFWmODHcOf pzKKY8HBTbf1oXasxIHfb5y/i5VRwJSF4qk71QUZaKXGsO0Wxe+3U9oe9aKkReRfBQEw TvjA7R+g+3owR54EpPqOnljpIATGd/1FCPuUjdhWaDVEOnaji+2AjX1GoyrAMKMCHDIA qvnm/aXz5q5p9+24zBEmoHY2oP3qbfiOC3ulBQWbpSHj8okBtA8G1i2pkZwjXVr7W4FH CWWQ== X-Gm-Message-State: AO0yUKWEorDAs1iH50m416Q3SNkcYmFq5ORkMYiwr5s0djcVBW78E8pz p5UB+9CX5LEk6CArMUqFvNYKseSWsTrcjSdeFPuNLaxm6pScdla/QAA7j5hf/levEcqI6FIZIqw gMKI3wbET2DyKCc5oFiz3EC9E X-Received: by 2002:a17:907:72c1:b0:8b1:7569:ad58 with SMTP id du1-20020a17090772c100b008b17569ad58mr4848792ejc.2.1676641726468; Fri, 17 Feb 2023 05:48:46 -0800 (PST) X-Google-Smtp-Source: AK7set8dScSkWfRlq0RX8QSswoAOat82FsG+rCLCZ5wYVMw47uLr1CKX3blizMd0kItf9w+BCuGUrQ== X-Received: by 2002:a17:907:72c1:b0:8b1:7569:ad58 with SMTP id du1-20020a17090772c100b008b17569ad58mr4848763ejc.2.1676641726232; Fri, 17 Feb 2023 05:48:46 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id l21-20020a50d6d5000000b004aab36ad060sm2302248edj.92.2023.02.17.05.48.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:48:45 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 11/16] drm/nouveau: fence: fail to emit when fence context is killed Date: Fri, 17 Feb 2023 14:48:15 +0100 Message-Id: <20230217134820.14672-6-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The new VM_BIND UAPI implementation introduced in subsequent commits will allow asynchronous jobs processing push buffers and emitting fences. If a fence context is killed, e.g. due to a channel fault, jobs which are already queued for execution might still emit new fences. In such a case a job would hang forever. To fix that, fail to emit a new fence on a killed fence context with -ENODEV to unblock the job. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nouveau/nouveau_fence.c | 7 +++++++ drivers/gpu/drm/nouveau/nouveau_fence.h | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_fence.c b/drivers/gpu/drm/nouv= eau/nouveau_fence.c index ee5e9d40c166..62c70d9a32e6 100644 --- a/drivers/gpu/drm/nouveau/nouveau_fence.c +++ b/drivers/gpu/drm/nouveau/nouveau_fence.c @@ -96,6 +96,7 @@ nouveau_fence_context_kill(struct nouveau_fence_chan *fct= x, int error) if (nouveau_fence_signal(fence)) nvif_event_block(&fctx->event); } + fctx->killed =3D 1; spin_unlock_irqrestore(&fctx->lock, flags); } =20 @@ -226,6 +227,12 @@ nouveau_fence_emit(struct nouveau_fence *fence, struct= nouveau_channel *chan) dma_fence_get(&fence->base); spin_lock_irq(&fctx->lock); =20 + if (unlikely(fctx->killed)) { + spin_unlock_irq(&fctx->lock); + dma_fence_put(&fence->base); + return -ENODEV; + } + if (nouveau_fence_update(chan, fctx)) nvif_event_block(&fctx->event); =20 diff --git a/drivers/gpu/drm/nouveau/nouveau_fence.h b/drivers/gpu/drm/nouv= eau/nouveau_fence.h index 0ca2bc85adf6..00a08699bb58 100644 --- a/drivers/gpu/drm/nouveau/nouveau_fence.h +++ b/drivers/gpu/drm/nouveau/nouveau_fence.h @@ -45,7 +45,7 @@ struct nouveau_fence_chan { char name[32]; =20 struct nvif_event event; - int notify_ref, dead; + int notify_ref, dead, killed; }; =20 struct nouveau_fence_priv { --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2F1BC05027 for ; Fri, 17 Feb 2023 13:50:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230078AbjBQNuX (ORCPT ); Fri, 17 Feb 2023 08:50:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229941AbjBQNuD (ORCPT ); Fri, 17 Feb 2023 08:50:03 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53D6717164 for ; Fri, 17 Feb 2023 05:48:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641732; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nuqfhAPXoP8eMSRnoJXE/EUnRUmH5Dpmadrq5ZDJBI4=; b=YD8FDFYCv9WovaOETyPPQ04XLY971aR/whh80U4Hd0uhZNPI9PARMeYwD6Bd6UWBcYi91W VBCMG54Kc11V8vp2IDH09QtbXyCZQf2C24l2ZKiBnZwJp1XmEqUIdMJSbWJDZ9m4KPNtYD dFNOpUwsEPxuzEyTftzE0aDtleSwmuE= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-159-IqCugGyDM0yXVBfd7lREhg-1; Fri, 17 Feb 2023 08:48:51 -0500 X-MC-Unique: IqCugGyDM0yXVBfd7lREhg-1 Received: by mail-ed1-f71.google.com with SMTP id cn9-20020a0564020ca900b004a25d8d7593so1229655edb.0 for ; Fri, 17 Feb 2023 05:48:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nuqfhAPXoP8eMSRnoJXE/EUnRUmH5Dpmadrq5ZDJBI4=; b=sw0Q4luYdiXVnFhsWWe3ipoXSiwo3y/Kv8THGFK7Er7ZQHECj1zwxaf5/RQTOVAGRc aug8Co/GD0bgk2Gmw/dqTXomUojxZ3LtFEMIF14qub/F39PupU7sXKPP+SBWIY8X/ofZ n+0ytjDvCYmf3qCihgF871AlUzp0rY7mQmBrKAc2B0uTUBz0r0CS1W8brGKb+6daOTKD ogeq85vlGThtLyLv72qFKW3ePEzkCdhTYAq9qUvdF2xiykD9im6gwtMKZlTaJULgjTrN T6YwaoXHqKB5EKUiTr4roMLzqe3B7frIsk6OclRudVj+yspwVQJ8VcrY40SMsfRKmcNn i0SA== X-Gm-Message-State: AO0yUKWBZo0ORugkk8kvufKJp9aGoYE6kcSMlvNTQigH9Df6pdAhaY44 6sQWaVzFQ+RQLriPCLLL8Jn80VGE6BYKeHLHmFYtEIpkzG4D28QnDh8qsAzDOxZwcTvuCl1QF3s gygb43Ov/BcpClRXqZuVjThgp X-Received: by 2002:a17:906:5012:b0:87d:9447:f7fb with SMTP id s18-20020a170906501200b0087d9447f7fbmr444211ejj.38.1676641730481; Fri, 17 Feb 2023 05:48:50 -0800 (PST) X-Google-Smtp-Source: AK7set89k4Od4Os7LWbT0Iek0fo+d31O5+Il7AqZlhgERwv5aHbfx3F8XejW0D6aFl2/IjkHLHgKFg== X-Received: by 2002:a17:906:5012:b0:87d:9447:f7fb with SMTP id s18-20020a170906501200b0087d9447f7fbmr444186ejj.38.1676641730324; Fri, 17 Feb 2023 05:48:50 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id y23-20020a17090614d700b008b155de0984sm2180964ejc.170.2023.02.17.05.48.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:48:49 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 12/16] drm/nouveau: chan: provide nouveau_channel_kill() Date: Fri, 17 Feb 2023 14:48:16 +0100 Message-Id: <20230217134820.14672-7-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The new VM_BIND UAPI implementation introduced in subsequent commits will allow asynchronous jobs processing push buffers and emitting fences. If a job times out, we need a way to recover from this situation. For now, simply kill the channel to unblock all hung up jobs and signal userspace that the device is dead on the next EXEC or VM_BIND ioctl. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nouveau/nouveau_chan.c | 14 +++++++++++--- drivers/gpu/drm/nouveau/nouveau_chan.h | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_chan.c b/drivers/gpu/drm/nouve= au/nouveau_chan.c index 1068abe41024..6f47e997d9cf 100644 --- a/drivers/gpu/drm/nouveau/nouveau_chan.c +++ b/drivers/gpu/drm/nouveau/nouveau_chan.c @@ -40,6 +40,14 @@ MODULE_PARM_DESC(vram_pushbuf, "Create DMA push buffers = in VRAM"); int nouveau_vram_pushbuf; module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400); =20 +void +nouveau_channel_kill(struct nouveau_channel *chan) +{ + atomic_set(&chan->killed, 1); + if (chan->fence) + nouveau_fence_context_kill(chan->fence, -ENODEV); +} + static int nouveau_channel_killed(struct nvif_event *event, void *repv, u32 repc) { @@ -47,9 +55,9 @@ nouveau_channel_killed(struct nvif_event *event, void *re= pv, u32 repc) struct nouveau_cli *cli =3D (void *)chan->user.client; =20 NV_PRINTK(warn, cli, "channel %d killed!\n", chan->chid); - atomic_set(&chan->killed, 1); - if (chan->fence) - nouveau_fence_context_kill(chan->fence, -ENODEV); + + if (unlikely(!atomic_read(&chan->killed))) + nouveau_channel_kill(chan); =20 return NVIF_EVENT_DROP; } diff --git a/drivers/gpu/drm/nouveau/nouveau_chan.h b/drivers/gpu/drm/nouve= au/nouveau_chan.h index e06a8ffed31a..e483f4a254da 100644 --- a/drivers/gpu/drm/nouveau/nouveau_chan.h +++ b/drivers/gpu/drm/nouveau/nouveau_chan.h @@ -65,6 +65,7 @@ int nouveau_channel_new(struct nouveau_drm *, struct nvi= f_device *, bool priv, u32 vram, u32 gart, struct nouveau_channel **); void nouveau_channel_del(struct nouveau_channel **); int nouveau_channel_idle(struct nouveau_channel *); +void nouveau_channel_kill(struct nouveau_channel *); =20 extern int nouveau_vram_pushbuf; =20 --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2DC38C05027 for ; Fri, 17 Feb 2023 13:50:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230098AbjBQNu2 (ORCPT ); Fri, 17 Feb 2023 08:50:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229970AbjBQNuG (ORCPT ); Fri, 17 Feb 2023 08:50:06 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75C79497FA for ; Fri, 17 Feb 2023 05:48:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641737; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+jwORAYkDykD+zRwq4/W8kGZAUEb7WEZ6froGnNnKQo=; b=fZ7L12DQL0L04m3WtlghMW4uiQaROr99PtO3toGmmj1pgabgqiHA13zbDdCUdhAQPZc3fd UltHIof0X63cHISMTe7eq1QmF77h6YCALLwxnpmyePSl2j82j1iJi1JxrwLz9w7j5HOBCJ CL6A9fxCGqG6INkVHii0Ksgeaf3sFhA= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-658-9dFJbUPsM4ecdjd4g16yvw-1; Fri, 17 Feb 2023 08:48:56 -0500 X-MC-Unique: 9dFJbUPsM4ecdjd4g16yvw-1 Received: by mail-ed1-f72.google.com with SMTP id fd8-20020a056402388800b004ad793116d5so76458edb.23 for ; Fri, 17 Feb 2023 05:48:56 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+jwORAYkDykD+zRwq4/W8kGZAUEb7WEZ6froGnNnKQo=; b=0YKqoldvo2MEUVppE3N8AUUj4YzUxwVBO1qYzt/bCmzvnwf0rlkcnakn92L97ZhC8E ie1mejlzxOAoqesreZyL6IwIgaNVvy34C8fQ7htE6ZEPls1IVfXMPB51ndbuI12SmWwX A+DCYNH3i5tFZSSdQBiKWJFJMEGJw8LfXnRqJEn/8axmgPgqbNCmxVcIx4daJHTIEnw4 ClHSWzHMeJtCyr8xUt/GRznL8KC4VDpH6LDlPyintH/ZDJqXyntEKjPY5sC6QroQMv1A Sx8vaOo8aeX/HcZzggp4HI49QApru9EeXj+uzzQbjteosa9RiOnA7p9/7JBz3pOuRWVe nxRg== X-Gm-Message-State: AO0yUKU0zQJ1u79T9oa8kE1xTcfm9Wrcx092omkwSZt1N+7WB28jP0WO uqPD9GYGT9hol6qc5QezUfgLDki88+hAsGwXAFvGx1fSKl1W5F4tLzpxR/IOZuvqLmfwOWNQMhX pVGPOC8GZlHc7HYtwyKnuObf3 X-Received: by 2002:a17:907:a07:b0:8b1:3483:b825 with SMTP id bb7-20020a1709070a0700b008b13483b825mr781083ejc.51.1676641734863; Fri, 17 Feb 2023 05:48:54 -0800 (PST) X-Google-Smtp-Source: AK7set9WHy7sGVtWDLx+MtMSDQ9zJTHh15IhGstVUFRdvHEFJnbBNxGQLQqyI6AMCklLT5KVNUI2rw== X-Received: by 2002:a17:907:a07:b0:8b1:3483:b825 with SMTP id bb7-20020a1709070a0700b008b13483b825mr781063ejc.51.1676641734464; Fri, 17 Feb 2023 05:48:54 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id rh16-20020a17090720f000b008b6aea4738esm317545ejb.42.2023.02.17.05.48.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:48:53 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 13/16] drm/nouveau: nvkm/vmm: implement raw ops to manage uvmm Date: Fri, 17 Feb 2023 14:48:17 +0100 Message-Id: <20230217134820.14672-8-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The new VM_BIND UAPI uses the DRM GPU VA manager to manage the VA space. Hence, we a need a way to manipulate the MMUs page tables without going through the internal range allocator implemented by nvkm/vmm. This patch adds a raw interface for nvkm/vmm to pass the resposibility for managing the address space and the corresponding map/unmap/sparse operations to the upper layers. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nouveau/include/nvif/if000c.h | 26 ++- drivers/gpu/drm/nouveau/include/nvif/vmm.h | 19 +- .../gpu/drm/nouveau/include/nvkm/subdev/mmu.h | 20 +- drivers/gpu/drm/nouveau/nouveau_svm.c | 2 +- drivers/gpu/drm/nouveau/nouveau_vmm.c | 4 +- drivers/gpu/drm/nouveau/nvif/vmm.c | 100 +++++++- .../gpu/drm/nouveau/nvkm/subdev/mmu/uvmm.c | 213 ++++++++++++++++-- drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c | 197 ++++++++++++---- drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h | 25 ++ .../drm/nouveau/nvkm/subdev/mmu/vmmgf100.c | 16 +- .../drm/nouveau/nvkm/subdev/mmu/vmmgp100.c | 16 +- .../gpu/drm/nouveau/nvkm/subdev/mmu/vmmnv50.c | 27 ++- 12 files changed, 566 insertions(+), 99 deletions(-) diff --git a/drivers/gpu/drm/nouveau/include/nvif/if000c.h b/drivers/gpu/dr= m/nouveau/include/nvif/if000c.h index 9c7ff56831c5..a5a182b3c28d 100644 --- a/drivers/gpu/drm/nouveau/include/nvif/if000c.h +++ b/drivers/gpu/drm/nouveau/include/nvif/if000c.h @@ -3,7 +3,10 @@ struct nvif_vmm_v0 { __u8 version; __u8 page_nr; - __u8 managed; +#define NVIF_VMM_V0_TYPE_UNMANAGED = 0x00 +#define NVIF_VMM_V0_TYPE_MANAGED = 0x01 +#define NVIF_VMM_V0_TYPE_RAW = 0x02 + __u8 type; __u8 pad03[5]; __u64 addr; __u64 size; @@ -17,6 +20,7 @@ struct nvif_vmm_v0 { #define NVIF_VMM_V0_UNMAP = 0x04 #define NVIF_VMM_V0_PFNMAP = 0x05 #define NVIF_VMM_V0_PFNCLR = 0x06 +#define NVIF_VMM_V0_RAW = 0x07 #define NVIF_VMM_V0_MTHD(i) ((i) += 0x80) =20 struct nvif_vmm_page_v0 { @@ -66,6 +70,26 @@ struct nvif_vmm_unmap_v0 { __u64 addr; }; =20 +struct nvif_vmm_raw_v0 { + __u8 version; +#define NVIF_VMM_RAW_V0_GET 0x0 +#define NVIF_VMM_RAW_V0_PUT 0x1 +#define NVIF_VMM_RAW_V0_MAP 0x2 +#define NVIF_VMM_RAW_V0_UNMAP 0x3 +#define NVIF_VMM_RAW_V0_SPARSE 0x4 + __u8 op; + __u8 sparse; + __u8 ref; + __u8 shift; + __u32 argc; + __u8 pad01[7]; + __u64 addr; + __u64 size; + __u64 offset; + __u64 memory; + __u64 argv; +}; + struct nvif_vmm_pfnmap_v0 { __u8 version; __u8 page; diff --git a/drivers/gpu/drm/nouveau/include/nvif/vmm.h b/drivers/gpu/drm/n= ouveau/include/nvif/vmm.h index a2ee92201ace..0ecedd0ee0a5 100644 --- a/drivers/gpu/drm/nouveau/include/nvif/vmm.h +++ b/drivers/gpu/drm/nouveau/include/nvif/vmm.h @@ -4,6 +4,12 @@ struct nvif_mem; struct nvif_mmu; =20 +enum nvif_vmm_type { + UNMANAGED, + MANAGED, + RAW, +}; + enum nvif_vmm_get { ADDR, PTES, @@ -30,8 +36,9 @@ struct nvif_vmm { int page_nr; }; =20 -int nvif_vmm_ctor(struct nvif_mmu *, const char *name, s32 oclass, bool ma= naged, - u64 addr, u64 size, void *argv, u32 argc, struct nvif_vmm *); +int nvif_vmm_ctor(struct nvif_mmu *, const char *name, s32 oclass, + enum nvif_vmm_type, u64 addr, u64 size, void *argv, u32 argc, + struct nvif_vmm *); void nvif_vmm_dtor(struct nvif_vmm *); int nvif_vmm_get(struct nvif_vmm *, enum nvif_vmm_get, bool sparse, u8 page, u8 align, u64 size, struct nvif_vma *); @@ -39,4 +46,12 @@ void nvif_vmm_put(struct nvif_vmm *, struct nvif_vma *); int nvif_vmm_map(struct nvif_vmm *, u64 addr, u64 size, void *argv, u32 ar= gc, struct nvif_mem *, u64 offset); int nvif_vmm_unmap(struct nvif_vmm *, u64); + +int nvif_vmm_raw_get(struct nvif_vmm *vmm, u64 addr, u64 size, u8 shift); +int nvif_vmm_raw_put(struct nvif_vmm *vmm, u64 addr, u64 size, u8 shift); +int nvif_vmm_raw_map(struct nvif_vmm *vmm, u64 addr, u64 size, u8 shift, + void *argv, u32 argc, struct nvif_mem *mem, u64 offset); +int nvif_vmm_raw_unmap(struct nvif_vmm *vmm, u64 addr, u64 size, + u8 shift, bool sparse); +int nvif_vmm_raw_sparse(struct nvif_vmm *vmm, u64 addr, u64 size, bool ref= ); #endif diff --git a/drivers/gpu/drm/nouveau/include/nvkm/subdev/mmu.h b/drivers/gp= u/drm/nouveau/include/nvkm/subdev/mmu.h index 70e7887ef4b4..2fd2f2433fc7 100644 --- a/drivers/gpu/drm/nouveau/include/nvkm/subdev/mmu.h +++ b/drivers/gpu/drm/nouveau/include/nvkm/subdev/mmu.h @@ -17,6 +17,7 @@ struct nvkm_vma { bool part:1; /* Region was split from an allocated region by map(). */ bool busy:1; /* Region busy (for temporarily preventing user access). */ bool mapped:1; /* Region contains valid pages. */ + bool no_comp:1; /* Force no memory compression. */ struct nvkm_memory *memory; /* Memory currently mapped into VMA. */ struct nvkm_tags *tags; /* Compression tag reference. */ }; @@ -27,10 +28,26 @@ struct nvkm_vmm { const char *name; u32 debug; struct kref kref; - struct mutex mutex; + + struct { + struct mutex vmm; + struct mutex ref; + struct mutex map; + } mutex; =20 u64 start; u64 limit; + struct { + struct { + u64 addr; + u64 size; + } p; + struct { + u64 addr; + u64 size; + } n; + bool raw; + } managed; =20 struct nvkm_vmm_pt *pd; struct list_head join; @@ -70,6 +87,7 @@ struct nvkm_vmm_map { =20 const struct nvkm_vmm_page *page; =20 + bool no_comp; struct nvkm_tags *tags; u64 next; u64 type; diff --git a/drivers/gpu/drm/nouveau/nouveau_svm.c b/drivers/gpu/drm/nouvea= u/nouveau_svm.c index a74ba8d84ba7..186351ecf72f 100644 --- a/drivers/gpu/drm/nouveau/nouveau_svm.c +++ b/drivers/gpu/drm/nouveau/nouveau_svm.c @@ -350,7 +350,7 @@ nouveau_svmm_init(struct drm_device *dev, void *data, * VMM instead of the standard one. */ ret =3D nvif_vmm_ctor(&cli->mmu, "svmVmm", - cli->vmm.vmm.object.oclass, true, + cli->vmm.vmm.object.oclass, MANAGED, args->unmanaged_addr, args->unmanaged_size, &(struct gp100_vmm_v0) { .fault_replay =3D true, diff --git a/drivers/gpu/drm/nouveau/nouveau_vmm.c b/drivers/gpu/drm/nouvea= u/nouveau_vmm.c index 67d6619fcd5e..a6602c012671 100644 --- a/drivers/gpu/drm/nouveau/nouveau_vmm.c +++ b/drivers/gpu/drm/nouveau/nouveau_vmm.c @@ -128,8 +128,8 @@ nouveau_vmm_fini(struct nouveau_vmm *vmm) int nouveau_vmm_init(struct nouveau_cli *cli, s32 oclass, struct nouveau_vmm *= vmm) { - int ret =3D nvif_vmm_ctor(&cli->mmu, "drmVmm", oclass, false, PAGE_SIZE, - 0, NULL, 0, &vmm->vmm); + int ret =3D nvif_vmm_ctor(&cli->mmu, "drmVmm", oclass, UNMANAGED, + PAGE_SIZE, 0, NULL, 0, &vmm->vmm); if (ret) return ret; =20 diff --git a/drivers/gpu/drm/nouveau/nvif/vmm.c b/drivers/gpu/drm/nouveau/n= vif/vmm.c index 6053d6dc2184..99296f03371a 100644 --- a/drivers/gpu/drm/nouveau/nvif/vmm.c +++ b/drivers/gpu/drm/nouveau/nvif/vmm.c @@ -104,6 +104,90 @@ nvif_vmm_get(struct nvif_vmm *vmm, enum nvif_vmm_get t= ype, bool sparse, return ret; } =20 +int +nvif_vmm_raw_get(struct nvif_vmm *vmm, u64 addr, u64 size, + u8 shift) +{ + struct nvif_vmm_raw_v0 args =3D { + .version =3D 0, + .op =3D NVIF_VMM_RAW_V0_GET, + .addr =3D addr, + .size =3D size, + .shift =3D shift, + }; + + return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW, + &args, sizeof(args)); +} + +int +nvif_vmm_raw_put(struct nvif_vmm *vmm, u64 addr, u64 size, u8 shift) +{ + struct nvif_vmm_raw_v0 args =3D { + .version =3D 0, + .op =3D NVIF_VMM_RAW_V0_PUT, + .addr =3D addr, + .size =3D size, + .shift =3D shift, + }; + + return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW, + &args, sizeof(args)); +} + +int +nvif_vmm_raw_map(struct nvif_vmm *vmm, u64 addr, u64 size, u8 shift, + void *argv, u32 argc, struct nvif_mem *mem, u64 offset) +{ + struct nvif_vmm_raw_v0 args =3D { + .version =3D 0, + .op =3D NVIF_VMM_RAW_V0_MAP, + .addr =3D addr, + .size =3D size, + .shift =3D shift, + .memory =3D nvif_handle(&mem->object), + .offset =3D offset, + .argv =3D (u64)(uintptr_t)argv, + .argc =3D argc, + }; + + + return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW, + &args, sizeof(args)); +} + +int +nvif_vmm_raw_unmap(struct nvif_vmm *vmm, u64 addr, u64 size, + u8 shift, bool sparse) +{ + struct nvif_vmm_raw_v0 args =3D { + .version =3D 0, + .op =3D NVIF_VMM_RAW_V0_UNMAP, + .addr =3D addr, + .size =3D size, + .shift =3D shift, + .sparse =3D sparse, + }; + + return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW, + &args, sizeof(args)); +} + +int +nvif_vmm_raw_sparse(struct nvif_vmm *vmm, u64 addr, u64 size, bool ref) +{ + struct nvif_vmm_raw_v0 args =3D { + .version =3D 0, + .op =3D NVIF_VMM_RAW_V0_SPARSE, + .addr =3D addr, + .size =3D size, + .ref =3D ref, + }; + + return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW, + &args, sizeof(args)); +} + void nvif_vmm_dtor(struct nvif_vmm *vmm) { @@ -112,8 +196,9 @@ nvif_vmm_dtor(struct nvif_vmm *vmm) } =20 int -nvif_vmm_ctor(struct nvif_mmu *mmu, const char *name, s32 oclass, bool man= aged, - u64 addr, u64 size, void *argv, u32 argc, struct nvif_vmm *vmm) +nvif_vmm_ctor(struct nvif_mmu *mmu, const char *name, s32 oclass, + enum nvif_vmm_type type, u64 addr, u64 size, void *argv, u32 argc, + struct nvif_vmm *vmm) { struct nvif_vmm_v0 *args; u32 argn =3D sizeof(*args) + argc; @@ -125,9 +210,18 @@ nvif_vmm_ctor(struct nvif_mmu *mmu, const char *name, = s32 oclass, bool managed, if (!(args =3D kmalloc(argn, GFP_KERNEL))) return -ENOMEM; args->version =3D 0; - args->managed =3D managed; args->addr =3D addr; args->size =3D size; + + switch (type) { + case UNMANAGED: args->type =3D NVIF_VMM_V0_TYPE_UNMANAGED; break; + case MANAGED: args->type =3D NVIF_VMM_V0_TYPE_MANAGED; break; + case RAW: args->type =3D NVIF_VMM_V0_TYPE_RAW; break; + default: + WARN_ON(1); + return -EINVAL; + } + memcpy(args->data, argv, argc); =20 ret =3D nvif_object_ctor(&mmu->object, name ? name : "nvifVmm", 0, diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/uvmm.c b/drivers/gpu/d= rm/nouveau/nvkm/subdev/mmu/uvmm.c index 524cd3c0e3fe..38b7ced934b1 100644 --- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/uvmm.c +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/uvmm.c @@ -58,10 +58,13 @@ nvkm_uvmm_mthd_pfnclr(struct nvkm_uvmm *uvmm, void *arg= v, u32 argc) } else return ret; =20 + if (nvkm_vmm_in_managed_range(vmm, addr, size) && vmm->managed.raw) + return -EINVAL; + if (size) { - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); ret =3D nvkm_vmm_pfn_unmap(vmm, addr, size); - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); } =20 return ret; @@ -88,10 +91,13 @@ nvkm_uvmm_mthd_pfnmap(struct nvkm_uvmm *uvmm, void *arg= v, u32 argc) } else return ret; =20 + if (nvkm_vmm_in_managed_range(vmm, addr, size) && vmm->managed.raw) + return -EINVAL; + if (size) { - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); ret =3D nvkm_vmm_pfn_map(vmm, page, addr, size, phys); - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); } =20 return ret; @@ -113,7 +119,10 @@ nvkm_uvmm_mthd_unmap(struct nvkm_uvmm *uvmm, void *arg= v, u32 argc) } else return ret; =20 - mutex_lock(&vmm->mutex); + if (nvkm_vmm_in_managed_range(vmm, addr, 0) && vmm->managed.raw) + return -EINVAL; + + mutex_lock(&vmm->mutex.vmm); vma =3D nvkm_vmm_node_search(vmm, addr); if (ret =3D -ENOENT, !vma || vma->addr !=3D addr) { VMM_DEBUG(vmm, "lookup %016llx: %016llx", @@ -134,7 +143,7 @@ nvkm_uvmm_mthd_unmap(struct nvkm_uvmm *uvmm, void *argv= , u32 argc) nvkm_vmm_unmap_locked(vmm, vma, false); ret =3D 0; done: - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); return ret; } =20 @@ -159,13 +168,16 @@ nvkm_uvmm_mthd_map(struct nvkm_uvmm *uvmm, void *argv= , u32 argc) } else return ret; =20 + if (nvkm_vmm_in_managed_range(vmm, addr, size) && vmm->managed.raw) + return -EINVAL; + memory =3D nvkm_umem_search(client, handle); if (IS_ERR(memory)) { VMM_DEBUG(vmm, "memory %016llx %ld\n", handle, PTR_ERR(memory)); return PTR_ERR(memory); } =20 - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); if (ret =3D -ENOENT, !(vma =3D nvkm_vmm_node_search(vmm, addr))) { VMM_DEBUG(vmm, "lookup %016llx", addr); goto fail; @@ -198,7 +210,7 @@ nvkm_uvmm_mthd_map(struct nvkm_uvmm *uvmm, void *argv, = u32 argc) } } vma->busy =3D true; - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); =20 ret =3D nvkm_memory_map(memory, offset, vmm, vma, argv, argc); if (ret =3D=3D 0) { @@ -207,11 +219,11 @@ nvkm_uvmm_mthd_map(struct nvkm_uvmm *uvmm, void *argv= , u32 argc) return 0; } =20 - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); vma->busy =3D false; nvkm_vmm_unmap_region(vmm, vma); fail: - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); nvkm_memory_unref(&memory); return ret; } @@ -232,7 +244,7 @@ nvkm_uvmm_mthd_put(struct nvkm_uvmm *uvmm, void *argv, = u32 argc) } else return ret; =20 - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); vma =3D nvkm_vmm_node_search(vmm, args->v0.addr); if (ret =3D -ENOENT, !vma || vma->addr !=3D addr || vma->part) { VMM_DEBUG(vmm, "lookup %016llx: %016llx %d", addr, @@ -248,7 +260,7 @@ nvkm_uvmm_mthd_put(struct nvkm_uvmm *uvmm, void *argv, = u32 argc) nvkm_vmm_put_locked(vmm, vma); ret =3D 0; done: - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); return ret; } =20 @@ -275,10 +287,10 @@ nvkm_uvmm_mthd_get(struct nvkm_uvmm *uvmm, void *argv= , u32 argc) } else return ret; =20 - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); ret =3D nvkm_vmm_get_locked(vmm, getref, mapref, sparse, page, align, size, &vma); - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); if (ret) return ret; =20 @@ -314,6 +326,167 @@ nvkm_uvmm_mthd_page(struct nvkm_uvmm *uvmm, void *arg= v, u32 argc) return 0; } =20 +static inline int +nvkm_uvmm_page_index(struct nvkm_uvmm *uvmm, u64 size, u8 shift, u8 *refd) +{ + struct nvkm_vmm *vmm =3D uvmm->vmm; + const struct nvkm_vmm_page *page; + + if (likely(shift)) { + for (page =3D vmm->func->page; page->shift; page++) { + if (shift =3D=3D page->shift) + break; + } + + if (!page->shift || !IS_ALIGNED(size, 1ULL << page->shift)) { + VMM_DEBUG(vmm, "page %d %016llx", shift, size); + return -EINVAL; + } + } else { + return -EINVAL; + } + *refd =3D page - vmm->func->page; + + return 0; +} + +static int +nvkm_uvmm_mthd_raw_get(struct nvkm_uvmm *uvmm, struct nvif_vmm_raw_v0 *arg= s) +{ + struct nvkm_vmm *vmm =3D uvmm->vmm; + u8 refd; + int ret; + + if (!nvkm_vmm_in_managed_range(vmm, args->addr, args->size)) + return -EINVAL; + + ret =3D nvkm_uvmm_page_index(uvmm, args->size, args->shift, &refd); + if (ret) + return ret; + + return nvkm_vmm_raw_get(vmm, args->addr, args->size, refd); +} + +static int +nvkm_uvmm_mthd_raw_put(struct nvkm_uvmm *uvmm, struct nvif_vmm_raw_v0 *arg= s) +{ + struct nvkm_vmm *vmm =3D uvmm->vmm; + u8 refd; + int ret; + + if (!nvkm_vmm_in_managed_range(vmm, args->addr, args->size)) + return -EINVAL; + + ret =3D nvkm_uvmm_page_index(uvmm, args->size, args->shift, &refd); + if (ret) + return ret; + + nvkm_vmm_raw_put(vmm, args->addr, args->size, refd); + + return 0; +} + +static int +nvkm_uvmm_mthd_raw_map(struct nvkm_uvmm *uvmm, struct nvif_vmm_raw_v0 *arg= s) +{ + struct nvkm_client *client =3D uvmm->object.client; + struct nvkm_vmm *vmm =3D uvmm->vmm; + struct nvkm_vma vma =3D { + .addr =3D args->addr, + .size =3D args->size, + .used =3D true, + .mapref =3D false, + .no_comp =3D true, + }; + struct nvkm_memory *memory; + u64 handle =3D args->memory; + u8 refd; + int ret; + + if (!nvkm_vmm_in_managed_range(vmm, args->addr, args->size)) + return -EINVAL; + + ret =3D nvkm_uvmm_page_index(uvmm, args->size, args->shift, &refd); + if (ret) + return ret; + + vma.page =3D vma.refd =3D refd; + + memory =3D nvkm_umem_search(client, args->memory); + if (IS_ERR(memory)) { + VMM_DEBUG(vmm, "memory %016llx %ld\n", handle, PTR_ERR(memory)); + return PTR_ERR(memory); + } + + ret =3D nvkm_memory_map(memory, args->offset, vmm, &vma, + (void *)args->argv, args->argc); + + nvkm_memory_unref(&vma.memory); + nvkm_memory_unref(&memory); + return ret; +} + +static int +nvkm_uvmm_mthd_raw_unmap(struct nvkm_uvmm *uvmm, struct nvif_vmm_raw_v0 *a= rgs) +{ + struct nvkm_vmm *vmm =3D uvmm->vmm; + u8 refd; + int ret; + + if (!nvkm_vmm_in_managed_range(vmm, args->addr, args->size)) + return -EINVAL; + + ret =3D nvkm_uvmm_page_index(uvmm, args->size, args->shift, &refd); + if (ret) + return ret; + + nvkm_vmm_raw_unmap(vmm, args->addr, args->size, + args->sparse, refd); + + return 0; +} + +static int +nvkm_uvmm_mthd_raw_sparse(struct nvkm_uvmm *uvmm, struct nvif_vmm_raw_v0 *= args) +{ + struct nvkm_vmm *vmm =3D uvmm->vmm; + + if (!nvkm_vmm_in_managed_range(vmm, args->addr, args->size)) + return -EINVAL; + + return nvkm_vmm_raw_sparse(vmm, args->addr, args->size, args->ref); +} + +static int +nvkm_uvmm_mthd_raw(struct nvkm_uvmm *uvmm, void *argv, u32 argc) +{ + union { + struct nvif_vmm_raw_v0 v0; + } *args =3D argv; + int ret =3D -ENOSYS; + + if (!uvmm->vmm->managed.raw) + return -EINVAL; + + if ((ret =3D nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, true))) + return ret; + + switch (args->v0.op) { + case NVIF_VMM_RAW_V0_GET: + return nvkm_uvmm_mthd_raw_get(uvmm, &args->v0); + case NVIF_VMM_RAW_V0_PUT: + return nvkm_uvmm_mthd_raw_put(uvmm, &args->v0); + case NVIF_VMM_RAW_V0_MAP: + return nvkm_uvmm_mthd_raw_map(uvmm, &args->v0); + case NVIF_VMM_RAW_V0_UNMAP: + return nvkm_uvmm_mthd_raw_unmap(uvmm, &args->v0); + case NVIF_VMM_RAW_V0_SPARSE: + return nvkm_uvmm_mthd_raw_sparse(uvmm, &args->v0); + default: + return -EINVAL; + }; +} + static int nvkm_uvmm_mthd(struct nvkm_object *object, u32 mthd, void *argv, u32 argc) { @@ -326,6 +499,7 @@ nvkm_uvmm_mthd(struct nvkm_object *object, u32 mthd, vo= id *argv, u32 argc) case NVIF_VMM_V0_UNMAP : return nvkm_uvmm_mthd_unmap (uvmm, argv, argc); case NVIF_VMM_V0_PFNMAP: return nvkm_uvmm_mthd_pfnmap(uvmm, argv, argc); case NVIF_VMM_V0_PFNCLR: return nvkm_uvmm_mthd_pfnclr(uvmm, argv, argc); + case NVIF_VMM_V0_RAW : return nvkm_uvmm_mthd_raw (uvmm, argv, argc); case NVIF_VMM_V0_MTHD(0x00) ... NVIF_VMM_V0_MTHD(0x7f): if (uvmm->vmm->func->mthd) { return uvmm->vmm->func->mthd(uvmm->vmm, @@ -366,10 +540,11 @@ nvkm_uvmm_new(const struct nvkm_oclass *oclass, void = *argv, u32 argc, struct nvkm_uvmm *uvmm; int ret =3D -ENOSYS; u64 addr, size; - bool managed; + bool managed, raw; =20 if (!(ret =3D nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, more))) { - managed =3D args->v0.managed !=3D 0; + managed =3D args->v0.type =3D=3D NVIF_VMM_V0_TYPE_MANAGED; + raw =3D args->v0.type =3D=3D NVIF_VMM_V0_TYPE_RAW; addr =3D args->v0.addr; size =3D args->v0.size; } else @@ -377,12 +552,13 @@ nvkm_uvmm_new(const struct nvkm_oclass *oclass, void = *argv, u32 argc, =20 if (!(uvmm =3D kzalloc(sizeof(*uvmm), GFP_KERNEL))) return -ENOMEM; + nvkm_object_ctor(&nvkm_uvmm, oclass, &uvmm->object); *pobject =3D &uvmm->object; =20 if (!mmu->vmm) { - ret =3D mmu->func->vmm.ctor(mmu, managed, addr, size, argv, argc, - NULL, "user", &uvmm->vmm); + ret =3D mmu->func->vmm.ctor(mmu, managed || raw, addr, size, + argv, argc, NULL, "user", &uvmm->vmm); if (ret) return ret; =20 @@ -393,6 +569,7 @@ nvkm_uvmm_new(const struct nvkm_oclass *oclass, void *a= rgv, u32 argc, =20 uvmm->vmm =3D nvkm_vmm_ref(mmu->vmm); } + uvmm->vmm->managed.raw =3D raw; =20 page =3D uvmm->vmm->func->page; args->v0.page_nr =3D 0; diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c b/drivers/gpu/dr= m/nouveau/nvkm/subdev/mmu/vmm.c index ae793f400ba1..eb5fcadcb39a 100644 --- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c @@ -676,41 +676,18 @@ nvkm_vmm_ptes_sparse(struct nvkm_vmm *vmm, u64 addr, = u64 size, bool ref) return 0; } =20 -static void -nvkm_vmm_ptes_unmap_put(struct nvkm_vmm *vmm, const struct nvkm_vmm_page *= page, - u64 addr, u64 size, bool sparse, bool pfn) -{ - const struct nvkm_vmm_desc_func *func =3D page->desc->func; - nvkm_vmm_iter(vmm, page, addr, size, "unmap + unref", - false, pfn, nvkm_vmm_unref_ptes, NULL, NULL, - sparse ? func->sparse : func->invalid ? func->invalid : - func->unmap); -} - -static int -nvkm_vmm_ptes_get_map(struct nvkm_vmm *vmm, const struct nvkm_vmm_page *pa= ge, - u64 addr, u64 size, struct nvkm_vmm_map *map, - nvkm_vmm_pte_func func) -{ - u64 fail =3D nvkm_vmm_iter(vmm, page, addr, size, "ref + map", true, - false, nvkm_vmm_ref_ptes, func, map, NULL); - if (fail !=3D ~0ULL) { - if ((size =3D fail - addr)) - nvkm_vmm_ptes_unmap_put(vmm, page, addr, size, false, false); - return -ENOMEM; - } - return 0; -} - static void nvkm_vmm_ptes_unmap(struct nvkm_vmm *vmm, const struct nvkm_vmm_page *page, u64 addr, u64 size, bool sparse, bool pfn) { const struct nvkm_vmm_desc_func *func =3D page->desc->func; + + mutex_lock(&vmm->mutex.map); nvkm_vmm_iter(vmm, page, addr, size, "unmap", false, pfn, NULL, NULL, NULL, sparse ? func->sparse : func->invalid ? func->invalid : func->unmap); + mutex_unlock(&vmm->mutex.map); } =20 static void @@ -718,33 +695,108 @@ nvkm_vmm_ptes_map(struct nvkm_vmm *vmm, const struct= nvkm_vmm_page *page, u64 addr, u64 size, struct nvkm_vmm_map *map, nvkm_vmm_pte_func func) { + mutex_lock(&vmm->mutex.map); nvkm_vmm_iter(vmm, page, addr, size, "map", false, false, NULL, func, map, NULL); + mutex_unlock(&vmm->mutex.map); } =20 static void -nvkm_vmm_ptes_put(struct nvkm_vmm *vmm, const struct nvkm_vmm_page *page, - u64 addr, u64 size) +nvkm_vmm_ptes_put_locked(struct nvkm_vmm *vmm, const struct nvkm_vmm_page = *page, + u64 addr, u64 size) { nvkm_vmm_iter(vmm, page, addr, size, "unref", false, false, nvkm_vmm_unref_ptes, NULL, NULL, NULL); } =20 +static void +nvkm_vmm_ptes_put(struct nvkm_vmm *vmm, const struct nvkm_vmm_page *page, + u64 addr, u64 size) +{ + mutex_lock(&vmm->mutex.ref); + nvkm_vmm_ptes_put_locked(vmm, page, addr, size); + mutex_unlock(&vmm->mutex.ref); +} + static int nvkm_vmm_ptes_get(struct nvkm_vmm *vmm, const struct nvkm_vmm_page *page, u64 addr, u64 size) { - u64 fail =3D nvkm_vmm_iter(vmm, page, addr, size, "ref", true, false, - nvkm_vmm_ref_ptes, NULL, NULL, NULL); + u64 fail; + + mutex_lock(&vmm->mutex.ref); + fail =3D nvkm_vmm_iter(vmm, page, addr, size, "ref", true, false, + nvkm_vmm_ref_ptes, NULL, NULL, NULL); if (fail !=3D ~0ULL) { if (fail !=3D addr) - nvkm_vmm_ptes_put(vmm, page, addr, fail - addr); + nvkm_vmm_ptes_put_locked(vmm, page, addr, fail - addr); + mutex_unlock(&vmm->mutex.ref); + return -ENOMEM; + } + mutex_unlock(&vmm->mutex.ref); + return 0; +} + +static void +__nvkm_vmm_ptes_unmap_put(struct nvkm_vmm *vmm, const struct nvkm_vmm_page= *page, + u64 addr, u64 size, bool sparse, bool pfn) +{ + const struct nvkm_vmm_desc_func *func =3D page->desc->func; + + nvkm_vmm_iter(vmm, page, addr, size, "unmap + unref", + false, pfn, nvkm_vmm_unref_ptes, NULL, NULL, + sparse ? func->sparse : func->invalid ? func->invalid : + func->unmap); +} + +static void +nvkm_vmm_ptes_unmap_put(struct nvkm_vmm *vmm, const struct nvkm_vmm_page *= page, + u64 addr, u64 size, bool sparse, bool pfn) +{ + if (vmm->managed.raw) { + nvkm_vmm_ptes_unmap(vmm, page, addr, size, sparse, pfn); + nvkm_vmm_ptes_put(vmm, page, addr, size); + } else { + __nvkm_vmm_ptes_unmap_put(vmm, page, addr, size, sparse, pfn); + } +} + +static int +__nvkm_vmm_ptes_get_map(struct nvkm_vmm *vmm, const struct nvkm_vmm_page *= page, + u64 addr, u64 size, struct nvkm_vmm_map *map, + nvkm_vmm_pte_func func) +{ + u64 fail =3D nvkm_vmm_iter(vmm, page, addr, size, "ref + map", true, + false, nvkm_vmm_ref_ptes, func, map, NULL); + if (fail !=3D ~0ULL) { + if ((size =3D fail - addr)) + nvkm_vmm_ptes_unmap_put(vmm, page, addr, size, false, false); return -ENOMEM; } return 0; } =20 -static inline struct nvkm_vma * +static int +nvkm_vmm_ptes_get_map(struct nvkm_vmm *vmm, const struct nvkm_vmm_page *pa= ge, + u64 addr, u64 size, struct nvkm_vmm_map *map, + nvkm_vmm_pte_func func) +{ + int ret; + + if (vmm->managed.raw) { + ret =3D nvkm_vmm_ptes_get(vmm, page, addr, size); + if (ret) + return ret; + + nvkm_vmm_ptes_map(vmm, page, addr, size, map, func); + + return 0; + } else { + return __nvkm_vmm_ptes_get_map(vmm, page, addr, size, map, func); + } +} + +struct nvkm_vma * nvkm_vma_new(u64 addr, u64 size) { struct nvkm_vma *vma =3D kzalloc(sizeof(*vma), GFP_KERNEL); @@ -1045,7 +1097,9 @@ nvkm_vmm_ctor(const struct nvkm_vmm_func *func, struc= t nvkm_mmu *mmu, vmm->debug =3D mmu->subdev.debug; kref_init(&vmm->kref); =20 - __mutex_init(&vmm->mutex, "&vmm->mutex", key ? key : &_key); + __mutex_init(&vmm->mutex.vmm, "&vmm->mutex.vmm", key ? key : &_key); + mutex_init(&vmm->mutex.ref); + mutex_init(&vmm->mutex.map); =20 /* Locate the smallest page size supported by the backend, it will * have the deepest nesting of page tables. @@ -1101,6 +1155,9 @@ nvkm_vmm_ctor(const struct nvkm_vmm_func *func, struc= t nvkm_mmu *mmu, if (addr && (ret =3D nvkm_vmm_ctor_managed(vmm, 0, addr))) return ret; =20 + vmm->managed.p.addr =3D 0; + vmm->managed.p.size =3D addr; + /* NVKM-managed area. */ if (size) { if (!(vma =3D nvkm_vma_new(addr, size))) @@ -1114,6 +1171,9 @@ nvkm_vmm_ctor(const struct nvkm_vmm_func *func, struc= t nvkm_mmu *mmu, size =3D vmm->limit - addr; if (size && (ret =3D nvkm_vmm_ctor_managed(vmm, addr, size))) return ret; + + vmm->managed.n.addr =3D addr; + vmm->managed.n.size =3D size; } else { /* Address-space fully managed by NVKM, requiring calls to * nvkm_vmm_get()/nvkm_vmm_put() to allocate address-space. @@ -1362,9 +1422,9 @@ void nvkm_vmm_unmap(struct nvkm_vmm *vmm, struct nvkm_vma *vma) { if (vma->memory) { - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); nvkm_vmm_unmap_locked(vmm, vma, false); - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); } } =20 @@ -1423,6 +1483,8 @@ nvkm_vmm_map_locked(struct nvkm_vmm *vmm, struct nvkm= _vma *vma, nvkm_vmm_pte_func func; int ret; =20 + map->no_comp =3D vma->no_comp; + /* Make sure we won't overrun the end of the memory object. */ if (unlikely(nvkm_memory_size(map->memory) < map->offset + vma->size)) { VMM_DEBUG(vmm, "overrun %016llx %016llx %016llx", @@ -1507,10 +1569,15 @@ nvkm_vmm_map(struct nvkm_vmm *vmm, struct nvkm_vma = *vma, void *argv, u32 argc, struct nvkm_vmm_map *map) { int ret; - mutex_lock(&vmm->mutex); + + if (nvkm_vmm_in_managed_range(vmm, vma->addr, vma->size) && + vmm->managed.raw) + return nvkm_vmm_map_locked(vmm, vma, argv, argc, map); + + mutex_lock(&vmm->mutex.vmm); ret =3D nvkm_vmm_map_locked(vmm, vma, argv, argc, map); vma->busy =3D false; - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); return ret; } =20 @@ -1620,9 +1687,9 @@ nvkm_vmm_put(struct nvkm_vmm *vmm, struct nvkm_vma **= pvma) { struct nvkm_vma *vma =3D *pvma; if (vma) { - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); nvkm_vmm_put_locked(vmm, vma); - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); *pvma =3D NULL; } } @@ -1769,9 +1836,49 @@ int nvkm_vmm_get(struct nvkm_vmm *vmm, u8 page, u64 size, struct nvkm_vma **pv= ma) { int ret; - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); ret =3D nvkm_vmm_get_locked(vmm, false, true, false, page, 0, size, pvma); - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); + return ret; +} + +void +nvkm_vmm_raw_unmap(struct nvkm_vmm *vmm, u64 addr, u64 size, + bool sparse, u8 refd) +{ + const struct nvkm_vmm_page *page =3D &vmm->func->page[refd]; + + nvkm_vmm_ptes_unmap(vmm, page, addr, size, sparse, false); +} + +void +nvkm_vmm_raw_put(struct nvkm_vmm *vmm, u64 addr, u64 size, u8 refd) +{ + const struct nvkm_vmm_page *page =3D vmm->func->page; + + nvkm_vmm_ptes_put(vmm, &page[refd], addr, size); +} + +int +nvkm_vmm_raw_get(struct nvkm_vmm *vmm, u64 addr, u64 size, u8 refd) +{ + const struct nvkm_vmm_page *page =3D vmm->func->page; + + if (unlikely(!size)) + return -EINVAL; + + return nvkm_vmm_ptes_get(vmm, &page[refd], addr, size); +} + +int +nvkm_vmm_raw_sparse(struct nvkm_vmm *vmm, u64 addr, u64 size, bool ref) +{ + int ret; + + mutex_lock(&vmm->mutex.ref); + ret =3D nvkm_vmm_ptes_sparse(vmm, addr, size, ref); + mutex_unlock(&vmm->mutex.ref); + return ret; } =20 @@ -1779,9 +1886,9 @@ void nvkm_vmm_part(struct nvkm_vmm *vmm, struct nvkm_memory *inst) { if (inst && vmm && vmm->func->part) { - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); vmm->func->part(vmm, inst); - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); } } =20 @@ -1790,9 +1897,9 @@ nvkm_vmm_join(struct nvkm_vmm *vmm, struct nvkm_memor= y *inst) { int ret =3D 0; if (vmm->func->join) { - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); ret =3D vmm->func->join(vmm, inst); - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); } return ret; } diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h b/drivers/gpu/dr= m/nouveau/nvkm/subdev/mmu/vmm.h index f6188aa9171c..f9bc30cdb2b3 100644 --- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.h @@ -163,6 +163,7 @@ int nvkm_vmm_new_(const struct nvkm_vmm_func *, struct = nvkm_mmu *, u32 pd_header, bool managed, u64 addr, u64 size, struct lock_class_key *, const char *name, struct nvkm_vmm **); +struct nvkm_vma *nvkm_vma_new(u64 addr, u64 size); struct nvkm_vma *nvkm_vmm_node_search(struct nvkm_vmm *, u64 addr); struct nvkm_vma *nvkm_vmm_node_split(struct nvkm_vmm *, struct nvkm_vma *, u64 addr, u64 size); @@ -173,6 +174,30 @@ void nvkm_vmm_put_locked(struct nvkm_vmm *, struct nvk= m_vma *); void nvkm_vmm_unmap_locked(struct nvkm_vmm *, struct nvkm_vma *, bool pfn); void nvkm_vmm_unmap_region(struct nvkm_vmm *, struct nvkm_vma *); =20 +int nvkm_vmm_raw_get(struct nvkm_vmm *vmm, u64 addr, u64 size, u8 refd); +void nvkm_vmm_raw_put(struct nvkm_vmm *vmm, u64 addr, u64 size, u8 refd); +void nvkm_vmm_raw_unmap(struct nvkm_vmm *vmm, u64 addr, u64 size, + bool sparse, u8 refd); +int nvkm_vmm_raw_sparse(struct nvkm_vmm *, u64 addr, u64 size, bool ref); + +static inline bool +nvkm_vmm_in_managed_range(struct nvkm_vmm *vmm, u64 start, u64 size) +{ + u64 p_start =3D vmm->managed.p.addr; + u64 p_end =3D p_start + vmm->managed.p.size; + u64 n_start =3D vmm->managed.n.addr; + u64 n_end =3D n_start + vmm->managed.n.size; + u64 end =3D start + size; + + if (start >=3D p_start && end <=3D p_end) + return true; + + if (start >=3D n_start && end <=3D n_end) + return true; + + return false; +} + #define NVKM_VMM_PFN_ADDR 0xfffffffffffff0= 00ULL #define NVKM_VMM_PFN_ADDR_SHIFT = 12 #define NVKM_VMM_PFN_APER 0x00000000000000= f0ULL diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmgf100.c b/drivers/g= pu/drm/nouveau/nvkm/subdev/mmu/vmmgf100.c index 5438384d9a67..5e857c02e9aa 100644 --- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmgf100.c +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmgf100.c @@ -287,15 +287,17 @@ gf100_vmm_valid(struct nvkm_vmm *vmm, void *argv, u32= argc, return -EINVAL; } =20 - ret =3D nvkm_memory_tags_get(memory, device, tags, - nvkm_ltc_tags_clear, - &map->tags); - if (ret) { - VMM_DEBUG(vmm, "comp %d", ret); - return ret; + if (!map->no_comp) { + ret =3D nvkm_memory_tags_get(memory, device, tags, + nvkm_ltc_tags_clear, + &map->tags); + if (ret) { + VMM_DEBUG(vmm, "comp %d", ret); + return ret; + } } =20 - if (map->tags->mn) { + if (!map->no_comp && map->tags->mn) { u64 tags =3D map->tags->mn->offset + (map->offset >> 17); if (page->shift =3D=3D 17 || !gm20x) { map->type |=3D tags << 44; diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmgp100.c b/drivers/g= pu/drm/nouveau/nvkm/subdev/mmu/vmmgp100.c index 17899fc95b2d..f3630d0e0d55 100644 --- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmgp100.c +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmgp100.c @@ -453,15 +453,17 @@ gp100_vmm_valid(struct nvkm_vmm *vmm, void *argv, u32= argc, return -EINVAL; } =20 - ret =3D nvkm_memory_tags_get(memory, device, tags, - nvkm_ltc_tags_clear, - &map->tags); - if (ret) { - VMM_DEBUG(vmm, "comp %d", ret); - return ret; + if (!map->no_comp) { + ret =3D nvkm_memory_tags_get(memory, device, tags, + nvkm_ltc_tags_clear, + &map->tags); + if (ret) { + VMM_DEBUG(vmm, "comp %d", ret); + return ret; + } } =20 - if (map->tags->mn) { + if (!map->no_comp && map->tags->mn) { tags =3D map->tags->mn->offset + (map->offset >> 16); map->ctag |=3D ((1ULL << page->shift) >> 16) << 36; map->type |=3D tags << 36; diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmnv50.c b/drivers/gp= u/drm/nouveau/nvkm/subdev/mmu/vmmnv50.c index b7548dcd72c7..ff08ad5005a9 100644 --- a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmnv50.c +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmnv50.c @@ -296,19 +296,22 @@ nv50_vmm_valid(struct nvkm_vmm *vmm, void *argv, u32 = argc, return -EINVAL; } =20 - ret =3D nvkm_memory_tags_get(memory, device, tags, NULL, - &map->tags); - if (ret) { - VMM_DEBUG(vmm, "comp %d", ret); - return ret; - } + if (!map->no_comp) { + ret =3D nvkm_memory_tags_get(memory, device, tags, NULL, + &map->tags); + if (ret) { + VMM_DEBUG(vmm, "comp %d", ret); + return ret; + } =20 - if (map->tags->mn) { - u32 tags =3D map->tags->mn->offset + (map->offset >> 16); - map->ctag |=3D (u64)comp << 49; - map->type |=3D (u64)comp << 47; - map->type |=3D (u64)tags << 49; - map->next |=3D map->ctag; + if (map->tags->mn) { + u32 tags =3D map->tags->mn->offset + + (map->offset >> 16); + map->ctag |=3D (u64)comp << 49; + map->type |=3D (u64)comp << 47; + map->type |=3D (u64)tags << 49; + map->next |=3D map->ctag; + } } } =20 --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A616EC64ED8 for ; Fri, 17 Feb 2023 13:50:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230107AbjBQNul (ORCPT ); Fri, 17 Feb 2023 08:50:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230008AbjBQNuL (ORCPT ); Fri, 17 Feb 2023 08:50:11 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE7A43B3C5 for ; Fri, 17 Feb 2023 05:49:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641742; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iIWonkC5C/glRECsErna8rexTQ7GLEMuQBk+usFaDoA=; b=GQaVYtnoDR0qawnFSoFh5geinYRwiAjZiT362GlVFApZqsgZMTP7sdcVkQxeaDOWqe7J6q Y2n25cPhSz+ub9JH2iKWBXtCNLJpW4egTXhMgyhctSdJVXX8ByOc1dnIqzv+ak6y6rQQvT KceI48i0fVWqLYRAQeRJEl4WwrIfd3U= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-653-cSuBkGtdNZGwgzJ1F5STCw-1; Fri, 17 Feb 2023 08:49:00 -0500 X-MC-Unique: cSuBkGtdNZGwgzJ1F5STCw-1 Received: by mail-ed1-f70.google.com with SMTP id j10-20020a05640211ca00b004acbf564d75so1096290edw.5 for ; Fri, 17 Feb 2023 05:49:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iIWonkC5C/glRECsErna8rexTQ7GLEMuQBk+usFaDoA=; b=2xb33qrObgvtOn1HV80/4tGUN/Ynyjw+mi+mJvU1hjopQ8a+iCb9o8rqGr1D5tI9gu voyFI8Bq+Y7XlmBQyniZczGYiBhZUo3CQZ4TqveuDj8EHXy45PKyETW4K+G7ir0XvT8N CD0/r0LHkFW0d8P08Fx+FeHG0mqGHsbuffyzecQwmTshizatt0XCfAML/SEql02KEHzx N7G4fFRoczYsgFxNwk7mXRph29aSHQinb4Vk64Ycfg9OrXnbprF2brZI45E7rYfzu1W5 iziGTfGw8UzIY0Xj6kDOogX4xOkXMgvcYwzdQtXUi1tLWMAfq7cojxlHeGzMz8HW0ked z8Pw== X-Gm-Message-State: AO0yUKXRtd7drYJbS1t35wU4kSK3i+Qj2rmijPdnJxZapFKgXJp8oT7Y ui+hO4UOgA0msnZv2g2znFYBxEy0uOoJEzkFCea2SQadXCyhH6Egg4RdhXV7WASHuNQjNUZLV09 tlDkuTIgUUL8bXxLgWLn1JkEV X-Received: by 2002:a05:6402:614:b0:4ad:7439:db03 with SMTP id n20-20020a056402061400b004ad7439db03mr3594319edv.12.1676641739042; Fri, 17 Feb 2023 05:48:59 -0800 (PST) X-Google-Smtp-Source: AK7set+SRUNF0RcZPReWAaplGNeyliS+73v8sATcOmGYtCQrEx1ENWuAncTyZ8oYhebOzec3XwW8/Q== X-Received: by 2002:a05:6402:614:b0:4ad:7439:db03 with SMTP id n20-20020a056402061400b004ad7439db03mr3594292edv.12.1676641738668; Fri, 17 Feb 2023 05:48:58 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id q1-20020a50aa81000000b004acbe5409b4sm2324194edc.48.2023.02.17.05.48.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:48:58 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 14/16] drm/nouveau: implement uvmm for user mode bindings Date: Fri, 17 Feb 2023 14:48:18 +0100 Message-Id: <20230217134820.14672-9-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" uvmm provides the driver abstraction around the DRM GPU VA manager connecting it to the nouveau infrastructure. It handles the split and merge operations provided by the DRM GPU VA manager for map operations colliding with existent mappings and takes care of the driver specific locking around the DRM GPU VA manager. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nouveau/Kbuild | 1 + drivers/gpu/drm/nouveau/nouveau_abi16.c | 7 + drivers/gpu/drm/nouveau/nouveau_bo.c | 147 +-- drivers/gpu/drm/nouveau/nouveau_bo.h | 2 +- drivers/gpu/drm/nouveau/nouveau_drm.c | 2 + drivers/gpu/drm/nouveau/nouveau_drv.h | 48 + drivers/gpu/drm/nouveau/nouveau_gem.c | 25 +- drivers/gpu/drm/nouveau/nouveau_mem.h | 5 + drivers/gpu/drm/nouveau/nouveau_prime.c | 2 +- drivers/gpu/drm/nouveau/nouveau_uvmm.c | 1090 +++++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_uvmm.h | 110 +++ 11 files changed, 1378 insertions(+), 61 deletions(-) create mode 100644 drivers/gpu/drm/nouveau/nouveau_uvmm.c create mode 100644 drivers/gpu/drm/nouveau/nouveau_uvmm.h diff --git a/drivers/gpu/drm/nouveau/Kbuild b/drivers/gpu/drm/nouveau/Kbuild index 5e5617006da5..ee281bb76463 100644 --- a/drivers/gpu/drm/nouveau/Kbuild +++ b/drivers/gpu/drm/nouveau/Kbuild @@ -47,6 +47,7 @@ nouveau-y +=3D nouveau_prime.o nouveau-y +=3D nouveau_sgdma.o nouveau-y +=3D nouveau_ttm.o nouveau-y +=3D nouveau_vmm.o +nouveau-y +=3D nouveau_uvmm.o =20 # DRM - modesetting nouveau-$(CONFIG_DRM_NOUVEAU_BACKLIGHT) +=3D nouveau_backlight.o diff --git a/drivers/gpu/drm/nouveau/nouveau_abi16.c b/drivers/gpu/drm/nouv= eau/nouveau_abi16.c index 82dab51d8aeb..36cc80eb0e20 100644 --- a/drivers/gpu/drm/nouveau/nouveau_abi16.c +++ b/drivers/gpu/drm/nouveau/nouveau_abi16.c @@ -261,6 +261,13 @@ nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS) if (!drm->channel) return nouveau_abi16_put(abi16, -ENODEV); =20 + /* If uvmm wasn't initialized until now disable it completely to prevent + * userspace from mixing up UAPIs. + * + * The client lock is already acquired by nouveau_abi16_get(). + */ + __nouveau_cli_uvmm_disable(cli); + device =3D &abi16->device; engine =3D NV_DEVICE_HOST_RUNLIST_ENGINES_GR; =20 diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau= /nouveau_bo.c index bf6984c8754c..f3d73d6edd46 100644 --- a/drivers/gpu/drm/nouveau/nouveau_bo.c +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c @@ -199,7 +199,7 @@ nouveau_bo_fixup_align(struct nouveau_bo *nvbo, int *al= ign, u64 *size) =20 struct nouveau_bo * nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size, int *align, u32 domai= n, - u32 tile_mode, u32 tile_flags) + u32 tile_mode, u32 tile_flags, bool internal) { struct nouveau_drm *drm =3D cli->drm; struct nouveau_bo *nvbo; @@ -235,68 +235,103 @@ nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size,= int *align, u32 domain, nvbo->force_coherent =3D true; } =20 - if (cli->device.info.family >=3D NV_DEVICE_INFO_V0_FERMI) { - nvbo->kind =3D (tile_flags & 0x0000ff00) >> 8; - if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) { - kfree(nvbo); - return ERR_PTR(-EINVAL); + nvbo->contig =3D !(tile_flags & NOUVEAU_GEM_TILE_NONCONTIG); + if (!nouveau_cli_uvmm(cli) || internal) { + /* for BO noVM allocs, don't assign kinds */ + if (cli->device.info.family >=3D NV_DEVICE_INFO_V0_FERMI) { + nvbo->kind =3D (tile_flags & 0x0000ff00) >> 8; + if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) { + kfree(nvbo); + return ERR_PTR(-EINVAL); + } + + nvbo->comp =3D mmu->kind[nvbo->kind] !=3D nvbo->kind; + } else if (cli->device.info.family >=3D NV_DEVICE_INFO_V0_TESLA) { + nvbo->kind =3D (tile_flags & 0x00007f00) >> 8; + nvbo->comp =3D (tile_flags & 0x00030000) >> 16; + if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) { + kfree(nvbo); + return ERR_PTR(-EINVAL); + } + } else { + nvbo->zeta =3D (tile_flags & 0x00000007); } + nvbo->mode =3D tile_mode; + + /* Determine the desirable target GPU page size for the buffer. */ + for (i =3D 0; i < vmm->page_nr; i++) { + /* Because we cannot currently allow VMM maps to fail + * during buffer migration, we need to determine page + * size for the buffer up-front, and pre-allocate its + * page tables. + * + * Skip page sizes that can't support needed domains. + */ + if (cli->device.info.family > NV_DEVICE_INFO_V0_CURIE && + (domain & NOUVEAU_GEM_DOMAIN_VRAM) && !vmm->page[i].vram) + continue; + if ((domain & NOUVEAU_GEM_DOMAIN_GART) && + (!vmm->page[i].host || vmm->page[i].shift > PAGE_SHIFT)) + continue; =20 - nvbo->comp =3D mmu->kind[nvbo->kind] !=3D nvbo->kind; - } else - if (cli->device.info.family >=3D NV_DEVICE_INFO_V0_TESLA) { - nvbo->kind =3D (tile_flags & 0x00007f00) >> 8; - nvbo->comp =3D (tile_flags & 0x00030000) >> 16; - if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) { + /* Select this page size if it's the first that supports + * the potential memory domains, or when it's compatible + * with the requested compression settings. + */ + if (pi < 0 || !nvbo->comp || vmm->page[i].comp) + pi =3D i; + + /* Stop once the buffer is larger than the current page size. */ + if (*size >=3D 1ULL << vmm->page[i].shift) + break; + } + + if (WARN_ON(pi < 0)) { kfree(nvbo); return ERR_PTR(-EINVAL); } - } else { - nvbo->zeta =3D (tile_flags & 0x00000007); - } - nvbo->mode =3D tile_mode; - nvbo->contig =3D !(tile_flags & NOUVEAU_GEM_TILE_NONCONTIG); - - /* Determine the desirable target GPU page size for the buffer. */ - for (i =3D 0; i < vmm->page_nr; i++) { - /* Because we cannot currently allow VMM maps to fail - * during buffer migration, we need to determine page - * size for the buffer up-front, and pre-allocate its - * page tables. - * - * Skip page sizes that can't support needed domains. - */ - if (cli->device.info.family > NV_DEVICE_INFO_V0_CURIE && - (domain & NOUVEAU_GEM_DOMAIN_VRAM) && !vmm->page[i].vram) - continue; - if ((domain & NOUVEAU_GEM_DOMAIN_GART) && - (!vmm->page[i].host || vmm->page[i].shift > PAGE_SHIFT)) - continue; =20 - /* Select this page size if it's the first that supports - * the potential memory domains, or when it's compatible - * with the requested compression settings. - */ - if (pi < 0 || !nvbo->comp || vmm->page[i].comp) - pi =3D i; - - /* Stop once the buffer is larger than the current page size. */ - if (*size >=3D 1ULL << vmm->page[i].shift) - break; - } + /* Disable compression if suitable settings couldn't be found. */ + if (nvbo->comp && !vmm->page[pi].comp) { + if (mmu->object.oclass >=3D NVIF_CLASS_MMU_GF100) + nvbo->kind =3D mmu->kind[nvbo->kind]; + nvbo->comp =3D 0; + } + nvbo->page =3D vmm->page[pi].shift; + } else { + /* reject other tile flags when in VM mode. */ + if (tile_mode) + return ERR_PTR(-EINVAL); + if (tile_flags & ~NOUVEAU_GEM_TILE_NONCONTIG) + return ERR_PTR(-EINVAL); =20 - if (WARN_ON(pi < 0)) { - kfree(nvbo); - return ERR_PTR(-EINVAL); - } + /* Determine the desirable target GPU page size for the buffer. */ + for (i =3D 0; i < vmm->page_nr; i++) { + /* Because we cannot currently allow VMM maps to fail + * during buffer migration, we need to determine page + * size for the buffer up-front, and pre-allocate its + * page tables. + * + * Skip page sizes that can't support needed domains. + */ + if ((domain & NOUVEAU_GEM_DOMAIN_VRAM) && !vmm->page[i].vram) + continue; + if ((domain & NOUVEAU_GEM_DOMAIN_GART) && + (!vmm->page[i].host || vmm->page[i].shift > PAGE_SHIFT)) + continue; =20 - /* Disable compression if suitable settings couldn't be found. */ - if (nvbo->comp && !vmm->page[pi].comp) { - if (mmu->object.oclass >=3D NVIF_CLASS_MMU_GF100) - nvbo->kind =3D mmu->kind[nvbo->kind]; - nvbo->comp =3D 0; + if (pi < 0) + pi =3D i; + /* Stop once the buffer is larger than the current page size. */ + if (*size >=3D 1ULL << vmm->page[i].shift) + break; + } + if (WARN_ON(pi < 0)) { + kfree(nvbo); + return ERR_PTR(-EINVAL); + } + nvbo->page =3D vmm->page[pi].shift; } - nvbo->page =3D vmm->page[pi].shift; =20 nouveau_bo_fixup_align(nvbo, align, size); =20 @@ -334,7 +369,7 @@ nouveau_bo_new(struct nouveau_cli *cli, u64 size, int a= lign, int ret; =20 nvbo =3D nouveau_bo_alloc(cli, &size, &align, domain, tile_mode, - tile_flags); + tile_flags, true); if (IS_ERR(nvbo)) return PTR_ERR(nvbo); =20 @@ -938,6 +973,7 @@ static void nouveau_bo_move_ntfy(struct ttm_buffer_obje= ct *bo, list_for_each_entry(vma, &nvbo->vma_list, head) { nouveau_vma_map(vma, mem); } + nouveau_uvmm_bo_map_all(nvbo, mem); } else { list_for_each_entry(vma, &nvbo->vma_list, head) { ret =3D dma_resv_wait_timeout(bo->base.resv, @@ -946,6 +982,7 @@ static void nouveau_bo_move_ntfy(struct ttm_buffer_obje= ct *bo, WARN_ON(ret <=3D 0); nouveau_vma_unmap(vma); } + nouveau_uvmm_bo_unmap_all(nvbo); } =20 if (new_reg) diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.h b/drivers/gpu/drm/nouveau= /nouveau_bo.h index 774dd93ca76b..cb85207d9e8f 100644 --- a/drivers/gpu/drm/nouveau/nouveau_bo.h +++ b/drivers/gpu/drm/nouveau/nouveau_bo.h @@ -73,7 +73,7 @@ extern struct ttm_device_funcs nouveau_bo_driver; =20 void nouveau_bo_move_init(struct nouveau_drm *); struct nouveau_bo *nouveau_bo_alloc(struct nouveau_cli *, u64 *size, int *= align, - u32 domain, u32 tile_mode, u32 tile_flags); + u32 domain, u32 tile_mode, u32 tile_flags, bool internal); int nouveau_bo_init(struct nouveau_bo *, u64 size, int align, u32 domain, struct sg_table *sg, struct dma_resv *robj); int nouveau_bo_new(struct nouveau_cli *, u64 size, int align, u32 domain, diff --git a/drivers/gpu/drm/nouveau/nouveau_drm.c b/drivers/gpu/drm/nouvea= u/nouveau_drm.c index cc7c5b4a05fd..cde843156700 100644 --- a/drivers/gpu/drm/nouveau/nouveau_drm.c +++ b/drivers/gpu/drm/nouveau/nouveau_drm.c @@ -68,6 +68,7 @@ #include "nouveau_platform.h" #include "nouveau_svm.h" #include "nouveau_dmem.h" +#include "nouveau_uvmm.h" =20 DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0, "DRM_UT_CORE", @@ -190,6 +191,7 @@ nouveau_cli_fini(struct nouveau_cli *cli) WARN_ON(!list_empty(&cli->worker)); =20 usif_client_fini(cli); + nouveau_uvmm_fini(&cli->uvmm); nouveau_vmm_fini(&cli->svm); nouveau_vmm_fini(&cli->vmm); nvif_mmu_dtor(&cli->mmu); diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouvea= u/nouveau_drv.h index 20a7f31b9082..d634f1054d65 100644 --- a/drivers/gpu/drm/nouveau/nouveau_drv.h +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h @@ -64,6 +64,7 @@ struct platform_device; #include "nouveau_fence.h" #include "nouveau_bios.h" #include "nouveau_vmm.h" +#include "nouveau_uvmm.h" =20 struct nouveau_drm_tile { struct nouveau_fence *fence; @@ -91,6 +92,8 @@ struct nouveau_cli { struct nvif_mmu mmu; struct nouveau_vmm vmm; struct nouveau_vmm svm; + struct nouveau_uvmm uvmm; + const struct nvif_mclass *mem; =20 struct list_head head; @@ -112,15 +115,60 @@ struct nouveau_cli_work { struct dma_fence_cb cb; }; =20 +static inline struct nouveau_uvmm * +nouveau_cli_uvmm(struct nouveau_cli *cli) +{ + if (!cli || !cli->uvmm.vmm.cli) + return NULL; + + return &cli->uvmm; +} + +static inline struct nouveau_uvmm * +nouveau_cli_uvmm_locked(struct nouveau_cli *cli) +{ + struct nouveau_uvmm *uvmm; + + mutex_lock(&cli->mutex); + uvmm =3D nouveau_cli_uvmm(cli); + mutex_unlock(&cli->mutex); + + return uvmm; +} + static inline struct nouveau_vmm * nouveau_cli_vmm(struct nouveau_cli *cli) { + struct nouveau_uvmm *uvmm; + + uvmm =3D nouveau_cli_uvmm(cli); + if (uvmm) + return &uvmm->vmm; + if (cli->svm.cli) return &cli->svm; =20 return &cli->vmm; } =20 +static inline void +__nouveau_cli_uvmm_disable(struct nouveau_cli *cli) +{ + struct nouveau_uvmm *uvmm; + + uvmm =3D nouveau_cli_uvmm(cli); + if (!uvmm) + cli->uvmm.disabled =3D true; +} + +static inline void +nouveau_cli_uvmm_disable(struct nouveau_cli *cli) +{ + mutex_lock(&cli->mutex); + __nouveau_cli_uvmm_disable(cli); + mutex_unlock(&cli->mutex); +} + void nouveau_cli_work_queue(struct nouveau_cli *, struct dma_fence *, struct nouveau_cli_work *); =20 diff --git a/drivers/gpu/drm/nouveau/nouveau_gem.c b/drivers/gpu/drm/nouvea= u/nouveau_gem.c index 4369c8dc8b5b..10c60b0a8dc8 100644 --- a/drivers/gpu/drm/nouveau/nouveau_gem.c +++ b/drivers/gpu/drm/nouveau/nouveau_gem.c @@ -120,7 +120,11 @@ nouveau_gem_object_open(struct drm_gem_object *gem, st= ruct drm_file *file_priv) goto out; } =20 - ret =3D nouveau_vma_new(nvbo, vmm, &vma); + /* only create a VMA on binding */ + if (!nouveau_cli_uvmm(cli)) + ret =3D nouveau_vma_new(nvbo, vmm, &vma); + else + ret =3D 0; pm_runtime_mark_last_busy(dev); pm_runtime_put_autosuspend(dev); out: @@ -187,6 +191,9 @@ nouveau_gem_object_close(struct drm_gem_object *gem, st= ruct drm_file *file_priv) if (vmm->vmm.object.oclass < NVIF_CLASS_VMM_NV50) return; =20 + if (nouveau_cli_uvmm(cli)) + return; + ret =3D ttm_bo_reserve(&nvbo->bo, false, false, NULL); if (ret) return; @@ -231,7 +238,7 @@ nouveau_gem_new(struct nouveau_cli *cli, u64 size, int = align, uint32_t domain, domain |=3D NOUVEAU_GEM_DOMAIN_CPU; =20 nvbo =3D nouveau_bo_alloc(cli, &size, &align, domain, tile_mode, - tile_flags); + tile_flags, false); if (IS_ERR(nvbo)) return PTR_ERR(nvbo); =20 @@ -279,13 +286,15 @@ nouveau_gem_info(struct drm_file *file_priv, struct d= rm_gem_object *gem, else rep->domain =3D NOUVEAU_GEM_DOMAIN_VRAM; rep->offset =3D nvbo->offset; - if (vmm->vmm.object.oclass >=3D NVIF_CLASS_VMM_NV50) { + if (vmm->vmm.object.oclass >=3D NVIF_CLASS_VMM_NV50 && + !nouveau_cli_uvmm(cli)) { vma =3D nouveau_vma_find(nvbo, vmm); if (!vma) return -EINVAL; =20 rep->offset =3D vma->addr; - } + } else + rep->offset =3D 0; =20 rep->size =3D nvbo->bo.base.size; rep->map_handle =3D drm_vma_node_offset_addr(&nvbo->bo.base.vma_node); @@ -310,6 +319,11 @@ nouveau_gem_ioctl_new(struct drm_device *dev, void *da= ta, struct nouveau_bo *nvbo =3D NULL; int ret =3D 0; =20 + /* If uvmm wasn't initialized until now disable it completely to prevent + * userspace from mixing up UAPIs. + */ + nouveau_cli_uvmm_disable(cli); + ret =3D nouveau_gem_new(cli, req->info.size, req->align, req->info.domain, req->info.tile_mode, req->info.tile_flags, &nvbo); @@ -715,6 +729,9 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void = *data, if (unlikely(!abi16)) return -ENOMEM; =20 + if (unlikely(nouveau_cli_uvmm(cli))) + return -ENOSYS; + list_for_each_entry(temp, &abi16->channels, head) { if (temp->chan->chid =3D=3D req->channel) { chan =3D temp->chan; diff --git a/drivers/gpu/drm/nouveau/nouveau_mem.h b/drivers/gpu/drm/nouvea= u/nouveau_mem.h index 76c86d8bb01e..5365a3d3a17f 100644 --- a/drivers/gpu/drm/nouveau/nouveau_mem.h +++ b/drivers/gpu/drm/nouveau/nouveau_mem.h @@ -35,4 +35,9 @@ int nouveau_mem_vram(struct ttm_resource *, bool contig, = u8 page); int nouveau_mem_host(struct ttm_resource *, struct ttm_tt *); void nouveau_mem_fini(struct nouveau_mem *); int nouveau_mem_map(struct nouveau_mem *, struct nvif_vmm *, struct nvif_v= ma *); +int +nouveau_mem_map_fixed(struct nouveau_mem *mem, + struct nvif_vmm *vmm, + u8 kind, u64 addr, + u64 offset, u64 range); #endif diff --git a/drivers/gpu/drm/nouveau/nouveau_prime.c b/drivers/gpu/drm/nouv= eau/nouveau_prime.c index f42c2b1b0363..6a883b9a799a 100644 --- a/drivers/gpu/drm/nouveau/nouveau_prime.c +++ b/drivers/gpu/drm/nouveau/nouveau_prime.c @@ -50,7 +50,7 @@ struct drm_gem_object *nouveau_gem_prime_import_sg_table(= struct drm_device *dev, =20 dma_resv_lock(robj, NULL); nvbo =3D nouveau_bo_alloc(&drm->client, &size, &align, - NOUVEAU_GEM_DOMAIN_GART, 0, 0); + NOUVEAU_GEM_DOMAIN_GART, 0, 0, true); if (IS_ERR(nvbo)) { obj =3D ERR_CAST(nvbo); goto unlock; diff --git a/drivers/gpu/drm/nouveau/nouveau_uvmm.c b/drivers/gpu/drm/nouve= au/nouveau_uvmm.c new file mode 100644 index 000000000000..2f7747a5a917 --- /dev/null +++ b/drivers/gpu/drm/nouveau/nouveau_uvmm.c @@ -0,0 +1,1090 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright (c) 2022 Red Hat. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software= "), + * to deal in the Software without restriction, including without limitati= on + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Danilo Krummrich + * + */ + +/* + * Locking: + * + * The uvmm mutex protects any operations on the GPU VA space provided by = the + * DRM GPU VA manager. + * + * The DRM GEM GPUVA lock protects a GEM's GPUVA list. It also protects si= ngle + * map/unmap operations against a BO move, which itself walks the GEM's GP= UVA + * list in order to map/unmap it's entries. + * + * We'd also need to protect the DRM_GPUVA_EVICTED flag for each individual + * GPUVA, however this isn't necessary since any read or write to this flag + * happens when we already took the DRM GEM GPUVA lock of the backing GEM = of + * the particular GPUVA. + */ + +#include "nouveau_drv.h" +#include "nouveau_gem.h" +#include "nouveau_mem.h" +#include "nouveau_uvmm.h" + +#include +#include + +#include +#include +#include + +#define NOUVEAU_VA_SPACE_BITS 47 /* FIXME */ +#define NOUVEAU_VA_SPACE_START 0x0 +#define NOUVEAU_VA_SPACE_END (1ULL << NOUVEAU_VA_SPACE_BITS) + +struct uvmm_map_args { + u64 addr; + u64 range; + u8 kind; +}; + +int +nouveau_uvmm_validate_range(struct nouveau_uvmm *uvmm, u64 addr, u64 range) +{ + u64 end =3D addr + range; + u64 unmanaged_end =3D uvmm->unmanaged_addr + + uvmm->unmanaged_size; + + if (addr & ~PAGE_MASK) + return -EINVAL; + + if (range & ~PAGE_MASK) + return -EINVAL; + + if (end <=3D addr) + return -EINVAL; + + if (addr < NOUVEAU_VA_SPACE_START || + end > NOUVEAU_VA_SPACE_END) + return -EINVAL; + + if (addr < unmanaged_end && + end > uvmm->unmanaged_addr) + return -EINVAL; + + return 0; +} + +static int +nouveau_uvmm_vmm_sparse_ref(struct nouveau_uvmm *uvmm, + u64 addr, u64 range) +{ + struct nvif_vmm *vmm =3D &uvmm->vmm.vmm; + + return nvif_vmm_raw_sparse(vmm, addr, range, true); +} + +static int +nouveau_uvmm_vmm_sparse_unref(struct nouveau_uvmm *uvmm, + u64 addr, u64 range) +{ + struct nvif_vmm *vmm =3D &uvmm->vmm.vmm; + + return nvif_vmm_raw_sparse(vmm, addr, range, false); +} + +static int +nouveau_uvmm_vmm_get(struct nouveau_uvmm *uvmm, + u64 addr, u64 range) +{ + struct nvif_vmm *vmm =3D &uvmm->vmm.vmm; + + return nvif_vmm_raw_get(vmm, addr, range, PAGE_SHIFT); +} + +static int +nouveau_uvmm_vmm_put(struct nouveau_uvmm *uvmm, + u64 addr, u64 range) +{ + struct nvif_vmm *vmm =3D &uvmm->vmm.vmm; + + return nvif_vmm_raw_put(vmm, addr, range, PAGE_SHIFT); +} + +static int +nouveau_uvmm_vmm_unmap(struct nouveau_uvmm *uvmm, + u64 addr, u64 range, bool sparse) +{ + struct nvif_vmm *vmm =3D &uvmm->vmm.vmm; + + return nvif_vmm_raw_unmap(vmm, addr, range, PAGE_SHIFT, sparse); +} + +static int +nouveau_uvmm_vmm_map(struct nouveau_uvmm *uvmm, + u64 addr, u64 range, + u64 bo_offset, u8 kind, + struct nouveau_mem *mem) +{ + struct nvif_vmm *vmm =3D &uvmm->vmm.vmm; + union { + struct gf100_vmm_map_v0 gf100; + } args; + u32 argc =3D 0; + + switch (vmm->object.oclass) { + case NVIF_CLASS_VMM_GF100: + case NVIF_CLASS_VMM_GM200: + case NVIF_CLASS_VMM_GP100: + args.gf100.version =3D 0; + if (mem->mem.type & NVIF_MEM_VRAM) + args.gf100.vol =3D 0; + else + args.gf100.vol =3D 1; + args.gf100.ro =3D 0; + args.gf100.priv =3D 0; + args.gf100.kind =3D kind; + argc =3D sizeof(args.gf100); + break; + default: + WARN_ON(1); + return -ENOSYS; + } + + return nvif_vmm_raw_map(vmm, addr, range, PAGE_SHIFT, + &args, argc, + &mem->mem, bo_offset); +} + +static int +nouveau_uvma_region_sparse_unref(struct nouveau_uvma_region *reg) +{ + u64 addr =3D reg->region.va.addr << PAGE_SHIFT; + u64 range =3D reg->region.va.range << PAGE_SHIFT; + + if (!reg->region.sparse) + return 0; + + return nouveau_uvmm_vmm_sparse_unref(reg->uvmm, addr, range); +} + +static int +nouveau_uvma_vmm_put(struct nouveau_uvma *uvma) +{ + u64 addr =3D uvma->va.va.addr << PAGE_SHIFT; + u64 range =3D uvma->va.va.range << PAGE_SHIFT; + + return nouveau_uvmm_vmm_put(uvma->uvmm, addr, range); +} + +static int +nouveau_uvma_map(struct nouveau_uvma *uvma, + struct nouveau_mem *mem) +{ + u64 addr =3D uvma->va.va.addr << PAGE_SHIFT; + u64 offset =3D uvma->va.gem.offset << PAGE_SHIFT; + u64 range =3D uvma->va.va.range << PAGE_SHIFT; + + return nouveau_uvmm_vmm_map(uvma->uvmm, addr, range, + offset, uvma->kind, mem); +} + +static int +nouveau_uvma_unmap(struct nouveau_uvma *uvma) +{ + u64 addr =3D uvma->va.va.addr << PAGE_SHIFT; + u64 range =3D uvma->va.va.range << PAGE_SHIFT; + bool sparse =3D uvma->va.region->sparse; + + if (drm_gpuva_evicted(&uvma->va)) + return 0; + + return nouveau_uvmm_vmm_unmap(uvma->uvmm, addr, range, sparse); +} + +static int +nouveau_uvma_alloc(struct nouveau_uvma **puvma) +{ + *puvma =3D kzalloc(sizeof(**puvma), GFP_KERNEL); + if (!*puvma) + return -ENOMEM; + + return 0; +} + +static void +nouveau_uvma_free(struct nouveau_uvma *uvma) +{ + kfree(uvma); +} + +static int +__nouveau_uvma_insert(struct nouveau_uvmm *uvmm, + struct nouveau_uvma *uvma) +{ + return drm_gpuva_insert(&uvmm->umgr, &uvma->va); +} + +static int +nouveau_uvma_insert(struct nouveau_uvmm *uvmm, + struct nouveau_uvma *uvma, + struct drm_gem_object *obj, + u64 bo_offset, u64 addr, + u64 range, u8 kind) +{ + int ret; + + addr >>=3D PAGE_SHIFT; + bo_offset >>=3D PAGE_SHIFT; + range >>=3D PAGE_SHIFT; + + uvma->uvmm =3D uvmm; + uvma->kind =3D kind; + uvma->va.va.addr =3D addr; + uvma->va.va.range =3D range; + uvma->va.gem.offset =3D bo_offset; + uvma->va.gem.obj =3D obj; + + ret =3D __nouveau_uvma_insert(uvmm, uvma); + if (ret) + return ret; + + return 0; +} + +static void +nouveau_uvma_remove(struct nouveau_uvma *uvma) +{ + drm_gpuva_remove(&uvma->va); +} + +static void +nouveau_uvma_gem_get(struct nouveau_uvma *uvma) +{ + drm_gem_object_get(uvma->va.gem.obj); +} + +static void +nouveau_uvma_gem_put(struct nouveau_uvma *uvma) +{ + drm_gem_object_put(uvma->va.gem.obj); +} + +static int +nouveau_uvma_region_alloc(struct nouveau_uvma_region **preg) +{ + *preg =3D kzalloc(sizeof(**preg), GFP_KERNEL); + if (!*preg) + return -ENOMEM; + + return 0; +} + +static void +nouveau_uvma_region_free(struct nouveau_uvma_region *reg) +{ + kfree(reg); +} + +static int +__nouveau_uvma_region_insert(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_region *reg) +{ + return drm_gpuva_region_insert(&uvmm->umgr, ®->region); +} + +static int +nouveau_uvma_region_insert(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_region *reg, + u64 addr, u64 range, + bool sparse) +{ + int ret; + + reg->uvmm =3D uvmm; + reg->region.va.addr =3D addr >> PAGE_SHIFT; + reg->region.va.range =3D range >> PAGE_SHIFT; + reg->region.sparse =3D sparse; + + ret =3D __nouveau_uvma_region_insert(uvmm, reg); + if (ret) + return ret; + + return 0; +} + +int +nouveau_uvma_region_create(struct nouveau_uvmm *uvmm, + u64 addr, u64 range, + bool sparse) +{ + struct nouveau_uvma_region *reg; + int ret; + + ret =3D nouveau_uvma_region_alloc(®); + if (ret) + return ret; + + ret =3D nouveau_uvma_region_insert(uvmm, reg, addr, range, sparse); + if (ret) + goto err_free_region; + + if (sparse) { + ret =3D nouveau_uvmm_vmm_sparse_ref(uvmm, addr, range); + if (ret) + goto err_region_remove; + } + + return 0; + +err_region_remove: + drm_gpuva_region_remove(®->region); +err_free_region: + nouveau_uvma_region_free(reg); + return ret; +} + +static struct nouveau_uvma_region * +nouveau_uvma_region_find(struct nouveau_uvmm *uvmm, + u64 addr, u64 range) +{ + struct drm_gpuva_region *reg; + + reg =3D drm_gpuva_region_find(&uvmm->umgr, + addr >> PAGE_SHIFT, + range >> PAGE_SHIFT); + if (!reg) + return NULL; + + return uvma_region_from_va_region(reg); +} + +static void +nouveau_uvma_region_remove(struct nouveau_uvma_region *reg) +{ + drm_gpuva_region_remove(®->region); +} + +int +__nouveau_uvma_region_destroy(struct nouveau_uvma_region *reg) +{ + struct nouveau_uvmm *uvmm =3D reg->uvmm; + u64 addr =3D reg->region.va.addr << PAGE_SHIFT; + u64 range =3D reg->region.va.range << PAGE_SHIFT; + bool sparse =3D reg->region.sparse; + + if (!drm_gpuva_region_empty(®->region)) + return -EBUSY; + + nouveau_uvma_region_remove(reg); + + if (sparse) + nouveau_uvmm_vmm_sparse_unref(uvmm, addr, range); + + nouveau_uvma_region_free(reg); + + return 0; +} + +int +nouveau_uvma_region_destroy(struct nouveau_uvmm *uvmm, + u64 addr, u64 range) +{ + struct nouveau_uvma_region *reg; + + reg =3D nouveau_uvma_region_find(uvmm, addr, range); + if (!reg) + return -ENOENT; + + return __nouveau_uvma_region_destroy(reg); +} + +static void +op_map_prepare_unwind(struct nouveau_uvma *uvma) +{ + nouveau_uvma_gem_put(uvma); + nouveau_uvma_remove(uvma); + nouveau_uvma_free(uvma); +} + +static void +op_unmap_prepare_unwind(struct drm_gpuva *va) +{ + drm_gpuva_insert(va->mgr, va); +} + +static void +uvmm_sm_prepare_unwind(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops, + struct drm_gpuva_op *last, + struct uvmm_map_args *args) +{ + struct drm_gpuva_op *op =3D last; + u64 vmm_get_start =3D args ? args->addr : 0; + u64 vmm_get_end =3D args ? args->addr + args->range : 0; + + /* Unwind GPUVA space. */ + drm_gpuva_for_each_op_from_reverse(op, ops) { + switch (op->op) { + case DRM_GPUVA_OP_MAP: + op_map_prepare_unwind(new->map); + break; + case DRM_GPUVA_OP_REMAP: { + struct drm_gpuva_op_remap *r =3D &op->remap; + + if (r->next) + op_map_prepare_unwind(new->next); + + if (r->prev) + op_map_prepare_unwind(new->prev); + + op_unmap_prepare_unwind(r->unmap->va); + break; + } + case DRM_GPUVA_OP_UNMAP: + op_unmap_prepare_unwind(op->unmap.va); + break; + default: + break; + } + } + + /* Unmap operation don't allocate page tables, hence skip the following + * page table unwind. + */ + if (!args) + return; + + drm_gpuva_for_each_op(op, ops) { + switch (op->op) { + case DRM_GPUVA_OP_MAP: { + u64 vmm_get_range =3D vmm_get_end - vmm_get_start; + + if (vmm_get_range) + nouveau_uvmm_vmm_put(uvmm, vmm_get_start, + vmm_get_range); + break; + } + case DRM_GPUVA_OP_REMAP: { + struct drm_gpuva_op_remap *r =3D &op->remap; + struct drm_gpuva *va =3D r->unmap->va; + u64 ustart =3D va->va.addr << PAGE_SHIFT; + u64 urange =3D va->va.range << PAGE_SHIFT; + u64 uend =3D ustart + urange; + + if (r->prev) + vmm_get_start =3D uend; + + if (r->next) + vmm_get_end =3D ustart; + + if (r->prev && r->next) + vmm_get_start =3D vmm_get_end =3D 0; + + break; + } + case DRM_GPUVA_OP_UNMAP: { + struct drm_gpuva_op_unmap *u =3D &op->unmap; + struct drm_gpuva *va =3D u->va; + u64 ustart =3D va->va.addr << PAGE_SHIFT; + u64 urange =3D va->va.range << PAGE_SHIFT; + u64 uend =3D ustart + urange; + + /* Nothing to do for mappings we merge with. */ + if (uend =3D=3D vmm_get_start || + ustart =3D=3D vmm_get_end) + break; + + if (ustart > vmm_get_start) { + u64 vmm_get_range =3D ustart - vmm_get_start; + + nouveau_uvmm_vmm_put(uvmm, vmm_get_start, + vmm_get_range); + } + vmm_get_start =3D uend; + break; + } + default: + break; + } + + if (op =3D=3D last) + break; + } +} + +void +nouveau_uvmm_sm_map_prepare_unwind(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops, + u64 addr, u64 range) +{ + struct drm_gpuva_op *last =3D drm_gpuva_last_op(ops); + struct uvmm_map_args args =3D { + .addr =3D addr, + .range =3D range, + }; + + uvmm_sm_prepare_unwind(uvmm, new, ops, last, &args); +} + +void +nouveau_uvmm_sm_unmap_prepare_unwind(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops) +{ + struct drm_gpuva_op *last =3D drm_gpuva_last_op(ops); + + uvmm_sm_prepare_unwind(uvmm, new, ops, last, NULL); +} + +static int +op_map_prepare(struct nouveau_uvmm *uvmm, + struct nouveau_uvma **puvma, + struct drm_gpuva_op_map *m, + struct uvmm_map_args *args) +{ + struct nouveau_uvma *uvma; + int ret; + + ret =3D nouveau_uvma_alloc(&uvma); + if (ret) + goto err; + + ret =3D nouveau_uvma_insert(uvmm, uvma, m->gem.obj, + m->gem.offset << PAGE_SHIFT, + m->va.addr << PAGE_SHIFT, + m->va.range << PAGE_SHIFT, + args->kind); + if (ret) + goto err_free_uvma; + + /* Keep a reference until this uvma is destroyed. */ + nouveau_uvma_gem_get(uvma); + + *puvma =3D uvma; + return 0; + +err_free_uvma: + nouveau_uvma_free(uvma); +err: + *puvma =3D NULL; + return ret; +} + +static void +op_unmap_prepare(struct drm_gpuva_op_unmap *u) +{ + struct nouveau_uvma *uvma =3D uvma_from_va(u->va); + + nouveau_uvma_remove(uvma); +} + +static int +uvmm_sm_prepare(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops, + struct uvmm_map_args *args) +{ + struct drm_gpuva_op *op; + u64 vmm_get_start =3D args ? args->addr : 0; + u64 vmm_get_end =3D args ? args->addr + args->range : 0; + int ret; + + drm_gpuva_for_each_op(op, ops) { + switch (op->op) { + case DRM_GPUVA_OP_MAP: { + u64 vmm_get_range =3D vmm_get_end - vmm_get_start; + + ret =3D op_map_prepare(uvmm, &new->map, &op->map, args); + if (ret) + goto unwind; + + if (args && vmm_get_range) { + ret =3D nouveau_uvmm_vmm_get(uvmm, vmm_get_start, + vmm_get_range); + if (ret) { + op_map_prepare_unwind(new->map); + goto unwind; + } + } + break; + } + case DRM_GPUVA_OP_REMAP: { + struct drm_gpuva_op_remap *r =3D &op->remap; + struct drm_gpuva *va =3D r->unmap->va; + struct uvmm_map_args remap_args =3D { + .kind =3D uvma_from_va(va)->kind, + }; + u64 ustart =3D va->va.addr << PAGE_SHIFT; + u64 urange =3D va->va.range << PAGE_SHIFT; + u64 uend =3D ustart + urange; + + op_unmap_prepare(r->unmap); + + if (r->prev) { + ret =3D op_map_prepare(uvmm, &new->prev, r->prev, + &remap_args); + if (ret) + goto unwind; + + if (args) + vmm_get_start =3D uend; + } + + if (r->next) { + ret =3D op_map_prepare(uvmm, &new->next, r->next, + &remap_args); + if (ret) { + if (r->prev) + op_map_prepare_unwind(new->prev); + goto unwind; + } + + if (args) + vmm_get_end =3D ustart; + } + + if (args && (r->prev && r->next)) + vmm_get_start =3D vmm_get_end =3D 0; + + break; + } + case DRM_GPUVA_OP_UNMAP: { + struct drm_gpuva_op_unmap *u =3D &op->unmap; + struct drm_gpuva *va =3D u->va; + u64 ustart =3D va->va.addr << PAGE_SHIFT; + u64 urange =3D va->va.range << PAGE_SHIFT; + u64 uend =3D ustart + urange; + + op_unmap_prepare(u); + + if (!args) + break; + + /* Nothing to do for mappings we merge with. */ + if (uend =3D=3D vmm_get_start || + ustart =3D=3D vmm_get_end) + break; + + if (ustart > vmm_get_start) { + u64 vmm_get_range =3D ustart - vmm_get_start; + + ret =3D nouveau_uvmm_vmm_get(uvmm, vmm_get_start, + vmm_get_range); + if (ret) { + op_unmap_prepare_unwind(va); + goto unwind; + } + } + vmm_get_start =3D uend; + + break; + } + default: + ret =3D -EINVAL; + goto unwind; + } + } + + return 0; + +unwind: + if (op !=3D drm_gpuva_first_op(ops)) + uvmm_sm_prepare_unwind(uvmm, new, ops, + drm_gpuva_prev_op(op), + args); + return ret; +} + +int +nouveau_uvmm_sm_map_prepare(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops, + u64 addr, u64 range, u8 kind) +{ + struct uvmm_map_args args =3D { + .addr =3D addr, + .range =3D range, + .kind =3D kind, + }; + + return uvmm_sm_prepare(uvmm, new, ops, &args); +} + +int +nouveau_uvmm_sm_unmap_prepare(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops) +{ + return uvmm_sm_prepare(uvmm, new, ops, NULL); +} + +struct drm_gpuva_ops * +nouveau_uvmm_sm_map_ops(struct nouveau_uvmm *uvmm, + u64 addr, u64 range, + struct drm_gem_object *obj, u64 offset) +{ + return drm_gpuva_sm_map_ops_create(&uvmm->umgr, + addr >> PAGE_SHIFT, + range >> PAGE_SHIFT, + obj, offset >> PAGE_SHIFT); +} + +struct drm_gpuva_ops * +nouveau_uvmm_sm_unmap_ops(struct nouveau_uvmm *uvmm, + u64 addr, u64 range) +{ + return drm_gpuva_sm_unmap_ops_create(&uvmm->umgr, + addr >> PAGE_SHIFT, + range >> PAGE_SHIFT); +} + +static struct drm_gem_object * +op_gem_obj(struct drm_gpuva_op *op) +{ + switch (op->op) { + case DRM_GPUVA_OP_MAP: + return op->map.gem.obj; + case DRM_GPUVA_OP_REMAP: + return op->remap.unmap->va->gem.obj; + case DRM_GPUVA_OP_UNMAP: + return op->unmap.va->gem.obj; + default: + WARN(1, "Unknown operation.\n"); + return NULL; + } +} + +static void +op_map(struct nouveau_uvma *uvma) +{ + struct nouveau_bo *nvbo =3D nouveau_gem_object(uvma->va.gem.obj); + + nouveau_uvma_map(uvma, nouveau_mem(nvbo->bo.resource)); + drm_gpuva_link(&uvma->va); +} + +static void +op_unmap(struct drm_gpuva_op_unmap *u) +{ + struct drm_gpuva *va =3D u->va; + struct nouveau_uvma *uvma =3D uvma_from_va(va); + + /* nouveau_uvma_unmap() does not try to unmap if backing BO is + * evicted. + */ + if (!u->keep) + nouveau_uvma_unmap(uvma); + drm_gpuva_unlink(va); +} + +static void +op_unmap_range(struct drm_gpuva_op_unmap *u, + u64 addr, u64 range) +{ + struct nouveau_uvma *uvma =3D uvma_from_va(u->va); + bool sparse =3D uvma->va.region->sparse; + + addr <<=3D PAGE_SHIFT; + range <<=3D PAGE_SHIFT; + + if (!drm_gpuva_evicted(u->va)) + nouveau_uvmm_vmm_unmap(uvma->uvmm, addr, range, sparse); + + drm_gpuva_unlink(u->va); +} + +static void +op_remap(struct drm_gpuva_op_remap *r, + struct nouveau_uvma_alloc *new) +{ + struct drm_gpuva_op_unmap *u =3D r->unmap; + struct nouveau_uvma *uvma =3D uvma_from_va(u->va); + u64 addr =3D uvma->va.va.addr; + u64 range =3D uvma->va.va.range; + + if (r->prev) { + addr =3D r->prev->va.addr + r->prev->va.range; + drm_gpuva_link(&new->prev->va); + } + + if (r->next) { + range =3D r->next->va.addr - addr; + drm_gpuva_link(&new->next->va); + } + + op_unmap_range(u, addr, range); +} + +static int +uvmm_sm(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops) +{ + struct drm_gpuva_op *op; + + drm_gpuva_for_each_op(op, ops) { + struct drm_gem_object *obj =3D op_gem_obj(op); + + if (!obj) + return -EINVAL; + + drm_gem_gpuva_lock(obj); + switch (op->op) { + case DRM_GPUVA_OP_MAP: + op_map(new->map); + break; + case DRM_GPUVA_OP_REMAP: { + op_remap(&op->remap, new); + break; + } + case DRM_GPUVA_OP_UNMAP: + op_unmap(&op->unmap); + break; + default: + break; + } + drm_gem_gpuva_unlock(obj); + } + + return 0; +} + +int +nouveau_uvmm_sm_map(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops) +{ + return uvmm_sm(uvmm, new, ops); +} + +int +nouveau_uvmm_sm_unmap(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops) +{ + return uvmm_sm(uvmm, new, ops); +} + +static void +uvmm_sm_cleanup(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops, bool unmap) +{ + struct drm_gpuva_op *op; + + drm_gpuva_for_each_op(op, ops) { + switch (op->op) { + case DRM_GPUVA_OP_MAP: + break; + case DRM_GPUVA_OP_REMAP: { + struct drm_gpuva_op_remap *r =3D &op->remap; + struct drm_gpuva_op_map *p =3D r->prev; + struct drm_gpuva_op_map *n =3D r->next; + struct drm_gpuva *va =3D r->unmap->va; + struct nouveau_uvma *uvma =3D uvma_from_va(va); + + if (unmap) { + u64 addr =3D va->va.addr << PAGE_SHIFT; + u64 end =3D addr + (va->va.range << PAGE_SHIFT); + + if (p) + addr =3D (p->va.addr << PAGE_SHIFT) + + (n->va.range << PAGE_SHIFT); + + if (n) + end =3D n->va.addr << PAGE_SHIFT; + + nouveau_uvmm_vmm_put(uvmm, addr, end - addr); + } + + nouveau_uvma_gem_put(uvma); + nouveau_uvma_free(uvma); + break; + } + case DRM_GPUVA_OP_UNMAP: { + struct drm_gpuva_op_unmap *u =3D &op->unmap; + struct drm_gpuva *va =3D u->va; + struct nouveau_uvma *uvma =3D uvma_from_va(va); + + if (unmap) + nouveau_uvma_vmm_put(uvma); + + nouveau_uvma_gem_put(uvma); + nouveau_uvma_free(uvma); + break; + } + default: + break; + } + } +} + +void +nouveau_uvmm_sm_map_cleanup(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops) +{ + uvmm_sm_cleanup(uvmm, new, ops, false); +} + +void +nouveau_uvmm_sm_unmap_cleanup(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops) +{ + uvmm_sm_cleanup(uvmm, new, ops, true); +} + +void +nouveau_uvmm_bo_map_all(struct nouveau_bo *nvbo, struct nouveau_mem *mem) +{ + struct drm_gem_object *obj =3D &nvbo->bo.base; + struct drm_gpuva *va; + + drm_gem_gpuva_lock(obj); + drm_gem_for_each_gpuva(va, obj) { + struct nouveau_uvma *uvma =3D uvma_from_va(va); + + nouveau_uvma_map(uvma, mem); + drm_gpuva_evict(va, false); + } + drm_gem_gpuva_unlock(obj); +} + +void +nouveau_uvmm_bo_unmap_all(struct nouveau_bo *nvbo) +{ + struct drm_gem_object *obj =3D &nvbo->bo.base; + struct drm_gpuva *va; + + drm_gem_gpuva_lock(obj); + drm_gem_for_each_gpuva(va, obj) { + struct nouveau_uvma *uvma =3D uvma_from_va(va); + + nouveau_uvma_unmap(uvma); + drm_gpuva_evict(va, true); + } + drm_gem_gpuva_unlock(obj); +} + +int +nouveau_uvmm_init(struct nouveau_uvmm *uvmm, struct nouveau_cli *cli, + struct drm_nouveau_vm_init *init) +{ + int ret; + u64 unmanaged_end =3D init->unmanaged_addr + init->unmanaged_size; + + mutex_init(&uvmm->mutex); + + mutex_lock(&cli->mutex); + + if (unlikely(cli->uvmm.disabled)) { + ret =3D -ENOSYS; + goto out_unlock; + } + + if (unmanaged_end <=3D init->unmanaged_addr) { + ret =3D -EINVAL; + goto out_unlock; + } + + if (unmanaged_end > NOUVEAU_VA_SPACE_END) { + ret =3D -EINVAL; + goto out_unlock; + } + + uvmm->unmanaged_addr =3D init->unmanaged_addr; + uvmm->unmanaged_size =3D init->unmanaged_size; + + drm_gpuva_manager_init(&uvmm->umgr, cli->name, + NOUVEAU_VA_SPACE_START >> PAGE_SHIFT, + NOUVEAU_VA_SPACE_END >> PAGE_SHIFT, + init->unmanaged_addr >> PAGE_SHIFT, + init->unmanaged_size >> PAGE_SHIFT, + NULL, DRM_GPUVA_MANAGER_REGIONS); + + ret =3D nvif_vmm_ctor(&cli->mmu, "uvmm", + cli->vmm.vmm.object.oclass, RAW, + init->unmanaged_addr, init->unmanaged_size, + NULL, 0, &cli->uvmm.vmm.vmm); + if (ret) + goto out_free_gpuva_mgr; + + cli->uvmm.vmm.cli =3D cli; + mutex_unlock(&cli->mutex); + + return 0; + +out_free_gpuva_mgr: + drm_gpuva_manager_destroy(&uvmm->umgr); +out_unlock: + mutex_unlock(&cli->mutex); + return ret; +} + +void +nouveau_uvmm_fini(struct nouveau_uvmm *uvmm) +{ + DRM_GPUVA_ITER(it, &uvmm->umgr); + DRM_GPUVA_REGION_ITER(__it, &uvmm->umgr); + struct nouveau_cli *cli =3D uvmm->vmm.cli; + + if (!cli) + return; + + nouveau_uvmm_lock(uvmm); + drm_gpuva_iter_for_each(it) { + struct drm_gpuva *va =3D it.va; + struct nouveau_uvma *uvma =3D uvma_from_va(va); + struct drm_gem_object *obj =3D va->gem.obj; + + drm_gpuva_iter_remove(&it); + + drm_gem_gpuva_lock(obj); + nouveau_uvma_unmap(uvma); + drm_gpuva_unlink(va); + drm_gem_gpuva_unlock(obj); + + nouveau_uvma_vmm_put(uvma); + + nouveau_uvma_gem_put(uvma); + nouveau_uvma_free(uvma); + } + + drm_gpuva_iter_for_each(__it) { + struct drm_gpuva_region *reg =3D __it.reg; + struct nouveau_uvma_region *ureg =3D uvma_region_from_va_region(reg); + + if (unlikely(reg =3D=3D &uvmm->umgr.kernel_alloc_region)) + continue; + + drm_gpuva_iter_remove(&__it); + + nouveau_uvma_region_sparse_unref(ureg); + nouveau_uvma_region_free(ureg); + } + nouveau_uvmm_unlock(uvmm); + + mutex_lock(&cli->mutex); + nouveau_vmm_fini(&uvmm->vmm); + drm_gpuva_manager_destroy(&uvmm->umgr); + mutex_unlock(&cli->mutex); +} diff --git a/drivers/gpu/drm/nouveau/nouveau_uvmm.h b/drivers/gpu/drm/nouve= au/nouveau_uvmm.h new file mode 100644 index 000000000000..858840e9e0c5 --- /dev/null +++ b/drivers/gpu/drm/nouveau/nouveau_uvmm.h @@ -0,0 +1,110 @@ +/* SPDX-License-Identifier: MIT */ + +#ifndef __NOUVEAU_UVMM_H__ +#define __NOUVEAU_UVMM_H__ + +#include + +#include "nouveau_drv.h" + +struct nouveau_uvmm { + struct nouveau_vmm vmm; + struct drm_gpuva_manager umgr; + struct mutex mutex; + + u64 unmanaged_addr; + u64 unmanaged_size; + + bool disabled; +}; + +struct nouveau_uvma_region { + struct drm_gpuva_region region; + struct nouveau_uvmm *uvmm; +}; + +struct nouveau_uvma { + struct drm_gpuva va; + struct nouveau_uvmm *uvmm; + u64 handle; + u8 kind; +}; + +struct nouveau_uvma_alloc { + struct nouveau_uvma *map; + struct nouveau_uvma *prev; + struct nouveau_uvma *next; +}; + +#define uvmm_from_mgr(x) container_of((x), struct nouveau_uvmm, umgr) +#define uvma_from_va(x) container_of((x), struct nouveau_uvma, va) +#define uvma_region_from_va_region(x) container_of((x), struct nouveau_uvm= a_region, region) + +int nouveau_uvmm_init(struct nouveau_uvmm *uvmm, struct nouveau_cli *cli, + struct drm_nouveau_vm_init *init); +void nouveau_uvmm_fini(struct nouveau_uvmm *uvmm); + +void nouveau_uvmm_bo_map_all(struct nouveau_bo *nvbov, struct nouveau_mem = *mem); +void nouveau_uvmm_bo_unmap_all(struct nouveau_bo *nvbo); + +int nouveau_uvmm_validate_range(struct nouveau_uvmm *uvmm, + u64 addr, u64 range); + +int nouveau_uvma_region_create(struct nouveau_uvmm *uvmm, + u64 addr, u64 range, + bool sparse); +int __nouveau_uvma_region_destroy(struct nouveau_uvma_region *reg); +int nouveau_uvma_region_destroy(struct nouveau_uvmm *uvmm, + u64 addr, u64 range); + +struct drm_gpuva_ops * +nouveau_uvmm_sm_map_ops(struct nouveau_uvmm *uvmm, + u64 addr, u64 range, + struct drm_gem_object *obj, u64 offset); +struct drm_gpuva_ops * +nouveau_uvmm_sm_unmap_ops(struct nouveau_uvmm *uvmm, + u64 addr, u64 range); + +void +nouveau_uvmm_sm_map_prepare_unwind(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops, + u64 addr, u64 range); +void +nouveau_uvmm_sm_unmap_prepare_unwind(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops); + +int nouveau_uvmm_sm_map_prepare(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops, + u64 addr, u64 range, u8 kind); +int nouveau_uvmm_sm_unmap_prepare(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops); + +int nouveau_uvmm_sm_map(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops); +int nouveau_uvmm_sm_unmap(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops); + +void nouveau_uvmm_sm_map_cleanup(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops); +void nouveau_uvmm_sm_unmap_cleanup(struct nouveau_uvmm *uvmm, + struct nouveau_uvma_alloc *new, + struct drm_gpuva_ops *ops); + +static inline void nouveau_uvmm_lock(struct nouveau_uvmm *uvmm) +{ + mutex_lock(&uvmm->mutex); +} + +static inline void nouveau_uvmm_unlock(struct nouveau_uvmm *uvmm) +{ + mutex_unlock(&uvmm->mutex); +} + +#endif --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E7E9C05027 for ; Fri, 17 Feb 2023 13:50:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229968AbjBQNuo (ORCPT ); Fri, 17 Feb 2023 08:50:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230012AbjBQNuL (ORCPT ); Fri, 17 Feb 2023 08:50:11 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB052642CE for ; Fri, 17 Feb 2023 05:49:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641745; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1gEEEVEriS4+00Hosnu6p0Wtu19O1LPFPG0fsqoPCf8=; b=Gl5bJ2YswY6Ybjh4itGdr7mgAoMOPzAUPnnuF0qTUZQxpyPV4SquM/+Emi9scF3ZfV5AKR LAqu8Rv8Sm0sK/XtmPWBvuq85npTWExu+d/j7y+Lmrsm1IlxOZPevJceKPtP1bYIUpFSKD Oc0+2dXfmsiPaxqROI5MLuID+yaGm0c= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-512-L_ZhcNYaO1u_bRY4qgvUtA-1; Fri, 17 Feb 2023 08:49:04 -0500 X-MC-Unique: L_ZhcNYaO1u_bRY4qgvUtA-1 Received: by mail-ed1-f69.google.com with SMTP id x14-20020a05640226ce00b004acc4f8aa3fso1828228edd.3 for ; Fri, 17 Feb 2023 05:49:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1gEEEVEriS4+00Hosnu6p0Wtu19O1LPFPG0fsqoPCf8=; b=3Td3oX5H5C6D2GyzNQgO2omgKASOEUEOHdocAlsqnX8mhvRjSB4KfBTsGuAKaRzKUy hXpZKiui0ZYWuDg+iz3YrykYUAlY5ePjZHHuI7dI07kdkiMbu3idREIHzdPg1rseMvMw pxxqbEHx7UFJv2Pr37L7AyL/NuxzuCR03lFKAITY3APJw0TZ4X3IHCkUTxaGEJ3bgVLx krYT5rfLNfCd8L7rTcVuD62tkjwUWKU5m8MTBoXuKXWW+C12+9IbbNL9znYCNkM4AgwX MTAyHNjsN+fO6qMFPgEWkwMbDFWapoV2Jf5eoxalMgvuVyWRl9T1GXfzbDN3gtx8f0l6 6flw== X-Gm-Message-State: AO0yUKXyGz/4wXyzjZJn/NcwGJASf8poppcex/UoPmg5uV2zfBqbIm+O W7GA4DXUMQHRtHyyj88rG59W9QwCnHS7HK55DIIs08NEM5WsFO/8zvtJL0RIxoWRquYu25tIJBV 71vcGMy1jW68h3a/Hr/327LAh X-Received: by 2002:a17:906:374d:b0:886:ec6e:4c1 with SMTP id e13-20020a170906374d00b00886ec6e04c1mr633408ejc.59.1676641743066; Fri, 17 Feb 2023 05:49:03 -0800 (PST) X-Google-Smtp-Source: AK7set/r/69ni+Mba8zkIatb38Z/tPHhrGIr7lvXbTHyyRyCQvluYDtYhE4iPQaBSZPM3iki4Tqzrg== X-Received: by 2002:a17:906:374d:b0:886:ec6e:4c1 with SMTP id e13-20020a170906374d00b00886ec6e04c1mr633385ejc.59.1676641742698; Fri, 17 Feb 2023 05:49:02 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id um6-20020a170906cf8600b008b13b0dabcasm2154793ejb.182.2023.02.17.05.49.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:49:02 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 15/16] drm/nouveau: implement new VM_BIND UAPI Date: Fri, 17 Feb 2023 14:48:19 +0100 Message-Id: <20230217134820.14672-10-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This commit provides the implementation for the new uapi motivated by the Vulkan API. It allows user mode drivers (UMDs) to: 1) Initialize a GPU virtual address (VA) space via the new DRM_IOCTL_NOUVEAU_VM_INIT ioctl for UMDs to specify the portion of VA space managed by the kernel and userspace, respectively. 2) Allocate and free a VA space region as well as bind and unbind memory to the GPUs VA space via the new DRM_IOCTL_NOUVEAU_VM_BIND ioctl. UMDs can request the named operations to be processed either synchronously or asynchronously. It supports DRM syncobjs (incl. timelines) as synchronization mechanism. The management of the GPU VA mappings is implemented with the DRM GPU VA manager. 3) Execute push buffers with the new DRM_IOCTL_NOUVEAU_EXEC ioctl. The execution happens asynchronously. It supports DRM syncobj (incl. timelines) as synchronization mechanism. DRM GEM object locking is handled with drm_exec. Both, DRM_IOCTL_NOUVEAU_VM_BIND and DRM_IOCTL_NOUVEAU_EXEC, use the DRM GPU scheduler for the asynchronous paths. Signed-off-by: Danilo Krummrich --- Documentation/gpu/driver-uapi.rst | 3 + drivers/gpu/drm/nouveau/Kbuild | 2 + drivers/gpu/drm/nouveau/Kconfig | 2 + drivers/gpu/drm/nouveau/nouveau_abi16.c | 16 + drivers/gpu/drm/nouveau/nouveau_abi16.h | 1 + drivers/gpu/drm/nouveau/nouveau_drm.c | 24 +- drivers/gpu/drm/nouveau/nouveau_drv.h | 9 +- drivers/gpu/drm/nouveau/nouveau_exec.c | 322 ++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_exec.h | 39 ++ drivers/gpu/drm/nouveau/nouveau_sched.c | 467 ++++++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_sched.h | 96 +++++ drivers/gpu/drm/nouveau/nouveau_uvmm.c | 446 ++++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_uvmm.h | 28 ++ 13 files changed, 1451 insertions(+), 4 deletions(-) create mode 100644 drivers/gpu/drm/nouveau/nouveau_exec.c create mode 100644 drivers/gpu/drm/nouveau/nouveau_exec.h create mode 100644 drivers/gpu/drm/nouveau/nouveau_sched.c create mode 100644 drivers/gpu/drm/nouveau/nouveau_sched.h diff --git a/Documentation/gpu/driver-uapi.rst b/Documentation/gpu/driver-u= api.rst index 9c7ca6e33a68..c08bcbb95fb3 100644 --- a/Documentation/gpu/driver-uapi.rst +++ b/Documentation/gpu/driver-uapi.rst @@ -13,4 +13,7 @@ drm/nouveau uAPI VM_BIND / EXEC uAPI ------------------- =20 +.. kernel-doc:: drivers/gpu/drm/nouveau/nouveau_exec.c + :doc: Overview + .. kernel-doc:: include/uapi/drm/nouveau_drm.h diff --git a/drivers/gpu/drm/nouveau/Kbuild b/drivers/gpu/drm/nouveau/Kbuild index ee281bb76463..cf6b3a80c0c8 100644 --- a/drivers/gpu/drm/nouveau/Kbuild +++ b/drivers/gpu/drm/nouveau/Kbuild @@ -47,6 +47,8 @@ nouveau-y +=3D nouveau_prime.o nouveau-y +=3D nouveau_sgdma.o nouveau-y +=3D nouveau_ttm.o nouveau-y +=3D nouveau_vmm.o +nouveau-y +=3D nouveau_exec.o +nouveau-y +=3D nouveau_sched.o nouveau-y +=3D nouveau_uvmm.o =20 # DRM - modesetting diff --git a/drivers/gpu/drm/nouveau/Kconfig b/drivers/gpu/drm/nouveau/Kcon= fig index a70bd65e1400..c52e8096cca4 100644 --- a/drivers/gpu/drm/nouveau/Kconfig +++ b/drivers/gpu/drm/nouveau/Kconfig @@ -10,6 +10,8 @@ config DRM_NOUVEAU select DRM_KMS_HELPER select DRM_TTM select DRM_TTM_HELPER + select DRM_EXEC + select DRM_SCHED select I2C select I2C_ALGOBIT select BACKLIGHT_CLASS_DEVICE if DRM_NOUVEAU_BACKLIGHT diff --git a/drivers/gpu/drm/nouveau/nouveau_abi16.c b/drivers/gpu/drm/nouv= eau/nouveau_abi16.c index 36cc80eb0e20..694777a58bca 100644 --- a/drivers/gpu/drm/nouveau/nouveau_abi16.c +++ b/drivers/gpu/drm/nouveau/nouveau_abi16.c @@ -35,6 +35,7 @@ #include "nouveau_chan.h" #include "nouveau_abi16.h" #include "nouveau_vmm.h" +#include "nouveau_sched.h" =20 static struct nouveau_abi16 * nouveau_abi16(struct drm_file *file_priv) @@ -125,6 +126,17 @@ nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16, { struct nouveau_abi16_ntfy *ntfy, *temp; =20 + /* When a client exits without waiting for it's queued up jobs to + * finish it might happen that we fault the channel. This is due to + * drm_file_free() calling drm_gem_release() before the postclose() + * callback. Hence, we can't tear down this scheduler entity before + * uvmm mappings are unmapped. Currently, we can't detect this case. + * + * However, this should be rare and harmless, since the channel isn't + * needed anymore. + */ + nouveau_sched_entity_fini(&chan->sched_entity); + /* wait for all activity to stop before cleaning up */ if (chan->chan) nouveau_channel_idle(chan->chan); @@ -311,6 +323,10 @@ nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS) if (ret) goto done; =20 + ret =3D nouveau_sched_entity_init(&chan->sched_entity, &drm->sched); + if (ret) + goto done; + init->channel =3D chan->chan->chid; =20 if (device->info.family >=3D NV_DEVICE_INFO_V0_TESLA) diff --git a/drivers/gpu/drm/nouveau/nouveau_abi16.h b/drivers/gpu/drm/nouv= eau/nouveau_abi16.h index 27eae85f33e6..8209eb28feaf 100644 --- a/drivers/gpu/drm/nouveau/nouveau_abi16.h +++ b/drivers/gpu/drm/nouveau/nouveau_abi16.h @@ -26,6 +26,7 @@ struct nouveau_abi16_chan { struct nouveau_bo *ntfy; struct nouveau_vma *ntfy_vma; struct nvkm_mm heap; + struct nouveau_sched_entity sched_entity; }; =20 struct nouveau_abi16 { diff --git a/drivers/gpu/drm/nouveau/nouveau_drm.c b/drivers/gpu/drm/nouvea= u/nouveau_drm.c index cde843156700..a5b1c7e7d24f 100644 --- a/drivers/gpu/drm/nouveau/nouveau_drm.c +++ b/drivers/gpu/drm/nouveau/nouveau_drm.c @@ -68,7 +68,9 @@ #include "nouveau_platform.h" #include "nouveau_svm.h" #include "nouveau_dmem.h" +#include "nouveau_exec.h" #include "nouveau_uvmm.h" +#include "nouveau_sched.h" =20 DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0, "DRM_UT_CORE", @@ -190,6 +192,7 @@ nouveau_cli_fini(struct nouveau_cli *cli) flush_work(&cli->work); WARN_ON(!list_empty(&cli->worker)); =20 + nouveau_sched_entity_fini(&cli->sched_entity); usif_client_fini(cli); nouveau_uvmm_fini(&cli->uvmm); nouveau_vmm_fini(&cli->svm); @@ -297,6 +300,11 @@ nouveau_cli_init(struct nouveau_drm *drm, const char *= sname, } =20 cli->mem =3D &mems[ret]; + + ret =3D nouveau_sched_entity_init(&cli->sched_entity, &drm->sched); + if (ret) + goto done; + return 0; done: if (ret) @@ -609,8 +617,13 @@ nouveau_drm_device_init(struct drm_device *dev) pm_runtime_put(dev->dev); } =20 - return 0; + ret =3D nouveau_sched_init(&drm->sched, drm); + if (ret) + goto fail_sched_init; =20 + return 0; +fail_sched_init: + nouveau_display_fini(dev, false, false); fail_dispinit: nouveau_display_destroy(dev); fail_dispctor: @@ -635,6 +648,8 @@ nouveau_drm_device_fini(struct drm_device *dev) struct nouveau_cli *cli, *temp_cli; struct nouveau_drm *drm =3D nouveau_drm(dev); =20 + nouveau_sched_fini(&drm->sched); + if (nouveau_pmops_runtime()) { pm_runtime_get_sync(dev->dev); pm_runtime_forbid(dev->dev); @@ -1175,6 +1190,9 @@ nouveau_ioctls[] =3D { DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_R= ENDER_ALLOW), DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_R= ENDER_ALLOW), DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_RENDER_AL= LOW), + DRM_IOCTL_DEF_DRV(NOUVEAU_VM_INIT, nouveau_uvmm_ioctl_vm_init, DRM_RENDER= _ALLOW), + DRM_IOCTL_DEF_DRV(NOUVEAU_VM_BIND, nouveau_uvmm_ioctl_vm_bind, DRM_RENDER= _ALLOW), + DRM_IOCTL_DEF_DRV(NOUVEAU_EXEC, nouveau_exec_ioctl_exec, DRM_RENDER_ALLOW= ), }; =20 long @@ -1223,7 +1241,9 @@ static struct drm_driver driver_stub =3D { .driver_features =3D DRIVER_GEM | DRIVER_MODESET | - DRIVER_RENDER, + DRIVER_RENDER | + DRIVER_SYNCOBJ | DRIVER_SYNCOBJ_TIMELINE | + DRIVER_GEM_GPUVA, .open =3D nouveau_drm_open, .postclose =3D nouveau_drm_postclose, .lastclose =3D nouveau_vga_lastclose, diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouvea= u/nouveau_drv.h index d634f1054d65..94de792ef3ca 100644 --- a/drivers/gpu/drm/nouveau/nouveau_drv.h +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h @@ -10,8 +10,8 @@ #define DRIVER_DATE "20120801" =20 #define DRIVER_MAJOR 1 -#define DRIVER_MINOR 3 -#define DRIVER_PATCHLEVEL 1 +#define DRIVER_MINOR 4 +#define DRIVER_PATCHLEVEL 0 =20 /* * 1.1.1: @@ -63,6 +63,7 @@ struct platform_device; =20 #include "nouveau_fence.h" #include "nouveau_bios.h" +#include "nouveau_sched.h" #include "nouveau_vmm.h" #include "nouveau_uvmm.h" =20 @@ -94,6 +95,8 @@ struct nouveau_cli { struct nouveau_vmm svm; struct nouveau_uvmm uvmm; =20 + struct nouveau_sched_entity sched_entity; + const struct nvif_mclass *mem; =20 struct list_head head; @@ -305,6 +308,8 @@ struct nouveau_drm { struct mutex lock; bool component_registered; } audio; + + struct drm_gpu_scheduler sched; }; =20 static inline struct nouveau_drm * diff --git a/drivers/gpu/drm/nouveau/nouveau_exec.c b/drivers/gpu/drm/nouve= au/nouveau_exec.c new file mode 100644 index 000000000000..536956a79279 --- /dev/null +++ b/drivers/gpu/drm/nouveau/nouveau_exec.c @@ -0,0 +1,322 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright (c) 2022 Red Hat. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software= "), + * to deal in the Software without restriction, including without limitati= on + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Danilo Krummrich + * + */ + +#include + +#include "nouveau_drv.h" +#include "nouveau_gem.h" +#include "nouveau_mem.h" +#include "nouveau_dma.h" +#include "nouveau_exec.h" +#include "nouveau_abi16.h" +#include "nouveau_chan.h" +#include "nouveau_sched.h" +#include "nouveau_uvmm.h" + +/** + * DOC: Overview + * + * Nouveau's VM_BIND / EXEC UAPI consists of three ioctls: DRM_NOUVEAU_VM_= INIT, + * DRM_NOUVEAU_VM_BIND and DRM_NOUVEAU_EXEC. + * + * In order to use the UAPI firstly a user client must initialize the VA s= pace + * using the DRM_NOUVEAU_VM_INIT ioctl specifying which region of the VA s= pace + * should be managed by the kernel and which by the UMD. + * + * The DRM_NOUVEAU_VM_BIND ioctl provides clients an interface to manage t= he + * userspace-managable portion of the VA space. It provides operations to + * allocate and free a VA space regions and operations to map and unmap me= mory + * within such a region. Bind operations crossing region boundaries are not + * permitted. + * + * When allocating a VA space region userspace may flag this region as spa= rse. + * If a region is flagged as sparse the kernel will take care that for the= whole + * region sparse mappings are created. Subsequently requested actual memory + * backed mappings for a sparse region will take precedence over the sparse + * mappings. If the memory backed mappings are unmapped the kernel will ma= ke + * sure that sparse mappings will take their place again. + * + * When using the VM_BIND ioctl to request the kernel to map memory to a g= iven + * virtual address in the GPU's VA space there is no guarantee that the ac= tual + * mappings are created in the GPU's MMU. If the given memory is swapped o= ut + * at the time the bind operation is executed the kernel will stash the ma= pping + * details into it's internal alloctor and create the actual MMU mappings = once + * the memory is swapped back in. While this is transparent for userspace,= it is + * guaranteed that all the backing memory is swapped back in and all the m= emory + * mappings, as requested by userspace previously, are actually mapped onc= e the + * DRM_NOUVEAU_EXEC ioctl is called to submit an exec job. + * + * Contrary to VM_BIND map requests, unmap requests are allowed to span ov= er VA + * space regions and completely untouched areas of the VA space. + * + * Generally, all rules for constellations like mapping and unmapping over + * boundaries of existing mappings are documented in the &drm_gpuva_manage= r. + * + * When a VA space region is freed, all existing mappings within this regi= on are + * unmapped automatically. + * + * A VM_BIND job can be executed either synchronously or asynchronously. If + * exectued asynchronously, userspace may provide a list of syncobjs this = job + * will wait for and/or a list of syncobj the kernel will trigger once the + * VM_BIND finished execution. If executed synchronously the ioctl will bl= ock + * until the bind job is finished and no syncobjs are permitted by the ker= nel. + * + * To execute a push buffer the UAPI provides the DRM_NOUVEAU_EXEC ioctl. = EXEC + * jobs are always executed asynchronously, and, equal to VM_BIND jobs, pr= ovide + * the option to synchronize them with syncobjs. + * + * Besides that EXEC job can be scheduled for a specified channel to execu= te on. + * + * Since VM_BIND jobs update the GPU's VA space on job submit, EXEC jobs d= o have + * an up to date view of the VA space. However, the actual mappings might = still + * be pending. Hence, EXEC jobs require to have the particular fences - of + * the corresponding VM_BIND jobs they depent on - attached to them. + */ + +static int +nouveau_exec_job_submit(struct nouveau_job *job) +{ + struct nouveau_exec_job *exec_job =3D to_nouveau_exec_job(job); + struct nouveau_cli *cli =3D exec_job->base.cli; + struct nouveau_uvmm *uvmm =3D nouveau_cli_uvmm(cli); + struct drm_exec *exec =3D &job->exec; + int ret; + + nouveau_uvmm_lock(uvmm); + drm_exec_while_not_all_locked(exec) { + DRM_GPUVA_ITER(it, &uvmm->umgr); + + drm_gpuva_iter_for_each(it) { + struct drm_gpuva *va =3D it.va; + + ret =3D drm_exec_prepare_obj(exec, va->gem.obj, 1); + drm_exec_break_on_contention(exec); + if (ret) + return ret; + } + } + nouveau_uvmm_unlock(uvmm); + + return 0; +} + +static struct dma_fence * +nouveau_exec_job_run(struct nouveau_job *job) +{ + struct nouveau_exec_job *exec_job =3D to_nouveau_exec_job(job); + struct nouveau_fence *fence; + int i, ret; + + ret =3D nouveau_dma_wait(job->chan, exec_job->push.count + 1, 16); + if (ret) { + NV_PRINTK(err, job->cli, "nv50cal_space: %d\n", ret); + return ERR_PTR(ret); + } + + for (i =3D 0; i < exec_job->push.count; i++) { + nv50_dma_push(job->chan, exec_job->push.s[i].va, + exec_job->push.s[i].va_len); + } + + ret =3D nouveau_fence_new(job->chan, false, &fence); + if (ret) { + NV_PRINTK(err, job->cli, "error fencing pushbuf: %d\n", ret); + WIND_RING(job->chan); + return ERR_PTR(ret); + } + + return &fence->base; +} +static void +nouveau_exec_job_free(struct nouveau_job *job) +{ + struct nouveau_exec_job *exec_job =3D to_nouveau_exec_job(job); + + nouveau_base_job_free(job); + + kfree(exec_job->push.s); + kfree(exec_job); +} + +static enum drm_gpu_sched_stat +nouveau_exec_job_timeout(struct nouveau_job *job) +{ + struct nouveau_channel *chan =3D job->chan; + + if (unlikely(!atomic_read(&chan->killed))) + nouveau_channel_kill(chan); + + NV_PRINTK(warn, job->cli, "job timeout, channel %d killed!\n", + chan->chid); + + nouveau_sched_entity_fini(job->entity); + + return DRM_GPU_SCHED_STAT_ENODEV; +} + +static struct nouveau_job_ops nouveau_exec_job_ops =3D { + .submit =3D nouveau_exec_job_submit, + .run =3D nouveau_exec_job_run, + .free =3D nouveau_exec_job_free, + .timeout =3D nouveau_exec_job_timeout, +}; + +int +nouveau_exec_job_init(struct nouveau_exec_job **pjob, + struct nouveau_exec_job_args *args) +{ + struct nouveau_exec_job *job; + int ret; + + job =3D *pjob =3D kzalloc(sizeof(*job), GFP_KERNEL); + if (!job) + return -ENOMEM; + + job->push.count =3D args->push.count; + job->push.s =3D kmemdup(args->push.s, + sizeof(*args->push.s) * + args->push.count, + GFP_KERNEL); + if (!job->push.s) { + ret =3D -ENOMEM; + goto err_free_job; + } + + job->base.ops =3D &nouveau_exec_job_ops; + job->base.resv_usage =3D DMA_RESV_USAGE_WRITE; + + ret =3D nouveau_base_job_init(&job->base, &args->base); + if (ret) + goto err_free_pushs; + + return 0; + +err_free_pushs: + kfree(job->push.s); +err_free_job: + kfree(job); + *pjob =3D NULL; + + return ret; +} + +static int +nouveau_exec(struct nouveau_exec_job_args *args) +{ + struct nouveau_exec_job *job; + int ret; + + ret =3D nouveau_exec_job_init(&job, args); + if (ret) + return ret; + + ret =3D nouveau_job_submit(&job->base); + if (ret) + goto err_job_fini; + + return 0; + +err_job_fini: + nouveau_job_fini(&job->base); + return ret; +} + +int +nouveau_exec_ioctl_exec(struct drm_device *dev, + void *data, + struct drm_file *file_priv) +{ + struct nouveau_abi16 *abi16 =3D nouveau_abi16_get(file_priv); + struct nouveau_cli *cli =3D nouveau_cli(file_priv); + struct nouveau_abi16_chan *chan16; + struct nouveau_channel *chan =3D NULL; + struct nouveau_exec_job_args args =3D {}; + struct drm_nouveau_exec *req =3D data; + int ret =3D 0; + + if (unlikely(!abi16)) + return -ENOMEM; + + /* abi16 locks already */ + if (unlikely(!nouveau_cli_uvmm(cli))) + return nouveau_abi16_put(abi16, -ENOSYS); + + list_for_each_entry(chan16, &abi16->channels, head) { + if (chan16->chan->chid =3D=3D req->channel) { + chan =3D chan16->chan; + break; + } + } + + if (!chan) + return nouveau_abi16_put(abi16, -ENOENT); + + if (unlikely(atomic_read(&chan->killed))) + return nouveau_abi16_put(abi16, -ENODEV); + + if (!chan->dma.ib_max) + return nouveau_abi16_put(abi16, -ENOSYS); + + if (unlikely(req->push_count =3D=3D 0)) + goto out; + + if (unlikely(req->push_count > NOUVEAU_GEM_MAX_PUSH)) { + NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n", + req->push_count, NOUVEAU_GEM_MAX_PUSH); + return nouveau_abi16_put(abi16, -EINVAL); + } + + args.push.count =3D req->push_count; + args.push.s =3D u_memcpya(req->push_ptr, req->push_count, + sizeof(*args.push.s)); + if (IS_ERR(args.push.s)) { + ret =3D PTR_ERR(args.push.s); + goto out; + } + + ret =3D nouveau_job_ucopy_syncs(&args.base, + req->wait_count, req->wait_ptr, + req->sig_count, req->sig_ptr); + if (ret) + goto out_free_pushs; + + args.base.sched_entity =3D &chan16->sched_entity; + args.base.chan =3D chan; + args.base.file_priv =3D file_priv; + + ret =3D nouveau_exec(&args); + if (ret) + goto out_free_syncs; + +out_free_syncs: + u_free(args.base.out_sync.s); + u_free(args.base.in_sync.s); +out_free_pushs: + u_free(args.push.s); +out: + return nouveau_abi16_put(abi16, ret); +} diff --git a/drivers/gpu/drm/nouveau/nouveau_exec.h b/drivers/gpu/drm/nouve= au/nouveau_exec.h new file mode 100644 index 000000000000..5bca1f9aba74 --- /dev/null +++ b/drivers/gpu/drm/nouveau/nouveau_exec.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: MIT */ + +#ifndef __NOUVEAU_EXEC_H__ +#define __NOUVEAU_EXEC_H__ + +#include + +#include "nouveau_drv.h" +#include "nouveau_sched.h" + +struct nouveau_exec_job_args { + struct nouveau_job_args base; + struct drm_exec exec; + + struct { + struct drm_nouveau_exec_push *s; + u32 count; + } push; +}; + +struct nouveau_exec_job { + struct nouveau_job base; + + struct { + struct drm_nouveau_exec_push *s; + u32 count; + } push; +}; + +#define to_nouveau_exec_job(job) \ + container_of((job), struct nouveau_exec_job, base) + +int nouveau_exec_job_init(struct nouveau_exec_job **job, + struct nouveau_exec_job_args *args); + +int nouveau_exec_ioctl_exec(struct drm_device *dev, void *data, + struct drm_file *file_priv); + +#endif diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.c b/drivers/gpu/drm/nouv= eau/nouveau_sched.c new file mode 100644 index 000000000000..8e32fcbc09ab --- /dev/null +++ b/drivers/gpu/drm/nouveau/nouveau_sched.c @@ -0,0 +1,467 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright (c) 2022 Red Hat. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software= "), + * to deal in the Software without restriction, including without limitati= on + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Danilo Krummrich + * + */ + +#include +#include +#include + +#include "nouveau_drv.h" +#include "nouveau_gem.h" +#include "nouveau_mem.h" +#include "nouveau_dma.h" +#include "nouveau_exec.h" +#include "nouveau_abi16.h" +#include "nouveau_chan.h" +#include "nouveau_sched.h" + +/* FIXME + * + * We want to make sure that jobs currently executing can't be deferred by + * other jobs competing for the hardware. Otherwise we might end up with j= ob + * timeouts just because of too many clients submitting too many jobs. We = don't + * want jobs to time out because of system load, but because of the job be= ing + * too bulky. + * + * For now allow for up to 16 concurrent jobs in flight until we know how = many + * rings the hardware can process in parallel. + */ +#define NOUVEAU_SCHED_HW_SUBMISSIONS 16 +#define NOUVEAU_SCHED_JOB_TIMEOUT_MS 10000 + +int +nouveau_job_ucopy_syncs(struct nouveau_job_args *args, + u32 inc, u64 ins, + u32 outc, u64 outs) +{ + struct drm_nouveau_sync **s; + int ret; + + if (inc) { + s =3D &args->in_sync.s; + + args->in_sync.count =3D inc; + *s =3D u_memcpya(ins, inc, sizeof(**s)); + if (IS_ERR(*s)) { + ret =3D PTR_ERR(*s); + goto err_out; + } + } + + if (outc) { + s =3D &args->out_sync.s; + + args->out_sync.count =3D outc; + *s =3D u_memcpya(outs, outc, sizeof(**s)); + if (IS_ERR(*s)) { + ret =3D PTR_ERR(*s); + goto err_free_ins; + } + } + + return 0; + +err_free_ins: + u_free(args->in_sync.s); +err_out: + return ret; +} + +int +nouveau_base_job_init(struct nouveau_job *job, + struct nouveau_job_args *args) +{ + struct nouveau_sched_entity *entity =3D args->sched_entity; + int ret; + + job->file_priv =3D args->file_priv; + job->cli =3D nouveau_cli(args->file_priv); + job->chan =3D args->chan; + job->entity =3D entity; + + job->in_sync.count =3D args->in_sync.count; + if (job->in_sync.count) { + if (job->sync) + return -EINVAL; + + job->in_sync.data =3D kmemdup(args->in_sync.s, + sizeof(*args->in_sync.s) * + args->in_sync.count, + GFP_KERNEL); + if (!job->in_sync.data) + return -ENOMEM; + } + + job->out_sync.count =3D args->out_sync.count; + if (job->out_sync.count) { + if (job->sync) { + ret =3D -EINVAL; + goto err_free_in_sync; + } + + job->out_sync.data =3D kmemdup(args->out_sync.s, + sizeof(*args->out_sync.s) * + args->out_sync.count, + GFP_KERNEL); + if (!job->out_sync.data) { + ret =3D -ENOMEM; + goto err_free_in_sync; + } + + job->out_sync.objs =3D kcalloc(job->out_sync.count, + sizeof(*job->out_sync.objs), + GFP_KERNEL); + if (!job->out_sync.objs) { + ret =3D -ENOMEM; + goto err_free_out_sync; + } + + job->out_sync.chains =3D kcalloc(job->out_sync.count, + sizeof(*job->out_sync.chains), + GFP_KERNEL); + if (!job->out_sync.chains) { + ret =3D -ENOMEM; + goto err_free_objs; + } + + } + + ret =3D drm_sched_job_init(&job->base, &entity->base, NULL); + if (ret) + goto err_free_chains; + + return 0; + +err_free_chains: + kfree(job->out_sync.chains); +err_free_objs: + kfree(job->out_sync.objs); +err_free_out_sync: + kfree(job->out_sync.data); +err_free_in_sync: + kfree(job->in_sync.data); +return ret; +} + +void +nouveau_base_job_free(struct nouveau_job *job) +{ + kfree(job->in_sync.data); + kfree(job->out_sync.data); + kfree(job->out_sync.objs); + kfree(job->out_sync.chains); +} + +void nouveau_job_fini(struct nouveau_job *job) +{ + dma_fence_put(job->done_fence); + drm_sched_job_cleanup(&job->base); + job->ops->free(job); +} + +static int +sync_find_fence(struct nouveau_job *job, + struct drm_nouveau_sync *sync, + struct dma_fence **fence) +{ + u32 stype =3D sync->flags & DRM_NOUVEAU_SYNC_TYPE_MASK; + u64 point =3D 0; + int ret; + + if (stype !=3D DRM_NOUVEAU_SYNC_SYNCOBJ && + stype !=3D DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ) + return -EOPNOTSUPP; + + if (stype =3D=3D DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ) + point =3D sync->timeline_value; + + ret =3D drm_syncobj_find_fence(job->file_priv, + sync->handle, point, + sync->flags, fence); + if (ret) + return ret; + + return 0; +} + +static int +nouveau_job_add_deps(struct nouveau_job *job) +{ + struct dma_fence *in_fence =3D NULL; + int ret, i; + + for (i =3D 0; i < job->in_sync.count; i++) { + struct drm_nouveau_sync *sync =3D &job->in_sync.data[i]; + + ret =3D sync_find_fence(job, sync, &in_fence); + if (ret) { + NV_PRINTK(warn, job->cli, + "Failed to find syncobj (-> in): handle=3D%d\n", + sync->handle); + return ret; + } + + ret =3D drm_sched_job_add_dependency(&job->base, in_fence); + if (ret) + return ret; + } + + return 0; +} + +static int +nouveau_job_fence_attach_prepare(struct nouveau_job *job) +{ + int i, ret; + + for (i =3D 0; i < job->out_sync.count; i++) { + struct drm_nouveau_sync *sync =3D &job->out_sync.data[i]; + struct drm_syncobj **pobj =3D &job->out_sync.objs[i]; + struct dma_fence_chain **pchain =3D &job->out_sync.chains[i]; + u32 stype =3D sync->flags & DRM_NOUVEAU_SYNC_TYPE_MASK; + + if (stype !=3D DRM_NOUVEAU_SYNC_SYNCOBJ && + stype !=3D DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ) { + ret =3D -EINVAL; + goto err_sync_cleanup; + } + + *pobj =3D drm_syncobj_find(job->file_priv, sync->handle); + if (!*pobj) { + NV_PRINTK(warn, job->cli, + "Failed to find syncobj (-> out): handle=3D%d\n", + sync->handle); + ret =3D -ENOENT; + goto err_sync_cleanup; + } + + if (stype =3D=3D DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ) { + *pchain =3D dma_fence_chain_alloc(); + if (!*pchain) { + ret =3D -ENOMEM; + goto err_sync_cleanup; + } + } + } + + return 0; + +err_sync_cleanup: + for (i =3D 0; i < job->out_sync.count; i++) { + struct drm_syncobj *obj =3D job->out_sync.objs[i]; + struct dma_fence_chain *chain =3D job->out_sync.chains[i]; + + if (obj) + drm_syncobj_put(obj); + + if (chain) + dma_fence_chain_free(chain); + } + return ret; +} + +static void +nouveau_job_fence_attach(struct nouveau_job *job) +{ + struct dma_fence *fence =3D job->done_fence; + int i; + + for (i =3D 0; i < job->out_sync.count; i++) { + struct drm_nouveau_sync *sync =3D &job->out_sync.data[i]; + struct drm_syncobj *obj =3D job->out_sync.objs[i]; + struct dma_fence_chain *chain =3D job->out_sync.chains[i]; + u32 stype =3D sync->flags & DRM_NOUVEAU_SYNC_TYPE_MASK; + + if (stype =3D=3D DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ) { + drm_syncobj_add_point(obj, chain, fence, + sync->timeline_value); + } else { + drm_syncobj_replace_fence(obj, fence); + } + + drm_syncobj_put(obj); + } +} + +static int +nouveau_job_validate(struct nouveau_job *job) +{ + struct drm_exec *exec =3D &job->exec; + struct drm_gem_object *obj; + unsigned long index; + int ret; + + drm_exec_for_each_locked_object(exec, index, obj) { + struct nouveau_bo *nvbo =3D nouveau_gem_object(obj); + + ret =3D nouveau_bo_validate(nvbo, true, false); + if (ret) + return ret; + } + + return 0; +} + +static void +nouveau_job_resv_add_fence(struct nouveau_job *job) +{ + struct drm_exec *exec =3D &job->exec; + struct drm_gem_object *obj; + unsigned long index; + + drm_exec_for_each_locked_object(exec, index, obj) { + struct dma_resv *resv =3D obj->resv; + + dma_resv_add_fence(resv, job->done_fence, job->resv_usage); + } +} + +int +nouveau_job_submit(struct nouveau_job *job) +{ + struct nouveau_sched_entity *entity =3D to_nouveau_sched_entity(job->base= .entity); + int ret; + + drm_exec_init(&job->exec, true); + + /* Make sure the job appears on the sched_entity's queue in the same + * order as it was submitted. + */ + mutex_lock(&entity->job.mutex); + + if (job->ops->submit) { + ret =3D job->ops->submit(job); + if (ret) + goto err; + } + + ret =3D nouveau_job_validate(job); + if (ret) + goto err; + + ret =3D nouveau_job_add_deps(job); + if (ret) + goto err; + + ret =3D nouveau_job_fence_attach_prepare(job); + if (ret) + goto err; + + drm_sched_job_arm(&job->base); + job->done_fence =3D dma_fence_get(&job->base.s_fence->finished); + + nouveau_job_fence_attach(job); + nouveau_job_resv_add_fence(job); + + drm_exec_fini(&job->exec); + + drm_sched_entity_push_job(&job->base); + + mutex_unlock(&entity->job.mutex); + + if (job->sync) + dma_fence_wait(job->done_fence, true); + + return 0; + +err: + mutex_unlock(&entity->job.mutex); + drm_exec_fini(&job->exec); + return ret; +} + +static struct dma_fence * +nouveau_job_run(struct nouveau_job *job) +{ + return job->ops->run(job); +} + +static struct dma_fence * +nouveau_sched_run_job(struct drm_sched_job *sched_job) +{ + struct nouveau_job *job =3D to_nouveau_job(sched_job); + + return nouveau_job_run(job); +} + +static enum drm_gpu_sched_stat +nouveau_sched_timedout_job(struct drm_sched_job *sched_job) +{ + struct nouveau_job *job =3D to_nouveau_job(sched_job); + + if (job->ops->timeout) + return job->ops->timeout(job); + + NV_PRINTK(warn, job->cli, "Job timed out.\n"); + + return DRM_GPU_SCHED_STAT_ENODEV; +} + +static void +nouveau_sched_free_job(struct drm_sched_job *sched_job) +{ + struct nouveau_job *job =3D to_nouveau_job(sched_job); + + nouveau_job_fini(job); +} + +int nouveau_sched_entity_init(struct nouveau_sched_entity *entity, + struct drm_gpu_scheduler *sched) +{ + + mutex_init(&entity->job.mutex); + + return drm_sched_entity_init(&entity->base, + DRM_SCHED_PRIORITY_NORMAL, + &sched, 1, NULL); +} + +void +nouveau_sched_entity_fini(struct nouveau_sched_entity *entity) +{ + drm_sched_entity_destroy(&entity->base); +} + +static const struct drm_sched_backend_ops nouveau_sched_ops =3D { + .run_job =3D nouveau_sched_run_job, + .timedout_job =3D nouveau_sched_timedout_job, + .free_job =3D nouveau_sched_free_job, +}; + +int nouveau_sched_init(struct drm_gpu_scheduler *sched, + struct nouveau_drm *drm) +{ + long job_hang_limit =3D msecs_to_jiffies(NOUVEAU_SCHED_JOB_TIMEOUT_MS); + + return drm_sched_init(sched, &nouveau_sched_ops, + NOUVEAU_SCHED_HW_SUBMISSIONS, 0, job_hang_limit, + NULL, NULL, "nouveau", drm->dev->dev); +} + +void nouveau_sched_fini(struct drm_gpu_scheduler *sched) +{ + drm_sched_fini(sched); +} diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.h b/drivers/gpu/drm/nouv= eau/nouveau_sched.h new file mode 100644 index 000000000000..407a62b18788 --- /dev/null +++ b/drivers/gpu/drm/nouveau/nouveau_sched.h @@ -0,0 +1,96 @@ +/* SPDX-License-Identifier: MIT */ + +#ifndef NOUVEAU_SCHED_H +#define NOUVEAU_SCHED_H + +#include + +#include +#include + +#include "nouveau_drv.h" + +#define to_nouveau_job(sched_job) \ + container_of((sched_job), struct nouveau_job, base) + +struct nouveau_job_args { + struct nouveau_channel *chan; + struct drm_file *file_priv; + struct nouveau_sched_entity *sched_entity; + + struct { + struct drm_nouveau_sync *s; + u32 count; + } in_sync; + + struct { + struct drm_nouveau_sync *s; + u32 count; + } out_sync; +}; + +struct nouveau_job { + struct drm_sched_job base; + + struct nouveau_sched_entity *entity; + + struct drm_file *file_priv; + struct nouveau_cli *cli; + struct nouveau_channel *chan; + + struct drm_exec exec; + enum dma_resv_usage resv_usage; + struct dma_fence *done_fence; + + bool sync; + + struct { + struct drm_nouveau_sync *data; + u32 count; + } in_sync; + + struct { + struct drm_nouveau_sync *data; + struct drm_syncobj **objs; + struct dma_fence_chain **chains; + u32 count; + } out_sync; + + struct nouveau_job_ops { + int (*submit)(struct nouveau_job *); + struct dma_fence *(*run)(struct nouveau_job *); + void (*free)(struct nouveau_job *); + enum drm_gpu_sched_stat (*timeout)(struct nouveau_job *); + } *ops; +}; + +int nouveau_job_ucopy_syncs(struct nouveau_job_args *args, + u32 inc, u64 ins, + u32 outc, u64 outs); + +int nouveau_base_job_init(struct nouveau_job *job, + struct nouveau_job_args *args); +void nouveau_base_job_free(struct nouveau_job *job); + +int nouveau_job_submit(struct nouveau_job *job); +void nouveau_job_fini(struct nouveau_job *job); + +#define to_nouveau_sched_entity(entity) \ + container_of((entity), struct nouveau_sched_entity, base) + +struct nouveau_sched_entity { + struct drm_sched_entity base; + struct { + struct mutex mutex; + } job; +}; + +int nouveau_sched_entity_init(struct nouveau_sched_entity *entity, + struct drm_gpu_scheduler *sched); +void nouveau_sched_entity_fini(struct nouveau_sched_entity *entity); + +int nouveau_sched_init(struct drm_gpu_scheduler *sched, + struct nouveau_drm *drm); +void nouveau_sched_fini(struct drm_gpu_scheduler *sched); + +#endif diff --git a/drivers/gpu/drm/nouveau/nouveau_uvmm.c b/drivers/gpu/drm/nouve= au/nouveau_uvmm.c index 2f7747a5a917..a23b71c31021 100644 --- a/drivers/gpu/drm/nouveau/nouveau_uvmm.c +++ b/drivers/gpu/drm/nouveau/nouveau_uvmm.c @@ -57,6 +57,40 @@ #define NOUVEAU_VA_SPACE_START 0x0 #define NOUVEAU_VA_SPACE_END (1ULL << NOUVEAU_VA_SPACE_BITS) =20 +#define list_for_each_op(_op, _ops) list_for_each_entry(_op, _ops, entry) +#define list_for_each_op_continue_reverse(_op, _ops) \ + list_for_each_entry_continue_reverse(_op, _ops, entry) +#define list_for_each_op_safe(_op, _n, _ops) list_for_each_entry_safe(_op,= _n, _ops, entry) + +enum bind_op { + OP_ALLOC =3D DRM_NOUVEAU_VM_BIND_OP_ALLOC, + OP_FREE =3D DRM_NOUVEAU_VM_BIND_OP_FREE, + OP_MAP =3D DRM_NOUVEAU_VM_BIND_OP_MAP, + OP_UNMAP =3D DRM_NOUVEAU_VM_BIND_OP_UNMAP, +}; + +struct bind_job_op { + struct list_head entry; + + enum bind_op op; + u32 flags; + + struct { + u64 addr; + u64 range; + } va; + + struct { + u32 handle; + u64 offset; + struct drm_gem_object *obj; + } gem; + + struct nouveau_uvma_region *reg; + struct nouveau_uvma_alloc new; + struct drm_gpuva_ops *ops; +}; + struct uvmm_map_args { u64 addr; u64 range; @@ -953,6 +987,418 @@ nouveau_uvmm_sm_unmap_cleanup(struct nouveau_uvmm *uv= mm, uvmm_sm_cleanup(uvmm, new, ops, true); } =20 +static int +bind_validate_op(struct nouveau_job *job, + struct bind_job_op *op) +{ + struct nouveau_uvmm *uvmm =3D nouveau_cli_uvmm(job->cli); + struct drm_gem_object *obj =3D op->gem.obj; + + if (op->op =3D=3D OP_MAP) { + if (op->gem.offset & ~PAGE_MASK) + return -EINVAL; + + if (obj->size <=3D op->gem.offset) + return -EINVAL; + + if (op->va.range > (obj->size - op->gem.offset)) + return -EINVAL; + } + + return nouveau_uvmm_validate_range(uvmm, op->va.addr, op->va.range); +} + +static int +uvmm_bind_job_submit(struct nouveau_job *job) +{ + struct nouveau_uvmm *uvmm =3D nouveau_cli_uvmm(job->cli); + struct nouveau_uvmm_bind_job *bind_job =3D to_uvmm_bind_job(job); + struct drm_exec *exec =3D &job->exec; + struct bind_job_op *op; + int ret; + + /* We need to keep holding the uvmm lock until this function can't fail + * anymore, since we need to be able to unind all GPUVA space changes on + * failure. + */ + nouveau_uvmm_lock(uvmm); + list_for_each_op(op, &bind_job->ops) { + + if (op->op =3D=3D OP_MAP) { + op->gem.obj =3D drm_gem_object_lookup(job->file_priv, + op->gem.handle); + if (!op->gem.obj) { + ret =3D -ENOENT; + goto unwind; + } + } + + ret =3D bind_validate_op(job, op); + if (ret) + goto unwind; + + switch (op->op) { + case OP_ALLOC: { + bool sparse =3D op->flags & DRM_NOUVEAU_VM_BIND_SPARSE; + + ret =3D nouveau_uvma_region_create(uvmm, + op->va.addr, + op->va.range, + sparse); + if (ret) + goto unwind; + + break; + } + case OP_FREE: + op->reg =3D nouveau_uvma_region_find(uvmm, op->va.addr, + op->va.range); + if (!op->reg) + goto unwind; + + op->ops =3D nouveau_uvmm_sm_unmap_ops(uvmm, + op->va.addr, + op->va.range); + if (IS_ERR(op->ops)) { + ret =3D PTR_ERR(op->ops); + goto unwind; + } + + ret =3D nouveau_uvmm_sm_unmap_prepare(uvmm, &op->new, + op->ops); + if (ret) + goto unwind; + + nouveau_uvma_region_remove(op->reg); + + break; + case OP_MAP: + op->ops =3D nouveau_uvmm_sm_map_ops(uvmm, + op->va.addr, + op->va.range, + op->gem.obj, + op->gem.offset); + if (IS_ERR(op->ops)) { + ret =3D PTR_ERR(op->ops); + goto unwind; + } + + ret =3D nouveau_uvmm_sm_map_prepare(uvmm, &op->new, + op->ops, op->va.addr, + op->va.range, + op->flags && 0xff); + if (ret) + goto unwind; + + break; + case OP_UNMAP: + op->ops =3D nouveau_uvmm_sm_unmap_ops(uvmm, + op->va.addr, + op->va.range); + if (IS_ERR(op->ops)) { + ret =3D PTR_ERR(op->ops); + goto unwind; + } + + ret =3D nouveau_uvmm_sm_unmap_prepare(uvmm, &op->new, + op->ops); + if (ret) + goto unwind; + + break; + default: + ret =3D -EINVAL; + goto unwind; + } + } + + drm_exec_while_not_all_locked(exec) { + list_for_each_op(op, &bind_job->ops) { + if (op->op !=3D OP_MAP) + continue; + + ret =3D drm_exec_prepare_obj(exec, op->gem.obj, 1); + drm_exec_break_on_contention(exec); + if (ret) + goto unwind; + } + } + nouveau_uvmm_unlock(uvmm); + + return 0; + +unwind: + list_for_each_op_continue_reverse(op, &bind_job->ops) { + switch (op->op) { + case OP_ALLOC: + nouveau_uvma_region_destroy(uvmm, op->va.addr, + op->va.range); + break; + case OP_FREE: + __nouveau_uvma_region_insert(uvmm, op->reg); + nouveau_uvmm_sm_unmap_prepare_unwind(uvmm, &op->new, + op->ops); + break; + case OP_MAP: + nouveau_uvmm_sm_map_prepare_unwind(uvmm, &op->new, + op->ops, + op->va.addr, + op->va.range); + break; + case OP_UNMAP: + nouveau_uvmm_sm_unmap_prepare_unwind(uvmm, &op->new, + op->ops); + break; + } + + drm_gpuva_ops_free(&uvmm->umgr, op->ops); + op->ops =3D NULL; + op->reg =3D NULL; + } + + nouveau_uvmm_unlock(uvmm); + return ret; +} + +static struct dma_fence * +uvmm_bind_job_run(struct nouveau_job *job) +{ + struct nouveau_uvmm_bind_job *bind_job =3D to_uvmm_bind_job(job); + struct nouveau_uvmm *uvmm =3D nouveau_cli_uvmm(job->cli); + struct bind_job_op *op; + int ret =3D 0; + + list_for_each_op(op, &bind_job->ops) { + switch (op->op) { + case OP_ALLOC: + /* noop */ + break; + case OP_MAP: + ret =3D nouveau_uvmm_sm_map(uvmm, &op->new, op->ops); + if (ret) + goto out; + break; + case OP_FREE: + fallthrough; + case OP_UNMAP: + ret =3D nouveau_uvmm_sm_unmap(uvmm, &op->new, op->ops); + if (ret) + goto out; + break; + } + } + +out: + if (ret) + NV_PRINTK(err, job->cli, "bind job failed: %d\n", ret); + return ERR_PTR(ret); +} + +static void +uvmm_bind_job_free(struct nouveau_job *job) +{ + struct nouveau_uvmm_bind_job *bind_job =3D to_uvmm_bind_job(job); + struct nouveau_uvmm *uvmm =3D nouveau_cli_uvmm(job->cli); + struct bind_job_op *op, *next; + + list_for_each_op_safe(op, next, &bind_job->ops) { + struct drm_gem_object *obj =3D op->gem.obj; + + /* When uvmm_bind_job_submit() failed op->ops and op->reg will + * be NULL, hence we correctly skip the cleanup. + */ + switch (op->op) { + case OP_ALLOC: + /* noop */ + break; + case OP_FREE: + if (!IS_ERR_OR_NULL(op->ops)) + nouveau_uvmm_sm_unmap_cleanup(uvmm, &op->new, + op->ops); + + if (op->reg) { + nouveau_uvma_region_sparse_unref(op->reg); + nouveau_uvma_region_free(op->reg); + } + + break; + case OP_MAP: + if (!IS_ERR_OR_NULL(op->ops)) + nouveau_uvmm_sm_map_cleanup(uvmm, &op->new, + op->ops); + break; + case OP_UNMAP: + if (!IS_ERR_OR_NULL(op->ops)) + nouveau_uvmm_sm_unmap_cleanup(uvmm, &op->new, + op->ops); + break; + } + + if (!IS_ERR_OR_NULL(op->ops)) + drm_gpuva_ops_free(&uvmm->umgr, op->ops); + + if (obj) + drm_gem_object_put(obj); + + list_del(&op->entry); + kfree(op); + } + + nouveau_base_job_free(job); + kfree(bind_job); +} + +static struct nouveau_job_ops nouveau_bind_job_ops =3D { + .submit =3D uvmm_bind_job_submit, + .run =3D uvmm_bind_job_run, + .free =3D uvmm_bind_job_free, +}; + +static int +bind_job_op_from_uop(struct bind_job_op **pop, + struct drm_nouveau_vm_bind_op *uop) +{ + struct bind_job_op *op; + + op =3D *pop =3D kzalloc(sizeof(*op), GFP_KERNEL); + if (!op) + return -ENOMEM; + + op->op =3D uop->op; + op->flags =3D uop->flags; + op->va.addr =3D uop->addr; + op->va.range =3D uop->range; + op->gem.handle =3D uop->handle; + op->gem.offset =3D uop->bo_offset; + + return 0; +} + +static void +bind_job_ops_free(struct list_head *ops) +{ + struct bind_job_op *op, *next; + + list_for_each_op_safe(op, next, ops) { + list_del(&op->entry); + kfree(op); + } +} + +int +nouveau_uvmm_bind_job_init(struct nouveau_uvmm_bind_job **pjob, + struct nouveau_uvmm_bind_job_args *args) +{ + struct nouveau_uvmm_bind_job *job; + struct bind_job_op *op; + int i, ret; + + job =3D *pjob =3D kzalloc(sizeof(*job), GFP_KERNEL); + if (!job) + return -ENOMEM; + + INIT_LIST_HEAD(&job->ops); + + for (i =3D 0; i < args->op.count; i++) { + ret =3D bind_job_op_from_uop(&op, &args->op.s[i]); + if (ret) + goto err_free; + + list_add_tail(&op->entry, &job->ops); + } + + job->base.sync =3D !(args->flags & DRM_NOUVEAU_VM_BIND_RUN_ASYNC); + job->base.ops =3D &nouveau_bind_job_ops; + job->base.resv_usage =3D DMA_RESV_USAGE_BOOKKEEP; + + ret =3D nouveau_base_job_init(&job->base, &args->base); + if (ret) + goto err_free; + + return 0; + +err_free: + bind_job_ops_free(&job->ops); + kfree(job); + *pjob =3D NULL; + + return ret; +} + +int +nouveau_uvmm_ioctl_vm_init(struct drm_device *dev, + void *data, + struct drm_file *file_priv) +{ + struct nouveau_cli *cli =3D nouveau_cli(file_priv); + struct drm_nouveau_vm_init *init =3D data; + + return nouveau_uvmm_init(&cli->uvmm, cli, init); +} + +static int +nouveau_uvmm_vm_bind(struct nouveau_uvmm_bind_job_args *args) +{ + struct nouveau_uvmm_bind_job *job; + int ret; + + ret =3D nouveau_uvmm_bind_job_init(&job, args); + if (ret) + return ret; + + ret =3D nouveau_job_submit(&job->base); + if (ret) + goto err_job_fini; + + return 0; + +err_job_fini: + nouveau_job_fini(&job->base); + return ret; +} + +int +nouveau_uvmm_ioctl_vm_bind(struct drm_device *dev, + void *data, + struct drm_file *file_priv) +{ + struct nouveau_cli *cli =3D nouveau_cli(file_priv); + struct nouveau_uvmm_bind_job_args args =3D {}; + struct drm_nouveau_vm_bind *req =3D data; + int ret =3D 0; + + if (unlikely(!nouveau_cli_uvmm_locked(cli))) + return -ENOSYS; + + args.flags =3D req->flags; + + args.op.count =3D req->op_count; + args.op.s =3D u_memcpya(req->op_ptr, req->op_count, + sizeof(*args.op.s)); + if (IS_ERR(args.op.s)) + return PTR_ERR(args.op.s); + + ret =3D nouveau_job_ucopy_syncs(&args.base, + req->wait_count, req->wait_ptr, + req->sig_count, req->sig_ptr); + if (ret) + goto out_free_ops; + + args.base.sched_entity =3D &cli->sched_entity; + args.base.file_priv =3D file_priv; + + ret =3D nouveau_uvmm_vm_bind(&args); + if (ret) + goto out_free_syncs; + +out_free_syncs: + u_free(args.base.out_sync.s); + u_free(args.base.in_sync.s); +out_free_ops: + u_free(args.op.s); + return ret; +} + void nouveau_uvmm_bo_map_all(struct nouveau_bo *nvbo, struct nouveau_mem *mem) { diff --git a/drivers/gpu/drm/nouveau/nouveau_uvmm.h b/drivers/gpu/drm/nouve= au/nouveau_uvmm.h index 858840e9e0c5..d86a521100fa 100644 --- a/drivers/gpu/drm/nouveau/nouveau_uvmm.h +++ b/drivers/gpu/drm/nouveau/nouveau_uvmm.h @@ -36,6 +36,25 @@ struct nouveau_uvma_alloc { struct nouveau_uvma *next; }; =20 +struct nouveau_uvmm_bind_job { + struct nouveau_job base; + + /* struct bind_job_op */ + struct list_head ops; +}; + +struct nouveau_uvmm_bind_job_args { + struct nouveau_job_args base; + unsigned int flags; + + struct { + struct drm_nouveau_vm_bind_op *s; + u32 count; + } op; +}; + +#define to_uvmm_bind_job(job) container_of((job), struct nouveau_uvmm_bind= _job, base) + #define uvmm_from_mgr(x) container_of((x), struct nouveau_uvmm, umgr) #define uvma_from_va(x) container_of((x), struct nouveau_uvma, va) #define uvma_region_from_va_region(x) container_of((x), struct nouveau_uvm= a_region, region) @@ -97,6 +116,15 @@ void nouveau_uvmm_sm_unmap_cleanup(struct nouveau_uvmm = *uvmm, struct nouveau_uvma_alloc *new, struct drm_gpuva_ops *ops); =20 +int nouveau_uvmm_bind_job_init(struct nouveau_uvmm_bind_job **pjob, + struct nouveau_uvmm_bind_job_args *args); + +int nouveau_uvmm_ioctl_vm_init(struct drm_device *dev, void *data, + struct drm_file *file_priv); + +int nouveau_uvmm_ioctl_vm_bind(struct drm_device *dev, void *data, + struct drm_file *file_priv); + static inline void nouveau_uvmm_lock(struct nouveau_uvmm *uvmm) { mutex_lock(&uvmm->mutex); --=20 2.39.1 From nobody Thu Sep 11 16:20:03 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6742BC6379F for ; Fri, 17 Feb 2023 13:50:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230121AbjBQNur (ORCPT ); Fri, 17 Feb 2023 08:50:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230011AbjBQNuL (ORCPT ); Fri, 17 Feb 2023 08:50:11 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C42386928D for ; Fri, 17 Feb 2023 05:49:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641749; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kGtWB8otfeqdskrUgWVgT1wxpbtVBJjmwPhyWCdCJZU=; b=bKy9DeJRI9nCuaWqjOPAWwpttrO1DDKXDahOuDT5wMeLVNXpgOLrvKGaVG48TCvwzXWSeo kwORv4kUvLg6HOSofUGRF+4qbi2IhOndTI6+7GWz9HcaqhLj1BDDzclLM1Ig+aMYW+hgrh kYHDxmAIOHHnjoLkl6OKRC80iv9aO/I= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-562-cN5NGqpqMb2HFgHuaXdISQ-1; Fri, 17 Feb 2023 08:49:07 -0500 X-MC-Unique: cN5NGqpqMb2HFgHuaXdISQ-1 Received: by mail-ed1-f72.google.com with SMTP id d9-20020a056402400900b004ad062fee5eso1742280eda.17 for ; Fri, 17 Feb 2023 05:49:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kGtWB8otfeqdskrUgWVgT1wxpbtVBJjmwPhyWCdCJZU=; b=drnnfB5/sm6PO0zPkzvjCyv1NEWYCn/forZ7vglMWfCx+IgZYTWIP1StB/qFJYGewF XUCvLoiSumf4yOfeWVH2yNYYX8m07h58pgdepRLb03F0GLFJIXqjaZQ9pMPrIdDsTgFU bPX5SK5MLNW7Jjozxe83wTmE6WzbwCESEkG8M6UwbnmwWk5zAKVV1j9f1NAh4LMWX4Gs XaVuJEaqYo9AVo9Ib5UfsFQDqqUqvKdPz7NDrzWjelYRxc2WYTC7ghYu4L4cZeTCyj/s T0blB1pfe7aTphJaGW1NufZHRIfkXDUnV9KkovckuvJnPH2VvfhIosxZFjYaoumhrel/ RKYw== X-Gm-Message-State: AO0yUKWiwt2pfDs8Y0Ub0CM7vJxIHRk3/5WUn4RI3Sk/dgGg61wGTGKz p6vSrX/9cmZYhMKhxgWwZjCwdPTwvVJ1MEFT7lR0A/ZBjBlkiPBbh5+UYfNx7A3Z6lIqiqyyfqC 6G3uhEvQHAWQeO1ckqEA+Al24 X-Received: by 2002:a17:907:a508:b0:8af:54d0:181d with SMTP id vr8-20020a170907a50800b008af54d0181dmr10190318ejc.35.1676641746801; Fri, 17 Feb 2023 05:49:06 -0800 (PST) X-Google-Smtp-Source: AK7set9vd30xGC9ziQLVSo9CH8dMbfP5K/22/ulKcpKmlY9Tzn88e5jN0KhVGuEzFC+li22riHt+gQ== X-Received: by 2002:a17:907:a508:b0:8af:54d0:181d with SMTP id vr8-20020a170907a50800b008af54d0181dmr10190298ejc.35.1676641746605; Fri, 17 Feb 2023 05:49:06 -0800 (PST) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id v26-20020a1709060b5a00b008b1787ce722sm1516323ejg.152.2023.02.17.05.49.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 05:49:06 -0800 (PST) From: Danilo Krummrich To: airlied@gmail.com, daniel@ffwll.ch, tzimmermann@suse.de, mripard@kernel.org, corbet@lwn.net, christian.koenig@amd.com, bskeggs@redhat.com, Liam.Howlett@oracle.com, matthew.brost@intel.com, boris.brezillon@collabora.com, alexdeucher@gmail.com, ogabbay@kernel.org, bagasdotme@gmail.com, willy@infradead.org, jason@jlekstrand.net Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH drm-next v2 16/16] drm/nouveau: debugfs: implement DRM GPU VA debugfs Date: Fri, 17 Feb 2023 14:48:20 +0100 Message-Id: <20230217134820.14672-11-dakr@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230217134422.14116-1-dakr@redhat.com> References: <20230217134422.14116-1-dakr@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Provide the driver indirection iterating over all DRM GPU VA spaces to enable the common 'gpuvas' debugfs file for dumping DRM GPU VA spaces. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nouveau/nouveau_debugfs.c | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/drivers/gpu/drm/nouveau/nouveau_debugfs.c b/drivers/gpu/drm/no= uveau/nouveau_debugfs.c index 2a36d1ca8fda..7f6ccc5d1d86 100644 --- a/drivers/gpu/drm/nouveau/nouveau_debugfs.c +++ b/drivers/gpu/drm/nouveau/nouveau_debugfs.c @@ -202,6 +202,29 @@ nouveau_debugfs_pstate_open(struct inode *inode, struc= t file *file) return single_open(file, nouveau_debugfs_pstate_get, inode->i_private); } =20 +static int +nouveau_debugfs_gpuva(struct seq_file *m, void *data) +{ + struct drm_info_node *node =3D (struct drm_info_node *) m->private; + struct nouveau_drm *drm =3D nouveau_drm(node->minor->dev); + struct nouveau_cli *cli; + + mutex_lock(&drm->clients_lock); + list_for_each_entry(cli, &drm->clients, head) { + struct nouveau_uvmm *uvmm =3D nouveau_cli_uvmm(cli); + + if (!uvmm) + continue; + + nouveau_uvmm_lock(uvmm); + drm_debugfs_gpuva_info(m, &uvmm->umgr); + nouveau_uvmm_unlock(uvmm); + } + mutex_unlock(&drm->clients_lock); + + return 0; +} + static const struct file_operations nouveau_pstate_fops =3D { .owner =3D THIS_MODULE, .open =3D nouveau_debugfs_pstate_open, @@ -213,6 +236,7 @@ static const struct file_operations nouveau_pstate_fops= =3D { static struct drm_info_list nouveau_debugfs_list[] =3D { { "vbios.rom", nouveau_debugfs_vbios_image, 0, NULL }, { "strap_peek", nouveau_debugfs_strap_peek, 0, NULL }, + DRM_DEBUGFS_GPUVA_INFO(nouveau_debugfs_gpuva, NULL), }; #define NOUVEAU_DEBUGFS_ENTRIES ARRAY_SIZE(nouveau_debugfs_list) =20 --=20 2.39.1