xref: /dragonfly/sys/dev/drm/include/drm/ttm/ttm_bo_api.h (revision 1c9138ce)
1 /**************************************************************************
2  *
3  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
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:
13  *
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.
17  *
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.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29  */
30 
31 #ifndef _TTM_BO_API_H_
32 #define _TTM_BO_API_H_
33 
34 #include <drm/drmP.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/reservation.h>
44 
45 struct ttm_bo_device;
46 
47 struct drm_mm_node;
48 
49 struct ttm_placement;
50 
51 struct ttm_place;
52 
53 /**
54  * struct ttm_bus_placement
55  *
56  * @addr:		mapped virtual address
57  * @base:		bus base address
58  * @is_iomem:		is this io memory ?
59  * @size:		size in byte
60  * @offset:		offset from the base address
61  * @io_reserved_vm:     The VM system has a refcount in @io_reserved_count
62  * @io_reserved_count:  Refcounting the numbers of callers to ttm_mem_io_reserve
63  *
64  * Structure indicating the bus placement of an object.
65  */
66 struct ttm_bus_placement {
67 	void		*addr;
68 	phys_addr_t	base;
69 	unsigned long	size;
70 	unsigned long	offset;
71 	bool		is_iomem;
72 	bool		io_reserved_vm;
73 	uint64_t        io_reserved_count;
74 };
75 
76 
77 /**
78  * struct ttm_mem_reg
79  *
80  * @mm_node: Memory manager node.
81  * @size: Requested size of memory region.
82  * @num_pages: Actual size of memory region in pages.
83  * @page_alignment: Page alignment.
84  * @placement: Placement flags.
85  * @bus: Placement on io bus accessible to the CPU
86  *
87  * Structure indicating the placement and space resources used by a
88  * buffer object.
89  */
90 
91 struct ttm_mem_reg {
92 	void *mm_node;
93 	unsigned long start;
94 	unsigned long size;
95 	unsigned long num_pages;
96 	uint32_t page_alignment;
97 	uint32_t mem_type;
98 	uint32_t placement;
99 	struct ttm_bus_placement bus;
100 };
101 
102 /**
103  * enum ttm_bo_type
104  *
105  * @ttm_bo_type_device:	These are 'normal' buffers that can
106  * be mmapped by user space. Each of these bos occupy a slot in the
107  * device address space, that can be used for normal vm operations.
108  *
109  * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
110  * but they cannot be accessed from user-space. For kernel-only use.
111  *
112  * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
113  * driver.
114  */
115 
116 enum ttm_bo_type {
117 	ttm_bo_type_device,
118 	ttm_bo_type_kernel,
119 	ttm_bo_type_sg
120 };
121 
122 struct ttm_tt;
123 
124 /**
125  * struct ttm_buffer_object
126  *
127  * @bdev: Pointer to the buffer object device structure.
128  * @type: The bo type.
129  * @destroy: Destruction function. If NULL, kfree is used.
130  * @num_pages: Actual number of pages.
131  * @acc_size: Accounted size for this object.
132  * @kref: Reference count of this buffer object. When this refcount reaches
133  * zero, the object is put on the delayed delete list.
134  * @list_kref: List reference count of this buffer object. This member is
135  * used to avoid destruction while the buffer object is still on a list.
136  * Lru lists may keep one refcount, the delayed delete list, and kref != 0
137  * keeps one refcount. When this refcount reaches zero,
138  * the object is destroyed.
139  * @mem: structure describing current placement.
140  * @persistent_swap_storage: Usually the swap storage is deleted for buffers
141  * pinned in physical memory. If this behaviour is not desired, this member
142  * holds a pointer to a persistent shmem object.
143  * @ttm: TTM structure holding system pages.
144  * @evicted: Whether the object was evicted without user-space knowing.
145  * @cpu_writes: For synchronization. Number of cpu writers.
146  * @lru: List head for the lru list.
147  * @ddestroy: List head for the delayed destroy list.
148  * @swap: List head for swap LRU list.
149  * @moving: Fence set when BO is moving
150  * @vma_node: Address space manager node.
151  * @offset: The current GPU offset, which can have different meanings
152  * depending on the memory type. For SYSTEM type memory, it should be 0.
153  * @cur_placement: Hint of current placement.
154  * @wu_mutex: Wait unreserved mutex.
155  *
156  * Base class for TTM buffer object, that deals with data placement and CPU
157  * mappings. GPU mappings are really up to the driver, but for simpler GPUs
158  * the driver can usually use the placement offset @offset directly as the
159  * GPU virtual address. For drivers implementing multiple
160  * GPU memory manager contexts, the driver should manage the address space
161  * in these contexts separately and use these objects to get the correct
162  * placement and caching for these GPU maps. This makes it possible to use
163  * these objects for even quite elaborate memory management schemes.
164  * The destroy member, the API visibility of this object makes it possible
165  * to derive driver specific types.
166  */
167 
168 struct ttm_buffer_object {
169 	/**
170 	 * Members constant at init.
171 	 */
172 
173 	struct ttm_bo_global *glob;
174 	struct ttm_bo_device *bdev;
175 	enum ttm_bo_type type;
176 	void (*destroy) (struct ttm_buffer_object *);
177 	unsigned long num_pages;
178 	size_t acc_size;
179 
180 	/**
181 	* Members not needing protection.
182 	*/
183 
184 	struct kref kref;
185 	struct kref list_kref;
186 
187 	/**
188 	 * Members protected by the bo::resv::reserved lock.
189 	 */
190 
191 	struct ttm_mem_reg mem;
192 	struct vm_object *persistent_swap_storage;
193 	struct ttm_tt *ttm;
194 	bool evicted;
195 
196 	/**
197 	 * Members protected by the bo::reserved lock only when written to.
198 	 */
199 
200 	atomic_t cpu_writers;
201 
202 	/**
203 	 * Members protected by the bdev::lru_lock.
204 	 */
205 
206 	struct list_head lru;
207 	struct list_head ddestroy;
208 	struct list_head swap;
209 	struct list_head io_reserve_lru;
210 
211 	/**
212 	 * Members protected by a bo reservation.
213 	 */
214 
215 	struct dma_fence *moving;
216 
217 	RB_ENTRY(ttm_buffer_object) vm_rb;	/* DragonFly */
218 	struct drm_vma_offset_node vma_node;
219 
220 	unsigned priority;
221 
222 	/**
223 	 * Special members that are protected by the reserve lock
224 	 * and the bo::lock when written to. Can be read with
225 	 * either of these locks held.
226 	 */
227 
228 	uint64_t offset; /* GPU address space is independent of CPU word size */
229 	uint32_t cur_placement;
230 
231 	struct sg_table *sg;
232 
233 	struct reservation_object *resv;
234 	struct reservation_object ttm_resv;
235 	struct lock wu_mutex;
236 };
237 
238 /**
239  * struct ttm_bo_kmap_obj
240  *
241  * @virtual: The current kernel virtual address.
242  * @page: The page when kmap'ing a single page.
243  * @bo_kmap_type: Type of bo_kmap.
244  *
245  * Object describing a kernel mapping. Since a TTM bo may be located
246  * in various memory types with various caching policies, the
247  * mapping can either be an ioremap, a vmap, a kmap or part of a
248  * premapped region.
249  */
250 
251 #define TTM_BO_MAP_IOMEM_MASK 0x80
252 struct ttm_bo_kmap_obj {
253 	void *virtual;
254 	struct page *page;
255 	enum {
256 		ttm_bo_map_iomap        = 1 | TTM_BO_MAP_IOMEM_MASK,
257 		ttm_bo_map_vmap         = 2,
258 		ttm_bo_map_kmap         = 3,
259 		ttm_bo_map_premapped    = 4 | TTM_BO_MAP_IOMEM_MASK,
260 	} bo_kmap_type;
261 	struct ttm_buffer_object *bo;
262 };
263 
264 /**
265  * ttm_bo_reference - reference a struct ttm_buffer_object
266  *
267  * @bo: The buffer object.
268  *
269  * Returns a refcounted pointer to a buffer object.
270  */
271 
272 static inline struct ttm_buffer_object *
273 ttm_bo_reference(struct ttm_buffer_object *bo)
274 {
275 	kref_get(&bo->kref);
276 	return bo;
277 }
278 
279 /**
280  * ttm_bo_wait - wait for buffer idle.
281  *
282  * @bo:  The buffer object.
283  * @interruptible:  Use interruptible wait.
284  * @no_wait:  Return immediately if buffer is busy.
285  *
286  * This function must be called with the bo::mutex held, and makes
287  * sure any previous rendering to the buffer is completed.
288  * Note: It might be necessary to block validations before the
289  * wait by reserving the buffer.
290  * Returns -EBUSY if no_wait is true and the buffer is busy.
291  * Returns -ERESTARTSYS if interrupted by a signal.
292  */
293 extern int ttm_bo_wait(struct ttm_buffer_object *bo,
294 		       bool interruptible, bool no_wait);
295 
296 /**
297  * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo
298  *
299  * @placement:  Return immediately if buffer is busy.
300  * @mem:  The struct ttm_mem_reg indicating the region where the bo resides
301  * @new_flags: Describes compatible placement found
302  *
303  * Returns true if the placement is compatible
304  */
305 extern bool ttm_bo_mem_compat(struct ttm_placement *placement,
306 			      struct ttm_mem_reg *mem,
307 			      uint32_t *new_flags);
308 
309 /**
310  * ttm_bo_validate
311  *
312  * @bo: The buffer object.
313  * @placement: Proposed placement for the buffer object.
314  * @interruptible: Sleep interruptible if sleeping.
315  * @no_wait_gpu: Return immediately if the GPU is busy.
316  *
317  * Changes placement and caching policy of the buffer object
318  * according proposed placement.
319  * Returns
320  * -EINVAL on invalid proposed placement.
321  * -ENOMEM on out-of-memory condition.
322  * -EBUSY if no_wait is true and buffer busy.
323  * -ERESTARTSYS if interrupted by a signal.
324  */
325 extern int ttm_bo_validate(struct ttm_buffer_object *bo,
326 				struct ttm_placement *placement,
327 				bool interruptible,
328 				bool no_wait_gpu);
329 
330 /**
331  * ttm_bo_unref
332  *
333  * @bo: The buffer object.
334  *
335  * Unreference and clear a pointer to a buffer object.
336  */
337 extern void ttm_bo_unref(struct ttm_buffer_object **bo);
338 
339 /**
340  * ttm_bo_add_to_lru
341  *
342  * @bo: The buffer object.
343  *
344  * Add this bo to the relevant mem type lru and, if it's backed by
345  * system pages (ttms) to the swap list.
346  * This function must be called with struct ttm_bo_global::lru_lock held, and
347  * is typically called immediately prior to unreserving a bo.
348  */
349 extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
350 
351 /**
352  * ttm_bo_del_from_lru
353  *
354  * @bo: The buffer object.
355  *
356  * Remove this bo from all lru lists used to lookup and reserve an object.
357  * This function must be called with struct ttm_bo_global::lru_lock held,
358  * and is usually called just immediately after the bo has been reserved to
359  * avoid recursive reservation from lru lists.
360  */
361 extern void ttm_bo_del_from_lru(struct ttm_buffer_object *bo);
362 
363 /**
364  * ttm_bo_move_to_lru_tail
365  *
366  * @bo: The buffer object.
367  *
368  * Move this BO to the tail of all lru lists used to lookup and reserve an
369  * object. This function must be called with struct ttm_bo_global::lru_lock
370  * held, and is used to make a BO less likely to be considered for eviction.
371  */
372 extern void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo);
373 
374 /**
375  * ttm_bo_lock_delayed_workqueue
376  *
377  * Prevent the delayed workqueue from running.
378  * Returns
379  * True if the workqueue was queued at the time
380  */
381 extern int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
382 
383 /**
384  * ttm_bo_unlock_delayed_workqueue
385  *
386  * Allows the delayed workqueue to run.
387  */
388 extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev,
389 					    int resched);
390 
391 /**
392  * ttm_bo_eviction_valuable
393  *
394  * @bo: The buffer object to evict
395  * @place: the placement we need to make room for
396  *
397  * Check if it is valuable to evict the BO to make room for the given placement.
398  */
399 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
400 			      const struct ttm_place *place);
401 
402 /**
403  * ttm_bo_synccpu_write_grab
404  *
405  * @bo: The buffer object:
406  * @no_wait: Return immediately if buffer is busy.
407  *
408  * Synchronizes a buffer object for CPU RW access. This means
409  * command submission that affects the buffer will return -EBUSY
410  * until ttm_bo_synccpu_write_release is called.
411  *
412  * Returns
413  * -EBUSY if the buffer is busy and no_wait is true.
414  * -ERESTARTSYS if interrupted by a signal.
415  */
416 extern int
417 ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
418 
419 /**
420  * ttm_bo_synccpu_write_release:
421  *
422  * @bo : The buffer object.
423  *
424  * Releases a synccpu lock.
425  */
426 extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);
427 
428 /**
429  * ttm_bo_acc_size
430  *
431  * @bdev: Pointer to a ttm_bo_device struct.
432  * @bo_size: size of the buffer object in byte.
433  * @struct_size: size of the structure holding buffer object datas
434  *
435  * Returns size to account for a buffer object
436  */
437 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
438 		       unsigned long bo_size,
439 		       unsigned struct_size);
440 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
441 			   unsigned long bo_size,
442 			   unsigned struct_size);
443 
444 /**
445  * ttm_bo_init_reserved
446  *
447  * @bdev: Pointer to a ttm_bo_device struct.
448  * @bo: Pointer to a ttm_buffer_object to be initialized.
449  * @size: Requested size of buffer object.
450  * @type: Requested type of buffer object.
451  * @flags: Initial placement flags.
452  * @page_alignment: Data alignment in pages.
453  * @interruptible: If needing to sleep to wait for GPU resources,
454  * sleep interruptible.
455  * @persistent_swap_storage: Usually the swap storage is deleted for buffers
456  * pinned in physical memory. If this behaviour is not desired, this member
457  * holds a pointer to a persistent shmem object. Typically, this would
458  * point to the shmem object backing a GEM object if TTM is used to back a
459  * GEM user interface.
460  * @acc_size: Accounted size for this object.
461  * @resv: Pointer to a reservation_object, or NULL to let ttm allocate one.
462  * @destroy: Destroy function. Use NULL for kfree().
463  *
464  * This function initializes a pre-allocated struct ttm_buffer_object.
465  * As this object may be part of a larger structure, this function,
466  * together with the @destroy function,
467  * enables driver-specific objects derived from a ttm_buffer_object.
468  *
469  * On successful return, the caller owns an object kref to @bo. The kref and
470  * list_kref are usually set to 1, but note that in some situations, other
471  * tasks may already be holding references to @bo as well.
472  * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
473  * and it is the caller's responsibility to call ttm_bo_unreserve.
474  *
475  * If a failure occurs, the function will call the @destroy function, or
476  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
477  * illegal and will likely cause memory corruption.
478  *
479  * Returns
480  * -ENOMEM: Out of memory.
481  * -EINVAL: Invalid placement flags.
482  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
483  */
484 
485 extern int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
486 				struct ttm_buffer_object *bo,
487 				unsigned long size,
488 				enum ttm_bo_type type,
489 				struct ttm_placement *placement,
490 				uint32_t page_alignment,
491 				bool interrubtible,
492 				struct vm_object *persistent_swap_storage,
493 				size_t acc_size,
494 				struct sg_table *sg,
495 				struct reservation_object *resv,
496 				void (*destroy) (struct ttm_buffer_object *));
497 
498 /**
499  * ttm_bo_init
500  *
501  * @bdev: Pointer to a ttm_bo_device struct.
502  * @bo: Pointer to a ttm_buffer_object to be initialized.
503  * @size: Requested size of buffer object.
504  * @type: Requested type of buffer object.
505  * @flags: Initial placement flags.
506  * @page_alignment: Data alignment in pages.
507  * @interruptible: If needing to sleep to wait for GPU resources,
508  * sleep interruptible.
509  * @persistent_swap_storage: Usually the swap storage is deleted for buffers
510  * pinned in physical memory. If this behaviour is not desired, this member
511  * holds a pointer to a persistent shmem object. Typically, this would
512  * point to the shmem object backing a GEM object if TTM is used to back a
513  * GEM user interface.
514  * @acc_size: Accounted size for this object.
515  * @resv: Pointer to a reservation_object, or NULL to let ttm allocate one.
516  * @destroy: Destroy function. Use NULL for kfree().
517  *
518  * This function initializes a pre-allocated struct ttm_buffer_object.
519  * As this object may be part of a larger structure, this function,
520  * together with the @destroy function,
521  * enables driver-specific objects derived from a ttm_buffer_object.
522  *
523  * On successful return, the caller owns an object kref to @bo. The kref and
524  * list_kref are usually set to 1, but note that in some situations, other
525  * tasks may already be holding references to @bo as well.
526  *
527  * If a failure occurs, the function will call the @destroy function, or
528  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
529  * illegal and will likely cause memory corruption.
530  *
531  * Returns
532  * -ENOMEM: Out of memory.
533  * -EINVAL: Invalid placement flags.
534  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
535  */
536 
537 extern int ttm_bo_init(struct ttm_bo_device *bdev,
538 			struct ttm_buffer_object *bo,
539 			unsigned long size,
540 			enum ttm_bo_type type,
541 			struct ttm_placement *placement,
542 			uint32_t page_alignment,
543 			bool interrubtible,
544 			struct vm_object *persistent_swap_storage,
545 			size_t acc_size,
546 			struct sg_table *sg,
547 			struct reservation_object *resv,
548 			void (*destroy) (struct ttm_buffer_object *));
549 
550 /**
551  * ttm_bo_create
552  *
553  * @bdev: Pointer to a ttm_bo_device struct.
554  * @size: Requested size of buffer object.
555  * @type: Requested type of buffer object.
556  * @placement: Initial placement.
557  * @page_alignment: Data alignment in pages.
558  * @interruptible: If needing to sleep while waiting for GPU resources,
559  * sleep interruptible.
560  * @persistent_swap_storage: Usually the swap storage is deleted for buffers
561  * pinned in physical memory. If this behaviour is not desired, this member
562  * holds a pointer to a persistent shmem object. Typically, this would
563  * point to the shmem object backing a GEM object if TTM is used to back a
564  * GEM user interface.
565  * @p_bo: On successful completion *p_bo points to the created object.
566  *
567  * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
568  * on that object. The destroy function is set to kfree().
569  * Returns
570  * -ENOMEM: Out of memory.
571  * -EINVAL: Invalid placement flags.
572  * -ERESTARTSYS: Interrupted by signal while waiting for resources.
573  */
574 
575 extern int ttm_bo_create(struct ttm_bo_device *bdev,
576 				unsigned long size,
577 				enum ttm_bo_type type,
578 				struct ttm_placement *placement,
579 				uint32_t page_alignment,
580 				bool interruptible,
581 				struct vm_object *persistent_swap_storage,
582 				struct ttm_buffer_object **p_bo);
583 
584 /**
585  * ttm_bo_init_mm
586  *
587  * @bdev: Pointer to a ttm_bo_device struct.
588  * @mem_type: The memory type.
589  * @p_size: size managed area in pages.
590  *
591  * Initialize a manager for a given memory type.
592  * Note: if part of driver firstopen, it must be protected from a
593  * potentially racing lastclose.
594  * Returns:
595  * -EINVAL: invalid size or memory type.
596  * -ENOMEM: Not enough memory.
597  * May also return driver-specified errors.
598  */
599 
600 extern int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
601 				unsigned long p_size);
602 /**
603  * ttm_bo_clean_mm
604  *
605  * @bdev: Pointer to a ttm_bo_device struct.
606  * @mem_type: The memory type.
607  *
608  * Take down a manager for a given memory type after first walking
609  * the LRU list to evict any buffers left alive.
610  *
611  * Normally, this function is part of lastclose() or unload(), and at that
612  * point there shouldn't be any buffers left created by user-space, since
613  * there should've been removed by the file descriptor release() method.
614  * However, before this function is run, make sure to signal all sync objects,
615  * and verify that the delayed delete queue is empty. The driver must also
616  * make sure that there are no NO_EVICT buffers present in this memory type
617  * when the call is made.
618  *
619  * If this function is part of a VT switch, the caller must make sure that
620  * there are no appications currently validating buffers before this
621  * function is called. The caller can do that by first taking the
622  * struct ttm_bo_device::ttm_lock in write mode.
623  *
624  * Returns:
625  * -EINVAL: invalid or uninitialized memory type.
626  * -EBUSY: There are still buffers left in this memory type.
627  */
628 
629 extern int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type);
630 
631 /**
632  * ttm_bo_evict_mm
633  *
634  * @bdev: Pointer to a ttm_bo_device struct.
635  * @mem_type: The memory type.
636  *
637  * Evicts all buffers on the lru list of the memory type.
638  * This is normally part of a VT switch or an
639  * out-of-memory-space-due-to-fragmentation handler.
640  * The caller must make sure that there are no other processes
641  * currently validating buffers, and can do that by taking the
642  * struct ttm_bo_device::ttm_lock in write mode.
643  *
644  * Returns:
645  * -EINVAL: Invalid or uninitialized memory type.
646  * -ERESTARTSYS: The call was interrupted by a signal while waiting to
647  * evict a buffer.
648  */
649 
650 extern int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
651 
652 /**
653  * ttm_kmap_obj_virtual
654  *
655  * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
656  * @is_iomem: Pointer to an integer that on return indicates 1 if the
657  * virtual map is io memory, 0 if normal memory.
658  *
659  * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
660  * If *is_iomem is 1 on return, the virtual address points to an io memory area,
661  * that should strictly be accessed by the iowriteXX() and similar functions.
662  */
663 
664 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
665 					 bool *is_iomem)
666 {
667 	*is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
668 	return map->virtual;
669 }
670 
671 /**
672  * ttm_bo_kmap
673  *
674  * @bo: The buffer object.
675  * @start_page: The first page to map.
676  * @num_pages: Number of pages to map.
677  * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
678  *
679  * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
680  * data in the buffer object. The ttm_kmap_obj_virtual function can then be
681  * used to obtain a virtual address to the data.
682  *
683  * Returns
684  * -ENOMEM: Out of memory.
685  * -EINVAL: Invalid range.
686  */
687 
688 extern int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
689 		       unsigned long num_pages, struct ttm_bo_kmap_obj *map);
690 
691 /**
692  * ttm_bo_kunmap
693  *
694  * @map: Object describing the map to unmap.
695  *
696  * Unmaps a kernel map set up by ttm_bo_kmap.
697  */
698 
699 extern void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
700 
701 /**
702  * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object.
703  *
704  * @vma:       vma as input from the fbdev mmap method.
705  * @bo:        The bo backing the address space. The address space will
706  * have the same size as the bo, and start at offset 0.
707  *
708  * This function is intended to be called by the fbdev mmap method
709  * if the fbdev address space is to be backed by a bo.
710  */
711 
712 extern int ttm_fbdev_mmap(struct vm_area_struct *vma,
713 			  struct ttm_buffer_object *bo);
714 
715 /**
716  * ttm_bo_default_iomem_pfn - get a pfn for a page offset
717  *
718  * @bo: the BO we need to look up the pfn for
719  * @page_offset: offset inside the BO to look up.
720  *
721  * Calculate the PFN for iomem based mappings during page fault
722  */
723 unsigned long ttm_bo_default_io_mem_pfn(struct ttm_buffer_object *bo,
724 				        unsigned long page_offset);
725 
726 /**
727  * ttm_bo_mmap - mmap out of the ttm device address space.
728  *
729  * @filp:      filp as input from the mmap method.
730  * @vma:       vma as input from the mmap method.
731  * @bdev:      Pointer to the ttm_bo_device with the address space manager.
732  *
733  * This function is intended to be called by the device mmap method.
734  * if the device address space is to be backed by the bo manager.
735  */
736 
737 extern int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
738 		       struct ttm_bo_device *bdev);
739 
740 /**
741  * ttm_bo_io
742  *
743  * @bdev:      Pointer to the struct ttm_bo_device.
744  * @filp:      Pointer to the struct file attempting to read / write.
745  * @wbuf:      User-space pointer to address of buffer to write. NULL on read.
746  * @rbuf:      User-space pointer to address of buffer to read into.
747  * Null on write.
748  * @count:     Number of bytes to read / write.
749  * @f_pos:     Pointer to current file position.
750  * @write:     1 for read, 0 for write.
751  *
752  * This function implements read / write into ttm buffer objects, and is
753  * intended to
754  * be called from the fops::read and fops::write method.
755  * Returns:
756  * See man (2) write, man(2) read. In particular,
757  * the function may return -ERESTARTSYS if
758  * interrupted by a signal.
759  */
760 
761 extern ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
762 			 const char __user *wbuf, char __user *rbuf,
763 			 size_t count, loff_t *f_pos, bool write);
764 
765 extern void ttm_bo_swapout_all(struct ttm_bo_device *bdev);
766 extern int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo);
767 #endif
768