blob: 640fb265dd5a6f7e3f619efc52bd607e37cca811 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
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 */
Jerome Glisseca262a9992009-12-08 15:33:32 +010030/* Notes:
31 *
32 * We store bo pointer in drm_mm_node struct so we know which bo own a
33 * specific node. There is no protection on the pointer, thus to make
34 * sure things don't go berserk you have to access this pointer while
35 * holding the global lru lock and make sure anytime you free a node you
36 * reset the pointer to NULL.
37 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020038
39#include "ttm/ttm_module.h"
40#include "ttm/ttm_bo_driver.h"
41#include "ttm/ttm_placement.h"
42#include <linux/jiffies.h>
43#include <linux/slab.h>
44#include <linux/sched.h>
45#include <linux/mm.h>
46#include <linux/file.h>
47#include <linux/module.h>
48
49#define TTM_ASSERT_LOCKED(param)
50#define TTM_DEBUG(fmt, arg...)
51#define TTM_BO_HASH_ORDER 13
52
53static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020054static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020055static void ttm_bo_global_kobj_release(struct kobject *kobj);
56
57static struct attribute ttm_bo_count = {
58 .name = "bo_count",
59 .mode = S_IRUGO
60};
61
62static ssize_t ttm_bo_global_show(struct kobject *kobj,
63 struct attribute *attr,
64 char *buffer)
65{
66 struct ttm_bo_global *glob =
67 container_of(kobj, struct ttm_bo_global, kobj);
68
69 return snprintf(buffer, PAGE_SIZE, "%lu\n",
70 (unsigned long) atomic_read(&glob->bo_count));
71}
72
73static struct attribute *ttm_bo_global_attrs[] = {
74 &ttm_bo_count,
75 NULL
76};
77
78static struct sysfs_ops ttm_bo_global_ops = {
79 .show = &ttm_bo_global_show
80};
81
82static struct kobj_type ttm_bo_glob_kobj_type = {
83 .release = &ttm_bo_global_kobj_release,
84 .sysfs_ops = &ttm_bo_global_ops,
85 .default_attrs = ttm_bo_global_attrs
86};
87
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020088
89static inline uint32_t ttm_bo_type_flags(unsigned type)
90{
91 return 1 << (type);
92}
93
94static void ttm_bo_release_list(struct kref *list_kref)
95{
96 struct ttm_buffer_object *bo =
97 container_of(list_kref, struct ttm_buffer_object, list_kref);
98 struct ttm_bo_device *bdev = bo->bdev;
99
100 BUG_ON(atomic_read(&bo->list_kref.refcount));
101 BUG_ON(atomic_read(&bo->kref.refcount));
102 BUG_ON(atomic_read(&bo->cpu_writers));
103 BUG_ON(bo->sync_obj != NULL);
104 BUG_ON(bo->mem.mm_node != NULL);
105 BUG_ON(!list_empty(&bo->lru));
106 BUG_ON(!list_empty(&bo->ddestroy));
107
108 if (bo->ttm)
109 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200110 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200111 if (bo->destroy)
112 bo->destroy(bo);
113 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200114 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200115 kfree(bo);
116 }
117}
118
119int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
120{
121
122 if (interruptible) {
123 int ret = 0;
124
125 ret = wait_event_interruptible(bo->event_queue,
126 atomic_read(&bo->reserved) == 0);
127 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100128 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200129 } else {
130 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
131 }
132 return 0;
133}
134
135static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
136{
137 struct ttm_bo_device *bdev = bo->bdev;
138 struct ttm_mem_type_manager *man;
139
140 BUG_ON(!atomic_read(&bo->reserved));
141
142 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
143
144 BUG_ON(!list_empty(&bo->lru));
145
146 man = &bdev->man[bo->mem.mem_type];
147 list_add_tail(&bo->lru, &man->lru);
148 kref_get(&bo->list_kref);
149
150 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200151 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200152 kref_get(&bo->list_kref);
153 }
154 }
155}
156
157/**
158 * Call with the lru_lock held.
159 */
160
161static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
162{
163 int put_count = 0;
164
165 if (!list_empty(&bo->swap)) {
166 list_del_init(&bo->swap);
167 ++put_count;
168 }
169 if (!list_empty(&bo->lru)) {
170 list_del_init(&bo->lru);
171 ++put_count;
172 }
173
174 /*
175 * TODO: Add a driver hook to delete from
176 * driver-specific LRU's here.
177 */
178
179 return put_count;
180}
181
182int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
183 bool interruptible,
184 bool no_wait, bool use_sequence, uint32_t sequence)
185{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200186 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200187 int ret;
188
189 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
190 if (use_sequence && bo->seq_valid &&
191 (sequence - bo->val_seq < (1 << 31))) {
192 return -EAGAIN;
193 }
194
195 if (no_wait)
196 return -EBUSY;
197
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200198 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200199 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200200 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200201
202 if (unlikely(ret))
203 return ret;
204 }
205
206 if (use_sequence) {
207 bo->val_seq = sequence;
208 bo->seq_valid = true;
209 } else {
210 bo->seq_valid = false;
211 }
212
213 return 0;
214}
215EXPORT_SYMBOL(ttm_bo_reserve);
216
217static void ttm_bo_ref_bug(struct kref *list_kref)
218{
219 BUG();
220}
221
222int ttm_bo_reserve(struct ttm_buffer_object *bo,
223 bool interruptible,
224 bool no_wait, bool use_sequence, uint32_t sequence)
225{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200226 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200227 int put_count = 0;
228 int ret;
229
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200230 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200231 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
232 sequence);
233 if (likely(ret == 0))
234 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200235 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200236
237 while (put_count--)
238 kref_put(&bo->list_kref, ttm_bo_ref_bug);
239
240 return ret;
241}
242
243void ttm_bo_unreserve(struct ttm_buffer_object *bo)
244{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200245 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200246
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200247 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200248 ttm_bo_add_to_lru(bo);
249 atomic_set(&bo->reserved, 0);
250 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200251 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200252}
253EXPORT_SYMBOL(ttm_bo_unreserve);
254
255/*
256 * Call bo->mutex locked.
257 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200258static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
259{
260 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200261 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200262 int ret = 0;
263 uint32_t page_flags = 0;
264
265 TTM_ASSERT_LOCKED(&bo->mutex);
266 bo->ttm = NULL;
267
Dave Airliead49f502009-07-10 22:36:26 +1000268 if (bdev->need_dma32)
269 page_flags |= TTM_PAGE_FLAG_DMA32;
270
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200271 switch (bo->type) {
272 case ttm_bo_type_device:
273 if (zero_alloc)
274 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
275 case ttm_bo_type_kernel:
276 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200277 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200278 if (unlikely(bo->ttm == NULL))
279 ret = -ENOMEM;
280 break;
281 case ttm_bo_type_user:
282 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
283 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200284 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000285 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200286 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000287 break;
288 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200289
290 ret = ttm_tt_set_user(bo->ttm, current,
291 bo->buffer_start, bo->num_pages);
292 if (unlikely(ret != 0))
293 ttm_tt_destroy(bo->ttm);
294 break;
295 default:
296 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
297 ret = -EINVAL;
298 break;
299 }
300
301 return ret;
302}
303
304static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
305 struct ttm_mem_reg *mem,
306 bool evict, bool interruptible, bool no_wait)
307{
308 struct ttm_bo_device *bdev = bo->bdev;
309 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
310 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
311 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
312 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
313 int ret = 0;
314
315 if (old_is_pci || new_is_pci ||
316 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
317 ttm_bo_unmap_virtual(bo);
318
319 /*
320 * Create and bind a ttm if required.
321 */
322
323 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
324 ret = ttm_bo_add_ttm(bo, false);
325 if (ret)
326 goto out_err;
327
328 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
329 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200330 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200331
332 if (mem->mem_type != TTM_PL_SYSTEM) {
333 ret = ttm_tt_bind(bo->ttm, mem);
334 if (ret)
335 goto out_err;
336 }
337
338 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100339 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200340 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200341 goto moved;
342 }
343
344 }
345
Dave Airliee024e112009-06-24 09:48:08 +1000346 if (bdev->driver->move_notify)
347 bdev->driver->move_notify(bo, mem);
348
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200349 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
350 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
351 ret = ttm_bo_move_ttm(bo, evict, no_wait, mem);
352 else if (bdev->driver->move)
353 ret = bdev->driver->move(bo, evict, interruptible,
354 no_wait, mem);
355 else
356 ret = ttm_bo_move_memcpy(bo, evict, no_wait, mem);
357
358 if (ret)
359 goto out_err;
360
361moved:
362 if (bo->evicted) {
363 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
364 if (ret)
365 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
366 bo->evicted = false;
367 }
368
369 if (bo->mem.mm_node) {
370 spin_lock(&bo->lock);
371 bo->offset = (bo->mem.mm_node->start << PAGE_SHIFT) +
372 bdev->man[bo->mem.mem_type].gpu_offset;
373 bo->cur_placement = bo->mem.placement;
374 spin_unlock(&bo->lock);
375 }
376
377 return 0;
378
379out_err:
380 new_man = &bdev->man[bo->mem.mem_type];
381 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
382 ttm_tt_unbind(bo->ttm);
383 ttm_tt_destroy(bo->ttm);
384 bo->ttm = NULL;
385 }
386
387 return ret;
388}
389
390/**
391 * If bo idle, remove from delayed- and lru lists, and unref.
392 * If not idle, and already on delayed list, do nothing.
393 * If not idle, and not on delayed list, put on delayed list,
394 * up the list_kref and schedule a delayed list check.
395 */
396
397static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
398{
399 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200400 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200401 struct ttm_bo_driver *driver = bdev->driver;
402 int ret;
403
404 spin_lock(&bo->lock);
405 (void) ttm_bo_wait(bo, false, false, !remove_all);
406
407 if (!bo->sync_obj) {
408 int put_count;
409
410 spin_unlock(&bo->lock);
411
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200412 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200413 ret = ttm_bo_reserve_locked(bo, false, false, false, 0);
414 BUG_ON(ret);
415 if (bo->ttm)
416 ttm_tt_unbind(bo->ttm);
417
418 if (!list_empty(&bo->ddestroy)) {
419 list_del_init(&bo->ddestroy);
420 kref_put(&bo->list_kref, ttm_bo_ref_bug);
421 }
422 if (bo->mem.mm_node) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100423 bo->mem.mm_node->private = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200424 drm_mm_put_block(bo->mem.mm_node);
425 bo->mem.mm_node = NULL;
426 }
427 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200428 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200429
430 atomic_set(&bo->reserved, 0);
431
432 while (put_count--)
433 kref_put(&bo->list_kref, ttm_bo_release_list);
434
435 return 0;
436 }
437
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200438 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200439 if (list_empty(&bo->ddestroy)) {
440 void *sync_obj = bo->sync_obj;
441 void *sync_obj_arg = bo->sync_obj_arg;
442
443 kref_get(&bo->list_kref);
444 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200445 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200446 spin_unlock(&bo->lock);
447
448 if (sync_obj)
449 driver->sync_obj_flush(sync_obj, sync_obj_arg);
450 schedule_delayed_work(&bdev->wq,
451 ((HZ / 100) < 1) ? 1 : HZ / 100);
452 ret = 0;
453
454 } else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200455 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200456 spin_unlock(&bo->lock);
457 ret = -EBUSY;
458 }
459
460 return ret;
461}
462
463/**
464 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
465 * encountered buffers.
466 */
467
468static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
469{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200470 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200471 struct ttm_buffer_object *entry, *nentry;
472 struct list_head *list, *next;
473 int ret;
474
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200475 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200476 list_for_each_safe(list, next, &bdev->ddestroy) {
477 entry = list_entry(list, struct ttm_buffer_object, ddestroy);
478 nentry = NULL;
479
480 /*
481 * Protect the next list entry from destruction while we
482 * unlock the lru_lock.
483 */
484
485 if (next != &bdev->ddestroy) {
486 nentry = list_entry(next, struct ttm_buffer_object,
487 ddestroy);
488 kref_get(&nentry->list_kref);
489 }
490 kref_get(&entry->list_kref);
491
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200492 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200493 ret = ttm_bo_cleanup_refs(entry, remove_all);
494 kref_put(&entry->list_kref, ttm_bo_release_list);
495
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200496 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200497 if (nentry) {
498 bool next_onlist = !list_empty(next);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200499 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200500 kref_put(&nentry->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200501 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200502 /*
503 * Someone might have raced us and removed the
504 * next entry from the list. We don't bother restarting
505 * list traversal.
506 */
507
508 if (!next_onlist)
509 break;
510 }
511 if (ret)
512 break;
513 }
514 ret = !list_empty(&bdev->ddestroy);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200515 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200516
517 return ret;
518}
519
520static void ttm_bo_delayed_workqueue(struct work_struct *work)
521{
522 struct ttm_bo_device *bdev =
523 container_of(work, struct ttm_bo_device, wq.work);
524
525 if (ttm_bo_delayed_delete(bdev, false)) {
526 schedule_delayed_work(&bdev->wq,
527 ((HZ / 100) < 1) ? 1 : HZ / 100);
528 }
529}
530
531static void ttm_bo_release(struct kref *kref)
532{
533 struct ttm_buffer_object *bo =
534 container_of(kref, struct ttm_buffer_object, kref);
535 struct ttm_bo_device *bdev = bo->bdev;
536
537 if (likely(bo->vm_node != NULL)) {
538 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
539 drm_mm_put_block(bo->vm_node);
540 bo->vm_node = NULL;
541 }
542 write_unlock(&bdev->vm_lock);
543 ttm_bo_cleanup_refs(bo, false);
544 kref_put(&bo->list_kref, ttm_bo_release_list);
545 write_lock(&bdev->vm_lock);
546}
547
548void ttm_bo_unref(struct ttm_buffer_object **p_bo)
549{
550 struct ttm_buffer_object *bo = *p_bo;
551 struct ttm_bo_device *bdev = bo->bdev;
552
553 *p_bo = NULL;
554 write_lock(&bdev->vm_lock);
555 kref_put(&bo->kref, ttm_bo_release);
556 write_unlock(&bdev->vm_lock);
557}
558EXPORT_SYMBOL(ttm_bo_unref);
559
Jerome Glisseca262a9992009-12-08 15:33:32 +0100560static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
561 bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200562{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200563 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200564 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200565 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100566 struct ttm_placement placement;
567 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200568
569 spin_lock(&bo->lock);
570 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
571 spin_unlock(&bo->lock);
572
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200573 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100574 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200575 printk(KERN_ERR TTM_PFX
576 "Failed to expire sync object before "
577 "buffer eviction.\n");
578 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200579 goto out;
580 }
581
582 BUG_ON(!atomic_read(&bo->reserved));
583
584 evict_mem = bo->mem;
585 evict_mem.mm_node = NULL;
586
Jerome Glisseca262a9992009-12-08 15:33:32 +0100587 bdev->driver->evict_flags(bo, &placement);
588 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
589 no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200590 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100591 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200592 printk(KERN_ERR TTM_PFX
593 "Failed to find memory space for "
594 "buffer 0x%p eviction.\n", bo);
595 goto out;
596 }
597
598 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
599 no_wait);
600 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100601 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200602 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +0100603 spin_lock(&glob->lru_lock);
604 if (evict_mem.mm_node) {
605 evict_mem.mm_node->private = NULL;
606 drm_mm_put_block(evict_mem.mm_node);
607 evict_mem.mm_node = NULL;
608 }
609 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200610 goto out;
611 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200612 bo->evicted = true;
613out:
614 return ret;
615}
616
Jerome Glisseca262a9992009-12-08 15:33:32 +0100617static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
618 uint32_t mem_type,
619 bool interruptible, bool no_wait)
620{
621 struct ttm_bo_global *glob = bdev->glob;
622 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
623 struct ttm_buffer_object *bo;
624 int ret, put_count = 0;
625
626 spin_lock(&glob->lru_lock);
627 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
628 kref_get(&bo->list_kref);
629 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, false, 0);
630 if (likely(ret == 0))
631 put_count = ttm_bo_del_from_lru(bo);
632 spin_unlock(&glob->lru_lock);
633 if (unlikely(ret != 0))
634 return ret;
635 while (put_count--)
636 kref_put(&bo->list_kref, ttm_bo_ref_bug);
637 ret = ttm_bo_evict(bo, interruptible, no_wait);
638 ttm_bo_unreserve(bo);
639 kref_put(&bo->list_kref, ttm_bo_release_list);
640 return ret;
641}
642
643static int ttm_bo_man_get_node(struct ttm_buffer_object *bo,
644 struct ttm_mem_type_manager *man,
645 struct ttm_placement *placement,
646 struct ttm_mem_reg *mem,
647 struct drm_mm_node **node)
648{
649 struct ttm_bo_global *glob = bo->glob;
650 unsigned long lpfn;
651 int ret;
652
653 lpfn = placement->lpfn;
654 if (!lpfn)
655 lpfn = man->size;
656 *node = NULL;
657 do {
658 ret = drm_mm_pre_get(&man->manager);
659 if (unlikely(ret))
660 return ret;
661
662 spin_lock(&glob->lru_lock);
663 *node = drm_mm_search_free_in_range(&man->manager,
664 mem->num_pages, mem->page_alignment,
665 placement->fpfn, lpfn, 1);
666 if (unlikely(*node == NULL)) {
667 spin_unlock(&glob->lru_lock);
668 return 0;
669 }
670 *node = drm_mm_get_block_atomic_range(*node, mem->num_pages,
671 mem->page_alignment,
672 placement->fpfn,
673 lpfn);
674 spin_unlock(&glob->lru_lock);
675 } while (*node == NULL);
676 return 0;
677}
678
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200679/**
680 * Repeatedly evict memory from the LRU for @mem_type until we create enough
681 * space, or we've evicted everything and there isn't enough space.
682 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100683static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
684 uint32_t mem_type,
685 struct ttm_placement *placement,
686 struct ttm_mem_reg *mem,
687 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200688{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100689 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200690 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200691 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Jerome Glisseca262a9992009-12-08 15:33:32 +0100692 struct drm_mm_node *node;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200693 int ret;
694
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200695 do {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100696 ret = ttm_bo_man_get_node(bo, man, placement, mem, &node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200697 if (unlikely(ret != 0))
698 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100699 if (node)
700 break;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200701 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100702 if (list_empty(&man->lru)) {
703 spin_unlock(&glob->lru_lock);
704 break;
705 }
706 spin_unlock(&glob->lru_lock);
707 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
708 no_wait);
709 if (unlikely(ret != 0))
710 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200711 } while (1);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100712 if (node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200713 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200714 mem->mm_node = node;
715 mem->mem_type = mem_type;
716 return 0;
717}
718
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200719static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
720 uint32_t cur_placement,
721 uint32_t proposed_placement)
722{
723 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
724 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
725
726 /**
727 * Keep current caching if possible.
728 */
729
730 if ((cur_placement & caching) != 0)
731 result |= (cur_placement & caching);
732 else if ((man->default_caching & caching) != 0)
733 result |= man->default_caching;
734 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
735 result |= TTM_PL_FLAG_CACHED;
736 else if ((TTM_PL_FLAG_WC & caching) != 0)
737 result |= TTM_PL_FLAG_WC;
738 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
739 result |= TTM_PL_FLAG_UNCACHED;
740
741 return result;
742}
743
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200744static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
745 bool disallow_fixed,
746 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200747 uint32_t proposed_placement,
748 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200749{
750 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
751
752 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
753 return false;
754
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200755 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200756 return false;
757
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200758 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200759 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200760
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200761 cur_flags |= (proposed_placement & man->available_caching);
762
763 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200764 return true;
765}
766
Jerome Glisseca262a9992009-12-08 15:33:32 +0100767static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
768{
769 int i;
770
771 for (i = 0; i <= TTM_PL_PRIV5; i++)
772 if (flags & (1 << i)) {
773 *mem_type = i;
774 return 0;
775 }
776 return -EINVAL;
777}
778
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200779/**
780 * Creates space for memory region @mem according to its type.
781 *
782 * This function first searches for free space in compatible memory types in
783 * the priority order defined by the driver. If free space isn't found, then
784 * ttm_bo_mem_force_space is attempted in priority order to evict and find
785 * space.
786 */
787int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100788 struct ttm_placement *placement,
789 struct ttm_mem_reg *mem,
790 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200791{
792 struct ttm_bo_device *bdev = bo->bdev;
793 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200794 uint32_t mem_type = TTM_PL_SYSTEM;
795 uint32_t cur_flags = 0;
796 bool type_found = false;
797 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100798 bool has_erestartsys = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200799 struct drm_mm_node *node = NULL;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100800 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200801
802 mem->mm_node = NULL;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100803 for (i = 0; i <= placement->num_placement; ++i) {
804 ret = ttm_mem_type_from_flags(placement->placement[i],
805 &mem_type);
806 if (ret)
807 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200808 man = &bdev->man[mem_type];
809
810 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100811 bo->type == ttm_bo_type_user,
812 mem_type,
813 placement->placement[i],
814 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200815
816 if (!type_ok)
817 continue;
818
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200819 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
820 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100821 /*
822 * Use the access and other non-mapping-related flag bits from
823 * the memory placement flags to the current flags
824 */
825 ttm_flag_masked(&cur_flags, placement->placement[i],
826 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200827
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200828 if (mem_type == TTM_PL_SYSTEM)
829 break;
830
831 if (man->has_type && man->use_type) {
832 type_found = true;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100833 ret = ttm_bo_man_get_node(bo, man, placement, mem,
834 &node);
835 if (unlikely(ret))
836 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200837 }
838 if (node)
839 break;
840 }
841
842 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) {
843 mem->mm_node = node;
844 mem->mem_type = mem_type;
845 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100846 if (node)
847 node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200848 return 0;
849 }
850
851 if (!type_found)
852 return -EINVAL;
853
Jerome Glisseca262a9992009-12-08 15:33:32 +0100854 for (i = 0; i <= placement->num_busy_placement; ++i) {
855 ret = ttm_mem_type_from_flags(placement->placement[i],
856 &mem_type);
857 if (ret)
858 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200859 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200860 if (!man->has_type)
861 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200862 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100863 bo->type == ttm_bo_type_user,
864 mem_type,
865 placement->placement[i],
866 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867 continue;
868
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200869 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
870 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100871 /*
872 * Use the access and other non-mapping-related flag bits from
873 * the memory placement flags to the current flags
874 */
875 ttm_flag_masked(&cur_flags, placement->placement[i],
876 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200877
Jerome Glisseca262a9992009-12-08 15:33:32 +0100878 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
879 interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880 if (ret == 0 && mem->mm_node) {
881 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100882 mem->mm_node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200883 return 0;
884 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100885 if (ret == -ERESTARTSYS)
886 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200887 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100888 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200889 return ret;
890}
891EXPORT_SYMBOL(ttm_bo_mem_space);
892
893int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
894{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200895 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
896 return -EBUSY;
897
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100898 return wait_event_interruptible(bo->event_queue,
899 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200900}
901
902int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100903 struct ttm_placement *placement,
904 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200905{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200906 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200907 int ret = 0;
908 struct ttm_mem_reg mem;
909
910 BUG_ON(!atomic_read(&bo->reserved));
911
912 /*
913 * FIXME: It's possible to pipeline buffer moves.
914 * Have the driver move function wait for idle when necessary,
915 * instead of doing it here.
916 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200917 spin_lock(&bo->lock);
918 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
919 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200920 if (ret)
921 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922 mem.num_pages = bo->num_pages;
923 mem.size = mem.num_pages << PAGE_SHIFT;
924 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200925 /*
926 * Determine where to move the buffer.
927 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100928 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929 if (ret)
930 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200931 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200932out_unlock:
933 if (ret && mem.mm_node) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200934 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100935 mem.mm_node->private = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200936 drm_mm_put_block(mem.mm_node);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200937 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938 }
939 return ret;
940}
941
Jerome Glisseca262a9992009-12-08 15:33:32 +0100942static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200943 struct ttm_mem_reg *mem)
944{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100945 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200946
Jerome Glisseca262a9992009-12-08 15:33:32 +0100947 for (i = 0; i < placement->num_placement; i++) {
948 if ((placement->placement[i] & mem->placement &
949 TTM_PL_MASK_CACHING) &&
950 (placement->placement[i] & mem->placement &
951 TTM_PL_MASK_MEM))
952 return i;
953 }
954 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200955}
956
957int ttm_buffer_object_validate(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100958 struct ttm_placement *placement,
959 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200960{
961 int ret;
962
963 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +0100964 /* Check that range is valid */
965 if (placement->lpfn || placement->fpfn)
966 if (placement->fpfn > placement->lpfn ||
967 (placement->lpfn - placement->fpfn) < bo->num_pages)
968 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200969 /*
970 * Check whether we need to move buffer.
971 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100972 ret = ttm_bo_mem_compat(placement, &bo->mem);
973 if (ret < 0) {
974 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait);
975 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100977 } else {
978 /*
979 * Use the access and other non-mapping-related flag bits from
980 * the compatible memory placement flags to the active flags
981 */
982 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
983 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200984 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200985 /*
986 * We might need to add a TTM.
987 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
989 ret = ttm_bo_add_ttm(bo, true);
990 if (ret)
991 return ret;
992 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200993 return 0;
994}
995EXPORT_SYMBOL(ttm_buffer_object_validate);
996
997int
998ttm_bo_check_placement(struct ttm_buffer_object *bo,
999 uint32_t set_flags, uint32_t clr_flags)
1000{
1001 uint32_t new_mask = set_flags | clr_flags;
1002
1003 if ((bo->type == ttm_bo_type_user) &&
1004 (clr_flags & TTM_PL_FLAG_CACHED)) {
1005 printk(KERN_ERR TTM_PFX
1006 "User buffers require cache-coherent memory.\n");
1007 return -EINVAL;
1008 }
1009
1010 if (!capable(CAP_SYS_ADMIN)) {
1011 if (new_mask & TTM_PL_FLAG_NO_EVICT) {
1012 printk(KERN_ERR TTM_PFX "Need to be root to modify"
1013 " NO_EVICT status.\n");
1014 return -EINVAL;
1015 }
1016
1017 if ((clr_flags & bo->mem.placement & TTM_PL_MASK_MEMTYPE) &&
1018 (bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1019 printk(KERN_ERR TTM_PFX
1020 "Incompatible memory specification"
1021 " for NO_EVICT buffer.\n");
1022 return -EINVAL;
1023 }
1024 }
1025 return 0;
1026}
1027
1028int ttm_buffer_object_init(struct ttm_bo_device *bdev,
1029 struct ttm_buffer_object *bo,
1030 unsigned long size,
1031 enum ttm_bo_type type,
1032 uint32_t flags,
1033 uint32_t page_alignment,
1034 unsigned long buffer_start,
1035 bool interruptible,
1036 struct file *persistant_swap_storage,
1037 size_t acc_size,
1038 void (*destroy) (struct ttm_buffer_object *))
1039{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001040 int i, c, ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001041 unsigned long num_pages;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001042 uint32_t placements[8];
1043 struct ttm_placement placement;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044
1045 size += buffer_start & ~PAGE_MASK;
1046 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1047 if (num_pages == 0) {
1048 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1049 return -EINVAL;
1050 }
1051 bo->destroy = destroy;
1052
1053 spin_lock_init(&bo->lock);
1054 kref_init(&bo->kref);
1055 kref_init(&bo->list_kref);
1056 atomic_set(&bo->cpu_writers, 0);
1057 atomic_set(&bo->reserved, 1);
1058 init_waitqueue_head(&bo->event_queue);
1059 INIT_LIST_HEAD(&bo->lru);
1060 INIT_LIST_HEAD(&bo->ddestroy);
1061 INIT_LIST_HEAD(&bo->swap);
1062 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001063 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001064 bo->type = type;
1065 bo->num_pages = num_pages;
1066 bo->mem.mem_type = TTM_PL_SYSTEM;
1067 bo->mem.num_pages = bo->num_pages;
1068 bo->mem.mm_node = NULL;
1069 bo->mem.page_alignment = page_alignment;
1070 bo->buffer_start = buffer_start & PAGE_MASK;
1071 bo->priv_flags = 0;
1072 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1073 bo->seq_valid = false;
1074 bo->persistant_swap_storage = persistant_swap_storage;
1075 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001076 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077
1078 ret = ttm_bo_check_placement(bo, flags, 0ULL);
1079 if (unlikely(ret != 0))
1080 goto out_err;
1081
1082 /*
1083 * If no caching attributes are set, accept any form of caching.
1084 */
1085
1086 if ((flags & TTM_PL_MASK_CACHING) == 0)
1087 flags |= TTM_PL_MASK_CACHING;
1088
1089 /*
1090 * For ttm_bo_type_device buffers, allocate
1091 * address space from the device.
1092 */
1093
1094 if (bo->type == ttm_bo_type_device) {
1095 ret = ttm_bo_setup_vm(bo);
1096 if (ret)
1097 goto out_err;
1098 }
1099
Jerome Glisseca262a9992009-12-08 15:33:32 +01001100 placement.fpfn = 0;
1101 placement.lpfn = 0;
1102 for (i = 0, c = 0; i <= TTM_PL_PRIV5; i++)
1103 if (flags & (1 << i))
1104 placements[c++] = (flags & ~TTM_PL_MASK_MEM) | (1 << i);
1105 placement.placement = placements;
1106 placement.num_placement = c;
1107 placement.busy_placement = placements;
1108 placement.num_busy_placement = c;
1109 ret = ttm_buffer_object_validate(bo, &placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110 if (ret)
1111 goto out_err;
1112
1113 ttm_bo_unreserve(bo);
1114 return 0;
1115
1116out_err:
1117 ttm_bo_unreserve(bo);
1118 ttm_bo_unref(&bo);
1119
1120 return ret;
1121}
1122EXPORT_SYMBOL(ttm_buffer_object_init);
1123
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001124static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125 unsigned long num_pages)
1126{
1127 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1128 PAGE_MASK;
1129
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001130 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001131}
1132
1133int ttm_buffer_object_create(struct ttm_bo_device *bdev,
1134 unsigned long size,
1135 enum ttm_bo_type type,
1136 uint32_t flags,
1137 uint32_t page_alignment,
1138 unsigned long buffer_start,
1139 bool interruptible,
1140 struct file *persistant_swap_storage,
1141 struct ttm_buffer_object **p_bo)
1142{
1143 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001144 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001145 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146
1147 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001148 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001149 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001150 if (unlikely(ret != 0))
1151 return ret;
1152
1153 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1154
1155 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001156 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001157 return -ENOMEM;
1158 }
1159
1160 ret = ttm_buffer_object_init(bdev, bo, size, type, flags,
1161 page_alignment, buffer_start,
1162 interruptible,
1163 persistant_swap_storage, acc_size, NULL);
1164 if (likely(ret == 0))
1165 *p_bo = bo;
1166
1167 return ret;
1168}
1169
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001170static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001171 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001172{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001173 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001174 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001175 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001176
1177 /*
1178 * Can't use standard list traversal since we're unlocking.
1179 */
1180
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001181 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001182 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001183 spin_unlock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001184 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1185 if (ret) {
1186 if (allow_errors) {
1187 return ret;
1188 } else {
1189 printk(KERN_ERR TTM_PFX
1190 "Cleanup eviction failed\n");
1191 }
1192 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001193 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001194 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001195 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196 return 0;
1197}
1198
1199int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1200{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001201 struct ttm_bo_global *glob = bdev->glob;
Roel Kluinc96e7c72009-08-03 14:22:53 +02001202 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203 int ret = -EINVAL;
1204
1205 if (mem_type >= TTM_NUM_MEM_TYPES) {
1206 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1207 return ret;
1208 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001209 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001210
1211 if (!man->has_type) {
1212 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1213 "memory manager type %u\n", mem_type);
1214 return ret;
1215 }
1216
1217 man->use_type = false;
1218 man->has_type = false;
1219
1220 ret = 0;
1221 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001222 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001224 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001225 if (drm_mm_clean(&man->manager))
1226 drm_mm_takedown(&man->manager);
1227 else
1228 ret = -EBUSY;
1229
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001230 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231 }
1232
1233 return ret;
1234}
1235EXPORT_SYMBOL(ttm_bo_clean_mm);
1236
1237int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1238{
1239 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1240
1241 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1242 printk(KERN_ERR TTM_PFX
1243 "Illegal memory manager memory type %u.\n",
1244 mem_type);
1245 return -EINVAL;
1246 }
1247
1248 if (!man->has_type) {
1249 printk(KERN_ERR TTM_PFX
1250 "Memory type %u has not been initialized.\n",
1251 mem_type);
1252 return 0;
1253 }
1254
Jerome Glisseca262a9992009-12-08 15:33:32 +01001255 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256}
1257EXPORT_SYMBOL(ttm_bo_evict_mm);
1258
1259int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001260 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001261{
1262 int ret = -EINVAL;
1263 struct ttm_mem_type_manager *man;
1264
1265 if (type >= TTM_NUM_MEM_TYPES) {
1266 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1267 return ret;
1268 }
1269
1270 man = &bdev->man[type];
1271 if (man->has_type) {
1272 printk(KERN_ERR TTM_PFX
1273 "Memory manager already initialized for type %d\n",
1274 type);
1275 return ret;
1276 }
1277
1278 ret = bdev->driver->init_mem_type(bdev, type, man);
1279 if (ret)
1280 return ret;
1281
1282 ret = 0;
1283 if (type != TTM_PL_SYSTEM) {
1284 if (!p_size) {
1285 printk(KERN_ERR TTM_PFX
1286 "Zero size memory manager type %d\n",
1287 type);
1288 return ret;
1289 }
Jerome Glisseca262a9992009-12-08 15:33:32 +01001290 ret = drm_mm_init(&man->manager, 0, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291 if (ret)
1292 return ret;
1293 }
1294 man->has_type = true;
1295 man->use_type = true;
1296 man->size = p_size;
1297
1298 INIT_LIST_HEAD(&man->lru);
1299
1300 return 0;
1301}
1302EXPORT_SYMBOL(ttm_bo_init_mm);
1303
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001304static void ttm_bo_global_kobj_release(struct kobject *kobj)
1305{
1306 struct ttm_bo_global *glob =
1307 container_of(kobj, struct ttm_bo_global, kobj);
1308
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001309 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1310 __free_page(glob->dummy_read_page);
1311 kfree(glob);
1312}
1313
1314void ttm_bo_global_release(struct ttm_global_reference *ref)
1315{
1316 struct ttm_bo_global *glob = ref->object;
1317
1318 kobject_del(&glob->kobj);
1319 kobject_put(&glob->kobj);
1320}
1321EXPORT_SYMBOL(ttm_bo_global_release);
1322
1323int ttm_bo_global_init(struct ttm_global_reference *ref)
1324{
1325 struct ttm_bo_global_ref *bo_ref =
1326 container_of(ref, struct ttm_bo_global_ref, ref);
1327 struct ttm_bo_global *glob = ref->object;
1328 int ret;
1329
1330 mutex_init(&glob->device_list_mutex);
1331 spin_lock_init(&glob->lru_lock);
1332 glob->mem_glob = bo_ref->mem_glob;
1333 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1334
1335 if (unlikely(glob->dummy_read_page == NULL)) {
1336 ret = -ENOMEM;
1337 goto out_no_drp;
1338 }
1339
1340 INIT_LIST_HEAD(&glob->swap_lru);
1341 INIT_LIST_HEAD(&glob->device_list);
1342
1343 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1344 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1345 if (unlikely(ret != 0)) {
1346 printk(KERN_ERR TTM_PFX
1347 "Could not register buffer object swapout.\n");
1348 goto out_no_shrink;
1349 }
1350
1351 glob->ttm_bo_extra_size =
1352 ttm_round_pot(sizeof(struct ttm_tt)) +
1353 ttm_round_pot(sizeof(struct ttm_backend));
1354
1355 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1356 ttm_round_pot(sizeof(struct ttm_buffer_object));
1357
1358 atomic_set(&glob->bo_count, 0);
1359
1360 kobject_init(&glob->kobj, &ttm_bo_glob_kobj_type);
1361 ret = kobject_add(&glob->kobj, ttm_get_kobj(), "buffer_objects");
1362 if (unlikely(ret != 0))
1363 kobject_put(&glob->kobj);
1364 return ret;
1365out_no_shrink:
1366 __free_page(glob->dummy_read_page);
1367out_no_drp:
1368 kfree(glob);
1369 return ret;
1370}
1371EXPORT_SYMBOL(ttm_bo_global_init);
1372
1373
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001374int ttm_bo_device_release(struct ttm_bo_device *bdev)
1375{
1376 int ret = 0;
1377 unsigned i = TTM_NUM_MEM_TYPES;
1378 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001379 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001380
1381 while (i--) {
1382 man = &bdev->man[i];
1383 if (man->has_type) {
1384 man->use_type = false;
1385 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1386 ret = -EBUSY;
1387 printk(KERN_ERR TTM_PFX
1388 "DRM memory manager type %d "
1389 "is not clean.\n", i);
1390 }
1391 man->has_type = false;
1392 }
1393 }
1394
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001395 mutex_lock(&glob->device_list_mutex);
1396 list_del(&bdev->device_list);
1397 mutex_unlock(&glob->device_list_mutex);
1398
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001399 if (!cancel_delayed_work(&bdev->wq))
1400 flush_scheduled_work();
1401
1402 while (ttm_bo_delayed_delete(bdev, true))
1403 ;
1404
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001405 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001406 if (list_empty(&bdev->ddestroy))
1407 TTM_DEBUG("Delayed destroy list was clean\n");
1408
1409 if (list_empty(&bdev->man[0].lru))
1410 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001411 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001412
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001413 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1414 write_lock(&bdev->vm_lock);
1415 drm_mm_takedown(&bdev->addr_space_mm);
1416 write_unlock(&bdev->vm_lock);
1417
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001418 return ret;
1419}
1420EXPORT_SYMBOL(ttm_bo_device_release);
1421
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001422int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001423 struct ttm_bo_global *glob,
1424 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001425 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001426 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001427{
1428 int ret = -EINVAL;
1429
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001430 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001431 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001432
1433 memset(bdev->man, 0, sizeof(bdev->man));
1434
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001435 /*
1436 * Initialize the system memory buffer type.
1437 * Other types need to be driver / IOCTL initialized.
1438 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001439 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001440 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001441 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001442
1443 bdev->addr_space_rb = RB_ROOT;
1444 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1445 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001446 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001447
1448 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1449 bdev->nice_mode = true;
1450 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001451 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001452 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001453 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001454
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001455 mutex_lock(&glob->device_list_mutex);
1456 list_add_tail(&bdev->device_list, &glob->device_list);
1457 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001458
1459 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001460out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001461 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001462out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001463 return ret;
1464}
1465EXPORT_SYMBOL(ttm_bo_device_init);
1466
1467/*
1468 * buffer object vm functions.
1469 */
1470
1471bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1472{
1473 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1474
1475 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1476 if (mem->mem_type == TTM_PL_SYSTEM)
1477 return false;
1478
1479 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1480 return false;
1481
1482 if (mem->placement & TTM_PL_FLAG_CACHED)
1483 return false;
1484 }
1485 return true;
1486}
1487
1488int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
1489 struct ttm_mem_reg *mem,
1490 unsigned long *bus_base,
1491 unsigned long *bus_offset, unsigned long *bus_size)
1492{
1493 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1494
1495 *bus_size = 0;
1496 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1497 return -EINVAL;
1498
1499 if (ttm_mem_reg_is_pci(bdev, mem)) {
1500 *bus_offset = mem->mm_node->start << PAGE_SHIFT;
1501 *bus_size = mem->num_pages << PAGE_SHIFT;
1502 *bus_base = man->io_offset;
1503 }
1504
1505 return 0;
1506}
1507
1508void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1509{
1510 struct ttm_bo_device *bdev = bo->bdev;
1511 loff_t offset = (loff_t) bo->addr_space_offset;
1512 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1513
1514 if (!bdev->dev_mapping)
1515 return;
1516
1517 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1518}
Dave Airliee024e112009-06-24 09:48:08 +10001519EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001520
1521static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1522{
1523 struct ttm_bo_device *bdev = bo->bdev;
1524 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1525 struct rb_node *parent = NULL;
1526 struct ttm_buffer_object *cur_bo;
1527 unsigned long offset = bo->vm_node->start;
1528 unsigned long cur_offset;
1529
1530 while (*cur) {
1531 parent = *cur;
1532 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1533 cur_offset = cur_bo->vm_node->start;
1534 if (offset < cur_offset)
1535 cur = &parent->rb_left;
1536 else if (offset > cur_offset)
1537 cur = &parent->rb_right;
1538 else
1539 BUG();
1540 }
1541
1542 rb_link_node(&bo->vm_rb, parent, cur);
1543 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1544}
1545
1546/**
1547 * ttm_bo_setup_vm:
1548 *
1549 * @bo: the buffer to allocate address space for
1550 *
1551 * Allocate address space in the drm device so that applications
1552 * can mmap the buffer and access the contents. This only
1553 * applies to ttm_bo_type_device objects as others are not
1554 * placed in the drm device address space.
1555 */
1556
1557static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1558{
1559 struct ttm_bo_device *bdev = bo->bdev;
1560 int ret;
1561
1562retry_pre_get:
1563 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1564 if (unlikely(ret != 0))
1565 return ret;
1566
1567 write_lock(&bdev->vm_lock);
1568 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1569 bo->mem.num_pages, 0, 0);
1570
1571 if (unlikely(bo->vm_node == NULL)) {
1572 ret = -ENOMEM;
1573 goto out_unlock;
1574 }
1575
1576 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1577 bo->mem.num_pages, 0);
1578
1579 if (unlikely(bo->vm_node == NULL)) {
1580 write_unlock(&bdev->vm_lock);
1581 goto retry_pre_get;
1582 }
1583
1584 ttm_bo_vm_insert_rb(bo);
1585 write_unlock(&bdev->vm_lock);
1586 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1587
1588 return 0;
1589out_unlock:
1590 write_unlock(&bdev->vm_lock);
1591 return ret;
1592}
1593
1594int ttm_bo_wait(struct ttm_buffer_object *bo,
1595 bool lazy, bool interruptible, bool no_wait)
1596{
1597 struct ttm_bo_driver *driver = bo->bdev->driver;
1598 void *sync_obj;
1599 void *sync_obj_arg;
1600 int ret = 0;
1601
1602 if (likely(bo->sync_obj == NULL))
1603 return 0;
1604
1605 while (bo->sync_obj) {
1606
1607 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1608 void *tmp_obj = bo->sync_obj;
1609 bo->sync_obj = NULL;
1610 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1611 spin_unlock(&bo->lock);
1612 driver->sync_obj_unref(&tmp_obj);
1613 spin_lock(&bo->lock);
1614 continue;
1615 }
1616
1617 if (no_wait)
1618 return -EBUSY;
1619
1620 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1621 sync_obj_arg = bo->sync_obj_arg;
1622 spin_unlock(&bo->lock);
1623 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1624 lazy, interruptible);
1625 if (unlikely(ret != 0)) {
1626 driver->sync_obj_unref(&sync_obj);
1627 spin_lock(&bo->lock);
1628 return ret;
1629 }
1630 spin_lock(&bo->lock);
1631 if (likely(bo->sync_obj == sync_obj &&
1632 bo->sync_obj_arg == sync_obj_arg)) {
1633 void *tmp_obj = bo->sync_obj;
1634 bo->sync_obj = NULL;
1635 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1636 &bo->priv_flags);
1637 spin_unlock(&bo->lock);
1638 driver->sync_obj_unref(&sync_obj);
1639 driver->sync_obj_unref(&tmp_obj);
1640 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001641 } else {
1642 spin_unlock(&bo->lock);
1643 driver->sync_obj_unref(&sync_obj);
1644 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001645 }
1646 }
1647 return 0;
1648}
1649EXPORT_SYMBOL(ttm_bo_wait);
1650
1651void ttm_bo_unblock_reservation(struct ttm_buffer_object *bo)
1652{
1653 atomic_set(&bo->reserved, 0);
1654 wake_up_all(&bo->event_queue);
1655}
1656
1657int ttm_bo_block_reservation(struct ttm_buffer_object *bo, bool interruptible,
1658 bool no_wait)
1659{
1660 int ret;
1661
1662 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
1663 if (no_wait)
1664 return -EBUSY;
1665 else if (interruptible) {
1666 ret = wait_event_interruptible
1667 (bo->event_queue, atomic_read(&bo->reserved) == 0);
1668 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001669 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001670 } else {
1671 wait_event(bo->event_queue,
1672 atomic_read(&bo->reserved) == 0);
1673 }
1674 }
1675 return 0;
1676}
1677
1678int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1679{
1680 int ret = 0;
1681
1682 /*
1683 * Using ttm_bo_reserve instead of ttm_bo_block_reservation
1684 * makes sure the lru lists are updated.
1685 */
1686
1687 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1688 if (unlikely(ret != 0))
1689 return ret;
1690 spin_lock(&bo->lock);
1691 ret = ttm_bo_wait(bo, false, true, no_wait);
1692 spin_unlock(&bo->lock);
1693 if (likely(ret == 0))
1694 atomic_inc(&bo->cpu_writers);
1695 ttm_bo_unreserve(bo);
1696 return ret;
1697}
1698
1699void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1700{
1701 if (atomic_dec_and_test(&bo->cpu_writers))
1702 wake_up_all(&bo->event_queue);
1703}
1704
1705/**
1706 * A buffer object shrink method that tries to swap out the first
1707 * buffer object on the bo_global::swap_lru list.
1708 */
1709
1710static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1711{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001712 struct ttm_bo_global *glob =
1713 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001714 struct ttm_buffer_object *bo;
1715 int ret = -EBUSY;
1716 int put_count;
1717 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1718
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001719 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001720 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001721 if (unlikely(list_empty(&glob->swap_lru))) {
1722 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001723 return -EBUSY;
1724 }
1725
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001726 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001727 struct ttm_buffer_object, swap);
1728 kref_get(&bo->list_kref);
1729
1730 /**
1731 * Reserve buffer. Since we unlock while sleeping, we need
1732 * to re-check that nobody removed us from the swap-list while
1733 * we slept.
1734 */
1735
1736 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1737 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001738 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001739 ttm_bo_wait_unreserved(bo, false);
1740 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001741 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001742 }
1743 }
1744
1745 BUG_ON(ret != 0);
1746 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001747 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001748
1749 while (put_count--)
1750 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1751
1752 /**
1753 * Wait for GPU, then move to system cached.
1754 */
1755
1756 spin_lock(&bo->lock);
1757 ret = ttm_bo_wait(bo, false, false, false);
1758 spin_unlock(&bo->lock);
1759
1760 if (unlikely(ret != 0))
1761 goto out;
1762
1763 if ((bo->mem.placement & swap_placement) != swap_placement) {
1764 struct ttm_mem_reg evict_mem;
1765
1766 evict_mem = bo->mem;
1767 evict_mem.mm_node = NULL;
1768 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1769 evict_mem.mem_type = TTM_PL_SYSTEM;
1770
1771 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1772 false, false);
1773 if (unlikely(ret != 0))
1774 goto out;
1775 }
1776
1777 ttm_bo_unmap_virtual(bo);
1778
1779 /**
1780 * Swap out. Buffer will be swapped in again as soon as
1781 * anyone tries to access a ttm page.
1782 */
1783
1784 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1785out:
1786
1787 /**
1788 *
1789 * Unreserve without putting on LRU to avoid swapping out an
1790 * already swapped buffer.
1791 */
1792
1793 atomic_set(&bo->reserved, 0);
1794 wake_up_all(&bo->event_queue);
1795 kref_put(&bo->list_kref, ttm_bo_release_list);
1796 return ret;
1797}
1798
1799void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1800{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001801 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001802 ;
1803}