Subversion Repositories Kolibri OS

Rev

Rev 4103 | Rev 4568 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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