18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright 2020 Advanced Micro Devices, Inc. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation 78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * Authors: Christian König 238c2ecf20Sopenharmony_ci */ 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#ifndef _TTM_RESOURCE_H_ 268c2ecf20Sopenharmony_ci#define _TTM_RESOURCE_H_ 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#include <linux/types.h> 298c2ecf20Sopenharmony_ci#include <linux/mutex.h> 308c2ecf20Sopenharmony_ci#include <linux/dma-fence.h> 318c2ecf20Sopenharmony_ci#include <drm/drm_print.h> 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define TTM_MAX_BO_PRIORITY 4U 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistruct ttm_bo_device; 368c2ecf20Sopenharmony_cistruct ttm_resource_manager; 378c2ecf20Sopenharmony_cistruct ttm_resource; 388c2ecf20Sopenharmony_cistruct ttm_place; 398c2ecf20Sopenharmony_cistruct ttm_buffer_object; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistruct ttm_resource_manager_func { 428c2ecf20Sopenharmony_ci /** 438c2ecf20Sopenharmony_ci * struct ttm_resource_manager_func member alloc 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * @man: Pointer to a memory type manager. 468c2ecf20Sopenharmony_ci * @bo: Pointer to the buffer object we're allocating space for. 478c2ecf20Sopenharmony_ci * @placement: Placement details. 488c2ecf20Sopenharmony_ci * @flags: Additional placement flags. 498c2ecf20Sopenharmony_ci * @mem: Pointer to a struct ttm_resource to be filled in. 508c2ecf20Sopenharmony_ci * 518c2ecf20Sopenharmony_ci * This function should allocate space in the memory type managed 528c2ecf20Sopenharmony_ci * by @man. Placement details if 538c2ecf20Sopenharmony_ci * applicable are given by @placement. If successful, 548c2ecf20Sopenharmony_ci * @mem::mm_node should be set to a non-null value, and 558c2ecf20Sopenharmony_ci * @mem::start should be set to a value identifying the beginning 568c2ecf20Sopenharmony_ci * of the range allocated, and the function should return zero. 578c2ecf20Sopenharmony_ci * If the memory region accommodate the buffer object, @mem::mm_node 588c2ecf20Sopenharmony_ci * should be set to NULL, and the function should return 0. 598c2ecf20Sopenharmony_ci * If a system error occurred, preventing the request to be fulfilled, 608c2ecf20Sopenharmony_ci * the function should return a negative error code. 618c2ecf20Sopenharmony_ci * 628c2ecf20Sopenharmony_ci * Note that @mem::mm_node will only be dereferenced by 638c2ecf20Sopenharmony_ci * struct ttm_resource_manager functions and optionally by the driver, 648c2ecf20Sopenharmony_ci * which has knowledge of the underlying type. 658c2ecf20Sopenharmony_ci * 668c2ecf20Sopenharmony_ci * This function may not be called from within atomic context, so 678c2ecf20Sopenharmony_ci * an implementation can and must use either a mutex or a spinlock to 688c2ecf20Sopenharmony_ci * protect any data structures managing the space. 698c2ecf20Sopenharmony_ci */ 708c2ecf20Sopenharmony_ci int (*alloc)(struct ttm_resource_manager *man, 718c2ecf20Sopenharmony_ci struct ttm_buffer_object *bo, 728c2ecf20Sopenharmony_ci const struct ttm_place *place, 738c2ecf20Sopenharmony_ci struct ttm_resource *mem); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci /** 768c2ecf20Sopenharmony_ci * struct ttm_resource_manager_func member free 778c2ecf20Sopenharmony_ci * 788c2ecf20Sopenharmony_ci * @man: Pointer to a memory type manager. 798c2ecf20Sopenharmony_ci * @mem: Pointer to a struct ttm_resource to be filled in. 808c2ecf20Sopenharmony_ci * 818c2ecf20Sopenharmony_ci * This function frees memory type resources previously allocated 828c2ecf20Sopenharmony_ci * and that are identified by @mem::mm_node and @mem::start. May not 838c2ecf20Sopenharmony_ci * be called from within atomic context. 848c2ecf20Sopenharmony_ci */ 858c2ecf20Sopenharmony_ci void (*free)(struct ttm_resource_manager *man, 868c2ecf20Sopenharmony_ci struct ttm_resource *mem); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci /** 898c2ecf20Sopenharmony_ci * struct ttm_resource_manager_func member debug 908c2ecf20Sopenharmony_ci * 918c2ecf20Sopenharmony_ci * @man: Pointer to a memory type manager. 928c2ecf20Sopenharmony_ci * @printer: Prefix to be used in printout to identify the caller. 938c2ecf20Sopenharmony_ci * 948c2ecf20Sopenharmony_ci * This function is called to print out the state of the memory 958c2ecf20Sopenharmony_ci * type manager to aid debugging of out-of-memory conditions. 968c2ecf20Sopenharmony_ci * It may not be called from within atomic context. 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_ci void (*debug)(struct ttm_resource_manager *man, 998c2ecf20Sopenharmony_ci struct drm_printer *printer); 1008c2ecf20Sopenharmony_ci}; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci/** 1038c2ecf20Sopenharmony_ci * struct ttm_resource_manager 1048c2ecf20Sopenharmony_ci * 1058c2ecf20Sopenharmony_ci * @use_type: The memory type is enabled. 1068c2ecf20Sopenharmony_ci * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory 1078c2ecf20Sopenharmony_ci * managed by this memory type. 1088c2ecf20Sopenharmony_ci * @gpu_offset: If used, the GPU offset of the first managed page of 1098c2ecf20Sopenharmony_ci * fixed memory or the first managed location in an aperture. 1108c2ecf20Sopenharmony_ci * @size: Size of the managed region. 1118c2ecf20Sopenharmony_ci * @func: structure pointer implementing the range manager. See above 1128c2ecf20Sopenharmony_ci * @move_lock: lock for move fence 1138c2ecf20Sopenharmony_ci * static information. bdev::driver::io_mem_free is never used. 1148c2ecf20Sopenharmony_ci * @lru: The lru list for this memory type. 1158c2ecf20Sopenharmony_ci * @move: The fence of the last pipelined move operation. 1168c2ecf20Sopenharmony_ci * 1178c2ecf20Sopenharmony_ci * This structure is used to identify and manage memory types for a device. 1188c2ecf20Sopenharmony_ci */ 1198c2ecf20Sopenharmony_cistruct ttm_resource_manager { 1208c2ecf20Sopenharmony_ci /* 1218c2ecf20Sopenharmony_ci * No protection. Constant from start. 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_ci bool use_type; 1248c2ecf20Sopenharmony_ci bool use_tt; 1258c2ecf20Sopenharmony_ci uint64_t size; 1268c2ecf20Sopenharmony_ci const struct ttm_resource_manager_func *func; 1278c2ecf20Sopenharmony_ci spinlock_t move_lock; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci /* 1308c2ecf20Sopenharmony_ci * Protected by the global->lru_lock. 1318c2ecf20Sopenharmony_ci */ 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci struct list_head lru[TTM_MAX_BO_PRIORITY]; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* 1368c2ecf20Sopenharmony_ci * Protected by @move_lock. 1378c2ecf20Sopenharmony_ci */ 1388c2ecf20Sopenharmony_ci struct dma_fence *move; 1398c2ecf20Sopenharmony_ci}; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci/** 1428c2ecf20Sopenharmony_ci * struct ttm_bus_placement 1438c2ecf20Sopenharmony_ci * 1448c2ecf20Sopenharmony_ci * @addr: mapped virtual address 1458c2ecf20Sopenharmony_ci * @offset: physical addr 1468c2ecf20Sopenharmony_ci * @is_iomem: is this io memory ? 1478c2ecf20Sopenharmony_ci * 1488c2ecf20Sopenharmony_ci * Structure indicating the bus placement of an object. 1498c2ecf20Sopenharmony_ci */ 1508c2ecf20Sopenharmony_cistruct ttm_bus_placement { 1518c2ecf20Sopenharmony_ci void *addr; 1528c2ecf20Sopenharmony_ci phys_addr_t offset; 1538c2ecf20Sopenharmony_ci bool is_iomem; 1548c2ecf20Sopenharmony_ci}; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci/** 1578c2ecf20Sopenharmony_ci * struct ttm_resource 1588c2ecf20Sopenharmony_ci * 1598c2ecf20Sopenharmony_ci * @mm_node: Memory manager node. 1608c2ecf20Sopenharmony_ci * @size: Requested size of memory region. 1618c2ecf20Sopenharmony_ci * @num_pages: Actual size of memory region in pages. 1628c2ecf20Sopenharmony_ci * @page_alignment: Page alignment. 1638c2ecf20Sopenharmony_ci * @placement: Placement flags. 1648c2ecf20Sopenharmony_ci * @bus: Placement on io bus accessible to the CPU 1658c2ecf20Sopenharmony_ci * 1668c2ecf20Sopenharmony_ci * Structure indicating the placement and space resources used by a 1678c2ecf20Sopenharmony_ci * buffer object. 1688c2ecf20Sopenharmony_ci */ 1698c2ecf20Sopenharmony_cistruct ttm_resource { 1708c2ecf20Sopenharmony_ci void *mm_node; 1718c2ecf20Sopenharmony_ci unsigned long start; 1728c2ecf20Sopenharmony_ci unsigned long size; 1738c2ecf20Sopenharmony_ci unsigned long num_pages; 1748c2ecf20Sopenharmony_ci uint32_t page_alignment; 1758c2ecf20Sopenharmony_ci uint32_t mem_type; 1768c2ecf20Sopenharmony_ci uint32_t placement; 1778c2ecf20Sopenharmony_ci struct ttm_bus_placement bus; 1788c2ecf20Sopenharmony_ci}; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci/** 1818c2ecf20Sopenharmony_ci * ttm_resource_manager_set_used 1828c2ecf20Sopenharmony_ci * 1838c2ecf20Sopenharmony_ci * @man: A memory manager object. 1848c2ecf20Sopenharmony_ci * @used: usage state to set. 1858c2ecf20Sopenharmony_ci * 1868c2ecf20Sopenharmony_ci * Set the manager in use flag. If disabled the manager is no longer 1878c2ecf20Sopenharmony_ci * used for object placement. 1888c2ecf20Sopenharmony_ci */ 1898c2ecf20Sopenharmony_cistatic inline void 1908c2ecf20Sopenharmony_cittm_resource_manager_set_used(struct ttm_resource_manager *man, bool used) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci man->use_type = used; 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci/** 1968c2ecf20Sopenharmony_ci * ttm_resource_manager_used 1978c2ecf20Sopenharmony_ci * 1988c2ecf20Sopenharmony_ci * @man: Manager to get used state for 1998c2ecf20Sopenharmony_ci * 2008c2ecf20Sopenharmony_ci * Get the in use flag for a manager. 2018c2ecf20Sopenharmony_ci * Returns: 2028c2ecf20Sopenharmony_ci * true is used, false if not. 2038c2ecf20Sopenharmony_ci */ 2048c2ecf20Sopenharmony_cistatic inline bool ttm_resource_manager_used(struct ttm_resource_manager *man) 2058c2ecf20Sopenharmony_ci{ 2068c2ecf20Sopenharmony_ci return man->use_type; 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci/** 2108c2ecf20Sopenharmony_ci * ttm_resource_manager_cleanup 2118c2ecf20Sopenharmony_ci * 2128c2ecf20Sopenharmony_ci * @man: A memory manager object. 2138c2ecf20Sopenharmony_ci * 2148c2ecf20Sopenharmony_ci * Cleanup the move fences from the memory manager object. 2158c2ecf20Sopenharmony_ci */ 2168c2ecf20Sopenharmony_cistatic inline void 2178c2ecf20Sopenharmony_cittm_resource_manager_cleanup(struct ttm_resource_manager *man) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci dma_fence_put(man->move); 2208c2ecf20Sopenharmony_ci man->move = NULL; 2218c2ecf20Sopenharmony_ci} 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ciint ttm_resource_alloc(struct ttm_buffer_object *bo, 2248c2ecf20Sopenharmony_ci const struct ttm_place *place, 2258c2ecf20Sopenharmony_ci struct ttm_resource *res); 2268c2ecf20Sopenharmony_civoid ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource *res); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_civoid ttm_resource_manager_init(struct ttm_resource_manager *man, 2298c2ecf20Sopenharmony_ci unsigned long p_size); 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ciint ttm_resource_manager_force_list_clean(struct ttm_bo_device *bdev, 2328c2ecf20Sopenharmony_ci struct ttm_resource_manager *man); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_civoid ttm_resource_manager_debug(struct ttm_resource_manager *man, 2358c2ecf20Sopenharmony_ci struct drm_printer *p); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci#endif 238