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/mutex.h>
30 #include <linux/dma-buf-map.h>
31 #include <linux/dma-fence.h>
32 #include <drm/drm_print.h>
33 #include <drm/ttm/ttm_caching.h>
34 #include <drm/ttm/ttm_kmap_iter.h>
36 #define TTM_MAX_BO_PRIORITY 4U
39 struct ttm_resource_manager;
42 struct ttm_buffer_object;
49 struct ttm_resource_manager_func {
51 * struct ttm_resource_manager_func member alloc
53 * @man: Pointer to a memory type manager.
54 * @bo: Pointer to the buffer object we're allocating space for.
55 * @place: Placement details.
56 * @res: Resulting pointer to the ttm_resource.
58 * This function should allocate space in the memory type managed
59 * by @man. Placement details if applicable are given by @place. If
60 * successful, a filled in ttm_resource object should be returned in
61 * @res. @res::start should be set to a value identifying the beginning
62 * of the range allocated, and the function should return zero.
63 * If the manager can't fulfill the request -ENOSPC should be returned.
64 * If a system error occurred, preventing the request to be fulfilled,
65 * the function should return a negative error code.
67 * This function may not be called from within atomic context and needs
68 * to take care of its own locking to protect any data structures
71 int (*alloc)(struct ttm_resource_manager *man,
72 struct ttm_buffer_object *bo,
73 const struct ttm_place *place,
74 struct ttm_resource **res);
77 * struct ttm_resource_manager_func member free
79 * @man: Pointer to a memory type manager.
80 * @res: Pointer to a struct ttm_resource to be freed.
82 * This function frees memory type resources previously allocated.
83 * May not be called from within atomic context.
85 void (*free)(struct ttm_resource_manager *man,
86 struct ttm_resource *res);
89 * struct ttm_resource_manager_func member debug
91 * @man: Pointer to a memory type manager.
92 * @printer: Prefix to be used in printout to identify the caller.
94 * This function is called to print out the state of the memory
95 * type manager to aid debugging of out-of-memory conditions.
96 * It may not be called from within atomic context.
98 void (*debug)(struct ttm_resource_manager *man,
99 struct drm_printer *printer);
103 * struct ttm_resource_manager
105 * @use_type: The memory type is enabled.
106 * @use_tt: If a TT object should be used for the backing store.
107 * @size: Size of the managed region.
108 * @func: structure pointer implementing the range manager. See above
109 * @move_lock: lock for move fence
110 * static information. bdev::driver::io_mem_free is never used.
111 * @lru: The lru list for this memory type.
112 * @move: The fence of the last pipelined move operation.
114 * This structure is used to identify and manage memory types for a device.
116 struct ttm_resource_manager {
118 * No protection. Constant from start.
123 const struct ttm_resource_manager_func *func;
124 spinlock_t move_lock;
127 * Protected by the global->lru_lock.
130 struct list_head lru[TTM_MAX_BO_PRIORITY];
133 * Protected by @move_lock.
135 struct dma_fence *move;
139 * struct ttm_bus_placement
141 * @addr: mapped virtual address
142 * @offset: physical addr
143 * @is_iomem: is this io memory ?
144 * @caching: See enum ttm_caching
146 * Structure indicating the bus placement of an object.
148 struct ttm_bus_placement {
152 enum ttm_caching caching;
156 * struct ttm_resource
158 * @start: Start of the allocation.
159 * @num_pages: Actual size of resource in pages.
160 * @mem_type: Resource type of the allocation.
161 * @placement: Placement flags.
162 * @bus: Placement on io bus accessible to the CPU
164 * Structure indicating the placement and space resources used by a
167 struct ttm_resource {
169 unsigned long num_pages;
172 struct ttm_bus_placement bus;
176 * struct ttm_kmap_iter_iomap - Specialization for a struct io_mapping +
177 * struct sg_table backed struct ttm_resource.
178 * @base: Embedded struct ttm_kmap_iter providing the usage interface.
179 * @iomap: struct io_mapping representing the underlying linear io_memory.
180 * @st: sg_table into @iomap, representing the memory of the struct ttm_resource.
181 * @start: Offset that needs to be subtracted from @st to make
182 * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
183 * @cache: Scatterlist traversal cache for fast lookups.
184 * @cache.sg: Pointer to the currently cached scatterlist segment.
185 * @cache.i: First index of @sg. PAGE_SIZE granularity.
186 * @cache.end: Last index + 1 of @sg. PAGE_SIZE granularity.
187 * @cache.offs: First offset into @iomap of @sg. PAGE_SIZE granularity.
189 struct ttm_kmap_iter_iomap {
190 struct ttm_kmap_iter base;
191 struct io_mapping *iomap;
193 resource_size_t start;
195 struct scatterlist *sg;
203 * struct ttm_kmap_iter_linear_io - Iterator specialization for linear io
204 * @base: The base iterator
205 * @dmap: Points to the starting address of the region
206 * @needs_unmap: Whether we need to unmap on fini
208 struct ttm_kmap_iter_linear_io {
209 struct ttm_kmap_iter base;
210 struct dma_buf_map dmap;
215 * ttm_resource_manager_set_used
217 * @man: A memory manager object.
218 * @used: usage state to set.
220 * Set the manager in use flag. If disabled the manager is no longer
221 * used for object placement.
224 ttm_resource_manager_set_used(struct ttm_resource_manager *man, bool used)
228 for (i = 0; i < TTM_MAX_BO_PRIORITY; i++)
229 WARN_ON(!list_empty(&man->lru[i]));
230 man->use_type = used;
234 * ttm_resource_manager_used
236 * @man: Manager to get used state for
238 * Get the in use flag for a manager.
240 * true is used, false if not.
242 static inline bool ttm_resource_manager_used(struct ttm_resource_manager *man)
244 return man->use_type;
248 * ttm_resource_manager_cleanup
250 * @man: A memory manager object.
252 * Cleanup the move fences from the memory manager object.
255 ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
257 dma_fence_put(man->move);
261 void ttm_resource_init(struct ttm_buffer_object *bo,
262 const struct ttm_place *place,
263 struct ttm_resource *res);
264 int ttm_resource_alloc(struct ttm_buffer_object *bo,
265 const struct ttm_place *place,
266 struct ttm_resource **res);
267 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res);
268 bool ttm_resource_compat(struct ttm_resource *res,
269 struct ttm_placement *placement);
271 void ttm_resource_manager_init(struct ttm_resource_manager *man,
272 unsigned long p_size);
274 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
275 struct ttm_resource_manager *man);
277 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
278 struct drm_printer *p);
280 struct ttm_kmap_iter *
281 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
282 struct io_mapping *iomap,
284 resource_size_t start);
286 struct ttm_kmap_iter_linear_io;
288 struct ttm_kmap_iter *
289 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
290 struct ttm_device *bdev,
291 struct ttm_resource *mem);
293 void ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
294 struct ttm_device *bdev,
295 struct ttm_resource *mem);