]>
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 | ||
25d0479a JP |
31 | #define pr_fmt(fmt) "[TTM] " fmt |
32 | ||
760285e7 DH |
33 | #include <drm/ttm/ttm_module.h> |
34 | #include <drm/ttm/ttm_bo_driver.h> | |
35 | #include <drm/ttm/ttm_placement.h> | |
ba4e7d97 TH |
36 | #include <linux/jiffies.h> |
37 | #include <linux/slab.h> | |
38 | #include <linux/sched.h> | |
39 | #include <linux/mm.h> | |
40 | #include <linux/file.h> | |
41 | #include <linux/module.h> | |
60063497 | 42 | #include <linux/atomic.h> |
f2c24b83 | 43 | #include <linux/reservation.h> |
ba4e7d97 TH |
44 | |
45 | #define TTM_ASSERT_LOCKED(param) | |
46 | #define TTM_DEBUG(fmt, arg...) | |
47 | #define TTM_BO_HASH_ORDER 13 | |
48 | ||
ba4e7d97 | 49 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink); |
a987fcaa TH |
50 | static void ttm_bo_global_kobj_release(struct kobject *kobj); |
51 | ||
52 | static struct attribute ttm_bo_count = { | |
53 | .name = "bo_count", | |
54 | .mode = S_IRUGO | |
55 | }; | |
56 | ||
f1217ed0 CK |
57 | static inline int ttm_mem_type_from_place(const struct ttm_place *place, |
58 | uint32_t *mem_type) | |
fb53f862 JG |
59 | { |
60 | int i; | |
61 | ||
62 | for (i = 0; i <= TTM_PL_PRIV5; i++) | |
f1217ed0 | 63 | if (place->flags & (1 << i)) { |
fb53f862 JG |
64 | *mem_type = i; |
65 | return 0; | |
66 | } | |
67 | return -EINVAL; | |
68 | } | |
69 | ||
5012f506 | 70 | static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type) |
fb53f862 | 71 | { |
5012f506 JG |
72 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
73 | ||
25d0479a JP |
74 | pr_err(" has_type: %d\n", man->has_type); |
75 | pr_err(" use_type: %d\n", man->use_type); | |
76 | pr_err(" flags: 0x%08X\n", man->flags); | |
77 | pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset); | |
78 | pr_err(" size: %llu\n", man->size); | |
79 | pr_err(" available_caching: 0x%08X\n", man->available_caching); | |
80 | pr_err(" default_caching: 0x%08X\n", man->default_caching); | |
d961db75 BS |
81 | if (mem_type != TTM_PL_SYSTEM) |
82 | (*man->func->debug)(man, TTM_PFX); | |
fb53f862 JG |
83 | } |
84 | ||
85 | static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, | |
86 | struct ttm_placement *placement) | |
87 | { | |
fb53f862 JG |
88 | int i, ret, mem_type; |
89 | ||
25d0479a JP |
90 | pr_err("No space for %p (%lu pages, %luK, %luM)\n", |
91 | bo, bo->mem.num_pages, bo->mem.size >> 10, | |
92 | bo->mem.size >> 20); | |
fb53f862 | 93 | for (i = 0; i < placement->num_placement; i++) { |
f1217ed0 | 94 | ret = ttm_mem_type_from_place(&placement->placement[i], |
fb53f862 JG |
95 | &mem_type); |
96 | if (ret) | |
97 | return; | |
25d0479a | 98 | pr_err(" placement[%d]=0x%08X (%d)\n", |
f1217ed0 | 99 | i, placement->placement[i].flags, mem_type); |
5012f506 | 100 | ttm_mem_type_debug(bo->bdev, mem_type); |
fb53f862 JG |
101 | } |
102 | } | |
103 | ||
a987fcaa TH |
104 | static ssize_t ttm_bo_global_show(struct kobject *kobj, |
105 | struct attribute *attr, | |
106 | char *buffer) | |
107 | { | |
108 | struct ttm_bo_global *glob = | |
109 | container_of(kobj, struct ttm_bo_global, kobj); | |
110 | ||
111 | return snprintf(buffer, PAGE_SIZE, "%lu\n", | |
112 | (unsigned long) atomic_read(&glob->bo_count)); | |
113 | } | |
114 | ||
115 | static struct attribute *ttm_bo_global_attrs[] = { | |
116 | &ttm_bo_count, | |
117 | NULL | |
118 | }; | |
119 | ||
52cf25d0 | 120 | static const struct sysfs_ops ttm_bo_global_ops = { |
a987fcaa TH |
121 | .show = &ttm_bo_global_show |
122 | }; | |
123 | ||
124 | static struct kobj_type ttm_bo_glob_kobj_type = { | |
125 | .release = &ttm_bo_global_kobj_release, | |
126 | .sysfs_ops = &ttm_bo_global_ops, | |
127 | .default_attrs = ttm_bo_global_attrs | |
128 | }; | |
129 | ||
ba4e7d97 TH |
130 | |
131 | static inline uint32_t ttm_bo_type_flags(unsigned type) | |
132 | { | |
133 | return 1 << (type); | |
134 | } | |
135 | ||
136 | static void ttm_bo_release_list(struct kref *list_kref) | |
137 | { | |
138 | struct ttm_buffer_object *bo = | |
139 | container_of(list_kref, struct ttm_buffer_object, list_kref); | |
140 | struct ttm_bo_device *bdev = bo->bdev; | |
57de4ba9 | 141 | size_t acc_size = bo->acc_size; |
ba4e7d97 TH |
142 | |
143 | BUG_ON(atomic_read(&bo->list_kref.refcount)); | |
144 | BUG_ON(atomic_read(&bo->kref.refcount)); | |
145 | BUG_ON(atomic_read(&bo->cpu_writers)); | |
ba4e7d97 TH |
146 | BUG_ON(bo->mem.mm_node != NULL); |
147 | BUG_ON(!list_empty(&bo->lru)); | |
148 | BUG_ON(!list_empty(&bo->ddestroy)); | |
149 | ||
150 | if (bo->ttm) | |
151 | ttm_tt_destroy(bo->ttm); | |
a987fcaa | 152 | atomic_dec(&bo->glob->bo_count); |
5e338405 ML |
153 | if (bo->resv == &bo->ttm_resv) |
154 | reservation_object_fini(&bo->ttm_resv); | |
c58f009e | 155 | mutex_destroy(&bo->wu_mutex); |
ba4e7d97 TH |
156 | if (bo->destroy) |
157 | bo->destroy(bo); | |
158 | else { | |
ba4e7d97 TH |
159 | kfree(bo); |
160 | } | |
57de4ba9 | 161 | ttm_mem_global_free(bdev->glob->mem_glob, acc_size); |
ba4e7d97 TH |
162 | } |
163 | ||
d6ea8886 | 164 | void ttm_bo_add_to_lru(struct ttm_buffer_object *bo) |
ba4e7d97 TH |
165 | { |
166 | struct ttm_bo_device *bdev = bo->bdev; | |
167 | struct ttm_mem_type_manager *man; | |
168 | ||
009a9dad | 169 | lockdep_assert_held(&bo->resv->lock.base); |
ba4e7d97 TH |
170 | |
171 | if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { | |
172 | ||
173 | BUG_ON(!list_empty(&bo->lru)); | |
174 | ||
175 | man = &bdev->man[bo->mem.mem_type]; | |
176 | list_add_tail(&bo->lru, &man->lru); | |
177 | kref_get(&bo->list_kref); | |
178 | ||
179 | if (bo->ttm != NULL) { | |
a987fcaa | 180 | list_add_tail(&bo->swap, &bo->glob->swap_lru); |
ba4e7d97 TH |
181 | kref_get(&bo->list_kref); |
182 | } | |
183 | } | |
184 | } | |
34820324 | 185 | EXPORT_SYMBOL(ttm_bo_add_to_lru); |
ba4e7d97 | 186 | |
d6ea8886 | 187 | int ttm_bo_del_from_lru(struct ttm_buffer_object *bo) |
ba4e7d97 TH |
188 | { |
189 | int put_count = 0; | |
190 | ||
191 | if (!list_empty(&bo->swap)) { | |
192 | list_del_init(&bo->swap); | |
193 | ++put_count; | |
194 | } | |
195 | if (!list_empty(&bo->lru)) { | |
196 | list_del_init(&bo->lru); | |
197 | ++put_count; | |
198 | } | |
199 | ||
200 | /* | |
201 | * TODO: Add a driver hook to delete from | |
202 | * driver-specific LRU's here. | |
203 | */ | |
204 | ||
205 | return put_count; | |
206 | } | |
207 | ||
ba4e7d97 TH |
208 | static void ttm_bo_ref_bug(struct kref *list_kref) |
209 | { | |
210 | BUG(); | |
211 | } | |
212 | ||
d6ea8886 DA |
213 | void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count, |
214 | bool never_free) | |
215 | { | |
2357cbe5 TH |
216 | kref_sub(&bo->list_kref, count, |
217 | (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list); | |
d6ea8886 DA |
218 | } |
219 | ||
34820324 | 220 | void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo) |
5e45d7df | 221 | { |
34820324 | 222 | int put_count; |
ecff665f | 223 | |
34820324 ML |
224 | spin_lock(&bo->glob->lru_lock); |
225 | put_count = ttm_bo_del_from_lru(bo); | |
226 | spin_unlock(&bo->glob->lru_lock); | |
227 | ttm_bo_list_ref_sub(bo, put_count, true); | |
ecff665f | 228 | } |
34820324 | 229 | EXPORT_SYMBOL(ttm_bo_del_sub_from_lru); |
ecff665f | 230 | |
ba4e7d97 TH |
231 | /* |
232 | * Call bo->mutex locked. | |
233 | */ | |
ba4e7d97 TH |
234 | static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc) |
235 | { | |
236 | struct ttm_bo_device *bdev = bo->bdev; | |
a987fcaa | 237 | struct ttm_bo_global *glob = bo->glob; |
ba4e7d97 TH |
238 | int ret = 0; |
239 | uint32_t page_flags = 0; | |
240 | ||
241 | TTM_ASSERT_LOCKED(&bo->mutex); | |
242 | bo->ttm = NULL; | |
243 | ||
ad49f501 DA |
244 | if (bdev->need_dma32) |
245 | page_flags |= TTM_PAGE_FLAG_DMA32; | |
246 | ||
ba4e7d97 TH |
247 | switch (bo->type) { |
248 | case ttm_bo_type_device: | |
249 | if (zero_alloc) | |
250 | page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC; | |
251 | case ttm_bo_type_kernel: | |
649bf3ca JG |
252 | bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
253 | page_flags, glob->dummy_read_page); | |
ba4e7d97 TH |
254 | if (unlikely(bo->ttm == NULL)) |
255 | ret = -ENOMEM; | |
256 | break; | |
129b78bf DA |
257 | case ttm_bo_type_sg: |
258 | bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, | |
259 | page_flags | TTM_PAGE_FLAG_SG, | |
260 | glob->dummy_read_page); | |
261 | if (unlikely(bo->ttm == NULL)) { | |
262 | ret = -ENOMEM; | |
263 | break; | |
264 | } | |
265 | bo->ttm->sg = bo->sg; | |
266 | break; | |
ba4e7d97 | 267 | default: |
25d0479a | 268 | pr_err("Illegal buffer object type\n"); |
ba4e7d97 TH |
269 | ret = -EINVAL; |
270 | break; | |
271 | } | |
272 | ||
273 | return ret; | |
274 | } | |
275 | ||
276 | static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, | |
277 | struct ttm_mem_reg *mem, | |
9d87fa21 | 278 | bool evict, bool interruptible, |
97a875cb | 279 | bool no_wait_gpu) |
ba4e7d97 TH |
280 | { |
281 | struct ttm_bo_device *bdev = bo->bdev; | |
282 | bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem); | |
283 | bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem); | |
284 | struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type]; | |
285 | struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type]; | |
286 | int ret = 0; | |
287 | ||
288 | if (old_is_pci || new_is_pci || | |
eba67093 TH |
289 | ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) { |
290 | ret = ttm_mem_io_lock(old_man, true); | |
291 | if (unlikely(ret != 0)) | |
292 | goto out_err; | |
293 | ttm_bo_unmap_virtual_locked(bo); | |
294 | ttm_mem_io_unlock(old_man); | |
295 | } | |
ba4e7d97 TH |
296 | |
297 | /* | |
298 | * Create and bind a ttm if required. | |
299 | */ | |
300 | ||
8d3bb236 BS |
301 | if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
302 | if (bo->ttm == NULL) { | |
ff02b13f BS |
303 | bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED); |
304 | ret = ttm_bo_add_ttm(bo, zero); | |
8d3bb236 BS |
305 | if (ret) |
306 | goto out_err; | |
307 | } | |
ba4e7d97 TH |
308 | |
309 | ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement); | |
310 | if (ret) | |
87ef9209 | 311 | goto out_err; |
ba4e7d97 TH |
312 | |
313 | if (mem->mem_type != TTM_PL_SYSTEM) { | |
314 | ret = ttm_tt_bind(bo->ttm, mem); | |
315 | if (ret) | |
316 | goto out_err; | |
317 | } | |
318 | ||
319 | if (bo->mem.mem_type == TTM_PL_SYSTEM) { | |
82ef594e BS |
320 | if (bdev->driver->move_notify) |
321 | bdev->driver->move_notify(bo, mem); | |
ca262a99 | 322 | bo->mem = *mem; |
ba4e7d97 | 323 | mem->mm_node = NULL; |
ba4e7d97 TH |
324 | goto moved; |
325 | } | |
ba4e7d97 TH |
326 | } |
327 | ||
9f1feed2 BS |
328 | if (bdev->driver->move_notify) |
329 | bdev->driver->move_notify(bo, mem); | |
330 | ||
ba4e7d97 TH |
331 | if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && |
332 | !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) | |
97a875cb | 333 | ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem); |
ba4e7d97 TH |
334 | else if (bdev->driver->move) |
335 | ret = bdev->driver->move(bo, evict, interruptible, | |
97a875cb | 336 | no_wait_gpu, mem); |
ba4e7d97 | 337 | else |
97a875cb | 338 | ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem); |
ba4e7d97 | 339 | |
9f1feed2 BS |
340 | if (ret) { |
341 | if (bdev->driver->move_notify) { | |
342 | struct ttm_mem_reg tmp_mem = *mem; | |
343 | *mem = bo->mem; | |
344 | bo->mem = tmp_mem; | |
345 | bdev->driver->move_notify(bo, mem); | |
346 | bo->mem = *mem; | |
014b3440 | 347 | *mem = tmp_mem; |
9f1feed2 | 348 | } |
ba4e7d97 | 349 | |
9f1feed2 BS |
350 | goto out_err; |
351 | } | |
dc97b340 | 352 | |
ba4e7d97 TH |
353 | moved: |
354 | if (bo->evicted) { | |
9ef7506f RC |
355 | if (bdev->driver->invalidate_caches) { |
356 | ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); | |
357 | if (ret) | |
358 | pr_err("Can not flush read caches\n"); | |
359 | } | |
ba4e7d97 TH |
360 | bo->evicted = false; |
361 | } | |
362 | ||
363 | if (bo->mem.mm_node) { | |
d961db75 | 364 | bo->offset = (bo->mem.start << PAGE_SHIFT) + |
ba4e7d97 TH |
365 | bdev->man[bo->mem.mem_type].gpu_offset; |
366 | bo->cur_placement = bo->mem.placement; | |
354fb52c TH |
367 | } else |
368 | bo->offset = 0; | |
ba4e7d97 TH |
369 | |
370 | return 0; | |
371 | ||
372 | out_err: | |
373 | new_man = &bdev->man[bo->mem.mem_type]; | |
374 | if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) { | |
375 | ttm_tt_unbind(bo->ttm); | |
376 | ttm_tt_destroy(bo->ttm); | |
377 | bo->ttm = NULL; | |
378 | } | |
379 | ||
380 | return ret; | |
381 | } | |
382 | ||
1df6a2eb | 383 | /** |
40d857bb | 384 | * Call bo::reserved. |
1df6a2eb | 385 | * Will release GPU memory type usage on destruction. |
40d857bb TH |
386 | * This is the place to put in driver specific hooks to release |
387 | * driver private resources. | |
388 | * Will release the bo::reserved lock. | |
1df6a2eb TH |
389 | */ |
390 | ||
391 | static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) | |
392 | { | |
dc97b340 JG |
393 | if (bo->bdev->driver->move_notify) |
394 | bo->bdev->driver->move_notify(bo, NULL); | |
395 | ||
1df6a2eb | 396 | if (bo->ttm) { |
1df6a2eb TH |
397 | ttm_tt_unbind(bo->ttm); |
398 | ttm_tt_destroy(bo->ttm); | |
399 | bo->ttm = NULL; | |
1df6a2eb | 400 | } |
40d857bb | 401 | ttm_bo_mem_put(bo, &bo->mem); |
1df6a2eb | 402 | |
5e338405 | 403 | ww_mutex_unlock (&bo->resv->lock); |
1df6a2eb TH |
404 | } |
405 | ||
f2c24b83 ML |
406 | static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo) |
407 | { | |
408 | struct reservation_object_list *fobj; | |
409 | struct fence *fence; | |
410 | int i; | |
411 | ||
412 | fobj = reservation_object_get_list(bo->resv); | |
413 | fence = reservation_object_get_excl(bo->resv); | |
414 | if (fence && !fence->ops->signaled) | |
415 | fence_enable_sw_signaling(fence); | |
416 | ||
417 | for (i = 0; fobj && i < fobj->shared_count; ++i) { | |
418 | fence = rcu_dereference_protected(fobj->shared[i], | |
419 | reservation_object_held(bo->resv)); | |
420 | ||
421 | if (!fence->ops->signaled) | |
422 | fence_enable_sw_signaling(fence); | |
423 | } | |
424 | } | |
425 | ||
e1efc9b6 | 426 | static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo) |
ba4e7d97 TH |
427 | { |
428 | struct ttm_bo_device *bdev = bo->bdev; | |
a987fcaa | 429 | struct ttm_bo_global *glob = bo->glob; |
e1efc9b6 | 430 | int put_count; |
ba4e7d97 TH |
431 | int ret; |
432 | ||
4154f051 | 433 | spin_lock(&glob->lru_lock); |
0eff2a24 | 434 | ret = __ttm_bo_reserve(bo, false, true, false, NULL); |
4154f051 | 435 | |
dd7cfd64 | 436 | if (!ret) { |
f2c24b83 | 437 | if (!ttm_bo_wait(bo, false, false, true)) { |
dd7cfd64 | 438 | put_count = ttm_bo_del_from_lru(bo); |
ba4e7d97 | 439 | |
dd7cfd64 ML |
440 | spin_unlock(&glob->lru_lock); |
441 | ttm_bo_cleanup_memtype_use(bo); | |
ba4e7d97 | 442 | |
dd7cfd64 | 443 | ttm_bo_list_ref_sub(bo, put_count, true); |
4154f051 | 444 | |
dd7cfd64 | 445 | return; |
f2c24b83 ML |
446 | } else |
447 | ttm_bo_flush_all_fences(bo); | |
15205fbc TH |
448 | |
449 | /* | |
450 | * Make NO_EVICT bos immediately available to | |
451 | * shrinkers, now that they are queued for | |
452 | * destruction. | |
453 | */ | |
454 | if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) { | |
455 | bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT; | |
456 | ttm_bo_add_to_lru(bo); | |
457 | } | |
458 | ||
c7523083 | 459 | __ttm_bo_unreserve(bo); |
15205fbc | 460 | } |
e1efc9b6 TH |
461 | |
462 | kref_get(&bo->list_kref); | |
463 | list_add_tail(&bo->ddestroy, &bdev->ddestroy); | |
464 | spin_unlock(&glob->lru_lock); | |
e1efc9b6 | 465 | |
e1efc9b6 TH |
466 | schedule_delayed_work(&bdev->wq, |
467 | ((HZ / 100) < 1) ? 1 : HZ / 100); | |
468 | } | |
469 | ||
470 | /** | |
85b144f8 | 471 | * function ttm_bo_cleanup_refs_and_unlock |
e1efc9b6 TH |
472 | * If bo idle, remove from delayed- and lru lists, and unref. |
473 | * If not idle, do nothing. | |
474 | * | |
85b144f8 ML |
475 | * Must be called with lru_lock and reservation held, this function |
476 | * will drop both before returning. | |
477 | * | |
e1efc9b6 | 478 | * @interruptible Any sleeps should occur interruptibly. |
e1efc9b6 TH |
479 | * @no_wait_gpu Never wait for gpu. Return -EBUSY instead. |
480 | */ | |
481 | ||
85b144f8 ML |
482 | static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo, |
483 | bool interruptible, | |
484 | bool no_wait_gpu) | |
e1efc9b6 TH |
485 | { |
486 | struct ttm_bo_global *glob = bo->glob; | |
487 | int put_count; | |
85b144f8 | 488 | int ret; |
e1efc9b6 | 489 | |
85b144f8 | 490 | ret = ttm_bo_wait(bo, false, false, true); |
e1efc9b6 | 491 | |
85b144f8 | 492 | if (ret && !no_wait_gpu) { |
472db7ab ML |
493 | long lret; |
494 | ww_mutex_unlock(&bo->resv->lock); | |
495 | spin_unlock(&glob->lru_lock); | |
496 | ||
497 | lret = reservation_object_wait_timeout_rcu(bo->resv, | |
498 | true, | |
499 | interruptible, | |
500 | 30 * HZ); | |
501 | ||
502 | if (lret < 0) | |
503 | return lret; | |
504 | else if (lret == 0) | |
505 | return -EBUSY; | |
b8e902f2 | 506 | |
85b144f8 | 507 | spin_lock(&glob->lru_lock); |
0eff2a24 | 508 | ret = __ttm_bo_reserve(bo, false, true, false, NULL); |
b8e902f2 | 509 | |
85b144f8 ML |
510 | /* |
511 | * We raced, and lost, someone else holds the reservation now, | |
512 | * and is probably busy in ttm_bo_cleanup_memtype_use. | |
513 | * | |
514 | * Even if it's not the case, because we finished waiting any | |
515 | * delayed destruction would succeed, so just return success | |
516 | * here. | |
517 | */ | |
518 | if (ret) { | |
519 | spin_unlock(&glob->lru_lock); | |
520 | return 0; | |
521 | } | |
7040138f ML |
522 | |
523 | /* | |
524 | * remove sync_obj with ttm_bo_wait, the wait should be | |
525 | * finished, and no new wait object should have been added. | |
526 | */ | |
7040138f ML |
527 | ret = ttm_bo_wait(bo, false, false, true); |
528 | WARN_ON(ret); | |
529 | } | |
ba4e7d97 | 530 | |
85b144f8 | 531 | if (ret || unlikely(list_empty(&bo->ddestroy))) { |
c7523083 | 532 | __ttm_bo_unreserve(bo); |
a987fcaa | 533 | spin_unlock(&glob->lru_lock); |
85b144f8 | 534 | return ret; |
ba4e7d97 TH |
535 | } |
536 | ||
e1efc9b6 TH |
537 | put_count = ttm_bo_del_from_lru(bo); |
538 | list_del_init(&bo->ddestroy); | |
539 | ++put_count; | |
540 | ||
541 | spin_unlock(&glob->lru_lock); | |
542 | ttm_bo_cleanup_memtype_use(bo); | |
543 | ||
d6ea8886 | 544 | ttm_bo_list_ref_sub(bo, put_count, true); |
e1efc9b6 TH |
545 | |
546 | return 0; | |
ba4e7d97 TH |
547 | } |
548 | ||
549 | /** | |
550 | * Traverse the delayed list, and call ttm_bo_cleanup_refs on all | |
551 | * encountered buffers. | |
552 | */ | |
553 | ||
554 | static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) | |
555 | { | |
a987fcaa | 556 | struct ttm_bo_global *glob = bdev->glob; |
1a961ce0 LB |
557 | struct ttm_buffer_object *entry = NULL; |
558 | int ret = 0; | |
ba4e7d97 | 559 | |
a987fcaa | 560 | spin_lock(&glob->lru_lock); |
1a961ce0 LB |
561 | if (list_empty(&bdev->ddestroy)) |
562 | goto out_unlock; | |
563 | ||
564 | entry = list_first_entry(&bdev->ddestroy, | |
565 | struct ttm_buffer_object, ddestroy); | |
566 | kref_get(&entry->list_kref); | |
567 | ||
568 | for (;;) { | |
569 | struct ttm_buffer_object *nentry = NULL; | |
570 | ||
571 | if (entry->ddestroy.next != &bdev->ddestroy) { | |
572 | nentry = list_first_entry(&entry->ddestroy, | |
573 | struct ttm_buffer_object, ddestroy); | |
ba4e7d97 TH |
574 | kref_get(&nentry->list_kref); |
575 | } | |
ba4e7d97 | 576 | |
0eff2a24 | 577 | ret = __ttm_bo_reserve(entry, false, true, false, NULL); |
63d0a419 ML |
578 | if (remove_all && ret) { |
579 | spin_unlock(&glob->lru_lock); | |
c7523083 | 580 | ret = __ttm_bo_reserve(entry, false, false, |
0eff2a24 | 581 | false, NULL); |
63d0a419 ML |
582 | spin_lock(&glob->lru_lock); |
583 | } | |
584 | ||
85b144f8 ML |
585 | if (!ret) |
586 | ret = ttm_bo_cleanup_refs_and_unlock(entry, false, | |
587 | !remove_all); | |
588 | else | |
589 | spin_unlock(&glob->lru_lock); | |
590 | ||
ba4e7d97 | 591 | kref_put(&entry->list_kref, ttm_bo_release_list); |
1a961ce0 LB |
592 | entry = nentry; |
593 | ||
594 | if (ret || !entry) | |
595 | goto out; | |
ba4e7d97 | 596 | |
a987fcaa | 597 | spin_lock(&glob->lru_lock); |
1a961ce0 | 598 | if (list_empty(&entry->ddestroy)) |
ba4e7d97 TH |
599 | break; |
600 | } | |
ba4e7d97 | 601 | |
1a961ce0 LB |
602 | out_unlock: |
603 | spin_unlock(&glob->lru_lock); | |
604 | out: | |
605 | if (entry) | |
606 | kref_put(&entry->list_kref, ttm_bo_release_list); | |
ba4e7d97 TH |
607 | return ret; |
608 | } | |
609 | ||
610 | static void ttm_bo_delayed_workqueue(struct work_struct *work) | |
611 | { | |
612 | struct ttm_bo_device *bdev = | |
613 | container_of(work, struct ttm_bo_device, wq.work); | |
614 | ||
615 | if (ttm_bo_delayed_delete(bdev, false)) { | |
616 | schedule_delayed_work(&bdev->wq, | |
617 | ((HZ / 100) < 1) ? 1 : HZ / 100); | |
618 | } | |
619 | } | |
620 | ||
621 | static void ttm_bo_release(struct kref *kref) | |
622 | { | |
623 | struct ttm_buffer_object *bo = | |
624 | container_of(kref, struct ttm_buffer_object, kref); | |
625 | struct ttm_bo_device *bdev = bo->bdev; | |
eba67093 | 626 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; |
ba4e7d97 | 627 | |
72525b3f | 628 | drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node); |
eba67093 TH |
629 | ttm_mem_io_lock(man, false); |
630 | ttm_mem_io_free_vm(bo); | |
631 | ttm_mem_io_unlock(man); | |
e1efc9b6 | 632 | ttm_bo_cleanup_refs_or_queue(bo); |
ba4e7d97 | 633 | kref_put(&bo->list_kref, ttm_bo_release_list); |
ba4e7d97 TH |
634 | } |
635 | ||
636 | void ttm_bo_unref(struct ttm_buffer_object **p_bo) | |
637 | { | |
638 | struct ttm_buffer_object *bo = *p_bo; | |
ba4e7d97 TH |
639 | |
640 | *p_bo = NULL; | |
ba4e7d97 | 641 | kref_put(&bo->kref, ttm_bo_release); |
ba4e7d97 TH |
642 | } |
643 | EXPORT_SYMBOL(ttm_bo_unref); | |
644 | ||
7c5ee536 MG |
645 | int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev) |
646 | { | |
647 | return cancel_delayed_work_sync(&bdev->wq); | |
648 | } | |
649 | EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue); | |
650 | ||
651 | void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched) | |
652 | { | |
653 | if (resched) | |
654 | schedule_delayed_work(&bdev->wq, | |
655 | ((HZ / 100) < 1) ? 1 : HZ / 100); | |
656 | } | |
657 | EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue); | |
658 | ||
ca262a99 | 659 | static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible, |
97a875cb | 660 | bool no_wait_gpu) |
ba4e7d97 | 661 | { |
ba4e7d97 TH |
662 | struct ttm_bo_device *bdev = bo->bdev; |
663 | struct ttm_mem_reg evict_mem; | |
ca262a99 JG |
664 | struct ttm_placement placement; |
665 | int ret = 0; | |
ba4e7d97 | 666 | |
1717c0e2 | 667 | ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); |
ba4e7d97 | 668 | |
78ecf091 | 669 | if (unlikely(ret != 0)) { |
98ffc415 | 670 | if (ret != -ERESTARTSYS) { |
25d0479a | 671 | pr_err("Failed to expire sync object before buffer eviction\n"); |
78ecf091 | 672 | } |
ba4e7d97 TH |
673 | goto out; |
674 | } | |
675 | ||
009a9dad | 676 | lockdep_assert_held(&bo->resv->lock.base); |
ba4e7d97 TH |
677 | |
678 | evict_mem = bo->mem; | |
679 | evict_mem.mm_node = NULL; | |
eba67093 TH |
680 | evict_mem.bus.io_reserved_vm = false; |
681 | evict_mem.bus.io_reserved_count = 0; | |
ba4e7d97 | 682 | |
7cb7d1d7 JG |
683 | placement.num_placement = 0; |
684 | placement.num_busy_placement = 0; | |
ca262a99 JG |
685 | bdev->driver->evict_flags(bo, &placement); |
686 | ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible, | |
97a875cb | 687 | no_wait_gpu); |
ba4e7d97 | 688 | if (ret) { |
fb53f862 | 689 | if (ret != -ERESTARTSYS) { |
25d0479a JP |
690 | pr_err("Failed to find memory space for buffer 0x%p eviction\n", |
691 | bo); | |
fb53f862 JG |
692 | ttm_bo_mem_space_debug(bo, &placement); |
693 | } | |
ba4e7d97 TH |
694 | goto out; |
695 | } | |
696 | ||
697 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible, | |
97a875cb | 698 | no_wait_gpu); |
ba4e7d97 | 699 | if (ret) { |
98ffc415 | 700 | if (ret != -ERESTARTSYS) |
25d0479a | 701 | pr_err("Buffer eviction failed\n"); |
42311ff9 | 702 | ttm_bo_mem_put(bo, &evict_mem); |
ba4e7d97 TH |
703 | goto out; |
704 | } | |
ca262a99 JG |
705 | bo->evicted = true; |
706 | out: | |
707 | return ret; | |
708 | } | |
709 | ||
710 | static int ttm_mem_evict_first(struct ttm_bo_device *bdev, | |
711 | uint32_t mem_type, | |
97a875cb | 712 | bool interruptible, |
9d87fa21 | 713 | bool no_wait_gpu) |
ca262a99 JG |
714 | { |
715 | struct ttm_bo_global *glob = bdev->glob; | |
716 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; | |
717 | struct ttm_buffer_object *bo; | |
e7ab2019 | 718 | int ret = -EBUSY, put_count; |
ba4e7d97 | 719 | |
a987fcaa | 720 | spin_lock(&glob->lru_lock); |
e7ab2019 | 721 | list_for_each_entry(bo, &man->lru, lru) { |
0eff2a24 | 722 | ret = __ttm_bo_reserve(bo, false, true, false, NULL); |
e7ab2019 ML |
723 | if (!ret) |
724 | break; | |
725 | } | |
726 | ||
727 | if (ret) { | |
9c51ba1d | 728 | spin_unlock(&glob->lru_lock); |
e7ab2019 | 729 | return ret; |
9c51ba1d TH |
730 | } |
731 | ||
ca262a99 | 732 | kref_get(&bo->list_kref); |
9c51ba1d | 733 | |
e1efc9b6 | 734 | if (!list_empty(&bo->ddestroy)) { |
e7ab2019 ML |
735 | ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible, |
736 | no_wait_gpu); | |
e1efc9b6 | 737 | kref_put(&bo->list_kref, ttm_bo_release_list); |
b8e902f2 | 738 | return ret; |
e1efc9b6 TH |
739 | } |
740 | ||
9c51ba1d | 741 | put_count = ttm_bo_del_from_lru(bo); |
a987fcaa | 742 | spin_unlock(&glob->lru_lock); |
9c51ba1d TH |
743 | |
744 | BUG_ON(ret != 0); | |
745 | ||
d6ea8886 | 746 | ttm_bo_list_ref_sub(bo, put_count, true); |
9c51ba1d | 747 | |
97a875cb | 748 | ret = ttm_bo_evict(bo, interruptible, no_wait_gpu); |
ca262a99 | 749 | ttm_bo_unreserve(bo); |
9c51ba1d | 750 | |
ca262a99 | 751 | kref_put(&bo->list_kref, ttm_bo_release_list); |
ba4e7d97 TH |
752 | return ret; |
753 | } | |
754 | ||
42311ff9 BS |
755 | void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem) |
756 | { | |
d961db75 | 757 | struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type]; |
42311ff9 | 758 | |
d961db75 BS |
759 | if (mem->mm_node) |
760 | (*man->func->put_node)(man, mem); | |
42311ff9 BS |
761 | } |
762 | EXPORT_SYMBOL(ttm_bo_mem_put); | |
763 | ||
ba4e7d97 TH |
764 | /** |
765 | * Repeatedly evict memory from the LRU for @mem_type until we create enough | |
766 | * space, or we've evicted everything and there isn't enough space. | |
767 | */ | |
ca262a99 JG |
768 | static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, |
769 | uint32_t mem_type, | |
f1217ed0 | 770 | const struct ttm_place *place, |
ca262a99 | 771 | struct ttm_mem_reg *mem, |
9d87fa21 | 772 | bool interruptible, |
9d87fa21 | 773 | bool no_wait_gpu) |
ba4e7d97 | 774 | { |
ca262a99 | 775 | struct ttm_bo_device *bdev = bo->bdev; |
ba4e7d97 | 776 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
ba4e7d97 TH |
777 | int ret; |
778 | ||
ba4e7d97 | 779 | do { |
f1217ed0 | 780 | ret = (*man->func->get_node)(man, bo, place, mem); |
ca262a99 JG |
781 | if (unlikely(ret != 0)) |
782 | return ret; | |
d961db75 | 783 | if (mem->mm_node) |
ba4e7d97 | 784 | break; |
97a875cb ML |
785 | ret = ttm_mem_evict_first(bdev, mem_type, |
786 | interruptible, no_wait_gpu); | |
ba4e7d97 TH |
787 | if (unlikely(ret != 0)) |
788 | return ret; | |
ba4e7d97 | 789 | } while (1); |
d961db75 | 790 | if (mem->mm_node == NULL) |
ba4e7d97 | 791 | return -ENOMEM; |
ba4e7d97 TH |
792 | mem->mem_type = mem_type; |
793 | return 0; | |
794 | } | |
795 | ||
ae3e8122 TH |
796 | static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man, |
797 | uint32_t cur_placement, | |
798 | uint32_t proposed_placement) | |
799 | { | |
800 | uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING; | |
801 | uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING; | |
802 | ||
803 | /** | |
804 | * Keep current caching if possible. | |
805 | */ | |
806 | ||
807 | if ((cur_placement & caching) != 0) | |
808 | result |= (cur_placement & caching); | |
809 | else if ((man->default_caching & caching) != 0) | |
810 | result |= man->default_caching; | |
811 | else if ((TTM_PL_FLAG_CACHED & caching) != 0) | |
812 | result |= TTM_PL_FLAG_CACHED; | |
813 | else if ((TTM_PL_FLAG_WC & caching) != 0) | |
814 | result |= TTM_PL_FLAG_WC; | |
815 | else if ((TTM_PL_FLAG_UNCACHED & caching) != 0) | |
816 | result |= TTM_PL_FLAG_UNCACHED; | |
817 | ||
818 | return result; | |
819 | } | |
820 | ||
ba4e7d97 | 821 | static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, |
ba4e7d97 | 822 | uint32_t mem_type, |
f1217ed0 | 823 | const struct ttm_place *place, |
ae3e8122 | 824 | uint32_t *masked_placement) |
ba4e7d97 TH |
825 | { |
826 | uint32_t cur_flags = ttm_bo_type_flags(mem_type); | |
827 | ||
f1217ed0 | 828 | if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0) |
ba4e7d97 TH |
829 | return false; |
830 | ||
f1217ed0 | 831 | if ((place->flags & man->available_caching) == 0) |
ba4e7d97 | 832 | return false; |
ba4e7d97 | 833 | |
f1217ed0 | 834 | cur_flags |= (place->flags & man->available_caching); |
ae3e8122 TH |
835 | |
836 | *masked_placement = cur_flags; | |
ba4e7d97 TH |
837 | return true; |
838 | } | |
839 | ||
840 | /** | |
841 | * Creates space for memory region @mem according to its type. | |
842 | * | |
843 | * This function first searches for free space in compatible memory types in | |
844 | * the priority order defined by the driver. If free space isn't found, then | |
845 | * ttm_bo_mem_force_space is attempted in priority order to evict and find | |
846 | * space. | |
847 | */ | |
848 | int ttm_bo_mem_space(struct ttm_buffer_object *bo, | |
ca262a99 JG |
849 | struct ttm_placement *placement, |
850 | struct ttm_mem_reg *mem, | |
97a875cb | 851 | bool interruptible, |
9d87fa21 | 852 | bool no_wait_gpu) |
ba4e7d97 TH |
853 | { |
854 | struct ttm_bo_device *bdev = bo->bdev; | |
855 | struct ttm_mem_type_manager *man; | |
ba4e7d97 TH |
856 | uint32_t mem_type = TTM_PL_SYSTEM; |
857 | uint32_t cur_flags = 0; | |
858 | bool type_found = false; | |
859 | bool type_ok = false; | |
98ffc415 | 860 | bool has_erestartsys = false; |
ca262a99 | 861 | int i, ret; |
ba4e7d97 TH |
862 | |
863 | mem->mm_node = NULL; | |
b6637526 | 864 | for (i = 0; i < placement->num_placement; ++i) { |
f1217ed0 CK |
865 | const struct ttm_place *place = &placement->placement[i]; |
866 | ||
867 | ret = ttm_mem_type_from_place(place, &mem_type); | |
ca262a99 JG |
868 | if (ret) |
869 | return ret; | |
ba4e7d97 TH |
870 | man = &bdev->man[mem_type]; |
871 | ||
f1217ed0 | 872 | type_ok = ttm_bo_mt_compatible(man, mem_type, place, |
ca262a99 | 873 | &cur_flags); |
ba4e7d97 TH |
874 | |
875 | if (!type_ok) | |
876 | continue; | |
877 | ||
ae3e8122 TH |
878 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
879 | cur_flags); | |
ca262a99 JG |
880 | /* |
881 | * Use the access and other non-mapping-related flag bits from | |
882 | * the memory placement flags to the current flags | |
883 | */ | |
f1217ed0 | 884 | ttm_flag_masked(&cur_flags, place->flags, |
ca262a99 | 885 | ~TTM_PL_MASK_MEMTYPE); |
ae3e8122 | 886 | |
ba4e7d97 TH |
887 | if (mem_type == TTM_PL_SYSTEM) |
888 | break; | |
889 | ||
890 | if (man->has_type && man->use_type) { | |
891 | type_found = true; | |
f1217ed0 | 892 | ret = (*man->func->get_node)(man, bo, place, mem); |
ca262a99 JG |
893 | if (unlikely(ret)) |
894 | return ret; | |
ba4e7d97 | 895 | } |
d961db75 | 896 | if (mem->mm_node) |
ba4e7d97 TH |
897 | break; |
898 | } | |
899 | ||
d961db75 | 900 | if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) { |
ba4e7d97 TH |
901 | mem->mem_type = mem_type; |
902 | mem->placement = cur_flags; | |
903 | return 0; | |
904 | } | |
905 | ||
906 | if (!type_found) | |
907 | return -EINVAL; | |
908 | ||
b6637526 | 909 | for (i = 0; i < placement->num_busy_placement; ++i) { |
f1217ed0 CK |
910 | const struct ttm_place *place = &placement->busy_placement[i]; |
911 | ||
912 | ret = ttm_mem_type_from_place(place, &mem_type); | |
ca262a99 JG |
913 | if (ret) |
914 | return ret; | |
ba4e7d97 | 915 | man = &bdev->man[mem_type]; |
ba4e7d97 TH |
916 | if (!man->has_type) |
917 | continue; | |
f1217ed0 | 918 | if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags)) |
ba4e7d97 TH |
919 | continue; |
920 | ||
ae3e8122 TH |
921 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
922 | cur_flags); | |
ca262a99 JG |
923 | /* |
924 | * Use the access and other non-mapping-related flag bits from | |
925 | * the memory placement flags to the current flags | |
926 | */ | |
f1217ed0 | 927 | ttm_flag_masked(&cur_flags, place->flags, |
ca262a99 | 928 | ~TTM_PL_MASK_MEMTYPE); |
ae3e8122 | 929 | |
0eaddb28 TH |
930 | if (mem_type == TTM_PL_SYSTEM) { |
931 | mem->mem_type = mem_type; | |
932 | mem->placement = cur_flags; | |
933 | mem->mm_node = NULL; | |
934 | return 0; | |
935 | } | |
936 | ||
f1217ed0 | 937 | ret = ttm_bo_mem_force_space(bo, mem_type, place, mem, |
97a875cb | 938 | interruptible, no_wait_gpu); |
ba4e7d97 TH |
939 | if (ret == 0 && mem->mm_node) { |
940 | mem->placement = cur_flags; | |
941 | return 0; | |
942 | } | |
98ffc415 TH |
943 | if (ret == -ERESTARTSYS) |
944 | has_erestartsys = true; | |
ba4e7d97 | 945 | } |
98ffc415 | 946 | ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM; |
ba4e7d97 TH |
947 | return ret; |
948 | } | |
949 | EXPORT_SYMBOL(ttm_bo_mem_space); | |
950 | ||
6e87fa48 | 951 | static int ttm_bo_move_buffer(struct ttm_buffer_object *bo, |
ca262a99 | 952 | struct ttm_placement *placement, |
97a875cb | 953 | bool interruptible, |
9d87fa21 | 954 | bool no_wait_gpu) |
ba4e7d97 | 955 | { |
ba4e7d97 TH |
956 | int ret = 0; |
957 | struct ttm_mem_reg mem; | |
958 | ||
009a9dad | 959 | lockdep_assert_held(&bo->resv->lock.base); |
ba4e7d97 TH |
960 | |
961 | /* | |
962 | * FIXME: It's possible to pipeline buffer moves. | |
963 | * Have the driver move function wait for idle when necessary, | |
964 | * instead of doing it here. | |
965 | */ | |
1717c0e2 | 966 | ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); |
ba4e7d97 TH |
967 | if (ret) |
968 | return ret; | |
ba4e7d97 TH |
969 | mem.num_pages = bo->num_pages; |
970 | mem.size = mem.num_pages << PAGE_SHIFT; | |
971 | mem.page_alignment = bo->mem.page_alignment; | |
eba67093 TH |
972 | mem.bus.io_reserved_vm = false; |
973 | mem.bus.io_reserved_count = 0; | |
ba4e7d97 TH |
974 | /* |
975 | * Determine where to move the buffer. | |
976 | */ | |
97a875cb ML |
977 | ret = ttm_bo_mem_space(bo, placement, &mem, |
978 | interruptible, no_wait_gpu); | |
ba4e7d97 TH |
979 | if (ret) |
980 | goto out_unlock; | |
97a875cb ML |
981 | ret = ttm_bo_handle_move_mem(bo, &mem, false, |
982 | interruptible, no_wait_gpu); | |
ba4e7d97 | 983 | out_unlock: |
d961db75 BS |
984 | if (ret && mem.mm_node) |
985 | ttm_bo_mem_put(bo, &mem); | |
ba4e7d97 TH |
986 | return ret; |
987 | } | |
988 | ||
59c8e663 TH |
989 | static bool ttm_bo_mem_compat(struct ttm_placement *placement, |
990 | struct ttm_mem_reg *mem, | |
991 | uint32_t *new_flags) | |
ba4e7d97 | 992 | { |
ca262a99 | 993 | int i; |
e22238ea | 994 | |
ca262a99 | 995 | for (i = 0; i < placement->num_placement; i++) { |
f1217ed0 CK |
996 | const struct ttm_place *heap = &placement->placement[i]; |
997 | if (mem->mm_node && heap->lpfn != 0 && | |
998 | (mem->start < heap->fpfn || | |
999 | mem->start + mem->num_pages > heap->lpfn)) | |
1000 | continue; | |
1001 | ||
1002 | *new_flags = heap->flags; | |
59c8e663 TH |
1003 | if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && |
1004 | (*new_flags & mem->placement & TTM_PL_MASK_MEM)) | |
1005 | return true; | |
1006 | } | |
1007 | ||
1008 | for (i = 0; i < placement->num_busy_placement; i++) { | |
f1217ed0 CK |
1009 | const struct ttm_place *heap = &placement->busy_placement[i]; |
1010 | if (mem->mm_node && heap->lpfn != 0 && | |
1011 | (mem->start < heap->fpfn || | |
1012 | mem->start + mem->num_pages > heap->lpfn)) | |
1013 | continue; | |
1014 | ||
1015 | *new_flags = heap->flags; | |
59c8e663 TH |
1016 | if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && |
1017 | (*new_flags & mem->placement & TTM_PL_MASK_MEM)) | |
1018 | return true; | |
ca262a99 | 1019 | } |
59c8e663 TH |
1020 | |
1021 | return false; | |
ba4e7d97 TH |
1022 | } |
1023 | ||
09855acb JG |
1024 | int ttm_bo_validate(struct ttm_buffer_object *bo, |
1025 | struct ttm_placement *placement, | |
97a875cb | 1026 | bool interruptible, |
9d87fa21 | 1027 | bool no_wait_gpu) |
ba4e7d97 TH |
1028 | { |
1029 | int ret; | |
59c8e663 | 1030 | uint32_t new_flags; |
ba4e7d97 | 1031 | |
009a9dad | 1032 | lockdep_assert_held(&bo->resv->lock.base); |
ba4e7d97 TH |
1033 | /* |
1034 | * Check whether we need to move buffer. | |
1035 | */ | |
59c8e663 | 1036 | if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) { |
97a875cb ML |
1037 | ret = ttm_bo_move_buffer(bo, placement, interruptible, |
1038 | no_wait_gpu); | |
ca262a99 | 1039 | if (ret) |
ba4e7d97 | 1040 | return ret; |
ca262a99 JG |
1041 | } else { |
1042 | /* | |
1043 | * Use the access and other non-mapping-related flag bits from | |
1044 | * the compatible memory placement flags to the active flags | |
1045 | */ | |
59c8e663 | 1046 | ttm_flag_masked(&bo->mem.placement, new_flags, |
ca262a99 | 1047 | ~TTM_PL_MASK_MEMTYPE); |
ba4e7d97 | 1048 | } |
ba4e7d97 TH |
1049 | /* |
1050 | * We might need to add a TTM. | |
1051 | */ | |
ba4e7d97 TH |
1052 | if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { |
1053 | ret = ttm_bo_add_ttm(bo, true); | |
1054 | if (ret) | |
1055 | return ret; | |
1056 | } | |
ba4e7d97 TH |
1057 | return 0; |
1058 | } | |
09855acb | 1059 | EXPORT_SYMBOL(ttm_bo_validate); |
ba4e7d97 | 1060 | |
09855acb JG |
1061 | int ttm_bo_init(struct ttm_bo_device *bdev, |
1062 | struct ttm_buffer_object *bo, | |
1063 | unsigned long size, | |
1064 | enum ttm_bo_type type, | |
1065 | struct ttm_placement *placement, | |
1066 | uint32_t page_alignment, | |
09855acb | 1067 | bool interruptible, |
5df23979 | 1068 | struct file *persistent_swap_storage, |
09855acb | 1069 | size_t acc_size, |
129b78bf | 1070 | struct sg_table *sg, |
09855acb | 1071 | void (*destroy) (struct ttm_buffer_object *)) |
ba4e7d97 | 1072 | { |
09855acb | 1073 | int ret = 0; |
ba4e7d97 | 1074 | unsigned long num_pages; |
57de4ba9 | 1075 | struct ttm_mem_global *mem_glob = bdev->glob->mem_glob; |
5e338405 | 1076 | bool locked; |
57de4ba9 JG |
1077 | |
1078 | ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); | |
1079 | if (ret) { | |
25d0479a | 1080 | pr_err("Out of kernel memory\n"); |
57de4ba9 JG |
1081 | if (destroy) |
1082 | (*destroy)(bo); | |
1083 | else | |
1084 | kfree(bo); | |
1085 | return -ENOMEM; | |
1086 | } | |
ba4e7d97 | 1087 | |
ba4e7d97 TH |
1088 | num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
1089 | if (num_pages == 0) { | |
25d0479a | 1090 | pr_err("Illegal buffer object size\n"); |
7dfbbdcf TH |
1091 | if (destroy) |
1092 | (*destroy)(bo); | |
1093 | else | |
1094 | kfree(bo); | |
a393c730 | 1095 | ttm_mem_global_free(mem_glob, acc_size); |
ba4e7d97 TH |
1096 | return -EINVAL; |
1097 | } | |
1098 | bo->destroy = destroy; | |
1099 | ||
ba4e7d97 TH |
1100 | kref_init(&bo->kref); |
1101 | kref_init(&bo->list_kref); | |
1102 | atomic_set(&bo->cpu_writers, 0); | |
ba4e7d97 TH |
1103 | INIT_LIST_HEAD(&bo->lru); |
1104 | INIT_LIST_HEAD(&bo->ddestroy); | |
1105 | INIT_LIST_HEAD(&bo->swap); | |
eba67093 | 1106 | INIT_LIST_HEAD(&bo->io_reserve_lru); |
c58f009e | 1107 | mutex_init(&bo->wu_mutex); |
ba4e7d97 | 1108 | bo->bdev = bdev; |
a987fcaa | 1109 | bo->glob = bdev->glob; |
ba4e7d97 TH |
1110 | bo->type = type; |
1111 | bo->num_pages = num_pages; | |
eb6d2c39 | 1112 | bo->mem.size = num_pages << PAGE_SHIFT; |
ba4e7d97 TH |
1113 | bo->mem.mem_type = TTM_PL_SYSTEM; |
1114 | bo->mem.num_pages = bo->num_pages; | |
1115 | bo->mem.mm_node = NULL; | |
1116 | bo->mem.page_alignment = page_alignment; | |
eba67093 TH |
1117 | bo->mem.bus.io_reserved_vm = false; |
1118 | bo->mem.bus.io_reserved_count = 0; | |
ba4e7d97 TH |
1119 | bo->priv_flags = 0; |
1120 | bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); | |
5df23979 | 1121 | bo->persistent_swap_storage = persistent_swap_storage; |
ba4e7d97 | 1122 | bo->acc_size = acc_size; |
129b78bf | 1123 | bo->sg = sg; |
5e338405 ML |
1124 | bo->resv = &bo->ttm_resv; |
1125 | reservation_object_init(bo->resv); | |
a987fcaa | 1126 | atomic_inc(&bo->glob->bo_count); |
72525b3f | 1127 | drm_vma_node_reset(&bo->vma_node); |
ba4e7d97 | 1128 | |
ba4e7d97 TH |
1129 | /* |
1130 | * For ttm_bo_type_device buffers, allocate | |
1131 | * address space from the device. | |
1132 | */ | |
f1217ed0 CK |
1133 | if (bo->type == ttm_bo_type_device || |
1134 | bo->type == ttm_bo_type_sg) | |
abf19035 DH |
1135 | ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node, |
1136 | bo->mem.num_pages); | |
ba4e7d97 | 1137 | |
5e338405 ML |
1138 | locked = ww_mutex_trylock(&bo->resv->lock); |
1139 | WARN_ON(!locked); | |
ba4e7d97 | 1140 | |
5e338405 ML |
1141 | if (likely(!ret)) |
1142 | ret = ttm_bo_validate(bo, placement, interruptible, false); | |
ba4e7d97 | 1143 | |
ba4e7d97 | 1144 | ttm_bo_unreserve(bo); |
5e338405 ML |
1145 | |
1146 | if (unlikely(ret)) | |
1147 | ttm_bo_unref(&bo); | |
ba4e7d97 TH |
1148 | |
1149 | return ret; | |
1150 | } | |
09855acb | 1151 | EXPORT_SYMBOL(ttm_bo_init); |
ba4e7d97 | 1152 | |
57de4ba9 JG |
1153 | size_t ttm_bo_acc_size(struct ttm_bo_device *bdev, |
1154 | unsigned long bo_size, | |
1155 | unsigned struct_size) | |
ba4e7d97 | 1156 | { |
57de4ba9 JG |
1157 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; |
1158 | size_t size = 0; | |
ba4e7d97 | 1159 | |
57de4ba9 JG |
1160 | size += ttm_round_pot(struct_size); |
1161 | size += PAGE_ALIGN(npages * sizeof(void *)); | |
1162 | size += ttm_round_pot(sizeof(struct ttm_tt)); | |
1163 | return size; | |
ba4e7d97 | 1164 | } |
57de4ba9 JG |
1165 | EXPORT_SYMBOL(ttm_bo_acc_size); |
1166 | ||
1167 | size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, | |
1168 | unsigned long bo_size, | |
1169 | unsigned struct_size) | |
1170 | { | |
1171 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; | |
1172 | size_t size = 0; | |
1173 | ||
1174 | size += ttm_round_pot(struct_size); | |
1175 | size += PAGE_ALIGN(npages * sizeof(void *)); | |
1176 | size += PAGE_ALIGN(npages * sizeof(dma_addr_t)); | |
1177 | size += ttm_round_pot(sizeof(struct ttm_dma_tt)); | |
1178 | return size; | |
1179 | } | |
1180 | EXPORT_SYMBOL(ttm_bo_dma_acc_size); | |
ba4e7d97 | 1181 | |
09855acb JG |
1182 | int ttm_bo_create(struct ttm_bo_device *bdev, |
1183 | unsigned long size, | |
1184 | enum ttm_bo_type type, | |
1185 | struct ttm_placement *placement, | |
1186 | uint32_t page_alignment, | |
09855acb | 1187 | bool interruptible, |
5df23979 | 1188 | struct file *persistent_swap_storage, |
09855acb | 1189 | struct ttm_buffer_object **p_bo) |
ba4e7d97 TH |
1190 | { |
1191 | struct ttm_buffer_object *bo; | |
57de4ba9 | 1192 | size_t acc_size; |
ca262a99 | 1193 | int ret; |
ba4e7d97 | 1194 | |
ba4e7d97 | 1195 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
a393c730 | 1196 | if (unlikely(bo == NULL)) |
ba4e7d97 | 1197 | return -ENOMEM; |
ba4e7d97 | 1198 | |
a393c730 | 1199 | acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object)); |
09855acb | 1200 | ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment, |
0b91c4a1 MS |
1201 | interruptible, persistent_swap_storage, acc_size, |
1202 | NULL, NULL); | |
ba4e7d97 TH |
1203 | if (likely(ret == 0)) |
1204 | *p_bo = bo; | |
1205 | ||
1206 | return ret; | |
1207 | } | |
4d798937 | 1208 | EXPORT_SYMBOL(ttm_bo_create); |
ba4e7d97 | 1209 | |
ba4e7d97 | 1210 | static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, |
ca262a99 | 1211 | unsigned mem_type, bool allow_errors) |
ba4e7d97 | 1212 | { |
ca262a99 | 1213 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
a987fcaa | 1214 | struct ttm_bo_global *glob = bdev->glob; |
ba4e7d97 | 1215 | int ret; |
ba4e7d97 TH |
1216 | |
1217 | /* | |
1218 | * Can't use standard list traversal since we're unlocking. | |
1219 | */ | |
1220 | ||
a987fcaa | 1221 | spin_lock(&glob->lru_lock); |
ca262a99 | 1222 | while (!list_empty(&man->lru)) { |
a987fcaa | 1223 | spin_unlock(&glob->lru_lock); |
97a875cb | 1224 | ret = ttm_mem_evict_first(bdev, mem_type, false, false); |
ca262a99 JG |
1225 | if (ret) { |
1226 | if (allow_errors) { | |
1227 | return ret; | |
1228 | } else { | |
25d0479a | 1229 | pr_err("Cleanup eviction failed\n"); |
ca262a99 JG |
1230 | } |
1231 | } | |
a987fcaa | 1232 | spin_lock(&glob->lru_lock); |
ba4e7d97 | 1233 | } |
a987fcaa | 1234 | spin_unlock(&glob->lru_lock); |
ba4e7d97 TH |
1235 | return 0; |
1236 | } | |
1237 | ||
1238 | int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type) | |
1239 | { | |
c96e7c7a | 1240 | struct ttm_mem_type_manager *man; |
ba4e7d97 TH |
1241 | int ret = -EINVAL; |
1242 | ||
1243 | if (mem_type >= TTM_NUM_MEM_TYPES) { | |
25d0479a | 1244 | pr_err("Illegal memory type %d\n", mem_type); |
ba4e7d97 TH |
1245 | return ret; |
1246 | } | |
c96e7c7a | 1247 | man = &bdev->man[mem_type]; |
ba4e7d97 TH |
1248 | |
1249 | if (!man->has_type) { | |
25d0479a JP |
1250 | pr_err("Trying to take down uninitialized memory manager type %u\n", |
1251 | mem_type); | |
ba4e7d97 TH |
1252 | return ret; |
1253 | } | |
1254 | ||
1255 | man->use_type = false; | |
1256 | man->has_type = false; | |
1257 | ||
1258 | ret = 0; | |
1259 | if (mem_type > 0) { | |
ca262a99 | 1260 | ttm_bo_force_list_clean(bdev, mem_type, false); |
ba4e7d97 | 1261 | |
d961db75 | 1262 | ret = (*man->func->takedown)(man); |
ba4e7d97 TH |
1263 | } |
1264 | ||
1265 | return ret; | |
1266 | } | |
1267 | EXPORT_SYMBOL(ttm_bo_clean_mm); | |
1268 | ||
1269 | int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type) | |
1270 | { | |
1271 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; | |
1272 | ||
1273 | if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) { | |
25d0479a | 1274 | pr_err("Illegal memory manager memory type %u\n", mem_type); |
ba4e7d97 TH |
1275 | return -EINVAL; |
1276 | } | |
1277 | ||
1278 | if (!man->has_type) { | |
25d0479a | 1279 | pr_err("Memory type %u has not been initialized\n", mem_type); |
ba4e7d97 TH |
1280 | return 0; |
1281 | } | |
1282 | ||
ca262a99 | 1283 | return ttm_bo_force_list_clean(bdev, mem_type, true); |
ba4e7d97 TH |
1284 | } |
1285 | EXPORT_SYMBOL(ttm_bo_evict_mm); | |
1286 | ||
1287 | int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, | |
ca262a99 | 1288 | unsigned long p_size) |
ba4e7d97 TH |
1289 | { |
1290 | int ret = -EINVAL; | |
1291 | struct ttm_mem_type_manager *man; | |
1292 | ||
dbc4a5b8 | 1293 | BUG_ON(type >= TTM_NUM_MEM_TYPES); |
ba4e7d97 | 1294 | man = &bdev->man[type]; |
dbc4a5b8 | 1295 | BUG_ON(man->has_type); |
eba67093 TH |
1296 | man->io_reserve_fastpath = true; |
1297 | man->use_io_reserve_lru = false; | |
1298 | mutex_init(&man->io_reserve_mutex); | |
1299 | INIT_LIST_HEAD(&man->io_reserve_lru); | |
ba4e7d97 TH |
1300 | |
1301 | ret = bdev->driver->init_mem_type(bdev, type, man); | |
1302 | if (ret) | |
1303 | return ret; | |
d961db75 | 1304 | man->bdev = bdev; |
ba4e7d97 TH |
1305 | |
1306 | ret = 0; | |
1307 | if (type != TTM_PL_SYSTEM) { | |
d961db75 | 1308 | ret = (*man->func->init)(man, p_size); |
ba4e7d97 TH |
1309 | if (ret) |
1310 | return ret; | |
1311 | } | |
1312 | man->has_type = true; | |
1313 | man->use_type = true; | |
1314 | man->size = p_size; | |
1315 | ||
1316 | INIT_LIST_HEAD(&man->lru); | |
1317 | ||
1318 | return 0; | |
1319 | } | |
1320 | EXPORT_SYMBOL(ttm_bo_init_mm); | |
1321 | ||
a987fcaa TH |
1322 | static void ttm_bo_global_kobj_release(struct kobject *kobj) |
1323 | { | |
1324 | struct ttm_bo_global *glob = | |
1325 | container_of(kobj, struct ttm_bo_global, kobj); | |
1326 | ||
a987fcaa TH |
1327 | ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink); |
1328 | __free_page(glob->dummy_read_page); | |
1329 | kfree(glob); | |
1330 | } | |
1331 | ||
ba4420c2 | 1332 | void ttm_bo_global_release(struct drm_global_reference *ref) |
a987fcaa TH |
1333 | { |
1334 | struct ttm_bo_global *glob = ref->object; | |
1335 | ||
1336 | kobject_del(&glob->kobj); | |
1337 | kobject_put(&glob->kobj); | |
1338 | } | |
1339 | EXPORT_SYMBOL(ttm_bo_global_release); | |
1340 | ||
ba4420c2 | 1341 | int ttm_bo_global_init(struct drm_global_reference *ref) |
a987fcaa TH |
1342 | { |
1343 | struct ttm_bo_global_ref *bo_ref = | |
1344 | container_of(ref, struct ttm_bo_global_ref, ref); | |
1345 | struct ttm_bo_global *glob = ref->object; | |
1346 | int ret; | |
1347 | ||
1348 | mutex_init(&glob->device_list_mutex); | |
1349 | spin_lock_init(&glob->lru_lock); | |
1350 | glob->mem_glob = bo_ref->mem_glob; | |
1351 | glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); | |
1352 | ||
1353 | if (unlikely(glob->dummy_read_page == NULL)) { | |
1354 | ret = -ENOMEM; | |
1355 | goto out_no_drp; | |
1356 | } | |
1357 | ||
1358 | INIT_LIST_HEAD(&glob->swap_lru); | |
1359 | INIT_LIST_HEAD(&glob->device_list); | |
1360 | ||
1361 | ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout); | |
1362 | ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink); | |
1363 | if (unlikely(ret != 0)) { | |
25d0479a | 1364 | pr_err("Could not register buffer object swapout\n"); |
a987fcaa TH |
1365 | goto out_no_shrink; |
1366 | } | |
1367 | ||
a987fcaa TH |
1368 | atomic_set(&glob->bo_count, 0); |
1369 | ||
b642ed06 RD |
1370 | ret = kobject_init_and_add( |
1371 | &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects"); | |
a987fcaa TH |
1372 | if (unlikely(ret != 0)) |
1373 | kobject_put(&glob->kobj); | |
1374 | return ret; | |
1375 | out_no_shrink: | |
1376 | __free_page(glob->dummy_read_page); | |
1377 | out_no_drp: | |
1378 | kfree(glob); | |
1379 | return ret; | |
1380 | } | |
1381 | EXPORT_SYMBOL(ttm_bo_global_init); | |
1382 | ||
1383 | ||
ba4e7d97 TH |
1384 | int ttm_bo_device_release(struct ttm_bo_device *bdev) |
1385 | { | |
1386 | int ret = 0; | |
1387 | unsigned i = TTM_NUM_MEM_TYPES; | |
1388 | struct ttm_mem_type_manager *man; | |
a987fcaa | 1389 | struct ttm_bo_global *glob = bdev->glob; |
ba4e7d97 TH |
1390 | |
1391 | while (i--) { | |
1392 | man = &bdev->man[i]; | |
1393 | if (man->has_type) { | |
1394 | man->use_type = false; | |
1395 | if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) { | |
1396 | ret = -EBUSY; | |
25d0479a JP |
1397 | pr_err("DRM memory manager type %d is not clean\n", |
1398 | i); | |
ba4e7d97 TH |
1399 | } |
1400 | man->has_type = false; | |
1401 | } | |
1402 | } | |
1403 | ||
a987fcaa TH |
1404 | mutex_lock(&glob->device_list_mutex); |
1405 | list_del(&bdev->device_list); | |
1406 | mutex_unlock(&glob->device_list_mutex); | |
1407 | ||
f094cfc6 | 1408 | cancel_delayed_work_sync(&bdev->wq); |
ba4e7d97 TH |
1409 | |
1410 | while (ttm_bo_delayed_delete(bdev, true)) | |
1411 | ; | |
1412 | ||
a987fcaa | 1413 | spin_lock(&glob->lru_lock); |
ba4e7d97 TH |
1414 | if (list_empty(&bdev->ddestroy)) |
1415 | TTM_DEBUG("Delayed destroy list was clean\n"); | |
1416 | ||
1417 | if (list_empty(&bdev->man[0].lru)) | |
1418 | TTM_DEBUG("Swap list was clean\n"); | |
a987fcaa | 1419 | spin_unlock(&glob->lru_lock); |
ba4e7d97 | 1420 | |
72525b3f | 1421 | drm_vma_offset_manager_destroy(&bdev->vma_manager); |
ba4e7d97 | 1422 | |
ba4e7d97 TH |
1423 | return ret; |
1424 | } | |
1425 | EXPORT_SYMBOL(ttm_bo_device_release); | |
1426 | ||
ba4e7d97 | 1427 | int ttm_bo_device_init(struct ttm_bo_device *bdev, |
a987fcaa TH |
1428 | struct ttm_bo_global *glob, |
1429 | struct ttm_bo_driver *driver, | |
44d847b7 | 1430 | struct address_space *mapping, |
51c8b407 | 1431 | uint64_t file_page_offset, |
ad49f501 | 1432 | bool need_dma32) |
ba4e7d97 TH |
1433 | { |
1434 | int ret = -EINVAL; | |
1435 | ||
ba4e7d97 | 1436 | bdev->driver = driver; |
ba4e7d97 TH |
1437 | |
1438 | memset(bdev->man, 0, sizeof(bdev->man)); | |
1439 | ||
ba4e7d97 TH |
1440 | /* |
1441 | * Initialize the system memory buffer type. | |
1442 | * Other types need to be driver / IOCTL initialized. | |
1443 | */ | |
ca262a99 | 1444 | ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0); |
ba4e7d97 | 1445 | if (unlikely(ret != 0)) |
a987fcaa | 1446 | goto out_no_sys; |
ba4e7d97 | 1447 | |
72525b3f DH |
1448 | drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset, |
1449 | 0x10000000); | |
ba4e7d97 | 1450 | INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); |
ba4e7d97 | 1451 | INIT_LIST_HEAD(&bdev->ddestroy); |
44d847b7 | 1452 | bdev->dev_mapping = mapping; |
a987fcaa | 1453 | bdev->glob = glob; |
ad49f501 | 1454 | bdev->need_dma32 = need_dma32; |
65705962 | 1455 | bdev->val_seq = 0; |
a987fcaa TH |
1456 | mutex_lock(&glob->device_list_mutex); |
1457 | list_add_tail(&bdev->device_list, &glob->device_list); | |
1458 | mutex_unlock(&glob->device_list_mutex); | |
ba4e7d97 TH |
1459 | |
1460 | return 0; | |
a987fcaa | 1461 | out_no_sys: |
ba4e7d97 TH |
1462 | return ret; |
1463 | } | |
1464 | EXPORT_SYMBOL(ttm_bo_device_init); | |
1465 | ||
1466 | /* | |
1467 | * buffer object vm functions. | |
1468 | */ | |
1469 | ||
1470 | bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) | |
1471 | { | |
1472 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; | |
1473 | ||
1474 | if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) { | |
1475 | if (mem->mem_type == TTM_PL_SYSTEM) | |
1476 | return false; | |
1477 | ||
1478 | if (man->flags & TTM_MEMTYPE_FLAG_CMA) | |
1479 | return false; | |
1480 | ||
1481 | if (mem->placement & TTM_PL_FLAG_CACHED) | |
1482 | return false; | |
1483 | } | |
1484 | return true; | |
1485 | } | |
1486 | ||
eba67093 | 1487 | void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo) |
ba4e7d97 TH |
1488 | { |
1489 | struct ttm_bo_device *bdev = bo->bdev; | |
ba4e7d97 | 1490 | |
51335df9 | 1491 | drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping); |
eba67093 | 1492 | ttm_mem_io_free_vm(bo); |
ba4e7d97 | 1493 | } |
eba67093 TH |
1494 | |
1495 | void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) | |
1496 | { | |
1497 | struct ttm_bo_device *bdev = bo->bdev; | |
1498 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; | |
1499 | ||
1500 | ttm_mem_io_lock(man, false); | |
1501 | ttm_bo_unmap_virtual_locked(bo); | |
1502 | ttm_mem_io_unlock(man); | |
ba4e7d97 | 1503 | } |
eba67093 TH |
1504 | |
1505 | ||
e024e110 | 1506 | EXPORT_SYMBOL(ttm_bo_unmap_virtual); |
ba4e7d97 | 1507 | |
ba4e7d97 | 1508 | int ttm_bo_wait(struct ttm_buffer_object *bo, |
1717c0e2 | 1509 | bool lazy, bool interruptible, bool no_wait) |
ba4e7d97 | 1510 | { |
f2c24b83 ML |
1511 | struct reservation_object_list *fobj; |
1512 | struct reservation_object *resv; | |
1513 | struct fence *excl; | |
1514 | long timeout = 15 * HZ; | |
1515 | int i; | |
7040138f | 1516 | |
f2c24b83 ML |
1517 | resv = bo->resv; |
1518 | fobj = reservation_object_get_list(resv); | |
1519 | excl = reservation_object_get_excl(resv); | |
1520 | if (excl) { | |
1521 | if (!fence_is_signaled(excl)) { | |
1522 | if (no_wait) | |
1523 | return -EBUSY; | |
ba4e7d97 | 1524 | |
f2c24b83 ML |
1525 | timeout = fence_wait_timeout(excl, |
1526 | interruptible, timeout); | |
ba4e7d97 | 1527 | } |
f2c24b83 | 1528 | } |
ba4e7d97 | 1529 | |
f2c24b83 ML |
1530 | for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) { |
1531 | struct fence *fence; | |
1532 | fence = rcu_dereference_protected(fobj->shared[i], | |
1533 | reservation_object_held(resv)); | |
ba4e7d97 | 1534 | |
f2c24b83 ML |
1535 | if (!fence_is_signaled(fence)) { |
1536 | if (no_wait) | |
1537 | return -EBUSY; | |
dd7cfd64 | 1538 | |
f2c24b83 ML |
1539 | timeout = fence_wait_timeout(fence, |
1540 | interruptible, timeout); | |
ba4e7d97 TH |
1541 | } |
1542 | } | |
f2c24b83 ML |
1543 | |
1544 | if (timeout < 0) | |
1545 | return timeout; | |
1546 | ||
1547 | if (timeout == 0) | |
1548 | return -EBUSY; | |
1549 | ||
1550 | reservation_object_add_excl_fence(resv, NULL); | |
1551 | clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags); | |
1552 | return 0; | |
ba4e7d97 TH |
1553 | } |
1554 | EXPORT_SYMBOL(ttm_bo_wait); | |
1555 | ||
ba4e7d97 TH |
1556 | int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait) |
1557 | { | |
1558 | int ret = 0; | |
1559 | ||
1560 | /* | |
8cfe92d6 | 1561 | * Using ttm_bo_reserve makes sure the lru lists are updated. |
ba4e7d97 TH |
1562 | */ |
1563 | ||
0eff2a24 | 1564 | ret = ttm_bo_reserve(bo, true, no_wait, false, NULL); |
ba4e7d97 TH |
1565 | if (unlikely(ret != 0)) |
1566 | return ret; | |
1717c0e2 | 1567 | ret = ttm_bo_wait(bo, false, true, no_wait); |
ba4e7d97 TH |
1568 | if (likely(ret == 0)) |
1569 | atomic_inc(&bo->cpu_writers); | |
1570 | ttm_bo_unreserve(bo); | |
1571 | return ret; | |
1572 | } | |
d1ede145 | 1573 | EXPORT_SYMBOL(ttm_bo_synccpu_write_grab); |
ba4e7d97 TH |
1574 | |
1575 | void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo) | |
1576 | { | |
654aa792 | 1577 | atomic_dec(&bo->cpu_writers); |
ba4e7d97 | 1578 | } |
d1ede145 | 1579 | EXPORT_SYMBOL(ttm_bo_synccpu_write_release); |
ba4e7d97 TH |
1580 | |
1581 | /** | |
1582 | * A buffer object shrink method that tries to swap out the first | |
1583 | * buffer object on the bo_global::swap_lru list. | |
1584 | */ | |
1585 | ||
1586 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink) | |
1587 | { | |
a987fcaa TH |
1588 | struct ttm_bo_global *glob = |
1589 | container_of(shrink, struct ttm_bo_global, shrink); | |
ba4e7d97 TH |
1590 | struct ttm_buffer_object *bo; |
1591 | int ret = -EBUSY; | |
1592 | int put_count; | |
1593 | uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM); | |
1594 | ||
a987fcaa | 1595 | spin_lock(&glob->lru_lock); |
2b7b3ad2 | 1596 | list_for_each_entry(bo, &glob->swap_lru, swap) { |
0eff2a24 | 1597 | ret = __ttm_bo_reserve(bo, false, true, false, NULL); |
2b7b3ad2 ML |
1598 | if (!ret) |
1599 | break; | |
1600 | } | |
85b144f8 | 1601 | |
2b7b3ad2 ML |
1602 | if (ret) { |
1603 | spin_unlock(&glob->lru_lock); | |
1604 | return ret; | |
1605 | } | |
e1efc9b6 | 1606 | |
2b7b3ad2 | 1607 | kref_get(&bo->list_kref); |
ba4e7d97 | 1608 | |
2b7b3ad2 ML |
1609 | if (!list_empty(&bo->ddestroy)) { |
1610 | ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false); | |
1611 | kref_put(&bo->list_kref, ttm_bo_release_list); | |
1612 | return ret; | |
ba4e7d97 TH |
1613 | } |
1614 | ||
ba4e7d97 | 1615 | put_count = ttm_bo_del_from_lru(bo); |
a987fcaa | 1616 | spin_unlock(&glob->lru_lock); |
ba4e7d97 | 1617 | |
d6ea8886 | 1618 | ttm_bo_list_ref_sub(bo, put_count, true); |
ba4e7d97 TH |
1619 | |
1620 | /** | |
1621 | * Wait for GPU, then move to system cached. | |
1622 | */ | |
1623 | ||
1717c0e2 | 1624 | ret = ttm_bo_wait(bo, false, false, false); |
ba4e7d97 TH |
1625 | |
1626 | if (unlikely(ret != 0)) | |
1627 | goto out; | |
1628 | ||
1629 | if ((bo->mem.placement & swap_placement) != swap_placement) { | |
1630 | struct ttm_mem_reg evict_mem; | |
1631 | ||
1632 | evict_mem = bo->mem; | |
1633 | evict_mem.mm_node = NULL; | |
1634 | evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; | |
1635 | evict_mem.mem_type = TTM_PL_SYSTEM; | |
1636 | ||
1637 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, | |
97a875cb | 1638 | false, false); |
ba4e7d97 TH |
1639 | if (unlikely(ret != 0)) |
1640 | goto out; | |
1641 | } | |
1642 | ||
1643 | ttm_bo_unmap_virtual(bo); | |
1644 | ||
1645 | /** | |
1646 | * Swap out. Buffer will be swapped in again as soon as | |
1647 | * anyone tries to access a ttm page. | |
1648 | */ | |
1649 | ||
3f09ea4e TH |
1650 | if (bo->bdev->driver->swap_notify) |
1651 | bo->bdev->driver->swap_notify(bo); | |
1652 | ||
5df23979 | 1653 | ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage); |
ba4e7d97 TH |
1654 | out: |
1655 | ||
1656 | /** | |
1657 | * | |
1658 | * Unreserve without putting on LRU to avoid swapping out an | |
1659 | * already swapped buffer. | |
1660 | */ | |
1661 | ||
c7523083 | 1662 | __ttm_bo_unreserve(bo); |
ba4e7d97 TH |
1663 | kref_put(&bo->list_kref, ttm_bo_release_list); |
1664 | return ret; | |
1665 | } | |
1666 | ||
1667 | void ttm_bo_swapout_all(struct ttm_bo_device *bdev) | |
1668 | { | |
a987fcaa | 1669 | while (ttm_bo_swapout(&bdev->glob->shrink) == 0) |
ba4e7d97 TH |
1670 | ; |
1671 | } | |
e99e1e78 | 1672 | EXPORT_SYMBOL(ttm_bo_swapout_all); |
c58f009e TH |
1673 | |
1674 | /** | |
1675 | * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become | |
1676 | * unreserved | |
1677 | * | |
1678 | * @bo: Pointer to buffer | |
1679 | */ | |
1680 | int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo) | |
1681 | { | |
1682 | int ret; | |
1683 | ||
1684 | /* | |
1685 | * In the absense of a wait_unlocked API, | |
1686 | * Use the bo::wu_mutex to avoid triggering livelocks due to | |
1687 | * concurrent use of this function. Note that this use of | |
1688 | * bo::wu_mutex can go away if we change locking order to | |
1689 | * mmap_sem -> bo::reserve. | |
1690 | */ | |
1691 | ret = mutex_lock_interruptible(&bo->wu_mutex); | |
1692 | if (unlikely(ret != 0)) | |
1693 | return -ERESTARTSYS; | |
1694 | if (!ww_mutex_is_locked(&bo->resv->lock)) | |
1695 | goto out_unlock; | |
c7523083 | 1696 | ret = __ttm_bo_reserve(bo, true, false, false, NULL); |
c58f009e TH |
1697 | if (unlikely(ret != 0)) |
1698 | goto out_unlock; | |
c7523083 | 1699 | __ttm_bo_unreserve(bo); |
c58f009e TH |
1700 | |
1701 | out_unlock: | |
1702 | mutex_unlock(&bo->wu_mutex); | |
1703 | return ret; | |
1704 | } |