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