blob: 5ef0103bd0b60b722f55783c9c80e05cf2230aae [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);
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010082 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
Jerome Glissefb53f862009-12-09 21:55:10 +010083 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
84 man->available_caching);
85 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
86 man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100087 if (mem_type != TTM_PL_SYSTEM)
88 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010089}
90
91static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
92 struct ttm_placement *placement)
93{
Jerome Glissefb53f862009-12-09 21:55:10 +010094 int i, ret, mem_type;
95
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010096 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
Jerome Glissefb53f862009-12-09 21:55:10 +010097 bo, bo->mem.num_pages, bo->mem.size >> 10,
98 bo->mem.size >> 20);
99 for (i = 0; i < placement->num_placement; i++) {
100 ret = ttm_mem_type_from_flags(placement->placement[i],
101 &mem_type);
102 if (ret)
103 return;
Jerome Glissefb53f862009-12-09 21:55:10 +0100104 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
105 i, placement->placement[i], mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +0100106 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100107 }
108}
109
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200110static ssize_t ttm_bo_global_show(struct kobject *kobj,
111 struct attribute *attr,
112 char *buffer)
113{
114 struct ttm_bo_global *glob =
115 container_of(kobj, struct ttm_bo_global, kobj);
116
117 return snprintf(buffer, PAGE_SIZE, "%lu\n",
118 (unsigned long) atomic_read(&glob->bo_count));
119}
120
121static struct attribute *ttm_bo_global_attrs[] = {
122 &ttm_bo_count,
123 NULL
124};
125
Emese Revfy52cf25d2010-01-19 02:58:23 +0100126static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200127 .show = &ttm_bo_global_show
128};
129
130static struct kobj_type ttm_bo_glob_kobj_type = {
131 .release = &ttm_bo_global_kobj_release,
132 .sysfs_ops = &ttm_bo_global_ops,
133 .default_attrs = ttm_bo_global_attrs
134};
135
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200136
137static inline uint32_t ttm_bo_type_flags(unsigned type)
138{
139 return 1 << (type);
140}
141
142static void ttm_bo_release_list(struct kref *list_kref)
143{
144 struct ttm_buffer_object *bo =
145 container_of(list_kref, struct ttm_buffer_object, list_kref);
146 struct ttm_bo_device *bdev = bo->bdev;
147
148 BUG_ON(atomic_read(&bo->list_kref.refcount));
149 BUG_ON(atomic_read(&bo->kref.refcount));
150 BUG_ON(atomic_read(&bo->cpu_writers));
151 BUG_ON(bo->sync_obj != NULL);
152 BUG_ON(bo->mem.mm_node != NULL);
153 BUG_ON(!list_empty(&bo->lru));
154 BUG_ON(!list_empty(&bo->ddestroy));
155
156 if (bo->ttm)
157 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200158 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200159 if (bo->destroy)
160 bo->destroy(bo);
161 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200162 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200163 kfree(bo);
164 }
165}
166
167int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
168{
169
170 if (interruptible) {
171 int ret = 0;
172
173 ret = wait_event_interruptible(bo->event_queue,
174 atomic_read(&bo->reserved) == 0);
175 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100176 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200177 } else {
178 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
179 }
180 return 0;
181}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000182EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200183
184static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
185{
186 struct ttm_bo_device *bdev = bo->bdev;
187 struct ttm_mem_type_manager *man;
188
189 BUG_ON(!atomic_read(&bo->reserved));
190
191 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
192
193 BUG_ON(!list_empty(&bo->lru));
194
195 man = &bdev->man[bo->mem.mem_type];
196 list_add_tail(&bo->lru, &man->lru);
197 kref_get(&bo->list_kref);
198
199 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200200 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200201 kref_get(&bo->list_kref);
202 }
203 }
204}
205
206/**
207 * Call with the lru_lock held.
208 */
209
210static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
211{
212 int put_count = 0;
213
214 if (!list_empty(&bo->swap)) {
215 list_del_init(&bo->swap);
216 ++put_count;
217 }
218 if (!list_empty(&bo->lru)) {
219 list_del_init(&bo->lru);
220 ++put_count;
221 }
222
223 /*
224 * TODO: Add a driver hook to delete from
225 * driver-specific LRU's here.
226 */
227
228 return put_count;
229}
230
231int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
232 bool interruptible,
233 bool no_wait, bool use_sequence, uint32_t sequence)
234{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200235 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200236 int ret;
237
238 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
239 if (use_sequence && bo->seq_valid &&
240 (sequence - bo->val_seq < (1 << 31))) {
241 return -EAGAIN;
242 }
243
244 if (no_wait)
245 return -EBUSY;
246
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200247 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200248 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200249 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200250
251 if (unlikely(ret))
252 return ret;
253 }
254
255 if (use_sequence) {
256 bo->val_seq = sequence;
257 bo->seq_valid = true;
258 } else {
259 bo->seq_valid = false;
260 }
261
262 return 0;
263}
264EXPORT_SYMBOL(ttm_bo_reserve);
265
266static void ttm_bo_ref_bug(struct kref *list_kref)
267{
268 BUG();
269}
270
271int ttm_bo_reserve(struct ttm_buffer_object *bo,
272 bool interruptible,
273 bool no_wait, bool use_sequence, uint32_t sequence)
274{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200275 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200276 int put_count = 0;
277 int ret;
278
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200279 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200280 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
281 sequence);
282 if (likely(ret == 0))
283 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200284 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200285
286 while (put_count--)
287 kref_put(&bo->list_kref, ttm_bo_ref_bug);
288
289 return ret;
290}
291
292void ttm_bo_unreserve(struct ttm_buffer_object *bo)
293{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200294 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200295
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200296 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200297 ttm_bo_add_to_lru(bo);
298 atomic_set(&bo->reserved, 0);
299 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200300 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200301}
302EXPORT_SYMBOL(ttm_bo_unreserve);
303
304/*
305 * Call bo->mutex locked.
306 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200307static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
308{
309 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200310 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200311 int ret = 0;
312 uint32_t page_flags = 0;
313
314 TTM_ASSERT_LOCKED(&bo->mutex);
315 bo->ttm = NULL;
316
Dave Airliead49f502009-07-10 22:36:26 +1000317 if (bdev->need_dma32)
318 page_flags |= TTM_PAGE_FLAG_DMA32;
319
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200320 switch (bo->type) {
321 case ttm_bo_type_device:
322 if (zero_alloc)
323 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
324 case ttm_bo_type_kernel:
325 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200326 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200327 if (unlikely(bo->ttm == NULL))
328 ret = -ENOMEM;
329 break;
330 case ttm_bo_type_user:
331 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
332 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200333 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000334 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200335 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000336 break;
337 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200338
339 ret = ttm_tt_set_user(bo->ttm, current,
340 bo->buffer_start, bo->num_pages);
341 if (unlikely(ret != 0))
342 ttm_tt_destroy(bo->ttm);
343 break;
344 default:
345 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
346 ret = -EINVAL;
347 break;
348 }
349
350 return ret;
351}
352
353static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
354 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000355 bool evict, bool interruptible,
356 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200357{
358 struct ttm_bo_device *bdev = bo->bdev;
359 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
360 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
361 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
362 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
363 int ret = 0;
364
365 if (old_is_pci || new_is_pci ||
366 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
367 ttm_bo_unmap_virtual(bo);
368
369 /*
370 * Create and bind a ttm if required.
371 */
372
373 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
374 ret = ttm_bo_add_ttm(bo, false);
375 if (ret)
376 goto out_err;
377
378 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
379 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200380 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200381
382 if (mem->mem_type != TTM_PL_SYSTEM) {
383 ret = ttm_tt_bind(bo->ttm, mem);
384 if (ret)
385 goto out_err;
386 }
387
388 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100389 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200391 goto moved;
392 }
393
394 }
395
Dave Airliee024e112009-06-24 09:48:08 +1000396 if (bdev->driver->move_notify)
397 bdev->driver->move_notify(bo, mem);
398
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200399 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
400 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000401 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200402 else if (bdev->driver->move)
403 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000404 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200405 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000406 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200407
408 if (ret)
409 goto out_err;
410
411moved:
412 if (bo->evicted) {
413 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
414 if (ret)
415 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
416 bo->evicted = false;
417 }
418
419 if (bo->mem.mm_node) {
420 spin_lock(&bo->lock);
Ben Skeggsd961db72010-08-05 10:48:18 +1000421 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200422 bdev->man[bo->mem.mem_type].gpu_offset;
423 bo->cur_placement = bo->mem.placement;
424 spin_unlock(&bo->lock);
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100425 } else
426 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200427
428 return 0;
429
430out_err:
431 new_man = &bdev->man[bo->mem.mem_type];
432 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
433 ttm_tt_unbind(bo->ttm);
434 ttm_tt_destroy(bo->ttm);
435 bo->ttm = NULL;
436 }
437
438 return ret;
439}
440
441/**
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200442 * Call bo::reserved and with the lru lock held.
443 * Will release GPU memory type usage on destruction.
444 * This is the place to put in driver specific hooks.
445 * Will release the bo::reserved lock and the
446 * lru lock on exit.
447 */
448
449static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
450{
451 struct ttm_bo_global *glob = bo->glob;
452
453 if (bo->ttm) {
454
455 /**
456 * Release the lru_lock, since we don't want to have
457 * an atomic requirement on ttm_tt[unbind|destroy].
458 */
459
460 spin_unlock(&glob->lru_lock);
461 ttm_tt_unbind(bo->ttm);
462 ttm_tt_destroy(bo->ttm);
463 bo->ttm = NULL;
464 spin_lock(&glob->lru_lock);
465 }
466
Dave Airliec9220b02010-10-08 08:57:10 +1000467 ttm_bo_mem_put_locked(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200468
469 atomic_set(&bo->reserved, 0);
470 wake_up_all(&bo->event_queue);
471 spin_unlock(&glob->lru_lock);
472}
473
474
475/**
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200476 * If bo idle, remove from delayed- and lru lists, and unref.
477 * If not idle, and already on delayed list, do nothing.
478 * If not idle, and not on delayed list, put on delayed list,
479 * up the list_kref and schedule a delayed list check.
480 */
481
482static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
483{
484 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200485 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200486 struct ttm_bo_driver *driver = bdev->driver;
487 int ret;
488
489 spin_lock(&bo->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200490retry:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200491 (void) ttm_bo_wait(bo, false, false, !remove_all);
492
493 if (!bo->sync_obj) {
494 int put_count;
495
496 spin_unlock(&bo->lock);
497
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200498 spin_lock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200499 ret = ttm_bo_reserve_locked(bo, false, !remove_all, false, 0);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100500
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200501 /**
502 * Someone else has the object reserved. Bail and retry.
503 */
504
505 if (unlikely(ret == -EBUSY)) {
506 spin_unlock(&glob->lru_lock);
507 spin_lock(&bo->lock);
508 goto requeue;
509 }
510
511 /**
512 * We can re-check for sync object without taking
513 * the bo::lock since setting the sync object requires
514 * also bo::reserved. A busy object at this point may
515 * be caused by another thread starting an accelerated
516 * eviction.
517 */
518
519 if (unlikely(bo->sync_obj)) {
520 atomic_set(&bo->reserved, 0);
521 wake_up_all(&bo->event_queue);
522 spin_unlock(&glob->lru_lock);
523 spin_lock(&bo->lock);
524 if (remove_all)
525 goto retry;
526 else
527 goto requeue;
528 }
529
530 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200531
532 if (!list_empty(&bo->ddestroy)) {
533 list_del_init(&bo->ddestroy);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100534 ++put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200535 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200536
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200537 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200538
539 while (put_count--)
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100540 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200541
542 return 0;
543 }
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200544requeue:
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200545 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200546 if (list_empty(&bo->ddestroy)) {
547 void *sync_obj = bo->sync_obj;
548 void *sync_obj_arg = bo->sync_obj_arg;
549
550 kref_get(&bo->list_kref);
551 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200552 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200553 spin_unlock(&bo->lock);
554
555 if (sync_obj)
556 driver->sync_obj_flush(sync_obj, sync_obj_arg);
557 schedule_delayed_work(&bdev->wq,
558 ((HZ / 100) < 1) ? 1 : HZ / 100);
559 ret = 0;
560
561 } else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200562 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200563 spin_unlock(&bo->lock);
564 ret = -EBUSY;
565 }
566
567 return ret;
568}
569
570/**
571 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
572 * encountered buffers.
573 */
574
575static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
576{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200577 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100578 struct ttm_buffer_object *entry = NULL;
579 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200580
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200581 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100582 if (list_empty(&bdev->ddestroy))
583 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200584
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100585 entry = list_first_entry(&bdev->ddestroy,
586 struct ttm_buffer_object, ddestroy);
587 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200588
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100589 for (;;) {
590 struct ttm_buffer_object *nentry = NULL;
591
592 if (entry->ddestroy.next != &bdev->ddestroy) {
593 nentry = list_first_entry(&entry->ddestroy,
594 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200595 kref_get(&nentry->list_kref);
596 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200597
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200598 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200599 ret = ttm_bo_cleanup_refs(entry, remove_all);
600 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100601 entry = nentry;
602
603 if (ret || !entry)
604 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200605
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200606 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100607 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200608 break;
609 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200610
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100611out_unlock:
612 spin_unlock(&glob->lru_lock);
613out:
614 if (entry)
615 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616 return ret;
617}
618
619static void ttm_bo_delayed_workqueue(struct work_struct *work)
620{
621 struct ttm_bo_device *bdev =
622 container_of(work, struct ttm_bo_device, wq.work);
623
624 if (ttm_bo_delayed_delete(bdev, false)) {
625 schedule_delayed_work(&bdev->wq,
626 ((HZ / 100) < 1) ? 1 : HZ / 100);
627 }
628}
629
630static void ttm_bo_release(struct kref *kref)
631{
632 struct ttm_buffer_object *bo =
633 container_of(kref, struct ttm_buffer_object, kref);
634 struct ttm_bo_device *bdev = bo->bdev;
635
636 if (likely(bo->vm_node != NULL)) {
637 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
638 drm_mm_put_block(bo->vm_node);
639 bo->vm_node = NULL;
640 }
641 write_unlock(&bdev->vm_lock);
642 ttm_bo_cleanup_refs(bo, false);
643 kref_put(&bo->list_kref, ttm_bo_release_list);
644 write_lock(&bdev->vm_lock);
645}
646
647void ttm_bo_unref(struct ttm_buffer_object **p_bo)
648{
649 struct ttm_buffer_object *bo = *p_bo;
650 struct ttm_bo_device *bdev = bo->bdev;
651
652 *p_bo = NULL;
653 write_lock(&bdev->vm_lock);
654 kref_put(&bo->kref, ttm_bo_release);
655 write_unlock(&bdev->vm_lock);
656}
657EXPORT_SYMBOL(ttm_bo_unref);
658
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400659int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
660{
661 return cancel_delayed_work_sync(&bdev->wq);
662}
663EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
664
665void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
666{
667 if (resched)
668 schedule_delayed_work(&bdev->wq,
669 ((HZ / 100) < 1) ? 1 : HZ / 100);
670}
671EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
672
Jerome Glisseca262a9992009-12-08 15:33:32 +0100673static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000674 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200675{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200676 struct ttm_bo_device *bdev = bo->bdev;
677 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100678 struct ttm_placement placement;
679 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200680
681 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000682 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200683 spin_unlock(&bo->lock);
684
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200685 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100686 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200687 printk(KERN_ERR TTM_PFX
688 "Failed to expire sync object before "
689 "buffer eviction.\n");
690 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200691 goto out;
692 }
693
694 BUG_ON(!atomic_read(&bo->reserved));
695
696 evict_mem = bo->mem;
697 evict_mem.mm_node = NULL;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200698 evict_mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200699
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100700 placement.fpfn = 0;
701 placement.lpfn = 0;
702 placement.num_placement = 0;
703 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100704 bdev->driver->evict_flags(bo, &placement);
705 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000706 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200707 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100708 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200709 printk(KERN_ERR TTM_PFX
710 "Failed to find memory space for "
711 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100712 ttm_bo_mem_space_debug(bo, &placement);
713 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200714 goto out;
715 }
716
717 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000718 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200719 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100720 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200721 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000722 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200723 goto out;
724 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200725 bo->evicted = true;
726out:
727 return ret;
728}
729
Jerome Glisseca262a9992009-12-08 15:33:32 +0100730static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
731 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000732 bool interruptible, bool no_wait_reserve,
733 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100734{
735 struct ttm_bo_global *glob = bdev->glob;
736 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
737 struct ttm_buffer_object *bo;
738 int ret, put_count = 0;
739
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100740retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100741 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100742 if (list_empty(&man->lru)) {
743 spin_unlock(&glob->lru_lock);
744 return -EBUSY;
745 }
746
Jerome Glisseca262a9992009-12-08 15:33:32 +0100747 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
748 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100749
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000750 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100751
752 if (unlikely(ret == -EBUSY)) {
753 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000754 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100755 ret = ttm_bo_wait_unreserved(bo, interruptible);
756
757 kref_put(&bo->list_kref, ttm_bo_release_list);
758
759 /**
760 * We *need* to retry after releasing the lru lock.
761 */
762
763 if (unlikely(ret != 0))
764 return ret;
765 goto retry;
766 }
767
768 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100769 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100770
771 BUG_ON(ret != 0);
772
Jerome Glisseca262a9992009-12-08 15:33:32 +0100773 while (put_count--)
774 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100775
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000776 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100777 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100778
Jerome Glisseca262a9992009-12-08 15:33:32 +0100779 kref_put(&bo->list_kref, ttm_bo_release_list);
780 return ret;
781}
782
Ben Skeggs42311ff2010-08-04 12:07:08 +1000783void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
784{
Ben Skeggsd961db72010-08-05 10:48:18 +1000785 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000786
Ben Skeggsd961db72010-08-05 10:48:18 +1000787 if (mem->mm_node)
788 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000789}
790EXPORT_SYMBOL(ttm_bo_mem_put);
791
Dave Airliec9220b02010-10-08 08:57:10 +1000792void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
793{
794 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
795
796 if (mem->mm_node)
797 (*man->func->put_node_locked)(man, mem);
798}
799EXPORT_SYMBOL(ttm_bo_mem_put_locked);
800
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200801/**
802 * Repeatedly evict memory from the LRU for @mem_type until we create enough
803 * space, or we've evicted everything and there isn't enough space.
804 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100805static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
806 uint32_t mem_type,
807 struct ttm_placement *placement,
808 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000809 bool interruptible,
810 bool no_wait_reserve,
811 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200812{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100813 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200814 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200815 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200816 int ret;
817
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200818 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000819 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200820 if (unlikely(ret != 0))
821 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000822 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100823 break;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200824 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100825 if (list_empty(&man->lru)) {
826 spin_unlock(&glob->lru_lock);
827 break;
828 }
829 spin_unlock(&glob->lru_lock);
830 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000831 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100832 if (unlikely(ret != 0))
833 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200834 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000835 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200836 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200837 mem->mem_type = mem_type;
838 return 0;
839}
840
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200841static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
842 uint32_t cur_placement,
843 uint32_t proposed_placement)
844{
845 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
846 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
847
848 /**
849 * Keep current caching if possible.
850 */
851
852 if ((cur_placement & caching) != 0)
853 result |= (cur_placement & caching);
854 else if ((man->default_caching & caching) != 0)
855 result |= man->default_caching;
856 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
857 result |= TTM_PL_FLAG_CACHED;
858 else if ((TTM_PL_FLAG_WC & caching) != 0)
859 result |= TTM_PL_FLAG_WC;
860 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
861 result |= TTM_PL_FLAG_UNCACHED;
862
863 return result;
864}
865
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200866static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
867 bool disallow_fixed,
868 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200869 uint32_t proposed_placement,
870 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200871{
872 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
873
874 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
875 return false;
876
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200877 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200878 return false;
879
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200880 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200881 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200883 cur_flags |= (proposed_placement & man->available_caching);
884
885 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200886 return true;
887}
888
889/**
890 * Creates space for memory region @mem according to its type.
891 *
892 * This function first searches for free space in compatible memory types in
893 * the priority order defined by the driver. If free space isn't found, then
894 * ttm_bo_mem_force_space is attempted in priority order to evict and find
895 * space.
896 */
897int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100898 struct ttm_placement *placement,
899 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000900 bool interruptible, bool no_wait_reserve,
901 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200902{
903 struct ttm_bo_device *bdev = bo->bdev;
904 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200905 uint32_t mem_type = TTM_PL_SYSTEM;
906 uint32_t cur_flags = 0;
907 bool type_found = false;
908 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100909 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100910 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200911
912 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000913 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100914 ret = ttm_mem_type_from_flags(placement->placement[i],
915 &mem_type);
916 if (ret)
917 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200918 man = &bdev->man[mem_type];
919
920 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100921 bo->type == ttm_bo_type_user,
922 mem_type,
923 placement->placement[i],
924 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200925
926 if (!type_ok)
927 continue;
928
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200929 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
930 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100931 /*
932 * Use the access and other non-mapping-related flag bits from
933 * the memory placement flags to the current flags
934 */
935 ttm_flag_masked(&cur_flags, placement->placement[i],
936 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200937
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938 if (mem_type == TTM_PL_SYSTEM)
939 break;
940
941 if (man->has_type && man->use_type) {
942 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000943 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100944 if (unlikely(ret))
945 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200946 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000947 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200948 break;
949 }
950
Ben Skeggsd961db72010-08-05 10:48:18 +1000951 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200952 mem->mem_type = mem_type;
953 mem->placement = cur_flags;
954 return 0;
955 }
956
957 if (!type_found)
958 return -EINVAL;
959
Dave Airlieb6637522009-12-14 14:51:35 +1000960 for (i = 0; i < placement->num_busy_placement; ++i) {
961 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100962 &mem_type);
963 if (ret)
964 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200965 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200966 if (!man->has_type)
967 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200968 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100969 bo->type == ttm_bo_type_user,
970 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000971 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100972 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200973 continue;
974
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200975 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
976 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100977 /*
978 * Use the access and other non-mapping-related flag bits from
979 * the memory placement flags to the current flags
980 */
Dave Airlieb6637522009-12-14 14:51:35 +1000981 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100982 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200983
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100984
985 if (mem_type == TTM_PL_SYSTEM) {
986 mem->mem_type = mem_type;
987 mem->placement = cur_flags;
988 mem->mm_node = NULL;
989 return 0;
990 }
991
Jerome Glisseca262a9992009-12-08 15:33:32 +0100992 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000993 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200994 if (ret == 0 && mem->mm_node) {
995 mem->placement = cur_flags;
996 return 0;
997 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100998 if (ret == -ERESTARTSYS)
999 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001000 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001001 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001002 return ret;
1003}
1004EXPORT_SYMBOL(ttm_bo_mem_space);
1005
1006int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1007{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001008 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1009 return -EBUSY;
1010
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001011 return wait_event_interruptible(bo->event_queue,
1012 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001014EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015
1016int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001017 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001018 bool interruptible, bool no_wait_reserve,
1019 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001020{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001021 int ret = 0;
1022 struct ttm_mem_reg mem;
1023
1024 BUG_ON(!atomic_read(&bo->reserved));
1025
1026 /*
1027 * FIXME: It's possible to pipeline buffer moves.
1028 * Have the driver move function wait for idle when necessary,
1029 * instead of doing it here.
1030 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001031 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001032 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001033 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001034 if (ret)
1035 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036 mem.num_pages = bo->num_pages;
1037 mem.size = mem.num_pages << PAGE_SHIFT;
1038 mem.page_alignment = bo->mem.page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001039 mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001040 /*
1041 * Determine where to move the buffer.
1042 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001043 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044 if (ret)
1045 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001046 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001047out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001048 if (ret && mem.mm_node)
1049 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050 return ret;
1051}
1052
Jerome Glisseca262a9992009-12-08 15:33:32 +01001053static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001054 struct ttm_mem_reg *mem)
1055{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001056 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001057
Ben Skeggsd961db72010-08-05 10:48:18 +10001058 if (mem->mm_node && placement->lpfn != 0 &&
1059 (mem->start < placement->fpfn ||
1060 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001061 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001062
Jerome Glisseca262a9992009-12-08 15:33:32 +01001063 for (i = 0; i < placement->num_placement; i++) {
1064 if ((placement->placement[i] & mem->placement &
1065 TTM_PL_MASK_CACHING) &&
1066 (placement->placement[i] & mem->placement &
1067 TTM_PL_MASK_MEM))
1068 return i;
1069 }
1070 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001071}
1072
Jerome Glisse09855ac2009-12-10 17:16:27 +01001073int ttm_bo_validate(struct ttm_buffer_object *bo,
1074 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001075 bool interruptible, bool no_wait_reserve,
1076 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077{
1078 int ret;
1079
1080 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001081 /* Check that range is valid */
1082 if (placement->lpfn || placement->fpfn)
1083 if (placement->fpfn > placement->lpfn ||
1084 (placement->lpfn - placement->fpfn) < bo->num_pages)
1085 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086 /*
1087 * Check whether we need to move buffer.
1088 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001089 ret = ttm_bo_mem_compat(placement, &bo->mem);
1090 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001091 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001092 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001094 } else {
1095 /*
1096 * Use the access and other non-mapping-related flag bits from
1097 * the compatible memory placement flags to the active flags
1098 */
1099 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1100 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102 /*
1103 * We might need to add a TTM.
1104 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001105 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1106 ret = ttm_bo_add_ttm(bo, true);
1107 if (ret)
1108 return ret;
1109 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110 return 0;
1111}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001112EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113
Jerome Glisse09855ac2009-12-10 17:16:27 +01001114int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1115 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001116{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001117 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001118
Jerome Glisse09855ac2009-12-10 17:16:27 +01001119 if (placement->fpfn || placement->lpfn) {
1120 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1121 printk(KERN_ERR TTM_PFX "Page number range to small "
1122 "Need %lu pages, range is [%u, %u]\n",
1123 bo->mem.num_pages, placement->fpfn,
1124 placement->lpfn);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125 return -EINVAL;
1126 }
Jerome Glisse09855ac2009-12-10 17:16:27 +01001127 }
1128 for (i = 0; i < placement->num_placement; i++) {
1129 if (!capable(CAP_SYS_ADMIN)) {
1130 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1131 printk(KERN_ERR TTM_PFX "Need to be root to "
1132 "modify NO_EVICT status.\n");
1133 return -EINVAL;
1134 }
1135 }
1136 }
1137 for (i = 0; i < placement->num_busy_placement; i++) {
1138 if (!capable(CAP_SYS_ADMIN)) {
1139 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1140 printk(KERN_ERR TTM_PFX "Need to be root to "
1141 "modify NO_EVICT status.\n");
1142 return -EINVAL;
1143 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 }
1145 }
1146 return 0;
1147}
1148
Jerome Glisse09855ac2009-12-10 17:16:27 +01001149int ttm_bo_init(struct ttm_bo_device *bdev,
1150 struct ttm_buffer_object *bo,
1151 unsigned long size,
1152 enum ttm_bo_type type,
1153 struct ttm_placement *placement,
1154 uint32_t page_alignment,
1155 unsigned long buffer_start,
1156 bool interruptible,
1157 struct file *persistant_swap_storage,
1158 size_t acc_size,
1159 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001160{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001161 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001162 unsigned long num_pages;
1163
1164 size += buffer_start & ~PAGE_MASK;
1165 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1166 if (num_pages == 0) {
1167 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1168 return -EINVAL;
1169 }
1170 bo->destroy = destroy;
1171
1172 spin_lock_init(&bo->lock);
1173 kref_init(&bo->kref);
1174 kref_init(&bo->list_kref);
1175 atomic_set(&bo->cpu_writers, 0);
1176 atomic_set(&bo->reserved, 1);
1177 init_waitqueue_head(&bo->event_queue);
1178 INIT_LIST_HEAD(&bo->lru);
1179 INIT_LIST_HEAD(&bo->ddestroy);
1180 INIT_LIST_HEAD(&bo->swap);
1181 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001182 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001183 bo->type = type;
1184 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001185 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001186 bo->mem.mem_type = TTM_PL_SYSTEM;
1187 bo->mem.num_pages = bo->num_pages;
1188 bo->mem.mm_node = NULL;
1189 bo->mem.page_alignment = page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001190 bo->mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001191 bo->buffer_start = buffer_start & PAGE_MASK;
1192 bo->priv_flags = 0;
1193 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1194 bo->seq_valid = false;
1195 bo->persistant_swap_storage = persistant_swap_storage;
1196 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001197 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001198
Jerome Glisse09855ac2009-12-10 17:16:27 +01001199 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200 if (unlikely(ret != 0))
1201 goto out_err;
1202
1203 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001204 * For ttm_bo_type_device buffers, allocate
1205 * address space from the device.
1206 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001207 if (bo->type == ttm_bo_type_device) {
1208 ret = ttm_bo_setup_vm(bo);
1209 if (ret)
1210 goto out_err;
1211 }
1212
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001213 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001214 if (ret)
1215 goto out_err;
1216
1217 ttm_bo_unreserve(bo);
1218 return 0;
1219
1220out_err:
1221 ttm_bo_unreserve(bo);
1222 ttm_bo_unref(&bo);
1223
1224 return ret;
1225}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001226EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001227
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001228static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001229 unsigned long num_pages)
1230{
1231 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1232 PAGE_MASK;
1233
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001234 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001235}
1236
Jerome Glisse09855ac2009-12-10 17:16:27 +01001237int ttm_bo_create(struct ttm_bo_device *bdev,
1238 unsigned long size,
1239 enum ttm_bo_type type,
1240 struct ttm_placement *placement,
1241 uint32_t page_alignment,
1242 unsigned long buffer_start,
1243 bool interruptible,
1244 struct file *persistant_swap_storage,
1245 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001246{
1247 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001248 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001249 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001250
1251 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001252 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001253 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001254 if (unlikely(ret != 0))
1255 return ret;
1256
1257 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1258
1259 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001260 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001261 return -ENOMEM;
1262 }
1263
Jerome Glisse09855ac2009-12-10 17:16:27 +01001264 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1265 buffer_start, interruptible,
1266 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001267 if (likely(ret == 0))
1268 *p_bo = bo;
1269
1270 return ret;
1271}
1272
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001273static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001274 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001275{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001276 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001277 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001278 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001279
1280 /*
1281 * Can't use standard list traversal since we're unlocking.
1282 */
1283
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001284 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001285 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001286 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001287 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001288 if (ret) {
1289 if (allow_errors) {
1290 return ret;
1291 } else {
1292 printk(KERN_ERR TTM_PFX
1293 "Cleanup eviction failed\n");
1294 }
1295 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001296 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001297 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001298 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299 return 0;
1300}
1301
1302int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1303{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001304 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001305 int ret = -EINVAL;
1306
1307 if (mem_type >= TTM_NUM_MEM_TYPES) {
1308 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1309 return ret;
1310 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001311 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001312
1313 if (!man->has_type) {
1314 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1315 "memory manager type %u\n", mem_type);
1316 return ret;
1317 }
1318
1319 man->use_type = false;
1320 man->has_type = false;
1321
1322 ret = 0;
1323 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001324 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001325
Ben Skeggsd961db72010-08-05 10:48:18 +10001326 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001327 }
1328
1329 return ret;
1330}
1331EXPORT_SYMBOL(ttm_bo_clean_mm);
1332
1333int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1334{
1335 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1336
1337 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1338 printk(KERN_ERR TTM_PFX
1339 "Illegal memory manager memory type %u.\n",
1340 mem_type);
1341 return -EINVAL;
1342 }
1343
1344 if (!man->has_type) {
1345 printk(KERN_ERR TTM_PFX
1346 "Memory type %u has not been initialized.\n",
1347 mem_type);
1348 return 0;
1349 }
1350
Jerome Glisseca262a9992009-12-08 15:33:32 +01001351 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001352}
1353EXPORT_SYMBOL(ttm_bo_evict_mm);
1354
1355int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001356 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001357{
1358 int ret = -EINVAL;
1359 struct ttm_mem_type_manager *man;
1360
1361 if (type >= TTM_NUM_MEM_TYPES) {
1362 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1363 return ret;
1364 }
1365
1366 man = &bdev->man[type];
1367 if (man->has_type) {
1368 printk(KERN_ERR TTM_PFX
1369 "Memory manager already initialized for type %d\n",
1370 type);
1371 return ret;
1372 }
1373
1374 ret = bdev->driver->init_mem_type(bdev, type, man);
1375 if (ret)
1376 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001377 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001378
1379 ret = 0;
1380 if (type != TTM_PL_SYSTEM) {
1381 if (!p_size) {
1382 printk(KERN_ERR TTM_PFX
1383 "Zero size memory manager type %d\n",
1384 type);
1385 return ret;
1386 }
Ben Skeggsd961db72010-08-05 10:48:18 +10001387
1388 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001389 if (ret)
1390 return ret;
1391 }
1392 man->has_type = true;
1393 man->use_type = true;
1394 man->size = p_size;
1395
1396 INIT_LIST_HEAD(&man->lru);
1397
1398 return 0;
1399}
1400EXPORT_SYMBOL(ttm_bo_init_mm);
1401
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001402static void ttm_bo_global_kobj_release(struct kobject *kobj)
1403{
1404 struct ttm_bo_global *glob =
1405 container_of(kobj, struct ttm_bo_global, kobj);
1406
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001407 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1408 __free_page(glob->dummy_read_page);
1409 kfree(glob);
1410}
1411
Dave Airlieba4420c2010-03-09 10:56:52 +10001412void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001413{
1414 struct ttm_bo_global *glob = ref->object;
1415
1416 kobject_del(&glob->kobj);
1417 kobject_put(&glob->kobj);
1418}
1419EXPORT_SYMBOL(ttm_bo_global_release);
1420
Dave Airlieba4420c2010-03-09 10:56:52 +10001421int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001422{
1423 struct ttm_bo_global_ref *bo_ref =
1424 container_of(ref, struct ttm_bo_global_ref, ref);
1425 struct ttm_bo_global *glob = ref->object;
1426 int ret;
1427
1428 mutex_init(&glob->device_list_mutex);
1429 spin_lock_init(&glob->lru_lock);
1430 glob->mem_glob = bo_ref->mem_glob;
1431 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1432
1433 if (unlikely(glob->dummy_read_page == NULL)) {
1434 ret = -ENOMEM;
1435 goto out_no_drp;
1436 }
1437
1438 INIT_LIST_HEAD(&glob->swap_lru);
1439 INIT_LIST_HEAD(&glob->device_list);
1440
1441 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1442 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1443 if (unlikely(ret != 0)) {
1444 printk(KERN_ERR TTM_PFX
1445 "Could not register buffer object swapout.\n");
1446 goto out_no_shrink;
1447 }
1448
1449 glob->ttm_bo_extra_size =
1450 ttm_round_pot(sizeof(struct ttm_tt)) +
1451 ttm_round_pot(sizeof(struct ttm_backend));
1452
1453 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1454 ttm_round_pot(sizeof(struct ttm_buffer_object));
1455
1456 atomic_set(&glob->bo_count, 0);
1457
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001458 ret = kobject_init_and_add(
1459 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001460 if (unlikely(ret != 0))
1461 kobject_put(&glob->kobj);
1462 return ret;
1463out_no_shrink:
1464 __free_page(glob->dummy_read_page);
1465out_no_drp:
1466 kfree(glob);
1467 return ret;
1468}
1469EXPORT_SYMBOL(ttm_bo_global_init);
1470
1471
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001472int ttm_bo_device_release(struct ttm_bo_device *bdev)
1473{
1474 int ret = 0;
1475 unsigned i = TTM_NUM_MEM_TYPES;
1476 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001477 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001478
1479 while (i--) {
1480 man = &bdev->man[i];
1481 if (man->has_type) {
1482 man->use_type = false;
1483 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1484 ret = -EBUSY;
1485 printk(KERN_ERR TTM_PFX
1486 "DRM memory manager type %d "
1487 "is not clean.\n", i);
1488 }
1489 man->has_type = false;
1490 }
1491 }
1492
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001493 mutex_lock(&glob->device_list_mutex);
1494 list_del(&bdev->device_list);
1495 mutex_unlock(&glob->device_list_mutex);
1496
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497 if (!cancel_delayed_work(&bdev->wq))
1498 flush_scheduled_work();
1499
1500 while (ttm_bo_delayed_delete(bdev, true))
1501 ;
1502
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001503 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001504 if (list_empty(&bdev->ddestroy))
1505 TTM_DEBUG("Delayed destroy list was clean\n");
1506
1507 if (list_empty(&bdev->man[0].lru))
1508 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001509 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001511 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1512 write_lock(&bdev->vm_lock);
1513 drm_mm_takedown(&bdev->addr_space_mm);
1514 write_unlock(&bdev->vm_lock);
1515
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001516 return ret;
1517}
1518EXPORT_SYMBOL(ttm_bo_device_release);
1519
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001520int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001521 struct ttm_bo_global *glob,
1522 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001523 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001524 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525{
1526 int ret = -EINVAL;
1527
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001528 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001530
1531 memset(bdev->man, 0, sizeof(bdev->man));
1532
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001533 /*
1534 * Initialize the system memory buffer type.
1535 * Other types need to be driver / IOCTL initialized.
1536 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001537 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001539 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001540
1541 bdev->addr_space_rb = RB_ROOT;
1542 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1543 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001544 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001545
1546 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1547 bdev->nice_mode = true;
1548 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001549 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001550 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001551 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001552
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001553 mutex_lock(&glob->device_list_mutex);
1554 list_add_tail(&bdev->device_list, &glob->device_list);
1555 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001556
1557 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001558out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001559 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001560out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001561 return ret;
1562}
1563EXPORT_SYMBOL(ttm_bo_device_init);
1564
1565/*
1566 * buffer object vm functions.
1567 */
1568
1569bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1570{
1571 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1572
1573 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1574 if (mem->mem_type == TTM_PL_SYSTEM)
1575 return false;
1576
1577 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1578 return false;
1579
1580 if (mem->placement & TTM_PL_FLAG_CACHED)
1581 return false;
1582 }
1583 return true;
1584}
1585
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001586void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1587{
1588 struct ttm_bo_device *bdev = bo->bdev;
1589 loff_t offset = (loff_t) bo->addr_space_offset;
1590 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1591
1592 if (!bdev->dev_mapping)
1593 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001594 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Jerome Glisse82c5da62010-04-09 14:39:23 +02001595 ttm_mem_io_free(bdev, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001596}
Dave Airliee024e112009-06-24 09:48:08 +10001597EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001598
1599static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1600{
1601 struct ttm_bo_device *bdev = bo->bdev;
1602 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1603 struct rb_node *parent = NULL;
1604 struct ttm_buffer_object *cur_bo;
1605 unsigned long offset = bo->vm_node->start;
1606 unsigned long cur_offset;
1607
1608 while (*cur) {
1609 parent = *cur;
1610 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1611 cur_offset = cur_bo->vm_node->start;
1612 if (offset < cur_offset)
1613 cur = &parent->rb_left;
1614 else if (offset > cur_offset)
1615 cur = &parent->rb_right;
1616 else
1617 BUG();
1618 }
1619
1620 rb_link_node(&bo->vm_rb, parent, cur);
1621 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1622}
1623
1624/**
1625 * ttm_bo_setup_vm:
1626 *
1627 * @bo: the buffer to allocate address space for
1628 *
1629 * Allocate address space in the drm device so that applications
1630 * can mmap the buffer and access the contents. This only
1631 * applies to ttm_bo_type_device objects as others are not
1632 * placed in the drm device address space.
1633 */
1634
1635static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1636{
1637 struct ttm_bo_device *bdev = bo->bdev;
1638 int ret;
1639
1640retry_pre_get:
1641 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1642 if (unlikely(ret != 0))
1643 return ret;
1644
1645 write_lock(&bdev->vm_lock);
1646 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1647 bo->mem.num_pages, 0, 0);
1648
1649 if (unlikely(bo->vm_node == NULL)) {
1650 ret = -ENOMEM;
1651 goto out_unlock;
1652 }
1653
1654 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1655 bo->mem.num_pages, 0);
1656
1657 if (unlikely(bo->vm_node == NULL)) {
1658 write_unlock(&bdev->vm_lock);
1659 goto retry_pre_get;
1660 }
1661
1662 ttm_bo_vm_insert_rb(bo);
1663 write_unlock(&bdev->vm_lock);
1664 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1665
1666 return 0;
1667out_unlock:
1668 write_unlock(&bdev->vm_lock);
1669 return ret;
1670}
1671
1672int ttm_bo_wait(struct ttm_buffer_object *bo,
1673 bool lazy, bool interruptible, bool no_wait)
1674{
1675 struct ttm_bo_driver *driver = bo->bdev->driver;
1676 void *sync_obj;
1677 void *sync_obj_arg;
1678 int ret = 0;
1679
1680 if (likely(bo->sync_obj == NULL))
1681 return 0;
1682
1683 while (bo->sync_obj) {
1684
1685 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1686 void *tmp_obj = bo->sync_obj;
1687 bo->sync_obj = NULL;
1688 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1689 spin_unlock(&bo->lock);
1690 driver->sync_obj_unref(&tmp_obj);
1691 spin_lock(&bo->lock);
1692 continue;
1693 }
1694
1695 if (no_wait)
1696 return -EBUSY;
1697
1698 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1699 sync_obj_arg = bo->sync_obj_arg;
1700 spin_unlock(&bo->lock);
1701 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1702 lazy, interruptible);
1703 if (unlikely(ret != 0)) {
1704 driver->sync_obj_unref(&sync_obj);
1705 spin_lock(&bo->lock);
1706 return ret;
1707 }
1708 spin_lock(&bo->lock);
1709 if (likely(bo->sync_obj == sync_obj &&
1710 bo->sync_obj_arg == sync_obj_arg)) {
1711 void *tmp_obj = bo->sync_obj;
1712 bo->sync_obj = NULL;
1713 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1714 &bo->priv_flags);
1715 spin_unlock(&bo->lock);
1716 driver->sync_obj_unref(&sync_obj);
1717 driver->sync_obj_unref(&tmp_obj);
1718 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001719 } else {
1720 spin_unlock(&bo->lock);
1721 driver->sync_obj_unref(&sync_obj);
1722 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001723 }
1724 }
1725 return 0;
1726}
1727EXPORT_SYMBOL(ttm_bo_wait);
1728
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001729int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1730{
1731 int ret = 0;
1732
1733 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001734 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001735 */
1736
1737 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1738 if (unlikely(ret != 0))
1739 return ret;
1740 spin_lock(&bo->lock);
1741 ret = ttm_bo_wait(bo, false, true, no_wait);
1742 spin_unlock(&bo->lock);
1743 if (likely(ret == 0))
1744 atomic_inc(&bo->cpu_writers);
1745 ttm_bo_unreserve(bo);
1746 return ret;
1747}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001748EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001749
1750void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1751{
1752 if (atomic_dec_and_test(&bo->cpu_writers))
1753 wake_up_all(&bo->event_queue);
1754}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001755EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001756
1757/**
1758 * A buffer object shrink method that tries to swap out the first
1759 * buffer object on the bo_global::swap_lru list.
1760 */
1761
1762static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1763{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001764 struct ttm_bo_global *glob =
1765 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001766 struct ttm_buffer_object *bo;
1767 int ret = -EBUSY;
1768 int put_count;
1769 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1770
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001771 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001772 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001773 if (unlikely(list_empty(&glob->swap_lru))) {
1774 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001775 return -EBUSY;
1776 }
1777
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001778 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001779 struct ttm_buffer_object, swap);
1780 kref_get(&bo->list_kref);
1781
1782 /**
1783 * Reserve buffer. Since we unlock while sleeping, we need
1784 * to re-check that nobody removed us from the swap-list while
1785 * we slept.
1786 */
1787
1788 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1789 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001790 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001791 ttm_bo_wait_unreserved(bo, false);
1792 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001793 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001794 }
1795 }
1796
1797 BUG_ON(ret != 0);
1798 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001799 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800
1801 while (put_count--)
1802 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1803
1804 /**
1805 * Wait for GPU, then move to system cached.
1806 */
1807
1808 spin_lock(&bo->lock);
1809 ret = ttm_bo_wait(bo, false, false, false);
1810 spin_unlock(&bo->lock);
1811
1812 if (unlikely(ret != 0))
1813 goto out;
1814
1815 if ((bo->mem.placement & swap_placement) != swap_placement) {
1816 struct ttm_mem_reg evict_mem;
1817
1818 evict_mem = bo->mem;
1819 evict_mem.mm_node = NULL;
1820 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1821 evict_mem.mem_type = TTM_PL_SYSTEM;
1822
1823 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001824 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001825 if (unlikely(ret != 0))
1826 goto out;
1827 }
1828
1829 ttm_bo_unmap_virtual(bo);
1830
1831 /**
1832 * Swap out. Buffer will be swapped in again as soon as
1833 * anyone tries to access a ttm page.
1834 */
1835
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001836 if (bo->bdev->driver->swap_notify)
1837 bo->bdev->driver->swap_notify(bo);
1838
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001839 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1840out:
1841
1842 /**
1843 *
1844 * Unreserve without putting on LRU to avoid swapping out an
1845 * already swapped buffer.
1846 */
1847
1848 atomic_set(&bo->reserved, 0);
1849 wake_up_all(&bo->event_queue);
1850 kref_put(&bo->list_kref, ttm_bo_release_list);
1851 return ret;
1852}
1853
1854void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1855{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001856 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001857 ;
1858}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001859EXPORT_SYMBOL(ttm_bo_swapout_all);