xref: /openbmc/linux/include/drm/ttm/ttm_device.h (revision 7f9321ff)
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_DEVICE_H_
26 #define _TTM_DEVICE_H_
27 
28 #include <linux/types.h>
29 #include <linux/workqueue.h>
30 #include <drm/ttm/ttm_resource.h>
31 #include <drm/ttm/ttm_pool.h>
32 
33 #define TTM_NUM_MEM_TYPES 8
34 
35 struct ttm_device;
36 struct ttm_placement;
37 struct ttm_buffer_object;
38 struct ttm_operation_ctx;
39 
40 /**
41  * struct ttm_global - Buffer object driver global data.
42  *
43  * @dummy_read_page: Pointer to a dummy page used for mapping requests
44  * of unpopulated pages.
45  * @shrink: A shrink callback object used for buffer object swap.
46  * @device_list_mutex: Mutex protecting the device list.
47  * This mutex is held while traversing the device list for pm options.
48  * @lru_lock: Spinlock protecting the bo subsystem lru lists.
49  * @device_list: List of buffer object devices.
50  * @swap_lru: Lru list of buffer objects used for swapping.
51  */
52 extern struct ttm_global {
53 
54 	/**
55 	 * Constant after init.
56 	 */
57 
58 	struct page *dummy_read_page;
59 	spinlock_t lru_lock;
60 
61 	/**
62 	 * Protected by ttm_global_mutex.
63 	 */
64 	struct list_head device_list;
65 
66 	/**
67 	 * Protected by the lru_lock.
68 	 */
69 	struct list_head swap_lru[TTM_MAX_BO_PRIORITY];
70 
71 	/**
72 	 * Internal protection.
73 	 */
74 	atomic_t bo_count;
75 } ttm_glob;
76 
77 struct ttm_device_funcs {
78 	/**
79 	 * ttm_tt_create
80 	 *
81 	 * @bo: The buffer object to create the ttm for.
82 	 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
83 	 *
84 	 * Create a struct ttm_tt to back data with system memory pages.
85 	 * No pages are actually allocated.
86 	 * Returns:
87 	 * NULL: Out of memory.
88 	 */
89 	struct ttm_tt *(*ttm_tt_create)(struct ttm_buffer_object *bo,
90 					uint32_t page_flags);
91 
92 	/**
93 	 * ttm_tt_populate
94 	 *
95 	 * @ttm: The struct ttm_tt to contain the backing pages.
96 	 *
97 	 * Allocate all backing pages
98 	 * Returns:
99 	 * -ENOMEM: Out of memory.
100 	 */
101 	int (*ttm_tt_populate)(struct ttm_device *bdev,
102 			       struct ttm_tt *ttm,
103 			       struct ttm_operation_ctx *ctx);
104 
105 	/**
106 	 * ttm_tt_unpopulate
107 	 *
108 	 * @ttm: The struct ttm_tt to contain the backing pages.
109 	 *
110 	 * Free all backing page
111 	 */
112 	void (*ttm_tt_unpopulate)(struct ttm_device *bdev,
113 				  struct ttm_tt *ttm);
114 
115 	/**
116 	 * ttm_tt_destroy
117 	 *
118 	 * @bdev: Pointer to a ttm device
119 	 * @ttm: Pointer to a struct ttm_tt.
120 	 *
121 	 * Destroy the backend. This will be call back from ttm_tt_destroy so
122 	 * don't call ttm_tt_destroy from the callback or infinite loop.
123 	 */
124 	void (*ttm_tt_destroy)(struct ttm_device *bdev, struct ttm_tt *ttm);
125 
126 	/**
127 	 * struct ttm_bo_driver member eviction_valuable
128 	 *
129 	 * @bo: the buffer object to be evicted
130 	 * @place: placement we need room for
131 	 *
132 	 * Check with the driver if it is valuable to evict a BO to make room
133 	 * for a certain placement.
134 	 */
135 	bool (*eviction_valuable)(struct ttm_buffer_object *bo,
136 				  const struct ttm_place *place);
137 	/**
138 	 * struct ttm_bo_driver member evict_flags:
139 	 *
140 	 * @bo: the buffer object to be evicted
141 	 *
142 	 * Return the bo flags for a buffer which is not mapped to the hardware.
143 	 * These will be placed in proposed_flags so that when the move is
144 	 * finished, they'll end up in bo->mem.flags
145 	 * This should not cause multihop evictions, and the core will warn
146 	 * if one is proposed.
147 	 */
148 
149 	void (*evict_flags)(struct ttm_buffer_object *bo,
150 			    struct ttm_placement *placement);
151 
152 	/**
153 	 * struct ttm_bo_driver member move:
154 	 *
155 	 * @bo: the buffer to move
156 	 * @evict: whether this motion is evicting the buffer from
157 	 * the graphics address space
158 	 * @ctx: context for this move with parameters
159 	 * @new_mem: the new memory region receiving the buffer
160 	 @ @hop: placement for driver directed intermediate hop
161 	 *
162 	 * Move a buffer between two memory regions.
163 	 * Returns errno -EMULTIHOP if driver requests a hop
164 	 */
165 	int (*move)(struct ttm_buffer_object *bo, bool evict,
166 		    struct ttm_operation_ctx *ctx,
167 		    struct ttm_resource *new_mem,
168 		    struct ttm_place *hop);
169 
170 	/**
171 	 * struct ttm_bo_driver_member verify_access
172 	 *
173 	 * @bo: Pointer to a buffer object.
174 	 * @filp: Pointer to a struct file trying to access the object.
175 	 *
176 	 * Called from the map / write / read methods to verify that the
177 	 * caller is permitted to access the buffer object.
178 	 * This member may be set to NULL, which will refuse this kind of
179 	 * access for all buffer objects.
180 	 * This function should return 0 if access is granted, -EPERM otherwise.
181 	 */
182 	int (*verify_access)(struct ttm_buffer_object *bo,
183 			     struct file *filp);
184 
185 	/**
186 	 * Hook to notify driver about a resource delete.
187 	 */
188 	void (*delete_mem_notify)(struct ttm_buffer_object *bo);
189 
190 	/**
191 	 * notify the driver that we're about to swap out this bo
192 	 */
193 	void (*swap_notify)(struct ttm_buffer_object *bo);
194 
195 	/**
196 	 * Driver callback on when mapping io memory (for bo_move_memcpy
197 	 * for instance). TTM will take care to call io_mem_free whenever
198 	 * the mapping is not use anymore. io_mem_reserve & io_mem_free
199 	 * are balanced.
200 	 */
201 	int (*io_mem_reserve)(struct ttm_device *bdev,
202 			      struct ttm_resource *mem);
203 	void (*io_mem_free)(struct ttm_device *bdev,
204 			    struct ttm_resource *mem);
205 
206 	/**
207 	 * Return the pfn for a given page_offset inside the BO.
208 	 *
209 	 * @bo: the BO to look up the pfn for
210 	 * @page_offset: the offset to look up
211 	 */
212 	unsigned long (*io_mem_pfn)(struct ttm_buffer_object *bo,
213 				    unsigned long page_offset);
214 
215 	/**
216 	 * Read/write memory buffers for ptrace access
217 	 *
218 	 * @bo: the BO to access
219 	 * @offset: the offset from the start of the BO
220 	 * @buf: pointer to source/destination buffer
221 	 * @len: number of bytes to copy
222 	 * @write: whether to read (0) from or write (non-0) to BO
223 	 *
224 	 * If successful, this function should return the number of
225 	 * bytes copied, -EIO otherwise. If the number of bytes
226 	 * returned is < len, the function may be called again with
227 	 * the remainder of the buffer to copy.
228 	 */
229 	int (*access_memory)(struct ttm_buffer_object *bo, unsigned long offset,
230 			     void *buf, int len, int write);
231 
232 	/**
233 	 * struct ttm_bo_driver member del_from_lru_notify
234 	 *
235 	 * @bo: the buffer object deleted from lru
236 	 *
237 	 * notify driver that a BO was deleted from LRU.
238 	 */
239 	void (*del_from_lru_notify)(struct ttm_buffer_object *bo);
240 
241 	/**
242 	 * Notify the driver that we're about to release a BO
243 	 *
244 	 * @bo: BO that is about to be released
245 	 *
246 	 * Gives the driver a chance to do any cleanup, including
247 	 * adding fences that may force a delayed delete
248 	 */
249 	void (*release_notify)(struct ttm_buffer_object *bo);
250 };
251 
252 /**
253  * struct ttm_device - Buffer object driver device-specific data.
254  *
255  * @device_list: Our entry in the global device list.
256  * @funcs: Function table for the device.
257  * @sysman: Resource manager for the system domain.
258  * @man_drv: An array of resource_managers.
259  * @vma_manager: Address space manager.
260  * @pool: page pool for the device.
261  * @dev_mapping: A pointer to the struct address_space representing the
262  * device address space.
263  * @wq: Work queue structure for the delayed delete workqueue.
264  */
265 struct ttm_device {
266 	/*
267 	 * Constant after bo device init
268 	 */
269 	struct list_head device_list;
270 	struct ttm_device_funcs *funcs;
271 
272 	/*
273 	 * Access via ttm_manager_type.
274 	 */
275 	struct ttm_resource_manager sysman;
276 	struct ttm_resource_manager *man_drv[TTM_NUM_MEM_TYPES];
277 
278 	/*
279 	 * Protected by internal locks.
280 	 */
281 	struct drm_vma_offset_manager *vma_manager;
282 	struct ttm_pool pool;
283 
284 	/*
285 	 * Protected by the global:lru lock.
286 	 */
287 	struct list_head ddestroy;
288 
289 	/*
290 	 * Protected by load / firstopen / lastclose /unload sync.
291 	 */
292 	struct address_space *dev_mapping;
293 
294 	/*
295 	 * Internal protection.
296 	 */
297 	struct delayed_work wq;
298 };
299 
300 static inline struct ttm_resource_manager *
301 ttm_manager_type(struct ttm_device *bdev, int mem_type)
302 {
303 	return bdev->man_drv[mem_type];
304 }
305 
306 static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
307 					  struct ttm_resource_manager *manager)
308 {
309 	bdev->man_drv[type] = manager;
310 }
311 
312 int ttm_device_init(struct ttm_device *bdev, struct ttm_device_funcs *funcs,
313 		    struct device *dev, struct address_space *mapping,
314 		    struct drm_vma_offset_manager *vma_manager,
315 		    bool use_dma_alloc, bool use_dma32);
316 void ttm_device_fini(struct ttm_device *bdev);
317 
318 #endif
319