blob: 1fbb2eea5e88d5d17b59ddffa40a29b0424f8069 [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
Jerome Glissefb53f862009-12-09 21:55:10 +010062static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
63{
64 int i;
65
66 for (i = 0; i <= TTM_PL_PRIV5; i++)
67 if (flags & (1 << i)) {
68 *mem_type = i;
69 return 0;
70 }
71 return -EINVAL;
72}
73
74static void ttm_mem_type_manager_debug(struct ttm_bo_global *glob,
75 struct ttm_mem_type_manager *man)
76{
77 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
78 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
79 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
80 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
81 printk(KERN_ERR TTM_PFX " io_offset: 0x%08lX\n", man->io_offset);
82 printk(KERN_ERR TTM_PFX " io_size: %ld\n", man->io_size);
83 printk(KERN_ERR TTM_PFX " size: %ld\n", (unsigned long)man->size);
84 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
85 man->available_caching);
86 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
87 man->default_caching);
88 spin_lock(&glob->lru_lock);
89 drm_mm_debug_table(&man->manager, TTM_PFX);
90 spin_unlock(&glob->lru_lock);
91}
92
93static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
94 struct ttm_placement *placement)
95{
96 struct ttm_bo_device *bdev = bo->bdev;
97 struct ttm_bo_global *glob = bo->glob;
98 struct ttm_mem_type_manager *man;
99 int i, ret, mem_type;
100
101 printk(KERN_ERR TTM_PFX "No space for %p (%ld pages, %ldK, %ldM)\n",
102 bo, bo->mem.num_pages, bo->mem.size >> 10,
103 bo->mem.size >> 20);
104 for (i = 0; i < placement->num_placement; i++) {
105 ret = ttm_mem_type_from_flags(placement->placement[i],
106 &mem_type);
107 if (ret)
108 return;
109 man = &bdev->man[mem_type];
110 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
111 i, placement->placement[i], mem_type);
112 ttm_mem_type_manager_debug(glob, man);
113 }
114}
115
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200116static ssize_t ttm_bo_global_show(struct kobject *kobj,
117 struct attribute *attr,
118 char *buffer)
119{
120 struct ttm_bo_global *glob =
121 container_of(kobj, struct ttm_bo_global, kobj);
122
123 return snprintf(buffer, PAGE_SIZE, "%lu\n",
124 (unsigned long) atomic_read(&glob->bo_count));
125}
126
127static struct attribute *ttm_bo_global_attrs[] = {
128 &ttm_bo_count,
129 NULL
130};
131
132static struct sysfs_ops ttm_bo_global_ops = {
133 .show = &ttm_bo_global_show
134};
135
136static struct kobj_type ttm_bo_glob_kobj_type = {
137 .release = &ttm_bo_global_kobj_release,
138 .sysfs_ops = &ttm_bo_global_ops,
139 .default_attrs = ttm_bo_global_attrs
140};
141
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200142
143static inline uint32_t ttm_bo_type_flags(unsigned type)
144{
145 return 1 << (type);
146}
147
148static void ttm_bo_release_list(struct kref *list_kref)
149{
150 struct ttm_buffer_object *bo =
151 container_of(list_kref, struct ttm_buffer_object, list_kref);
152 struct ttm_bo_device *bdev = bo->bdev;
153
154 BUG_ON(atomic_read(&bo->list_kref.refcount));
155 BUG_ON(atomic_read(&bo->kref.refcount));
156 BUG_ON(atomic_read(&bo->cpu_writers));
157 BUG_ON(bo->sync_obj != NULL);
158 BUG_ON(bo->mem.mm_node != NULL);
159 BUG_ON(!list_empty(&bo->lru));
160 BUG_ON(!list_empty(&bo->ddestroy));
161
162 if (bo->ttm)
163 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200164 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165 if (bo->destroy)
166 bo->destroy(bo);
167 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200168 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169 kfree(bo);
170 }
171}
172
173int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
174{
175
176 if (interruptible) {
177 int ret = 0;
178
179 ret = wait_event_interruptible(bo->event_queue,
180 atomic_read(&bo->reserved) == 0);
181 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100182 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200183 } else {
184 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
185 }
186 return 0;
187}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000188EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200189
190static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
191{
192 struct ttm_bo_device *bdev = bo->bdev;
193 struct ttm_mem_type_manager *man;
194
195 BUG_ON(!atomic_read(&bo->reserved));
196
197 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
198
199 BUG_ON(!list_empty(&bo->lru));
200
201 man = &bdev->man[bo->mem.mem_type];
202 list_add_tail(&bo->lru, &man->lru);
203 kref_get(&bo->list_kref);
204
205 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200206 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200207 kref_get(&bo->list_kref);
208 }
209 }
210}
211
212/**
213 * Call with the lru_lock held.
214 */
215
216static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
217{
218 int put_count = 0;
219
220 if (!list_empty(&bo->swap)) {
221 list_del_init(&bo->swap);
222 ++put_count;
223 }
224 if (!list_empty(&bo->lru)) {
225 list_del_init(&bo->lru);
226 ++put_count;
227 }
228
229 /*
230 * TODO: Add a driver hook to delete from
231 * driver-specific LRU's here.
232 */
233
234 return put_count;
235}
236
237int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
238 bool interruptible,
239 bool no_wait, bool use_sequence, uint32_t sequence)
240{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200241 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200242 int ret;
243
244 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
245 if (use_sequence && bo->seq_valid &&
246 (sequence - bo->val_seq < (1 << 31))) {
247 return -EAGAIN;
248 }
249
250 if (no_wait)
251 return -EBUSY;
252
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200253 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200255 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200256
257 if (unlikely(ret))
258 return ret;
259 }
260
261 if (use_sequence) {
262 bo->val_seq = sequence;
263 bo->seq_valid = true;
264 } else {
265 bo->seq_valid = false;
266 }
267
268 return 0;
269}
270EXPORT_SYMBOL(ttm_bo_reserve);
271
272static void ttm_bo_ref_bug(struct kref *list_kref)
273{
274 BUG();
275}
276
277int ttm_bo_reserve(struct ttm_buffer_object *bo,
278 bool interruptible,
279 bool no_wait, bool use_sequence, uint32_t sequence)
280{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200281 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200282 int put_count = 0;
283 int ret;
284
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200285 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200286 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
287 sequence);
288 if (likely(ret == 0))
289 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200290 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200291
292 while (put_count--)
293 kref_put(&bo->list_kref, ttm_bo_ref_bug);
294
295 return ret;
296}
297
298void ttm_bo_unreserve(struct ttm_buffer_object *bo)
299{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200300 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200301
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200302 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200303 ttm_bo_add_to_lru(bo);
304 atomic_set(&bo->reserved, 0);
305 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200306 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200307}
308EXPORT_SYMBOL(ttm_bo_unreserve);
309
310/*
311 * Call bo->mutex locked.
312 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200313static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
314{
315 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200316 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200317 int ret = 0;
318 uint32_t page_flags = 0;
319
320 TTM_ASSERT_LOCKED(&bo->mutex);
321 bo->ttm = NULL;
322
Dave Airliead49f502009-07-10 22:36:26 +1000323 if (bdev->need_dma32)
324 page_flags |= TTM_PAGE_FLAG_DMA32;
325
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326 switch (bo->type) {
327 case ttm_bo_type_device:
328 if (zero_alloc)
329 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
330 case ttm_bo_type_kernel:
331 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200332 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200333 if (unlikely(bo->ttm == NULL))
334 ret = -ENOMEM;
335 break;
336 case ttm_bo_type_user:
337 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
338 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200339 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000340 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200341 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000342 break;
343 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344
345 ret = ttm_tt_set_user(bo->ttm, current,
346 bo->buffer_start, bo->num_pages);
347 if (unlikely(ret != 0))
348 ttm_tt_destroy(bo->ttm);
349 break;
350 default:
351 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
352 ret = -EINVAL;
353 break;
354 }
355
356 return ret;
357}
358
359static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
360 struct ttm_mem_reg *mem,
361 bool evict, bool interruptible, bool no_wait)
362{
363 struct ttm_bo_device *bdev = bo->bdev;
364 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
365 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
366 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
367 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
368 int ret = 0;
369
370 if (old_is_pci || new_is_pci ||
371 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
372 ttm_bo_unmap_virtual(bo);
373
374 /*
375 * Create and bind a ttm if required.
376 */
377
378 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
379 ret = ttm_bo_add_ttm(bo, false);
380 if (ret)
381 goto out_err;
382
383 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
384 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200385 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200386
387 if (mem->mem_type != TTM_PL_SYSTEM) {
388 ret = ttm_tt_bind(bo->ttm, mem);
389 if (ret)
390 goto out_err;
391 }
392
393 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100394 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200395 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200396 goto moved;
397 }
398
399 }
400
Dave Airliee024e112009-06-24 09:48:08 +1000401 if (bdev->driver->move_notify)
402 bdev->driver->move_notify(bo, mem);
403
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200404 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
405 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
406 ret = ttm_bo_move_ttm(bo, evict, no_wait, mem);
407 else if (bdev->driver->move)
408 ret = bdev->driver->move(bo, evict, interruptible,
409 no_wait, mem);
410 else
411 ret = ttm_bo_move_memcpy(bo, evict, no_wait, mem);
412
413 if (ret)
414 goto out_err;
415
416moved:
417 if (bo->evicted) {
418 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
419 if (ret)
420 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
421 bo->evicted = false;
422 }
423
424 if (bo->mem.mm_node) {
425 spin_lock(&bo->lock);
426 bo->offset = (bo->mem.mm_node->start << PAGE_SHIFT) +
427 bdev->man[bo->mem.mem_type].gpu_offset;
428 bo->cur_placement = bo->mem.placement;
429 spin_unlock(&bo->lock);
430 }
431
432 return 0;
433
434out_err:
435 new_man = &bdev->man[bo->mem.mem_type];
436 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
437 ttm_tt_unbind(bo->ttm);
438 ttm_tt_destroy(bo->ttm);
439 bo->ttm = NULL;
440 }
441
442 return ret;
443}
444
445/**
446 * If bo idle, remove from delayed- and lru lists, and unref.
447 * If not idle, and already on delayed list, do nothing.
448 * If not idle, and not on delayed list, put on delayed list,
449 * up the list_kref and schedule a delayed list check.
450 */
451
452static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
453{
454 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200455 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200456 struct ttm_bo_driver *driver = bdev->driver;
457 int ret;
458
459 spin_lock(&bo->lock);
460 (void) ttm_bo_wait(bo, false, false, !remove_all);
461
462 if (!bo->sync_obj) {
463 int put_count;
464
465 spin_unlock(&bo->lock);
466
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200467 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200468 ret = ttm_bo_reserve_locked(bo, false, false, false, 0);
469 BUG_ON(ret);
470 if (bo->ttm)
471 ttm_tt_unbind(bo->ttm);
472
473 if (!list_empty(&bo->ddestroy)) {
474 list_del_init(&bo->ddestroy);
475 kref_put(&bo->list_kref, ttm_bo_ref_bug);
476 }
477 if (bo->mem.mm_node) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100478 bo->mem.mm_node->private = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200479 drm_mm_put_block(bo->mem.mm_node);
480 bo->mem.mm_node = NULL;
481 }
482 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200483 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200484
485 atomic_set(&bo->reserved, 0);
486
487 while (put_count--)
488 kref_put(&bo->list_kref, ttm_bo_release_list);
489
490 return 0;
491 }
492
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200493 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200494 if (list_empty(&bo->ddestroy)) {
495 void *sync_obj = bo->sync_obj;
496 void *sync_obj_arg = bo->sync_obj_arg;
497
498 kref_get(&bo->list_kref);
499 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200500 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200501 spin_unlock(&bo->lock);
502
503 if (sync_obj)
504 driver->sync_obj_flush(sync_obj, sync_obj_arg);
505 schedule_delayed_work(&bdev->wq,
506 ((HZ / 100) < 1) ? 1 : HZ / 100);
507 ret = 0;
508
509 } else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200510 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200511 spin_unlock(&bo->lock);
512 ret = -EBUSY;
513 }
514
515 return ret;
516}
517
518/**
519 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
520 * encountered buffers.
521 */
522
523static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
524{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200525 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200526 struct ttm_buffer_object *entry, *nentry;
527 struct list_head *list, *next;
528 int ret;
529
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200530 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200531 list_for_each_safe(list, next, &bdev->ddestroy) {
532 entry = list_entry(list, struct ttm_buffer_object, ddestroy);
533 nentry = NULL;
534
535 /*
536 * Protect the next list entry from destruction while we
537 * unlock the lru_lock.
538 */
539
540 if (next != &bdev->ddestroy) {
541 nentry = list_entry(next, struct ttm_buffer_object,
542 ddestroy);
543 kref_get(&nentry->list_kref);
544 }
545 kref_get(&entry->list_kref);
546
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200547 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200548 ret = ttm_bo_cleanup_refs(entry, remove_all);
549 kref_put(&entry->list_kref, ttm_bo_release_list);
550
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200551 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200552 if (nentry) {
553 bool next_onlist = !list_empty(next);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200554 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200555 kref_put(&nentry->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200556 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200557 /*
558 * Someone might have raced us and removed the
559 * next entry from the list. We don't bother restarting
560 * list traversal.
561 */
562
563 if (!next_onlist)
564 break;
565 }
566 if (ret)
567 break;
568 }
569 ret = !list_empty(&bdev->ddestroy);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200570 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200571
572 return ret;
573}
574
575static void ttm_bo_delayed_workqueue(struct work_struct *work)
576{
577 struct ttm_bo_device *bdev =
578 container_of(work, struct ttm_bo_device, wq.work);
579
580 if (ttm_bo_delayed_delete(bdev, false)) {
581 schedule_delayed_work(&bdev->wq,
582 ((HZ / 100) < 1) ? 1 : HZ / 100);
583 }
584}
585
586static void ttm_bo_release(struct kref *kref)
587{
588 struct ttm_buffer_object *bo =
589 container_of(kref, struct ttm_buffer_object, kref);
590 struct ttm_bo_device *bdev = bo->bdev;
591
592 if (likely(bo->vm_node != NULL)) {
593 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
594 drm_mm_put_block(bo->vm_node);
595 bo->vm_node = NULL;
596 }
597 write_unlock(&bdev->vm_lock);
598 ttm_bo_cleanup_refs(bo, false);
599 kref_put(&bo->list_kref, ttm_bo_release_list);
600 write_lock(&bdev->vm_lock);
601}
602
603void ttm_bo_unref(struct ttm_buffer_object **p_bo)
604{
605 struct ttm_buffer_object *bo = *p_bo;
606 struct ttm_bo_device *bdev = bo->bdev;
607
608 *p_bo = NULL;
609 write_lock(&bdev->vm_lock);
610 kref_put(&bo->kref, ttm_bo_release);
611 write_unlock(&bdev->vm_lock);
612}
613EXPORT_SYMBOL(ttm_bo_unref);
614
Jerome Glisseca262a9992009-12-08 15:33:32 +0100615static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
616 bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200617{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200618 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200619 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200620 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100621 struct ttm_placement placement;
622 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200623
624 spin_lock(&bo->lock);
625 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
626 spin_unlock(&bo->lock);
627
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200628 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100629 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200630 printk(KERN_ERR TTM_PFX
631 "Failed to expire sync object before "
632 "buffer eviction.\n");
633 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200634 goto out;
635 }
636
637 BUG_ON(!atomic_read(&bo->reserved));
638
639 evict_mem = bo->mem;
640 evict_mem.mm_node = NULL;
641
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100642 placement.fpfn = 0;
643 placement.lpfn = 0;
644 placement.num_placement = 0;
645 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100646 bdev->driver->evict_flags(bo, &placement);
647 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
648 no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100650 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200651 printk(KERN_ERR TTM_PFX
652 "Failed to find memory space for "
653 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100654 ttm_bo_mem_space_debug(bo, &placement);
655 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656 goto out;
657 }
658
659 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
660 no_wait);
661 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100662 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200663 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +0100664 spin_lock(&glob->lru_lock);
665 if (evict_mem.mm_node) {
666 evict_mem.mm_node->private = NULL;
667 drm_mm_put_block(evict_mem.mm_node);
668 evict_mem.mm_node = NULL;
669 }
670 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200671 goto out;
672 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200673 bo->evicted = true;
674out:
675 return ret;
676}
677
Jerome Glisseca262a9992009-12-08 15:33:32 +0100678static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
679 uint32_t mem_type,
680 bool interruptible, bool no_wait)
681{
682 struct ttm_bo_global *glob = bdev->glob;
683 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
684 struct ttm_buffer_object *bo;
685 int ret, put_count = 0;
686
687 spin_lock(&glob->lru_lock);
688 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
689 kref_get(&bo->list_kref);
690 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, false, 0);
691 if (likely(ret == 0))
692 put_count = ttm_bo_del_from_lru(bo);
693 spin_unlock(&glob->lru_lock);
694 if (unlikely(ret != 0))
695 return ret;
696 while (put_count--)
697 kref_put(&bo->list_kref, ttm_bo_ref_bug);
698 ret = ttm_bo_evict(bo, interruptible, no_wait);
699 ttm_bo_unreserve(bo);
700 kref_put(&bo->list_kref, ttm_bo_release_list);
701 return ret;
702}
703
704static int ttm_bo_man_get_node(struct ttm_buffer_object *bo,
705 struct ttm_mem_type_manager *man,
706 struct ttm_placement *placement,
707 struct ttm_mem_reg *mem,
708 struct drm_mm_node **node)
709{
710 struct ttm_bo_global *glob = bo->glob;
711 unsigned long lpfn;
712 int ret;
713
714 lpfn = placement->lpfn;
715 if (!lpfn)
716 lpfn = man->size;
717 *node = NULL;
718 do {
719 ret = drm_mm_pre_get(&man->manager);
720 if (unlikely(ret))
721 return ret;
722
723 spin_lock(&glob->lru_lock);
724 *node = drm_mm_search_free_in_range(&man->manager,
725 mem->num_pages, mem->page_alignment,
726 placement->fpfn, lpfn, 1);
727 if (unlikely(*node == NULL)) {
728 spin_unlock(&glob->lru_lock);
729 return 0;
730 }
731 *node = drm_mm_get_block_atomic_range(*node, mem->num_pages,
732 mem->page_alignment,
733 placement->fpfn,
734 lpfn);
735 spin_unlock(&glob->lru_lock);
736 } while (*node == NULL);
737 return 0;
738}
739
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200740/**
741 * Repeatedly evict memory from the LRU for @mem_type until we create enough
742 * space, or we've evicted everything and there isn't enough space.
743 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100744static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
745 uint32_t mem_type,
746 struct ttm_placement *placement,
747 struct ttm_mem_reg *mem,
748 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200749{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100750 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200751 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200752 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Jerome Glisseca262a9992009-12-08 15:33:32 +0100753 struct drm_mm_node *node;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200754 int ret;
755
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200756 do {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100757 ret = ttm_bo_man_get_node(bo, man, placement, mem, &node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200758 if (unlikely(ret != 0))
759 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100760 if (node)
761 break;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200762 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100763 if (list_empty(&man->lru)) {
764 spin_unlock(&glob->lru_lock);
765 break;
766 }
767 spin_unlock(&glob->lru_lock);
768 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
769 no_wait);
770 if (unlikely(ret != 0))
771 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200772 } while (1);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100773 if (node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200774 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200775 mem->mm_node = node;
776 mem->mem_type = mem_type;
777 return 0;
778}
779
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200780static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
781 uint32_t cur_placement,
782 uint32_t proposed_placement)
783{
784 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
785 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
786
787 /**
788 * Keep current caching if possible.
789 */
790
791 if ((cur_placement & caching) != 0)
792 result |= (cur_placement & caching);
793 else if ((man->default_caching & caching) != 0)
794 result |= man->default_caching;
795 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
796 result |= TTM_PL_FLAG_CACHED;
797 else if ((TTM_PL_FLAG_WC & caching) != 0)
798 result |= TTM_PL_FLAG_WC;
799 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
800 result |= TTM_PL_FLAG_UNCACHED;
801
802 return result;
803}
804
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200805static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
806 bool disallow_fixed,
807 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200808 uint32_t proposed_placement,
809 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200810{
811 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
812
813 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
814 return false;
815
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200816 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200817 return false;
818
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200819 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200820 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200821
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200822 cur_flags |= (proposed_placement & man->available_caching);
823
824 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200825 return true;
826}
827
828/**
829 * Creates space for memory region @mem according to its type.
830 *
831 * This function first searches for free space in compatible memory types in
832 * the priority order defined by the driver. If free space isn't found, then
833 * ttm_bo_mem_force_space is attempted in priority order to evict and find
834 * space.
835 */
836int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100837 struct ttm_placement *placement,
838 struct ttm_mem_reg *mem,
839 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200840{
841 struct ttm_bo_device *bdev = bo->bdev;
842 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200843 uint32_t mem_type = TTM_PL_SYSTEM;
844 uint32_t cur_flags = 0;
845 bool type_found = false;
846 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100847 bool has_erestartsys = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200848 struct drm_mm_node *node = NULL;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100849 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200850
851 mem->mm_node = NULL;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100852 for (i = 0; i <= placement->num_placement; ++i) {
853 ret = ttm_mem_type_from_flags(placement->placement[i],
854 &mem_type);
855 if (ret)
856 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200857 man = &bdev->man[mem_type];
858
859 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100860 bo->type == ttm_bo_type_user,
861 mem_type,
862 placement->placement[i],
863 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200864
865 if (!type_ok)
866 continue;
867
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200868 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
869 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100870 /*
871 * Use the access and other non-mapping-related flag bits from
872 * the memory placement flags to the current flags
873 */
874 ttm_flag_masked(&cur_flags, placement->placement[i],
875 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200876
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877 if (mem_type == TTM_PL_SYSTEM)
878 break;
879
880 if (man->has_type && man->use_type) {
881 type_found = true;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100882 ret = ttm_bo_man_get_node(bo, man, placement, mem,
883 &node);
884 if (unlikely(ret))
885 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200886 }
887 if (node)
888 break;
889 }
890
891 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) {
892 mem->mm_node = node;
893 mem->mem_type = mem_type;
894 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100895 if (node)
896 node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200897 return 0;
898 }
899
900 if (!type_found)
901 return -EINVAL;
902
Jerome Glisseca262a9992009-12-08 15:33:32 +0100903 for (i = 0; i <= placement->num_busy_placement; ++i) {
904 ret = ttm_mem_type_from_flags(placement->placement[i],
905 &mem_type);
906 if (ret)
907 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200908 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200909 if (!man->has_type)
910 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200911 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100912 bo->type == ttm_bo_type_user,
913 mem_type,
914 placement->placement[i],
915 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200916 continue;
917
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200918 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
919 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100920 /*
921 * Use the access and other non-mapping-related flag bits from
922 * the memory placement flags to the current flags
923 */
924 ttm_flag_masked(&cur_flags, placement->placement[i],
925 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200926
Jerome Glisseca262a9992009-12-08 15:33:32 +0100927 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
928 interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929 if (ret == 0 && mem->mm_node) {
930 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100931 mem->mm_node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200932 return 0;
933 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100934 if (ret == -ERESTARTSYS)
935 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200936 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100937 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938 return ret;
939}
940EXPORT_SYMBOL(ttm_bo_mem_space);
941
942int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
943{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200944 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
945 return -EBUSY;
946
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100947 return wait_event_interruptible(bo->event_queue,
948 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200949}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000950EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951
952int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100953 struct ttm_placement *placement,
954 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200955{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200956 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200957 int ret = 0;
958 struct ttm_mem_reg mem;
959
960 BUG_ON(!atomic_read(&bo->reserved));
961
962 /*
963 * FIXME: It's possible to pipeline buffer moves.
964 * Have the driver move function wait for idle when necessary,
965 * instead of doing it here.
966 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200967 spin_lock(&bo->lock);
968 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
969 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970 if (ret)
971 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200972 mem.num_pages = bo->num_pages;
973 mem.size = mem.num_pages << PAGE_SHIFT;
974 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200975 /*
976 * Determine where to move the buffer.
977 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100978 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200979 if (ret)
980 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200981 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200982out_unlock:
983 if (ret && mem.mm_node) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200984 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100985 mem.mm_node->private = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200986 drm_mm_put_block(mem.mm_node);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200987 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988 }
989 return ret;
990}
991
Jerome Glisseca262a9992009-12-08 15:33:32 +0100992static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200993 struct ttm_mem_reg *mem)
994{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100995 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200996
Jerome Glisseca262a9992009-12-08 15:33:32 +0100997 for (i = 0; i < placement->num_placement; i++) {
998 if ((placement->placement[i] & mem->placement &
999 TTM_PL_MASK_CACHING) &&
1000 (placement->placement[i] & mem->placement &
1001 TTM_PL_MASK_MEM))
1002 return i;
1003 }
1004 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005}
1006
Jerome Glisse09855ac2009-12-10 17:16:27 +01001007int ttm_bo_validate(struct ttm_buffer_object *bo,
1008 struct ttm_placement *placement,
1009 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001010{
1011 int ret;
1012
1013 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001014 /* Check that range is valid */
1015 if (placement->lpfn || placement->fpfn)
1016 if (placement->fpfn > placement->lpfn ||
1017 (placement->lpfn - placement->fpfn) < bo->num_pages)
1018 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019 /*
1020 * Check whether we need to move buffer.
1021 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001022 ret = ttm_bo_mem_compat(placement, &bo->mem);
1023 if (ret < 0) {
1024 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait);
1025 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001026 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001027 } else {
1028 /*
1029 * Use the access and other non-mapping-related flag bits from
1030 * the compatible memory placement flags to the active flags
1031 */
1032 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1033 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001034 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001035 /*
1036 * We might need to add a TTM.
1037 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001038 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1039 ret = ttm_bo_add_ttm(bo, true);
1040 if (ret)
1041 return ret;
1042 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001043 return 0;
1044}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001045EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046
Jerome Glisse09855ac2009-12-10 17:16:27 +01001047int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1048 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001050 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001051
Jerome Glisse09855ac2009-12-10 17:16:27 +01001052 if (placement->fpfn || placement->lpfn) {
1053 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1054 printk(KERN_ERR TTM_PFX "Page number range to small "
1055 "Need %lu pages, range is [%u, %u]\n",
1056 bo->mem.num_pages, placement->fpfn,
1057 placement->lpfn);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001058 return -EINVAL;
1059 }
Jerome Glisse09855ac2009-12-10 17:16:27 +01001060 }
1061 for (i = 0; i < placement->num_placement; i++) {
1062 if (!capable(CAP_SYS_ADMIN)) {
1063 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1064 printk(KERN_ERR TTM_PFX "Need to be root to "
1065 "modify NO_EVICT status.\n");
1066 return -EINVAL;
1067 }
1068 }
1069 }
1070 for (i = 0; i < placement->num_busy_placement; i++) {
1071 if (!capable(CAP_SYS_ADMIN)) {
1072 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1073 printk(KERN_ERR TTM_PFX "Need to be root to "
1074 "modify NO_EVICT status.\n");
1075 return -EINVAL;
1076 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077 }
1078 }
1079 return 0;
1080}
1081
Jerome Glisse09855ac2009-12-10 17:16:27 +01001082int ttm_bo_init(struct ttm_bo_device *bdev,
1083 struct ttm_buffer_object *bo,
1084 unsigned long size,
1085 enum ttm_bo_type type,
1086 struct ttm_placement *placement,
1087 uint32_t page_alignment,
1088 unsigned long buffer_start,
1089 bool interruptible,
1090 struct file *persistant_swap_storage,
1091 size_t acc_size,
1092 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001094 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001095 unsigned long num_pages;
1096
1097 size += buffer_start & ~PAGE_MASK;
1098 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1099 if (num_pages == 0) {
1100 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1101 return -EINVAL;
1102 }
1103 bo->destroy = destroy;
1104
1105 spin_lock_init(&bo->lock);
1106 kref_init(&bo->kref);
1107 kref_init(&bo->list_kref);
1108 atomic_set(&bo->cpu_writers, 0);
1109 atomic_set(&bo->reserved, 1);
1110 init_waitqueue_head(&bo->event_queue);
1111 INIT_LIST_HEAD(&bo->lru);
1112 INIT_LIST_HEAD(&bo->ddestroy);
1113 INIT_LIST_HEAD(&bo->swap);
1114 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001115 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001116 bo->type = type;
1117 bo->num_pages = num_pages;
1118 bo->mem.mem_type = TTM_PL_SYSTEM;
1119 bo->mem.num_pages = bo->num_pages;
1120 bo->mem.mm_node = NULL;
1121 bo->mem.page_alignment = page_alignment;
1122 bo->buffer_start = buffer_start & PAGE_MASK;
1123 bo->priv_flags = 0;
1124 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1125 bo->seq_valid = false;
1126 bo->persistant_swap_storage = persistant_swap_storage;
1127 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001128 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001129
Jerome Glisse09855ac2009-12-10 17:16:27 +01001130 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001131 if (unlikely(ret != 0))
1132 goto out_err;
1133
1134 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 * For ttm_bo_type_device buffers, allocate
1136 * address space from the device.
1137 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001138 if (bo->type == ttm_bo_type_device) {
1139 ret = ttm_bo_setup_vm(bo);
1140 if (ret)
1141 goto out_err;
1142 }
1143
Jerome Glisse09855ac2009-12-10 17:16:27 +01001144 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001145 if (ret)
1146 goto out_err;
1147
1148 ttm_bo_unreserve(bo);
1149 return 0;
1150
1151out_err:
1152 ttm_bo_unreserve(bo);
1153 ttm_bo_unref(&bo);
1154
1155 return ret;
1156}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001157EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001159static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001160 unsigned long num_pages)
1161{
1162 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1163 PAGE_MASK;
1164
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001165 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001166}
1167
Jerome Glisse09855ac2009-12-10 17:16:27 +01001168int ttm_bo_create(struct ttm_bo_device *bdev,
1169 unsigned long size,
1170 enum ttm_bo_type type,
1171 struct ttm_placement *placement,
1172 uint32_t page_alignment,
1173 unsigned long buffer_start,
1174 bool interruptible,
1175 struct file *persistant_swap_storage,
1176 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001177{
1178 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001179 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001180 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001181
1182 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001183 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001184 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185 if (unlikely(ret != 0))
1186 return ret;
1187
1188 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1189
1190 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001191 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001192 return -ENOMEM;
1193 }
1194
Jerome Glisse09855ac2009-12-10 17:16:27 +01001195 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1196 buffer_start, interruptible,
1197 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001198 if (likely(ret == 0))
1199 *p_bo = bo;
1200
1201 return ret;
1202}
1203
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001204static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001205 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001206{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001207 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001208 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001210
1211 /*
1212 * Can't use standard list traversal since we're unlocking.
1213 */
1214
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001215 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001216 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001217 spin_unlock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001218 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1219 if (ret) {
1220 if (allow_errors) {
1221 return ret;
1222 } else {
1223 printk(KERN_ERR TTM_PFX
1224 "Cleanup eviction failed\n");
1225 }
1226 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001227 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001229 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001230 return 0;
1231}
1232
1233int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1234{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001235 struct ttm_bo_global *glob = bdev->glob;
Roel Kluinc96e7c72009-08-03 14:22:53 +02001236 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001237 int ret = -EINVAL;
1238
1239 if (mem_type >= TTM_NUM_MEM_TYPES) {
1240 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1241 return ret;
1242 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001243 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001244
1245 if (!man->has_type) {
1246 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1247 "memory manager type %u\n", mem_type);
1248 return ret;
1249 }
1250
1251 man->use_type = false;
1252 man->has_type = false;
1253
1254 ret = 0;
1255 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001256 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001257
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001258 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001259 if (drm_mm_clean(&man->manager))
1260 drm_mm_takedown(&man->manager);
1261 else
1262 ret = -EBUSY;
1263
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001264 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001265 }
1266
1267 return ret;
1268}
1269EXPORT_SYMBOL(ttm_bo_clean_mm);
1270
1271int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1272{
1273 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1274
1275 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1276 printk(KERN_ERR TTM_PFX
1277 "Illegal memory manager memory type %u.\n",
1278 mem_type);
1279 return -EINVAL;
1280 }
1281
1282 if (!man->has_type) {
1283 printk(KERN_ERR TTM_PFX
1284 "Memory type %u has not been initialized.\n",
1285 mem_type);
1286 return 0;
1287 }
1288
Jerome Glisseca262a9992009-12-08 15:33:32 +01001289 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001290}
1291EXPORT_SYMBOL(ttm_bo_evict_mm);
1292
1293int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001294 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001295{
1296 int ret = -EINVAL;
1297 struct ttm_mem_type_manager *man;
1298
1299 if (type >= TTM_NUM_MEM_TYPES) {
1300 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1301 return ret;
1302 }
1303
1304 man = &bdev->man[type];
1305 if (man->has_type) {
1306 printk(KERN_ERR TTM_PFX
1307 "Memory manager already initialized for type %d\n",
1308 type);
1309 return ret;
1310 }
1311
1312 ret = bdev->driver->init_mem_type(bdev, type, man);
1313 if (ret)
1314 return ret;
1315
1316 ret = 0;
1317 if (type != TTM_PL_SYSTEM) {
1318 if (!p_size) {
1319 printk(KERN_ERR TTM_PFX
1320 "Zero size memory manager type %d\n",
1321 type);
1322 return ret;
1323 }
Jerome Glisseca262a9992009-12-08 15:33:32 +01001324 ret = drm_mm_init(&man->manager, 0, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001325 if (ret)
1326 return ret;
1327 }
1328 man->has_type = true;
1329 man->use_type = true;
1330 man->size = p_size;
1331
1332 INIT_LIST_HEAD(&man->lru);
1333
1334 return 0;
1335}
1336EXPORT_SYMBOL(ttm_bo_init_mm);
1337
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001338static void ttm_bo_global_kobj_release(struct kobject *kobj)
1339{
1340 struct ttm_bo_global *glob =
1341 container_of(kobj, struct ttm_bo_global, kobj);
1342
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001343 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1344 __free_page(glob->dummy_read_page);
1345 kfree(glob);
1346}
1347
1348void ttm_bo_global_release(struct ttm_global_reference *ref)
1349{
1350 struct ttm_bo_global *glob = ref->object;
1351
1352 kobject_del(&glob->kobj);
1353 kobject_put(&glob->kobj);
1354}
1355EXPORT_SYMBOL(ttm_bo_global_release);
1356
1357int ttm_bo_global_init(struct ttm_global_reference *ref)
1358{
1359 struct ttm_bo_global_ref *bo_ref =
1360 container_of(ref, struct ttm_bo_global_ref, ref);
1361 struct ttm_bo_global *glob = ref->object;
1362 int ret;
1363
1364 mutex_init(&glob->device_list_mutex);
1365 spin_lock_init(&glob->lru_lock);
1366 glob->mem_glob = bo_ref->mem_glob;
1367 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1368
1369 if (unlikely(glob->dummy_read_page == NULL)) {
1370 ret = -ENOMEM;
1371 goto out_no_drp;
1372 }
1373
1374 INIT_LIST_HEAD(&glob->swap_lru);
1375 INIT_LIST_HEAD(&glob->device_list);
1376
1377 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1378 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1379 if (unlikely(ret != 0)) {
1380 printk(KERN_ERR TTM_PFX
1381 "Could not register buffer object swapout.\n");
1382 goto out_no_shrink;
1383 }
1384
1385 glob->ttm_bo_extra_size =
1386 ttm_round_pot(sizeof(struct ttm_tt)) +
1387 ttm_round_pot(sizeof(struct ttm_backend));
1388
1389 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1390 ttm_round_pot(sizeof(struct ttm_buffer_object));
1391
1392 atomic_set(&glob->bo_count, 0);
1393
1394 kobject_init(&glob->kobj, &ttm_bo_glob_kobj_type);
1395 ret = kobject_add(&glob->kobj, ttm_get_kobj(), "buffer_objects");
1396 if (unlikely(ret != 0))
1397 kobject_put(&glob->kobj);
1398 return ret;
1399out_no_shrink:
1400 __free_page(glob->dummy_read_page);
1401out_no_drp:
1402 kfree(glob);
1403 return ret;
1404}
1405EXPORT_SYMBOL(ttm_bo_global_init);
1406
1407
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001408int ttm_bo_device_release(struct ttm_bo_device *bdev)
1409{
1410 int ret = 0;
1411 unsigned i = TTM_NUM_MEM_TYPES;
1412 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001413 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001414
1415 while (i--) {
1416 man = &bdev->man[i];
1417 if (man->has_type) {
1418 man->use_type = false;
1419 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1420 ret = -EBUSY;
1421 printk(KERN_ERR TTM_PFX
1422 "DRM memory manager type %d "
1423 "is not clean.\n", i);
1424 }
1425 man->has_type = false;
1426 }
1427 }
1428
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001429 mutex_lock(&glob->device_list_mutex);
1430 list_del(&bdev->device_list);
1431 mutex_unlock(&glob->device_list_mutex);
1432
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001433 if (!cancel_delayed_work(&bdev->wq))
1434 flush_scheduled_work();
1435
1436 while (ttm_bo_delayed_delete(bdev, true))
1437 ;
1438
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001439 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001440 if (list_empty(&bdev->ddestroy))
1441 TTM_DEBUG("Delayed destroy list was clean\n");
1442
1443 if (list_empty(&bdev->man[0].lru))
1444 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001445 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001446
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001447 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1448 write_lock(&bdev->vm_lock);
1449 drm_mm_takedown(&bdev->addr_space_mm);
1450 write_unlock(&bdev->vm_lock);
1451
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001452 return ret;
1453}
1454EXPORT_SYMBOL(ttm_bo_device_release);
1455
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001456int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001457 struct ttm_bo_global *glob,
1458 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001459 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001460 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001461{
1462 int ret = -EINVAL;
1463
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001464 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001465 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001466
1467 memset(bdev->man, 0, sizeof(bdev->man));
1468
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001469 /*
1470 * Initialize the system memory buffer type.
1471 * Other types need to be driver / IOCTL initialized.
1472 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001473 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001474 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001475 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001476
1477 bdev->addr_space_rb = RB_ROOT;
1478 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1479 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001480 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001481
1482 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1483 bdev->nice_mode = true;
1484 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001485 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001486 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001487 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001489 mutex_lock(&glob->device_list_mutex);
1490 list_add_tail(&bdev->device_list, &glob->device_list);
1491 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001492
1493 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001494out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001495 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001496out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497 return ret;
1498}
1499EXPORT_SYMBOL(ttm_bo_device_init);
1500
1501/*
1502 * buffer object vm functions.
1503 */
1504
1505bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1506{
1507 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1508
1509 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1510 if (mem->mem_type == TTM_PL_SYSTEM)
1511 return false;
1512
1513 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1514 return false;
1515
1516 if (mem->placement & TTM_PL_FLAG_CACHED)
1517 return false;
1518 }
1519 return true;
1520}
1521
1522int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
1523 struct ttm_mem_reg *mem,
1524 unsigned long *bus_base,
1525 unsigned long *bus_offset, unsigned long *bus_size)
1526{
1527 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1528
1529 *bus_size = 0;
1530 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1531 return -EINVAL;
1532
1533 if (ttm_mem_reg_is_pci(bdev, mem)) {
1534 *bus_offset = mem->mm_node->start << PAGE_SHIFT;
1535 *bus_size = mem->num_pages << PAGE_SHIFT;
1536 *bus_base = man->io_offset;
1537 }
1538
1539 return 0;
1540}
1541
1542void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1543{
1544 struct ttm_bo_device *bdev = bo->bdev;
1545 loff_t offset = (loff_t) bo->addr_space_offset;
1546 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1547
1548 if (!bdev->dev_mapping)
1549 return;
1550
1551 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1552}
Dave Airliee024e112009-06-24 09:48:08 +10001553EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001554
1555static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1556{
1557 struct ttm_bo_device *bdev = bo->bdev;
1558 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1559 struct rb_node *parent = NULL;
1560 struct ttm_buffer_object *cur_bo;
1561 unsigned long offset = bo->vm_node->start;
1562 unsigned long cur_offset;
1563
1564 while (*cur) {
1565 parent = *cur;
1566 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1567 cur_offset = cur_bo->vm_node->start;
1568 if (offset < cur_offset)
1569 cur = &parent->rb_left;
1570 else if (offset > cur_offset)
1571 cur = &parent->rb_right;
1572 else
1573 BUG();
1574 }
1575
1576 rb_link_node(&bo->vm_rb, parent, cur);
1577 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1578}
1579
1580/**
1581 * ttm_bo_setup_vm:
1582 *
1583 * @bo: the buffer to allocate address space for
1584 *
1585 * Allocate address space in the drm device so that applications
1586 * can mmap the buffer and access the contents. This only
1587 * applies to ttm_bo_type_device objects as others are not
1588 * placed in the drm device address space.
1589 */
1590
1591static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1592{
1593 struct ttm_bo_device *bdev = bo->bdev;
1594 int ret;
1595
1596retry_pre_get:
1597 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1598 if (unlikely(ret != 0))
1599 return ret;
1600
1601 write_lock(&bdev->vm_lock);
1602 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1603 bo->mem.num_pages, 0, 0);
1604
1605 if (unlikely(bo->vm_node == NULL)) {
1606 ret = -ENOMEM;
1607 goto out_unlock;
1608 }
1609
1610 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1611 bo->mem.num_pages, 0);
1612
1613 if (unlikely(bo->vm_node == NULL)) {
1614 write_unlock(&bdev->vm_lock);
1615 goto retry_pre_get;
1616 }
1617
1618 ttm_bo_vm_insert_rb(bo);
1619 write_unlock(&bdev->vm_lock);
1620 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1621
1622 return 0;
1623out_unlock:
1624 write_unlock(&bdev->vm_lock);
1625 return ret;
1626}
1627
1628int ttm_bo_wait(struct ttm_buffer_object *bo,
1629 bool lazy, bool interruptible, bool no_wait)
1630{
1631 struct ttm_bo_driver *driver = bo->bdev->driver;
1632 void *sync_obj;
1633 void *sync_obj_arg;
1634 int ret = 0;
1635
1636 if (likely(bo->sync_obj == NULL))
1637 return 0;
1638
1639 while (bo->sync_obj) {
1640
1641 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1642 void *tmp_obj = bo->sync_obj;
1643 bo->sync_obj = NULL;
1644 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1645 spin_unlock(&bo->lock);
1646 driver->sync_obj_unref(&tmp_obj);
1647 spin_lock(&bo->lock);
1648 continue;
1649 }
1650
1651 if (no_wait)
1652 return -EBUSY;
1653
1654 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1655 sync_obj_arg = bo->sync_obj_arg;
1656 spin_unlock(&bo->lock);
1657 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1658 lazy, interruptible);
1659 if (unlikely(ret != 0)) {
1660 driver->sync_obj_unref(&sync_obj);
1661 spin_lock(&bo->lock);
1662 return ret;
1663 }
1664 spin_lock(&bo->lock);
1665 if (likely(bo->sync_obj == sync_obj &&
1666 bo->sync_obj_arg == sync_obj_arg)) {
1667 void *tmp_obj = bo->sync_obj;
1668 bo->sync_obj = NULL;
1669 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1670 &bo->priv_flags);
1671 spin_unlock(&bo->lock);
1672 driver->sync_obj_unref(&sync_obj);
1673 driver->sync_obj_unref(&tmp_obj);
1674 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001675 } else {
1676 spin_unlock(&bo->lock);
1677 driver->sync_obj_unref(&sync_obj);
1678 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001679 }
1680 }
1681 return 0;
1682}
1683EXPORT_SYMBOL(ttm_bo_wait);
1684
1685void ttm_bo_unblock_reservation(struct ttm_buffer_object *bo)
1686{
1687 atomic_set(&bo->reserved, 0);
1688 wake_up_all(&bo->event_queue);
1689}
1690
1691int ttm_bo_block_reservation(struct ttm_buffer_object *bo, bool interruptible,
1692 bool no_wait)
1693{
1694 int ret;
1695
1696 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
1697 if (no_wait)
1698 return -EBUSY;
1699 else if (interruptible) {
1700 ret = wait_event_interruptible
1701 (bo->event_queue, atomic_read(&bo->reserved) == 0);
1702 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001703 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001704 } else {
1705 wait_event(bo->event_queue,
1706 atomic_read(&bo->reserved) == 0);
1707 }
1708 }
1709 return 0;
1710}
1711
1712int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1713{
1714 int ret = 0;
1715
1716 /*
1717 * Using ttm_bo_reserve instead of ttm_bo_block_reservation
1718 * makes sure the lru lists are updated.
1719 */
1720
1721 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1722 if (unlikely(ret != 0))
1723 return ret;
1724 spin_lock(&bo->lock);
1725 ret = ttm_bo_wait(bo, false, true, no_wait);
1726 spin_unlock(&bo->lock);
1727 if (likely(ret == 0))
1728 atomic_inc(&bo->cpu_writers);
1729 ttm_bo_unreserve(bo);
1730 return ret;
1731}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001732EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001733
1734void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1735{
1736 if (atomic_dec_and_test(&bo->cpu_writers))
1737 wake_up_all(&bo->event_queue);
1738}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001739EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001740
1741/**
1742 * A buffer object shrink method that tries to swap out the first
1743 * buffer object on the bo_global::swap_lru list.
1744 */
1745
1746static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1747{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001748 struct ttm_bo_global *glob =
1749 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001750 struct ttm_buffer_object *bo;
1751 int ret = -EBUSY;
1752 int put_count;
1753 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1754
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001755 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001756 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001757 if (unlikely(list_empty(&glob->swap_lru))) {
1758 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001759 return -EBUSY;
1760 }
1761
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001762 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001763 struct ttm_buffer_object, swap);
1764 kref_get(&bo->list_kref);
1765
1766 /**
1767 * Reserve buffer. Since we unlock while sleeping, we need
1768 * to re-check that nobody removed us from the swap-list while
1769 * we slept.
1770 */
1771
1772 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1773 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001774 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001775 ttm_bo_wait_unreserved(bo, false);
1776 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001777 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001778 }
1779 }
1780
1781 BUG_ON(ret != 0);
1782 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001783 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001784
1785 while (put_count--)
1786 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1787
1788 /**
1789 * Wait for GPU, then move to system cached.
1790 */
1791
1792 spin_lock(&bo->lock);
1793 ret = ttm_bo_wait(bo, false, false, false);
1794 spin_unlock(&bo->lock);
1795
1796 if (unlikely(ret != 0))
1797 goto out;
1798
1799 if ((bo->mem.placement & swap_placement) != swap_placement) {
1800 struct ttm_mem_reg evict_mem;
1801
1802 evict_mem = bo->mem;
1803 evict_mem.mm_node = NULL;
1804 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1805 evict_mem.mem_type = TTM_PL_SYSTEM;
1806
1807 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1808 false, false);
1809 if (unlikely(ret != 0))
1810 goto out;
1811 }
1812
1813 ttm_bo_unmap_virtual(bo);
1814
1815 /**
1816 * Swap out. Buffer will be swapped in again as soon as
1817 * anyone tries to access a ttm page.
1818 */
1819
1820 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1821out:
1822
1823 /**
1824 *
1825 * Unreserve without putting on LRU to avoid swapping out an
1826 * already swapped buffer.
1827 */
1828
1829 atomic_set(&bo->reserved, 0);
1830 wake_up_all(&bo->event_queue);
1831 kref_put(&bo->list_kref, ttm_bo_release_list);
1832 return ret;
1833}
1834
1835void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1836{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001837 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001838 ;
1839}