xref: /openbmc/linux/include/drm/ttm/ttm_resource.h (revision 12cecbf9)
1 /*
2  * Copyright 2020 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors: Christian König
23  */
24 
25 #ifndef _TTM_RESOURCE_H_
26 #define _TTM_RESOURCE_H_
27 
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>
33 
34 #include <drm/drm_print.h>
35 #include <drm/ttm/ttm_caching.h>
36 #include <drm/ttm/ttm_kmap_iter.h>
37 
38 #define TTM_MAX_BO_PRIORITY	4U
39 #define TTM_NUM_MEM_TYPES 8
40 
41 struct ttm_device;
42 struct ttm_resource_manager;
43 struct ttm_resource;
44 struct ttm_place;
45 struct ttm_buffer_object;
46 struct ttm_placement;
47 struct iosys_map;
48 struct io_mapping;
49 struct sg_table;
50 struct scatterlist;
51 
52 struct ttm_resource_manager_func {
53 	/**
54 	 * struct ttm_resource_manager_func member alloc
55 	 *
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.
60 	 *
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.
69 	 *
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
72 	 * managing the space.
73 	 */
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);
78 
79 	/**
80 	 * struct ttm_resource_manager_func member free
81 	 *
82 	 * @man: Pointer to a memory type manager.
83 	 * @res: Pointer to a struct ttm_resource to be freed.
84 	 *
85 	 * This function frees memory type resources previously allocated.
86 	 * May not be called from within atomic context.
87 	 */
88 	void (*free)(struct ttm_resource_manager *man,
89 		     struct ttm_resource *res);
90 
91 	/**
92 	 * struct ttm_resource_manager_func member debug
93 	 *
94 	 * @man: Pointer to a memory type manager.
95 	 * @printer: Prefix to be used in printout to identify the caller.
96 	 *
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.
100 	 */
101 	void (*debug)(struct ttm_resource_manager *man,
102 		      struct drm_printer *printer);
103 };
104 
105 /**
106  * struct ttm_resource_manager
107  *
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.
116  *
117  * This structure is used to identify and manage memory types for a device.
118  */
119 struct ttm_resource_manager {
120 	/*
121 	 * No protection. Constant from start.
122 	 */
123 	bool use_type;
124 	bool use_tt;
125 	struct ttm_device *bdev;
126 	uint64_t size;
127 	const struct ttm_resource_manager_func *func;
128 	spinlock_t move_lock;
129 
130 	/*
131 	 * Protected by @move_lock.
132 	 */
133 	struct dma_fence *move;
134 
135 	/*
136 	 * Protected by the bdev->lru_lock.
137 	 */
138 	struct list_head lru[TTM_MAX_BO_PRIORITY];
139 
140 	/**
141 	 * @usage: How much of the resources are used, protected by the
142 	 * bdev->lru_lock.
143 	 */
144 	uint64_t usage;
145 };
146 
147 /**
148  * struct ttm_bus_placement
149  *
150  * @addr:		mapped virtual address
151  * @offset:		physical addr
152  * @is_iomem:		is this io memory ?
153  * @caching:		See enum ttm_caching
154  *
155  * Structure indicating the bus placement of an object.
156  */
157 struct ttm_bus_placement {
158 	void			*addr;
159 	phys_addr_t		offset;
160 	bool			is_iomem;
161 	enum ttm_caching	caching;
162 };
163 
164 /**
165  * struct ttm_resource
166  *
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
173  *
174  * Structure indicating the placement and space resources used by a
175  * buffer object.
176  */
177 struct ttm_resource {
178 	unsigned long start;
179 	unsigned long num_pages;
180 	uint32_t mem_type;
181 	uint32_t placement;
182 	struct ttm_bus_placement bus;
183 	struct ttm_buffer_object *bo;
184 
185 	/**
186 	 * @lru: Least recently used list, see &ttm_resource_manager.lru
187 	 */
188 	struct list_head lru;
189 };
190 
191 /**
192  * struct ttm_resource_cursor
193  *
194  * @priority: the current priority
195  *
196  * Cursor to iterate over the resources in a manager.
197  */
198 struct ttm_resource_cursor {
199 	unsigned int priority;
200 };
201 
202 /**
203  * struct ttm_lru_bulk_move_pos
204  *
205  * @first: first res in the bulk move range
206  * @last: last res in the bulk move range
207  *
208  * Range of resources for a lru bulk move.
209  */
210 struct ttm_lru_bulk_move_pos {
211 	struct ttm_resource *first;
212 	struct ttm_resource *last;
213 };
214 
215 /**
216  * struct ttm_lru_bulk_move
217  *
218  * @pos: first/last lru entry for resources in the each domain/priority
219  *
220  * Container for the current bulk move state. Should be used with
221  * ttm_lru_bulk_move_init() and ttm_bo_set_bulk_move().
222  */
223 struct ttm_lru_bulk_move {
224 	struct ttm_lru_bulk_move_pos pos[TTM_NUM_MEM_TYPES][TTM_MAX_BO_PRIORITY];
225 };
226 
227 /**
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.
240  */
241 struct ttm_kmap_iter_iomap {
242 	struct ttm_kmap_iter base;
243 	struct io_mapping *iomap;
244 	struct sg_table *st;
245 	resource_size_t start;
246 	struct {
247 		struct scatterlist *sg;
248 		pgoff_t i;
249 		pgoff_t end;
250 		pgoff_t offs;
251 	} cache;
252 };
253 
254 /**
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
259  */
260 struct ttm_kmap_iter_linear_io {
261 	struct ttm_kmap_iter base;
262 	struct iosys_map dmap;
263 	bool needs_unmap;
264 };
265 
266 /**
267  * ttm_resource_manager_set_used
268  *
269  * @man: A memory manager object.
270  * @used: usage state to set.
271  *
272  * Set the manager in use flag. If disabled the manager is no longer
273  * used for object placement.
274  */
275 static inline void
276 ttm_resource_manager_set_used(struct ttm_resource_manager *man, bool used)
277 {
278 	int i;
279 
280 	for (i = 0; i < TTM_MAX_BO_PRIORITY; i++)
281 		WARN_ON(!list_empty(&man->lru[i]));
282 	man->use_type = used;
283 }
284 
285 /**
286  * ttm_resource_manager_used
287  *
288  * @man: Manager to get used state for
289  *
290  * Get the in use flag for a manager.
291  * Returns:
292  * true is used, false if not.
293  */
294 static inline bool ttm_resource_manager_used(struct ttm_resource_manager *man)
295 {
296 	return man->use_type;
297 }
298 
299 /**
300  * ttm_resource_manager_cleanup
301  *
302  * @man: A memory manager object.
303  *
304  * Cleanup the move fences from the memory manager object.
305  */
306 static inline void
307 ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
308 {
309 	dma_fence_put(man->move);
310 	man->move = NULL;
311 }
312 
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);
315 
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);
321 
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);
327 
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);
336 
337 void ttm_resource_manager_init(struct ttm_resource_manager *man,
338 			       struct ttm_device *bdev,
339 			       uint64_t size);
340 
341 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
342 				   struct ttm_resource_manager *man);
343 
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);
347 
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);
355 
356 /**
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
361  *
362  * Iterate over all the evictable resources in a resource manager.
363  */
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))
367 
368 struct ttm_kmap_iter *
369 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
370 			 struct io_mapping *iomap,
371 			 struct sg_table *st,
372 			 resource_size_t start);
373 
374 struct ttm_kmap_iter_linear_io;
375 
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);
380 
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);
384 
385 void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
386 					 struct dentry * parent,
387 					 const char *name);
388 #endif
389