1 1.25 riastrad /* $NetBSD: drm_gem.c,v 1.25 2024/05/22 15:59:12 riastradh Exp $ */ 2 1.7 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2008 Intel Corporation 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice (including the next 14 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 15 1.1 riastrad * Software. 16 1.1 riastrad * 17 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 1.1 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 1.1 riastrad * IN THE SOFTWARE. 24 1.1 riastrad * 25 1.1 riastrad * Authors: 26 1.1 riastrad * Eric Anholt <eric (at) anholt.net> 27 1.1 riastrad * 28 1.1 riastrad */ 29 1.1 riastrad 30 1.7 riastrad #include <sys/cdefs.h> 31 1.25 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_gem.c,v 1.25 2024/05/22 15:59:12 riastradh Exp $"); 32 1.7 riastrad 33 1.1 riastrad #include <linux/types.h> 34 1.1 riastrad #include <linux/slab.h> 35 1.1 riastrad #include <linux/mm.h> 36 1.1 riastrad #include <linux/uaccess.h> 37 1.1 riastrad #include <linux/fs.h> 38 1.1 riastrad #include <linux/file.h> 39 1.1 riastrad #include <linux/module.h> 40 1.1 riastrad #include <linux/mman.h> 41 1.1 riastrad #include <linux/pagemap.h> 42 1.1 riastrad #include <linux/shmem_fs.h> 43 1.1 riastrad #include <linux/dma-buf.h> 44 1.16 riastrad #include <linux/mem_encrypt.h> 45 1.16 riastrad #include <linux/pagevec.h> 46 1.16 riastrad 47 1.16 riastrad #include <drm/drm.h> 48 1.16 riastrad #include <drm/drm_device.h> 49 1.16 riastrad #include <drm/drm_drv.h> 50 1.16 riastrad #include <drm/drm_file.h> 51 1.16 riastrad #include <drm/drm_gem.h> 52 1.16 riastrad #include <drm/drm_print.h> 53 1.3 riastrad #include <drm/drm_vma_manager.h> 54 1.16 riastrad 55 1.7 riastrad #include "drm_internal.h" 56 1.1 riastrad 57 1.4 riastrad #ifdef __NetBSD__ 58 1.4 riastrad #include <uvm/uvm_extern.h> 59 1.13 riastrad #include <linux/nbsd-namespace.h> 60 1.4 riastrad #endif 61 1.4 riastrad 62 1.1 riastrad /** @file drm_gem.c 63 1.1 riastrad * 64 1.1 riastrad * This file provides some of the base ioctls and library routines for 65 1.1 riastrad * the graphics memory manager implemented by each device driver. 66 1.1 riastrad * 67 1.1 riastrad * Because various devices have different requirements in terms of 68 1.1 riastrad * synchronization and migration strategies, implementing that is left up to 69 1.1 riastrad * the driver, and all that the general API provides should be generic -- 70 1.1 riastrad * allocating objects, reading/writing data with the cpu, freeing objects. 71 1.1 riastrad * Even there, platform-dependent optimizations for reading/writing data with 72 1.1 riastrad * the CPU mean we'll likely hook those out to driver-specific calls. However, 73 1.1 riastrad * the DRI2 implementation wants to have at least allocate/mmap be generic. 74 1.1 riastrad * 75 1.1 riastrad * The goal was to have swap-backed object allocation managed through 76 1.1 riastrad * struct file. However, file descriptors as handles to a struct file have 77 1.1 riastrad * two major failings: 78 1.1 riastrad * - Process limits prevent more than 1024 or so being used at a time by 79 1.1 riastrad * default. 80 1.1 riastrad * - Inability to allocate high fds will aggravate the X Server's select() 81 1.1 riastrad * handling, and likely that of many GL client applications as well. 82 1.1 riastrad * 83 1.1 riastrad * This led to a plan of using our own integer IDs (called handles, following 84 1.1 riastrad * DRM terminology) to mimic fds, and implement the fd syscalls we need as 85 1.1 riastrad * ioctls. The objects themselves will still include the struct file so 86 1.1 riastrad * that we can transition to fds if the required kernel infrastructure shows 87 1.1 riastrad * up at a later date, and as our interface with shmfs for memory allocation. 88 1.1 riastrad */ 89 1.1 riastrad 90 1.1 riastrad /** 91 1.3 riastrad * drm_gem_init - Initialize the GEM device fields 92 1.3 riastrad * @dev: drm_devic structure to initialize 93 1.1 riastrad */ 94 1.1 riastrad int 95 1.1 riastrad drm_gem_init(struct drm_device *dev) 96 1.1 riastrad { 97 1.3 riastrad struct drm_vma_offset_manager *vma_offset_manager; 98 1.1 riastrad 99 1.3 riastrad mutex_init(&dev->object_name_lock); 100 1.16 riastrad idr_init_base(&dev->object_name_idr, 1); 101 1.1 riastrad 102 1.3 riastrad vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL); 103 1.3 riastrad if (!vma_offset_manager) { 104 1.1 riastrad DRM_ERROR("out of memory\n"); 105 1.1 riastrad return -ENOMEM; 106 1.1 riastrad } 107 1.1 riastrad 108 1.3 riastrad dev->vma_offset_manager = vma_offset_manager; 109 1.3 riastrad drm_vma_offset_manager_init(vma_offset_manager, 110 1.3 riastrad DRM_FILE_PAGE_OFFSET_START, 111 1.3 riastrad DRM_FILE_PAGE_OFFSET_SIZE); 112 1.1 riastrad 113 1.1 riastrad return 0; 114 1.1 riastrad } 115 1.1 riastrad 116 1.1 riastrad void 117 1.1 riastrad drm_gem_destroy(struct drm_device *dev) 118 1.1 riastrad { 119 1.1 riastrad 120 1.3 riastrad drm_vma_offset_manager_destroy(dev->vma_offset_manager); 121 1.3 riastrad kfree(dev->vma_offset_manager); 122 1.3 riastrad dev->vma_offset_manager = NULL; 123 1.2 riastrad 124 1.2 riastrad idr_destroy(&dev->object_name_idr); 125 1.13 riastrad mutex_destroy(&dev->object_name_lock); 126 1.1 riastrad } 127 1.1 riastrad 128 1.1 riastrad /** 129 1.3 riastrad * drm_gem_object_init - initialize an allocated shmem-backed GEM object 130 1.3 riastrad * @dev: drm_device the object should be initialized for 131 1.3 riastrad * @obj: drm_gem_object to initialize 132 1.3 riastrad * @size: object size 133 1.3 riastrad * 134 1.1 riastrad * Initialize an already allocated GEM object of the specified size with 135 1.1 riastrad * shmfs backing store. 136 1.1 riastrad */ 137 1.1 riastrad int drm_gem_object_init(struct drm_device *dev, 138 1.1 riastrad struct drm_gem_object *obj, size_t size) 139 1.1 riastrad { 140 1.4 riastrad #ifndef __NetBSD__ 141 1.3 riastrad struct file *filp; 142 1.4 riastrad #endif 143 1.3 riastrad 144 1.3 riastrad drm_gem_private_object_init(dev, obj, size); 145 1.1 riastrad 146 1.2 riastrad #ifdef __NetBSD__ 147 1.6 riastrad /* 148 1.6 riastrad * A uao may not have size 0, but a gem object may. Allocate a 149 1.6 riastrad * spurious page so we needn't teach uao how to have size 0. 150 1.6 riastrad */ 151 1.8 riastrad obj->filp = uao_create(MAX(size, PAGE_SIZE), 0); 152 1.5 riastrad /* 153 1.5 riastrad * XXX This is gross. We ought to do it the other way around: 154 1.5 riastrad * set the uao to have the main uvm object's lock. However, 155 1.5 riastrad * uvm_obj_setlock is not safe on uvm_aobjs. 156 1.5 riastrad */ 157 1.15 ad rw_obj_hold(obj->filp->vmobjlock); 158 1.8 riastrad uvm_obj_setlock(&obj->gemo_uvmobj, obj->filp->vmobjlock); 159 1.2 riastrad #else 160 1.3 riastrad filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); 161 1.3 riastrad if (IS_ERR(filp)) 162 1.3 riastrad return PTR_ERR(filp); 163 1.3 riastrad 164 1.3 riastrad obj->filp = filp; 165 1.2 riastrad #endif 166 1.1 riastrad 167 1.1 riastrad return 0; 168 1.1 riastrad } 169 1.1 riastrad EXPORT_SYMBOL(drm_gem_object_init); 170 1.1 riastrad 171 1.1 riastrad /** 172 1.7 riastrad * drm_gem_private_object_init - initialize an allocated private GEM object 173 1.3 riastrad * @dev: drm_device the object should be initialized for 174 1.3 riastrad * @obj: drm_gem_object to initialize 175 1.3 riastrad * @size: object size 176 1.3 riastrad * 177 1.1 riastrad * Initialize an already allocated GEM object of the specified size with 178 1.1 riastrad * no GEM provided backing store. Instead the caller is responsible for 179 1.1 riastrad * backing the object and handling it. 180 1.1 riastrad */ 181 1.3 riastrad void drm_gem_private_object_init(struct drm_device *dev, 182 1.3 riastrad struct drm_gem_object *obj, size_t size) 183 1.1 riastrad { 184 1.1 riastrad BUG_ON((size & (PAGE_SIZE - 1)) != 0); 185 1.1 riastrad 186 1.1 riastrad obj->dev = dev; 187 1.2 riastrad #ifdef __NetBSD__ 188 1.8 riastrad obj->filp = NULL; 189 1.2 riastrad KASSERT(drm_core_check_feature(dev, DRIVER_GEM)); 190 1.2 riastrad KASSERT(dev->driver->gem_uvm_ops != NULL); 191 1.21 riastrad uvm_obj_init(&obj->gemo_uvmobj, dev->driver->gem_uvm_ops, 192 1.21 riastrad /*allocate lock*/true, /*nrefs*/1); 193 1.2 riastrad #else 194 1.1 riastrad obj->filp = NULL; 195 1.2 riastrad #endif 196 1.1 riastrad 197 1.1 riastrad kref_init(&obj->refcount); 198 1.3 riastrad obj->handle_count = 0; 199 1.1 riastrad obj->size = size; 200 1.16 riastrad dma_resv_init(&obj->_resv); 201 1.16 riastrad if (!obj->resv) 202 1.16 riastrad obj->resv = &obj->_resv; 203 1.16 riastrad 204 1.20 riastrad #ifdef __NetBSD__ 205 1.20 riastrad drm_vma_node_init(&obj->vma_node); 206 1.20 riastrad #else 207 1.3 riastrad drm_vma_node_reset(&obj->vma_node); 208 1.4 riastrad #endif 209 1.3 riastrad } 210 1.3 riastrad EXPORT_SYMBOL(drm_gem_private_object_init); 211 1.1 riastrad 212 1.3 riastrad static void 213 1.3 riastrad drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp) 214 1.3 riastrad { 215 1.3 riastrad /* 216 1.3 riastrad * Note: obj->dma_buf can't disappear as long as we still hold a 217 1.3 riastrad * handle reference in obj->handle_count. 218 1.3 riastrad */ 219 1.3 riastrad mutex_lock(&filp->prime.lock); 220 1.3 riastrad if (obj->dma_buf) { 221 1.3 riastrad drm_prime_remove_buf_handle_locked(&filp->prime, 222 1.3 riastrad obj->dma_buf); 223 1.3 riastrad } 224 1.3 riastrad mutex_unlock(&filp->prime.lock); 225 1.1 riastrad } 226 1.1 riastrad 227 1.1 riastrad /** 228 1.7 riastrad * drm_gem_object_handle_free - release resources bound to userspace handles 229 1.3 riastrad * @obj: GEM object to clean up. 230 1.3 riastrad * 231 1.3 riastrad * Called after the last handle to the object has been closed 232 1.3 riastrad * 233 1.3 riastrad * Removes any name for the object. Note that this must be 234 1.3 riastrad * called before drm_gem_object_free or we'll be touching 235 1.3 riastrad * freed memory 236 1.1 riastrad */ 237 1.3 riastrad static void drm_gem_object_handle_free(struct drm_gem_object *obj) 238 1.1 riastrad { 239 1.3 riastrad struct drm_device *dev = obj->dev; 240 1.3 riastrad 241 1.3 riastrad /* Remove any name for this object */ 242 1.3 riastrad if (obj->name) { 243 1.3 riastrad idr_remove(&dev->object_name_idr, obj->name); 244 1.3 riastrad obj->name = 0; 245 1.3 riastrad } 246 1.3 riastrad } 247 1.1 riastrad 248 1.3 riastrad static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj) 249 1.3 riastrad { 250 1.4 riastrad #ifndef __NetBSD__ 251 1.3 riastrad /* Unbreak the reference cycle if we have an exported dma_buf. */ 252 1.3 riastrad if (obj->dma_buf) { 253 1.3 riastrad dma_buf_put(obj->dma_buf); 254 1.3 riastrad obj->dma_buf = NULL; 255 1.1 riastrad } 256 1.4 riastrad #endif 257 1.1 riastrad } 258 1.1 riastrad 259 1.1 riastrad static void 260 1.16 riastrad drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj) 261 1.1 riastrad { 262 1.16 riastrad struct drm_device *dev = obj->dev; 263 1.16 riastrad bool final = false; 264 1.16 riastrad 265 1.3 riastrad if (WARN_ON(obj->handle_count == 0)) 266 1.3 riastrad return; 267 1.3 riastrad 268 1.3 riastrad /* 269 1.3 riastrad * Must bump handle count first as this may be the last 270 1.3 riastrad * ref, in which case the object would disappear before we 271 1.3 riastrad * checked for a name 272 1.3 riastrad */ 273 1.3 riastrad 274 1.16 riastrad mutex_lock(&dev->object_name_lock); 275 1.3 riastrad if (--obj->handle_count == 0) { 276 1.3 riastrad drm_gem_object_handle_free(obj); 277 1.3 riastrad drm_gem_object_exported_dma_buf_free(obj); 278 1.16 riastrad final = true; 279 1.1 riastrad } 280 1.16 riastrad mutex_unlock(&dev->object_name_lock); 281 1.3 riastrad 282 1.16 riastrad if (final) 283 1.16 riastrad drm_gem_object_put_unlocked(obj); 284 1.16 riastrad } 285 1.16 riastrad 286 1.16 riastrad /* 287 1.16 riastrad * Called at device or object close to release the file's 288 1.16 riastrad * handle references on objects. 289 1.16 riastrad */ 290 1.16 riastrad static int 291 1.16 riastrad drm_gem_object_release_handle(int id, void *ptr, void *data) 292 1.16 riastrad { 293 1.16 riastrad struct drm_file *file_priv = data; 294 1.16 riastrad struct drm_gem_object *obj = ptr; 295 1.16 riastrad struct drm_device *dev = obj->dev; 296 1.16 riastrad 297 1.16 riastrad if (obj->funcs && obj->funcs->close) 298 1.16 riastrad obj->funcs->close(obj, file_priv); 299 1.16 riastrad else if (dev->driver->gem_close_object) 300 1.16 riastrad dev->driver->gem_close_object(obj, file_priv); 301 1.16 riastrad 302 1.16 riastrad drm_gem_remove_prime_handles(obj, file_priv); 303 1.16 riastrad drm_vma_node_revoke(&obj->vma_node, file_priv); 304 1.16 riastrad 305 1.16 riastrad drm_gem_object_handle_put_unlocked(obj); 306 1.16 riastrad 307 1.16 riastrad return 0; 308 1.1 riastrad } 309 1.1 riastrad 310 1.1 riastrad /** 311 1.3 riastrad * drm_gem_handle_delete - deletes the given file-private handle 312 1.3 riastrad * @filp: drm file-private structure to use for the handle look up 313 1.3 riastrad * @handle: userspace handle to delete 314 1.3 riastrad * 315 1.16 riastrad * Removes the GEM handle from the @filp lookup table which has been added with 316 1.16 riastrad * drm_gem_handle_create(). If this is the last handle also cleans up linked 317 1.16 riastrad * resources like GEM names. 318 1.1 riastrad */ 319 1.1 riastrad int 320 1.1 riastrad drm_gem_handle_delete(struct drm_file *filp, u32 handle) 321 1.1 riastrad { 322 1.1 riastrad struct drm_gem_object *obj; 323 1.1 riastrad 324 1.1 riastrad spin_lock(&filp->table_lock); 325 1.1 riastrad 326 1.1 riastrad /* Check if we currently have a reference on the object */ 327 1.16 riastrad obj = idr_replace(&filp->object_idr, NULL, handle); 328 1.16 riastrad spin_unlock(&filp->table_lock); 329 1.16 riastrad if (IS_ERR_OR_NULL(obj)) 330 1.1 riastrad return -EINVAL; 331 1.1 riastrad 332 1.16 riastrad /* Release driver's reference and decrement refcount. */ 333 1.16 riastrad drm_gem_object_release_handle(handle, obj, filp); 334 1.16 riastrad 335 1.16 riastrad /* And finally make the handle available for future allocations. */ 336 1.16 riastrad spin_lock(&filp->table_lock); 337 1.1 riastrad idr_remove(&filp->object_idr, handle); 338 1.1 riastrad spin_unlock(&filp->table_lock); 339 1.1 riastrad 340 1.1 riastrad return 0; 341 1.1 riastrad } 342 1.1 riastrad EXPORT_SYMBOL(drm_gem_handle_delete); 343 1.1 riastrad 344 1.1 riastrad /** 345 1.16 riastrad * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object 346 1.16 riastrad * @file: drm file-private structure containing the gem object 347 1.16 riastrad * @dev: corresponding drm_device 348 1.16 riastrad * @handle: gem object handle 349 1.16 riastrad * @offset: return location for the fake mmap offset 350 1.16 riastrad * 351 1.16 riastrad * This implements the &drm_driver.dumb_map_offset kms driver callback for 352 1.16 riastrad * drivers which use gem to manage their backing storage. 353 1.16 riastrad * 354 1.16 riastrad * Returns: 355 1.16 riastrad * 0 on success or a negative error code on failure. 356 1.16 riastrad */ 357 1.16 riastrad int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, 358 1.16 riastrad u32 handle, u64 *offset) 359 1.16 riastrad { 360 1.16 riastrad struct drm_gem_object *obj; 361 1.16 riastrad int ret; 362 1.16 riastrad 363 1.16 riastrad obj = drm_gem_object_lookup(file, handle); 364 1.16 riastrad if (!obj) 365 1.16 riastrad return -ENOENT; 366 1.16 riastrad 367 1.16 riastrad /* Don't allow imported objects to be mapped */ 368 1.16 riastrad if (obj->import_attach) { 369 1.16 riastrad ret = -EINVAL; 370 1.16 riastrad goto out; 371 1.16 riastrad } 372 1.16 riastrad 373 1.16 riastrad ret = drm_gem_create_mmap_offset(obj); 374 1.16 riastrad if (ret) 375 1.16 riastrad goto out; 376 1.16 riastrad 377 1.16 riastrad *offset = drm_vma_node_offset_addr(&obj->vma_node); 378 1.16 riastrad out: 379 1.16 riastrad drm_gem_object_put_unlocked(obj); 380 1.16 riastrad 381 1.16 riastrad return ret; 382 1.16 riastrad } 383 1.16 riastrad EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset); 384 1.16 riastrad 385 1.16 riastrad /** 386 1.3 riastrad * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers 387 1.3 riastrad * @file: drm file-private structure to remove the dumb handle from 388 1.3 riastrad * @dev: corresponding drm_device 389 1.3 riastrad * @handle: the dumb handle to remove 390 1.16 riastrad * 391 1.16 riastrad * This implements the &drm_driver.dumb_destroy kms driver callback for drivers 392 1.16 riastrad * which use gem to manage their backing storage. 393 1.3 riastrad */ 394 1.3 riastrad int drm_gem_dumb_destroy(struct drm_file *file, 395 1.3 riastrad struct drm_device *dev, 396 1.3 riastrad uint32_t handle) 397 1.3 riastrad { 398 1.3 riastrad return drm_gem_handle_delete(file, handle); 399 1.3 riastrad } 400 1.3 riastrad EXPORT_SYMBOL(drm_gem_dumb_destroy); 401 1.3 riastrad 402 1.3 riastrad /** 403 1.3 riastrad * drm_gem_handle_create_tail - internal functions to create a handle 404 1.3 riastrad * @file_priv: drm file-private structure to register the handle for 405 1.3 riastrad * @obj: object to register 406 1.7 riastrad * @handlep: pointer to return the created handle to the caller 407 1.16 riastrad * 408 1.16 riastrad * This expects the &drm_device.object_name_lock to be held already and will 409 1.16 riastrad * drop it before returning. Used to avoid races in establishing new handles 410 1.16 riastrad * when importing an object from either an flink name or a dma-buf. 411 1.16 riastrad * 412 1.16 riastrad * Handles must be release again through drm_gem_handle_delete(). This is done 413 1.16 riastrad * when userspace closes @file_priv for all attached handles, or through the 414 1.16 riastrad * GEM_CLOSE ioctl for individual handles. 415 1.1 riastrad */ 416 1.1 riastrad int 417 1.3 riastrad drm_gem_handle_create_tail(struct drm_file *file_priv, 418 1.3 riastrad struct drm_gem_object *obj, 419 1.3 riastrad u32 *handlep) 420 1.1 riastrad { 421 1.1 riastrad struct drm_device *dev = obj->dev; 422 1.16 riastrad u32 handle; 423 1.1 riastrad int ret; 424 1.1 riastrad 425 1.3 riastrad WARN_ON(!mutex_is_locked(&dev->object_name_lock)); 426 1.16 riastrad if (obj->handle_count++ == 0) 427 1.16 riastrad drm_gem_object_get(obj); 428 1.3 riastrad 429 1.1 riastrad /* 430 1.3 riastrad * Get the user-visible handle using idr. Preload and perform 431 1.3 riastrad * allocation under our spinlock. 432 1.1 riastrad */ 433 1.3 riastrad idr_preload(GFP_KERNEL); 434 1.3 riastrad spin_lock(&file_priv->table_lock); 435 1.1 riastrad 436 1.3 riastrad ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT); 437 1.16 riastrad 438 1.1 riastrad spin_unlock(&file_priv->table_lock); 439 1.3 riastrad idr_preload_end(); 440 1.16 riastrad 441 1.3 riastrad mutex_unlock(&dev->object_name_lock); 442 1.7 riastrad if (ret < 0) 443 1.7 riastrad goto err_unref; 444 1.7 riastrad 445 1.16 riastrad handle = ret; 446 1.1 riastrad 447 1.16 riastrad ret = drm_vma_node_allow(&obj->vma_node, file_priv); 448 1.7 riastrad if (ret) 449 1.7 riastrad goto err_remove; 450 1.1 riastrad 451 1.16 riastrad if (obj->funcs && obj->funcs->open) { 452 1.16 riastrad ret = obj->funcs->open(obj, file_priv); 453 1.16 riastrad if (ret) 454 1.16 riastrad goto err_revoke; 455 1.16 riastrad } else if (dev->driver->gem_open_object) { 456 1.1 riastrad ret = dev->driver->gem_open_object(obj, file_priv); 457 1.7 riastrad if (ret) 458 1.7 riastrad goto err_revoke; 459 1.1 riastrad } 460 1.1 riastrad 461 1.16 riastrad *handlep = handle; 462 1.1 riastrad return 0; 463 1.7 riastrad 464 1.7 riastrad err_revoke: 465 1.16 riastrad drm_vma_node_revoke(&obj->vma_node, file_priv); 466 1.7 riastrad err_remove: 467 1.7 riastrad spin_lock(&file_priv->table_lock); 468 1.16 riastrad idr_remove(&file_priv->object_idr, handle); 469 1.7 riastrad spin_unlock(&file_priv->table_lock); 470 1.7 riastrad err_unref: 471 1.16 riastrad drm_gem_object_handle_put_unlocked(obj); 472 1.7 riastrad return ret; 473 1.1 riastrad } 474 1.3 riastrad 475 1.3 riastrad /** 476 1.7 riastrad * drm_gem_handle_create - create a gem handle for an object 477 1.3 riastrad * @file_priv: drm file-private structure to register the handle for 478 1.3 riastrad * @obj: object to register 479 1.3 riastrad * @handlep: pionter to return the created handle to the caller 480 1.3 riastrad * 481 1.16 riastrad * Create a handle for this object. This adds a handle reference to the object, 482 1.16 riastrad * which includes a regular reference count. Callers will likely want to 483 1.16 riastrad * dereference the object afterwards. 484 1.16 riastrad * 485 1.16 riastrad * Since this publishes @obj to userspace it must be fully set up by this point, 486 1.16 riastrad * drivers must call this last in their buffer object creation callbacks. 487 1.3 riastrad */ 488 1.7 riastrad int drm_gem_handle_create(struct drm_file *file_priv, 489 1.7 riastrad struct drm_gem_object *obj, 490 1.7 riastrad u32 *handlep) 491 1.3 riastrad { 492 1.3 riastrad mutex_lock(&obj->dev->object_name_lock); 493 1.3 riastrad 494 1.3 riastrad return drm_gem_handle_create_tail(file_priv, obj, handlep); 495 1.3 riastrad } 496 1.1 riastrad EXPORT_SYMBOL(drm_gem_handle_create); 497 1.1 riastrad 498 1.1 riastrad 499 1.1 riastrad /** 500 1.1 riastrad * drm_gem_free_mmap_offset - release a fake mmap offset for an object 501 1.1 riastrad * @obj: obj in question 502 1.1 riastrad * 503 1.1 riastrad * This routine frees fake offsets allocated by drm_gem_create_mmap_offset(). 504 1.16 riastrad * 505 1.16 riastrad * Note that drm_gem_object_release() already calls this function, so drivers 506 1.16 riastrad * don't have to take care of releasing the mmap offset themselves when freeing 507 1.16 riastrad * the GEM object. 508 1.1 riastrad */ 509 1.1 riastrad void 510 1.1 riastrad drm_gem_free_mmap_offset(struct drm_gem_object *obj) 511 1.1 riastrad { 512 1.1 riastrad struct drm_device *dev = obj->dev; 513 1.1 riastrad 514 1.3 riastrad drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node); 515 1.1 riastrad } 516 1.1 riastrad EXPORT_SYMBOL(drm_gem_free_mmap_offset); 517 1.1 riastrad 518 1.1 riastrad /** 519 1.3 riastrad * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object 520 1.3 riastrad * @obj: obj in question 521 1.3 riastrad * @size: the virtual size 522 1.3 riastrad * 523 1.3 riastrad * GEM memory mapping works by handing back to userspace a fake mmap offset 524 1.3 riastrad * it can use in a subsequent mmap(2) call. The DRM core code then looks 525 1.3 riastrad * up the object based on the offset and sets up the various memory mapping 526 1.3 riastrad * structures. 527 1.3 riastrad * 528 1.3 riastrad * This routine allocates and attaches a fake offset for @obj, in cases where 529 1.16 riastrad * the virtual size differs from the physical size (ie. &drm_gem_object.size). 530 1.16 riastrad * Otherwise just use drm_gem_create_mmap_offset(). 531 1.16 riastrad * 532 1.16 riastrad * This function is idempotent and handles an already allocated mmap offset 533 1.16 riastrad * transparently. Drivers do not need to check for this case. 534 1.3 riastrad */ 535 1.3 riastrad int 536 1.3 riastrad drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size) 537 1.3 riastrad { 538 1.3 riastrad struct drm_device *dev = obj->dev; 539 1.3 riastrad 540 1.3 riastrad return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node, 541 1.3 riastrad size / PAGE_SIZE); 542 1.3 riastrad } 543 1.3 riastrad EXPORT_SYMBOL(drm_gem_create_mmap_offset_size); 544 1.3 riastrad 545 1.3 riastrad /** 546 1.1 riastrad * drm_gem_create_mmap_offset - create a fake mmap offset for an object 547 1.1 riastrad * @obj: obj in question 548 1.1 riastrad * 549 1.1 riastrad * GEM memory mapping works by handing back to userspace a fake mmap offset 550 1.1 riastrad * it can use in a subsequent mmap(2) call. The DRM core code then looks 551 1.1 riastrad * up the object based on the offset and sets up the various memory mapping 552 1.1 riastrad * structures. 553 1.1 riastrad * 554 1.1 riastrad * This routine allocates and attaches a fake offset for @obj. 555 1.16 riastrad * 556 1.16 riastrad * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release 557 1.16 riastrad * the fake offset again. 558 1.1 riastrad */ 559 1.3 riastrad int drm_gem_create_mmap_offset(struct drm_gem_object *obj) 560 1.3 riastrad { 561 1.3 riastrad return drm_gem_create_mmap_offset_size(obj, obj->size); 562 1.3 riastrad } 563 1.3 riastrad EXPORT_SYMBOL(drm_gem_create_mmap_offset); 564 1.3 riastrad 565 1.16 riastrad /* 566 1.16 riastrad * Move pages to appropriate lru and release the pagevec, decrementing the 567 1.16 riastrad * ref count of those pages. 568 1.16 riastrad */ 569 1.18 riastrad #ifndef __NetBSD__ 570 1.16 riastrad static void drm_gem_check_release_pagevec(struct pagevec *pvec) 571 1.16 riastrad { 572 1.16 riastrad check_move_unevictable_pages(pvec); 573 1.16 riastrad __pagevec_release(pvec); 574 1.16 riastrad cond_resched(); 575 1.16 riastrad } 576 1.18 riastrad #endif 577 1.16 riastrad 578 1.3 riastrad /** 579 1.3 riastrad * drm_gem_get_pages - helper to allocate backing pages for a GEM object 580 1.3 riastrad * from shmem 581 1.3 riastrad * @obj: obj in question 582 1.7 riastrad * 583 1.7 riastrad * This reads the page-array of the shmem-backing storage of the given gem 584 1.7 riastrad * object. An array of pages is returned. If a page is not allocated or 585 1.7 riastrad * swapped-out, this will allocate/swap-in the required pages. Note that the 586 1.7 riastrad * whole object is covered by the page-array and pinned in memory. 587 1.7 riastrad * 588 1.7 riastrad * Use drm_gem_put_pages() to release the array and unpin all pages. 589 1.7 riastrad * 590 1.7 riastrad * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()). 591 1.7 riastrad * If you require other GFP-masks, you have to do those allocations yourself. 592 1.7 riastrad * 593 1.7 riastrad * Note that you are not allowed to change gfp-zones during runtime. That is, 594 1.7 riastrad * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as 595 1.7 riastrad * set during initialization. If you have special zone constraints, set them 596 1.16 riastrad * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care 597 1.7 riastrad * to keep pages in the required zone during swap-in. 598 1.3 riastrad */ 599 1.4 riastrad #ifdef __NetBSD__ 600 1.4 riastrad struct page ** 601 1.7 riastrad drm_gem_get_pages(struct drm_gem_object *obj) 602 1.4 riastrad { 603 1.4 riastrad struct vm_page *vm_page; 604 1.4 riastrad struct page **pages; 605 1.17 riastrad unsigned i, npages; 606 1.4 riastrad int ret; 607 1.4 riastrad 608 1.24 riastrad KASSERT((obj->size & (PAGE_SIZE - 1)) == 0); 609 1.4 riastrad 610 1.17 riastrad npages = obj->size >> PAGE_SHIFT; 611 1.17 riastrad pages = kvmalloc_array(npages, sizeof(*pages), GFP_KERNEL); 612 1.4 riastrad if (pages == NULL) { 613 1.4 riastrad ret = -ENOMEM; 614 1.4 riastrad goto fail0; 615 1.4 riastrad } 616 1.4 riastrad 617 1.4 riastrad /* XXX errno NetBSD->Linux */ 618 1.22 riastrad ret = -uvm_obj_wirepages(obj->filp, 0, obj->size, NULL); 619 1.4 riastrad if (ret) 620 1.4 riastrad goto fail1; 621 1.4 riastrad 622 1.22 riastrad rw_enter(obj->filp->vmobjlock, RW_READER); 623 1.22 riastrad for (i = 0; i < npages; i++) { 624 1.22 riastrad vm_page = uvm_pagelookup(obj->filp, ptoa(i)); 625 1.22 riastrad pages[i] = container_of(vm_page, struct page, p_vmp); 626 1.22 riastrad } 627 1.22 riastrad rw_exit(obj->filp->vmobjlock); 628 1.4 riastrad 629 1.4 riastrad return pages; 630 1.4 riastrad 631 1.17 riastrad fail1: kvfree(pages); 632 1.4 riastrad fail0: return ERR_PTR(ret); 633 1.4 riastrad } 634 1.4 riastrad #else 635 1.7 riastrad struct page **drm_gem_get_pages(struct drm_gem_object *obj) 636 1.1 riastrad { 637 1.3 riastrad struct address_space *mapping; 638 1.3 riastrad struct page *p, **pages; 639 1.16 riastrad struct pagevec pvec; 640 1.3 riastrad int i, npages; 641 1.3 riastrad 642 1.3 riastrad /* This is the shared memory object that backs the GEM resource */ 643 1.16 riastrad mapping = obj->filp->f_mapping; 644 1.3 riastrad 645 1.3 riastrad /* We already BUG_ON() for non-page-aligned sizes in 646 1.3 riastrad * drm_gem_object_init(), so we should never hit this unless 647 1.3 riastrad * driver author is doing something really wrong: 648 1.3 riastrad */ 649 1.3 riastrad WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); 650 1.1 riastrad 651 1.3 riastrad npages = obj->size >> PAGE_SHIFT; 652 1.1 riastrad 653 1.16 riastrad pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 654 1.3 riastrad if (pages == NULL) 655 1.3 riastrad return ERR_PTR(-ENOMEM); 656 1.3 riastrad 657 1.16 riastrad mapping_set_unevictable(mapping); 658 1.16 riastrad 659 1.3 riastrad for (i = 0; i < npages; i++) { 660 1.7 riastrad p = shmem_read_mapping_page(mapping, i); 661 1.3 riastrad if (IS_ERR(p)) 662 1.3 riastrad goto fail; 663 1.3 riastrad pages[i] = p; 664 1.3 riastrad 665 1.7 riastrad /* Make sure shmem keeps __GFP_DMA32 allocated pages in the 666 1.7 riastrad * correct region during swapin. Note that this requires 667 1.7 riastrad * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping) 668 1.7 riastrad * so shmem can relocate pages during swapin if required. 669 1.3 riastrad */ 670 1.7 riastrad BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) && 671 1.3 riastrad (page_to_pfn(p) >= 0x00100000UL)); 672 1.1 riastrad } 673 1.1 riastrad 674 1.3 riastrad return pages; 675 1.3 riastrad 676 1.3 riastrad fail: 677 1.16 riastrad mapping_clear_unevictable(mapping); 678 1.16 riastrad pagevec_init(&pvec); 679 1.16 riastrad while (i--) { 680 1.16 riastrad if (!pagevec_add(&pvec, pages[i])) 681 1.16 riastrad drm_gem_check_release_pagevec(&pvec); 682 1.16 riastrad } 683 1.16 riastrad if (pagevec_count(&pvec)) 684 1.16 riastrad drm_gem_check_release_pagevec(&pvec); 685 1.3 riastrad 686 1.16 riastrad kvfree(pages); 687 1.3 riastrad return ERR_CAST(p); 688 1.3 riastrad } 689 1.4 riastrad #endif 690 1.3 riastrad EXPORT_SYMBOL(drm_gem_get_pages); 691 1.3 riastrad 692 1.3 riastrad /** 693 1.3 riastrad * drm_gem_put_pages - helper to free backing pages for a GEM object 694 1.3 riastrad * @obj: obj in question 695 1.3 riastrad * @pages: pages to free 696 1.3 riastrad * @dirty: if true, pages will be marked as dirty 697 1.3 riastrad * @accessed: if true, the pages will be marked as accessed 698 1.3 riastrad */ 699 1.4 riastrad #ifdef __NetBSD__ 700 1.4 riastrad void 701 1.4 riastrad drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, bool dirty, 702 1.4 riastrad bool accessed __unused /* XXX */) 703 1.4 riastrad { 704 1.4 riastrad unsigned i; 705 1.4 riastrad 706 1.4 riastrad for (i = 0; i < (obj->size >> PAGE_SHIFT); i++) { 707 1.11 ad if (dirty) { 708 1.15 ad rw_enter(obj->filp->vmobjlock, RW_WRITER); 709 1.11 ad uvm_pagemarkdirty(&pages[i]->p_vmp, 710 1.11 ad UVM_PAGE_STATUS_DIRTY); 711 1.15 ad rw_exit(obj->filp->vmobjlock); 712 1.11 ad } 713 1.4 riastrad } 714 1.4 riastrad 715 1.8 riastrad uvm_obj_unwirepages(obj->filp, 0, obj->size); 716 1.4 riastrad } 717 1.4 riastrad #else 718 1.3 riastrad void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, 719 1.3 riastrad bool dirty, bool accessed) 720 1.3 riastrad { 721 1.3 riastrad int i, npages; 722 1.16 riastrad struct address_space *mapping; 723 1.16 riastrad struct pagevec pvec; 724 1.16 riastrad 725 1.16 riastrad mapping = file_inode(obj->filp)->i_mapping; 726 1.16 riastrad mapping_clear_unevictable(mapping); 727 1.3 riastrad 728 1.3 riastrad /* We already BUG_ON() for non-page-aligned sizes in 729 1.3 riastrad * drm_gem_object_init(), so we should never hit this unless 730 1.3 riastrad * driver author is doing something really wrong: 731 1.3 riastrad */ 732 1.3 riastrad WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); 733 1.3 riastrad 734 1.3 riastrad npages = obj->size >> PAGE_SHIFT; 735 1.1 riastrad 736 1.16 riastrad pagevec_init(&pvec); 737 1.3 riastrad for (i = 0; i < npages; i++) { 738 1.16 riastrad if (!pages[i]) 739 1.16 riastrad continue; 740 1.16 riastrad 741 1.3 riastrad if (dirty) 742 1.3 riastrad set_page_dirty(pages[i]); 743 1.1 riastrad 744 1.3 riastrad if (accessed) 745 1.3 riastrad mark_page_accessed(pages[i]); 746 1.1 riastrad 747 1.3 riastrad /* Undo the reference we took when populating the table */ 748 1.16 riastrad if (!pagevec_add(&pvec, pages[i])) 749 1.16 riastrad drm_gem_check_release_pagevec(&pvec); 750 1.3 riastrad } 751 1.16 riastrad if (pagevec_count(&pvec)) 752 1.16 riastrad drm_gem_check_release_pagevec(&pvec); 753 1.3 riastrad 754 1.16 riastrad kvfree(pages); 755 1.1 riastrad } 756 1.4 riastrad #endif 757 1.3 riastrad EXPORT_SYMBOL(drm_gem_put_pages); 758 1.1 riastrad 759 1.16 riastrad static int objects_lookup(struct drm_file *filp, u32 *handle, int count, 760 1.16 riastrad struct drm_gem_object **objs) 761 1.1 riastrad { 762 1.16 riastrad int i, ret = 0; 763 1.1 riastrad struct drm_gem_object *obj; 764 1.1 riastrad 765 1.1 riastrad spin_lock(&filp->table_lock); 766 1.1 riastrad 767 1.16 riastrad for (i = 0; i < count; i++) { 768 1.16 riastrad /* Check if we currently have a reference on the object */ 769 1.16 riastrad obj = idr_find(&filp->object_idr, handle[i]); 770 1.16 riastrad if (!obj) { 771 1.16 riastrad ret = -ENOENT; 772 1.16 riastrad break; 773 1.16 riastrad } 774 1.16 riastrad drm_gem_object_get(obj); 775 1.16 riastrad objs[i] = obj; 776 1.16 riastrad } 777 1.16 riastrad spin_unlock(&filp->table_lock); 778 1.16 riastrad 779 1.16 riastrad return ret; 780 1.16 riastrad } 781 1.16 riastrad 782 1.16 riastrad /** 783 1.16 riastrad * drm_gem_objects_lookup - look up GEM objects from an array of handles 784 1.16 riastrad * @filp: DRM file private date 785 1.16 riastrad * @bo_handles: user pointer to array of userspace handle 786 1.16 riastrad * @count: size of handle array 787 1.16 riastrad * @objs_out: returned pointer to array of drm_gem_object pointers 788 1.16 riastrad * 789 1.16 riastrad * Takes an array of userspace handles and returns a newly allocated array of 790 1.16 riastrad * GEM objects. 791 1.16 riastrad * 792 1.16 riastrad * For a single handle lookup, use drm_gem_object_lookup(). 793 1.16 riastrad * 794 1.16 riastrad * Returns: 795 1.16 riastrad * 796 1.16 riastrad * @objs filled in with GEM object pointers. Returned GEM objects need to be 797 1.16 riastrad * released with drm_gem_object_put(). -ENOENT is returned on a lookup 798 1.16 riastrad * failure. 0 is returned on success. 799 1.16 riastrad * 800 1.16 riastrad */ 801 1.16 riastrad int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, 802 1.16 riastrad int count, struct drm_gem_object ***objs_out) 803 1.16 riastrad { 804 1.16 riastrad int ret; 805 1.16 riastrad u32 *handles; 806 1.16 riastrad struct drm_gem_object **objs; 807 1.16 riastrad 808 1.16 riastrad if (!count) 809 1.16 riastrad return 0; 810 1.16 riastrad 811 1.16 riastrad objs = kvmalloc_array(count, sizeof(struct drm_gem_object *), 812 1.16 riastrad GFP_KERNEL | __GFP_ZERO); 813 1.16 riastrad if (!objs) 814 1.16 riastrad return -ENOMEM; 815 1.16 riastrad 816 1.16 riastrad handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL); 817 1.16 riastrad if (!handles) { 818 1.16 riastrad ret = -ENOMEM; 819 1.16 riastrad goto out; 820 1.16 riastrad } 821 1.16 riastrad 822 1.16 riastrad if (copy_from_user(handles, bo_handles, count * sizeof(u32))) { 823 1.16 riastrad ret = -EFAULT; 824 1.16 riastrad DRM_DEBUG("Failed to copy in GEM handles\n"); 825 1.16 riastrad goto out; 826 1.1 riastrad } 827 1.1 riastrad 828 1.16 riastrad ret = objects_lookup(filp, handles, count, objs); 829 1.16 riastrad *objs_out = objs; 830 1.1 riastrad 831 1.16 riastrad out: 832 1.16 riastrad kvfree(handles); 833 1.16 riastrad return ret; 834 1.16 riastrad 835 1.16 riastrad } 836 1.16 riastrad EXPORT_SYMBOL(drm_gem_objects_lookup); 837 1.16 riastrad 838 1.16 riastrad /** 839 1.16 riastrad * drm_gem_object_lookup - look up a GEM object from its handle 840 1.16 riastrad * @filp: DRM file private date 841 1.16 riastrad * @handle: userspace handle 842 1.16 riastrad * 843 1.16 riastrad * Returns: 844 1.16 riastrad * 845 1.16 riastrad * A reference to the object named by the handle if such exists on @filp, NULL 846 1.16 riastrad * otherwise. 847 1.16 riastrad * 848 1.16 riastrad * If looking up an array of handles, use drm_gem_objects_lookup(). 849 1.16 riastrad */ 850 1.16 riastrad struct drm_gem_object * 851 1.16 riastrad drm_gem_object_lookup(struct drm_file *filp, u32 handle) 852 1.16 riastrad { 853 1.16 riastrad struct drm_gem_object *obj = NULL; 854 1.1 riastrad 855 1.16 riastrad objects_lookup(filp, &handle, 1, &obj); 856 1.1 riastrad return obj; 857 1.1 riastrad } 858 1.1 riastrad EXPORT_SYMBOL(drm_gem_object_lookup); 859 1.1 riastrad 860 1.1 riastrad /** 861 1.16 riastrad * drm_gem_dma_resv_wait - Wait on GEM object's reservation's objects 862 1.16 riastrad * shared and/or exclusive fences. 863 1.16 riastrad * @filep: DRM file private date 864 1.16 riastrad * @handle: userspace handle 865 1.16 riastrad * @wait_all: if true, wait on all fences, else wait on just exclusive fence 866 1.16 riastrad * @timeout: timeout value in jiffies or zero to return immediately 867 1.16 riastrad * 868 1.16 riastrad * Returns: 869 1.16 riastrad * 870 1.16 riastrad * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or 871 1.16 riastrad * greater than 0 on success. 872 1.16 riastrad */ 873 1.16 riastrad long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle, 874 1.16 riastrad bool wait_all, unsigned long timeout) 875 1.16 riastrad { 876 1.16 riastrad long ret; 877 1.16 riastrad struct drm_gem_object *obj; 878 1.16 riastrad 879 1.16 riastrad obj = drm_gem_object_lookup(filep, handle); 880 1.16 riastrad if (!obj) { 881 1.16 riastrad DRM_DEBUG("Failed to look up GEM BO %d\n", handle); 882 1.16 riastrad return -EINVAL; 883 1.16 riastrad } 884 1.16 riastrad 885 1.16 riastrad ret = dma_resv_wait_timeout_rcu(obj->resv, wait_all, 886 1.16 riastrad true, timeout); 887 1.16 riastrad if (ret == 0) 888 1.16 riastrad ret = -ETIME; 889 1.16 riastrad else if (ret > 0) 890 1.16 riastrad ret = 0; 891 1.16 riastrad 892 1.16 riastrad drm_gem_object_put_unlocked(obj); 893 1.16 riastrad 894 1.16 riastrad return ret; 895 1.16 riastrad } 896 1.16 riastrad EXPORT_SYMBOL(drm_gem_dma_resv_wait); 897 1.16 riastrad 898 1.16 riastrad /** 899 1.3 riastrad * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl 900 1.3 riastrad * @dev: drm_device 901 1.3 riastrad * @data: ioctl data 902 1.3 riastrad * @file_priv: drm file-private structure 903 1.3 riastrad * 904 1.1 riastrad * Releases the handle to an mm object. 905 1.1 riastrad */ 906 1.1 riastrad int 907 1.1 riastrad drm_gem_close_ioctl(struct drm_device *dev, void *data, 908 1.1 riastrad struct drm_file *file_priv) 909 1.1 riastrad { 910 1.1 riastrad struct drm_gem_close *args = data; 911 1.1 riastrad int ret; 912 1.1 riastrad 913 1.7 riastrad if (!drm_core_check_feature(dev, DRIVER_GEM)) 914 1.16 riastrad return -EOPNOTSUPP; 915 1.1 riastrad 916 1.1 riastrad ret = drm_gem_handle_delete(file_priv, args->handle); 917 1.1 riastrad 918 1.1 riastrad return ret; 919 1.1 riastrad } 920 1.1 riastrad 921 1.1 riastrad /** 922 1.3 riastrad * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl 923 1.3 riastrad * @dev: drm_device 924 1.3 riastrad * @data: ioctl data 925 1.3 riastrad * @file_priv: drm file-private structure 926 1.3 riastrad * 927 1.1 riastrad * Create a global name for an object, returning the name. 928 1.1 riastrad * 929 1.1 riastrad * Note that the name does not hold a reference; when the object 930 1.1 riastrad * is freed, the name goes away. 931 1.1 riastrad */ 932 1.1 riastrad int 933 1.1 riastrad drm_gem_flink_ioctl(struct drm_device *dev, void *data, 934 1.1 riastrad struct drm_file *file_priv) 935 1.1 riastrad { 936 1.1 riastrad struct drm_gem_flink *args = data; 937 1.1 riastrad struct drm_gem_object *obj; 938 1.1 riastrad int ret; 939 1.1 riastrad 940 1.7 riastrad if (!drm_core_check_feature(dev, DRIVER_GEM)) 941 1.16 riastrad return -EOPNOTSUPP; 942 1.1 riastrad 943 1.16 riastrad obj = drm_gem_object_lookup(file_priv, args->handle); 944 1.1 riastrad if (obj == NULL) 945 1.1 riastrad return -ENOENT; 946 1.1 riastrad 947 1.9 riastrad idr_preload(GFP_KERNEL); 948 1.3 riastrad mutex_lock(&dev->object_name_lock); 949 1.3 riastrad /* prevent races with concurrent gem_close. */ 950 1.3 riastrad if (obj->handle_count == 0) { 951 1.3 riastrad ret = -ENOENT; 952 1.1 riastrad goto err; 953 1.1 riastrad } 954 1.1 riastrad 955 1.1 riastrad if (!obj->name) { 956 1.16 riastrad ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL); 957 1.3 riastrad if (ret < 0) 958 1.1 riastrad goto err; 959 1.1 riastrad 960 1.3 riastrad obj->name = ret; 961 1.1 riastrad } 962 1.1 riastrad 963 1.3 riastrad args->name = (uint64_t) obj->name; 964 1.3 riastrad ret = 0; 965 1.3 riastrad 966 1.1 riastrad err: 967 1.9 riastrad mutex_unlock(&dev->object_name_lock); 968 1.3 riastrad idr_preload_end(); 969 1.16 riastrad drm_gem_object_put_unlocked(obj); 970 1.1 riastrad return ret; 971 1.1 riastrad } 972 1.1 riastrad 973 1.1 riastrad /** 974 1.3 riastrad * drm_gem_open - implementation of the GEM_OPEN ioctl 975 1.3 riastrad * @dev: drm_device 976 1.3 riastrad * @data: ioctl data 977 1.3 riastrad * @file_priv: drm file-private structure 978 1.3 riastrad * 979 1.1 riastrad * Open an object using the global name, returning a handle and the size. 980 1.1 riastrad * 981 1.1 riastrad * This handle (of course) holds a reference to the object, so the object 982 1.1 riastrad * will not go away until the handle is deleted. 983 1.1 riastrad */ 984 1.1 riastrad int 985 1.1 riastrad drm_gem_open_ioctl(struct drm_device *dev, void *data, 986 1.1 riastrad struct drm_file *file_priv) 987 1.1 riastrad { 988 1.1 riastrad struct drm_gem_open *args = data; 989 1.1 riastrad struct drm_gem_object *obj; 990 1.1 riastrad int ret; 991 1.1 riastrad u32 handle; 992 1.1 riastrad 993 1.7 riastrad if (!drm_core_check_feature(dev, DRIVER_GEM)) 994 1.16 riastrad return -EOPNOTSUPP; 995 1.1 riastrad 996 1.3 riastrad mutex_lock(&dev->object_name_lock); 997 1.1 riastrad obj = idr_find(&dev->object_name_idr, (int) args->name); 998 1.3 riastrad if (obj) { 999 1.16 riastrad drm_gem_object_get(obj); 1000 1.3 riastrad } else { 1001 1.3 riastrad mutex_unlock(&dev->object_name_lock); 1002 1.1 riastrad return -ENOENT; 1003 1.3 riastrad } 1004 1.1 riastrad 1005 1.3 riastrad /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */ 1006 1.3 riastrad ret = drm_gem_handle_create_tail(file_priv, obj, &handle); 1007 1.16 riastrad drm_gem_object_put_unlocked(obj); 1008 1.1 riastrad if (ret) 1009 1.1 riastrad return ret; 1010 1.1 riastrad 1011 1.1 riastrad args->handle = handle; 1012 1.1 riastrad args->size = obj->size; 1013 1.1 riastrad 1014 1.1 riastrad return 0; 1015 1.1 riastrad } 1016 1.1 riastrad 1017 1.1 riastrad /** 1018 1.3 riastrad * gem_gem_open - initalizes GEM file-private structures at devnode open time 1019 1.3 riastrad * @dev: drm_device which is being opened by userspace 1020 1.3 riastrad * @file_private: drm file-private structure to set up 1021 1.3 riastrad * 1022 1.1 riastrad * Called at device open time, sets up the structure for handling refcounting 1023 1.1 riastrad * of mm objects. 1024 1.1 riastrad */ 1025 1.1 riastrad void 1026 1.1 riastrad drm_gem_open(struct drm_device *dev, struct drm_file *file_private) 1027 1.1 riastrad { 1028 1.16 riastrad idr_init_base(&file_private->object_idr, 1); 1029 1.1 riastrad spin_lock_init(&file_private->table_lock); 1030 1.1 riastrad } 1031 1.1 riastrad 1032 1.1 riastrad /** 1033 1.3 riastrad * drm_gem_release - release file-private GEM resources 1034 1.3 riastrad * @dev: drm_device which is being closed by userspace 1035 1.3 riastrad * @file_private: drm file-private structure to clean up 1036 1.3 riastrad * 1037 1.1 riastrad * Called at close time when the filp is going away. 1038 1.1 riastrad * 1039 1.1 riastrad * Releases any remaining references on objects by this filp. 1040 1.1 riastrad */ 1041 1.1 riastrad void 1042 1.1 riastrad drm_gem_release(struct drm_device *dev, struct drm_file *file_private) 1043 1.1 riastrad { 1044 1.1 riastrad idr_for_each(&file_private->object_idr, 1045 1.1 riastrad &drm_gem_object_release_handle, file_private); 1046 1.1 riastrad idr_destroy(&file_private->object_idr); 1047 1.2 riastrad #ifdef __NetBSD__ 1048 1.2 riastrad spin_lock_destroy(&file_private->table_lock); 1049 1.2 riastrad #endif 1050 1.1 riastrad } 1051 1.1 riastrad 1052 1.16 riastrad /** 1053 1.16 riastrad * drm_gem_object_release - release GEM buffer object resources 1054 1.16 riastrad * @obj: GEM buffer object 1055 1.16 riastrad * 1056 1.16 riastrad * This releases any structures and resources used by @obj and is the invers of 1057 1.16 riastrad * drm_gem_object_init(). 1058 1.16 riastrad */ 1059 1.1 riastrad void 1060 1.1 riastrad drm_gem_object_release(struct drm_gem_object *obj) 1061 1.1 riastrad { 1062 1.4 riastrad #ifndef __NetBSD__ 1063 1.3 riastrad WARN_ON(obj->dma_buf); 1064 1.4 riastrad #endif 1065 1.3 riastrad 1066 1.2 riastrad #ifdef __NetBSD__ 1067 1.8 riastrad if (obj->filp) 1068 1.8 riastrad uao_detach(obj->filp); 1069 1.21 riastrad uvm_obj_destroy(&obj->gemo_uvmobj, /*free lock*/true); 1070 1.2 riastrad #else 1071 1.1 riastrad if (obj->filp) 1072 1.3 riastrad fput(obj->filp); 1073 1.2 riastrad #endif 1074 1.3 riastrad 1075 1.16 riastrad dma_resv_fini(&obj->_resv); 1076 1.3 riastrad drm_gem_free_mmap_offset(obj); 1077 1.23 riastrad #ifdef __NetBSD__ 1078 1.23 riastrad drm_vma_node_destroy(&obj->vma_node); 1079 1.23 riastrad #endif 1080 1.1 riastrad } 1081 1.1 riastrad EXPORT_SYMBOL(drm_gem_object_release); 1082 1.1 riastrad 1083 1.1 riastrad /** 1084 1.3 riastrad * drm_gem_object_free - free a GEM object 1085 1.3 riastrad * @kref: kref of the object to free 1086 1.3 riastrad * 1087 1.1 riastrad * Called after the last reference to the object has been lost. 1088 1.16 riastrad * Must be called holding &drm_device.struct_mutex. 1089 1.1 riastrad * 1090 1.1 riastrad * Frees the object 1091 1.1 riastrad */ 1092 1.1 riastrad void 1093 1.1 riastrad drm_gem_object_free(struct kref *kref) 1094 1.1 riastrad { 1095 1.7 riastrad struct drm_gem_object *obj = 1096 1.7 riastrad container_of(kref, struct drm_gem_object, refcount); 1097 1.1 riastrad struct drm_device *dev = obj->dev; 1098 1.1 riastrad 1099 1.16 riastrad if (obj->funcs) { 1100 1.16 riastrad obj->funcs->free(obj); 1101 1.16 riastrad } else if (dev->driver->gem_free_object_unlocked) { 1102 1.16 riastrad dev->driver->gem_free_object_unlocked(obj); 1103 1.16 riastrad } else if (dev->driver->gem_free_object) { 1104 1.16 riastrad WARN_ON(!mutex_is_locked(&dev->struct_mutex)); 1105 1.1 riastrad 1106 1.1 riastrad dev->driver->gem_free_object(obj); 1107 1.16 riastrad } 1108 1.1 riastrad } 1109 1.1 riastrad EXPORT_SYMBOL(drm_gem_object_free); 1110 1.1 riastrad 1111 1.16 riastrad /** 1112 1.16 riastrad * drm_gem_object_put_unlocked - drop a GEM buffer object reference 1113 1.16 riastrad * @obj: GEM buffer object 1114 1.16 riastrad * 1115 1.16 riastrad * This releases a reference to @obj. Callers must not hold the 1116 1.16 riastrad * &drm_device.struct_mutex lock when calling this function. 1117 1.16 riastrad * 1118 1.16 riastrad * See also __drm_gem_object_put(). 1119 1.16 riastrad */ 1120 1.16 riastrad void 1121 1.16 riastrad drm_gem_object_put_unlocked(struct drm_gem_object *obj) 1122 1.16 riastrad { 1123 1.16 riastrad struct drm_device *dev; 1124 1.16 riastrad 1125 1.16 riastrad if (!obj) 1126 1.16 riastrad return; 1127 1.16 riastrad 1128 1.16 riastrad dev = obj->dev; 1129 1.16 riastrad 1130 1.16 riastrad if (dev->driver->gem_free_object) { 1131 1.16 riastrad might_lock(&dev->struct_mutex); 1132 1.16 riastrad if (kref_put_mutex(&obj->refcount, drm_gem_object_free, 1133 1.16 riastrad &dev->struct_mutex)) 1134 1.16 riastrad mutex_unlock(&dev->struct_mutex); 1135 1.16 riastrad } else { 1136 1.16 riastrad kref_put(&obj->refcount, drm_gem_object_free); 1137 1.16 riastrad } 1138 1.16 riastrad } 1139 1.16 riastrad EXPORT_SYMBOL(drm_gem_object_put_unlocked); 1140 1.16 riastrad 1141 1.16 riastrad /** 1142 1.16 riastrad * drm_gem_object_put - release a GEM buffer object reference 1143 1.16 riastrad * @obj: GEM buffer object 1144 1.16 riastrad * 1145 1.16 riastrad * This releases a reference to @obj. Callers must hold the 1146 1.16 riastrad * &drm_device.struct_mutex lock when calling this function, even when the 1147 1.16 riastrad * driver doesn't use &drm_device.struct_mutex for anything. 1148 1.16 riastrad * 1149 1.16 riastrad * For drivers not encumbered with legacy locking use 1150 1.16 riastrad * drm_gem_object_put_unlocked() instead. 1151 1.16 riastrad */ 1152 1.16 riastrad void 1153 1.16 riastrad drm_gem_object_put(struct drm_gem_object *obj) 1154 1.16 riastrad { 1155 1.16 riastrad if (obj) { 1156 1.16 riastrad WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); 1157 1.16 riastrad 1158 1.16 riastrad kref_put(&obj->refcount, drm_gem_object_free); 1159 1.16 riastrad } 1160 1.16 riastrad } 1161 1.16 riastrad EXPORT_SYMBOL(drm_gem_object_put); 1162 1.16 riastrad 1163 1.2 riastrad #ifndef __NetBSD__ 1164 1.16 riastrad /** 1165 1.16 riastrad * drm_gem_vm_open - vma->ops->open implementation for GEM 1166 1.16 riastrad * @vma: VM area structure 1167 1.16 riastrad * 1168 1.16 riastrad * This function implements the #vm_operations_struct open() callback for GEM 1169 1.16 riastrad * drivers. This must be used together with drm_gem_vm_close(). 1170 1.16 riastrad */ 1171 1.1 riastrad void drm_gem_vm_open(struct vm_area_struct *vma) 1172 1.1 riastrad { 1173 1.1 riastrad struct drm_gem_object *obj = vma->vm_private_data; 1174 1.1 riastrad 1175 1.16 riastrad drm_gem_object_get(obj); 1176 1.1 riastrad } 1177 1.1 riastrad EXPORT_SYMBOL(drm_gem_vm_open); 1178 1.1 riastrad 1179 1.16 riastrad /** 1180 1.16 riastrad * drm_gem_vm_close - vma->ops->close implementation for GEM 1181 1.16 riastrad * @vma: VM area structure 1182 1.16 riastrad * 1183 1.16 riastrad * This function implements the #vm_operations_struct close() callback for GEM 1184 1.16 riastrad * drivers. This must be used together with drm_gem_vm_open(). 1185 1.16 riastrad */ 1186 1.1 riastrad void drm_gem_vm_close(struct vm_area_struct *vma) 1187 1.1 riastrad { 1188 1.1 riastrad struct drm_gem_object *obj = vma->vm_private_data; 1189 1.1 riastrad 1190 1.16 riastrad drm_gem_object_put_unlocked(obj); 1191 1.1 riastrad } 1192 1.1 riastrad EXPORT_SYMBOL(drm_gem_vm_close); 1193 1.1 riastrad 1194 1.3 riastrad /** 1195 1.3 riastrad * drm_gem_mmap_obj - memory map a GEM object 1196 1.3 riastrad * @obj: the GEM object to map 1197 1.3 riastrad * @obj_size: the object size to be mapped, in bytes 1198 1.3 riastrad * @vma: VMA for the area to be mapped 1199 1.3 riastrad * 1200 1.3 riastrad * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops 1201 1.3 riastrad * provided by the driver. Depending on their requirements, drivers can either 1202 1.3 riastrad * provide a fault handler in their gem_vm_ops (in which case any accesses to 1203 1.3 riastrad * the object will be trapped, to perform migration, GTT binding, surface 1204 1.3 riastrad * register allocation, or performance monitoring), or mmap the buffer memory 1205 1.3 riastrad * synchronously after calling drm_gem_mmap_obj. 1206 1.3 riastrad * 1207 1.3 riastrad * This function is mainly intended to implement the DMABUF mmap operation, when 1208 1.3 riastrad * the GEM object is not looked up based on its fake offset. To implement the 1209 1.3 riastrad * DRM mmap operation, drivers should use the drm_gem_mmap() function. 1210 1.3 riastrad * 1211 1.3 riastrad * drm_gem_mmap_obj() assumes the user is granted access to the buffer while 1212 1.3 riastrad * drm_gem_mmap() prevents unprivileged users from mapping random objects. So 1213 1.3 riastrad * callers must verify access restrictions before calling this helper. 1214 1.3 riastrad * 1215 1.3 riastrad * Return 0 or success or -EINVAL if the object size is smaller than the VMA 1216 1.3 riastrad * size, or if no gem_vm_ops are provided. 1217 1.3 riastrad */ 1218 1.3 riastrad int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size, 1219 1.3 riastrad struct vm_area_struct *vma) 1220 1.3 riastrad { 1221 1.3 riastrad struct drm_device *dev = obj->dev; 1222 1.16 riastrad int ret; 1223 1.3 riastrad 1224 1.3 riastrad /* Check for valid size. */ 1225 1.3 riastrad if (obj_size < vma->vm_end - vma->vm_start) 1226 1.3 riastrad return -EINVAL; 1227 1.3 riastrad 1228 1.3 riastrad /* Take a ref for this mapping of the object, so that the fault 1229 1.3 riastrad * handler can dereference the mmap offset's pointer to the object. 1230 1.3 riastrad * This reference is cleaned up by the corresponding vm_close 1231 1.3 riastrad * (which should happen whether the vma was created by this call, or 1232 1.3 riastrad * by a vm_open due to mremap or partial unmap or whatever). 1233 1.3 riastrad */ 1234 1.16 riastrad drm_gem_object_get(obj); 1235 1.16 riastrad 1236 1.16 riastrad if (obj->funcs && obj->funcs->mmap) { 1237 1.16 riastrad ret = obj->funcs->mmap(obj, vma); 1238 1.16 riastrad if (ret) { 1239 1.16 riastrad drm_gem_object_put_unlocked(obj); 1240 1.16 riastrad return ret; 1241 1.16 riastrad } 1242 1.16 riastrad WARN_ON(!(vma->vm_flags & VM_DONTEXPAND)); 1243 1.16 riastrad } else { 1244 1.16 riastrad if (obj->funcs && obj->funcs->vm_ops) 1245 1.16 riastrad vma->vm_ops = obj->funcs->vm_ops; 1246 1.16 riastrad else if (dev->driver->gem_vm_ops) 1247 1.16 riastrad vma->vm_ops = dev->driver->gem_vm_ops; 1248 1.16 riastrad else { 1249 1.16 riastrad drm_gem_object_put_unlocked(obj); 1250 1.16 riastrad return -EINVAL; 1251 1.16 riastrad } 1252 1.16 riastrad 1253 1.16 riastrad vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; 1254 1.16 riastrad vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 1255 1.16 riastrad vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); 1256 1.16 riastrad } 1257 1.16 riastrad 1258 1.16 riastrad vma->vm_private_data = obj; 1259 1.3 riastrad 1260 1.3 riastrad return 0; 1261 1.3 riastrad } 1262 1.3 riastrad EXPORT_SYMBOL(drm_gem_mmap_obj); 1263 1.1 riastrad 1264 1.1 riastrad /** 1265 1.1 riastrad * drm_gem_mmap - memory map routine for GEM objects 1266 1.1 riastrad * @filp: DRM file pointer 1267 1.1 riastrad * @vma: VMA for the area to be mapped 1268 1.1 riastrad * 1269 1.1 riastrad * If a driver supports GEM object mapping, mmap calls on the DRM file 1270 1.1 riastrad * descriptor will end up here. 1271 1.1 riastrad * 1272 1.3 riastrad * Look up the GEM object based on the offset passed in (vma->vm_pgoff will 1273 1.1 riastrad * contain the fake offset we created when the GTT map ioctl was called on 1274 1.3 riastrad * the object) and map it with a call to drm_gem_mmap_obj(). 1275 1.3 riastrad * 1276 1.3 riastrad * If the caller is not granted access to the buffer object, the mmap will fail 1277 1.3 riastrad * with EACCES. Please see the vma manager for more information. 1278 1.1 riastrad */ 1279 1.1 riastrad int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 1280 1.1 riastrad { 1281 1.1 riastrad struct drm_file *priv = filp->private_data; 1282 1.1 riastrad struct drm_device *dev = priv->minor->dev; 1283 1.7 riastrad struct drm_gem_object *obj = NULL; 1284 1.3 riastrad struct drm_vma_offset_node *node; 1285 1.3 riastrad int ret; 1286 1.1 riastrad 1287 1.16 riastrad if (drm_dev_is_unplugged(dev)) 1288 1.1 riastrad return -ENODEV; 1289 1.1 riastrad 1290 1.7 riastrad drm_vma_offset_lock_lookup(dev->vma_offset_manager); 1291 1.7 riastrad node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager, 1292 1.7 riastrad vma->vm_pgoff, 1293 1.7 riastrad vma_pages(vma)); 1294 1.7 riastrad if (likely(node)) { 1295 1.7 riastrad obj = container_of(node, struct drm_gem_object, vma_node); 1296 1.7 riastrad /* 1297 1.7 riastrad * When the object is being freed, after it hits 0-refcnt it 1298 1.7 riastrad * proceeds to tear down the object. In the process it will 1299 1.7 riastrad * attempt to remove the VMA offset and so acquire this 1300 1.7 riastrad * mgr->vm_lock. Therefore if we find an object with a 0-refcnt 1301 1.7 riastrad * that matches our range, we know it is in the process of being 1302 1.7 riastrad * destroyed and will be freed as soon as we release the lock - 1303 1.7 riastrad * so we have to check for the 0-refcnted object and treat it as 1304 1.7 riastrad * invalid. 1305 1.7 riastrad */ 1306 1.7 riastrad if (!kref_get_unless_zero(&obj->refcount)) 1307 1.7 riastrad obj = NULL; 1308 1.7 riastrad } 1309 1.7 riastrad drm_vma_offset_unlock_lookup(dev->vma_offset_manager); 1310 1.7 riastrad 1311 1.7 riastrad if (!obj) 1312 1.7 riastrad return -EINVAL; 1313 1.1 riastrad 1314 1.16 riastrad if (!drm_vma_node_is_allowed(node, priv)) { 1315 1.16 riastrad drm_gem_object_put_unlocked(obj); 1316 1.3 riastrad return -EACCES; 1317 1.1 riastrad } 1318 1.1 riastrad 1319 1.16 riastrad if (node->readonly) { 1320 1.16 riastrad if (vma->vm_flags & VM_WRITE) { 1321 1.16 riastrad drm_gem_object_put_unlocked(obj); 1322 1.16 riastrad return -EINVAL; 1323 1.16 riastrad } 1324 1.16 riastrad 1325 1.16 riastrad vma->vm_flags &= ~VM_MAYWRITE; 1326 1.16 riastrad } 1327 1.16 riastrad 1328 1.7 riastrad ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, 1329 1.7 riastrad vma); 1330 1.1 riastrad 1331 1.16 riastrad drm_gem_object_put_unlocked(obj); 1332 1.1 riastrad 1333 1.1 riastrad return ret; 1334 1.1 riastrad } 1335 1.1 riastrad EXPORT_SYMBOL(drm_gem_mmap); 1336 1.16 riastrad #endif /* defined(__NetBSD__) */ 1337 1.16 riastrad 1338 1.16 riastrad void drm_gem_print_info(struct drm_printer *p, unsigned int indent, 1339 1.16 riastrad const struct drm_gem_object *obj) 1340 1.16 riastrad { 1341 1.16 riastrad drm_printf_indent(p, indent, "name=%d\n", obj->name); 1342 1.16 riastrad drm_printf_indent(p, indent, "refcount=%u\n", 1343 1.16 riastrad kref_read(&obj->refcount)); 1344 1.16 riastrad drm_printf_indent(p, indent, "start=%08lx\n", 1345 1.16 riastrad drm_vma_node_start(&obj->vma_node)); 1346 1.16 riastrad drm_printf_indent(p, indent, "size=%zu\n", obj->size); 1347 1.16 riastrad drm_printf_indent(p, indent, "imported=%s\n", 1348 1.16 riastrad obj->import_attach ? "yes" : "no"); 1349 1.16 riastrad 1350 1.16 riastrad if (obj->funcs && obj->funcs->print_info) 1351 1.16 riastrad obj->funcs->print_info(p, indent, obj); 1352 1.16 riastrad else if (obj->dev->driver->gem_print_info) 1353 1.16 riastrad obj->dev->driver->gem_print_info(p, indent, obj); 1354 1.16 riastrad } 1355 1.16 riastrad 1356 1.16 riastrad int drm_gem_pin(struct drm_gem_object *obj) 1357 1.16 riastrad { 1358 1.16 riastrad if (obj->funcs && obj->funcs->pin) 1359 1.16 riastrad return obj->funcs->pin(obj); 1360 1.16 riastrad else if (obj->dev->driver->gem_prime_pin) 1361 1.16 riastrad return obj->dev->driver->gem_prime_pin(obj); 1362 1.16 riastrad else 1363 1.16 riastrad return 0; 1364 1.16 riastrad } 1365 1.2 riastrad 1366 1.16 riastrad void drm_gem_unpin(struct drm_gem_object *obj) 1367 1.16 riastrad { 1368 1.16 riastrad if (obj->funcs && obj->funcs->unpin) 1369 1.16 riastrad obj->funcs->unpin(obj); 1370 1.16 riastrad else if (obj->dev->driver->gem_prime_unpin) 1371 1.16 riastrad obj->dev->driver->gem_prime_unpin(obj); 1372 1.16 riastrad } 1373 1.16 riastrad 1374 1.16 riastrad void *drm_gem_vmap(struct drm_gem_object *obj) 1375 1.16 riastrad { 1376 1.16 riastrad void *vaddr; 1377 1.16 riastrad 1378 1.16 riastrad if (obj->funcs && obj->funcs->vmap) 1379 1.16 riastrad vaddr = obj->funcs->vmap(obj); 1380 1.16 riastrad else if (obj->dev->driver->gem_prime_vmap) 1381 1.16 riastrad vaddr = obj->dev->driver->gem_prime_vmap(obj); 1382 1.16 riastrad else 1383 1.16 riastrad vaddr = ERR_PTR(-EOPNOTSUPP); 1384 1.16 riastrad 1385 1.16 riastrad if (!vaddr) 1386 1.16 riastrad vaddr = ERR_PTR(-ENOMEM); 1387 1.16 riastrad 1388 1.16 riastrad return vaddr; 1389 1.16 riastrad } 1390 1.16 riastrad 1391 1.16 riastrad void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr) 1392 1.16 riastrad { 1393 1.16 riastrad if (!vaddr) 1394 1.16 riastrad return; 1395 1.16 riastrad 1396 1.16 riastrad if (obj->funcs && obj->funcs->vunmap) 1397 1.16 riastrad obj->funcs->vunmap(obj, vaddr); 1398 1.16 riastrad else if (obj->dev->driver->gem_prime_vunmap) 1399 1.16 riastrad obj->dev->driver->gem_prime_vunmap(obj, vaddr); 1400 1.16 riastrad } 1401 1.16 riastrad 1402 1.16 riastrad /** 1403 1.16 riastrad * drm_gem_lock_reservations - Sets up the ww context and acquires 1404 1.16 riastrad * the lock on an array of GEM objects. 1405 1.16 riastrad * 1406 1.16 riastrad * Once you've locked your reservations, you'll want to set up space 1407 1.16 riastrad * for your shared fences (if applicable), submit your job, then 1408 1.16 riastrad * drm_gem_unlock_reservations(). 1409 1.16 riastrad * 1410 1.16 riastrad * @objs: drm_gem_objects to lock 1411 1.16 riastrad * @count: Number of objects in @objs 1412 1.16 riastrad * @acquire_ctx: struct ww_acquire_ctx that will be initialized as 1413 1.16 riastrad * part of tracking this set of locked reservations. 1414 1.16 riastrad */ 1415 1.16 riastrad int 1416 1.16 riastrad drm_gem_lock_reservations(struct drm_gem_object **objs, int count, 1417 1.16 riastrad struct ww_acquire_ctx *acquire_ctx) 1418 1.16 riastrad { 1419 1.16 riastrad int contended = -1; 1420 1.16 riastrad int i, ret; 1421 1.16 riastrad 1422 1.16 riastrad ww_acquire_init(acquire_ctx, &reservation_ww_class); 1423 1.16 riastrad 1424 1.16 riastrad retry: 1425 1.16 riastrad if (contended != -1) { 1426 1.16 riastrad struct drm_gem_object *obj = objs[contended]; 1427 1.16 riastrad 1428 1.16 riastrad ret = dma_resv_lock_slow_interruptible(obj->resv, 1429 1.16 riastrad acquire_ctx); 1430 1.16 riastrad if (ret) { 1431 1.16 riastrad ww_acquire_done(acquire_ctx); 1432 1.16 riastrad return ret; 1433 1.16 riastrad } 1434 1.16 riastrad } 1435 1.16 riastrad 1436 1.16 riastrad for (i = 0; i < count; i++) { 1437 1.16 riastrad if (i == contended) 1438 1.16 riastrad continue; 1439 1.16 riastrad 1440 1.16 riastrad ret = dma_resv_lock_interruptible(objs[i]->resv, 1441 1.16 riastrad acquire_ctx); 1442 1.16 riastrad if (ret) { 1443 1.16 riastrad int j; 1444 1.16 riastrad 1445 1.16 riastrad for (j = 0; j < i; j++) 1446 1.16 riastrad dma_resv_unlock(objs[j]->resv); 1447 1.16 riastrad 1448 1.16 riastrad if (contended != -1 && contended >= i) 1449 1.16 riastrad dma_resv_unlock(objs[contended]->resv); 1450 1.16 riastrad 1451 1.16 riastrad if (ret == -EDEADLK) { 1452 1.16 riastrad contended = i; 1453 1.16 riastrad goto retry; 1454 1.16 riastrad } 1455 1.16 riastrad 1456 1.16 riastrad ww_acquire_done(acquire_ctx); 1457 1.16 riastrad return ret; 1458 1.16 riastrad } 1459 1.16 riastrad } 1460 1.16 riastrad 1461 1.16 riastrad ww_acquire_done(acquire_ctx); 1462 1.16 riastrad 1463 1.16 riastrad return 0; 1464 1.16 riastrad } 1465 1.16 riastrad EXPORT_SYMBOL(drm_gem_lock_reservations); 1466 1.16 riastrad 1467 1.16 riastrad void 1468 1.16 riastrad drm_gem_unlock_reservations(struct drm_gem_object **objs, int count, 1469 1.16 riastrad struct ww_acquire_ctx *acquire_ctx) 1470 1.16 riastrad { 1471 1.16 riastrad int i; 1472 1.16 riastrad 1473 1.16 riastrad for (i = 0; i < count; i++) 1474 1.16 riastrad dma_resv_unlock(objs[i]->resv); 1475 1.16 riastrad 1476 1.16 riastrad ww_acquire_fini(acquire_ctx); 1477 1.16 riastrad } 1478 1.16 riastrad EXPORT_SYMBOL(drm_gem_unlock_reservations); 1479 1.16 riastrad 1480 1.16 riastrad /** 1481 1.16 riastrad * drm_gem_fence_array_add - Adds the fence to an array of fences to be 1482 1.16 riastrad * waited on, deduplicating fences from the same context. 1483 1.16 riastrad * 1484 1.16 riastrad * @fence_array: array of dma_fence * for the job to block on. 1485 1.16 riastrad * @fence: the dma_fence to add to the list of dependencies. 1486 1.16 riastrad * 1487 1.16 riastrad * Returns: 1488 1.16 riastrad * 0 on success, or an error on failing to expand the array. 1489 1.16 riastrad */ 1490 1.16 riastrad int drm_gem_fence_array_add(struct xarray *fence_array, 1491 1.16 riastrad struct dma_fence *fence) 1492 1.16 riastrad { 1493 1.16 riastrad struct dma_fence *entry; 1494 1.16 riastrad unsigned long index; 1495 1.16 riastrad u32 id = 0; 1496 1.16 riastrad int ret; 1497 1.16 riastrad 1498 1.16 riastrad if (!fence) 1499 1.16 riastrad return 0; 1500 1.16 riastrad 1501 1.16 riastrad /* Deduplicate if we already depend on a fence from the same context. 1502 1.16 riastrad * This lets the size of the array of deps scale with the number of 1503 1.16 riastrad * engines involved, rather than the number of BOs. 1504 1.16 riastrad */ 1505 1.16 riastrad xa_for_each(fence_array, index, entry) { 1506 1.16 riastrad if (entry->context != fence->context) 1507 1.16 riastrad continue; 1508 1.16 riastrad 1509 1.16 riastrad if (dma_fence_is_later(fence, entry)) { 1510 1.16 riastrad dma_fence_put(entry); 1511 1.16 riastrad xa_store(fence_array, index, fence, GFP_KERNEL); 1512 1.16 riastrad } else { 1513 1.16 riastrad dma_fence_put(fence); 1514 1.16 riastrad } 1515 1.16 riastrad return 0; 1516 1.16 riastrad } 1517 1.16 riastrad 1518 1.16 riastrad ret = xa_alloc(fence_array, &id, fence, xa_limit_32b, GFP_KERNEL); 1519 1.16 riastrad if (ret != 0) 1520 1.16 riastrad dma_fence_put(fence); 1521 1.16 riastrad 1522 1.16 riastrad return ret; 1523 1.16 riastrad } 1524 1.16 riastrad EXPORT_SYMBOL(drm_gem_fence_array_add); 1525 1.16 riastrad 1526 1.16 riastrad /** 1527 1.16 riastrad * drm_gem_fence_array_add_implicit - Adds the implicit dependencies tracked 1528 1.16 riastrad * in the GEM object's reservation object to an array of dma_fences for use in 1529 1.16 riastrad * scheduling a rendering job. 1530 1.16 riastrad * 1531 1.16 riastrad * This should be called after drm_gem_lock_reservations() on your array of 1532 1.16 riastrad * GEM objects used in the job but before updating the reservations with your 1533 1.16 riastrad * own fences. 1534 1.16 riastrad * 1535 1.16 riastrad * @fence_array: array of dma_fence * for the job to block on. 1536 1.16 riastrad * @obj: the gem object to add new dependencies from. 1537 1.16 riastrad * @write: whether the job might write the object (so we need to depend on 1538 1.16 riastrad * shared fences in the reservation object). 1539 1.16 riastrad */ 1540 1.16 riastrad int drm_gem_fence_array_add_implicit(struct xarray *fence_array, 1541 1.16 riastrad struct drm_gem_object *obj, 1542 1.16 riastrad bool write) 1543 1.16 riastrad { 1544 1.16 riastrad int ret; 1545 1.16 riastrad struct dma_fence **fences; 1546 1.16 riastrad unsigned int i, fence_count; 1547 1.16 riastrad 1548 1.16 riastrad if (!write) { 1549 1.16 riastrad struct dma_fence *fence = 1550 1.16 riastrad dma_resv_get_excl_rcu(obj->resv); 1551 1.16 riastrad 1552 1.16 riastrad return drm_gem_fence_array_add(fence_array, fence); 1553 1.16 riastrad } 1554 1.16 riastrad 1555 1.16 riastrad ret = dma_resv_get_fences_rcu(obj->resv, NULL, 1556 1.16 riastrad &fence_count, &fences); 1557 1.16 riastrad if (ret || !fence_count) 1558 1.16 riastrad return ret; 1559 1.16 riastrad 1560 1.16 riastrad for (i = 0; i < fence_count; i++) { 1561 1.16 riastrad ret = drm_gem_fence_array_add(fence_array, fences[i]); 1562 1.16 riastrad if (ret) 1563 1.16 riastrad break; 1564 1.16 riastrad } 1565 1.16 riastrad 1566 1.16 riastrad for (; i < fence_count; i++) 1567 1.16 riastrad dma_fence_put(fences[i]); 1568 1.16 riastrad kfree(fences); 1569 1.16 riastrad return ret; 1570 1.16 riastrad } 1571 1.16 riastrad EXPORT_SYMBOL(drm_gem_fence_array_add_implicit); 1572