Merge tag 'iommu-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux
[linux/fpc-iii.git] / include / drm / ttm / ttm_bo_api.h
blob2564e66e67d747cfb07bb6b0b5e3cd4a6f4fe7f6
1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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
16 * of the Software.
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>
41 #include <linux/mm.h>
42 #include <linux/bitmap.h>
43 #include <linux/dma-resv.h>
45 #include "ttm_resource.h"
47 struct ttm_bo_global;
49 struct ttm_bo_device;
51 struct dma_buf_map;
53 struct drm_mm_node;
55 struct ttm_placement;
57 struct ttm_place;
59 struct ttm_lru_bulk_move;
61 /**
62 * enum ttm_bo_type
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
72 * driver.
75 enum ttm_bo_type {
76 ttm_bo_type_device,
77 ttm_bo_type_kernel,
78 ttm_bo_type_sg
81 struct ttm_tt;
83 /**
84 * struct ttm_buffer_object
86 * @base: drm_gem_object superclass data.
87 * @bdev: Pointer to the buffer object device structure.
88 * @type: The bo type.
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 *);
128 unsigned long num_pages;
129 size_t acc_size;
132 * Members not needing protection.
134 struct kref kref;
137 * Members protected by the bo::resv::reserved lock.
140 struct ttm_resource mem;
141 struct ttm_tt *ttm;
142 bool deleted;
145 * Members protected by the bdev::lru_lock.
148 struct list_head lru;
149 struct list_head ddestroy;
150 struct list_head swap;
153 * Members protected by a bo reservation.
156 struct dma_fence *moving;
157 unsigned priority;
158 unsigned pin_count;
161 * Special members that are protected by the reserve lock
162 * and the bo::lock when written to. Can be read with
163 * either of these locks held.
166 struct sg_table *sg;
170 * struct ttm_bo_kmap_obj
172 * @virtual: The current kernel virtual address.
173 * @page: The page when kmap'ing a single page.
174 * @bo_kmap_type: Type of bo_kmap.
176 * Object describing a kernel mapping. Since a TTM bo may be located
177 * in various memory types with various caching policies, the
178 * mapping can either be an ioremap, a vmap, a kmap or part of a
179 * premapped region.
182 #define TTM_BO_MAP_IOMEM_MASK 0x80
183 struct ttm_bo_kmap_obj {
184 void *virtual;
185 struct page *page;
186 enum {
187 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
188 ttm_bo_map_vmap = 2,
189 ttm_bo_map_kmap = 3,
190 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
191 } bo_kmap_type;
192 struct ttm_buffer_object *bo;
196 * struct ttm_operation_ctx
198 * @interruptible: Sleep interruptible if sleeping.
199 * @no_wait_gpu: Return immediately if the GPU is busy.
200 * @gfp_retry_mayfail: Set the __GFP_RETRY_MAYFAIL when allocation pages.
201 * @allow_res_evict: Allow eviction of reserved BOs. Can be used when multiple
202 * BOs share the same reservation object.
203 * @force_alloc: Don't check the memory account during suspend or CPU page
204 * faults. Should only be used by TTM internally.
205 * @resv: Reservation object to allow reserved evictions with.
207 * Context for TTM operations like changing buffer placement or general memory
208 * allocation.
210 struct ttm_operation_ctx {
211 bool interruptible;
212 bool no_wait_gpu;
213 bool gfp_retry_mayfail;
214 bool allow_res_evict;
215 bool force_alloc;
216 struct dma_resv *resv;
217 uint64_t bytes_moved;
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)
227 kref_get(&bo->kref);
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))
244 return NULL;
245 return bo;
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);
282 * ttm_bo_validate
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.
290 * Returns
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);
301 * ttm_bo_put
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.
336 * Returns
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.
392 * Returns
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,
400 unsigned long size,
401 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,
406 struct sg_table *sg,
407 struct dma_resv *resv,
408 void (*destroy) (struct ttm_buffer_object *));
411 * ttm_bo_init
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.
442 * Returns
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 *));
455 * ttm_kmap_obj_virtual
457 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
458 * @is_iomem: Pointer to an integer that on return indicates 1 if the
459 * virtual map is io memory, 0 if normal memory.
461 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
462 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
463 * that should strictly be accessed by the iowriteXX() and similar functions.
465 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
466 bool *is_iomem)
468 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
469 return map->virtual;
473 * ttm_bo_kmap
475 * @bo: The buffer object.
476 * @start_page: The first page to map.
477 * @num_pages: Number of pages to map.
478 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
480 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
481 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
482 * used to obtain a virtual address to the data.
484 * Returns
485 * -ENOMEM: Out of memory.
486 * -EINVAL: Invalid range.
488 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
489 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
492 * ttm_bo_kunmap
494 * @map: Object describing the map to unmap.
496 * Unmaps a kernel map set up by ttm_bo_kmap.
498 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
501 * ttm_bo_vmap
503 * @bo: The buffer object.
504 * @map: pointer to a struct dma_buf_map representing the map.
506 * Sets up a kernel virtual mapping, using ioremap or vmap to the
507 * data in the buffer object. The parameter @map returns the virtual
508 * address as struct dma_buf_map. Unmap the buffer with ttm_bo_vunmap().
510 * Returns
511 * -ENOMEM: Out of memory.
512 * -EINVAL: Invalid range.
514 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
517 * ttm_bo_vunmap
519 * @bo: The buffer object.
520 * @map: Object describing the map to unmap.
522 * Unmaps a kernel map set up by ttm_bo_vmap().
524 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
527 * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
529 * @vma: vma as input from the fbdev mmap method.
530 * @bo: The bo backing the address space.
532 * Maps a buffer object.
534 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
537 * ttm_bo_mmap - mmap out of the ttm device address space.
539 * @filp: filp as input from the mmap method.
540 * @vma: vma as input from the mmap method.
541 * @bdev: Pointer to the ttm_bo_device with the address space manager.
543 * This function is intended to be called by the device mmap method.
544 * if the device address space is to be backed by the bo manager.
546 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
547 struct ttm_bo_device *bdev);
550 * ttm_bo_io
552 * @bdev: Pointer to the struct ttm_bo_device.
553 * @filp: Pointer to the struct file attempting to read / write.
554 * @wbuf: User-space pointer to address of buffer to write. NULL on read.
555 * @rbuf: User-space pointer to address of buffer to read into.
556 * Null on write.
557 * @count: Number of bytes to read / write.
558 * @f_pos: Pointer to current file position.
559 * @write: 1 for read, 0 for write.
561 * This function implements read / write into ttm buffer objects, and is
562 * intended to
563 * be called from the fops::read and fops::write method.
564 * Returns:
565 * See man (2) write, man(2) read. In particular,
566 * the function may return -ERESTARTSYS if
567 * interrupted by a signal.
569 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
570 const char __user *wbuf, char __user *rbuf,
571 size_t count, loff_t *f_pos, bool write);
573 int ttm_bo_swapout(struct ttm_operation_ctx *ctx);
576 * ttm_bo_uses_embedded_gem_object - check if the given bo uses the
577 * embedded drm_gem_object.
579 * Most ttm drivers are using gem too, so the embedded
580 * ttm_buffer_object.base will be initialized by the driver (before
581 * calling ttm_bo_init). It is also possible to use ttm without gem
582 * though (vmwgfx does that).
584 * This helper will figure whenever a given ttm bo is a gem object too
585 * or not.
587 * @bo: The bo to check.
589 static inline bool ttm_bo_uses_embedded_gem_object(struct ttm_buffer_object *bo)
591 return bo->base.dev != NULL;
595 * ttm_bo_pin - Pin the buffer object.
596 * @bo: The buffer object to pin
598 * Make sure the buffer is not evicted any more during memory pressure.
600 static inline void ttm_bo_pin(struct ttm_buffer_object *bo)
602 dma_resv_assert_held(bo->base.resv);
603 ++bo->pin_count;
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 --bo->pin_count;
619 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
620 struct ttm_resource_manager *man,
621 const struct ttm_place *place,
622 struct ttm_operation_ctx *ctx,
623 struct ww_acquire_ctx *ticket);
625 /* Default number of pre-faulted pages in the TTM fault handler */
626 #define TTM_BO_VM_NUM_PREFAULT 16
628 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
629 struct vm_fault *vmf);
631 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
632 pgprot_t prot,
633 pgoff_t num_prefault,
634 pgoff_t fault_page_size);
636 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
638 void ttm_bo_vm_open(struct vm_area_struct *vma);
640 void ttm_bo_vm_close(struct vm_area_struct *vma);
642 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
643 void *buf, int len, int write);
645 #endif