// SPDX-License-Identifier: GPL-2.0 OR MIT12#include <drm/drm_exec.h>3#include <drm/drm_gem.h>45#include <linux/dma-resv.h>6#include <linux/export.h>78/**9* DOC: Overview10*11* This component mainly abstracts the retry loop necessary for locking12* multiple GEM objects while preparing hardware operations (e.g. command13* submissions, page table updates etc..).14*15* If a contention is detected while locking a GEM object the cleanup procedure16* unlocks all previously locked GEM objects and locks the contended one first17* before locking any further objects.18*19* After an object is locked fences slots can optionally be reserved on the20* dma_resv object inside the GEM object.21*22* A typical usage pattern should look like this::23*24* struct drm_gem_object *obj;25* struct drm_exec exec;26* unsigned long index;27* int ret;28*29* drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT);30* drm_exec_until_all_locked(&exec) {31* ret = drm_exec_prepare_obj(&exec, boA, 1);32* drm_exec_retry_on_contention(&exec);33* if (ret)34* goto error;35*36* ret = drm_exec_prepare_obj(&exec, boB, 1);37* drm_exec_retry_on_contention(&exec);38* if (ret)39* goto error;40* }41*42* drm_exec_for_each_locked_object(&exec, index, obj) {43* dma_resv_add_fence(obj->resv, fence, DMA_RESV_USAGE_READ);44* ...45* }46* drm_exec_fini(&exec);47*48* See struct dma_exec for more details.49*/5051/* Dummy value used to initially enter the retry loop */52#define DRM_EXEC_DUMMY ((void *)~0)5354/* Unlock all objects and drop references */55static void drm_exec_unlock_all(struct drm_exec *exec)56{57struct drm_gem_object *obj;58unsigned long index;5960drm_exec_for_each_locked_object_reverse(exec, index, obj) {61dma_resv_unlock(obj->resv);62drm_gem_object_put(obj);63}6465drm_gem_object_put(exec->prelocked);66exec->prelocked = NULL;67}6869/**70* drm_exec_init - initialize a drm_exec object71* @exec: the drm_exec object to initialize72* @flags: controls locking behavior, see DRM_EXEC_* defines73* @nr: the initial # of objects74*75* Initialize the object and make sure that we can track locked objects.76*77* If nr is non-zero then it is used as the initial objects table size.78* In either case, the table will grow (be re-allocated) on demand.79*/80void drm_exec_init(struct drm_exec *exec, u32 flags, unsigned nr)81{82if (!nr)83nr = PAGE_SIZE / sizeof(void *);8485exec->flags = flags;86exec->objects = kvmalloc_array(nr, sizeof(void *), GFP_KERNEL);8788/* If allocation here fails, just delay that till the first use */89exec->max_objects = exec->objects ? nr : 0;90exec->num_objects = 0;91exec->contended = DRM_EXEC_DUMMY;92exec->prelocked = NULL;93}94EXPORT_SYMBOL(drm_exec_init);9596/**97* drm_exec_fini - finalize a drm_exec object98* @exec: the drm_exec object to finalize99*100* Unlock all locked objects, drop the references to objects and free all memory101* used for tracking the state.102*/103void drm_exec_fini(struct drm_exec *exec)104{105drm_exec_unlock_all(exec);106kvfree(exec->objects);107if (exec->contended != DRM_EXEC_DUMMY) {108drm_gem_object_put(exec->contended);109ww_acquire_fini(&exec->ticket);110}111}112EXPORT_SYMBOL(drm_exec_fini);113114/**115* drm_exec_cleanup - cleanup when contention is detected116* @exec: the drm_exec object to cleanup117*118* Cleanup the current state and return true if we should stay inside the retry119* loop, false if there wasn't any contention detected and we can keep the120* objects locked.121*/122bool drm_exec_cleanup(struct drm_exec *exec)123{124if (likely(!exec->contended)) {125ww_acquire_done(&exec->ticket);126return false;127}128129if (likely(exec->contended == DRM_EXEC_DUMMY)) {130exec->contended = NULL;131ww_acquire_init(&exec->ticket, &reservation_ww_class);132return true;133}134135drm_exec_unlock_all(exec);136exec->num_objects = 0;137return true;138}139EXPORT_SYMBOL(drm_exec_cleanup);140141/* Track the locked object in the array */142static int drm_exec_obj_locked(struct drm_exec *exec,143struct drm_gem_object *obj)144{145if (unlikely(exec->num_objects == exec->max_objects)) {146size_t size = exec->max_objects * sizeof(void *);147void *tmp;148149tmp = kvrealloc(exec->objects, size + PAGE_SIZE, GFP_KERNEL);150if (!tmp)151return -ENOMEM;152153exec->objects = tmp;154exec->max_objects += PAGE_SIZE / sizeof(void *);155}156drm_gem_object_get(obj);157exec->objects[exec->num_objects++] = obj;158159return 0;160}161162/* Make sure the contended object is locked first */163static int drm_exec_lock_contended(struct drm_exec *exec)164{165struct drm_gem_object *obj = exec->contended;166int ret;167168if (likely(!obj))169return 0;170171/* Always cleanup the contention so that error handling can kick in */172exec->contended = NULL;173if (exec->flags & DRM_EXEC_INTERRUPTIBLE_WAIT) {174ret = dma_resv_lock_slow_interruptible(obj->resv,175&exec->ticket);176if (unlikely(ret))177goto error_dropref;178} else {179dma_resv_lock_slow(obj->resv, &exec->ticket);180}181182ret = drm_exec_obj_locked(exec, obj);183if (unlikely(ret))184goto error_unlock;185186exec->prelocked = obj;187return 0;188189error_unlock:190dma_resv_unlock(obj->resv);191192error_dropref:193drm_gem_object_put(obj);194return ret;195}196197/**198* drm_exec_lock_obj - lock a GEM object for use199* @exec: the drm_exec object with the state200* @obj: the GEM object to lock201*202* Lock a GEM object for use and grab a reference to it.203*204* Returns: -EDEADLK if a contention is detected, -EALREADY when object is205* already locked (can be suppressed by setting the DRM_EXEC_IGNORE_DUPLICATES206* flag), -ENOMEM when memory allocation failed and zero for success.207*/208int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj)209{210int ret;211212ret = drm_exec_lock_contended(exec);213if (unlikely(ret))214return ret;215216if (exec->prelocked == obj) {217drm_gem_object_put(exec->prelocked);218exec->prelocked = NULL;219return 0;220}221222if (exec->flags & DRM_EXEC_INTERRUPTIBLE_WAIT)223ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket);224else225ret = dma_resv_lock(obj->resv, &exec->ticket);226227if (unlikely(ret == -EDEADLK)) {228drm_gem_object_get(obj);229exec->contended = obj;230return -EDEADLK;231}232233if (unlikely(ret == -EALREADY) &&234exec->flags & DRM_EXEC_IGNORE_DUPLICATES)235return 0;236237if (unlikely(ret))238return ret;239240ret = drm_exec_obj_locked(exec, obj);241if (ret)242goto error_unlock;243244return 0;245246error_unlock:247dma_resv_unlock(obj->resv);248return ret;249}250EXPORT_SYMBOL(drm_exec_lock_obj);251252/**253* drm_exec_unlock_obj - unlock a GEM object in this exec context254* @exec: the drm_exec object with the state255* @obj: the GEM object to unlock256*257* Unlock the GEM object and remove it from the collection of locked objects.258* Should only be used to unlock the most recently locked objects. It's not time259* efficient to unlock objects locked long ago.260*/261void drm_exec_unlock_obj(struct drm_exec *exec, struct drm_gem_object *obj)262{263unsigned int i;264265for (i = exec->num_objects; i--;) {266if (exec->objects[i] == obj) {267dma_resv_unlock(obj->resv);268for (++i; i < exec->num_objects; ++i)269exec->objects[i - 1] = exec->objects[i];270--exec->num_objects;271drm_gem_object_put(obj);272return;273}274275}276}277EXPORT_SYMBOL(drm_exec_unlock_obj);278279/**280* drm_exec_prepare_obj - prepare a GEM object for use281* @exec: the drm_exec object with the state282* @obj: the GEM object to prepare283* @num_fences: how many fences to reserve284*285* Prepare a GEM object for use by locking it and reserving fence slots.286*287* Returns: -EDEADLK if a contention is detected, -EALREADY when object is288* already locked, -ENOMEM when memory allocation failed and zero for success.289*/290int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,291unsigned int num_fences)292{293int ret;294295ret = drm_exec_lock_obj(exec, obj);296if (ret)297return ret;298299ret = dma_resv_reserve_fences(obj->resv, num_fences);300if (ret) {301drm_exec_unlock_obj(exec, obj);302return ret;303}304305return 0;306}307EXPORT_SYMBOL(drm_exec_prepare_obj);308309/**310* drm_exec_prepare_array - helper to prepare an array of objects311* @exec: the drm_exec object with the state312* @objects: array of GEM object to prepare313* @num_objects: number of GEM objects in the array314* @num_fences: number of fences to reserve on each GEM object315*316* Prepares all GEM objects in an array, aborts on first error.317* Reserves @num_fences on each GEM object after locking it.318*319* Returns: -EDEADLOCK on contention, -EALREADY when object is already locked,320* -ENOMEM when memory allocation failed and zero for success.321*/322int drm_exec_prepare_array(struct drm_exec *exec,323struct drm_gem_object **objects,324unsigned int num_objects,325unsigned int num_fences)326{327int ret;328329for (unsigned int i = 0; i < num_objects; ++i) {330ret = drm_exec_prepare_obj(exec, objects[i], num_fences);331if (unlikely(ret))332return ret;333}334335return 0;336}337EXPORT_SYMBOL(drm_exec_prepare_array);338339MODULE_DESCRIPTION("DRM execution context");340MODULE_LICENSE("Dual MIT/GPL");341342343