2 * Copyright 2020 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: Christian König
25 #ifndef _TTM_RESOURCE_H_
26 #define _TTM_RESOURCE_H_
28 #include <linux/types.h>
29 #include <linux/list.h>
30 #include <linux/mutex.h>
31 #include <linux/iosys-map.h>
32 #include <linux/dma-fence.h>
34 #include <drm/drm_print.h>
35 #include <drm/ttm/ttm_caching.h>
36 #include <drm/ttm/ttm_kmap_iter.h>
38 #define TTM_MAX_BO_PRIORITY 4U
39 #define TTM_NUM_MEM_TYPES 8
42 struct ttm_resource_manager;
45 struct ttm_buffer_object;
52 struct ttm_resource_manager_func {
54 * struct ttm_resource_manager_func member alloc
56 * @man: Pointer to a memory type manager.
57 * @bo: Pointer to the buffer object we're allocating space for.
58 * @place: Placement details.
59 * @res: Resulting pointer to the ttm_resource.
61 * This function should allocate space in the memory type managed
62 * by @man. Placement details if applicable are given by @place. If
63 * successful, a filled in ttm_resource object should be returned in
64 * @res. @res::start should be set to a value identifying the beginning
65 * of the range allocated, and the function should return zero.
66 * If the manager can't fulfill the request -ENOSPC should be returned.
67 * If a system error occurred, preventing the request to be fulfilled,
68 * the function should return a negative error code.
70 * This function may not be called from within atomic context and needs
71 * to take care of its own locking to protect any data structures
74 int (*alloc)(struct ttm_resource_manager *man,
75 struct ttm_buffer_object *bo,
76 const struct ttm_place *place,
77 struct ttm_resource **res);
80 * struct ttm_resource_manager_func member free
82 * @man: Pointer to a memory type manager.
83 * @res: Pointer to a struct ttm_resource to be freed.
85 * This function frees memory type resources previously allocated.
86 * May not be called from within atomic context.
88 void (*free)(struct ttm_resource_manager *man,
89 struct ttm_resource *res);
92 * struct ttm_resource_manager_func member debug
94 * @man: Pointer to a memory type manager.
95 * @printer: Prefix to be used in printout to identify the caller.
97 * This function is called to print out the state of the memory
98 * type manager to aid debugging of out-of-memory conditions.
99 * It may not be called from within atomic context.
101 void (*debug)(struct ttm_resource_manager *man,
102 struct drm_printer *printer);
106 * struct ttm_resource_manager
108 * @use_type: The memory type is enabled.
109 * @use_tt: If a TT object should be used for the backing store.
110 * @size: Size of the managed region.
111 * @bdev: ttm device this manager belongs to
112 * @func: structure pointer implementing the range manager. See above
113 * @move_lock: lock for move fence
114 * @move: The fence of the last pipelined move operation.
115 * @lru: The lru list for this memory type.
117 * This structure is used to identify and manage memory types for a device.
119 struct ttm_resource_manager {
121 * No protection. Constant from start.
125 struct ttm_device *bdev;
127 const struct ttm_resource_manager_func *func;
128 spinlock_t move_lock;
131 * Protected by @move_lock.
133 struct dma_fence *move;
136 * Protected by the bdev->lru_lock.
138 struct list_head lru[TTM_MAX_BO_PRIORITY];
141 * @usage: How much of the resources are used, protected by the
148 * struct ttm_bus_placement
150 * @addr: mapped virtual address
151 * @offset: physical addr
152 * @is_iomem: is this io memory ?
153 * @caching: See enum ttm_caching
155 * Structure indicating the bus placement of an object.
157 struct ttm_bus_placement {
161 enum ttm_caching caching;
165 * struct ttm_resource
167 * @start: Start of the allocation.
168 * @num_pages: Actual size of resource in pages.
169 * @mem_type: Resource type of the allocation.
170 * @placement: Placement flags.
171 * @bus: Placement on io bus accessible to the CPU
172 * @bo: weak reference to the BO, protected by ttm_device::lru_lock
174 * Structure indicating the placement and space resources used by a
177 struct ttm_resource {
179 unsigned long num_pages;
182 struct ttm_bus_placement bus;
183 struct ttm_buffer_object *bo;
186 * @lru: Least recently used list, see &ttm_resource_manager.lru
188 struct list_head lru;
192 * struct ttm_resource_cursor
194 * @priority: the current priority
196 * Cursor to iterate over the resources in a manager.
198 struct ttm_resource_cursor {
199 unsigned int priority;
203 * struct ttm_lru_bulk_move_pos
205 * @first: first res in the bulk move range
206 * @last: last res in the bulk move range
208 * Range of resources for a lru bulk move.
210 struct ttm_lru_bulk_move_pos {
211 struct ttm_resource *first;
212 struct ttm_resource *last;
216 * struct ttm_lru_bulk_move
218 * @pos: first/last lru entry for resources in the each domain/priority
220 * Container for the current bulk move state. Should be used with
221 * ttm_lru_bulk_move_init() and ttm_bo_set_bulk_move().
223 struct ttm_lru_bulk_move {
224 struct ttm_lru_bulk_move_pos pos[TTM_NUM_MEM_TYPES][TTM_MAX_BO_PRIORITY];
228 * struct ttm_kmap_iter_iomap - Specialization for a struct io_mapping +
229 * struct sg_table backed struct ttm_resource.
230 * @base: Embedded struct ttm_kmap_iter providing the usage interface.
231 * @iomap: struct io_mapping representing the underlying linear io_memory.
232 * @st: sg_table into @iomap, representing the memory of the struct ttm_resource.
233 * @start: Offset that needs to be subtracted from @st to make
234 * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
235 * @cache: Scatterlist traversal cache for fast lookups.
236 * @cache.sg: Pointer to the currently cached scatterlist segment.
237 * @cache.i: First index of @sg. PAGE_SIZE granularity.
238 * @cache.end: Last index + 1 of @sg. PAGE_SIZE granularity.
239 * @cache.offs: First offset into @iomap of @sg. PAGE_SIZE granularity.
241 struct ttm_kmap_iter_iomap {
242 struct ttm_kmap_iter base;
243 struct io_mapping *iomap;
245 resource_size_t start;
247 struct scatterlist *sg;
255 * struct ttm_kmap_iter_linear_io - Iterator specialization for linear io
256 * @base: The base iterator
257 * @dmap: Points to the starting address of the region
258 * @needs_unmap: Whether we need to unmap on fini
260 struct ttm_kmap_iter_linear_io {
261 struct ttm_kmap_iter base;
262 struct iosys_map dmap;
267 * ttm_resource_manager_set_used
269 * @man: A memory manager object.
270 * @used: usage state to set.
272 * Set the manager in use flag. If disabled the manager is no longer
273 * used for object placement.
276 ttm_resource_manager_set_used(struct ttm_resource_manager *man, bool used)
280 for (i = 0; i < TTM_MAX_BO_PRIORITY; i++)
281 WARN_ON(!list_empty(&man->lru[i]));
282 man->use_type = used;
286 * ttm_resource_manager_used
288 * @man: Manager to get used state for
290 * Get the in use flag for a manager.
292 * true is used, false if not.
294 static inline bool ttm_resource_manager_used(struct ttm_resource_manager *man)
296 return man->use_type;
300 * ttm_resource_manager_cleanup
302 * @man: A memory manager object.
304 * Cleanup the move fences from the memory manager object.
307 ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
309 dma_fence_put(man->move);
313 void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk);
314 void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk);
316 void ttm_resource_add_bulk_move(struct ttm_resource *res,
317 struct ttm_buffer_object *bo);
318 void ttm_resource_del_bulk_move(struct ttm_resource *res,
319 struct ttm_buffer_object *bo);
320 void ttm_resource_move_to_lru_tail(struct ttm_resource *res);
322 void ttm_resource_init(struct ttm_buffer_object *bo,
323 const struct ttm_place *place,
324 struct ttm_resource *res);
325 void ttm_resource_fini(struct ttm_resource_manager *man,
326 struct ttm_resource *res);
328 int ttm_resource_alloc(struct ttm_buffer_object *bo,
329 const struct ttm_place *place,
330 struct ttm_resource **res);
331 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res);
332 bool ttm_resource_compat(struct ttm_resource *res,
333 struct ttm_placement *placement);
334 void ttm_resource_set_bo(struct ttm_resource *res,
335 struct ttm_buffer_object *bo);
337 void ttm_resource_manager_init(struct ttm_resource_manager *man,
338 struct ttm_device *bdev,
341 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
342 struct ttm_resource_manager *man);
344 uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man);
345 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
346 struct drm_printer *p);
348 struct ttm_resource *
349 ttm_resource_manager_first(struct ttm_resource_manager *man,
350 struct ttm_resource_cursor *cursor);
351 struct ttm_resource *
352 ttm_resource_manager_next(struct ttm_resource_manager *man,
353 struct ttm_resource_cursor *cursor,
354 struct ttm_resource *res);
357 * ttm_resource_manager_for_each_res - iterate over all resources
358 * @man: the resource manager
359 * @cursor: struct ttm_resource_cursor for the current position
360 * @res: the current resource
362 * Iterate over all the evictable resources in a resource manager.
364 #define ttm_resource_manager_for_each_res(man, cursor, res) \
365 for (res = ttm_resource_manager_first(man, cursor); res; \
366 res = ttm_resource_manager_next(man, cursor, res))
368 struct ttm_kmap_iter *
369 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
370 struct io_mapping *iomap,
372 resource_size_t start);
374 struct ttm_kmap_iter_linear_io;
376 struct ttm_kmap_iter *
377 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
378 struct ttm_device *bdev,
379 struct ttm_resource *mem);
381 void ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
382 struct ttm_device *bdev,
383 struct ttm_resource *mem);
385 void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
386 struct dentry * parent,