xref: /openbmc/linux/include/drm/ttm/ttm_resource.h (revision c4a11bf4)
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/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>
35 
36 #define TTM_MAX_BO_PRIORITY	4U
37 
38 struct ttm_device;
39 struct ttm_resource_manager;
40 struct ttm_resource;
41 struct ttm_place;
42 struct ttm_buffer_object;
43 struct ttm_placement;
44 struct dma_buf_map;
45 struct io_mapping;
46 struct sg_table;
47 struct scatterlist;
48 
49 struct ttm_resource_manager_func {
50 	/**
51 	 * struct ttm_resource_manager_func member alloc
52 	 *
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.
57 	 *
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.
66 	 *
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
69 	 * managing the space.
70 	 */
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);
75 
76 	/**
77 	 * struct ttm_resource_manager_func member free
78 	 *
79 	 * @man: Pointer to a memory type manager.
80 	 * @res: Pointer to a struct ttm_resource to be freed.
81 	 *
82 	 * This function frees memory type resources previously allocated.
83 	 * May not be called from within atomic context.
84 	 */
85 	void (*free)(struct ttm_resource_manager *man,
86 		     struct ttm_resource *res);
87 
88 	/**
89 	 * struct ttm_resource_manager_func member debug
90 	 *
91 	 * @man: Pointer to a memory type manager.
92 	 * @printer: Prefix to be used in printout to identify the caller.
93 	 *
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.
97 	 */
98 	void (*debug)(struct ttm_resource_manager *man,
99 		      struct drm_printer *printer);
100 };
101 
102 /**
103  * struct ttm_resource_manager
104  *
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.
113  *
114  * This structure is used to identify and manage memory types for a device.
115  */
116 struct ttm_resource_manager {
117 	/*
118 	 * No protection. Constant from start.
119 	 */
120 	bool use_type;
121 	bool use_tt;
122 	uint64_t size;
123 	const struct ttm_resource_manager_func *func;
124 	spinlock_t move_lock;
125 
126 	/*
127 	 * Protected by the global->lru_lock.
128 	 */
129 
130 	struct list_head lru[TTM_MAX_BO_PRIORITY];
131 
132 	/*
133 	 * Protected by @move_lock.
134 	 */
135 	struct dma_fence *move;
136 };
137 
138 /**
139  * struct ttm_bus_placement
140  *
141  * @addr:		mapped virtual address
142  * @offset:		physical addr
143  * @is_iomem:		is this io memory ?
144  * @caching:		See enum ttm_caching
145  *
146  * Structure indicating the bus placement of an object.
147  */
148 struct ttm_bus_placement {
149 	void			*addr;
150 	phys_addr_t		offset;
151 	bool			is_iomem;
152 	enum ttm_caching	caching;
153 };
154 
155 /**
156  * struct ttm_resource
157  *
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
163  *
164  * Structure indicating the placement and space resources used by a
165  * buffer object.
166  */
167 struct ttm_resource {
168 	unsigned long start;
169 	unsigned long num_pages;
170 	uint32_t mem_type;
171 	uint32_t placement;
172 	struct ttm_bus_placement bus;
173 };
174 
175 /**
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.
188  */
189 struct ttm_kmap_iter_iomap {
190 	struct ttm_kmap_iter base;
191 	struct io_mapping *iomap;
192 	struct sg_table *st;
193 	resource_size_t start;
194 	struct {
195 		struct scatterlist *sg;
196 		pgoff_t i;
197 		pgoff_t end;
198 		pgoff_t offs;
199 	} cache;
200 };
201 
202 /**
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
207  */
208 struct ttm_kmap_iter_linear_io {
209 	struct ttm_kmap_iter base;
210 	struct dma_buf_map dmap;
211 	bool needs_unmap;
212 };
213 
214 /**
215  * ttm_resource_manager_set_used
216  *
217  * @man: A memory manager object.
218  * @used: usage state to set.
219  *
220  * Set the manager in use flag. If disabled the manager is no longer
221  * used for object placement.
222  */
223 static inline void
224 ttm_resource_manager_set_used(struct ttm_resource_manager *man, bool used)
225 {
226 	int i;
227 
228 	for (i = 0; i < TTM_MAX_BO_PRIORITY; i++)
229 		WARN_ON(!list_empty(&man->lru[i]));
230 	man->use_type = used;
231 }
232 
233 /**
234  * ttm_resource_manager_used
235  *
236  * @man: Manager to get used state for
237  *
238  * Get the in use flag for a manager.
239  * Returns:
240  * true is used, false if not.
241  */
242 static inline bool ttm_resource_manager_used(struct ttm_resource_manager *man)
243 {
244 	return man->use_type;
245 }
246 
247 /**
248  * ttm_resource_manager_cleanup
249  *
250  * @man: A memory manager object.
251  *
252  * Cleanup the move fences from the memory manager object.
253  */
254 static inline void
255 ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
256 {
257 	dma_fence_put(man->move);
258 	man->move = NULL;
259 }
260 
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);
270 
271 void ttm_resource_manager_init(struct ttm_resource_manager *man,
272 			       unsigned long p_size);
273 
274 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
275 				   struct ttm_resource_manager *man);
276 
277 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
278 				struct drm_printer *p);
279 
280 struct ttm_kmap_iter *
281 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
282 			 struct io_mapping *iomap,
283 			 struct sg_table *st,
284 			 resource_size_t start);
285 
286 struct ttm_kmap_iter_linear_io;
287 
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);
292 
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);
296 #endif
297