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 #ifndef _TTM_BO_DRIVER_H_
0031 #define _TTM_BO_DRIVER_H_
0032 
0033 #include <drm/drm_mm.h>
0034 #include <drm/drm_vma_manager.h>
0035 #include <linux/workqueue.h>
0036 #include <linux/fs.h>
0037 #include <linux/spinlock.h>
0038 #include <linux/dma-resv.h>
0039 
0040 #include <drm/ttm/ttm_device.h>
0041 
0042 #include "ttm_bo_api.h"
0043 #include "ttm_kmap_iter.h"
0044 #include "ttm_placement.h"
0045 #include "ttm_tt.h"
0046 #include "ttm_pool.h"
0047 
0048 /*
0049  * ttm_bo.c
0050  */
0051 
0052 /**
0053  * ttm_bo_mem_space
0054  *
0055  * @bo: Pointer to a struct ttm_buffer_object. the data of which
0056  * we want to allocate space for.
0057  * @proposed_placement: Proposed new placement for the buffer object.
0058  * @mem: A struct ttm_resource.
0059  * @interruptible: Sleep interruptible when sliping.
0060  * @no_wait_gpu: Return immediately if the GPU is busy.
0061  *
0062  * Allocate memory space for the buffer object pointed to by @bo, using
0063  * the placement flags in @mem, potentially evicting other idle buffer objects.
0064  * This function may sleep while waiting for space to become available.
0065  * Returns:
0066  * -EBUSY: No space available (only if no_wait == 1).
0067  * -ENOMEM: Could not allocate memory for the buffer object, either due to
0068  * fragmentation or concurrent allocators.
0069  * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
0070  */
0071 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
0072              struct ttm_placement *placement,
0073              struct ttm_resource **mem,
0074              struct ttm_operation_ctx *ctx);
0075 
0076 /**
0077  * ttm_bo_unmap_virtual
0078  *
0079  * @bo: tear down the virtual mappings for this BO
0080  */
0081 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
0082 
0083 /**
0084  * ttm_bo_reserve:
0085  *
0086  * @bo: A pointer to a struct ttm_buffer_object.
0087  * @interruptible: Sleep interruptible if waiting.
0088  * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
0089  * @ticket: ticket used to acquire the ww_mutex.
0090  *
0091  * Locks a buffer object for validation. (Or prevents other processes from
0092  * locking it for validation), while taking a number of measures to prevent
0093  * deadlocks.
0094  *
0095  * Returns:
0096  * -EDEADLK: The reservation may cause a deadlock.
0097  * Release all buffer reservations, wait for @bo to become unreserved and
0098  * try again.
0099  * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
0100  * a signal. Release all buffer reservations and return to user-space.
0101  * -EBUSY: The function needed to sleep, but @no_wait was true
0102  * -EALREADY: Bo already reserved using @ticket. This error code will only
0103  * be returned if @use_ticket is set to true.
0104  */
0105 static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
0106                  bool interruptible, bool no_wait,
0107                  struct ww_acquire_ctx *ticket)
0108 {
0109     int ret = 0;
0110 
0111     if (no_wait) {
0112         bool success;
0113         if (WARN_ON(ticket))
0114             return -EBUSY;
0115 
0116         success = dma_resv_trylock(bo->base.resv);
0117         return success ? 0 : -EBUSY;
0118     }
0119 
0120     if (interruptible)
0121         ret = dma_resv_lock_interruptible(bo->base.resv, ticket);
0122     else
0123         ret = dma_resv_lock(bo->base.resv, ticket);
0124     if (ret == -EINTR)
0125         return -ERESTARTSYS;
0126     return ret;
0127 }
0128 
0129 /**
0130  * ttm_bo_reserve_slowpath:
0131  * @bo: A pointer to a struct ttm_buffer_object.
0132  * @interruptible: Sleep interruptible if waiting.
0133  * @sequence: Set (@bo)->sequence to this value after lock
0134  *
0135  * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
0136  * from all our other reservations. Because there are no other reservations
0137  * held by us, this function cannot deadlock any more.
0138  */
0139 static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
0140                       bool interruptible,
0141                       struct ww_acquire_ctx *ticket)
0142 {
0143     if (interruptible) {
0144         int ret = dma_resv_lock_slow_interruptible(bo->base.resv,
0145                                ticket);
0146         if (ret == -EINTR)
0147             ret = -ERESTARTSYS;
0148         return ret;
0149     }
0150     dma_resv_lock_slow(bo->base.resv, ticket);
0151     return 0;
0152 }
0153 
0154 static inline void
0155 ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
0156 {
0157     spin_lock(&bo->bdev->lru_lock);
0158     ttm_bo_move_to_lru_tail(bo);
0159     spin_unlock(&bo->bdev->lru_lock);
0160 }
0161 
0162 static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
0163                      struct ttm_resource *new_mem)
0164 {
0165     WARN_ON(bo->resource);
0166     bo->resource = new_mem;
0167 }
0168 
0169 /**
0170  * ttm_bo_move_null = assign memory for a buffer object.
0171  * @bo: The bo to assign the memory to
0172  * @new_mem: The memory to be assigned.
0173  *
0174  * Assign the memory from new_mem to the memory of the buffer object bo.
0175  */
0176 static inline void ttm_bo_move_null(struct ttm_buffer_object *bo,
0177                     struct ttm_resource *new_mem)
0178 {
0179     ttm_resource_free(bo, &bo->resource);
0180     ttm_bo_assign_mem(bo, new_mem);
0181 }
0182 
0183 /**
0184  * ttm_bo_unreserve
0185  *
0186  * @bo: A pointer to a struct ttm_buffer_object.
0187  *
0188  * Unreserve a previous reservation of @bo.
0189  */
0190 static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
0191 {
0192     ttm_bo_move_to_lru_tail_unlocked(bo);
0193     dma_resv_unlock(bo->base.resv);
0194 }
0195 
0196 /*
0197  * ttm_bo_util.c
0198  */
0199 int ttm_mem_io_reserve(struct ttm_device *bdev,
0200                struct ttm_resource *mem);
0201 void ttm_mem_io_free(struct ttm_device *bdev,
0202              struct ttm_resource *mem);
0203 
0204 /**
0205  * ttm_bo_move_memcpy
0206  *
0207  * @bo: A pointer to a struct ttm_buffer_object.
0208  * @interruptible: Sleep interruptible if waiting.
0209  * @no_wait_gpu: Return immediately if the GPU is busy.
0210  * @new_mem: struct ttm_resource indicating where to move.
0211  *
0212  * Fallback move function for a mappable buffer object in mappable memory.
0213  * The function will, if successful,
0214  * free any old aperture space, and set (@new_mem)->mm_node to NULL,
0215  * and update the (@bo)->mem placement flags. If unsuccessful, the old
0216  * data remains untouched, and it's up to the caller to free the
0217  * memory space indicated by @new_mem.
0218  * Returns:
0219  * !0: Failure.
0220  */
0221 
0222 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
0223                struct ttm_operation_ctx *ctx,
0224                struct ttm_resource *new_mem);
0225 
0226 /**
0227  * ttm_bo_move_accel_cleanup.
0228  *
0229  * @bo: A pointer to a struct ttm_buffer_object.
0230  * @fence: A fence object that signals when moving is complete.
0231  * @evict: This is an evict move. Don't return until the buffer is idle.
0232  * @pipeline: evictions are to be pipelined.
0233  * @new_mem: struct ttm_resource indicating where to move.
0234  *
0235  * Accelerated move function to be called when an accelerated move
0236  * has been scheduled. The function will create a new temporary buffer object
0237  * representing the old placement, and put the sync object on both buffer
0238  * objects. After that the newly created buffer object is unref'd to be
0239  * destroyed when the move is complete. This will help pipeline
0240  * buffer moves.
0241  */
0242 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
0243                   struct dma_fence *fence, bool evict,
0244                   bool pipeline,
0245                   struct ttm_resource *new_mem);
0246 
0247 /**
0248  * ttm_bo_move_sync_cleanup.
0249  *
0250  * @bo: A pointer to a struct ttm_buffer_object.
0251  * @new_mem: struct ttm_resource indicating where to move.
0252  *
0253  * Special case of ttm_bo_move_accel_cleanup where the bo is guaranteed
0254  * by the caller to be idle. Typically used after memcpy buffer moves.
0255  */
0256 void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo,
0257                   struct ttm_resource *new_mem);
0258 
0259 /**
0260  * ttm_bo_pipeline_gutting.
0261  *
0262  * @bo: A pointer to a struct ttm_buffer_object.
0263  *
0264  * Pipelined gutting a BO of its backing store.
0265  */
0266 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);
0267 
0268 /**
0269  * ttm_io_prot
0270  *
0271  * bo: ttm buffer object
0272  * res: ttm resource object
0273  * @tmp: Page protection flag for a normal, cached mapping.
0274  *
0275  * Utility function that returns the pgprot_t that should be used for
0276  * setting up a PTE with the caching model indicated by @c_state.
0277  */
0278 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
0279              pgprot_t tmp);
0280 
0281 /**
0282  * ttm_bo_tt_bind
0283  *
0284  * Bind the object tt to a memory resource.
0285  */
0286 int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem);
0287 
0288 /**
0289  * ttm_bo_tt_destroy.
0290  */
0291 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
0292 
0293 void ttm_move_memcpy(bool clear,
0294              u32 num_pages,
0295              struct ttm_kmap_iter *dst_iter,
0296              struct ttm_kmap_iter *src_iter);
0297 
0298 struct ttm_kmap_iter *
0299 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
0300              struct io_mapping *iomap,
0301              struct sg_table *st,
0302              resource_size_t start);
0303 #endif