1#ifndef __DRM_GEM_H__ 2#define __DRM_GEM_H__ 3 4/* 5 * GEM Graphics Execution Manager Driver Interfaces 6 * 7 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 8 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 9 * Copyright (c) 2009-2010, Code Aurora Forum. 10 * All rights reserved. 11 * Copyright �� 2014 Intel Corporation 12 * Daniel Vetter <daniel.vetter@ffwll.ch> 13 * 14 * Author: Rickard E. (Rik) Faith <faith@valinux.com> 15 * Author: Gareth Hughes <gareth@valinux.com> 16 * 17 * Permission is hereby granted, free of charge, to any person obtaining a 18 * copy of this software and associated documentation files (the "Software"), 19 * to deal in the Software without restriction, including without limitation 20 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 21 * and/or sell copies of the Software, and to permit persons to whom the 22 * Software is furnished to do so, subject to the following conditions: 23 * 24 * The above copyright notice and this permission notice (including the next 25 * paragraph) shall be included in all copies or substantial portions of the 26 * Software. 27 * 28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 31 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 32 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 33 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 34 * OTHER DEALINGS IN THE SOFTWARE. 35 */ 36 37#include <linux/kref.h> 38#include <linux/dma-resv.h> 39#include <linux/list.h> 40#include <linux/mutex.h> 41 42#include <drm/drm_vma_manager.h> 43 44struct iosys_map; 45struct drm_gem_object; 46 47/** 48 * enum drm_gem_object_status - bitmask of object state for fdinfo reporting 49 * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned) 50 * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace 51 * 52 * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status 53 * and drm_show_fdinfo(). Note that an object can DRM_GEM_OBJECT_PURGEABLE if 54 * it still active or not resident, in which case drm_show_fdinfo() will not 55 * account for it as purgeable. So drivers do not need to check if the buffer 56 * is idle and resident to return this bit. (Ie. userspace can mark a buffer 57 * as purgeable even while it is still busy on the GPU.. it does not _actually_ 58 * become puregeable until it becomes idle. The status gem object func does 59 * not need to consider this.) 60 */ 61enum drm_gem_object_status { 62 DRM_GEM_OBJECT_RESIDENT = BIT(0), 63 DRM_GEM_OBJECT_PURGEABLE = BIT(1), 64}; 65 66/** 67 * struct drm_gem_object_funcs - GEM object functions 68 */ 69struct drm_gem_object_funcs { 70 /** 71 * @free: 72 * 73 * Deconstructor for drm_gem_objects. 74 * 75 * This callback is mandatory. 76 */ 77 void (*free)(struct drm_gem_object *obj); 78 79 /** 80 * @open: 81 * 82 * Called upon GEM handle creation. 83 * 84 * This callback is optional. 85 */ 86 int (*open)(struct drm_gem_object *obj, struct drm_file *file); 87 88 /** 89 * @close: 90 * 91 * Called upon GEM handle release. 92 * 93 * This callback is optional. 94 */ 95 void (*close)(struct drm_gem_object *obj, struct drm_file *file); 96 97 /** 98 * @print_info: 99 * 100 * If driver subclasses struct &drm_gem_object, it can implement this 101 * optional hook for printing additional driver specific info. 102 * 103 * drm_printf_indent() should be used in the callback passing it the 104 * indent argument. 105 * 106 * This callback is called from drm_gem_print_info(). 107 * 108 * This callback is optional. 109 */ 110 void (*print_info)(struct drm_printer *p, unsigned int indent, 111 const struct drm_gem_object *obj); 112 113 /** 114 * @export: 115 * 116 * Export backing buffer as a &dma_buf. 117 * If this is not set drm_gem_prime_export() is used. 118 * 119 * This callback is optional. 120 */ 121 struct dma_buf *(*export)(struct drm_gem_object *obj, int flags); 122 123 /** 124 * @pin: 125 * 126 * Pin backing buffer in memory. Used by the drm_gem_map_attach() helper. 127 * 128 * This callback is optional. 129 */ 130 int (*pin)(struct drm_gem_object *obj); 131 132 /** 133 * @unpin: 134 * 135 * Unpin backing buffer. Used by the drm_gem_map_detach() helper. 136 * 137 * This callback is optional. 138 */ 139 void (*unpin)(struct drm_gem_object *obj); 140 141 /** 142 * @get_sg_table: 143 * 144 * Returns a Scatter-Gather table representation of the buffer. 145 * Used when exporting a buffer by the drm_gem_map_dma_buf() helper. 146 * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table() 147 * in drm_gem_unmap_buf(), therefore these helpers and this callback 148 * here cannot be used for sg tables pointing at driver private memory 149 * ranges. 150 * 151 * See also drm_prime_pages_to_sg(). 152 */ 153 struct sg_table *(*get_sg_table)(struct drm_gem_object *obj); 154 155 /** 156 * @vmap: 157 * 158 * Returns a virtual address for the buffer. Used by the 159 * drm_gem_dmabuf_vmap() helper. 160 * 161 * This callback is optional. 162 */ 163 int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map); 164 165 /** 166 * @vunmap: 167 * 168 * Releases the address previously returned by @vmap. Used by the 169 * drm_gem_dmabuf_vunmap() helper. 170 * 171 * This callback is optional. 172 */ 173 void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map); 174 175 /** 176 * @mmap: 177 * 178 * Handle mmap() of the gem object, setup vma accordingly. 179 * 180 * This callback is optional. 181 * 182 * The callback is used by both drm_gem_mmap_obj() and 183 * drm_gem_prime_mmap(). When @mmap is present @vm_ops is not 184 * used, the @mmap callback must set vma->vm_ops instead. 185 */ 186#ifdef __linux__ 187 int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma); 188#else 189 int (*mmap)(struct drm_gem_object *, vm_prot_t, voff_t, vsize_t); 190#endif 191 192 /** 193 * @evict: 194 * 195 * Evicts gem object out from memory. Used by the drm_gem_object_evict() 196 * helper. Returns 0 on success, -errno otherwise. 197 * 198 * This callback is optional. 199 */ 200 int (*evict)(struct drm_gem_object *obj); 201 202 /** 203 * @status: 204 * 205 * The optional status callback can return additional object state 206 * which determines which stats the object is counted against. The 207 * callback is called under table_lock. Racing against object status 208 * change is "harmless", and the callback can expect to not race 209 * against object destruction. 210 * 211 * Called by drm_show_memory_stats(). 212 */ 213 enum drm_gem_object_status (*status)(struct drm_gem_object *obj); 214 215 /** 216 * @vm_ops: 217 * 218 * Virtual memory operations used with mmap. 219 * 220 * This is optional but necessary for mmap support. 221 */ 222#ifdef __linux__ 223 const struct vm_operations_struct *vm_ops; 224#else 225 const struct uvm_pagerops *vm_ops; 226#endif 227}; 228 229/** 230 * struct drm_gem_lru - A simple LRU helper 231 * 232 * A helper for tracking GEM objects in a given state, to aid in 233 * driver's shrinker implementation. Tracks the count of pages 234 * for lockless &shrinker.count_objects, and provides 235 * &drm_gem_lru_scan for driver's &shrinker.scan_objects 236 * implementation. 237 */ 238struct drm_gem_lru { 239 /** 240 * @lock: 241 * 242 * Lock protecting movement of GEM objects between LRUs. All 243 * LRUs that the object can move between should be protected 244 * by the same lock. 245 */ 246 struct rwlock *lock; 247 248 /** 249 * @count: 250 * 251 * The total number of backing pages of the GEM objects in 252 * this LRU. 253 */ 254 long count; 255 256 /** 257 * @list: 258 * 259 * The LRU list. 260 */ 261 struct list_head list; 262}; 263 264/** 265 * struct drm_gem_object - GEM buffer object 266 * 267 * This structure defines the generic parts for GEM buffer objects, which are 268 * mostly around handling mmap and userspace handles. 269 * 270 * Buffer objects are often abbreviated to BO. 271 */ 272struct drm_gem_object { 273 /* 274 * This must be first as uobj is cast to ttm_buffer_object for 275 * radeon_ttm_fault() the first member of that struct is drm_gem_object 276 */ 277 struct uvm_object uobj; 278 279 /** 280 * @refcount: 281 * 282 * Reference count of this object 283 * 284 * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked() 285 * or drm_gem_object_put() to release a reference to a GEM 286 * buffer object. 287 */ 288 struct kref refcount; 289 290 /** 291 * @handle_count: 292 * 293 * This is the GEM file_priv handle count of this object. 294 * 295 * Each handle also holds a reference. Note that when the handle_count 296 * drops to 0 any global names (e.g. the id in the flink namespace) will 297 * be cleared. 298 * 299 * Protected by &drm_device.object_name_lock. 300 */ 301 unsigned handle_count; 302 303 /** 304 * @dev: DRM dev this object belongs to. 305 */ 306 struct drm_device *dev; 307 308 /** 309 * @filp: 310 * 311 * SHMEM file node used as backing storage for swappable buffer objects. 312 * GEM also supports driver private objects with driver-specific backing 313 * storage (contiguous DMA memory, special reserved blocks). In this 314 * case @filp is NULL. 315 */ 316#ifdef __linux__ 317 struct file *filp; 318#endif 319 320 /** 321 * @vma_node: 322 * 323 * Mapping info for this object to support mmap. Drivers are supposed to 324 * allocate the mmap offset using drm_gem_create_mmap_offset(). The 325 * offset itself can be retrieved using drm_vma_node_offset_addr(). 326 * 327 * Memory mapping itself is handled by drm_gem_mmap(), which also checks 328 * that userspace is allowed to access the object. 329 */ 330 struct drm_vma_offset_node vma_node; 331 332 /** 333 * @size: 334 * 335 * Size of the object, in bytes. Immutable over the object's 336 * lifetime. 337 */ 338 size_t size; 339 340 /** 341 * @name: 342 * 343 * Global name for this object, starts at 1. 0 means unnamed. 344 * Access is covered by &drm_device.object_name_lock. This is used by 345 * the GEM_FLINK and GEM_OPEN ioctls. 346 */ 347 int name; 348 349 /** 350 * @dma_buf: 351 * 352 * dma-buf associated with this GEM object. 353 * 354 * Pointer to the dma-buf associated with this gem object (either 355 * through importing or exporting). We break the resulting reference 356 * loop when the last gem handle for this object is released. 357 * 358 * Protected by &drm_device.object_name_lock. 359 */ 360 struct dma_buf *dma_buf; 361 362 /** 363 * @import_attach: 364 * 365 * dma-buf attachment backing this object. 366 * 367 * Any foreign dma_buf imported as a gem object has this set to the 368 * attachment point for the device. This is invariant over the lifetime 369 * of a gem object. 370 * 371 * The &drm_gem_object_funcs.free callback is responsible for 372 * cleaning up the dma_buf attachment and references acquired at import 373 * time. 374 * 375 * Note that the drm gem/prime core does not depend upon drivers setting 376 * this field any more. So for drivers where this doesn't make sense 377 * (e.g. virtual devices or a displaylink behind an usb bus) they can 378 * simply leave it as NULL. 379 */ 380 struct dma_buf_attachment *import_attach; 381 382 /** 383 * @resv: 384 * 385 * Pointer to reservation object associated with the this GEM object. 386 * 387 * Normally (@resv == &@_resv) except for imported GEM objects. 388 */ 389 struct dma_resv *resv; 390 391 /** 392 * @_resv: 393 * 394 * A reservation object for this GEM object. 395 * 396 * This is unused for imported GEM objects. 397 */ 398 struct dma_resv _resv; 399 400 /** 401 * @gpuva: 402 * 403 * Provides the list of GPU VAs attached to this GEM object. 404 * 405 * Drivers should lock list accesses with the GEMs &dma_resv lock 406 * (&drm_gem_object.resv) or a custom lock if one is provided. 407 */ 408 struct { 409 struct list_head list; 410 411#ifdef CONFIG_LOCKDEP 412 struct lockdep_map *lock_dep_map; 413#endif 414 } gpuva; 415 416 /** 417 * @funcs: 418 * 419 * Optional GEM object functions. If this is set, it will be used instead of the 420 * corresponding &drm_driver GEM callbacks. 421 * 422 * New drivers should use this. 423 * 424 */ 425 const struct drm_gem_object_funcs *funcs; 426 427 /** 428 * @lru_node: 429 * 430 * List node in a &drm_gem_lru. 431 */ 432 struct list_head lru_node; 433 434 /** 435 * @lru: 436 * 437 * The current LRU list that the GEM object is on. 438 */ 439 struct drm_gem_lru *lru; 440 441 SPLAY_ENTRY(drm_gem_object) entry; 442 struct uvm_object *uao; 443}; 444 445/** 446 * DRM_GEM_FOPS - Default drm GEM file operations 447 * 448 * This macro provides a shorthand for setting the GEM file ops in the 449 * &file_operations structure. If all you need are the default ops, use 450 * DEFINE_DRM_GEM_FOPS instead. 451 */ 452#define DRM_GEM_FOPS \ 453 .open = drm_open,\ 454 .release = drm_release,\ 455 .unlocked_ioctl = drm_ioctl,\ 456 .compat_ioctl = drm_compat_ioctl,\ 457 .poll = drm_poll,\ 458 .read = drm_read,\ 459 .llseek = noop_llseek,\ 460 .mmap = drm_gem_mmap 461 462/** 463 * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers 464 * @name: name for the generated structure 465 * 466 * This macro autogenerates a suitable &struct file_operations for GEM based 467 * drivers, which can be assigned to &drm_driver.fops. Note that this structure 468 * cannot be shared between drivers, because it contains a reference to the 469 * current module using THIS_MODULE. 470 * 471 * Note that the declaration is already marked as static - if you need a 472 * non-static version of this you're probably doing it wrong and will break the 473 * THIS_MODULE reference by accident. 474 */ 475#define DEFINE_DRM_GEM_FOPS(name) \ 476 static const struct file_operations name = {\ 477 .owner = THIS_MODULE,\ 478 DRM_GEM_FOPS,\ 479 } 480 481void drm_gem_object_release(struct drm_gem_object *obj); 482void drm_gem_object_free(struct kref *kref); 483int drm_gem_object_init(struct drm_device *dev, 484 struct drm_gem_object *obj, size_t size); 485void drm_gem_private_object_init(struct drm_device *dev, 486 struct drm_gem_object *obj, size_t size); 487void drm_gem_private_object_fini(struct drm_gem_object *obj); 488#ifdef __linux__ 489void drm_gem_vm_open(struct vm_area_struct *vma); 490void drm_gem_vm_close(struct vm_area_struct *vma); 491int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size, 492 struct vm_area_struct *vma); 493int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma); 494#else 495struct uvm_object *drm_gem_mmap(struct file *, vm_prot_t, voff_t, vsize_t); 496#endif 497 498/** 499 * drm_gem_object_get - acquire a GEM buffer object reference 500 * @obj: GEM buffer object 501 * 502 * This function acquires an additional reference to @obj. It is illegal to 503 * call this without already holding a reference. No locks required. 504 */ 505static inline void drm_gem_object_get(struct drm_gem_object *obj) 506{ 507 kref_get(&obj->refcount); 508} 509 510__attribute__((nonnull)) 511static inline void 512__drm_gem_object_put(struct drm_gem_object *obj) 513{ 514 kref_put(&obj->refcount, drm_gem_object_free); 515} 516 517/** 518 * drm_gem_object_put - drop a GEM buffer object reference 519 * @obj: GEM buffer object 520 * 521 * This releases a reference to @obj. 522 */ 523static inline void 524drm_gem_object_put(struct drm_gem_object *obj) 525{ 526 if (obj) 527 __drm_gem_object_put(obj); 528} 529 530int drm_gem_handle_create(struct drm_file *file_priv, 531 struct drm_gem_object *obj, 532 u32 *handlep); 533int drm_gem_handle_delete(struct drm_file *filp, u32 handle); 534 535 536void drm_gem_free_mmap_offset(struct drm_gem_object *obj); 537int drm_gem_create_mmap_offset(struct drm_gem_object *obj); 538int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size); 539 540struct vm_page **drm_gem_get_pages(struct drm_gem_object *obj); 541void drm_gem_put_pages(struct drm_gem_object *obj, struct vm_page **pages, 542 bool dirty, bool accessed); 543 544int drm_gem_vmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map); 545void drm_gem_vunmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map); 546 547int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, 548 int count, struct drm_gem_object ***objs_out); 549struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle); 550long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle, 551 bool wait_all, unsigned long timeout); 552int drm_gem_lock_reservations(struct drm_gem_object **objs, int count, 553 struct ww_acquire_ctx *acquire_ctx); 554void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count, 555 struct ww_acquire_ctx *acquire_ctx); 556int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, 557 u32 handle, u64 *offset); 558 559void drm_gem_lru_init(struct drm_gem_lru *lru, struct rwlock *lock); 560void drm_gem_lru_remove(struct drm_gem_object *obj); 561void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj); 562void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj); 563unsigned long drm_gem_lru_scan(struct drm_gem_lru *lru, 564 unsigned int nr_to_scan, 565 unsigned long *remaining, 566 bool (*shrink)(struct drm_gem_object *obj)); 567 568int drm_gem_evict(struct drm_gem_object *obj); 569 570/** 571 * drm_gem_object_is_shared_for_memory_stats - helper for shared memory stats 572 * 573 * This helper should only be used for fdinfo shared memory stats to determine 574 * if a GEM object is shared. 575 * 576 * @obj: obj in question 577 */ 578static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object *obj) 579{ 580 return (obj->handle_count > 1) || obj->dma_buf; 581} 582 583#ifdef CONFIG_LOCKDEP 584/** 585 * drm_gem_gpuva_set_lock() - Set the lock protecting accesses to the gpuva list. 586 * @obj: the &drm_gem_object 587 * @lock: the lock used to protect the gpuva list. The locking primitive 588 * must contain a dep_map field. 589 * 590 * Call this if you're not proctecting access to the gpuva list with the 591 * dma-resv lock, but with a custom lock. 592 */ 593#define drm_gem_gpuva_set_lock(obj, lock) \ 594 if (!WARN((obj)->gpuva.lock_dep_map, \ 595 "GEM GPUVA lock should be set only once.")) \ 596 (obj)->gpuva.lock_dep_map = &(lock)->dep_map 597#define drm_gem_gpuva_assert_lock_held(obj) \ 598 lockdep_assert((obj)->gpuva.lock_dep_map ? \ 599 lock_is_held((obj)->gpuva.lock_dep_map) : \ 600 dma_resv_held((obj)->resv)) 601#else 602#define drm_gem_gpuva_set_lock(obj, lock) do {} while (0) 603#define drm_gem_gpuva_assert_lock_held(obj) do {} while (0) 604#endif 605 606/** 607 * drm_gem_gpuva_init() - initialize the gpuva list of a GEM object 608 * @obj: the &drm_gem_object 609 * 610 * This initializes the &drm_gem_object's &drm_gpuva list. 611 * 612 * Calling this function is only necessary for drivers intending to support the 613 * &drm_driver_feature DRIVER_GEM_GPUVA. 614 * 615 * See also drm_gem_gpuva_set_lock(). 616 */ 617static inline void drm_gem_gpuva_init(struct drm_gem_object *obj) 618{ 619 INIT_LIST_HEAD(&obj->gpuva.list); 620} 621 622/** 623 * drm_gem_for_each_gpuva() - iternator to walk over a list of gpuvas 624 * @entry__: &drm_gpuva structure to assign to in each iteration step 625 * @obj__: the &drm_gem_object the &drm_gpuvas to walk are associated with 626 * 627 * This iterator walks over all &drm_gpuva structures associated with the 628 * &drm_gpuva_manager. 629 */ 630#define drm_gem_for_each_gpuva(entry__, obj__) \ 631 list_for_each_entry(entry__, &(obj__)->gpuva.list, gem.entry) 632 633/** 634 * drm_gem_for_each_gpuva_safe() - iternator to safely walk over a list of 635 * gpuvas 636 * @entry__: &drm_gpuva structure to assign to in each iteration step 637 * @next__: &next &drm_gpuva to store the next step 638 * @obj__: the &drm_gem_object the &drm_gpuvas to walk are associated with 639 * 640 * This iterator walks over all &drm_gpuva structures associated with the 641 * &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence 642 * it is save against removal of elements. 643 */ 644#define drm_gem_for_each_gpuva_safe(entry__, next__, obj__) \ 645 list_for_each_entry_safe(entry__, next__, &(obj__)->gpuva.list, gem.entry) 646 647void drm_ref(struct uvm_object *); 648void drm_unref(struct uvm_object *); 649 650#endif /* __DRM_GEM_H__ */ 651