From patchwork Fri Feb 17 13:44:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144721 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 26A62C6379F for ; Fri, 17 Feb 2023 13:44:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD0646B0073; Fri, 17 Feb 2023 08:44:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A59A76B0074; Fri, 17 Feb 2023 08:44:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8AC606B0075; Fri, 17 Feb 2023 08:44:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 760526B0073 for ; Fri, 17 Feb 2023 08:44:41 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 545F11A0255 for ; Fri, 17 Feb 2023 13:44:41 +0000 (UTC) X-FDA: 80476903962.23.6E0DD00 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 37BC040009 for ; Fri, 17 Feb 2023 13:44:39 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AdvLkc8d; spf=pass (imf11.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641479; a=rsa-sha256; cv=none; b=GfnSfz9cjw+rTvS55wPByKca9Glv3A6Gz8jttHzmFtq82ijSk0jagakr8ngN+yM0uN3Mp6 ddOlTUvNL0GKjW/KhKYqQ7r4nxa3RBFzty1OoLA76GUjzv8Rb9woH91lhZHwUevM9+gOvf btYRWXVIlfT9OZfPoGjoKoVLrwbjHV0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AdvLkc8d; spf=pass (imf11.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641479; h=from:from:sender: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:dkim-signature; bh=B0IjfZRZB9FdjtHsb79gae4VEMZHyq9PsFfSTQw35tA=; b=tEvznRJpFhdUfLe0kNk57BvVVh9dMMC7dI5Qa1gnwutLTZ1dMaiDVigSr+nC5aqJjCWoUz GEOnMftoCh05zX+50t3mSwB1ug1Wa26L38SSICwvcW0/gS+q1AtEmemjQxoTwYYBGkL1EH tTyKSuGCjAOlLd/F6Ha5dCf3fsXJYWc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641478; 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=AdvLkc8dGu+teJuqKS/i8933Y8AW2j1w1Fi7whW2+vlSFqs3IJUo8CQVjrHWYJvU2k5e0d QuLec0qAavwVMWPMcw8vNLlV0bTVcpQ06nMFTcENA8/QVCeFWAAcr5dWZdzYnIvdXJSqVT Oj9dOQwBR++0kM5jFY7b/pPOaT4kuBs= 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-250-UIi7WwOsN36OVnG1WnG--w-1; Fri, 17 Feb 2023 08:44:34 -0500 X-MC-Unique: UIi7WwOsN36OVnG1WnG--w-1 Received: by mail-ed1-f72.google.com with SMTP id cn9-20020a0564020ca900b004a25d8d7593so1215852edb.0 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=RCUxDswSP2Y9Q17MDV4QszjSjDhlJgC0iJ09IcPkeKBbtZv7STQlvvX7CMxN/agDFm q4djOtl3vFtFGrFl6RrcxIHdjd/raYlY+5WuQX/37mLTYVPy97/uOKdqhwDKUKZBpTGi A29oUI7AlPnGpvsRCICtrTYz8WIHSWKEMRnIoLQl45Y08wB2owQgvfhvRKh2Udocm34h mvIsDPJcxQ0bFYbzBGmObaxFqjGwbgmGsXHv8KnFvd8nIF1KG8/9pjjx3Zv32syn/eou ulD9IaFpofWTkw3tpK3gqupjYEosTDSo1v65B7WjhBnna+AxQ2o00UD9pRGphD8H398Q xaMw== X-Gm-Message-State: AO0yUKUod0apyTAg7Bb6hKiRz3Kty8wOtxlzK8V5aV5Lo9qzWJg2p2pR prRl7nW2YQ1Ox/gPefvZUTa8xzcrIZk6pz99I1TofdQ/NqEARSh6IpQGeq4qjNNPVBs2Q/2J7Lu 3jz+z9dt69E0= X-Received: by 2002:a05:6402:410:b0:4ab:4949:32e9 with SMTP id q16-20020a056402041000b004ab494932e9mr984196edv.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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Queue-Id: 37BC040009 X-Rspamd-Server: rspam01 X-Stat-Signature: 9m5xcgpignc36n1maedw6twqibyz5qy3 X-HE-Tag: 1676641479-613518 X-HE-Meta: U2FsdGVkX1+4x+gUrv2m1hicISZZ7nSJhGb437wlfkHuGUHYdJPg8ZiI+Q6rUBneY9KG3DZxQyncMID33aCLD1FC1luar5K3bgZBGGVgEpD5KW+4rcUL+Lkf7J6BJy3kMFIMPwsl0AxBIGZgJVpKSCGyFYpft7P0CrLfJQTnB67HE356o0Nkn/nZid2L14y8QA4xMhWeT5C9Gf9IliMGsO4R2EiqD+eRcwznx9QJ0xvkKMtY6eQGFlpwVYuDEeope9OgeZfPPj2lc/e6uXiWprz/7t2kYqhaPxWseUavz6FEF9H79Rw9T/cfROJVBpnt1ARVRTnTe9A+rZP2SreEhAEaCOjBqdWjUpRZo7h0YjbV3hHpUUjDly4pahPg9efsuccXe3pGw/u++qDV05N8I8G2r0c0+RrIxR1SySdOtPJGQ/Lq8fH9/+52RkR9JjWIo8dVWYstEXqvukfuxJMrB8eepd0hfgXcN4pmQt13hroi10IUbR13qREdzvnZ+noTb1kW+O1SIQzT+nySAbhznk9R2KFI+ANdDSRbwZ7wuuVEx1Y7AWm1AXFN/M0bybaa9kQGxWv9BdbTtY8bAa0ia6a7JMYbsLYmZvDoVHmYbySwLPi1+IAcD9fKt+DRUHGl5fKxR9Dt1oiRkgKpzliFMXEIcw9FKYXcwodFdCdhE6pABSSlsMFkrq+wm2PrzLEP2+DzdVleI75wsPoagfLOxYqrC23IY+FForw1iEBsCkpPBqfhmqDj5eZhtqIUipK4Y5ZA4OEexNHFxe+MOtK4wCxdnr7E0Hc9Th5vZADCgIobsBrfRmSJkBiJt53Iyx7N+e9XaO0cu+E/Yn7OWk1ylPH+iHwlybuPZ0pYwJhZ9l7N5eaBhBxZztkBtXwj0iWGhRVHuZKSuE9K0Xq7pHAqs+LC/yHlAmeEJsckcVpRB96Wkyghn9yaxZ5RIbtLgjERu6+XKLocqasq7PNDmWO zEwnDl8h HywtXWYCMBV3V7wtOtVMN5kiOiWu8cD4aop4wxEuJPJ72vDaoWhXjEFbygDjSWnJtaO44J9gaR01T4dyyiqwBT56PjmJaV+vXv6ze7XY6E6lktLqDmitCVXegHvJhMjS4X905YB4vy9rxMfgusFzz+1NyPOoTq+6xDuaVrqBF1EK7dtYZrJnE8kl2tdtITf4sIoXea6xra+4CBUPa0wIUHHvNTusCshs1g7kONuRQy+QLkRvNAkeFsMus4m063qJyASarKsHOhk4YDD3q6KlFN+6DSMgV2Q9enVyM9tdKXj21mWnZui17FSkz8eu1L70D3wW3UiZYiSb0ep35aUfE4SGlg3Gaekg3pzgcLOvS+WA3n2MH6d+9Z/M0/GDhJ8K7OeGDv5DyAQGDCFZJuPriqiXrVjLeSLL22HYsDCZsX/wtH6w= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Christian König 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önig --- 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: +DRM Execution context +===================== + +.. 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 ============= 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. +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) += drm_panel_orientation_quirks.o # # Memory-management helpers # +# +obj-$(CONFIG_DRM_EXEC) += drm_exec.o obj-$(CONFIG_DRM_BUDDY) += drm_buddy.o diff --git a/drivers/gpu/drm/amd/amdgpu/Kconfig b/drivers/gpu/drm/amd/amdgpu/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 procedure + * unlocks all previously locked GEM objects and locks the contended one first + * 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 = drm_exec_prepare_obj(&exec, boA, 1); + * drm_exec_continue_on_contention(&exec); + * if (ret) + * goto error; + * + * ret = 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 = kmalloc(PAGE_SIZE, GFP_KERNEL); + + /* If allocation here fails, just delay that till the first use */ + container->max_objects = container->objects ? + PAGE_SIZE / sizeof(void *) : 0; + container->num_objects = 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 == container->max_objects)) { + size_t size = container->max_objects * sizeof(void *); + void *tmp; + + tmp = kvrealloc(container->objects, size, size + PAGE_SIZE, + GFP_KERNEL); + if (!tmp) + return -ENOMEM; + + container->objects = tmp; + container->max_objects += PAGE_SIZE / sizeof(void *); + } + drm_gem_object_get(obj); + container->objects[container->num_objects++] = 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 duplicate + * objects. + */ +void drm_exec_init(struct drm_exec *exec, bool interruptible) +{ + exec->interruptible = interruptible; + drm_exec_objects_init(&exec->locked); + drm_exec_objects_init(&exec->duplicates); + exec->contended = 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 != 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 == DRM_EXEC_DUMMY)) { + exec->contended = NULL; + ww_acquire_init(&exec->ticket, &reservation_ww_class); + return true; + } + + drm_exec_unlock_all(exec); + exec->locked.num_objects = 0; + exec->duplicates.num_objects = 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 = drm_exec_objects_add(container, obj); + if (ret) + return ret; + } + + if (num_fences) { + ret = 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 = exec->contended; + int ret; + + if (likely(!obj)) + return 0; + + if (exec->interruptible) { + ret = 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 = 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 = 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. All + * 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 = drm_exec_lock_contended(exec); + if (unlikely(ret)) + return ret; + + if (exec->interruptible) + ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket); + else + ret = dma_resv_lock(obj->resv, &exec->ticket); + + if (unlikely(ret == -EDEADLK)) { + drm_gem_object_get(obj); + exec->contended = obj; + return -EDEADLK; + } + + if (unlikely(ret == -EALREADY)) { + struct drm_exec_objects *container = &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] == obj) + container = NULL; + + ret = drm_exec_obj_locked(container, obj, num_fences); + if (ret) + return ret; + + } else if (unlikely(ret)) { + return ret; + + } else { + ret = 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 container + */ +#define drm_exec_objects_for_each(array, index, obj) \ + for (index = 0, obj = (array)->objects[0]; \ + index < (array)->num_objects; \ + ++index, obj = (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 objects + * @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 locked. + */ +#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 cleanup + * @exec: drm_exec object + * + * Control flow helper to continue when a contention was detected and we need 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 detected + * and we need to clean up and re-start the loop to prepare all GEM objects. + */ +#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 objects + * 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 From patchwork Fri Feb 17 13:44:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144722 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E4EDC05027 for ; Fri, 17 Feb 2023 13:44:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B70966B0074; Fri, 17 Feb 2023 08:44:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AD03F6B0075; Fri, 17 Feb 2023 08:44:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D54F6B0078; Fri, 17 Feb 2023 08:44:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7857F6B0074 for ; Fri, 17 Feb 2023 08:44:42 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2131016053F for ; Fri, 17 Feb 2023 13:44:42 +0000 (UTC) X-FDA: 80476904004.10.F65657E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 1261114000E for ; Fri, 17 Feb 2023 13:44:39 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VgF1xCMR; spf=pass (imf23.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641480; a=rsa-sha256; cv=none; b=aK1/vHrONzbFf2CnlfCDuH8r7HgZBejWZsMw6PR5aYMRuDdoVD8Q6Zez4aGWM2XS0cOHSv RyUQulHWl/8DcoTHCD6KUuBOtCehX7cf1nbzXCUXHsh6kFLnaAvXOSIWmWbqJPcPuxaKef Kf0s7ZOcwIEzziGbYkjj3mq5HoYyNzk= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VgF1xCMR; spf=pass (imf23.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641480; h=from:from:sender: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:dkim-signature; bh=7uKZrgGFJpURUNNqYPRQvVp3RBdWsUReZR9Qa+W+o5s=; b=39sUeFPfza4hrozEuLNpxtOipqbHSDlUK6gEsLPQUa3PZMnByYBn2EEkmB8j2ypDTg+ALv nDKUz6XS5JN5nDc6zFVMOLwmbbiXcMGAcfZntbehAdQdLVCy6heOawueiY0NpFz4xQO0y9 w8eDUtOGOghbX9c1EyV+Mo4iKwuiDxA= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7uKZrgGFJpURUNNqYPRQvVp3RBdWsUReZR9Qa+W+o5s=; b=VgF1xCMRViNC5MoMXSeppypDYZslYo6iwTqkrAGW/CKURrRaWaH2oBB8ghQnQa96PFRRQt l8mndVHpU4WyuW4Tuntk2qgDBEn1uSvp/jC95zpheDdx7iUMCn6VmfWgDbQGyNp5d/QnTh jdV6Vd6DuDgjwm6fxcMe9ljdF7rNCfY= 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-402-_ppvwkfHO0y03FlztoPngA-1; Fri, 17 Feb 2023 08:44:38 -0500 X-MC-Unique: _ppvwkfHO0y03FlztoPngA-1 Received: by mail-ed1-f70.google.com with SMTP id fd8-20020a056402388800b004ad793116d5so61967edb.23 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=S/UjoHni8dMx8/5VuoxxNSVITC8t7YKVbo3As2yPlIIRIGh7jqXNS+ld9qAYKtSvKH fh7trG/uR982pI/6xdUgiENfn1SHbMrKyQ7grX4BKVPvB97KkHKfB5U6fg08T3AGuDLo bAeoZPG36OLcVYguPZRLAYYZuoDUIo2nbN3xI6wIhjaPdu5nGVgu9Ts0xjmAYQv1dUEv fmUgxzrY8JO9P/0mGHpBAUqfm3gSW0drcRfU0UhminWDgIFueax0riKtaUtpPwaPKUJf KEWv/orfy2lT3VdONpFQqHAihBbtoYXfm42oxfiHueBR+tDdBZseXLdGHntLHgHOnsEj 57cA== X-Gm-Message-State: AO0yUKVZsXHI5ZtWtZHcrbgTSHH+W9vygpCfzxF7VOYLEVivqULVECjw KDfa02a1AmtKwafB95HOljCkahbLFisG0baSS0K6qROerTFRTP6JmvJ7Txv/8F/QNXD13vt3peJ E0Qxkdp8E2wU= X-Received: by 2002:aa7:c9c6:0:b0:4ae:e51e:9e3e with SMTP id i6-20020aa7c9c6000000b004aee51e9e3emr348790edt.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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Queue-Id: 1261114000E X-Rspamd-Server: rspam01 X-Stat-Signature: uezkra74qanjxxokgffo6uwmqmbtgjyu X-HE-Tag: 1676641479-408617 X-HE-Meta: U2FsdGVkX1/3WjhJ1dSSl8PrWs5050bt6qDsB6ATiwG1dF6z37K5CXQUDnIRgrPVMrGM8D3+W+HLGwZpyaq7+GQed6vk/IXOtRUbuEhWFVeVq0J4XhMy5gvwawuK950wahd4D+iwdUgY94i5rZNaWFnFzSzkj1aq5DmwkDGlM76watjzUnTnTXrjnR8drgFU++H1o14/Bj4Xq/yjKCW6GG5ge7HM9+mPTLkctr3NEcLg1Vxb5Cz+mY1FKx0lJ+oQiXwjdT5A0hh8iiqNqYfLpC+oFSs3XbcOZe/HGkGeFHMm4VM5Wl08MufnIGjwf1aGHuJpKyfvJO2c7fHByu5bumd2P4AW0oi9IoBqb+EHWQrW0azq4n2kWepV7K7CdSNIoUISmRR3Urv0Erls/QSnigB2E5V3/szeMog/N7bHYAZ3bb/EsBI1k7JZo/Cj6N0xBRPRyQTkpPj/hNenYdepvliHEnIh+o/+bSFHHjBN1AeSk/fUiypGemofFsIYozyclKMSlYv/iMyWQTF8k3mJp2bOp1ElbjpbIeODzmGdfvxvN4jOm9NNEOTWXVI99xT4rlu7FAVudDtvC5mqg5SpQ8ktw/0GIrCdSnwXj+TUalDYNH0HhN6zOtqRPLazCWGBrV8Ma8Ydli+UzX1J1zZQyACc8JM7nCfycCVWkCi2GevoAPzyu3DRzc8ekBuo/Rb+cU7ZPKSvY4QaGD0dt0mkLEofdsZmH7cAlVgbuZuCJN7icSaJ6tLIo5VvTGZ20hNJL5VFFfENyVPm8JostqrgtrSQfJ1Iu1iT1GYTitbwsVDg9N2K7kZku8fGjG4QX3Za3uTj5zLGIhcbJZj72fS3lQzgps18Ma4m2rfFgUO2KGdS+waOiMnyzR94CP9v/AKV9mEa2zAg/Poux6a5w8ucBQ0gMBmg78lCVqPeDRjlQrJ1ingt5r4pLjx7bppn2RQr9HbYy6qcrkfEDvP5oMu dQZOOlLT twatz4gfZjOLHPX1bUtwdS3IFaRPJdhYJ714uwEDjVD1BKOvu+v+k6Rfb9mPAJ9ROjmbODxnOW2haumgJIVxgkqXrB0+gqxJwFtR5FR9QnTxW1Vsu0iPH5zbBEqO4tV/xAIco1z0EUjip7PnGG07WnGDKT0dfwzKzbcoyxDPi7bfDVehDQm6z7l3SfX4gVGT0fL6fzutmhf3EfxWdRJROjmJ4Ifh29vSm8gPdA/1vywsPlanKkuL953fARNKKf2wZzdKWsX9qK7abJ3OKrVZLhsVBaJ96lFYotuzSP0PCTbzTP2RiSLzZTWz7MI1pLsYK4zkbakOSzsbw2mgekBl1W0u2xwgVok8p/9kg2uJDKvShyMujlDY7LvCFVG/E/BBpP6RKojvrhEEvP0SOgnRemtMpKa3q/Ke8m4lgWkVE6BEUw1Y6ZgfxQIrE7Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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; } - drm_gem_object_get(obj); return 0; error_unlock: From patchwork Fri Feb 17 13:44:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144723 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7010C6379F for ; Fri, 17 Feb 2023 13:44:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4904B280001; Fri, 17 Feb 2023 08:44:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 418206B0078; Fri, 17 Feb 2023 08:44:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BA19280001; Fri, 17 Feb 2023 08:44:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1AA276B0075 for ; Fri, 17 Feb 2023 08:44:46 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E535D1204D0 for ; Fri, 17 Feb 2023 13:44:45 +0000 (UTC) X-FDA: 80476904130.02.0AD2AFC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf03.hostedemail.com (Postfix) with ESMTP id E983F20005 for ; Fri, 17 Feb 2023 13:44:43 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ANKtewFx; spf=pass (imf03.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641484; h=from:from:sender: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:dkim-signature; bh=ysk0aOdzp2igGOtHeZoTd2E71aY9qQ/NU2yUGbV3q7I=; b=1Oq+DSRwUzikG0SnWqtPnWA6uPN0d8jnh8G1LsTyVI6BvNDBfuSFHpaL0AI1QlH7I9/ncT LjmEzKnvaDHtDD5Q/XZ3bwtmV7eqU2/xfut/nMC5Bh+MjgPyLzeN/TnpSBxRrMD8DcfUu6 wnf5weQhJFtFkOMV7HPDk/U9yarUV4E= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ANKtewFx; spf=pass (imf03.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641484; a=rsa-sha256; cv=none; b=NIEHXecDyH6QV2Sqvx6I1LH1ISbPr6AwyPpyH7pj2taDtOhYnFeyxbLLPiJzGAEdJYTihK Z3j69L+mQOdrGAGWjm0YVckIZj6OJ1S976BdUzpXlWp9BfzVfkyyroyJL6aOSy0iUGbydz oAehu8IC/kVO0Sp7DRbYYxIBYLmWKxk= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ysk0aOdzp2igGOtHeZoTd2E71aY9qQ/NU2yUGbV3q7I=; b=ANKtewFx3+0ZDdkRZU15nps/EZcWE8ctnZh1y9h86Aq4ZeT2krQ+z8cn2CZrkF7zR3WOvZ P4FNmFjBnwFxaQIUCjMf0DkoBH4DK9fZX2/b0zPRHmSt5iXF9mmqiJwYxU4nOOLb9COoYY bnRB4UKNtLbbiA+gN71DckoB+8PgjTc= 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-556-oOeKZWSTMM2thW_PIbhVMw-1; Fri, 17 Feb 2023 08:44:42 -0500 X-MC-Unique: oOeKZWSTMM2thW_PIbhVMw-1 Received: by mail-ed1-f72.google.com with SMTP id j10-20020a05640211ca00b004acbf564d75so1082127edw.5 for ; Fri, 17 Feb 2023 05:44:41 -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=yzIiExBJjhTWUspTtbMw1myvENbQRHtlsb+jV3/YpHyZTj8JI5CYbfac4lQp9hdHKM NKLqEsG4OShq06RNikgOSv7xhK5pkRYW6VT4RVMUMgs8vaGU9Jxo/yrUWkAhVsOM2yjn FaAPXeoyfbKD1PNJR7azpFzhttY2OOgFWbiYvfocLhR9VaLxXnfHH2q9sK8wOGpGzRnB ojygdCkucXxhdJaOwnNiwKg74fgGTuvcUtCNUxE5hoYlFbdDNeK4JNYSxZE4jDvbVCPQ eWIldyrlSs+sfy2MgBFq/SlgX68S2fQuNBrQgU62V/l+NNAH5rGWKV25ygLW3paoqkzT Mr7A== X-Gm-Message-State: AO0yUKUWuW8EuzajPRGHHI9ccvIfeqvIPo5CcaVXs220xHWaY3fAM8Zk gpJVMSqETeqOoXbi0mDTRFrUbrgqwxfBz9/x3oPH9z07uB6DSPPUEeyaQB5bkBQY9Vfuf529L5U HdyS0g54UrKw= X-Received: by 2002:a17:907:d309:b0:8b1:2dda:b60d with SMTP id vg9-20020a170907d30900b008b12ddab60dmr5401613ejc.20.1676641481150; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: E983F20005 X-Stat-Signature: yx9fqkdbbwy1i3no1pjsex5wbj4xdrcy X-HE-Tag: 1676641483-83493 X-HE-Meta: U2FsdGVkX1/5cjOzVvF276SsGGadu5Df024rZFwdG9VqubKVx7Ze9QLJrpc8c8LIYdISBAq89cgX240t5j0qInvMAmW7OI/DPcQPh3VTFdVIOMeInh5aLTFdyEOZs7fDJb6zRhCCr7c6MiwfsWYgWYNII/8/NSzTEdWXFivI4A8i8AJxAIJCvBPysSN65i+356v8U7m9Vu0NJH81XD5wzSj41QaS5V+IGJpL9aVSVG/B1YUiizW3i0YCg8pwALTDe9lp7MPVIG66h+07OABya7CJL9dKnUbhLRXL45gW1qBgW9YQ8xd1Mei3h5hdKaCARpkw6pK6T9xdqbccYlYNn+XYbaoO8xX0/8C8a+pHST8N/79+SLH4u9TWFln0OQf037g+FM2ebcIixbVpHgX4dDXbS7yf2u64JbhLm6NdepKVNkrEKx/EaL9ng7260TV4PnO9c5mAO6ga/Cc65SJI80b2gj3cgRVKQvyFHUmNqx9V/ghPNWVvQ7qNZ3j+pgAGEUOJl3YM7qQjtAuanM0KywmD7Ki/mf8C/aZpGNXr9oF2aL30dFFoewyd/grkCiSEs+N6vYMdSQiefudrd4E4I0u8IIn0U9jO3n1PeFI5Cfsh4S0nPWxCVvBQ3AZDFPTmT2VSpUA367MTnQJxIskAMPOcwl0g0K8xCy1vTwcIspyK73iELdFG1iVXobcx1p9EI0f311TUeIeO936iWaeCs23y2u+B9uLq5uEqCY/RoWAmIAZdAZpuuQDB3SuAvsEuohNcMFqbea53Laml6nWJdEku+DJHRlfPvrtn0asqWHgDv3yrt99R53dRjfDgtsvyJU4cH3z5qg/bjpBohWejFddNiisAu1s/PBUznDjPgK6vh7vjyBzdRZdc+vOnDJaNZXYm1vZ5ZX0QTk3Ly6xcRG8tMjAUzMEVGdryPMjheQpv8X9SlmnEVF0xcaw7LyJqjHhsAFCMy1WE1IQ5d1R hSq1PLca /P9ItglsvGJpaiQFgsLoJU9h9bkwcXMv8R+AR5m0zc9vFk11TccjuNAwvYJU1noNBimFV7PUIaJWKeJz+pXGu03D1a43a6Xnly7cz3lMeCxNC6107oZGi1KYfcE0K104VbqHHMpdL3VnKsEze8MuK6YJdDz58SCQIKg8+Pnmgl7SQTDh1HLcFpu1YcJHPtR5CCgX/eKvyXFIi/aXLXCSChxNBV0yNfxMeOQ9dL08PNt7LKoh3nB3Q+69/BG+aNkm699ElLJ8PD1tJb80zy4CEWYDcImOeyC2UxlRGG1fWdHX3T89oynWKsNGYTYGVKl6Q2nS3JD5uYudrDf9Hzg7Xgo1Myj+iTZBXK1LTmv5hk8BazUGSDCEqNJDGKMnHziZZfJ2ajoQ+saVP5B6NiHq/Mx92idfi2jTXSeunZnVPD5qeupH51Q6zP4ha1A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 = { \ .mas = __MA_STATE(&(__mgr)->mt, 0, 0), .mgr = __mgr, .entry = NULL, } \#define sample_iter_for_each_range(it__, start__, end__) \ for ((it__).mas.index = start__, (it__).entry = mas_find(&(it__).mas, end__ - 1); \ (it__).entry; (it__).entry = 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)) -#define MA_STATE(name, mt, first, end) \ - struct ma_state name = { \ +#define __MA_STATE(mt, first, end) \ + { \ .tree = mt, \ .index = first, \ .last = end, \ @@ -435,6 +435,9 @@ struct ma_wr_state { .alloc = NULL, \ } +#define MA_STATE(name, mt, first, end) \ + struct ma_state name = __MA_STATE(mt, first, end) + #define MA_WR_STATE(name, ma_state, wr_entry) \ struct ma_wr_state name = { \ .mas = ma_state, \ From patchwork Fri Feb 17 13:44:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144724 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3641AC6379F for ; Fri, 17 Feb 2023 13:44:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C3140280002; Fri, 17 Feb 2023 08:44:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB90E6B007B; Fri, 17 Feb 2023 08:44:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0BC9280002; Fri, 17 Feb 2023 08:44:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8A1EA6B0078 for ; Fri, 17 Feb 2023 08:44:50 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6BE9F1203AB for ; Fri, 17 Feb 2023 13:44:50 +0000 (UTC) X-FDA: 80476904340.18.573EF9B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 43653A0023 for ; Fri, 17 Feb 2023 13:44:47 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KdZ5OVRO; spf=pass (imf25.hostedemail.com: domain of dakr@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641488; h=from:from:sender: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:dkim-signature; bh=Hw5y2doCl00T3ibUla5BtLSkevMMipuwdAIlo8ABos8=; b=gu3Sh1pEPMCp5cucw08TTPjxbyNESLZiPCc5b/+IZvayq1m/Ywvco6BSoX4qaaGmjT5h5/ QrGAnnrofYN5Bdu8p8S9xOyWXfp5STD5mIC0tN8N2fK3UeEK2nRdVSsu17PRdSBu2f7t3N LkHJ6otosHG1Un6dVy5GUHFUbUwxNdI= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KdZ5OVRO; spf=pass (imf25.hostedemail.com: domain of dakr@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641488; a=rsa-sha256; cv=none; b=Tszqj1/biRf7680vity670G/BXaNOq7NqCX3EtMG/T+KhE6Ijpbd0C5GkfijWRs+ZZkGCL beW/HgOOg2ksI71zYPuCqoC+BDOthiE5Ha3ZQj6Ye7TJo9AjiGU/YtMcQ86eFHzB+5hIDQ H9vSPxClIn+lGYuRDy1Jtizg4fBG4Bk= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Hw5y2doCl00T3ibUla5BtLSkevMMipuwdAIlo8ABos8=; b=KdZ5OVRO/J0tjc8Ie8acigPl6WxpZ6q7cHfGuFNpOQLF6FcVM44mkBSjU4aFSPFI4hBejL lGDjpDTiarTpcEVeDrs3Jwr2g/pIcjH9HeeDBdHecRb3vGnzHv+swQUO4oxdA7His5CfvV 0nOqRVO3cj0jcgk+AYfSQ8bNjp4cMto= 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-235-ydqZVGD3Nz-TcQkSKNIKag-1; Fri, 17 Feb 2023 08:44:46 -0500 X-MC-Unique: ydqZVGD3Nz-TcQkSKNIKag-1 Received: by mail-ed1-f71.google.com with SMTP id cz5-20020a0564021ca500b004a6c2f6a226so1232976edb.15 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=vWYwG4RpS486SICNbEoeFjfQC9+vwgAgZMeX/NH+XpS1xl1myAnwGirSb3gsGtnr9Q LmXE02rU+H5k0lOlbPU3kwlhdXvQm8OPdqFLTtutq/DBJdgIpMWOM7DoVqrFieUJEH0e T1J3bdE/QViExnJY7qXUpON7Wo19sylMf0iUy+BROnFebT/u4ZRXnnb1LrVAT88kRNKB vVdfPQVQn4kYs7htxgn4+0eFh0P2OK1v/hYhM4h9B3t8M9RhZQyNoTf3yY5zKKVD6K7N xqIrwtraRhUltZC28Ch+q1fHIDdBuSHbrndqnmGlOUok+XVv0fFMsvktpweqmG7YOIXI eQtw== X-Gm-Message-State: AO0yUKU8vnecM2JrZi9MYWLnPHmfFPSRcXei23Swg1MoyArdXJKYuFHW sroJGG9TzRHxLDf/KDJ++5upbp5yKt6Ew7JecuJoxDMWN6t9NqTMTtgrnQZDZiUDCrK+VD12gNB 2aTY4mCZfF4U= X-Received: by 2002:a17:907:7204:b0:8ab:a378:5f96 with SMTP id dr4-20020a170907720400b008aba3785f96mr6877792ejc.3.1676641485228; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 43653A0023 X-Stat-Signature: g1tzhjbhejps58yjqunhq8bykfg6gndr X-HE-Tag: 1676641487-684523 X-HE-Meta: U2FsdGVkX18BSsS6TnuIjPClJ/rhJRrEI/QbXxzd9zSujluoyTKHBPYFb4/ylmUycgkaLJdOa41MkMguPr0WK7KFV63HfDRq3atew0DqHRy3UgExlHiOvI0qCOe5xb6nuOGM+Cln723jXfTJbmu3TqBOGxLZ4h/BV3CDPZk8Q6QD6COHODCGxCwVfrN6D1vT3p+hx74uwS3tnn5r8gGkXzBXNP6lfrrJIdbAUwYHyy7OPlAOUJsq0RAdQGQ6B8NNM8Y09TaKMPVQzS24k6aWqoefoOnXumDccbUOWJxfqa2XvU46DqRuR/HaF0NH/6j+JFsvk+JkKUYwOeKiv1yMnUW43OAeY266LQKUsEuayBshDz/Jfadwrm+FKMWPs1WUhMfCxA20bm9raC9hyPhAZ7gYEuCvZg2LdarNcIvkfnryWva0e2w0118/f86UEF1gxu8JeYQNrYF75J144yJxiKsGVCRfoOwgdCGGZ8w4aMqmCF9hmCvFrPwi+FXijslMzp/bbVjr9YwOuSB59fy36n7MSBNTRf3QU2XO444yh6DTGEfxeBXb6WhCFlp87aH4V/SEioAvLcURpsoIFw4JnNAYucPJeOqNkQSJeNcjRuG+LJaq3D1LVIkKx6dIgyo4ayd9DpHPivZ3KW3cOnOBAVvRoOlemvJOzG6SLMHb7R/70qvDPzcbBhSTAfiuVw0vxvHzxTbG2qiyHzfmG3O1dha/hB5Tjo1SnjZ4pcbZI2Sud5rEJSnNX7pi8Al9I41e2d4K3YBCZbtr5EruaaK/W9ZMKufxriF+VkFBvojU6+ykFd5W5CiLK7Y2Iso+uGcVIa0I4IiVpMEm0QAQEIgGTN+2Nz/19eyf0ND4jovREbn6N2sJ2JM7X8oVkanf1U9IiAN4Iwe3Yr5JN+nqqe5tgW6lKPcBTJoQuffzlfNDE/M3oLqALlrWVqgwXmYT0B6DO56OJy6lMGTuXL6ItuE HKN3+4jr ED34IZLTj8sbVKrk0VF/2+iTpQg5JpQKwi+Bp0yC4Vj2476rbuM1R6XFIo7EIit0g1f1+IJlAMq9xOaF1kyiMy4xjTMQhuBTG3u6B+5cxz7YKfQ157IzdTpQoxM70GZRMpfQfZwAoy67JoooluJ0CgIAuTN1a6G5Nte84kGKFgBrG7NJQMr65xmvDwgShitjQicO4rA/gn4Lpcn5le/i8ztdsc0Rt8sZvVc4R+ItPof/VdEusKCreDGzI8Z/KIqtaCrPgr5H5jsQnzMN9AY5cMRyz8X6RQTIQ9o7rAsiIhUqDS4HftXT1wHonMCjVkguFWplpaLEeEL+SIKynzovoVnirM4smAePL78kn/TAPHhb8zzbYv0Jyj8J1fV0KElxcdOK42XzaLpRgnapX5YNHpK0KFwc9iUjiTMj2RebZ+PotG4sWwRR3MJpswg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 #define MAPLE_HEIGHT_MAX 31 @@ -559,11 +560,16 @@ static inline void mas_set(struct ma_state *mas, unsigned long index) mas_set_range(mas, index, index); } -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) == MT_FLAGS_LOCK_EXTERN; } +static inline bool mt_lock_none(const struct maple_tree *mt) +{ + return (mt->ma_flags & MT_FLAGS_LOCK_MASK) == 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 = 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; - if (mt_external_lock(mt)) { + if (mt_lock_external(mt)) { BUG_ON(!mt_lock_is_held(mt)); mt->ma_flags &= ~MT_FLAGS_USE_RCU; + } else if (mt_lock_none(mt)) { + mt->ma_flags &= ~MT_FLAGS_USE_RCU; } else { mtree_lock(mt); mt->ma_flags &= ~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; - if (mt_external_lock(mt)) { + if (mt_lock_external(mt)) { BUG_ON(!mt_lock_is_held(mt)); mt->ma_flags |= MT_FLAGS_USE_RCU; + } else if (mt_lock_none(mt)) { + mt->ma_flags |= MT_FLAGS_USE_RCU; } else { mtree_lock(mt); mt->ma_flags |= 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) 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); } 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; } - 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); From patchwork Fri Feb 17 13:44:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144725 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7C65C05027 for ; Fri, 17 Feb 2023 13:44:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 44110280003; Fri, 17 Feb 2023 08:44:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C9D96B007B; Fri, 17 Feb 2023 08:44:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 189456B0074; Fri, 17 Feb 2023 08:44:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 015206B0074 for ; Fri, 17 Feb 2023 08:44:56 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CD47040433 for ; Fri, 17 Feb 2023 13:44:56 +0000 (UTC) X-FDA: 80476904592.18.36B5482 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id 892441A000A for ; Fri, 17 Feb 2023 13:44:54 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RVMoe3OE; spf=pass (imf19.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641494; h=from:from:sender: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:dkim-signature; bh=2dQGSqJNfvjw1TcjewHtrd025tzeuBg4RcAC7yA56II=; b=Mc3eV+P2FM7osyAbCRTN/srQhoR0aNYOD7r57BZDwQlPTZI0LflRnbKIEM89WShrMhjjL0 jP/RdyK5UnIdswltaVBVxDRnnRiot+z1YxqXVOnGfL8MDX9GLvnSq+SEj4UPelRLo1x857 tTdwWXl4JOrg2Cw9rPEaUOj2Q5u/h54= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=RVMoe3OE; spf=pass (imf19.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641494; a=rsa-sha256; cv=none; b=OXYIapRvxaHhwnuJaQMEXclbTwdTEIbUfajvD+ONStli61c7iBQEbfiU/l/Vi0lNy4E/tE VmXvZNbgeIT7WoLKfV9GqClzqL9/81hP/r+HpR7zyGOK1VZ7rdxdfjMF64rMknhHWFKYPM 8EjM5q6/34bpQJpM8kBkGQ5j+OCVo4g= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641493; 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=2dQGSqJNfvjw1TcjewHtrd025tzeuBg4RcAC7yA56II=; b=RVMoe3OElx1t2cJQ1aGt8zKO3Jh+OW5lE1Cn2+Mv5aBh6mtj3ih5uVl2i0OMC2XrsNXQpd eY7SWH83u3bVuwNTVLA1tBUrS1OYqjOcZZhh2jsawfIfqRFVwxAE/HxMstsYIXWZX+K+tr l2ifzYnjZhCsHXLFVbcySX+9RNy+5bI= 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-196--Jdn_k5wOVGVkwwznmgkMQ-1; Fri, 17 Feb 2023 08:44:52 -0500 X-MC-Unique: -Jdn_k5wOVGVkwwznmgkMQ-1 Received: by mail-ed1-f72.google.com with SMTP id m28-20020a50999c000000b004a245f58006so2149426edb.12 for ; Fri, 17 Feb 2023 05:44:52 -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=ZanNA+byqQalzh4HqReeEV9w2byOnmpqMZ3QjxWNZgg8UqK0RTGD6XLfSlZT8PsOOG aHPEK7lGVvfST8DzpLm/5QLBh3MydPaobPtfltUaNRzURJezhx/DjXuvkiHZRlY4ryBQ ol8JTFxH7DphvKVhOrsOgLpgW3Dy3UyJN46espWDVNdi8DC54sIVL0mEGRnepdcIRh4U m8+ZtiB/fQaSsb12byiKVakjzje7VQjo5o/8Ix7kp0OzyaTp//VrYpgHNH6dlf2OCzEd cJtHIqD1CyMViSRsPLISKleUDa1e2VVXuifnAP00Q2iPvBa+QwyCSJr+g2FSy48VIeXm NGog== X-Gm-Message-State: AO0yUKUEjTNy8FT1KRXHn6Cnx6unSDPWfzLOZh2oPNoW9M6wSnmh1Kq9 ACBxqhXJiPw9IcRPoy06VmPg4T/dfUZsUlfI79IyOwFO38oBEeAmWklJz8nxM7QeosZrOenUVGr Ja/eLZamYJDQ= X-Received: by 2002:a17:906:7fc8:b0:88d:5043:2299 with SMTP id r8-20020a1709067fc800b0088d50432299mr1149590ejs.51.1676641490112; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 892441A000A X-Stat-Signature: 1gsxk3rpzgx5d1aprxbd6pdh4ag54cbk X-Rspam-User: X-HE-Tag: 1676641494-854024 X-HE-Meta: U2FsdGVkX18iOZgdTealTYQuvitW22LFbgIWEgNB4mr78ZOwWNEGXi4TyQCz8IBfqrCta3zi+qCTWgmTi/UCg60qXscM+DNE+y31apazp49CrHSyCBMf5znQwlYQlZfzyPBDenBJprosKfkD+17SOLOWdB4tGhHN5X9Rwyecfzpxnx/0NX5/q3MP7FlSEEC4c2Ix4vSdwhgsH+TuB1yhR7MjBECSwwO1yqen8sU6On6kxPjhmsIilS+iKUhkV//onvRtZYBM3Lf6Ca2A8nw0dJ7YTJqZegL9HMSQuiSfLP0mbPjdbvj3rYa9vvH4/8OuvsjzguBFqrItVPJYK0jqyA9s9tp6MZN5A1+mnYcGA6nYQ3lO9ovvUGjlF/VtIYBFa51h6IWAR2A3Dgl00YAfYrzPIUFJqUhYSAXlmU7PIV3etvpiR01MHnkhDCtFnJXv75nnlpC+e6Sp9hir4mANAIC+nCVqmuDJXuDPksHJTjwuzHEO9wkOhmZD1g2gfGdg4irJVlWqcM/DvjkZYL4dnBnhjUBrSozz5wYXCAM3uEJ3S3J1WvVziNb2bbRTY3kOLMvrctqocnP/rVvW4hWGqMTEz1PA6B2Wvt6qUiUbdzed3FhZwLd6K2hKx9oBuWm9zzR61AtxCS1fH8OiCZ58m/5XTtLRlDLPnXy+lS03ZMzXVmcLy3C4qIbrK9wQ8YDwfYrLOkVAqqVCeeWGi9mTvsM374iNL5khBbdw+A4V7JQTd2m4vJ0iwbCk9y+O6gtSzsYz/kbmxTfVUMSA7cHLxtrDVS33EAyBG5+wyqG6cG9m1Khudr4tgDy8E6dFLOQobSeneO4IO07r3lRt1EX1tvIzyfxUeoS4yz2E742Ll1FGA7jFQvA6NtXVnAWcSEqN+lyvvzvbn9WbLOosTroniSGRLZHOSSiVY3qJ1qdVLM4mNBrofTwIJsT1NaS36d/TPqzjompak7yzEvO2swA HFkxRmd7 e9LUnzow8lEFwNUgdeBtBLSziV3IRG/m9/c8S0T/wkSnTmJYHXIaKZUl+TofSTKpERNh0eDLspjnBIIKU341uluKToJsUR1eAFQhOHr8ydFwhcMdooCh+y3uiUXI7VGqZe6OzQfZLRhJWwU+yRINK9ChwZKiIcuijMEJ1BioBSZ+Sv+Oomj+FAt6ISmMxkcLXNQVRRPXIST1RW1f/75Yrc5s7hG6hMORSZL8H+XnTCioeBDNRdp8mgU5yWpclludcLzrjhvX0YVZ9CiYwB5AVZvxxy58rtcV4bDmHZFDN249ke/bA8ltdCd8hu4aGC+KW41Kc2U6QWjvjXyNsyay5zC92kh2qcmYG/NmFTcf85ZB2KxhKPx1gEZ18oysOo0aPNf5jwZXli5S7z2DtquoEGB3u73ByK9MmPaPViQlG8FDKLv4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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: +DRM GPU VA Manager +================== + +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 =================== 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 := \ drm_vblank.o \ drm_vblank_work.o \ drm_vma_manager.o \ + drm_gpuva_mgr.o \ drm_writeback.o drm-$(CONFIG_DRM_LEGACY) += \ 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 = &obj->_resv; + 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_mgr.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 limitation + * 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 track + * of a GPU's virtual address (VA) space and manages the corresponding virtual + * 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 create + * 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 address + * space. + * + * Besides the GPU VA space regions (&drm_gpuva_region) allocated by a driver + * the &drm_gpuva_manager contains a special region representing the portion 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 destroyed. + * + * 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 splitting 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 implement + * 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 mappings + * 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 single + * map operation. The caller might receive no callback at all if no operation is + * required, e.g. if the requested mapping already exists in the exact same way. + * + * The single map operation, if existent, represents the original map operation + * requested by the caller. Please note that this operation might be altered + * comparing it with the original map operation, e.g. because it was merged with + * an already existent mapping. Hence, drivers must execute this map operation + * 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 consists 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, one 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 referred 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 mappings + * 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 implemented + * 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=n) + * + * 0 a 1 + * req: |-----------| (bo_offset=n) + * + * 0 a 1 + * new: |-----------| (bo_offset=n) + * + * + * 2) Requested mapping is identical, except for the BO offset, hence replace + * the mapping. + * + * :: + * + * 0 a 1 + * old: |-----------| (bo_offset=n) + * + * 0 a 1 + * req: |-----------| (bo_offset=m) + * + * 0 a 1 + * new: |-----------| (bo_offset=m) + * + * + * 3) Requested mapping is identical, except for the backing BO, hence replace + * the mapping. + * + * :: + * + * 0 a 1 + * old: |-----------| (bo_offset=n) + * + * 0 b 1 + * req: |-----------| (bo_offset=n) + * + * 0 b 1 + * new: |-----------| (bo_offset=n) + * + * + * 4) Existent mapping is a left aligned subset of the requested one, hence + * replace the existent one. + * + * :: + * + * 0 a 1 + * old: |-----| (bo_offset=n) + * + * 0 a 2 + * req: |-----------| (bo_offset=n) + * + * 0 a 2 + * new: |-----------| (bo_offset=n) + * + * .. 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 one, + * but backed by a different BO. Hence, map the requested mapping and split + * the existent one adjusting it's BO offset. + * + * :: + * + * 0 a 2 + * old: |-----------| (bo_offset=n) + * + * 0 b 1 + * req: |-----| (bo_offset=n) + * + * 0 b 1 a' 2 + * new: |-----|-----| (b.bo_offset=n, a.bo_offset=n+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=n) + * + * 0 a 1 + * req: |-----| (bo_offset=n) + * + * 0 a 2 + * new: |-----------| (bo_offset=n) + * + * + * 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 split + * the existent one, without adjusting the BO offset. + * + * :: + * + * 0 a 2 + * old: |-----------| (bo_offset=n) + * + * 1 b 2 + * req: |-----| (bo_offset=m) + * + * 0 a 1 b 2 + * new: |-----|-----| (a.bo_offset=n,b.bo_offset=m) + * + * + * 8) Existent mapping is a superset of the requested mapping, hence noop. + * + * :: + * + * 0 a 2 + * old: |-----------| (bo_offset=n) + * + * 1 a 2 + * req: |-----| (bo_offset=n+1) + * + * 0 a 2 + * new: |-----------| (bo_offset=n) + * + * + * 9) Existent mapping is overlapped at the end by the requested mapping backed + * 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=n) + * + * 1 b 3 + * req: |-----------| (bo_offset=m) + * + * 0 a 1 b 3 + * new: |-----|-----------| (a.bo_offset=n,b.bo_offset=m) + * + * + * 10) Existent mapping is overlapped by the requested mapping, both having the + * same backing BO with a contiguous offset. Hence, merge both mappings. + * + * :: + * + * 0 a 2 + * old: |-----------| (bo_offset=n) + * + * 1 a 3 + * req: |-----------| (bo_offset=n+1) + * + * 0 a 3 + * new: |-----------------| (bo_offset=n) + * + * + * 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=n) + * + * 1 b 2 + * req: |-----| (bo_offset=m) + * + * 0 a 1 b 2 a' 3 + * new: |-----|-----|-----| (a.bo_offset=n,b.bo_offset=m,a'.bo_offset=n+2) + * + * + * 12) Requested mapping is a contiguous subset of the existent one, hence noop. + * + * :: + * + * 0 a 3 + * old: |-----------------| (bo_offset=n) + * + * 1 a 2 + * req: |-----| (bo_offset=n+1) + * + * 0 a 3 + * old: |-----------------| (bo_offset=n) + * + * + * 13) Existent mapping is a right aligned subset of the requested one, hence + * replace the existent one. + * + * :: + * + * 1 a 2 + * old: |-----| (bo_offset=n+1) + * + * 0 a 2 + * req: |-----------| (bo_offset=n) + * + * 0 a 2 + * new: |-----------| (bo_offset=n) + * + * .. 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=n+1) + * + * 0 a 3 + * req: |----------------| (bo_offset=n) + * + * 0 a 3 + * new: |----------------| (bo_offset=n) + * + * .. 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 mapping + * backed by a different BO. Hence, map the requested mapping and split up + * the existent one, adjusting it's BO offset accordingly. + * + * :: + * + * 1 a 3 + * old: |-----------| (bo_offset=n) + * + * 0 b 2 + * req: |-----------| (bo_offset=m) + * + * 0 b 2 a' 3 + * new: |-----------|-----| (b.bo_offset=m,a.bo_offset=n+2) + * + * + * 16) Requested mapping fills the gap between two existent mappings all having + * the same backing BO, such that all three have a contiguous BO offset. + * Hence, merge all mappings. + * + * :: + * + * 0 a 1 + * old: |-----------| (bo_offset=n) + * + * 2 a 3 + * old': |-----------| (bo_offset=n+2) + * + * 1 a 2 + * req: |-----------| (bo_offset=n+1) + * + * a + * new: |----------------------------------| (bo_offset=n) + */ + +/** + * 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 want to + * protect the following operations: inserting, removing and iterating + * &drm_gpuva and &drm_gpuva_region objects as well as generating all kinds 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_unmap + * @flags: the feature flags of the &drm_gpuva_manager + * + * The &drm_gpuva_manager must be initialized with this function before use. + * + * Note that @mgr must be cleared to 0 before calling this function. The given + * &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 = start_offset; + mgr->mm_range = range; + + mgr->name = name ? name : "unknown"; + mgr->ops = ops; + mgr->flags = flags; + + memset(&mgr->kernel_alloc_region, 0, sizeof(struct drm_gpuva_region)); + mgr->kernel_alloc_region.va.addr = reserve_offset; + mgr->kernel_alloc_region.va.range = 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 = 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 = addr + range; + u64 mm_start = mgr->mm_start; + u64 mm_end = 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 range) +{ + u64 end = addr + range; + u64 kstart = mgr->kernel_alloc_region.va.addr; + u64 kend = 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 = it.reg; + + if (reg->va.addr <= addr && + reg->va.addr + reg->va.range >= addr + range && + reg != &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 = va->va.addr; + u64 range = va->va.range; + MA_STATE(mas, &mgr->va_mt, addr, addr + range - 1); + struct drm_gpuva_region *reg = 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 = drm_gpuva_in_region(mgr, addr, range); + if (unlikely(!reg)) + return -EINVAL; + } + + if (unlikely(drm_gpuva_find_first(mgr, addr, range))) + return -EEXIST; + + ret = mas_store_gfp(&mas, va, GFP_KERNEL); + if (unlikely(ret)) + return ret; + + va->mgr = mgr; + va->region = 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 = drm_gpuva_find_first(mgr, addr, range); + if (!va) + goto out; + + if (va->va.range != 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 address. + * + * Note that if there is any free space between the GPU VA mappings no mapping + * 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 <= 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 mapping + * 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 >= (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 = reg->va.addr; + u64 range = 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 = mas_store_gfp(&mas, reg, GFP_KERNEL); + if (unlikely(ret)) + return ret; + + reg->mgr = 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 = 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 = reg->mgr; + + if (unlikely(!(mgr->flags & DRM_GPUVA_MANAGER_REGIONS))) + return; + + if (unlikely(reg == &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 given + * 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 = drm_gpuva_region_find_first(mgr, addr, range); + if (!reg) + goto out; + + if (reg->va.range != 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 = {}; + + op.op = DRM_GPUVA_OP_MAP; + op.map.va.addr = addr; + op.map.va.range = range; + op.map.gem.obj = obj; + op.map.gem.offset = 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 = {}; + struct drm_gpuva_op_remap *r; + + op.op = DRM_GPUVA_OP_REMAP; + r = &op.remap; + r->prev = prev; + r->next = next; + r->unmap = 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 = {}; + + op.op = DRM_GPUVA_OP_UNMAP; + op.unmap.va = va; + op.unmap.keep = 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 = NULL; + u64 req_end = req_addr + req_range; + bool skip_pmerge = false, skip_nmerge = false; + int ret; + + step = 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 = it.va; + struct drm_gem_object *obj = va->gem.obj; + u64 offset = va->gem.offset; + u64 addr = va->va.addr; + u64 range = va->va.range; + u64 end = addr + range; + bool merge = 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 = true; + skip_nmerge = true; + + if (addr == req_addr) { + merge &= obj == req_obj && + offset == req_offset; + + if (end == req_end) { + if (merge) + goto done; + + ret = op_unmap_cb(step, priv, va, false); + if (ret) + return ret; + break; + } + + if (end < req_end) { + skip_nmerge = false; + ret = op_unmap_cb(step, priv, va, merge); + if (ret) + return ret; + goto next; + } + + if (end > req_end) { + struct drm_gpuva_op_map n = { + .va.addr = req_end, + .va.range = range - req_range, + .gem.obj = obj, + .gem.offset = offset + req_range, + }; + struct drm_gpuva_op_unmap u = { .va = va }; + + if (merge) + goto done; + + ret = op_remap_cb(step, priv, NULL, &n, &u); + if (ret) + return ret; + break; + } + } else if (addr < req_addr) { + u64 ls_range = req_addr - addr; + struct drm_gpuva_op_map p = { + .va.addr = addr, + .va.range = ls_range, + .gem.obj = obj, + .gem.offset = offset, + }; + struct drm_gpuva_op_unmap u = { .va = va }; + + merge &= obj == req_obj && + offset + ls_range == req_offset; + + if (end == req_end) { + if (merge) + goto done; + + ret = op_remap_cb(step, priv, &p, NULL, &u); + if (ret) + return ret; + break; + } + + if (end < req_end) { + u64 new_addr = addr; + u64 new_range = req_range + ls_range; + u64 new_offset = 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 = new_offset; + req_addr = new_addr; + req_range = new_range; + ret = op_unmap_cb(step, priv, va, true); + if (ret) + return ret; + goto next; + } + + ret = op_remap_cb(step, priv, &p, NULL, &u); + if (ret) + return ret; + goto next; + } + + if (end > req_end) { + struct drm_gpuva_op_map n = { + .va.addr = req_end, + .va.range = end - req_end, + .gem.obj = obj, + .gem.offset = offset + ls_range + + req_range, + }; + + if (merge) + goto done; + + ret = op_remap_cb(step, priv, &p, &n, &u); + if (ret) + return ret; + break; + } + } else if (addr > req_addr) { + merge &= obj == req_obj && + offset == req_offset + + (addr - req_addr); + + if (!prev) + skip_pmerge = false; + + if (end == req_end) { + ret = op_unmap_cb(step, priv, va, merge); + if (ret) + return ret; + break; + } + + if (end < req_end) { + skip_nmerge = false; + ret = op_unmap_cb(step, priv, va, merge); + if (ret) + return ret; + goto next; + } + + if (end > req_end) { + struct drm_gpuva_op_map n = { + .va.addr = req_end, + .va.range = end - req_end, + .gem.obj = obj, + .gem.offset = offset + req_end - addr, + }; + struct drm_gpuva_op_unmap u = { .va = va }; + u64 new_end = end; + u64 new_range = 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 = new_end; + req_range = new_range; + ret = op_unmap_cb(step, priv, va, true); + if (ret) + return ret; + break; + } + + ret = op_remap_cb(step, priv, NULL, &n, &u); + if (ret) + return ret; + break; + } + } +next: + prev = va; + } + + va = skip_pmerge ? NULL : drm_gpuva_find_prev(mgr, req_addr); + if (va) { + struct drm_gem_object *obj = va->gem.obj; + u64 offset = va->gem.offset; + u64 addr = va->va.addr; + u64 range = va->va.range; + u64 new_offset = offset; + u64 new_addr = addr; + u64 new_range = req_range + range; + bool merge = gpuva_should_merge(va) && + obj == req_obj && + offset + range == req_offset; + + if (mgr->flags & DRM_GPUVA_MANAGER_REGIONS) + merge &= drm_gpuva_in_any_region(mgr, new_addr, + new_range); + + if (merge) { + ret = op_unmap_cb(step, priv, va, true); + if (ret) + return ret; + + req_offset = new_offset; + req_addr = new_addr; + req_range = new_range; + } + } + + va = skip_nmerge ? NULL : drm_gpuva_find_next(mgr, req_end); + if (va) { + struct drm_gem_object *obj = va->gem.obj; + u64 offset = va->gem.offset; + u64 addr = va->va.addr; + u64 range = va->va.range; + u64 end = addr + range; + u64 new_range = req_range + range; + u64 new_end = end; + bool merge = gpuva_should_merge(va) && + obj == req_obj && + offset == req_offset + req_range; + + if (mgr->flags & DRM_GPUVA_MANAGER_REGIONS) + merge &= drm_gpuva_in_any_region(mgr, req_addr, + new_range); + + if (merge) { + ret = op_unmap_cb(step, priv, va, true); + if (ret) + return ret; + + req_range = new_range; + req_end = new_end; + } + } + + ret = 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 = req_addr + req_range; + int ret; + + step = ops->sm_unmap_step; + + drm_gpuva_iter_for_each_range(it, req_addr, req_end) { + struct drm_gpuva *va = it.va; + struct drm_gpuva_op_map prev = {}, next = {}; + bool prev_split = false, next_split = false; + struct drm_gem_object *obj = va->gem.obj; + u64 offset = va->gem.offset; + u64 addr = va->va.addr; + u64 range = va->va.range; + u64 end = addr + range; + + if (addr < req_addr) { + prev.va.addr = addr; + prev.va.range = req_addr - addr; + prev.gem.obj = obj; + prev.gem.offset = offset; + + prev_split = true; + } + + if (end > req_end) { + next.va.addr = req_end; + next.va.range = end - req_end; + next.gem.obj = obj; + next.gem.offset = offset + (req_end - addr); + + next_split = true; + } + + if (prev_split || next_split) { + struct drm_gpuva_op_unmap unmap = { .va = va }; + + ret = op_remap_cb(step, priv, &prev, &next, &unmap); + if (ret) + return ret; + } else { + ret = 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 within + * the callback. In case the driver decides to copy and store the operations for + * later processing neither this function nor &drm_gpuva_sm_unmap is allowed 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 required, + * 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 note + * that the map operation might has been modified, e.g. if it was merged with + * 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 within + * the callback. In case the driver decides to copy and store the operations 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 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 unmap and remap operations, depending on + * whether there are actual overlapping mappings to split. + * + * There can be an arbitrary amount of unmap operations and a maximum of two + * 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 = mgr->ops; + struct drm_gpuva_op *op; + + if (fn && fn->op_alloc) + op = fn->op_alloc(); + else + op = 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 = 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 = priv; + struct drm_gpuva_manager *mgr = args->mgr; + struct drm_gpuva_ops *ops = args->ops; + struct drm_gpuva_op *op; + + op = gpuva_op_alloc(mgr); + if (unlikely(!op)) + goto err; + + memcpy(op, __op, sizeof(*op)); + + if (op->op == DRM_GPUVA_OP_REMAP) { + struct drm_gpuva_op_remap *__r = &__op->remap; + struct drm_gpuva_op_remap *r = &op->remap; + + r->unmap = kmemdup(__r->unmap, sizeof(*r->unmap), + GFP_KERNEL); + if (unlikely(!r->unmap)) + goto err_free_op; + + if (__r->prev) { + r->prev = kmemdup(__r->prev, sizeof(*r->prev), + GFP_KERNEL); + if (unlikely(!r->prev)) + goto err_free_unmap; + } + + if (__r->next) { + r->next = 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 = { + .sm_map_step = drm_gpuva_sm_step, + .sm_unmap_step = drm_gpuva_sm_step, +}; + +/** + * drm_gpuva_sm_map_ops_create - creates the &drm_gpuva_ops to split and merge + * @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 merging + * of existent mapping(s) with the newly requested one. + * + * The list can be iterated with &drm_gpuva_for_each_op and must be processed + * 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 mapping + * 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 note + * that the map operation might has been modified, e.g. if it was merged with an + * existent mapping. + * + * Note that before calling this function again with another mapping request it + * is necessary to update the &drm_gpuva_manager's view of the GPU VA space. 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_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 failure + */ +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 = kzalloc(sizeof(*ops), GFP_KERNEL); + if (unlikely(!ops)) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&ops->list); + + args.mgr = mgr; + args.ops = ops; + + ret = __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 processed + * in the given order. It can contain unmap and remap operations, depending on + * whether there are actual overlapping mappings to split. + * + * There can be an arbitrary amount of unmap operations and a maximum of two + * remap operations. + * + * Note that before calling this function again with another range to unmap it + * is necessary to update the &drm_gpuva_manager's view of the GPU VA space. The + * previously obtained operations must be processed or abandoned. 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. + * + * 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 failure + */ +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 = kzalloc(sizeof(*ops), GFP_KERNEL); + if (unlikely(!ops)) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&ops->list); + + args.mgr = mgr; + args.ops = ops; + + ret = __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 processed + * 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 failure + */ +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 = 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 = gpuva_op_alloc(mgr); + if (!op) { + ret = -ENOMEM; + goto err_free_ops; + } + + op->op = DRM_GPUVA_OP_PREFETCH; + op->prefetch.va = 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 every + * GPUVA attached to a GEM. + * + * The list can be iterated with &drm_gpuva_for_each_op and consists out of 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 failure + */ +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 = kzalloc(sizeof(*ops), GFP_KERNEL); + if (!ops) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&ops->list); + + drm_gem_for_each_gpuva(va, obj) { + op = gpuva_op_alloc(mgr); + if (!op) { + ret = -ENOMEM; + goto err_free_ops; + } + + op->op = DRM_GPUVA_OP_UNMAP; + op->unmap.va = 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 associated + * 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 == 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 = BIT(7), + /** + * @DRIVER_GEM_GPUVA: + * + * Driver supports user defined GPU VA bindings for GEM objects. + */ + DRIVER_GEM_GPUVA = BIT(8), /* IMPORTANT: Below are all the legacy flags, add new ones above. */ 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 @@ #include #include +#include +#include #include @@ -337,6 +339,17 @@ struct drm_gem_object { */ struct dma_resv _resv; + /** + * @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, struct drm_gem_object *obj); unsigned long drm_gem_lru_scan(struct drm_gem_lru *lru, unsigned nr_to_scan, bool (*shrink)(struct drm_gem_object *obj)); +/** + * 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 support 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 gpuvas + * @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(), hence + * 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 limitation + * 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_region + */ + 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 = (1 << 0), + + /** + * @DRM_GPUVA_USERBITS: user defined bits + */ + DRM_GPUVA_USERBITS = (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 start); +struct drm_gpuva *drm_gpuva_find_next(struct drm_gpuva_manager *mgr, u64 end); + +/** + * drm_gpuva_evict - sets whether the backing GEM of this &drm_gpuva is evicted + * @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 |= DRM_GPUVA_EVICTED; + else + va->flags &= ~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 = (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) tree + */ +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 = { \ + .mas = __MA_STATE(&(mgr__)->va_mt, 0, 0), \ + .mgr = mgr__, \ + .va = 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 = { \ + .mas = __MA_STATE(&(mgr__)->region_mt, 0, 0), \ + .mgr = mgr__, \ + .reg = NULL, \ + } + +/** + * drm_gpuva_iter_for_each_range - iternator to walk over a range of entries + * @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 overlap) + * + * 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 = start__, (it__).entry = mas_find(&(it__).mas, end__ - 1); \ + (it__).entry; (it__).entry = 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_manager. + */ +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 manager. + * + * A remap operation is generated when an existing GPU VA mmapping is split 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 mapping. + * @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 address + * 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 driver + * specific data for creating the new mappings from the unmap operations's + * &drm_gpuva structure which typically is embedded in larger driver specific + * 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)->list, 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 elements. + */ +#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 point + * @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 beginning + * 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__ */ From patchwork Fri Feb 17 13:48:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144726 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8ECDDC64EC4 for ; Fri, 17 Feb 2023 13:48:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 131406B0072; Fri, 17 Feb 2023 08:48:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E2286B0073; Fri, 17 Feb 2023 08:48:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC4766B0074; Fri, 17 Feb 2023 08:48:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id DD9DF6B0072 for ; Fri, 17 Feb 2023 08:48:29 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AECE1C0661 for ; Fri, 17 Feb 2023 13:48:29 +0000 (UTC) X-FDA: 80476913538.23.4916E0A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 977D710000D for ; Fri, 17 Feb 2023 13:48:27 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OZpFGAGi; spf=pass (imf14.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641707; h=from:from:sender: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:dkim-signature; bh=Px4ciz6by/X66jGqheLS55GJ7hc35ETECtAGNQXcKuY=; b=ZZ+ibmsx/craozziuKjf2mKZ32/mQ5z8lVqCtXUE/c0X+4eHdRTpMTKRwL33jcFifbrpZv UBnCS4xYTv45vJ7kHp3cmS5C2BumsD/WKvGYObE4Gi+Q3+iKhcxv7XNEvn9bofMF3Y29Id y8u//soVpy1Le5N8Aijew0/KzDzkBnc= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OZpFGAGi; spf=pass (imf14.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641707; a=rsa-sha256; cv=none; b=nNdAtinNqDTEoQBPaQ/wLBRh2ATE35FRiLuLhx9BOGSqa/YxqA4SX7dI0ikh0VZaMX9zjW EznWEG0+kIFrB7mSalcFKtxETMOt0ojJmy06IlMCouilx9PVYXFsIxXl9KxcMhW8RTFqjw QWOOcqsJbCvLXNXntjGjfUvcmRHSlJM= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Px4ciz6by/X66jGqheLS55GJ7hc35ETECtAGNQXcKuY=; b=OZpFGAGiqdH3cmzpusJwaNbvby4UYKDfhBoud00W4Yx9OoKECXg5rh4dZCK2VN9VcQbaNQ ZBXA0esVyClADIbD7pvn+T3rmLukPd1ocoTPZO3BGmbkwm3s326Luf+Wn2Yov8WBs4nrew iwjNsEUb2p/bdgU4fWWexNpjx4yfE/I= 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-348-LgexFbYtN1iFtJIi8ZAhNw-1; Fri, 17 Feb 2023 08:48:25 -0500 X-MC-Unique: LgexFbYtN1iFtJIi8ZAhNw-1 Received: by mail-ed1-f70.google.com with SMTP id bd13-20020a056402206d00b004acd97105ffso2028918edb.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=cVavBbpwdmk7NI0oUbySNHJgAancJodMsXbMKmtM88y7LBpcqWX19Ax5kii336T3Mk m4L5gyuleH7g4KLu3UFUVoaRfRolqveLDFT9PpmZCM9X/FBBqqlBObTpKCJdj2BM7k1M 2tib5JdfsTmUhICMFH+PyaUx8CNR8qiZf49xT3l9WT2X2ArbK0K+nx/yN2bYootS0TaM KaCSvmu9C5/DvpjGafF07WMl4NhWFyQqCGnTFeAJdtnao+du05S/lW4IBrhaDIXE8x/5 Cg3VoOk/x5JgnW8LAcrV78gM3YPWPc6bEBfnU27209ur1BtIpfBESFndJbQ7+Om79+AR 3vxw== X-Gm-Message-State: AO0yUKVMdF0zZ9Y71lKNIf/6ruqx1W0kyOVNYjSD2ukOIKzrnixEAWmZ JpxZPeRvevdA/o6iBZO9s4BtU8kDzQIF1clirNGhTzLOcxTan6ZqgQDI/4ePh3/8hPY08s+W+Bu vuP9nWymOHpk= X-Received: by 2002:a17:906:2c4c:b0:8b1:16b3:303b with SMTP id f12-20020a1709062c4c00b008b116b3303bmr8902651ejh.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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 977D710000D X-Stat-Signature: xipkrf4srcum1neukmxenkcc6y6t19h1 X-Rspam-User: X-HE-Tag: 1676641707-557448 X-HE-Meta: U2FsdGVkX1+m6/oEcsAOP/eQjxSVMWW/Hj5LW9yf6Z1Ba1pjMTOwfSZ5NFwSyZ6nHtefGMXVML9lSPIvRN+2zmEWD9uhiJ66Y8XaRcBVAILHIpFuaZFJKYG/ev57JGAbOmPskrbjhu33fEEhb7+IZMT3W2HHvnOzc1bMxTwvl6q1W5emRXkMcPSZnOVNfbWcx9Ken1qQXKhWV4XXdWVsVqtagk1/iowwKZEVHMqf6+aNpcd9XLU8zCFrqCLxHm22kPak927TxUUewokztYm3lhU07L2hbpxZWZv+Dibiwt6W1JiVWiNnWQ0OdrdS9GmjmUbE0JlyFOhJbOt1bJswOu/JYc3xc/EeWQyyP09D/DQptN0NqV5Q56VG8qa2KnQr48pm3oJhzXunEykqWnYHvxhqZOaUoyP6fa6DsigBVtDNzxIuhJr3km+wxlW2LMT4q+yTAnZ+jtl2gI0aBimuo7OLmWsFfv58FQ5LSkAF+QFFt72jxbzxaJp/uB7xr7tErsC9hBbxd3K4N1M87v4XxKfXVNgc7x6Gs5py4uDAolIXav/RZsVSAEMOWu/ws0SvHrbI5Vn+4jyGn1tw+nJFDd8yER96ttTvXdLZOJueuixJEuuIF0sTHWotu3VTPBUwZLR/tvaS+35gsaXXTvNAAILcYNYXjH4HABOLupbPMEd3i8i3v9fg/6xP0rwY2FXuzxaOhTQQAna94IEJr3N/Z4HU9LLElstuWzSXxycDe6Lf5lQjMhbafhvBTbnjPrL+Ka4peT5h4WPuA/ZzXNF/aAu4oADzrvQPlDaq60VgP4DC40crLYl2B8Gnr+XjiPqni8EyXUwS3M+duH4OrM7AcNH8yQfL9RK1Il7Z5MF41BR/vv9ZifCqjbAL60LgtHYAzUnDIT+f8DOz2K+y+sVI3RGPyWaNjQr7b7UEhaO5GMgFpaJRpePPkJMzf6XlxsVkvrROwdaZHR9m6y3u2pN jVJO10zm GDzTWk6FmlfI0Onp63Ee2K7BjxnuX2Gyatn5b0oq/WLBbe3plXQ4uk/Zl+EFhQhzEpO78FcOSDOflO6nCEF7RzddhOEN1dQ1jcYxtOwoTI3fhDmXxFE+xMREE5wpxOtlfO+znYHGY+mLLrBnhf/5WkYfRtliK18oB+SvhmJfH+uqqFMVxzLBELB0kd5O1MO5aqxKayZqKCW7vvn6YNsCqkGRrHqfnGqBq49bTggmGQx3cLa9mhx0956UPMGx5QksVJWdJl7ezmbvoJS2qDMeV8xM1rO1vRYIypxUhrvQFA2czXmv6cp1uNHEeu/1+xNs0z1FXWWuMTZMAdgI5q+fGyzUMFYUCLdH4KM87GIu84xuCUospDafLklowsbcYsG0JNRYImeJsDC+qrnK+HubROxgxGRILVdiNNqSjkw7x3YasvEk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 #include "drm_crtc_internal.h" #include "drm_internal.h" @@ -175,6 +176,61 @@ static const struct file_operations drm_debugfs_fops = { .release = single_release, }; +/** + * 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 | end | 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 = __it.reg; + + if (reg == &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 = 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); /** * 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 @@ #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 provide 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_GPUVA, 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, 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 *files, int count, struct dentry *root, @@ -155,6 +174,12 @@ static inline void drm_debugfs_add_files(struct drm_device *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 #endif /* _DRM_DEBUGFS_H_ */ From patchwork Fri Feb 17 13:48:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144727 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E1F7C6379F for ; Fri, 17 Feb 2023 13:48:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E29666B0073; Fri, 17 Feb 2023 08:48:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DD90D6B0074; Fri, 17 Feb 2023 08:48:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C53016B0078; Fri, 17 Feb 2023 08:48:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B75416B0073 for ; Fri, 17 Feb 2023 08:48:33 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7A883AAB63 for ; Fri, 17 Feb 2023 13:48:33 +0000 (UTC) X-FDA: 80476913706.14.6898287 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 732651C0002 for ; Fri, 17 Feb 2023 13:48:31 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GUrqjOgf; spf=pass (imf21.hostedemail.com: domain of dakr@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641711; h=from:from:sender: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:dkim-signature; bh=eEmQDOQyU6gF5gMBW9RNpV/N/n3J9qaRJsJk6Xv6fHM=; b=anTS2SBbAvYFdSvEdvQPZ/g4IiBXum2GUcRr9v9crdddNNhHrrB73e5sEvi4Metdsv1YYZ BVXhi8eYhBOE+z/JYumL0ZPVDMNUAuZ7JDGDwgh3YmTdLMpY+SoXSgloHzMFpyVOQyTRKX q+XmrPezvi+JDqxPbC2pcenceAJi4fE= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GUrqjOgf; spf=pass (imf21.hostedemail.com: domain of dakr@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641711; a=rsa-sha256; cv=none; b=2No1byfvjrzjOV44CarYJry6FX/y0CgcRmybqkS0Ms1U1NLsrMNJr1uQ1+JbT9yE1lz2eV ENnkgAmQZlybSpEI4G9293CawkTD/VWIysnRbMRzQSo1TVOXDnLghu1ijppZcEANz0+gVz 6XKjAKkphnNX1E747EmghnpC7DRKe50= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eEmQDOQyU6gF5gMBW9RNpV/N/n3J9qaRJsJk6Xv6fHM=; b=GUrqjOgfwAgu3k3oX4nMZP/eFV/MK3U3EBvDmHLD2sRDl7hN12kDtQ3Eh+LZymrQRnjQE+ Bm13oyToTimNx7K5fSPfiAc47x2Tu0GwtLe1uY88GwIHWUZn4B2rxRTgdi/r07FgfcKvfL m4jNIDfKhzf3ddBDzf57zBk/G1GZ5i8= 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-L8tRbwwMMKyguvDw07nE2w-1; Fri, 17 Feb 2023 08:48:29 -0500 X-MC-Unique: L8tRbwwMMKyguvDw07nE2w-1 Received: by mail-ed1-f70.google.com with SMTP id v3-20020a056402174300b004aab6614de9so1103660edx.6 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=eO8KWA5LNU4jRqEMPguWmRlKS84Gop2CBs9RY9w5cHb5oyxi5tQWc0Q91jI6bQcrsd uc/hcIDODMxYv4nlb5AzNGbnNhNV8yQCN/zBUnX8PjeLZ6mKmewImnD6SlsKl+z18CvW G60P/kF4jak4kszLi7FriCLcM7vaqnmjRSRvUtXGhaH1gFbDaWSZ+sa4zYdHKm/ENN5M gu7Jx5htx7j95WDykzM7/hXHqIiZlOanoK+DczBW6i7LrQbvsQlYTBkZzB710vCd4Ru4 rTPrUHs1lIHmG2ksf3ZoT4BK2y0kT0lEhQgYSP66q0tXmPzj7f7Uj/6UhSpg81RY1tPu tEVg== X-Gm-Message-State: AO0yUKWIZYEFWn6vhH9zCL/6No9GxHnhtc3DDdLoRZPMSKJXZv1bVZwm OM7rLAAVGDMyE8DMnYbcvT/EySpXZcxswvk5vBGgCU3F3k9IzZOfVo5WuSrUXbtgVgrXKGhGpTo MEHjRjCECvpg= X-Received: by 2002:a17:906:5e45:b0:861:7a02:1046 with SMTP id b5-20020a1709065e4500b008617a021046mr9000410eju.37.1676641708466; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 732651C0002 X-Rspam-User: X-Stat-Signature: eirrtg6owmk383i8ur6hh1uad6g83fet X-HE-Tag: 1676641711-839585 X-HE-Meta: U2FsdGVkX1+7wGu3/KoWjQrrulZVS9qeeC75Hw8TUh7rtUHMrQGBxhUzODccjcOrGKnAm0isucGE38Id1I/DEvLez3LtLWJal2kGYPUa9rIDXdZdEyhhJCnKkur5GXurAurnzsRKtTLr3rJgbwoyLXPlL4Lm0cKkUi57+nR0/1Zl3e8LWK+AbkVO7hwQwo2lBtJznpWj5+5/xBIjdJ2Jri1qye62aJYnYDkcSR4TTeOPqYeDVnJs5f417rTz8vLBfhFo0TemrFWuN/fofj8cv88lYp+lGG5Pcrp4h5U8Ax1nL14an0ZT8d70SbN4cPxNhjR1bv4VbsYSjvPAO8SGWyw4Gf+qQ1HnSVo42K4AZjw8kVzaTkAVhtT4E1sqQNAhD3wRuRx6KjHB6CPyWVkanxtypxtc1rBBlbUZewT95tcRM5Y99jLZuEg7V5asr2smLlbN+ne7Cco96w0bsab7oj9igtymPZmk0qyoxuYQLCm/13e4uaf2BgKAiSnEz/XgeFQ+EUTqg9nZzly1MoFNoqXo4CAEsnlkExSuQ1xXwo/7ws3AGLp8NZBYsK2QNrCwrpQidtG3jse2yMZCXHP+ZvT3vzxpKLBqgxq0NrunBP9oytET1lmRMMenr248uJSWMugr0X1h+sGLHCbYQu8oKHyIwHuQeSU1eteVM2k2Sm/KINPrUEvMAL364mqzJ+oZjM89e7ZlbYs99uEb4i8hzaY2h+FwibMm00TaiEov4E1jQX3Ph3cNke5oLEO+rZKh/BF73ufufOHPjTojQawv0SCzujZoljN9nlIiShGGXBo8z+/vikQgMJVu5o40kfMvL0ATeYpGycfgg2ERjVCmrVuOaRrEO3+01ol5uNAftd6if/jMNcZEUJ/yulYTUtwoTUTxeQRUBrw0ckdKn9nsLniBsGzqSpYSldBG71dvZ+xav7nD4WffQTpkhS9jHt1RGCSnhefF1gebg914ufi 2tzEstFf dm7vIBxUZBpbuNABQLrIivSQqe6qh1ppVTI+7YOxNvbxCw06CnZIzcPhkfQEgofaoInVNXlpf3EWdmghoCCoTHxAF2Ey/WUhAM9Zf1OxuBA6uAwarASeYQHuC7su0N2mIyd+UQbZTK/nOCdGdxXf5EnWcEITGJiSt6bABzpXVFXqq7cjIZd0GV7TEQc4qDW+8YfjwC6a58RxTdZ8yUQmPlm1PnJTVTNFMi+ZKyUJ82Lel3a06boSvdLy55wS1IpW0u6AiUrp0cRdcRCR48MbXV1SCkD9zmsqWQmem/eMJ6YEBy3c6w9Xh2XnfGVpRxdtbHHOe2u6wNyCNrMuY+RXbWGxHD5RFQ2JUnF4bO1awx1KsZvJ2SUTFvznSVUsDHqrWQxAgRNjcLuoA7sqoQ5e9cDzP06wjM+IZBFtv8wuU4Yf2USyoUvUyELyfDw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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-uapi.rst index 4411e6919a3d..9c7ca6e33a68 100644 --- a/Documentation/gpu/driver-uapi.rst +++ b/Documentation/gpu/driver-uapi.rst @@ -6,3 +6,11 @@ drm/i915 uAPI ============= .. kernel-doc:: include/uapi/drm/i915_drm.h + +drm/nouveau uAPI +================ + +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; }; +/** + * 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 kernel + * which portion of the VA space is managed by the UMD and kernel respectively. + */ +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 field. + */ +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 enclosed by + * a previously allocated VA space region. If the region is sparse, existing + * 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 mapping + * 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 mapped 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 asynchronously + * by the kernel. + * + * If this flag is not supplied the kernel executes the associated operations + * 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 pass 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 + DRM_NOUVEAU_GEM_CPU_FINI, struct drm_nouveau_gem_cpu_fini) #define DRM_IOCTL_NOUVEAU_GEM_INFO DRM_IOWR(DRM_COMMAND_BASE + DRM_NOUVEAU_GEM_INFO, struct drm_nouveau_gem_info) +#define DRM_IOCTL_NOUVEAU_VM_INIT DRM_IOWR(DRM_COMMAND_BASE + DRM_NOUVEAU_VM_INIT, struct drm_nouveau_vm_init) +#define DRM_IOCTL_NOUVEAU_VM_BIND DRM_IOWR(DRM_COMMAND_BASE + DRM_NOUVEAU_VM_BIND, struct drm_nouveau_vm_bind) +#define DRM_IOCTL_NOUVEAU_EXEC DRM_IOWR(DRM_COMMAND_BASE + DRM_NOUVEAU_EXEC, struct drm_nouveau_exec) #if defined(__cplusplus) } #endif From patchwork Fri Feb 17 13:48:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144728 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 026A0C6379F for ; Fri, 17 Feb 2023 13:48:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9966F6B0074; Fri, 17 Feb 2023 08:48:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 96C726B0078; Fri, 17 Feb 2023 08:48:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80E626B007B; Fri, 17 Feb 2023 08:48:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 73B716B0074 for ; Fri, 17 Feb 2023 08:48:37 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 50BE4C0623 for ; Fri, 17 Feb 2023 13:48:37 +0000 (UTC) X-FDA: 80476913874.07.82759AE Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 423CC1C000A for ; Fri, 17 Feb 2023 13:48:35 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cxWsr1Tn; spf=pass (imf21.hostedemail.com: domain of dakr@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641715; h=from:from:sender: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:dkim-signature; bh=cfAnd4OS8K7FlDcC4KqE1N+eZSmvaMlTDnF99DGXOtY=; b=TNKZaRLngNamPsIixcG47y7aPPxvWJ97XJCrzy6HzXBPdUcPcd4a7CaOqT1qSBNOoonFw0 q3Xos4Aom1YmiMZQejg9WIKk6seAH3AszOUU0zrl/xhrqOUnaKRpD6PqZzoGoiM9KxBxhL ZbxIsnSTOc7ExPovHZadAdUvj8kQyFg= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cxWsr1Tn; spf=pass (imf21.hostedemail.com: domain of dakr@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641715; a=rsa-sha256; cv=none; b=dXy6WRLJPJ9RDPJfDpD3+CspS3cib1JwH55/e/W7rGcSOSqGi/kbt5P8fbv1QEnBSFemGG fHLejrw4IK/jDnCdM+dWriFX4Hvb5KigM8JHtcHc61ndHqnp+WC1QJUyNqB8ZDGKs+Ul7r aWmVSE++OIoEMa9+7oCQHXnr4hOtVOA= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cfAnd4OS8K7FlDcC4KqE1N+eZSmvaMlTDnF99DGXOtY=; b=cxWsr1TnZBkLIsbVYJGAD6K5VFKD4mmTNLm2LdLOnhgqFQca01vblbbzBuEOsWUTWiUzgi s/in2PpmwmC0IboPCzAKk7QjhbUZyFY182fj/VuqJzcEGeLdXjQojfLIxIYbbaqbQIWynA 2sCNDIvkMvgVlkpi5926UEtUnIblir0= 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-673-vk-XnnP4NcaMMCzfnZoc6g-1; Fri, 17 Feb 2023 08:48:33 -0500 X-MC-Unique: vk-XnnP4NcaMMCzfnZoc6g-1 Received: by mail-ed1-f71.google.com with SMTP id m28-20020a50999c000000b004a245f58006so2161929edb.12 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=0guNwznD/MmX44GHN4xv/0tmKgVt0aWepVd3/31eVQxPbshxDxP60vdTIRL7JOeXNx WFeQAKMphKpA2XK73op5+5B+D7QHfmMm3Fk6RKzdEPMkLue4DwzqAmwtSxPrYnhrWI0g ZwFtPZXzgzCWMeX194xhe0p0zTusNlMgdQUykL5n+XdbKzFtxUL+cnk0HkppjXmxLz5G gX081Gpx+yW3bOoDE8mUsFp7Br9sbhy9LZA8bmBjkTo9K+D6UHpFLCsYUIUQqNVoMn+1 OwSYSWZfVKUjFKbk60SapCL5OX908DmQuowFT4hVSjwx3j2qvbfhpW/oLzyifvFVr7Fq gd5A== X-Gm-Message-State: AO0yUKWWIH8lt5bRE622fwjbozN/CynZmbq4FxlheXwjw8hBiX71XevN GUxx3fh4lu12ErgAGZYvJixFaYGcB3/t9ArsIaOGZdiMCeJ5wVCggLrBbJkoF5dgZ8gXM2J4IQO cRjY67IkGyFM= X-Received: by 2002:a17:907:212d:b0:882:2027:c8e2 with SMTP id qo13-20020a170907212d00b008822027c8e2mr103444ejb.56.1676641712434; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: qcta6yemhwi9srgw3hzkb3ctub9m95a3 X-Rspamd-Queue-Id: 423CC1C000A X-HE-Tag: 1676641715-275236 X-HE-Meta: U2FsdGVkX1/QYErbdMujw+aOuzH+1L70fTNHS69jMs5RrSyaCIOSFeB80exIzePt3qLLOrhd7JVOP0Y2tx88rN15mOMwcsfsyc4+baNTgbXqCS4l+rNLd20f0DCzB/beGnhJhc2zSd5bHflSuBBsr/G3ikxfdwtqUwMrrHzjZX/ZS6Gp0XZsvKIOKp7lK1+1t74zYh7Uf3mGIH6XtoYqBZjCw+B5fE0Ezexy3PhxxGS2UIfqMqTP3yeT1qS/qpEPal/Vpcd0L5aQp8iC1r5X/LwW8rr5/XcJ/OaBQ44Een1TzvO7MJdzvCOjUAmD+tWDKkuFJ3pEl3R6uDAHa5NTPA1M+cnIm2jXpxMr8aAs+65Cn4mZ3U4SXuEXosBwwCLWcFvdZGdUVFRTfTSC2s/GkuCQT+vkjchyXdNmP/o9RSOSDlBnpEycx+CUpPuyQL6z201UME0+NFg2cbnjWIE/+I59Rsj47Pd11XbkWAUK7K7QwPg+I58y6RRvw9ICzybsVVJ9FMJcrSY41eyFtNC+Eder/yID1jN4tg06N4N/BbUBlEClfiTCtVmFJPPoUVPubWKf3+rICK4//yeLlwh9CazPNy/VXyEVxQp53HUrzuF4243fSpFEmyDrQyn/aTAYglzGbjXsDFiKydEVHk8MVB8EMPIKSoAxz9h/VMGhI8IBdd4fvskZ+qF05WhyFVzZeUv+hcbutaJ2gPujepQ766hVkafOBSHvVQMileI7yXkCdJjh2vN6gui7/7NE1MJL945pxBzVvX7z9p75CloaYkKYH/U6r1aslmbj4B8WR1+CKEW//4fyosfm1nRhaIgPLCvHzgL+v4i3ltTNWLqnAGv35QUJUwIrY1rHlN8bpLDVkbfLI6AWKMd/hBSNdMSE0IkahO0mYf1u5BwffI4jkE+8YsptueHtM7/KRM4afpx0FDinFuDXZTmU+JlJlNdckIWTXINoafFc72oCWeW uwNRbK+u YCkLlgbWufzQAmFqcyu4YbG5CyKZ+GjZGLRZw9ECapVplos87pc2KFK/ehdyBKF13npXBoNXbp2BlLBttpBv1QAdrJQ//tBoUbxq2RopCY+OYN3hfCLGDFqA4NO+pdMeuQflHk0+5gvXzWURlORZRkgf7KuYTDctdG5zsM+K8O/KizGfCgehMxOwAJNfaoGl25xwOeaBfDUEMclTqS7r76jfU+47FwxEsYHo2NAZj/OiCB41ro0n/rq6brq29tQXzqvU/fkJ4IKAsVIAqU/+QUffm8tn8hfH6VHB/30y6IaVcocEOErZws+1+yE/3d+3ecMVdW4Ymc+HsEMjT7qDa9SFM+iFJROBB1X0IXiFnf93RiB1EjQPbyrLlCg6zX52MUjiemdCEGzy4JYISTvrfRDv3B+ngJHve5zYnBGd7Aw09L22WZ33u3HfJWw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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, int *align, u32 domain, struct nouveau_drm *drm = cli->drm; struct nouveau_bo *nvbo; struct nvif_mmu *mmu = &cli->mmu; - struct nvif_vmm *vmm = cli->svm.cli ? &cli->svm.vmm : &cli->vmm.vmm; + struct nvif_vmm *vmm = &nouveau_cli_vmm(cli)->vmm; int i, pi = -1; if (!*size) { diff --git a/drivers/gpu/drm/nouveau/nouveau_chan.c b/drivers/gpu/drm/nouveau/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 nvif_device *device, chan->device = device; chan->drm = drm; - chan->vmm = cli->svm.cli ? &cli->svm : &cli->vmm; + chan->vmm = nouveau_cli_vmm(cli); atomic_set(&chan->killed, 0); /* allocate memory for dma push buffer */ diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/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; }; +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 *); diff --git a/drivers/gpu/drm/nouveau/nouveau_gem.c b/drivers/gpu/drm/nouveau/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, struct drm_file *file_priv) struct nouveau_bo *nvbo = nouveau_gem_object(gem); struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); struct device *dev = drm->dev->dev; - struct nouveau_vmm *vmm = cli->svm.cli ? &cli->svm : &cli->vmm; + struct nouveau_vmm *vmm = nouveau_cli_vmm(cli); struct nouveau_vma *vma; int ret; @@ -180,7 +180,7 @@ nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv) struct nouveau_bo *nvbo = nouveau_gem_object(gem); struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); struct device *dev = drm->dev->dev; - struct nouveau_vmm *vmm = cli->svm.cli ? &cli->svm : & cli->vmm; + struct nouveau_vmm *vmm = nouveau_cli_vmm(cli); struct nouveau_vma *vma; int ret; @@ -269,7 +269,7 @@ nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem, { struct nouveau_cli *cli = nouveau_cli(file_priv); struct nouveau_bo *nvbo = nouveau_gem_object(gem); - struct nouveau_vmm *vmm = cli->svm.cli ? &cli->svm : &cli->vmm; + struct nouveau_vmm *vmm = nouveau_cli_vmm(cli); struct nouveau_vma *vma; if (is_power_of_2(nvbo->valid_domains)) From patchwork Fri Feb 17 13:48:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144729 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25BD2C64EC4 for ; Fri, 17 Feb 2023 13:48:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C06526B0078; Fri, 17 Feb 2023 08:48:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B8F306B007B; Fri, 17 Feb 2023 08:48:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A57666B007D; Fri, 17 Feb 2023 08:48:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 96F5A6B0078 for ; Fri, 17 Feb 2023 08:48:42 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7367D8010A for ; Fri, 17 Feb 2023 13:48:42 +0000 (UTC) X-FDA: 80476914084.21.64FA3B5 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf05.hostedemail.com (Postfix) with ESMTP id 1B5D2100006 for ; Fri, 17 Feb 2023 13:48:39 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XSbaPwo1; spf=pass (imf05.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641720; h=from:from:sender: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:dkim-signature; bh=s63r6ewLpHy8dX6pfqzPL7Kskgx+EPYod2Ty9Ts4erk=; b=gPT+hFGUdcvJUqzawX5gjsn6UXV3UhlWWpm7325YQzmsO+AkPKcvsaF5Rp4H3DrrdO7OU4 jkdLS5dViLufbjidvL7EotYj8eyN0GNh9XZa/zO8ZkkgZoY6VxzbvKuuLy2mFx+mAi2/r8 tweiqQiA0fVPtldCD62TJ0xBKpX+q3Q= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XSbaPwo1; spf=pass (imf05.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641720; a=rsa-sha256; cv=none; b=zR/+M2UiMaTEO0ZQ60fZeW7O2ABJ6rAmK13YFt1bPfEBYUhE+1oGMspO4A1SOwWBS4Y09q jQsEqlpegBG5vIQmw85tPUxjKc46vpVOkZVTilxj5jvlMR8uA/QYOmE7ZrIGiMvRLBn8DW kXLr+IQjsa2zRMyDETXo65JHM47f/fg= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s63r6ewLpHy8dX6pfqzPL7Kskgx+EPYod2Ty9Ts4erk=; b=XSbaPwo12LAB4EfolymFY1np9bhNKEikg7WY5CkRhhFruIcugO1F1cH+Z3XWTrs48q1PHa gHofN2dX0duKcqxbS9ABwOPBRBDv/ZNAzLJp8ClVy6kG+93QD42I513bKT++GbzGNV9peZ Mgxw4BVQJjyYlYmubuuHv2PUgrbqovo= 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-582-J-IRXXBfMU6e02rMGACXZw-1; Fri, 17 Feb 2023 08:48:38 -0500 X-MC-Unique: J-IRXXBfMU6e02rMGACXZw-1 Received: by mail-ed1-f72.google.com with SMTP id z20-20020a05640235d400b004a26cc7f6cbso1756214edc.4 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=J0LXCV6Yme3HN/j39OtAJuxYqcWNW4ClcCfDC7gcWlxUEV3MKxM+62/nqYF5T+kEUG qyakQyo0AsvFbHWKX3TG3C9i7SXQpAvNjt77wIzJWtiFXJ8jfAPBiRhDG2jr3MCOq1Yo rloqLXEOMae0XHKEof3vLBp2XMbOGyTKYDUHNO8RQH2K77E8D9AUHokWAYmEu7D7a4ZI 3/2CTvhCJbAVAbGAPpDn3yZ2+iyX7FRXuG8t+gI0AhCdCQ+kBpB9KFv7slstZKJLacrx eHVfWpJpEMZjGb7cqFb01gAh2/UheFydbbEK9ArHwwQ/Raz4n26tNgv7z3zoOJW07Dyk NjZA== X-Gm-Message-State: AO0yUKX3+Ax33qrq0GvfeL4ckn1WW8tBXmcMnjdFfNLatH3yZiALE8gv FAJD6V7n159NzXGOYjjxlx8+kegHjIYs+Vwvs/7Tuzl8Jb75LCaLKVqEdyf9D7kxLPSQfltCvx0 ZZVUmqTDLMUo= X-Received: by 2002:a17:906:9750:b0:8b3:946d:51c8 with SMTP id o16-20020a170906975000b008b3946d51c8mr1973104ejy.29.1676641717144; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1B5D2100006 X-Stat-Signature: gbugk78jogbh1qioqmg1fcu6xnyfgrw1 X-HE-Tag: 1676641719-577326 X-HE-Meta: U2FsdGVkX184PVgbtuaQK5Ozl3ONZcspcYt9+nA9JcbLGIPSux6i6KZ3MozmI4mhvX8uGD/eNulUOkrCShM/Y8d9yXse3QLw/Ts3X+ttuREY+k+I65kgquOJNBb5zzNsB6KgmKrc6dUfepp5xAIYEHBRND5cONuTkU/XRhpzSgTtUT67PTCItL/KQqK9AmeC4GjMoLqYJsL74Wa5xtJnVuDzulr54oEPuLn9uVB4OgH4MOLzkvt1gBtNKSmKpbmroVmaESeULAdg5FE9TVV+kH30WvXPVfakZdEnaiInads0aIhSOlgB0merZCABvm4qtK/NFqZhO1cLMqJ4ABtda6+qpugtP92T77bVAQO/2p3VNV5WhuAruHf0yYUuOQyjn9XswTnw6yDWKEf3HHQfhgmw1/7qqxh5rhxxfzZiUJSP6qbaD7nZoXDzDhtpHiBrtfHAxDpo9ICIrVKwYSPjTATVEZ5NwxAEsCBVRJNjJ7wz39QKjhSd2ujpWHAfCWNLFQT5aiQcvVjqq5c9paXQrSsccfie/bkJ9DNY3St0IqXETC+qDMzxZ/w3VzWUlIwfmAk25hClApBO38fZEaIhez18CeKZ/enJqqvYz97bMvbq2S/A4oasXmbbT7e5k5DBA/KVt1Iy5uDINcCndbEUE1YS+1waU7xoFFoTg53HA82PMMrJRT0of5633OAcDAKHiGQLvost/jqPTYHJnam9x4i7kTVI7DAzrUPSQpIo6Yy1Q3QVCS+kT9TsSQNVjmk6oJMp1Hup7mc+d4WAOFq7bNuB3u0l3thviVw6uNIC8qOwXBeXdAcLimBq9muEnXbAeHiq3mknljIL2DBjjuzCm7JOTHTBu+VDLKp9+9qcEWNyRH6EdMn9Kad1O3srb/9Jgl8YruVO6uMJGt9NzKFVNChxhRwuAnPPZZhXI/IXj0YAbdMtOzhRR9WnkHPkhgrYfxq0ao9wsw7B736u4Wm HT7sJx+H L7rYXBvTtD8qVL8iobhrEUgx36wa3g4kMkHjYsXPrfqEsKlaj0MAAnlkc0j4cu0ok5yLnVwH7NsIcJ5xZAd79hmgwOmjulLlgjogIxQl4epBCMZq/xHSdPYhEPpyHVhDbsew++DWMOyRzpHLKoOWjoZTqLczMqSnnuvtIcGz8r0PA2uhnU51VR622+JNvT2VOhXjGffFJOAg2bg9bFJl7b1HZVVce+kodoSLcPBaALwe2ukCfyzHrRiKARotsQm1ePXfV/0AZ+CqlItNpEyuW0Gbl5ficxVbi1KPBGu3JqpAKufiSPKvDhfjs/fdKYXifJ37mrUQn/+mDPQ3cnyJH0Eb+QvrNna1sMxflqk3C+nrqSC2pTrmWkzc4+9NNRoZev/mIp/Ki/pocHSI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 = 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 = &drm->ttm.bdev; + 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(). From patchwork Fri Feb 17 13:48:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144730 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71B80C05027 for ; Fri, 17 Feb 2023 13:48:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1305E6B0072; Fri, 17 Feb 2023 08:48:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E1596B0073; Fri, 17 Feb 2023 08:48:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEACB6B007B; Fri, 17 Feb 2023 08:48:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E168D6B0072 for ; Fri, 17 Feb 2023 08:48:48 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id BE68B801A6 for ; Fri, 17 Feb 2023 13:48:48 +0000 (UTC) X-FDA: 80476914336.25.4AAE372 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id A2C891A000B for ; Fri, 17 Feb 2023 13:48:46 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=C1U29M52; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641726; h=from:from:sender: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:dkim-signature; bh=qV+blR6Tkr/sKsOr7dEvAgWdCXkrbGqs9gehh/nqNIE=; b=MnoHFHnDQhVLVXeIV9/zunRS5rADHhSbKHosJAIY/grqGCNlBm4Q2E38r/m0DEQBiweA+D rxeODGx/lpmTRmuye7FdFMNbC0FJMVK1Ayu4XbPxbrlkfqUK6ldbrcRKx87ujMiRUqnBxl YwZ1D4JfH0az024u2maBkIf+tNiamDI= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=C1U29M52; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641726; a=rsa-sha256; cv=none; b=6LV5jbiRF6q6T7i5zZ4Oa8kcS/9K1JJm+7f9PUSA+nVlopQC9HJGOpXHLrhZuu3M+5gTub ZGpijR8MRJjnbifqvz1kFVi0x2ZXFxr/1tIJWJOn7QkahgZ2vlNwvdrBiYixezGW8AglwI F62hs5bD5Oo86m5egGN5Ms0eimPWRlQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641726; 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=qV+blR6Tkr/sKsOr7dEvAgWdCXkrbGqs9gehh/nqNIE=; b=C1U29M522pGTUT0/O5aAJewBetfkLPq1Tp6k0JX4h0HS4vQeUu3QXAsg1DTnD83NhtJdab CXcsCHgsjAhMkpdNKwpTPD/otKIe33p6j52Fu3nSw5ZJ5i4xWbF2TITi6PUzeVQwcVBLu2 erd9sbu5GysalHoIdOC5ZsM83iCVujY= 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-628-fQ8TU7nWNeSSgyGDm_ZJeA-1; Fri, 17 Feb 2023 08:48:43 -0500 X-MC-Unique: fQ8TU7nWNeSSgyGDm_ZJeA-1 Received: by mail-ed1-f69.google.com with SMTP id z17-20020a05640240d100b004acc6cf6322so2013598edb.18 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=lnZCMHSjAkbEAxQUQqKz3Dw2sZkAqLB/AoxM0V5rArpOHWk4zAUnqFctSO2S1E+weY Pt3mkbZgV9TzuYEjbbqsCM3bT9wNTd0EkUGnU9NuWZBnaRDOMXcYNEqAoUFZX9UomRmx 3dQgPpcdZH/Qal0vE1SeHEwNjkULc+jcyedIZujLe+XVKkgjqo3vyHfq6ZlRHBuX6htN O7RwuqMc5cKAM165KL/p5DA8o7I/6PpIM/VihglHi4jT+0y1NJopvlAYzVgzArkiM++3 dlP53MeaUDaRH2oN3k0JpVL35n9KVvLWCR0msb+9Hk/D8Zsf0OVPjNcuf1RZRMgw6poG zWcg== X-Gm-Message-State: AO0yUKX41VMO1tjj3vG2GEBg67BT/6jhY3cS7gya2gL1Ka6/B9rKyX98 3YwgLN93bGTipw84OzX41AreQB3lgnwik1b4zHzOTHBL8C2pKoBAQ4W4znPHnrqkNi2p85iRa46 V0i8b+7kjM5s= X-Received: by 2002:a05:6402:457:b0:4ad:7640:ff47 with SMTP id p23-20020a056402045700b004ad7640ff47mr3317210edw.13.1676641721927; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: A2C891A000B X-Stat-Signature: pwij1eg5reuza7orykw5gf6at64j4cne X-HE-Tag: 1676641726-547547 X-HE-Meta: U2FsdGVkX1+Gvj9+3c/q3oc5hGrprUV5Jx0rt8B8umK3Z40Ysp8bJ3gtZ4Fo/+x8n0zgbSNOt051D1lzGpj4lf1G/Vn3iL4/auRQSAyMBY1uv0OoJjrXNeywI39UUmOUivZ+B4ZbN4NFMYChRwIVrF93K37PMoaHhHkt4GSWI5YnuOWDudtns8yoM02KFLuAwmY9xqkaqXu1YubbityUYi+xGMJVD+5DBI9dvv+EgkTCrWXjY2hz8wx+t5fluN+ND3gTy4deOYg7kSYzoanhCzh3M3q/ybEBhqZlNPm3GJ7opTodC0tqwG4yb1zuYGkRJXy2n6ma4pJHuhnBF7QseG72cw++9buMlMSkRdZuKh2e+tsWXZOw6KtGoEflUPreHak2aE7dGIlbiNjNILMrcpYVM2B7pGLFknNshrlq+7pORkqS7jq4BgyGCOYBDS698QTzN2EI6LZ+Qm1EpD7c7rJ6R4Y3KTbPJOq1Iy+ID3Ep9kwY0WaeUFK43/7IvXniAJYDIWWXnrsIF30JocH7SNKOWkXAIPxXwuObIqGq+m4Ly5+bx3wDAImx8tpOfJ2l3wBmeU5dRIHHp9FNG1pv73sHw9YiY1fvN0MgbLv492CN8At28vNPTlakRo7x1Na2SBEuSwUnFU167lAcQL/vK4vosFYUGA34yyvlo9tBzFcgCWNk46tFnVJNdD/J0qs3Slk396761T3Tr+QIArF5RLGApUz1QdHKVMmmnD9Tlf+6rfbu/D8S8oQPGeoPvDGCTHamRqe7S8t1HOMcjXPqxBw7DfUXWs4tjn6Dh9Ng3NLqnHCyPy3Z9QeJdNr/ecuI14CGhRQltvUank+abTTXqE59Exrd9d8c6MWEmibE4oi20VP7ROLSki+tOCsPFaHxMTGIrEgkQcSYndzm0fusZuJTVSbg++Gv82jCTipEWRF1V2j81hWU449oMO1bhYNZJdFTd/prR50CjO1ffLW jgvwR+io /YuyB8QiHyBjKbABw8rKk0hgY9J8GbhhyciLmzU+VdIrFuyEuELCghBIP1c2rphb9vOWdz/K8yUgekgVaGx5d15d9Y0qgTS2+XghKMvep+t2Jkssvr0SaPpDvg+fvech0m2jH32yBQqatKRGX4ruowXKoSmQaPWK/u++FkXPGzUuH2TZYfnuLGK8vrODlDi5W3h2pTu41Tc5EMNNsJVCCRerOyGdIosP71tnvimAsFpmURC83HtgI8JIfDOOIBNDmIK8HQ4d5GHow55o5/KtKI5ruV92CArJhHm2XO2xwN4itqFTNJLUQfs4hi5Si9zXScMH6PK+kMKx1ieghNN9jAezRdMttoFg+vl2gpDf2EsGHgFLzdpmQ6JDIu7frTJhuvfEVOFsSkS2ekPgLxAoYM6AwyUzAO1nBhrwvfFG0xm5hpU8fQgDg83pf3Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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/nouveau/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; } +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 = (void __force __user *)(uintptr_t)user; + + size *= nmemb; + + mem = 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 diff --git a/drivers/gpu/drm/nouveau/nouveau_gem.c b/drivers/gpu/drm/nouveau/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 *chan, return 0; } -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 = (void __force __user *)(uintptr_t)user; - - size *= nmemb; - - mem = 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, From patchwork Fri Feb 17 13:48:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144731 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40746C05027 for ; Fri, 17 Feb 2023 13:48:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8C946B0073; Fri, 17 Feb 2023 08:48:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D3EDD6B007B; Fri, 17 Feb 2023 08:48:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C04D36B007D; Fri, 17 Feb 2023 08:48:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B23326B0073 for ; Fri, 17 Feb 2023 08:48:51 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 86706160283 for ; Fri, 17 Feb 2023 13:48:51 +0000 (UTC) X-FDA: 80476914462.27.3E6B53F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf29.hostedemail.com (Postfix) with ESMTP id 5B27A120013 for ; Fri, 17 Feb 2023 13:48:49 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="X/n01YSN"; spf=pass (imf29.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641729; h=from:from:sender: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:dkim-signature; bh=d5/2RnRF2JWHGpr9RK4XcaUSwl3Atw4selg5mVgZ4/g=; b=fBJrHrfWvmMZU9WGaya0YKyXwGOFMDl8tqShxWnRKND+0YlNyOoTFsPSP6FohHnLDHfeYk 8s/5S7bmwstX+bTFpbdud2n0hl9qCreu0WlkqeSvLIsBaxeYX4eSykdLN6d20FTlO8GJXc j3uNqnUzGPgOCsFG6L69oQ7p/apnsPg= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="X/n01YSN"; spf=pass (imf29.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641729; a=rsa-sha256; cv=none; b=4QHYEAqe3od615YLZOGGmGB1AKSEJOt71jcCRlSD3axnOQupezs0jJm/S5P9clT6jTOOBt j/APr0j3sY467Kf2X4OkwR6qdQZUbSnYSzsdlvcyp5CA9WfYvq4kqjfzBB0VuCL6NCSJkc /DCmfhbVzd+cCQZJpocNKAbELKtYVLQ= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d5/2RnRF2JWHGpr9RK4XcaUSwl3Atw4selg5mVgZ4/g=; b=X/n01YSN1gpk+KK903jEsu4aTusHdMHCMKZku9kCbirFEX4XXlUq/tCUMe+2w/N0alUNbx rlmoxasiGW0XiRg3iteIbnlEuBUlKpAhisQrxs2MbWQeYDaahLzK5LJKy4ecL0dnZ2WMuc 16iMaleWtdWNqmMfx67UI8K6Ie/XRaY= 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-414-u1XCysXYPtGRANcwJ384eA-1; Fri, 17 Feb 2023 08:48:47 -0500 X-MC-Unique: u1XCysXYPtGRANcwJ384eA-1 Received: by mail-ed1-f71.google.com with SMTP id ee29-20020a056402291d00b0049ec3a108beso1653522edb.7 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=DBZ97ZKsMp9wpgKA/JTfpRnXklZ3Ezu82ZV/n2MPt4owpwxvtwVQv/ufIstzK8TERD fB89x7BtQP7NHRUaMJvx3M16qVcEpvqN7K6NHqvoOl06ohH2XXqT+9PjLp0eTdFjkoii 8Is6IUSQ55sMxkl9X0lVM04cd3UwsXSqFkpE8e2vKav9pDV/puZ0y0W/KZlbxHp1yDZ2 QNjyEHWS2llk9wWA/vE6aX2ZrAzISWGfjo6GlA0hmVx/ocJvnstpIMtMKEfaBzHhitDc P9fnO/APu3UIqfly1DFr03hwshHaHsR18xps77r/kF3cOvo+4Mwb6aHzYS1n8v2q++50 GEyw== X-Gm-Message-State: AO0yUKUFMW7lbE6oh5WFcoeJNXt42pmVL4edGOLT6VkIRtceGeu1uqdW OnFB7VRoVRfCnI8r6SRkBjqE9wGa0P+o23Xl/qEhD/7spr66wSVaHNMlr7SpoIyUU5cWPAkZvVb bX3Lh8umVK6Y= X-Received: by 2002:a17:907:72c1:b0:8b1:7569:ad58 with SMTP id du1-20020a17090772c100b008b17569ad58mr4848778ejc.2.1676641726460; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 5B27A120013 X-Stat-Signature: g6un5eyfy9oticmcr4w69mdnfuu87heu X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676641729-559936 X-HE-Meta: U2FsdGVkX1+37qQZMUHFyK1ATO8+MzIiQZ4xSKND3EO5TLCxWOFHvVCuby6kuTbhLCCVbAbPoms9z6oslHA3ju2M5C3F96CyTowsrJ2aWM+sUJ6asDxWLdypXl0plUXq7B5gfuX9Kg+1+KwVwolW1+sOJEINLcJ4b/lZTkS6MqX0/H2R8NEoH5KjlcVvfO93Yx71E7AjiiuvBvPIId6wDjt2fPWc0lA/Lrs49GL895Pu3yPEbwManbyhuwxZY9VQu6i3aQTrlHfmZY0DgGaZkfkXZ4M7R6fovP0XMhdJt8Z1NL6nXTWoDauPEX4gMzkE4ZGuS1WXzHnKSz+aArg4ihxOxxLpIlgi29nU12YDJX4QZHNY0mLKXq+cDLVO5qX/5z/9IrTdADlOWXQ7UEXV/J5th3w5ICCzg1krSDfCUrsK9ZnKNo+AkXcekKoDtesi+93wygc1EfEf/MHrAjgzShBIZd6uqhNKAP2qUpv2ne0MD2S1O6beokJgspveYtiJpnKxbU9sLc8r+Cos2gNcTBI9wCIutAFdv+cQRg8RfgGpFRQzD6jDf4xB/bUVRKQ9oYX+LW2Zco4YP+EPJ4FvlwCjHPVD8g5qMpLHlfXNOsEYV+ZSSxnPNGj49ScoY7n8eSvnbHFilNsvDuww2dilhKZwseziJ6ekm4jR6SYlUmwjr3hD1EED7kpSiCGNnVI144N/GENteVI44VN/FHtJLMjBAT7v78m1t6o5kXXK0IPkUCFmYCwcLMFFuRvpq31mW/RbC1neES1+chDceGqNDL2+duRTFmMRC6a+P/3GNCN/A1yxagZO3ewQRGpiSEGMh1CqbQeF4Fz4Kh2tpA8flfF+gzBbXU0N96rsOXo1l5KqtPx6+85eFmYc7ECleT0QPLgyPKRdnOGBUH4IuKN0Wsoj+KL1Ua/wtDzgv7qaPNCmrEXQu5GM6dpsKDXEzwcEGAOdIu62zdK4/5001hw 5mUQaE7A WnljMKucdQjp736uKBEOQhoyvh5+vSJnGI90CBzfXPauogNsIGAB9dE9a7mLfJ6vEJi/fw6eX4hL4fqvvxNdqD8EfRb/wfrDp7MJ/xoAVdDZ5esOKbCgqqYTyMdxi5KOY6Ph23HD/e/wnIsUQuE2eZ/t+bznFKGc1XylqY1jv+JSto0I/8aVrGxsT6zNnwLJWDDr9JjrYt5BDJnbfok4NeXjQhi1kMVnkjwqcnokL7suiEeS+v2MN7LPUcq+Su+UvZUAAFrTePk7NMpVNE4uulh/bSjHc52Zi5yE/czLQ4689BFsNmOsaJd1uekz/tFsf7QBOoObGBs2zd6nXeyrproIXRzMaZGb8d+z0CyBKyc+tvU1osWlmZz4Zjrs3IkRjH2SD70Lmp0yzvC4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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/nouveau/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 *fctx, int error) if (nouveau_fence_signal(fence)) nvif_event_block(&fctx->event); } + fctx->killed = 1; spin_unlock_irqrestore(&fctx->lock, flags); } @@ -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); + 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); diff --git a/drivers/gpu/drm/nouveau/nouveau_fence.h b/drivers/gpu/drm/nouveau/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]; struct nvif_event event; - int notify_ref, dead; + int notify_ref, dead, killed; }; struct nouveau_fence_priv { From patchwork Fri Feb 17 13:48:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144733 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 76B5CC05027 for ; Fri, 17 Feb 2023 13:49:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85D8F6B0080; Fri, 17 Feb 2023 08:49:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E5816B0081; Fri, 17 Feb 2023 08:49:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 610DD6B0082; Fri, 17 Feb 2023 08:49:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 538DB6B0080 for ; Fri, 17 Feb 2023 08:49:05 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 30B28140669 for ; Fri, 17 Feb 2023 13:49:05 +0000 (UTC) X-FDA: 80476915050.18.3911DB7 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 229EA20018 for ; Fri, 17 Feb 2023 13:49:02 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gk90RsR6; spf=pass (imf13.hostedemail.com: domain of dakr@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641743; h=from:from:sender: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:dkim-signature; bh=nuqfhAPXoP8eMSRnoJXE/EUnRUmH5Dpmadrq5ZDJBI4=; b=I4Hjqb3VwcHsPba3NsQSHdXhU1oQFEUIduUTxRFQcePVfx5pOiHibwwHSDAkdbU9YcVJhz y0yq9jzmDPwK9GT7ubanAMMsvxFYMHm5wOr6VrmCG+eMbFv4Qg3Mk4DlvzqHtbcovRUjm1 xUrYvPDgvJ2HTwB7JYXE51Zo5Fqb6bM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gk90RsR6; spf=pass (imf13.hostedemail.com: domain of dakr@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641743; a=rsa-sha256; cv=none; b=EvxTrWZQ6yw6sFLImUS9BraWYxyPUdUmDc209caVxk/bsrMIrVaawCwMUCMKiOaZpcxWeb YtOctl163YlaE0H4415L4qiYCyDAJbsZNKT0jSQkAjS9e8hb7tnP59QBHTDCu99fJPtyeU QLG1QMvk+Bcp7rvV7UXgyYiZ4t0bjmc= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nuqfhAPXoP8eMSRnoJXE/EUnRUmH5Dpmadrq5ZDJBI4=; b=gk90RsR6DTRHwGsLXyS/JgVjbC/BC+xugRterCc180pAijNjsrxYAinPlvIeh6tr0z/NvW VITumhAI5sE/Gv5SGpwjogaa0IhVarRi/UftR6f/quV0P2gDyOyUg895oS+UB+QQ3uTieo spNpHAY7N+uvoq+Kr3Yjv3xnma5r9DI= 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-249-qcrm-5GTOBqCdwl-bPPgnQ-1; Fri, 17 Feb 2023 08:48:51 -0500 X-MC-Unique: qcrm-5GTOBqCdwl-bPPgnQ-1 Received: by mail-ed1-f70.google.com with SMTP id ec16-20020a0564020d5000b004ad51f8fc36so1496229edb.22 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=fx007XPTPViEYgIshY0nfcnANcXlg65hEJPFGTlyEaEnCcT0Pr8tEezn6O865I5/cG GQ3O6zfFxLTCAZPt+vJGdMlK/6UvQOkGuv/r6p7iA49fOGFYkoJW5EsbPDmdKhzcfUek hibnyJy3WqLEUVRXQoaHwTQ0iLIBea4PyuoM9IdL0/kfegJcWYWqZzjF37PhsIOlU90T VciOmJW63ojSQYs4duesaNbmCRzh/ISigHDPs6lVhl24FparvMp9fr+wkESXLZxC8tLT 5ZBCp36rogYQV3BTANd64OfddcTPCyEcX2YkQbNhEBQsJM3JHB6OGqHdtK2rMunGjEOB VjAw== X-Gm-Message-State: AO0yUKWcQAku7Bg9Z6n5QWNYuHwFe+MtenLdQI/X1d23v4tnepXSGN7H IEt0QWokjfh/orTNowngE1laRrM1WMGYTPlXtBEx2QpwxIotKXelG2kGmENSpQaEiYX36Y3Lnml Zvu6SvX2ElxQ= X-Received: by 2002:a17:906:5012:b0:87d:9447:f7fb with SMTP id s18-20020a170906501200b0087d9447f7fbmr444194ejj.38.1676641730479; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 229EA20018 X-Stat-Signature: 6x97z6ihk44gcuq1bsuu7cznfetkb9jh X-Rspam-User: X-HE-Tag: 1676641742-246152 X-HE-Meta: U2FsdGVkX1+8o3IfB8xTFK8IL0enbIWdnCi5Ints1t/CXv+PhTVAovG6B0kv/6HOcGyLusCtmo9JI5S1R/GzyujwjJcq/of9hzo9a06eFq9zlu5lM4ux6stEYZwW+XI7yiNvyR6AYz0ZIHci0I2U6XwxIqWUgHtMFtOUsgGfWdV8DS8vv+2lm4aKOYJyMY7q18zJ/y96a1+R9rUZAAp5kE/HBkB7fphY9dMaSk1PG4ttPGQOWJvLeOpTfTF7tfcsWV98LpGMHPbfRTdcPxLj+068Cq348GnJ72Vzd3Qr2/Bw7brEdMCV11HeGWlgvk2tYQVCsiWU9NqQvR24wgdqq2KIRQ02pxFpXoaRMzoGEj/B7oIrxj8H9OAJtHkLk6wZxD9XTJZXmClT4YCBpiVWFMevGaLbIEuwmRuIy/9JzkhOFF4OGNIWgDsGpVi2pcmMqI5I5o4OtdPyaUNuiDmcMY88KsRoOQPDtvVgoFpY3vB1Ep+jP+JHmycbrrErZte+K44ewzL2tmncpIqcwqBtlLgX9hgO/x6plspUnAWh5MUON2BQVQjH+vt/qBBSf8MzuMFJzfm43IUTXGuTBRnwC6uhAG8Z2ns1G0R07Al8Ucxbvq8iSpgGZ0XsWeyuZH9I2h+f+mQljxf0zGApxfM3qR0EEBo6LLKGKJujDfrISRg5wBLaNPKwGW4amZ3b9Rmyqi9zRt4QsQgUpyxJv1V8zOH4dKBrg3hg73pjy+AOriVzklchbbgpkHPfGBL1c7heXu2hqEvvrCYIKelGquSrLlQ3M4q8koz880dEip0JV2dLEF5/JzL2tpO0KYKM/K6f33dx25Vg5p1XxIm3fx+fDMx5z+RQMWgoiEX/fVPAGmRn8tt1O4399lSDhy2mijCqHtFT5JYaYmnF7caEVRV4dFHJo0A4nJCw0NHAzhM6OhmU6FIvOg2ptH1KBhBNki4W3Uqt1RjspnT/alDWcCL scNrYrlD gLMYGNqThn0xEPBN4WG/nnYTWzWVy1qupygnNslkwvy2K+ULWrxg92eoxoNChD6bx0Lacr2idyA6eYZ59WnE64UiBznKb28L+OpzpDgQmcpH1/d85yM2A0Z9sTd5oqYooEOWFUOmLymvujsv8S+lKAq1C8V8oRaSUqOfpbnErUxFwnOgGjQWgBFad9Zzc4TqwhOzNYaJS3tV1AbQ3ENyg19JIt8MQf3vFrXuLNoOUlJyD92oxK9qn0y1O0Ps73IJqclV+r6lGcajDO7/p5F+ztc3r3awA0clx30EIG/tml3/UFbtRGAp/FbLXRrHIlZMJSFoVSxgf+wQ09Bj26tZNvx1HSW5zdVAnplSHxuZl2ofPjmVQoO1pfpnNy9oyw+grrK+HisPR8mHtHLC4Dnt8VnNqja2qh3ifSNAOXJuRQHgJIexSpsfyhIbuVA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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/nouveau/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); +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 *repv, u32 repc) struct nouveau_cli *cli = (void *)chan->user.client; 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); return NVIF_EVENT_DROP; } diff --git a/drivers/gpu/drm/nouveau/nouveau_chan.h b/drivers/gpu/drm/nouveau/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 nvif_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 *); extern int nouveau_vram_pushbuf; From patchwork Fri Feb 17 13:48:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144732 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FD68C05027 for ; Fri, 17 Feb 2023 13:49:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C06576B007D; Fri, 17 Feb 2023 08:49:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BDCF7280003; Fri, 17 Feb 2023 08:49:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A083D280002; Fri, 17 Feb 2023 08:49:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 8FC926B007D for ; Fri, 17 Feb 2023 08:49:00 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 57EFD1C5EFB for ; Fri, 17 Feb 2023 13:49:00 +0000 (UTC) X-FDA: 80476914840.04.B631728 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 26D821C001E for ; Fri, 17 Feb 2023 13:48:57 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=e3oxQl+e; spf=pass (imf18.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641738; h=from:from:sender: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:dkim-signature; bh=+jwORAYkDykD+zRwq4/W8kGZAUEb7WEZ6froGnNnKQo=; b=jrDSzXjdofCS9UBf1Zqp6GHNaH1Jc+z/W7GLGfiakQPz5b9y4/gKeE4JESrtUxzD59xZi9 i5OnYwmC7raXIECl9I0E3R3t+Z7brqNpD72n2g0ZR6RFBCFJqo9i+PDbV3vRRQ2PBe2Z61 RTK6bYTo1/8EX86BJmmEHx7hphcoo1w= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=e3oxQl+e; spf=pass (imf18.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641738; a=rsa-sha256; cv=none; b=8VWH8flXDpaNi5/FeJg45ZQ30/aAU5cW4M9Rg5e7vhaiyvn254Jm7MztlOVg9Pfv9oy4dX uFEvS+s05gn/kNtCGF7+Ag6iLnBgtoAxTE/q8IyhPb7JqDOPSMu2+G7oDEtAKuFLSi5p4D 20F9YrufqbUntNezAopV2bMvGH/bhuQ= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+jwORAYkDykD+zRwq4/W8kGZAUEb7WEZ6froGnNnKQo=; b=e3oxQl+eVfbcBojGNdIo4NiehNReIpQfrpUH8ZY7e8rYvo0ptznjnwNvql+Jz0v4/JGjh7 ijNt1ChfyxKvZ4ImTcaovSWbGRYaLEp7CDXqsbTchWIyNrFb6I6tI7fjxi6yoY7x375gLD 7p0B2vW7wVI2cpggn1LYlAM3ySVYa3Y= 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-658-ihtdOspzMYqakO-kjtFgmg-1; Fri, 17 Feb 2023 08:48:56 -0500 X-MC-Unique: ihtdOspzMYqakO-kjtFgmg-1 Received: by mail-ed1-f71.google.com with SMTP id j10-20020a05640211ca00b004acbf564d75so1096024edw.5 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=ndWUiVSljBE7HvkG+31+tPtmfborlmA22Ho6jMeCaNw6mbjMAhBbIWg4M16SeTknYN MShgeOW3iIZlsEl/Uw85Ust4j2cNU+Iq4TeiXW/iytlv4NaivHuY0fZBRn42i/SZKJUn HalJRle5FyAIaHDoDNNuii2MYesxm8n9e8DNXmAwuIGJxKLdZb7NtB8OILs5KnWoqfFe 8vLXqUnLOspb8LWHFVI8S3is9R61/Kq8KjvwBtTQBj2VfDyAq7dRVvZ7j5nwm1hSnkWA +lf3p9pk+ScXV9AM7eGvTjscOQxB8vHn9ZXCgodmNFzMBPf2qZ2IoqWvxRSmIkFBnhFA PwAQ== X-Gm-Message-State: AO0yUKX6dHh5wzlot86bn6u4W9N7Pip2/DIXWpL0GO/tzqYejZ2us7bR UCPbY4WUponXpvZ8qngdWKBEuAkPbjUpFoqsWFgxMLn9IkIU6Hatv6HUUIEuzvhCd+LJ3+h4j/A eyubjxWE8uH0= X-Received: by 2002:a17:907:a07:b0:8b1:3483:b825 with SMTP id bb7-20020a1709070a0700b008b13483b825mr781102ejc.51.1676641734875; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 26D821C001E X-Stat-Signature: dww5zejn4ofq4isjtu4zoqzcd1o8a7pq X-HE-Tag: 1676641737-246320 X-HE-Meta: U2FsdGVkX19JNeJ2JAS05NdSW6cFx7BD8641u9pg0PJYDRLLm3o6FMZhw8u3WDa46nX7/nKChCBjXq0quXwdo9lZ/YqWlA8xDHj9v3KiD5pGRM9fXNd7TZmgTgsjy9DQ92HA1VIZc5L0lwsLwboek27n/kXppXQ6W0adSSBgCT/EKd7OmUMtrlZR09KmSClirwZUmI+ANusOBqLocbDbBjvI3w1XCuH2e/N5Fg1UCNbN4V2S8eMn+iO8oLVFRDngTHrhwaVeXrqB2bfL/FJJnAv3SrrwgZscVhHqu0Zu5YHNue6wev3s7y1IWMQPgvYC99CdK7Cx2yDzQK729JltiST0bchGRcjwOtpXbiPwzqQ/s4viHvQF+sn4k2oQRL20Eoiq1iBZ8WLHR8lHW1l58Gh9vUaeHJvwL1SzQaPCKBgbO9lFPjAp0oEoVEY+p1VQaONWGm5BccenJGshgIfpDBfIOaYdOgNGYzT0BNT2Mt1+/WS9g/g+UynzNlHun9rzcKf8hDpRchsZxfG1oS6aYakDTcuvRgxQgUlKMnbstn8Noun+xIy896Q/h5aXdrfSRJx6rLdmq4699Azie8e+3DuYvCj3yiyj86Ld4/lZA9ndVFmN+poeJIJenzkT+T8Kbgn1dVexAt/fGYoIWy8M7svkjBqsJgLawvIyK2ChHvX5ZsbMxegpehTrySsc4Lbq1XVx0qwTyhM0X09GK0wqiweDANUsgh8tG+nnCY7oRvkHbO65vpm0mCeJnKD0rZUrY8LwZKqUf+cZyKby0PkdsUi4EJNLM6v2vfJGCix0Eq+0EtojqsiE8K4NVXqF1BbjSB7GeGPO/x9VL3qDJaVbH01UDsvYe3D5jSu7F6nUvFMHWW8YW3OOOTHoHJJTGOILcarwb5MiaMmw5jqE0+v00fXBe2ImPELTcKwGKPPWQcDCfccMu6tbA5OW/JORwGcm89md7uMOR2IJf90rfMz GSY9jlVZ NLz56829FAyhSReVNVsVo1ZddN6AxACfR0EHK+tnt/H3+1GiI3dyuSTSf4C6mxVvodlmk++2KJUxHjTgjf+3xaxgU1gdc+F6pQ4SOg8YDS40R6+ebkAtXS77WlgFr3eHY7I4ws/6OKV9iX/gQdoIixHb75qwdZ8Kk4xbcZ+o9D9FeJGFkGUUwKd4shwqpsDvHvqZ6u/W1ADdVr8OS4TZ1FMpBRPCYsdTr+m1sNSd3zYRCoucrhFEwtpKK1BrobytU9op83h/9UH3sQnlS7eqPjahCbAU+nCog3VfENpRUaUoskSz7cCqRjGzmNjlPRO6xPIP1Fz1jhKtEUAQCCJSOYF/1B2Dql6sF37eoiLtjVLnaNCRSzsqZHkkLAkXNkOtrdOtxrwn3JNphVKU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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/drm/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) struct nvif_vmm_page_v0 { @@ -66,6 +70,26 @@ struct nvif_vmm_unmap_v0 { __u64 addr; }; +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/nouveau/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; +enum nvif_vmm_type { + UNMANAGED, + MANAGED, + RAW, +}; + enum nvif_vmm_get { ADDR, PTES, @@ -30,8 +36,9 @@ struct nvif_vmm { int page_nr; }; -int nvif_vmm_ctor(struct nvif_mmu *, const char *name, s32 oclass, bool managed, - 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 argc, 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/gpu/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; u64 start; u64 limit; + struct { + struct { + u64 addr; + u64 size; + } p; + struct { + u64 addr; + u64 size; + } n; + bool raw; + } managed; struct nvkm_vmm_pt *pd; struct list_head join; @@ -70,6 +87,7 @@ struct nvkm_vmm_map { const struct nvkm_vmm_page *page; + 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/nouveau/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 = 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 = true, diff --git a/drivers/gpu/drm/nouveau/nouveau_vmm.c b/drivers/gpu/drm/nouveau/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 = nvif_vmm_ctor(&cli->mmu, "drmVmm", oclass, false, PAGE_SIZE, - 0, NULL, 0, &vmm->vmm); + int ret = nvif_vmm_ctor(&cli->mmu, "drmVmm", oclass, UNMANAGED, + PAGE_SIZE, 0, NULL, 0, &vmm->vmm); if (ret) return ret; diff --git a/drivers/gpu/drm/nouveau/nvif/vmm.c b/drivers/gpu/drm/nouveau/nvif/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 type, bool sparse, return ret; } +int +nvif_vmm_raw_get(struct nvif_vmm *vmm, u64 addr, u64 size, + u8 shift) +{ + struct nvif_vmm_raw_v0 args = { + .version = 0, + .op = NVIF_VMM_RAW_V0_GET, + .addr = addr, + .size = size, + .shift = 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 = { + .version = 0, + .op = NVIF_VMM_RAW_V0_PUT, + .addr = addr, + .size = size, + .shift = 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 = { + .version = 0, + .op = NVIF_VMM_RAW_V0_MAP, + .addr = addr, + .size = size, + .shift = shift, + .memory = nvif_handle(&mem->object), + .offset = offset, + .argv = (u64)(uintptr_t)argv, + .argc = 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 = { + .version = 0, + .op = NVIF_VMM_RAW_V0_UNMAP, + .addr = addr, + .size = size, + .shift = shift, + .sparse = 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 = { + .version = 0, + .op = NVIF_VMM_RAW_V0_SPARSE, + .addr = addr, + .size = size, + .ref = 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) } int -nvif_vmm_ctor(struct nvif_mmu *mmu, const char *name, s32 oclass, bool managed, - 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 = sizeof(*args) + argc; @@ -125,9 +210,18 @@ nvif_vmm_ctor(struct nvif_mmu *mmu, const char *name, s32 oclass, bool managed, if (!(args = kmalloc(argn, GFP_KERNEL))) return -ENOMEM; args->version = 0; - args->managed = managed; args->addr = addr; args->size = size; + + switch (type) { + case UNMANAGED: args->type = NVIF_VMM_V0_TYPE_UNMANAGED; break; + case MANAGED: args->type = NVIF_VMM_V0_TYPE_MANAGED; break; + case RAW: args->type = NVIF_VMM_V0_TYPE_RAW; break; + default: + WARN_ON(1); + return -EINVAL; + } + memcpy(args->data, argv, argc); ret = nvif_object_ctor(&mmu->object, name ? name : "nvifVmm", 0, diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/uvmm.c b/drivers/gpu/drm/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 *argv, u32 argc) } else return ret; + 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 = nvkm_vmm_pfn_unmap(vmm, addr, size); - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); } return ret; @@ -88,10 +91,13 @@ nvkm_uvmm_mthd_pfnmap(struct nvkm_uvmm *uvmm, void *argv, u32 argc) } else return ret; + 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 = nvkm_vmm_pfn_map(vmm, page, addr, size, phys); - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); } return ret; @@ -113,7 +119,10 @@ nvkm_uvmm_mthd_unmap(struct nvkm_uvmm *uvmm, void *argv, u32 argc) } else return ret; - mutex_lock(&vmm->mutex); + if (nvkm_vmm_in_managed_range(vmm, addr, 0) && vmm->managed.raw) + return -EINVAL; + + mutex_lock(&vmm->mutex.vmm); vma = nvkm_vmm_node_search(vmm, addr); if (ret = -ENOENT, !vma || vma->addr != 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 = 0; done: - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); return ret; } @@ -159,13 +168,16 @@ nvkm_uvmm_mthd_map(struct nvkm_uvmm *uvmm, void *argv, u32 argc) } else return ret; + if (nvkm_vmm_in_managed_range(vmm, addr, size) && vmm->managed.raw) + return -EINVAL; + memory = nvkm_umem_search(client, handle); if (IS_ERR(memory)) { VMM_DEBUG(vmm, "memory %016llx %ld\n", handle, PTR_ERR(memory)); return PTR_ERR(memory); } - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); if (ret = -ENOENT, !(vma = 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 = true; - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); ret = nvkm_memory_map(memory, offset, vmm, vma, argv, argc); if (ret == 0) { @@ -207,11 +219,11 @@ nvkm_uvmm_mthd_map(struct nvkm_uvmm *uvmm, void *argv, u32 argc) return 0; } - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); vma->busy = 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; - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); vma = nvkm_vmm_node_search(vmm, args->v0.addr); if (ret = -ENOENT, !vma || vma->addr != 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 = 0; done: - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); return ret; } @@ -275,10 +287,10 @@ nvkm_uvmm_mthd_get(struct nvkm_uvmm *uvmm, void *argv, u32 argc) } else return ret; - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); ret = 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; @@ -314,6 +326,167 @@ nvkm_uvmm_mthd_page(struct nvkm_uvmm *uvmm, void *argv, u32 argc) return 0; } +static inline int +nvkm_uvmm_page_index(struct nvkm_uvmm *uvmm, u64 size, u8 shift, u8 *refd) +{ + struct nvkm_vmm *vmm = uvmm->vmm; + const struct nvkm_vmm_page *page; + + if (likely(shift)) { + for (page = vmm->func->page; page->shift; page++) { + if (shift == 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 = page - vmm->func->page; + + return 0; +} + +static int +nvkm_uvmm_mthd_raw_get(struct nvkm_uvmm *uvmm, struct nvif_vmm_raw_v0 *args) +{ + struct nvkm_vmm *vmm = uvmm->vmm; + u8 refd; + int ret; + + if (!nvkm_vmm_in_managed_range(vmm, args->addr, args->size)) + return -EINVAL; + + ret = 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 *args) +{ + struct nvkm_vmm *vmm = uvmm->vmm; + u8 refd; + int ret; + + if (!nvkm_vmm_in_managed_range(vmm, args->addr, args->size)) + return -EINVAL; + + ret = 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 *args) +{ + struct nvkm_client *client = uvmm->object.client; + struct nvkm_vmm *vmm = uvmm->vmm; + struct nvkm_vma vma = { + .addr = args->addr, + .size = args->size, + .used = true, + .mapref = false, + .no_comp = true, + }; + struct nvkm_memory *memory; + u64 handle = args->memory; + u8 refd; + int ret; + + if (!nvkm_vmm_in_managed_range(vmm, args->addr, args->size)) + return -EINVAL; + + ret = nvkm_uvmm_page_index(uvmm, args->size, args->shift, &refd); + if (ret) + return ret; + + vma.page = vma.refd = refd; + + memory = 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 = 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 *args) +{ + struct nvkm_vmm *vmm = uvmm->vmm; + u8 refd; + int ret; + + if (!nvkm_vmm_in_managed_range(vmm, args->addr, args->size)) + return -EINVAL; + + ret = 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 = 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 = argv; + int ret = -ENOSYS; + + if (!uvmm->vmm->managed.raw) + return -EINVAL; + + if ((ret = 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, void *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 = -ENOSYS; u64 addr, size; - bool managed; + bool managed, raw; if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, more))) { - managed = args->v0.managed != 0; + managed = args->v0.type == NVIF_VMM_V0_TYPE_MANAGED; + raw = args->v0.type == NVIF_VMM_V0_TYPE_RAW; addr = args->v0.addr; size = args->v0.size; } else @@ -377,12 +552,13 @@ nvkm_uvmm_new(const struct nvkm_oclass *oclass, void *argv, u32 argc, if (!(uvmm = kzalloc(sizeof(*uvmm), GFP_KERNEL))) return -ENOMEM; + nvkm_object_ctor(&nvkm_uvmm, oclass, &uvmm->object); *pobject = &uvmm->object; if (!mmu->vmm) { - ret = mmu->func->vmm.ctor(mmu, managed, addr, size, argv, argc, - NULL, "user", &uvmm->vmm); + ret = mmu->func->vmm.ctor(mmu, managed || raw, addr, size, + argv, argc, NULL, "user", &uvmm->vmm); if (ret) return ret; @@ -393,6 +569,7 @@ nvkm_uvmm_new(const struct nvkm_oclass *oclass, void *argv, u32 argc, uvmm->vmm = nvkm_vmm_ref(mmu->vmm); } + uvmm->vmm->managed.raw = raw; page = uvmm->vmm->func->page; args->v0.page_nr = 0; diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmm.c b/drivers/gpu/drm/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; } -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 = 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 *page, - u64 addr, u64 size, struct nvkm_vmm_map *map, - nvkm_vmm_pte_func func) -{ - u64 fail = nvkm_vmm_iter(vmm, page, addr, size, "ref + map", true, - false, nvkm_vmm_ref_ptes, func, map, NULL); - if (fail != ~0ULL) { - if ((size = 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 = 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); } 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); } 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); } +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 = 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 = nvkm_vmm_iter(vmm, page, addr, size, "ref", true, false, + nvkm_vmm_ref_ptes, NULL, NULL, NULL); if (fail != ~0ULL) { if (fail != 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 = 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 = nvkm_vmm_iter(vmm, page, addr, size, "ref + map", true, + false, nvkm_vmm_ref_ptes, func, map, NULL); + if (fail != ~0ULL) { + if ((size = fail - addr)) + nvkm_vmm_ptes_unmap_put(vmm, page, addr, size, false, false); return -ENOMEM; } return 0; } -static inline struct nvkm_vma * +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) +{ + int ret; + + if (vmm->managed.raw) { + ret = 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 = kzalloc(sizeof(*vma), GFP_KERNEL); @@ -1045,7 +1097,9 @@ nvkm_vmm_ctor(const struct nvkm_vmm_func *func, struct nvkm_mmu *mmu, vmm->debug = mmu->subdev.debug; kref_init(&vmm->kref); - __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); /* 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, struct nvkm_mmu *mmu, if (addr && (ret = nvkm_vmm_ctor_managed(vmm, 0, addr))) return ret; + vmm->managed.p.addr = 0; + vmm->managed.p.size = addr; + /* NVKM-managed area. */ if (size) { if (!(vma = nvkm_vma_new(addr, size))) @@ -1114,6 +1171,9 @@ nvkm_vmm_ctor(const struct nvkm_vmm_func *func, struct nvkm_mmu *mmu, size = vmm->limit - addr; if (size && (ret = nvkm_vmm_ctor_managed(vmm, addr, size))) return ret; + + vmm->managed.n.addr = addr; + vmm->managed.n.size = 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); } } @@ -1423,6 +1483,8 @@ nvkm_vmm_map_locked(struct nvkm_vmm *vmm, struct nvkm_vma *vma, nvkm_vmm_pte_func func; int ret; + map->no_comp = 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 = nvkm_vmm_map_locked(vmm, vma, argv, argc, map); vma->busy = false; - mutex_unlock(&vmm->mutex); + mutex_unlock(&vmm->mutex.vmm); return ret; } @@ -1620,9 +1687,9 @@ nvkm_vmm_put(struct nvkm_vmm *vmm, struct nvkm_vma **pvma) { struct nvkm_vma *vma = *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 = NULL; } } @@ -1769,9 +1836,49 @@ int nvkm_vmm_get(struct nvkm_vmm *vmm, u8 page, u64 size, struct nvkm_vma **pvma) { int ret; - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); ret = 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 = &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 = 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 = 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 = nvkm_vmm_ptes_sparse(vmm, addr, size, ref); + mutex_unlock(&vmm->mutex.ref); + return ret; } @@ -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); } } @@ -1790,9 +1897,9 @@ nvkm_vmm_join(struct nvkm_vmm *vmm, struct nvkm_memory *inst) { int ret = 0; if (vmm->func->join) { - mutex_lock(&vmm->mutex); + mutex_lock(&vmm->mutex.vmm); ret = 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/drm/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 nvkm_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 *); +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 = vmm->managed.p.addr; + u64 p_end = p_start + vmm->managed.p.size; + u64 n_start = vmm->managed.n.addr; + u64 n_end = n_start + vmm->managed.n.size; + u64 end = start + size; + + if (start >= p_start && end <= p_end) + return true; + + if (start >= n_start && end <= n_end) + return true; + + return false; +} + #define NVKM_VMM_PFN_ADDR 0xfffffffffffff000ULL #define NVKM_VMM_PFN_ADDR_SHIFT 12 #define NVKM_VMM_PFN_APER 0x00000000000000f0ULL diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmgf100.c b/drivers/gpu/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; } - ret = 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 = 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->tags->mn) { + if (!map->no_comp && map->tags->mn) { u64 tags = map->tags->mn->offset + (map->offset >> 17); if (page->shift == 17 || !gm20x) { map->type |= tags << 44; diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmgp100.c b/drivers/gpu/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; } - ret = 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 = 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->tags->mn) { + if (!map->no_comp && map->tags->mn) { tags = map->tags->mn->offset + (map->offset >> 16); map->ctag |= ((1ULL << page->shift) >> 16) << 36; map->type |= tags << 36; diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/vmmnv50.c b/drivers/gpu/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; } - ret = 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 = nvkm_memory_tags_get(memory, device, tags, NULL, + &map->tags); + if (ret) { + VMM_DEBUG(vmm, "comp %d", ret); + return ret; + } - if (map->tags->mn) { - u32 tags = map->tags->mn->offset + (map->offset >> 16); - map->ctag |= (u64)comp << 49; - map->type |= (u64)comp << 47; - map->type |= (u64)tags << 49; - map->next |= map->ctag; + if (map->tags->mn) { + u32 tags = map->tags->mn->offset + + (map->offset >> 16); + map->ctag |= (u64)comp << 49; + map->type |= (u64)comp << 47; + map->type |= (u64)tags << 49; + map->next |= map->ctag; + } } } From patchwork Fri Feb 17 13:48:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144734 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CD41C6379F for ; Fri, 17 Feb 2023 13:49:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 241BD280002; Fri, 17 Feb 2023 08:49:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F2326B0081; Fri, 17 Feb 2023 08:49:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEACB280002; Fri, 17 Feb 2023 08:49:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D9AB26B0080 for ; Fri, 17 Feb 2023 08:49:04 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B5B66C0136 for ; Fri, 17 Feb 2023 13:49:04 +0000 (UTC) X-FDA: 80476915008.21.66C2F53 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf27.hostedemail.com (Postfix) with ESMTP id 964DD40007 for ; Fri, 17 Feb 2023 13:49:02 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=V9u3z9pD; spf=pass (imf27.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641742; h=from:from:sender: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:dkim-signature; bh=iIWonkC5C/glRECsErna8rexTQ7GLEMuQBk+usFaDoA=; b=NpT/ekx1TEzzFYLwWdYANRpZSiAmpIU/61z1PWFpla3cOOPGg8dFx9/jVdPJ7D4Cvq22El 7DdVxzZWtpwOZuUafBYRBore+oicXKnxvLt/n9AWKMwoL2js6TcWa+oeIo2cUEl9HRF+FW B1k4a2WGsK8l/a8w738yLMwjRFUEFP0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=V9u3z9pD; spf=pass (imf27.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641742; a=rsa-sha256; cv=none; b=PIQ70Oxca4bbI9GEcNOLXm8ETxJbu7ZRNp+8nuQAHyMY50lYpRboRVvfslLnL7AAhn2je0 48XgJKYLuRtU6A2gcuEdhj1eBHx0rpC5Z8Qqzo4S0HtG5kRKa5rHsL7K/t1K0TtpjC9R4C iMy2nlz2OXPCTc/g0xIfvvLSWlQSYDQ= 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-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iIWonkC5C/glRECsErna8rexTQ7GLEMuQBk+usFaDoA=; b=V9u3z9pD0ohAFYDVORfZt7/QCEijr/AEf5yuTzysqmGPydjGuvyPTrp0fOJ05xT7Z9OxDj XnBWVUXu349rHOaESaJKyJ0NEkRbArMwNycn8lyEF60xN754wRWSrc0XcBLpHReGA1Z5eE m0ADzlOkRfkEousxx9A+7doR/9VhCgs= 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-250-SwKajX3NPpyJdYogLga7Eg-1; Fri, 17 Feb 2023 08:49:00 -0500 X-MC-Unique: SwKajX3NPpyJdYogLga7Eg-1 Received: by mail-ed1-f69.google.com with SMTP id x14-20020a05640226ce00b004acc4f8aa3fso1828008edd.3 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=M4AJmtJvKikZJd0SU1xbHFxbAop1ju11gJeVWElMzGE01SeHTmvJHGRlp6TrjOzYpw THHiNC8vk1gKZw1FAixcmtWmCb57suqX8ufIxDLOYflIJvMwblARx/zjo3izQcwvWDFO 9v4zVvxIsyGJnri7Z8J33VdKdUOEwIpPbkOlTLjodXibHAIK3H3kbNvKkfuH0wBTcMcD G6qTleRP3cweBIXlxrQG6kxn9P/SD/cLD0wwaryGySK4vWHQUyloyxCbZ1LKVSWNpa6W uVIncprobEM86xy1gAXQ45u0NHpZBFj5vy57yMK0+D/i4YLRmhdTJMuwHzpiaj8v7vOX v+Yg== X-Gm-Message-State: AO0yUKVrr31B3Rk3a2Ry6TdnH4tLHZxedOM8a30DZJYBEX1DSi4hcR43 GvQnnXx+Yu5Kru+FR+woqU2nGy7xv5V209W6aAqsvnAj26Zyy9WdONElPQ5Snb7MyAe3RS7GijJ kCu82udZicfQ= X-Received: by 2002:a05:6402:614:b0:4ad:7439:db03 with SMTP id n20-20020a056402061400b004ad7439db03mr3594326edv.12.1676641739090; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 964DD40007 X-Stat-Signature: moadyuapzq9nmh5icraggb3ayakkr89x X-HE-Tag: 1676641742-906594 X-HE-Meta: U2FsdGVkX18Fl+A6AVCTPKHY9omX4vnErybXsOKS4Lz4pMfLAzvu6OTU22UmYf2ATbWHcoFLdSZ+opJ47sl/KnlcLtF8DQRZ4CmlbpphHnQ6q8EHKFpeHSjSXBVvNrlXDDoexK7Hr6pFXcnNzgfhOaJO3pl+JNt10amhsjTFIH8NlhtyUw/JWz7T06/IZyW54+zhcPC2T1hM3chfqZlAWmnIiVOJjzGHULa1j8p3jqcn+FDbRT2G1MptmQC/FW9URR7H/6ZmcqLDtBtLvqKKFWd7JMwrJnXo3CXP8F6xtWRSfMMuI7WHV4k61Q9MjRpGotOrnTRafwJA0JmFpX+NgwmVkkMpLZj7GvHBEzJsLmZzkXmwuLjoW6EDHJd1AbwOkA4yfe3LP6u5CXo0t2FNQNFloRfilR2ULcBltHo1R47coFkGyV3L7lAHJp7LakKxY8bd7ELsYi3ifHyHsWGH5/L5hGFQxJrVWB6bx68fZzvptedHemEH5dOqzlfk6bxIAJ/CvxSM2jWBT9zs2lJkv97hjCVEZLgWL6/ULyd/u5h8WFUb9TNzDmwrnrNulQUwJysT4XlYiPfIJR+xiIOMuC62HnFvescE5xBRD+NXaqW1+6o3P90ik0alEhLQ+CJ5aZRAWmvZFygROk4PNxSFJg5RpIUWCPsyVNmTOE0w6BXwWEtlqJaE+BW4RJ4PBBwmhm0rDBFQ2ds0IoeEpfR1OCVaI3L6e4sVos+t5liiNAKNzssg9GmKAeppSlMnYi0NcTNG63ovyFbPjJsEPClzHweBbvZsomTtlmLb9R/h6DLtTVDAqiZpv6rylARBlTLFF0TE1H2bIXJpHk1BufKjku/tBq96/prTeaPPblH+mDGeHgSYau0SxP1CMrPi9+Cpz4ZrEHHTfOYzoNgcTUAw04IV+5QJYfMGmIcTlwvBU+LchHx3C0gAK0hZJuDVaUpL+5OoOhAkHmliyWRWnNB d/BxCte2 midRYRKNfs04dijLFtzQLWbag4BnooFJHrotoq8S6YT2I3UpXsgQZFAKSuMM3DkUR/8Y1g5UZ5havXyGDuE0pMDUBzdmjqXJMkrZOw1qO37MiYkdI6A2psi8uzUUhOW7/2R8/c0yBxQ2W0sx0bEORgKVvdJtqniI1aaG+5vr7O3mldVOKy+ofGjWzwZfFmjkj7oocs9ZbVR4aZraHPE9K3MhPojAyppwRMTfy3cfpciotIX4GXF5FwRXGdYePg2fmFlq33KWAYcnkuUxyDWy2WMC3UCF98I+d9QVupUpFkmXk4k8APwcnEaD+Es5JzbVan4EA082GeTe8ze+TbZotbd1bAb3vWk1vnzHRtNADU2kRuCELAXo+c3Dw6YuYMIqeB4IKq7Gd0XHd1d4U4LRn87OT/L/lihw85roE3ci8KzAlByf5hq7dZZX7artq5H078400 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 += nouveau_prime.o nouveau-y += nouveau_sgdma.o nouveau-y += nouveau_ttm.o nouveau-y += nouveau_vmm.o +nouveau-y += nouveau_uvmm.o # DRM - modesetting nouveau-$(CONFIG_DRM_NOUVEAU_BACKLIGHT) += nouveau_backlight.o diff --git a/drivers/gpu/drm/nouveau/nouveau_abi16.c b/drivers/gpu/drm/nouveau/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); + /* 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 = &abi16->device; engine = NV_DEVICE_HOST_RUNLIST_ENGINES_GR; 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 *align, u64 *size) struct nouveau_bo * nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size, int *align, u32 domain, - u32 tile_mode, u32 tile_flags) + u32 tile_mode, u32 tile_flags, bool internal) { struct nouveau_drm *drm = 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 = true; } - if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI) { - nvbo->kind = (tile_flags & 0x0000ff00) >> 8; - if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) { - kfree(nvbo); - return ERR_PTR(-EINVAL); + nvbo->contig = !(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 >= NV_DEVICE_INFO_V0_FERMI) { + nvbo->kind = (tile_flags & 0x0000ff00) >> 8; + if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) { + kfree(nvbo); + return ERR_PTR(-EINVAL); + } + + nvbo->comp = mmu->kind[nvbo->kind] != nvbo->kind; + } else if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA) { + nvbo->kind = (tile_flags & 0x00007f00) >> 8; + nvbo->comp = (tile_flags & 0x00030000) >> 16; + if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) { + kfree(nvbo); + return ERR_PTR(-EINVAL); + } + } else { + nvbo->zeta = (tile_flags & 0x00000007); } + nvbo->mode = tile_mode; + + /* Determine the desirable target GPU page size for the buffer. */ + for (i = 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; - nvbo->comp = mmu->kind[nvbo->kind] != nvbo->kind; - } else - if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA) { - nvbo->kind = (tile_flags & 0x00007f00) >> 8; - nvbo->comp = (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 = i; + + /* Stop once the buffer is larger than the current page size. */ + if (*size >= 1ULL << vmm->page[i].shift) + break; + } + + if (WARN_ON(pi < 0)) { kfree(nvbo); return ERR_PTR(-EINVAL); } - } else { - nvbo->zeta = (tile_flags & 0x00000007); - } - nvbo->mode = tile_mode; - nvbo->contig = !(tile_flags & NOUVEAU_GEM_TILE_NONCONTIG); - - /* Determine the desirable target GPU page size for the buffer. */ - for (i = 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; - /* 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 = i; - - /* Stop once the buffer is larger than the current page size. */ - if (*size >= 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 >= NVIF_CLASS_MMU_GF100) + nvbo->kind = mmu->kind[nvbo->kind]; + nvbo->comp = 0; + } + nvbo->page = 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); - if (WARN_ON(pi < 0)) { - kfree(nvbo); - return ERR_PTR(-EINVAL); - } + /* Determine the desirable target GPU page size for the buffer. */ + for (i = 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; - /* Disable compression if suitable settings couldn't be found. */ - if (nvbo->comp && !vmm->page[pi].comp) { - if (mmu->object.oclass >= NVIF_CLASS_MMU_GF100) - nvbo->kind = mmu->kind[nvbo->kind]; - nvbo->comp = 0; + if (pi < 0) + pi = i; + /* Stop once the buffer is larger than the current page size. */ + if (*size >= 1ULL << vmm->page[i].shift) + break; + } + if (WARN_ON(pi < 0)) { + kfree(nvbo); + return ERR_PTR(-EINVAL); + } + nvbo->page = vmm->page[pi].shift; } - nvbo->page = vmm->page[pi].shift; nouveau_bo_fixup_align(nvbo, align, size); @@ -334,7 +369,7 @@ nouveau_bo_new(struct nouveau_cli *cli, u64 size, int align, int ret; nvbo = nouveau_bo_alloc(cli, &size, &align, domain, tile_mode, - tile_flags); + tile_flags, true); if (IS_ERR(nvbo)) return PTR_ERR(nvbo); @@ -938,6 +973,7 @@ static void nouveau_bo_move_ntfy(struct ttm_buffer_object *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 = dma_resv_wait_timeout(bo->base.resv, @@ -946,6 +982,7 @@ static void nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, WARN_ON(ret <= 0); nouveau_vma_unmap(vma); } + nouveau_uvmm_bo_unmap_all(nvbo); } 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; 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/nouveau/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" 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)); 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/nouveau/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" 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; struct list_head head; @@ -112,15 +115,60 @@ struct nouveau_cli_work { struct dma_fence_cb cb; }; +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 = 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 = nouveau_cli_uvmm(cli); + if (uvmm) + return &uvmm->vmm; + if (cli->svm.cli) return &cli->svm; return &cli->vmm; } +static inline void +__nouveau_cli_uvmm_disable(struct nouveau_cli *cli) +{ + struct nouveau_uvmm *uvmm; + + uvmm = nouveau_cli_uvmm(cli); + if (!uvmm) + cli->uvmm.disabled = 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 *); diff --git a/drivers/gpu/drm/nouveau/nouveau_gem.c b/drivers/gpu/drm/nouveau/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, struct drm_file *file_priv) goto out; } - ret = nouveau_vma_new(nvbo, vmm, &vma); + /* only create a VMA on binding */ + if (!nouveau_cli_uvmm(cli)) + ret = nouveau_vma_new(nvbo, vmm, &vma); + else + ret = 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, struct drm_file *file_priv) if (vmm->vmm.object.oclass < NVIF_CLASS_VMM_NV50) return; + if (nouveau_cli_uvmm(cli)) + return; + ret = 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 |= NOUVEAU_GEM_DOMAIN_CPU; nvbo = nouveau_bo_alloc(cli, &size, &align, domain, tile_mode, - tile_flags); + tile_flags, false); if (IS_ERR(nvbo)) return PTR_ERR(nvbo); @@ -279,13 +286,15 @@ nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem, else rep->domain = NOUVEAU_GEM_DOMAIN_VRAM; rep->offset = nvbo->offset; - if (vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) { + if (vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50 && + !nouveau_cli_uvmm(cli)) { vma = nouveau_vma_find(nvbo, vmm); if (!vma) return -EINVAL; rep->offset = vma->addr; - } + } else + rep->offset = 0; rep->size = nvbo->bo.base.size; rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.base.vma_node); @@ -310,6 +319,11 @@ nouveau_gem_ioctl_new(struct drm_device *dev, void *data, struct nouveau_bo *nvbo = NULL; int ret = 0; + /* If uvmm wasn't initialized until now disable it completely to prevent + * userspace from mixing up UAPIs. + */ + nouveau_cli_uvmm_disable(cli); + ret = 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; + if (unlikely(nouveau_cli_uvmm(cli))) + return -ENOSYS; + list_for_each_entry(temp, &abi16->channels, head) { if (temp->chan->chid == req->channel) { chan = temp->chan; diff --git a/drivers/gpu/drm/nouveau/nouveau_mem.h b/drivers/gpu/drm/nouveau/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_vma *); +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/nouveau/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, dma_resv_lock(robj, NULL); nvbo = 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 = ERR_CAST(nvbo); goto unlock; diff --git a/drivers/gpu/drm/nouveau/nouveau_uvmm.c b/drivers/gpu/drm/nouveau/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 limitation + * 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 single + * map/unmap operations against a BO move, which itself walks the GEM's GPUVA + * 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 = addr + range; + u64 unmanaged_end = uvmm->unmanaged_addr + + uvmm->unmanaged_size; + + if (addr & ~PAGE_MASK) + return -EINVAL; + + if (range & ~PAGE_MASK) + return -EINVAL; + + if (end <= 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 = &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 = &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 = &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 = &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 = &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 = &uvmm->vmm.vmm; + union { + struct gf100_vmm_map_v0 gf100; + } args; + u32 argc = 0; + + switch (vmm->object.oclass) { + case NVIF_CLASS_VMM_GF100: + case NVIF_CLASS_VMM_GM200: + case NVIF_CLASS_VMM_GP100: + args.gf100.version = 0; + if (mem->mem.type & NVIF_MEM_VRAM) + args.gf100.vol = 0; + else + args.gf100.vol = 1; + args.gf100.ro = 0; + args.gf100.priv = 0; + args.gf100.kind = kind; + argc = 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 = reg->region.va.addr << PAGE_SHIFT; + u64 range = 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 = uvma->va.va.addr << PAGE_SHIFT; + u64 range = 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 = uvma->va.va.addr << PAGE_SHIFT; + u64 offset = uvma->va.gem.offset << PAGE_SHIFT; + u64 range = 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 = uvma->va.va.addr << PAGE_SHIFT; + u64 range = uvma->va.va.range << PAGE_SHIFT; + bool sparse = 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 = 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 >>= PAGE_SHIFT; + bo_offset >>= PAGE_SHIFT; + range >>= PAGE_SHIFT; + + uvma->uvmm = uvmm; + uvma->kind = kind; + uvma->va.va.addr = addr; + uvma->va.va.range = range; + uvma->va.gem.offset = bo_offset; + uvma->va.gem.obj = obj; + + ret = __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 = 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 = uvmm; + reg->region.va.addr = addr >> PAGE_SHIFT; + reg->region.va.range = range >> PAGE_SHIFT; + reg->region.sparse = sparse; + + ret = __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 = nouveau_uvma_region_alloc(®); + if (ret) + return ret; + + ret = nouveau_uvma_region_insert(uvmm, reg, addr, range, sparse); + if (ret) + goto err_free_region; + + if (sparse) { + ret = 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 = 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 = reg->uvmm; + u64 addr = reg->region.va.addr << PAGE_SHIFT; + u64 range = reg->region.va.range << PAGE_SHIFT; + bool sparse = 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 = 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 = last; + u64 vmm_get_start = args ? args->addr : 0; + u64 vmm_get_end = 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 = &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 = 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 = &op->remap; + struct drm_gpuva *va = r->unmap->va; + u64 ustart = va->va.addr << PAGE_SHIFT; + u64 urange = va->va.range << PAGE_SHIFT; + u64 uend = ustart + urange; + + if (r->prev) + vmm_get_start = uend; + + if (r->next) + vmm_get_end = ustart; + + if (r->prev && r->next) + vmm_get_start = vmm_get_end = 0; + + break; + } + case DRM_GPUVA_OP_UNMAP: { + struct drm_gpuva_op_unmap *u = &op->unmap; + struct drm_gpuva *va = u->va; + u64 ustart = va->va.addr << PAGE_SHIFT; + u64 urange = va->va.range << PAGE_SHIFT; + u64 uend = ustart + urange; + + /* Nothing to do for mappings we merge with. */ + if (uend == vmm_get_start || + ustart == vmm_get_end) + break; + + if (ustart > vmm_get_start) { + u64 vmm_get_range = ustart - vmm_get_start; + + nouveau_uvmm_vmm_put(uvmm, vmm_get_start, + vmm_get_range); + } + vmm_get_start = uend; + break; + } + default: + break; + } + + if (op == 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 = drm_gpuva_last_op(ops); + struct uvmm_map_args args = { + .addr = addr, + .range = 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 = 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 = nouveau_uvma_alloc(&uvma); + if (ret) + goto err; + + ret = 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 = uvma; + return 0; + +err_free_uvma: + nouveau_uvma_free(uvma); +err: + *puvma = NULL; + return ret; +} + +static void +op_unmap_prepare(struct drm_gpuva_op_unmap *u) +{ + struct nouveau_uvma *uvma = 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 = args ? args->addr : 0; + u64 vmm_get_end = 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 = vmm_get_end - vmm_get_start; + + ret = op_map_prepare(uvmm, &new->map, &op->map, args); + if (ret) + goto unwind; + + if (args && vmm_get_range) { + ret = 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 = &op->remap; + struct drm_gpuva *va = r->unmap->va; + struct uvmm_map_args remap_args = { + .kind = uvma_from_va(va)->kind, + }; + u64 ustart = va->va.addr << PAGE_SHIFT; + u64 urange = va->va.range << PAGE_SHIFT; + u64 uend = ustart + urange; + + op_unmap_prepare(r->unmap); + + if (r->prev) { + ret = op_map_prepare(uvmm, &new->prev, r->prev, + &remap_args); + if (ret) + goto unwind; + + if (args) + vmm_get_start = uend; + } + + if (r->next) { + ret = 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 = ustart; + } + + if (args && (r->prev && r->next)) + vmm_get_start = vmm_get_end = 0; + + break; + } + case DRM_GPUVA_OP_UNMAP: { + struct drm_gpuva_op_unmap *u = &op->unmap; + struct drm_gpuva *va = u->va; + u64 ustart = va->va.addr << PAGE_SHIFT; + u64 urange = va->va.range << PAGE_SHIFT; + u64 uend = ustart + urange; + + op_unmap_prepare(u); + + if (!args) + break; + + /* Nothing to do for mappings we merge with. */ + if (uend == vmm_get_start || + ustart == vmm_get_end) + break; + + if (ustart > vmm_get_start) { + u64 vmm_get_range = ustart - vmm_get_start; + + ret = nouveau_uvmm_vmm_get(uvmm, vmm_get_start, + vmm_get_range); + if (ret) { + op_unmap_prepare_unwind(va); + goto unwind; + } + } + vmm_get_start = uend; + + break; + } + default: + ret = -EINVAL; + goto unwind; + } + } + + return 0; + +unwind: + if (op != 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 = { + .addr = addr, + .range = range, + .kind = 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 = 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 = u->va; + struct nouveau_uvma *uvma = 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 = uvma_from_va(u->va); + bool sparse = uvma->va.region->sparse; + + addr <<= PAGE_SHIFT; + range <<= 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 = r->unmap; + struct nouveau_uvma *uvma = uvma_from_va(u->va); + u64 addr = uvma->va.va.addr; + u64 range = uvma->va.va.range; + + if (r->prev) { + addr = r->prev->va.addr + r->prev->va.range; + drm_gpuva_link(&new->prev->va); + } + + if (r->next) { + range = 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 = 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 = &op->remap; + struct drm_gpuva_op_map *p = r->prev; + struct drm_gpuva_op_map *n = r->next; + struct drm_gpuva *va = r->unmap->va; + struct nouveau_uvma *uvma = uvma_from_va(va); + + if (unmap) { + u64 addr = va->va.addr << PAGE_SHIFT; + u64 end = addr + (va->va.range << PAGE_SHIFT); + + if (p) + addr = (p->va.addr << PAGE_SHIFT) + + (n->va.range << PAGE_SHIFT); + + if (n) + end = 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 = &op->unmap; + struct drm_gpuva *va = u->va; + struct nouveau_uvma *uvma = 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 = &nvbo->bo.base; + struct drm_gpuva *va; + + drm_gem_gpuva_lock(obj); + drm_gem_for_each_gpuva(va, obj) { + struct nouveau_uvma *uvma = 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 = &nvbo->bo.base; + struct drm_gpuva *va; + + drm_gem_gpuva_lock(obj); + drm_gem_for_each_gpuva(va, obj) { + struct nouveau_uvma *uvma = 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 = init->unmanaged_addr + init->unmanaged_size; + + mutex_init(&uvmm->mutex); + + mutex_lock(&cli->mutex); + + if (unlikely(cli->uvmm.disabled)) { + ret = -ENOSYS; + goto out_unlock; + } + + if (unmanaged_end <= init->unmanaged_addr) { + ret = -EINVAL; + goto out_unlock; + } + + if (unmanaged_end > NOUVEAU_VA_SPACE_END) { + ret = -EINVAL; + goto out_unlock; + } + + uvmm->unmanaged_addr = init->unmanaged_addr; + uvmm->unmanaged_size = 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 = 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 = 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 = uvmm->vmm.cli; + + if (!cli) + return; + + nouveau_uvmm_lock(uvmm); + drm_gpuva_iter_for_each(it) { + struct drm_gpuva *va = it.va; + struct nouveau_uvma *uvma = uvma_from_va(va); + struct drm_gem_object *obj = 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 = __it.reg; + struct nouveau_uvma_region *ureg = uvma_region_from_va_region(reg); + + if (unlikely(reg == &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/nouveau/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_uvma_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 From patchwork Fri Feb 17 13:48:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144736 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D83EC05027 for ; Fri, 17 Feb 2023 13:49:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EEC306B0078; Fri, 17 Feb 2023 08:49:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E9BFC6B0081; Fri, 17 Feb 2023 08:49:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA2C5280003; Fri, 17 Feb 2023 08:49:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B53646B0078 for ; Fri, 17 Feb 2023 08:49:10 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 822B7140814 for ; Fri, 17 Feb 2023 13:49:10 +0000 (UTC) X-FDA: 80476915260.25.3BE9FC8 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id 6CC901A0020 for ; Fri, 17 Feb 2023 13:49:08 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E7+88XrN; spf=pass (imf19.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641748; h=from:from:sender: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:dkim-signature; bh=1gEEEVEriS4+00Hosnu6p0Wtu19O1LPFPG0fsqoPCf8=; b=LqQFCKOAux6djyEIbFmmUguUZQguFRswzv4CAbI9DHU/UxFl5aB8RH6pAHBJkYWbf1cWT2 WmZaKSbZATSi5I5kArxzClbfyo47AjkvjIiVW2y8L/+MmkuLZJv0smsgLj5hAsd9mNkRk0 ZETnUBaaAx5IMMizbNQkhxcweVWY+Rc= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E7+88XrN; spf=pass (imf19.hostedemail.com: domain of dakr@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641748; a=rsa-sha256; cv=none; b=Ltk77D9xa/aN1vAWEXCR/pcJpUGzEFxoT6qu0qJ7B0nRK9tUSo3wvEcebG0Bp3LbZ31cGL W1sad1dySVesZFH3eDkDl/zjrH/e5gJFGUUTTO9cuJg2CrFShpkyJfA0en+Z18doBGjSVR xoT3INUX2TpzlZL2yflK2wjcg2/FUnQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641747; 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=1gEEEVEriS4+00Hosnu6p0Wtu19O1LPFPG0fsqoPCf8=; b=E7+88XrNgHXTDihHpuv/+ivpcb5Uuy9uRsp8djsFgEFc8uHYJkeJZLEBapVNhH3VJKW/NA +8QFbrQjX95pCiH0OP4YBbNyJOH8W+Le42ZvN/leMCYvcjgocKYlc4xmVwGzkHug62EBzZ Z1sMipaQE9HCn/8c+l7ZebEhdUPJ8UM= 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-344-s6D7OIsBOfOP1NxhZ9iqgA-1; Fri, 17 Feb 2023 08:49:04 -0500 X-MC-Unique: s6D7OIsBOfOP1NxhZ9iqgA-1 Received: by mail-ed1-f71.google.com with SMTP id cn9-20020a0564020ca900b004a25d8d7593so1230416edb.0 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=DyOJbK+/ll1rb/z6EDkMflq+KDXFuoXjmaXQ0J1nH7uTpmP971ulYcGSKCcmXKqZsz gwcT9ry2QvGXMehB4onLsEVph3FVSYPsplhhnT+R0a0Vihw4s8A5PMgebBA8MxHVbUNw rAAiaB8L7SHLkXmix1Dikq8Z6JCFw/9o6Hs68XqVXvVILAzzHbEdswSWXvXFNT5cPpu7 QFdKhzc/NZhwpr7YxGpy4u2rIo8jVvZXRe5WpExgBY+t8Kkd/jxDDVPEfGfKWteTcMdq 3zRq1SerfJymUZwHhlLG+DA1ldOg1lBpn2EykM0gCmWchx0K2l/HDDFsKgPJZtjmttMu XtcA== X-Gm-Message-State: AO0yUKWAACkvx5+d89X6jrKiBsLRdSJxU2O2e2ZUPOTcSa/0obEBH5ke 1q2mD8loAW3HOnthBm+r9xcIb4zIWM1tuLOsL23tXcoxED3xIpAu9cBRtuLYelIpJZ/AetM+Iys arRjeW+6NQ8g= X-Received: by 2002:a17:906:374d:b0:886:ec6e:4c1 with SMTP id e13-20020a170906374d00b00886ec6e04c1mr633397ejc.59.1676641743063; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 58a4k9ybduifujbion7gcnr34a31ewfg X-Rspamd-Queue-Id: 6CC901A0020 X-HE-Tag: 1676641748-780590 X-HE-Meta: U2FsdGVkX1+GNInP93itDicKof9TJc44Uqg2W9AkHKxfY1QudFNyvRF685gxV6gNQYQ4Y6/+BU+8k/maBia3ymKhAKmqc4zLaGBymaIBDrEGsHYZuaXwHBxIdfQlgueE6PHugaFnMrRdyw53+nY2/GuABnMnwfK5MyksGXLX2KVuWiOcaRy74Y01aE3DbjGlyU3MZKDn2KlVjtP36ZCr6+1my2GjfRYNzuVPvCuePhHpN9Oyqrcw4EXMyh/P5TW6AKdsZVuqbBsPV6gLbhXCuZMaPNbhnl2yU3cQHb6aj+MtMhio7fRosj1A4t9bqZ1dNfrhNZfkUs4GrJ6vUFe+8/D5C8MBzKE+7CMETQbdbHfOPHMeK32zibQmbgbtWGJMoBk5eQuW0oPr32KqPB1kEwUloYT1wU0rVBsf/DS7T98MJuu2XXusbco01m8KLQ8fROVVX1pxh5kF7bjqE/AVqPYgdVwSSrbrAlQzJ7DvjXHKHS29/B18e5xxEJuY+w+nnidWhbcZJxyqaHmf7cIcCsI1lgsZqg61yfe6hzVyLUxAx1RHNRSDsLDpDvVhU0L8Z7Y2kj8vX3cyTu6JlWfsSw5zJPUJ0IPKZpS6eQ3BgPcgQ/lwqeBX2SmSKe1ncGneaD0b2D2FwbI4PzBzCc+QB8l3+Cj2lIJxVEKA8MsjW94Wu6LBP7oidB9D1L0L9xaNDKoFZjE4sxC49YtfbZqro57w73aPItmMSvM5OtZ8iNYoS1/CvWfOThjBsmk6CqptfuyrwyFvNTNYM7sSE0c1fpQ08/03q4bpEICoudzTbvqCI9hLuoEVJysdCRiqw5ki2jxDZUjeyimZnUCO209XTjOybENlI/91iGnOEDcxMkQZQRZGVjYIyhR2AFEKxaN2aq9M5q7PsIcICHKCLjzcYaHexr9Jdh55Fkj+FU3curY9qDiwSDI5XlSQcGf/m4euJYFFXsF+VNt8V8olMgN ACJFx1A1 pRqjKwo77Y6C5vQf4+wSK53JNVnc1hYx3gVKy3dNNyqWxf8QtcKeCiuBxuHzBvFfEMkrK7UqZ9zUyuqm5k2hq3iF9LcDdAcLKlqZsyaG/ynFiaMimvhMUHji+NXSZS/y7oZ0br8P6zRHBOtCJgsBVmAqV6X9jNULwEyHRNV0PLmKuhJ0XMW9tml1HBaOCX951qONLOJqh/giRhs5vkaSwdca99aPwGlHZESKW7gXMqU12HwTHUfZP4s9UmI47xDb7QjpAJZJw6pBUgBqUIkpJNivn1uweuyD3zAwv1atVFtj89U1E7qgEMwUuJCN4eZV3SoId1jremfXQNKBz6xEFMyuzkWykyIdtpF5lRIRm7QPuPqu4uDG1k2Xo0oOpO82lQ1pB7Pz8AIiZ9CrrsxQTOk8NuxuljrBlvFwUL5h2lRnsrWvicQDrlqH2sQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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-uapi.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 ------------------- +.. 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 += nouveau_prime.o nouveau-y += nouveau_sgdma.o nouveau-y += nouveau_ttm.o nouveau-y += nouveau_vmm.o +nouveau-y += nouveau_exec.o +nouveau-y += nouveau_sched.o nouveau-y += nouveau_uvmm.o # DRM - modesetting diff --git a/drivers/gpu/drm/nouveau/Kconfig b/drivers/gpu/drm/nouveau/Kconfig 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/nouveau/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" 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; + /* 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; + ret = nouveau_sched_entity_init(&chan->sched_entity, &drm->sched); + if (ret) + goto done; + init->channel = chan->chan->chid; if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) diff --git a/drivers/gpu/drm/nouveau/nouveau_abi16.h b/drivers/gpu/drm/nouveau/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; }; struct nouveau_abi16 { diff --git a/drivers/gpu/drm/nouveau/nouveau_drm.c b/drivers/gpu/drm/nouveau/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" 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)); + 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, } cli->mem = &mems[ret]; + + ret = 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); } - return 0; + ret = nouveau_sched_init(&drm->sched, drm); + if (ret) + goto fail_sched_init; + 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 = nouveau_drm(dev); + 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[] = { DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_RENDER_ALLOW), DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_RENDER_ALLOW), DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_RENDER_ALLOW), + 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), }; long @@ -1223,7 +1241,9 @@ static struct drm_driver driver_stub = { .driver_features = DRIVER_GEM | DRIVER_MODESET | - DRIVER_RENDER, + DRIVER_RENDER | + DRIVER_SYNCOBJ | DRIVER_SYNCOBJ_TIMELINE | + DRIVER_GEM_GPUVA, .open = nouveau_drm_open, .postclose = nouveau_drm_postclose, .lastclose = nouveau_vga_lastclose, diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/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" #define DRIVER_MAJOR 1 -#define DRIVER_MINOR 3 -#define DRIVER_PATCHLEVEL 1 +#define DRIVER_MINOR 4 +#define DRIVER_PATCHLEVEL 0 /* * 1.1.1: @@ -63,6 +63,7 @@ struct platform_device; #include "nouveau_fence.h" #include "nouveau_bios.h" +#include "nouveau_sched.h" #include "nouveau_vmm.h" #include "nouveau_uvmm.h" @@ -94,6 +95,8 @@ struct nouveau_cli { struct nouveau_vmm svm; struct nouveau_uvmm uvmm; + struct nouveau_sched_entity sched_entity; + const struct nvif_mclass *mem; struct list_head head; @@ -305,6 +308,8 @@ struct nouveau_drm { struct mutex lock; bool component_registered; } audio; + + struct drm_gpu_scheduler sched; }; static inline struct nouveau_drm * diff --git a/drivers/gpu/drm/nouveau/nouveau_exec.c b/drivers/gpu/drm/nouveau/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 limitation + * 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 space + * using the DRM_NOUVEAU_VM_INIT ioctl specifying which region of the VA space + * should be managed by the kernel and which by the UMD. + * + * The DRM_NOUVEAU_VM_BIND ioctl provides clients an interface to manage the + * userspace-managable portion of the VA space. It provides operations to + * allocate and free a VA space regions and operations to map and unmap memory + * within such a region. Bind operations crossing region boundaries are not + * permitted. + * + * When allocating a VA space region userspace may flag this region as sparse. + * 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 make + * sure that sparse mappings will take their place again. + * + * When using the VM_BIND ioctl to request the kernel to map memory to a given + * virtual address in the GPU's VA space there is no guarantee that the actual + * mappings are created in the GPU's MMU. If the given memory is swapped out + * at the time the bind operation is executed the kernel will stash the mapping + * 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 memory + * mappings, as requested by userspace previously, are actually mapped once the + * DRM_NOUVEAU_EXEC ioctl is called to submit an exec job. + * + * Contrary to VM_BIND map requests, unmap requests are allowed to span over 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_manager. + * + * When a VA space region is freed, all existing mappings within this region 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 block + * until the bind job is finished and no syncobjs are permitted by the kernel. + * + * 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, provide + * the option to synchronize them with syncobjs. + * + * Besides that EXEC job can be scheduled for a specified channel to execute on. + * + * Since VM_BIND jobs update the GPU's VA space on job submit, EXEC jobs do 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 = to_nouveau_exec_job(job); + struct nouveau_cli *cli = exec_job->base.cli; + struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(cli); + struct drm_exec *exec = &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 = it.va; + + ret = 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 = to_nouveau_exec_job(job); + struct nouveau_fence *fence; + int i, ret; + + ret = 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 = 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 = 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 = 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 = 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 = { + .submit = nouveau_exec_job_submit, + .run = nouveau_exec_job_run, + .free = nouveau_exec_job_free, + .timeout = 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 = *pjob = kzalloc(sizeof(*job), GFP_KERNEL); + if (!job) + return -ENOMEM; + + job->push.count = args->push.count; + job->push.s = kmemdup(args->push.s, + sizeof(*args->push.s) * + args->push.count, + GFP_KERNEL); + if (!job->push.s) { + ret = -ENOMEM; + goto err_free_job; + } + + job->base.ops = &nouveau_exec_job_ops; + job->base.resv_usage = DMA_RESV_USAGE_WRITE; + + ret = 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 = NULL; + + return ret; +} + +static int +nouveau_exec(struct nouveau_exec_job_args *args) +{ + struct nouveau_exec_job *job; + int ret; + + ret = nouveau_exec_job_init(&job, args); + if (ret) + return ret; + + ret = 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 = nouveau_abi16_get(file_priv); + struct nouveau_cli *cli = nouveau_cli(file_priv); + struct nouveau_abi16_chan *chan16; + struct nouveau_channel *chan = NULL; + struct nouveau_exec_job_args args = {}; + struct drm_nouveau_exec *req = data; + int ret = 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 == req->channel) { + chan = 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 == 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 = req->push_count; + args.push.s = u_memcpya(req->push_ptr, req->push_count, + sizeof(*args.push.s)); + if (IS_ERR(args.push.s)) { + ret = PTR_ERR(args.push.s); + goto out; + } + + ret = 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 = &chan16->sched_entity; + args.base.chan = chan; + args.base.file_priv = file_priv; + + ret = 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/nouveau/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/nouveau/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 limitation + * 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 job + * 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 being + * 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 = &args->in_sync.s; + + args->in_sync.count = inc; + *s = u_memcpya(ins, inc, sizeof(**s)); + if (IS_ERR(*s)) { + ret = PTR_ERR(*s); + goto err_out; + } + } + + if (outc) { + s = &args->out_sync.s; + + args->out_sync.count = outc; + *s = u_memcpya(outs, outc, sizeof(**s)); + if (IS_ERR(*s)) { + ret = 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 = args->sched_entity; + int ret; + + job->file_priv = args->file_priv; + job->cli = nouveau_cli(args->file_priv); + job->chan = args->chan; + job->entity = entity; + + job->in_sync.count = args->in_sync.count; + if (job->in_sync.count) { + if (job->sync) + return -EINVAL; + + job->in_sync.data = 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 = args->out_sync.count; + if (job->out_sync.count) { + if (job->sync) { + ret = -EINVAL; + goto err_free_in_sync; + } + + job->out_sync.data = kmemdup(args->out_sync.s, + sizeof(*args->out_sync.s) * + args->out_sync.count, + GFP_KERNEL); + if (!job->out_sync.data) { + ret = -ENOMEM; + goto err_free_in_sync; + } + + job->out_sync.objs = kcalloc(job->out_sync.count, + sizeof(*job->out_sync.objs), + GFP_KERNEL); + if (!job->out_sync.objs) { + ret = -ENOMEM; + goto err_free_out_sync; + } + + job->out_sync.chains = kcalloc(job->out_sync.count, + sizeof(*job->out_sync.chains), + GFP_KERNEL); + if (!job->out_sync.chains) { + ret = -ENOMEM; + goto err_free_objs; + } + + } + + ret = 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 = sync->flags & DRM_NOUVEAU_SYNC_TYPE_MASK; + u64 point = 0; + int ret; + + if (stype != DRM_NOUVEAU_SYNC_SYNCOBJ && + stype != DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ) + return -EOPNOTSUPP; + + if (stype == DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ) + point = sync->timeline_value; + + ret = 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 = NULL; + int ret, i; + + for (i = 0; i < job->in_sync.count; i++) { + struct drm_nouveau_sync *sync = &job->in_sync.data[i]; + + ret = sync_find_fence(job, sync, &in_fence); + if (ret) { + NV_PRINTK(warn, job->cli, + "Failed to find syncobj (-> in): handle=%d\n", + sync->handle); + return ret; + } + + ret = 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 = 0; i < job->out_sync.count; i++) { + struct drm_nouveau_sync *sync = &job->out_sync.data[i]; + struct drm_syncobj **pobj = &job->out_sync.objs[i]; + struct dma_fence_chain **pchain = &job->out_sync.chains[i]; + u32 stype = sync->flags & DRM_NOUVEAU_SYNC_TYPE_MASK; + + if (stype != DRM_NOUVEAU_SYNC_SYNCOBJ && + stype != DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ) { + ret = -EINVAL; + goto err_sync_cleanup; + } + + *pobj = drm_syncobj_find(job->file_priv, sync->handle); + if (!*pobj) { + NV_PRINTK(warn, job->cli, + "Failed to find syncobj (-> out): handle=%d\n", + sync->handle); + ret = -ENOENT; + goto err_sync_cleanup; + } + + if (stype == DRM_NOUVEAU_SYNC_TIMELINE_SYNCOBJ) { + *pchain = dma_fence_chain_alloc(); + if (!*pchain) { + ret = -ENOMEM; + goto err_sync_cleanup; + } + } + } + + return 0; + +err_sync_cleanup: + for (i = 0; i < job->out_sync.count; i++) { + struct drm_syncobj *obj = job->out_sync.objs[i]; + struct dma_fence_chain *chain = 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 = job->done_fence; + int i; + + for (i = 0; i < job->out_sync.count; i++) { + struct drm_nouveau_sync *sync = &job->out_sync.data[i]; + struct drm_syncobj *obj = job->out_sync.objs[i]; + struct dma_fence_chain *chain = job->out_sync.chains[i]; + u32 stype = sync->flags & DRM_NOUVEAU_SYNC_TYPE_MASK; + + if (stype == 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 = &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 = nouveau_gem_object(obj); + + ret = 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 = &job->exec; + struct drm_gem_object *obj; + unsigned long index; + + drm_exec_for_each_locked_object(exec, index, obj) { + struct dma_resv *resv = 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 = 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 = job->ops->submit(job); + if (ret) + goto err; + } + + ret = nouveau_job_validate(job); + if (ret) + goto err; + + ret = nouveau_job_add_deps(job); + if (ret) + goto err; + + ret = nouveau_job_fence_attach_prepare(job); + if (ret) + goto err; + + drm_sched_job_arm(&job->base); + job->done_fence = 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 = 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 = 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 = 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 = { + .run_job = nouveau_sched_run_job, + .timedout_job = nouveau_sched_timedout_job, + .free_job = nouveau_sched_free_job, +}; + +int nouveau_sched_init(struct drm_gpu_scheduler *sched, + struct nouveau_drm *drm) +{ + long job_hang_limit = 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/nouveau/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/nouveau/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) +#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 = DRM_NOUVEAU_VM_BIND_OP_ALLOC, + OP_FREE = DRM_NOUVEAU_VM_BIND_OP_FREE, + OP_MAP = DRM_NOUVEAU_VM_BIND_OP_MAP, + OP_UNMAP = 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 *uvmm, uvmm_sm_cleanup(uvmm, new, ops, true); } +static int +bind_validate_op(struct nouveau_job *job, + struct bind_job_op *op) +{ + struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli); + struct drm_gem_object *obj = op->gem.obj; + + if (op->op == OP_MAP) { + if (op->gem.offset & ~PAGE_MASK) + return -EINVAL; + + if (obj->size <= 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 = nouveau_cli_uvmm(job->cli); + struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job); + struct drm_exec *exec = &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 == OP_MAP) { + op->gem.obj = drm_gem_object_lookup(job->file_priv, + op->gem.handle); + if (!op->gem.obj) { + ret = -ENOENT; + goto unwind; + } + } + + ret = bind_validate_op(job, op); + if (ret) + goto unwind; + + switch (op->op) { + case OP_ALLOC: { + bool sparse = op->flags & DRM_NOUVEAU_VM_BIND_SPARSE; + + ret = nouveau_uvma_region_create(uvmm, + op->va.addr, + op->va.range, + sparse); + if (ret) + goto unwind; + + break; + } + case OP_FREE: + op->reg = nouveau_uvma_region_find(uvmm, op->va.addr, + op->va.range); + if (!op->reg) + goto unwind; + + op->ops = nouveau_uvmm_sm_unmap_ops(uvmm, + op->va.addr, + op->va.range); + if (IS_ERR(op->ops)) { + ret = PTR_ERR(op->ops); + goto unwind; + } + + ret = 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 = nouveau_uvmm_sm_map_ops(uvmm, + op->va.addr, + op->va.range, + op->gem.obj, + op->gem.offset); + if (IS_ERR(op->ops)) { + ret = PTR_ERR(op->ops); + goto unwind; + } + + ret = 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 = nouveau_uvmm_sm_unmap_ops(uvmm, + op->va.addr, + op->va.range); + if (IS_ERR(op->ops)) { + ret = PTR_ERR(op->ops); + goto unwind; + } + + ret = nouveau_uvmm_sm_unmap_prepare(uvmm, &op->new, + op->ops); + if (ret) + goto unwind; + + break; + default: + ret = -EINVAL; + goto unwind; + } + } + + drm_exec_while_not_all_locked(exec) { + list_for_each_op(op, &bind_job->ops) { + if (op->op != OP_MAP) + continue; + + ret = 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 = NULL; + op->reg = 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 = to_uvmm_bind_job(job); + struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli); + struct bind_job_op *op; + int ret = 0; + + list_for_each_op(op, &bind_job->ops) { + switch (op->op) { + case OP_ALLOC: + /* noop */ + break; + case OP_MAP: + ret = nouveau_uvmm_sm_map(uvmm, &op->new, op->ops); + if (ret) + goto out; + break; + case OP_FREE: + fallthrough; + case OP_UNMAP: + ret = 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 = to_uvmm_bind_job(job); + struct nouveau_uvmm *uvmm = 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 = 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 = { + .submit = uvmm_bind_job_submit, + .run = uvmm_bind_job_run, + .free = 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 = *pop = kzalloc(sizeof(*op), GFP_KERNEL); + if (!op) + return -ENOMEM; + + op->op = uop->op; + op->flags = uop->flags; + op->va.addr = uop->addr; + op->va.range = uop->range; + op->gem.handle = uop->handle; + op->gem.offset = 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 = *pjob = kzalloc(sizeof(*job), GFP_KERNEL); + if (!job) + return -ENOMEM; + + INIT_LIST_HEAD(&job->ops); + + for (i = 0; i < args->op.count; i++) { + ret = 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 = !(args->flags & DRM_NOUVEAU_VM_BIND_RUN_ASYNC); + job->base.ops = &nouveau_bind_job_ops; + job->base.resv_usage = DMA_RESV_USAGE_BOOKKEEP; + + ret = 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 = NULL; + + return ret; +} + +int +nouveau_uvmm_ioctl_vm_init(struct drm_device *dev, + void *data, + struct drm_file *file_priv) +{ + struct nouveau_cli *cli = nouveau_cli(file_priv); + struct drm_nouveau_vm_init *init = 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 = nouveau_uvmm_bind_job_init(&job, args); + if (ret) + return ret; + + ret = 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 = nouveau_cli(file_priv); + struct nouveau_uvmm_bind_job_args args = {}; + struct drm_nouveau_vm_bind *req = data; + int ret = 0; + + if (unlikely(!nouveau_cli_uvmm_locked(cli))) + return -ENOSYS; + + args.flags = req->flags; + + args.op.count = req->op_count; + args.op.s = 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 = 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 = &cli->sched_entity; + args.base.file_priv = file_priv; + + ret = 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/nouveau/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; }; +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_uvma_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); +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); From patchwork Fri Feb 17 13:48:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13144735 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F912C64ED9 for ; Fri, 17 Feb 2023 13:49:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EED356B0081; Fri, 17 Feb 2023 08:49:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E9D8B6B0082; Fri, 17 Feb 2023 08:49:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6490280003; Fri, 17 Feb 2023 08:49:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C91D46B0081 for ; Fri, 17 Feb 2023 08:49:11 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A230040433 for ; Fri, 17 Feb 2023 13:49:11 +0000 (UTC) X-FDA: 80476915302.12.1DA1E96 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 8B52F1C001D for ; Fri, 17 Feb 2023 13:49:09 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=h+OxsMam; spf=pass (imf21.hostedemail.com: domain of dakr@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676641749; h=from:from:sender: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:dkim-signature; bh=kGtWB8otfeqdskrUgWVgT1wxpbtVBJjmwPhyWCdCJZU=; b=OwxfxNi/aG14DwqJkDOBgQalcT27P2s7jo5yMwHd4x5gmH1O/JQs6z6BCCXSwy+eL8vGGn 0gAxYzdHXrepQh+nNUeZZKJjVg6hvohn00T5GPxImHiNrLOigyOAB0ZGEq25GKbqAxbHHI mPL8EwlacJLjoPy9ANOXog0lhH+pqf8= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=h+OxsMam; spf=pass (imf21.hostedemail.com: domain of dakr@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dakr@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676641749; a=rsa-sha256; cv=none; b=d2jbaWsMpXAqPrh5vDaBzKy0TTwksi9UPVdT5+Y56h3qznpGKMAALEPsPgGyfPHi5IaQ5z HUHNfTDeKMOkNEQLYIiAP53sjo/SQYN3r8RVf3F9FEh9LP8CHeu8CA9nkVSEwniwwh/3Wp SaYblnTfvgIGsLxY/1PVSDcLhI1NV+Y= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676641748; 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=kGtWB8otfeqdskrUgWVgT1wxpbtVBJjmwPhyWCdCJZU=; b=h+OxsMam3hx1Jdiv9YrKSTNrHIoU+Cy6PZ7iaWKjzvuhQ2oX1QfRggkp/VvR7mPcCaV77E JvtQGOBxqxVXOA2ssGi3+AG8Dol5a4oECXFjk3LQg7/6hDrHjELPy7tg7PIrCSs/SLraQQ MBTYxeDG2WrQIcwjCm8cpWcTalF7BTo= 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-351-lQ2Fm5IwMjuWK8_Bp2ZZ9w-1; Fri, 17 Feb 2023 08:49:07 -0500 X-MC-Unique: lQ2Fm5IwMjuWK8_Bp2ZZ9w-1 Received: by mail-ed1-f70.google.com with SMTP id h14-20020a056402094e00b004acbe232c03so1643596edz.9 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=3NxWcmDgDKOJt8KNNNRANOCGZaENzEuLm8yKk6fT5oYe3ruofg9lOPFZraC+rU2m4T bCJEmtBVuX7pHJ1XRzyBBwwFMl3wzc4Ztt69cqcAecLPyGI25WGBH61cmSXn+FVCYBsi 8WzKGicUalER+2aDQanc31ukbXhr/iTdltjBL/miwAiRepC/x+5UyZBlB+/s/J9Qs4RF KYzltsWe1BiOESdHBvpbNn+Qk5pSWtTr5XdYvqfoU3XI8bHf48a7YT9TrfaX9a7XIXQ5 KoY5aCykAVD+DsJX6TIsAuaZKluPeA4vkyUXN22HnQfYZCY2xJpZkwR0921hPpxFjYPf ZT9g== X-Gm-Message-State: AO0yUKWO8+cmv83CLyiZsdYALj+aZzQlGt2sll+4S3LpqZUux5ghD3+I FLaWM8qdA6WWgbNaEJA6QIKu9m8exKrOdW0AqMNWL01BbuyHYjBdn5sSTyGAdbDRWYO3luk/JXF CJ3no+JMOOug= X-Received: by 2002:a17:907:a508:b0:8af:54d0:181d with SMTP id vr8-20020a170907a50800b008af54d0181dmr10190325ejc.35.1676641746803; 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 8B52F1C001D X-Rspam-User: X-Stat-Signature: kwuo5wkib9sjpe99muibnag566p6gbbn X-HE-Tag: 1676641749-272038 X-HE-Meta: U2FsdGVkX19CpAiNQiJccQiXUkz1+UfsAFMJoBwqWXSPNj7eorUC8is37q61+aAw/X33aMqh5G03g4cxjH2hs/FFMzqSfjKV2Pg5smiKPplJ3UkYiNs67Wyem+I8QkfGMyPCsK4sd0+Yt8HHll4SYLe+dx2wwikXFUvJcJ8+zkZCDhnK2LP5f2zjA+WjUooGvHz7CQEzamhL6o3RnevUC8Vy8mkMLwkle6p5AiBAQbCiLE5XV9IB/mfmXpaDYjpb43QMmdYUdj/Ws0eCxOBHNRDIluXD7jYZF6ckHc//eoNSR3SIih+J2iixOh2C1TaQd5W61TxblTtuP0iHT37eHMwUbfHFAh5w3G/knjnBby38JfRUv8qyjFiZXsEWGD0BwChS6UMKoVNm6EWSxFxfPYR8Vol208H1zQ8M4KhHD3dp/b4L+WbXgKLXZdH1Ji5Bw4py5a/u1p7hIyaWaBCYgXP2ZmWSljdtu9S1lcoJJ3reX6rCEJml30YEHs7QwBK3irCpHk++nzTEGu+wODMvUtqVb0ZZSAu1UqiQc2Q0i7+ezcFR2DcBNbQBwWNqw89V4GaUgzDwNK47ueQaJf0JjPy9bZxTo8ft7Bul+ruDIXsYV6IOKI7CIj+dY+z+xBpaqcho3orVVKmvhILPo0lyiai7hJuS6PwDSU/pCXMrqH2eF6ceTcVudBrKxNxxPzjR4qa/KjtoQTgT0wFZhKFRoaGTbr6MbEo3dprmn2rxGIVkNA9hOkPJEaqmP6MnldFLCNWPYAr+L010JQmPgN9Whhl2bwPBrMEdx7Dv0/Dcl+GUFbB5SKwirUVWyss67GD9ar+6Inv632aAwIeBzEDOM9JoWlEFDvrNigTIEeNS4qP95wp47+20FdWubj5WbYbkJqwGvNYRrLEqLeCLMqSxjTQbCCbTnQYcKTHZdfn+L6quxp+ZGqER6ykDrhnbzxW2GXy6UEDGbYHNTDUt4f0 utQchHEh EV9JLn1Sdd0k9cqgkHJL79iHDf+0UHFCgtyGRmST479zxOpdclWbXHI9r+3iclNLGhc2eHj2pZyltUsD5a3C71GXnmx3yS0SG5+eYEtbfqnItnM5MbO5LlF0NH501sR+BZQtJsftnPUe1toBNkGWE27kUC/DEmjy/qmeLOAwwtZHwfV2Hs+AQBedW8HwJ3vN8uvS2ZLAoBa18ibnBqkGCktw/yJrL6NfKn3IvCFqN7MNdDM6VpaealQn9cXfXZ8DZ/iAfkbASTmTQiP5swpSbbHT8L7Ncxls8jxXz0qvcpX4dC9xc4FMUpfsXfFuxJyYn8Ul3QbNHUIAUPfD5eDzSJ8ouKekSOkprM32xpnwbmCuYiugmHAP2+l1zs4Y3C9GVqCubjzpbQf9/fcg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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/nouveau/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, struct file *file) return single_open(file, nouveau_debugfs_pstate_get, inode->i_private); } +static int +nouveau_debugfs_gpuva(struct seq_file *m, void *data) +{ + struct drm_info_node *node = (struct drm_info_node *) m->private; + struct nouveau_drm *drm = 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 = 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 = { .owner = THIS_MODULE, .open = nouveau_debugfs_pstate_open, @@ -213,6 +236,7 @@ static const struct file_operations nouveau_pstate_fops = { static struct drm_info_list nouveau_debugfs_list[] = { { "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)