1/************************************************************************** 2 * 3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24 * USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27/* 28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 29 */ 30 31#ifndef _TTM_BO_API_H_ 32#define _TTM_BO_API_H_ 33 34#include <drm/drm_gem.h> 35#include <drm/drm_hashtab.h> 36#include <drm/drm_vma_manager.h> 37#include <linux/kref.h> 38#include <linux/list.h> 39#include <linux/wait.h> 40#include <linux/mutex.h> 41#include <linux/mm.h> 42#include <linux/bitmap.h> 43#include <linux/dma-resv.h> 44 45#include "ttm_resource.h" 46 47struct ttm_bo_global; 48 49struct ttm_bo_device; 50 51struct drm_mm_node; 52 53struct ttm_placement; 54 55struct ttm_place; 56 57struct ttm_lru_bulk_move; 58 59/** 60 * enum ttm_bo_type 61 * 62 * @ttm_bo_type_device: These are 'normal' buffers that can 63 * be mmapped by user space. Each of these bos occupy a slot in the 64 * device address space, that can be used for normal vm operations. 65 * 66 * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers, 67 * but they cannot be accessed from user-space. For kernel-only use. 68 * 69 * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another 70 * driver. 71 */ 72 73enum ttm_bo_type { 74 ttm_bo_type_device, 75 ttm_bo_type_kernel, 76 ttm_bo_type_sg 77}; 78 79struct ttm_tt; 80 81/** 82 * struct ttm_buffer_object 83 * 84 * @base: drm_gem_object superclass data. 85 * @bdev: Pointer to the buffer object device structure. 86 * @type: The bo type. 87 * @destroy: Destruction function. If NULL, kfree is used. 88 * @num_pages: Actual number of pages. 89 * @acc_size: Accounted size for this object. 90 * @kref: Reference count of this buffer object. When this refcount reaches 91 * zero, the object is destroyed or put on the delayed delete list. 92 * @mem: structure describing current placement. 93 * @persistent_swap_storage: Usually the swap storage is deleted for buffers 94 * pinned in physical memory. If this behaviour is not desired, this member 95 * holds a pointer to a persistent shmem object. 96 * @ttm: TTM structure holding system pages. 97 * @evicted: Whether the object was evicted without user-space knowing. 98 * @deleted: True if the object is only a zombie and already deleted. 99 * @lru: List head for the lru list. 100 * @ddestroy: List head for the delayed destroy list. 101 * @swap: List head for swap LRU list. 102 * @moving: Fence set when BO is moving 103 * @offset: The current GPU offset, which can have different meanings 104 * depending on the memory type. For SYSTEM type memory, it should be 0. 105 * @cur_placement: Hint of current placement. 106 * 107 * Base class for TTM buffer object, that deals with data placement and CPU 108 * mappings. GPU mappings are really up to the driver, but for simpler GPUs 109 * the driver can usually use the placement offset @offset directly as the 110 * GPU virtual address. For drivers implementing multiple 111 * GPU memory manager contexts, the driver should manage the address space 112 * in these contexts separately and use these objects to get the correct 113 * placement and caching for these GPU maps. This makes it possible to use 114 * these objects for even quite elaborate memory management schemes. 115 * The destroy member, the API visibility of this object makes it possible 116 * to derive driver specific types. 117 */ 118 119struct ttm_buffer_object { 120 struct drm_gem_object base; 121 122 /** 123 * Members constant at init. 124 */ 125 126 struct ttm_bo_device *bdev; 127 enum ttm_bo_type type; 128 void (*destroy) (struct ttm_buffer_object *); 129 unsigned long num_pages; 130 size_t acc_size; 131 132 /** 133 * Members not needing protection. 134 */ 135 struct kref kref; 136 137 /** 138 * Members protected by the bo::resv::reserved lock. 139 */ 140 141 struct ttm_resource mem; 142 struct file *persistent_swap_storage; 143 struct ttm_tt *ttm; 144 bool deleted; 145 146 /** 147 * Members protected by the bdev::lru_lock. 148 */ 149 150 struct list_head lru; 151 struct list_head ddestroy; 152 struct list_head swap; 153 154 /** 155 * Members protected by a bo reservation. 156 */ 157 158 struct dma_fence *moving; 159 unsigned priority; 160 161 /** 162 * Special members that are protected by the reserve lock 163 * and the bo::lock when written to. Can be read with 164 * either of these locks held. 165 */ 166 167 struct sg_table *sg; 168}; 169 170/** 171 * struct ttm_bo_kmap_obj 172 * 173 * @virtual: The current kernel virtual address. 174 * @page: The page when kmap'ing a single page. 175 * @bo_kmap_type: Type of bo_kmap. 176 * 177 * Object describing a kernel mapping. Since a TTM bo may be located 178 * in various memory types with various caching policies, the 179 * mapping can either be an ioremap, a vmap, a kmap or part of a 180 * premapped region. 181 */ 182 183#define TTM_BO_MAP_IOMEM_MASK 0x80 184struct ttm_bo_kmap_obj { 185 void *virtual; 186 struct page *page; 187 enum { 188 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK, 189 ttm_bo_map_vmap = 2, 190 ttm_bo_map_kmap = 3, 191 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK, 192 } bo_kmap_type; 193 struct ttm_buffer_object *bo; 194}; 195 196/** 197 * struct ttm_operation_ctx 198 * 199 * @interruptible: Sleep interruptible if sleeping. 200 * @no_wait_gpu: Return immediately if the GPU is busy. 201 * @resv: Reservation object to allow reserved evictions with. 202 * @flags: Including the following flags 203 * 204 * Context for TTM operations like changing buffer placement or general memory 205 * allocation. 206 */ 207struct ttm_operation_ctx { 208 bool interruptible; 209 bool no_wait_gpu; 210 struct dma_resv *resv; 211 uint64_t bytes_moved; 212 uint32_t flags; 213}; 214 215/* Allow eviction of reserved BOs */ 216#define TTM_OPT_FLAG_ALLOW_RES_EVICT 0x1 217/* when serving page fault or suspend, allow alloc anyway */ 218#define TTM_OPT_FLAG_FORCE_ALLOC 0x2 219 220/** 221 * ttm_bo_get - reference a struct ttm_buffer_object 222 * 223 * @bo: The buffer object. 224 */ 225static inline void ttm_bo_get(struct ttm_buffer_object *bo) 226{ 227 kref_get(&bo->kref); 228} 229 230/** 231 * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless 232 * its refcount has already reached zero. 233 * @bo: The buffer object. 234 * 235 * Used to reference a TTM buffer object in lookups where the object is removed 236 * from the lookup structure during the destructor and for RCU lookups. 237 * 238 * Returns: @bo if the referencing was successful, NULL otherwise. 239 */ 240static inline __must_check struct ttm_buffer_object * 241ttm_bo_get_unless_zero(struct ttm_buffer_object *bo) 242{ 243 if (!kref_get_unless_zero(&bo->kref)) 244 return NULL; 245 return bo; 246} 247 248/** 249 * ttm_bo_wait - wait for buffer idle. 250 * 251 * @bo: The buffer object. 252 * @interruptible: Use interruptible wait. 253 * @no_wait: Return immediately if buffer is busy. 254 * 255 * This function must be called with the bo::mutex held, and makes 256 * sure any previous rendering to the buffer is completed. 257 * Note: It might be necessary to block validations before the 258 * wait by reserving the buffer. 259 * Returns -EBUSY if no_wait is true and the buffer is busy. 260 * Returns -ERESTARTSYS if interrupted by a signal. 261 */ 262int ttm_bo_wait(struct ttm_buffer_object *bo, bool interruptible, bool no_wait); 263 264/** 265 * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo 266 * 267 * @placement: Return immediately if buffer is busy. 268 * @mem: The struct ttm_resource indicating the region where the bo resides 269 * @new_flags: Describes compatible placement found 270 * 271 * Returns true if the placement is compatible 272 */ 273bool ttm_bo_mem_compat(struct ttm_placement *placement, struct ttm_resource *mem, 274 uint32_t *new_flags); 275 276/** 277 * ttm_bo_validate 278 * 279 * @bo: The buffer object. 280 * @placement: Proposed placement for the buffer object. 281 * @ctx: validation parameters. 282 * 283 * Changes placement and caching policy of the buffer object 284 * according proposed placement. 285 * Returns 286 * -EINVAL on invalid proposed placement. 287 * -ENOMEM on out-of-memory condition. 288 * -EBUSY if no_wait is true and buffer busy. 289 * -ERESTARTSYS if interrupted by a signal. 290 */ 291int ttm_bo_validate(struct ttm_buffer_object *bo, 292 struct ttm_placement *placement, 293 struct ttm_operation_ctx *ctx); 294 295/** 296 * ttm_bo_put 297 * 298 * @bo: The buffer object. 299 * 300 * Unreference a buffer object. 301 */ 302void ttm_bo_put(struct ttm_buffer_object *bo); 303 304/** 305 * ttm_bo_move_to_lru_tail 306 * 307 * @bo: The buffer object. 308 * @bulk: optional bulk move structure to remember BO positions 309 * 310 * Move this BO to the tail of all lru lists used to lookup and reserve an 311 * object. This function must be called with struct ttm_bo_global::lru_lock 312 * held, and is used to make a BO less likely to be considered for eviction. 313 */ 314void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo, 315 struct ttm_lru_bulk_move *bulk); 316 317/** 318 * ttm_bo_bulk_move_lru_tail 319 * 320 * @bulk: bulk move structure 321 * 322 * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that 323 * BO order never changes. Should be called with ttm_bo_global::lru_lock held. 324 */ 325void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk); 326 327/** 328 * ttm_bo_lock_delayed_workqueue 329 * 330 * Prevent the delayed workqueue from running. 331 * Returns 332 * True if the workqueue was queued at the time 333 */ 334int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev); 335 336/** 337 * ttm_bo_unlock_delayed_workqueue 338 * 339 * Allows the delayed workqueue to run. 340 */ 341void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched); 342 343/** 344 * ttm_bo_eviction_valuable 345 * 346 * @bo: The buffer object to evict 347 * @place: the placement we need to make room for 348 * 349 * Check if it is valuable to evict the BO to make room for the given placement. 350 */ 351bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, 352 const struct ttm_place *place); 353 354size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, 355 unsigned long bo_size, 356 unsigned struct_size); 357 358/** 359 * ttm_bo_init_reserved 360 * 361 * @bdev: Pointer to a ttm_bo_device struct. 362 * @bo: Pointer to a ttm_buffer_object to be initialized. 363 * @size: Requested size of buffer object. 364 * @type: Requested type of buffer object. 365 * @flags: Initial placement flags. 366 * @page_alignment: Data alignment in pages. 367 * @ctx: TTM operation context for memory allocation. 368 * @acc_size: Accounted size for this object. 369 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one. 370 * @destroy: Destroy function. Use NULL for kfree(). 371 * 372 * This function initializes a pre-allocated struct ttm_buffer_object. 373 * As this object may be part of a larger structure, this function, 374 * together with the @destroy function, 375 * enables driver-specific objects derived from a ttm_buffer_object. 376 * 377 * On successful return, the caller owns an object kref to @bo. The kref and 378 * list_kref are usually set to 1, but note that in some situations, other 379 * tasks may already be holding references to @bo as well. 380 * Furthermore, if resv == NULL, the buffer's reservation lock will be held, 381 * and it is the caller's responsibility to call ttm_bo_unreserve. 382 * 383 * If a failure occurs, the function will call the @destroy function, or 384 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is 385 * illegal and will likely cause memory corruption. 386 * 387 * Returns 388 * -ENOMEM: Out of memory. 389 * -EINVAL: Invalid placement flags. 390 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources. 391 */ 392 393int ttm_bo_init_reserved(struct ttm_bo_device *bdev, 394 struct ttm_buffer_object *bo, 395 unsigned long size, 396 enum ttm_bo_type type, 397 struct ttm_placement *placement, 398 uint32_t page_alignment, 399 struct ttm_operation_ctx *ctx, 400 size_t acc_size, 401 struct sg_table *sg, 402 struct dma_resv *resv, 403 void (*destroy) (struct ttm_buffer_object *)); 404 405/** 406 * ttm_bo_init 407 * 408 * @bdev: Pointer to a ttm_bo_device struct. 409 * @bo: Pointer to a ttm_buffer_object to be initialized. 410 * @size: Requested size of buffer object. 411 * @type: Requested type of buffer object. 412 * @flags: Initial placement flags. 413 * @page_alignment: Data alignment in pages. 414 * @interruptible: If needing to sleep to wait for GPU resources, 415 * sleep interruptible. 416 * pinned in physical memory. If this behaviour is not desired, this member 417 * holds a pointer to a persistent shmem object. Typically, this would 418 * point to the shmem object backing a GEM object if TTM is used to back a 419 * GEM user interface. 420 * @acc_size: Accounted size for this object. 421 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one. 422 * @destroy: Destroy function. Use NULL for kfree(). 423 * 424 * This function initializes a pre-allocated struct ttm_buffer_object. 425 * As this object may be part of a larger structure, this function, 426 * together with the @destroy function, 427 * enables driver-specific objects derived from a ttm_buffer_object. 428 * 429 * On successful return, the caller owns an object kref to @bo. The kref and 430 * list_kref are usually set to 1, but note that in some situations, other 431 * tasks may already be holding references to @bo as well. 432 * 433 * If a failure occurs, the function will call the @destroy function, or 434 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is 435 * illegal and will likely cause memory corruption. 436 * 437 * Returns 438 * -ENOMEM: Out of memory. 439 * -EINVAL: Invalid placement flags. 440 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources. 441 */ 442int ttm_bo_init(struct ttm_bo_device *bdev, struct ttm_buffer_object *bo, 443 unsigned long size, enum ttm_bo_type type, 444 struct ttm_placement *placement, 445 uint32_t page_alignment, bool interrubtible, size_t acc_size, 446 struct sg_table *sg, struct dma_resv *resv, 447 void (*destroy) (struct ttm_buffer_object *)); 448 449/** 450 * ttm_bo_create 451 * 452 * @bdev: Pointer to a ttm_bo_device struct. 453 * @size: Requested size of buffer object. 454 * @type: Requested type of buffer object. 455 * @placement: Initial placement. 456 * @page_alignment: Data alignment in pages. 457 * @interruptible: If needing to sleep while waiting for GPU resources, 458 * sleep interruptible. 459 * @p_bo: On successful completion *p_bo points to the created object. 460 * 461 * This function allocates a ttm_buffer_object, and then calls ttm_bo_init 462 * on that object. The destroy function is set to kfree(). 463 * Returns 464 * -ENOMEM: Out of memory. 465 * -EINVAL: Invalid placement flags. 466 * -ERESTARTSYS: Interrupted by signal while waiting for resources. 467 */ 468int ttm_bo_create(struct ttm_bo_device *bdev, unsigned long size, 469 enum ttm_bo_type type, struct ttm_placement *placement, 470 uint32_t page_alignment, bool interruptible, 471 struct ttm_buffer_object **p_bo); 472 473/** 474 * ttm_bo_evict_mm 475 * 476 * @bdev: Pointer to a ttm_bo_device struct. 477 * @mem_type: The memory type. 478 * 479 * Evicts all buffers on the lru list of the memory type. 480 * This is normally part of a VT switch or an 481 * out-of-memory-space-due-to-fragmentation handler. 482 * The caller must make sure that there are no other processes 483 * currently validating buffers, and can do that by taking the 484 * struct ttm_bo_device::ttm_lock in write mode. 485 * 486 * Returns: 487 * -EINVAL: Invalid or uninitialized memory type. 488 * -ERESTARTSYS: The call was interrupted by a signal while waiting to 489 * evict a buffer. 490 */ 491int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type); 492 493/** 494 * ttm_kmap_obj_virtual 495 * 496 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap. 497 * @is_iomem: Pointer to an integer that on return indicates 1 if the 498 * virtual map is io memory, 0 if normal memory. 499 * 500 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap. 501 * If *is_iomem is 1 on return, the virtual address points to an io memory area, 502 * that should strictly be accessed by the iowriteXX() and similar functions. 503 */ 504static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map, 505 bool *is_iomem) 506{ 507 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK); 508 return map->virtual; 509} 510 511/** 512 * ttm_bo_kmap 513 * 514 * @bo: The buffer object. 515 * @start_page: The first page to map. 516 * @num_pages: Number of pages to map. 517 * @map: pointer to a struct ttm_bo_kmap_obj representing the map. 518 * 519 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the 520 * data in the buffer object. The ttm_kmap_obj_virtual function can then be 521 * used to obtain a virtual address to the data. 522 * 523 * Returns 524 * -ENOMEM: Out of memory. 525 * -EINVAL: Invalid range. 526 */ 527int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page, 528 unsigned long num_pages, struct ttm_bo_kmap_obj *map); 529 530/** 531 * ttm_bo_kunmap 532 * 533 * @map: Object describing the map to unmap. 534 * 535 * Unmaps a kernel map set up by ttm_bo_kmap. 536 */ 537void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map); 538 539/** 540 * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object. 541 * 542 * @vma: vma as input from the fbdev mmap method. 543 * @bo: The bo backing the address space. 544 * 545 * Maps a buffer object. 546 */ 547int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo); 548 549/** 550 * ttm_bo_mmap - mmap out of the ttm device address space. 551 * 552 * @filp: filp as input from the mmap method. 553 * @vma: vma as input from the mmap method. 554 * @bdev: Pointer to the ttm_bo_device with the address space manager. 555 * 556 * This function is intended to be called by the device mmap method. 557 * if the device address space is to be backed by the bo manager. 558 */ 559int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma, 560 struct ttm_bo_device *bdev); 561 562/** 563 * ttm_bo_io 564 * 565 * @bdev: Pointer to the struct ttm_bo_device. 566 * @filp: Pointer to the struct file attempting to read / write. 567 * @wbuf: User-space pointer to address of buffer to write. NULL on read. 568 * @rbuf: User-space pointer to address of buffer to read into. 569 * Null on write. 570 * @count: Number of bytes to read / write. 571 * @f_pos: Pointer to current file position. 572 * @write: 1 for read, 0 for write. 573 * 574 * This function implements read / write into ttm buffer objects, and is 575 * intended to 576 * be called from the fops::read and fops::write method. 577 * Returns: 578 * See man (2) write, man(2) read. In particular, 579 * the function may return -ERESTARTSYS if 580 * interrupted by a signal. 581 */ 582ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp, 583 const char __user *wbuf, char __user *rbuf, 584 size_t count, loff_t *f_pos, bool write); 585 586int ttm_bo_swapout(struct ttm_bo_global *glob, 587 struct ttm_operation_ctx *ctx); 588void ttm_bo_swapout_all(void); 589 590/** 591 * ttm_bo_uses_embedded_gem_object - check if the given bo uses the 592 * embedded drm_gem_object. 593 * 594 * Most ttm drivers are using gem too, so the embedded 595 * ttm_buffer_object.base will be initialized by the driver (before 596 * calling ttm_bo_init). It is also possible to use ttm without gem 597 * though (vmwgfx does that). 598 * 599 * This helper will figure whenever a given ttm bo is a gem object too 600 * or not. 601 * 602 * @bo: The bo to check. 603 */ 604static inline bool ttm_bo_uses_embedded_gem_object(struct ttm_buffer_object *bo) 605{ 606 return bo->base.dev != NULL; 607} 608 609int ttm_mem_evict_first(struct ttm_bo_device *bdev, 610 struct ttm_resource_manager *man, 611 const struct ttm_place *place, 612 struct ttm_operation_ctx *ctx, 613 struct ww_acquire_ctx *ticket); 614 615/* Default number of pre-faulted pages in the TTM fault handler */ 616#define TTM_BO_VM_NUM_PREFAULT 16 617 618vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo, 619 struct vm_fault *vmf); 620 621vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, 622 pgprot_t prot, 623 pgoff_t num_prefault, 624 pgoff_t fault_page_size); 625 626vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf); 627 628void ttm_bo_vm_open(struct vm_area_struct *vma); 629 630void ttm_bo_vm_close(struct vm_area_struct *vma); 631 632int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr, 633 void *buf, int len, int write); 634 635#endif 636