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