drm/ttm: Add a bo list reserve fastpath (v2)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / drm / ttm / ttm_bo_api.h
blobb0fc9c12554be0cd8e443b55b4761654960deae0
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_hashtab.h"
35 #include <linux/kref.h>
36 #include <linux/list.h>
37 #include <linux/wait.h>
38 #include <linux/mutex.h>
39 #include <linux/mm.h>
40 #include <linux/rbtree.h>
41 #include <linux/bitmap.h>
43 struct ttm_bo_device;
45 struct drm_mm_node;
48 /**
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 {
61 unsigned fpfn;
62 unsigned lpfn;
63 unsigned num_placement;
64 const uint32_t *placement;
65 unsigned num_busy_placement;
66 const uint32_t *busy_placement;
69 /**
70 * struct ttm_bus_placement
72 * @addr: mapped virtual address
73 * @base: bus base address
74 * @is_iomem: is this io memory ?
75 * @size: size in byte
76 * @offset: offset from the base address
78 * Structure indicating the bus placement of an object.
80 struct ttm_bus_placement {
81 void *addr;
82 unsigned long base;
83 unsigned long size;
84 unsigned long offset;
85 bool is_iomem;
86 bool io_reserved;
90 /**
91 * struct ttm_mem_reg
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
101 * buffer object.
104 struct ttm_mem_reg {
105 void *mm_node;
106 unsigned long start;
107 unsigned long size;
108 unsigned long num_pages;
109 uint32_t page_alignment;
110 uint32_t mem_type;
111 uint32_t placement;
112 struct ttm_bus_placement bus;
116 * enum ttm_bo_type
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.
130 enum ttm_bo_type {
131 ttm_bo_type_device,
132 ttm_bo_type_user,
133 ttm_bo_type_kernel
136 struct ttm_tt;
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
143 * buffers.
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;
207 size_t acc_size;
210 * Members not needing protection.
213 struct kref kref;
214 struct kref list_kref;
215 wait_queue_head_t event_queue;
216 spinlock_t lock;
219 * Members protected by the bo::reserved lock.
222 struct ttm_mem_reg mem;
223 struct file *persistant_swap_storage;
224 struct ttm_tt *ttm;
225 bool evicted;
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;
240 uint32_t val_seq;
241 bool seq_valid;
244 * Members protected by the bdev::lru_lock
245 * only when written to.
248 atomic_t reserved;
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.
257 void *sync_obj_arg;
258 void *sync_obj;
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
289 * premapped region.
292 #define TTM_BO_MAP_IOMEM_MASK 0x80
293 struct ttm_bo_kmap_obj {
294 void *virtual;
295 struct page *page;
296 enum {
297 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
298 ttm_bo_map_vmap = 2,
299 ttm_bo_map_kmap = 3,
300 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
301 } bo_kmap_type;
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)
316 kref_get(&bo->kref);
317 return 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);
337 * ttm_bo_validate
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.
347 * Returns
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,
356 bool no_wait_gpu);
359 * ttm_bo_unref
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);
369 * ttm_bo_list_ref_sub
371 * @bo: The buffer object.
372 * @count: The number of references with which to decrease @bo::list_kref;
373 * @never_free: The refcount should not reach zero with this operation.
375 * Release @count lru list references to this buffer object.
377 extern void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
378 bool never_free);
381 * ttm_bo_add_to_lru
383 * @bo: The buffer object.
385 * Add this bo to the relevant mem type lru and, if it's backed by
386 * system pages (ttms) to the swap list.
387 * This function must be called with struct ttm_bo_global::lru_lock held, and
388 * is typically called immediately prior to unreserving a bo.
390 extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
393 * ttm_bo_del_from_lru
395 * @bo: The buffer object.
397 * Remove this bo from all lru lists used to lookup and reserve an object.
398 * This function must be called with struct ttm_bo_global::lru_lock held,
399 * and is usually called just immediately after the bo has been reserved to
400 * avoid recursive reservation from lru lists.
402 extern int ttm_bo_del_from_lru(struct ttm_buffer_object *bo);
406 * ttm_bo_lock_delayed_workqueue
408 * Prevent the delayed workqueue from running.
409 * Returns
410 * True if the workqueue was queued at the time
412 extern int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
415 * ttm_bo_unlock_delayed_workqueue
417 * Allows the delayed workqueue to run.
419 extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev,
420 int resched);
423 * ttm_bo_synccpu_write_grab
425 * @bo: The buffer object:
426 * @no_wait: Return immediately if buffer is busy.
428 * Synchronizes a buffer object for CPU RW access. This means
429 * blocking command submission that affects the buffer and
430 * waiting for buffer idle. This lock is recursive.
431 * Returns
432 * -EBUSY if the buffer is busy and no_wait is true.
433 * -ERESTARTSYS if interrupted by a signal.
436 extern int
437 ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
439 * ttm_bo_synccpu_write_release:
441 * @bo : The buffer object.
443 * Releases a synccpu lock.
445 extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);
448 * ttm_bo_init
450 * @bdev: Pointer to a ttm_bo_device struct.
451 * @bo: Pointer to a ttm_buffer_object to be initialized.
452 * @size: Requested size of buffer object.
453 * @type: Requested type of buffer object.
454 * @flags: Initial placement flags.
455 * @page_alignment: Data alignment in pages.
456 * @buffer_start: Virtual address of user space data backing a
457 * user buffer object.
458 * @interruptible: If needing to sleep to wait for GPU resources,
459 * sleep interruptible.
460 * @persistant_swap_storage: Usually the swap storage is deleted for buffers
461 * pinned in physical memory. If this behaviour is not desired, this member
462 * holds a pointer to a persistant shmem object. Typically, this would
463 * point to the shmem object backing a GEM object if TTM is used to back a
464 * GEM user interface.
465 * @acc_size: Accounted size for this object.
466 * @destroy: Destroy function. Use NULL for kfree().
468 * This function initializes a pre-allocated struct ttm_buffer_object.
469 * As this object may be part of a larger structure, this function,
470 * together with the @destroy function,
471 * enables driver-specific objects derived from a ttm_buffer_object.
472 * On successful return, the object kref and list_kref are set to 1.
473 * If a failure occurs, the function will call the @destroy function, or
474 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
475 * illegal and will likely cause memory corruption.
477 * Returns
478 * -ENOMEM: Out of memory.
479 * -EINVAL: Invalid placement flags.
480 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
483 extern int ttm_bo_init(struct ttm_bo_device *bdev,
484 struct ttm_buffer_object *bo,
485 unsigned long size,
486 enum ttm_bo_type type,
487 struct ttm_placement *placement,
488 uint32_t page_alignment,
489 unsigned long buffer_start,
490 bool interrubtible,
491 struct file *persistant_swap_storage,
492 size_t acc_size,
493 void (*destroy) (struct ttm_buffer_object *));
495 * ttm_bo_synccpu_object_init
497 * @bdev: Pointer to a ttm_bo_device struct.
498 * @bo: Pointer to a ttm_buffer_object to be initialized.
499 * @size: Requested size of buffer object.
500 * @type: Requested type of buffer object.
501 * @flags: Initial placement flags.
502 * @page_alignment: Data alignment in pages.
503 * @buffer_start: Virtual address of user space data backing a
504 * user buffer object.
505 * @interruptible: If needing to sleep while waiting for GPU resources,
506 * sleep interruptible.
507 * @persistant_swap_storage: Usually the swap storage is deleted for buffers
508 * pinned in physical memory. If this behaviour is not desired, this member
509 * holds a pointer to a persistant shmem object. Typically, this would
510 * point to the shmem object backing a GEM object if TTM is used to back a
511 * GEM user interface.
512 * @p_bo: On successful completion *p_bo points to the created object.
514 * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
515 * on that object. The destroy function is set to kfree().
516 * Returns
517 * -ENOMEM: Out of memory.
518 * -EINVAL: Invalid placement flags.
519 * -ERESTARTSYS: Interrupted by signal while waiting for resources.
522 extern int ttm_bo_create(struct ttm_bo_device *bdev,
523 unsigned long size,
524 enum ttm_bo_type type,
525 struct ttm_placement *placement,
526 uint32_t page_alignment,
527 unsigned long buffer_start,
528 bool interruptible,
529 struct file *persistant_swap_storage,
530 struct ttm_buffer_object **p_bo);
533 * ttm_bo_check_placement
535 * @bo: the buffer object.
536 * @placement: placements
538 * Performs minimal validity checking on an intended change of
539 * placement flags.
540 * Returns
541 * -EINVAL: Intended change is invalid or not allowed.
543 extern int ttm_bo_check_placement(struct ttm_buffer_object *bo,
544 struct ttm_placement *placement);
547 * ttm_bo_init_mm
549 * @bdev: Pointer to a ttm_bo_device struct.
550 * @mem_type: The memory type.
551 * @p_size: size managed area in pages.
553 * Initialize a manager for a given memory type.
554 * Note: if part of driver firstopen, it must be protected from a
555 * potentially racing lastclose.
556 * Returns:
557 * -EINVAL: invalid size or memory type.
558 * -ENOMEM: Not enough memory.
559 * May also return driver-specified errors.
562 extern int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
563 unsigned long p_size);
565 * ttm_bo_clean_mm
567 * @bdev: Pointer to a ttm_bo_device struct.
568 * @mem_type: The memory type.
570 * Take down a manager for a given memory type after first walking
571 * the LRU list to evict any buffers left alive.
573 * Normally, this function is part of lastclose() or unload(), and at that
574 * point there shouldn't be any buffers left created by user-space, since
575 * there should've been removed by the file descriptor release() method.
576 * However, before this function is run, make sure to signal all sync objects,
577 * and verify that the delayed delete queue is empty. The driver must also
578 * make sure that there are no NO_EVICT buffers present in this memory type
579 * when the call is made.
581 * If this function is part of a VT switch, the caller must make sure that
582 * there are no appications currently validating buffers before this
583 * function is called. The caller can do that by first taking the
584 * struct ttm_bo_device::ttm_lock in write mode.
586 * Returns:
587 * -EINVAL: invalid or uninitialized memory type.
588 * -EBUSY: There are still buffers left in this memory type.
591 extern int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type);
594 * ttm_bo_evict_mm
596 * @bdev: Pointer to a ttm_bo_device struct.
597 * @mem_type: The memory type.
599 * Evicts all buffers on the lru list of the memory type.
600 * This is normally part of a VT switch or an
601 * out-of-memory-space-due-to-fragmentation handler.
602 * The caller must make sure that there are no other processes
603 * currently validating buffers, and can do that by taking the
604 * struct ttm_bo_device::ttm_lock in write mode.
606 * Returns:
607 * -EINVAL: Invalid or uninitialized memory type.
608 * -ERESTARTSYS: The call was interrupted by a signal while waiting to
609 * evict a buffer.
612 extern int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
615 * ttm_kmap_obj_virtual
617 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
618 * @is_iomem: Pointer to an integer that on return indicates 1 if the
619 * virtual map is io memory, 0 if normal memory.
621 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
622 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
623 * that should strictly be accessed by the iowriteXX() and similar functions.
626 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
627 bool *is_iomem)
629 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
630 return map->virtual;
634 * ttm_bo_kmap
636 * @bo: The buffer object.
637 * @start_page: The first page to map.
638 * @num_pages: Number of pages to map.
639 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
641 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
642 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
643 * used to obtain a virtual address to the data.
645 * Returns
646 * -ENOMEM: Out of memory.
647 * -EINVAL: Invalid range.
650 extern int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
651 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
654 * ttm_bo_kunmap
656 * @map: Object describing the map to unmap.
658 * Unmaps a kernel map set up by ttm_bo_kmap.
661 extern void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
663 #if 0
664 #endif
667 * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object.
669 * @vma: vma as input from the fbdev mmap method.
670 * @bo: The bo backing the address space. The address space will
671 * have the same size as the bo, and start at offset 0.
673 * This function is intended to be called by the fbdev mmap method
674 * if the fbdev address space is to be backed by a bo.
677 extern int ttm_fbdev_mmap(struct vm_area_struct *vma,
678 struct ttm_buffer_object *bo);
681 * ttm_bo_mmap - mmap out of the ttm device address space.
683 * @filp: filp as input from the mmap method.
684 * @vma: vma as input from the mmap method.
685 * @bdev: Pointer to the ttm_bo_device with the address space manager.
687 * This function is intended to be called by the device mmap method.
688 * if the device address space is to be backed by the bo manager.
691 extern int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
692 struct ttm_bo_device *bdev);
695 * ttm_bo_io
697 * @bdev: Pointer to the struct ttm_bo_device.
698 * @filp: Pointer to the struct file attempting to read / write.
699 * @wbuf: User-space pointer to address of buffer to write. NULL on read.
700 * @rbuf: User-space pointer to address of buffer to read into.
701 * Null on write.
702 * @count: Number of bytes to read / write.
703 * @f_pos: Pointer to current file position.
704 * @write: 1 for read, 0 for write.
706 * This function implements read / write into ttm buffer objects, and is
707 * intended to
708 * be called from the fops::read and fops::write method.
709 * Returns:
710 * See man (2) write, man(2) read. In particular,
711 * the function may return -ERESTARTSYS if
712 * interrupted by a signal.
715 extern ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
716 const char __user *wbuf, char __user *rbuf,
717 size_t count, loff_t *f_pos, bool write);
719 extern void ttm_bo_swapout_all(struct ttm_bo_device *bdev);
721 #endif