blob: b42e3fae1bd508027a5821104c0f604fcee56383 [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;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200635 evict_mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200636
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100637 placement.fpfn = 0;
638 placement.lpfn = 0;
639 placement.num_placement = 0;
640 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100641 bdev->driver->evict_flags(bo, &placement);
642 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000643 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200644 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100645 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200646 printk(KERN_ERR TTM_PFX
647 "Failed to find memory space for "
648 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100649 ttm_bo_mem_space_debug(bo, &placement);
650 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200651 goto out;
652 }
653
654 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000655 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100657 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200658 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +0100659 spin_lock(&glob->lru_lock);
660 if (evict_mem.mm_node) {
661 evict_mem.mm_node->private = NULL;
662 drm_mm_put_block(evict_mem.mm_node);
663 evict_mem.mm_node = NULL;
664 }
665 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200666 goto out;
667 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200668 bo->evicted = true;
669out:
670 return ret;
671}
672
Jerome Glisseca262a9992009-12-08 15:33:32 +0100673static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
674 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000675 bool interruptible, bool no_wait_reserve,
676 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100677{
678 struct ttm_bo_global *glob = bdev->glob;
679 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
680 struct ttm_buffer_object *bo;
681 int ret, put_count = 0;
682
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100683retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100684 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100685 if (list_empty(&man->lru)) {
686 spin_unlock(&glob->lru_lock);
687 return -EBUSY;
688 }
689
Jerome Glisseca262a9992009-12-08 15:33:32 +0100690 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
691 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100692
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000693 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100694
695 if (unlikely(ret == -EBUSY)) {
696 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000697 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100698 ret = ttm_bo_wait_unreserved(bo, interruptible);
699
700 kref_put(&bo->list_kref, ttm_bo_release_list);
701
702 /**
703 * We *need* to retry after releasing the lru lock.
704 */
705
706 if (unlikely(ret != 0))
707 return ret;
708 goto retry;
709 }
710
711 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100712 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100713
714 BUG_ON(ret != 0);
715
Jerome Glisseca262a9992009-12-08 15:33:32 +0100716 while (put_count--)
717 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100718
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000719 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100720 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100721
Jerome Glisseca262a9992009-12-08 15:33:32 +0100722 kref_put(&bo->list_kref, ttm_bo_release_list);
723 return ret;
724}
725
726static int ttm_bo_man_get_node(struct ttm_buffer_object *bo,
727 struct ttm_mem_type_manager *man,
728 struct ttm_placement *placement,
729 struct ttm_mem_reg *mem,
730 struct drm_mm_node **node)
731{
732 struct ttm_bo_global *glob = bo->glob;
733 unsigned long lpfn;
734 int ret;
735
736 lpfn = placement->lpfn;
737 if (!lpfn)
738 lpfn = man->size;
739 *node = NULL;
740 do {
741 ret = drm_mm_pre_get(&man->manager);
742 if (unlikely(ret))
743 return ret;
744
745 spin_lock(&glob->lru_lock);
746 *node = drm_mm_search_free_in_range(&man->manager,
747 mem->num_pages, mem->page_alignment,
748 placement->fpfn, lpfn, 1);
749 if (unlikely(*node == NULL)) {
750 spin_unlock(&glob->lru_lock);
751 return 0;
752 }
753 *node = drm_mm_get_block_atomic_range(*node, mem->num_pages,
754 mem->page_alignment,
755 placement->fpfn,
756 lpfn);
757 spin_unlock(&glob->lru_lock);
758 } while (*node == NULL);
759 return 0;
760}
761
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200762/**
763 * Repeatedly evict memory from the LRU for @mem_type until we create enough
764 * space, or we've evicted everything and there isn't enough space.
765 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100766static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
767 uint32_t mem_type,
768 struct ttm_placement *placement,
769 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000770 bool interruptible,
771 bool no_wait_reserve,
772 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200773{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100774 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200775 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200776 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Jerome Glisseca262a9992009-12-08 15:33:32 +0100777 struct drm_mm_node *node;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200778 int ret;
779
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200780 do {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100781 ret = ttm_bo_man_get_node(bo, man, placement, mem, &node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200782 if (unlikely(ret != 0))
783 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100784 if (node)
785 break;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200786 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100787 if (list_empty(&man->lru)) {
788 spin_unlock(&glob->lru_lock);
789 break;
790 }
791 spin_unlock(&glob->lru_lock);
792 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000793 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100794 if (unlikely(ret != 0))
795 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200796 } while (1);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100797 if (node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200798 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200799 mem->mm_node = node;
800 mem->mem_type = mem_type;
801 return 0;
802}
803
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200804static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
805 uint32_t cur_placement,
806 uint32_t proposed_placement)
807{
808 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
809 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
810
811 /**
812 * Keep current caching if possible.
813 */
814
815 if ((cur_placement & caching) != 0)
816 result |= (cur_placement & caching);
817 else if ((man->default_caching & caching) != 0)
818 result |= man->default_caching;
819 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
820 result |= TTM_PL_FLAG_CACHED;
821 else if ((TTM_PL_FLAG_WC & caching) != 0)
822 result |= TTM_PL_FLAG_WC;
823 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
824 result |= TTM_PL_FLAG_UNCACHED;
825
826 return result;
827}
828
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200829static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
830 bool disallow_fixed,
831 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200832 uint32_t proposed_placement,
833 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200834{
835 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
836
837 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
838 return false;
839
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200840 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200841 return false;
842
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200843 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200844 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200845
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200846 cur_flags |= (proposed_placement & man->available_caching);
847
848 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200849 return true;
850}
851
852/**
853 * Creates space for memory region @mem according to its type.
854 *
855 * This function first searches for free space in compatible memory types in
856 * the priority order defined by the driver. If free space isn't found, then
857 * ttm_bo_mem_force_space is attempted in priority order to evict and find
858 * space.
859 */
860int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100861 struct ttm_placement *placement,
862 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000863 bool interruptible, bool no_wait_reserve,
864 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865{
866 struct ttm_bo_device *bdev = bo->bdev;
867 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200868 uint32_t mem_type = TTM_PL_SYSTEM;
869 uint32_t cur_flags = 0;
870 bool type_found = false;
871 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100872 bool has_erestartsys = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200873 struct drm_mm_node *node = NULL;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100874 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200875
876 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000877 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100878 ret = ttm_mem_type_from_flags(placement->placement[i],
879 &mem_type);
880 if (ret)
881 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882 man = &bdev->man[mem_type];
883
884 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100885 bo->type == ttm_bo_type_user,
886 mem_type,
887 placement->placement[i],
888 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200889
890 if (!type_ok)
891 continue;
892
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200893 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
894 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100895 /*
896 * Use the access and other non-mapping-related flag bits from
897 * the memory placement flags to the current flags
898 */
899 ttm_flag_masked(&cur_flags, placement->placement[i],
900 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200901
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200902 if (mem_type == TTM_PL_SYSTEM)
903 break;
904
905 if (man->has_type && man->use_type) {
906 type_found = true;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100907 ret = ttm_bo_man_get_node(bo, man, placement, mem,
908 &node);
909 if (unlikely(ret))
910 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200911 }
912 if (node)
913 break;
914 }
915
916 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) {
917 mem->mm_node = node;
918 mem->mem_type = mem_type;
919 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100920 if (node)
921 node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922 return 0;
923 }
924
925 if (!type_found)
926 return -EINVAL;
927
Dave Airlieb6637522009-12-14 14:51:35 +1000928 for (i = 0; i < placement->num_busy_placement; ++i) {
929 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100930 &mem_type);
931 if (ret)
932 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200933 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200934 if (!man->has_type)
935 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200936 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100937 bo->type == ttm_bo_type_user,
938 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000939 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100940 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200941 continue;
942
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200943 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
944 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100945 /*
946 * Use the access and other non-mapping-related flag bits from
947 * the memory placement flags to the current flags
948 */
Dave Airlieb6637522009-12-14 14:51:35 +1000949 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100950 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200951
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100952
953 if (mem_type == TTM_PL_SYSTEM) {
954 mem->mem_type = mem_type;
955 mem->placement = cur_flags;
956 mem->mm_node = NULL;
957 return 0;
958 }
959
Jerome Glisseca262a9992009-12-08 15:33:32 +0100960 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000961 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200962 if (ret == 0 && mem->mm_node) {
963 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100964 mem->mm_node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200965 return 0;
966 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100967 if (ret == -ERESTARTSYS)
968 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200969 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100970 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200971 return ret;
972}
973EXPORT_SYMBOL(ttm_bo_mem_space);
974
975int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
976{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200977 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
978 return -EBUSY;
979
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100980 return wait_event_interruptible(bo->event_queue,
981 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200982}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000983EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200984
985int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100986 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000987 bool interruptible, bool no_wait_reserve,
988 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200990 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200991 int ret = 0;
992 struct ttm_mem_reg mem;
993
994 BUG_ON(!atomic_read(&bo->reserved));
995
996 /*
997 * FIXME: It's possible to pipeline buffer moves.
998 * Have the driver move function wait for idle when necessary,
999 * instead of doing it here.
1000 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001001 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001002 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001003 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001004 if (ret)
1005 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006 mem.num_pages = bo->num_pages;
1007 mem.size = mem.num_pages << PAGE_SHIFT;
1008 mem.page_alignment = bo->mem.page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001009 mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001010 /*
1011 * Determine where to move the buffer.
1012 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001013 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001014 if (ret)
1015 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001016 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001017out_unlock:
1018 if (ret && mem.mm_node) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001019 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001020 mem.mm_node->private = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001021 drm_mm_put_block(mem.mm_node);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001022 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001023 }
1024 return ret;
1025}
1026
Jerome Glisseca262a9992009-12-08 15:33:32 +01001027static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001028 struct ttm_mem_reg *mem)
1029{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001030 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001031 struct drm_mm_node *node = mem->mm_node;
1032
1033 if (node && placement->lpfn != 0 &&
1034 (node->start < placement->fpfn ||
1035 node->start + node->size > placement->lpfn))
1036 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037
Jerome Glisseca262a9992009-12-08 15:33:32 +01001038 for (i = 0; i < placement->num_placement; i++) {
1039 if ((placement->placement[i] & mem->placement &
1040 TTM_PL_MASK_CACHING) &&
1041 (placement->placement[i] & mem->placement &
1042 TTM_PL_MASK_MEM))
1043 return i;
1044 }
1045 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046}
1047
Jerome Glisse09855ac2009-12-10 17:16:27 +01001048int ttm_bo_validate(struct ttm_buffer_object *bo,
1049 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001050 bool interruptible, bool no_wait_reserve,
1051 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052{
1053 int ret;
1054
1055 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001056 /* Check that range is valid */
1057 if (placement->lpfn || placement->fpfn)
1058 if (placement->fpfn > placement->lpfn ||
1059 (placement->lpfn - placement->fpfn) < bo->num_pages)
1060 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001061 /*
1062 * Check whether we need to move buffer.
1063 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001064 ret = ttm_bo_mem_compat(placement, &bo->mem);
1065 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001066 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001067 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001068 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001069 } else {
1070 /*
1071 * Use the access and other non-mapping-related flag bits from
1072 * the compatible memory placement flags to the active flags
1073 */
1074 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1075 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001076 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077 /*
1078 * We might need to add a TTM.
1079 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001080 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1081 ret = ttm_bo_add_ttm(bo, true);
1082 if (ret)
1083 return ret;
1084 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001085 return 0;
1086}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001087EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001088
Jerome Glisse09855ac2009-12-10 17:16:27 +01001089int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1090 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001091{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001092 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093
Jerome Glisse09855ac2009-12-10 17:16:27 +01001094 if (placement->fpfn || placement->lpfn) {
1095 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1096 printk(KERN_ERR TTM_PFX "Page number range to small "
1097 "Need %lu pages, range is [%u, %u]\n",
1098 bo->mem.num_pages, placement->fpfn,
1099 placement->lpfn);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001100 return -EINVAL;
1101 }
Jerome Glisse09855ac2009-12-10 17:16:27 +01001102 }
1103 for (i = 0; i < placement->num_placement; i++) {
1104 if (!capable(CAP_SYS_ADMIN)) {
1105 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1106 printk(KERN_ERR TTM_PFX "Need to be root to "
1107 "modify NO_EVICT status.\n");
1108 return -EINVAL;
1109 }
1110 }
1111 }
1112 for (i = 0; i < placement->num_busy_placement; i++) {
1113 if (!capable(CAP_SYS_ADMIN)) {
1114 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1115 printk(KERN_ERR TTM_PFX "Need to be root to "
1116 "modify NO_EVICT status.\n");
1117 return -EINVAL;
1118 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001119 }
1120 }
1121 return 0;
1122}
1123
Jerome Glisse09855ac2009-12-10 17:16:27 +01001124int ttm_bo_init(struct ttm_bo_device *bdev,
1125 struct ttm_buffer_object *bo,
1126 unsigned long size,
1127 enum ttm_bo_type type,
1128 struct ttm_placement *placement,
1129 uint32_t page_alignment,
1130 unsigned long buffer_start,
1131 bool interruptible,
1132 struct file *persistant_swap_storage,
1133 size_t acc_size,
1134 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001136 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001137 unsigned long num_pages;
1138
1139 size += buffer_start & ~PAGE_MASK;
1140 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1141 if (num_pages == 0) {
1142 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1143 return -EINVAL;
1144 }
1145 bo->destroy = destroy;
1146
1147 spin_lock_init(&bo->lock);
1148 kref_init(&bo->kref);
1149 kref_init(&bo->list_kref);
1150 atomic_set(&bo->cpu_writers, 0);
1151 atomic_set(&bo->reserved, 1);
1152 init_waitqueue_head(&bo->event_queue);
1153 INIT_LIST_HEAD(&bo->lru);
1154 INIT_LIST_HEAD(&bo->ddestroy);
1155 INIT_LIST_HEAD(&bo->swap);
1156 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001157 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158 bo->type = type;
1159 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001160 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161 bo->mem.mem_type = TTM_PL_SYSTEM;
1162 bo->mem.num_pages = bo->num_pages;
1163 bo->mem.mm_node = NULL;
1164 bo->mem.page_alignment = page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001165 bo->mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001166 bo->buffer_start = buffer_start & PAGE_MASK;
1167 bo->priv_flags = 0;
1168 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1169 bo->seq_valid = false;
1170 bo->persistant_swap_storage = persistant_swap_storage;
1171 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001172 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001173
Jerome Glisse09855ac2009-12-10 17:16:27 +01001174 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001175 if (unlikely(ret != 0))
1176 goto out_err;
1177
1178 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 * For ttm_bo_type_device buffers, allocate
1180 * address space from the device.
1181 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001182 if (bo->type == ttm_bo_type_device) {
1183 ret = ttm_bo_setup_vm(bo);
1184 if (ret)
1185 goto out_err;
1186 }
1187
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001188 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001189 if (ret)
1190 goto out_err;
1191
1192 ttm_bo_unreserve(bo);
1193 return 0;
1194
1195out_err:
1196 ttm_bo_unreserve(bo);
1197 ttm_bo_unref(&bo);
1198
1199 return ret;
1200}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001201EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001202
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001203static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001204 unsigned long num_pages)
1205{
1206 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1207 PAGE_MASK;
1208
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001209 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001210}
1211
Jerome Glisse09855ac2009-12-10 17:16:27 +01001212int ttm_bo_create(struct ttm_bo_device *bdev,
1213 unsigned long size,
1214 enum ttm_bo_type type,
1215 struct ttm_placement *placement,
1216 uint32_t page_alignment,
1217 unsigned long buffer_start,
1218 bool interruptible,
1219 struct file *persistant_swap_storage,
1220 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221{
1222 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001223 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001224 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001225
1226 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001227 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001228 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001229 if (unlikely(ret != 0))
1230 return ret;
1231
1232 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1233
1234 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001235 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001236 return -ENOMEM;
1237 }
1238
Jerome Glisse09855ac2009-12-10 17:16:27 +01001239 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1240 buffer_start, interruptible,
1241 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001242 if (likely(ret == 0))
1243 *p_bo = bo;
1244
1245 return ret;
1246}
1247
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001249 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001250{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001251 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001252 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001253 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001254
1255 /*
1256 * Can't use standard list traversal since we're unlocking.
1257 */
1258
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001259 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001260 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001261 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001262 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001263 if (ret) {
1264 if (allow_errors) {
1265 return ret;
1266 } else {
1267 printk(KERN_ERR TTM_PFX
1268 "Cleanup eviction failed\n");
1269 }
1270 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001271 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001273 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001274 return 0;
1275}
1276
1277int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1278{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001279 struct ttm_bo_global *glob = bdev->glob;
Roel Kluinc96e7c72009-08-03 14:22:53 +02001280 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001281 int ret = -EINVAL;
1282
1283 if (mem_type >= TTM_NUM_MEM_TYPES) {
1284 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1285 return ret;
1286 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001287 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001288
1289 if (!man->has_type) {
1290 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1291 "memory manager type %u\n", mem_type);
1292 return ret;
1293 }
1294
1295 man->use_type = false;
1296 man->has_type = false;
1297
1298 ret = 0;
1299 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001300 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001301
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001302 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001303 if (drm_mm_clean(&man->manager))
1304 drm_mm_takedown(&man->manager);
1305 else
1306 ret = -EBUSY;
1307
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001308 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001309 }
1310
1311 return ret;
1312}
1313EXPORT_SYMBOL(ttm_bo_clean_mm);
1314
1315int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1316{
1317 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1318
1319 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1320 printk(KERN_ERR TTM_PFX
1321 "Illegal memory manager memory type %u.\n",
1322 mem_type);
1323 return -EINVAL;
1324 }
1325
1326 if (!man->has_type) {
1327 printk(KERN_ERR TTM_PFX
1328 "Memory type %u has not been initialized.\n",
1329 mem_type);
1330 return 0;
1331 }
1332
Jerome Glisseca262a9992009-12-08 15:33:32 +01001333 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001334}
1335EXPORT_SYMBOL(ttm_bo_evict_mm);
1336
1337int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001338 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001339{
1340 int ret = -EINVAL;
1341 struct ttm_mem_type_manager *man;
1342
1343 if (type >= TTM_NUM_MEM_TYPES) {
1344 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1345 return ret;
1346 }
1347
1348 man = &bdev->man[type];
1349 if (man->has_type) {
1350 printk(KERN_ERR TTM_PFX
1351 "Memory manager already initialized for type %d\n",
1352 type);
1353 return ret;
1354 }
1355
1356 ret = bdev->driver->init_mem_type(bdev, type, man);
1357 if (ret)
1358 return ret;
1359
1360 ret = 0;
1361 if (type != TTM_PL_SYSTEM) {
1362 if (!p_size) {
1363 printk(KERN_ERR TTM_PFX
1364 "Zero size memory manager type %d\n",
1365 type);
1366 return ret;
1367 }
Jerome Glisseca262a9992009-12-08 15:33:32 +01001368 ret = drm_mm_init(&man->manager, 0, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001369 if (ret)
1370 return ret;
1371 }
1372 man->has_type = true;
1373 man->use_type = true;
1374 man->size = p_size;
1375
1376 INIT_LIST_HEAD(&man->lru);
1377
1378 return 0;
1379}
1380EXPORT_SYMBOL(ttm_bo_init_mm);
1381
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001382static void ttm_bo_global_kobj_release(struct kobject *kobj)
1383{
1384 struct ttm_bo_global *glob =
1385 container_of(kobj, struct ttm_bo_global, kobj);
1386
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001387 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1388 __free_page(glob->dummy_read_page);
1389 kfree(glob);
1390}
1391
1392void ttm_bo_global_release(struct ttm_global_reference *ref)
1393{
1394 struct ttm_bo_global *glob = ref->object;
1395
1396 kobject_del(&glob->kobj);
1397 kobject_put(&glob->kobj);
1398}
1399EXPORT_SYMBOL(ttm_bo_global_release);
1400
1401int ttm_bo_global_init(struct ttm_global_reference *ref)
1402{
1403 struct ttm_bo_global_ref *bo_ref =
1404 container_of(ref, struct ttm_bo_global_ref, ref);
1405 struct ttm_bo_global *glob = ref->object;
1406 int ret;
1407
1408 mutex_init(&glob->device_list_mutex);
1409 spin_lock_init(&glob->lru_lock);
1410 glob->mem_glob = bo_ref->mem_glob;
1411 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1412
1413 if (unlikely(glob->dummy_read_page == NULL)) {
1414 ret = -ENOMEM;
1415 goto out_no_drp;
1416 }
1417
1418 INIT_LIST_HEAD(&glob->swap_lru);
1419 INIT_LIST_HEAD(&glob->device_list);
1420
1421 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1422 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1423 if (unlikely(ret != 0)) {
1424 printk(KERN_ERR TTM_PFX
1425 "Could not register buffer object swapout.\n");
1426 goto out_no_shrink;
1427 }
1428
1429 glob->ttm_bo_extra_size =
1430 ttm_round_pot(sizeof(struct ttm_tt)) +
1431 ttm_round_pot(sizeof(struct ttm_backend));
1432
1433 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1434 ttm_round_pot(sizeof(struct ttm_buffer_object));
1435
1436 atomic_set(&glob->bo_count, 0);
1437
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001438 ret = kobject_init_and_add(
1439 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001440 if (unlikely(ret != 0))
1441 kobject_put(&glob->kobj);
1442 return ret;
1443out_no_shrink:
1444 __free_page(glob->dummy_read_page);
1445out_no_drp:
1446 kfree(glob);
1447 return ret;
1448}
1449EXPORT_SYMBOL(ttm_bo_global_init);
1450
1451
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001452int ttm_bo_device_release(struct ttm_bo_device *bdev)
1453{
1454 int ret = 0;
1455 unsigned i = TTM_NUM_MEM_TYPES;
1456 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001457 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001458
1459 while (i--) {
1460 man = &bdev->man[i];
1461 if (man->has_type) {
1462 man->use_type = false;
1463 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1464 ret = -EBUSY;
1465 printk(KERN_ERR TTM_PFX
1466 "DRM memory manager type %d "
1467 "is not clean.\n", i);
1468 }
1469 man->has_type = false;
1470 }
1471 }
1472
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001473 mutex_lock(&glob->device_list_mutex);
1474 list_del(&bdev->device_list);
1475 mutex_unlock(&glob->device_list_mutex);
1476
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001477 if (!cancel_delayed_work(&bdev->wq))
1478 flush_scheduled_work();
1479
1480 while (ttm_bo_delayed_delete(bdev, true))
1481 ;
1482
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001483 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001484 if (list_empty(&bdev->ddestroy))
1485 TTM_DEBUG("Delayed destroy list was clean\n");
1486
1487 if (list_empty(&bdev->man[0].lru))
1488 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001489 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001490
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001491 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1492 write_lock(&bdev->vm_lock);
1493 drm_mm_takedown(&bdev->addr_space_mm);
1494 write_unlock(&bdev->vm_lock);
1495
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001496 return ret;
1497}
1498EXPORT_SYMBOL(ttm_bo_device_release);
1499
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001501 struct ttm_bo_global *glob,
1502 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001503 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001504 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001505{
1506 int ret = -EINVAL;
1507
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001508 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001509 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510
1511 memset(bdev->man, 0, sizeof(bdev->man));
1512
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001513 /*
1514 * Initialize the system memory buffer type.
1515 * Other types need to be driver / IOCTL initialized.
1516 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001517 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001518 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001519 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001520
1521 bdev->addr_space_rb = RB_ROOT;
1522 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1523 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001524 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525
1526 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1527 bdev->nice_mode = true;
1528 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001530 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001531 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001532
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001533 mutex_lock(&glob->device_list_mutex);
1534 list_add_tail(&bdev->device_list, &glob->device_list);
1535 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536
1537 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001538out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001539 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001540out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001541 return ret;
1542}
1543EXPORT_SYMBOL(ttm_bo_device_init);
1544
1545/*
1546 * buffer object vm functions.
1547 */
1548
1549bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1550{
1551 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1552
1553 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1554 if (mem->mem_type == TTM_PL_SYSTEM)
1555 return false;
1556
1557 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1558 return false;
1559
1560 if (mem->placement & TTM_PL_FLAG_CACHED)
1561 return false;
1562 }
1563 return true;
1564}
1565
1566int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
1567 struct ttm_mem_reg *mem,
1568 unsigned long *bus_base,
1569 unsigned long *bus_offset, unsigned long *bus_size)
1570{
1571 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1572
1573 *bus_size = 0;
1574 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1575 return -EINVAL;
1576
1577 if (ttm_mem_reg_is_pci(bdev, mem)) {
1578 *bus_offset = mem->mm_node->start << PAGE_SHIFT;
1579 *bus_size = mem->num_pages << PAGE_SHIFT;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001580 *bus_base = man->io_offset + (uintptr_t)man->io_addr;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001581 }
1582
1583 return 0;
1584}
1585
1586void 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
1729void ttm_bo_unblock_reservation(struct ttm_buffer_object *bo)
1730{
1731 atomic_set(&bo->reserved, 0);
1732 wake_up_all(&bo->event_queue);
1733}
1734
1735int ttm_bo_block_reservation(struct ttm_buffer_object *bo, bool interruptible,
1736 bool no_wait)
1737{
1738 int ret;
1739
1740 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
1741 if (no_wait)
1742 return -EBUSY;
1743 else if (interruptible) {
1744 ret = wait_event_interruptible
1745 (bo->event_queue, atomic_read(&bo->reserved) == 0);
1746 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001747 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001748 } else {
1749 wait_event(bo->event_queue,
1750 atomic_read(&bo->reserved) == 0);
1751 }
1752 }
1753 return 0;
1754}
1755
1756int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1757{
1758 int ret = 0;
1759
1760 /*
1761 * Using ttm_bo_reserve instead of ttm_bo_block_reservation
1762 * makes sure the lru lists are updated.
1763 */
1764
1765 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1766 if (unlikely(ret != 0))
1767 return ret;
1768 spin_lock(&bo->lock);
1769 ret = ttm_bo_wait(bo, false, true, no_wait);
1770 spin_unlock(&bo->lock);
1771 if (likely(ret == 0))
1772 atomic_inc(&bo->cpu_writers);
1773 ttm_bo_unreserve(bo);
1774 return ret;
1775}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001776EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001777
1778void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1779{
1780 if (atomic_dec_and_test(&bo->cpu_writers))
1781 wake_up_all(&bo->event_queue);
1782}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001783EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001784
1785/**
1786 * A buffer object shrink method that tries to swap out the first
1787 * buffer object on the bo_global::swap_lru list.
1788 */
1789
1790static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1791{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001792 struct ttm_bo_global *glob =
1793 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001794 struct ttm_buffer_object *bo;
1795 int ret = -EBUSY;
1796 int put_count;
1797 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1798
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001799 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001801 if (unlikely(list_empty(&glob->swap_lru))) {
1802 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001803 return -EBUSY;
1804 }
1805
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001806 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001807 struct ttm_buffer_object, swap);
1808 kref_get(&bo->list_kref);
1809
1810 /**
1811 * Reserve buffer. Since we unlock while sleeping, we need
1812 * to re-check that nobody removed us from the swap-list while
1813 * we slept.
1814 */
1815
1816 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1817 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001818 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001819 ttm_bo_wait_unreserved(bo, false);
1820 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001821 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001822 }
1823 }
1824
1825 BUG_ON(ret != 0);
1826 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001827 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001828
1829 while (put_count--)
1830 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1831
1832 /**
1833 * Wait for GPU, then move to system cached.
1834 */
1835
1836 spin_lock(&bo->lock);
1837 ret = ttm_bo_wait(bo, false, false, false);
1838 spin_unlock(&bo->lock);
1839
1840 if (unlikely(ret != 0))
1841 goto out;
1842
1843 if ((bo->mem.placement & swap_placement) != swap_placement) {
1844 struct ttm_mem_reg evict_mem;
1845
1846 evict_mem = bo->mem;
1847 evict_mem.mm_node = NULL;
1848 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1849 evict_mem.mem_type = TTM_PL_SYSTEM;
1850
1851 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001852 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001853 if (unlikely(ret != 0))
1854 goto out;
1855 }
1856
1857 ttm_bo_unmap_virtual(bo);
1858
1859 /**
1860 * Swap out. Buffer will be swapped in again as soon as
1861 * anyone tries to access a ttm page.
1862 */
1863
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001864 if (bo->bdev->driver->swap_notify)
1865 bo->bdev->driver->swap_notify(bo);
1866
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001867 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1868out:
1869
1870 /**
1871 *
1872 * Unreserve without putting on LRU to avoid swapping out an
1873 * already swapped buffer.
1874 */
1875
1876 atomic_set(&bo->reserved, 0);
1877 wake_up_all(&bo->event_queue);
1878 kref_put(&bo->list_kref, ttm_bo_release_list);
1879 return ret;
1880}
1881
1882void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1883{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001884 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001885 ;
1886}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001887EXPORT_SYMBOL(ttm_bo_swapout_all);