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_hashtab.h"
35 #include <linux/kref.h>
36 #include <linux/list.h>
37 #include <linux/wait.h>
38 #include <linux/mutex.h>
40 #include <linux/rbtree.h>
41 #include <linux/bitmap.h>
49 * struct ttm_placement
51 * @fpfn: first valid page frame number to put the object
52 * @lpfn: last valid page frame number to put the object
53 * @num_placement: number of prefered placements
54 * @placement: prefered placements
55 * @num_busy_placement: number of prefered placements when need to evict buffer
56 * @busy_placement: prefered placements when need to evict buffer
58 * Structure indicating the placement you request for an object.
60 struct ttm_placement {
63 unsigned num_placement;
64 const uint32_t *placement;
65 unsigned num_busy_placement;
66 const uint32_t *busy_placement;
70 * struct ttm_bus_placement
72 * @addr: mapped virtual address
73 * @base: bus base address
74 * @is_iomem: is this io memory ?
76 * @offset: offset from the base address
78 * Structure indicating the bus placement of an object.
80 struct ttm_bus_placement {
93 * @mm_node: Memory manager node.
94 * @size: Requested size of memory region.
95 * @num_pages: Actual size of memory region in pages.
96 * @page_alignment: Page alignment.
97 * @placement: Placement flags.
98 * @bus: Placement on io bus accessible to the CPU
100 * Structure indicating the placement and space resources used by a
108 unsigned long num_pages;
109 uint32_t page_alignment;
112 struct ttm_bus_placement bus;
118 * @ttm_bo_type_device: These are 'normal' buffers that can
119 * be mmapped by user space. Each of these bos occupy a slot in the
120 * device address space, that can be used for normal vm operations.
122 * @ttm_bo_type_user: These are user-space memory areas that are made
123 * available to the GPU by mapping the buffer pages into the GPU aperture
124 * space. These buffers cannot be mmaped from the device address space.
126 * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
127 * but they cannot be accessed from user-space. For kernel-only use.
139 * struct ttm_buffer_object
141 * @bdev: Pointer to the buffer object device structure.
142 * @buffer_start: The virtual user-space start address of ttm_bo_type_user
144 * @type: The bo type.
145 * @destroy: Destruction function. If NULL, kfree is used.
146 * @num_pages: Actual number of pages.
147 * @addr_space_offset: Address space offset.
148 * @acc_size: Accounted size for this object.
149 * @kref: Reference count of this buffer object. When this refcount reaches
150 * zero, the object is put on the delayed delete list.
151 * @list_kref: List reference count of this buffer object. This member is
152 * used to avoid destruction while the buffer object is still on a list.
153 * Lru lists may keep one refcount, the delayed delete list, and kref != 0
154 * keeps one refcount. When this refcount reaches zero,
155 * the object is destroyed.
156 * @event_queue: Queue for processes waiting on buffer object status change.
157 * @lock: spinlock protecting mostly synchronization members.
158 * @mem: structure describing current placement.
159 * @persistant_swap_storage: Usually the swap storage is deleted for buffers
160 * pinned in physical memory. If this behaviour is not desired, this member
161 * holds a pointer to a persistant shmem object.
162 * @ttm: TTM structure holding system pages.
163 * @evicted: Whether the object was evicted without user-space knowing.
164 * @cpu_writes: For synchronization. Number of cpu writers.
165 * @lru: List head for the lru list.
166 * @ddestroy: List head for the delayed destroy list.
167 * @swap: List head for swap LRU list.
168 * @val_seq: Sequence of the validation holding the @reserved lock.
169 * Used to avoid starvation when many processes compete to validate the
170 * buffer. This member is protected by the bo_device::lru_lock.
171 * @seq_valid: The value of @val_seq is valid. This value is protected by
172 * the bo_device::lru_lock.
173 * @reserved: Deadlock-free lock used for synchronization state transitions.
174 * @sync_obj_arg: Opaque argument to synchronization object function.
175 * @sync_obj: Pointer to a synchronization object.
176 * @priv_flags: Flags describing buffer object internal state.
177 * @vm_rb: Rb node for the vm rb tree.
178 * @vm_node: Address space manager node.
179 * @offset: The current GPU offset, which can have different meanings
180 * depending on the memory type. For SYSTEM type memory, it should be 0.
181 * @cur_placement: Hint of current placement.
183 * Base class for TTM buffer object, that deals with data placement and CPU
184 * mappings. GPU mappings are really up to the driver, but for simpler GPUs
185 * the driver can usually use the placement offset @offset directly as the
186 * GPU virtual address. For drivers implementing multiple
187 * GPU memory manager contexts, the driver should manage the address space
188 * in these contexts separately and use these objects to get the correct
189 * placement and caching for these GPU maps. This makes it possible to use
190 * these objects for even quite elaborate memory management schemes.
191 * The destroy member, the API visibility of this object makes it possible
192 * to derive driver specific types.
195 struct ttm_buffer_object {
197 * Members constant at init.
200 struct ttm_bo_global *glob;
201 struct ttm_bo_device *bdev;
202 unsigned long buffer_start;
203 enum ttm_bo_type type;
204 void (*destroy) (struct ttm_buffer_object *);
205 unsigned long num_pages;
206 uint64_t addr_space_offset;
210 * Members not needing protection.
214 struct kref list_kref;
215 wait_queue_head_t event_queue;
219 * Members protected by the bo::reserved lock.
222 struct ttm_mem_reg mem;
223 struct file *persistant_swap_storage;
228 * Members protected by the bo::reserved lock only when written to.
231 atomic_t cpu_writers;
234 * Members protected by the bdev::lru_lock.
237 struct list_head lru;
238 struct list_head ddestroy;
239 struct list_head swap;
244 * Members protected by the bdev::lru_lock
245 * only when written to.
251 * Members protected by the bo::lock
252 * In addition, setting sync_obj to anything else
253 * than NULL requires bo::reserved to be held. This allows for
254 * checking NULL while reserved but not holding bo::lock.
259 unsigned long priv_flags;
262 * Members protected by the bdev::vm_lock
265 struct rb_node vm_rb;
266 struct drm_mm_node *vm_node;
270 * Special members that are protected by the reserve lock
271 * and the bo::lock when written to. Can be read with
272 * either of these locks held.
275 unsigned long offset;
276 uint32_t cur_placement;
280 * struct ttm_bo_kmap_obj
282 * @virtual: The current kernel virtual address.
283 * @page: The page when kmap'ing a single page.
284 * @bo_kmap_type: Type of bo_kmap.
286 * Object describing a kernel mapping. Since a TTM bo may be located
287 * in various memory types with various caching policies, the
288 * mapping can either be an ioremap, a vmap, a kmap or part of a
292 #define TTM_BO_MAP_IOMEM_MASK 0x80
293 struct ttm_bo_kmap_obj {
297 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
300 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
302 struct ttm_buffer_object *bo;
306 * ttm_bo_reference - reference a struct ttm_buffer_object
308 * @bo: The buffer object.
310 * Returns a refcounted pointer to a buffer object.
313 static inline struct ttm_buffer_object *
314 ttm_bo_reference(struct ttm_buffer_object *bo)
321 * ttm_bo_wait - wait for buffer idle.
323 * @bo: The buffer object.
324 * @interruptible: Use interruptible wait.
325 * @no_wait: Return immediately if buffer is busy.
327 * This function must be called with the bo::mutex held, and makes
328 * sure any previous rendering to the buffer is completed.
329 * Note: It might be necessary to block validations before the
330 * wait by reserving the buffer.
331 * Returns -EBUSY if no_wait is true and the buffer is busy.
332 * Returns -ERESTARTSYS if interrupted by a signal.
334 extern int ttm_bo_wait(struct ttm_buffer_object *bo, bool lazy,
335 bool interruptible, bool no_wait);
339 * @bo: The buffer object.
340 * @placement: Proposed placement for the buffer object.
341 * @interruptible: Sleep interruptible if sleeping.
342 * @no_wait_reserve: Return immediately if other buffers are busy.
343 * @no_wait_gpu: Return immediately if the GPU is busy.
345 * Changes placement and caching policy of the buffer object
346 * according proposed placement.
348 * -EINVAL on invalid proposed placement.
349 * -ENOMEM on out-of-memory condition.
350 * -EBUSY if no_wait is true and buffer busy.
351 * -ERESTARTSYS if interrupted by a signal.
353 extern int ttm_bo_validate(struct ttm_buffer_object *bo,
354 struct ttm_placement *placement,
355 bool interruptible, bool no_wait_reserve,
361 * @bo: The buffer object.
363 * Unreference and clear a pointer to a buffer object.
365 extern void ttm_bo_unref(struct ttm_buffer_object **bo);
368 * ttm_bo_lock_delayed_workqueue
370 * Prevent the delayed workqueue from running.
372 * True if the workqueue was queued at the time
374 extern int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
377 * ttm_bo_unlock_delayed_workqueue
379 * Allows the delayed workqueue to run.
381 extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev,
385 * ttm_bo_synccpu_write_grab
387 * @bo: The buffer object:
388 * @no_wait: Return immediately if buffer is busy.
390 * Synchronizes a buffer object for CPU RW access. This means
391 * blocking command submission that affects the buffer and
392 * waiting for buffer idle. This lock is recursive.
394 * -EBUSY if the buffer is busy and no_wait is true.
395 * -ERESTARTSYS if interrupted by a signal.
399 ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
401 * ttm_bo_synccpu_write_release:
403 * @bo : The buffer object.
405 * Releases a synccpu lock.
407 extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);
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 * @buffer_start: Virtual address of user space data backing a
419 * user buffer object.
420 * @interruptible: If needing to sleep to wait for GPU resources,
421 * sleep interruptible.
422 * @persistant_swap_storage: Usually the swap storage is deleted for buffers
423 * pinned in physical memory. If this behaviour is not desired, this member
424 * holds a pointer to a persistant shmem object. Typically, this would
425 * point to the shmem object backing a GEM object if TTM is used to back a
426 * GEM user interface.
427 * @acc_size: Accounted size for this object.
428 * @destroy: Destroy function. Use NULL for kfree().
430 * This function initializes a pre-allocated struct ttm_buffer_object.
431 * As this object may be part of a larger structure, this function,
432 * together with the @destroy function,
433 * enables driver-specific objects derived from a ttm_buffer_object.
434 * On successful return, the object kref and list_kref are set to 1.
435 * If a failure occurs, the function will call the @destroy function, or
436 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
437 * illegal and will likely cause memory corruption.
440 * -ENOMEM: Out of memory.
441 * -EINVAL: Invalid placement flags.
442 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
445 extern int ttm_bo_init(struct ttm_bo_device *bdev,
446 struct ttm_buffer_object *bo,
448 enum ttm_bo_type type,
449 struct ttm_placement *placement,
450 uint32_t page_alignment,
451 unsigned long buffer_start,
453 struct file *persistant_swap_storage,
455 void (*destroy) (struct ttm_buffer_object *));
457 * ttm_bo_synccpu_object_init
459 * @bdev: Pointer to a ttm_bo_device struct.
460 * @bo: Pointer to a ttm_buffer_object to be initialized.
461 * @size: Requested size of buffer object.
462 * @type: Requested type of buffer object.
463 * @flags: Initial placement flags.
464 * @page_alignment: Data alignment in pages.
465 * @buffer_start: Virtual address of user space data backing a
466 * user buffer object.
467 * @interruptible: If needing to sleep while waiting for GPU resources,
468 * sleep interruptible.
469 * @persistant_swap_storage: Usually the swap storage is deleted for buffers
470 * pinned in physical memory. If this behaviour is not desired, this member
471 * holds a pointer to a persistant shmem object. Typically, this would
472 * point to the shmem object backing a GEM object if TTM is used to back a
473 * GEM user interface.
474 * @p_bo: On successful completion *p_bo points to the created object.
476 * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
477 * on that object. The destroy function is set to kfree().
479 * -ENOMEM: Out of memory.
480 * -EINVAL: Invalid placement flags.
481 * -ERESTARTSYS: Interrupted by signal while waiting for resources.
484 extern int ttm_bo_create(struct ttm_bo_device *bdev,
486 enum ttm_bo_type type,
487 struct ttm_placement *placement,
488 uint32_t page_alignment,
489 unsigned long buffer_start,
491 struct file *persistant_swap_storage,
492 struct ttm_buffer_object **p_bo);
495 * ttm_bo_check_placement
497 * @bo: the buffer object.
498 * @placement: placements
500 * Performs minimal validity checking on an intended change of
503 * -EINVAL: Intended change is invalid or not allowed.
505 extern int ttm_bo_check_placement(struct ttm_buffer_object *bo,
506 struct ttm_placement *placement);
511 * @bdev: Pointer to a ttm_bo_device struct.
512 * @mem_type: The memory type.
513 * @p_size: size managed area in pages.
515 * Initialize a manager for a given memory type.
516 * Note: if part of driver firstopen, it must be protected from a
517 * potentially racing lastclose.
519 * -EINVAL: invalid size or memory type.
520 * -ENOMEM: Not enough memory.
521 * May also return driver-specified errors.
524 extern int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
525 unsigned long p_size);
529 * @bdev: Pointer to a ttm_bo_device struct.
530 * @mem_type: The memory type.
532 * Take down a manager for a given memory type after first walking
533 * the LRU list to evict any buffers left alive.
535 * Normally, this function is part of lastclose() or unload(), and at that
536 * point there shouldn't be any buffers left created by user-space, since
537 * there should've been removed by the file descriptor release() method.
538 * However, before this function is run, make sure to signal all sync objects,
539 * and verify that the delayed delete queue is empty. The driver must also
540 * make sure that there are no NO_EVICT buffers present in this memory type
541 * when the call is made.
543 * If this function is part of a VT switch, the caller must make sure that
544 * there are no appications currently validating buffers before this
545 * function is called. The caller can do that by first taking the
546 * struct ttm_bo_device::ttm_lock in write mode.
549 * -EINVAL: invalid or uninitialized memory type.
550 * -EBUSY: There are still buffers left in this memory type.
553 extern int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type);
558 * @bdev: Pointer to a ttm_bo_device struct.
559 * @mem_type: The memory type.
561 * Evicts all buffers on the lru list of the memory type.
562 * This is normally part of a VT switch or an
563 * out-of-memory-space-due-to-fragmentation handler.
564 * The caller must make sure that there are no other processes
565 * currently validating buffers, and can do that by taking the
566 * struct ttm_bo_device::ttm_lock in write mode.
569 * -EINVAL: Invalid or uninitialized memory type.
570 * -ERESTARTSYS: The call was interrupted by a signal while waiting to
574 extern int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
577 * ttm_kmap_obj_virtual
579 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
580 * @is_iomem: Pointer to an integer that on return indicates 1 if the
581 * virtual map is io memory, 0 if normal memory.
583 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
584 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
585 * that should strictly be accessed by the iowriteXX() and similar functions.
588 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
591 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
598 * @bo: The buffer object.
599 * @start_page: The first page to map.
600 * @num_pages: Number of pages to map.
601 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
603 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
604 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
605 * used to obtain a virtual address to the data.
608 * -ENOMEM: Out of memory.
609 * -EINVAL: Invalid range.
612 extern int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
613 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
618 * @map: Object describing the map to unmap.
620 * Unmaps a kernel map set up by ttm_bo_kmap.
623 extern void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
629 * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object.
631 * @vma: vma as input from the fbdev mmap method.
632 * @bo: The bo backing the address space. The address space will
633 * have the same size as the bo, and start at offset 0.
635 * This function is intended to be called by the fbdev mmap method
636 * if the fbdev address space is to be backed by a bo.
639 extern int ttm_fbdev_mmap(struct vm_area_struct *vma,
640 struct ttm_buffer_object *bo);
643 * ttm_bo_mmap - mmap out of the ttm device address space.
645 * @filp: filp as input from the mmap method.
646 * @vma: vma as input from the mmap method.
647 * @bdev: Pointer to the ttm_bo_device with the address space manager.
649 * This function is intended to be called by the device mmap method.
650 * if the device address space is to be backed by the bo manager.
653 extern int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
654 struct ttm_bo_device *bdev);
659 * @bdev: Pointer to the struct ttm_bo_device.
660 * @filp: Pointer to the struct file attempting to read / write.
661 * @wbuf: User-space pointer to address of buffer to write. NULL on read.
662 * @rbuf: User-space pointer to address of buffer to read into.
664 * @count: Number of bytes to read / write.
665 * @f_pos: Pointer to current file position.
666 * @write: 1 for read, 0 for write.
668 * This function implements read / write into ttm buffer objects, and is
670 * be called from the fops::read and fops::write method.
672 * See man (2) write, man(2) read. In particular,
673 * the function may return -ERESTARTSYS if
674 * interrupted by a signal.
677 extern ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
678 const char __user *wbuf, char __user *rbuf,
679 size_t count, loff_t *f_pos, bool write);
681 extern void ttm_bo_swapout_all(struct ttm_bo_device *bdev);