1768 lines · 47.6 KB · C
RawDownload
/* * Copyright © 2008 Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. * * Authors: *    Eric Anholt <eric@anholt.net> * */ #include <linux/dma-buf.h>#include <linux/export.h>#include <linux/file.h>#include <linux/fs.h>#ifdef CONFIG_TRANSPARENT_HUGEPAGE#include <linux/fs_context.h>#endif#include <linux/iosys-map.h>#include <linux/mem_encrypt.h>#include <linux/mm.h>#include <linux/mman.h>#include <linux/module.h>#include <linux/pagemap.h>#include <linux/folio_batch.h>#include <linux/sched/mm.h>#include <linux/shmem_fs.h>#include <linux/slab.h>#include <linux/string_helpers.h>#include <linux/types.h>#include <linux/uaccess.h> #include <drm/drm.h>#include <drm/drm_device.h>#include <drm/drm_drv.h>#include <drm/drm_file.h>#include <drm/drm_gem.h>#include <drm/drm_managed.h>#include <drm/drm_print.h>#include <drm/drm_vma_manager.h> #include "drm_internal.h" /** @file drm_gem.c * * This file provides some of the base ioctls and library routines for * the graphics memory manager implemented by each device driver. * * Because various devices have different requirements in terms of * synchronization and migration strategies, implementing that is left up to * the driver, and all that the general API provides should be generic -- * allocating objects, reading/writing data with the cpu, freeing objects. * Even there, platform-dependent optimizations for reading/writing data with * the CPU mean we'll likely hook those out to driver-specific calls.  However, * the DRI2 implementation wants to have at least allocate/mmap be generic. * * The goal was to have swap-backed object allocation managed through * struct file.  However, file descriptors as handles to a struct file have * two major failings: * - Process limits prevent more than 1024 or so being used at a time by *   default. * - Inability to allocate high fds will aggravate the X Server's select() *   handling, and likely that of many GL client applications as well. * * This led to a plan of using our own integer IDs (called handles, following * DRM terminology) to mimic fds, and implement the fd syscalls we need as * ioctls.  The objects themselves will still include the struct file so * that we can transition to fds if the required kernel infrastructure shows * up at a later date, and as our interface with shmfs for memory allocation. */ #ifdef CONFIG_TRANSPARENT_HUGEPAGEstatic void drm_gem_huge_mnt_free(struct drm_device *dev, void *data){	kern_unmount(dev->huge_mnt);} /** * drm_gem_huge_mnt_create - Create, mount and use a huge tmpfs mountpoint * @dev: DRM device that will use the huge tmpfs mountpoint * @value: huge tmpfs mount option value * * This function creates and mounts a dedicated huge tmpfs mountpoint for the * lifetime of the DRM device @dev which is used at GEM object initialization * with drm_gem_object_init(). * * The most common option for @value is "within_size" which only allocates huge * pages if the page will be fully within the GEM object size. "always", * "advise" and "never" are supported too but the latter would just create a * mountpoint similar to the default one (`shm_mnt`). See shmemfs and * Transparent Hugepage for more information. * * Returns: * 0 on success or a negative error code on failure. */int drm_gem_huge_mnt_create(struct drm_device *dev, const char *value){	struct file_system_type *type;	struct fs_context *fc;	int ret; 	if (unlikely(drm_gem_get_huge_mnt(dev)))		return 0; 	type = get_fs_type("tmpfs");	if (unlikely(!type))		return -EOPNOTSUPP;	fc = fs_context_for_mount(type, SB_KERNMOUNT);	if (IS_ERR(fc))		return PTR_ERR(fc);	ret = vfs_parse_fs_string(fc, "source", "tmpfs");	if (unlikely(ret))		return -ENOPARAM;	ret = vfs_parse_fs_string(fc, "huge", value);	if (unlikely(ret))		return -ENOPARAM; 	dev->huge_mnt = fc_mount_longterm(fc);	put_fs_context(fc); 	return drmm_add_action_or_reset(dev, drm_gem_huge_mnt_free, NULL);}EXPORT_SYMBOL_GPL(drm_gem_huge_mnt_create);#endif static voiddrm_gem_init_release(struct drm_device *dev, void *ptr){	drm_vma_offset_manager_destroy(dev->vma_offset_manager);} /** * drm_gem_init - Initialize the GEM device fields * @dev: drm_devic structure to initialize */intdrm_gem_init(struct drm_device *dev){	struct drm_vma_offset_manager *vma_offset_manager; 	mutex_init(&dev->object_name_lock);	idr_init_base(&dev->object_name_idr, 1); 	vma_offset_manager = drmm_kzalloc(dev, sizeof(*vma_offset_manager),					  GFP_KERNEL);	if (!vma_offset_manager)		return -ENOMEM; 	dev->vma_offset_manager = vma_offset_manager;	drm_vma_offset_manager_init(vma_offset_manager,				    DRM_FILE_PAGE_OFFSET_START,				    DRM_FILE_PAGE_OFFSET_SIZE); 	return drmm_add_action(dev, drm_gem_init_release, NULL);} /** * drm_gem_object_init - initialize an allocated shmem-backed GEM object * * @dev: drm_device the object should be initialized for * @obj: drm_gem_object to initialize * @size: object size * * Initialize an already allocated GEM object of the specified size with * shmfs backing store. A huge mountpoint can be used by calling * drm_gem_huge_mnt_create() beforehand. */int drm_gem_object_init(struct drm_device *dev, struct drm_gem_object *obj,			size_t size){	struct vfsmount *huge_mnt;	struct file *filp;	const vma_flags_t flags = mk_vma_flags(VMA_NORESERVE_BIT); 	drm_gem_private_object_init(dev, obj, size); 	huge_mnt = drm_gem_get_huge_mnt(dev);	if (huge_mnt)		filp = shmem_file_setup_with_mnt(huge_mnt, "drm mm object",						 size, flags);	else		filp = shmem_file_setup("drm mm object", size, flags); 	if (IS_ERR(filp))		return PTR_ERR(filp); 	obj->filp = filp; 	return 0;}EXPORT_SYMBOL(drm_gem_object_init); /** * drm_gem_private_object_init - initialize an allocated private GEM object * @dev: drm_device the object should be initialized for * @obj: drm_gem_object to initialize * @size: object size * * Initialize an already allocated GEM object of the specified size with * no GEM provided backing store. Instead the caller is responsible for * backing the object and handling it. */void drm_gem_private_object_init(struct drm_device *dev,				 struct drm_gem_object *obj, size_t size){	BUG_ON((size & (PAGE_SIZE - 1)) != 0); 	obj->dev = dev;	obj->filp = NULL; 	kref_init(&obj->refcount);	obj->handle_count = 0;	obj->size = size;	mutex_init(&obj->gpuva.lock);	dma_resv_init(&obj->_resv);	if (!obj->resv)		obj->resv = &obj->_resv; 	drm_gem_gpuva_init(obj); 	drm_vma_node_reset(&obj->vma_node);	INIT_LIST_HEAD(&obj->lru_node);}EXPORT_SYMBOL(drm_gem_private_object_init); /** * drm_gem_private_object_fini - Finalize a failed drm_gem_object * @obj: drm_gem_object * * Uninitialize an already allocated GEM object when it initialized failed */void drm_gem_private_object_fini(struct drm_gem_object *obj){	WARN_ON(obj->dma_buf); 	dma_resv_fini(&obj->_resv);	mutex_destroy(&obj->gpuva.lock);}EXPORT_SYMBOL(drm_gem_private_object_fini); static void drm_gem_object_handle_get(struct drm_gem_object *obj){	struct drm_device *dev = obj->dev; 	drm_WARN_ON(dev, !mutex_is_locked(&dev->object_name_lock)); 	if (obj->handle_count++ == 0)		drm_gem_object_get(obj);} /** * drm_gem_object_handle_get_if_exists_unlocked - acquire reference on user-space handle, if any * @obj: GEM object * * Acquires a reference on the GEM buffer object's handle. Required to keep * the GEM object alive. Call drm_gem_object_handle_put_if_exists_unlocked() * to release the reference. Does nothing if the buffer object has no handle. * * Returns: * True if a handle exists, or false otherwise */bool drm_gem_object_handle_get_if_exists_unlocked(struct drm_gem_object *obj){	struct drm_device *dev = obj->dev; 	guard(mutex)(&dev->object_name_lock); 	/*	 * First ref taken during GEM object creation, if any. Some	 * drivers set up internal framebuffers with GEM objects that	 * do not have a GEM handle. Hence, this counter can be zero.	 */	if (!obj->handle_count)		return false; 	drm_gem_object_handle_get(obj); 	return true;} /** * drm_gem_object_handle_free - release resources bound to userspace handles * @obj: GEM object to clean up. * * Called after the last handle to the object has been closed * * Removes any name for the object. Note that this must be * called before drm_gem_object_free or we'll be touching * freed memory */static void drm_gem_object_handle_free(struct drm_gem_object *obj){	struct drm_device *dev = obj->dev; 	/* Remove any name for this object */	if (obj->name) {		idr_remove(&dev->object_name_idr, obj->name);		obj->name = 0;	}} static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj){	/* Unbreak the reference cycle if we have an exported dma_buf. */	if (obj->dma_buf) {		dma_buf_put(obj->dma_buf);		obj->dma_buf = NULL;	}} /** * drm_gem_object_handle_put_unlocked - releases reference on user-space handle * @obj: GEM object * * Releases a reference on the GEM buffer object's handle. Possibly releases * the GEM buffer object and associated dma-buf objects. */void drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj){	struct drm_device *dev = obj->dev;	bool final = false; 	if (drm_WARN_ON(dev, READ_ONCE(obj->handle_count) == 0))		return; 	/*	 * Must bump handle count first as this may be the last	 * ref, in which case the object would disappear before	 * we checked for a name.	 */ 	mutex_lock(&dev->object_name_lock);	if (--obj->handle_count == 0) {		drm_gem_object_handle_free(obj);		drm_gem_object_exported_dma_buf_free(obj);		final = true;	}	mutex_unlock(&dev->object_name_lock); 	if (final)		drm_gem_object_put(obj);} /* * Called at device or object close to release the file's * handle references on objects. */static intdrm_gem_object_release_handle(int id, void *ptr, void *data){	struct drm_file *file_priv = data;	struct drm_gem_object *obj = ptr; 	if (drm_WARN_ON(obj->dev, !data))		return 0; 	if (obj->funcs->close)		obj->funcs->close(obj, file_priv); 	mutex_lock(&file_priv->prime.lock); 	drm_prime_remove_buf_handle(&file_priv->prime, id); 	mutex_unlock(&file_priv->prime.lock); 	drm_vma_node_revoke(&obj->vma_node, file_priv); 	drm_gem_object_handle_put_unlocked(obj); 	return 0;} /** * drm_gem_handle_delete - deletes the given file-private handle * @filp: drm file-private structure to use for the handle look up * @handle: userspace handle to delete * * Removes the GEM handle from the @filp lookup table which has been added with * drm_gem_handle_create(). If this is the last handle also cleans up linked * resources like GEM names. */intdrm_gem_handle_delete(struct drm_file *filp, u32 handle){	struct drm_gem_object *obj; 	spin_lock(&filp->table_lock); 	/* Check if we currently have a reference on the object */	obj = idr_replace(&filp->object_idr, NULL, handle);	spin_unlock(&filp->table_lock);	if (IS_ERR_OR_NULL(obj))		return -EINVAL; 	/* Release driver's reference and decrement refcount. */	drm_gem_object_release_handle(handle, obj, filp); 	/* And finally make the handle available for future allocations. */	spin_lock(&filp->table_lock);	idr_remove(&filp->object_idr, handle);	spin_unlock(&filp->table_lock); 	return 0;}EXPORT_SYMBOL(drm_gem_handle_delete); /** * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object * @file: drm file-private structure containing the gem object * @dev: corresponding drm_device * @handle: gem object handle * @offset: return location for the fake mmap offset * * This implements the &drm_driver.dumb_map_offset kms driver callback for * drivers which use gem to manage their backing storage. * * Returns: * 0 on success or a negative error code on failure. */int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,			    u32 handle, u64 *offset){	struct drm_gem_object *obj;	int ret; 	obj = drm_gem_object_lookup(file, handle);	if (!obj)		return -ENOENT; 	/* Don't allow imported objects to be mapped */	if (drm_gem_is_imported(obj)) {		ret = -EINVAL;		goto out;	} 	ret = drm_gem_create_mmap_offset(obj);	if (ret)		goto out; 	*offset = drm_vma_node_offset_addr(&obj->vma_node);out:	drm_gem_object_put(obj); 	return ret;}EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset); /** * drm_gem_handle_create_tail - internal functions to create a handle * @file_priv: drm file-private structure to register the handle for * @obj: object to register * @handlep: pointer to return the created handle to the caller * * This expects the &drm_device.object_name_lock to be held already and will * drop it before returning. Used to avoid races in establishing new handles * when importing an object from either an flink name or a dma-buf. * * Handles must be release again through drm_gem_handle_delete(). This is done * when userspace closes @file_priv for all attached handles, or through the * GEM_CLOSE ioctl for individual handles. */intdrm_gem_handle_create_tail(struct drm_file *file_priv,			   struct drm_gem_object *obj,			   u32 *handlep){	struct drm_device *dev = obj->dev;	u32 handle;	int ret; 	WARN_ON(!mutex_is_locked(&dev->object_name_lock)); 	drm_gem_object_handle_get(obj); 	/*	 * Get the user-visible handle using idr.  Preload and perform	 * allocation under our spinlock.	 */	idr_preload(GFP_KERNEL);	spin_lock(&file_priv->table_lock); 	ret = idr_alloc(&file_priv->object_idr, NULL, 1, 0, GFP_NOWAIT); 	spin_unlock(&file_priv->table_lock);

Lines 1–500 of 1768.