Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[pandora-kernel.git] / include / drm / ttm / ttm_bo_driver.h
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 "drm_mm.h"
36 #include "linux/workqueue.h"
37 #include "linux/fs.h"
38 #include "linux/spinlock.h"
39
40 struct ttm_backend;
41
42 struct ttm_backend_func {
43         /**
44          * struct ttm_backend_func member populate
45          *
46          * @backend: Pointer to a struct ttm_backend.
47          * @num_pages: Number of pages to populate.
48          * @pages: Array of pointers to ttm pages.
49          * @dummy_read_page: Page to be used instead of NULL pages in the
50          * array @pages.
51          *
52          * Populate the backend with ttm pages. Depending on the backend,
53          * it may or may not copy the @pages array.
54          */
55         int (*populate) (struct ttm_backend *backend,
56                          unsigned long num_pages, struct page **pages,
57                          struct page *dummy_read_page);
58         /**
59          * struct ttm_backend_func member clear
60          *
61          * @backend: Pointer to a struct ttm_backend.
62          *
63          * This is an "unpopulate" function. Release all resources
64          * allocated with populate.
65          */
66         void (*clear) (struct ttm_backend *backend);
67
68         /**
69          * struct ttm_backend_func member bind
70          *
71          * @backend: Pointer to a struct ttm_backend.
72          * @bo_mem: Pointer to a struct ttm_mem_reg describing the
73          * memory type and location for binding.
74          *
75          * Bind the backend pages into the aperture in the location
76          * indicated by @bo_mem. This function should be able to handle
77          * differences between aperture- and system page sizes.
78          */
79         int (*bind) (struct ttm_backend *backend, struct ttm_mem_reg *bo_mem);
80
81         /**
82          * struct ttm_backend_func member unbind
83          *
84          * @backend: Pointer to a struct ttm_backend.
85          *
86          * Unbind previously bound backend pages. This function should be
87          * able to handle differences between aperture- and system page sizes.
88          */
89         int (*unbind) (struct ttm_backend *backend);
90
91         /**
92          * struct ttm_backend_func member destroy
93          *
94          * @backend: Pointer to a struct ttm_backend.
95          *
96          * Destroy the backend.
97          */
98         void (*destroy) (struct ttm_backend *backend);
99 };
100
101 /**
102  * struct ttm_backend
103  *
104  * @bdev: Pointer to a struct ttm_bo_device.
105  * @flags: For driver use.
106  * @func: Pointer to a struct ttm_backend_func that describes
107  * the backend methods.
108  *
109  */
110
111 struct ttm_backend {
112         struct ttm_bo_device *bdev;
113         uint32_t flags;
114         struct ttm_backend_func *func;
115 };
116
117 #define TTM_PAGE_FLAG_VMALLOC         (1 << 0)
118 #define TTM_PAGE_FLAG_USER            (1 << 1)
119 #define TTM_PAGE_FLAG_USER_DIRTY      (1 << 2)
120 #define TTM_PAGE_FLAG_WRITE           (1 << 3)
121 #define TTM_PAGE_FLAG_SWAPPED         (1 << 4)
122 #define TTM_PAGE_FLAG_PERSISTANT_SWAP (1 << 5)
123 #define TTM_PAGE_FLAG_ZERO_ALLOC      (1 << 6)
124
125 enum ttm_caching_state {
126         tt_uncached,
127         tt_wc,
128         tt_cached
129 };
130
131 /**
132  * struct ttm_tt
133  *
134  * @dummy_read_page: Page to map where the ttm_tt page array contains a NULL
135  * pointer.
136  * @pages: Array of pages backing the data.
137  * @first_himem_page: Himem pages are put last in the page array, which
138  * enables us to run caching attribute changes on only the first part
139  * of the page array containing lomem pages. This is the index of the
140  * first himem page.
141  * @last_lomem_page: Index of the last lomem page in the page array.
142  * @num_pages: Number of pages in the page array.
143  * @bdev: Pointer to the current struct ttm_bo_device.
144  * @be: Pointer to the ttm backend.
145  * @tsk: The task for user ttm.
146  * @start: virtual address for user ttm.
147  * @swap_storage: Pointer to shmem struct file for swap storage.
148  * @caching_state: The current caching state of the pages.
149  * @state: The current binding state of the pages.
150  *
151  * This is a structure holding the pages, caching- and aperture binding
152  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
153  * memory.
154  */
155
156 struct ttm_tt {
157         struct page *dummy_read_page;
158         struct page **pages;
159         long first_himem_page;
160         long last_lomem_page;
161         uint32_t page_flags;
162         unsigned long num_pages;
163         struct ttm_bo_device *bdev;
164         struct ttm_backend *be;
165         struct task_struct *tsk;
166         unsigned long start;
167         struct file *swap_storage;
168         enum ttm_caching_state caching_state;
169         enum {
170                 tt_bound,
171                 tt_unbound,
172                 tt_unpopulated,
173         } state;
174 };
175
176 #define TTM_MEMTYPE_FLAG_FIXED         (1 << 0) /* Fixed (on-card) PCI memory */
177 #define TTM_MEMTYPE_FLAG_MAPPABLE      (1 << 1) /* Memory mappable */
178 #define TTM_MEMTYPE_FLAG_NEEDS_IOREMAP (1 << 2) /* Fixed memory needs ioremap
179                                                    before kernel access. */
180 #define TTM_MEMTYPE_FLAG_CMA           (1 << 3) /* Can't map aperture */
181
182 /**
183  * struct ttm_mem_type_manager
184  *
185  * @has_type: The memory type has been initialized.
186  * @use_type: The memory type is enabled.
187  * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory
188  * managed by this memory type.
189  * @gpu_offset: If used, the GPU offset of the first managed page of
190  * fixed memory or the first managed location in an aperture.
191  * @io_offset: The io_offset of the first managed page of IO memory or
192  * the first managed location in an aperture. For TTM_MEMTYPE_FLAG_CMA
193  * memory, this should be set to NULL.
194  * @io_size: The size of a managed IO region (fixed memory or aperture).
195  * @io_addr: Virtual kernel address if the io region is pre-mapped. For
196  * TTM_MEMTYPE_FLAG_NEEDS_IOREMAP there is no pre-mapped io map and
197  * @io_addr should be set to NULL.
198  * @size: Size of the managed region.
199  * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,
200  * as defined in ttm_placement_common.h
201  * @default_caching: The default caching policy used for a buffer object
202  * placed in this memory type if the user doesn't provide one.
203  * @manager: The range manager used for this memory type. FIXME: If the aperture
204  * has a page size different from the underlying system, the granularity
205  * of this manager should take care of this. But the range allocating code
206  * in ttm_bo.c needs to be modified for this.
207  * @lru: The lru list for this memory type.
208  *
209  * This structure is used to identify and manage memory types for a device.
210  * It's set up by the ttm_bo_driver::init_mem_type method.
211  */
212
213 struct ttm_mem_type_manager {
214
215         /*
216          * No protection. Constant from start.
217          */
218
219         bool has_type;
220         bool use_type;
221         uint32_t flags;
222         unsigned long gpu_offset;
223         unsigned long io_offset;
224         unsigned long io_size;
225         void *io_addr;
226         uint64_t size;
227         uint32_t available_caching;
228         uint32_t default_caching;
229
230         /*
231          * Protected by the bdev->lru_lock.
232          * TODO: Consider one lru_lock per ttm_mem_type_manager.
233          * Plays ill with list removal, though.
234          */
235
236         struct drm_mm manager;
237         struct list_head lru;
238 };
239
240 /**
241  * struct ttm_bo_driver
242  *
243  * @mem_type_prio: Priority array of memory types to place a buffer object in
244  * if it fits without evicting buffers from any of these memory types.
245  * @mem_busy_prio: Priority array of memory types to place a buffer object in
246  * if it needs to evict buffers to make room.
247  * @num_mem_type_prio: Number of elements in the @mem_type_prio array.
248  * @num_mem_busy_prio: Number of elements in the @num_mem_busy_prio array.
249  * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
250  * @invalidate_caches: Callback to invalidate read caches when a buffer object
251  * has been evicted.
252  * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
253  * structure.
254  * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
255  * @move: Callback for a driver to hook in accelerated functions to
256  * move a buffer.
257  * If set to NULL, a potentially slow memcpy() move is used.
258  * @sync_obj_signaled: See ttm_fence_api.h
259  * @sync_obj_wait: See ttm_fence_api.h
260  * @sync_obj_flush: See ttm_fence_api.h
261  * @sync_obj_unref: See ttm_fence_api.h
262  * @sync_obj_ref: See ttm_fence_api.h
263  */
264
265 struct ttm_bo_driver {
266         const uint32_t *mem_type_prio;
267         const uint32_t *mem_busy_prio;
268         uint32_t num_mem_type_prio;
269         uint32_t num_mem_busy_prio;
270
271         /**
272          * struct ttm_bo_driver member create_ttm_backend_entry
273          *
274          * @bdev: The buffer object device.
275          *
276          * Create a driver specific struct ttm_backend.
277          */
278
279         struct ttm_backend *(*create_ttm_backend_entry)
280          (struct ttm_bo_device *bdev);
281
282         /**
283          * struct ttm_bo_driver member invalidate_caches
284          *
285          * @bdev: the buffer object device.
286          * @flags: new placement of the rebound buffer object.
287          *
288          * A previosly evicted buffer has been rebound in a
289          * potentially new location. Tell the driver that it might
290          * consider invalidating read (texture) caches on the next command
291          * submission as a consequence.
292          */
293
294         int (*invalidate_caches) (struct ttm_bo_device *bdev, uint32_t flags);
295         int (*init_mem_type) (struct ttm_bo_device *bdev, uint32_t type,
296                               struct ttm_mem_type_manager *man);
297         /**
298          * struct ttm_bo_driver member evict_flags:
299          *
300          * @bo: the buffer object to be evicted
301          *
302          * Return the bo flags for a buffer which is not mapped to the hardware.
303          * These will be placed in proposed_flags so that when the move is
304          * finished, they'll end up in bo->mem.flags
305          */
306
307          uint32_t(*evict_flags) (struct ttm_buffer_object *bo);
308         /**
309          * struct ttm_bo_driver member move:
310          *
311          * @bo: the buffer to move
312          * @evict: whether this motion is evicting the buffer from
313          * the graphics address space
314          * @interruptible: Use interruptible sleeps if possible when sleeping.
315          * @no_wait: whether this should give up and return -EBUSY
316          * if this move would require sleeping
317          * @new_mem: the new memory region receiving the buffer
318          *
319          * Move a buffer between two memory regions.
320          */
321         int (*move) (struct ttm_buffer_object *bo,
322                      bool evict, bool interruptible,
323                      bool no_wait, struct ttm_mem_reg *new_mem);
324
325         /**
326          * struct ttm_bo_driver_member verify_access
327          *
328          * @bo: Pointer to a buffer object.
329          * @filp: Pointer to a struct file trying to access the object.
330          *
331          * Called from the map / write / read methods to verify that the
332          * caller is permitted to access the buffer object.
333          * This member may be set to NULL, which will refuse this kind of
334          * access for all buffer objects.
335          * This function should return 0 if access is granted, -EPERM otherwise.
336          */
337         int (*verify_access) (struct ttm_buffer_object *bo,
338                               struct file *filp);
339
340         /**
341          * In case a driver writer dislikes the TTM fence objects,
342          * the driver writer can replace those with sync objects of
343          * his / her own. If it turns out that no driver writer is
344          * using these. I suggest we remove these hooks and plug in
345          * fences directly. The bo driver needs the following functionality:
346          * See the corresponding functions in the fence object API
347          * documentation.
348          */
349
350         bool (*sync_obj_signaled) (void *sync_obj, void *sync_arg);
351         int (*sync_obj_wait) (void *sync_obj, void *sync_arg,
352                               bool lazy, bool interruptible);
353         int (*sync_obj_flush) (void *sync_obj, void *sync_arg);
354         void (*sync_obj_unref) (void **sync_obj);
355         void *(*sync_obj_ref) (void *sync_obj);
356 };
357
358 #define TTM_NUM_MEM_TYPES 8
359
360 #define TTM_BO_PRIV_FLAG_MOVING  0      /* Buffer object is moving and needs
361                                            idling before CPU mapping */
362 #define TTM_BO_PRIV_FLAG_MAX 1
363 /**
364  * struct ttm_bo_device - Buffer object driver device-specific data.
365  *
366  * @mem_glob: Pointer to a struct ttm_mem_global object for accounting.
367  * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
368  * @count: Current number of buffer object.
369  * @pages: Current number of pinned pages.
370  * @dummy_read_page: Pointer to a dummy page used for mapping requests
371  * of unpopulated pages.
372  * @shrink: A shrink callback object used for buffre object swap.
373  * @ttm_bo_extra_size: Extra size (sizeof(struct ttm_buffer_object) excluded)
374  * used by a buffer object. This is excluding page arrays and backing pages.
375  * @ttm_bo_size: This is @ttm_bo_extra_size + sizeof(struct ttm_buffer_object).
376  * @man: An array of mem_type_managers.
377  * @addr_space_mm: Range manager for the device address space.
378  * lru_lock: Spinlock that protects the buffer+device lru lists and
379  * ddestroy lists.
380  * @nice_mode: Try nicely to wait for buffer idle when cleaning a manager.
381  * If a GPU lockup has been detected, this is forced to 0.
382  * @dev_mapping: A pointer to the struct address_space representing the
383  * device address space.
384  * @wq: Work queue structure for the delayed delete workqueue.
385  *
386  */
387
388 struct ttm_bo_device {
389
390         /*
391          * Constant after bo device init / atomic.
392          */
393
394         struct ttm_mem_global *mem_glob;
395         struct ttm_bo_driver *driver;
396         struct page *dummy_read_page;
397         struct ttm_mem_shrink shrink;
398
399         size_t ttm_bo_extra_size;
400         size_t ttm_bo_size;
401
402         rwlock_t vm_lock;
403         /*
404          * Protected by the vm lock.
405          */
406         struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
407         struct rb_root addr_space_rb;
408         struct drm_mm addr_space_mm;
409
410         /*
411          * Might want to change this to one lock per manager.
412          */
413         spinlock_t lru_lock;
414         /*
415          * Protected by the lru lock.
416          */
417         struct list_head ddestroy;
418         struct list_head swap_lru;
419
420         /*
421          * Protected by load / firstopen / lastclose /unload sync.
422          */
423
424         bool nice_mode;
425         struct address_space *dev_mapping;
426
427         /*
428          * Internal protection.
429          */
430
431         struct delayed_work wq;
432 };
433
434 /**
435  * ttm_flag_masked
436  *
437  * @old: Pointer to the result and original value.
438  * @new: New value of bits.
439  * @mask: Mask of bits to change.
440  *
441  * Convenience function to change a number of bits identified by a mask.
442  */
443
444 static inline uint32_t
445 ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
446 {
447         *old ^= (*old ^ new) & mask;
448         return *old;
449 }
450
451 /**
452  * ttm_tt_create
453  *
454  * @bdev: pointer to a struct ttm_bo_device:
455  * @size: Size of the data needed backing.
456  * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
457  * @dummy_read_page: See struct ttm_bo_device.
458  *
459  * Create a struct ttm_tt to back data with system memory pages.
460  * No pages are actually allocated.
461  * Returns:
462  * NULL: Out of memory.
463  */
464 extern struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev,
465                                     unsigned long size,
466                                     uint32_t page_flags,
467                                     struct page *dummy_read_page);
468
469 /**
470  * ttm_tt_set_user:
471  *
472  * @ttm: The struct ttm_tt to populate.
473  * @tsk: A struct task_struct for which @start is a valid user-space address.
474  * @start: A valid user-space address.
475  * @num_pages: Size in pages of the user memory area.
476  *
477  * Populate a struct ttm_tt with a user-space memory area after first pinning
478  * the pages backing it.
479  * Returns:
480  * !0: Error.
481  */
482
483 extern int ttm_tt_set_user(struct ttm_tt *ttm,
484                            struct task_struct *tsk,
485                            unsigned long start, unsigned long num_pages);
486
487 /**
488  * ttm_ttm_bind:
489  *
490  * @ttm: The struct ttm_tt containing backing pages.
491  * @bo_mem: The struct ttm_mem_reg identifying the binding location.
492  *
493  * Bind the pages of @ttm to an aperture location identified by @bo_mem
494  */
495 extern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
496
497 /**
498  * ttm_ttm_destroy:
499  *
500  * @ttm: The struct ttm_tt.
501  *
502  * Unbind, unpopulate and destroy a struct ttm_tt.
503  */
504 extern void ttm_tt_destroy(struct ttm_tt *ttm);
505
506 /**
507  * ttm_ttm_unbind:
508  *
509  * @ttm: The struct ttm_tt.
510  *
511  * Unbind a struct ttm_tt.
512  */
513 extern void ttm_tt_unbind(struct ttm_tt *ttm);
514
515 /**
516  * ttm_ttm_destroy:
517  *
518  * @ttm: The struct ttm_tt.
519  * @index: Index of the desired page.
520  *
521  * Return a pointer to the struct page backing @ttm at page
522  * index @index. If the page is unpopulated, one will be allocated to
523  * populate that index.
524  *
525  * Returns:
526  * NULL on OOM.
527  */
528 extern struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index);
529
530 /**
531  * ttm_tt_cache_flush:
532  *
533  * @pages: An array of pointers to struct page:s to flush.
534  * @num_pages: Number of pages to flush.
535  *
536  * Flush the data of the indicated pages from the cpu caches.
537  * This is used when changing caching attributes of the pages from
538  * cache-coherent.
539  */
540 extern void ttm_tt_cache_flush(struct page *pages[], unsigned long num_pages);
541
542 /**
543  * ttm_tt_set_placement_caching:
544  *
545  * @ttm A struct ttm_tt the backing pages of which will change caching policy.
546  * @placement: Flag indicating the desired caching policy.
547  *
548  * This function will change caching policy of any default kernel mappings of
549  * the pages backing @ttm. If changing from cached to uncached or
550  * write-combined,
551  * all CPU caches will first be flushed to make sure the data of the pages
552  * hit RAM. This function may be very costly as it involves global TLB
553  * and cache flushes and potential page splitting / combining.
554  */
555 extern int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
556 extern int ttm_tt_swapout(struct ttm_tt *ttm,
557                           struct file *persistant_swap_storage);
558
559 /*
560  * ttm_bo.c
561  */
562
563 /**
564  * ttm_mem_reg_is_pci
565  *
566  * @bdev: Pointer to a struct ttm_bo_device.
567  * @mem: A valid struct ttm_mem_reg.
568  *
569  * Returns true if the memory described by @mem is PCI memory,
570  * false otherwise.
571  */
572 extern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,
573                                    struct ttm_mem_reg *mem);
574
575 /**
576  * ttm_bo_mem_space
577  *
578  * @bo: Pointer to a struct ttm_buffer_object. the data of which
579  * we want to allocate space for.
580  * @proposed_placement: Proposed new placement for the buffer object.
581  * @mem: A struct ttm_mem_reg.
582  * @interruptible: Sleep interruptible when sliping.
583  * @no_wait: Don't sleep waiting for space to become available.
584  *
585  * Allocate memory space for the buffer object pointed to by @bo, using
586  * the placement flags in @mem, potentially evicting other idle buffer objects.
587  * This function may sleep while waiting for space to become available.
588  * Returns:
589  * -EBUSY: No space available (only if no_wait == 1).
590  * -ENOMEM: Could not allocate memory for the buffer object, either due to
591  * fragmentation or concurrent allocators.
592  * -ERESTART: An interruptible sleep was interrupted by a signal.
593  */
594 extern int ttm_bo_mem_space(struct ttm_buffer_object *bo,
595                             uint32_t proposed_placement,
596                             struct ttm_mem_reg *mem,
597                             bool interruptible, bool no_wait);
598 /**
599  * ttm_bo_wait_for_cpu
600  *
601  * @bo: Pointer to a struct ttm_buffer_object.
602  * @no_wait: Don't sleep while waiting.
603  *
604  * Wait until a buffer object is no longer sync'ed for CPU access.
605  * Returns:
606  * -EBUSY: Buffer object was sync'ed for CPU access. (only if no_wait == 1).
607  * -ERESTART: An interruptible sleep was interrupted by a signal.
608  */
609
610 extern int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait);
611
612 /**
613  * ttm_bo_pci_offset - Get the PCI offset for the buffer object memory.
614  *
615  * @bo Pointer to a struct ttm_buffer_object.
616  * @bus_base On return the base of the PCI region
617  * @bus_offset On return the byte offset into the PCI region
618  * @bus_size On return the byte size of the buffer object or zero if
619  * the buffer object memory is not accessible through a PCI region.
620  *
621  * Returns:
622  * -EINVAL if the buffer object is currently not mappable.
623  * 0 otherwise.
624  */
625
626 extern int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
627                              struct ttm_mem_reg *mem,
628                              unsigned long *bus_base,
629                              unsigned long *bus_offset,
630                              unsigned long *bus_size);
631
632 extern int ttm_bo_device_release(struct ttm_bo_device *bdev);
633
634 /**
635  * ttm_bo_device_init
636  *
637  * @bdev: A pointer to a struct ttm_bo_device to initialize.
638  * @mem_global: A pointer to an initialized struct ttm_mem_global.
639  * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
640  * @file_page_offset: Offset into the device address space that is available
641  * for buffer data. This ensures compatibility with other users of the
642  * address space.
643  *
644  * Initializes a struct ttm_bo_device:
645  * Returns:
646  * !0: Failure.
647  */
648 extern int ttm_bo_device_init(struct ttm_bo_device *bdev,
649                               struct ttm_mem_global *mem_glob,
650                               struct ttm_bo_driver *driver,
651                               uint64_t file_page_offset);
652
653 /**
654  * ttm_bo_reserve:
655  *
656  * @bo: A pointer to a struct ttm_buffer_object.
657  * @interruptible: Sleep interruptible if waiting.
658  * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
659  * @use_sequence: If @bo is already reserved, Only sleep waiting for
660  * it to become unreserved if @sequence < (@bo)->sequence.
661  *
662  * Locks a buffer object for validation. (Or prevents other processes from
663  * locking it for validation) and removes it from lru lists, while taking
664  * a number of measures to prevent deadlocks.
665  *
666  * Deadlocks may occur when two processes try to reserve multiple buffers in
667  * different order, either by will or as a result of a buffer being evicted
668  * to make room for a buffer already reserved. (Buffers are reserved before
669  * they are evicted). The following algorithm prevents such deadlocks from
670  * occuring:
671  * 1) Buffers are reserved with the lru spinlock held. Upon successful
672  * reservation they are removed from the lru list. This stops a reserved buffer
673  * from being evicted. However the lru spinlock is released between the time
674  * a buffer is selected for eviction and the time it is reserved.
675  * Therefore a check is made when a buffer is reserved for eviction, that it
676  * is still the first buffer in the lru list, before it is removed from the
677  * list. @check_lru == 1 forces this check. If it fails, the function returns
678  * -EINVAL, and the caller should then choose a new buffer to evict and repeat
679  * the procedure.
680  * 2) Processes attempting to reserve multiple buffers other than for eviction,
681  * (typically execbuf), should first obtain a unique 32-bit
682  * validation sequence number,
683  * and call this function with @use_sequence == 1 and @sequence == the unique
684  * sequence number. If upon call of this function, the buffer object is already
685  * reserved, the validation sequence is checked against the validation
686  * sequence of the process currently reserving the buffer,
687  * and if the current validation sequence is greater than that of the process
688  * holding the reservation, the function returns -EAGAIN. Otherwise it sleeps
689  * waiting for the buffer to become unreserved, after which it retries
690  * reserving.
691  * The caller should, when receiving an -EAGAIN error
692  * release all its buffer reservations, wait for @bo to become unreserved, and
693  * then rerun the validation with the same validation sequence. This procedure
694  * will always guarantee that the process with the lowest validation sequence
695  * will eventually succeed, preventing both deadlocks and starvation.
696  *
697  * Returns:
698  * -EAGAIN: The reservation may cause a deadlock.
699  * Release all buffer reservations, wait for @bo to become unreserved and
700  * try again. (only if use_sequence == 1).
701  * -ERESTART: A wait for the buffer to become unreserved was interrupted by
702  * a signal. Release all buffer reservations and return to user-space.
703  */
704 extern int ttm_bo_reserve(struct ttm_buffer_object *bo,
705                           bool interruptible,
706                           bool no_wait, bool use_sequence, uint32_t sequence);
707
708 /**
709  * ttm_bo_unreserve
710  *
711  * @bo: A pointer to a struct ttm_buffer_object.
712  *
713  * Unreserve a previous reservation of @bo.
714  */
715 extern void ttm_bo_unreserve(struct ttm_buffer_object *bo);
716
717 /**
718  * ttm_bo_wait_unreserved
719  *
720  * @bo: A pointer to a struct ttm_buffer_object.
721  *
722  * Wait for a struct ttm_buffer_object to become unreserved.
723  * This is typically used in the execbuf code to relax cpu-usage when
724  * a potential deadlock condition backoff.
725  */
726 extern int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo,
727                                   bool interruptible);
728
729 /**
730  * ttm_bo_block_reservation
731  *
732  * @bo: A pointer to a struct ttm_buffer_object.
733  * @interruptible: Use interruptible sleep when waiting.
734  * @no_wait: Don't sleep, but rather return -EBUSY.
735  *
736  * Block reservation for validation by simply reserving the buffer.
737  * This is intended for single buffer use only without eviction,
738  * and thus needs no deadlock protection.
739  *
740  * Returns:
741  * -EBUSY: If no_wait == 1 and the buffer is already reserved.
742  * -ERESTART: If interruptible == 1 and the process received a signal
743  * while sleeping.
744  */
745 extern int ttm_bo_block_reservation(struct ttm_buffer_object *bo,
746                                     bool interruptible, bool no_wait);
747
748 /**
749  * ttm_bo_unblock_reservation
750  *
751  * @bo: A pointer to a struct ttm_buffer_object.
752  *
753  * Unblocks reservation leaving lru lists untouched.
754  */
755 extern void ttm_bo_unblock_reservation(struct ttm_buffer_object *bo);
756
757 /*
758  * ttm_bo_util.c
759  */
760
761 /**
762  * ttm_bo_move_ttm
763  *
764  * @bo: A pointer to a struct ttm_buffer_object.
765  * @evict: 1: This is an eviction. Don't try to pipeline.
766  * @no_wait: Never sleep, but rather return with -EBUSY.
767  * @new_mem: struct ttm_mem_reg indicating where to move.
768  *
769  * Optimized move function for a buffer object with both old and
770  * new placement backed by a TTM. The function will, if successful,
771  * free any old aperture space, and set (@new_mem)->mm_node to NULL,
772  * and update the (@bo)->mem placement flags. If unsuccessful, the old
773  * data remains untouched, and it's up to the caller to free the
774  * memory space indicated by @new_mem.
775  * Returns:
776  * !0: Failure.
777  */
778
779 extern int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
780                            bool evict, bool no_wait,
781                            struct ttm_mem_reg *new_mem);
782
783 /**
784  * ttm_bo_move_memcpy
785  *
786  * @bo: A pointer to a struct ttm_buffer_object.
787  * @evict: 1: This is an eviction. Don't try to pipeline.
788  * @no_wait: Never sleep, but rather return with -EBUSY.
789  * @new_mem: struct ttm_mem_reg indicating where to move.
790  *
791  * Fallback move function for a mappable buffer object in mappable memory.
792  * The function will, if successful,
793  * free any old aperture space, and set (@new_mem)->mm_node to NULL,
794  * and update the (@bo)->mem placement flags. If unsuccessful, the old
795  * data remains untouched, and it's up to the caller to free the
796  * memory space indicated by @new_mem.
797  * Returns:
798  * !0: Failure.
799  */
800
801 extern int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
802                               bool evict,
803                               bool no_wait, struct ttm_mem_reg *new_mem);
804
805 /**
806  * ttm_bo_free_old_node
807  *
808  * @bo: A pointer to a struct ttm_buffer_object.
809  *
810  * Utility function to free an old placement after a successful move.
811  */
812 extern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
813
814 /**
815  * ttm_bo_move_accel_cleanup.
816  *
817  * @bo: A pointer to a struct ttm_buffer_object.
818  * @sync_obj: A sync object that signals when moving is complete.
819  * @sync_obj_arg: An argument to pass to the sync object idle / wait
820  * functions.
821  * @evict: This is an evict move. Don't return until the buffer is idle.
822  * @no_wait: Never sleep, but rather return with -EBUSY.
823  * @new_mem: struct ttm_mem_reg indicating where to move.
824  *
825  * Accelerated move function to be called when an accelerated move
826  * has been scheduled. The function will create a new temporary buffer object
827  * representing the old placement, and put the sync object on both buffer
828  * objects. After that the newly created buffer object is unref'd to be
829  * destroyed when the move is complete. This will help pipeline
830  * buffer moves.
831  */
832
833 extern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
834                                      void *sync_obj,
835                                      void *sync_obj_arg,
836                                      bool evict, bool no_wait,
837                                      struct ttm_mem_reg *new_mem);
838 /**
839  * ttm_io_prot
840  *
841  * @c_state: Caching state.
842  * @tmp: Page protection flag for a normal, cached mapping.
843  *
844  * Utility function that returns the pgprot_t that should be used for
845  * setting up a PTE with the caching model indicated by @c_state.
846  */
847 extern pgprot_t ttm_io_prot(enum ttm_caching_state c_state, pgprot_t tmp);
848
849 #if (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
850 #define TTM_HAS_AGP
851 #include <linux/agp_backend.h>
852
853 /**
854  * ttm_agp_backend_init
855  *
856  * @bdev: Pointer to a struct ttm_bo_device.
857  * @bridge: The agp bridge this device is sitting on.
858  *
859  * Create a TTM backend that uses the indicated AGP bridge as an aperture
860  * for TT memory. This function uses the linux agpgart interface to
861  * bind and unbind memory backing a ttm_tt.
862  */
863 extern struct ttm_backend *ttm_agp_backend_init(struct ttm_bo_device *bdev,
864                                                 struct agp_bridge_data *bridge);
865 #endif
866
867 #endif