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/iosys-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 * @bdev: ttm device this manager belongs to
109 * @func: structure pointer implementing the range manager. See above
110 * @move_lock: lock for move fence
111 * @move: The fence of the last pipelined move operation.
112 * @lru: The lru list for this memory type.
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.
122 struct ttm_device *bdev;
124 const struct ttm_resource_manager_func *func;
125 spinlock_t move_lock;
128 * Protected by @move_lock.
130 struct dma_fence *move;
133 * Protected by the bdev->lru_lock.
135 struct list_head lru[TTM_MAX_BO_PRIORITY];
138 * @usage: How much of the resources are used, protected by the
145 * struct ttm_bus_placement
147 * @addr: mapped virtual address
148 * @offset: physical addr
149 * @is_iomem: is this io memory ?
150 * @caching: See enum ttm_caching
152 * Structure indicating the bus placement of an object.
154 struct ttm_bus_placement {
158 enum ttm_caching caching;
162 * struct ttm_resource
164 * @start: Start of the allocation.
165 * @num_pages: Actual size of resource in pages.
166 * @mem_type: Resource type of the allocation.
167 * @placement: Placement flags.
168 * @bus: Placement on io bus accessible to the CPU
169 * @bo: weak reference to the BO, protected by ttm_device::lru_lock
171 * Structure indicating the placement and space resources used by a
174 struct ttm_resource {
176 unsigned long num_pages;
179 struct ttm_bus_placement bus;
180 struct ttm_buffer_object *bo;
184 * struct ttm_kmap_iter_iomap - Specialization for a struct io_mapping +
185 * struct sg_table backed struct ttm_resource.
186 * @base: Embedded struct ttm_kmap_iter providing the usage interface.
187 * @iomap: struct io_mapping representing the underlying linear io_memory.
188 * @st: sg_table into @iomap, representing the memory of the struct ttm_resource.
189 * @start: Offset that needs to be subtracted from @st to make
190 * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
191 * @cache: Scatterlist traversal cache for fast lookups.
192 * @cache.sg: Pointer to the currently cached scatterlist segment.
193 * @cache.i: First index of @sg. PAGE_SIZE granularity.
194 * @cache.end: Last index + 1 of @sg. PAGE_SIZE granularity.
195 * @cache.offs: First offset into @iomap of @sg. PAGE_SIZE granularity.
197 struct ttm_kmap_iter_iomap {
198 struct ttm_kmap_iter base;
199 struct io_mapping *iomap;
201 resource_size_t start;
203 struct scatterlist *sg;
211 * struct ttm_kmap_iter_linear_io - Iterator specialization for linear io
212 * @base: The base iterator
213 * @dmap: Points to the starting address of the region
214 * @needs_unmap: Whether we need to unmap on fini
216 struct ttm_kmap_iter_linear_io {
217 struct ttm_kmap_iter base;
218 struct iosys_map dmap;
223 * ttm_resource_manager_set_used
225 * @man: A memory manager object.
226 * @used: usage state to set.
228 * Set the manager in use flag. If disabled the manager is no longer
229 * used for object placement.
232 ttm_resource_manager_set_used(struct ttm_resource_manager *man, bool used)
236 for (i = 0; i < TTM_MAX_BO_PRIORITY; i++)
237 WARN_ON(!list_empty(&man->lru[i]));
238 man->use_type = used;
242 * ttm_resource_manager_used
244 * @man: Manager to get used state for
246 * Get the in use flag for a manager.
248 * true is used, false if not.
250 static inline bool ttm_resource_manager_used(struct ttm_resource_manager *man)
252 return man->use_type;
256 * ttm_resource_manager_cleanup
258 * @man: A memory manager object.
260 * Cleanup the move fences from the memory manager object.
263 ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
265 dma_fence_put(man->move);
269 void ttm_resource_init(struct ttm_buffer_object *bo,
270 const struct ttm_place *place,
271 struct ttm_resource *res);
272 void ttm_resource_fini(struct ttm_resource_manager *man,
273 struct ttm_resource *res);
275 int ttm_resource_alloc(struct ttm_buffer_object *bo,
276 const struct ttm_place *place,
277 struct ttm_resource **res);
278 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res);
279 bool ttm_resource_compat(struct ttm_resource *res,
280 struct ttm_placement *placement);
281 void ttm_resource_set_bo(struct ttm_resource *res,
282 struct ttm_buffer_object *bo);
284 void ttm_resource_manager_init(struct ttm_resource_manager *man,
285 struct ttm_device *bdev,
288 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
289 struct ttm_resource_manager *man);
291 uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man);
292 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
293 struct drm_printer *p);
295 struct ttm_kmap_iter *
296 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
297 struct io_mapping *iomap,
299 resource_size_t start);
301 struct ttm_kmap_iter_linear_io;
303 struct ttm_kmap_iter *
304 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
305 struct ttm_device *bdev,
306 struct ttm_resource *mem);
308 void ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
309 struct ttm_device *bdev,
310 struct ttm_resource *mem);