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 #ifndef _TTM_BO_DRIVER_H_
31 #define _TTM_BO_DRIVER_H_
32 
33 #include <ttm/ttm_bo_api.h>
34 #include <ttm/ttm_memory.h>
35 #include <ttm/ttm_module.h>
36 #include <ttm/ttm_placement.h>
37 #include <drm/drm_mm.h>
38 #include <drm/drm_global.h>
39 #include <drm/drm_vma_manager.h>
40 #include <linux/workqueue.h>
41 #include <linux/fs.h>
42 #include <linux/spinlock.h>
43 #include <linux/reservation.h>
44 
45 struct ttm_backend_func {
46 	/**
47 	 * struct ttm_backend_func member bind
48 	 *
49 	 * @ttm: Pointer to a struct ttm_tt.
50 	 * @bo_mem: Pointer to a struct ttm_mem_reg describing the
51 	 * memory type and location for binding.
52 	 *
53 	 * Bind the backend pages into the aperture in the location
54 	 * indicated by @bo_mem. This function should be able to handle
55 	 * differences between aperture and system page sizes.
56 	 */
57 	int (*bind) (struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
58 
59 	/**
60 	 * struct ttm_backend_func member unbind
61 	 *
62 	 * @ttm: Pointer to a struct ttm_tt.
63 	 *
64 	 * Unbind previously bound backend pages. This function should be
65 	 * able to handle differences between aperture and system page sizes.
66 	 */
67 	int (*unbind) (struct ttm_tt *ttm);
68 
69 	/**
70 	 * struct ttm_backend_func member destroy
71 	 *
72 	 * @ttm: Pointer to a struct ttm_tt.
73 	 *
74 	 * Destroy the backend. This will be call back from ttm_tt_destroy so
75 	 * don't call ttm_tt_destroy from the callback or infinite loop.
76 	 */
77 	void (*destroy) (struct ttm_tt *ttm);
78 };
79 
80 #define TTM_PAGE_FLAG_WRITE           (1 << 3)
81 #define TTM_PAGE_FLAG_SWAPPED         (1 << 4)
82 #define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)
83 #define TTM_PAGE_FLAG_ZERO_ALLOC      (1 << 6)
84 #define TTM_PAGE_FLAG_DMA32           (1 << 7)
85 #define TTM_PAGE_FLAG_SG              (1 << 8)
86 
87 enum ttm_caching_state {
88 	tt_uncached,
89 	tt_wc,
90 	tt_cached
91 };
92 
93 /**
94  * struct ttm_tt
95  *
96  * @bdev: Pointer to a struct ttm_bo_device.
97  * @func: Pointer to a struct ttm_backend_func that describes
98  * the backend methods.
99  * @dummy_read_page: Page to map where the ttm_tt page array contains a NULL
100  * pointer.
101  * @pages: Array of pages backing the data.
102  * @num_pages: Number of pages in the page array.
103  * @bdev: Pointer to the current struct ttm_bo_device.
104  * @be: Pointer to the ttm backend.
105  * @swap_storage: Pointer to shmem struct file for swap storage.
106  * @caching_state: The current caching state of the pages.
107  * @state: The current binding state of the pages.
108  *
109  * This is a structure holding the pages, caching- and aperture binding
110  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
111  * memory.
112  */
113 
114 struct ttm_tt {
115 	struct ttm_bo_device *bdev;
116 	struct ttm_backend_func *func;
117 	struct page *dummy_read_page;
118 	struct page **pages;
119 	uint32_t page_flags;
120 	unsigned long num_pages;
121 	struct sg_table *sg; /* for SG objects via dma-buf */
122 	struct ttm_bo_global *glob;
123 	struct vm_object *swap_storage;
124 	enum ttm_caching_state caching_state;
125 	enum {
126 		tt_bound,
127 		tt_unbound,
128 		tt_unpopulated,
129 	} state;
130 };
131 
132 /**
133  * struct ttm_dma_tt
134  *
135  * @ttm: Base ttm_tt struct.
136  * @cpu_address: The CPU address of the pages
137  * @dma_address: The DMA (bus) addresses of the pages
138  * @pages_list: used by some page allocation backend
139  *
140  * This is a structure holding the pages, caching- and aperture binding
141  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
142  * memory.
143  */
144 struct ttm_dma_tt {
145 	struct ttm_tt ttm;
146 	void **cpu_address;
147 	dma_addr_t *dma_address;
148 	struct list_head pages_list;
149 };
150 
151 #define TTM_MEMTYPE_FLAG_FIXED         (1 << 0)	/* Fixed (on-card) PCI memory */
152 #define TTM_MEMTYPE_FLAG_MAPPABLE      (1 << 1)	/* Memory mappable */
153 #define TTM_MEMTYPE_FLAG_CMA           (1 << 3)	/* Can't map aperture */
154 
155 struct ttm_mem_type_manager;
156 
157 struct ttm_mem_type_manager_func {
158 	/**
159 	 * struct ttm_mem_type_manager member init
160 	 *
161 	 * @man: Pointer to a memory type manager.
162 	 * @p_size: Implementation dependent, but typically the size of the
163 	 * range to be managed in pages.
164 	 *
165 	 * Called to initialize a private range manager. The function is
166 	 * expected to initialize the man::priv member.
167 	 * Returns 0 on success, negative error code on failure.
168 	 */
169 	int  (*init)(struct ttm_mem_type_manager *man, unsigned long p_size);
170 
171 	/**
172 	 * struct ttm_mem_type_manager member takedown
173 	 *
174 	 * @man: Pointer to a memory type manager.
175 	 *
176 	 * Called to undo the setup done in init. All allocated resources
177 	 * should be freed.
178 	 */
179 	int  (*takedown)(struct ttm_mem_type_manager *man);
180 
181 	/**
182 	 * struct ttm_mem_type_manager member get_node
183 	 *
184 	 * @man: Pointer to a memory type manager.
185 	 * @bo: Pointer to the buffer object we're allocating space for.
186 	 * @placement: Placement details.
187 	 * @mem: Pointer to a struct ttm_mem_reg to be filled in.
188 	 *
189 	 * This function should allocate space in the memory type managed
190 	 * by @man. Placement details if
191 	 * applicable are given by @placement. If successful,
192 	 * @mem::mm_node should be set to a non-null value, and
193 	 * @mem::start should be set to a value identifying the beginning
194 	 * of the range allocated, and the function should return zero.
195 	 * If the memory region accommodate the buffer object, @mem::mm_node
196 	 * should be set to NULL, and the function should return 0.
197 	 * If a system error occurred, preventing the request to be fulfilled,
198 	 * the function should return a negative error code.
199 	 *
200 	 * Note that @mem::mm_node will only be dereferenced by
201 	 * struct ttm_mem_type_manager functions and optionally by the driver,
202 	 * which has knowledge of the underlying type.
203 	 *
204 	 * This function may not be called from within atomic context, so
205 	 * an implementation can and must use either a mutex or a spinlock to
206 	 * protect any data structures managing the space.
207 	 */
208 	int  (*get_node)(struct ttm_mem_type_manager *man,
209 			 struct ttm_buffer_object *bo,
210 			 const struct ttm_place *place,
211 			 struct ttm_mem_reg *mem);
212 
213 	/**
214 	 * struct ttm_mem_type_manager member put_node
215 	 *
216 	 * @man: Pointer to a memory type manager.
217 	 * @mem: Pointer to a struct ttm_mem_reg to be filled in.
218 	 *
219 	 * This function frees memory type resources previously allocated
220 	 * and that are identified by @mem::mm_node and @mem::start. May not
221 	 * be called from within atomic context.
222 	 */
223 	void (*put_node)(struct ttm_mem_type_manager *man,
224 			 struct ttm_mem_reg *mem);
225 
226 	/**
227 	 * struct ttm_mem_type_manager member debug
228 	 *
229 	 * @man: Pointer to a memory type manager.
230 	 * @prefix: Prefix to be used in printout to identify the caller.
231 	 *
232 	 * This function is called to print out the state of the memory
233 	 * type manager to aid debugging of out-of-memory conditions.
234 	 * It may not be called from within atomic context.
235 	 */
236 	void (*debug)(struct ttm_mem_type_manager *man, const char *prefix);
237 };
238 
239 /**
240  * struct ttm_mem_type_manager
241  *
242  * @has_type: The memory type has been initialized.
243  * @use_type: The memory type is enabled.
244  * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory
245  * managed by this memory type.
246  * @gpu_offset: If used, the GPU offset of the first managed page of
247  * fixed memory or the first managed location in an aperture.
248  * @size: Size of the managed region.
249  * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,
250  * as defined in ttm_placement_common.h
251  * @default_caching: The default caching policy used for a buffer object
252  * placed in this memory type if the user doesn't provide one.
253  * @func: structure pointer implementing the range manager. See above
254  * @priv: Driver private closure for @func.
255  * @io_reserve_mutex: Mutex optionally protecting shared io_reserve structures
256  * @use_io_reserve_lru: Use an lru list to try to unreserve io_mem_regions
257  * reserved by the TTM vm system.
258  * @io_reserve_lru: Optional lru list for unreserving io mem regions.
259  * @io_reserve_fastpath: Only use bdev::driver::io_mem_reserve to obtain
260  * @move_lock: lock for move fence
261  * static information. bdev::driver::io_mem_free is never used.
262  * @lru: The lru list for this memory type.
263  * @move: The fence of the last pipelined move operation.
264  *
265  * This structure is used to identify and manage memory types for a device.
266  * It's set up by the ttm_bo_driver::init_mem_type method.
267  */
268 
269 
270 
271 struct ttm_mem_type_manager {
272 	struct ttm_bo_device *bdev;
273 
274 	/*
275 	 * No protection. Constant from start.
276 	 */
277 
278 	bool has_type;
279 	bool use_type;
280 	uint32_t flags;
281 	uint64_t gpu_offset; /* GPU address space is independent of CPU word size */
282 	uint64_t size;
283 	uint32_t available_caching;
284 	uint32_t default_caching;
285 	const struct ttm_mem_type_manager_func *func;
286 	void *priv;
287 	struct lock io_reserve_mutex;
288 	bool use_io_reserve_lru;
289 	bool io_reserve_fastpath;
290 	spinlock_t move_lock;
291 
292 	/*
293 	 * Protected by @io_reserve_mutex:
294 	 */
295 
296 	struct list_head io_reserve_lru;
297 
298 	/*
299 	 * Protected by the global->lru_lock.
300 	 */
301 
302 	struct list_head lru;
303 
304 	/*
305 	 * Protected by @move_lock.
306 	 */
307 	struct fence *move;
308 };
309 
310 /**
311  * struct ttm_bo_driver
312  *
313  * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
314  * @invalidate_caches: Callback to invalidate read caches when a buffer object
315  * has been evicted.
316  * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
317  * structure.
318  * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
319  * @move: Callback for a driver to hook in accelerated functions to
320  * move a buffer.
321  * If set to NULL, a potentially slow memcpy() move is used.
322  */
323 
324 struct ttm_bo_driver {
325 	/**
326 	 * ttm_tt_create
327 	 *
328 	 * @bdev: pointer to a struct ttm_bo_device:
329 	 * @size: Size of the data needed backing.
330 	 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
331 	 * @dummy_read_page: See struct ttm_bo_device.
332 	 *
333 	 * Create a struct ttm_tt to back data with system memory pages.
334 	 * No pages are actually allocated.
335 	 * Returns:
336 	 * NULL: Out of memory.
337 	 */
338 	struct ttm_tt *(*ttm_tt_create)(struct ttm_bo_device *bdev,
339 					unsigned long size,
340 					uint32_t page_flags,
341 					struct page *dummy_read_page);
342 
343 	/**
344 	 * ttm_tt_populate
345 	 *
346 	 * @ttm: The struct ttm_tt to contain the backing pages.
347 	 *
348 	 * Allocate all backing pages
349 	 * Returns:
350 	 * -ENOMEM: Out of memory.
351 	 */
352 	int (*ttm_tt_populate)(struct ttm_tt *ttm);
353 
354 	/**
355 	 * ttm_tt_unpopulate
356 	 *
357 	 * @ttm: The struct ttm_tt to contain the backing pages.
358 	 *
359 	 * Free all backing page
360 	 */
361 	void (*ttm_tt_unpopulate)(struct ttm_tt *ttm);
362 
363 	/**
364 	 * struct ttm_bo_driver member invalidate_caches
365 	 *
366 	 * @bdev: the buffer object device.
367 	 * @flags: new placement of the rebound buffer object.
368 	 *
369 	 * A previosly evicted buffer has been rebound in a
370 	 * potentially new location. Tell the driver that it might
371 	 * consider invalidating read (texture) caches on the next command
372 	 * submission as a consequence.
373 	 */
374 
375 	int (*invalidate_caches) (struct ttm_bo_device *bdev, uint32_t flags);
376 	int (*init_mem_type) (struct ttm_bo_device *bdev, uint32_t type,
377 			      struct ttm_mem_type_manager *man);
378 	/**
379 	 * struct ttm_bo_driver member evict_flags:
380 	 *
381 	 * @bo: the buffer object to be evicted
382 	 *
383 	 * Return the bo flags for a buffer which is not mapped to the hardware.
384 	 * These will be placed in proposed_flags so that when the move is
385 	 * finished, they'll end up in bo->mem.flags
386 	 */
387 
388 	 void(*evict_flags) (struct ttm_buffer_object *bo,
389 				struct ttm_placement *placement);
390 	/**
391 	 * struct ttm_bo_driver member move:
392 	 *
393 	 * @bo: the buffer to move
394 	 * @evict: whether this motion is evicting the buffer from
395 	 * the graphics address space
396 	 * @interruptible: Use interruptible sleeps if possible when sleeping.
397 	 * @no_wait: whether this should give up and return -EBUSY
398 	 * if this move would require sleeping
399 	 * @new_mem: the new memory region receiving the buffer
400 	 *
401 	 * Move a buffer between two memory regions.
402 	 */
403 	int (*move) (struct ttm_buffer_object *bo,
404 		     bool evict, bool interruptible,
405 		     bool no_wait_gpu,
406 		     struct ttm_mem_reg *new_mem);
407 
408 	/**
409 	 * struct ttm_bo_driver_member verify_access
410 	 *
411 	 * @bo: Pointer to a buffer object.
412 	 * @filp: Pointer to a struct file trying to access the object.
413 	 *
414 	 * Called from the map / write / read methods to verify that the
415 	 * caller is permitted to access the buffer object.
416 	 * This member may be set to NULL, which will refuse this kind of
417 	 * access for all buffer objects.
418 	 * This function should return 0 if access is granted, -EPERM otherwise.
419 	 */
420 	int (*verify_access) (struct ttm_buffer_object *bo,
421 			      struct file *filp);
422 
423 	/* hook to notify driver about a driver move so it
424 	 * can do tiling things */
425 	void (*move_notify)(struct ttm_buffer_object *bo,
426 			    struct ttm_mem_reg *new_mem);
427 	/* notify the driver we are taking a fault on this BO
428 	 * and have reserved it */
429 	int (*fault_reserve_notify)(struct ttm_buffer_object *bo);
430 
431 	/**
432 	 * notify the driver that we're about to swap out this bo
433 	 */
434 	void (*swap_notify) (struct ttm_buffer_object *bo);
435 
436 	/**
437 	 * Driver callback on when mapping io memory (for bo_move_memcpy
438 	 * for instance). TTM will take care to call io_mem_free whenever
439 	 * the mapping is not use anymore. io_mem_reserve & io_mem_free
440 	 * are balanced.
441 	 */
442 	int (*io_mem_reserve)(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);
443 	void (*io_mem_free)(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);
444 
445 	/**
446 	 * Optional driver callback for when BO is removed from the LRU.
447 	 * Called with LRU lock held immediately before the removal.
448 	 */
449 	void (*lru_removal)(struct ttm_buffer_object *bo);
450 
451 	/**
452 	 * Return the list_head after which a BO should be inserted in the LRU.
453 	 */
454 	struct list_head *(*lru_tail)(struct ttm_buffer_object *bo);
455 	struct list_head *(*swap_lru_tail)(struct ttm_buffer_object *bo);
456 };
457 
458 /**
459  * struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.
460  */
461 
462 struct ttm_bo_global_ref {
463 	struct drm_global_reference ref;
464 	struct ttm_mem_global *mem_glob;
465 };
466 
467 /**
468  * struct ttm_bo_global - Buffer object driver global data.
469  *
470  * @mem_glob: Pointer to a struct ttm_mem_global object for accounting.
471  * @dummy_read_page: Pointer to a dummy page used for mapping requests
472  * of unpopulated pages.
473  * @shrink: A shrink callback object used for buffer object swap.
474  * @device_list_mutex: Mutex protecting the device list.
475  * This mutex is held while traversing the device list for pm options.
476  * @lru_lock: Spinlock protecting the bo subsystem lru lists.
477  * @device_list: List of buffer object devices.
478  * @swap_lru: Lru list of buffer objects used for swapping.
479  */
480 
481 struct ttm_bo_global {
482 
483 	/**
484 	 * Constant after init.
485 	 */
486 
487 	struct kobject kobj;
488 	struct ttm_mem_global *mem_glob;
489 	struct page *dummy_read_page;
490 	struct ttm_mem_shrink shrink;
491 	struct lock device_list_mutex;
492 	struct lock lru_lock;
493 
494 	/**
495 	 * Protected by device_list_mutex.
496 	 */
497 	struct list_head device_list;
498 
499 	/**
500 	 * Protected by the lru_lock.
501 	 */
502 	struct list_head swap_lru;
503 
504 	/**
505 	 * Internal protection.
506 	 */
507 	atomic_t bo_count;
508 };
509 
510 
511 #define TTM_NUM_MEM_TYPES 8
512 
513 #define TTM_BO_PRIV_FLAG_MOVING  0	/* Buffer object is moving and needs
514 					   idling before CPU mapping */
515 #define TTM_BO_PRIV_FLAG_ACTIVE	 1
516 #define TTM_BO_PRIV_FLAG_MAX 2
517 /**
518  * struct ttm_bo_device - Buffer object driver device-specific data.
519  *
520  * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
521  * @man: An array of mem_type_managers.
522  * @vma_manager: Address space manager
523  * lru_lock: Spinlock that protects the buffer+device lru lists and
524  * ddestroy lists.
525  * @dev_mapping: A pointer to the struct address_space representing the
526  * device address space.
527  * @wq: Work queue structure for the delayed delete workqueue.
528  *
529  */
530 
531 struct ttm_bo_device {
532 
533 	/*
534 	 * Constant after bo device init / atomic.
535 	 */
536 	struct list_head device_list;
537 	struct ttm_bo_global *glob;
538 	struct ttm_bo_driver *driver;
539 	struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
540 
541 	/*
542 	 * Protected by internal locks.
543 	 */
544 	struct drm_vma_offset_manager vma_manager;
545 
546 	/*
547 	 * Protected by the global:lru lock.
548 	 */
549 	struct list_head ddestroy;
550 
551 	/*
552 	 * Protected by load / firstopen / lastclose /unload sync.
553 	 */
554 
555 	struct address_space *dev_mapping;
556 
557 	/*
558 	 * Internal protection.
559 	 */
560 
561 	struct delayed_work wq;
562 
563 	bool need_dma32;
564 };
565 
566 /**
567  * ttm_flag_masked
568  *
569  * @old: Pointer to the result and original value.
570  * @new: New value of bits.
571  * @mask: Mask of bits to change.
572  *
573  * Convenience function to change a number of bits identified by a mask.
574  */
575 
576 static inline uint32_t
577 ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
578 {
579 	*old ^= (*old ^ new) & mask;
580 	return *old;
581 }
582 
583 /**
584  * ttm_tt_init
585  *
586  * @ttm: The struct ttm_tt.
587  * @bdev: pointer to a struct ttm_bo_device:
588  * @size: Size of the data needed backing.
589  * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
590  * @dummy_read_page: See struct ttm_bo_device.
591  *
592  * Create a struct ttm_tt to back data with system memory pages.
593  * No pages are actually allocated.
594  * Returns:
595  * NULL: Out of memory.
596  */
597 extern int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
598 			unsigned long size, uint32_t page_flags,
599 			struct page *dummy_read_page);
600 extern int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
601 			   unsigned long size, uint32_t page_flags,
602 			   struct page *dummy_read_page);
603 
604 /**
605  * ttm_tt_fini
606  *
607  * @ttm: the ttm_tt structure.
608  *
609  * Free memory of ttm_tt structure
610  */
611 extern void ttm_tt_fini(struct ttm_tt *ttm);
612 extern void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
613 
614 /**
615  * ttm_ttm_bind:
616  *
617  * @ttm: The struct ttm_tt containing backing pages.
618  * @bo_mem: The struct ttm_mem_reg identifying the binding location.
619  *
620  * Bind the pages of @ttm to an aperture location identified by @bo_mem
621  */
622 extern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
623 
624 /**
625  * ttm_ttm_destroy:
626  *
627  * @ttm: The struct ttm_tt.
628  *
629  * Unbind, unpopulate and destroy common struct ttm_tt.
630  */
631 extern void ttm_tt_destroy(struct ttm_tt *ttm);
632 
633 /**
634  * ttm_ttm_unbind:
635  *
636  * @ttm: The struct ttm_tt.
637  *
638  * Unbind a struct ttm_tt.
639  */
640 extern void ttm_tt_unbind(struct ttm_tt *ttm);
641 
642 /**
643  * ttm_tt_swapin:
644  *
645  * @ttm: The struct ttm_tt.
646  *
647  * Swap in a previously swap out ttm_tt.
648  */
649 extern int ttm_tt_swapin(struct ttm_tt *ttm);
650 
651 /**
652  * ttm_tt_set_placement_caching:
653  *
654  * @ttm A struct ttm_tt the backing pages of which will change caching policy.
655  * @placement: Flag indicating the desired caching policy.
656  *
657  * This function will change caching policy of any default kernel mappings of
658  * the pages backing @ttm. If changing from cached to uncached or
659  * write-combined,
660  * all CPU caches will first be flushed to make sure the data of the pages
661  * hit RAM. This function may be very costly as it involves global TLB
662  * and cache flushes and potential page splitting / combining.
663  */
664 extern int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
665 extern int ttm_tt_swapout(struct ttm_tt *ttm,
666 			  struct vm_object *persistent_swap_storage);
667 
668 /**
669  * ttm_tt_unpopulate - free pages from a ttm
670  *
671  * @ttm: Pointer to the ttm_tt structure
672  *
673  * Calls the driver method to free all pages from a ttm
674  */
675 extern void ttm_tt_unpopulate(struct ttm_tt *ttm);
676 
677 /*
678  * ttm_bo.c
679  */
680 
681 /**
682  * ttm_mem_reg_is_pci
683  *
684  * @bdev: Pointer to a struct ttm_bo_device.
685  * @mem: A valid struct ttm_mem_reg.
686  *
687  * Returns true if the memory described by @mem is PCI memory,
688  * false otherwise.
689  */
690 extern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,
691 				   struct ttm_mem_reg *mem);
692 
693 /**
694  * ttm_bo_mem_space
695  *
696  * @bo: Pointer to a struct ttm_buffer_object. the data of which
697  * we want to allocate space for.
698  * @proposed_placement: Proposed new placement for the buffer object.
699  * @mem: A struct ttm_mem_reg.
700  * @interruptible: Sleep interruptible when sliping.
701  * @no_wait_gpu: Return immediately if the GPU is busy.
702  *
703  * Allocate memory space for the buffer object pointed to by @bo, using
704  * the placement flags in @mem, potentially evicting other idle buffer objects.
705  * This function may sleep while waiting for space to become available.
706  * Returns:
707  * -EBUSY: No space available (only if no_wait == 1).
708  * -ENOMEM: Could not allocate memory for the buffer object, either due to
709  * fragmentation or concurrent allocators.
710  * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
711  */
712 extern int ttm_bo_mem_space(struct ttm_buffer_object *bo,
713 				struct ttm_placement *placement,
714 				struct ttm_mem_reg *mem,
715 				bool interruptible,
716 				bool no_wait_gpu);
717 
718 extern void ttm_bo_mem_put(struct ttm_buffer_object *bo,
719 			   struct ttm_mem_reg *mem);
720 extern void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
721 				  struct ttm_mem_reg *mem);
722 
723 extern void ttm_bo_global_release(struct drm_global_reference *ref);
724 extern int ttm_bo_global_init(struct drm_global_reference *ref);
725 
726 extern int ttm_bo_device_release(struct ttm_bo_device *bdev);
727 
728 /**
729  * ttm_bo_device_init
730  *
731  * @bdev: A pointer to a struct ttm_bo_device to initialize.
732  * @glob: A pointer to an initialized struct ttm_bo_global.
733  * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
734  * @mapping: The address space to use for this bo.
735  * @file_page_offset: Offset into the device address space that is available
736  * for buffer data. This ensures compatibility with other users of the
737  * address space.
738  *
739  * Initializes a struct ttm_bo_device:
740  * Returns:
741  * !0: Failure.
742  */
743 extern int ttm_bo_device_init(struct ttm_bo_device *bdev,
744 			      struct ttm_bo_global *glob,
745 			      struct ttm_bo_driver *driver,
746 			      struct address_space *mapping,
747 			      uint64_t file_page_offset, bool need_dma32);
748 
749 /**
750  * ttm_bo_unmap_virtual
751  *
752  * @bo: tear down the virtual mappings for this BO
753  */
754 extern void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
755 
756 /**
757  * ttm_bo_unmap_virtual
758  *
759  * @bo: tear down the virtual mappings for this BO
760  *
761  * The caller must take ttm_mem_io_lock before calling this function.
762  */
763 extern void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);
764 
765 extern int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo);
766 extern void ttm_mem_io_free_vm(struct ttm_buffer_object *bo);
767 extern int ttm_mem_io_lock(struct ttm_mem_type_manager *man,
768 			   bool interruptible);
769 extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
770 
771 extern void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo);
772 
773 struct list_head *ttm_bo_default_lru_tail(struct ttm_buffer_object *bo);
774 struct list_head *ttm_bo_default_swap_lru_tail(struct ttm_buffer_object *bo);
775 
776 /**
777  * __ttm_bo_reserve:
778  *
779  * @bo: A pointer to a struct ttm_buffer_object.
780  * @interruptible: Sleep interruptible if waiting.
781  * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
782  * @ticket: ticket used to acquire the ww_mutex.
783  *
784  * Will not remove reserved buffers from the lru lists.
785  * Otherwise identical to ttm_bo_reserve.
786  *
787  * Returns:
788  * -EDEADLK: The reservation may cause a deadlock.
789  * Release all buffer reservations, wait for @bo to become unreserved and
790  * try again. (only if use_sequence == 1).
791  * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
792  * a signal. Release all buffer reservations and return to user-space.
793  * -EBUSY: The function needed to sleep, but @no_wait was true
794  * -EALREADY: Bo already reserved using @ticket. This error code will only
795  * be returned if @use_ticket is set to true.
796  */
797 static inline int __ttm_bo_reserve(struct ttm_buffer_object *bo,
798 				   bool interruptible, bool no_wait,
799 				   struct ww_acquire_ctx *ticket)
800 {
801 	int ret = 0;
802 
803 	if (no_wait) {
804 		bool success;
805 		if (WARN_ON(ticket))
806 			return -EBUSY;
807 
808 		success = ww_mutex_trylock(&bo->resv->lock);
809 		return success ? 0 : -EBUSY;
810 	}
811 
812 	if (interruptible)
813 		ret = ww_mutex_lock_interruptible(&bo->resv->lock, ticket);
814 	else
815 		ret = ww_mutex_lock(&bo->resv->lock, ticket);
816 	if (ret == -EINTR)
817 		return -ERESTARTSYS;
818 	return ret;
819 }
820 
821 /**
822  * ttm_bo_reserve:
823  *
824  * @bo: A pointer to a struct ttm_buffer_object.
825  * @interruptible: Sleep interruptible if waiting.
826  * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
827  * @ticket: ticket used to acquire the ww_mutex.
828  *
829  * Locks a buffer object for validation. (Or prevents other processes from
830  * locking it for validation) and removes it from lru lists, while taking
831  * a number of measures to prevent deadlocks.
832  *
833  * Deadlocks may occur when two processes try to reserve multiple buffers in
834  * different order, either by will or as a result of a buffer being evicted
835  * to make room for a buffer already reserved. (Buffers are reserved before
836  * they are evicted). The following algorithm prevents such deadlocks from
837  * occurring:
838  * Processes attempting to reserve multiple buffers other than for eviction,
839  * (typically execbuf), should first obtain a unique 32-bit
840  * validation sequence number,
841  * and call this function with @use_ticket == 1 and @ticket->stamp == the unique
842  * sequence number. If upon call of this function, the buffer object is already
843  * reserved, the validation sequence is checked against the validation
844  * sequence of the process currently reserving the buffer,
845  * and if the current validation sequence is greater than that of the process
846  * holding the reservation, the function returns -EAGAIN. Otherwise it sleeps
847  * waiting for the buffer to become unreserved, after which it retries
848  * reserving.
849  * The caller should, when receiving an -EAGAIN error
850  * release all its buffer reservations, wait for @bo to become unreserved, and
851  * then rerun the validation with the same validation sequence. This procedure
852  * will always guarantee that the process with the lowest validation sequence
853  * will eventually succeed, preventing both deadlocks and starvation.
854  *
855  * Returns:
856  * -EDEADLK: The reservation may cause a deadlock.
857  * Release all buffer reservations, wait for @bo to become unreserved and
858  * try again. (only if use_sequence == 1).
859  * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
860  * a signal. Release all buffer reservations and return to user-space.
861  * -EBUSY: The function needed to sleep, but @no_wait was true
862  * -EALREADY: Bo already reserved using @ticket. This error code will only
863  * be returned if @use_ticket is set to true.
864  */
865 static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
866 				 bool interruptible, bool no_wait,
867 				 struct ww_acquire_ctx *ticket)
868 {
869 	int ret;
870 
871 	WARN_ON(!atomic_read(&bo->kref.refcount));
872 
873 	ret = __ttm_bo_reserve(bo, interruptible, no_wait, ticket);
874 	if (likely(ret == 0))
875 		ttm_bo_del_sub_from_lru(bo);
876 
877 	return ret;
878 }
879 
880 /**
881  * ttm_bo_reserve_slowpath:
882  * @bo: A pointer to a struct ttm_buffer_object.
883  * @interruptible: Sleep interruptible if waiting.
884  * @sequence: Set (@bo)->sequence to this value after lock
885  *
886  * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
887  * from all our other reservations. Because there are no other reservations
888  * held by us, this function cannot deadlock any more.
889  */
890 static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
891 					  bool interruptible,
892 					  struct ww_acquire_ctx *ticket)
893 {
894 	int ret = 0;
895 
896 	WARN_ON(!atomic_read(&bo->kref.refcount));
897 
898 	if (interruptible)
899 		ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
900 						       ticket);
901 	else
902 		ww_mutex_lock_slow(&bo->resv->lock, ticket);
903 
904 	if (likely(ret == 0))
905 		ttm_bo_del_sub_from_lru(bo);
906 	else if (ret == -EINTR)
907 		ret = -ERESTARTSYS;
908 
909 	return ret;
910 }
911 
912 /**
913  * __ttm_bo_unreserve
914  * @bo: A pointer to a struct ttm_buffer_object.
915  *
916  * Unreserve a previous reservation of @bo where the buffer object is
917  * already on lru lists.
918  */
919 static inline void __ttm_bo_unreserve(struct ttm_buffer_object *bo)
920 {
921 	ww_mutex_unlock(&bo->resv->lock);
922 }
923 
924 /**
925  * ttm_bo_unreserve
926  *
927  * @bo: A pointer to a struct ttm_buffer_object.
928  *
929  * Unreserve a previous reservation of @bo.
930  */
931 static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
932 {
933 	if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
934 		lockmgr(&bo->glob->lru_lock, LK_EXCLUSIVE);
935 		ttm_bo_add_to_lru(bo);
936 		lockmgr(&bo->glob->lru_lock, LK_RELEASE);
937 	}
938 	__ttm_bo_unreserve(bo);
939 }
940 
941 /**
942  * ttm_bo_unreserve_ticket
943  * @bo: A pointer to a struct ttm_buffer_object.
944  * @ticket: ww_acquire_ctx used for reserving
945  *
946  * Unreserve a previous reservation of @bo made with @ticket.
947  */
948 static inline void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo,
949 					   struct ww_acquire_ctx *t)
950 {
951 	ttm_bo_unreserve(bo);
952 }
953 
954 /*
955  * ttm_bo_util.c
956  */
957 
958 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
959 		       struct ttm_mem_reg *mem);
960 void ttm_mem_io_free(struct ttm_bo_device *bdev,
961 		     struct ttm_mem_reg *mem);
962 /**
963  * ttm_bo_move_ttm
964  *
965  * @bo: A pointer to a struct ttm_buffer_object.
966  * @interruptible: Sleep interruptible if waiting.
967  * @no_wait_gpu: Return immediately if the GPU is busy.
968  * @new_mem: struct ttm_mem_reg indicating where to move.
969  *
970  * Optimized move function for a buffer object with both old and
971  * new placement backed by a TTM. The function will, if successful,
972  * free any old aperture space, and set (@new_mem)->mm_node to NULL,
973  * and update the (@bo)->mem placement flags. If unsuccessful, the old
974  * data remains untouched, and it's up to the caller to free the
975  * memory space indicated by @new_mem.
976  * Returns:
977  * !0: Failure.
978  */
979 
980 extern int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
981 			   bool interruptible, bool no_wait_gpu,
982 			   struct ttm_mem_reg *new_mem);
983 
984 /**
985  * ttm_bo_move_memcpy
986  *
987  * @bo: A pointer to a struct ttm_buffer_object.
988  * @interruptible: Sleep interruptible if waiting.
989  * @no_wait_gpu: Return immediately if the GPU is busy.
990  * @new_mem: struct ttm_mem_reg indicating where to move.
991  *
992  * Fallback move function for a mappable buffer object in mappable memory.
993  * The function will, if successful,
994  * free any old aperture space, and set (@new_mem)->mm_node to NULL,
995  * and update the (@bo)->mem placement flags. If unsuccessful, the old
996  * data remains untouched, and it's up to the caller to free the
997  * memory space indicated by @new_mem.
998  * Returns:
999  * !0: Failure.
1000  */
1001 
1002 extern int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
1003 			      bool interruptible, bool no_wait_gpu,
1004 			      struct ttm_mem_reg *new_mem);
1005 
1006 /**
1007  * ttm_bo_free_old_node
1008  *
1009  * @bo: A pointer to a struct ttm_buffer_object.
1010  *
1011  * Utility function to free an old placement after a successful move.
1012  */
1013 extern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
1014 
1015 /**
1016  * ttm_bo_move_accel_cleanup.
1017  *
1018  * @bo: A pointer to a struct ttm_buffer_object.
1019  * @fence: A fence object that signals when moving is complete.
1020  * @evict: This is an evict move. Don't return until the buffer is idle.
1021  * @new_mem: struct ttm_mem_reg indicating where to move.
1022  *
1023  * Accelerated move function to be called when an accelerated move
1024  * has been scheduled. The function will create a new temporary buffer object
1025  * representing the old placement, and put the sync object on both buffer
1026  * objects. After that the newly created buffer object is unref'd to be
1027  * destroyed when the move is complete. This will help pipeline
1028  * buffer moves.
1029  */
1030 
1031 extern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
1032 				     struct fence *fence, bool evict,
1033 				     struct ttm_mem_reg *new_mem);
1034 
1035 /**
1036  * ttm_bo_pipeline_move.
1037  *
1038  * @bo: A pointer to a struct ttm_buffer_object.
1039  * @fence: A fence object that signals when moving is complete.
1040  * @evict: This is an evict move. Don't return until the buffer is idle.
1041  * @new_mem: struct ttm_mem_reg indicating where to move.
1042  *
1043  * Function for pipelining accelerated moves. Either free the memory
1044  * immediately or hang it on a temporary buffer object.
1045  */
1046 int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
1047 			 struct fence *fence, bool evict,
1048 			 struct ttm_mem_reg *new_mem);
1049 
1050 /**
1051  * ttm_io_prot
1052  *
1053  * @c_state: Caching state.
1054  * @tmp: Page protection flag for a normal, cached mapping.
1055  *
1056  * Utility function that returns the pgprot_t that should be used for
1057  * setting up a PTE with the caching model indicated by @c_state.
1058  */
1059 extern pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
1060 
1061 extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
1062 
1063 #if IS_ENABLED(CONFIG_AGP)
1064 #include <linux/agp_backend.h>
1065 
1066 /**
1067  * ttm_agp_tt_create
1068  *
1069  * @bdev: Pointer to a struct ttm_bo_device.
1070  * @bridge: The agp bridge this device is sitting on.
1071  * @size: Size of the data needed backing.
1072  * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
1073  * @dummy_read_page: See struct ttm_bo_device.
1074  *
1075  *
1076  * Create a TTM backend that uses the indicated AGP bridge as an aperture
1077  * for TT memory. This function uses the linux agpgart interface to
1078  * bind and unbind memory backing a ttm_tt.
1079  */
1080 extern struct ttm_tt *ttm_agp_tt_create(struct ttm_bo_device *bdev,
1081 					struct agp_bridge_data *bridge,
1082 					unsigned long size, uint32_t page_flags,
1083 					struct page *dummy_read_page);
1084 int ttm_agp_tt_populate(struct ttm_tt *ttm);
1085 void ttm_agp_tt_unpopulate(struct ttm_tt *ttm);
1086 #endif
1087 
1088 /* required for DragonFly VM, see ttm/ttm_bo_vm.c */
1089 struct ttm_bo_device_buffer_objects;
1090 int ttm_bo_cmp_rb_tree_items(struct ttm_buffer_object *a,
1091         struct ttm_buffer_object *b);
1092 RB_PROTOTYPE(ttm_bo_device_buffer_objects, ttm_buffer_object, vm_rb,
1093 	ttm_bo_cmp_rb_tree_items);
1094 
1095 #endif
1096