Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2020 Advanced Micro Devices, Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: Christian König
0023  */
0024 
0025 #ifndef _TTM_DEVICE_H_
0026 #define _TTM_DEVICE_H_
0027 
0028 #include <linux/types.h>
0029 #include <linux/workqueue.h>
0030 #include <drm/ttm/ttm_resource.h>
0031 #include <drm/ttm/ttm_pool.h>
0032 
0033 struct ttm_device;
0034 struct ttm_placement;
0035 struct ttm_buffer_object;
0036 struct ttm_operation_ctx;
0037 
0038 /**
0039  * struct ttm_global - Buffer object driver global data.
0040  */
0041 extern struct ttm_global {
0042 
0043     /**
0044      * @dummy_read_page: Pointer to a dummy page used for mapping requests
0045      * of unpopulated pages. Constant after init.
0046      */
0047     struct page *dummy_read_page;
0048 
0049     /**
0050      * @device_list: List of buffer object devices. Protected by
0051      * ttm_global_mutex.
0052      */
0053     struct list_head device_list;
0054 
0055     /**
0056      * @bo_count: Number of buffer objects allocated by devices.
0057      */
0058     atomic_t bo_count;
0059 } ttm_glob;
0060 
0061 struct ttm_device_funcs {
0062     /**
0063      * ttm_tt_create
0064      *
0065      * @bo: The buffer object to create the ttm for.
0066      * @page_flags: Page flags as identified by TTM_TT_FLAG_XX flags.
0067      *
0068      * Create a struct ttm_tt to back data with system memory pages.
0069      * No pages are actually allocated.
0070      * Returns:
0071      * NULL: Out of memory.
0072      */
0073     struct ttm_tt *(*ttm_tt_create)(struct ttm_buffer_object *bo,
0074                     uint32_t page_flags);
0075 
0076     /**
0077      * ttm_tt_populate
0078      *
0079      * @ttm: The struct ttm_tt to contain the backing pages.
0080      *
0081      * Allocate all backing pages
0082      * Returns:
0083      * -ENOMEM: Out of memory.
0084      */
0085     int (*ttm_tt_populate)(struct ttm_device *bdev,
0086                    struct ttm_tt *ttm,
0087                    struct ttm_operation_ctx *ctx);
0088 
0089     /**
0090      * ttm_tt_unpopulate
0091      *
0092      * @ttm: The struct ttm_tt to contain the backing pages.
0093      *
0094      * Free all backing page
0095      */
0096     void (*ttm_tt_unpopulate)(struct ttm_device *bdev,
0097                   struct ttm_tt *ttm);
0098 
0099     /**
0100      * ttm_tt_destroy
0101      *
0102      * @bdev: Pointer to a ttm device
0103      * @ttm: Pointer to a struct ttm_tt.
0104      *
0105      * Destroy the backend. This will be call back from ttm_tt_destroy so
0106      * don't call ttm_tt_destroy from the callback or infinite loop.
0107      */
0108     void (*ttm_tt_destroy)(struct ttm_device *bdev, struct ttm_tt *ttm);
0109 
0110     /**
0111      * struct ttm_bo_driver member eviction_valuable
0112      *
0113      * @bo: the buffer object to be evicted
0114      * @place: placement we need room for
0115      *
0116      * Check with the driver if it is valuable to evict a BO to make room
0117      * for a certain placement.
0118      */
0119     bool (*eviction_valuable)(struct ttm_buffer_object *bo,
0120                   const struct ttm_place *place);
0121     /**
0122      * struct ttm_bo_driver member evict_flags:
0123      *
0124      * @bo: the buffer object to be evicted
0125      *
0126      * Return the bo flags for a buffer which is not mapped to the hardware.
0127      * These will be placed in proposed_flags so that when the move is
0128      * finished, they'll end up in bo->mem.flags
0129      * This should not cause multihop evictions, and the core will warn
0130      * if one is proposed.
0131      */
0132 
0133     void (*evict_flags)(struct ttm_buffer_object *bo,
0134                 struct ttm_placement *placement);
0135 
0136     /**
0137      * struct ttm_bo_driver member move:
0138      *
0139      * @bo: the buffer to move
0140      * @evict: whether this motion is evicting the buffer from
0141      * the graphics address space
0142      * @ctx: context for this move with parameters
0143      * @new_mem: the new memory region receiving the buffer
0144      @ @hop: placement for driver directed intermediate hop
0145      *
0146      * Move a buffer between two memory regions.
0147      * Returns errno -EMULTIHOP if driver requests a hop
0148      */
0149     int (*move)(struct ttm_buffer_object *bo, bool evict,
0150             struct ttm_operation_ctx *ctx,
0151             struct ttm_resource *new_mem,
0152             struct ttm_place *hop);
0153 
0154     /**
0155      * Hook to notify driver about a resource delete.
0156      */
0157     void (*delete_mem_notify)(struct ttm_buffer_object *bo);
0158 
0159     /**
0160      * notify the driver that we're about to swap out this bo
0161      */
0162     void (*swap_notify)(struct ttm_buffer_object *bo);
0163 
0164     /**
0165      * Driver callback on when mapping io memory (for bo_move_memcpy
0166      * for instance). TTM will take care to call io_mem_free whenever
0167      * the mapping is not use anymore. io_mem_reserve & io_mem_free
0168      * are balanced.
0169      */
0170     int (*io_mem_reserve)(struct ttm_device *bdev,
0171                   struct ttm_resource *mem);
0172     void (*io_mem_free)(struct ttm_device *bdev,
0173                 struct ttm_resource *mem);
0174 
0175     /**
0176      * Return the pfn for a given page_offset inside the BO.
0177      *
0178      * @bo: the BO to look up the pfn for
0179      * @page_offset: the offset to look up
0180      */
0181     unsigned long (*io_mem_pfn)(struct ttm_buffer_object *bo,
0182                     unsigned long page_offset);
0183 
0184     /**
0185      * Read/write memory buffers for ptrace access
0186      *
0187      * @bo: the BO to access
0188      * @offset: the offset from the start of the BO
0189      * @buf: pointer to source/destination buffer
0190      * @len: number of bytes to copy
0191      * @write: whether to read (0) from or write (non-0) to BO
0192      *
0193      * If successful, this function should return the number of
0194      * bytes copied, -EIO otherwise. If the number of bytes
0195      * returned is < len, the function may be called again with
0196      * the remainder of the buffer to copy.
0197      */
0198     int (*access_memory)(struct ttm_buffer_object *bo, unsigned long offset,
0199                  void *buf, int len, int write);
0200 
0201     /**
0202      * Notify the driver that we're about to release a BO
0203      *
0204      * @bo: BO that is about to be released
0205      *
0206      * Gives the driver a chance to do any cleanup, including
0207      * adding fences that may force a delayed delete
0208      */
0209     void (*release_notify)(struct ttm_buffer_object *bo);
0210 };
0211 
0212 /**
0213  * struct ttm_device - Buffer object driver device-specific data.
0214  */
0215 struct ttm_device {
0216     /**
0217      * @device_list: Our entry in the global device list.
0218      * Constant after bo device init
0219      */
0220     struct list_head device_list;
0221 
0222     /**
0223      * @funcs: Function table for the device.
0224      * Constant after bo device init
0225      */
0226     struct ttm_device_funcs *funcs;
0227 
0228     /**
0229      * @sysman: Resource manager for the system domain.
0230      * Access via ttm_manager_type.
0231      */
0232     struct ttm_resource_manager sysman;
0233 
0234     /**
0235      * @man_drv: An array of resource_managers, one per resource type.
0236      */
0237     struct ttm_resource_manager *man_drv[TTM_NUM_MEM_TYPES];
0238 
0239     /**
0240      * @vma_manager: Address space manager for finding BOs to mmap.
0241      */
0242     struct drm_vma_offset_manager *vma_manager;
0243 
0244     /**
0245      * @pool: page pool for the device.
0246      */
0247     struct ttm_pool pool;
0248 
0249     /**
0250      * @lru_lock: Protection for the per manager LRU and ddestroy lists.
0251      */
0252     spinlock_t lru_lock;
0253 
0254     /**
0255      * @ddestroy: Destroyed but not yet cleaned up buffer objects.
0256      */
0257     struct list_head ddestroy;
0258 
0259     /**
0260      * @pinned: Buffer objects which are pinned and so not on any LRU list.
0261      */
0262     struct list_head pinned;
0263 
0264     /**
0265      * @dev_mapping: A pointer to the struct address_space for invalidating
0266      * CPU mappings on buffer move. Protected by load/unload sync.
0267      */
0268     struct address_space *dev_mapping;
0269 
0270     /**
0271      * @wq: Work queue structure for the delayed delete workqueue.
0272      */
0273     struct delayed_work wq;
0274 };
0275 
0276 int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags);
0277 int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
0278                gfp_t gfp_flags);
0279 
0280 static inline struct ttm_resource_manager *
0281 ttm_manager_type(struct ttm_device *bdev, int mem_type)
0282 {
0283     BUILD_BUG_ON(__builtin_constant_p(mem_type)
0284              && mem_type >= TTM_NUM_MEM_TYPES);
0285     return bdev->man_drv[mem_type];
0286 }
0287 
0288 static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
0289                       struct ttm_resource_manager *manager)
0290 {
0291     BUILD_BUG_ON(__builtin_constant_p(type) && type >= TTM_NUM_MEM_TYPES);
0292     bdev->man_drv[type] = manager;
0293 }
0294 
0295 int ttm_device_init(struct ttm_device *bdev, struct ttm_device_funcs *funcs,
0296             struct device *dev, struct address_space *mapping,
0297             struct drm_vma_offset_manager *vma_manager,
0298             bool use_dma_alloc, bool use_dma32);
0299 void ttm_device_fini(struct ttm_device *bdev);
0300 void ttm_device_clear_dma_mappings(struct ttm_device *bdev);
0301 
0302 #endif