Back to home page

OSCL-LXR

 
 

    


0001 /**************************************************************************
0002  *
0003  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
0004  * All Rights Reserved.
0005  *
0006  * Permission is hereby granted, free of charge, to any person obtaining a
0007  * copy of this software and associated documentation files (the
0008  * "Software"), to deal in the Software without restriction, including
0009  * without limitation the rights to use, copy, modify, merge, publish,
0010  * distribute, sub license, and/or sell copies of the Software, and to
0011  * permit persons to whom the Software is furnished to do so, subject to
0012  * the following conditions:
0013  *
0014  * The above copyright notice and this permission notice (including the
0015  * next paragraph) shall be included in all copies or substantial portions
0016  * of the Software.
0017  *
0018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0020  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
0021  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
0022  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0023  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
0024  * USE OR OTHER DEALINGS IN THE SOFTWARE.
0025  *
0026  **************************************************************************/
0027 /*
0028  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
0029  */
0030 
0031 #ifndef _TTM_BO_API_H_
0032 #define _TTM_BO_API_H_
0033 
0034 #include <drm/drm_gem.h>
0035 #include <drm/drm_vma_manager.h>
0036 #include <linux/kref.h>
0037 #include <linux/list.h>
0038 #include <linux/wait.h>
0039 #include <linux/mutex.h>
0040 #include <linux/mm.h>
0041 #include <linux/bitmap.h>
0042 #include <linux/dma-resv.h>
0043 
0044 #include "ttm_resource.h"
0045 
0046 struct ttm_global;
0047 
0048 struct ttm_device;
0049 
0050 struct iosys_map;
0051 
0052 struct drm_mm_node;
0053 
0054 struct ttm_placement;
0055 
0056 struct ttm_place;
0057 
0058 /**
0059  * enum ttm_bo_type
0060  *
0061  * @ttm_bo_type_device: These are 'normal' buffers that can
0062  * be mmapped by user space. Each of these bos occupy a slot in the
0063  * device address space, that can be used for normal vm operations.
0064  *
0065  * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
0066  * but they cannot be accessed from user-space. For kernel-only use.
0067  *
0068  * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
0069  * driver.
0070  */
0071 
0072 enum ttm_bo_type {
0073     ttm_bo_type_device,
0074     ttm_bo_type_kernel,
0075     ttm_bo_type_sg
0076 };
0077 
0078 struct ttm_tt;
0079 
0080 /**
0081  * struct ttm_buffer_object
0082  *
0083  * @base: drm_gem_object superclass data.
0084  * @bdev: Pointer to the buffer object device structure.
0085  * @type: The bo type.
0086  * @page_alignment: Page alignment.
0087  * @destroy: Destruction function. If NULL, kfree is used.
0088  * @num_pages: Actual number of pages.
0089  * @kref: Reference count of this buffer object. When this refcount reaches
0090  * zero, the object is destroyed or put on the delayed delete list.
0091  * @mem: structure describing current placement.
0092  * @ttm: TTM structure holding system pages.
0093  * @evicted: Whether the object was evicted without user-space knowing.
0094  * @deleted: True if the object is only a zombie and already deleted.
0095  * @ddestroy: List head for the delayed destroy list.
0096  * @swap: List head for swap LRU list.
0097  * @offset: The current GPU offset, which can have different meanings
0098  * depending on the memory type. For SYSTEM type memory, it should be 0.
0099  * @cur_placement: Hint of current placement.
0100  *
0101  * Base class for TTM buffer object, that deals with data placement and CPU
0102  * mappings. GPU mappings are really up to the driver, but for simpler GPUs
0103  * the driver can usually use the placement offset @offset directly as the
0104  * GPU virtual address. For drivers implementing multiple
0105  * GPU memory manager contexts, the driver should manage the address space
0106  * in these contexts separately and use these objects to get the correct
0107  * placement and caching for these GPU maps. This makes it possible to use
0108  * these objects for even quite elaborate memory management schemes.
0109  * The destroy member, the API visibility of this object makes it possible
0110  * to derive driver specific types.
0111  */
0112 
0113 struct ttm_buffer_object {
0114     struct drm_gem_object base;
0115 
0116     /**
0117      * Members constant at init.
0118      */
0119 
0120     struct ttm_device *bdev;
0121     enum ttm_bo_type type;
0122     uint32_t page_alignment;
0123     void (*destroy) (struct ttm_buffer_object *);
0124 
0125     /**
0126     * Members not needing protection.
0127     */
0128     struct kref kref;
0129 
0130     /**
0131      * Members protected by the bo::resv::reserved lock.
0132      */
0133 
0134     struct ttm_resource *resource;
0135     struct ttm_tt *ttm;
0136     bool deleted;
0137     struct ttm_lru_bulk_move *bulk_move;
0138 
0139     /**
0140      * Members protected by the bdev::lru_lock.
0141      */
0142 
0143     struct list_head ddestroy;
0144 
0145     /**
0146      * Members protected by a bo reservation.
0147      */
0148 
0149     unsigned priority;
0150     unsigned pin_count;
0151 
0152     /**
0153      * Special members that are protected by the reserve lock
0154      * and the bo::lock when written to. Can be read with
0155      * either of these locks held.
0156      */
0157 
0158     struct sg_table *sg;
0159 };
0160 
0161 /**
0162  * struct ttm_bo_kmap_obj
0163  *
0164  * @virtual: The current kernel virtual address.
0165  * @page: The page when kmap'ing a single page.
0166  * @bo_kmap_type: Type of bo_kmap.
0167  *
0168  * Object describing a kernel mapping. Since a TTM bo may be located
0169  * in various memory types with various caching policies, the
0170  * mapping can either be an ioremap, a vmap, a kmap or part of a
0171  * premapped region.
0172  */
0173 
0174 #define TTM_BO_MAP_IOMEM_MASK 0x80
0175 struct ttm_bo_kmap_obj {
0176     void *virtual;
0177     struct page *page;
0178     enum {
0179         ttm_bo_map_iomap        = 1 | TTM_BO_MAP_IOMEM_MASK,
0180         ttm_bo_map_vmap         = 2,
0181         ttm_bo_map_kmap         = 3,
0182         ttm_bo_map_premapped    = 4 | TTM_BO_MAP_IOMEM_MASK,
0183     } bo_kmap_type;
0184     struct ttm_buffer_object *bo;
0185 };
0186 
0187 /**
0188  * struct ttm_operation_ctx
0189  *
0190  * @interruptible: Sleep interruptible if sleeping.
0191  * @no_wait_gpu: Return immediately if the GPU is busy.
0192  * @gfp_retry_mayfail: Set the __GFP_RETRY_MAYFAIL when allocation pages.
0193  * @allow_res_evict: Allow eviction of reserved BOs. Can be used when multiple
0194  * BOs share the same reservation object.
0195  * @force_alloc: Don't check the memory account during suspend or CPU page
0196  * faults. Should only be used by TTM internally.
0197  * @resv: Reservation object to allow reserved evictions with.
0198  *
0199  * Context for TTM operations like changing buffer placement or general memory
0200  * allocation.
0201  */
0202 struct ttm_operation_ctx {
0203     bool interruptible;
0204     bool no_wait_gpu;
0205     bool gfp_retry_mayfail;
0206     bool allow_res_evict;
0207     bool force_alloc;
0208     struct dma_resv *resv;
0209     uint64_t bytes_moved;
0210 };
0211 
0212 /**
0213  * ttm_bo_get - reference a struct ttm_buffer_object
0214  *
0215  * @bo: The buffer object.
0216  */
0217 static inline void ttm_bo_get(struct ttm_buffer_object *bo)
0218 {
0219     kref_get(&bo->kref);
0220 }
0221 
0222 /**
0223  * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless
0224  * its refcount has already reached zero.
0225  * @bo: The buffer object.
0226  *
0227  * Used to reference a TTM buffer object in lookups where the object is removed
0228  * from the lookup structure during the destructor and for RCU lookups.
0229  *
0230  * Returns: @bo if the referencing was successful, NULL otherwise.
0231  */
0232 static inline __must_check struct ttm_buffer_object *
0233 ttm_bo_get_unless_zero(struct ttm_buffer_object *bo)
0234 {
0235     if (!kref_get_unless_zero(&bo->kref))
0236         return NULL;
0237     return bo;
0238 }
0239 
0240 /**
0241  * ttm_bo_wait - wait for buffer idle.
0242  *
0243  * @bo:  The buffer object.
0244  * @interruptible:  Use interruptible wait.
0245  * @no_wait:  Return immediately if buffer is busy.
0246  *
0247  * This function must be called with the bo::mutex held, and makes
0248  * sure any previous rendering to the buffer is completed.
0249  * Note: It might be necessary to block validations before the
0250  * wait by reserving the buffer.
0251  * Returns -EBUSY if no_wait is true and the buffer is busy.
0252  * Returns -ERESTARTSYS if interrupted by a signal.
0253  */
0254 int ttm_bo_wait(struct ttm_buffer_object *bo, bool interruptible, bool no_wait);
0255 
0256 static inline int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
0257 {
0258     return ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
0259 }
0260 
0261 /**
0262  * ttm_bo_validate
0263  *
0264  * @bo: The buffer object.
0265  * @placement: Proposed placement for the buffer object.
0266  * @ctx: validation parameters.
0267  *
0268  * Changes placement and caching policy of the buffer object
0269  * according proposed placement.
0270  * Returns
0271  * -EINVAL on invalid proposed placement.
0272  * -ENOMEM on out-of-memory condition.
0273  * -EBUSY if no_wait is true and buffer busy.
0274  * -ERESTARTSYS if interrupted by a signal.
0275  */
0276 int ttm_bo_validate(struct ttm_buffer_object *bo,
0277             struct ttm_placement *placement,
0278             struct ttm_operation_ctx *ctx);
0279 
0280 /**
0281  * ttm_bo_put
0282  *
0283  * @bo: The buffer object.
0284  *
0285  * Unreference a buffer object.
0286  */
0287 void ttm_bo_put(struct ttm_buffer_object *bo);
0288 
0289 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo);
0290 void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo,
0291               struct ttm_lru_bulk_move *bulk);
0292 
0293 /**
0294  * ttm_bo_lock_delayed_workqueue
0295  *
0296  * Prevent the delayed workqueue from running.
0297  * Returns
0298  * True if the workqueue was queued at the time
0299  */
0300 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev);
0301 
0302 /**
0303  * ttm_bo_unlock_delayed_workqueue
0304  *
0305  * Allows the delayed workqueue to run.
0306  */
0307 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
0308 
0309 /**
0310  * ttm_bo_eviction_valuable
0311  *
0312  * @bo: The buffer object to evict
0313  * @place: the placement we need to make room for
0314  *
0315  * Check if it is valuable to evict the BO to make room for the given placement.
0316  */
0317 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
0318                   const struct ttm_place *place);
0319 
0320 /**
0321  * ttm_bo_init_reserved
0322  *
0323  * @bdev: Pointer to a ttm_device struct.
0324  * @bo: Pointer to a ttm_buffer_object to be initialized.
0325  * @size: Requested size of buffer object.
0326  * @type: Requested type of buffer object.
0327  * @placement: Initial placement for buffer object.
0328  * @page_alignment: Data alignment in pages.
0329  * @ctx: TTM operation context for memory allocation.
0330  * @sg: Scatter-gather table.
0331  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
0332  * @destroy: Destroy function. Use NULL for kfree().
0333  *
0334  * This function initializes a pre-allocated struct ttm_buffer_object.
0335  * As this object may be part of a larger structure, this function,
0336  * together with the @destroy function,
0337  * enables driver-specific objects derived from a ttm_buffer_object.
0338  *
0339  * On successful return, the caller owns an object kref to @bo. The kref and
0340  * list_kref are usually set to 1, but note that in some situations, other
0341  * tasks may already be holding references to @bo as well.
0342  * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
0343  * and it is the caller's responsibility to call ttm_bo_unreserve.
0344  *
0345  * If a failure occurs, the function will call the @destroy function, or
0346  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
0347  * illegal and will likely cause memory corruption.
0348  *
0349  * Returns
0350  * -ENOMEM: Out of memory.
0351  * -EINVAL: Invalid placement flags.
0352  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
0353  */
0354 
0355 int ttm_bo_init_reserved(struct ttm_device *bdev,
0356              struct ttm_buffer_object *bo,
0357              size_t size, enum ttm_bo_type type,
0358              struct ttm_placement *placement,
0359              uint32_t page_alignment,
0360              struct ttm_operation_ctx *ctx,
0361              struct sg_table *sg, struct dma_resv *resv,
0362              void (*destroy) (struct ttm_buffer_object *));
0363 
0364 /**
0365  * ttm_bo_init
0366  *
0367  * @bdev: Pointer to a ttm_device struct.
0368  * @bo: Pointer to a ttm_buffer_object to be initialized.
0369  * @size: Requested size of buffer object.
0370  * @type: Requested type of buffer object.
0371  * @placement: Initial placement for buffer object.
0372  * @page_alignment: Data alignment in pages.
0373  * @interruptible: If needing to sleep to wait for GPU resources,
0374  * sleep interruptible.
0375  * pinned in physical memory. If this behaviour is not desired, this member
0376  * holds a pointer to a persistent shmem object. Typically, this would
0377  * point to the shmem object backing a GEM object if TTM is used to back a
0378  * GEM user interface.
0379  * @sg: Scatter-gather table.
0380  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
0381  * @destroy: Destroy function. Use NULL for kfree().
0382  *
0383  * This function initializes a pre-allocated struct ttm_buffer_object.
0384  * As this object may be part of a larger structure, this function,
0385  * together with the @destroy function,
0386  * enables driver-specific objects derived from a ttm_buffer_object.
0387  *
0388  * On successful return, the caller owns an object kref to @bo. The kref and
0389  * list_kref are usually set to 1, but note that in some situations, other
0390  * tasks may already be holding references to @bo as well.
0391  *
0392  * If a failure occurs, the function will call the @destroy function, or
0393  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
0394  * illegal and will likely cause memory corruption.
0395  *
0396  * Returns
0397  * -ENOMEM: Out of memory.
0398  * -EINVAL: Invalid placement flags.
0399  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
0400  */
0401 int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
0402         size_t size, enum ttm_bo_type type,
0403         struct ttm_placement *placement,
0404         uint32_t page_alignment, bool interrubtible,
0405         struct sg_table *sg, struct dma_resv *resv,
0406         void (*destroy) (struct ttm_buffer_object *));
0407 
0408 /**
0409  * ttm_kmap_obj_virtual
0410  *
0411  * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
0412  * @is_iomem: Pointer to an integer that on return indicates 1 if the
0413  * virtual map is io memory, 0 if normal memory.
0414  *
0415  * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
0416  * If *is_iomem is 1 on return, the virtual address points to an io memory area,
0417  * that should strictly be accessed by the iowriteXX() and similar functions.
0418  */
0419 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
0420                      bool *is_iomem)
0421 {
0422     *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
0423     return map->virtual;
0424 }
0425 
0426 /**
0427  * ttm_bo_kmap
0428  *
0429  * @bo: The buffer object.
0430  * @start_page: The first page to map.
0431  * @num_pages: Number of pages to map.
0432  * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
0433  *
0434  * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
0435  * data in the buffer object. The ttm_kmap_obj_virtual function can then be
0436  * used to obtain a virtual address to the data.
0437  *
0438  * Returns
0439  * -ENOMEM: Out of memory.
0440  * -EINVAL: Invalid range.
0441  */
0442 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
0443         unsigned long num_pages, struct ttm_bo_kmap_obj *map);
0444 
0445 /**
0446  * ttm_bo_kunmap
0447  *
0448  * @map: Object describing the map to unmap.
0449  *
0450  * Unmaps a kernel map set up by ttm_bo_kmap.
0451  */
0452 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
0453 
0454 /**
0455  * ttm_bo_vmap
0456  *
0457  * @bo: The buffer object.
0458  * @map: pointer to a struct iosys_map representing the map.
0459  *
0460  * Sets up a kernel virtual mapping, using ioremap or vmap to the
0461  * data in the buffer object. The parameter @map returns the virtual
0462  * address as struct iosys_map. Unmap the buffer with ttm_bo_vunmap().
0463  *
0464  * Returns
0465  * -ENOMEM: Out of memory.
0466  * -EINVAL: Invalid range.
0467  */
0468 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct iosys_map *map);
0469 
0470 /**
0471  * ttm_bo_vunmap
0472  *
0473  * @bo: The buffer object.
0474  * @map: Object describing the map to unmap.
0475  *
0476  * Unmaps a kernel map set up by ttm_bo_vmap().
0477  */
0478 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct iosys_map *map);
0479 
0480 /**
0481  * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
0482  *
0483  * @vma:       vma as input from the fbdev mmap method.
0484  * @bo:        The bo backing the address space.
0485  *
0486  * Maps a buffer object.
0487  */
0488 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
0489 
0490 /**
0491  * ttm_bo_io
0492  *
0493  * @bdev:      Pointer to the struct ttm_device.
0494  * @filp:      Pointer to the struct file attempting to read / write.
0495  * @wbuf:      User-space pointer to address of buffer to write. NULL on read.
0496  * @rbuf:      User-space pointer to address of buffer to read into.
0497  * Null on write.
0498  * @count:     Number of bytes to read / write.
0499  * @f_pos:     Pointer to current file position.
0500  * @write:     1 for read, 0 for write.
0501  *
0502  * This function implements read / write into ttm buffer objects, and is
0503  * intended to
0504  * be called from the fops::read and fops::write method.
0505  * Returns:
0506  * See man (2) write, man(2) read. In particular,
0507  * the function may return -ERESTARTSYS if
0508  * interrupted by a signal.
0509  */
0510 ssize_t ttm_bo_io(struct ttm_device *bdev, struct file *filp,
0511           const char __user *wbuf, char __user *rbuf,
0512           size_t count, loff_t *f_pos, bool write);
0513 
0514 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
0515            gfp_t gfp_flags);
0516 
0517 void ttm_bo_pin(struct ttm_buffer_object *bo);
0518 void ttm_bo_unpin(struct ttm_buffer_object *bo);
0519 
0520 int ttm_mem_evict_first(struct ttm_device *bdev,
0521             struct ttm_resource_manager *man,
0522             const struct ttm_place *place,
0523             struct ttm_operation_ctx *ctx,
0524             struct ww_acquire_ctx *ticket);
0525 
0526 /* Default number of pre-faulted pages in the TTM fault handler */
0527 #define TTM_BO_VM_NUM_PREFAULT 16
0528 
0529 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
0530                  struct vm_fault *vmf);
0531 
0532 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
0533                     pgprot_t prot,
0534                     pgoff_t num_prefault);
0535 
0536 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
0537 
0538 void ttm_bo_vm_open(struct vm_area_struct *vma);
0539 
0540 void ttm_bo_vm_close(struct vm_area_struct *vma);
0541 
0542 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
0543              void *buf, int len, int write);
0544 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all);
0545 
0546 vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
0547 
0548 #endif