1*592ffb21SWarner Losh /************************************************************************** 2*592ffb21SWarner Losh * 3*592ffb21SWarner Losh * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 4*592ffb21SWarner Losh * All Rights Reserved. 5*592ffb21SWarner Losh * 6*592ffb21SWarner Losh * Permission is hereby granted, free of charge, to any person obtaining a 7*592ffb21SWarner Losh * copy of this software and associated documentation files (the 8*592ffb21SWarner Losh * "Software"), to deal in the Software without restriction, including 9*592ffb21SWarner Losh * without limitation the rights to use, copy, modify, merge, publish, 10*592ffb21SWarner Losh * distribute, sub license, and/or sell copies of the Software, and to 11*592ffb21SWarner Losh * permit persons to whom the Software is furnished to do so, subject to 12*592ffb21SWarner Losh * the following conditions: 13*592ffb21SWarner Losh * 14*592ffb21SWarner Losh * The above copyright notice and this permission notice (including the 15*592ffb21SWarner Losh * next paragraph) shall be included in all copies or substantial portions 16*592ffb21SWarner Losh * of the Software. 17*592ffb21SWarner Losh * 18*592ffb21SWarner Losh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19*592ffb21SWarner Losh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20*592ffb21SWarner Losh * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21*592ffb21SWarner Losh * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22*592ffb21SWarner Losh * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23*592ffb21SWarner Losh * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24*592ffb21SWarner Losh * USE OR OTHER DEALINGS IN THE SOFTWARE. 25*592ffb21SWarner Losh * 26*592ffb21SWarner Losh **************************************************************************/ 27*592ffb21SWarner Losh /* 28*592ffb21SWarner Losh * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 29*592ffb21SWarner Losh */ 30*592ffb21SWarner Losh /* $FreeBSD$ */ 31*592ffb21SWarner Losh 32*592ffb21SWarner Losh #ifndef _TTM_BO_API_H_ 33*592ffb21SWarner Losh #define _TTM_BO_API_H_ 34*592ffb21SWarner Losh 35*592ffb21SWarner Losh #include <dev/drm2/drmP.h> 36*592ffb21SWarner Losh 37*592ffb21SWarner Losh struct ttm_bo_device; 38*592ffb21SWarner Losh 39*592ffb21SWarner Losh struct drm_mm_node; 40*592ffb21SWarner Losh 41*592ffb21SWarner Losh 42*592ffb21SWarner Losh /** 43*592ffb21SWarner Losh * struct ttm_placement 44*592ffb21SWarner Losh * 45*592ffb21SWarner Losh * @fpfn: first valid page frame number to put the object 46*592ffb21SWarner Losh * @lpfn: last valid page frame number to put the object 47*592ffb21SWarner Losh * @num_placement: number of preferred placements 48*592ffb21SWarner Losh * @placement: preferred placements 49*592ffb21SWarner Losh * @num_busy_placement: number of preferred placements when need to evict buffer 50*592ffb21SWarner Losh * @busy_placement: preferred placements when need to evict buffer 51*592ffb21SWarner Losh * 52*592ffb21SWarner Losh * Structure indicating the placement you request for an object. 53*592ffb21SWarner Losh */ 54*592ffb21SWarner Losh struct ttm_placement { 55*592ffb21SWarner Losh unsigned fpfn; 56*592ffb21SWarner Losh unsigned lpfn; 57*592ffb21SWarner Losh unsigned num_placement; 58*592ffb21SWarner Losh const uint32_t *placement; 59*592ffb21SWarner Losh unsigned num_busy_placement; 60*592ffb21SWarner Losh const uint32_t *busy_placement; 61*592ffb21SWarner Losh }; 62*592ffb21SWarner Losh 63*592ffb21SWarner Losh /** 64*592ffb21SWarner Losh * struct ttm_bus_placement 65*592ffb21SWarner Losh * 66*592ffb21SWarner Losh * @addr: mapped virtual address 67*592ffb21SWarner Losh * @base: bus base address 68*592ffb21SWarner Losh * @is_iomem: is this io memory ? 69*592ffb21SWarner Losh * @size: size in byte 70*592ffb21SWarner Losh * @offset: offset from the base address 71*592ffb21SWarner Losh * @io_reserved_vm: The VM system has a refcount in @io_reserved_count 72*592ffb21SWarner Losh * @io_reserved_count: Refcounting the numbers of callers to ttm_mem_io_reserve 73*592ffb21SWarner Losh * 74*592ffb21SWarner Losh * Structure indicating the bus placement of an object. 75*592ffb21SWarner Losh */ 76*592ffb21SWarner Losh struct ttm_bus_placement { 77*592ffb21SWarner Losh void *addr; 78*592ffb21SWarner Losh unsigned long base; 79*592ffb21SWarner Losh unsigned long size; 80*592ffb21SWarner Losh unsigned long offset; 81*592ffb21SWarner Losh bool is_iomem; 82*592ffb21SWarner Losh bool io_reserved_vm; 83*592ffb21SWarner Losh uint64_t io_reserved_count; 84*592ffb21SWarner Losh }; 85*592ffb21SWarner Losh 86*592ffb21SWarner Losh 87*592ffb21SWarner Losh /** 88*592ffb21SWarner Losh * struct ttm_mem_reg 89*592ffb21SWarner Losh * 90*592ffb21SWarner Losh * @mm_node: Memory manager node. 91*592ffb21SWarner Losh * @size: Requested size of memory region. 92*592ffb21SWarner Losh * @num_pages: Actual size of memory region in pages. 93*592ffb21SWarner Losh * @page_alignment: Page alignment. 94*592ffb21SWarner Losh * @placement: Placement flags. 95*592ffb21SWarner Losh * @bus: Placement on io bus accessible to the CPU 96*592ffb21SWarner Losh * 97*592ffb21SWarner Losh * Structure indicating the placement and space resources used by a 98*592ffb21SWarner Losh * buffer object. 99*592ffb21SWarner Losh */ 100*592ffb21SWarner Losh 101*592ffb21SWarner Losh struct ttm_mem_reg { 102*592ffb21SWarner Losh void *mm_node; 103*592ffb21SWarner Losh unsigned long start; 104*592ffb21SWarner Losh unsigned long size; 105*592ffb21SWarner Losh unsigned long num_pages; 106*592ffb21SWarner Losh uint32_t page_alignment; 107*592ffb21SWarner Losh uint32_t mem_type; 108*592ffb21SWarner Losh uint32_t placement; 109*592ffb21SWarner Losh struct ttm_bus_placement bus; 110*592ffb21SWarner Losh }; 111*592ffb21SWarner Losh 112*592ffb21SWarner Losh /** 113*592ffb21SWarner Losh * enum ttm_bo_type 114*592ffb21SWarner Losh * 115*592ffb21SWarner Losh * @ttm_bo_type_device: These are 'normal' buffers that can 116*592ffb21SWarner Losh * be mmapped by user space. Each of these bos occupy a slot in the 117*592ffb21SWarner Losh * device address space, that can be used for normal vm operations. 118*592ffb21SWarner Losh * 119*592ffb21SWarner Losh * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers, 120*592ffb21SWarner Losh * but they cannot be accessed from user-space. For kernel-only use. 121*592ffb21SWarner Losh * 122*592ffb21SWarner Losh * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another 123*592ffb21SWarner Losh * driver. 124*592ffb21SWarner Losh */ 125*592ffb21SWarner Losh 126*592ffb21SWarner Losh enum ttm_bo_type { 127*592ffb21SWarner Losh ttm_bo_type_device, 128*592ffb21SWarner Losh ttm_bo_type_kernel, 129*592ffb21SWarner Losh ttm_bo_type_sg 130*592ffb21SWarner Losh }; 131*592ffb21SWarner Losh 132*592ffb21SWarner Losh struct ttm_tt; 133*592ffb21SWarner Losh 134*592ffb21SWarner Losh /** 135*592ffb21SWarner Losh * struct ttm_buffer_object 136*592ffb21SWarner Losh * 137*592ffb21SWarner Losh * @bdev: Pointer to the buffer object device structure. 138*592ffb21SWarner Losh * @type: The bo type. 139*592ffb21SWarner Losh * @destroy: Destruction function. If NULL, kfree is used. 140*592ffb21SWarner Losh * @num_pages: Actual number of pages. 141*592ffb21SWarner Losh * @addr_space_offset: Address space offset. 142*592ffb21SWarner Losh * @acc_size: Accounted size for this object. 143*592ffb21SWarner Losh * @kref: Reference count of this buffer object. When this refcount reaches 144*592ffb21SWarner Losh * zero, the object is put on the delayed delete list. 145*592ffb21SWarner Losh * @list_kref: List reference count of this buffer object. This member is 146*592ffb21SWarner Losh * used to avoid destruction while the buffer object is still on a list. 147*592ffb21SWarner Losh * Lru lists may keep one refcount, the delayed delete list, and kref != 0 148*592ffb21SWarner Losh * keeps one refcount. When this refcount reaches zero, 149*592ffb21SWarner Losh * the object is destroyed. 150*592ffb21SWarner Losh * @event_queue: Queue for processes waiting on buffer object status change. 151*592ffb21SWarner Losh * @mem: structure describing current placement. 152*592ffb21SWarner Losh * @persistent_swap_storage: Usually the swap storage is deleted for buffers 153*592ffb21SWarner Losh * pinned in physical memory. If this behaviour is not desired, this member 154*592ffb21SWarner Losh * holds a pointer to a persistent shmem object. 155*592ffb21SWarner Losh * @ttm: TTM structure holding system pages. 156*592ffb21SWarner Losh * @evicted: Whether the object was evicted without user-space knowing. 157*592ffb21SWarner Losh * @cpu_writes: For synchronization. Number of cpu writers. 158*592ffb21SWarner Losh * @lru: List head for the lru list. 159*592ffb21SWarner Losh * @ddestroy: List head for the delayed destroy list. 160*592ffb21SWarner Losh * @swap: List head for swap LRU list. 161*592ffb21SWarner Losh * @val_seq: Sequence of the validation holding the @reserved lock. 162*592ffb21SWarner Losh * Used to avoid starvation when many processes compete to validate the 163*592ffb21SWarner Losh * buffer. This member is protected by the bo_device::lru_lock. 164*592ffb21SWarner Losh * @seq_valid: The value of @val_seq is valid. This value is protected by 165*592ffb21SWarner Losh * the bo_device::lru_lock. 166*592ffb21SWarner Losh * @reserved: Deadlock-free lock used for synchronization state transitions. 167*592ffb21SWarner Losh * @sync_obj: Pointer to a synchronization object. 168*592ffb21SWarner Losh * @priv_flags: Flags describing buffer object internal state. 169*592ffb21SWarner Losh * @vm_rb: Rb node for the vm rb tree. 170*592ffb21SWarner Losh * @vm_node: Address space manager node. 171*592ffb21SWarner Losh * @offset: The current GPU offset, which can have different meanings 172*592ffb21SWarner Losh * depending on the memory type. For SYSTEM type memory, it should be 0. 173*592ffb21SWarner Losh * @cur_placement: Hint of current placement. 174*592ffb21SWarner Losh * 175*592ffb21SWarner Losh * Base class for TTM buffer object, that deals with data placement and CPU 176*592ffb21SWarner Losh * mappings. GPU mappings are really up to the driver, but for simpler GPUs 177*592ffb21SWarner Losh * the driver can usually use the placement offset @offset directly as the 178*592ffb21SWarner Losh * GPU virtual address. For drivers implementing multiple 179*592ffb21SWarner Losh * GPU memory manager contexts, the driver should manage the address space 180*592ffb21SWarner Losh * in these contexts separately and use these objects to get the correct 181*592ffb21SWarner Losh * placement and caching for these GPU maps. This makes it possible to use 182*592ffb21SWarner Losh * these objects for even quite elaborate memory management schemes. 183*592ffb21SWarner Losh * The destroy member, the API visibility of this object makes it possible 184*592ffb21SWarner Losh * to derive driver specific types. 185*592ffb21SWarner Losh */ 186*592ffb21SWarner Losh 187*592ffb21SWarner Losh struct ttm_buffer_object { 188*592ffb21SWarner Losh /** 189*592ffb21SWarner Losh * Members constant at init. 190*592ffb21SWarner Losh */ 191*592ffb21SWarner Losh 192*592ffb21SWarner Losh struct ttm_bo_global *glob; 193*592ffb21SWarner Losh struct ttm_bo_device *bdev; 194*592ffb21SWarner Losh enum ttm_bo_type type; 195*592ffb21SWarner Losh void (*destroy) (struct ttm_buffer_object *); 196*592ffb21SWarner Losh unsigned long num_pages; 197*592ffb21SWarner Losh uint64_t addr_space_offset; 198*592ffb21SWarner Losh size_t acc_size; 199*592ffb21SWarner Losh 200*592ffb21SWarner Losh /** 201*592ffb21SWarner Losh * Members not needing protection. 202*592ffb21SWarner Losh */ 203*592ffb21SWarner Losh 204*592ffb21SWarner Losh u_int kref; 205*592ffb21SWarner Losh u_int list_kref; 206*592ffb21SWarner Losh /* wait_queue_head_t event_queue; */ 207*592ffb21SWarner Losh 208*592ffb21SWarner Losh /** 209*592ffb21SWarner Losh * Members protected by the bo::reserved lock. 210*592ffb21SWarner Losh */ 211*592ffb21SWarner Losh 212*592ffb21SWarner Losh struct ttm_mem_reg mem; 213*592ffb21SWarner Losh struct vm_object *persistent_swap_storage; 214*592ffb21SWarner Losh struct ttm_tt *ttm; 215*592ffb21SWarner Losh bool evicted; 216*592ffb21SWarner Losh 217*592ffb21SWarner Losh /** 218*592ffb21SWarner Losh * Members protected by the bo::reserved lock only when written to. 219*592ffb21SWarner Losh */ 220*592ffb21SWarner Losh 221*592ffb21SWarner Losh atomic_t cpu_writers; 222*592ffb21SWarner Losh 223*592ffb21SWarner Losh /** 224*592ffb21SWarner Losh * Members protected by the bdev::lru_lock. 225*592ffb21SWarner Losh */ 226*592ffb21SWarner Losh 227*592ffb21SWarner Losh struct list_head lru; 228*592ffb21SWarner Losh struct list_head ddestroy; 229*592ffb21SWarner Losh struct list_head swap; 230*592ffb21SWarner Losh struct list_head io_reserve_lru; 231*592ffb21SWarner Losh uint32_t val_seq; 232*592ffb21SWarner Losh bool seq_valid; 233*592ffb21SWarner Losh 234*592ffb21SWarner Losh /** 235*592ffb21SWarner Losh * Members protected by the bdev::lru_lock 236*592ffb21SWarner Losh * only when written to. 237*592ffb21SWarner Losh */ 238*592ffb21SWarner Losh 239*592ffb21SWarner Losh atomic_t reserved; 240*592ffb21SWarner Losh 241*592ffb21SWarner Losh /** 242*592ffb21SWarner Losh * Members protected by struct buffer_object_device::fence_lock 243*592ffb21SWarner Losh * In addition, setting sync_obj to anything else 244*592ffb21SWarner Losh * than NULL requires bo::reserved to be held. This allows for 245*592ffb21SWarner Losh * checking NULL while reserved but not holding the mentioned lock. 246*592ffb21SWarner Losh */ 247*592ffb21SWarner Losh 248*592ffb21SWarner Losh void *sync_obj; 249*592ffb21SWarner Losh unsigned long priv_flags; 250*592ffb21SWarner Losh 251*592ffb21SWarner Losh /** 252*592ffb21SWarner Losh * Members protected by the bdev::vm_lock 253*592ffb21SWarner Losh */ 254*592ffb21SWarner Losh 255*592ffb21SWarner Losh RB_ENTRY(ttm_buffer_object) vm_rb; 256*592ffb21SWarner Losh struct drm_mm_node *vm_node; 257*592ffb21SWarner Losh 258*592ffb21SWarner Losh 259*592ffb21SWarner Losh /** 260*592ffb21SWarner Losh * Special members that are protected by the reserve lock 261*592ffb21SWarner Losh * and the bo::lock when written to. Can be read with 262*592ffb21SWarner Losh * either of these locks held. 263*592ffb21SWarner Losh */ 264*592ffb21SWarner Losh 265*592ffb21SWarner Losh unsigned long offset; 266*592ffb21SWarner Losh uint32_t cur_placement; 267*592ffb21SWarner Losh 268*592ffb21SWarner Losh struct sg_table *sg; 269*592ffb21SWarner Losh }; 270*592ffb21SWarner Losh 271*592ffb21SWarner Losh /** 272*592ffb21SWarner Losh * struct ttm_bo_kmap_obj 273*592ffb21SWarner Losh * 274*592ffb21SWarner Losh * @virtual: The current kernel virtual address. 275*592ffb21SWarner Losh * @page: The page when kmap'ing a single page. 276*592ffb21SWarner Losh * @bo_kmap_type: Type of bo_kmap. 277*592ffb21SWarner Losh * 278*592ffb21SWarner Losh * Object describing a kernel mapping. Since a TTM bo may be located 279*592ffb21SWarner Losh * in various memory types with various caching policies, the 280*592ffb21SWarner Losh * mapping can either be an ioremap, a vmap, a kmap or part of a 281*592ffb21SWarner Losh * premapped region. 282*592ffb21SWarner Losh */ 283*592ffb21SWarner Losh 284*592ffb21SWarner Losh #define TTM_BO_MAP_IOMEM_MASK 0x80 285*592ffb21SWarner Losh struct ttm_bo_kmap_obj { 286*592ffb21SWarner Losh void *virtual; 287*592ffb21SWarner Losh struct vm_page *page; 288*592ffb21SWarner Losh struct sf_buf *sf; 289*592ffb21SWarner Losh int num_pages; 290*592ffb21SWarner Losh unsigned long size; 291*592ffb21SWarner Losh enum { 292*592ffb21SWarner Losh ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK, 293*592ffb21SWarner Losh ttm_bo_map_vmap = 2, 294*592ffb21SWarner Losh ttm_bo_map_kmap = 3, 295*592ffb21SWarner Losh ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK, 296*592ffb21SWarner Losh } bo_kmap_type; 297*592ffb21SWarner Losh struct ttm_buffer_object *bo; 298*592ffb21SWarner Losh }; 299*592ffb21SWarner Losh 300*592ffb21SWarner Losh /** 301*592ffb21SWarner Losh * ttm_bo_reference - reference a struct ttm_buffer_object 302*592ffb21SWarner Losh * 303*592ffb21SWarner Losh * @bo: The buffer object. 304*592ffb21SWarner Losh * 305*592ffb21SWarner Losh * Returns a refcounted pointer to a buffer object. 306*592ffb21SWarner Losh */ 307*592ffb21SWarner Losh 308*592ffb21SWarner Losh static inline struct ttm_buffer_object * 309*592ffb21SWarner Losh ttm_bo_reference(struct ttm_buffer_object *bo) 310*592ffb21SWarner Losh { 311*592ffb21SWarner Losh refcount_acquire(&bo->kref); 312*592ffb21SWarner Losh return bo; 313*592ffb21SWarner Losh } 314*592ffb21SWarner Losh 315*592ffb21SWarner Losh /** 316*592ffb21SWarner Losh * ttm_bo_wait - wait for buffer idle. 317*592ffb21SWarner Losh * 318*592ffb21SWarner Losh * @bo: The buffer object. 319*592ffb21SWarner Losh * @interruptible: Use interruptible wait. 320*592ffb21SWarner Losh * @no_wait: Return immediately if buffer is busy. 321*592ffb21SWarner Losh * 322*592ffb21SWarner Losh * This function must be called with the bo::mutex held, and makes 323*592ffb21SWarner Losh * sure any previous rendering to the buffer is completed. 324*592ffb21SWarner Losh * Note: It might be necessary to block validations before the 325*592ffb21SWarner Losh * wait by reserving the buffer. 326*592ffb21SWarner Losh * Returns -EBUSY if no_wait is true and the buffer is busy. 327*592ffb21SWarner Losh * Returns -ERESTARTSYS if interrupted by a signal. 328*592ffb21SWarner Losh */ 329*592ffb21SWarner Losh extern int ttm_bo_wait(struct ttm_buffer_object *bo, bool lazy, 330*592ffb21SWarner Losh bool interruptible, bool no_wait); 331*592ffb21SWarner Losh /** 332*592ffb21SWarner Losh * ttm_bo_validate 333*592ffb21SWarner Losh * 334*592ffb21SWarner Losh * @bo: The buffer object. 335*592ffb21SWarner Losh * @placement: Proposed placement for the buffer object. 336*592ffb21SWarner Losh * @interruptible: Sleep interruptible if sleeping. 337*592ffb21SWarner Losh * @no_wait_gpu: Return immediately if the GPU is busy. 338*592ffb21SWarner Losh * 339*592ffb21SWarner Losh * Changes placement and caching policy of the buffer object 340*592ffb21SWarner Losh * according proposed placement. 341*592ffb21SWarner Losh * Returns 342*592ffb21SWarner Losh * -EINVAL on invalid proposed placement. 343*592ffb21SWarner Losh * -ENOMEM on out-of-memory condition. 344*592ffb21SWarner Losh * -EBUSY if no_wait is true and buffer busy. 345*592ffb21SWarner Losh * -ERESTARTSYS if interrupted by a signal. 346*592ffb21SWarner Losh */ 347*592ffb21SWarner Losh extern int ttm_bo_validate(struct ttm_buffer_object *bo, 348*592ffb21SWarner Losh struct ttm_placement *placement, 349*592ffb21SWarner Losh bool interruptible, 350*592ffb21SWarner Losh bool no_wait_gpu); 351*592ffb21SWarner Losh 352*592ffb21SWarner Losh /** 353*592ffb21SWarner Losh * ttm_bo_unref 354*592ffb21SWarner Losh * 355*592ffb21SWarner Losh * @bo: The buffer object. 356*592ffb21SWarner Losh * 357*592ffb21SWarner Losh * Unreference and clear a pointer to a buffer object. 358*592ffb21SWarner Losh */ 359*592ffb21SWarner Losh extern void ttm_bo_unref(struct ttm_buffer_object **bo); 360*592ffb21SWarner Losh 361*592ffb21SWarner Losh 362*592ffb21SWarner Losh /** 363*592ffb21SWarner Losh * ttm_bo_list_ref_sub 364*592ffb21SWarner Losh * 365*592ffb21SWarner Losh * @bo: The buffer object. 366*592ffb21SWarner Losh * @count: The number of references with which to decrease @bo::list_kref; 367*592ffb21SWarner Losh * @never_free: The refcount should not reach zero with this operation. 368*592ffb21SWarner Losh * 369*592ffb21SWarner Losh * Release @count lru list references to this buffer object. 370*592ffb21SWarner Losh */ 371*592ffb21SWarner Losh extern void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count, 372*592ffb21SWarner Losh bool never_free); 373*592ffb21SWarner Losh 374*592ffb21SWarner Losh /** 375*592ffb21SWarner Losh * ttm_bo_add_to_lru 376*592ffb21SWarner Losh * 377*592ffb21SWarner Losh * @bo: The buffer object. 378*592ffb21SWarner Losh * 379*592ffb21SWarner Losh * Add this bo to the relevant mem type lru and, if it's backed by 380*592ffb21SWarner Losh * system pages (ttms) to the swap list. 381*592ffb21SWarner Losh * This function must be called with struct ttm_bo_global::lru_lock held, and 382*592ffb21SWarner Losh * is typically called immediately prior to unreserving a bo. 383*592ffb21SWarner Losh */ 384*592ffb21SWarner Losh extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo); 385*592ffb21SWarner Losh 386*592ffb21SWarner Losh /** 387*592ffb21SWarner Losh * ttm_bo_del_from_lru 388*592ffb21SWarner Losh * 389*592ffb21SWarner Losh * @bo: The buffer object. 390*592ffb21SWarner Losh * 391*592ffb21SWarner Losh * Remove this bo from all lru lists used to lookup and reserve an object. 392*592ffb21SWarner Losh * This function must be called with struct ttm_bo_global::lru_lock held, 393*592ffb21SWarner Losh * and is usually called just immediately after the bo has been reserved to 394*592ffb21SWarner Losh * avoid recursive reservation from lru lists. 395*592ffb21SWarner Losh */ 396*592ffb21SWarner Losh extern int ttm_bo_del_from_lru(struct ttm_buffer_object *bo); 397*592ffb21SWarner Losh 398*592ffb21SWarner Losh 399*592ffb21SWarner Losh /** 400*592ffb21SWarner Losh * ttm_bo_lock_delayed_workqueue 401*592ffb21SWarner Losh * 402*592ffb21SWarner Losh * Prevent the delayed workqueue from running. 403*592ffb21SWarner Losh * Returns 404*592ffb21SWarner Losh * True if the workqueue was queued at the time 405*592ffb21SWarner Losh */ 406*592ffb21SWarner Losh extern int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev); 407*592ffb21SWarner Losh 408*592ffb21SWarner Losh /** 409*592ffb21SWarner Losh * ttm_bo_unlock_delayed_workqueue 410*592ffb21SWarner Losh * 411*592ffb21SWarner Losh * Allows the delayed workqueue to run. 412*592ffb21SWarner Losh */ 413*592ffb21SWarner Losh extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, 414*592ffb21SWarner Losh int resched); 415*592ffb21SWarner Losh 416*592ffb21SWarner Losh /** 417*592ffb21SWarner Losh * ttm_bo_synccpu_write_grab 418*592ffb21SWarner Losh * 419*592ffb21SWarner Losh * @bo: The buffer object: 420*592ffb21SWarner Losh * @no_wait: Return immediately if buffer is busy. 421*592ffb21SWarner Losh * 422*592ffb21SWarner Losh * Synchronizes a buffer object for CPU RW access. This means 423*592ffb21SWarner Losh * command submission that affects the buffer will return -EBUSY 424*592ffb21SWarner Losh * until ttm_bo_synccpu_write_release is called. 425*592ffb21SWarner Losh * 426*592ffb21SWarner Losh * Returns 427*592ffb21SWarner Losh * -EBUSY if the buffer is busy and no_wait is true. 428*592ffb21SWarner Losh * -ERESTARTSYS if interrupted by a signal. 429*592ffb21SWarner Losh */ 430*592ffb21SWarner Losh extern int 431*592ffb21SWarner Losh ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait); 432*592ffb21SWarner Losh 433*592ffb21SWarner Losh /** 434*592ffb21SWarner Losh * ttm_bo_synccpu_write_release: 435*592ffb21SWarner Losh * 436*592ffb21SWarner Losh * @bo : The buffer object. 437*592ffb21SWarner Losh * 438*592ffb21SWarner Losh * Releases a synccpu lock. 439*592ffb21SWarner Losh */ 440*592ffb21SWarner Losh extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo); 441*592ffb21SWarner Losh 442*592ffb21SWarner Losh /** 443*592ffb21SWarner Losh * ttm_bo_acc_size 444*592ffb21SWarner Losh * 445*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct. 446*592ffb21SWarner Losh * @bo_size: size of the buffer object in byte. 447*592ffb21SWarner Losh * @struct_size: size of the structure holding buffer object datas 448*592ffb21SWarner Losh * 449*592ffb21SWarner Losh * Returns size to account for a buffer object 450*592ffb21SWarner Losh */ 451*592ffb21SWarner Losh size_t ttm_bo_acc_size(struct ttm_bo_device *bdev, 452*592ffb21SWarner Losh unsigned long bo_size, 453*592ffb21SWarner Losh unsigned struct_size); 454*592ffb21SWarner Losh size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, 455*592ffb21SWarner Losh unsigned long bo_size, 456*592ffb21SWarner Losh unsigned struct_size); 457*592ffb21SWarner Losh 458*592ffb21SWarner Losh /** 459*592ffb21SWarner Losh * ttm_bo_init 460*592ffb21SWarner Losh * 461*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct. 462*592ffb21SWarner Losh * @bo: Pointer to a ttm_buffer_object to be initialized. 463*592ffb21SWarner Losh * @size: Requested size of buffer object. 464*592ffb21SWarner Losh * @type: Requested type of buffer object. 465*592ffb21SWarner Losh * @flags: Initial placement flags. 466*592ffb21SWarner Losh * @page_alignment: Data alignment in pages. 467*592ffb21SWarner Losh * @interruptible: If needing to sleep to wait for GPU resources, 468*592ffb21SWarner Losh * sleep interruptible. 469*592ffb21SWarner Losh * @persistent_swap_storage: Usually the swap storage is deleted for buffers 470*592ffb21SWarner Losh * pinned in physical memory. If this behaviour is not desired, this member 471*592ffb21SWarner Losh * holds a pointer to a persistent shmem object. Typically, this would 472*592ffb21SWarner Losh * point to the shmem object backing a GEM object if TTM is used to back a 473*592ffb21SWarner Losh * GEM user interface. 474*592ffb21SWarner Losh * @acc_size: Accounted size for this object. 475*592ffb21SWarner Losh * @destroy: Destroy function. Use NULL for kfree(). 476*592ffb21SWarner Losh * 477*592ffb21SWarner Losh * This function initializes a pre-allocated struct ttm_buffer_object. 478*592ffb21SWarner Losh * As this object may be part of a larger structure, this function, 479*592ffb21SWarner Losh * together with the @destroy function, 480*592ffb21SWarner Losh * enables driver-specific objects derived from a ttm_buffer_object. 481*592ffb21SWarner Losh * On successful return, the object kref and list_kref are set to 1. 482*592ffb21SWarner Losh * If a failure occurs, the function will call the @destroy function, or 483*592ffb21SWarner Losh * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is 484*592ffb21SWarner Losh * illegal and will likely cause memory corruption. 485*592ffb21SWarner Losh * 486*592ffb21SWarner Losh * Returns 487*592ffb21SWarner Losh * -ENOMEM: Out of memory. 488*592ffb21SWarner Losh * -EINVAL: Invalid placement flags. 489*592ffb21SWarner Losh * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources. 490*592ffb21SWarner Losh */ 491*592ffb21SWarner Losh 492*592ffb21SWarner Losh extern int ttm_bo_init(struct ttm_bo_device *bdev, 493*592ffb21SWarner Losh struct ttm_buffer_object *bo, 494*592ffb21SWarner Losh unsigned long size, 495*592ffb21SWarner Losh enum ttm_bo_type type, 496*592ffb21SWarner Losh struct ttm_placement *placement, 497*592ffb21SWarner Losh uint32_t page_alignment, 498*592ffb21SWarner Losh bool interrubtible, 499*592ffb21SWarner Losh struct vm_object *persistent_swap_storage, 500*592ffb21SWarner Losh size_t acc_size, 501*592ffb21SWarner Losh struct sg_table *sg, 502*592ffb21SWarner Losh void (*destroy) (struct ttm_buffer_object *)); 503*592ffb21SWarner Losh 504*592ffb21SWarner Losh /** 505*592ffb21SWarner Losh * ttm_bo_synccpu_object_init 506*592ffb21SWarner Losh * 507*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct. 508*592ffb21SWarner Losh * @bo: Pointer to a ttm_buffer_object to be initialized. 509*592ffb21SWarner Losh * @size: Requested size of buffer object. 510*592ffb21SWarner Losh * @type: Requested type of buffer object. 511*592ffb21SWarner Losh * @flags: Initial placement flags. 512*592ffb21SWarner Losh * @page_alignment: Data alignment in pages. 513*592ffb21SWarner Losh * @interruptible: If needing to sleep while waiting for GPU resources, 514*592ffb21SWarner Losh * sleep interruptible. 515*592ffb21SWarner Losh * @persistent_swap_storage: Usually the swap storage is deleted for buffers 516*592ffb21SWarner Losh * pinned in physical memory. If this behaviour is not desired, this member 517*592ffb21SWarner Losh * holds a pointer to a persistent shmem object. Typically, this would 518*592ffb21SWarner Losh * point to the shmem object backing a GEM object if TTM is used to back a 519*592ffb21SWarner Losh * GEM user interface. 520*592ffb21SWarner Losh * @p_bo: On successful completion *p_bo points to the created object. 521*592ffb21SWarner Losh * 522*592ffb21SWarner Losh * This function allocates a ttm_buffer_object, and then calls ttm_bo_init 523*592ffb21SWarner Losh * on that object. The destroy function is set to kfree(). 524*592ffb21SWarner Losh * Returns 525*592ffb21SWarner Losh * -ENOMEM: Out of memory. 526*592ffb21SWarner Losh * -EINVAL: Invalid placement flags. 527*592ffb21SWarner Losh * -ERESTARTSYS: Interrupted by signal while waiting for resources. 528*592ffb21SWarner Losh */ 529*592ffb21SWarner Losh 530*592ffb21SWarner Losh extern int ttm_bo_create(struct ttm_bo_device *bdev, 531*592ffb21SWarner Losh unsigned long size, 532*592ffb21SWarner Losh enum ttm_bo_type type, 533*592ffb21SWarner Losh struct ttm_placement *placement, 534*592ffb21SWarner Losh uint32_t page_alignment, 535*592ffb21SWarner Losh bool interruptible, 536*592ffb21SWarner Losh struct vm_object *persistent_swap_storage, 537*592ffb21SWarner Losh struct ttm_buffer_object **p_bo); 538*592ffb21SWarner Losh 539*592ffb21SWarner Losh /** 540*592ffb21SWarner Losh * ttm_bo_check_placement 541*592ffb21SWarner Losh * 542*592ffb21SWarner Losh * @bo: the buffer object. 543*592ffb21SWarner Losh * @placement: placements 544*592ffb21SWarner Losh * 545*592ffb21SWarner Losh * Performs minimal validity checking on an intended change of 546*592ffb21SWarner Losh * placement flags. 547*592ffb21SWarner Losh * Returns 548*592ffb21SWarner Losh * -EINVAL: Intended change is invalid or not allowed. 549*592ffb21SWarner Losh */ 550*592ffb21SWarner Losh extern int ttm_bo_check_placement(struct ttm_buffer_object *bo, 551*592ffb21SWarner Losh struct ttm_placement *placement); 552*592ffb21SWarner Losh 553*592ffb21SWarner Losh /** 554*592ffb21SWarner Losh * ttm_bo_init_mm 555*592ffb21SWarner Losh * 556*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct. 557*592ffb21SWarner Losh * @mem_type: The memory type. 558*592ffb21SWarner Losh * @p_size: size managed area in pages. 559*592ffb21SWarner Losh * 560*592ffb21SWarner Losh * Initialize a manager for a given memory type. 561*592ffb21SWarner Losh * Note: if part of driver firstopen, it must be protected from a 562*592ffb21SWarner Losh * potentially racing lastclose. 563*592ffb21SWarner Losh * Returns: 564*592ffb21SWarner Losh * -EINVAL: invalid size or memory type. 565*592ffb21SWarner Losh * -ENOMEM: Not enough memory. 566*592ffb21SWarner Losh * May also return driver-specified errors. 567*592ffb21SWarner Losh */ 568*592ffb21SWarner Losh 569*592ffb21SWarner Losh extern int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, 570*592ffb21SWarner Losh unsigned long p_size); 571*592ffb21SWarner Losh /** 572*592ffb21SWarner Losh * ttm_bo_clean_mm 573*592ffb21SWarner Losh * 574*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct. 575*592ffb21SWarner Losh * @mem_type: The memory type. 576*592ffb21SWarner Losh * 577*592ffb21SWarner Losh * Take down a manager for a given memory type after first walking 578*592ffb21SWarner Losh * the LRU list to evict any buffers left alive. 579*592ffb21SWarner Losh * 580*592ffb21SWarner Losh * Normally, this function is part of lastclose() or unload(), and at that 581*592ffb21SWarner Losh * point there shouldn't be any buffers left created by user-space, since 582*592ffb21SWarner Losh * there should've been removed by the file descriptor release() method. 583*592ffb21SWarner Losh * However, before this function is run, make sure to signal all sync objects, 584*592ffb21SWarner Losh * and verify that the delayed delete queue is empty. The driver must also 585*592ffb21SWarner Losh * make sure that there are no NO_EVICT buffers present in this memory type 586*592ffb21SWarner Losh * when the call is made. 587*592ffb21SWarner Losh * 588*592ffb21SWarner Losh * If this function is part of a VT switch, the caller must make sure that 589*592ffb21SWarner Losh * there are no appications currently validating buffers before this 590*592ffb21SWarner Losh * function is called. The caller can do that by first taking the 591*592ffb21SWarner Losh * struct ttm_bo_device::ttm_lock in write mode. 592*592ffb21SWarner Losh * 593*592ffb21SWarner Losh * Returns: 594*592ffb21SWarner Losh * -EINVAL: invalid or uninitialized memory type. 595*592ffb21SWarner Losh * -EBUSY: There are still buffers left in this memory type. 596*592ffb21SWarner Losh */ 597*592ffb21SWarner Losh 598*592ffb21SWarner Losh extern int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type); 599*592ffb21SWarner Losh 600*592ffb21SWarner Losh /** 601*592ffb21SWarner Losh * ttm_bo_evict_mm 602*592ffb21SWarner Losh * 603*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct. 604*592ffb21SWarner Losh * @mem_type: The memory type. 605*592ffb21SWarner Losh * 606*592ffb21SWarner Losh * Evicts all buffers on the lru list of the memory type. 607*592ffb21SWarner Losh * This is normally part of a VT switch or an 608*592ffb21SWarner Losh * out-of-memory-space-due-to-fragmentation handler. 609*592ffb21SWarner Losh * The caller must make sure that there are no other processes 610*592ffb21SWarner Losh * currently validating buffers, and can do that by taking the 611*592ffb21SWarner Losh * struct ttm_bo_device::ttm_lock in write mode. 612*592ffb21SWarner Losh * 613*592ffb21SWarner Losh * Returns: 614*592ffb21SWarner Losh * -EINVAL: Invalid or uninitialized memory type. 615*592ffb21SWarner Losh * -ERESTARTSYS: The call was interrupted by a signal while waiting to 616*592ffb21SWarner Losh * evict a buffer. 617*592ffb21SWarner Losh */ 618*592ffb21SWarner Losh 619*592ffb21SWarner Losh extern int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type); 620*592ffb21SWarner Losh 621*592ffb21SWarner Losh /** 622*592ffb21SWarner Losh * ttm_kmap_obj_virtual 623*592ffb21SWarner Losh * 624*592ffb21SWarner Losh * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap. 625*592ffb21SWarner Losh * @is_iomem: Pointer to an integer that on return indicates 1 if the 626*592ffb21SWarner Losh * virtual map is io memory, 0 if normal memory. 627*592ffb21SWarner Losh * 628*592ffb21SWarner Losh * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap. 629*592ffb21SWarner Losh * If *is_iomem is 1 on return, the virtual address points to an io memory area, 630*592ffb21SWarner Losh * that should strictly be accessed by the iowriteXX() and similar functions. 631*592ffb21SWarner Losh */ 632*592ffb21SWarner Losh 633*592ffb21SWarner Losh static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map, 634*592ffb21SWarner Losh bool *is_iomem) 635*592ffb21SWarner Losh { 636*592ffb21SWarner Losh *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK); 637*592ffb21SWarner Losh return map->virtual; 638*592ffb21SWarner Losh } 639*592ffb21SWarner Losh 640*592ffb21SWarner Losh /** 641*592ffb21SWarner Losh * ttm_bo_kmap 642*592ffb21SWarner Losh * 643*592ffb21SWarner Losh * @bo: The buffer object. 644*592ffb21SWarner Losh * @start_page: The first page to map. 645*592ffb21SWarner Losh * @num_pages: Number of pages to map. 646*592ffb21SWarner Losh * @map: pointer to a struct ttm_bo_kmap_obj representing the map. 647*592ffb21SWarner Losh * 648*592ffb21SWarner Losh * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the 649*592ffb21SWarner Losh * data in the buffer object. The ttm_kmap_obj_virtual function can then be 650*592ffb21SWarner Losh * used to obtain a virtual address to the data. 651*592ffb21SWarner Losh * 652*592ffb21SWarner Losh * Returns 653*592ffb21SWarner Losh * -ENOMEM: Out of memory. 654*592ffb21SWarner Losh * -EINVAL: Invalid range. 655*592ffb21SWarner Losh */ 656*592ffb21SWarner Losh 657*592ffb21SWarner Losh extern int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page, 658*592ffb21SWarner Losh unsigned long num_pages, struct ttm_bo_kmap_obj *map); 659*592ffb21SWarner Losh 660*592ffb21SWarner Losh /** 661*592ffb21SWarner Losh * ttm_bo_kunmap 662*592ffb21SWarner Losh * 663*592ffb21SWarner Losh * @map: Object describing the map to unmap. 664*592ffb21SWarner Losh * 665*592ffb21SWarner Losh * Unmaps a kernel map set up by ttm_bo_kmap. 666*592ffb21SWarner Losh */ 667*592ffb21SWarner Losh 668*592ffb21SWarner Losh extern void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map); 669*592ffb21SWarner Losh 670*592ffb21SWarner Losh /** 671*592ffb21SWarner Losh * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object. 672*592ffb21SWarner Losh * 673*592ffb21SWarner Losh * @vma: vma as input from the fbdev mmap method. 674*592ffb21SWarner Losh * @bo: The bo backing the address space. The address space will 675*592ffb21SWarner Losh * have the same size as the bo, and start at offset 0. 676*592ffb21SWarner Losh * 677*592ffb21SWarner Losh * This function is intended to be called by the fbdev mmap method 678*592ffb21SWarner Losh * if the fbdev address space is to be backed by a bo. 679*592ffb21SWarner Losh */ 680*592ffb21SWarner Losh 681*592ffb21SWarner Losh /* XXXKIB 682*592ffb21SWarner Losh extern int ttm_fbdev_mmap(struct vm_area_struct *vma, 683*592ffb21SWarner Losh struct ttm_buffer_object *bo); 684*592ffb21SWarner Losh */ 685*592ffb21SWarner Losh /** 686*592ffb21SWarner Losh * ttm_bo_mmap - mmap out of the ttm device address space. 687*592ffb21SWarner Losh * 688*592ffb21SWarner Losh * @filp: filp as input from the mmap method. 689*592ffb21SWarner Losh * @vma: vma as input from the mmap method. 690*592ffb21SWarner Losh * @bdev: Pointer to the ttm_bo_device with the address space manager. 691*592ffb21SWarner Losh * 692*592ffb21SWarner Losh * This function is intended to be called by the device mmap method. 693*592ffb21SWarner Losh * if the device address space is to be backed by the bo manager. 694*592ffb21SWarner Losh */ 695*592ffb21SWarner Losh /* XXXKIB 696*592ffb21SWarner Losh extern int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma, 697*592ffb21SWarner Losh struct ttm_bo_device *bdev); 698*592ffb21SWarner Losh */ 699*592ffb21SWarner Losh /** 700*592ffb21SWarner Losh * ttm_bo_io 701*592ffb21SWarner Losh * 702*592ffb21SWarner Losh * @bdev: Pointer to the struct ttm_bo_device. 703*592ffb21SWarner Losh * @filp: Pointer to the struct file attempting to read / write. 704*592ffb21SWarner Losh * @wbuf: User-space pointer to address of buffer to write. NULL on read. 705*592ffb21SWarner Losh * @rbuf: User-space pointer to address of buffer to read into. 706*592ffb21SWarner Losh * Null on write. 707*592ffb21SWarner Losh * @count: Number of bytes to read / write. 708*592ffb21SWarner Losh * @f_pos: Pointer to current file position. 709*592ffb21SWarner Losh * @write: 1 for read, 0 for write. 710*592ffb21SWarner Losh * 711*592ffb21SWarner Losh * This function implements read / write into ttm buffer objects, and is 712*592ffb21SWarner Losh * intended to 713*592ffb21SWarner Losh * be called from the fops::read and fops::write method. 714*592ffb21SWarner Losh * Returns: 715*592ffb21SWarner Losh * See man (2) write, man(2) read. In particular, 716*592ffb21SWarner Losh * the function may return -ERESTARTSYS if 717*592ffb21SWarner Losh * interrupted by a signal. 718*592ffb21SWarner Losh */ 719*592ffb21SWarner Losh 720*592ffb21SWarner Losh extern ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp, 721*592ffb21SWarner Losh const char *wbuf, char *rbuf, 722*592ffb21SWarner Losh size_t count, off_t *f_pos, bool write); 723*592ffb21SWarner Losh 724*592ffb21SWarner Losh extern void ttm_bo_swapout_all(struct ttm_bo_device *bdev); 725*592ffb21SWarner Losh 726*592ffb21SWarner Losh /** 727*592ffb21SWarner Losh * ttm_bo_is_reserved - return an indication if a ttm buffer object is reserved 728*592ffb21SWarner Losh * 729*592ffb21SWarner Losh * @bo: The buffer object to check. 730*592ffb21SWarner Losh * 731*592ffb21SWarner Losh * This function returns an indication if a bo is reserved or not, and should 732*592ffb21SWarner Losh * only be used to print an error when it is not from incorrect api usage, since 733*592ffb21SWarner Losh * there's no guarantee that it is the caller that is holding the reservation. 734*592ffb21SWarner Losh */ 735*592ffb21SWarner Losh static inline bool ttm_bo_is_reserved(struct ttm_buffer_object *bo) 736*592ffb21SWarner Losh { 737*592ffb21SWarner Losh return atomic_read(&bo->reserved); 738*592ffb21SWarner Losh } 739*592ffb21SWarner Losh 740*592ffb21SWarner Losh #endif 741