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"
59 struct ttm_lru_bulk_move;
64 * @ttm_bo_type_device: These are 'normal' buffers that can
65 * be mmapped by user space. Each of these bos occupy a slot in the
66 * device address space, that can be used for normal vm operations.
68 * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
69 * but they cannot be accessed from user-space. For kernel-only use.
71 * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
84 * struct ttm_buffer_object
86 * @base: drm_gem_object superclass data.
87 * @bdev: Pointer to the buffer object device structure.
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 void (*destroy) (struct ttm_buffer_object *);
129 * Members not needing protection.
134 * Members protected by the bo::resv::reserved lock.
137 struct ttm_resource mem;
142 * Members protected by the bdev::lru_lock.
145 struct list_head lru;
146 struct list_head ddestroy;
149 * Members protected by a bo reservation.
152 struct dma_fence *moving;
157 * Special members that are protected by the reserve lock
158 * and the bo::lock when written to. Can be read with
159 * either of these locks held.
166 * struct ttm_bo_kmap_obj
168 * @virtual: The current kernel virtual address.
169 * @page: The page when kmap'ing a single page.
170 * @bo_kmap_type: Type of bo_kmap.
172 * Object describing a kernel mapping. Since a TTM bo may be located
173 * in various memory types with various caching policies, the
174 * mapping can either be an ioremap, a vmap, a kmap or part of a
178 #define TTM_BO_MAP_IOMEM_MASK 0x80
179 struct ttm_bo_kmap_obj {
183 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
186 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
188 struct ttm_buffer_object *bo;
192 * struct ttm_operation_ctx
194 * @interruptible: Sleep interruptible if sleeping.
195 * @no_wait_gpu: Return immediately if the GPU is busy.
196 * @gfp_retry_mayfail: Set the __GFP_RETRY_MAYFAIL when allocation pages.
197 * @allow_res_evict: Allow eviction of reserved BOs. Can be used when multiple
198 * BOs share the same reservation object.
199 * @force_alloc: Don't check the memory account during suspend or CPU page
200 * faults. Should only be used by TTM internally.
201 * @resv: Reservation object to allow reserved evictions with.
203 * Context for TTM operations like changing buffer placement or general memory
206 struct ttm_operation_ctx {
209 bool gfp_retry_mayfail;
210 bool allow_res_evict;
212 struct dma_resv *resv;
213 uint64_t bytes_moved;
217 * ttm_bo_get - reference a struct ttm_buffer_object
219 * @bo: The buffer object.
221 static inline void ttm_bo_get(struct ttm_buffer_object *bo)
227 * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless
228 * its refcount has already reached zero.
229 * @bo: The buffer object.
231 * Used to reference a TTM buffer object in lookups where the object is removed
232 * from the lookup structure during the destructor and for RCU lookups.
234 * Returns: @bo if the referencing was successful, NULL otherwise.
236 static inline __must_check struct ttm_buffer_object *
237 ttm_bo_get_unless_zero(struct ttm_buffer_object *bo)
239 if (!kref_get_unless_zero(&bo->kref))
245 * ttm_bo_wait - wait for buffer idle.
247 * @bo: The buffer object.
248 * @interruptible: Use interruptible wait.
249 * @no_wait: Return immediately if buffer is busy.
251 * This function must be called with the bo::mutex held, and makes
252 * sure any previous rendering to the buffer is completed.
253 * Note: It might be necessary to block validations before the
254 * wait by reserving the buffer.
255 * Returns -EBUSY if no_wait is true and the buffer is busy.
256 * Returns -ERESTARTSYS if interrupted by a signal.
258 int ttm_bo_wait(struct ttm_buffer_object *bo, bool interruptible, bool no_wait);
260 static inline int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
262 return ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
266 * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo
268 * @placement: Return immediately if buffer is busy.
269 * @mem: The struct ttm_resource indicating the region where the bo resides
270 * @new_flags: Describes compatible placement found
272 * Returns true if the placement is compatible
274 bool ttm_bo_mem_compat(struct ttm_placement *placement, struct ttm_resource *mem,
275 uint32_t *new_flags);
280 * @bo: The buffer object.
281 * @placement: Proposed placement for the buffer object.
282 * @ctx: validation parameters.
284 * Changes placement and caching policy of the buffer object
285 * according proposed placement.
287 * -EINVAL on invalid proposed placement.
288 * -ENOMEM on out-of-memory condition.
289 * -EBUSY if no_wait is true and buffer busy.
290 * -ERESTARTSYS if interrupted by a signal.
292 int ttm_bo_validate(struct ttm_buffer_object *bo,
293 struct ttm_placement *placement,
294 struct ttm_operation_ctx *ctx);
299 * @bo: The buffer object.
301 * Unreference a buffer object.
303 void ttm_bo_put(struct ttm_buffer_object *bo);
306 * ttm_bo_move_to_lru_tail
308 * @bo: The buffer object.
309 * @mem: Resource object.
310 * @bulk: optional bulk move structure to remember BO positions
312 * Move this BO to the tail of all lru lists used to lookup and reserve an
313 * object. This function must be called with struct ttm_global::lru_lock
314 * held, and is used to make a BO less likely to be considered for eviction.
316 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
317 struct ttm_resource *mem,
318 struct ttm_lru_bulk_move *bulk);
321 * ttm_bo_bulk_move_lru_tail
323 * @bulk: bulk move structure
325 * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
326 * BO order never changes. Should be called with ttm_global::lru_lock held.
328 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk);
331 * ttm_bo_lock_delayed_workqueue
333 * Prevent the delayed workqueue from running.
335 * True if the workqueue was queued at the time
337 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev);
340 * ttm_bo_unlock_delayed_workqueue
342 * Allows the delayed workqueue to run.
344 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
347 * ttm_bo_eviction_valuable
349 * @bo: The buffer object to evict
350 * @place: the placement we need to make room for
352 * Check if it is valuable to evict the BO to make room for the given placement.
354 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
355 const struct ttm_place *place);
358 * ttm_bo_init_reserved
360 * @bdev: Pointer to a ttm_device struct.
361 * @bo: Pointer to a ttm_buffer_object to be initialized.
362 * @size: Requested size of buffer object.
363 * @type: Requested type of buffer object.
364 * @flags: Initial placement flags.
365 * @page_alignment: Data alignment in pages.
366 * @ctx: TTM operation context for memory allocation.
367 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
368 * @destroy: Destroy function. Use NULL for kfree().
370 * This function initializes a pre-allocated struct ttm_buffer_object.
371 * As this object may be part of a larger structure, this function,
372 * together with the @destroy function,
373 * enables driver-specific objects derived from a ttm_buffer_object.
375 * On successful return, the caller owns an object kref to @bo. The kref and
376 * list_kref are usually set to 1, but note that in some situations, other
377 * tasks may already be holding references to @bo as well.
378 * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
379 * and it is the caller's responsibility to call ttm_bo_unreserve.
381 * If a failure occurs, the function will call the @destroy function, or
382 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
383 * illegal and will likely cause memory corruption.
386 * -ENOMEM: Out of memory.
387 * -EINVAL: Invalid placement flags.
388 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
391 int ttm_bo_init_reserved(struct ttm_device *bdev,
392 struct ttm_buffer_object *bo,
393 size_t size, enum ttm_bo_type type,
394 struct ttm_placement *placement,
395 uint32_t page_alignment,
396 struct ttm_operation_ctx *ctx,
397 struct sg_table *sg, struct dma_resv *resv,
398 void (*destroy) (struct ttm_buffer_object *));
403 * @bdev: Pointer to a ttm_device struct.
404 * @bo: Pointer to a ttm_buffer_object to be initialized.
405 * @size: Requested size of buffer object.
406 * @type: Requested type of buffer object.
407 * @flags: Initial placement flags.
408 * @page_alignment: Data alignment in pages.
409 * @interruptible: If needing to sleep to wait for GPU resources,
410 * sleep interruptible.
411 * pinned in physical memory. If this behaviour is not desired, this member
412 * holds a pointer to a persistent shmem object. Typically, this would
413 * point to the shmem object backing a GEM object if TTM is used to back a
414 * GEM user interface.
415 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
416 * @destroy: Destroy function. Use NULL for kfree().
418 * This function initializes a pre-allocated struct ttm_buffer_object.
419 * As this object may be part of a larger structure, this function,
420 * together with the @destroy function,
421 * enables driver-specific objects derived from a ttm_buffer_object.
423 * On successful return, the caller owns an object kref to @bo. The kref and
424 * list_kref are usually set to 1, but note that in some situations, other
425 * tasks may already be holding references to @bo as well.
427 * If a failure occurs, the function will call the @destroy function, or
428 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
429 * illegal and will likely cause memory corruption.
432 * -ENOMEM: Out of memory.
433 * -EINVAL: Invalid placement flags.
434 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
436 int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
437 size_t size, enum ttm_bo_type type,
438 struct ttm_placement *placement,
439 uint32_t page_alignment, bool interrubtible,
440 struct sg_table *sg, struct dma_resv *resv,
441 void (*destroy) (struct ttm_buffer_object *));
444 * ttm_kmap_obj_virtual
446 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
447 * @is_iomem: Pointer to an integer that on return indicates 1 if the
448 * virtual map is io memory, 0 if normal memory.
450 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
451 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
452 * that should strictly be accessed by the iowriteXX() and similar functions.
454 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
457 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
464 * @bo: The buffer object.
465 * @start_page: The first page to map.
466 * @num_pages: Number of pages to map.
467 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
469 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
470 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
471 * used to obtain a virtual address to the data.
474 * -ENOMEM: Out of memory.
475 * -EINVAL: Invalid range.
477 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
478 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
483 * @map: Object describing the map to unmap.
485 * Unmaps a kernel map set up by ttm_bo_kmap.
487 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
492 * @bo: The buffer object.
493 * @map: pointer to a struct dma_buf_map representing the map.
495 * Sets up a kernel virtual mapping, using ioremap or vmap to the
496 * data in the buffer object. The parameter @map returns the virtual
497 * address as struct dma_buf_map. Unmap the buffer with ttm_bo_vunmap().
500 * -ENOMEM: Out of memory.
501 * -EINVAL: Invalid range.
503 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
508 * @bo: The buffer object.
509 * @map: Object describing the map to unmap.
511 * Unmaps a kernel map set up by ttm_bo_vmap().
513 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
516 * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
518 * @vma: vma as input from the fbdev mmap method.
519 * @bo: The bo backing the address space.
521 * Maps a buffer object.
523 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
526 * ttm_bo_mmap - mmap out of the ttm device address space.
528 * @filp: filp as input from the mmap method.
529 * @vma: vma as input from the mmap method.
530 * @bdev: Pointer to the ttm_device with the address space manager.
532 * This function is intended to be called by the device mmap method.
533 * if the device address space is to be backed by the bo manager.
535 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
536 struct ttm_device *bdev);
541 * @bdev: Pointer to the struct ttm_device.
542 * @filp: Pointer to the struct file attempting to read / write.
543 * @wbuf: User-space pointer to address of buffer to write. NULL on read.
544 * @rbuf: User-space pointer to address of buffer to read into.
546 * @count: Number of bytes to read / write.
547 * @f_pos: Pointer to current file position.
548 * @write: 1 for read, 0 for write.
550 * This function implements read / write into ttm buffer objects, and is
552 * be called from the fops::read and fops::write method.
554 * See man (2) write, man(2) read. In particular,
555 * the function may return -ERESTARTSYS if
556 * interrupted by a signal.
558 ssize_t ttm_bo_io(struct ttm_device *bdev, struct file *filp,
559 const char __user *wbuf, char __user *rbuf,
560 size_t count, loff_t *f_pos, bool write);
562 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
566 * ttm_bo_uses_embedded_gem_object - check if the given bo uses the
567 * embedded drm_gem_object.
569 * Most ttm drivers are using gem too, so the embedded
570 * ttm_buffer_object.base will be initialized by the driver (before
571 * calling ttm_bo_init). It is also possible to use ttm without gem
572 * though (vmwgfx does that).
574 * This helper will figure whenever a given ttm bo is a gem object too
577 * @bo: The bo to check.
579 static inline bool ttm_bo_uses_embedded_gem_object(struct ttm_buffer_object *bo)
581 return bo->base.dev != NULL;
585 * ttm_bo_pin - Pin the buffer object.
586 * @bo: The buffer object to pin
588 * Make sure the buffer is not evicted any more during memory pressure.
590 static inline void ttm_bo_pin(struct ttm_buffer_object *bo)
592 dma_resv_assert_held(bo->base.resv);
593 WARN_ON_ONCE(!kref_read(&bo->kref));
598 * ttm_bo_unpin - Unpin the buffer object.
599 * @bo: The buffer object to unpin
601 * Allows the buffer object to be evicted again during memory pressure.
603 static inline void ttm_bo_unpin(struct ttm_buffer_object *bo)
605 dma_resv_assert_held(bo->base.resv);
606 WARN_ON_ONCE(!kref_read(&bo->kref));
613 int ttm_mem_evict_first(struct ttm_device *bdev,
614 struct ttm_resource_manager *man,
615 const struct ttm_place *place,
616 struct ttm_operation_ctx *ctx,
617 struct ww_acquire_ctx *ticket);
619 /* Default number of pre-faulted pages in the TTM fault handler */
620 #define TTM_BO_VM_NUM_PREFAULT 16
622 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
623 struct vm_fault *vmf);
625 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
627 pgoff_t num_prefault,
628 pgoff_t fault_page_size);
630 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
632 void ttm_bo_vm_open(struct vm_area_struct *vma);
634 void ttm_bo_vm_close(struct vm_area_struct *vma);
636 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
637 void *buf, int len, int write);
638 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all);