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 28673a394bSEric Anholt #include <linux/types.h> 29673a394bSEric Anholt #include <linux/slab.h> 30673a394bSEric Anholt #include <linux/mm.h> 31673a394bSEric Anholt #include <linux/uaccess.h> 32673a394bSEric Anholt #include <linux/fs.h> 33673a394bSEric Anholt #include <linux/file.h> 34673a394bSEric Anholt #include <linux/module.h> 35673a394bSEric Anholt #include <linux/mman.h> 36673a394bSEric Anholt #include <linux/pagemap.h> 37673a394bSEric Anholt #include "drmP.h" 38673a394bSEric Anholt 39673a394bSEric Anholt /** @file drm_gem.c 40673a394bSEric Anholt * 41673a394bSEric Anholt * This file provides some of the base ioctls and library routines for 42673a394bSEric Anholt * the graphics memory manager implemented by each device driver. 43673a394bSEric Anholt * 44673a394bSEric Anholt * Because various devices have different requirements in terms of 45673a394bSEric Anholt * synchronization and migration strategies, implementing that is left up to 46673a394bSEric Anholt * the driver, and all that the general API provides should be generic -- 47673a394bSEric Anholt * allocating objects, reading/writing data with the cpu, freeing objects. 48673a394bSEric Anholt * Even there, platform-dependent optimizations for reading/writing data with 49673a394bSEric Anholt * the CPU mean we'll likely hook those out to driver-specific calls. However, 50673a394bSEric Anholt * the DRI2 implementation wants to have at least allocate/mmap be generic. 51673a394bSEric Anholt * 52673a394bSEric Anholt * The goal was to have swap-backed object allocation managed through 53673a394bSEric Anholt * struct file. However, file descriptors as handles to a struct file have 54673a394bSEric Anholt * two major failings: 55673a394bSEric Anholt * - Process limits prevent more than 1024 or so being used at a time by 56673a394bSEric Anholt * default. 57673a394bSEric Anholt * - Inability to allocate high fds will aggravate the X Server's select() 58673a394bSEric Anholt * handling, and likely that of many GL client applications as well. 59673a394bSEric Anholt * 60673a394bSEric Anholt * This led to a plan of using our own integer IDs (called handles, following 61673a394bSEric Anholt * DRM terminology) to mimic fds, and implement the fd syscalls we need as 62673a394bSEric Anholt * ioctls. The objects themselves will still include the struct file so 63673a394bSEric Anholt * that we can transition to fds if the required kernel infrastructure shows 64673a394bSEric Anholt * up at a later date, and as our interface with shmfs for memory allocation. 65673a394bSEric Anholt */ 66673a394bSEric Anholt 67a2c0a97bSJesse Barnes /* 68a2c0a97bSJesse Barnes * We make up offsets for buffer objects so we can recognize them at 69a2c0a97bSJesse Barnes * mmap time. 70a2c0a97bSJesse Barnes */ 71a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1) 72a2c0a97bSJesse Barnes #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16) 73a2c0a97bSJesse Barnes 74673a394bSEric Anholt /** 75673a394bSEric Anholt * Initialize the GEM device fields 76673a394bSEric Anholt */ 77673a394bSEric Anholt 78673a394bSEric Anholt int 79673a394bSEric Anholt drm_gem_init(struct drm_device *dev) 80673a394bSEric Anholt { 81a2c0a97bSJesse Barnes struct drm_gem_mm *mm; 82a2c0a97bSJesse Barnes 83673a394bSEric Anholt spin_lock_init(&dev->object_name_lock); 84673a394bSEric Anholt idr_init(&dev->object_name_idr); 85673a394bSEric Anholt atomic_set(&dev->object_count, 0); 86673a394bSEric Anholt atomic_set(&dev->object_memory, 0); 87673a394bSEric Anholt atomic_set(&dev->pin_count, 0); 88673a394bSEric Anholt atomic_set(&dev->pin_memory, 0); 89673a394bSEric Anholt atomic_set(&dev->gtt_count, 0); 90673a394bSEric Anholt atomic_set(&dev->gtt_memory, 0); 91a2c0a97bSJesse Barnes 92a2c0a97bSJesse Barnes mm = drm_calloc(1, sizeof(struct drm_gem_mm), DRM_MEM_MM); 93a2c0a97bSJesse Barnes if (!mm) { 94a2c0a97bSJesse Barnes DRM_ERROR("out of memory\n"); 95a2c0a97bSJesse Barnes return -ENOMEM; 96a2c0a97bSJesse Barnes } 97a2c0a97bSJesse Barnes 98a2c0a97bSJesse Barnes dev->mm_private = mm; 99a2c0a97bSJesse Barnes 100a2c0a97bSJesse Barnes if (drm_ht_create(&mm->offset_hash, 19)) { 101a2c0a97bSJesse Barnes drm_free(mm, sizeof(struct drm_gem_mm), DRM_MEM_MM); 102a2c0a97bSJesse Barnes return -ENOMEM; 103a2c0a97bSJesse Barnes } 104a2c0a97bSJesse Barnes 105a2c0a97bSJesse Barnes if (drm_mm_init(&mm->offset_manager, DRM_FILE_PAGE_OFFSET_START, 106a2c0a97bSJesse Barnes DRM_FILE_PAGE_OFFSET_SIZE)) { 107a2c0a97bSJesse Barnes drm_ht_remove(&mm->offset_hash); 108ad45aa9eSChris Wilson drm_free(mm, sizeof(struct drm_gem_mm), DRM_MEM_MM); 109a2c0a97bSJesse Barnes return -ENOMEM; 110a2c0a97bSJesse Barnes } 111a2c0a97bSJesse Barnes 112673a394bSEric Anholt return 0; 113673a394bSEric Anholt } 114673a394bSEric Anholt 115a2c0a97bSJesse Barnes void 116a2c0a97bSJesse Barnes drm_gem_destroy(struct drm_device *dev) 117a2c0a97bSJesse Barnes { 118a2c0a97bSJesse Barnes struct drm_gem_mm *mm = dev->mm_private; 119a2c0a97bSJesse Barnes 120a2c0a97bSJesse Barnes drm_mm_takedown(&mm->offset_manager); 121a2c0a97bSJesse Barnes drm_ht_remove(&mm->offset_hash); 122a2c0a97bSJesse Barnes drm_free(mm, sizeof(struct drm_gem_mm), DRM_MEM_MM); 123a2c0a97bSJesse Barnes dev->mm_private = NULL; 124a2c0a97bSJesse Barnes } 125a2c0a97bSJesse Barnes 126673a394bSEric Anholt /** 127673a394bSEric Anholt * Allocate a GEM object of the specified size with shmfs backing store 128673a394bSEric Anholt */ 129673a394bSEric Anholt struct drm_gem_object * 130673a394bSEric Anholt drm_gem_object_alloc(struct drm_device *dev, size_t size) 131673a394bSEric Anholt { 132673a394bSEric Anholt struct drm_gem_object *obj; 133673a394bSEric Anholt 134673a394bSEric Anholt BUG_ON((size & (PAGE_SIZE - 1)) != 0); 135673a394bSEric Anholt 136673a394bSEric Anholt obj = kcalloc(1, sizeof(*obj), GFP_KERNEL); 137673a394bSEric Anholt 138673a394bSEric Anholt obj->dev = dev; 139fc8744adSLinus Torvalds obj->filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); 140673a394bSEric Anholt if (IS_ERR(obj->filp)) { 141673a394bSEric Anholt kfree(obj); 142673a394bSEric Anholt return NULL; 143673a394bSEric Anholt } 144673a394bSEric Anholt 145673a394bSEric Anholt kref_init(&obj->refcount); 146673a394bSEric Anholt kref_init(&obj->handlecount); 147673a394bSEric Anholt obj->size = size; 148673a394bSEric Anholt if (dev->driver->gem_init_object != NULL && 149673a394bSEric Anholt dev->driver->gem_init_object(obj) != 0) { 150673a394bSEric Anholt fput(obj->filp); 151673a394bSEric Anholt kfree(obj); 152673a394bSEric Anholt return NULL; 153673a394bSEric Anholt } 154673a394bSEric Anholt atomic_inc(&dev->object_count); 155673a394bSEric Anholt atomic_add(obj->size, &dev->object_memory); 156673a394bSEric Anholt return obj; 157673a394bSEric Anholt } 158673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_alloc); 159673a394bSEric Anholt 160673a394bSEric Anholt /** 161673a394bSEric Anholt * Removes the mapping from handle to filp for this object. 162673a394bSEric Anholt */ 163673a394bSEric Anholt static int 164673a394bSEric Anholt drm_gem_handle_delete(struct drm_file *filp, int handle) 165673a394bSEric Anholt { 166673a394bSEric Anholt struct drm_device *dev; 167673a394bSEric Anholt struct drm_gem_object *obj; 168673a394bSEric Anholt 169673a394bSEric Anholt /* This is gross. The idr system doesn't let us try a delete and 170673a394bSEric Anholt * return an error code. It just spews if you fail at deleting. 171673a394bSEric Anholt * So, we have to grab a lock around finding the object and then 172673a394bSEric Anholt * doing the delete on it and dropping the refcount, or the user 173673a394bSEric Anholt * could race us to double-decrement the refcount and cause a 174673a394bSEric Anholt * use-after-free later. Given the frequency of our handle lookups, 175673a394bSEric Anholt * we may want to use ida for number allocation and a hash table 176673a394bSEric Anholt * for the pointers, anyway. 177673a394bSEric Anholt */ 178673a394bSEric Anholt spin_lock(&filp->table_lock); 179673a394bSEric Anholt 180673a394bSEric Anholt /* Check if we currently have a reference on the object */ 181673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 182673a394bSEric Anholt if (obj == NULL) { 183673a394bSEric Anholt spin_unlock(&filp->table_lock); 184673a394bSEric Anholt return -EINVAL; 185673a394bSEric Anholt } 186673a394bSEric Anholt dev = obj->dev; 187673a394bSEric Anholt 188673a394bSEric Anholt /* Release reference and decrement refcount. */ 189673a394bSEric Anholt idr_remove(&filp->object_idr, handle); 190673a394bSEric Anholt spin_unlock(&filp->table_lock); 191673a394bSEric Anholt 192673a394bSEric Anholt mutex_lock(&dev->struct_mutex); 193673a394bSEric Anholt drm_gem_object_handle_unreference(obj); 194673a394bSEric Anholt mutex_unlock(&dev->struct_mutex); 195673a394bSEric Anholt 196673a394bSEric Anholt return 0; 197673a394bSEric Anholt } 198673a394bSEric Anholt 199673a394bSEric Anholt /** 200673a394bSEric Anholt * Create a handle for this object. This adds a handle reference 201673a394bSEric Anholt * to the object, which includes a regular reference count. Callers 202673a394bSEric Anholt * will likely want to dereference the object afterwards. 203673a394bSEric Anholt */ 204673a394bSEric Anholt int 205673a394bSEric Anholt drm_gem_handle_create(struct drm_file *file_priv, 206673a394bSEric Anholt struct drm_gem_object *obj, 207673a394bSEric Anholt int *handlep) 208673a394bSEric Anholt { 209673a394bSEric Anholt int ret; 210673a394bSEric Anholt 211673a394bSEric Anholt /* 212673a394bSEric Anholt * Get the user-visible handle using idr. 213673a394bSEric Anholt */ 214673a394bSEric Anholt again: 215673a394bSEric Anholt /* ensure there is space available to allocate a handle */ 216673a394bSEric Anholt if (idr_pre_get(&file_priv->object_idr, GFP_KERNEL) == 0) 217673a394bSEric Anholt return -ENOMEM; 218673a394bSEric Anholt 219673a394bSEric Anholt /* do the allocation under our spinlock */ 220673a394bSEric Anholt spin_lock(&file_priv->table_lock); 221673a394bSEric Anholt ret = idr_get_new_above(&file_priv->object_idr, obj, 1, handlep); 222673a394bSEric Anholt spin_unlock(&file_priv->table_lock); 223673a394bSEric Anholt if (ret == -EAGAIN) 224673a394bSEric Anholt goto again; 225673a394bSEric Anholt 226673a394bSEric Anholt if (ret != 0) 227673a394bSEric Anholt return ret; 228673a394bSEric Anholt 229673a394bSEric Anholt drm_gem_object_handle_reference(obj); 230673a394bSEric Anholt return 0; 231673a394bSEric Anholt } 232673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_handle_create); 233673a394bSEric Anholt 234673a394bSEric Anholt /** Returns a reference to the object named by the handle. */ 235673a394bSEric Anholt struct drm_gem_object * 236673a394bSEric Anholt drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, 237673a394bSEric Anholt int handle) 238673a394bSEric Anholt { 239673a394bSEric Anholt struct drm_gem_object *obj; 240673a394bSEric Anholt 241673a394bSEric Anholt spin_lock(&filp->table_lock); 242673a394bSEric Anholt 243673a394bSEric Anholt /* Check if we currently have a reference on the object */ 244673a394bSEric Anholt obj = idr_find(&filp->object_idr, handle); 245673a394bSEric Anholt if (obj == NULL) { 246673a394bSEric Anholt spin_unlock(&filp->table_lock); 247673a394bSEric Anholt return NULL; 248673a394bSEric Anholt } 249673a394bSEric Anholt 250673a394bSEric Anholt drm_gem_object_reference(obj); 251673a394bSEric Anholt 252673a394bSEric Anholt spin_unlock(&filp->table_lock); 253673a394bSEric Anholt 254673a394bSEric Anholt return obj; 255673a394bSEric Anholt } 256673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_lookup); 257673a394bSEric Anholt 258673a394bSEric Anholt /** 259673a394bSEric Anholt * Releases the handle to an mm object. 260673a394bSEric Anholt */ 261673a394bSEric Anholt int 262673a394bSEric Anholt drm_gem_close_ioctl(struct drm_device *dev, void *data, 263673a394bSEric Anholt struct drm_file *file_priv) 264673a394bSEric Anholt { 265673a394bSEric Anholt struct drm_gem_close *args = data; 266673a394bSEric Anholt int ret; 267673a394bSEric Anholt 268673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 269673a394bSEric Anholt return -ENODEV; 270673a394bSEric Anholt 271673a394bSEric Anholt ret = drm_gem_handle_delete(file_priv, args->handle); 272673a394bSEric Anholt 273673a394bSEric Anholt return ret; 274673a394bSEric Anholt } 275673a394bSEric Anholt 276673a394bSEric Anholt /** 277673a394bSEric Anholt * Create a global name for an object, returning the name. 278673a394bSEric Anholt * 279673a394bSEric Anholt * Note that the name does not hold a reference; when the object 280673a394bSEric Anholt * is freed, the name goes away. 281673a394bSEric Anholt */ 282673a394bSEric Anholt int 283673a394bSEric Anholt drm_gem_flink_ioctl(struct drm_device *dev, void *data, 284673a394bSEric Anholt struct drm_file *file_priv) 285673a394bSEric Anholt { 286673a394bSEric Anholt struct drm_gem_flink *args = data; 287673a394bSEric Anholt struct drm_gem_object *obj; 288673a394bSEric Anholt int ret; 289673a394bSEric Anholt 290673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 291673a394bSEric Anholt return -ENODEV; 292673a394bSEric Anholt 293673a394bSEric Anholt obj = drm_gem_object_lookup(dev, file_priv, args->handle); 294673a394bSEric Anholt if (obj == NULL) 295d4e7b898SEric Anholt return -EBADF; 296673a394bSEric Anholt 297673a394bSEric Anholt again: 2983e49c4f4SChris Wilson if (idr_pre_get(&dev->object_name_idr, GFP_KERNEL) == 0) { 2993e49c4f4SChris Wilson ret = -ENOMEM; 3003e49c4f4SChris Wilson goto err; 3013e49c4f4SChris Wilson } 302673a394bSEric Anholt 303673a394bSEric Anholt spin_lock(&dev->object_name_lock); 3048d59bae5SChris Wilson if (!obj->name) { 305673a394bSEric Anholt ret = idr_get_new_above(&dev->object_name_idr, obj, 1, 306673a394bSEric Anholt &obj->name); 3078d59bae5SChris Wilson args->name = (uint64_t) obj->name; 308673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 3098d59bae5SChris Wilson 310673a394bSEric Anholt if (ret == -EAGAIN) 311673a394bSEric Anholt goto again; 312673a394bSEric Anholt 3133e49c4f4SChris Wilson if (ret != 0) 3143e49c4f4SChris Wilson goto err; 315673a394bSEric Anholt 3168d59bae5SChris Wilson /* Allocate a reference for the name table. */ 3178d59bae5SChris Wilson drm_gem_object_reference(obj); 3188d59bae5SChris Wilson } else { 319673a394bSEric Anholt args->name = (uint64_t) obj->name; 3208d59bae5SChris Wilson spin_unlock(&dev->object_name_lock); 3218d59bae5SChris Wilson ret = 0; 3228d59bae5SChris Wilson } 3233e49c4f4SChris Wilson 3243e49c4f4SChris Wilson err: 3253e49c4f4SChris Wilson mutex_lock(&dev->struct_mutex); 3263e49c4f4SChris Wilson drm_gem_object_unreference(obj); 3273e49c4f4SChris Wilson mutex_unlock(&dev->struct_mutex); 3283e49c4f4SChris Wilson return ret; 329673a394bSEric Anholt } 330673a394bSEric Anholt 331673a394bSEric Anholt /** 332673a394bSEric Anholt * Open an object using the global name, returning a handle and the size. 333673a394bSEric Anholt * 334673a394bSEric Anholt * This handle (of course) holds a reference to the object, so the object 335673a394bSEric Anholt * will not go away until the handle is deleted. 336673a394bSEric Anholt */ 337673a394bSEric Anholt int 338673a394bSEric Anholt drm_gem_open_ioctl(struct drm_device *dev, void *data, 339673a394bSEric Anholt struct drm_file *file_priv) 340673a394bSEric Anholt { 341673a394bSEric Anholt struct drm_gem_open *args = data; 342673a394bSEric Anholt struct drm_gem_object *obj; 343673a394bSEric Anholt int ret; 344673a394bSEric Anholt int handle; 345673a394bSEric Anholt 346673a394bSEric Anholt if (!(dev->driver->driver_features & DRIVER_GEM)) 347673a394bSEric Anholt return -ENODEV; 348673a394bSEric Anholt 349673a394bSEric Anholt spin_lock(&dev->object_name_lock); 350673a394bSEric Anholt obj = idr_find(&dev->object_name_idr, (int) args->name); 351673a394bSEric Anholt if (obj) 352673a394bSEric Anholt drm_gem_object_reference(obj); 353673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 354673a394bSEric Anholt if (!obj) 355673a394bSEric Anholt return -ENOENT; 356673a394bSEric Anholt 357673a394bSEric Anholt ret = drm_gem_handle_create(file_priv, obj, &handle); 358673a394bSEric Anholt mutex_lock(&dev->struct_mutex); 359673a394bSEric Anholt drm_gem_object_unreference(obj); 360673a394bSEric Anholt mutex_unlock(&dev->struct_mutex); 361673a394bSEric Anholt if (ret) 362673a394bSEric Anholt return ret; 363673a394bSEric Anholt 364673a394bSEric Anholt args->handle = handle; 365673a394bSEric Anholt args->size = obj->size; 366673a394bSEric Anholt 367673a394bSEric Anholt return 0; 368673a394bSEric Anholt } 369673a394bSEric Anholt 370673a394bSEric Anholt /** 371673a394bSEric Anholt * Called at device open time, sets up the structure for handling refcounting 372673a394bSEric Anholt * of mm objects. 373673a394bSEric Anholt */ 374673a394bSEric Anholt void 375673a394bSEric Anholt drm_gem_open(struct drm_device *dev, struct drm_file *file_private) 376673a394bSEric Anholt { 377673a394bSEric Anholt idr_init(&file_private->object_idr); 378673a394bSEric Anholt spin_lock_init(&file_private->table_lock); 379673a394bSEric Anholt } 380673a394bSEric Anholt 381673a394bSEric Anholt /** 382673a394bSEric Anholt * Called at device close to release the file's 383673a394bSEric Anholt * handle references on objects. 384673a394bSEric Anholt */ 385673a394bSEric Anholt static int 386673a394bSEric Anholt drm_gem_object_release_handle(int id, void *ptr, void *data) 387673a394bSEric Anholt { 388673a394bSEric Anholt struct drm_gem_object *obj = ptr; 389673a394bSEric Anholt 390673a394bSEric Anholt drm_gem_object_handle_unreference(obj); 391673a394bSEric Anholt 392673a394bSEric Anholt return 0; 393673a394bSEric Anholt } 394673a394bSEric Anholt 395673a394bSEric Anholt /** 396673a394bSEric Anholt * Called at close time when the filp is going away. 397673a394bSEric Anholt * 398673a394bSEric Anholt * Releases any remaining references on objects by this filp. 399673a394bSEric Anholt */ 400673a394bSEric Anholt void 401673a394bSEric Anholt drm_gem_release(struct drm_device *dev, struct drm_file *file_private) 402673a394bSEric Anholt { 403673a394bSEric Anholt mutex_lock(&dev->struct_mutex); 404673a394bSEric Anholt idr_for_each(&file_private->object_idr, 405673a394bSEric Anholt &drm_gem_object_release_handle, NULL); 406673a394bSEric Anholt 407673a394bSEric Anholt idr_destroy(&file_private->object_idr); 408673a394bSEric Anholt mutex_unlock(&dev->struct_mutex); 409673a394bSEric Anholt } 410673a394bSEric Anholt 411673a394bSEric Anholt /** 412673a394bSEric Anholt * Called after the last reference to the object has been lost. 413673a394bSEric Anholt * 414673a394bSEric Anholt * Frees the object 415673a394bSEric Anholt */ 416673a394bSEric Anholt void 417673a394bSEric Anholt drm_gem_object_free(struct kref *kref) 418673a394bSEric Anholt { 419673a394bSEric Anholt struct drm_gem_object *obj = (struct drm_gem_object *) kref; 420673a394bSEric Anholt struct drm_device *dev = obj->dev; 421673a394bSEric Anholt 422673a394bSEric Anholt BUG_ON(!mutex_is_locked(&dev->struct_mutex)); 423673a394bSEric Anholt 424673a394bSEric Anholt if (dev->driver->gem_free_object != NULL) 425673a394bSEric Anholt dev->driver->gem_free_object(obj); 426673a394bSEric Anholt 427673a394bSEric Anholt fput(obj->filp); 428673a394bSEric Anholt atomic_dec(&dev->object_count); 429673a394bSEric Anholt atomic_sub(obj->size, &dev->object_memory); 430673a394bSEric Anholt kfree(obj); 431673a394bSEric Anholt } 432673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_free); 433673a394bSEric Anholt 434673a394bSEric Anholt /** 435673a394bSEric Anholt * Called after the last handle to the object has been closed 436673a394bSEric Anholt * 437673a394bSEric Anholt * Removes any name for the object. Note that this must be 438673a394bSEric Anholt * called before drm_gem_object_free or we'll be touching 439673a394bSEric Anholt * freed memory 440673a394bSEric Anholt */ 441673a394bSEric Anholt void 442673a394bSEric Anholt drm_gem_object_handle_free(struct kref *kref) 443673a394bSEric Anholt { 444673a394bSEric Anholt struct drm_gem_object *obj = container_of(kref, 445673a394bSEric Anholt struct drm_gem_object, 446673a394bSEric Anholt handlecount); 447673a394bSEric Anholt struct drm_device *dev = obj->dev; 448673a394bSEric Anholt 449673a394bSEric Anholt /* Remove any name for this object */ 450673a394bSEric Anholt spin_lock(&dev->object_name_lock); 451673a394bSEric Anholt if (obj->name) { 452673a394bSEric Anholt idr_remove(&dev->object_name_idr, obj->name); 4538d59bae5SChris Wilson obj->name = 0; 454673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 455673a394bSEric Anholt /* 456673a394bSEric Anholt * The object name held a reference to this object, drop 457673a394bSEric Anholt * that now. 458673a394bSEric Anholt */ 459673a394bSEric Anholt drm_gem_object_unreference(obj); 460673a394bSEric Anholt } else 461673a394bSEric Anholt spin_unlock(&dev->object_name_lock); 462673a394bSEric Anholt 463673a394bSEric Anholt } 464673a394bSEric Anholt EXPORT_SYMBOL(drm_gem_object_handle_free); 465673a394bSEric Anholt 466*ab00b3e5SJesse Barnes void drm_gem_vm_open(struct vm_area_struct *vma) 467*ab00b3e5SJesse Barnes { 468*ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 469*ab00b3e5SJesse Barnes 470*ab00b3e5SJesse Barnes drm_gem_object_reference(obj); 471*ab00b3e5SJesse Barnes } 472*ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_open); 473*ab00b3e5SJesse Barnes 474*ab00b3e5SJesse Barnes void drm_gem_vm_close(struct vm_area_struct *vma) 475*ab00b3e5SJesse Barnes { 476*ab00b3e5SJesse Barnes struct drm_gem_object *obj = vma->vm_private_data; 477*ab00b3e5SJesse Barnes struct drm_device *dev = obj->dev; 478*ab00b3e5SJesse Barnes 479*ab00b3e5SJesse Barnes mutex_lock(&dev->struct_mutex); 480*ab00b3e5SJesse Barnes drm_gem_object_unreference(obj); 481*ab00b3e5SJesse Barnes mutex_unlock(&dev->struct_mutex); 482*ab00b3e5SJesse Barnes } 483*ab00b3e5SJesse Barnes EXPORT_SYMBOL(drm_gem_vm_close); 484*ab00b3e5SJesse Barnes 485*ab00b3e5SJesse Barnes 486a2c0a97bSJesse Barnes /** 487a2c0a97bSJesse Barnes * drm_gem_mmap - memory map routine for GEM objects 488a2c0a97bSJesse Barnes * @filp: DRM file pointer 489a2c0a97bSJesse Barnes * @vma: VMA for the area to be mapped 490a2c0a97bSJesse Barnes * 491a2c0a97bSJesse Barnes * If a driver supports GEM object mapping, mmap calls on the DRM file 492a2c0a97bSJesse Barnes * descriptor will end up here. 493a2c0a97bSJesse Barnes * 494a2c0a97bSJesse Barnes * If we find the object based on the offset passed in (vma->vm_pgoff will 495a2c0a97bSJesse Barnes * contain the fake offset we created when the GTT map ioctl was called on 496a2c0a97bSJesse Barnes * the object), we set up the driver fault handler so that any accesses 497a2c0a97bSJesse Barnes * to the object can be trapped, to perform migration, GTT binding, surface 498a2c0a97bSJesse Barnes * register allocation, or performance monitoring. 499a2c0a97bSJesse Barnes */ 500a2c0a97bSJesse Barnes int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 501a2c0a97bSJesse Barnes { 502a2c0a97bSJesse Barnes struct drm_file *priv = filp->private_data; 503a2c0a97bSJesse Barnes struct drm_device *dev = priv->minor->dev; 504a2c0a97bSJesse Barnes struct drm_gem_mm *mm = dev->mm_private; 505a2c0a97bSJesse Barnes struct drm_map *map = NULL; 506a2c0a97bSJesse Barnes struct drm_gem_object *obj; 507a2c0a97bSJesse Barnes struct drm_hash_item *hash; 508a2c0a97bSJesse Barnes unsigned long prot; 509a2c0a97bSJesse Barnes int ret = 0; 510a2c0a97bSJesse Barnes 511a2c0a97bSJesse Barnes mutex_lock(&dev->struct_mutex); 512a2c0a97bSJesse Barnes 513a2c0a97bSJesse Barnes if (drm_ht_find_item(&mm->offset_hash, vma->vm_pgoff, &hash)) { 514a2c0a97bSJesse Barnes mutex_unlock(&dev->struct_mutex); 515a2c0a97bSJesse Barnes return drm_mmap(filp, vma); 516a2c0a97bSJesse Barnes } 517a2c0a97bSJesse Barnes 518a2c0a97bSJesse Barnes map = drm_hash_entry(hash, struct drm_map_list, hash)->map; 519a2c0a97bSJesse Barnes if (!map || 520a2c0a97bSJesse Barnes ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) { 521a2c0a97bSJesse Barnes ret = -EPERM; 522a2c0a97bSJesse Barnes goto out_unlock; 523a2c0a97bSJesse Barnes } 524a2c0a97bSJesse Barnes 525a2c0a97bSJesse Barnes /* Check for valid size. */ 526a2c0a97bSJesse Barnes if (map->size < vma->vm_end - vma->vm_start) { 527a2c0a97bSJesse Barnes ret = -EINVAL; 528a2c0a97bSJesse Barnes goto out_unlock; 529a2c0a97bSJesse Barnes } 530a2c0a97bSJesse Barnes 531a2c0a97bSJesse Barnes obj = map->handle; 532a2c0a97bSJesse Barnes if (!obj->dev->driver->gem_vm_ops) { 533a2c0a97bSJesse Barnes ret = -EINVAL; 534a2c0a97bSJesse Barnes goto out_unlock; 535a2c0a97bSJesse Barnes } 536a2c0a97bSJesse Barnes 537a2c0a97bSJesse Barnes vma->vm_flags |= VM_RESERVED | VM_IO | VM_PFNMAP | VM_DONTEXPAND; 538a2c0a97bSJesse Barnes vma->vm_ops = obj->dev->driver->gem_vm_ops; 539a2c0a97bSJesse Barnes vma->vm_private_data = map->handle; 540a2c0a97bSJesse Barnes /* FIXME: use pgprot_writecombine when available */ 541a2c0a97bSJesse Barnes prot = pgprot_val(vma->vm_page_prot); 542ae14dc05SDave Airlie #ifdef CONFIG_X86 543a2c0a97bSJesse Barnes prot |= _PAGE_CACHE_WC; 544ae14dc05SDave Airlie #endif 545a2c0a97bSJesse Barnes vma->vm_page_prot = __pgprot(prot); 546a2c0a97bSJesse Barnes 547*ab00b3e5SJesse Barnes /* Take a ref for this mapping of the object, so that the fault 548*ab00b3e5SJesse Barnes * handler can dereference the mmap offset's pointer to the object. 549*ab00b3e5SJesse Barnes * This reference is cleaned up by the corresponding vm_close 550*ab00b3e5SJesse Barnes * (which should happen whether the vma was created by this call, or 551*ab00b3e5SJesse Barnes * by a vm_open due to mremap or partial unmap or whatever). 552*ab00b3e5SJesse Barnes */ 553*ab00b3e5SJesse Barnes drm_gem_object_reference(obj); 554*ab00b3e5SJesse Barnes 555a2c0a97bSJesse Barnes vma->vm_file = filp; /* Needed for drm_vm_open() */ 556a2c0a97bSJesse Barnes drm_vm_open_locked(vma); 557a2c0a97bSJesse Barnes 558a2c0a97bSJesse Barnes out_unlock: 559a2c0a97bSJesse Barnes mutex_unlock(&dev->struct_mutex); 560a2c0a97bSJesse Barnes 561a2c0a97bSJesse Barnes return ret; 562a2c0a97bSJesse Barnes } 563a2c0a97bSJesse Barnes EXPORT_SYMBOL(drm_gem_mmap); 564