Commit | Line | Data |
---|---|---|
ba4e7d97 TH |
1 | /************************************************************************** |
2 | * | |
3 | * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA | |
4 | * All Rights Reserved. | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a | |
7 | * copy of this software and associated documentation files (the | |
8 | * "Software"), to deal in the Software without restriction, including | |
9 | * without limitation the rights to use, copy, modify, merge, publish, | |
10 | * distribute, sub license, and/or sell copies of the Software, and to | |
11 | * permit persons to whom the Software is furnished to do so, subject to | |
12 | * the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice (including the | |
15 | * next paragraph) shall be included in all copies or substantial portions | |
16 | * of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, | |
22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. | |
25 | * | |
26 | **************************************************************************/ | |
27 | /* | |
28 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> | |
29 | */ | |
30 | #ifndef _TTM_BO_DRIVER_H_ | |
31 | #define _TTM_BO_DRIVER_H_ | |
32 | ||
a1ce3928 | 33 | #include <drm/drm_mm.h> |
72525b3f | 34 | #include <drm/drm_vma_manager.h> |
a1ce3928 DH |
35 | #include <linux/workqueue.h> |
36 | #include <linux/fs.h> | |
37 | #include <linux/spinlock.h> | |
52791eee | 38 | #include <linux/dma-resv.h> |
ba4e7d97 | 39 | |
8af8a109 CK |
40 | #include <drm/ttm/ttm_device.h> |
41 | ||
2da83319 MY |
42 | #include "ttm_bo_api.h" |
43 | #include "ttm_memory.h" | |
2da83319 | 44 | #include "ttm_placement.h" |
81f5ec02 | 45 | #include "ttm_tt.h" |
ee5d2a8e | 46 | #include "ttm_pool.h" |
2da83319 | 47 | |
8c7655a0 CK |
48 | /** |
49 | * struct ttm_lru_bulk_move_pos | |
50 | * | |
51 | * @first: first BO in the bulk move range | |
52 | * @last: last BO in the bulk move range | |
53 | * | |
54 | * Positions for a lru bulk move. | |
55 | */ | |
56 | struct ttm_lru_bulk_move_pos { | |
57 | struct ttm_buffer_object *first; | |
58 | struct ttm_buffer_object *last; | |
59 | }; | |
60 | ||
61 | /** | |
62 | * struct ttm_lru_bulk_move | |
63 | * | |
64 | * @tt: first/last lru entry for BOs in the TT domain | |
65 | * @vram: first/last lru entry for BOs in the VRAM domain | |
66 | * @swap: first/last lru entry for BOs on the swap list | |
67 | * | |
68 | * Helper structure for bulk moves on the LRU list. | |
69 | */ | |
70 | struct ttm_lru_bulk_move { | |
71 | struct ttm_lru_bulk_move_pos tt[TTM_MAX_BO_PRIORITY]; | |
72 | struct ttm_lru_bulk_move_pos vram[TTM_MAX_BO_PRIORITY]; | |
73 | struct ttm_lru_bulk_move_pos swap[TTM_MAX_BO_PRIORITY]; | |
74 | }; | |
75 | ||
ba4e7d97 TH |
76 | /* |
77 | * ttm_bo.c | |
78 | */ | |
79 | ||
ba4e7d97 TH |
80 | /** |
81 | * ttm_bo_mem_space | |
82 | * | |
83 | * @bo: Pointer to a struct ttm_buffer_object. the data of which | |
84 | * we want to allocate space for. | |
85 | * @proposed_placement: Proposed new placement for the buffer object. | |
2966141a | 86 | * @mem: A struct ttm_resource. |
ba4e7d97 | 87 | * @interruptible: Sleep interruptible when sliping. |
9d87fa21 | 88 | * @no_wait_gpu: Return immediately if the GPU is busy. |
ba4e7d97 TH |
89 | * |
90 | * Allocate memory space for the buffer object pointed to by @bo, using | |
91 | * the placement flags in @mem, potentially evicting other idle buffer objects. | |
92 | * This function may sleep while waiting for space to become available. | |
93 | * Returns: | |
94 | * -EBUSY: No space available (only if no_wait == 1). | |
95 | * -ENOMEM: Could not allocate memory for the buffer object, either due to | |
96 | * fragmentation or concurrent allocators. | |
98ffc415 | 97 | * -ERESTARTSYS: An interruptible sleep was interrupted by a signal. |
ba4e7d97 | 98 | */ |
1144b63a CK |
99 | int ttm_bo_mem_space(struct ttm_buffer_object *bo, |
100 | struct ttm_placement *placement, | |
2966141a | 101 | struct ttm_resource *mem, |
c13c55d6 | 102 | struct ttm_operation_ctx *ctx); |
1144b63a | 103 | |
e024e110 DA |
104 | /** |
105 | * ttm_bo_unmap_virtual | |
106 | * | |
107 | * @bo: tear down the virtual mappings for this BO | |
108 | */ | |
1144b63a | 109 | void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo); |
ba4e7d97 | 110 | |
34820324 | 111 | /** |
46bca88b | 112 | * ttm_bo_reserve: |
34820324 ML |
113 | * |
114 | * @bo: A pointer to a struct ttm_buffer_object. | |
115 | * @interruptible: Sleep interruptible if waiting. | |
116 | * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY. | |
dfd5e50e | 117 | * @ticket: ticket used to acquire the ww_mutex. |
34820324 | 118 | * |
46bca88b DA |
119 | * Locks a buffer object for validation. (Or prevents other processes from |
120 | * locking it for validation), while taking a number of measures to prevent | |
121 | * deadlocks. | |
34820324 ML |
122 | * |
123 | * Returns: | |
124 | * -EDEADLK: The reservation may cause a deadlock. | |
125 | * Release all buffer reservations, wait for @bo to become unreserved and | |
46bca88b | 126 | * try again. |
34820324 ML |
127 | * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by |
128 | * a signal. Release all buffer reservations and return to user-space. | |
129 | * -EBUSY: The function needed to sleep, but @no_wait was true | |
130 | * -EALREADY: Bo already reserved using @ticket. This error code will only | |
131 | * be returned if @use_ticket is set to true. | |
132 | */ | |
46bca88b DA |
133 | static inline int ttm_bo_reserve(struct ttm_buffer_object *bo, |
134 | bool interruptible, bool no_wait, | |
135 | struct ww_acquire_ctx *ticket) | |
34820324 ML |
136 | { |
137 | int ret = 0; | |
138 | ||
139 | if (no_wait) { | |
140 | bool success; | |
141 | if (WARN_ON(ticket)) | |
142 | return -EBUSY; | |
143 | ||
52791eee | 144 | success = dma_resv_trylock(bo->base.resv); |
34820324 ML |
145 | return success ? 0 : -EBUSY; |
146 | } | |
147 | ||
148 | if (interruptible) | |
52791eee | 149 | ret = dma_resv_lock_interruptible(bo->base.resv, ticket); |
34820324 | 150 | else |
52791eee | 151 | ret = dma_resv_lock(bo->base.resv, ticket); |
34820324 ML |
152 | if (ret == -EINTR) |
153 | return -ERESTARTSYS; | |
154 | return ret; | |
155 | } | |
eba67093 | 156 | |
5e45d7df ML |
157 | /** |
158 | * ttm_bo_reserve_slowpath: | |
159 | * @bo: A pointer to a struct ttm_buffer_object. | |
160 | * @interruptible: Sleep interruptible if waiting. | |
161 | * @sequence: Set (@bo)->sequence to this value after lock | |
162 | * | |
163 | * This is called after ttm_bo_reserve returns -EAGAIN and we backed off | |
164 | * from all our other reservations. Because there are no other reservations | |
165 | * held by us, this function cannot deadlock any more. | |
166 | */ | |
34820324 ML |
167 | static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo, |
168 | bool interruptible, | |
169 | struct ww_acquire_ctx *ticket) | |
170 | { | |
46bca88b DA |
171 | if (interruptible) { |
172 | int ret = dma_resv_lock_slow_interruptible(bo->base.resv, | |
173 | ticket); | |
174 | if (ret == -EINTR) | |
175 | ret = -ERESTARTSYS; | |
176 | return ret; | |
177 | } | |
178 | dma_resv_lock_slow(bo->base.resv, ticket); | |
179 | return 0; | |
34820324 | 180 | } |
ba4e7d97 | 181 | |
3d1a88e1 CK |
182 | static inline void |
183 | ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo) | |
20784cdf | 184 | { |
8af8a109 | 185 | spin_lock(&ttm_glob.lru_lock); |
3d1a88e1 | 186 | ttm_bo_move_to_lru_tail(bo, &bo->mem, NULL); |
8af8a109 | 187 | spin_unlock(&ttm_glob.lru_lock); |
20784cdf DA |
188 | } |
189 | ||
2ee476f7 DA |
190 | static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo, |
191 | struct ttm_resource *new_mem) | |
192 | { | |
193 | bo->mem = *new_mem; | |
194 | new_mem->mm_node = NULL; | |
195 | } | |
196 | ||
ecfe6953 DA |
197 | /** |
198 | * ttm_bo_move_null = assign memory for a buffer object. | |
199 | * @bo: The bo to assign the memory to | |
200 | * @new_mem: The memory to be assigned. | |
201 | * | |
202 | * Assign the memory from new_mem to the memory of the buffer object bo. | |
203 | */ | |
204 | static inline void ttm_bo_move_null(struct ttm_buffer_object *bo, | |
205 | struct ttm_resource *new_mem) | |
206 | { | |
207 | struct ttm_resource *old_mem = &bo->mem; | |
208 | ||
209 | WARN_ON(old_mem->mm_node != NULL); | |
2ee476f7 | 210 | ttm_bo_assign_mem(bo, new_mem); |
ecfe6953 DA |
211 | } |
212 | ||
ecff665f | 213 | /** |
34820324 ML |
214 | * ttm_bo_unreserve |
215 | * | |
95762c2b TH |
216 | * @bo: A pointer to a struct ttm_buffer_object. |
217 | * | |
34820324 | 218 | * Unreserve a previous reservation of @bo. |
95762c2b | 219 | */ |
34820324 ML |
220 | static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo) |
221 | { | |
20784cdf | 222 | ttm_bo_move_to_lru_tail_unlocked(bo); |
52791eee | 223 | dma_resv_unlock(bo->base.resv); |
c7523083 TH |
224 | } |
225 | ||
ba4e7d97 TH |
226 | /* |
227 | * ttm_bo_util.c | |
228 | */ | |
8af8a109 | 229 | int ttm_mem_io_reserve(struct ttm_device *bdev, |
2966141a | 230 | struct ttm_resource *mem); |
8af8a109 | 231 | void ttm_mem_io_free(struct ttm_device *bdev, |
2966141a | 232 | struct ttm_resource *mem); |
ba4e7d97 TH |
233 | |
234 | /** | |
235 | * ttm_bo_move_memcpy | |
236 | * | |
237 | * @bo: A pointer to a struct ttm_buffer_object. | |
77dfc28b | 238 | * @interruptible: Sleep interruptible if waiting. |
9d87fa21 | 239 | * @no_wait_gpu: Return immediately if the GPU is busy. |
2966141a | 240 | * @new_mem: struct ttm_resource indicating where to move. |
ba4e7d97 TH |
241 | * |
242 | * Fallback move function for a mappable buffer object in mappable memory. | |
243 | * The function will, if successful, | |
244 | * free any old aperture space, and set (@new_mem)->mm_node to NULL, | |
245 | * and update the (@bo)->mem placement flags. If unsuccessful, the old | |
246 | * data remains untouched, and it's up to the caller to free the | |
247 | * memory space indicated by @new_mem. | |
248 | * Returns: | |
249 | * !0: Failure. | |
250 | */ | |
251 | ||
1144b63a | 252 | int ttm_bo_move_memcpy(struct ttm_buffer_object *bo, |
3e98d829 | 253 | struct ttm_operation_ctx *ctx, |
2966141a | 254 | struct ttm_resource *new_mem); |
ba4e7d97 | 255 | |
ba4e7d97 TH |
256 | /** |
257 | * ttm_bo_move_accel_cleanup. | |
258 | * | |
259 | * @bo: A pointer to a struct ttm_buffer_object. | |
f2c24b83 | 260 | * @fence: A fence object that signals when moving is complete. |
ba4e7d97 | 261 | * @evict: This is an evict move. Don't return until the buffer is idle. |
e46f468f | 262 | * @pipeline: evictions are to be pipelined. |
2966141a | 263 | * @new_mem: struct ttm_resource indicating where to move. |
ba4e7d97 TH |
264 | * |
265 | * Accelerated move function to be called when an accelerated move | |
266 | * has been scheduled. The function will create a new temporary buffer object | |
267 | * representing the old placement, and put the sync object on both buffer | |
268 | * objects. After that the newly created buffer object is unref'd to be | |
269 | * destroyed when the move is complete. This will help pipeline | |
270 | * buffer moves. | |
271 | */ | |
1144b63a CK |
272 | int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo, |
273 | struct dma_fence *fence, bool evict, | |
e46f468f | 274 | bool pipeline, |
2966141a | 275 | struct ttm_resource *new_mem); |
3ddf4ad9 | 276 | |
5d951098 CK |
277 | /** |
278 | * ttm_bo_pipeline_gutting. | |
279 | * | |
280 | * @bo: A pointer to a struct ttm_buffer_object. | |
281 | * | |
1e55a53a | 282 | * Pipelined gutting a BO of its backing store. |
5d951098 CK |
283 | */ |
284 | int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo); | |
285 | ||
ba4e7d97 TH |
286 | /** |
287 | * ttm_io_prot | |
288 | * | |
867bcecd CK |
289 | * bo: ttm buffer object |
290 | * res: ttm resource object | |
ba4e7d97 TH |
291 | * @tmp: Page protection flag for a normal, cached mapping. |
292 | * | |
293 | * Utility function that returns the pgprot_t that should be used for | |
294 | * setting up a PTE with the caching model indicated by @c_state. | |
295 | */ | |
867bcecd CK |
296 | pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res, |
297 | pgprot_t tmp); | |
ba4e7d97 | 298 | |
9e9a153b DA |
299 | /** |
300 | * ttm_bo_tt_bind | |
301 | * | |
302 | * Bind the object tt to a memory resource. | |
303 | */ | |
304 | int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem); | |
305 | ||
2ff6e69c DA |
306 | /** |
307 | * ttm_bo_tt_destroy. | |
308 | */ | |
309 | void ttm_bo_tt_destroy(struct ttm_buffer_object *bo); | |
310 | ||
3c90424b DA |
311 | /** |
312 | * ttm_range_man_init | |
313 | * | |
314 | * @bdev: ttm device | |
37205891 | 315 | * @type: memory manager type |
37205891 | 316 | * @use_tt: if the memory manager uses tt |
3c90424b DA |
317 | * @p_size: size of area to be managed in pages. |
318 | * | |
319 | * Initialise a generic range manager for the selected memory type. | |
320 | * The range manager is installed for this device in the type slot. | |
321 | */ | |
8af8a109 | 322 | int ttm_range_man_init(struct ttm_device *bdev, |
9c3006a4 | 323 | unsigned type, bool use_tt, |
3c90424b DA |
324 | unsigned long p_size); |
325 | ||
56ee8b1c DA |
326 | /** |
327 | * ttm_range_man_fini | |
328 | * | |
329 | * @bdev: ttm device | |
330 | * @type: memory manager type | |
331 | * | |
332 | * Remove the generic range manager from a slot and tear it down. | |
333 | */ | |
8af8a109 | 334 | int ttm_range_man_fini(struct ttm_device *bdev, |
37205891 | 335 | unsigned type); |
56ee8b1c | 336 | |
ba4e7d97 | 337 | #endif |