Linux 3.11-rc3
[cris-mirror.git] / include / drm / ttm / ttm_bo_api.h
blob8a6aa56ece52a53cad3734c756a6dbc19a5d7ac1
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_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>
42 #include <linux/reservation.h>
44 struct ttm_bo_device;
46 struct drm_mm_node;
49 /**
50 * struct ttm_placement
52 * @fpfn: first valid page frame number to put the object
53 * @lpfn: last valid page frame number to put the object
54 * @num_placement: number of preferred placements
55 * @placement: preferred placements
56 * @num_busy_placement: number of preferred placements when need to evict buffer
57 * @busy_placement: preferred placements when need to evict buffer
59 * Structure indicating the placement you request for an object.
61 struct ttm_placement {
62 unsigned fpfn;
63 unsigned lpfn;
64 unsigned num_placement;
65 const uint32_t *placement;
66 unsigned num_busy_placement;
67 const uint32_t *busy_placement;
70 /**
71 * struct ttm_bus_placement
73 * @addr: mapped virtual address
74 * @base: bus base address
75 * @is_iomem: is this io memory ?
76 * @size: size in byte
77 * @offset: offset from the base address
78 * @io_reserved_vm: The VM system has a refcount in @io_reserved_count
79 * @io_reserved_count: Refcounting the numbers of callers to ttm_mem_io_reserve
81 * Structure indicating the bus placement of an object.
83 struct ttm_bus_placement {
84 void *addr;
85 unsigned long base;
86 unsigned long size;
87 unsigned long offset;
88 bool is_iomem;
89 bool io_reserved_vm;
90 uint64_t io_reserved_count;
94 /**
95 * struct ttm_mem_reg
97 * @mm_node: Memory manager node.
98 * @size: Requested size of memory region.
99 * @num_pages: Actual size of memory region in pages.
100 * @page_alignment: Page alignment.
101 * @placement: Placement flags.
102 * @bus: Placement on io bus accessible to the CPU
104 * Structure indicating the placement and space resources used by a
105 * buffer object.
108 struct ttm_mem_reg {
109 void *mm_node;
110 unsigned long start;
111 unsigned long size;
112 unsigned long num_pages;
113 uint32_t page_alignment;
114 uint32_t mem_type;
115 uint32_t placement;
116 struct ttm_bus_placement bus;
120 * enum ttm_bo_type
122 * @ttm_bo_type_device: These are 'normal' buffers that can
123 * be mmapped by user space. Each of these bos occupy a slot in the
124 * device address space, that can be used for normal vm operations.
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.
129 * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
130 * driver.
133 enum ttm_bo_type {
134 ttm_bo_type_device,
135 ttm_bo_type_kernel,
136 ttm_bo_type_sg
139 struct ttm_tt;
142 * struct ttm_buffer_object
144 * @bdev: Pointer to the buffer object device structure.
145 * @type: The bo type.
146 * @destroy: Destruction function. If NULL, kfree is used.
147 * @num_pages: Actual number of pages.
148 * @addr_space_offset: Address space offset.
149 * @acc_size: Accounted size for this object.
150 * @kref: Reference count of this buffer object. When this refcount reaches
151 * zero, the object is put on the delayed delete list.
152 * @list_kref: List reference count of this buffer object. This member is
153 * used to avoid destruction while the buffer object is still on a list.
154 * Lru lists may keep one refcount, the delayed delete list, and kref != 0
155 * keeps one refcount. When this refcount reaches zero,
156 * the object is destroyed.
157 * @mem: structure describing current placement.
158 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
159 * pinned in physical memory. If this behaviour is not desired, this member
160 * holds a pointer to a persistent shmem object.
161 * @ttm: TTM structure holding system pages.
162 * @evicted: Whether the object was evicted without user-space knowing.
163 * @cpu_writes: For synchronization. Number of cpu writers.
164 * @lru: List head for the lru list.
165 * @ddestroy: List head for the delayed destroy list.
166 * @swap: List head for swap LRU list.
167 * @sync_obj: Pointer to a synchronization object.
168 * @priv_flags: Flags describing buffer object internal state.
169 * @vm_rb: Rb node for the vm rb tree.
170 * @vm_node: Address space manager node.
171 * @offset: The current GPU offset, which can have different meanings
172 * depending on the memory type. For SYSTEM type memory, it should be 0.
173 * @cur_placement: Hint of current placement.
175 * Base class for TTM buffer object, that deals with data placement and CPU
176 * mappings. GPU mappings are really up to the driver, but for simpler GPUs
177 * the driver can usually use the placement offset @offset directly as the
178 * GPU virtual address. For drivers implementing multiple
179 * GPU memory manager contexts, the driver should manage the address space
180 * in these contexts separately and use these objects to get the correct
181 * placement and caching for these GPU maps. This makes it possible to use
182 * these objects for even quite elaborate memory management schemes.
183 * The destroy member, the API visibility of this object makes it possible
184 * to derive driver specific types.
187 struct ttm_buffer_object {
189 * Members constant at init.
192 struct ttm_bo_global *glob;
193 struct ttm_bo_device *bdev;
194 enum ttm_bo_type type;
195 void (*destroy) (struct ttm_buffer_object *);
196 unsigned long num_pages;
197 uint64_t addr_space_offset;
198 size_t acc_size;
201 * Members not needing protection.
204 struct kref kref;
205 struct kref list_kref;
208 * Members protected by the bo::resv::reserved lock.
211 struct ttm_mem_reg mem;
212 struct file *persistent_swap_storage;
213 struct ttm_tt *ttm;
214 bool evicted;
217 * Members protected by the bo::reserved lock only when written to.
220 atomic_t cpu_writers;
223 * Members protected by the bdev::lru_lock.
226 struct list_head lru;
227 struct list_head ddestroy;
228 struct list_head swap;
229 struct list_head io_reserve_lru;
232 * Members protected by struct buffer_object_device::fence_lock
233 * In addition, setting sync_obj to anything else
234 * than NULL requires bo::reserved to be held. This allows for
235 * checking NULL while reserved but not holding the mentioned lock.
238 void *sync_obj;
239 unsigned long priv_flags;
242 * Members protected by the bdev::vm_lock
245 struct rb_node vm_rb;
246 struct drm_mm_node *vm_node;
250 * Special members that are protected by the reserve lock
251 * and the bo::lock when written to. Can be read with
252 * either of these locks held.
255 unsigned long offset;
256 uint32_t cur_placement;
258 struct sg_table *sg;
260 struct reservation_object *resv;
261 struct reservation_object ttm_resv;
265 * struct ttm_bo_kmap_obj
267 * @virtual: The current kernel virtual address.
268 * @page: The page when kmap'ing a single page.
269 * @bo_kmap_type: Type of bo_kmap.
271 * Object describing a kernel mapping. Since a TTM bo may be located
272 * in various memory types with various caching policies, the
273 * mapping can either be an ioremap, a vmap, a kmap or part of a
274 * premapped region.
277 #define TTM_BO_MAP_IOMEM_MASK 0x80
278 struct ttm_bo_kmap_obj {
279 void *virtual;
280 struct page *page;
281 enum {
282 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
283 ttm_bo_map_vmap = 2,
284 ttm_bo_map_kmap = 3,
285 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
286 } bo_kmap_type;
287 struct ttm_buffer_object *bo;
291 * ttm_bo_reference - reference a struct ttm_buffer_object
293 * @bo: The buffer object.
295 * Returns a refcounted pointer to a buffer object.
298 static inline struct ttm_buffer_object *
299 ttm_bo_reference(struct ttm_buffer_object *bo)
301 kref_get(&bo->kref);
302 return bo;
306 * ttm_bo_wait - wait for buffer idle.
308 * @bo: The buffer object.
309 * @interruptible: Use interruptible wait.
310 * @no_wait: Return immediately if buffer is busy.
312 * This function must be called with the bo::mutex held, and makes
313 * sure any previous rendering to the buffer is completed.
314 * Note: It might be necessary to block validations before the
315 * wait by reserving the buffer.
316 * Returns -EBUSY if no_wait is true and the buffer is busy.
317 * Returns -ERESTARTSYS if interrupted by a signal.
319 extern int ttm_bo_wait(struct ttm_buffer_object *bo, bool lazy,
320 bool interruptible, bool no_wait);
322 * ttm_bo_validate
324 * @bo: The buffer object.
325 * @placement: Proposed placement for the buffer object.
326 * @interruptible: Sleep interruptible if sleeping.
327 * @no_wait_gpu: Return immediately if the GPU is busy.
329 * Changes placement and caching policy of the buffer object
330 * according proposed placement.
331 * Returns
332 * -EINVAL on invalid proposed placement.
333 * -ENOMEM on out-of-memory condition.
334 * -EBUSY if no_wait is true and buffer busy.
335 * -ERESTARTSYS if interrupted by a signal.
337 extern int ttm_bo_validate(struct ttm_buffer_object *bo,
338 struct ttm_placement *placement,
339 bool interruptible,
340 bool no_wait_gpu);
343 * ttm_bo_unref
345 * @bo: The buffer object.
347 * Unreference and clear a pointer to a buffer object.
349 extern void ttm_bo_unref(struct ttm_buffer_object **bo);
353 * ttm_bo_list_ref_sub
355 * @bo: The buffer object.
356 * @count: The number of references with which to decrease @bo::list_kref;
357 * @never_free: The refcount should not reach zero with this operation.
359 * Release @count lru list references to this buffer object.
361 extern void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
362 bool never_free);
365 * ttm_bo_add_to_lru
367 * @bo: The buffer object.
369 * Add this bo to the relevant mem type lru and, if it's backed by
370 * system pages (ttms) to the swap list.
371 * This function must be called with struct ttm_bo_global::lru_lock held, and
372 * is typically called immediately prior to unreserving a bo.
374 extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
377 * ttm_bo_del_from_lru
379 * @bo: The buffer object.
381 * Remove this bo from all lru lists used to lookup and reserve an object.
382 * This function must be called with struct ttm_bo_global::lru_lock held,
383 * and is usually called just immediately after the bo has been reserved to
384 * avoid recursive reservation from lru lists.
386 extern int ttm_bo_del_from_lru(struct ttm_buffer_object *bo);
390 * ttm_bo_lock_delayed_workqueue
392 * Prevent the delayed workqueue from running.
393 * Returns
394 * True if the workqueue was queued at the time
396 extern int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
399 * ttm_bo_unlock_delayed_workqueue
401 * Allows the delayed workqueue to run.
403 extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev,
404 int resched);
407 * ttm_bo_synccpu_write_grab
409 * @bo: The buffer object:
410 * @no_wait: Return immediately if buffer is busy.
412 * Synchronizes a buffer object for CPU RW access. This means
413 * command submission that affects the buffer will return -EBUSY
414 * until ttm_bo_synccpu_write_release is called.
416 * Returns
417 * -EBUSY if the buffer is busy and no_wait is true.
418 * -ERESTARTSYS if interrupted by a signal.
420 extern int
421 ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
424 * ttm_bo_synccpu_write_release:
426 * @bo : The buffer object.
428 * Releases a synccpu lock.
430 extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);
433 * ttm_bo_acc_size
435 * @bdev: Pointer to a ttm_bo_device struct.
436 * @bo_size: size of the buffer object in byte.
437 * @struct_size: size of the structure holding buffer object datas
439 * Returns size to account for a buffer object
441 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
442 unsigned long bo_size,
443 unsigned struct_size);
444 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
445 unsigned long bo_size,
446 unsigned struct_size);
449 * ttm_bo_init
451 * @bdev: Pointer to a ttm_bo_device struct.
452 * @bo: Pointer to a ttm_buffer_object to be initialized.
453 * @size: Requested size of buffer object.
454 * @type: Requested type of buffer object.
455 * @flags: Initial placement flags.
456 * @page_alignment: Data alignment in pages.
457 * @interruptible: If needing to sleep to wait for GPU resources,
458 * sleep interruptible.
459 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
460 * pinned in physical memory. If this behaviour is not desired, this member
461 * holds a pointer to a persistent shmem object. Typically, this would
462 * point to the shmem object backing a GEM object if TTM is used to back a
463 * GEM user interface.
464 * @acc_size: Accounted size for this object.
465 * @destroy: Destroy function. Use NULL for kfree().
467 * This function initializes a pre-allocated struct ttm_buffer_object.
468 * As this object may be part of a larger structure, this function,
469 * together with the @destroy function,
470 * enables driver-specific objects derived from a ttm_buffer_object.
471 * On successful return, the object kref and list_kref are set to 1.
472 * If a failure occurs, the function will call the @destroy function, or
473 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
474 * illegal and will likely cause memory corruption.
476 * Returns
477 * -ENOMEM: Out of memory.
478 * -EINVAL: Invalid placement flags.
479 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
482 extern int ttm_bo_init(struct ttm_bo_device *bdev,
483 struct ttm_buffer_object *bo,
484 unsigned long size,
485 enum ttm_bo_type type,
486 struct ttm_placement *placement,
487 uint32_t page_alignment,
488 bool interrubtible,
489 struct file *persistent_swap_storage,
490 size_t acc_size,
491 struct sg_table *sg,
492 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 * @interruptible: If needing to sleep while waiting for GPU resources,
504 * sleep interruptible.
505 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
506 * pinned in physical memory. If this behaviour is not desired, this member
507 * holds a pointer to a persistent shmem object. Typically, this would
508 * point to the shmem object backing a GEM object if TTM is used to back a
509 * GEM user interface.
510 * @p_bo: On successful completion *p_bo points to the created object.
512 * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
513 * on that object. The destroy function is set to kfree().
514 * Returns
515 * -ENOMEM: Out of memory.
516 * -EINVAL: Invalid placement flags.
517 * -ERESTARTSYS: Interrupted by signal while waiting for resources.
520 extern int ttm_bo_create(struct ttm_bo_device *bdev,
521 unsigned long size,
522 enum ttm_bo_type type,
523 struct ttm_placement *placement,
524 uint32_t page_alignment,
525 bool interruptible,
526 struct file *persistent_swap_storage,
527 struct ttm_buffer_object **p_bo);
530 * ttm_bo_check_placement
532 * @bo: the buffer object.
533 * @placement: placements
535 * Performs minimal validity checking on an intended change of
536 * placement flags.
537 * Returns
538 * -EINVAL: Intended change is invalid or not allowed.
540 extern int ttm_bo_check_placement(struct ttm_buffer_object *bo,
541 struct ttm_placement *placement);
544 * ttm_bo_init_mm
546 * @bdev: Pointer to a ttm_bo_device struct.
547 * @mem_type: The memory type.
548 * @p_size: size managed area in pages.
550 * Initialize a manager for a given memory type.
551 * Note: if part of driver firstopen, it must be protected from a
552 * potentially racing lastclose.
553 * Returns:
554 * -EINVAL: invalid size or memory type.
555 * -ENOMEM: Not enough memory.
556 * May also return driver-specified errors.
559 extern int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
560 unsigned long p_size);
562 * ttm_bo_clean_mm
564 * @bdev: Pointer to a ttm_bo_device struct.
565 * @mem_type: The memory type.
567 * Take down a manager for a given memory type after first walking
568 * the LRU list to evict any buffers left alive.
570 * Normally, this function is part of lastclose() or unload(), and at that
571 * point there shouldn't be any buffers left created by user-space, since
572 * there should've been removed by the file descriptor release() method.
573 * However, before this function is run, make sure to signal all sync objects,
574 * and verify that the delayed delete queue is empty. The driver must also
575 * make sure that there are no NO_EVICT buffers present in this memory type
576 * when the call is made.
578 * If this function is part of a VT switch, the caller must make sure that
579 * there are no appications currently validating buffers before this
580 * function is called. The caller can do that by first taking the
581 * struct ttm_bo_device::ttm_lock in write mode.
583 * Returns:
584 * -EINVAL: invalid or uninitialized memory type.
585 * -EBUSY: There are still buffers left in this memory type.
588 extern int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type);
591 * ttm_bo_evict_mm
593 * @bdev: Pointer to a ttm_bo_device struct.
594 * @mem_type: The memory type.
596 * Evicts all buffers on the lru list of the memory type.
597 * This is normally part of a VT switch or an
598 * out-of-memory-space-due-to-fragmentation handler.
599 * The caller must make sure that there are no other processes
600 * currently validating buffers, and can do that by taking the
601 * struct ttm_bo_device::ttm_lock in write mode.
603 * Returns:
604 * -EINVAL: Invalid or uninitialized memory type.
605 * -ERESTARTSYS: The call was interrupted by a signal while waiting to
606 * evict a buffer.
609 extern int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
612 * ttm_kmap_obj_virtual
614 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
615 * @is_iomem: Pointer to an integer that on return indicates 1 if the
616 * virtual map is io memory, 0 if normal memory.
618 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
619 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
620 * that should strictly be accessed by the iowriteXX() and similar functions.
623 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
624 bool *is_iomem)
626 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
627 return map->virtual;
631 * ttm_bo_kmap
633 * @bo: The buffer object.
634 * @start_page: The first page to map.
635 * @num_pages: Number of pages to map.
636 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
638 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
639 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
640 * used to obtain a virtual address to the data.
642 * Returns
643 * -ENOMEM: Out of memory.
644 * -EINVAL: Invalid range.
647 extern int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
648 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
651 * ttm_bo_kunmap
653 * @map: Object describing the map to unmap.
655 * Unmaps a kernel map set up by ttm_bo_kmap.
658 extern void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
661 * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object.
663 * @vma: vma as input from the fbdev mmap method.
664 * @bo: The bo backing the address space. The address space will
665 * have the same size as the bo, and start at offset 0.
667 * This function is intended to be called by the fbdev mmap method
668 * if the fbdev address space is to be backed by a bo.
671 extern int ttm_fbdev_mmap(struct vm_area_struct *vma,
672 struct ttm_buffer_object *bo);
675 * ttm_bo_mmap - mmap out of the ttm device address space.
677 * @filp: filp as input from the mmap method.
678 * @vma: vma as input from the mmap method.
679 * @bdev: Pointer to the ttm_bo_device with the address space manager.
681 * This function is intended to be called by the device mmap method.
682 * if the device address space is to be backed by the bo manager.
685 extern int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
686 struct ttm_bo_device *bdev);
689 * ttm_bo_io
691 * @bdev: Pointer to the struct ttm_bo_device.
692 * @filp: Pointer to the struct file attempting to read / write.
693 * @wbuf: User-space pointer to address of buffer to write. NULL on read.
694 * @rbuf: User-space pointer to address of buffer to read into.
695 * Null on write.
696 * @count: Number of bytes to read / write.
697 * @f_pos: Pointer to current file position.
698 * @write: 1 for read, 0 for write.
700 * This function implements read / write into ttm buffer objects, and is
701 * intended to
702 * be called from the fops::read and fops::write method.
703 * Returns:
704 * See man (2) write, man(2) read. In particular,
705 * the function may return -ERESTARTSYS if
706 * interrupted by a signal.
709 extern ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
710 const char __user *wbuf, char __user *rbuf,
711 size_t count, loff_t *f_pos, bool write);
713 extern void ttm_bo_swapout_all(struct ttm_bo_device *bdev);
715 #endif