From mboxrd@z Thu Jan 1 00:00:00 1970 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 2982FC76188 for ; Tue, 4 Apr 2023 01:27:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75C416B0074; Mon, 3 Apr 2023 21:27:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 70C086B0075; Mon, 3 Apr 2023 21:27:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 55F8C6B0078; Mon, 3 Apr 2023 21:27:56 -0400 (EDT) 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 433DD6B0074 for ; Mon, 3 Apr 2023 21:27:56 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1DB33AC017 for ; Tue, 4 Apr 2023 01:27:56 +0000 (UTC) X-FDA: 80641972152.11.07B0FEA Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf30.hostedemail.com (Postfix) with ESMTP id DFF7F8001F for ; Tue, 4 Apr 2023 01:27:53 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eb+A2uvD; spf=pass (imf30.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=1680571674; 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=uFCRMKUSTUQC7wfjXmYlkuwwGxw7y+VdboflsRIya48=; b=KNI761k1T5DLP9DkYBfgLFOa7Fcu+K/W6DGLjkSvVB7/IusMuayqobW2QhdSNNp5OTxIrA GHkCEOJNMGWHIGIsCE5vaRpSXYh+wNzcfSS5+WvQV7/Rzs2x2HxI4VcSJXaKUO8A5tcZst ThonK5ZeVYqzuonf7+2Fhb2fCRmXVlw= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eb+A2uvD; spf=pass (imf30.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=1680571674; a=rsa-sha256; cv=none; b=xhd8dnk/RH2FcSYSSyWNMSXprSjRgZZrOUvZpltmpFFJpqEgcpBTSbtiU7guE9UhDuLLJN sPugdchG8781ZUTLXhx+B7BGqvhjhgf9JQCRmu41kGVdIDOITa6406SzdDkJcZ+mtly9M1 t2okXwHTyobp7xZDKS9zC+yk3O8LIUI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680571673; 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=uFCRMKUSTUQC7wfjXmYlkuwwGxw7y+VdboflsRIya48=; b=eb+A2uvDGPCZlPdNW7kMzA8jcX8Le0uWSAIEz19f7WFZKG7Qegpx+H0t9jKURJmqrifAdN rJTHRqFRWTfK6UGlYA0sxELv14oRjO3RC3GrCC0LRj1XmCunbdoOYkkxYMsGO3d21wVUqq E1YwD/bXPJuF9fgkBzpTnaauVFigVi0= 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_256_GCM_SHA384) id us-mta-518-j1lAPxXENKaQ6h9Usyrx0g-1; Mon, 03 Apr 2023 21:27:51 -0400 X-MC-Unique: j1lAPxXENKaQ6h9Usyrx0g-1 Received: by mail-ed1-f71.google.com with SMTP id i42-20020a0564020f2a00b004fd23c238beso43499133eda.0 for ; Mon, 03 Apr 2023 18:27:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680571670; 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=uFCRMKUSTUQC7wfjXmYlkuwwGxw7y+VdboflsRIya48=; b=ZKIegdjzKVwvAFlKxGFPQ5j7mdq5Qqvwnn071/1D27DG80RLKzai6f9FC2hbbtn9fr Eqs26F4kmL5qlwvyWNd/jqO96QoCLxlwbcbXDnzzGRzhOquGw/eO3jrAb/aXW1m+vlgm 7gn/5rYuXUvx++HHZsJQd8DKqKOe9ua3D1FQwQakPEukMB3g4y50krbMas4z2YOI/QNX SmVgzjJqK8E/V5MCtZvoj5kp+AiRIX+odqSYtB7x8+oF3PRRSs0cJPPfwMmCLxvvMZcp jQ6PcLbSsY2iXD4wYJ9EU4L4wV7vA5vUCAg9AmsB8raiNW5slLsxzhWmIeuMEpi9o+jc vmFQ== X-Gm-Message-State: AAQBX9c6c0/qPFKrGE7Q04L+7LPsp3EBhBDpSuA4qb+tBZuw/T+xkvRL nNIYbT5dv30pzgihRHlJF3S+sBWvJle36mb4sZ+chNEzpK6LoMJNITadrMjBA39/sNJ+4TQLqFQ SBkxgS2qlg+M= X-Received: by 2002:a17:907:74d:b0:939:e928:5323 with SMTP id xc13-20020a170907074d00b00939e9285323mr500519ejb.54.1680571670529; Mon, 03 Apr 2023 18:27:50 -0700 (PDT) X-Google-Smtp-Source: AKy350bpom8PYB2jg7n423eyuxOSQlrserIJUHg68ICIuVkaeAj6jyTGKiosBhLLovPvVZCnGClvXQ== X-Received: by 2002:a17:907:74d:b0:939:e928:5323 with SMTP id xc13-20020a170907074d00b00939e9285323mr500505ejb.54.1680571670239; Mon, 03 Apr 2023 18:27:50 -0700 (PDT) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id h23-20020a170906261700b008e0bb004976sm5183061ejc.134.2023.04.03.18.27.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Apr 2023 18:27:49 -0700 (PDT) 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, =?UTF-8?q?Christian=20K=C3=B6nig?= Subject: [PATCH drm-next v3 01/15] drm: execution context for GEM buffers v3 Date: Tue, 4 Apr 2023 03:27:27 +0200 Message-Id: <20230404012741.116502-2-dakr@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230404012741.116502-1-dakr@redhat.com> References: <20230404012741.116502-1-dakr@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DFF7F8001F X-Rspam-User: X-Stat-Signature: wzf76afkg8ctjt3yytuwfs4brni5qjy3 X-HE-Tag: 1680571673-119470 X-HE-Meta: U2FsdGVkX1+MjXMGuMUZ9MvdKhA3xAMqZo9qZQmanFAOfMizvIp/zRyEQ4iEPJZNqSovpnOOkPn/Q+VDuCh0/4e8T68z9tyTEFh2tVPLnyZdSVmAQMjQPmKDzVLMsMs2+S5VcalN4KnH0xLbbJ1ZSmkxovhNfxuyfifpRmeuTr0F06c47EGfYTsf7oni7M5MiKU+7ofWoHaMsTo0W+oRWfK0tERrlG896CwCEZADiNUerE828KR9oailEEYmITXRBrbhIxyTD4j9Sxe6CNSReMk6749JNNesjHmhF+fDb5JWpD2knyjAZly3eb6S/vhDFyhHY0JOHFwPCYIkAdudlRItH0f4Iy5xyy5eafCiVAVRtKipug4Vw0g2kDB5rxBTIXNHdgk88xf5NtKMEBr5ticEWVmYm0G3UeWooXGbJ9kDi0CT1VO/yfsitRa3Ruh34Zm2oQULPndx0jfTMG+Ox0oQ7K9kfat/H9kyqokgesY5px3WYkmtSULmWWTP/yCuQ4cDEKlb/+Sn1nOzfl/1abmO4b042L+z5BvnM7yySrHbTmUdJrKZh/zGP9L9hRwD4WNCn0Bw5bbgk+OOuOlUEp8NTU6TA9JTlBdy0dNM/mpDSsGcjQ5rEU6Cs2CkcKAAj2666LFqcnC9Vgr8N8KYHTQzVLcjFps0MlTYGJui8qMLO7A4RrHWhddd9INDDYWGUgSHS6sCoZMudZp9LmDueq/X1tAkVF1piFzNrh8hPIDc/E+/ujzPPvQ9siomifgcv54YhPi83YWMuPwjnSaYubUQW2rYfyjMhpdj2rBuZMn34BgGj8lSBdUvrrrjDUfY7Dwm6mP8jvBz1n3OxcijOKteO+yWLamltXlXKxW5kzjxsrxHgTpHb9KRnvFl9jQkw16lGmaX3e670jTTumPNEsY47YwyG9+Uigwa5yy89W0N6qy77JfZxo6nUuxaVwQOwh4cp5KEo0ECxQX9Mpp Tpy6W0mM X2Vv3TiFI3DC5DCpgnUNuJoIgBsIqjcEwEcMEVi0EYCe1ht5GZhRWbr7D7xpFws6PjTYNU8EweqR2Axn+4sXoGAbGHt77lEV80yfq3HQzv2t7dN2f8zwXr0bYoy7tK0MGOm6nArcixsQ5rInIdv3DC5wlrgQDCPzTzlpN/ws5M0kq30CLSBLUQbUjZaAZgH3pN9PxZmBDikQdVMuqezdNTfG1S54rbSmWJ5vw333NJSASrcDE6u1rMJ9ARC6eWveWYrEa/Iq+GKtKsFUFvuuhfnDy4AGkNrzmetkHDXyTCw0L39UKvIdcCQNKjEtSYKw3t0ZwCKcM/knaNgBXqweXOqV0xb6uSUksfOe1m8ubSaGhE7FvB90vikhqyb/4/kQg5JYLMkACEU5ydLsczqSB+WmwE5AT3yc9gG0oSZfleL80lErfAkIlGmhBt1in60tcN72+i+WdBXksAl4RYGmx3R7KSJCdro5jboWUQyh8ymBXdVIRPBLg6J6aj5aQP1ugKgudz4FKQ/2FIcKYbFEJCnVNWSw9CmSLNYa0 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. v3: drop duplicate tracking, radeon is really the only one needing that. 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/drm_exec.c | 249 +++++++++++++++++++++++++++++++++++ include/drm/drm_exec.h | 115 ++++++++++++++++ 5 files changed, 384 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 ba3fb04bb691..2dc81eb062eb 100644 --- a/drivers/gpu/drm/Kconfig +++ b/drivers/gpu/drm/Kconfig @@ -201,6 +201,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 a33257d2bc7f..9c6446eb3c83 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/drm_exec.c b/drivers/gpu/drm/drm_exec.c new file mode 100644 index 000000000000..df546cc5a227 --- /dev/null +++ b/drivers/gpu/drm/drm_exec.c @@ -0,0 +1,249 @@ +/* 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 + +/* 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_locked_object(exec, index, obj) { + dma_resv_unlock(obj->resv); + drm_gem_object_put(obj); + } + + if (exec->prelocked) { + dma_resv_unlock(exec->prelocked->resv); + drm_gem_object_put(exec->prelocked); + exec->prelocked = NULL; + } +} + +/** + * 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; + exec->objects = kmalloc(PAGE_SIZE, GFP_KERNEL); + + /* If allocation here fails, just delay that till the first use */ + exec->max_objects = exec->objects ? PAGE_SIZE / sizeof(void *) : 0; + exec->num_objects = 0; + exec->contended = DRM_EXEC_DUMMY; + exec->prelocked = NULL; +} +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); + kvfree(exec->objects); + 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->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 *exec, + struct drm_gem_object *obj) +{ + if (unlikely(exec->num_objects == exec->max_objects)) { + size_t size = exec->max_objects * sizeof(void *); + void *tmp; + + tmp = kvrealloc(exec->objects, size, size + PAGE_SIZE, + GFP_KERNEL); + if (!tmp) + return -ENOMEM; + + exec->objects = tmp; + exec->max_objects += PAGE_SIZE / sizeof(void *); + } + drm_gem_object_get(obj); + exec->objects[exec->num_objects++] = obj; + + return 0; +} + +/* 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, obj); + if (unlikely(ret)) { + dma_resv_unlock(obj->resv); + goto error_dropref; + } + + swap(exec->prelocked, obj); + +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->prelocked == obj) { + drm_gem_object_put(exec->prelocked); + exec->prelocked = NULL; + + return dma_resv_reserve_fences(obj->resv, num_fences); + } + + 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)) + return ret; + + ret = drm_exec_obj_locked(exec, obj); + if (ret) + goto error_unlock; + + /* Keep locked when reserving fences fails */ + return dma_resv_reserve_fences(obj->resv, num_fences); + +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..65e518c01db3 --- /dev/null +++ b/include/drm/drm_exec.h @@ -0,0 +1,115 @@ +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ + +#ifndef __DRM_EXEC_H__ +#define __DRM_EXEC_H__ + +#include + +struct drm_gem_object; + +/** + * 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; + + /** + * @num_objects: number of objects locked + */ + unsigned int num_objects; + + /** + * @max_objects: maximum objects in array + */ + unsigned int max_objects; + + /** + * @objects: array of the locked objects + */ + struct drm_gem_object **objects; + + /** + * @contended: contended GEM object we backet of for + */ + struct drm_gem_object *contended; + + /** + * @prelocked: already locked GEM object because of contention + */ + struct drm_gem_object *prelocked; +}; + +/** + * 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) \ + for (index = 0, obj = (exec)->objects[0]; \ + index < (exec)->num_objects; \ + ++index, obj = (exec)->objects[index]) + +/** + * 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; +} + +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 -- 2.39.2