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_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>
42 #include <linux/bitmap.h>
43 #include <linux/dma-resv.h>
45 #include "ttm_resource.h"
57 struct ttm_lru_bulk_move;
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.
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.
69 * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
82 * struct ttm_buffer_object
84 * @base: drm_gem_object superclass data.
85 * @bdev: Pointer to the buffer object device structure.
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.
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.
119 struct ttm_buffer_object {
120 struct drm_gem_object base;
123 * Members constant at init.
126 struct ttm_bo_device *bdev;
127 enum ttm_bo_type type;
128 void (*destroy) (struct ttm_buffer_object *);
129 unsigned long num_pages;
133 * Members not needing protection.
138 * Members protected by the bo::resv::reserved lock.
141 struct ttm_resource mem;
142 struct file *persistent_swap_storage;
147 * Members protected by the bdev::lru_lock.
150 struct list_head lru;
151 struct list_head ddestroy;
152 struct list_head swap;
155 * Members protected by a bo reservation.
158 struct dma_fence *moving;
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.
171 * struct ttm_bo_kmap_obj
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.
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
183 #define TTM_BO_MAP_IOMEM_MASK 0x80
184 struct ttm_bo_kmap_obj {
188 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
191 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
193 struct ttm_buffer_object *bo;
197 * struct ttm_operation_ctx
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
204 * Context for TTM operations like changing buffer placement or general memory
207 struct ttm_operation_ctx {
210 struct dma_resv *resv;
211 uint64_t bytes_moved;
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
221 * ttm_bo_get - reference a struct ttm_buffer_object
223 * @bo: The buffer object.
225 static inline void ttm_bo_get(struct ttm_buffer_object *bo)
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.
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.
238 * Returns: @bo if the referencing was successful, NULL otherwise.
240 static inline __must_check struct ttm_buffer_object *
241 ttm_bo_get_unless_zero(struct ttm_buffer_object *bo)
243 if (!kref_get_unless_zero(&bo->kref))
249 * ttm_bo_wait - wait for buffer idle.
251 * @bo: The buffer object.
252 * @interruptible: Use interruptible wait.
253 * @no_wait: Return immediately if buffer is busy.
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.
262 int ttm_bo_wait(struct ttm_buffer_object *bo, bool interruptible, bool no_wait);
264 static inline int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
266 return ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
270 * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo
272 * @placement: Return immediately if buffer is busy.
273 * @mem: The struct ttm_resource indicating the region where the bo resides
274 * @new_flags: Describes compatible placement found
276 * Returns true if the placement is compatible
278 bool ttm_bo_mem_compat(struct ttm_placement *placement, struct ttm_resource *mem,
279 uint32_t *new_flags);
284 * @bo: The buffer object.
285 * @placement: Proposed placement for the buffer object.
286 * @ctx: validation parameters.
288 * Changes placement and caching policy of the buffer object
289 * according proposed placement.
291 * -EINVAL on invalid proposed placement.
292 * -ENOMEM on out-of-memory condition.
293 * -EBUSY if no_wait is true and buffer busy.
294 * -ERESTARTSYS if interrupted by a signal.
296 int ttm_bo_validate(struct ttm_buffer_object *bo,
297 struct ttm_placement *placement,
298 struct ttm_operation_ctx *ctx);
303 * @bo: The buffer object.
305 * Unreference a buffer object.
307 void ttm_bo_put(struct ttm_buffer_object *bo);
310 * ttm_bo_move_to_lru_tail
312 * @bo: The buffer object.
313 * @bulk: optional bulk move structure to remember BO positions
315 * Move this BO to the tail of all lru lists used to lookup and reserve an
316 * object. This function must be called with struct ttm_bo_global::lru_lock
317 * held, and is used to make a BO less likely to be considered for eviction.
319 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
320 struct ttm_lru_bulk_move *bulk);
323 * ttm_bo_bulk_move_lru_tail
325 * @bulk: bulk move structure
327 * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
328 * BO order never changes. Should be called with ttm_bo_global::lru_lock held.
330 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk);
333 * ttm_bo_lock_delayed_workqueue
335 * Prevent the delayed workqueue from running.
337 * True if the workqueue was queued at the time
339 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
342 * ttm_bo_unlock_delayed_workqueue
344 * Allows the delayed workqueue to run.
346 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched);
349 * ttm_bo_eviction_valuable
351 * @bo: The buffer object to evict
352 * @place: the placement we need to make room for
354 * Check if it is valuable to evict the BO to make room for the given placement.
356 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
357 const struct ttm_place *place);
359 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
360 unsigned long bo_size,
361 unsigned struct_size);
364 * ttm_bo_init_reserved
366 * @bdev: Pointer to a ttm_bo_device struct.
367 * @bo: Pointer to a ttm_buffer_object to be initialized.
368 * @size: Requested size of buffer object.
369 * @type: Requested type of buffer object.
370 * @flags: Initial placement flags.
371 * @page_alignment: Data alignment in pages.
372 * @ctx: TTM operation context for memory allocation.
373 * @acc_size: Accounted size for this object.
374 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
375 * @destroy: Destroy function. Use NULL for kfree().
377 * This function initializes a pre-allocated struct ttm_buffer_object.
378 * As this object may be part of a larger structure, this function,
379 * together with the @destroy function,
380 * enables driver-specific objects derived from a ttm_buffer_object.
382 * On successful return, the caller owns an object kref to @bo. The kref and
383 * list_kref are usually set to 1, but note that in some situations, other
384 * tasks may already be holding references to @bo as well.
385 * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
386 * and it is the caller's responsibility to call ttm_bo_unreserve.
388 * If a failure occurs, the function will call the @destroy function, or
389 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
390 * illegal and will likely cause memory corruption.
393 * -ENOMEM: Out of memory.
394 * -EINVAL: Invalid placement flags.
395 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
398 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
399 struct ttm_buffer_object *bo,
401 enum ttm_bo_type type,
402 struct ttm_placement *placement,
403 uint32_t page_alignment,
404 struct ttm_operation_ctx *ctx,
407 struct dma_resv *resv,
408 void (*destroy) (struct ttm_buffer_object *));
413 * @bdev: Pointer to a ttm_bo_device struct.
414 * @bo: Pointer to a ttm_buffer_object to be initialized.
415 * @size: Requested size of buffer object.
416 * @type: Requested type of buffer object.
417 * @flags: Initial placement flags.
418 * @page_alignment: Data alignment in pages.
419 * @interruptible: If needing to sleep to wait for GPU resources,
420 * sleep interruptible.
421 * pinned in physical memory. If this behaviour is not desired, this member
422 * holds a pointer to a persistent shmem object. Typically, this would
423 * point to the shmem object backing a GEM object if TTM is used to back a
424 * GEM user interface.
425 * @acc_size: Accounted size for this object.
426 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
427 * @destroy: Destroy function. Use NULL for kfree().
429 * This function initializes a pre-allocated struct ttm_buffer_object.
430 * As this object may be part of a larger structure, this function,
431 * together with the @destroy function,
432 * enables driver-specific objects derived from a ttm_buffer_object.
434 * On successful return, the caller owns an object kref to @bo. The kref and
435 * list_kref are usually set to 1, but note that in some situations, other
436 * tasks may already be holding references to @bo as well.
438 * If a failure occurs, the function will call the @destroy function, or
439 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
440 * illegal and will likely cause memory corruption.
443 * -ENOMEM: Out of memory.
444 * -EINVAL: Invalid placement flags.
445 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
447 int ttm_bo_init(struct ttm_bo_device *bdev, struct ttm_buffer_object *bo,
448 unsigned long size, enum ttm_bo_type type,
449 struct ttm_placement *placement,
450 uint32_t page_alignment, bool interrubtible, size_t acc_size,
451 struct sg_table *sg, struct dma_resv *resv,
452 void (*destroy) (struct ttm_buffer_object *));
457 * @bdev: Pointer to a ttm_bo_device struct.
458 * @size: Requested size of buffer object.
459 * @type: Requested type of buffer object.
460 * @placement: Initial placement.
461 * @page_alignment: Data alignment in pages.
462 * @interruptible: If needing to sleep while waiting for GPU resources,
463 * sleep interruptible.
464 * @p_bo: On successful completion *p_bo points to the created object.
466 * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
467 * on that object. The destroy function is set to kfree().
469 * -ENOMEM: Out of memory.
470 * -EINVAL: Invalid placement flags.
471 * -ERESTARTSYS: Interrupted by signal while waiting for resources.
473 int ttm_bo_create(struct ttm_bo_device *bdev, unsigned long size,
474 enum ttm_bo_type type, struct ttm_placement *placement,
475 uint32_t page_alignment, bool interruptible,
476 struct ttm_buffer_object **p_bo);
481 * @bdev: Pointer to a ttm_bo_device struct.
482 * @mem_type: The memory type.
484 * Evicts all buffers on the lru list of the memory type.
485 * This is normally part of a VT switch or an
486 * out-of-memory-space-due-to-fragmentation handler.
487 * The caller must make sure that there are no other processes
488 * currently validating buffers, and can do that by taking the
489 * struct ttm_bo_device::ttm_lock in write mode.
492 * -EINVAL: Invalid or uninitialized memory type.
493 * -ERESTARTSYS: The call was interrupted by a signal while waiting to
496 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
499 * ttm_kmap_obj_virtual
501 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
502 * @is_iomem: Pointer to an integer that on return indicates 1 if the
503 * virtual map is io memory, 0 if normal memory.
505 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
506 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
507 * that should strictly be accessed by the iowriteXX() and similar functions.
509 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
512 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
519 * @bo: The buffer object.
520 * @start_page: The first page to map.
521 * @num_pages: Number of pages to map.
522 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
524 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
525 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
526 * used to obtain a virtual address to the data.
529 * -ENOMEM: Out of memory.
530 * -EINVAL: Invalid range.
532 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
533 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
538 * @map: Object describing the map to unmap.
540 * Unmaps a kernel map set up by ttm_bo_kmap.
542 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
545 * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
547 * @vma: vma as input from the fbdev mmap method.
548 * @bo: The bo backing the address space.
550 * Maps a buffer object.
552 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
555 * ttm_bo_mmap - mmap out of the ttm device address space.
557 * @filp: filp as input from the mmap method.
558 * @vma: vma as input from the mmap method.
559 * @bdev: Pointer to the ttm_bo_device with the address space manager.
561 * This function is intended to be called by the device mmap method.
562 * if the device address space is to be backed by the bo manager.
564 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
565 struct ttm_bo_device *bdev);
570 * @bdev: Pointer to the struct ttm_bo_device.
571 * @filp: Pointer to the struct file attempting to read / write.
572 * @wbuf: User-space pointer to address of buffer to write. NULL on read.
573 * @rbuf: User-space pointer to address of buffer to read into.
575 * @count: Number of bytes to read / write.
576 * @f_pos: Pointer to current file position.
577 * @write: 1 for read, 0 for write.
579 * This function implements read / write into ttm buffer objects, and is
581 * be called from the fops::read and fops::write method.
583 * See man (2) write, man(2) read. In particular,
584 * the function may return -ERESTARTSYS if
585 * interrupted by a signal.
587 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
588 const char __user *wbuf, char __user *rbuf,
589 size_t count, loff_t *f_pos, bool write);
591 int ttm_bo_swapout(struct ttm_bo_global *glob,
592 struct ttm_operation_ctx *ctx);
593 void ttm_bo_swapout_all(void);
596 * ttm_bo_uses_embedded_gem_object - check if the given bo uses the
597 * embedded drm_gem_object.
599 * Most ttm drivers are using gem too, so the embedded
600 * ttm_buffer_object.base will be initialized by the driver (before
601 * calling ttm_bo_init). It is also possible to use ttm without gem
602 * though (vmwgfx does that).
604 * This helper will figure whenever a given ttm bo is a gem object too
607 * @bo: The bo to check.
609 static inline bool ttm_bo_uses_embedded_gem_object(struct ttm_buffer_object *bo)
611 return bo->base.dev != NULL;
614 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
615 struct ttm_resource_manager *man,
616 const struct ttm_place *place,
617 struct ttm_operation_ctx *ctx,
618 struct ww_acquire_ctx *ticket);
620 /* Default number of pre-faulted pages in the TTM fault handler */
621 #define TTM_BO_VM_NUM_PREFAULT 16
623 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
624 struct vm_fault *vmf);
626 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
628 pgoff_t num_prefault,
629 pgoff_t fault_page_size);
631 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
633 void ttm_bo_vm_open(struct vm_area_struct *vma);
635 void ttm_bo_vm_close(struct vm_area_struct *vma);
637 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
638 void *buf, int len, int write);