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