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