xref: /openbmc/linux/include/drm/ttm/ttm_resource.h (revision cc2f2df4)
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/mutex.h>
30 #include <linux/atomic.h>
31 #include <linux/dma-buf-map.h>
32 #include <linux/dma-fence.h>
33 #include <drm/drm_print.h>
34 #include <drm/ttm/ttm_caching.h>
35 #include <drm/ttm/ttm_kmap_iter.h>
36 
37 #define TTM_MAX_BO_PRIORITY	4U
38 
39 struct ttm_device;
40 struct ttm_resource_manager;
41 struct ttm_resource;
42 struct ttm_place;
43 struct ttm_buffer_object;
44 struct ttm_placement;
45 struct dma_buf_map;
46 struct io_mapping;
47 struct sg_table;
48 struct scatterlist;
49 
50 struct ttm_resource_manager_func {
51 	/**
52 	 * struct ttm_resource_manager_func member alloc
53 	 *
54 	 * @man: Pointer to a memory type manager.
55 	 * @bo: Pointer to the buffer object we're allocating space for.
56 	 * @place: Placement details.
57 	 * @res: Resulting pointer to the ttm_resource.
58 	 *
59 	 * This function should allocate space in the memory type managed
60 	 * by @man. Placement details if applicable are given by @place. If
61 	 * successful, a filled in ttm_resource object should be returned in
62 	 * @res. @res::start should be set to a value identifying the beginning
63 	 * of the range allocated, and the function should return zero.
64 	 * If the manager can't fulfill the request -ENOSPC should be returned.
65 	 * If a system error occurred, preventing the request to be fulfilled,
66 	 * the function should return a negative error code.
67 	 *
68 	 * This function may not be called from within atomic context and needs
69 	 * to take care of its own locking to protect any data structures
70 	 * managing the space.
71 	 */
72 	int  (*alloc)(struct ttm_resource_manager *man,
73 		      struct ttm_buffer_object *bo,
74 		      const struct ttm_place *place,
75 		      struct ttm_resource **res);
76 
77 	/**
78 	 * struct ttm_resource_manager_func member free
79 	 *
80 	 * @man: Pointer to a memory type manager.
81 	 * @res: Pointer to a struct ttm_resource to be freed.
82 	 *
83 	 * This function frees memory type resources previously allocated.
84 	 * May not be called from within atomic context.
85 	 */
86 	void (*free)(struct ttm_resource_manager *man,
87 		     struct ttm_resource *res);
88 
89 	/**
90 	 * struct ttm_resource_manager_func member debug
91 	 *
92 	 * @man: Pointer to a memory type manager.
93 	 * @printer: Prefix to be used in printout to identify the caller.
94 	 *
95 	 * This function is called to print out the state of the memory
96 	 * type manager to aid debugging of out-of-memory conditions.
97 	 * It may not be called from within atomic context.
98 	 */
99 	void (*debug)(struct ttm_resource_manager *man,
100 		      struct drm_printer *printer);
101 };
102 
103 /**
104  * struct ttm_resource_manager
105  *
106  * @use_type: The memory type is enabled.
107  * @use_tt: If a TT object should be used for the backing store.
108  * @size: Size of the managed region.
109  * @bdev: ttm device this manager belongs to
110  * @func: structure pointer implementing the range manager. See above
111  * @move_lock: lock for move fence
112  * @move: The fence of the last pipelined move operation.
113  * @lru: The lru list for this memory type.
114  *
115  * This structure is used to identify and manage memory types for a device.
116  */
117 struct ttm_resource_manager {
118 	/*
119 	 * No protection. Constant from start.
120 	 */
121 	bool use_type;
122 	bool use_tt;
123 	struct ttm_device *bdev;
124 	uint64_t size;
125 	const struct ttm_resource_manager_func *func;
126 	spinlock_t move_lock;
127 
128 	/*
129 	 * Protected by @move_lock.
130 	 */
131 	struct dma_fence *move;
132 
133 	/*
134 	 * Protected by the bdev->lru_lock.
135 	 */
136 	struct list_head lru[TTM_MAX_BO_PRIORITY];
137 
138 	/**
139 	 * @usage: How much of the resources are used, protected by the
140 	 * bdev->lru_lock.
141 	 */
142 	uint64_t usage;
143 };
144 
145 /**
146  * struct ttm_bus_placement
147  *
148  * @addr:		mapped virtual address
149  * @offset:		physical addr
150  * @is_iomem:		is this io memory ?
151  * @caching:		See enum ttm_caching
152  *
153  * Structure indicating the bus placement of an object.
154  */
155 struct ttm_bus_placement {
156 	void			*addr;
157 	phys_addr_t		offset;
158 	bool			is_iomem;
159 	enum ttm_caching	caching;
160 };
161 
162 /**
163  * struct ttm_resource
164  *
165  * @start: Start of the allocation.
166  * @num_pages: Actual size of resource in pages.
167  * @mem_type: Resource type of the allocation.
168  * @placement: Placement flags.
169  * @bus: Placement on io bus accessible to the CPU
170  * @bo: weak reference to the BO, protected by ttm_device::lru_lock
171  *
172  * Structure indicating the placement and space resources used by a
173  * buffer object.
174  */
175 struct ttm_resource {
176 	unsigned long start;
177 	unsigned long num_pages;
178 	uint32_t mem_type;
179 	uint32_t placement;
180 	struct ttm_bus_placement bus;
181 	struct ttm_buffer_object *bo;
182 };
183 
184 /**
185  * struct ttm_kmap_iter_iomap - Specialization for a struct io_mapping +
186  * struct sg_table backed struct ttm_resource.
187  * @base: Embedded struct ttm_kmap_iter providing the usage interface.
188  * @iomap: struct io_mapping representing the underlying linear io_memory.
189  * @st: sg_table into @iomap, representing the memory of the struct ttm_resource.
190  * @start: Offset that needs to be subtracted from @st to make
191  * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
192  * @cache: Scatterlist traversal cache for fast lookups.
193  * @cache.sg: Pointer to the currently cached scatterlist segment.
194  * @cache.i: First index of @sg. PAGE_SIZE granularity.
195  * @cache.end: Last index + 1 of @sg. PAGE_SIZE granularity.
196  * @cache.offs: First offset into @iomap of @sg. PAGE_SIZE granularity.
197  */
198 struct ttm_kmap_iter_iomap {
199 	struct ttm_kmap_iter base;
200 	struct io_mapping *iomap;
201 	struct sg_table *st;
202 	resource_size_t start;
203 	struct {
204 		struct scatterlist *sg;
205 		pgoff_t i;
206 		pgoff_t end;
207 		pgoff_t offs;
208 	} cache;
209 };
210 
211 /**
212  * struct ttm_kmap_iter_linear_io - Iterator specialization for linear io
213  * @base: The base iterator
214  * @dmap: Points to the starting address of the region
215  * @needs_unmap: Whether we need to unmap on fini
216  */
217 struct ttm_kmap_iter_linear_io {
218 	struct ttm_kmap_iter base;
219 	struct dma_buf_map dmap;
220 	bool needs_unmap;
221 };
222 
223 /**
224  * ttm_resource_manager_set_used
225  *
226  * @man: A memory manager object.
227  * @used: usage state to set.
228  *
229  * Set the manager in use flag. If disabled the manager is no longer
230  * used for object placement.
231  */
232 static inline void
233 ttm_resource_manager_set_used(struct ttm_resource_manager *man, bool used)
234 {
235 	int i;
236 
237 	for (i = 0; i < TTM_MAX_BO_PRIORITY; i++)
238 		WARN_ON(!list_empty(&man->lru[i]));
239 	man->use_type = used;
240 }
241 
242 /**
243  * ttm_resource_manager_used
244  *
245  * @man: Manager to get used state for
246  *
247  * Get the in use flag for a manager.
248  * Returns:
249  * true is used, false if not.
250  */
251 static inline bool ttm_resource_manager_used(struct ttm_resource_manager *man)
252 {
253 	return man->use_type;
254 }
255 
256 /**
257  * ttm_resource_manager_cleanup
258  *
259  * @man: A memory manager object.
260  *
261  * Cleanup the move fences from the memory manager object.
262  */
263 static inline void
264 ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
265 {
266 	dma_fence_put(man->move);
267 	man->move = NULL;
268 }
269 
270 void ttm_resource_init(struct ttm_buffer_object *bo,
271                        const struct ttm_place *place,
272                        struct ttm_resource *res);
273 void ttm_resource_fini(struct ttm_resource_manager *man,
274 		       struct ttm_resource *res);
275 
276 int ttm_resource_alloc(struct ttm_buffer_object *bo,
277 		       const struct ttm_place *place,
278 		       struct ttm_resource **res);
279 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res);
280 bool ttm_resource_compat(struct ttm_resource *res,
281 			 struct ttm_placement *placement);
282 void ttm_resource_set_bo(struct ttm_resource *res,
283 			 struct ttm_buffer_object *bo);
284 
285 void ttm_resource_manager_init(struct ttm_resource_manager *man,
286 			       struct ttm_device *bdev,
287 			       uint64_t size);
288 
289 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
290 				   struct ttm_resource_manager *man);
291 
292 uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man);
293 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
294 				struct drm_printer *p);
295 
296 struct ttm_kmap_iter *
297 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
298 			 struct io_mapping *iomap,
299 			 struct sg_table *st,
300 			 resource_size_t start);
301 
302 struct ttm_kmap_iter_linear_io;
303 
304 struct ttm_kmap_iter *
305 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
306 			     struct ttm_device *bdev,
307 			     struct ttm_resource *mem);
308 
309 void ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
310 				  struct ttm_device *bdev,
311 				  struct ttm_resource *mem);
312 #endif
313