1247835Skib/**************************************************************************
2247835Skib *
3247835Skib * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA
4247835Skib * All Rights Reserved.
5247835Skib *
6247835Skib * Permission is hereby granted, free of charge, to any person obtaining a
7247835Skib * copy of this software and associated documentation files (the
8247835Skib * "Software"), to deal in the Software without restriction, including
9247835Skib * without limitation the rights to use, copy, modify, merge, publish,
10247835Skib * distribute, sub license, and/or sell copies of the Software, and to
11247835Skib * permit persons to whom the Software is furnished to do so, subject to
12247835Skib * the following conditions:
13247835Skib *
14247835Skib * The above copyright notice and this permission notice (including the
15247835Skib * next paragraph) shall be included in all copies or substantial portions
16247835Skib * of the Software.
17247835Skib *
18247835Skib * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19247835Skib * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20247835Skib * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21247835Skib * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22247835Skib * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23247835Skib * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24247835Skib * USE OR OTHER DEALINGS IN THE SOFTWARE.
25247835Skib *
26247835Skib **************************************************************************/
27247835Skib/*
28247835Skib * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29247835Skib */
30247835Skib/* $FreeBSD$ */
31247835Skib
32247835Skib#ifndef _TTM_BO_DRIVER_H_
33247835Skib#define _TTM_BO_DRIVER_H_
34247835Skib
35247835Skib#include <dev/drm2/drmP.h>
36247835Skib#include <dev/drm2/ttm/ttm_bo_api.h>
37247835Skib#include <dev/drm2/ttm/ttm_memory.h>
38247835Skib#include <dev/drm2/ttm/ttm_module.h>
39247835Skib#include <dev/drm2/drm_global.h>
40247835Skib#include <sys/rwlock.h>
41247835Skib#include <sys/tree.h>
42247835Skib
43247835Skibstruct ttm_backend_func {
44247835Skib	/**
45247835Skib	 * struct ttm_backend_func member bind
46247835Skib	 *
47247835Skib	 * @ttm: Pointer to a struct ttm_tt.
48247835Skib	 * @bo_mem: Pointer to a struct ttm_mem_reg describing the
49247835Skib	 * memory type and location for binding.
50247835Skib	 *
51247835Skib	 * Bind the backend pages into the aperture in the location
52247835Skib	 * indicated by @bo_mem. This function should be able to handle
53247835Skib	 * differences between aperture and system page sizes.
54247835Skib	 */
55247835Skib	int (*bind) (struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
56247835Skib
57247835Skib	/**
58247835Skib	 * struct ttm_backend_func member unbind
59247835Skib	 *
60247835Skib	 * @ttm: Pointer to a struct ttm_tt.
61247835Skib	 *
62247835Skib	 * Unbind previously bound backend pages. This function should be
63247835Skib	 * able to handle differences between aperture and system page sizes.
64247835Skib	 */
65247835Skib	int (*unbind) (struct ttm_tt *ttm);
66247835Skib
67247835Skib	/**
68247835Skib	 * struct ttm_backend_func member destroy
69247835Skib	 *
70247835Skib	 * @ttm: Pointer to a struct ttm_tt.
71247835Skib	 *
72247835Skib	 * Destroy the backend. This will be call back from ttm_tt_destroy so
73247835Skib	 * don't call ttm_tt_destroy from the callback or infinite loop.
74247835Skib	 */
75247835Skib	void (*destroy) (struct ttm_tt *ttm);
76247835Skib};
77247835Skib
78247835Skib#define TTM_PAGE_FLAG_WRITE           (1 << 3)
79247835Skib#define TTM_PAGE_FLAG_SWAPPED         (1 << 4)
80247835Skib#define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)
81247835Skib#define TTM_PAGE_FLAG_ZERO_ALLOC      (1 << 6)
82247835Skib#define TTM_PAGE_FLAG_DMA32           (1 << 7)
83247835Skib#define TTM_PAGE_FLAG_SG              (1 << 8)
84247835Skib
85247835Skibenum ttm_caching_state {
86247835Skib	tt_uncached,
87247835Skib	tt_wc,
88247835Skib	tt_cached
89247835Skib};
90247835Skib
91247835Skib/**
92247835Skib * struct ttm_tt
93247835Skib *
94247835Skib * @bdev: Pointer to a struct ttm_bo_device.
95247835Skib * @func: Pointer to a struct ttm_backend_func that describes
96247835Skib * the backend methods.
97247835Skib * @dummy_read_page: Page to map where the ttm_tt page array contains a NULL
98247835Skib * pointer.
99247835Skib * @pages: Array of pages backing the data.
100247835Skib * @num_pages: Number of pages in the page array.
101247835Skib * @bdev: Pointer to the current struct ttm_bo_device.
102247835Skib * @be: Pointer to the ttm backend.
103247835Skib * @swap_storage: Pointer to shmem struct file for swap storage.
104247835Skib * @caching_state: The current caching state of the pages.
105247835Skib * @state: The current binding state of the pages.
106247835Skib *
107247835Skib * This is a structure holding the pages, caching- and aperture binding
108247835Skib * status for a buffer object that isn't backed by fixed (VRAM / AGP)
109247835Skib * memory.
110247835Skib */
111247835Skib
112247835Skibstruct ttm_tt {
113247835Skib	struct ttm_bo_device *bdev;
114247835Skib	struct ttm_backend_func *func;
115247835Skib	struct vm_page *dummy_read_page;
116247835Skib	struct vm_page **pages;
117247835Skib	uint32_t page_flags;
118247835Skib	unsigned long num_pages;
119247835Skib	struct sg_table *sg; /* for SG objects via dma-buf */
120247835Skib	struct ttm_bo_global *glob;
121247835Skib	struct vm_object *swap_storage;
122247835Skib	enum ttm_caching_state caching_state;
123247835Skib	enum {
124247835Skib		tt_bound,
125247835Skib		tt_unbound,
126247835Skib		tt_unpopulated,
127247835Skib	} state;
128247835Skib};
129247835Skib
130247835Skib/**
131247835Skib * struct ttm_dma_tt
132247835Skib *
133247835Skib * @ttm: Base ttm_tt struct.
134247835Skib * @dma_address: The DMA (bus) addresses of the pages
135247835Skib * @pages_list: used by some page allocation backend
136247835Skib *
137247835Skib * This is a structure holding the pages, caching- and aperture binding
138247835Skib * status for a buffer object that isn't backed by fixed (VRAM / AGP)
139247835Skib * memory.
140247835Skib */
141247835Skibstruct ttm_dma_tt {
142247835Skib	struct ttm_tt ttm;
143247835Skib	dma_addr_t *dma_address;
144247835Skib	struct list_head pages_list;
145247835Skib};
146247835Skib
147247835Skib#define TTM_MEMTYPE_FLAG_FIXED         (1 << 0)	/* Fixed (on-card) PCI memory */
148247835Skib#define TTM_MEMTYPE_FLAG_MAPPABLE      (1 << 1)	/* Memory mappable */
149247835Skib#define TTM_MEMTYPE_FLAG_CMA           (1 << 3)	/* Can't map aperture */
150247835Skib
151247835Skibstruct ttm_mem_type_manager;
152247835Skib
153247835Skibstruct ttm_mem_type_manager_func {
154247835Skib	/**
155247835Skib	 * struct ttm_mem_type_manager member init
156247835Skib	 *
157247835Skib	 * @man: Pointer to a memory type manager.
158247835Skib	 * @p_size: Implementation dependent, but typically the size of the
159247835Skib	 * range to be managed in pages.
160247835Skib	 *
161247835Skib	 * Called to initialize a private range manager. The function is
162247835Skib	 * expected to initialize the man::priv member.
163247835Skib	 * Returns 0 on success, negative error code on failure.
164247835Skib	 */
165247835Skib	int  (*init)(struct ttm_mem_type_manager *man, unsigned long p_size);
166247835Skib
167247835Skib	/**
168247835Skib	 * struct ttm_mem_type_manager member takedown
169247835Skib	 *
170247835Skib	 * @man: Pointer to a memory type manager.
171247835Skib	 *
172247835Skib	 * Called to undo the setup done in init. All allocated resources
173247835Skib	 * should be freed.
174247835Skib	 */
175247835Skib	int  (*takedown)(struct ttm_mem_type_manager *man);
176247835Skib
177247835Skib	/**
178247835Skib	 * struct ttm_mem_type_manager member get_node
179247835Skib	 *
180247835Skib	 * @man: Pointer to a memory type manager.
181247835Skib	 * @bo: Pointer to the buffer object we're allocating space for.
182247835Skib	 * @placement: Placement details.
183247835Skib	 * @mem: Pointer to a struct ttm_mem_reg to be filled in.
184247835Skib	 *
185247835Skib	 * This function should allocate space in the memory type managed
186247835Skib	 * by @man. Placement details if
187247835Skib	 * applicable are given by @placement. If successful,
188247835Skib	 * @mem::mm_node should be set to a non-null value, and
189247835Skib	 * @mem::start should be set to a value identifying the beginning
190247835Skib	 * of the range allocated, and the function should return zero.
191247835Skib	 * If the memory region accommodate the buffer object, @mem::mm_node
192247835Skib	 * should be set to NULL, and the function should return 0.
193247835Skib	 * If a system error occurred, preventing the request to be fulfilled,
194247835Skib	 * the function should return a negative error code.
195247835Skib	 *
196247835Skib	 * Note that @mem::mm_node will only be dereferenced by
197247835Skib	 * struct ttm_mem_type_manager functions and optionally by the driver,
198247835Skib	 * which has knowledge of the underlying type.
199247835Skib	 *
200247835Skib	 * This function may not be called from within atomic context, so
201247835Skib	 * an implementation can and must use either a mutex or a spinlock to
202247835Skib	 * protect any data structures managing the space.
203247835Skib	 */
204247835Skib	int  (*get_node)(struct ttm_mem_type_manager *man,
205247835Skib			 struct ttm_buffer_object *bo,
206247835Skib			 struct ttm_placement *placement,
207247835Skib			 struct ttm_mem_reg *mem);
208247835Skib
209247835Skib	/**
210247835Skib	 * struct ttm_mem_type_manager member put_node
211247835Skib	 *
212247835Skib	 * @man: Pointer to a memory type manager.
213247835Skib	 * @mem: Pointer to a struct ttm_mem_reg to be filled in.
214247835Skib	 *
215247835Skib	 * This function frees memory type resources previously allocated
216247835Skib	 * and that are identified by @mem::mm_node and @mem::start. May not
217247835Skib	 * be called from within atomic context.
218247835Skib	 */
219247835Skib	void (*put_node)(struct ttm_mem_type_manager *man,
220247835Skib			 struct ttm_mem_reg *mem);
221247835Skib
222247835Skib	/**
223247835Skib	 * struct ttm_mem_type_manager member debug
224247835Skib	 *
225247835Skib	 * @man: Pointer to a memory type manager.
226247835Skib	 * @prefix: Prefix to be used in printout to identify the caller.
227247835Skib	 *
228247835Skib	 * This function is called to print out the state of the memory
229247835Skib	 * type manager to aid debugging of out-of-memory conditions.
230247835Skib	 * It may not be called from within atomic context.
231247835Skib	 */
232247835Skib	void (*debug)(struct ttm_mem_type_manager *man, const char *prefix);
233247835Skib};
234247835Skib
235247835Skib/**
236247835Skib * struct ttm_mem_type_manager
237247835Skib *
238247835Skib * @has_type: The memory type has been initialized.
239247835Skib * @use_type: The memory type is enabled.
240247835Skib * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory
241247835Skib * managed by this memory type.
242247835Skib * @gpu_offset: If used, the GPU offset of the first managed page of
243247835Skib * fixed memory or the first managed location in an aperture.
244247835Skib * @size: Size of the managed region.
245247835Skib * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,
246247835Skib * as defined in ttm_placement_common.h
247247835Skib * @default_caching: The default caching policy used for a buffer object
248247835Skib * placed in this memory type if the user doesn't provide one.
249247835Skib * @func: structure pointer implementing the range manager. See above
250247835Skib * @priv: Driver private closure for @func.
251247835Skib * @io_reserve_mutex: Mutex optionally protecting shared io_reserve structures
252247835Skib * @use_io_reserve_lru: Use an lru list to try to unreserve io_mem_regions
253247835Skib * reserved by the TTM vm system.
254247835Skib * @io_reserve_lru: Optional lru list for unreserving io mem regions.
255247835Skib * @io_reserve_fastpath: Only use bdev::driver::io_mem_reserve to obtain
256247835Skib * static information. bdev::driver::io_mem_free is never used.
257247835Skib * @lru: The lru list for this memory type.
258247835Skib *
259247835Skib * This structure is used to identify and manage memory types for a device.
260247835Skib * It's set up by the ttm_bo_driver::init_mem_type method.
261247835Skib */
262247835Skib
263247835Skib
264247835Skib
265247835Skibstruct ttm_mem_type_manager {
266247835Skib	struct ttm_bo_device *bdev;
267247835Skib
268247835Skib	/*
269247835Skib	 * No protection. Constant from start.
270247835Skib	 */
271247835Skib
272247835Skib	bool has_type;
273247835Skib	bool use_type;
274247835Skib	uint32_t flags;
275247835Skib	unsigned long gpu_offset;
276247835Skib	uint64_t size;
277247835Skib	uint32_t available_caching;
278247835Skib	uint32_t default_caching;
279247835Skib	const struct ttm_mem_type_manager_func *func;
280247835Skib	void *priv;
281247835Skib	struct sx io_reserve_mutex;
282247835Skib	bool use_io_reserve_lru;
283247835Skib	bool io_reserve_fastpath;
284247835Skib
285247835Skib	/*
286247835Skib	 * Protected by @io_reserve_mutex:
287247835Skib	 */
288247835Skib
289247835Skib	struct list_head io_reserve_lru;
290247835Skib
291247835Skib	/*
292247835Skib	 * Protected by the global->lru_lock.
293247835Skib	 */
294247835Skib
295247835Skib	struct list_head lru;
296247835Skib};
297247835Skib
298247835Skib/**
299247835Skib * struct ttm_bo_driver
300247835Skib *
301247835Skib * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
302247835Skib * @invalidate_caches: Callback to invalidate read caches when a buffer object
303247835Skib * has been evicted.
304247835Skib * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
305247835Skib * structure.
306247835Skib * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
307247835Skib * @move: Callback for a driver to hook in accelerated functions to
308247835Skib * move a buffer.
309247835Skib * If set to NULL, a potentially slow memcpy() move is used.
310247835Skib * @sync_obj_signaled: See ttm_fence_api.h
311247835Skib * @sync_obj_wait: See ttm_fence_api.h
312247835Skib * @sync_obj_flush: See ttm_fence_api.h
313247835Skib * @sync_obj_unref: See ttm_fence_api.h
314247835Skib * @sync_obj_ref: See ttm_fence_api.h
315247835Skib */
316247835Skib
317247835Skibstruct ttm_bo_driver {
318247835Skib	/**
319247835Skib	 * ttm_tt_create
320247835Skib	 *
321247835Skib	 * @bdev: pointer to a struct ttm_bo_device:
322247835Skib	 * @size: Size of the data needed backing.
323247835Skib	 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
324247835Skib	 * @dummy_read_page: See struct ttm_bo_device.
325247835Skib	 *
326247835Skib	 * Create a struct ttm_tt to back data with system memory pages.
327247835Skib	 * No pages are actually allocated.
328247835Skib	 * Returns:
329247835Skib	 * NULL: Out of memory.
330247835Skib	 */
331247835Skib	struct ttm_tt *(*ttm_tt_create)(struct ttm_bo_device *bdev,
332247835Skib					unsigned long size,
333247835Skib					uint32_t page_flags,
334247835Skib					struct vm_page *dummy_read_page);
335247835Skib
336247835Skib	/**
337247835Skib	 * ttm_tt_populate
338247835Skib	 *
339247835Skib	 * @ttm: The struct ttm_tt to contain the backing pages.
340247835Skib	 *
341247835Skib	 * Allocate all backing pages
342247835Skib	 * Returns:
343247835Skib	 * -ENOMEM: Out of memory.
344247835Skib	 */
345247835Skib	int (*ttm_tt_populate)(struct ttm_tt *ttm);
346247835Skib
347247835Skib	/**
348247835Skib	 * ttm_tt_unpopulate
349247835Skib	 *
350247835Skib	 * @ttm: The struct ttm_tt to contain the backing pages.
351247835Skib	 *
352247835Skib	 * Free all backing page
353247835Skib	 */
354247835Skib	void (*ttm_tt_unpopulate)(struct ttm_tt *ttm);
355247835Skib
356247835Skib	/**
357247835Skib	 * struct ttm_bo_driver member invalidate_caches
358247835Skib	 *
359247835Skib	 * @bdev: the buffer object device.
360247835Skib	 * @flags: new placement of the rebound buffer object.
361247835Skib	 *
362247835Skib	 * A previosly evicted buffer has been rebound in a
363247835Skib	 * potentially new location. Tell the driver that it might
364247835Skib	 * consider invalidating read (texture) caches on the next command
365247835Skib	 * submission as a consequence.
366247835Skib	 */
367247835Skib
368247835Skib	int (*invalidate_caches) (struct ttm_bo_device *bdev, uint32_t flags);
369247835Skib	int (*init_mem_type) (struct ttm_bo_device *bdev, uint32_t type,
370247835Skib			      struct ttm_mem_type_manager *man);
371247835Skib	/**
372247835Skib	 * struct ttm_bo_driver member evict_flags:
373247835Skib	 *
374247835Skib	 * @bo: the buffer object to be evicted
375247835Skib	 *
376247835Skib	 * Return the bo flags for a buffer which is not mapped to the hardware.
377247835Skib	 * These will be placed in proposed_flags so that when the move is
378247835Skib	 * finished, they'll end up in bo->mem.flags
379247835Skib	 */
380247835Skib
381247835Skib	 void(*evict_flags) (struct ttm_buffer_object *bo,
382247835Skib				struct ttm_placement *placement);
383247835Skib	/**
384247835Skib	 * struct ttm_bo_driver member move:
385247835Skib	 *
386247835Skib	 * @bo: the buffer to move
387247835Skib	 * @evict: whether this motion is evicting the buffer from
388247835Skib	 * the graphics address space
389247835Skib	 * @interruptible: Use interruptible sleeps if possible when sleeping.
390247835Skib	 * @no_wait: whether this should give up and return -EBUSY
391247835Skib	 * if this move would require sleeping
392247835Skib	 * @new_mem: the new memory region receiving the buffer
393247835Skib	 *
394247835Skib	 * Move a buffer between two memory regions.
395247835Skib	 */
396247835Skib	int (*move) (struct ttm_buffer_object *bo,
397247835Skib		     bool evict, bool interruptible,
398247835Skib		     bool no_wait_gpu,
399247835Skib		     struct ttm_mem_reg *new_mem);
400247835Skib
401247835Skib	/**
402247835Skib	 * struct ttm_bo_driver_member verify_access
403247835Skib	 *
404247835Skib	 * @bo: Pointer to a buffer object.
405247835Skib	 * @filp: Pointer to a struct file trying to access the object.
406247835Skib	 * FreeBSD: use devfs_get_cdevpriv etc.
407247835Skib	 *
408247835Skib	 * Called from the map / write / read methods to verify that the
409247835Skib	 * caller is permitted to access the buffer object.
410247835Skib	 * This member may be set to NULL, which will refuse this kind of
411247835Skib	 * access for all buffer objects.
412247835Skib	 * This function should return 0 if access is granted, -EPERM otherwise.
413247835Skib	 */
414247835Skib	int (*verify_access) (struct ttm_buffer_object *bo);
415247835Skib
416247835Skib	/**
417247835Skib	 * In case a driver writer dislikes the TTM fence objects,
418247835Skib	 * the driver writer can replace those with sync objects of
419247835Skib	 * his / her own. If it turns out that no driver writer is
420247835Skib	 * using these. I suggest we remove these hooks and plug in
421247835Skib	 * fences directly. The bo driver needs the following functionality:
422247835Skib	 * See the corresponding functions in the fence object API
423247835Skib	 * documentation.
424247835Skib	 */
425247835Skib
426247835Skib	bool (*sync_obj_signaled) (void *sync_obj);
427247835Skib	int (*sync_obj_wait) (void *sync_obj,
428247835Skib			      bool lazy, bool interruptible);
429247835Skib	int (*sync_obj_flush) (void *sync_obj);
430247835Skib	void (*sync_obj_unref) (void **sync_obj);
431247835Skib	void *(*sync_obj_ref) (void *sync_obj);
432247835Skib
433247835Skib	/* hook to notify driver about a driver move so it
434247835Skib	 * can do tiling things */
435247835Skib	void (*move_notify)(struct ttm_buffer_object *bo,
436247835Skib			    struct ttm_mem_reg *new_mem);
437247835Skib	/* notify the driver we are taking a fault on this BO
438247835Skib	 * and have reserved it */
439247835Skib	int (*fault_reserve_notify)(struct ttm_buffer_object *bo);
440247835Skib
441247835Skib	/**
442247835Skib	 * notify the driver that we're about to swap out this bo
443247835Skib	 */
444247835Skib	void (*swap_notify) (struct ttm_buffer_object *bo);
445247835Skib
446247835Skib	/**
447247835Skib	 * Driver callback on when mapping io memory (for bo_move_memcpy
448247835Skib	 * for instance). TTM will take care to call io_mem_free whenever
449247835Skib	 * the mapping is not use anymore. io_mem_reserve & io_mem_free
450247835Skib	 * are balanced.
451247835Skib	 */
452247835Skib	int (*io_mem_reserve)(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);
453247835Skib	void (*io_mem_free)(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);
454247835Skib};
455247835Skib
456247835Skib/**
457247835Skib * struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.
458247835Skib */
459247835Skib
460247835Skibstruct ttm_bo_global_ref {
461247835Skib	struct drm_global_reference ref;
462247835Skib	struct ttm_mem_global *mem_glob;
463247835Skib};
464247835Skib
465247835Skib/**
466247835Skib * struct ttm_bo_global - Buffer object driver global data.
467247835Skib *
468247835Skib * @mem_glob: Pointer to a struct ttm_mem_global object for accounting.
469247835Skib * @dummy_read_page: Pointer to a dummy page used for mapping requests
470247835Skib * of unpopulated pages.
471247835Skib * @shrink: A shrink callback object used for buffer object swap.
472247835Skib * @device_list_mutex: Mutex protecting the device list.
473247835Skib * This mutex is held while traversing the device list for pm options.
474247835Skib * @lru_lock: Spinlock protecting the bo subsystem lru lists.
475247835Skib * @device_list: List of buffer object devices.
476247835Skib * @swap_lru: Lru list of buffer objects used for swapping.
477247835Skib */
478247835Skib
479247835Skibstruct ttm_bo_global {
480247835Skib	u_int kobj_ref;
481247835Skib
482247835Skib	/**
483247835Skib	 * Constant after init.
484247835Skib	 */
485247835Skib
486247835Skib	struct ttm_mem_global *mem_glob;
487247835Skib	struct vm_page *dummy_read_page;
488247835Skib	struct ttm_mem_shrink shrink;
489247835Skib	struct sx device_list_mutex;
490247835Skib	struct mtx lru_lock;
491247835Skib
492247835Skib	/**
493247835Skib	 * Protected by device_list_mutex.
494247835Skib	 */
495247835Skib	struct list_head device_list;
496247835Skib
497247835Skib	/**
498247835Skib	 * Protected by the lru_lock.
499247835Skib	 */
500247835Skib	struct list_head swap_lru;
501247835Skib
502247835Skib	/**
503247835Skib	 * Internal protection.
504247835Skib	 */
505247835Skib	atomic_t bo_count;
506247835Skib};
507247835Skib
508247835Skib
509247835Skib#define TTM_NUM_MEM_TYPES 8
510247835Skib
511247835Skib#define TTM_BO_PRIV_FLAG_MOVING  0	/* Buffer object is moving and needs
512247835Skib					   idling before CPU mapping */
513247835Skib#define TTM_BO_PRIV_FLAG_MAX 1
514247835Skib/**
515247835Skib * struct ttm_bo_device - Buffer object driver device-specific data.
516247835Skib *
517247835Skib * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
518247835Skib * @man: An array of mem_type_managers.
519247835Skib * @fence_lock: Protects the synchronizing members on *all* bos belonging
520247835Skib * to this device.
521247835Skib * @addr_space_mm: Range manager for the device address space.
522247835Skib * lru_lock: Spinlock that protects the buffer+device lru lists and
523247835Skib * ddestroy lists.
524247835Skib * @val_seq: Current validation sequence.
525247835Skib * @dev_mapping: A pointer to the struct address_space representing the
526247835Skib * device address space.
527247835Skib * @wq: Work queue structure for the delayed delete workqueue.
528247835Skib *
529247835Skib */
530247835Skib
531247835Skibstruct ttm_bo_device {
532247835Skib
533247835Skib	/*
534247835Skib	 * Constant after bo device init / atomic.
535247835Skib	 */
536247835Skib	struct list_head device_list;
537247835Skib	struct ttm_bo_global *glob;
538247835Skib	struct ttm_bo_driver *driver;
539247835Skib	struct rwlock vm_lock;
540247835Skib	struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
541247835Skib	struct mtx fence_lock;
542247835Skib	/*
543247835Skib	 * Protected by the vm lock.
544247835Skib	 */
545247835Skib	RB_HEAD(ttm_bo_device_buffer_objects, ttm_buffer_object) addr_space_rb;
546247835Skib	struct drm_mm addr_space_mm;
547247835Skib
548247835Skib	/*
549247835Skib	 * Protected by the global:lru lock.
550247835Skib	 */
551247835Skib	struct list_head ddestroy;
552247835Skib	uint32_t val_seq;
553247835Skib
554247835Skib	/*
555247835Skib	 * Protected by load / firstopen / lastclose /unload sync.
556247835Skib	 */
557247835Skib
558247835Skib	struct address_space *dev_mapping;
559247835Skib
560247835Skib	/*
561247835Skib	 * Internal protection.
562247835Skib	 */
563247835Skib
564247835Skib	struct timeout_task wq;
565247835Skib
566247835Skib	bool need_dma32;
567247835Skib};
568247835Skib
569247835Skib/**
570247835Skib * ttm_flag_masked
571247835Skib *
572247835Skib * @old: Pointer to the result and original value.
573247835Skib * @new: New value of bits.
574247835Skib * @mask: Mask of bits to change.
575247835Skib *
576247835Skib * Convenience function to change a number of bits identified by a mask.
577247835Skib */
578247835Skib
579247835Skibstatic inline uint32_t
580247835Skibttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
581247835Skib{
582247835Skib	*old ^= (*old ^ new) & mask;
583247835Skib	return *old;
584247835Skib}
585247835Skib
586247835Skib/**
587247835Skib * ttm_tt_init
588247835Skib *
589247835Skib * @ttm: The struct ttm_tt.
590247835Skib * @bdev: pointer to a struct ttm_bo_device:
591247835Skib * @size: Size of the data needed backing.
592247835Skib * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
593247835Skib * @dummy_read_page: See struct ttm_bo_device.
594247835Skib *
595247835Skib * Create a struct ttm_tt to back data with system memory pages.
596247835Skib * No pages are actually allocated.
597247835Skib * Returns:
598247835Skib * NULL: Out of memory.
599247835Skib */
600247835Skibextern int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
601247835Skib			unsigned long size, uint32_t page_flags,
602247835Skib			struct vm_page *dummy_read_page);
603247835Skibextern int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
604247835Skib			   unsigned long size, uint32_t page_flags,
605247835Skib			   struct vm_page *dummy_read_page);
606247835Skib
607247835Skib/**
608247835Skib * ttm_tt_fini
609247835Skib *
610247835Skib * @ttm: the ttm_tt structure.
611247835Skib *
612247835Skib * Free memory of ttm_tt structure
613247835Skib */
614247835Skibextern void ttm_tt_fini(struct ttm_tt *ttm);
615247835Skibextern void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
616247835Skib
617247835Skib/**
618247835Skib * ttm_ttm_bind:
619247835Skib *
620247835Skib * @ttm: The struct ttm_tt containing backing pages.
621247835Skib * @bo_mem: The struct ttm_mem_reg identifying the binding location.
622247835Skib *
623247835Skib * Bind the pages of @ttm to an aperture location identified by @bo_mem
624247835Skib */
625247835Skibextern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
626247835Skib
627247835Skib/**
628247835Skib * ttm_ttm_destroy:
629247835Skib *
630247835Skib * @ttm: The struct ttm_tt.
631247835Skib *
632247835Skib * Unbind, unpopulate and destroy common struct ttm_tt.
633247835Skib */
634247835Skibextern void ttm_tt_destroy(struct ttm_tt *ttm);
635247835Skib
636247835Skib/**
637247835Skib * ttm_ttm_unbind:
638247835Skib *
639247835Skib * @ttm: The struct ttm_tt.
640247835Skib *
641247835Skib * Unbind a struct ttm_tt.
642247835Skib */
643247835Skibextern void ttm_tt_unbind(struct ttm_tt *ttm);
644247835Skib
645247835Skib/**
646247835Skib * ttm_tt_swapin:
647247835Skib *
648247835Skib * @ttm: The struct ttm_tt.
649247835Skib *
650247835Skib * Swap in a previously swap out ttm_tt.
651247835Skib */
652247835Skibextern int ttm_tt_swapin(struct ttm_tt *ttm);
653247835Skib
654247835Skib/**
655247835Skib * ttm_tt_cache_flush:
656247835Skib *
657247835Skib * @pages: An array of pointers to struct page:s to flush.
658247835Skib * @num_pages: Number of pages to flush.
659247835Skib *
660247835Skib * Flush the data of the indicated pages from the cpu caches.
661247835Skib * This is used when changing caching attributes of the pages from
662247835Skib * cache-coherent.
663247835Skib */
664247835Skibextern void ttm_tt_cache_flush(struct vm_page *pages[], unsigned long num_pages);
665247835Skib
666247835Skib/**
667247835Skib * ttm_tt_set_placement_caching:
668247835Skib *
669247835Skib * @ttm A struct ttm_tt the backing pages of which will change caching policy.
670247835Skib * @placement: Flag indicating the desired caching policy.
671247835Skib *
672247835Skib * This function will change caching policy of any default kernel mappings of
673247835Skib * the pages backing @ttm. If changing from cached to uncached or
674247835Skib * write-combined,
675247835Skib * all CPU caches will first be flushed to make sure the data of the pages
676247835Skib * hit RAM. This function may be very costly as it involves global TLB
677247835Skib * and cache flushes and potential page splitting / combining.
678247835Skib */
679247835Skibextern int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
680247835Skibextern int ttm_tt_swapout(struct ttm_tt *ttm,
681247835Skib			  struct vm_object *persistent_swap_storage);
682247835Skib
683247835Skib/*
684247835Skib * ttm_bo.c
685247835Skib */
686247835Skib
687247835Skib/**
688247835Skib * ttm_mem_reg_is_pci
689247835Skib *
690247835Skib * @bdev: Pointer to a struct ttm_bo_device.
691247835Skib * @mem: A valid struct ttm_mem_reg.
692247835Skib *
693247835Skib * Returns true if the memory described by @mem is PCI memory,
694247835Skib * false otherwise.
695247835Skib */
696247835Skibextern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,
697247835Skib				   struct ttm_mem_reg *mem);
698247835Skib
699247835Skib/**
700247835Skib * ttm_bo_mem_space
701247835Skib *
702247835Skib * @bo: Pointer to a struct ttm_buffer_object. the data of which
703247835Skib * we want to allocate space for.
704247835Skib * @proposed_placement: Proposed new placement for the buffer object.
705247835Skib * @mem: A struct ttm_mem_reg.
706247835Skib * @interruptible: Sleep interruptible when sliping.
707247835Skib * @no_wait_gpu: Return immediately if the GPU is busy.
708247835Skib *
709247835Skib * Allocate memory space for the buffer object pointed to by @bo, using
710247835Skib * the placement flags in @mem, potentially evicting other idle buffer objects.
711247835Skib * This function may sleep while waiting for space to become available.
712247835Skib * Returns:
713247835Skib * -EBUSY: No space available (only if no_wait == 1).
714247835Skib * -ENOMEM: Could not allocate memory for the buffer object, either due to
715247835Skib * fragmentation or concurrent allocators.
716247835Skib * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
717247835Skib */
718247835Skibextern int ttm_bo_mem_space(struct ttm_buffer_object *bo,
719247835Skib				struct ttm_placement *placement,
720247835Skib				struct ttm_mem_reg *mem,
721247835Skib				bool interruptible,
722247835Skib				bool no_wait_gpu);
723247835Skib
724247835Skibextern void ttm_bo_mem_put(struct ttm_buffer_object *bo,
725247835Skib			   struct ttm_mem_reg *mem);
726247835Skibextern void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
727247835Skib				  struct ttm_mem_reg *mem);
728247835Skib
729247835Skibextern void ttm_bo_global_release(struct drm_global_reference *ref);
730247835Skibextern int ttm_bo_global_init(struct drm_global_reference *ref);
731247835Skib
732247835Skibextern int ttm_bo_device_release(struct ttm_bo_device *bdev);
733247835Skib
734247835Skib/**
735247835Skib * ttm_bo_device_init
736247835Skib *
737247835Skib * @bdev: A pointer to a struct ttm_bo_device to initialize.
738247835Skib * @glob: A pointer to an initialized struct ttm_bo_global.
739247835Skib * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
740247835Skib * @file_page_offset: Offset into the device address space that is available
741247835Skib * for buffer data. This ensures compatibility with other users of the
742247835Skib * address space.
743247835Skib *
744247835Skib * Initializes a struct ttm_bo_device:
745247835Skib * Returns:
746247835Skib * !0: Failure.
747247835Skib */
748247835Skibextern int ttm_bo_device_init(struct ttm_bo_device *bdev,
749247835Skib			      struct ttm_bo_global *glob,
750247835Skib			      struct ttm_bo_driver *driver,
751247835Skib			      uint64_t file_page_offset, bool need_dma32);
752247835Skib
753247835Skib/**
754247835Skib * ttm_bo_unmap_virtual
755247835Skib *
756247835Skib * @bo: tear down the virtual mappings for this BO
757247835Skib */
758247835Skibextern void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
759247835Skib
760247835Skib/**
761247835Skib * ttm_bo_unmap_virtual
762247835Skib *
763247835Skib * @bo: tear down the virtual mappings for this BO
764247835Skib *
765247835Skib * The caller must take ttm_mem_io_lock before calling this function.
766247835Skib */
767247835Skibextern void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);
768247835Skib
769247835Skibextern int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo);
770247835Skibextern void ttm_mem_io_free_vm(struct ttm_buffer_object *bo);
771247835Skibextern int ttm_mem_io_lock(struct ttm_mem_type_manager *man,
772247835Skib			   bool interruptible);
773247835Skibextern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
774247835Skib
775247835Skib
776247835Skib/**
777247835Skib * ttm_bo_reserve:
778247835Skib *
779247835Skib * @bo: A pointer to a struct ttm_buffer_object.
780247835Skib * @interruptible: Sleep interruptible if waiting.
781247835Skib * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
782247835Skib * @use_sequence: If @bo is already reserved, Only sleep waiting for
783247835Skib * it to become unreserved if @sequence < (@bo)->sequence.
784247835Skib *
785247835Skib * Locks a buffer object for validation. (Or prevents other processes from
786247835Skib * locking it for validation) and removes it from lru lists, while taking
787247835Skib * a number of measures to prevent deadlocks.
788247835Skib *
789247835Skib * Deadlocks may occur when two processes try to reserve multiple buffers in
790247835Skib * different order, either by will or as a result of a buffer being evicted
791247835Skib * to make room for a buffer already reserved. (Buffers are reserved before
792247835Skib * they are evicted). The following algorithm prevents such deadlocks from
793247835Skib * occurring:
794254861Sdumbbell * Processes attempting to reserve multiple buffers other than for eviction,
795247835Skib * (typically execbuf), should first obtain a unique 32-bit
796247835Skib * validation sequence number,
797247835Skib * and call this function with @use_sequence == 1 and @sequence == the unique
798247835Skib * sequence number. If upon call of this function, the buffer object is already
799247835Skib * reserved, the validation sequence is checked against the validation
800247835Skib * sequence of the process currently reserving the buffer,
801247835Skib * and if the current validation sequence is greater than that of the process
802247835Skib * holding the reservation, the function returns -EAGAIN. Otherwise it sleeps
803247835Skib * waiting for the buffer to become unreserved, after which it retries
804247835Skib * reserving.
805247835Skib * The caller should, when receiving an -EAGAIN error
806247835Skib * release all its buffer reservations, wait for @bo to become unreserved, and
807247835Skib * then rerun the validation with the same validation sequence. This procedure
808247835Skib * will always guarantee that the process with the lowest validation sequence
809247835Skib * will eventually succeed, preventing both deadlocks and starvation.
810247835Skib *
811247835Skib * Returns:
812247835Skib * -EAGAIN: The reservation may cause a deadlock.
813247835Skib * Release all buffer reservations, wait for @bo to become unreserved and
814247835Skib * try again. (only if use_sequence == 1).
815247835Skib * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
816247835Skib * a signal. Release all buffer reservations and return to user-space.
817247835Skib * -EBUSY: The function needed to sleep, but @no_wait was true
818247835Skib * -EDEADLK: Bo already reserved using @sequence. This error code will only
819247835Skib * be returned if @use_sequence is set to true.
820247835Skib */
821247835Skibextern int ttm_bo_reserve(struct ttm_buffer_object *bo,
822247835Skib			  bool interruptible,
823247835Skib			  bool no_wait, bool use_sequence, uint32_t sequence);
824247835Skib
825254863Sdumbbell/**
826254863Sdumbbell * ttm_bo_reserve_slowpath_nolru:
827254863Sdumbbell * @bo: A pointer to a struct ttm_buffer_object.
828254863Sdumbbell * @interruptible: Sleep interruptible if waiting.
829254863Sdumbbell * @sequence: Set (@bo)->sequence to this value after lock
830254863Sdumbbell *
831254863Sdumbbell * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
832254863Sdumbbell * from all our other reservations. Because there are no other reservations
833254863Sdumbbell * held by us, this function cannot deadlock any more.
834254863Sdumbbell *
835254863Sdumbbell * Will not remove reserved buffers from the lru lists.
836254863Sdumbbell * Otherwise identical to ttm_bo_reserve_slowpath.
837254863Sdumbbell */
838254863Sdumbbellextern int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
839254863Sdumbbell					 bool interruptible,
840254863Sdumbbell					 uint32_t sequence);
841247835Skib
842254863Sdumbbell
843247835Skib/**
844254863Sdumbbell * ttm_bo_reserve_slowpath:
845254863Sdumbbell * @bo: A pointer to a struct ttm_buffer_object.
846254863Sdumbbell * @interruptible: Sleep interruptible if waiting.
847254863Sdumbbell * @sequence: Set (@bo)->sequence to this value after lock
848254863Sdumbbell *
849254863Sdumbbell * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
850254863Sdumbbell * from all our other reservations. Because there are no other reservations
851254863Sdumbbell * held by us, this function cannot deadlock any more.
852254863Sdumbbell */
853254863Sdumbbellextern int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
854254863Sdumbbell				   bool interruptible, uint32_t sequence);
855254863Sdumbbell
856254863Sdumbbell/**
857254861Sdumbbell * ttm_bo_reserve_nolru:
858247835Skib *
859247835Skib * @bo: A pointer to a struct ttm_buffer_object.
860247835Skib * @interruptible: Sleep interruptible if waiting.
861247835Skib * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
862247835Skib * @use_sequence: If @bo is already reserved, Only sleep waiting for
863247835Skib * it to become unreserved if @sequence < (@bo)->sequence.
864247835Skib *
865254861Sdumbbell * Will not remove reserved buffers from the lru lists.
866247835Skib * Otherwise identical to ttm_bo_reserve.
867247835Skib *
868247835Skib * Returns:
869247835Skib * -EAGAIN: The reservation may cause a deadlock.
870247835Skib * Release all buffer reservations, wait for @bo to become unreserved and
871247835Skib * try again. (only if use_sequence == 1).
872247835Skib * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
873247835Skib * a signal. Release all buffer reservations and return to user-space.
874247835Skib * -EBUSY: The function needed to sleep, but @no_wait was true
875247835Skib * -EDEADLK: Bo already reserved using @sequence. This error code will only
876247835Skib * be returned if @use_sequence is set to true.
877247835Skib */
878254861Sdumbbellextern int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
879247835Skib				 bool interruptible,
880247835Skib				 bool no_wait, bool use_sequence,
881247835Skib				 uint32_t sequence);
882247835Skib
883247835Skib/**
884247835Skib * ttm_bo_unreserve
885247835Skib *
886247835Skib * @bo: A pointer to a struct ttm_buffer_object.
887247835Skib *
888247835Skib * Unreserve a previous reservation of @bo.
889247835Skib */
890247835Skibextern void ttm_bo_unreserve(struct ttm_buffer_object *bo);
891247835Skib
892247835Skib/**
893247835Skib * ttm_bo_unreserve_locked
894247835Skib *
895247835Skib * @bo: A pointer to a struct ttm_buffer_object.
896247835Skib *
897247835Skib * Unreserve a previous reservation of @bo.
898247835Skib * Needs to be called with struct ttm_bo_global::lru_lock held.
899247835Skib */
900247835Skibextern void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo);
901247835Skib
902247835Skib/*
903247835Skib * ttm_bo_util.c
904247835Skib */
905247835Skib
906247835Skib/**
907247835Skib * ttm_bo_move_ttm
908247835Skib *
909247835Skib * @bo: A pointer to a struct ttm_buffer_object.
910247835Skib * @evict: 1: This is an eviction. Don't try to pipeline.
911247835Skib * @no_wait_gpu: Return immediately if the GPU is busy.
912247835Skib * @new_mem: struct ttm_mem_reg indicating where to move.
913247835Skib *
914247835Skib * Optimized move function for a buffer object with both old and
915247835Skib * new placement backed by a TTM. The function will, if successful,
916247835Skib * free any old aperture space, and set (@new_mem)->mm_node to NULL,
917247835Skib * and update the (@bo)->mem placement flags. If unsuccessful, the old
918247835Skib * data remains untouched, and it's up to the caller to free the
919247835Skib * memory space indicated by @new_mem.
920247835Skib * Returns:
921247835Skib * !0: Failure.
922247835Skib */
923247835Skib
924247835Skibextern int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
925247835Skib			   bool evict, bool no_wait_gpu,
926247835Skib			   struct ttm_mem_reg *new_mem);
927247835Skib
928247835Skib/**
929247835Skib * ttm_bo_move_memcpy
930247835Skib *
931247835Skib * @bo: A pointer to a struct ttm_buffer_object.
932247835Skib * @evict: 1: This is an eviction. Don't try to pipeline.
933247835Skib * @no_wait_gpu: Return immediately if the GPU is busy.
934247835Skib * @new_mem: struct ttm_mem_reg indicating where to move.
935247835Skib *
936247835Skib * Fallback move function for a mappable buffer object in mappable memory.
937247835Skib * The function will, if successful,
938247835Skib * free any old aperture space, and set (@new_mem)->mm_node to NULL,
939247835Skib * and update the (@bo)->mem placement flags. If unsuccessful, the old
940247835Skib * data remains untouched, and it's up to the caller to free the
941247835Skib * memory space indicated by @new_mem.
942247835Skib * Returns:
943247835Skib * !0: Failure.
944247835Skib */
945247835Skib
946247835Skibextern int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
947247835Skib			      bool evict, bool no_wait_gpu,
948247835Skib			      struct ttm_mem_reg *new_mem);
949247835Skib
950247835Skib/**
951247835Skib * ttm_bo_free_old_node
952247835Skib *
953247835Skib * @bo: A pointer to a struct ttm_buffer_object.
954247835Skib *
955247835Skib * Utility function to free an old placement after a successful move.
956247835Skib */
957247835Skibextern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
958247835Skib
959247835Skib/**
960247835Skib * ttm_bo_move_accel_cleanup.
961247835Skib *
962247835Skib * @bo: A pointer to a struct ttm_buffer_object.
963247835Skib * @sync_obj: A sync object that signals when moving is complete.
964247835Skib * @evict: This is an evict move. Don't return until the buffer is idle.
965247835Skib * @no_wait_gpu: Return immediately if the GPU is busy.
966247835Skib * @new_mem: struct ttm_mem_reg indicating where to move.
967247835Skib *
968247835Skib * Accelerated move function to be called when an accelerated move
969247835Skib * has been scheduled. The function will create a new temporary buffer object
970247835Skib * representing the old placement, and put the sync object on both buffer
971247835Skib * objects. After that the newly created buffer object is unref'd to be
972247835Skib * destroyed when the move is complete. This will help pipeline
973247835Skib * buffer moves.
974247835Skib */
975247835Skib
976247835Skibextern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
977247835Skib				     void *sync_obj,
978247835Skib				     bool evict, bool no_wait_gpu,
979247835Skib				     struct ttm_mem_reg *new_mem);
980247835Skib/**
981247835Skib * ttm_io_prot
982247835Skib *
983247835Skib * @c_state: Caching state.
984247835Skib * @tmp: Page protection flag for a normal, cached mapping.
985247835Skib *
986247835Skib * Utility function that returns the pgprot_t that should be used for
987247835Skib * setting up a PTE with the caching model indicated by @c_state.
988247835Skib */
989247835Skibextern vm_memattr_t ttm_io_prot(uint32_t caching_flags);
990247835Skib
991247835Skibextern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
992247835Skib
993247835Skib#if (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
994247835Skib#define TTM_HAS_AGP
995247835Skib#include <linux/agp_backend.h>
996247835Skib
997247835Skib/**
998247835Skib * ttm_agp_tt_create
999247835Skib *
1000247835Skib * @bdev: Pointer to a struct ttm_bo_device.
1001247835Skib * @bridge: The agp bridge this device is sitting on.
1002247835Skib * @size: Size of the data needed backing.
1003247835Skib * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
1004247835Skib * @dummy_read_page: See struct ttm_bo_device.
1005247835Skib *
1006247835Skib *
1007247835Skib * Create a TTM backend that uses the indicated AGP bridge as an aperture
1008247835Skib * for TT memory. This function uses the linux agpgart interface to
1009247835Skib * bind and unbind memory backing a ttm_tt.
1010247835Skib */
1011247835Skibextern struct ttm_tt *ttm_agp_tt_create(struct ttm_bo_device *bdev,
1012247835Skib					struct agp_bridge_data *bridge,
1013247835Skib					unsigned long size, uint32_t page_flags,
1014247835Skib					struct vm_page *dummy_read_page);
1015247835Skibint ttm_agp_tt_populate(struct ttm_tt *ttm);
1016247835Skibvoid ttm_agp_tt_unpopulate(struct ttm_tt *ttm);
1017247835Skib#endif
1018247835Skib
1019247835Skibint	ttm_bo_cmp_rb_tree_items(struct ttm_buffer_object *a,
1020247835Skib	    struct ttm_buffer_object *b);
1021247835Skib
1022247835SkibRB_PROTOTYPE(ttm_bo_device_buffer_objects, ttm_buffer_object, vm_rb,
1023247835Skib    ttm_bo_cmp_rb_tree_items);
1024247835Skib
1025247835Skib#endif
1026