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 36 #include <linux/kref.h> 37 #include <linux/list.h> 38 39 #include "ttm_device.h" 40 41 /* Default number of pre-faulted pages in the TTM fault handler */ 42 #if CONFIG_PGTABLE_LEVELS > 2 43 #define TTM_BO_VM_NUM_PREFAULT (1 << (PMD_SHIFT - PAGE_SHIFT)) 44 #else 45 #define TTM_BO_VM_NUM_PREFAULT 16 46 #endif 47 48 struct iosys_map; 49 50 struct ttm_global; 51 struct ttm_device; 52 struct ttm_placement; 53 struct ttm_place; 54 struct ttm_resource; 55 struct ttm_resource_manager; 56 struct ttm_tt; 57 58 /** 59 * enum ttm_bo_type 60 * 61 * @ttm_bo_type_device: These are 'normal' buffers that can 62 * be mmapped by user space. Each of these bos occupy a slot in the 63 * device address space, that can be used for normal vm operations. 64 * 65 * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers, 66 * but they cannot be accessed from user-space. For kernel-only use. 67 * 68 * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another 69 * driver. 70 */ 71 enum ttm_bo_type { 72 ttm_bo_type_device, 73 ttm_bo_type_kernel, 74 ttm_bo_type_sg 75 }; 76 77 /** 78 * struct ttm_buffer_object 79 * 80 * @base: drm_gem_object superclass data. 81 * @bdev: Pointer to the buffer object device structure. 82 * @type: The bo type. 83 * @page_alignment: Page alignment. 84 * @destroy: Destruction function. If NULL, kfree is used. 85 * @kref: Reference count of this buffer object. When this refcount reaches 86 * zero, the object is destroyed or put on the delayed delete list. 87 * @resource: structure describing current placement. 88 * @ttm: TTM structure holding system pages. 89 * @deleted: True if the object is only a zombie and already deleted. 90 * @bulk_move: The bulk move object. 91 * @priority: Priority for LRU, BOs with lower priority are evicted first. 92 * @pin_count: Pin count. 93 * 94 * Base class for TTM buffer object, that deals with data placement and CPU 95 * mappings. GPU mappings are really up to the driver, but for simpler GPUs 96 * the driver can usually use the placement offset @offset directly as the 97 * GPU virtual address. For drivers implementing multiple 98 * GPU memory manager contexts, the driver should manage the address space 99 * in these contexts separately and use these objects to get the correct 100 * placement and caching for these GPU maps. This makes it possible to use 101 * these objects for even quite elaborate memory management schemes. 102 * The destroy member, the API visibility of this object makes it possible 103 * to derive driver specific types. 104 */ 105 struct ttm_buffer_object { 106 struct drm_gem_object base; 107 108 /* 109 * Members constant at init. 110 */ 111 struct ttm_device *bdev; 112 enum ttm_bo_type type; 113 uint32_t page_alignment; 114 void (*destroy) (struct ttm_buffer_object *); 115 116 /* 117 * Members not needing protection. 118 */ 119 struct kref kref; 120 121 /* 122 * Members protected by the bo::resv::reserved lock. 123 */ 124 struct ttm_resource *resource; 125 struct ttm_tt *ttm; 126 bool deleted; 127 struct ttm_lru_bulk_move *bulk_move; 128 unsigned priority; 129 unsigned pin_count; 130 131 /** 132 * @delayed_delete: Work item used when we can't delete the BO 133 * immediately 134 */ 135 struct work_struct delayed_delete; 136 137 /** 138 * @sg: external source of pages and DMA addresses, protected by the 139 * reservation lock. 140 */ 141 struct sg_table *sg; 142 }; 143 144 #define TTM_BO_MAP_IOMEM_MASK 0x80 145 146 /** 147 * struct ttm_bo_kmap_obj 148 * 149 * @virtual: The current kernel virtual address. 150 * @page: The page when kmap'ing a single page. 151 * @bo_kmap_type: Type of bo_kmap. 152 * @bo: The TTM BO. 153 * 154 * Object describing a kernel mapping. Since a TTM bo may be located 155 * in various memory types with various caching policies, the 156 * mapping can either be an ioremap, a vmap, a kmap or part of a 157 * premapped region. 158 */ 159 struct ttm_bo_kmap_obj { 160 void *virtual; 161 struct page *page; 162 enum { 163 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK, 164 ttm_bo_map_vmap = 2, 165 ttm_bo_map_kmap = 3, 166 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK, 167 } bo_kmap_type; 168 struct ttm_buffer_object *bo; 169 }; 170 171 /** 172 * struct ttm_operation_ctx 173 * 174 * @interruptible: Sleep interruptible if sleeping. 175 * @no_wait_gpu: Return immediately if the GPU is busy. 176 * @gfp_retry_mayfail: Set the __GFP_RETRY_MAYFAIL when allocation pages. 177 * @allow_res_evict: Allow eviction of reserved BOs. Can be used when multiple 178 * BOs share the same reservation object. 179 * @force_alloc: Don't check the memory account during suspend or CPU page 180 * faults. Should only be used by TTM internally. 181 * @resv: Reservation object to allow reserved evictions with. 182 * @bytes_moved: Statistics on how many bytes have been moved. 183 * 184 * Context for TTM operations like changing buffer placement or general memory 185 * allocation. 186 */ 187 struct ttm_operation_ctx { 188 bool interruptible; 189 bool no_wait_gpu; 190 bool gfp_retry_mayfail; 191 bool allow_res_evict; 192 bool force_alloc; 193 struct dma_resv *resv; 194 uint64_t bytes_moved; 195 }; 196 197 struct ttm_lru_walk; 198 199 /** struct ttm_lru_walk_ops - Operations for a LRU walk. */ 200 struct ttm_lru_walk_ops { 201 /** 202 * process_bo - Process this bo. 203 * @walk: struct ttm_lru_walk describing the walk. 204 * @bo: A locked and referenced buffer object. 205 * 206 * Return: Negative error code on error, User-defined positive value 207 * (typically, but not always, size of the processed bo) on success. 208 * On success, the returned values are summed by the walk and the 209 * walk exits when its target is met. 210 * 0 also indicates success, -EBUSY means this bo was skipped. 211 */ 212 s64 (*process_bo)(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo); 213 }; 214 215 /** 216 * struct ttm_lru_walk - Structure describing a LRU walk. 217 */ 218 struct ttm_lru_walk { 219 /** @ops: Pointer to the ops structure. */ 220 const struct ttm_lru_walk_ops *ops; 221 /** @ctx: Pointer to the struct ttm_operation_ctx. */ 222 struct ttm_operation_ctx *ctx; 223 /** @ticket: The struct ww_acquire_ctx if any. */ 224 struct ww_acquire_ctx *ticket; 225 /** @trylock_only: Only use trylock for locking. */ 226 bool trylock_only; 227 }; 228 229 s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev, 230 struct ttm_resource_manager *man, s64 target); 231 232 /** 233 * ttm_bo_get - reference a struct ttm_buffer_object 234 * 235 * @bo: The buffer object. 236 */ 237 static inline void ttm_bo_get(struct ttm_buffer_object *bo) 238 { 239 kref_get(&bo->kref); 240 } 241 242 /** 243 * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless 244 * its refcount has already reached zero. 245 * @bo: The buffer object. 246 * 247 * Used to reference a TTM buffer object in lookups where the object is removed 248 * from the lookup structure during the destructor and for RCU lookups. 249 * 250 * Returns: @bo if the referencing was successful, NULL otherwise. 251 */ 252 static inline __must_check struct ttm_buffer_object * 253 ttm_bo_get_unless_zero(struct ttm_buffer_object *bo) 254 { 255 if (!kref_get_unless_zero(&bo->kref)) 256 return NULL; 257 return bo; 258 } 259 260 /** 261 * ttm_bo_reserve: 262 * 263 * @bo: A pointer to a struct ttm_buffer_object. 264 * @interruptible: Sleep interruptible if waiting. 265 * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY. 266 * @ticket: ticket used to acquire the ww_mutex. 267 * 268 * Locks a buffer object for validation. (Or prevents other processes from 269 * locking it for validation), while taking a number of measures to prevent 270 * deadlocks. 271 * 272 * Returns: 273 * -EDEADLK: The reservation may cause a deadlock. 274 * Release all buffer reservations, wait for @bo to become unreserved and 275 * try again. 276 * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by 277 * a signal. Release all buffer reservations and return to user-space. 278 * -EBUSY: The function needed to sleep, but @no_wait was true 279 * -EALREADY: Bo already reserved using @ticket. This error code will only 280 * be returned if @use_ticket is set to true. 281 */ 282 static inline int ttm_bo_reserve(struct ttm_buffer_object *bo, 283 bool interruptible, bool no_wait, 284 struct ww_acquire_ctx *ticket) 285 { 286 int ret = 0; 287 288 if (no_wait) { 289 bool success; 290 291 if (WARN_ON(ticket)) 292 return -EBUSY; 293 294 success = dma_resv_trylock(bo->base.resv); 295 return success ? 0 : -EBUSY; 296 } 297 298 if (interruptible) 299 ret = dma_resv_lock_interruptible(bo->base.resv, ticket); 300 else 301 ret = dma_resv_lock(bo->base.resv, ticket); 302 if (ret == -EINTR) 303 return -ERESTARTSYS; 304 return ret; 305 } 306 307 /** 308 * ttm_bo_reserve_slowpath: 309 * @bo: A pointer to a struct ttm_buffer_object. 310 * @interruptible: Sleep interruptible if waiting. 311 * @ticket: Ticket used to acquire the ww_mutex. 312 * 313 * This is called after ttm_bo_reserve returns -EAGAIN and we backed off 314 * from all our other reservations. Because there are no other reservations 315 * held by us, this function cannot deadlock any more. 316 */ 317 static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo, 318 bool interruptible, 319 struct ww_acquire_ctx *ticket) 320 { 321 if (interruptible) { 322 int ret = dma_resv_lock_slow_interruptible(bo->base.resv, 323 ticket); 324 if (ret == -EINTR) 325 ret = -ERESTARTSYS; 326 return ret; 327 } 328 dma_resv_lock_slow(bo->base.resv, ticket); 329 return 0; 330 } 331 332 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo); 333 334 static inline void 335 ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo) 336 { 337 spin_lock(&bo->bdev->lru_lock); 338 ttm_bo_move_to_lru_tail(bo); 339 spin_unlock(&bo->bdev->lru_lock); 340 } 341 342 static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo, 343 struct ttm_resource *new_mem) 344 { 345 WARN_ON(bo->resource); 346 bo->resource = new_mem; 347 } 348 349 /** 350 * ttm_bo_move_null - assign memory for a buffer object. 351 * @bo: The bo to assign the memory to 352 * @new_mem: The memory to be assigned. 353 * 354 * Assign the memory from new_mem to the memory of the buffer object bo. 355 */ 356 static inline void ttm_bo_move_null(struct ttm_buffer_object *bo, 357 struct ttm_resource *new_mem) 358 { 359 ttm_resource_free(bo, &bo->resource); 360 ttm_bo_assign_mem(bo, new_mem); 361 } 362 363 /** 364 * ttm_bo_unreserve 365 * 366 * @bo: A pointer to a struct ttm_buffer_object. 367 * 368 * Unreserve a previous reservation of @bo. 369 */ 370 static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo) 371 { 372 ttm_bo_move_to_lru_tail_unlocked(bo); 373 dma_resv_unlock(bo->base.resv); 374 } 375 376 /** 377 * ttm_kmap_obj_virtual 378 * 379 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap. 380 * @is_iomem: Pointer to an integer that on return indicates 1 if the 381 * virtual map is io memory, 0 if normal memory. 382 * 383 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap. 384 * If *is_iomem is 1 on return, the virtual address points to an io memory area, 385 * that should strictly be accessed by the iowriteXX() and similar functions. 386 */ 387 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map, 388 bool *is_iomem) 389 { 390 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK); 391 return map->virtual; 392 } 393 394 int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, 395 struct ttm_operation_ctx *ctx); 396 int ttm_bo_validate(struct ttm_buffer_object *bo, 397 struct ttm_placement *placement, 398 struct ttm_operation_ctx *ctx); 399 void ttm_bo_put(struct ttm_buffer_object *bo); 400 void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo, 401 struct ttm_lru_bulk_move *bulk); 402 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, 403 const struct ttm_place *place); 404 int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo, 405 enum ttm_bo_type type, struct ttm_placement *placement, 406 uint32_t alignment, struct ttm_operation_ctx *ctx, 407 struct sg_table *sg, struct dma_resv *resv, 408 void (*destroy)(struct ttm_buffer_object *)); 409 int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo, 410 enum ttm_bo_type type, struct ttm_placement *placement, 411 uint32_t alignment, bool interruptible, 412 struct sg_table *sg, struct dma_resv *resv, 413 void (*destroy)(struct ttm_buffer_object *)); 414 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page, 415 unsigned long num_pages, struct ttm_bo_kmap_obj *map); 416 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map); 417 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct iosys_map *map); 418 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct iosys_map *map); 419 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo); 420 s64 ttm_bo_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx, 421 struct ttm_resource_manager *man, gfp_t gfp_flags, 422 s64 target); 423 void ttm_bo_pin(struct ttm_buffer_object *bo); 424 void ttm_bo_unpin(struct ttm_buffer_object *bo); 425 int ttm_bo_evict_first(struct ttm_device *bdev, 426 struct ttm_resource_manager *man, 427 struct ttm_operation_ctx *ctx); 428 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo, 429 struct vm_fault *vmf); 430 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, 431 pgprot_t prot, 432 pgoff_t num_prefault); 433 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf); 434 void ttm_bo_vm_open(struct vm_area_struct *vma); 435 void ttm_bo_vm_close(struct vm_area_struct *vma); 436 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr, 437 void *buf, int len, int write); 438 vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot); 439 440 int ttm_bo_mem_space(struct ttm_buffer_object *bo, 441 struct ttm_placement *placement, 442 struct ttm_resource **mem, 443 struct ttm_operation_ctx *ctx); 444 445 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo); 446 /* 447 * ttm_bo_util.c 448 */ 449 int ttm_mem_io_reserve(struct ttm_device *bdev, 450 struct ttm_resource *mem); 451 void ttm_mem_io_free(struct ttm_device *bdev, 452 struct ttm_resource *mem); 453 void ttm_move_memcpy(bool clear, u32 num_pages, 454 struct ttm_kmap_iter *dst_iter, 455 struct ttm_kmap_iter *src_iter); 456 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo, 457 struct ttm_operation_ctx *ctx, 458 struct ttm_resource *new_mem); 459 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo, 460 struct dma_fence *fence, bool evict, 461 bool pipeline, 462 struct ttm_resource *new_mem); 463 void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo, 464 struct ttm_resource *new_mem); 465 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo); 466 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res, 467 pgprot_t tmp); 468 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo); 469 470 #endif 471