/**************************************************************************1*2* Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR18* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL20* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,21* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR22* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE23* USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/26/*27* Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>28*/29#ifndef _TTM_BO_DRIVER_H_30#define _TTM_BO_DRIVER_H_3132#include "ttm/ttm_bo_api.h"33#include "ttm/ttm_memory.h"34#include "ttm/ttm_module.h"35#include "drm_mm.h"36#include "drm_global.h"37#include "linux/workqueue.h"38#include "linux/fs.h"39#include "linux/spinlock.h"4041struct ttm_backend;4243struct ttm_backend_func {44/**45* struct ttm_backend_func member populate46*47* @backend: Pointer to a struct ttm_backend.48* @num_pages: Number of pages to populate.49* @pages: Array of pointers to ttm pages.50* @dummy_read_page: Page to be used instead of NULL pages in the51* array @pages.52* @dma_addrs: Array of DMA (bus) address of the ttm pages.53*54* Populate the backend with ttm pages. Depending on the backend,55* it may or may not copy the @pages array.56*/57int (*populate) (struct ttm_backend *backend,58unsigned long num_pages, struct page **pages,59struct page *dummy_read_page,60dma_addr_t *dma_addrs);61/**62* struct ttm_backend_func member clear63*64* @backend: Pointer to a struct ttm_backend.65*66* This is an "unpopulate" function. Release all resources67* allocated with populate.68*/69void (*clear) (struct ttm_backend *backend);7071/**72* struct ttm_backend_func member bind73*74* @backend: Pointer to a struct ttm_backend.75* @bo_mem: Pointer to a struct ttm_mem_reg describing the76* memory type and location for binding.77*78* Bind the backend pages into the aperture in the location79* indicated by @bo_mem. This function should be able to handle80* differences between aperture- and system page sizes.81*/82int (*bind) (struct ttm_backend *backend, struct ttm_mem_reg *bo_mem);8384/**85* struct ttm_backend_func member unbind86*87* @backend: Pointer to a struct ttm_backend.88*89* Unbind previously bound backend pages. This function should be90* able to handle differences between aperture- and system page sizes.91*/92int (*unbind) (struct ttm_backend *backend);9394/**95* struct ttm_backend_func member destroy96*97* @backend: Pointer to a struct ttm_backend.98*99* Destroy the backend.100*/101void (*destroy) (struct ttm_backend *backend);102};103104/**105* struct ttm_backend106*107* @bdev: Pointer to a struct ttm_bo_device.108* @flags: For driver use.109* @func: Pointer to a struct ttm_backend_func that describes110* the backend methods.111*112*/113114struct ttm_backend {115struct ttm_bo_device *bdev;116uint32_t flags;117struct ttm_backend_func *func;118};119120#define TTM_PAGE_FLAG_USER (1 << 1)121#define TTM_PAGE_FLAG_USER_DIRTY (1 << 2)122#define TTM_PAGE_FLAG_WRITE (1 << 3)123#define TTM_PAGE_FLAG_SWAPPED (1 << 4)124#define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)125#define TTM_PAGE_FLAG_ZERO_ALLOC (1 << 6)126#define TTM_PAGE_FLAG_DMA32 (1 << 7)127128enum ttm_caching_state {129tt_uncached,130tt_wc,131tt_cached132};133134/**135* struct ttm_tt136*137* @dummy_read_page: Page to map where the ttm_tt page array contains a NULL138* pointer.139* @pages: Array of pages backing the data.140* @first_himem_page: Himem pages are put last in the page array, which141* enables us to run caching attribute changes on only the first part142* of the page array containing lomem pages. This is the index of the143* first himem page.144* @last_lomem_page: Index of the last lomem page in the page array.145* @num_pages: Number of pages in the page array.146* @bdev: Pointer to the current struct ttm_bo_device.147* @be: Pointer to the ttm backend.148* @tsk: The task for user ttm.149* @start: virtual address for user ttm.150* @swap_storage: Pointer to shmem struct file for swap storage.151* @caching_state: The current caching state of the pages.152* @state: The current binding state of the pages.153* @dma_address: The DMA (bus) addresses of the pages (if TTM_PAGE_FLAG_DMA32)154*155* This is a structure holding the pages, caching- and aperture binding156* status for a buffer object that isn't backed by fixed (VRAM / AGP)157* memory.158*/159160struct ttm_tt {161struct page *dummy_read_page;162struct page **pages;163long first_himem_page;164long last_lomem_page;165uint32_t page_flags;166unsigned long num_pages;167struct ttm_bo_global *glob;168struct ttm_backend *be;169struct task_struct *tsk;170unsigned long start;171struct file *swap_storage;172enum ttm_caching_state caching_state;173enum {174tt_bound,175tt_unbound,176tt_unpopulated,177} state;178dma_addr_t *dma_address;179};180181#define TTM_MEMTYPE_FLAG_FIXED (1 << 0) /* Fixed (on-card) PCI memory */182#define TTM_MEMTYPE_FLAG_MAPPABLE (1 << 1) /* Memory mappable */183#define TTM_MEMTYPE_FLAG_CMA (1 << 3) /* Can't map aperture */184185struct ttm_mem_type_manager;186187struct ttm_mem_type_manager_func {188/**189* struct ttm_mem_type_manager member init190*191* @man: Pointer to a memory type manager.192* @p_size: Implementation dependent, but typically the size of the193* range to be managed in pages.194*195* Called to initialize a private range manager. The function is196* expected to initialize the man::priv member.197* Returns 0 on success, negative error code on failure.198*/199int (*init)(struct ttm_mem_type_manager *man, unsigned long p_size);200201/**202* struct ttm_mem_type_manager member takedown203*204* @man: Pointer to a memory type manager.205*206* Called to undo the setup done in init. All allocated resources207* should be freed.208*/209int (*takedown)(struct ttm_mem_type_manager *man);210211/**212* struct ttm_mem_type_manager member get_node213*214* @man: Pointer to a memory type manager.215* @bo: Pointer to the buffer object we're allocating space for.216* @placement: Placement details.217* @mem: Pointer to a struct ttm_mem_reg to be filled in.218*219* This function should allocate space in the memory type managed220* by @man. Placement details if221* applicable are given by @placement. If successful,222* @mem::mm_node should be set to a non-null value, and223* @mem::start should be set to a value identifying the beginning224* of the range allocated, and the function should return zero.225* If the memory region accommodate the buffer object, @mem::mm_node226* should be set to NULL, and the function should return 0.227* If a system error occurred, preventing the request to be fulfilled,228* the function should return a negative error code.229*230* Note that @mem::mm_node will only be dereferenced by231* struct ttm_mem_type_manager functions and optionally by the driver,232* which has knowledge of the underlying type.233*234* This function may not be called from within atomic context, so235* an implementation can and must use either a mutex or a spinlock to236* protect any data structures managing the space.237*/238int (*get_node)(struct ttm_mem_type_manager *man,239struct ttm_buffer_object *bo,240struct ttm_placement *placement,241struct ttm_mem_reg *mem);242243/**244* struct ttm_mem_type_manager member put_node245*246* @man: Pointer to a memory type manager.247* @mem: Pointer to a struct ttm_mem_reg to be filled in.248*249* This function frees memory type resources previously allocated250* and that are identified by @mem::mm_node and @mem::start. May not251* be called from within atomic context.252*/253void (*put_node)(struct ttm_mem_type_manager *man,254struct ttm_mem_reg *mem);255256/**257* struct ttm_mem_type_manager member debug258*259* @man: Pointer to a memory type manager.260* @prefix: Prefix to be used in printout to identify the caller.261*262* This function is called to print out the state of the memory263* type manager to aid debugging of out-of-memory conditions.264* It may not be called from within atomic context.265*/266void (*debug)(struct ttm_mem_type_manager *man, const char *prefix);267};268269/**270* struct ttm_mem_type_manager271*272* @has_type: The memory type has been initialized.273* @use_type: The memory type is enabled.274* @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory275* managed by this memory type.276* @gpu_offset: If used, the GPU offset of the first managed page of277* fixed memory or the first managed location in an aperture.278* @size: Size of the managed region.279* @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,280* as defined in ttm_placement_common.h281* @default_caching: The default caching policy used for a buffer object282* placed in this memory type if the user doesn't provide one.283* @func: structure pointer implementing the range manager. See above284* @priv: Driver private closure for @func.285* @io_reserve_mutex: Mutex optionally protecting shared io_reserve structures286* @use_io_reserve_lru: Use an lru list to try to unreserve io_mem_regions287* reserved by the TTM vm system.288* @io_reserve_lru: Optional lru list for unreserving io mem regions.289* @io_reserve_fastpath: Only use bdev::driver::io_mem_reserve to obtain290* static information. bdev::driver::io_mem_free is never used.291* @lru: The lru list for this memory type.292*293* This structure is used to identify and manage memory types for a device.294* It's set up by the ttm_bo_driver::init_mem_type method.295*/296297298299struct ttm_mem_type_manager {300struct ttm_bo_device *bdev;301302/*303* No protection. Constant from start.304*/305306bool has_type;307bool use_type;308uint32_t flags;309unsigned long gpu_offset;310uint64_t size;311uint32_t available_caching;312uint32_t default_caching;313const struct ttm_mem_type_manager_func *func;314void *priv;315struct mutex io_reserve_mutex;316bool use_io_reserve_lru;317bool io_reserve_fastpath;318319/*320* Protected by @io_reserve_mutex:321*/322323struct list_head io_reserve_lru;324325/*326* Protected by the global->lru_lock.327*/328329struct list_head lru;330};331332/**333* struct ttm_bo_driver334*335* @create_ttm_backend_entry: Callback to create a struct ttm_backend.336* @invalidate_caches: Callback to invalidate read caches when a buffer object337* has been evicted.338* @init_mem_type: Callback to initialize a struct ttm_mem_type_manager339* structure.340* @evict_flags: Callback to obtain placement flags when a buffer is evicted.341* @move: Callback for a driver to hook in accelerated functions to342* move a buffer.343* If set to NULL, a potentially slow memcpy() move is used.344* @sync_obj_signaled: See ttm_fence_api.h345* @sync_obj_wait: See ttm_fence_api.h346* @sync_obj_flush: See ttm_fence_api.h347* @sync_obj_unref: See ttm_fence_api.h348* @sync_obj_ref: See ttm_fence_api.h349*/350351struct ttm_bo_driver {352/**353* struct ttm_bo_driver member create_ttm_backend_entry354*355* @bdev: The buffer object device.356*357* Create a driver specific struct ttm_backend.358*/359360struct ttm_backend *(*create_ttm_backend_entry)361(struct ttm_bo_device *bdev);362363/**364* struct ttm_bo_driver member invalidate_caches365*366* @bdev: the buffer object device.367* @flags: new placement of the rebound buffer object.368*369* A previosly evicted buffer has been rebound in a370* potentially new location. Tell the driver that it might371* consider invalidating read (texture) caches on the next command372* submission as a consequence.373*/374375int (*invalidate_caches) (struct ttm_bo_device *bdev, uint32_t flags);376int (*init_mem_type) (struct ttm_bo_device *bdev, uint32_t type,377struct ttm_mem_type_manager *man);378/**379* struct ttm_bo_driver member evict_flags:380*381* @bo: the buffer object to be evicted382*383* Return the bo flags for a buffer which is not mapped to the hardware.384* These will be placed in proposed_flags so that when the move is385* finished, they'll end up in bo->mem.flags386*/387388void(*evict_flags) (struct ttm_buffer_object *bo,389struct ttm_placement *placement);390/**391* struct ttm_bo_driver member move:392*393* @bo: the buffer to move394* @evict: whether this motion is evicting the buffer from395* the graphics address space396* @interruptible: Use interruptible sleeps if possible when sleeping.397* @no_wait: whether this should give up and return -EBUSY398* if this move would require sleeping399* @new_mem: the new memory region receiving the buffer400*401* Move a buffer between two memory regions.402*/403int (*move) (struct ttm_buffer_object *bo,404bool evict, bool interruptible,405bool no_wait_reserve, bool no_wait_gpu,406struct ttm_mem_reg *new_mem);407408/**409* struct ttm_bo_driver_member verify_access410*411* @bo: Pointer to a buffer object.412* @filp: Pointer to a struct file trying to access the object.413*414* Called from the map / write / read methods to verify that the415* caller is permitted to access the buffer object.416* This member may be set to NULL, which will refuse this kind of417* access for all buffer objects.418* This function should return 0 if access is granted, -EPERM otherwise.419*/420int (*verify_access) (struct ttm_buffer_object *bo,421struct file *filp);422423/**424* In case a driver writer dislikes the TTM fence objects,425* the driver writer can replace those with sync objects of426* his / her own. If it turns out that no driver writer is427* using these. I suggest we remove these hooks and plug in428* fences directly. The bo driver needs the following functionality:429* See the corresponding functions in the fence object API430* documentation.431*/432433bool (*sync_obj_signaled) (void *sync_obj, void *sync_arg);434int (*sync_obj_wait) (void *sync_obj, void *sync_arg,435bool lazy, bool interruptible);436int (*sync_obj_flush) (void *sync_obj, void *sync_arg);437void (*sync_obj_unref) (void **sync_obj);438void *(*sync_obj_ref) (void *sync_obj);439440/* hook to notify driver about a driver move so it441* can do tiling things */442void (*move_notify)(struct ttm_buffer_object *bo,443struct ttm_mem_reg *new_mem);444/* notify the driver we are taking a fault on this BO445* and have reserved it */446int (*fault_reserve_notify)(struct ttm_buffer_object *bo);447448/**449* notify the driver that we're about to swap out this bo450*/451void (*swap_notify) (struct ttm_buffer_object *bo);452453/**454* Driver callback on when mapping io memory (for bo_move_memcpy455* for instance). TTM will take care to call io_mem_free whenever456* the mapping is not use anymore. io_mem_reserve & io_mem_free457* are balanced.458*/459int (*io_mem_reserve)(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);460void (*io_mem_free)(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);461};462463/**464* struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.465*/466467struct ttm_bo_global_ref {468struct drm_global_reference ref;469struct ttm_mem_global *mem_glob;470};471472/**473* struct ttm_bo_global - Buffer object driver global data.474*475* @mem_glob: Pointer to a struct ttm_mem_global object for accounting.476* @dummy_read_page: Pointer to a dummy page used for mapping requests477* of unpopulated pages.478* @shrink: A shrink callback object used for buffer object swap.479* @ttm_bo_extra_size: Extra size (sizeof(struct ttm_buffer_object) excluded)480* used by a buffer object. This is excluding page arrays and backing pages.481* @ttm_bo_size: This is @ttm_bo_extra_size + sizeof(struct ttm_buffer_object).482* @device_list_mutex: Mutex protecting the device list.483* This mutex is held while traversing the device list for pm options.484* @lru_lock: Spinlock protecting the bo subsystem lru lists.485* @device_list: List of buffer object devices.486* @swap_lru: Lru list of buffer objects used for swapping.487*/488489struct ttm_bo_global {490491/**492* Constant after init.493*/494495struct kobject kobj;496struct ttm_mem_global *mem_glob;497struct page *dummy_read_page;498struct ttm_mem_shrink shrink;499size_t ttm_bo_extra_size;500size_t ttm_bo_size;501struct mutex device_list_mutex;502spinlock_t lru_lock;503504/**505* Protected by device_list_mutex.506*/507struct list_head device_list;508509/**510* Protected by the lru_lock.511*/512struct list_head swap_lru;513514/**515* Internal protection.516*/517atomic_t bo_count;518};519520521#define TTM_NUM_MEM_TYPES 8522523#define TTM_BO_PRIV_FLAG_MOVING 0 /* Buffer object is moving and needs524idling before CPU mapping */525#define TTM_BO_PRIV_FLAG_MAX 1526/**527* struct ttm_bo_device - Buffer object driver device-specific data.528*529* @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.530* @man: An array of mem_type_managers.531* @fence_lock: Protects the synchronizing members on *all* bos belonging532* to this device.533* @addr_space_mm: Range manager for the device address space.534* lru_lock: Spinlock that protects the buffer+device lru lists and535* ddestroy lists.536* @val_seq: Current validation sequence.537* @nice_mode: Try nicely to wait for buffer idle when cleaning a manager.538* If a GPU lockup has been detected, this is forced to 0.539* @dev_mapping: A pointer to the struct address_space representing the540* device address space.541* @wq: Work queue structure for the delayed delete workqueue.542*543*/544545struct ttm_bo_device {546547/*548* Constant after bo device init / atomic.549*/550struct list_head device_list;551struct ttm_bo_global *glob;552struct ttm_bo_driver *driver;553rwlock_t vm_lock;554struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];555spinlock_t fence_lock;556/*557* Protected by the vm lock.558*/559struct rb_root addr_space_rb;560struct drm_mm addr_space_mm;561562/*563* Protected by the global:lru lock.564*/565struct list_head ddestroy;566uint32_t val_seq;567568/*569* Protected by load / firstopen / lastclose /unload sync.570*/571572bool nice_mode;573struct address_space *dev_mapping;574575/*576* Internal protection.577*/578579struct delayed_work wq;580581bool need_dma32;582};583584/**585* ttm_flag_masked586*587* @old: Pointer to the result and original value.588* @new: New value of bits.589* @mask: Mask of bits to change.590*591* Convenience function to change a number of bits identified by a mask.592*/593594static inline uint32_t595ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)596{597*old ^= (*old ^ new) & mask;598return *old;599}600601/**602* ttm_tt_create603*604* @bdev: pointer to a struct ttm_bo_device:605* @size: Size of the data needed backing.606* @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.607* @dummy_read_page: See struct ttm_bo_device.608*609* Create a struct ttm_tt to back data with system memory pages.610* No pages are actually allocated.611* Returns:612* NULL: Out of memory.613*/614extern struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev,615unsigned long size,616uint32_t page_flags,617struct page *dummy_read_page);618619/**620* ttm_tt_set_user:621*622* @ttm: The struct ttm_tt to populate.623* @tsk: A struct task_struct for which @start is a valid user-space address.624* @start: A valid user-space address.625* @num_pages: Size in pages of the user memory area.626*627* Populate a struct ttm_tt with a user-space memory area after first pinning628* the pages backing it.629* Returns:630* !0: Error.631*/632633extern int ttm_tt_set_user(struct ttm_tt *ttm,634struct task_struct *tsk,635unsigned long start, unsigned long num_pages);636637/**638* ttm_ttm_bind:639*640* @ttm: The struct ttm_tt containing backing pages.641* @bo_mem: The struct ttm_mem_reg identifying the binding location.642*643* Bind the pages of @ttm to an aperture location identified by @bo_mem644*/645extern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);646647/**648* ttm_tt_populate:649*650* @ttm: The struct ttm_tt to contain the backing pages.651*652* Add backing pages to all of @ttm653*/654extern int ttm_tt_populate(struct ttm_tt *ttm);655656/**657* ttm_ttm_destroy:658*659* @ttm: The struct ttm_tt.660*661* Unbind, unpopulate and destroy a struct ttm_tt.662*/663extern void ttm_tt_destroy(struct ttm_tt *ttm);664665/**666* ttm_ttm_unbind:667*668* @ttm: The struct ttm_tt.669*670* Unbind a struct ttm_tt.671*/672extern void ttm_tt_unbind(struct ttm_tt *ttm);673674/**675* ttm_ttm_destroy:676*677* @ttm: The struct ttm_tt.678* @index: Index of the desired page.679*680* Return a pointer to the struct page backing @ttm at page681* index @index. If the page is unpopulated, one will be allocated to682* populate that index.683*684* Returns:685* NULL on OOM.686*/687extern struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index);688689/**690* ttm_tt_cache_flush:691*692* @pages: An array of pointers to struct page:s to flush.693* @num_pages: Number of pages to flush.694*695* Flush the data of the indicated pages from the cpu caches.696* This is used when changing caching attributes of the pages from697* cache-coherent.698*/699extern void ttm_tt_cache_flush(struct page *pages[], unsigned long num_pages);700701/**702* ttm_tt_set_placement_caching:703*704* @ttm A struct ttm_tt the backing pages of which will change caching policy.705* @placement: Flag indicating the desired caching policy.706*707* This function will change caching policy of any default kernel mappings of708* the pages backing @ttm. If changing from cached to uncached or709* write-combined,710* all CPU caches will first be flushed to make sure the data of the pages711* hit RAM. This function may be very costly as it involves global TLB712* and cache flushes and potential page splitting / combining.713*/714extern int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);715extern int ttm_tt_swapout(struct ttm_tt *ttm,716struct file *persistent_swap_storage);717718/*719* ttm_bo.c720*/721722/**723* ttm_mem_reg_is_pci724*725* @bdev: Pointer to a struct ttm_bo_device.726* @mem: A valid struct ttm_mem_reg.727*728* Returns true if the memory described by @mem is PCI memory,729* false otherwise.730*/731extern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,732struct ttm_mem_reg *mem);733734/**735* ttm_bo_mem_space736*737* @bo: Pointer to a struct ttm_buffer_object. the data of which738* we want to allocate space for.739* @proposed_placement: Proposed new placement for the buffer object.740* @mem: A struct ttm_mem_reg.741* @interruptible: Sleep interruptible when sliping.742* @no_wait_reserve: Return immediately if other buffers are busy.743* @no_wait_gpu: Return immediately if the GPU is busy.744*745* Allocate memory space for the buffer object pointed to by @bo, using746* the placement flags in @mem, potentially evicting other idle buffer objects.747* This function may sleep while waiting for space to become available.748* Returns:749* -EBUSY: No space available (only if no_wait == 1).750* -ENOMEM: Could not allocate memory for the buffer object, either due to751* fragmentation or concurrent allocators.752* -ERESTARTSYS: An interruptible sleep was interrupted by a signal.753*/754extern int ttm_bo_mem_space(struct ttm_buffer_object *bo,755struct ttm_placement *placement,756struct ttm_mem_reg *mem,757bool interruptible,758bool no_wait_reserve, bool no_wait_gpu);759760extern void ttm_bo_mem_put(struct ttm_buffer_object *bo,761struct ttm_mem_reg *mem);762extern void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,763struct ttm_mem_reg *mem);764765/**766* ttm_bo_wait_for_cpu767*768* @bo: Pointer to a struct ttm_buffer_object.769* @no_wait: Don't sleep while waiting.770*771* Wait until a buffer object is no longer sync'ed for CPU access.772* Returns:773* -EBUSY: Buffer object was sync'ed for CPU access. (only if no_wait == 1).774* -ERESTARTSYS: An interruptible sleep was interrupted by a signal.775*/776777extern int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait);778779extern void ttm_bo_global_release(struct drm_global_reference *ref);780extern int ttm_bo_global_init(struct drm_global_reference *ref);781782extern int ttm_bo_device_release(struct ttm_bo_device *bdev);783784/**785* ttm_bo_device_init786*787* @bdev: A pointer to a struct ttm_bo_device to initialize.788* @mem_global: A pointer to an initialized struct ttm_mem_global.789* @driver: A pointer to a struct ttm_bo_driver set up by the caller.790* @file_page_offset: Offset into the device address space that is available791* for buffer data. This ensures compatibility with other users of the792* address space.793*794* Initializes a struct ttm_bo_device:795* Returns:796* !0: Failure.797*/798extern int ttm_bo_device_init(struct ttm_bo_device *bdev,799struct ttm_bo_global *glob,800struct ttm_bo_driver *driver,801uint64_t file_page_offset, bool need_dma32);802803/**804* ttm_bo_unmap_virtual805*806* @bo: tear down the virtual mappings for this BO807*/808extern void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);809810/**811* ttm_bo_unmap_virtual812*813* @bo: tear down the virtual mappings for this BO814*815* The caller must take ttm_mem_io_lock before calling this function.816*/817extern void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);818819extern int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo);820extern void ttm_mem_io_free_vm(struct ttm_buffer_object *bo);821extern int ttm_mem_io_lock(struct ttm_mem_type_manager *man,822bool interruptible);823extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);824825826/**827* ttm_bo_reserve:828*829* @bo: A pointer to a struct ttm_buffer_object.830* @interruptible: Sleep interruptible if waiting.831* @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.832* @use_sequence: If @bo is already reserved, Only sleep waiting for833* it to become unreserved if @sequence < (@bo)->sequence.834*835* Locks a buffer object for validation. (Or prevents other processes from836* locking it for validation) and removes it from lru lists, while taking837* a number of measures to prevent deadlocks.838*839* Deadlocks may occur when two processes try to reserve multiple buffers in840* different order, either by will or as a result of a buffer being evicted841* to make room for a buffer already reserved. (Buffers are reserved before842* they are evicted). The following algorithm prevents such deadlocks from843* occurring:844* 1) Buffers are reserved with the lru spinlock held. Upon successful845* reservation they are removed from the lru list. This stops a reserved buffer846* from being evicted. However the lru spinlock is released between the time847* a buffer is selected for eviction and the time it is reserved.848* Therefore a check is made when a buffer is reserved for eviction, that it849* is still the first buffer in the lru list, before it is removed from the850* list. @check_lru == 1 forces this check. If it fails, the function returns851* -EINVAL, and the caller should then choose a new buffer to evict and repeat852* the procedure.853* 2) Processes attempting to reserve multiple buffers other than for eviction,854* (typically execbuf), should first obtain a unique 32-bit855* validation sequence number,856* and call this function with @use_sequence == 1 and @sequence == the unique857* sequence number. If upon call of this function, the buffer object is already858* reserved, the validation sequence is checked against the validation859* sequence of the process currently reserving the buffer,860* and if the current validation sequence is greater than that of the process861* holding the reservation, the function returns -EAGAIN. Otherwise it sleeps862* waiting for the buffer to become unreserved, after which it retries863* reserving.864* The caller should, when receiving an -EAGAIN error865* release all its buffer reservations, wait for @bo to become unreserved, and866* then rerun the validation with the same validation sequence. This procedure867* will always guarantee that the process with the lowest validation sequence868* will eventually succeed, preventing both deadlocks and starvation.869*870* Returns:871* -EAGAIN: The reservation may cause a deadlock.872* Release all buffer reservations, wait for @bo to become unreserved and873* try again. (only if use_sequence == 1).874* -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by875* a signal. Release all buffer reservations and return to user-space.876* -EBUSY: The function needed to sleep, but @no_wait was true877* -EDEADLK: Bo already reserved using @sequence. This error code will only878* be returned if @use_sequence is set to true.879*/880extern int ttm_bo_reserve(struct ttm_buffer_object *bo,881bool interruptible,882bool no_wait, bool use_sequence, uint32_t sequence);883884885/**886* ttm_bo_reserve_locked:887*888* @bo: A pointer to a struct ttm_buffer_object.889* @interruptible: Sleep interruptible if waiting.890* @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.891* @use_sequence: If @bo is already reserved, Only sleep waiting for892* it to become unreserved if @sequence < (@bo)->sequence.893*894* Must be called with struct ttm_bo_global::lru_lock held,895* and will not remove reserved buffers from the lru lists.896* The function may release the LRU spinlock if it needs to sleep.897* Otherwise identical to ttm_bo_reserve.898*899* Returns:900* -EAGAIN: The reservation may cause a deadlock.901* Release all buffer reservations, wait for @bo to become unreserved and902* try again. (only if use_sequence == 1).903* -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by904* a signal. Release all buffer reservations and return to user-space.905* -EBUSY: The function needed to sleep, but @no_wait was true906* -EDEADLK: Bo already reserved using @sequence. This error code will only907* be returned if @use_sequence is set to true.908*/909extern int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,910bool interruptible,911bool no_wait, bool use_sequence,912uint32_t sequence);913914/**915* ttm_bo_unreserve916*917* @bo: A pointer to a struct ttm_buffer_object.918*919* Unreserve a previous reservation of @bo.920*/921extern void ttm_bo_unreserve(struct ttm_buffer_object *bo);922923/**924* ttm_bo_unreserve_locked925*926* @bo: A pointer to a struct ttm_buffer_object.927*928* Unreserve a previous reservation of @bo.929* Needs to be called with struct ttm_bo_global::lru_lock held.930*/931extern void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo);932933/**934* ttm_bo_wait_unreserved935*936* @bo: A pointer to a struct ttm_buffer_object.937*938* Wait for a struct ttm_buffer_object to become unreserved.939* This is typically used in the execbuf code to relax cpu-usage when940* a potential deadlock condition backoff.941*/942extern int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo,943bool interruptible);944945/*946* ttm_bo_util.c947*/948949/**950* ttm_bo_move_ttm951*952* @bo: A pointer to a struct ttm_buffer_object.953* @evict: 1: This is an eviction. Don't try to pipeline.954* @no_wait_reserve: Return immediately if other buffers are busy.955* @no_wait_gpu: Return immediately if the GPU is busy.956* @new_mem: struct ttm_mem_reg indicating where to move.957*958* Optimized move function for a buffer object with both old and959* new placement backed by a TTM. The function will, if successful,960* free any old aperture space, and set (@new_mem)->mm_node to NULL,961* and update the (@bo)->mem placement flags. If unsuccessful, the old962* data remains untouched, and it's up to the caller to free the963* memory space indicated by @new_mem.964* Returns:965* !0: Failure.966*/967968extern int ttm_bo_move_ttm(struct ttm_buffer_object *bo,969bool evict, bool no_wait_reserve,970bool no_wait_gpu, struct ttm_mem_reg *new_mem);971972/**973* ttm_bo_move_memcpy974*975* @bo: A pointer to a struct ttm_buffer_object.976* @evict: 1: This is an eviction. Don't try to pipeline.977* @no_wait_reserve: Return immediately if other buffers are busy.978* @no_wait_gpu: Return immediately if the GPU is busy.979* @new_mem: struct ttm_mem_reg indicating where to move.980*981* Fallback move function for a mappable buffer object in mappable memory.982* The function will, if successful,983* free any old aperture space, and set (@new_mem)->mm_node to NULL,984* and update the (@bo)->mem placement flags. If unsuccessful, the old985* data remains untouched, and it's up to the caller to free the986* memory space indicated by @new_mem.987* Returns:988* !0: Failure.989*/990991extern int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,992bool evict, bool no_wait_reserve,993bool no_wait_gpu, struct ttm_mem_reg *new_mem);994995/**996* ttm_bo_free_old_node997*998* @bo: A pointer to a struct ttm_buffer_object.999*1000* Utility function to free an old placement after a successful move.1001*/1002extern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);10031004/**1005* ttm_bo_move_accel_cleanup.1006*1007* @bo: A pointer to a struct ttm_buffer_object.1008* @sync_obj: A sync object that signals when moving is complete.1009* @sync_obj_arg: An argument to pass to the sync object idle / wait1010* functions.1011* @evict: This is an evict move. Don't return until the buffer is idle.1012* @no_wait_reserve: Return immediately if other buffers are busy.1013* @no_wait_gpu: Return immediately if the GPU is busy.1014* @new_mem: struct ttm_mem_reg indicating where to move.1015*1016* Accelerated move function to be called when an accelerated move1017* has been scheduled. The function will create a new temporary buffer object1018* representing the old placement, and put the sync object on both buffer1019* objects. After that the newly created buffer object is unref'd to be1020* destroyed when the move is complete. This will help pipeline1021* buffer moves.1022*/10231024extern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,1025void *sync_obj,1026void *sync_obj_arg,1027bool evict, bool no_wait_reserve,1028bool no_wait_gpu,1029struct ttm_mem_reg *new_mem);1030/**1031* ttm_io_prot1032*1033* @c_state: Caching state.1034* @tmp: Page protection flag for a normal, cached mapping.1035*1036* Utility function that returns the pgprot_t that should be used for1037* setting up a PTE with the caching model indicated by @c_state.1038*/1039extern pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);10401041extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;10421043#if (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))1044#define TTM_HAS_AGP1045#include <linux/agp_backend.h>10461047/**1048* ttm_agp_backend_init1049*1050* @bdev: Pointer to a struct ttm_bo_device.1051* @bridge: The agp bridge this device is sitting on.1052*1053* Create a TTM backend that uses the indicated AGP bridge as an aperture1054* for TT memory. This function uses the linux agpgart interface to1055* bind and unbind memory backing a ttm_tt.1056*/1057extern struct ttm_backend *ttm_agp_backend_init(struct ttm_bo_device *bdev,1058struct agp_bridge_data *bridge);1059#endif10601061#endif106210631064