hwrng: core - Don't use a stack buffer in add_early_randomness()
[linux/fpc-iii.git] / include / drm / ttm / ttm_bo_api.h
blob9eb940d6755feba2163526cefc2ef230a6d4ea69
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 <drm/drm_vma_manager.h>
36 #include <linux/kref.h>
37 #include <linux/list.h>
38 #include <linux/wait.h>
39 #include <linux/mutex.h>
40 #include <linux/mm.h>
41 #include <linux/bitmap.h>
42 #include <linux/reservation.h>
44 struct ttm_bo_device;
46 struct drm_mm_node;
48 struct ttm_placement;
50 /**
51 * struct ttm_bus_placement
53 * @addr: mapped virtual address
54 * @base: bus base address
55 * @is_iomem: is this io memory ?
56 * @size: size in byte
57 * @offset: offset from the base address
58 * @io_reserved_vm: The VM system has a refcount in @io_reserved_count
59 * @io_reserved_count: Refcounting the numbers of callers to ttm_mem_io_reserve
61 * Structure indicating the bus placement of an object.
63 struct ttm_bus_placement {
64 void *addr;
65 phys_addr_t base;
66 unsigned long size;
67 unsigned long offset;
68 bool is_iomem;
69 bool io_reserved_vm;
70 uint64_t io_reserved_count;
74 /**
75 * struct ttm_mem_reg
77 * @mm_node: Memory manager node.
78 * @size: Requested size of memory region.
79 * @num_pages: Actual size of memory region in pages.
80 * @page_alignment: Page alignment.
81 * @placement: Placement flags.
82 * @bus: Placement on io bus accessible to the CPU
84 * Structure indicating the placement and space resources used by a
85 * buffer object.
88 struct ttm_mem_reg {
89 void *mm_node;
90 unsigned long start;
91 unsigned long size;
92 unsigned long num_pages;
93 uint32_t page_alignment;
94 uint32_t mem_type;
95 uint32_t placement;
96 struct ttm_bus_placement bus;
99 /**
100 * enum ttm_bo_type
102 * @ttm_bo_type_device: These are 'normal' buffers that can
103 * be mmapped by user space. Each of these bos occupy a slot in the
104 * device address space, that can be used for normal vm operations.
106 * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
107 * but they cannot be accessed from user-space. For kernel-only use.
109 * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
110 * driver.
113 enum ttm_bo_type {
114 ttm_bo_type_device,
115 ttm_bo_type_kernel,
116 ttm_bo_type_sg
119 struct ttm_tt;
122 * struct ttm_buffer_object
124 * @bdev: Pointer to the buffer object device structure.
125 * @type: The bo type.
126 * @destroy: Destruction function. If NULL, kfree is used.
127 * @num_pages: Actual number of pages.
128 * @acc_size: Accounted size for this object.
129 * @kref: Reference count of this buffer object. When this refcount reaches
130 * zero, the object is put on the delayed delete list.
131 * @list_kref: List reference count of this buffer object. This member is
132 * used to avoid destruction while the buffer object is still on a list.
133 * Lru lists may keep one refcount, the delayed delete list, and kref != 0
134 * keeps one refcount. When this refcount reaches zero,
135 * the object is destroyed.
136 * @mem: structure describing current placement.
137 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
138 * pinned in physical memory. If this behaviour is not desired, this member
139 * holds a pointer to a persistent shmem object.
140 * @ttm: TTM structure holding system pages.
141 * @evicted: Whether the object was evicted without user-space knowing.
142 * @cpu_writes: For synchronization. Number of cpu writers.
143 * @lru: List head for the lru list.
144 * @ddestroy: List head for the delayed destroy list.
145 * @swap: List head for swap LRU list.
146 * @moving: Fence set when BO is moving
147 * @vma_node: Address space manager node.
148 * @offset: The current GPU offset, which can have different meanings
149 * depending on the memory type. For SYSTEM type memory, it should be 0.
150 * @cur_placement: Hint of current placement.
151 * @wu_mutex: Wait unreserved mutex.
153 * Base class for TTM buffer object, that deals with data placement and CPU
154 * mappings. GPU mappings are really up to the driver, but for simpler GPUs
155 * the driver can usually use the placement offset @offset directly as the
156 * GPU virtual address. For drivers implementing multiple
157 * GPU memory manager contexts, the driver should manage the address space
158 * in these contexts separately and use these objects to get the correct
159 * placement and caching for these GPU maps. This makes it possible to use
160 * these objects for even quite elaborate memory management schemes.
161 * The destroy member, the API visibility of this object makes it possible
162 * to derive driver specific types.
165 struct ttm_buffer_object {
167 * Members constant at init.
170 struct ttm_bo_global *glob;
171 struct ttm_bo_device *bdev;
172 enum ttm_bo_type type;
173 void (*destroy) (struct ttm_buffer_object *);
174 unsigned long num_pages;
175 size_t acc_size;
178 * Members not needing protection.
181 struct kref kref;
182 struct kref list_kref;
185 * Members protected by the bo::resv::reserved lock.
188 struct ttm_mem_reg mem;
189 struct file *persistent_swap_storage;
190 struct ttm_tt *ttm;
191 bool evicted;
194 * Members protected by the bo::reserved lock only when written to.
197 atomic_t cpu_writers;
200 * Members protected by the bdev::lru_lock.
203 struct list_head lru;
204 struct list_head ddestroy;
205 struct list_head swap;
206 struct list_head io_reserve_lru;
209 * Members protected by a bo reservation.
212 struct fence *moving;
214 struct drm_vma_offset_node vma_node;
217 * Special members that are protected by the reserve lock
218 * and the bo::lock when written to. Can be read with
219 * either of these locks held.
222 uint64_t offset; /* GPU address space is independent of CPU word size */
223 uint32_t cur_placement;
225 struct sg_table *sg;
227 struct reservation_object *resv;
228 struct reservation_object ttm_resv;
229 struct mutex wu_mutex;
233 * struct ttm_bo_kmap_obj
235 * @virtual: The current kernel virtual address.
236 * @page: The page when kmap'ing a single page.
237 * @bo_kmap_type: Type of bo_kmap.
239 * Object describing a kernel mapping. Since a TTM bo may be located
240 * in various memory types with various caching policies, the
241 * mapping can either be an ioremap, a vmap, a kmap or part of a
242 * premapped region.
245 #define TTM_BO_MAP_IOMEM_MASK 0x80
246 struct ttm_bo_kmap_obj {
247 void *virtual;
248 struct page *page;
249 enum {
250 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
251 ttm_bo_map_vmap = 2,
252 ttm_bo_map_kmap = 3,
253 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
254 } bo_kmap_type;
255 struct ttm_buffer_object *bo;
259 * ttm_bo_reference - reference a struct ttm_buffer_object
261 * @bo: The buffer object.
263 * Returns a refcounted pointer to a buffer object.
266 static inline struct ttm_buffer_object *
267 ttm_bo_reference(struct ttm_buffer_object *bo)
269 kref_get(&bo->kref);
270 return bo;
274 * ttm_bo_wait - wait for buffer idle.
276 * @bo: The buffer object.
277 * @interruptible: Use interruptible wait.
278 * @no_wait: Return immediately if buffer is busy.
280 * This function must be called with the bo::mutex held, and makes
281 * sure any previous rendering to the buffer is completed.
282 * Note: It might be necessary to block validations before the
283 * wait by reserving the buffer.
284 * Returns -EBUSY if no_wait is true and the buffer is busy.
285 * Returns -ERESTARTSYS if interrupted by a signal.
287 extern int ttm_bo_wait(struct ttm_buffer_object *bo,
288 bool interruptible, bool no_wait);
291 * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo
293 * @placement: Return immediately if buffer is busy.
294 * @mem: The struct ttm_mem_reg indicating the region where the bo resides
295 * @new_flags: Describes compatible placement found
297 * Returns true if the placement is compatible
299 extern bool ttm_bo_mem_compat(struct ttm_placement *placement,
300 struct ttm_mem_reg *mem,
301 uint32_t *new_flags);
304 * ttm_bo_validate
306 * @bo: The buffer object.
307 * @placement: Proposed placement for the buffer object.
308 * @interruptible: Sleep interruptible if sleeping.
309 * @no_wait_gpu: Return immediately if the GPU is busy.
311 * Changes placement and caching policy of the buffer object
312 * according proposed placement.
313 * Returns
314 * -EINVAL on invalid proposed placement.
315 * -ENOMEM on out-of-memory condition.
316 * -EBUSY if no_wait is true and buffer busy.
317 * -ERESTARTSYS if interrupted by a signal.
319 extern int ttm_bo_validate(struct ttm_buffer_object *bo,
320 struct ttm_placement *placement,
321 bool interruptible,
322 bool no_wait_gpu);
325 * ttm_bo_unref
327 * @bo: The buffer object.
329 * Unreference and clear a pointer to a buffer object.
331 extern void ttm_bo_unref(struct ttm_buffer_object **bo);
335 * ttm_bo_list_ref_sub
337 * @bo: The buffer object.
338 * @count: The number of references with which to decrease @bo::list_kref;
339 * @never_free: The refcount should not reach zero with this operation.
341 * Release @count lru list references to this buffer object.
343 extern void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
344 bool never_free);
347 * ttm_bo_add_to_lru
349 * @bo: The buffer object.
351 * Add this bo to the relevant mem type lru and, if it's backed by
352 * system pages (ttms) to the swap list.
353 * This function must be called with struct ttm_bo_global::lru_lock held, and
354 * is typically called immediately prior to unreserving a bo.
356 extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
359 * ttm_bo_del_from_lru
361 * @bo: The buffer object.
363 * Remove this bo from all lru lists used to lookup and reserve an object.
364 * This function must be called with struct ttm_bo_global::lru_lock held,
365 * and is usually called just immediately after the bo has been reserved to
366 * avoid recursive reservation from lru lists.
368 extern int ttm_bo_del_from_lru(struct ttm_buffer_object *bo);
371 * ttm_bo_move_to_lru_tail
373 * @bo: The buffer object.
375 * Move this BO to the tail of all lru lists used to lookup and reserve an
376 * object. This function must be called with struct ttm_bo_global::lru_lock
377 * held, and is used to make a BO less likely to be considered for eviction.
379 extern void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo);
382 * ttm_bo_lock_delayed_workqueue
384 * Prevent the delayed workqueue from running.
385 * Returns
386 * True if the workqueue was queued at the time
388 extern int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
391 * ttm_bo_unlock_delayed_workqueue
393 * Allows the delayed workqueue to run.
395 extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev,
396 int resched);
399 * ttm_bo_synccpu_write_grab
401 * @bo: The buffer object:
402 * @no_wait: Return immediately if buffer is busy.
404 * Synchronizes a buffer object for CPU RW access. This means
405 * command submission that affects the buffer will return -EBUSY
406 * until ttm_bo_synccpu_write_release is called.
408 * Returns
409 * -EBUSY if the buffer is busy and no_wait is true.
410 * -ERESTARTSYS if interrupted by a signal.
412 extern int
413 ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
416 * ttm_bo_synccpu_write_release:
418 * @bo : The buffer object.
420 * Releases a synccpu lock.
422 extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);
425 * ttm_bo_acc_size
427 * @bdev: Pointer to a ttm_bo_device struct.
428 * @bo_size: size of the buffer object in byte.
429 * @struct_size: size of the structure holding buffer object datas
431 * Returns size to account for a buffer object
433 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
434 unsigned long bo_size,
435 unsigned struct_size);
436 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
437 unsigned long bo_size,
438 unsigned struct_size);
441 * ttm_bo_init
443 * @bdev: Pointer to a ttm_bo_device struct.
444 * @bo: Pointer to a ttm_buffer_object to be initialized.
445 * @size: Requested size of buffer object.
446 * @type: Requested type of buffer object.
447 * @flags: Initial placement flags.
448 * @page_alignment: Data alignment in pages.
449 * @interruptible: If needing to sleep to wait for GPU resources,
450 * sleep interruptible.
451 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
452 * pinned in physical memory. If this behaviour is not desired, this member
453 * holds a pointer to a persistent shmem object. Typically, this would
454 * point to the shmem object backing a GEM object if TTM is used to back a
455 * GEM user interface.
456 * @acc_size: Accounted size for this object.
457 * @resv: Pointer to a reservation_object, or NULL to let ttm allocate one.
458 * @destroy: Destroy function. Use NULL for kfree().
460 * This function initializes a pre-allocated struct ttm_buffer_object.
461 * As this object may be part of a larger structure, this function,
462 * together with the @destroy function,
463 * enables driver-specific objects derived from a ttm_buffer_object.
464 * On successful return, the object kref and list_kref are set to 1.
465 * If a failure occurs, the function will call the @destroy function, or
466 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
467 * illegal and will likely cause memory corruption.
469 * Returns
470 * -ENOMEM: Out of memory.
471 * -EINVAL: Invalid placement flags.
472 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
475 extern int ttm_bo_init(struct ttm_bo_device *bdev,
476 struct ttm_buffer_object *bo,
477 unsigned long size,
478 enum ttm_bo_type type,
479 struct ttm_placement *placement,
480 uint32_t page_alignment,
481 bool interrubtible,
482 struct file *persistent_swap_storage,
483 size_t acc_size,
484 struct sg_table *sg,
485 struct reservation_object *resv,
486 void (*destroy) (struct ttm_buffer_object *));
489 * ttm_bo_create
491 * @bdev: Pointer to a ttm_bo_device struct.
492 * @size: Requested size of buffer object.
493 * @type: Requested type of buffer object.
494 * @placement: Initial placement.
495 * @page_alignment: Data alignment in pages.
496 * @interruptible: If needing to sleep while waiting for GPU resources,
497 * sleep interruptible.
498 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
499 * pinned in physical memory. If this behaviour is not desired, this member
500 * holds a pointer to a persistent shmem object. Typically, this would
501 * point to the shmem object backing a GEM object if TTM is used to back a
502 * GEM user interface.
503 * @p_bo: On successful completion *p_bo points to the created object.
505 * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
506 * on that object. The destroy function is set to kfree().
507 * Returns
508 * -ENOMEM: Out of memory.
509 * -EINVAL: Invalid placement flags.
510 * -ERESTARTSYS: Interrupted by signal while waiting for resources.
513 extern int ttm_bo_create(struct ttm_bo_device *bdev,
514 unsigned long size,
515 enum ttm_bo_type type,
516 struct ttm_placement *placement,
517 uint32_t page_alignment,
518 bool interruptible,
519 struct file *persistent_swap_storage,
520 struct ttm_buffer_object **p_bo);
523 * ttm_bo_init_mm
525 * @bdev: Pointer to a ttm_bo_device struct.
526 * @mem_type: The memory type.
527 * @p_size: size managed area in pages.
529 * Initialize a manager for a given memory type.
530 * Note: if part of driver firstopen, it must be protected from a
531 * potentially racing lastclose.
532 * Returns:
533 * -EINVAL: invalid size or memory type.
534 * -ENOMEM: Not enough memory.
535 * May also return driver-specified errors.
538 extern int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
539 unsigned long p_size);
541 * ttm_bo_clean_mm
543 * @bdev: Pointer to a ttm_bo_device struct.
544 * @mem_type: The memory type.
546 * Take down a manager for a given memory type after first walking
547 * the LRU list to evict any buffers left alive.
549 * Normally, this function is part of lastclose() or unload(), and at that
550 * point there shouldn't be any buffers left created by user-space, since
551 * there should've been removed by the file descriptor release() method.
552 * However, before this function is run, make sure to signal all sync objects,
553 * and verify that the delayed delete queue is empty. The driver must also
554 * make sure that there are no NO_EVICT buffers present in this memory type
555 * when the call is made.
557 * If this function is part of a VT switch, the caller must make sure that
558 * there are no appications currently validating buffers before this
559 * function is called. The caller can do that by first taking the
560 * struct ttm_bo_device::ttm_lock in write mode.
562 * Returns:
563 * -EINVAL: invalid or uninitialized memory type.
564 * -EBUSY: There are still buffers left in this memory type.
567 extern int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type);
570 * ttm_bo_evict_mm
572 * @bdev: Pointer to a ttm_bo_device struct.
573 * @mem_type: The memory type.
575 * Evicts all buffers on the lru list of the memory type.
576 * This is normally part of a VT switch or an
577 * out-of-memory-space-due-to-fragmentation handler.
578 * The caller must make sure that there are no other processes
579 * currently validating buffers, and can do that by taking the
580 * struct ttm_bo_device::ttm_lock in write mode.
582 * Returns:
583 * -EINVAL: Invalid or uninitialized memory type.
584 * -ERESTARTSYS: The call was interrupted by a signal while waiting to
585 * evict a buffer.
588 extern int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
591 * ttm_kmap_obj_virtual
593 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
594 * @is_iomem: Pointer to an integer that on return indicates 1 if the
595 * virtual map is io memory, 0 if normal memory.
597 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
598 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
599 * that should strictly be accessed by the iowriteXX() and similar functions.
602 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
603 bool *is_iomem)
605 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
606 return map->virtual;
610 * ttm_bo_kmap
612 * @bo: The buffer object.
613 * @start_page: The first page to map.
614 * @num_pages: Number of pages to map.
615 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
617 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
618 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
619 * used to obtain a virtual address to the data.
621 * Returns
622 * -ENOMEM: Out of memory.
623 * -EINVAL: Invalid range.
626 extern int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
627 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
630 * ttm_bo_kunmap
632 * @map: Object describing the map to unmap.
634 * Unmaps a kernel map set up by ttm_bo_kmap.
637 extern void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
640 * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object.
642 * @vma: vma as input from the fbdev mmap method.
643 * @bo: The bo backing the address space. The address space will
644 * have the same size as the bo, and start at offset 0.
646 * This function is intended to be called by the fbdev mmap method
647 * if the fbdev address space is to be backed by a bo.
650 extern int ttm_fbdev_mmap(struct vm_area_struct *vma,
651 struct ttm_buffer_object *bo);
654 * ttm_bo_mmap - mmap out of the ttm device address space.
656 * @filp: filp as input from the mmap method.
657 * @vma: vma as input from the mmap method.
658 * @bdev: Pointer to the ttm_bo_device with the address space manager.
660 * This function is intended to be called by the device mmap method.
661 * if the device address space is to be backed by the bo manager.
664 extern int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
665 struct ttm_bo_device *bdev);
668 * ttm_bo_io
670 * @bdev: Pointer to the struct ttm_bo_device.
671 * @filp: Pointer to the struct file attempting to read / write.
672 * @wbuf: User-space pointer to address of buffer to write. NULL on read.
673 * @rbuf: User-space pointer to address of buffer to read into.
674 * Null on write.
675 * @count: Number of bytes to read / write.
676 * @f_pos: Pointer to current file position.
677 * @write: 1 for read, 0 for write.
679 * This function implements read / write into ttm buffer objects, and is
680 * intended to
681 * be called from the fops::read and fops::write method.
682 * Returns:
683 * See man (2) write, man(2) read. In particular,
684 * the function may return -ERESTARTSYS if
685 * interrupted by a signal.
688 extern ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
689 const char __user *wbuf, char __user *rbuf,
690 size_t count, loff_t *f_pos, bool write);
692 extern void ttm_bo_swapout_all(struct ttm_bo_device *bdev);
693 extern int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo);
694 #endif