blob: 40631e2866f8f7849c48e5c2fd65a1ab4cd3490a [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
Jerome Glisse5012f502009-12-10 18:07:26 +010074static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010075{
Jerome Glisse5012f502009-12-10 18:07:26 +010076 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
77
Jerome Glissefb53f862009-12-09 21:55:10 +010078 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
79 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
80 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
81 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
82 printk(KERN_ERR TTM_PFX " io_offset: 0x%08lX\n", man->io_offset);
83 printk(KERN_ERR TTM_PFX " io_size: %ld\n", man->io_size);
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010084 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
Jerome Glissefb53f862009-12-09 21:55:10 +010085 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
86 man->available_caching);
87 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
88 man->default_caching);
Jerome Glisse5012f502009-12-10 18:07:26 +010089 if (mem_type != TTM_PL_SYSTEM) {
90 spin_lock(&bdev->glob->lru_lock);
91 drm_mm_debug_table(&man->manager, TTM_PFX);
92 spin_unlock(&bdev->glob->lru_lock);
93 }
Jerome Glissefb53f862009-12-09 21:55:10 +010094}
95
96static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
97 struct ttm_placement *placement)
98{
Jerome Glissefb53f862009-12-09 21:55:10 +010099 int i, ret, mem_type;
100
Jerome Glisseeb6d2c32009-12-10 16:15:52 +0100101 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
Jerome Glissefb53f862009-12-09 21:55:10 +0100102 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;
Jerome Glissefb53f862009-12-09 21:55:10 +0100109 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
110 i, placement->placement[i], mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +0100111 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100112 }
113}
114
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200115static ssize_t ttm_bo_global_show(struct kobject *kobj,
116 struct attribute *attr,
117 char *buffer)
118{
119 struct ttm_bo_global *glob =
120 container_of(kobj, struct ttm_bo_global, kobj);
121
122 return snprintf(buffer, PAGE_SIZE, "%lu\n",
123 (unsigned long) atomic_read(&glob->bo_count));
124}
125
126static struct attribute *ttm_bo_global_attrs[] = {
127 &ttm_bo_count,
128 NULL
129};
130
Emese Revfy52cf25d2010-01-19 02:58:23 +0100131static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200132 .show = &ttm_bo_global_show
133};
134
135static struct kobj_type ttm_bo_glob_kobj_type = {
136 .release = &ttm_bo_global_kobj_release,
137 .sysfs_ops = &ttm_bo_global_ops,
138 .default_attrs = ttm_bo_global_attrs
139};
140
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200141
142static inline uint32_t ttm_bo_type_flags(unsigned type)
143{
144 return 1 << (type);
145}
146
147static void ttm_bo_release_list(struct kref *list_kref)
148{
149 struct ttm_buffer_object *bo =
150 container_of(list_kref, struct ttm_buffer_object, list_kref);
151 struct ttm_bo_device *bdev = bo->bdev;
152
153 BUG_ON(atomic_read(&bo->list_kref.refcount));
154 BUG_ON(atomic_read(&bo->kref.refcount));
155 BUG_ON(atomic_read(&bo->cpu_writers));
156 BUG_ON(bo->sync_obj != NULL);
157 BUG_ON(bo->mem.mm_node != NULL);
158 BUG_ON(!list_empty(&bo->lru));
159 BUG_ON(!list_empty(&bo->ddestroy));
160
161 if (bo->ttm)
162 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200163 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164 if (bo->destroy)
165 bo->destroy(bo);
166 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200167 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200168 kfree(bo);
169 }
170}
171
172int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
173{
174
175 if (interruptible) {
176 int ret = 0;
177
178 ret = wait_event_interruptible(bo->event_queue,
179 atomic_read(&bo->reserved) == 0);
180 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100181 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200182 } else {
183 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
184 }
185 return 0;
186}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000187EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200188
189static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
190{
191 struct ttm_bo_device *bdev = bo->bdev;
192 struct ttm_mem_type_manager *man;
193
194 BUG_ON(!atomic_read(&bo->reserved));
195
196 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
197
198 BUG_ON(!list_empty(&bo->lru));
199
200 man = &bdev->man[bo->mem.mem_type];
201 list_add_tail(&bo->lru, &man->lru);
202 kref_get(&bo->list_kref);
203
204 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200205 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200206 kref_get(&bo->list_kref);
207 }
208 }
209}
210
211/**
212 * Call with the lru_lock held.
213 */
214
215static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
216{
217 int put_count = 0;
218
219 if (!list_empty(&bo->swap)) {
220 list_del_init(&bo->swap);
221 ++put_count;
222 }
223 if (!list_empty(&bo->lru)) {
224 list_del_init(&bo->lru);
225 ++put_count;
226 }
227
228 /*
229 * TODO: Add a driver hook to delete from
230 * driver-specific LRU's here.
231 */
232
233 return put_count;
234}
235
236int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
237 bool interruptible,
238 bool no_wait, bool use_sequence, uint32_t sequence)
239{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200240 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200241 int ret;
242
243 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
244 if (use_sequence && bo->seq_valid &&
245 (sequence - bo->val_seq < (1 << 31))) {
246 return -EAGAIN;
247 }
248
249 if (no_wait)
250 return -EBUSY;
251
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200252 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200253 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200254 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200255
256 if (unlikely(ret))
257 return ret;
258 }
259
260 if (use_sequence) {
261 bo->val_seq = sequence;
262 bo->seq_valid = true;
263 } else {
264 bo->seq_valid = false;
265 }
266
267 return 0;
268}
269EXPORT_SYMBOL(ttm_bo_reserve);
270
271static void ttm_bo_ref_bug(struct kref *list_kref)
272{
273 BUG();
274}
275
276int ttm_bo_reserve(struct ttm_buffer_object *bo,
277 bool interruptible,
278 bool no_wait, bool use_sequence, uint32_t sequence)
279{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200280 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200281 int put_count = 0;
282 int ret;
283
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200284 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200285 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
286 sequence);
287 if (likely(ret == 0))
288 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200289 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200290
291 while (put_count--)
292 kref_put(&bo->list_kref, ttm_bo_ref_bug);
293
294 return ret;
295}
296
297void ttm_bo_unreserve(struct ttm_buffer_object *bo)
298{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200299 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200300
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200301 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200302 ttm_bo_add_to_lru(bo);
303 atomic_set(&bo->reserved, 0);
304 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200305 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200306}
307EXPORT_SYMBOL(ttm_bo_unreserve);
308
309/*
310 * Call bo->mutex locked.
311 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200312static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
313{
314 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200315 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200316 int ret = 0;
317 uint32_t page_flags = 0;
318
319 TTM_ASSERT_LOCKED(&bo->mutex);
320 bo->ttm = NULL;
321
Dave Airliead49f502009-07-10 22:36:26 +1000322 if (bdev->need_dma32)
323 page_flags |= TTM_PAGE_FLAG_DMA32;
324
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200325 switch (bo->type) {
326 case ttm_bo_type_device:
327 if (zero_alloc)
328 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
329 case ttm_bo_type_kernel:
330 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200331 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200332 if (unlikely(bo->ttm == NULL))
333 ret = -ENOMEM;
334 break;
335 case ttm_bo_type_user:
336 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
337 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200338 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000339 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200340 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000341 break;
342 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200343
344 ret = ttm_tt_set_user(bo->ttm, current,
345 bo->buffer_start, bo->num_pages);
346 if (unlikely(ret != 0))
347 ttm_tt_destroy(bo->ttm);
348 break;
349 default:
350 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
351 ret = -EINVAL;
352 break;
353 }
354
355 return ret;
356}
357
358static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
359 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000360 bool evict, bool interruptible,
361 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200362{
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))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000406 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200407 else if (bdev->driver->move)
408 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000409 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200410 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000411 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200412
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);
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100430 } else
431 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200432
433 return 0;
434
435out_err:
436 new_man = &bdev->man[bo->mem.mem_type];
437 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
438 ttm_tt_unbind(bo->ttm);
439 ttm_tt_destroy(bo->ttm);
440 bo->ttm = NULL;
441 }
442
443 return ret;
444}
445
446/**
447 * If bo idle, remove from delayed- and lru lists, and unref.
448 * If not idle, and already on delayed list, do nothing.
449 * If not idle, and not on delayed list, put on delayed list,
450 * up the list_kref and schedule a delayed list check.
451 */
452
453static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
454{
455 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200456 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200457 struct ttm_bo_driver *driver = bdev->driver;
458 int ret;
459
460 spin_lock(&bo->lock);
461 (void) ttm_bo_wait(bo, false, false, !remove_all);
462
463 if (!bo->sync_obj) {
464 int put_count;
465
466 spin_unlock(&bo->lock);
467
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200468 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100469 put_count = ttm_bo_del_from_lru(bo);
470
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200471 ret = ttm_bo_reserve_locked(bo, false, false, false, 0);
472 BUG_ON(ret);
473 if (bo->ttm)
474 ttm_tt_unbind(bo->ttm);
475
476 if (!list_empty(&bo->ddestroy)) {
477 list_del_init(&bo->ddestroy);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100478 ++put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200479 }
480 if (bo->mem.mm_node) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100481 bo->mem.mm_node->private = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200482 drm_mm_put_block(bo->mem.mm_node);
483 bo->mem.mm_node = NULL;
484 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200485 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200486
487 atomic_set(&bo->reserved, 0);
488
489 while (put_count--)
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100490 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200491
492 return 0;
493 }
494
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200495 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200496 if (list_empty(&bo->ddestroy)) {
497 void *sync_obj = bo->sync_obj;
498 void *sync_obj_arg = bo->sync_obj_arg;
499
500 kref_get(&bo->list_kref);
501 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200502 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200503 spin_unlock(&bo->lock);
504
505 if (sync_obj)
506 driver->sync_obj_flush(sync_obj, sync_obj_arg);
507 schedule_delayed_work(&bdev->wq,
508 ((HZ / 100) < 1) ? 1 : HZ / 100);
509 ret = 0;
510
511 } else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200512 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200513 spin_unlock(&bo->lock);
514 ret = -EBUSY;
515 }
516
517 return ret;
518}
519
520/**
521 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
522 * encountered buffers.
523 */
524
525static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
526{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200527 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100528 struct ttm_buffer_object *entry = NULL;
529 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200530
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200531 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100532 if (list_empty(&bdev->ddestroy))
533 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200534
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100535 entry = list_first_entry(&bdev->ddestroy,
536 struct ttm_buffer_object, ddestroy);
537 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200538
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100539 for (;;) {
540 struct ttm_buffer_object *nentry = NULL;
541
542 if (entry->ddestroy.next != &bdev->ddestroy) {
543 nentry = list_first_entry(&entry->ddestroy,
544 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200545 kref_get(&nentry->list_kref);
546 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200547
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200548 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200549 ret = ttm_bo_cleanup_refs(entry, remove_all);
550 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100551 entry = nentry;
552
553 if (ret || !entry)
554 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200555
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200556 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100557 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200558 break;
559 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200560
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100561out_unlock:
562 spin_unlock(&glob->lru_lock);
563out:
564 if (entry)
565 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200566 return ret;
567}
568
569static void ttm_bo_delayed_workqueue(struct work_struct *work)
570{
571 struct ttm_bo_device *bdev =
572 container_of(work, struct ttm_bo_device, wq.work);
573
574 if (ttm_bo_delayed_delete(bdev, false)) {
575 schedule_delayed_work(&bdev->wq,
576 ((HZ / 100) < 1) ? 1 : HZ / 100);
577 }
578}
579
580static void ttm_bo_release(struct kref *kref)
581{
582 struct ttm_buffer_object *bo =
583 container_of(kref, struct ttm_buffer_object, kref);
584 struct ttm_bo_device *bdev = bo->bdev;
585
586 if (likely(bo->vm_node != NULL)) {
587 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
588 drm_mm_put_block(bo->vm_node);
589 bo->vm_node = NULL;
590 }
591 write_unlock(&bdev->vm_lock);
592 ttm_bo_cleanup_refs(bo, false);
593 kref_put(&bo->list_kref, ttm_bo_release_list);
594 write_lock(&bdev->vm_lock);
595}
596
597void ttm_bo_unref(struct ttm_buffer_object **p_bo)
598{
599 struct ttm_buffer_object *bo = *p_bo;
600 struct ttm_bo_device *bdev = bo->bdev;
601
602 *p_bo = NULL;
603 write_lock(&bdev->vm_lock);
604 kref_put(&bo->kref, ttm_bo_release);
605 write_unlock(&bdev->vm_lock);
606}
607EXPORT_SYMBOL(ttm_bo_unref);
608
Jerome Glisseca262a9992009-12-08 15:33:32 +0100609static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000610 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200611{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200612 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200613 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200614 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100615 struct ttm_placement placement;
616 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200617
618 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000619 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200620 spin_unlock(&bo->lock);
621
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200622 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100623 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200624 printk(KERN_ERR TTM_PFX
625 "Failed to expire sync object before "
626 "buffer eviction.\n");
627 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200628 goto out;
629 }
630
631 BUG_ON(!atomic_read(&bo->reserved));
632
633 evict_mem = bo->mem;
634 evict_mem.mm_node = NULL;
635
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100636 placement.fpfn = 0;
637 placement.lpfn = 0;
638 placement.num_placement = 0;
639 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100640 bdev->driver->evict_flags(bo, &placement);
641 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000642 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200643 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100644 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200645 printk(KERN_ERR TTM_PFX
646 "Failed to find memory space for "
647 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100648 ttm_bo_mem_space_debug(bo, &placement);
649 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200650 goto out;
651 }
652
653 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000654 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200655 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100656 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200657 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +0100658 spin_lock(&glob->lru_lock);
659 if (evict_mem.mm_node) {
660 evict_mem.mm_node->private = NULL;
661 drm_mm_put_block(evict_mem.mm_node);
662 evict_mem.mm_node = NULL;
663 }
664 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200665 goto out;
666 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200667 bo->evicted = true;
668out:
669 return ret;
670}
671
Jerome Glisseca262a9992009-12-08 15:33:32 +0100672static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
673 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000674 bool interruptible, bool no_wait_reserve,
675 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100676{
677 struct ttm_bo_global *glob = bdev->glob;
678 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
679 struct ttm_buffer_object *bo;
680 int ret, put_count = 0;
681
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100682retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100683 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100684 if (list_empty(&man->lru)) {
685 spin_unlock(&glob->lru_lock);
686 return -EBUSY;
687 }
688
Jerome Glisseca262a9992009-12-08 15:33:32 +0100689 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
690 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100691
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000692 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100693
694 if (unlikely(ret == -EBUSY)) {
695 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000696 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100697 ret = ttm_bo_wait_unreserved(bo, interruptible);
698
699 kref_put(&bo->list_kref, ttm_bo_release_list);
700
701 /**
702 * We *need* to retry after releasing the lru lock.
703 */
704
705 if (unlikely(ret != 0))
706 return ret;
707 goto retry;
708 }
709
710 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100711 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100712
713 BUG_ON(ret != 0);
714
Jerome Glisseca262a9992009-12-08 15:33:32 +0100715 while (put_count--)
716 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100717
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000718 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100719 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100720
Jerome Glisseca262a9992009-12-08 15:33:32 +0100721 kref_put(&bo->list_kref, ttm_bo_release_list);
722 return ret;
723}
724
725static int ttm_bo_man_get_node(struct ttm_buffer_object *bo,
726 struct ttm_mem_type_manager *man,
727 struct ttm_placement *placement,
728 struct ttm_mem_reg *mem,
729 struct drm_mm_node **node)
730{
731 struct ttm_bo_global *glob = bo->glob;
732 unsigned long lpfn;
733 int ret;
734
735 lpfn = placement->lpfn;
736 if (!lpfn)
737 lpfn = man->size;
738 *node = NULL;
739 do {
740 ret = drm_mm_pre_get(&man->manager);
741 if (unlikely(ret))
742 return ret;
743
744 spin_lock(&glob->lru_lock);
745 *node = drm_mm_search_free_in_range(&man->manager,
746 mem->num_pages, mem->page_alignment,
747 placement->fpfn, lpfn, 1);
748 if (unlikely(*node == NULL)) {
749 spin_unlock(&glob->lru_lock);
750 return 0;
751 }
752 *node = drm_mm_get_block_atomic_range(*node, mem->num_pages,
753 mem->page_alignment,
754 placement->fpfn,
755 lpfn);
756 spin_unlock(&glob->lru_lock);
757 } while (*node == NULL);
758 return 0;
759}
760
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200761/**
762 * Repeatedly evict memory from the LRU for @mem_type until we create enough
763 * space, or we've evicted everything and there isn't enough space.
764 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100765static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
766 uint32_t mem_type,
767 struct ttm_placement *placement,
768 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000769 bool interruptible,
770 bool no_wait_reserve,
771 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200772{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100773 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200774 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200775 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Jerome Glisseca262a9992009-12-08 15:33:32 +0100776 struct drm_mm_node *node;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200777 int ret;
778
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200779 do {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100780 ret = ttm_bo_man_get_node(bo, man, placement, mem, &node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200781 if (unlikely(ret != 0))
782 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100783 if (node)
784 break;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200785 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100786 if (list_empty(&man->lru)) {
787 spin_unlock(&glob->lru_lock);
788 break;
789 }
790 spin_unlock(&glob->lru_lock);
791 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000792 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100793 if (unlikely(ret != 0))
794 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200795 } while (1);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100796 if (node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200797 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200798 mem->mm_node = node;
799 mem->mem_type = mem_type;
800 return 0;
801}
802
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200803static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
804 uint32_t cur_placement,
805 uint32_t proposed_placement)
806{
807 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
808 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
809
810 /**
811 * Keep current caching if possible.
812 */
813
814 if ((cur_placement & caching) != 0)
815 result |= (cur_placement & caching);
816 else if ((man->default_caching & caching) != 0)
817 result |= man->default_caching;
818 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
819 result |= TTM_PL_FLAG_CACHED;
820 else if ((TTM_PL_FLAG_WC & caching) != 0)
821 result |= TTM_PL_FLAG_WC;
822 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
823 result |= TTM_PL_FLAG_UNCACHED;
824
825 return result;
826}
827
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200828static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
829 bool disallow_fixed,
830 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200831 uint32_t proposed_placement,
832 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200833{
834 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
835
836 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
837 return false;
838
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200839 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200840 return false;
841
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200842 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200843 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200844
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200845 cur_flags |= (proposed_placement & man->available_caching);
846
847 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200848 return true;
849}
850
851/**
852 * Creates space for memory region @mem according to its type.
853 *
854 * This function first searches for free space in compatible memory types in
855 * the priority order defined by the driver. If free space isn't found, then
856 * ttm_bo_mem_force_space is attempted in priority order to evict and find
857 * space.
858 */
859int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100860 struct ttm_placement *placement,
861 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000862 bool interruptible, bool no_wait_reserve,
863 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200864{
865 struct ttm_bo_device *bdev = bo->bdev;
866 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867 uint32_t mem_type = TTM_PL_SYSTEM;
868 uint32_t cur_flags = 0;
869 bool type_found = false;
870 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100871 bool has_erestartsys = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200872 struct drm_mm_node *node = NULL;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100873 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200874
875 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000876 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100877 ret = ttm_mem_type_from_flags(placement->placement[i],
878 &mem_type);
879 if (ret)
880 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200881 man = &bdev->man[mem_type];
882
883 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100884 bo->type == ttm_bo_type_user,
885 mem_type,
886 placement->placement[i],
887 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200888
889 if (!type_ok)
890 continue;
891
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200892 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
893 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100894 /*
895 * Use the access and other non-mapping-related flag bits from
896 * the memory placement flags to the current flags
897 */
898 ttm_flag_masked(&cur_flags, placement->placement[i],
899 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200900
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200901 if (mem_type == TTM_PL_SYSTEM)
902 break;
903
904 if (man->has_type && man->use_type) {
905 type_found = true;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100906 ret = ttm_bo_man_get_node(bo, man, placement, mem,
907 &node);
908 if (unlikely(ret))
909 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200910 }
911 if (node)
912 break;
913 }
914
915 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) {
916 mem->mm_node = node;
917 mem->mem_type = mem_type;
918 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100919 if (node)
920 node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200921 return 0;
922 }
923
924 if (!type_found)
925 return -EINVAL;
926
Dave Airlieb6637522009-12-14 14:51:35 +1000927 for (i = 0; i < placement->num_busy_placement; ++i) {
928 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100929 &mem_type);
930 if (ret)
931 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200932 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200933 if (!man->has_type)
934 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200935 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100936 bo->type == ttm_bo_type_user,
937 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000938 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100939 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200940 continue;
941
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200942 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
943 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100944 /*
945 * Use the access and other non-mapping-related flag bits from
946 * the memory placement flags to the current flags
947 */
Dave Airlieb6637522009-12-14 14:51:35 +1000948 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100949 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200950
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100951
952 if (mem_type == TTM_PL_SYSTEM) {
953 mem->mem_type = mem_type;
954 mem->placement = cur_flags;
955 mem->mm_node = NULL;
956 return 0;
957 }
958
Jerome Glisseca262a9992009-12-08 15:33:32 +0100959 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000960 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200961 if (ret == 0 && mem->mm_node) {
962 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100963 mem->mm_node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964 return 0;
965 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100966 if (ret == -ERESTARTSYS)
967 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200968 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100969 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970 return ret;
971}
972EXPORT_SYMBOL(ttm_bo_mem_space);
973
974int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
975{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
977 return -EBUSY;
978
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100979 return wait_event_interruptible(bo->event_queue,
980 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200981}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000982EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200983
984int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100985 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000986 bool interruptible, bool no_wait_reserve,
987 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200989 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200990 int ret = 0;
991 struct ttm_mem_reg mem;
992
993 BUG_ON(!atomic_read(&bo->reserved));
994
995 /*
996 * FIXME: It's possible to pipeline buffer moves.
997 * Have the driver move function wait for idle when necessary,
998 * instead of doing it here.
999 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001000 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001001 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001002 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001003 if (ret)
1004 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005 mem.num_pages = bo->num_pages;
1006 mem.size = mem.num_pages << PAGE_SHIFT;
1007 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001008 /*
1009 * Determine where to move the buffer.
1010 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001011 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001012 if (ret)
1013 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001014 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015out_unlock:
1016 if (ret && mem.mm_node) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001017 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001018 mem.mm_node->private = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019 drm_mm_put_block(mem.mm_node);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001020 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001021 }
1022 return ret;
1023}
1024
Jerome Glisseca262a9992009-12-08 15:33:32 +01001025static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001026 struct ttm_mem_reg *mem)
1027{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001028 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001029 struct drm_mm_node *node = mem->mm_node;
1030
1031 if (node && placement->lpfn != 0 &&
1032 (node->start < placement->fpfn ||
1033 node->start + node->size > placement->lpfn))
1034 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001035
Jerome Glisseca262a9992009-12-08 15:33:32 +01001036 for (i = 0; i < placement->num_placement; i++) {
1037 if ((placement->placement[i] & mem->placement &
1038 TTM_PL_MASK_CACHING) &&
1039 (placement->placement[i] & mem->placement &
1040 TTM_PL_MASK_MEM))
1041 return i;
1042 }
1043 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044}
1045
Jerome Glisse09855ac2009-12-10 17:16:27 +01001046int ttm_bo_validate(struct ttm_buffer_object *bo,
1047 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001048 bool interruptible, bool no_wait_reserve,
1049 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050{
1051 int ret;
1052
1053 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001054 /* Check that range is valid */
1055 if (placement->lpfn || placement->fpfn)
1056 if (placement->fpfn > placement->lpfn ||
1057 (placement->lpfn - placement->fpfn) < bo->num_pages)
1058 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001059 /*
1060 * Check whether we need to move buffer.
1061 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001062 ret = ttm_bo_mem_compat(placement, &bo->mem);
1063 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001064 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001065 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001066 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001067 } else {
1068 /*
1069 * Use the access and other non-mapping-related flag bits from
1070 * the compatible memory placement flags to the active flags
1071 */
1072 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1073 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001075 /*
1076 * We might need to add a TTM.
1077 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001078 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1079 ret = ttm_bo_add_ttm(bo, true);
1080 if (ret)
1081 return ret;
1082 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001083 return 0;
1084}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001085EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086
Jerome Glisse09855ac2009-12-10 17:16:27 +01001087int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1088 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001090 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001091
Jerome Glisse09855ac2009-12-10 17:16:27 +01001092 if (placement->fpfn || placement->lpfn) {
1093 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1094 printk(KERN_ERR TTM_PFX "Page number range to small "
1095 "Need %lu pages, range is [%u, %u]\n",
1096 bo->mem.num_pages, placement->fpfn,
1097 placement->lpfn);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098 return -EINVAL;
1099 }
Jerome Glisse09855ac2009-12-10 17:16:27 +01001100 }
1101 for (i = 0; i < placement->num_placement; i++) {
1102 if (!capable(CAP_SYS_ADMIN)) {
1103 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1104 printk(KERN_ERR TTM_PFX "Need to be root to "
1105 "modify NO_EVICT status.\n");
1106 return -EINVAL;
1107 }
1108 }
1109 }
1110 for (i = 0; i < placement->num_busy_placement; i++) {
1111 if (!capable(CAP_SYS_ADMIN)) {
1112 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1113 printk(KERN_ERR TTM_PFX "Need to be root to "
1114 "modify NO_EVICT status.\n");
1115 return -EINVAL;
1116 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117 }
1118 }
1119 return 0;
1120}
1121
Jerome Glisse09855ac2009-12-10 17:16:27 +01001122int ttm_bo_init(struct ttm_bo_device *bdev,
1123 struct ttm_buffer_object *bo,
1124 unsigned long size,
1125 enum ttm_bo_type type,
1126 struct ttm_placement *placement,
1127 uint32_t page_alignment,
1128 unsigned long buffer_start,
1129 bool interruptible,
1130 struct file *persistant_swap_storage,
1131 size_t acc_size,
1132 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001133{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001134 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 unsigned long num_pages;
1136
1137 size += buffer_start & ~PAGE_MASK;
1138 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1139 if (num_pages == 0) {
1140 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1141 return -EINVAL;
1142 }
1143 bo->destroy = destroy;
1144
1145 spin_lock_init(&bo->lock);
1146 kref_init(&bo->kref);
1147 kref_init(&bo->list_kref);
1148 atomic_set(&bo->cpu_writers, 0);
1149 atomic_set(&bo->reserved, 1);
1150 init_waitqueue_head(&bo->event_queue);
1151 INIT_LIST_HEAD(&bo->lru);
1152 INIT_LIST_HEAD(&bo->ddestroy);
1153 INIT_LIST_HEAD(&bo->swap);
1154 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001155 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001156 bo->type = type;
1157 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001158 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001159 bo->mem.mem_type = TTM_PL_SYSTEM;
1160 bo->mem.num_pages = bo->num_pages;
1161 bo->mem.mm_node = NULL;
1162 bo->mem.page_alignment = page_alignment;
1163 bo->buffer_start = buffer_start & PAGE_MASK;
1164 bo->priv_flags = 0;
1165 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1166 bo->seq_valid = false;
1167 bo->persistant_swap_storage = persistant_swap_storage;
1168 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001169 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001170
Jerome Glisse09855ac2009-12-10 17:16:27 +01001171 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001172 if (unlikely(ret != 0))
1173 goto out_err;
1174
1175 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001176 * For ttm_bo_type_device buffers, allocate
1177 * address space from the device.
1178 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 if (bo->type == ttm_bo_type_device) {
1180 ret = ttm_bo_setup_vm(bo);
1181 if (ret)
1182 goto out_err;
1183 }
1184
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001185 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001186 if (ret)
1187 goto out_err;
1188
1189 ttm_bo_unreserve(bo);
1190 return 0;
1191
1192out_err:
1193 ttm_bo_unreserve(bo);
1194 ttm_bo_unref(&bo);
1195
1196 return ret;
1197}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001198EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001200static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001201 unsigned long num_pages)
1202{
1203 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1204 PAGE_MASK;
1205
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001206 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001207}
1208
Jerome Glisse09855ac2009-12-10 17:16:27 +01001209int ttm_bo_create(struct ttm_bo_device *bdev,
1210 unsigned long size,
1211 enum ttm_bo_type type,
1212 struct ttm_placement *placement,
1213 uint32_t page_alignment,
1214 unsigned long buffer_start,
1215 bool interruptible,
1216 struct file *persistant_swap_storage,
1217 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001218{
1219 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001220 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001221 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001222
1223 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001224 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001225 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001226 if (unlikely(ret != 0))
1227 return ret;
1228
1229 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1230
1231 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001232 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001233 return -ENOMEM;
1234 }
1235
Jerome Glisse09855ac2009-12-10 17:16:27 +01001236 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1237 buffer_start, interruptible,
1238 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001239 if (likely(ret == 0))
1240 *p_bo = bo;
1241
1242 return ret;
1243}
1244
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001245static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001246 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001247{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001248 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001249 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001250 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251
1252 /*
1253 * Can't use standard list traversal since we're unlocking.
1254 */
1255
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001256 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001257 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001258 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001259 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001260 if (ret) {
1261 if (allow_errors) {
1262 return ret;
1263 } else {
1264 printk(KERN_ERR TTM_PFX
1265 "Cleanup eviction failed\n");
1266 }
1267 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001268 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001270 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271 return 0;
1272}
1273
1274int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1275{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001276 struct ttm_bo_global *glob = bdev->glob;
Roel Kluinc96e7c72009-08-03 14:22:53 +02001277 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001278 int ret = -EINVAL;
1279
1280 if (mem_type >= TTM_NUM_MEM_TYPES) {
1281 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1282 return ret;
1283 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001284 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001285
1286 if (!man->has_type) {
1287 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1288 "memory manager type %u\n", mem_type);
1289 return ret;
1290 }
1291
1292 man->use_type = false;
1293 man->has_type = false;
1294
1295 ret = 0;
1296 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001297 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001299 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001300 if (drm_mm_clean(&man->manager))
1301 drm_mm_takedown(&man->manager);
1302 else
1303 ret = -EBUSY;
1304
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001305 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306 }
1307
1308 return ret;
1309}
1310EXPORT_SYMBOL(ttm_bo_clean_mm);
1311
1312int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1313{
1314 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1315
1316 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1317 printk(KERN_ERR TTM_PFX
1318 "Illegal memory manager memory type %u.\n",
1319 mem_type);
1320 return -EINVAL;
1321 }
1322
1323 if (!man->has_type) {
1324 printk(KERN_ERR TTM_PFX
1325 "Memory type %u has not been initialized.\n",
1326 mem_type);
1327 return 0;
1328 }
1329
Jerome Glisseca262a9992009-12-08 15:33:32 +01001330 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001331}
1332EXPORT_SYMBOL(ttm_bo_evict_mm);
1333
1334int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001335 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001336{
1337 int ret = -EINVAL;
1338 struct ttm_mem_type_manager *man;
1339
1340 if (type >= TTM_NUM_MEM_TYPES) {
1341 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1342 return ret;
1343 }
1344
1345 man = &bdev->man[type];
1346 if (man->has_type) {
1347 printk(KERN_ERR TTM_PFX
1348 "Memory manager already initialized for type %d\n",
1349 type);
1350 return ret;
1351 }
1352
1353 ret = bdev->driver->init_mem_type(bdev, type, man);
1354 if (ret)
1355 return ret;
1356
1357 ret = 0;
1358 if (type != TTM_PL_SYSTEM) {
1359 if (!p_size) {
1360 printk(KERN_ERR TTM_PFX
1361 "Zero size memory manager type %d\n",
1362 type);
1363 return ret;
1364 }
Jerome Glisseca262a9992009-12-08 15:33:32 +01001365 ret = drm_mm_init(&man->manager, 0, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001366 if (ret)
1367 return ret;
1368 }
1369 man->has_type = true;
1370 man->use_type = true;
1371 man->size = p_size;
1372
1373 INIT_LIST_HEAD(&man->lru);
1374
1375 return 0;
1376}
1377EXPORT_SYMBOL(ttm_bo_init_mm);
1378
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001379static void ttm_bo_global_kobj_release(struct kobject *kobj)
1380{
1381 struct ttm_bo_global *glob =
1382 container_of(kobj, struct ttm_bo_global, kobj);
1383
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001384 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1385 __free_page(glob->dummy_read_page);
1386 kfree(glob);
1387}
1388
1389void ttm_bo_global_release(struct ttm_global_reference *ref)
1390{
1391 struct ttm_bo_global *glob = ref->object;
1392
1393 kobject_del(&glob->kobj);
1394 kobject_put(&glob->kobj);
1395}
1396EXPORT_SYMBOL(ttm_bo_global_release);
1397
1398int ttm_bo_global_init(struct ttm_global_reference *ref)
1399{
1400 struct ttm_bo_global_ref *bo_ref =
1401 container_of(ref, struct ttm_bo_global_ref, ref);
1402 struct ttm_bo_global *glob = ref->object;
1403 int ret;
1404
1405 mutex_init(&glob->device_list_mutex);
1406 spin_lock_init(&glob->lru_lock);
1407 glob->mem_glob = bo_ref->mem_glob;
1408 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1409
1410 if (unlikely(glob->dummy_read_page == NULL)) {
1411 ret = -ENOMEM;
1412 goto out_no_drp;
1413 }
1414
1415 INIT_LIST_HEAD(&glob->swap_lru);
1416 INIT_LIST_HEAD(&glob->device_list);
1417
1418 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1419 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1420 if (unlikely(ret != 0)) {
1421 printk(KERN_ERR TTM_PFX
1422 "Could not register buffer object swapout.\n");
1423 goto out_no_shrink;
1424 }
1425
1426 glob->ttm_bo_extra_size =
1427 ttm_round_pot(sizeof(struct ttm_tt)) +
1428 ttm_round_pot(sizeof(struct ttm_backend));
1429
1430 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1431 ttm_round_pot(sizeof(struct ttm_buffer_object));
1432
1433 atomic_set(&glob->bo_count, 0);
1434
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001435 ret = kobject_init_and_add(
1436 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001437 if (unlikely(ret != 0))
1438 kobject_put(&glob->kobj);
1439 return ret;
1440out_no_shrink:
1441 __free_page(glob->dummy_read_page);
1442out_no_drp:
1443 kfree(glob);
1444 return ret;
1445}
1446EXPORT_SYMBOL(ttm_bo_global_init);
1447
1448
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001449int ttm_bo_device_release(struct ttm_bo_device *bdev)
1450{
1451 int ret = 0;
1452 unsigned i = TTM_NUM_MEM_TYPES;
1453 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001454 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001455
1456 while (i--) {
1457 man = &bdev->man[i];
1458 if (man->has_type) {
1459 man->use_type = false;
1460 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1461 ret = -EBUSY;
1462 printk(KERN_ERR TTM_PFX
1463 "DRM memory manager type %d "
1464 "is not clean.\n", i);
1465 }
1466 man->has_type = false;
1467 }
1468 }
1469
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001470 mutex_lock(&glob->device_list_mutex);
1471 list_del(&bdev->device_list);
1472 mutex_unlock(&glob->device_list_mutex);
1473
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001474 if (!cancel_delayed_work(&bdev->wq))
1475 flush_scheduled_work();
1476
1477 while (ttm_bo_delayed_delete(bdev, true))
1478 ;
1479
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001480 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001481 if (list_empty(&bdev->ddestroy))
1482 TTM_DEBUG("Delayed destroy list was clean\n");
1483
1484 if (list_empty(&bdev->man[0].lru))
1485 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001486 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001487
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1489 write_lock(&bdev->vm_lock);
1490 drm_mm_takedown(&bdev->addr_space_mm);
1491 write_unlock(&bdev->vm_lock);
1492
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001493 return ret;
1494}
1495EXPORT_SYMBOL(ttm_bo_device_release);
1496
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001498 struct ttm_bo_global *glob,
1499 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001500 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001501 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001502{
1503 int ret = -EINVAL;
1504
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001505 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001506 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001507
1508 memset(bdev->man, 0, sizeof(bdev->man));
1509
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510 /*
1511 * Initialize the system memory buffer type.
1512 * Other types need to be driver / IOCTL initialized.
1513 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001514 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001515 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001516 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517
1518 bdev->addr_space_rb = RB_ROOT;
1519 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1520 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001521 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522
1523 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1524 bdev->nice_mode = true;
1525 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001527 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001528 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001530 mutex_lock(&glob->device_list_mutex);
1531 list_add_tail(&bdev->device_list, &glob->device_list);
1532 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001533
1534 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001535out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001537out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538 return ret;
1539}
1540EXPORT_SYMBOL(ttm_bo_device_init);
1541
1542/*
1543 * buffer object vm functions.
1544 */
1545
1546bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1547{
1548 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1549
1550 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1551 if (mem->mem_type == TTM_PL_SYSTEM)
1552 return false;
1553
1554 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1555 return false;
1556
1557 if (mem->placement & TTM_PL_FLAG_CACHED)
1558 return false;
1559 }
1560 return true;
1561}
1562
1563int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
1564 struct ttm_mem_reg *mem,
1565 unsigned long *bus_base,
1566 unsigned long *bus_offset, unsigned long *bus_size)
1567{
1568 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1569
1570 *bus_size = 0;
1571 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1572 return -EINVAL;
1573
1574 if (ttm_mem_reg_is_pci(bdev, mem)) {
1575 *bus_offset = mem->mm_node->start << PAGE_SHIFT;
1576 *bus_size = mem->num_pages << PAGE_SHIFT;
1577 *bus_base = man->io_offset;
1578 }
1579
1580 return 0;
1581}
1582
1583void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1584{
1585 struct ttm_bo_device *bdev = bo->bdev;
1586 loff_t offset = (loff_t) bo->addr_space_offset;
1587 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1588
1589 if (!bdev->dev_mapping)
1590 return;
1591
1592 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1593}
Dave Airliee024e112009-06-24 09:48:08 +10001594EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001595
1596static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1597{
1598 struct ttm_bo_device *bdev = bo->bdev;
1599 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1600 struct rb_node *parent = NULL;
1601 struct ttm_buffer_object *cur_bo;
1602 unsigned long offset = bo->vm_node->start;
1603 unsigned long cur_offset;
1604
1605 while (*cur) {
1606 parent = *cur;
1607 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1608 cur_offset = cur_bo->vm_node->start;
1609 if (offset < cur_offset)
1610 cur = &parent->rb_left;
1611 else if (offset > cur_offset)
1612 cur = &parent->rb_right;
1613 else
1614 BUG();
1615 }
1616
1617 rb_link_node(&bo->vm_rb, parent, cur);
1618 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1619}
1620
1621/**
1622 * ttm_bo_setup_vm:
1623 *
1624 * @bo: the buffer to allocate address space for
1625 *
1626 * Allocate address space in the drm device so that applications
1627 * can mmap the buffer and access the contents. This only
1628 * applies to ttm_bo_type_device objects as others are not
1629 * placed in the drm device address space.
1630 */
1631
1632static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1633{
1634 struct ttm_bo_device *bdev = bo->bdev;
1635 int ret;
1636
1637retry_pre_get:
1638 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1639 if (unlikely(ret != 0))
1640 return ret;
1641
1642 write_lock(&bdev->vm_lock);
1643 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1644 bo->mem.num_pages, 0, 0);
1645
1646 if (unlikely(bo->vm_node == NULL)) {
1647 ret = -ENOMEM;
1648 goto out_unlock;
1649 }
1650
1651 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1652 bo->mem.num_pages, 0);
1653
1654 if (unlikely(bo->vm_node == NULL)) {
1655 write_unlock(&bdev->vm_lock);
1656 goto retry_pre_get;
1657 }
1658
1659 ttm_bo_vm_insert_rb(bo);
1660 write_unlock(&bdev->vm_lock);
1661 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1662
1663 return 0;
1664out_unlock:
1665 write_unlock(&bdev->vm_lock);
1666 return ret;
1667}
1668
1669int ttm_bo_wait(struct ttm_buffer_object *bo,
1670 bool lazy, bool interruptible, bool no_wait)
1671{
1672 struct ttm_bo_driver *driver = bo->bdev->driver;
1673 void *sync_obj;
1674 void *sync_obj_arg;
1675 int ret = 0;
1676
1677 if (likely(bo->sync_obj == NULL))
1678 return 0;
1679
1680 while (bo->sync_obj) {
1681
1682 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1683 void *tmp_obj = bo->sync_obj;
1684 bo->sync_obj = NULL;
1685 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1686 spin_unlock(&bo->lock);
1687 driver->sync_obj_unref(&tmp_obj);
1688 spin_lock(&bo->lock);
1689 continue;
1690 }
1691
1692 if (no_wait)
1693 return -EBUSY;
1694
1695 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1696 sync_obj_arg = bo->sync_obj_arg;
1697 spin_unlock(&bo->lock);
1698 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1699 lazy, interruptible);
1700 if (unlikely(ret != 0)) {
1701 driver->sync_obj_unref(&sync_obj);
1702 spin_lock(&bo->lock);
1703 return ret;
1704 }
1705 spin_lock(&bo->lock);
1706 if (likely(bo->sync_obj == sync_obj &&
1707 bo->sync_obj_arg == sync_obj_arg)) {
1708 void *tmp_obj = bo->sync_obj;
1709 bo->sync_obj = NULL;
1710 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1711 &bo->priv_flags);
1712 spin_unlock(&bo->lock);
1713 driver->sync_obj_unref(&sync_obj);
1714 driver->sync_obj_unref(&tmp_obj);
1715 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001716 } else {
1717 spin_unlock(&bo->lock);
1718 driver->sync_obj_unref(&sync_obj);
1719 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001720 }
1721 }
1722 return 0;
1723}
1724EXPORT_SYMBOL(ttm_bo_wait);
1725
1726void ttm_bo_unblock_reservation(struct ttm_buffer_object *bo)
1727{
1728 atomic_set(&bo->reserved, 0);
1729 wake_up_all(&bo->event_queue);
1730}
1731
1732int ttm_bo_block_reservation(struct ttm_buffer_object *bo, bool interruptible,
1733 bool no_wait)
1734{
1735 int ret;
1736
1737 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
1738 if (no_wait)
1739 return -EBUSY;
1740 else if (interruptible) {
1741 ret = wait_event_interruptible
1742 (bo->event_queue, atomic_read(&bo->reserved) == 0);
1743 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001744 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001745 } else {
1746 wait_event(bo->event_queue,
1747 atomic_read(&bo->reserved) == 0);
1748 }
1749 }
1750 return 0;
1751}
1752
1753int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1754{
1755 int ret = 0;
1756
1757 /*
1758 * Using ttm_bo_reserve instead of ttm_bo_block_reservation
1759 * makes sure the lru lists are updated.
1760 */
1761
1762 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1763 if (unlikely(ret != 0))
1764 return ret;
1765 spin_lock(&bo->lock);
1766 ret = ttm_bo_wait(bo, false, true, no_wait);
1767 spin_unlock(&bo->lock);
1768 if (likely(ret == 0))
1769 atomic_inc(&bo->cpu_writers);
1770 ttm_bo_unreserve(bo);
1771 return ret;
1772}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001773EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001774
1775void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1776{
1777 if (atomic_dec_and_test(&bo->cpu_writers))
1778 wake_up_all(&bo->event_queue);
1779}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001780EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001781
1782/**
1783 * A buffer object shrink method that tries to swap out the first
1784 * buffer object on the bo_global::swap_lru list.
1785 */
1786
1787static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1788{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001789 struct ttm_bo_global *glob =
1790 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001791 struct ttm_buffer_object *bo;
1792 int ret = -EBUSY;
1793 int put_count;
1794 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1795
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001796 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001797 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001798 if (unlikely(list_empty(&glob->swap_lru))) {
1799 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800 return -EBUSY;
1801 }
1802
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001803 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001804 struct ttm_buffer_object, swap);
1805 kref_get(&bo->list_kref);
1806
1807 /**
1808 * Reserve buffer. Since we unlock while sleeping, we need
1809 * to re-check that nobody removed us from the swap-list while
1810 * we slept.
1811 */
1812
1813 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1814 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001815 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001816 ttm_bo_wait_unreserved(bo, false);
1817 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001818 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001819 }
1820 }
1821
1822 BUG_ON(ret != 0);
1823 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001824 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001825
1826 while (put_count--)
1827 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1828
1829 /**
1830 * Wait for GPU, then move to system cached.
1831 */
1832
1833 spin_lock(&bo->lock);
1834 ret = ttm_bo_wait(bo, false, false, false);
1835 spin_unlock(&bo->lock);
1836
1837 if (unlikely(ret != 0))
1838 goto out;
1839
1840 if ((bo->mem.placement & swap_placement) != swap_placement) {
1841 struct ttm_mem_reg evict_mem;
1842
1843 evict_mem = bo->mem;
1844 evict_mem.mm_node = NULL;
1845 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1846 evict_mem.mem_type = TTM_PL_SYSTEM;
1847
1848 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001849 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001850 if (unlikely(ret != 0))
1851 goto out;
1852 }
1853
1854 ttm_bo_unmap_virtual(bo);
1855
1856 /**
1857 * Swap out. Buffer will be swapped in again as soon as
1858 * anyone tries to access a ttm page.
1859 */
1860
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001861 if (bo->bdev->driver->swap_notify)
1862 bo->bdev->driver->swap_notify(bo);
1863
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001864 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1865out:
1866
1867 /**
1868 *
1869 * Unreserve without putting on LRU to avoid swapping out an
1870 * already swapped buffer.
1871 */
1872
1873 atomic_set(&bo->reserved, 0);
1874 wake_up_all(&bo->event_queue);
1875 kref_put(&bo->list_kref, ttm_bo_release_list);
1876 return ret;
1877}
1878
1879void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1880{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001881 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001882 ;
1883}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001884EXPORT_SYMBOL(ttm_bo_swapout_all);