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