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 * @acc_size: Accounted size for this object.
92 * @kref: Reference count of this buffer object. When this refcount reaches
93 * zero, the object is destroyed or put on the delayed delete list.
94 * @mem: structure describing current placement.
95 * @ttm: TTM structure holding system pages.
96 * @evicted: Whether the object was evicted without user-space knowing.
97 * @deleted: True if the object is only a zombie and already deleted.
98 * @lru: List head for the lru list.
99 * @ddestroy: List head for the delayed destroy list.
100 * @swap: List head for swap LRU list.
101 * @moving: Fence set when BO is moving
102 * @offset: The current GPU offset, which can have different meanings
103 * depending on the memory type. For SYSTEM type memory, it should be 0.
104 * @cur_placement: Hint of current placement.
106 * Base class for TTM buffer object, that deals with data placement and CPU
107 * mappings. GPU mappings are really up to the driver, but for simpler GPUs
108 * the driver can usually use the placement offset @offset directly as the
109 * GPU virtual address. For drivers implementing multiple
110 * GPU memory manager contexts, the driver should manage the address space
111 * in these contexts separately and use these objects to get the correct
112 * placement and caching for these GPU maps. This makes it possible to use
113 * these objects for even quite elaborate memory management schemes.
114 * The destroy member, the API visibility of this object makes it possible
115 * to derive driver specific types.
118 struct ttm_buffer_object {
119 struct drm_gem_object base;
122 * Members constant at init.
125 struct ttm_bo_device *bdev;
126 enum ttm_bo_type type;
127 void (*destroy) (struct ttm_buffer_object *);
131 * Members not needing protection.
136 * Members protected by the bo::resv::reserved lock.
139 struct ttm_resource mem;
144 * Members protected by the bdev::lru_lock.
147 struct list_head lru;
148 struct list_head ddestroy;
149 struct list_head swap;
152 * Members protected by a bo reservation.
155 struct dma_fence *moving;
160 * Special members that are protected by the reserve lock
161 * and the bo::lock when written to. Can be read with
162 * either of these locks held.
169 * struct ttm_bo_kmap_obj
171 * @virtual: The current kernel virtual address.
172 * @page: The page when kmap'ing a single page.
173 * @bo_kmap_type: Type of bo_kmap.
175 * Object describing a kernel mapping. Since a TTM bo may be located
176 * in various memory types with various caching policies, the
177 * mapping can either be an ioremap, a vmap, a kmap or part of a
181 #define TTM_BO_MAP_IOMEM_MASK 0x80
182 struct ttm_bo_kmap_obj {
186 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
189 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
191 struct ttm_buffer_object *bo;
195 * struct ttm_operation_ctx
197 * @interruptible: Sleep interruptible if sleeping.
198 * @no_wait_gpu: Return immediately if the GPU is busy.
199 * @gfp_retry_mayfail: Set the __GFP_RETRY_MAYFAIL when allocation pages.
200 * @allow_res_evict: Allow eviction of reserved BOs. Can be used when multiple
201 * BOs share the same reservation object.
202 * @force_alloc: Don't check the memory account during suspend or CPU page
203 * faults. Should only be used by TTM internally.
204 * @resv: Reservation object to allow reserved evictions with.
206 * Context for TTM operations like changing buffer placement or general memory
209 struct ttm_operation_ctx {
212 bool gfp_retry_mayfail;
213 bool allow_res_evict;
215 struct dma_resv *resv;
216 uint64_t bytes_moved;
220 * ttm_bo_get - reference a struct ttm_buffer_object
222 * @bo: The buffer object.
224 static inline void ttm_bo_get(struct ttm_buffer_object *bo)
230 * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless
231 * its refcount has already reached zero.
232 * @bo: The buffer object.
234 * Used to reference a TTM buffer object in lookups where the object is removed
235 * from the lookup structure during the destructor and for RCU lookups.
237 * Returns: @bo if the referencing was successful, NULL otherwise.
239 static inline __must_check struct ttm_buffer_object *
240 ttm_bo_get_unless_zero(struct ttm_buffer_object *bo)
242 if (!kref_get_unless_zero(&bo->kref))
248 * ttm_bo_wait - wait for buffer idle.
250 * @bo: The buffer object.
251 * @interruptible: Use interruptible wait.
252 * @no_wait: Return immediately if buffer is busy.
254 * This function must be called with the bo::mutex held, and makes
255 * sure any previous rendering to the buffer is completed.
256 * Note: It might be necessary to block validations before the
257 * wait by reserving the buffer.
258 * Returns -EBUSY if no_wait is true and the buffer is busy.
259 * Returns -ERESTARTSYS if interrupted by a signal.
261 int ttm_bo_wait(struct ttm_buffer_object *bo, bool interruptible, bool no_wait);
263 static inline int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
265 return ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
269 * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo
271 * @placement: Return immediately if buffer is busy.
272 * @mem: The struct ttm_resource indicating the region where the bo resides
273 * @new_flags: Describes compatible placement found
275 * Returns true if the placement is compatible
277 bool ttm_bo_mem_compat(struct ttm_placement *placement, struct ttm_resource *mem,
278 uint32_t *new_flags);
283 * @bo: The buffer object.
284 * @placement: Proposed placement for the buffer object.
285 * @ctx: validation parameters.
287 * Changes placement and caching policy of the buffer object
288 * according proposed placement.
290 * -EINVAL on invalid proposed placement.
291 * -ENOMEM on out-of-memory condition.
292 * -EBUSY if no_wait is true and buffer busy.
293 * -ERESTARTSYS if interrupted by a signal.
295 int ttm_bo_validate(struct ttm_buffer_object *bo,
296 struct ttm_placement *placement,
297 struct ttm_operation_ctx *ctx);
302 * @bo: The buffer object.
304 * Unreference a buffer object.
306 void ttm_bo_put(struct ttm_buffer_object *bo);
309 * ttm_bo_move_to_lru_tail
311 * @bo: The buffer object.
312 * @mem: Resource 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_resource *mem,
321 struct ttm_lru_bulk_move *bulk);
324 * ttm_bo_bulk_move_lru_tail
326 * @bulk: bulk move structure
328 * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
329 * BO order never changes. Should be called with ttm_bo_global::lru_lock held.
331 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk);
334 * ttm_bo_lock_delayed_workqueue
336 * Prevent the delayed workqueue from running.
338 * True if the workqueue was queued at the time
340 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
343 * ttm_bo_unlock_delayed_workqueue
345 * Allows the delayed workqueue to run.
347 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched);
350 * ttm_bo_eviction_valuable
352 * @bo: The buffer object to evict
353 * @place: the placement we need to make room for
355 * Check if it is valuable to evict the BO to make room for the given placement.
357 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
358 const struct ttm_place *place);
360 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
361 unsigned long bo_size,
362 unsigned struct_size);
365 * ttm_bo_init_reserved
367 * @bdev: Pointer to a ttm_bo_device struct.
368 * @bo: Pointer to a ttm_buffer_object to be initialized.
369 * @size: Requested size of buffer object.
370 * @type: Requested type of buffer object.
371 * @flags: Initial placement flags.
372 * @page_alignment: Data alignment in pages.
373 * @ctx: TTM operation context for memory allocation.
374 * @acc_size: Accounted size for this object.
375 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
376 * @destroy: Destroy function. Use NULL for kfree().
378 * This function initializes a pre-allocated struct ttm_buffer_object.
379 * As this object may be part of a larger structure, this function,
380 * together with the @destroy function,
381 * enables driver-specific objects derived from a ttm_buffer_object.
383 * On successful return, the caller owns an object kref to @bo. The kref and
384 * list_kref are usually set to 1, but note that in some situations, other
385 * tasks may already be holding references to @bo as well.
386 * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
387 * and it is the caller's responsibility to call ttm_bo_unreserve.
389 * If a failure occurs, the function will call the @destroy function, or
390 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
391 * illegal and will likely cause memory corruption.
394 * -ENOMEM: Out of memory.
395 * -EINVAL: Invalid placement flags.
396 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
399 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
400 struct ttm_buffer_object *bo,
401 size_t size, enum ttm_bo_type type,
402 struct ttm_placement *placement,
403 uint32_t page_alignment,
404 struct ttm_operation_ctx *ctx,
405 size_t acc_size, struct sg_table *sg,
406 struct dma_resv *resv,
407 void (*destroy) (struct ttm_buffer_object *));
412 * @bdev: Pointer to a ttm_bo_device struct.
413 * @bo: Pointer to a ttm_buffer_object to be initialized.
414 * @size: Requested size of buffer object.
415 * @type: Requested type of buffer object.
416 * @flags: Initial placement flags.
417 * @page_alignment: Data alignment in pages.
418 * @interruptible: If needing to sleep to wait for GPU resources,
419 * sleep interruptible.
420 * pinned in physical memory. If this behaviour is not desired, this member
421 * holds a pointer to a persistent shmem object. Typically, this would
422 * point to the shmem object backing a GEM object if TTM is used to back a
423 * GEM user interface.
424 * @acc_size: Accounted size for this object.
425 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
426 * @destroy: Destroy function. Use NULL for kfree().
428 * This function initializes a pre-allocated struct ttm_buffer_object.
429 * As this object may be part of a larger structure, this function,
430 * together with the @destroy function,
431 * enables driver-specific objects derived from a ttm_buffer_object.
433 * On successful return, the caller owns an object kref to @bo. The kref and
434 * list_kref are usually set to 1, but note that in some situations, other
435 * tasks may already be holding references to @bo as well.
437 * If a failure occurs, the function will call the @destroy function, or
438 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
439 * illegal and will likely cause memory corruption.
442 * -ENOMEM: Out of memory.
443 * -EINVAL: Invalid placement flags.
444 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
446 int ttm_bo_init(struct ttm_bo_device *bdev, struct ttm_buffer_object *bo,
447 size_t size, enum ttm_bo_type type,
448 struct ttm_placement *placement,
449 uint32_t page_alignment, bool interrubtible, size_t acc_size,
450 struct sg_table *sg, struct dma_resv *resv,
451 void (*destroy) (struct ttm_buffer_object *));
454 * ttm_kmap_obj_virtual
456 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
457 * @is_iomem: Pointer to an integer that on return indicates 1 if the
458 * virtual map is io memory, 0 if normal memory.
460 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
461 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
462 * that should strictly be accessed by the iowriteXX() and similar functions.
464 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
467 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
474 * @bo: The buffer object.
475 * @start_page: The first page to map.
476 * @num_pages: Number of pages to map.
477 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
479 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
480 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
481 * used to obtain a virtual address to the data.
484 * -ENOMEM: Out of memory.
485 * -EINVAL: Invalid range.
487 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
488 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
493 * @map: Object describing the map to unmap.
495 * Unmaps a kernel map set up by ttm_bo_kmap.
497 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
502 * @bo: The buffer object.
503 * @map: pointer to a struct dma_buf_map representing the map.
505 * Sets up a kernel virtual mapping, using ioremap or vmap to the
506 * data in the buffer object. The parameter @map returns the virtual
507 * address as struct dma_buf_map. Unmap the buffer with ttm_bo_vunmap().
510 * -ENOMEM: Out of memory.
511 * -EINVAL: Invalid range.
513 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
518 * @bo: The buffer object.
519 * @map: Object describing the map to unmap.
521 * Unmaps a kernel map set up by ttm_bo_vmap().
523 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
526 * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
528 * @vma: vma as input from the fbdev mmap method.
529 * @bo: The bo backing the address space.
531 * Maps a buffer object.
533 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
536 * ttm_bo_mmap - mmap out of the ttm device address space.
538 * @filp: filp as input from the mmap method.
539 * @vma: vma as input from the mmap method.
540 * @bdev: Pointer to the ttm_bo_device with the address space manager.
542 * This function is intended to be called by the device mmap method.
543 * if the device address space is to be backed by the bo manager.
545 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
546 struct ttm_bo_device *bdev);
551 * @bdev: Pointer to the struct ttm_bo_device.
552 * @filp: Pointer to the struct file attempting to read / write.
553 * @wbuf: User-space pointer to address of buffer to write. NULL on read.
554 * @rbuf: User-space pointer to address of buffer to read into.
556 * @count: Number of bytes to read / write.
557 * @f_pos: Pointer to current file position.
558 * @write: 1 for read, 0 for write.
560 * This function implements read / write into ttm buffer objects, and is
562 * be called from the fops::read and fops::write method.
564 * See man (2) write, man(2) read. In particular,
565 * the function may return -ERESTARTSYS if
566 * interrupted by a signal.
568 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
569 const char __user *wbuf, char __user *rbuf,
570 size_t count, loff_t *f_pos, bool write);
572 int ttm_bo_swapout(struct ttm_operation_ctx *ctx);
575 * ttm_bo_uses_embedded_gem_object - check if the given bo uses the
576 * embedded drm_gem_object.
578 * Most ttm drivers are using gem too, so the embedded
579 * ttm_buffer_object.base will be initialized by the driver (before
580 * calling ttm_bo_init). It is also possible to use ttm without gem
581 * though (vmwgfx does that).
583 * This helper will figure whenever a given ttm bo is a gem object too
586 * @bo: The bo to check.
588 static inline bool ttm_bo_uses_embedded_gem_object(struct ttm_buffer_object *bo)
590 return bo->base.dev != NULL;
594 * ttm_bo_pin - Pin the buffer object.
595 * @bo: The buffer object to pin
597 * Make sure the buffer is not evicted any more during memory pressure.
599 static inline void ttm_bo_pin(struct ttm_buffer_object *bo)
601 dma_resv_assert_held(bo->base.resv);
602 WARN_ON_ONCE(!kref_read(&bo->kref));
607 * ttm_bo_unpin - Unpin the buffer object.
608 * @bo: The buffer object to unpin
610 * Allows the buffer object to be evicted again during memory pressure.
612 static inline void ttm_bo_unpin(struct ttm_buffer_object *bo)
614 dma_resv_assert_held(bo->base.resv);
615 WARN_ON_ONCE(!bo->pin_count);
616 WARN_ON_ONCE(!kref_read(&bo->kref));
620 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
621 struct ttm_resource_manager *man,
622 const struct ttm_place *place,
623 struct ttm_operation_ctx *ctx,
624 struct ww_acquire_ctx *ticket);
626 /* Default number of pre-faulted pages in the TTM fault handler */
627 #define TTM_BO_VM_NUM_PREFAULT 16
629 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
630 struct vm_fault *vmf);
632 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
634 pgoff_t num_prefault,
635 pgoff_t fault_page_size);
637 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
639 void ttm_bo_vm_open(struct vm_area_struct *vma);
641 void ttm_bo_vm_close(struct vm_area_struct *vma);
643 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
644 void *buf, int len, int write);