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