1 /**************************************************************************
3 * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
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_
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>
40 struct ttm_backend_func {
42 * struct ttm_backend_func member bind
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.
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.
52 int (*bind) (struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
55 * struct ttm_backend_func member unbind
57 * @ttm: Pointer to a struct ttm_tt.
59 * Unbind previously bound backend pages. This function should be
60 * able to handle differences between aperture and system page sizes.
62 int (*unbind) (struct ttm_tt *ttm);
65 * struct ttm_backend_func member destroy
67 * @ttm: Pointer to a struct ttm_tt.
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.
72 void (*destroy) (struct ttm_tt *ttm);
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)
82 enum ttm_caching_state {
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
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.
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)
110 struct ttm_bo_device *bdev;
111 struct ttm_backend_func *func;
112 struct vm_page *dummy_read_page;
113 struct vm_page **pages;
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;
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
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)
140 dma_addr_t *dma_address;
141 struct list_head pages_list;
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 */
148 struct ttm_mem_type_manager;
150 struct ttm_mem_type_manager_func {
152 * struct ttm_mem_type_manager member init
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.
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.
162 int (*init)(struct ttm_mem_type_manager *man, unsigned long p_size);
165 * struct ttm_mem_type_manager member takedown
167 * @man: Pointer to a memory type manager.
169 * Called to undo the setup done in init. All allocated resources
172 int (*takedown)(struct ttm_mem_type_manager *man);
175 * struct ttm_mem_type_manager member get_node
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.
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.
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.
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.
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);
207 * struct ttm_mem_type_manager member put_node
209 * @man: Pointer to a memory type manager.
210 * @mem: Pointer to a struct ttm_mem_reg to be filled in.
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.
216 void (*put_node)(struct ttm_mem_type_manager *man,
217 struct ttm_mem_reg *mem);
220 * struct ttm_mem_type_manager member debug
222 * @man: Pointer to a memory type manager.
223 * @prefix: Prefix to be used in printout to identify the caller.
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.
229 void (*debug)(struct ttm_mem_type_manager *man, const char *prefix);
233 * struct ttm_mem_type_manager
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.
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.
262 struct ttm_mem_type_manager {
263 struct ttm_bo_device *bdev;
266 * No protection. Constant from start.
272 unsigned long gpu_offset;
274 uint32_t available_caching;
275 uint32_t default_caching;
276 const struct ttm_mem_type_manager_func *func;
278 struct lock io_reserve_mutex;
279 bool use_io_reserve_lru;
280 bool io_reserve_fastpath;
283 * Protected by @io_reserve_mutex:
286 struct list_head io_reserve_lru;
289 * Protected by the global->lru_lock.
292 struct list_head lru;
296 * struct ttm_bo_driver
298 * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
299 * @invalidate_caches: Callback to invalidate read caches when a buffer object
301 * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
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
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
314 struct ttm_bo_driver {
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.
323 * Create a struct ttm_tt to back data with system memory pages.
324 * No pages are actually allocated.
326 * NULL: Out of memory.
328 struct ttm_tt *(*ttm_tt_create)(struct ttm_bo_device *bdev,
331 struct vm_page *dummy_read_page);
336 * @ttm: The struct ttm_tt to contain the backing pages.
338 * Allocate all backing pages
340 * -ENOMEM: Out of memory.
342 int (*ttm_tt_populate)(struct ttm_tt *ttm);
347 * @ttm: The struct ttm_tt to contain the backing pages.
349 * Free all backing page
351 void (*ttm_tt_unpopulate)(struct ttm_tt *ttm);
354 * struct ttm_bo_driver member invalidate_caches
356 * @bdev: the buffer object device.
357 * @flags: new placement of the rebound buffer object.
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.
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);
369 * struct ttm_bo_driver member evict_flags:
371 * @bo: the buffer object to be evicted
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
378 void(*evict_flags) (struct ttm_buffer_object *bo,
379 struct ttm_placement *placement);
381 * struct ttm_bo_driver member move:
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
391 * Move a buffer between two memory regions.
393 int (*move) (struct ttm_buffer_object *bo,
394 bool evict, bool interruptible,
396 struct ttm_mem_reg *new_mem);
399 * struct ttm_bo_driver_member verify_access
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.
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.
411 int (*verify_access) (struct ttm_buffer_object *bo);
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
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);
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);
439 * notify the driver that we're about to swap out this bo
441 void (*swap_notify) (struct ttm_buffer_object *bo);
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
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);
454 * struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.
457 struct ttm_bo_global_ref {
458 struct drm_global_reference ref;
459 struct ttm_mem_global *mem_glob;
463 * struct ttm_bo_global - Buffer object driver global data.
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.
476 struct ttm_bo_global {
480 * Constant after init.
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;
490 * Protected by device_list_mutex.
492 struct list_head device_list;
495 * Protected by the lru_lock.
497 struct list_head swap_lru;
500 * Internal protection.
506 #define TTM_NUM_MEM_TYPES 8
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
512 * struct ttm_bo_device - Buffer object driver device-specific data.
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
518 * @addr_space_mm: Range manager for the device address space.
519 * lru_lock: Spinlock that protects the buffer+device lru lists and
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.
528 struct ttm_bo_device {
531 * Constant after bo device init / atomic.
533 struct list_head device_list;
534 struct ttm_bo_global *glob;
535 struct ttm_bo_driver *driver;
537 struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
538 struct lock fence_lock;
540 * Protected by the vm lock.
542 struct rb_root addr_space_rb;
543 struct drm_mm addr_space_mm;
546 * Protected by the global:lru lock.
548 struct list_head ddestroy;
552 * Protected by load / firstopen / lastclose /unload sync.
555 struct address_space *dev_mapping;
558 * Internal protection.
561 struct timeout_task wq;
569 * @old: Pointer to the result and original value.
570 * @new: New value of bits.
571 * @mask: Mask of bits to change.
573 * Convenience function to change a number of bits identified by a mask.
576 static inline uint32_t
577 ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
579 *old ^= (*old ^ new) & mask;
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.
592 * Create a struct ttm_tt to back data with system memory pages.
593 * No pages are actually allocated.
595 * NULL: Out of memory.
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);
607 * @ttm: the ttm_tt structure.
609 * Free memory of ttm_tt structure
611 extern void ttm_tt_fini(struct ttm_tt *ttm);
612 extern void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
617 * @ttm: The struct ttm_tt containing backing pages.
618 * @bo_mem: The struct ttm_mem_reg identifying the binding location.
620 * Bind the pages of @ttm to an aperture location identified by @bo_mem
622 extern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
627 * @ttm: The struct ttm_tt.
629 * Unbind, unpopulate and destroy common struct ttm_tt.
631 extern void ttm_tt_destroy(struct ttm_tt *ttm);
636 * @ttm: The struct ttm_tt.
638 * Unbind a struct ttm_tt.
640 extern void ttm_tt_unbind(struct ttm_tt *ttm);
645 * @ttm: The struct ttm_tt.
647 * Swap in a previously swap out ttm_tt.
649 extern int ttm_tt_swapin(struct ttm_tt *ttm);
652 * ttm_tt_cache_flush:
654 * @pages: An array of pointers to struct page:s to flush.
655 * @num_pages: Number of pages to flush.
657 * Flush the data of the indicated pages from the cpu caches.
658 * This is used when changing caching attributes of the pages from
661 extern void ttm_tt_cache_flush(struct vm_page *pages[], unsigned long num_pages);
664 * ttm_tt_set_placement_caching:
666 * @ttm A struct ttm_tt the backing pages of which will change caching policy.
667 * @placement: Flag indicating the desired caching policy.
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
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.
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);
687 * @bdev: Pointer to a struct ttm_bo_device.
688 * @mem: A valid struct ttm_mem_reg.
690 * Returns true if the memory described by @mem is PCI memory,
693 extern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,
694 struct ttm_mem_reg *mem);
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.
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.
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.
715 extern int ttm_bo_mem_space(struct ttm_buffer_object *bo,
716 struct ttm_placement *placement,
717 struct ttm_mem_reg *mem,
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);
726 extern void ttm_bo_global_release(struct drm_global_reference *ref);
727 extern int ttm_bo_global_init(struct drm_global_reference *ref);
729 extern int ttm_bo_device_release(struct ttm_bo_device *bdev);
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
741 * Initializes a struct ttm_bo_device:
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);
751 * ttm_bo_unmap_virtual
753 * @bo: tear down the virtual mappings for this BO
755 extern void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
758 * ttm_bo_unmap_virtual
760 * @bo: tear down the virtual mappings for this BO
762 * The caller must take ttm_mem_io_lock before calling this function.
764 extern void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);
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,
770 extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
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.
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.
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
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
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.
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.
818 extern int ttm_bo_reserve(struct ttm_buffer_object *bo,
820 bool no_wait, bool use_sequence, uint32_t sequence);
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
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.
832 * Will not remove reserved buffers from the lru lists.
833 * Otherwise identical to ttm_bo_reserve_slowpath.
835 extern int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
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
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.
850 extern int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
851 bool interruptible, uint32_t sequence);
854 * ttm_bo_reserve_nolru:
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.
862 * Will not remove reserved buffers from the lru lists.
863 * Otherwise identical to ttm_bo_reserve.
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.
875 extern int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
877 bool no_wait, bool use_sequence,
883 * @bo: A pointer to a struct ttm_buffer_object.
885 * Unreserve a previous reservation of @bo.
887 extern void ttm_bo_unreserve(struct ttm_buffer_object *bo);
890 * ttm_bo_unreserve_locked
892 * @bo: A pointer to a struct ttm_buffer_object.
894 * Unreserve a previous reservation of @bo.
895 * Needs to be called with struct ttm_bo_global::lru_lock held.
897 extern void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo);
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.
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.
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);
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.
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.
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);
948 * ttm_bo_free_old_node
950 * @bo: A pointer to a struct ttm_buffer_object.
952 * Utility function to free an old placement after a successful move.
954 extern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
957 * ttm_bo_move_accel_cleanup.
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.
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
973 extern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
975 bool evict, bool no_wait_gpu,
976 struct ttm_mem_reg *new_mem);
980 * @c_state: Caching state.
981 * @tmp: Page protection flag for a normal, cached mapping.
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.
986 extern vm_memattr_t ttm_io_prot(uint32_t caching_flags);
988 extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
990 #if (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
992 #include <linux/agp_backend.h>
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.
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.
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);