1673a394bSEric Anholt /*
2673a394bSEric Anholt * Copyright © 2008 Intel Corporation
3673a394bSEric Anholt *
4673a394bSEric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
5673a394bSEric Anholt * copy of this software and associated documentation files (the "Software"),
6673a394bSEric Anholt * to deal in the Software without restriction, including without limitation
7673a394bSEric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8673a394bSEric Anholt * and/or sell copies of the Software, and to permit persons to whom the
9673a394bSEric Anholt * Software is furnished to do so, subject to the following conditions:
10673a394bSEric Anholt *
11673a394bSEric Anholt * The above copyright notice and this permission notice (including the next
12673a394bSEric Anholt * paragraph) shall be included in all copies or substantial portions of the
13673a394bSEric Anholt * Software.
14673a394bSEric Anholt *
15673a394bSEric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16673a394bSEric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17673a394bSEric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18673a394bSEric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19673a394bSEric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20673a394bSEric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21673a394bSEric Anholt * IN THE SOFTWARE.
22673a394bSEric Anholt *
23673a394bSEric Anholt * Authors:
24673a394bSEric Anholt * Eric Anholt <eric@anholt.net>
25673a394bSEric Anholt *
26673a394bSEric Anholt */
27673a394bSEric Anholt
287994369fSLucas De Marchi #include <linux/dma-buf.h>
297994369fSLucas De Marchi #include <linux/file.h>
307994369fSLucas De Marchi #include <linux/fs.h>
317938f421SLucas De Marchi #include <linux/iosys-map.h>
3295cf9264STom Lendacky #include <linux/mem_encrypt.h>
337994369fSLucas De Marchi #include <linux/mm.h>
347994369fSLucas De Marchi #include <linux/mman.h>
357994369fSLucas De Marchi #include <linux/module.h>
367994369fSLucas De Marchi #include <linux/pagemap.h>
37fb4b4927SKuo-Hsin Yang #include <linux/pagevec.h>
387994369fSLucas De Marchi #include <linux/shmem_fs.h>
397994369fSLucas De Marchi #include <linux/slab.h>
40b8c75bd9SLucas De Marchi #include <linux/string_helpers.h>
417994369fSLucas De Marchi #include <linux/types.h>
427994369fSLucas De Marchi #include <linux/uaccess.h>
430500c04eSSam Ravnborg
441c535876SSam Ravnborg #include <drm/drm.h>
450500c04eSSam Ravnborg #include <drm/drm_device.h>
460500c04eSSam Ravnborg #include <drm/drm_drv.h>
470500c04eSSam Ravnborg #include <drm/drm_file.h>
48d9fc9413SDaniel Vetter #include <drm/drm_gem.h>
49641b9103SDaniel Vetter #include <drm/drm_managed.h>
5045d58b40SNoralf Trønnes #include <drm/drm_print.h>
510500c04eSSam Ravnborg #include <drm/drm_vma_manager.h>
520500c04eSSam Ravnborg
5367d0ec4eSDaniel Vetter #include "drm_internal.h"
54673a394bSEric Anholt
55673a394bSEric Anholt /** @file drm_gem.c
56673a394bSEric Anholt *
57673a394bSEric Anholt * This file provides some of the base ioctls and library routines for
58673a394bSEric Anholt * the graphics memory manager implemented by each device driver.
59673a394bSEric Anholt *
60673a394bSEric Anholt * Because various devices have different requirements in terms of
61673a394bSEric Anholt * synchronization and migration strategies, implementing that is left up to
62673a394bSEric Anholt * the driver, and all that the general API provides should be generic --
63673a394bSEric Anholt * allocating objects, reading/writing data with the cpu, freeing objects.
64673a394bSEric Anholt * Even there, platform-dependent optimizations for reading/writing data with
65673a394bSEric Anholt * the CPU mean we'll likely hook those out to driver-specific calls. However,
66673a394bSEric Anholt * the DRI2 implementation wants to have at least allocate/mmap be generic.
67673a394bSEric Anholt *
68673a394bSEric Anholt * The goal was to have swap-backed object allocation managed through
69673a394bSEric Anholt * struct file. However, file descriptors as handles to a struct file have
70673a394bSEric Anholt * two major failings:
71673a394bSEric Anholt * - Process limits prevent more than 1024 or so being used at a time by
72673a394bSEric Anholt * default.
73673a394bSEric Anholt * - Inability to allocate high fds will aggravate the X Server's select()
74673a394bSEric Anholt * handling, and likely that of many GL client applications as well.
75673a394bSEric Anholt *
76673a394bSEric Anholt * This led to a plan of using our own integer IDs (called handles, following
77673a394bSEric Anholt * DRM terminology) to mimic fds, and implement the fd syscalls we need as
78673a394bSEric Anholt * ioctls. The objects themselves will still include the struct file so
79673a394bSEric Anholt * that we can transition to fds if the required kernel infrastructure shows
80673a394bSEric Anholt * up at a later date, and as our interface with shmfs for memory allocation.
81673a394bSEric Anholt */
82673a394bSEric Anholt
83641b9103SDaniel Vetter static void
drm_gem_init_release(struct drm_device * dev,void * ptr)84641b9103SDaniel Vetter drm_gem_init_release(struct drm_device *dev, void *ptr)
85641b9103SDaniel Vetter {
86641b9103SDaniel Vetter drm_vma_offset_manager_destroy(dev->vma_offset_manager);
87641b9103SDaniel Vetter }
88641b9103SDaniel Vetter
89673a394bSEric Anholt /**
9089d61fc0SDaniel Vetter * drm_gem_init - Initialize the GEM device fields
9189d61fc0SDaniel Vetter * @dev: drm_devic structure to initialize
92673a394bSEric Anholt */
93673a394bSEric Anholt int
drm_gem_init(struct drm_device * dev)94673a394bSEric Anholt drm_gem_init(struct drm_device *dev)
95673a394bSEric Anholt {
96b04a5906SDaniel Vetter struct drm_vma_offset_manager *vma_offset_manager;
97a2c0a97bSJesse Barnes
98cd4f013fSDaniel Vetter mutex_init(&dev->object_name_lock);
99e86584c5SChris Wilson idr_init_base(&dev->object_name_idr, 1);
100a2c0a97bSJesse Barnes
101641b9103SDaniel Vetter vma_offset_manager = drmm_kzalloc(dev, sizeof(*vma_offset_manager),
102641b9103SDaniel Vetter GFP_KERNEL);
103b04a5906SDaniel Vetter if (!vma_offset_manager) {
104a2c0a97bSJesse Barnes DRM_ERROR("out of memory\n");
105a2c0a97bSJesse Barnes return -ENOMEM;
106a2c0a97bSJesse Barnes }
107a2c0a97bSJesse Barnes
108b04a5906SDaniel Vetter dev->vma_offset_manager = vma_offset_manager;
109b04a5906SDaniel Vetter drm_vma_offset_manager_init(vma_offset_manager,
1100de23977SDavid Herrmann DRM_FILE_PAGE_OFFSET_START,
11177ef8bbcSDavid Herrmann DRM_FILE_PAGE_OFFSET_SIZE);
112a2c0a97bSJesse Barnes
113641b9103SDaniel Vetter return drmm_add_action(dev, drm_gem_init_release, NULL);
114a2c0a97bSJesse Barnes }
115a2c0a97bSJesse Barnes
116673a394bSEric Anholt /**
11789d61fc0SDaniel Vetter * drm_gem_object_init - initialize an allocated shmem-backed GEM object
11889d61fc0SDaniel Vetter * @dev: drm_device the object should be initialized for
11989d61fc0SDaniel Vetter * @obj: drm_gem_object to initialize
12089d61fc0SDaniel Vetter * @size: object size
12189d61fc0SDaniel Vetter *
12262cb7011SAlan Cox * Initialize an already allocated GEM object of the specified size with
1231d397043SDaniel Vetter * shmfs backing store.
1241d397043SDaniel Vetter */
drm_gem_object_init(struct drm_device * dev,struct drm_gem_object * obj,size_t size)1251d397043SDaniel Vetter int drm_gem_object_init(struct drm_device *dev,
1261d397043SDaniel Vetter struct drm_gem_object *obj, size_t size)
1271d397043SDaniel Vetter {
12889c8233fSDavid Herrmann struct file *filp;
1291d397043SDaniel Vetter
1306ab11a26SDaniel Vetter drm_gem_private_object_init(dev, obj, size);
1316ab11a26SDaniel Vetter
13289c8233fSDavid Herrmann filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
13389c8233fSDavid Herrmann if (IS_ERR(filp))
13489c8233fSDavid Herrmann return PTR_ERR(filp);
1351d397043SDaniel Vetter
13689c8233fSDavid Herrmann obj->filp = filp;
1371d397043SDaniel Vetter
1381d397043SDaniel Vetter return 0;
1391d397043SDaniel Vetter }
1401d397043SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_init);
1411d397043SDaniel Vetter
1421d397043SDaniel Vetter /**
1432a5706a3SLaurent Pinchart * drm_gem_private_object_init - initialize an allocated private GEM object
14489d61fc0SDaniel Vetter * @dev: drm_device the object should be initialized for
14589d61fc0SDaniel Vetter * @obj: drm_gem_object to initialize
14689d61fc0SDaniel Vetter * @size: object size
14789d61fc0SDaniel Vetter *
14862cb7011SAlan Cox * Initialize an already allocated GEM object of the specified size with
14962cb7011SAlan Cox * no GEM provided backing store. Instead the caller is responsible for
15062cb7011SAlan Cox * backing the object and handling it.
15162cb7011SAlan Cox */
drm_gem_private_object_init(struct drm_device * dev,struct drm_gem_object * obj,size_t size)15289c8233fSDavid Herrmann void drm_gem_private_object_init(struct drm_device *dev,
15362cb7011SAlan Cox struct drm_gem_object *obj, size_t size)
15462cb7011SAlan Cox {
15562cb7011SAlan Cox BUG_ON((size & (PAGE_SIZE - 1)) != 0);
15662cb7011SAlan Cox
15762cb7011SAlan Cox obj->dev = dev;
15862cb7011SAlan Cox obj->filp = NULL;
15962cb7011SAlan Cox
16062cb7011SAlan Cox kref_init(&obj->refcount);
161a8e11d1cSDaniel Vetter obj->handle_count = 0;
16262cb7011SAlan Cox obj->size = size;
16352791eeeSChristian König dma_resv_init(&obj->_resv);
1641ba62714SRob Herring if (!obj->resv)
1651ba62714SRob Herring obj->resv = &obj->_resv;
1661ba62714SRob Herring
167e6303f32SDanilo Krummrich if (drm_core_check_feature(dev, DRIVER_GEM_GPUVA))
168e6303f32SDanilo Krummrich drm_gem_gpuva_init(obj);
169e6303f32SDanilo Krummrich
17088d7ebe5SDavid Herrmann drm_vma_node_reset(&obj->vma_node);
171e7c2af13SRob Clark INIT_LIST_HEAD(&obj->lru_node);
17262cb7011SAlan Cox }
17362cb7011SAlan Cox EXPORT_SYMBOL(drm_gem_private_object_init);
17462cb7011SAlan Cox
17536da5908SDaniel Vetter /**
1767df34a61SChunyouTang * drm_gem_private_object_fini - Finalize a failed drm_gem_object
1777df34a61SChunyouTang * @obj: drm_gem_object
1787df34a61SChunyouTang *
1797df34a61SChunyouTang * Uninitialize an already allocated GEM object when it initialized failed
1807df34a61SChunyouTang */
drm_gem_private_object_fini(struct drm_gem_object * obj)1817df34a61SChunyouTang void drm_gem_private_object_fini(struct drm_gem_object *obj)
1827df34a61SChunyouTang {
1837df34a61SChunyouTang WARN_ON(obj->dma_buf);
1847df34a61SChunyouTang
1857df34a61SChunyouTang dma_resv_fini(&obj->_resv);
1867df34a61SChunyouTang }
1877df34a61SChunyouTang EXPORT_SYMBOL(drm_gem_private_object_fini);
1887df34a61SChunyouTang
1897df34a61SChunyouTang /**
190c6a84325SThierry Reding * drm_gem_object_handle_free - release resources bound to userspace handles
19189d61fc0SDaniel Vetter * @obj: GEM object to clean up.
19289d61fc0SDaniel Vetter *
19336da5908SDaniel Vetter * Called after the last handle to the object has been closed
19436da5908SDaniel Vetter *
19536da5908SDaniel Vetter * Removes any name for the object. Note that this must be
19636da5908SDaniel Vetter * called before drm_gem_object_free or we'll be touching
19736da5908SDaniel Vetter * freed memory
19836da5908SDaniel Vetter */
drm_gem_object_handle_free(struct drm_gem_object * obj)19936da5908SDaniel Vetter static void drm_gem_object_handle_free(struct drm_gem_object *obj)
20036da5908SDaniel Vetter {
20136da5908SDaniel Vetter struct drm_device *dev = obj->dev;
20236da5908SDaniel Vetter
20336da5908SDaniel Vetter /* Remove any name for this object */
20436da5908SDaniel Vetter if (obj->name) {
20536da5908SDaniel Vetter idr_remove(&dev->object_name_idr, obj->name);
20636da5908SDaniel Vetter obj->name = 0;
207a8e11d1cSDaniel Vetter }
20836da5908SDaniel Vetter }
20936da5908SDaniel Vetter
drm_gem_object_exported_dma_buf_free(struct drm_gem_object * obj)210319c933cSDaniel Vetter static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
211319c933cSDaniel Vetter {
212319c933cSDaniel Vetter /* Unbreak the reference cycle if we have an exported dma_buf. */
213319c933cSDaniel Vetter if (obj->dma_buf) {
214319c933cSDaniel Vetter dma_buf_put(obj->dma_buf);
215319c933cSDaniel Vetter obj->dma_buf = NULL;
216319c933cSDaniel Vetter }
217319c933cSDaniel Vetter }
218319c933cSDaniel Vetter
219becee2a5SDaniel Vetter static void
drm_gem_object_handle_put_unlocked(struct drm_gem_object * obj)220e6b62714SThierry Reding drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
22136da5908SDaniel Vetter {
22298a8883aSChris Wilson struct drm_device *dev = obj->dev;
22398a8883aSChris Wilson bool final = false;
22498a8883aSChris Wilson
2256afe6929SChris Wilson if (WARN_ON(READ_ONCE(obj->handle_count) == 0))
22636da5908SDaniel Vetter return;
22736da5908SDaniel Vetter
22836da5908SDaniel Vetter /*
22936da5908SDaniel Vetter * Must bump handle count first as this may be the last
23036da5908SDaniel Vetter * ref, in which case the object would disappear before we
23136da5908SDaniel Vetter * checked for a name
23236da5908SDaniel Vetter */
23336da5908SDaniel Vetter
23498a8883aSChris Wilson mutex_lock(&dev->object_name_lock);
235319c933cSDaniel Vetter if (--obj->handle_count == 0) {
23636da5908SDaniel Vetter drm_gem_object_handle_free(obj);
237319c933cSDaniel Vetter drm_gem_object_exported_dma_buf_free(obj);
23898a8883aSChris Wilson final = true;
239319c933cSDaniel Vetter }
24098a8883aSChris Wilson mutex_unlock(&dev->object_name_lock);
241a8e11d1cSDaniel Vetter
24298a8883aSChris Wilson if (final)
243be6ee102SEmil Velikov drm_gem_object_put(obj);
24436da5908SDaniel Vetter }
24536da5908SDaniel Vetter
2468815b23aSChris Wilson /*
2478815b23aSChris Wilson * Called at device or object close to release the file's
2488815b23aSChris Wilson * handle references on objects.
2498815b23aSChris Wilson */
2508815b23aSChris Wilson static int
drm_gem_object_release_handle(int id,void * ptr,void * data)2518815b23aSChris Wilson drm_gem_object_release_handle(int id, void *ptr, void *data)
2528815b23aSChris Wilson {
2538815b23aSChris Wilson struct drm_file *file_priv = data;
2548815b23aSChris Wilson struct drm_gem_object *obj = ptr;
2558815b23aSChris Wilson
256d693def4SThomas Zimmermann if (obj->funcs->close)
257b39b5394SNoralf Trønnes obj->funcs->close(obj, file_priv);
258d0a133f7SChris Wilson
259ea2aa97cSJeffy Chen drm_prime_remove_buf_handle(&file_priv->prime, id);
260d9a1f0b4SDavid Herrmann drm_vma_node_revoke(&obj->vma_node, file_priv);
2618815b23aSChris Wilson
262e6b62714SThierry Reding drm_gem_object_handle_put_unlocked(obj);
2638815b23aSChris Wilson
2648815b23aSChris Wilson return 0;
2658815b23aSChris Wilson }
2668815b23aSChris Wilson
267673a394bSEric Anholt /**
26889d61fc0SDaniel Vetter * drm_gem_handle_delete - deletes the given file-private handle
26989d61fc0SDaniel Vetter * @filp: drm file-private structure to use for the handle look up
27089d61fc0SDaniel Vetter * @handle: userspace handle to delete
27189d61fc0SDaniel Vetter *
272df2e0900SDaniel Vetter * Removes the GEM handle from the @filp lookup table which has been added with
273df2e0900SDaniel Vetter * drm_gem_handle_create(). If this is the last handle also cleans up linked
274df2e0900SDaniel Vetter * resources like GEM names.
275673a394bSEric Anholt */
276ff72145bSDave Airlie int
drm_gem_handle_delete(struct drm_file * filp,u32 handle)277a1a2d1d3SPekka Paalanen drm_gem_handle_delete(struct drm_file *filp, u32 handle)
278673a394bSEric Anholt {
279673a394bSEric Anholt struct drm_gem_object *obj;
280673a394bSEric Anholt
281673a394bSEric Anholt spin_lock(&filp->table_lock);
282673a394bSEric Anholt
283673a394bSEric Anholt /* Check if we currently have a reference on the object */
284f6cd7daeSChris Wilson obj = idr_replace(&filp->object_idr, NULL, handle);
285673a394bSEric Anholt spin_unlock(&filp->table_lock);
286f6cd7daeSChris Wilson if (IS_ERR_OR_NULL(obj))
287673a394bSEric Anholt return -EINVAL;
288673a394bSEric Anholt
289f6cd7daeSChris Wilson /* Release driver's reference and decrement refcount. */
290f6cd7daeSChris Wilson drm_gem_object_release_handle(handle, obj, filp);
291f6cd7daeSChris Wilson
292f6cd7daeSChris Wilson /* And finally make the handle available for future allocations. */
293f6cd7daeSChris Wilson spin_lock(&filp->table_lock);
294673a394bSEric Anholt idr_remove(&filp->object_idr, handle);
295673a394bSEric Anholt spin_unlock(&filp->table_lock);
296673a394bSEric Anholt
297673a394bSEric Anholt return 0;
298673a394bSEric Anholt }
299ff72145bSDave Airlie EXPORT_SYMBOL(drm_gem_handle_delete);
300673a394bSEric Anholt
301673a394bSEric Anholt /**
302abd4e745SRob Herring * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
303db611527SNoralf Trønnes * @file: drm file-private structure containing the gem object
304db611527SNoralf Trønnes * @dev: corresponding drm_device
305db611527SNoralf Trønnes * @handle: gem object handle
306db611527SNoralf Trønnes * @offset: return location for the fake mmap offset
307db611527SNoralf Trønnes *
308db611527SNoralf Trønnes * This implements the &drm_driver.dumb_map_offset kms driver callback for
309db611527SNoralf Trønnes * drivers which use gem to manage their backing storage.
310db611527SNoralf Trønnes *
311db611527SNoralf Trønnes * Returns:
312db611527SNoralf Trønnes * 0 on success or a negative error code on failure.
313db611527SNoralf Trønnes */
drm_gem_dumb_map_offset(struct drm_file * file,struct drm_device * dev,u32 handle,u64 * offset)314abd4e745SRob Herring int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
315db611527SNoralf Trønnes u32 handle, u64 *offset)
316db611527SNoralf Trønnes {
317db611527SNoralf Trønnes struct drm_gem_object *obj;
318db611527SNoralf Trønnes int ret;
319db611527SNoralf Trønnes
320db611527SNoralf Trønnes obj = drm_gem_object_lookup(file, handle);
321db611527SNoralf Trønnes if (!obj)
322db611527SNoralf Trønnes return -ENOENT;
323db611527SNoralf Trønnes
32490378e58SNoralf Trønnes /* Don't allow imported objects to be mapped */
32590378e58SNoralf Trønnes if (obj->import_attach) {
32690378e58SNoralf Trønnes ret = -EINVAL;
32790378e58SNoralf Trønnes goto out;
32890378e58SNoralf Trønnes }
32990378e58SNoralf Trønnes
330db611527SNoralf Trønnes ret = drm_gem_create_mmap_offset(obj);
331db611527SNoralf Trønnes if (ret)
332db611527SNoralf Trønnes goto out;
333db611527SNoralf Trønnes
334db611527SNoralf Trønnes *offset = drm_vma_node_offset_addr(&obj->vma_node);
335db611527SNoralf Trønnes out:
336be6ee102SEmil Velikov drm_gem_object_put(obj);
337db611527SNoralf Trønnes
338db611527SNoralf Trønnes return ret;
339db611527SNoralf Trønnes }
340abd4e745SRob Herring EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
341db611527SNoralf Trønnes
34243387b37SDaniel Vetter /**
34320228c44SDaniel Vetter * drm_gem_handle_create_tail - internal functions to create a handle
34489d61fc0SDaniel Vetter * @file_priv: drm file-private structure to register the handle for
34589d61fc0SDaniel Vetter * @obj: object to register
3468bf8180fSThierry Reding * @handlep: pointer to return the created handle to the caller
34720228c44SDaniel Vetter *
348940eba2dSDaniel Vetter * This expects the &drm_device.object_name_lock to be held already and will
349940eba2dSDaniel Vetter * drop it before returning. Used to avoid races in establishing new handles
350940eba2dSDaniel Vetter * when importing an object from either an flink name or a dma-buf.
351df2e0900SDaniel Vetter *
352df2e0900SDaniel Vetter * Handles must be release again through drm_gem_handle_delete(). This is done
353df2e0900SDaniel Vetter * when userspace closes @file_priv for all attached handles, or through the
354df2e0900SDaniel Vetter * GEM_CLOSE ioctl for individual handles.
355673a394bSEric Anholt */
356673a394bSEric Anholt int
drm_gem_handle_create_tail(struct drm_file * file_priv,struct drm_gem_object * obj,u32 * handlep)35720228c44SDaniel Vetter drm_gem_handle_create_tail(struct drm_file *file_priv,
358673a394bSEric Anholt struct drm_gem_object *obj,
359a1a2d1d3SPekka Paalanen u32 *handlep)
360673a394bSEric Anholt {
361304eda32SBen Skeggs struct drm_device *dev = obj->dev;
3629649399eSChris Wilson u32 handle;
363673a394bSEric Anholt int ret;
364673a394bSEric Anholt
36520228c44SDaniel Vetter WARN_ON(!mutex_is_locked(&dev->object_name_lock));
36698a8883aSChris Wilson if (obj->handle_count++ == 0)
367e6b62714SThierry Reding drm_gem_object_get(obj);
36820228c44SDaniel Vetter
369673a394bSEric Anholt /*
3702e928815STejun Heo * Get the user-visible handle using idr. Preload and perform
3712e928815STejun Heo * allocation under our spinlock.
372673a394bSEric Anholt */
3732e928815STejun Heo idr_preload(GFP_KERNEL);
374673a394bSEric Anholt spin_lock(&file_priv->table_lock);
3752e928815STejun Heo
3762e928815STejun Heo ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
37798a8883aSChris Wilson
378673a394bSEric Anholt spin_unlock(&file_priv->table_lock);
3792e928815STejun Heo idr_preload_end();
38098a8883aSChris Wilson
381cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock);
3826984128dSChris Wilson if (ret < 0)
3836984128dSChris Wilson goto err_unref;
3846984128dSChris Wilson
3859649399eSChris Wilson handle = ret;
386673a394bSEric Anholt
387d9a1f0b4SDavid Herrmann ret = drm_vma_node_allow(&obj->vma_node, file_priv);
3886984128dSChris Wilson if (ret)
3896984128dSChris Wilson goto err_remove;
390304eda32SBen Skeggs
391d693def4SThomas Zimmermann if (obj->funcs->open) {
392b39b5394SNoralf Trønnes ret = obj->funcs->open(obj, file_priv);
393b39b5394SNoralf Trønnes if (ret)
394b39b5394SNoralf Trønnes goto err_revoke;
395304eda32SBen Skeggs }
396304eda32SBen Skeggs
3979649399eSChris Wilson *handlep = handle;
398673a394bSEric Anholt return 0;
3996984128dSChris Wilson
4006984128dSChris Wilson err_revoke:
401d9a1f0b4SDavid Herrmann drm_vma_node_revoke(&obj->vma_node, file_priv);
4026984128dSChris Wilson err_remove:
4036984128dSChris Wilson spin_lock(&file_priv->table_lock);
4049649399eSChris Wilson idr_remove(&file_priv->object_idr, handle);
4056984128dSChris Wilson spin_unlock(&file_priv->table_lock);
4066984128dSChris Wilson err_unref:
407e6b62714SThierry Reding drm_gem_object_handle_put_unlocked(obj);
4086984128dSChris Wilson return ret;
409673a394bSEric Anholt }
41020228c44SDaniel Vetter
41120228c44SDaniel Vetter /**
4128bf8180fSThierry Reding * drm_gem_handle_create - create a gem handle for an object
41389d61fc0SDaniel Vetter * @file_priv: drm file-private structure to register the handle for
41489d61fc0SDaniel Vetter * @obj: object to register
41582c0ef94SIgor Matheus Andrade Torrente * @handlep: pointer to return the created handle to the caller
41689d61fc0SDaniel Vetter *
41739031176SDaniel Vetter * Create a handle for this object. This adds a handle reference to the object,
41839031176SDaniel Vetter * which includes a regular reference count. Callers will likely want to
41939031176SDaniel Vetter * dereference the object afterwards.
42039031176SDaniel Vetter *
42139031176SDaniel Vetter * Since this publishes @obj to userspace it must be fully set up by this point,
42239031176SDaniel Vetter * drivers must call this last in their buffer object creation callbacks.
42320228c44SDaniel Vetter */
drm_gem_handle_create(struct drm_file * file_priv,struct drm_gem_object * obj,u32 * handlep)4248bf8180fSThierry Reding int drm_gem_handle_create(struct drm_file *file_priv,
42520228c44SDaniel Vetter struct drm_gem_object *obj,
42620228c44SDaniel Vetter u32 *handlep)
42720228c44SDaniel Vetter {
42820228c44SDaniel Vetter mutex_lock(&obj->dev->object_name_lock);
42920228c44SDaniel Vetter
43020228c44SDaniel Vetter return drm_gem_handle_create_tail(file_priv, obj, handlep);
43120228c44SDaniel Vetter }
432673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create);
433673a394bSEric Anholt
43475ef8b3bSRob Clark
43575ef8b3bSRob Clark /**
43675ef8b3bSRob Clark * drm_gem_free_mmap_offset - release a fake mmap offset for an object
43775ef8b3bSRob Clark * @obj: obj in question
43875ef8b3bSRob Clark *
43975ef8b3bSRob Clark * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
440f74418a4SDaniel Vetter *
441f74418a4SDaniel Vetter * Note that drm_gem_object_release() already calls this function, so drivers
442f74418a4SDaniel Vetter * don't have to take care of releasing the mmap offset themselves when freeing
443f74418a4SDaniel Vetter * the GEM object.
44475ef8b3bSRob Clark */
44575ef8b3bSRob Clark void
drm_gem_free_mmap_offset(struct drm_gem_object * obj)44675ef8b3bSRob Clark drm_gem_free_mmap_offset(struct drm_gem_object *obj)
44775ef8b3bSRob Clark {
44875ef8b3bSRob Clark struct drm_device *dev = obj->dev;
44975ef8b3bSRob Clark
450b04a5906SDaniel Vetter drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
45175ef8b3bSRob Clark }
45275ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_free_mmap_offset);
45375ef8b3bSRob Clark
45475ef8b3bSRob Clark /**
455367bbd49SRob Clark * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
456367bbd49SRob Clark * @obj: obj in question
457367bbd49SRob Clark * @size: the virtual size
458367bbd49SRob Clark *
459367bbd49SRob Clark * GEM memory mapping works by handing back to userspace a fake mmap offset
460367bbd49SRob Clark * it can use in a subsequent mmap(2) call. The DRM core code then looks
461367bbd49SRob Clark * up the object based on the offset and sets up the various memory mapping
462367bbd49SRob Clark * structures.
463367bbd49SRob Clark *
464367bbd49SRob Clark * This routine allocates and attaches a fake offset for @obj, in cases where
465940eba2dSDaniel Vetter * the virtual size differs from the physical size (ie. &drm_gem_object.size).
466940eba2dSDaniel Vetter * Otherwise just use drm_gem_create_mmap_offset().
467f74418a4SDaniel Vetter *
468f74418a4SDaniel Vetter * This function is idempotent and handles an already allocated mmap offset
469f74418a4SDaniel Vetter * transparently. Drivers do not need to check for this case.
470367bbd49SRob Clark */
471367bbd49SRob Clark int
drm_gem_create_mmap_offset_size(struct drm_gem_object * obj,size_t size)472367bbd49SRob Clark drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
473367bbd49SRob Clark {
474367bbd49SRob Clark struct drm_device *dev = obj->dev;
475367bbd49SRob Clark
476b04a5906SDaniel Vetter return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
477367bbd49SRob Clark size / PAGE_SIZE);
478367bbd49SRob Clark }
479367bbd49SRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
480367bbd49SRob Clark
481367bbd49SRob Clark /**
48275ef8b3bSRob Clark * drm_gem_create_mmap_offset - create a fake mmap offset for an object
48375ef8b3bSRob Clark * @obj: obj in question
48475ef8b3bSRob Clark *
48575ef8b3bSRob Clark * GEM memory mapping works by handing back to userspace a fake mmap offset
48675ef8b3bSRob Clark * it can use in a subsequent mmap(2) call. The DRM core code then looks
48775ef8b3bSRob Clark * up the object based on the offset and sets up the various memory mapping
48875ef8b3bSRob Clark * structures.
48975ef8b3bSRob Clark *
49075ef8b3bSRob Clark * This routine allocates and attaches a fake offset for @obj.
491f74418a4SDaniel Vetter *
492f74418a4SDaniel Vetter * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
493f74418a4SDaniel Vetter * the fake offset again.
49475ef8b3bSRob Clark */
drm_gem_create_mmap_offset(struct drm_gem_object * obj)495367bbd49SRob Clark int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
49675ef8b3bSRob Clark {
497367bbd49SRob Clark return drm_gem_create_mmap_offset_size(obj, obj->size);
49875ef8b3bSRob Clark }
49975ef8b3bSRob Clark EXPORT_SYMBOL(drm_gem_create_mmap_offset);
50075ef8b3bSRob Clark
501fb4b4927SKuo-Hsin Yang /*
5023291e09aSMatthew Wilcox (Oracle) * Move folios to appropriate lru and release the folios, decrementing the
5033291e09aSMatthew Wilcox (Oracle) * ref count of those folios.
504fb4b4927SKuo-Hsin Yang */
drm_gem_check_release_batch(struct folio_batch * fbatch)5053291e09aSMatthew Wilcox (Oracle) static void drm_gem_check_release_batch(struct folio_batch *fbatch)
506fb4b4927SKuo-Hsin Yang {
5073291e09aSMatthew Wilcox (Oracle) check_move_unevictable_folios(fbatch);
5083291e09aSMatthew Wilcox (Oracle) __folio_batch_release(fbatch);
509fb4b4927SKuo-Hsin Yang cond_resched();
510fb4b4927SKuo-Hsin Yang }
511fb4b4927SKuo-Hsin Yang
512bcc5c9d5SRob Clark /**
513bcc5c9d5SRob Clark * drm_gem_get_pages - helper to allocate backing pages for a GEM object
514bcc5c9d5SRob Clark * from shmem
515bcc5c9d5SRob Clark * @obj: obj in question
5160cdbe8acSDavid Herrmann *
5170cdbe8acSDavid Herrmann * This reads the page-array of the shmem-backing storage of the given gem
5180cdbe8acSDavid Herrmann * object. An array of pages is returned. If a page is not allocated or
5190cdbe8acSDavid Herrmann * swapped-out, this will allocate/swap-in the required pages. Note that the
5200cdbe8acSDavid Herrmann * whole object is covered by the page-array and pinned in memory.
5210cdbe8acSDavid Herrmann *
5220cdbe8acSDavid Herrmann * Use drm_gem_put_pages() to release the array and unpin all pages.
5230cdbe8acSDavid Herrmann *
5240cdbe8acSDavid Herrmann * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
5250cdbe8acSDavid Herrmann * If you require other GFP-masks, you have to do those allocations yourself.
5260cdbe8acSDavid Herrmann *
5270cdbe8acSDavid Herrmann * Note that you are not allowed to change gfp-zones during runtime. That is,
5280cdbe8acSDavid Herrmann * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
5290cdbe8acSDavid Herrmann * set during initialization. If you have special zone constraints, set them
5305b9fbfffSJordan Crouse * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care
5310cdbe8acSDavid Herrmann * to keep pages in the required zone during swap-in.
532e0b3d214SDaniel Vetter *
533e0b3d214SDaniel Vetter * This function is only valid on objects initialized with
534e0b3d214SDaniel Vetter * drm_gem_object_init(), but not for those initialized with
535e0b3d214SDaniel Vetter * drm_gem_private_object_init() only.
536bcc5c9d5SRob Clark */
drm_gem_get_pages(struct drm_gem_object * obj)5370cdbe8acSDavid Herrmann struct page **drm_gem_get_pages(struct drm_gem_object *obj)
538bcc5c9d5SRob Clark {
539bcc5c9d5SRob Clark struct address_space *mapping;
5403291e09aSMatthew Wilcox (Oracle) struct page **pages;
5413291e09aSMatthew Wilcox (Oracle) struct folio *folio;
5423291e09aSMatthew Wilcox (Oracle) struct folio_batch fbatch;
543*b7fd68abSMatthew Wilcox (Oracle) long i, j, npages;
544e0b3d214SDaniel Vetter
545e0b3d214SDaniel Vetter if (WARN_ON(!obj->filp))
546e0b3d214SDaniel Vetter return ERR_PTR(-EINVAL);
547e0b3d214SDaniel Vetter
548bcc5c9d5SRob Clark /* This is the shared memory object that backs the GEM resource */
54993c76a3dSAl Viro mapping = obj->filp->f_mapping;
550bcc5c9d5SRob Clark
551bcc5c9d5SRob Clark /* We already BUG_ON() for non-page-aligned sizes in
552bcc5c9d5SRob Clark * drm_gem_object_init(), so we should never hit this unless
553bcc5c9d5SRob Clark * driver author is doing something really wrong:
554bcc5c9d5SRob Clark */
555bcc5c9d5SRob Clark WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
556bcc5c9d5SRob Clark
557bcc5c9d5SRob Clark npages = obj->size >> PAGE_SHIFT;
558bcc5c9d5SRob Clark
5592098105eSMichal Hocko pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
560bcc5c9d5SRob Clark if (pages == NULL)
561bcc5c9d5SRob Clark return ERR_PTR(-ENOMEM);
562bcc5c9d5SRob Clark
563fb4b4927SKuo-Hsin Yang mapping_set_unevictable(mapping);
564fb4b4927SKuo-Hsin Yang
5653291e09aSMatthew Wilcox (Oracle) i = 0;
5663291e09aSMatthew Wilcox (Oracle) while (i < npages) {
567*b7fd68abSMatthew Wilcox (Oracle) long nr;
5683291e09aSMatthew Wilcox (Oracle) folio = shmem_read_folio_gfp(mapping, i,
5693291e09aSMatthew Wilcox (Oracle) mapping_gfp_mask(mapping));
5703291e09aSMatthew Wilcox (Oracle) if (IS_ERR(folio))
571bcc5c9d5SRob Clark goto fail;
572*b7fd68abSMatthew Wilcox (Oracle) nr = min(npages - i, folio_nr_pages(folio));
573*b7fd68abSMatthew Wilcox (Oracle) for (j = 0; j < nr; j++, i++)
5743291e09aSMatthew Wilcox (Oracle) pages[i] = folio_file_page(folio, i);
575bcc5c9d5SRob Clark
5762123000bSDavid Herrmann /* Make sure shmem keeps __GFP_DMA32 allocated pages in the
5772123000bSDavid Herrmann * correct region during swapin. Note that this requires
5782123000bSDavid Herrmann * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
5792123000bSDavid Herrmann * so shmem can relocate pages during swapin if required.
580bcc5c9d5SRob Clark */
581c62d2555SMichal Hocko BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
5823291e09aSMatthew Wilcox (Oracle) (folio_pfn(folio) >= 0x00100000UL));
583bcc5c9d5SRob Clark }
584bcc5c9d5SRob Clark
585bcc5c9d5SRob Clark return pages;
586bcc5c9d5SRob Clark
587bcc5c9d5SRob Clark fail:
588fb4b4927SKuo-Hsin Yang mapping_clear_unevictable(mapping);
5893291e09aSMatthew Wilcox (Oracle) folio_batch_init(&fbatch);
5903291e09aSMatthew Wilcox (Oracle) j = 0;
5913291e09aSMatthew Wilcox (Oracle) while (j < i) {
5923291e09aSMatthew Wilcox (Oracle) struct folio *f = page_folio(pages[j]);
5933291e09aSMatthew Wilcox (Oracle) if (!folio_batch_add(&fbatch, f))
5943291e09aSMatthew Wilcox (Oracle) drm_gem_check_release_batch(&fbatch);
5953291e09aSMatthew Wilcox (Oracle) j += folio_nr_pages(f);
596fb4b4927SKuo-Hsin Yang }
5973291e09aSMatthew Wilcox (Oracle) if (fbatch.nr)
5983291e09aSMatthew Wilcox (Oracle) drm_gem_check_release_batch(&fbatch);
599bcc5c9d5SRob Clark
6002098105eSMichal Hocko kvfree(pages);
6013291e09aSMatthew Wilcox (Oracle) return ERR_CAST(folio);
602bcc5c9d5SRob Clark }
603bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_get_pages);
604bcc5c9d5SRob Clark
605bcc5c9d5SRob Clark /**
606bcc5c9d5SRob Clark * drm_gem_put_pages - helper to free backing pages for a GEM object
607bcc5c9d5SRob Clark * @obj: obj in question
608bcc5c9d5SRob Clark * @pages: pages to free
609bcc5c9d5SRob Clark * @dirty: if true, pages will be marked as dirty
610bcc5c9d5SRob Clark * @accessed: if true, the pages will be marked as accessed
611bcc5c9d5SRob Clark */
drm_gem_put_pages(struct drm_gem_object * obj,struct page ** pages,bool dirty,bool accessed)612bcc5c9d5SRob Clark void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
613bcc5c9d5SRob Clark bool dirty, bool accessed)
614bcc5c9d5SRob Clark {
615bcc5c9d5SRob Clark int i, npages;
616fb4b4927SKuo-Hsin Yang struct address_space *mapping;
6173291e09aSMatthew Wilcox (Oracle) struct folio_batch fbatch;
618fb4b4927SKuo-Hsin Yang
619fb4b4927SKuo-Hsin Yang mapping = file_inode(obj->filp)->i_mapping;
620fb4b4927SKuo-Hsin Yang mapping_clear_unevictable(mapping);
621bcc5c9d5SRob Clark
622bcc5c9d5SRob Clark /* We already BUG_ON() for non-page-aligned sizes in
623bcc5c9d5SRob Clark * drm_gem_object_init(), so we should never hit this unless
624bcc5c9d5SRob Clark * driver author is doing something really wrong:
625bcc5c9d5SRob Clark */
626bcc5c9d5SRob Clark WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
627bcc5c9d5SRob Clark
628bcc5c9d5SRob Clark npages = obj->size >> PAGE_SHIFT;
629bcc5c9d5SRob Clark
6303291e09aSMatthew Wilcox (Oracle) folio_batch_init(&fbatch);
631bcc5c9d5SRob Clark for (i = 0; i < npages; i++) {
6323291e09aSMatthew Wilcox (Oracle) struct folio *folio;
6333291e09aSMatthew Wilcox (Oracle)
634930a4024SRob Herring if (!pages[i])
635930a4024SRob Herring continue;
6363291e09aSMatthew Wilcox (Oracle) folio = page_folio(pages[i]);
637930a4024SRob Herring
638bcc5c9d5SRob Clark if (dirty)
6393291e09aSMatthew Wilcox (Oracle) folio_mark_dirty(folio);
640bcc5c9d5SRob Clark
641bcc5c9d5SRob Clark if (accessed)
6423291e09aSMatthew Wilcox (Oracle) folio_mark_accessed(folio);
643bcc5c9d5SRob Clark
644bcc5c9d5SRob Clark /* Undo the reference we took when populating the table */
6453291e09aSMatthew Wilcox (Oracle) if (!folio_batch_add(&fbatch, folio))
6463291e09aSMatthew Wilcox (Oracle) drm_gem_check_release_batch(&fbatch);
6473291e09aSMatthew Wilcox (Oracle) i += folio_nr_pages(folio) - 1;
648bcc5c9d5SRob Clark }
6493291e09aSMatthew Wilcox (Oracle) if (folio_batch_count(&fbatch))
6503291e09aSMatthew Wilcox (Oracle) drm_gem_check_release_batch(&fbatch);
651bcc5c9d5SRob Clark
6522098105eSMichal Hocko kvfree(pages);
653bcc5c9d5SRob Clark }
654bcc5c9d5SRob Clark EXPORT_SYMBOL(drm_gem_put_pages);
655bcc5c9d5SRob Clark
objects_lookup(struct drm_file * filp,u32 * handle,int count,struct drm_gem_object ** objs)656c117aa4dSRob Herring static int objects_lookup(struct drm_file *filp, u32 *handle, int count,
657c117aa4dSRob Herring struct drm_gem_object **objs)
658c117aa4dSRob Herring {
659c117aa4dSRob Herring int i, ret = 0;
660c117aa4dSRob Herring struct drm_gem_object *obj;
661c117aa4dSRob Herring
662c117aa4dSRob Herring spin_lock(&filp->table_lock);
663c117aa4dSRob Herring
664c117aa4dSRob Herring for (i = 0; i < count; i++) {
665c117aa4dSRob Herring /* Check if we currently have a reference on the object */
666c117aa4dSRob Herring obj = idr_find(&filp->object_idr, handle[i]);
667c117aa4dSRob Herring if (!obj) {
668c117aa4dSRob Herring ret = -ENOENT;
669c117aa4dSRob Herring break;
670c117aa4dSRob Herring }
671c117aa4dSRob Herring drm_gem_object_get(obj);
672c117aa4dSRob Herring objs[i] = obj;
673c117aa4dSRob Herring }
674c117aa4dSRob Herring spin_unlock(&filp->table_lock);
675c117aa4dSRob Herring
676c117aa4dSRob Herring return ret;
677c117aa4dSRob Herring }
678c117aa4dSRob Herring
679c117aa4dSRob Herring /**
680c117aa4dSRob Herring * drm_gem_objects_lookup - look up GEM objects from an array of handles
681c117aa4dSRob Herring * @filp: DRM file private date
682c117aa4dSRob Herring * @bo_handles: user pointer to array of userspace handle
683c117aa4dSRob Herring * @count: size of handle array
684c117aa4dSRob Herring * @objs_out: returned pointer to array of drm_gem_object pointers
685c117aa4dSRob Herring *
686c117aa4dSRob Herring * Takes an array of userspace handles and returns a newly allocated array of
687c117aa4dSRob Herring * GEM objects.
688c117aa4dSRob Herring *
689c117aa4dSRob Herring * For a single handle lookup, use drm_gem_object_lookup().
690c117aa4dSRob Herring *
691c117aa4dSRob Herring * Returns:
692c117aa4dSRob Herring *
693c117aa4dSRob Herring * @objs filled in with GEM object pointers. Returned GEM objects need to be
694be6ee102SEmil Velikov * released with drm_gem_object_put(). -ENOENT is returned on a lookup
695c117aa4dSRob Herring * failure. 0 is returned on success.
696c117aa4dSRob Herring *
697c117aa4dSRob Herring */
drm_gem_objects_lookup(struct drm_file * filp,void __user * bo_handles,int count,struct drm_gem_object *** objs_out)698c117aa4dSRob Herring int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
699c117aa4dSRob Herring int count, struct drm_gem_object ***objs_out)
700c117aa4dSRob Herring {
701c117aa4dSRob Herring int ret;
702c117aa4dSRob Herring u32 *handles;
703c117aa4dSRob Herring struct drm_gem_object **objs;
704c117aa4dSRob Herring
705c117aa4dSRob Herring if (!count)
706c117aa4dSRob Herring return 0;
707c117aa4dSRob Herring
708c117aa4dSRob Herring objs = kvmalloc_array(count, sizeof(struct drm_gem_object *),
709c117aa4dSRob Herring GFP_KERNEL | __GFP_ZERO);
710c117aa4dSRob Herring if (!objs)
711c117aa4dSRob Herring return -ENOMEM;
712c117aa4dSRob Herring
713ec0bb482SDan Carpenter *objs_out = objs;
714ec0bb482SDan Carpenter
715c117aa4dSRob Herring handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
716c117aa4dSRob Herring if (!handles) {
717c117aa4dSRob Herring ret = -ENOMEM;
718c117aa4dSRob Herring goto out;
719c117aa4dSRob Herring }
720c117aa4dSRob Herring
721c117aa4dSRob Herring if (copy_from_user(handles, bo_handles, count * sizeof(u32))) {
722c117aa4dSRob Herring ret = -EFAULT;
723c117aa4dSRob Herring DRM_DEBUG("Failed to copy in GEM handles\n");
724c117aa4dSRob Herring goto out;
725c117aa4dSRob Herring }
726c117aa4dSRob Herring
727c117aa4dSRob Herring ret = objects_lookup(filp, handles, count, objs);
728c117aa4dSRob Herring out:
729c117aa4dSRob Herring kvfree(handles);
730c117aa4dSRob Herring return ret;
731c117aa4dSRob Herring
732c117aa4dSRob Herring }
733c117aa4dSRob Herring EXPORT_SYMBOL(drm_gem_objects_lookup);
734c117aa4dSRob Herring
735df2e0900SDaniel Vetter /**
7361e55a53aSMatt Roper * drm_gem_object_lookup - look up a GEM object from its handle
737df2e0900SDaniel Vetter * @filp: DRM file private date
738df2e0900SDaniel Vetter * @handle: userspace handle
739df2e0900SDaniel Vetter *
740df2e0900SDaniel Vetter * Returns:
741df2e0900SDaniel Vetter *
742df2e0900SDaniel Vetter * A reference to the object named by the handle if such exists on @filp, NULL
743df2e0900SDaniel Vetter * otherwise.
744c117aa4dSRob Herring *
745c117aa4dSRob Herring * If looking up an array of handles, use drm_gem_objects_lookup().
746df2e0900SDaniel Vetter */
747673a394bSEric Anholt struct drm_gem_object *
drm_gem_object_lookup(struct drm_file * filp,u32 handle)748a8ad0bd8SChris Wilson drm_gem_object_lookup(struct drm_file *filp, u32 handle)
749673a394bSEric Anholt {
750c117aa4dSRob Herring struct drm_gem_object *obj = NULL;
751673a394bSEric Anholt
752c117aa4dSRob Herring objects_lookup(filp, &handle, 1, &obj);
753673a394bSEric Anholt return obj;
754673a394bSEric Anholt }
755673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup);
756673a394bSEric Anholt
757673a394bSEric Anholt /**
75852791eeeSChristian König * drm_gem_dma_resv_wait - Wait on GEM object's reservation's objects
7591ba62714SRob Herring * shared and/or exclusive fences.
7601ba62714SRob Herring * @filep: DRM file private date
7611ba62714SRob Herring * @handle: userspace handle
7621ba62714SRob Herring * @wait_all: if true, wait on all fences, else wait on just exclusive fence
7631ba62714SRob Herring * @timeout: timeout value in jiffies or zero to return immediately
7641ba62714SRob Herring *
7651ba62714SRob Herring * Returns:
7661ba62714SRob Herring *
7671ba62714SRob Herring * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
7681ba62714SRob Herring * greater than 0 on success.
7691ba62714SRob Herring */
drm_gem_dma_resv_wait(struct drm_file * filep,u32 handle,bool wait_all,unsigned long timeout)77052791eeeSChristian König long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
7711ba62714SRob Herring bool wait_all, unsigned long timeout)
7721ba62714SRob Herring {
7731ba62714SRob Herring long ret;
7741ba62714SRob Herring struct drm_gem_object *obj;
7751ba62714SRob Herring
7761ba62714SRob Herring obj = drm_gem_object_lookup(filep, handle);
7771ba62714SRob Herring if (!obj) {
7781ba62714SRob Herring DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
7791ba62714SRob Herring return -EINVAL;
7801ba62714SRob Herring }
7811ba62714SRob Herring
7827bc80a54SChristian König ret = dma_resv_wait_timeout(obj->resv, dma_resv_usage_rw(wait_all),
7837bc80a54SChristian König true, timeout);
7841ba62714SRob Herring if (ret == 0)
7851ba62714SRob Herring ret = -ETIME;
7861ba62714SRob Herring else if (ret > 0)
7871ba62714SRob Herring ret = 0;
7881ba62714SRob Herring
789be6ee102SEmil Velikov drm_gem_object_put(obj);
7901ba62714SRob Herring
7911ba62714SRob Herring return ret;
7921ba62714SRob Herring }
79352791eeeSChristian König EXPORT_SYMBOL(drm_gem_dma_resv_wait);
7941ba62714SRob Herring
7951ba62714SRob Herring /**
79689d61fc0SDaniel Vetter * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
79789d61fc0SDaniel Vetter * @dev: drm_device
79889d61fc0SDaniel Vetter * @data: ioctl data
79989d61fc0SDaniel Vetter * @file_priv: drm file-private structure
80089d61fc0SDaniel Vetter *
801673a394bSEric Anholt * Releases the handle to an mm object.
802673a394bSEric Anholt */
803673a394bSEric Anholt int
drm_gem_close_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)804673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data,
805673a394bSEric Anholt struct drm_file *file_priv)
806673a394bSEric Anholt {
807673a394bSEric Anholt struct drm_gem_close *args = data;
808673a394bSEric Anholt int ret;
809673a394bSEric Anholt
8101bcecfacSAndrzej Hajda if (!drm_core_check_feature(dev, DRIVER_GEM))
81169fdf420SChris Wilson return -EOPNOTSUPP;
812673a394bSEric Anholt
813673a394bSEric Anholt ret = drm_gem_handle_delete(file_priv, args->handle);
814673a394bSEric Anholt
815673a394bSEric Anholt return ret;
816673a394bSEric Anholt }
817673a394bSEric Anholt
818673a394bSEric Anholt /**
81989d61fc0SDaniel Vetter * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
82089d61fc0SDaniel Vetter * @dev: drm_device
82189d61fc0SDaniel Vetter * @data: ioctl data
82289d61fc0SDaniel Vetter * @file_priv: drm file-private structure
82389d61fc0SDaniel Vetter *
824673a394bSEric Anholt * Create a global name for an object, returning the name.
825673a394bSEric Anholt *
826673a394bSEric Anholt * Note that the name does not hold a reference; when the object
827673a394bSEric Anholt * is freed, the name goes away.
828673a394bSEric Anholt */
829673a394bSEric Anholt int
drm_gem_flink_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)830673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data,
831673a394bSEric Anholt struct drm_file *file_priv)
832673a394bSEric Anholt {
833673a394bSEric Anholt struct drm_gem_flink *args = data;
834673a394bSEric Anholt struct drm_gem_object *obj;
835673a394bSEric Anholt int ret;
836673a394bSEric Anholt
8371bcecfacSAndrzej Hajda if (!drm_core_check_feature(dev, DRIVER_GEM))
83869fdf420SChris Wilson return -EOPNOTSUPP;
839673a394bSEric Anholt
840a8ad0bd8SChris Wilson obj = drm_gem_object_lookup(file_priv, args->handle);
841673a394bSEric Anholt if (obj == NULL)
842bf79cb91SChris Wilson return -ENOENT;
843673a394bSEric Anholt
844cd4f013fSDaniel Vetter mutex_lock(&dev->object_name_lock);
845a8e11d1cSDaniel Vetter /* prevent races with concurrent gem_close. */
846a8e11d1cSDaniel Vetter if (obj->handle_count == 0) {
847a8e11d1cSDaniel Vetter ret = -ENOENT;
848a8e11d1cSDaniel Vetter goto err;
849a8e11d1cSDaniel Vetter }
850a8e11d1cSDaniel Vetter
8518d59bae5SChris Wilson if (!obj->name) {
8520f646425SChris Wilson ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
8532e928815STejun Heo if (ret < 0)
8543e49c4f4SChris Wilson goto err;
8552e07fb22SYoungJun Cho
8562e07fb22SYoungJun Cho obj->name = ret;
8578d59bae5SChris Wilson }
8583e49c4f4SChris Wilson
8592e07fb22SYoungJun Cho args->name = (uint64_t) obj->name;
8602e07fb22SYoungJun Cho ret = 0;
8612e07fb22SYoungJun Cho
8623e49c4f4SChris Wilson err:
863cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock);
864be6ee102SEmil Velikov drm_gem_object_put(obj);
8653e49c4f4SChris Wilson return ret;
866673a394bSEric Anholt }
867673a394bSEric Anholt
868673a394bSEric Anholt /**
869e9d2871fSMauro Carvalho Chehab * drm_gem_open_ioctl - implementation of the GEM_OPEN ioctl
87089d61fc0SDaniel Vetter * @dev: drm_device
87189d61fc0SDaniel Vetter * @data: ioctl data
87289d61fc0SDaniel Vetter * @file_priv: drm file-private structure
87389d61fc0SDaniel Vetter *
874673a394bSEric Anholt * Open an object using the global name, returning a handle and the size.
875a9e10b16SSteve Cohen *
876a9e10b16SSteve Cohen * This handle (of course) holds a reference to the object, so the object
877a9e10b16SSteve Cohen * will not go away until the handle is deleted.
878673a394bSEric Anholt */
879673a394bSEric Anholt int
drm_gem_open_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)880673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data,
881673a394bSEric Anholt struct drm_file *file_priv)
882673a394bSEric Anholt {
883673a394bSEric Anholt struct drm_gem_open *args = data;
884673a394bSEric Anholt struct drm_gem_object *obj;
885673a394bSEric Anholt int ret;
886a1a2d1d3SPekka Paalanen u32 handle;
887673a394bSEric Anholt
8881bcecfacSAndrzej Hajda if (!drm_core_check_feature(dev, DRIVER_GEM))
88969fdf420SChris Wilson return -EOPNOTSUPP;
890673a394bSEric Anholt
891cd4f013fSDaniel Vetter mutex_lock(&dev->object_name_lock);
892673a394bSEric Anholt obj = idr_find(&dev->object_name_idr, (int) args->name);
89320228c44SDaniel Vetter if (obj) {
894e6b62714SThierry Reding drm_gem_object_get(obj);
89520228c44SDaniel Vetter } else {
896cd4f013fSDaniel Vetter mutex_unlock(&dev->object_name_lock);
897673a394bSEric Anholt return -ENOENT;
89820228c44SDaniel Vetter }
899673a394bSEric Anholt
90020228c44SDaniel Vetter /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
90120228c44SDaniel Vetter ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
902673a394bSEric Anholt if (ret)
9038490d6a7SSteve Cohen goto err;
904673a394bSEric Anholt
905673a394bSEric Anholt args->handle = handle;
906673a394bSEric Anholt args->size = obj->size;
907673a394bSEric Anholt
9088490d6a7SSteve Cohen err:
909c44264f9SDave Airlie drm_gem_object_put(obj);
9108490d6a7SSteve Cohen return ret;
911673a394bSEric Anholt }
912673a394bSEric Anholt
913673a394bSEric Anholt /**
9140ae865efSCai Huoqing * drm_gem_open - initializes GEM file-private structures at devnode open time
91589d61fc0SDaniel Vetter * @dev: drm_device which is being opened by userspace
91689d61fc0SDaniel Vetter * @file_private: drm file-private structure to set up
91789d61fc0SDaniel Vetter *
918673a394bSEric Anholt * Called at device open time, sets up the structure for handling refcounting
919673a394bSEric Anholt * of mm objects.
920673a394bSEric Anholt */
921673a394bSEric Anholt void
drm_gem_open(struct drm_device * dev,struct drm_file * file_private)922673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
923673a394bSEric Anholt {
924e86584c5SChris Wilson idr_init_base(&file_private->object_idr, 1);
925673a394bSEric Anholt spin_lock_init(&file_private->table_lock);
926673a394bSEric Anholt }
927673a394bSEric Anholt
928673a394bSEric Anholt /**
92989d61fc0SDaniel Vetter * drm_gem_release - release file-private GEM resources
93089d61fc0SDaniel Vetter * @dev: drm_device which is being closed by userspace
93189d61fc0SDaniel Vetter * @file_private: drm file-private structure to clean up
93289d61fc0SDaniel Vetter *
933673a394bSEric Anholt * Called at close time when the filp is going away.
934673a394bSEric Anholt *
935673a394bSEric Anholt * Releases any remaining references on objects by this filp.
936673a394bSEric Anholt */
937673a394bSEric Anholt void
drm_gem_release(struct drm_device * dev,struct drm_file * file_private)938673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
939673a394bSEric Anholt {
940673a394bSEric Anholt idr_for_each(&file_private->object_idr,
941304eda32SBen Skeggs &drm_gem_object_release_handle, file_private);
942673a394bSEric Anholt idr_destroy(&file_private->object_idr);
943673a394bSEric Anholt }
944673a394bSEric Anholt
945f74418a4SDaniel Vetter /**
946f74418a4SDaniel Vetter * drm_gem_object_release - release GEM buffer object resources
947f74418a4SDaniel Vetter * @obj: GEM buffer object
948f74418a4SDaniel Vetter *
9490ae865efSCai Huoqing * This releases any structures and resources used by @obj and is the inverse of
950f74418a4SDaniel Vetter * drm_gem_object_init().
951f74418a4SDaniel Vetter */
952fd632aa3SDaniel Vetter void
drm_gem_object_release(struct drm_gem_object * obj)953fd632aa3SDaniel Vetter drm_gem_object_release(struct drm_gem_object *obj)
954c3ae90c0SLuca Barbieri {
95562cb7011SAlan Cox if (obj->filp)
956c3ae90c0SLuca Barbieri fput(obj->filp);
95777472347SDavid Herrmann
9587df34a61SChunyouTang drm_gem_private_object_fini(obj);
9597df34a61SChunyouTang
96077472347SDavid Herrmann drm_gem_free_mmap_offset(obj);
961e7c2af13SRob Clark drm_gem_lru_remove(obj);
962c3ae90c0SLuca Barbieri }
963fd632aa3SDaniel Vetter EXPORT_SYMBOL(drm_gem_object_release);
964c3ae90c0SLuca Barbieri
965673a394bSEric Anholt /**
96689d61fc0SDaniel Vetter * drm_gem_object_free - free a GEM object
96789d61fc0SDaniel Vetter * @kref: kref of the object to free
96889d61fc0SDaniel Vetter *
969673a394bSEric Anholt * Called after the last reference to the object has been lost.
970673a394bSEric Anholt *
971673a394bSEric Anholt * Frees the object
972673a394bSEric Anholt */
973673a394bSEric Anholt void
drm_gem_object_free(struct kref * kref)974673a394bSEric Anholt drm_gem_object_free(struct kref *kref)
975673a394bSEric Anholt {
9766ff774bdSDaniel Vetter struct drm_gem_object *obj =
9776ff774bdSDaniel Vetter container_of(kref, struct drm_gem_object, refcount);
978673a394bSEric Anholt
979d693def4SThomas Zimmermann if (WARN_ON(!obj->funcs->free))
980d693def4SThomas Zimmermann return;
981d693def4SThomas Zimmermann
982b39b5394SNoralf Trønnes obj->funcs->free(obj);
9836d3e7fddSDaniel Vetter }
984673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free);
985673a394bSEric Anholt
986df2e0900SDaniel Vetter /**
987df2e0900SDaniel Vetter * drm_gem_vm_open - vma->ops->open implementation for GEM
988df2e0900SDaniel Vetter * @vma: VM area structure
989df2e0900SDaniel Vetter *
990df2e0900SDaniel Vetter * This function implements the #vm_operations_struct open() callback for GEM
991df2e0900SDaniel Vetter * drivers. This must be used together with drm_gem_vm_close().
992df2e0900SDaniel Vetter */
drm_gem_vm_open(struct vm_area_struct * vma)993ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma)
994ab00b3e5SJesse Barnes {
995ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data;
996ab00b3e5SJesse Barnes
997e6b62714SThierry Reding drm_gem_object_get(obj);
998ab00b3e5SJesse Barnes }
999ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open);
1000ab00b3e5SJesse Barnes
1001df2e0900SDaniel Vetter /**
1002df2e0900SDaniel Vetter * drm_gem_vm_close - vma->ops->close implementation for GEM
1003df2e0900SDaniel Vetter * @vma: VM area structure
1004df2e0900SDaniel Vetter *
1005df2e0900SDaniel Vetter * This function implements the #vm_operations_struct close() callback for GEM
1006df2e0900SDaniel Vetter * drivers. This must be used together with drm_gem_vm_open().
1007df2e0900SDaniel Vetter */
drm_gem_vm_close(struct vm_area_struct * vma)1008ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma)
1009ab00b3e5SJesse Barnes {
1010ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data;
1011ab00b3e5SJesse Barnes
1012be6ee102SEmil Velikov drm_gem_object_put(obj);
1013ab00b3e5SJesse Barnes }
1014ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close);
1015ab00b3e5SJesse Barnes
10161c5aafa6SLaurent Pinchart /**
10171c5aafa6SLaurent Pinchart * drm_gem_mmap_obj - memory map a GEM object
10181c5aafa6SLaurent Pinchart * @obj: the GEM object to map
10191c5aafa6SLaurent Pinchart * @obj_size: the object size to be mapped, in bytes
10201c5aafa6SLaurent Pinchart * @vma: VMA for the area to be mapped
10211c5aafa6SLaurent Pinchart *
1022d693def4SThomas Zimmermann * Set up the VMA to prepare mapping of the GEM object using the GEM object's
1023d693def4SThomas Zimmermann * vm_ops. Depending on their requirements, GEM objects can either
1024d693def4SThomas Zimmermann * provide a fault handler in their vm_ops (in which case any accesses to
10251c5aafa6SLaurent Pinchart * the object will be trapped, to perform migration, GTT binding, surface
10261c5aafa6SLaurent Pinchart * register allocation, or performance monitoring), or mmap the buffer memory
10271c5aafa6SLaurent Pinchart * synchronously after calling drm_gem_mmap_obj.
10281c5aafa6SLaurent Pinchart *
10291c5aafa6SLaurent Pinchart * This function is mainly intended to implement the DMABUF mmap operation, when
10301c5aafa6SLaurent Pinchart * the GEM object is not looked up based on its fake offset. To implement the
10311c5aafa6SLaurent Pinchart * DRM mmap operation, drivers should use the drm_gem_mmap() function.
10321c5aafa6SLaurent Pinchart *
1033ca481c9bSDavid Herrmann * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
1034ca481c9bSDavid Herrmann * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
1035ca481c9bSDavid Herrmann * callers must verify access restrictions before calling this helper.
1036ca481c9bSDavid Herrmann *
10371c5aafa6SLaurent Pinchart * Return 0 or success or -EINVAL if the object size is smaller than the VMA
1038d693def4SThomas Zimmermann * size, or if no vm_ops are provided.
10391c5aafa6SLaurent Pinchart */
drm_gem_mmap_obj(struct drm_gem_object * obj,unsigned long obj_size,struct vm_area_struct * vma)10401c5aafa6SLaurent Pinchart int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
10411c5aafa6SLaurent Pinchart struct vm_area_struct *vma)
10421c5aafa6SLaurent Pinchart {
1043c40069cbSGerd Hoffmann int ret;
10441c5aafa6SLaurent Pinchart
10451c5aafa6SLaurent Pinchart /* Check for valid size. */
10461c5aafa6SLaurent Pinchart if (obj_size < vma->vm_end - vma->vm_start)
10471c5aafa6SLaurent Pinchart return -EINVAL;
10481c5aafa6SLaurent Pinchart
10491c5aafa6SLaurent Pinchart /* Take a ref for this mapping of the object, so that the fault
10501c5aafa6SLaurent Pinchart * handler can dereference the mmap offset's pointer to the object.
10511c5aafa6SLaurent Pinchart * This reference is cleaned up by the corresponding vm_close
10521c5aafa6SLaurent Pinchart * (which should happen whether the vma was created by this call, or
10531c5aafa6SLaurent Pinchart * by a vm_open due to mremap or partial unmap or whatever).
10541c5aafa6SLaurent Pinchart */
1055e6b62714SThierry Reding drm_gem_object_get(obj);
10561c5aafa6SLaurent Pinchart
1057f49a51bfSDaniel Vetter vma->vm_private_data = obj;
105847d35c1cSThomas Zimmermann vma->vm_ops = obj->funcs->vm_ops;
1059f49a51bfSDaniel Vetter
1060d693def4SThomas Zimmermann if (obj->funcs->mmap) {
10619786b65bSGerd Hoffmann ret = obj->funcs->mmap(obj, vma);
106247d35c1cSThomas Zimmermann if (ret)
106347d35c1cSThomas Zimmermann goto err_drm_gem_object_put;
10649786b65bSGerd Hoffmann WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
10659786b65bSGerd Hoffmann } else {
106647d35c1cSThomas Zimmermann if (!vma->vm_ops) {
106747d35c1cSThomas Zimmermann ret = -EINVAL;
106847d35c1cSThomas Zimmermann goto err_drm_gem_object_put;
10699786b65bSGerd Hoffmann }
10709786b65bSGerd Hoffmann
10711c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
10729786b65bSGerd Hoffmann vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
10739786b65bSGerd Hoffmann vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
10749786b65bSGerd Hoffmann }
10759786b65bSGerd Hoffmann
10761c5aafa6SLaurent Pinchart return 0;
107747d35c1cSThomas Zimmermann
107847d35c1cSThomas Zimmermann err_drm_gem_object_put:
107947d35c1cSThomas Zimmermann drm_gem_object_put(obj);
108047d35c1cSThomas Zimmermann return ret;
10811c5aafa6SLaurent Pinchart }
10821c5aafa6SLaurent Pinchart EXPORT_SYMBOL(drm_gem_mmap_obj);
1083ab00b3e5SJesse Barnes
1084a2c0a97bSJesse Barnes /**
1085a2c0a97bSJesse Barnes * drm_gem_mmap - memory map routine for GEM objects
1086a2c0a97bSJesse Barnes * @filp: DRM file pointer
1087a2c0a97bSJesse Barnes * @vma: VMA for the area to be mapped
1088a2c0a97bSJesse Barnes *
1089a2c0a97bSJesse Barnes * If a driver supports GEM object mapping, mmap calls on the DRM file
1090a2c0a97bSJesse Barnes * descriptor will end up here.
1091a2c0a97bSJesse Barnes *
10921c5aafa6SLaurent Pinchart * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
1093a2c0a97bSJesse Barnes * contain the fake offset we created when the GTT map ioctl was called on
10941c5aafa6SLaurent Pinchart * the object) and map it with a call to drm_gem_mmap_obj().
1095ca481c9bSDavid Herrmann *
1096ca481c9bSDavid Herrmann * If the caller is not granted access to the buffer object, the mmap will fail
1097ca481c9bSDavid Herrmann * with EACCES. Please see the vma manager for more information.
1098a2c0a97bSJesse Barnes */
drm_gem_mmap(struct file * filp,struct vm_area_struct * vma)1099a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
1100a2c0a97bSJesse Barnes {
1101a2c0a97bSJesse Barnes struct drm_file *priv = filp->private_data;
1102a2c0a97bSJesse Barnes struct drm_device *dev = priv->minor->dev;
11032225cfe4SDaniel Vetter struct drm_gem_object *obj = NULL;
11040de23977SDavid Herrmann struct drm_vma_offset_node *node;
1105a8469aa8SDavid Herrmann int ret;
1106a2c0a97bSJesse Barnes
1107c07dcd61SDaniel Vetter if (drm_dev_is_unplugged(dev))
11082c07a21dSDave Airlie return -ENODEV;
11092c07a21dSDave Airlie
11102225cfe4SDaniel Vetter drm_vma_offset_lock_lookup(dev->vma_offset_manager);
11112225cfe4SDaniel Vetter node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
1112b04a5906SDaniel Vetter vma->vm_pgoff,
11130de23977SDavid Herrmann vma_pages(vma));
11142225cfe4SDaniel Vetter if (likely(node)) {
11152225cfe4SDaniel Vetter obj = container_of(node, struct drm_gem_object, vma_node);
11162225cfe4SDaniel Vetter /*
11172225cfe4SDaniel Vetter * When the object is being freed, after it hits 0-refcnt it
11182225cfe4SDaniel Vetter * proceeds to tear down the object. In the process it will
11192225cfe4SDaniel Vetter * attempt to remove the VMA offset and so acquire this
11202225cfe4SDaniel Vetter * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
11212225cfe4SDaniel Vetter * that matches our range, we know it is in the process of being
11222225cfe4SDaniel Vetter * destroyed and will be freed as soon as we release the lock -
11232225cfe4SDaniel Vetter * so we have to check for the 0-refcnted object and treat it as
11242225cfe4SDaniel Vetter * invalid.
11252225cfe4SDaniel Vetter */
11262225cfe4SDaniel Vetter if (!kref_get_unless_zero(&obj->refcount))
11272225cfe4SDaniel Vetter obj = NULL;
11282225cfe4SDaniel Vetter }
11292225cfe4SDaniel Vetter drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
11302225cfe4SDaniel Vetter
11312225cfe4SDaniel Vetter if (!obj)
1132197633b9SDaniel Vetter return -EINVAL;
11332225cfe4SDaniel Vetter
1134d9a1f0b4SDavid Herrmann if (!drm_vma_node_is_allowed(node, priv)) {
1135be6ee102SEmil Velikov drm_gem_object_put(obj);
1136ca481c9bSDavid Herrmann return -EACCES;
1137a2c0a97bSJesse Barnes }
1138a2c0a97bSJesse Barnes
11392225cfe4SDaniel Vetter ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
11402225cfe4SDaniel Vetter vma);
1141a2c0a97bSJesse Barnes
1142be6ee102SEmil Velikov drm_gem_object_put(obj);
1143a2c0a97bSJesse Barnes
1144a2c0a97bSJesse Barnes return ret;
1145a2c0a97bSJesse Barnes }
1146a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap);
114745d58b40SNoralf Trønnes
drm_gem_print_info(struct drm_printer * p,unsigned int indent,const struct drm_gem_object * obj)114845d58b40SNoralf Trønnes void drm_gem_print_info(struct drm_printer *p, unsigned int indent,
114945d58b40SNoralf Trønnes const struct drm_gem_object *obj)
115045d58b40SNoralf Trønnes {
115145d58b40SNoralf Trønnes drm_printf_indent(p, indent, "name=%d\n", obj->name);
115245d58b40SNoralf Trønnes drm_printf_indent(p, indent, "refcount=%u\n",
115345d58b40SNoralf Trønnes kref_read(&obj->refcount));
115445d58b40SNoralf Trønnes drm_printf_indent(p, indent, "start=%08lx\n",
115545d58b40SNoralf Trønnes drm_vma_node_start(&obj->vma_node));
115645d58b40SNoralf Trønnes drm_printf_indent(p, indent, "size=%zu\n", obj->size);
115745d58b40SNoralf Trønnes drm_printf_indent(p, indent, "imported=%s\n",
1158b8c75bd9SLucas De Marchi str_yes_no(obj->import_attach));
115945d58b40SNoralf Trønnes
1160d693def4SThomas Zimmermann if (obj->funcs->print_info)
1161b39b5394SNoralf Trønnes obj->funcs->print_info(p, indent, obj);
116245d58b40SNoralf Trønnes }
1163b39b5394SNoralf Trønnes
drm_gem_pin(struct drm_gem_object * obj)1164b39b5394SNoralf Trønnes int drm_gem_pin(struct drm_gem_object *obj)
1165b39b5394SNoralf Trønnes {
1166d693def4SThomas Zimmermann if (obj->funcs->pin)
1167b39b5394SNoralf Trønnes return obj->funcs->pin(obj);
1168319eeec5SSui Jingfeng
1169b39b5394SNoralf Trønnes return 0;
1170b39b5394SNoralf Trønnes }
1171b39b5394SNoralf Trønnes
drm_gem_unpin(struct drm_gem_object * obj)1172b39b5394SNoralf Trønnes void drm_gem_unpin(struct drm_gem_object *obj)
1173b39b5394SNoralf Trønnes {
1174d693def4SThomas Zimmermann if (obj->funcs->unpin)
1175b39b5394SNoralf Trønnes obj->funcs->unpin(obj);
1176b39b5394SNoralf Trønnes }
1177b39b5394SNoralf Trønnes
drm_gem_vmap(struct drm_gem_object * obj,struct iosys_map * map)11787938f421SLucas De Marchi int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map)
1179b39b5394SNoralf Trønnes {
118049a3f51dSThomas Zimmermann int ret;
1181b39b5394SNoralf Trønnes
118279e2cf2eSDmitry Osipenko dma_resv_assert_held(obj->resv);
118379e2cf2eSDmitry Osipenko
118449a3f51dSThomas Zimmermann if (!obj->funcs->vmap)
1185a745fb1cSThomas Zimmermann return -EOPNOTSUPP;
1186b39b5394SNoralf Trønnes
1187a745fb1cSThomas Zimmermann ret = obj->funcs->vmap(obj, map);
118849a3f51dSThomas Zimmermann if (ret)
1189a745fb1cSThomas Zimmermann return ret;
11907938f421SLucas De Marchi else if (iosys_map_is_null(map))
1191a745fb1cSThomas Zimmermann return -ENOMEM;
1192b39b5394SNoralf Trønnes
1193a745fb1cSThomas Zimmermann return 0;
1194b39b5394SNoralf Trønnes }
1195db0c6bd2SThomas Zimmermann EXPORT_SYMBOL(drm_gem_vmap);
1196b39b5394SNoralf Trønnes
drm_gem_vunmap(struct drm_gem_object * obj,struct iosys_map * map)11977938f421SLucas De Marchi void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
1198b39b5394SNoralf Trønnes {
119979e2cf2eSDmitry Osipenko dma_resv_assert_held(obj->resv);
120079e2cf2eSDmitry Osipenko
12017938f421SLucas De Marchi if (iosys_map_is_null(map))
1202b39b5394SNoralf Trønnes return;
1203b39b5394SNoralf Trønnes
1204d693def4SThomas Zimmermann if (obj->funcs->vunmap)
1205a745fb1cSThomas Zimmermann obj->funcs->vunmap(obj, map);
1206a745fb1cSThomas Zimmermann
1207a745fb1cSThomas Zimmermann /* Always set the mapping to NULL. Callers may rely on this. */
12087938f421SLucas De Marchi iosys_map_clear(map);
1209b39b5394SNoralf Trønnes }
1210db0c6bd2SThomas Zimmermann EXPORT_SYMBOL(drm_gem_vunmap);
12117edc3e3bSEric Anholt
drm_gem_vmap_unlocked(struct drm_gem_object * obj,struct iosys_map * map)121279e2cf2eSDmitry Osipenko int drm_gem_vmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map)
121379e2cf2eSDmitry Osipenko {
121479e2cf2eSDmitry Osipenko int ret;
121579e2cf2eSDmitry Osipenko
121679e2cf2eSDmitry Osipenko dma_resv_lock(obj->resv, NULL);
121779e2cf2eSDmitry Osipenko ret = drm_gem_vmap(obj, map);
121879e2cf2eSDmitry Osipenko dma_resv_unlock(obj->resv);
121979e2cf2eSDmitry Osipenko
122079e2cf2eSDmitry Osipenko return ret;
122179e2cf2eSDmitry Osipenko }
122279e2cf2eSDmitry Osipenko EXPORT_SYMBOL(drm_gem_vmap_unlocked);
122379e2cf2eSDmitry Osipenko
drm_gem_vunmap_unlocked(struct drm_gem_object * obj,struct iosys_map * map)122479e2cf2eSDmitry Osipenko void drm_gem_vunmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map)
122579e2cf2eSDmitry Osipenko {
122679e2cf2eSDmitry Osipenko dma_resv_lock(obj->resv, NULL);
122779e2cf2eSDmitry Osipenko drm_gem_vunmap(obj, map);
122879e2cf2eSDmitry Osipenko dma_resv_unlock(obj->resv);
122979e2cf2eSDmitry Osipenko }
123079e2cf2eSDmitry Osipenko EXPORT_SYMBOL(drm_gem_vunmap_unlocked);
123179e2cf2eSDmitry Osipenko
12327edc3e3bSEric Anholt /**
12337edc3e3bSEric Anholt * drm_gem_lock_reservations - Sets up the ww context and acquires
12347edc3e3bSEric Anholt * the lock on an array of GEM objects.
12357edc3e3bSEric Anholt *
12367edc3e3bSEric Anholt * Once you've locked your reservations, you'll want to set up space
12377edc3e3bSEric Anholt * for your shared fences (if applicable), submit your job, then
12387edc3e3bSEric Anholt * drm_gem_unlock_reservations().
12397edc3e3bSEric Anholt *
12407edc3e3bSEric Anholt * @objs: drm_gem_objects to lock
12417edc3e3bSEric Anholt * @count: Number of objects in @objs
12427edc3e3bSEric Anholt * @acquire_ctx: struct ww_acquire_ctx that will be initialized as
12437edc3e3bSEric Anholt * part of tracking this set of locked reservations.
12447edc3e3bSEric Anholt */
12457edc3e3bSEric Anholt int
drm_gem_lock_reservations(struct drm_gem_object ** objs,int count,struct ww_acquire_ctx * acquire_ctx)12467edc3e3bSEric Anholt drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
12477edc3e3bSEric Anholt struct ww_acquire_ctx *acquire_ctx)
12487edc3e3bSEric Anholt {
12497edc3e3bSEric Anholt int contended = -1;
12507edc3e3bSEric Anholt int i, ret;
12517edc3e3bSEric Anholt
12527edc3e3bSEric Anholt ww_acquire_init(acquire_ctx, &reservation_ww_class);
12537edc3e3bSEric Anholt
12547edc3e3bSEric Anholt retry:
12557edc3e3bSEric Anholt if (contended != -1) {
12567edc3e3bSEric Anholt struct drm_gem_object *obj = objs[contended];
12577edc3e3bSEric Anholt
125852791eeeSChristian König ret = dma_resv_lock_slow_interruptible(obj->resv,
12597edc3e3bSEric Anholt acquire_ctx);
12607edc3e3bSEric Anholt if (ret) {
12612939deacSDmitry Osipenko ww_acquire_fini(acquire_ctx);
12627edc3e3bSEric Anholt return ret;
12637edc3e3bSEric Anholt }
12647edc3e3bSEric Anholt }
12657edc3e3bSEric Anholt
12667edc3e3bSEric Anholt for (i = 0; i < count; i++) {
12677edc3e3bSEric Anholt if (i == contended)
12687edc3e3bSEric Anholt continue;
12697edc3e3bSEric Anholt
127052791eeeSChristian König ret = dma_resv_lock_interruptible(objs[i]->resv,
12717edc3e3bSEric Anholt acquire_ctx);
12727edc3e3bSEric Anholt if (ret) {
12737edc3e3bSEric Anholt int j;
12747edc3e3bSEric Anholt
12757edc3e3bSEric Anholt for (j = 0; j < i; j++)
127652791eeeSChristian König dma_resv_unlock(objs[j]->resv);
12777edc3e3bSEric Anholt
12787edc3e3bSEric Anholt if (contended != -1 && contended >= i)
127952791eeeSChristian König dma_resv_unlock(objs[contended]->resv);
12807edc3e3bSEric Anholt
12817edc3e3bSEric Anholt if (ret == -EDEADLK) {
12827edc3e3bSEric Anholt contended = i;
12837edc3e3bSEric Anholt goto retry;
12847edc3e3bSEric Anholt }
12857edc3e3bSEric Anholt
12862939deacSDmitry Osipenko ww_acquire_fini(acquire_ctx);
12877edc3e3bSEric Anholt return ret;
12887edc3e3bSEric Anholt }
12897edc3e3bSEric Anholt }
12907edc3e3bSEric Anholt
12917edc3e3bSEric Anholt ww_acquire_done(acquire_ctx);
12927edc3e3bSEric Anholt
12937edc3e3bSEric Anholt return 0;
12947edc3e3bSEric Anholt }
12957edc3e3bSEric Anholt EXPORT_SYMBOL(drm_gem_lock_reservations);
12967edc3e3bSEric Anholt
12977edc3e3bSEric Anholt void
drm_gem_unlock_reservations(struct drm_gem_object ** objs,int count,struct ww_acquire_ctx * acquire_ctx)12987edc3e3bSEric Anholt drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
12997edc3e3bSEric Anholt struct ww_acquire_ctx *acquire_ctx)
13007edc3e3bSEric Anholt {
13017edc3e3bSEric Anholt int i;
13027edc3e3bSEric Anholt
13037edc3e3bSEric Anholt for (i = 0; i < count; i++)
130452791eeeSChristian König dma_resv_unlock(objs[i]->resv);
13057edc3e3bSEric Anholt
13067edc3e3bSEric Anholt ww_acquire_fini(acquire_ctx);
13077edc3e3bSEric Anholt }
13087edc3e3bSEric Anholt EXPORT_SYMBOL(drm_gem_unlock_reservations);
1309e7c2af13SRob Clark
1310e7c2af13SRob Clark /**
1311e7c2af13SRob Clark * drm_gem_lru_init - initialize a LRU
1312e7c2af13SRob Clark *
1313e7c2af13SRob Clark * @lru: The LRU to initialize
1314e7c2af13SRob Clark * @lock: The lock protecting the LRU
1315e7c2af13SRob Clark */
1316e7c2af13SRob Clark void
drm_gem_lru_init(struct drm_gem_lru * lru,struct mutex * lock)1317e7c2af13SRob Clark drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock)
1318e7c2af13SRob Clark {
1319e7c2af13SRob Clark lru->lock = lock;
1320e7c2af13SRob Clark lru->count = 0;
1321e7c2af13SRob Clark INIT_LIST_HEAD(&lru->list);
1322e7c2af13SRob Clark }
1323e7c2af13SRob Clark EXPORT_SYMBOL(drm_gem_lru_init);
1324e7c2af13SRob Clark
1325e7c2af13SRob Clark static void
drm_gem_lru_remove_locked(struct drm_gem_object * obj)1326e7c2af13SRob Clark drm_gem_lru_remove_locked(struct drm_gem_object *obj)
1327e7c2af13SRob Clark {
1328e7c2af13SRob Clark obj->lru->count -= obj->size >> PAGE_SHIFT;
1329e7c2af13SRob Clark WARN_ON(obj->lru->count < 0);
1330e7c2af13SRob Clark list_del(&obj->lru_node);
1331e7c2af13SRob Clark obj->lru = NULL;
1332e7c2af13SRob Clark }
1333e7c2af13SRob Clark
1334e7c2af13SRob Clark /**
1335e7c2af13SRob Clark * drm_gem_lru_remove - remove object from whatever LRU it is in
1336e7c2af13SRob Clark *
1337e7c2af13SRob Clark * If the object is currently in any LRU, remove it.
1338e7c2af13SRob Clark *
1339e7c2af13SRob Clark * @obj: The GEM object to remove from current LRU
1340e7c2af13SRob Clark */
1341e7c2af13SRob Clark void
drm_gem_lru_remove(struct drm_gem_object * obj)1342e7c2af13SRob Clark drm_gem_lru_remove(struct drm_gem_object *obj)
1343e7c2af13SRob Clark {
1344e7c2af13SRob Clark struct drm_gem_lru *lru = obj->lru;
1345e7c2af13SRob Clark
1346e7c2af13SRob Clark if (!lru)
1347e7c2af13SRob Clark return;
1348e7c2af13SRob Clark
1349e7c2af13SRob Clark mutex_lock(lru->lock);
1350e7c2af13SRob Clark drm_gem_lru_remove_locked(obj);
1351e7c2af13SRob Clark mutex_unlock(lru->lock);
1352e7c2af13SRob Clark }
1353e7c2af13SRob Clark EXPORT_SYMBOL(drm_gem_lru_remove);
1354e7c2af13SRob Clark
1355b43f9afbSRob Clark /**
1356b43f9afbSRob Clark * drm_gem_lru_move_tail_locked - move the object to the tail of the LRU
1357b43f9afbSRob Clark *
1358b43f9afbSRob Clark * Like &drm_gem_lru_move_tail but lru lock must be held
1359b43f9afbSRob Clark *
1360b43f9afbSRob Clark * @lru: The LRU to move the object into.
1361b43f9afbSRob Clark * @obj: The GEM object to move into this LRU
1362b43f9afbSRob Clark */
1363b43f9afbSRob Clark void
drm_gem_lru_move_tail_locked(struct drm_gem_lru * lru,struct drm_gem_object * obj)1364e7c2af13SRob Clark drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj)
1365e7c2af13SRob Clark {
1366e7c2af13SRob Clark lockdep_assert_held_once(lru->lock);
1367e7c2af13SRob Clark
1368e7c2af13SRob Clark if (obj->lru)
1369e7c2af13SRob Clark drm_gem_lru_remove_locked(obj);
1370e7c2af13SRob Clark
1371e7c2af13SRob Clark lru->count += obj->size >> PAGE_SHIFT;
1372e7c2af13SRob Clark list_add_tail(&obj->lru_node, &lru->list);
1373e7c2af13SRob Clark obj->lru = lru;
1374e7c2af13SRob Clark }
1375b43f9afbSRob Clark EXPORT_SYMBOL(drm_gem_lru_move_tail_locked);
1376e7c2af13SRob Clark
1377e7c2af13SRob Clark /**
1378e7c2af13SRob Clark * drm_gem_lru_move_tail - move the object to the tail of the LRU
1379e7c2af13SRob Clark *
1380e7c2af13SRob Clark * If the object is already in this LRU it will be moved to the
1381e7c2af13SRob Clark * tail. Otherwise it will be removed from whichever other LRU
1382e7c2af13SRob Clark * it is in (if any) and moved into this LRU.
1383e7c2af13SRob Clark *
1384e7c2af13SRob Clark * @lru: The LRU to move the object into.
1385e7c2af13SRob Clark * @obj: The GEM object to move into this LRU
1386e7c2af13SRob Clark */
1387e7c2af13SRob Clark void
drm_gem_lru_move_tail(struct drm_gem_lru * lru,struct drm_gem_object * obj)1388e7c2af13SRob Clark drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj)
1389e7c2af13SRob Clark {
1390e7c2af13SRob Clark mutex_lock(lru->lock);
1391e7c2af13SRob Clark drm_gem_lru_move_tail_locked(lru, obj);
1392e7c2af13SRob Clark mutex_unlock(lru->lock);
1393e7c2af13SRob Clark }
1394e7c2af13SRob Clark EXPORT_SYMBOL(drm_gem_lru_move_tail);
1395e7c2af13SRob Clark
1396e7c2af13SRob Clark /**
1397e7c2af13SRob Clark * drm_gem_lru_scan - helper to implement shrinker.scan_objects
1398e7c2af13SRob Clark *
1399e7c2af13SRob Clark * If the shrink callback succeeds, it is expected that the driver
1400e7c2af13SRob Clark * move the object out of this LRU.
1401e7c2af13SRob Clark *
1402e7c2af13SRob Clark * If the LRU possibly contain active buffers, it is the responsibility
1403e7c2af13SRob Clark * of the shrink callback to check for this (ie. dma_resv_test_signaled())
1404e7c2af13SRob Clark * or if necessary block until the buffer becomes idle.
1405e7c2af13SRob Clark *
1406e7c2af13SRob Clark * @lru: The LRU to scan
1407e7c2af13SRob Clark * @nr_to_scan: The number of pages to try to reclaim
14089630b585SDmitry Osipenko * @remaining: The number of pages left to reclaim, should be initialized by caller
1409e7c2af13SRob Clark * @shrink: Callback to try to shrink/reclaim the object.
1410e7c2af13SRob Clark */
1411e7c2af13SRob Clark unsigned long
drm_gem_lru_scan(struct drm_gem_lru * lru,unsigned int nr_to_scan,unsigned long * remaining,bool (* shrink)(struct drm_gem_object * obj))14129630b585SDmitry Osipenko drm_gem_lru_scan(struct drm_gem_lru *lru,
14139630b585SDmitry Osipenko unsigned int nr_to_scan,
14149630b585SDmitry Osipenko unsigned long *remaining,
1415e7c2af13SRob Clark bool (*shrink)(struct drm_gem_object *obj))
1416e7c2af13SRob Clark {
1417e7c2af13SRob Clark struct drm_gem_lru still_in_lru;
1418e7c2af13SRob Clark struct drm_gem_object *obj;
1419e7c2af13SRob Clark unsigned freed = 0;
1420e7c2af13SRob Clark
1421e7c2af13SRob Clark drm_gem_lru_init(&still_in_lru, lru->lock);
1422e7c2af13SRob Clark
1423e7c2af13SRob Clark mutex_lock(lru->lock);
1424e7c2af13SRob Clark
1425e7c2af13SRob Clark while (freed < nr_to_scan) {
1426e7c2af13SRob Clark obj = list_first_entry_or_null(&lru->list, typeof(*obj), lru_node);
1427e7c2af13SRob Clark
1428e7c2af13SRob Clark if (!obj)
1429e7c2af13SRob Clark break;
1430e7c2af13SRob Clark
1431e7c2af13SRob Clark drm_gem_lru_move_tail_locked(&still_in_lru, obj);
1432e7c2af13SRob Clark
1433e7c2af13SRob Clark /*
1434e7c2af13SRob Clark * If it's in the process of being freed, gem_object->free()
1435e7c2af13SRob Clark * may be blocked on lock waiting to remove it. So just
1436e7c2af13SRob Clark * skip it.
1437e7c2af13SRob Clark */
1438e7c2af13SRob Clark if (!kref_get_unless_zero(&obj->refcount))
1439e7c2af13SRob Clark continue;
1440e7c2af13SRob Clark
1441e7c2af13SRob Clark /*
1442e7c2af13SRob Clark * Now that we own a reference, we can drop the lock for the
1443e7c2af13SRob Clark * rest of the loop body, to reduce contention with other
1444e7c2af13SRob Clark * code paths that need the LRU lock
1445e7c2af13SRob Clark */
1446e7c2af13SRob Clark mutex_unlock(lru->lock);
1447e7c2af13SRob Clark
1448e7c2af13SRob Clark /*
1449e7c2af13SRob Clark * Note that this still needs to be trylock, since we can
1450e7c2af13SRob Clark * hit shrinker in response to trying to get backing pages
1451e7c2af13SRob Clark * for this obj (ie. while it's lock is already held)
1452e7c2af13SRob Clark */
14539630b585SDmitry Osipenko if (!dma_resv_trylock(obj->resv)) {
14549630b585SDmitry Osipenko *remaining += obj->size >> PAGE_SHIFT;
1455e7c2af13SRob Clark goto tail;
14569630b585SDmitry Osipenko }
1457e7c2af13SRob Clark
1458e7c2af13SRob Clark if (shrink(obj)) {
1459e7c2af13SRob Clark freed += obj->size >> PAGE_SHIFT;
1460e7c2af13SRob Clark
1461e7c2af13SRob Clark /*
1462e7c2af13SRob Clark * If we succeeded in releasing the object's backing
1463e7c2af13SRob Clark * pages, we expect the driver to have moved the object
1464e7c2af13SRob Clark * out of this LRU
1465e7c2af13SRob Clark */
1466e7c2af13SRob Clark WARN_ON(obj->lru == &still_in_lru);
1467e7c2af13SRob Clark WARN_ON(obj->lru == lru);
1468e7c2af13SRob Clark }
1469e7c2af13SRob Clark
1470e7c2af13SRob Clark dma_resv_unlock(obj->resv);
1471e7c2af13SRob Clark
1472e7c2af13SRob Clark tail:
1473e7c2af13SRob Clark drm_gem_object_put(obj);
1474e7c2af13SRob Clark mutex_lock(lru->lock);
1475e7c2af13SRob Clark }
1476e7c2af13SRob Clark
1477e7c2af13SRob Clark /*
1478e7c2af13SRob Clark * Move objects we've skipped over out of the temporary still_in_lru
1479e7c2af13SRob Clark * back into this LRU
1480e7c2af13SRob Clark */
1481e7c2af13SRob Clark list_for_each_entry (obj, &still_in_lru.list, lru_node)
1482e7c2af13SRob Clark obj->lru = lru;
1483e7c2af13SRob Clark list_splice_tail(&still_in_lru.list, &lru->list);
1484e7c2af13SRob Clark lru->count += still_in_lru.count;
1485e7c2af13SRob Clark
1486e7c2af13SRob Clark mutex_unlock(lru->lock);
1487e7c2af13SRob Clark
1488e7c2af13SRob Clark return freed;
1489e7c2af13SRob Clark }
1490e7c2af13SRob Clark EXPORT_SYMBOL(drm_gem_lru_scan);
14917eabaa89SDmitry Osipenko
14927eabaa89SDmitry Osipenko /**
14937eabaa89SDmitry Osipenko * drm_gem_evict - helper to evict backing pages for a GEM object
14947eabaa89SDmitry Osipenko * @obj: obj in question
14957eabaa89SDmitry Osipenko */
drm_gem_evict(struct drm_gem_object * obj)14967eabaa89SDmitry Osipenko int drm_gem_evict(struct drm_gem_object *obj)
14977eabaa89SDmitry Osipenko {
14987eabaa89SDmitry Osipenko dma_resv_assert_held(obj->resv);
14997eabaa89SDmitry Osipenko
15007eabaa89SDmitry Osipenko if (!dma_resv_test_signaled(obj->resv, DMA_RESV_USAGE_READ))
15017eabaa89SDmitry Osipenko return -EBUSY;
15027eabaa89SDmitry Osipenko
15037eabaa89SDmitry Osipenko if (obj->funcs->evict)
15047eabaa89SDmitry Osipenko return obj->funcs->evict(obj);
15057eabaa89SDmitry Osipenko
15067eabaa89SDmitry Osipenko return 0;
15077eabaa89SDmitry Osipenko }
15087eabaa89SDmitry Osipenko EXPORT_SYMBOL(drm_gem_evict);
1509