1 /*	$NetBSD: ttm_bo_api.h,v 1.8 2021/12/19 12:40:44 riastradh Exp $	*/
2 
3 /**************************************************************************
4  *
5  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29 /*
30  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31  */
32 
33 #ifndef _TTM_BO_API_H_
34 #define _TTM_BO_API_H_
35 
36 #ifdef __NetBSD__
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/mutex.h>		/* XXX ugh include order botch */
40 #include <uvm/uvm_object.h>
41 #include <uvm/uvm_param.h>
42 #include <uvm/uvm_prot.h>
43 #endif
44 
45 #include <drm/drm_gem.h>
46 #include <drm/drm_hashtab.h>
47 #include <drm/drm_vma_manager.h>
48 #include <linux/atomic.h>
49 #include <linux/kref.h>
50 #include <linux/list.h>
51 #include <linux/wait.h>
52 #include <linux/mutex.h>
53 #include <linux/mm.h>
54 #include <linux/bitmap.h>
55 #include <linux/dma-resv.h>
56 
57 struct ttm_bo_global;
58 
59 struct ttm_bo_device;
60 
61 struct drm_mm_node;
62 
63 struct ttm_placement;
64 
65 struct ttm_place;
66 
67 struct ttm_lru_bulk_move;
68 
69 /**
70  * struct ttm_bus_placement
71  *
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
77  * @io_reserved_vm:     The VM system has a refcount in @io_reserved_count
78  * @io_reserved_count:  Refcounting the numbers of callers to ttm_mem_io_reserve
79  *
80  * Structure indicating the bus placement of an object.
81  */
82 struct ttm_bus_placement {
83 	void		*addr;
84 	phys_addr_t	base;
85 	unsigned long	size;
86 	unsigned long	offset;
87 	bool		is_iomem;
88 	bool		io_reserved_vm;
89 	uint64_t        io_reserved_count;
90 #ifdef __NetBSD__
91 	bus_space_handle_t	memh;
92 #endif
93 };
94 
95 
96 /**
97  * struct ttm_mem_reg
98  *
99  * @mm_node: Memory manager node.
100  * @size: Requested size of memory region.
101  * @num_pages: Actual size of memory region in pages.
102  * @page_alignment: Page alignment.
103  * @placement: Placement flags.
104  * @bus: Placement on io bus accessible to the CPU
105  *
106  * Structure indicating the placement and space resources used by a
107  * buffer object.
108  */
109 
110 struct ttm_mem_reg {
111 	void *mm_node;
112 	unsigned long start;
113 	unsigned long size;
114 	unsigned long num_pages;
115 	uint32_t page_alignment;
116 	uint32_t mem_type;
117 	uint32_t placement;
118 	struct ttm_bus_placement bus;
119 };
120 
121 /**
122  * enum ttm_bo_type
123  *
124  * @ttm_bo_type_device:	These are 'normal' buffers that can
125  * be mmapped by user space. Each of these bos occupy a slot in the
126  * device address space, that can be used for normal vm operations.
127  *
128  * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
129  * but they cannot be accessed from user-space. For kernel-only use.
130  *
131  * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
132  * driver.
133  */
134 
135 enum ttm_bo_type {
136 	ttm_bo_type_device,
137 	ttm_bo_type_kernel,
138 	ttm_bo_type_sg
139 };
140 
141 struct ttm_tt;
142 
143 /**
144  * struct ttm_buffer_object
145  *
146  * @base: drm_gem_object superclass data.
147  * @bdev: Pointer to the buffer object device structure.
148  * @type: The bo type.
149  * @destroy: Destruction function. If NULL, kfree is used.
150  * @num_pages: Actual number of pages.
151  * @acc_size: Accounted size for this object.
152  * @kref: Reference count of this buffer object. When this refcount reaches
153  * zero, the object is put on the delayed delete list.
154  * @list_kref: List reference count of this buffer object. This member is
155  * used to avoid destruction while the buffer object is still on a list.
156  * Lru lists may keep one refcount, the delayed delete list, and kref != 0
157  * keeps one refcount. When this refcount reaches zero,
158  * the object is destroyed.
159  * @mem: structure describing current placement.
160  * @persistent_swap_storage: Usually the swap storage is deleted for buffers
161  * pinned in physical memory. If this behaviour is not desired, this member
162  * holds a pointer to a persistent shmem object.
163  * @ttm: TTM structure holding system pages.
164  * @evicted: Whether the object was evicted without user-space knowing.
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  * @moving: Fence set when BO is moving
169  * @offset: The current GPU offset, which can have different meanings
170  * depending on the memory type. For SYSTEM type memory, it should be 0.
171  * @cur_placement: Hint of current placement.
172  *
173  * Base class for TTM buffer object, that deals with data placement and CPU
174  * mappings. GPU mappings are really up to the driver, but for simpler GPUs
175  * the driver can usually use the placement offset @offset directly as the
176  * GPU virtual address. For drivers implementing multiple
177  * GPU memory manager contexts, the driver should manage the address space
178  * in these contexts separately and use these objects to get the correct
179  * placement and caching for these GPU maps. This makes it possible to use
180  * these objects for even quite elaborate memory management schemes.
181  * The destroy member, the API visibility of this object makes it possible
182  * to derive driver specific types.
183  */
184 
185 struct ttm_buffer_object {
186 	struct drm_gem_object base;
187 
188 	/**
189 	 * Members constant at init.
190 	 */
191 
192 	struct ttm_bo_device *bdev;
193 	enum ttm_bo_type type;
194 	void (*destroy) (struct ttm_buffer_object *);
195 	unsigned long num_pages;
196 	size_t acc_size;
197 #ifdef __NetBSD__
198 	struct uvm_object uvmobj;
199 #endif
200 
201 	/**
202 	* Members not needing protection.
203 	*/
204 
205 	struct kref kref;
206 	struct kref list_kref;
207 
208 	/**
209 	 * Members protected by the bo::resv::reserved lock.
210 	 */
211 
212 	struct ttm_mem_reg mem;
213 	struct file *persistent_swap_storage;
214 	struct ttm_tt *ttm;
215 	bool evicted;
216 
217 	/**
218 	 * Members protected by the bdev::lru_lock.
219 	 */
220 
221 	struct list_head lru;
222 	struct list_head ddestroy;
223 	struct list_head swap;
224 	struct list_head io_reserve_lru;
225 
226 	/**
227 	 * Members protected by a bo reservation.
228 	 */
229 
230 	struct dma_fence *moving;
231 	unsigned priority;
232 
233 	/**
234 	 * Special members that are protected by the reserve lock
235 	 * and the bo::lock when written to. Can be read with
236 	 * either of these locks held.
237 	 */
238 
239 	uint64_t offset; /* GPU address space is independent of CPU word size */
240 
241 	struct sg_table *sg;
242 };
243 
244 /**
245  * struct ttm_bo_kmap_obj
246  *
247  * @virtual: The current kernel virtual address.
248  * @page: The page when kmap'ing a single page.
249  * @bo_kmap_type: Type of bo_kmap.
250  *
251  * Object describing a kernel mapping. Since a TTM bo may be located
252  * in various memory types with various caching policies, the
253  * mapping can either be an ioremap, a vmap, a kmap or part of a
254  * premapped region.
255  */
256 
257 #define TTM_BO_MAP_IOMEM_MASK 0x80
258 struct ttm_bo_kmap_obj {
259 	void *virtual;
260 #ifdef __NetBSD__
261 	union {
262 		struct {
263 			bus_space_handle_t	memh;
264 			bus_size_t		size;
265 		} io;
266 		struct {
267 			vsize_t			vsize;
268 		} vmapped;
269 		struct {
270 			struct page		*page;
271 		} kmapped;
272 	} u;
273 #else
274 	struct page *page;
275 #endif
276 	enum {
277 		ttm_bo_map_iomap        = 1 | TTM_BO_MAP_IOMEM_MASK,
278 		ttm_bo_map_vmap         = 2,
279 		ttm_bo_map_kmap         = 3,
280 		ttm_bo_map_premapped    = 4 | TTM_BO_MAP_IOMEM_MASK,
281 	} bo_kmap_type;
282 	struct ttm_buffer_object *bo;
283 };
284 
285 /**
286  * struct ttm_operation_ctx
287  *
288  * @interruptible: Sleep interruptible if sleeping.
289  * @no_wait_gpu: Return immediately if the GPU is busy.
290  * @resv: Reservation object to allow reserved evictions with.
291  * @flags: Including the following flags
292  *
293  * Context for TTM operations like changing buffer placement or general memory
294  * allocation.
295  */
296 struct ttm_operation_ctx {
297 	bool interruptible;
298 	bool no_wait_gpu;
299 	struct dma_resv *resv;
300 	uint64_t bytes_moved;
301 	uint32_t flags;
302 };
303 
304 /* Allow eviction of reserved BOs */
305 #define TTM_OPT_FLAG_ALLOW_RES_EVICT		0x1
306 /* when serving page fault or suspend, allow alloc anyway */
307 #define TTM_OPT_FLAG_FORCE_ALLOC		0x2
308 
309 /**
310  * ttm_bo_get - reference a struct ttm_buffer_object
311  *
312  * @bo: The buffer object.
313  */
ttm_bo_get(struct ttm_buffer_object * bo)314 static inline void ttm_bo_get(struct ttm_buffer_object *bo)
315 {
316 	kref_get(&bo->kref);
317 }
318 
319 /**
320  * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless
321  * its refcount has already reached zero.
322  * @bo: The buffer object.
323  *
324  * Used to reference a TTM buffer object in lookups where the object is removed
325  * from the lookup structure during the destructor and for RCU lookups.
326  *
327  * Returns: @bo if the referencing was successful, NULL otherwise.
328  */
329 static inline __must_check struct ttm_buffer_object *
ttm_bo_get_unless_zero(struct ttm_buffer_object * bo)330 ttm_bo_get_unless_zero(struct ttm_buffer_object *bo)
331 {
332 	if (!kref_get_unless_zero(&bo->kref))
333 		return NULL;
334 	return bo;
335 }
336 
337 /**
338  * ttm_bo_wait - wait for buffer idle.
339  *
340  * @bo:  The buffer object.
341  * @interruptible:  Use interruptible wait.
342  * @no_wait:  Return immediately if buffer is busy.
343  *
344  * This function must be called with the bo::mutex held, and makes
345  * sure any previous rendering to the buffer is completed.
346  * Note: It might be necessary to block validations before the
347  * wait by reserving the buffer.
348  * Returns -EBUSY if no_wait is true and the buffer is busy.
349  * Returns -ERESTARTSYS if interrupted by a signal.
350  */
351 int ttm_bo_wait(struct ttm_buffer_object *bo, bool interruptible, bool no_wait);
352 
353 /**
354  * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo
355  *
356  * @placement:  Return immediately if buffer is busy.
357  * @mem:  The struct ttm_mem_reg indicating the region where the bo resides
358  * @new_flags: Describes compatible placement found
359  *
360  * Returns true if the placement is compatible
361  */
362 bool ttm_bo_mem_compat(struct ttm_placement *placement, struct ttm_mem_reg *mem,
363 		       uint32_t *new_flags);
364 
365 /**
366  * ttm_bo_validate
367  *
368  * @bo: The buffer object.
369  * @placement: Proposed placement for the buffer object.
370  * @ctx: validation parameters.
371  *
372  * Changes placement and caching policy of the buffer object
373  * according proposed placement.
374  * Returns
375  * -EINVAL on invalid proposed placement.
376  * -ENOMEM on out-of-memory condition.
377  * -EBUSY if no_wait is true and buffer busy.
378  * -ERESTARTSYS if interrupted by a signal.
379  */
380 int ttm_bo_validate(struct ttm_buffer_object *bo,
381 		    struct ttm_placement *placement,
382 		    struct ttm_operation_ctx *ctx);
383 
384 /**
385  * ttm_bo_put
386  *
387  * @bo: The buffer object.
388  *
389  * Unreference a buffer object.
390  */
391 void ttm_bo_put(struct ttm_buffer_object *bo);
392 
393 /**
394  * ttm_bo_move_to_lru_tail
395  *
396  * @bo: The buffer object.
397  * @bulk: optional bulk move structure to remember BO positions
398  *
399  * Move this BO to the tail of all lru lists used to lookup and reserve an
400  * object. This function must be called with struct ttm_bo_global::lru_lock
401  * held, and is used to make a BO less likely to be considered for eviction.
402  */
403 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
404 			     struct ttm_lru_bulk_move *bulk);
405 
406 /**
407  * ttm_bo_bulk_move_lru_tail
408  *
409  * @bulk: bulk move structure
410  *
411  * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
412  * BO order never changes. Should be called with ttm_bo_global::lru_lock held.
413  */
414 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk);
415 
416 /**
417  * ttm_bo_lock_delayed_workqueue
418  *
419  * Prevent the delayed workqueue from running.
420  * Returns
421  * True if the workqueue was queued at the time
422  */
423 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
424 
425 /**
426  * ttm_bo_unlock_delayed_workqueue
427  *
428  * Allows the delayed workqueue to run.
429  */
430 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched);
431 
432 /**
433  * ttm_bo_eviction_valuable
434  *
435  * @bo: The buffer object to evict
436  * @place: the placement we need to make room for
437  *
438  * Check if it is valuable to evict the BO to make room for the given placement.
439  */
440 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
441 			      const struct ttm_place *place);
442 
443 /**
444  * ttm_bo_acc_size
445  *
446  * @bdev: Pointer to a ttm_bo_device struct.
447  * @bo_size: size of the buffer object in byte.
448  * @struct_size: size of the structure holding buffer object datas
449  *
450  * Returns size to account for a buffer object
451  */
452 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
453 		       unsigned long bo_size,
454 		       unsigned struct_size);
455 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
456 			   unsigned long bo_size,
457 			   unsigned struct_size);
458 
459 /**
460  * ttm_bo_init_reserved
461  *
462  * @bdev: Pointer to a ttm_bo_device struct.
463  * @bo: Pointer to a ttm_buffer_object to be initialized.
464  * @size: Requested size of buffer object.
465  * @type: Requested type of buffer object.
466  * @flags: Initial placement flags.
467  * @page_alignment: Data alignment in pages.
468  * @ctx: TTM operation context for memory allocation.
469  * @acc_size: Accounted size for this object.
470  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
471  * @destroy: Destroy function. Use NULL for kfree().
472  *
473  * This function initializes a pre-allocated struct ttm_buffer_object.
474  * As this object may be part of a larger structure, this function,
475  * together with the @destroy function,
476  * enables driver-specific objects derived from a ttm_buffer_object.
477  *
478  * On successful return, the caller owns an object kref to @bo. The kref and
479  * list_kref are usually set to 1, but note that in some situations, other
480  * tasks may already be holding references to @bo as well.
481  * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
482  * and it is the caller's responsibility to call ttm_bo_unreserve.
483  *
484  * If a failure occurs, the function will call the @destroy function, or
485  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
486  * illegal and will likely cause memory corruption.
487  *
488  * Returns
489  * -ENOMEM: Out of memory.
490  * -EINVAL: Invalid placement flags.
491  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
492  */
493 
494 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
495 			 struct ttm_buffer_object *bo,
496 			 unsigned long size,
497 			 enum ttm_bo_type type,
498 			 struct ttm_placement *placement,
499 			 uint32_t page_alignment,
500 			 struct ttm_operation_ctx *ctx,
501 			 size_t acc_size,
502 			 struct sg_table *sg,
503 			 struct dma_resv *resv,
504 			 void (*destroy) (struct ttm_buffer_object *));
505 
506 /**
507  * ttm_bo_init
508  *
509  * @bdev: Pointer to a ttm_bo_device struct.
510  * @bo: Pointer to a ttm_buffer_object to be initialized.
511  * @size: Requested size of buffer object.
512  * @type: Requested type of buffer object.
513  * @flags: Initial placement flags.
514  * @page_alignment: Data alignment in pages.
515  * @interruptible: If needing to sleep to wait for GPU resources,
516  * sleep interruptible.
517  * pinned in physical memory. If this behaviour is not desired, this member
518  * holds a pointer to a persistent shmem object. Typically, this would
519  * point to the shmem object backing a GEM object if TTM is used to back a
520  * GEM user interface.
521  * @acc_size: Accounted size for this object.
522  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
523  * @destroy: Destroy function. Use NULL for kfree().
524  *
525  * This function initializes a pre-allocated struct ttm_buffer_object.
526  * As this object may be part of a larger structure, this function,
527  * together with the @destroy function,
528  * enables driver-specific objects derived from a ttm_buffer_object.
529  *
530  * On successful return, the caller owns an object kref to @bo. The kref and
531  * list_kref are usually set to 1, but note that in some situations, other
532  * tasks may already be holding references to @bo as well.
533  *
534  * If a failure occurs, the function will call the @destroy function, or
535  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
536  * illegal and will likely cause memory corruption.
537  *
538  * Returns
539  * -ENOMEM: Out of memory.
540  * -EINVAL: Invalid placement flags.
541  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
542  */
543 int ttm_bo_init(struct ttm_bo_device *bdev, struct ttm_buffer_object *bo,
544 		unsigned long size, enum ttm_bo_type type,
545 		struct ttm_placement *placement,
546 		uint32_t page_alignment, bool interrubtible, size_t acc_size,
547 		struct sg_table *sg, struct dma_resv *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  * @p_bo: On successful completion *p_bo points to the created object.
561  *
562  * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
563  * on that object. The destroy function is set to kfree().
564  * Returns
565  * -ENOMEM: Out of memory.
566  * -EINVAL: Invalid placement flags.
567  * -ERESTARTSYS: Interrupted by signal while waiting for resources.
568  */
569 int ttm_bo_create(struct ttm_bo_device *bdev, unsigned long size,
570 		  enum ttm_bo_type type, struct ttm_placement *placement,
571 		  uint32_t page_alignment, bool interruptible,
572 		  struct ttm_buffer_object **p_bo);
573 
574 /**
575  * ttm_bo_init_mm
576  *
577  * @bdev: Pointer to a ttm_bo_device struct.
578  * @mem_type: The memory type.
579  * @p_size: size managed area in pages.
580  *
581  * Initialize a manager for a given memory type.
582  * Note: if part of driver firstopen, it must be protected from a
583  * potentially racing lastclose.
584  * Returns:
585  * -EINVAL: invalid size or memory type.
586  * -ENOMEM: Not enough memory.
587  * May also return driver-specified errors.
588  */
589 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
590 		   unsigned long p_size);
591 
592 /**
593  * ttm_bo_clean_mm
594  *
595  * @bdev: Pointer to a ttm_bo_device struct.
596  * @mem_type: The memory type.
597  *
598  * Take down a manager for a given memory type after first walking
599  * the LRU list to evict any buffers left alive.
600  *
601  * Normally, this function is part of lastclose() or unload(), and at that
602  * point there shouldn't be any buffers left created by user-space, since
603  * there should've been removed by the file descriptor release() method.
604  * However, before this function is run, make sure to signal all sync objects,
605  * and verify that the delayed delete queue is empty. The driver must also
606  * make sure that there are no NO_EVICT buffers present in this memory type
607  * when the call is made.
608  *
609  * If this function is part of a VT switch, the caller must make sure that
610  * there are no appications currently validating buffers before this
611  * function is called. The caller can do that by first taking the
612  * struct ttm_bo_device::ttm_lock in write mode.
613  *
614  * Returns:
615  * -EINVAL: invalid or uninitialized memory type.
616  * -EBUSY: There are still buffers left in this memory type.
617  */
618 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type);
619 
620 /**
621  * ttm_bo_evict_mm
622  *
623  * @bdev: Pointer to a ttm_bo_device struct.
624  * @mem_type: The memory type.
625  *
626  * Evicts all buffers on the lru list of the memory type.
627  * This is normally part of a VT switch or an
628  * out-of-memory-space-due-to-fragmentation handler.
629  * The caller must make sure that there are no other processes
630  * currently validating buffers, and can do that by taking the
631  * struct ttm_bo_device::ttm_lock in write mode.
632  *
633  * Returns:
634  * -EINVAL: Invalid or uninitialized memory type.
635  * -ERESTARTSYS: The call was interrupted by a signal while waiting to
636  * evict a buffer.
637  */
638 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
639 
640 /**
641  * ttm_kmap_obj_virtual
642  *
643  * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
644  * @is_iomem: Pointer to an integer that on return indicates 1 if the
645  * virtual map is io memory, 0 if normal memory.
646  *
647  * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
648  * If *is_iomem is 1 on return, the virtual address points to an io memory area,
649  * that should strictly be accessed by the iowriteXX() and similar functions.
650  */
ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj * map,bool * is_iomem)651 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
652 					 bool *is_iomem)
653 {
654 	*is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
655 	return map->virtual;
656 }
657 
658 /**
659  * ttm_bo_kmap
660  *
661  * @bo: The buffer object.
662  * @start_page: The first page to map.
663  * @num_pages: Number of pages to map.
664  * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
665  *
666  * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
667  * data in the buffer object. The ttm_kmap_obj_virtual function can then be
668  * used to obtain a virtual address to the data.
669  *
670  * Returns
671  * -ENOMEM: Out of memory.
672  * -EINVAL: Invalid range.
673  */
674 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
675 		unsigned long num_pages, struct ttm_bo_kmap_obj *map);
676 
677 /**
678  * ttm_bo_kunmap
679  *
680  * @map: Object describing the map to unmap.
681  *
682  * Unmaps a kernel map set up by ttm_bo_kmap.
683  */
684 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
685 
686 #ifdef __NetBSD__
687 
688 /* XXX ttm_fbdev_mmap?  */
689 
690 extern void ttm_bo_uvm_reference(struct uvm_object *);
691 extern void ttm_bo_uvm_detach(struct uvm_object *);
692 extern int ttm_bo_uvm_fault(struct uvm_faultinfo *, vaddr_t, struct vm_page **,
693     int, int, vm_prot_t, int);
694 extern int ttm_bo_mmap_object(struct ttm_bo_device *, off_t, size_t, vm_prot_t,
695     struct uvm_object **, voff_t *, struct file *);
696 
697 #else
698 
699 /**
700  * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
701  *
702  * @vma:       vma as input from the fbdev mmap method.
703  * @bo:        The bo backing the address space.
704  *
705  * Maps a buffer object.
706  */
707 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
708 
709 /**
710  * ttm_bo_mmap - mmap out of the ttm device address space.
711  *
712  * @filp:      filp as input from the mmap method.
713  * @vma:       vma as input from the mmap method.
714  * @bdev:      Pointer to the ttm_bo_device with the address space manager.
715  *
716  * This function is intended to be called by the device mmap method.
717  * if the device address space is to be backed by the bo manager.
718  */
719 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
720 		struct ttm_bo_device *bdev);
721 
722 #endif	/* __NetBSD__ */
723 
724 void *ttm_kmap_atomic_prot(struct page *page, pgprot_t prot);
725 
726 void ttm_kunmap_atomic_prot(void *addr, pgprot_t prot);
727 
728 /**
729  * ttm_bo_io
730  *
731  * @bdev:      Pointer to the struct ttm_bo_device.
732  * @filp:      Pointer to the struct file attempting to read / write.
733  * @wbuf:      User-space pointer to address of buffer to write. NULL on read.
734  * @rbuf:      User-space pointer to address of buffer to read into.
735  * Null on write.
736  * @count:     Number of bytes to read / write.
737  * @f_pos:     Pointer to current file position.
738  * @write:     1 for read, 0 for write.
739  *
740  * This function implements read / write into ttm buffer objects, and is
741  * intended to
742  * be called from the fops::read and fops::write method.
743  * Returns:
744  * See man (2) write, man(2) read. In particular,
745  * the function may return -ERESTARTSYS if
746  * interrupted by a signal.
747  */
748 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
749 		  const char __user *wbuf, char __user *rbuf,
750 		  size_t count, loff_t *f_pos, bool write);
751 
752 int ttm_bo_swapout(struct ttm_bo_global *glob,
753 			struct ttm_operation_ctx *ctx);
754 void ttm_bo_swapout_all(struct ttm_bo_device *bdev);
755 
756 /**
757  * ttm_bo_uses_embedded_gem_object - check if the given bo uses the
758  * embedded drm_gem_object.
759  *
760  * Most ttm drivers are using gem too, so the embedded
761  * ttm_buffer_object.base will be initialized by the driver (before
762  * calling ttm_bo_init).  It is also possible to use ttm without gem
763  * though (vmwgfx does that).
764  *
765  * This helper will figure whenever a given ttm bo is a gem object too
766  * or not.
767  *
768  * @bo: The bo to check.
769  */
ttm_bo_uses_embedded_gem_object(struct ttm_buffer_object * bo)770 static inline bool ttm_bo_uses_embedded_gem_object(struct ttm_buffer_object *bo)
771 {
772 	return bo->base.dev != NULL;
773 }
774 
775 /* Default number of pre-faulted pages in the TTM fault handler */
776 #define TTM_BO_VM_NUM_PREFAULT 16
777 
778 #ifndef __NetBSD__
779 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
780 			     struct vm_fault *vmf);
781 
782 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
783 				    pgprot_t prot,
784 				    pgoff_t num_prefault);
785 
786 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
787 
788 void ttm_bo_vm_open(struct vm_area_struct *vma);
789 
790 void ttm_bo_vm_close(struct vm_area_struct *vma);
791 
792 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
793 		     void *buf, int len, int write);
794 #endif	/* __NetBSD__ */
795 
796 #endif
797