1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #ifndef _TTM_BO_API_H_
32 #define _TTM_BO_API_H_
34 #include <drm/drm_gem.h>
35 #include <drm/drm_vma_manager.h>
36 #include <linux/kref.h>
37 #include <linux/list.h>
38 #include <linux/wait.h>
39 #include <linux/mutex.h>
41 #include <linux/bitmap.h>
42 #include <linux/dma-resv.h>
44 #include "ttm_resource.h"
58 struct ttm_lru_bulk_move;
63 * @ttm_bo_type_device: These are 'normal' buffers that can
64 * be mmapped by user space. Each of these bos occupy a slot in the
65 * device address space, that can be used for normal vm operations.
67 * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
68 * but they cannot be accessed from user-space. For kernel-only use.
70 * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
83 * struct ttm_buffer_object
85 * @base: drm_gem_object superclass data.
86 * @bdev: Pointer to the buffer object device structure.
88 * @page_alignment: Page alignment.
89 * @destroy: Destruction function. If NULL, kfree is used.
90 * @num_pages: Actual number of pages.
91 * @kref: Reference count of this buffer object. When this refcount reaches
92 * zero, the object is destroyed or put on the delayed delete list.
93 * @mem: structure describing current placement.
94 * @ttm: TTM structure holding system pages.
95 * @evicted: Whether the object was evicted without user-space knowing.
96 * @deleted: True if the object is only a zombie and already deleted.
97 * @lru: List head for the lru list.
98 * @ddestroy: List head for the delayed destroy list.
99 * @swap: List head for swap LRU list.
100 * @moving: Fence set when BO is moving
101 * @offset: The current GPU offset, which can have different meanings
102 * depending on the memory type. For SYSTEM type memory, it should be 0.
103 * @cur_placement: Hint of current placement.
105 * Base class for TTM buffer object, that deals with data placement and CPU
106 * mappings. GPU mappings are really up to the driver, but for simpler GPUs
107 * the driver can usually use the placement offset @offset directly as the
108 * GPU virtual address. For drivers implementing multiple
109 * GPU memory manager contexts, the driver should manage the address space
110 * in these contexts separately and use these objects to get the correct
111 * placement and caching for these GPU maps. This makes it possible to use
112 * these objects for even quite elaborate memory management schemes.
113 * The destroy member, the API visibility of this object makes it possible
114 * to derive driver specific types.
117 struct ttm_buffer_object {
118 struct drm_gem_object base;
121 * Members constant at init.
124 struct ttm_device *bdev;
125 enum ttm_bo_type type;
126 uint32_t page_alignment;
127 void (*destroy) (struct ttm_buffer_object *);
130 * Members not needing protection.
135 * Members protected by the bo::resv::reserved lock.
138 struct ttm_resource *resource;
143 * Members protected by the bdev::lru_lock.
146 struct list_head lru;
147 struct list_head ddestroy;
150 * Members protected by a bo reservation.
153 struct dma_fence *moving;
158 * Special members that are protected by the reserve lock
159 * and the bo::lock when written to. Can be read with
160 * either of these locks held.
167 * struct ttm_bo_kmap_obj
169 * @virtual: The current kernel virtual address.
170 * @page: The page when kmap'ing a single page.
171 * @bo_kmap_type: Type of bo_kmap.
173 * Object describing a kernel mapping. Since a TTM bo may be located
174 * in various memory types with various caching policies, the
175 * mapping can either be an ioremap, a vmap, a kmap or part of a
179 #define TTM_BO_MAP_IOMEM_MASK 0x80
180 struct ttm_bo_kmap_obj {
184 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
187 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
189 struct ttm_buffer_object *bo;
193 * struct ttm_operation_ctx
195 * @interruptible: Sleep interruptible if sleeping.
196 * @no_wait_gpu: Return immediately if the GPU is busy.
197 * @gfp_retry_mayfail: Set the __GFP_RETRY_MAYFAIL when allocation pages.
198 * @allow_res_evict: Allow eviction of reserved BOs. Can be used when multiple
199 * BOs share the same reservation object.
200 * @force_alloc: Don't check the memory account during suspend or CPU page
201 * faults. Should only be used by TTM internally.
202 * @resv: Reservation object to allow reserved evictions with.
204 * Context for TTM operations like changing buffer placement or general memory
207 struct ttm_operation_ctx {
210 bool gfp_retry_mayfail;
211 bool allow_res_evict;
213 struct dma_resv *resv;
214 uint64_t bytes_moved;
218 * ttm_bo_get - reference a struct ttm_buffer_object
220 * @bo: The buffer object.
222 static inline void ttm_bo_get(struct ttm_buffer_object *bo)
228 * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless
229 * its refcount has already reached zero.
230 * @bo: The buffer object.
232 * Used to reference a TTM buffer object in lookups where the object is removed
233 * from the lookup structure during the destructor and for RCU lookups.
235 * Returns: @bo if the referencing was successful, NULL otherwise.
237 static inline __must_check struct ttm_buffer_object *
238 ttm_bo_get_unless_zero(struct ttm_buffer_object *bo)
240 if (!kref_get_unless_zero(&bo->kref))
246 * ttm_bo_wait - wait for buffer idle.
248 * @bo: The buffer object.
249 * @interruptible: Use interruptible wait.
250 * @no_wait: Return immediately if buffer is busy.
252 * This function must be called with the bo::mutex held, and makes
253 * sure any previous rendering to the buffer is completed.
254 * Note: It might be necessary to block validations before the
255 * wait by reserving the buffer.
256 * Returns -EBUSY if no_wait is true and the buffer is busy.
257 * Returns -ERESTARTSYS if interrupted by a signal.
259 int ttm_bo_wait(struct ttm_buffer_object *bo, bool interruptible, bool no_wait);
261 static inline int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
263 return ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
269 * @bo: The buffer object.
270 * @placement: Proposed placement for the buffer object.
271 * @ctx: validation parameters.
273 * Changes placement and caching policy of the buffer object
274 * according proposed placement.
276 * -EINVAL on invalid proposed placement.
277 * -ENOMEM on out-of-memory condition.
278 * -EBUSY if no_wait is true and buffer busy.
279 * -ERESTARTSYS if interrupted by a signal.
281 int ttm_bo_validate(struct ttm_buffer_object *bo,
282 struct ttm_placement *placement,
283 struct ttm_operation_ctx *ctx);
288 * @bo: The buffer object.
290 * Unreference a buffer object.
292 void ttm_bo_put(struct ttm_buffer_object *bo);
295 * ttm_bo_move_to_lru_tail
297 * @bo: The buffer object.
298 * @mem: Resource object.
299 * @bulk: optional bulk move structure to remember BO positions
301 * Move this BO to the tail of all lru lists used to lookup and reserve an
302 * object. This function must be called with struct ttm_global::lru_lock
303 * held, and is used to make a BO less likely to be considered for eviction.
305 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
306 struct ttm_resource *mem,
307 struct ttm_lru_bulk_move *bulk);
310 * ttm_bo_bulk_move_lru_tail
312 * @bulk: bulk move structure
314 * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
315 * BO order never changes. Should be called with ttm_global::lru_lock held.
317 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk);
320 * ttm_bo_lock_delayed_workqueue
322 * Prevent the delayed workqueue from running.
324 * True if the workqueue was queued at the time
326 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev);
329 * ttm_bo_unlock_delayed_workqueue
331 * Allows the delayed workqueue to run.
333 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
336 * ttm_bo_eviction_valuable
338 * @bo: The buffer object to evict
339 * @place: the placement we need to make room for
341 * Check if it is valuable to evict the BO to make room for the given placement.
343 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
344 const struct ttm_place *place);
347 * ttm_bo_init_reserved
349 * @bdev: Pointer to a ttm_device struct.
350 * @bo: Pointer to a ttm_buffer_object to be initialized.
351 * @size: Requested size of buffer object.
352 * @type: Requested type of buffer object.
353 * @placement: Initial placement for buffer object.
354 * @page_alignment: Data alignment in pages.
355 * @ctx: TTM operation context for memory allocation.
356 * @sg: Scatter-gather table.
357 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
358 * @destroy: Destroy function. Use NULL for kfree().
360 * This function initializes a pre-allocated struct ttm_buffer_object.
361 * As this object may be part of a larger structure, this function,
362 * together with the @destroy function,
363 * enables driver-specific objects derived from a ttm_buffer_object.
365 * On successful return, the caller owns an object kref to @bo. The kref and
366 * list_kref are usually set to 1, but note that in some situations, other
367 * tasks may already be holding references to @bo as well.
368 * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
369 * and it is the caller's responsibility to call ttm_bo_unreserve.
371 * If a failure occurs, the function will call the @destroy function, or
372 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
373 * illegal and will likely cause memory corruption.
376 * -ENOMEM: Out of memory.
377 * -EINVAL: Invalid placement flags.
378 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
381 int ttm_bo_init_reserved(struct ttm_device *bdev,
382 struct ttm_buffer_object *bo,
383 size_t size, enum ttm_bo_type type,
384 struct ttm_placement *placement,
385 uint32_t page_alignment,
386 struct ttm_operation_ctx *ctx,
387 struct sg_table *sg, struct dma_resv *resv,
388 void (*destroy) (struct ttm_buffer_object *));
393 * @bdev: Pointer to a ttm_device struct.
394 * @bo: Pointer to a ttm_buffer_object to be initialized.
395 * @size: Requested size of buffer object.
396 * @type: Requested type of buffer object.
397 * @placement: Initial placement for buffer object.
398 * @page_alignment: Data alignment in pages.
399 * @interruptible: If needing to sleep to wait for GPU resources,
400 * sleep interruptible.
401 * pinned in physical memory. If this behaviour is not desired, this member
402 * holds a pointer to a persistent shmem object. Typically, this would
403 * point to the shmem object backing a GEM object if TTM is used to back a
404 * GEM user interface.
405 * @sg: Scatter-gather table.
406 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
407 * @destroy: Destroy function. Use NULL for kfree().
409 * This function initializes a pre-allocated struct ttm_buffer_object.
410 * As this object may be part of a larger structure, this function,
411 * together with the @destroy function,
412 * enables driver-specific objects derived from a ttm_buffer_object.
414 * On successful return, the caller owns an object kref to @bo. The kref and
415 * list_kref are usually set to 1, but note that in some situations, other
416 * tasks may already be holding references to @bo as well.
418 * If a failure occurs, the function will call the @destroy function, or
419 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
420 * illegal and will likely cause memory corruption.
423 * -ENOMEM: Out of memory.
424 * -EINVAL: Invalid placement flags.
425 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
427 int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
428 size_t size, enum ttm_bo_type type,
429 struct ttm_placement *placement,
430 uint32_t page_alignment, bool interrubtible,
431 struct sg_table *sg, struct dma_resv *resv,
432 void (*destroy) (struct ttm_buffer_object *));
435 * ttm_kmap_obj_virtual
437 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
438 * @is_iomem: Pointer to an integer that on return indicates 1 if the
439 * virtual map is io memory, 0 if normal memory.
441 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
442 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
443 * that should strictly be accessed by the iowriteXX() and similar functions.
445 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
448 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
455 * @bo: The buffer object.
456 * @start_page: The first page to map.
457 * @num_pages: Number of pages to map.
458 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
460 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
461 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
462 * used to obtain a virtual address to the data.
465 * -ENOMEM: Out of memory.
466 * -EINVAL: Invalid range.
468 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
469 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
474 * @map: Object describing the map to unmap.
476 * Unmaps a kernel map set up by ttm_bo_kmap.
478 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
483 * @bo: The buffer object.
484 * @map: pointer to a struct dma_buf_map representing the map.
486 * Sets up a kernel virtual mapping, using ioremap or vmap to the
487 * data in the buffer object. The parameter @map returns the virtual
488 * address as struct dma_buf_map. Unmap the buffer with ttm_bo_vunmap().
491 * -ENOMEM: Out of memory.
492 * -EINVAL: Invalid range.
494 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
499 * @bo: The buffer object.
500 * @map: Object describing the map to unmap.
502 * Unmaps a kernel map set up by ttm_bo_vmap().
504 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
507 * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
509 * @vma: vma as input from the fbdev mmap method.
510 * @bo: The bo backing the address space.
512 * Maps a buffer object.
514 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
519 * @bdev: Pointer to the struct ttm_device.
520 * @filp: Pointer to the struct file attempting to read / write.
521 * @wbuf: User-space pointer to address of buffer to write. NULL on read.
522 * @rbuf: User-space pointer to address of buffer to read into.
524 * @count: Number of bytes to read / write.
525 * @f_pos: Pointer to current file position.
526 * @write: 1 for read, 0 for write.
528 * This function implements read / write into ttm buffer objects, and is
530 * be called from the fops::read and fops::write method.
532 * See man (2) write, man(2) read. In particular,
533 * the function may return -ERESTARTSYS if
534 * interrupted by a signal.
536 ssize_t ttm_bo_io(struct ttm_device *bdev, struct file *filp,
537 const char __user *wbuf, char __user *rbuf,
538 size_t count, loff_t *f_pos, bool write);
540 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
544 * ttm_bo_pin - Pin the buffer object.
545 * @bo: The buffer object to pin
547 * Make sure the buffer is not evicted any more during memory pressure.
549 static inline void ttm_bo_pin(struct ttm_buffer_object *bo)
551 dma_resv_assert_held(bo->base.resv);
552 WARN_ON_ONCE(!kref_read(&bo->kref));
557 * ttm_bo_unpin - Unpin the buffer object.
558 * @bo: The buffer object to unpin
560 * Allows the buffer object to be evicted again during memory pressure.
562 static inline void ttm_bo_unpin(struct ttm_buffer_object *bo)
564 dma_resv_assert_held(bo->base.resv);
565 WARN_ON_ONCE(!kref_read(&bo->kref));
572 int ttm_mem_evict_first(struct ttm_device *bdev,
573 struct ttm_resource_manager *man,
574 const struct ttm_place *place,
575 struct ttm_operation_ctx *ctx,
576 struct ww_acquire_ctx *ticket);
578 /* Default number of pre-faulted pages in the TTM fault handler */
579 #define TTM_BO_VM_NUM_PREFAULT 16
581 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
582 struct vm_fault *vmf);
584 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
586 pgoff_t num_prefault);
588 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
590 void ttm_bo_vm_open(struct vm_area_struct *vma);
592 void ttm_bo_vm_close(struct vm_area_struct *vma);
594 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
595 void *buf, int len, int write);
596 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all);
598 vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);