blob: 4a73f401644d8008ea9ff10dc1b19f466296a1dd [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{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169 if (interruptible) {
Jean Delvare965d3802010-10-09 12:36:45 +0000170 return wait_event_interruptible(bo->event_queue,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200171 atomic_read(&bo->reserved) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200172 } else {
173 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
Jean Delvare965d3802010-10-09 12:36:45 +0000174 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200175 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200176}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000177EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200178
179static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
180{
181 struct ttm_bo_device *bdev = bo->bdev;
182 struct ttm_mem_type_manager *man;
183
184 BUG_ON(!atomic_read(&bo->reserved));
185
186 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
187
188 BUG_ON(!list_empty(&bo->lru));
189
190 man = &bdev->man[bo->mem.mem_type];
191 list_add_tail(&bo->lru, &man->lru);
192 kref_get(&bo->list_kref);
193
194 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200195 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200196 kref_get(&bo->list_kref);
197 }
198 }
199}
200
201/**
202 * Call with the lru_lock held.
203 */
204
205static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
206{
207 int put_count = 0;
208
209 if (!list_empty(&bo->swap)) {
210 list_del_init(&bo->swap);
211 ++put_count;
212 }
213 if (!list_empty(&bo->lru)) {
214 list_del_init(&bo->lru);
215 ++put_count;
216 }
217
218 /*
219 * TODO: Add a driver hook to delete from
220 * driver-specific LRU's here.
221 */
222
223 return put_count;
224}
225
226int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
227 bool interruptible,
228 bool no_wait, bool use_sequence, uint32_t sequence)
229{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200230 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200231 int ret;
232
233 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
234 if (use_sequence && bo->seq_valid &&
235 (sequence - bo->val_seq < (1 << 31))) {
236 return -EAGAIN;
237 }
238
239 if (no_wait)
240 return -EBUSY;
241
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200242 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200243 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200244 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200245
246 if (unlikely(ret))
247 return ret;
248 }
249
250 if (use_sequence) {
251 bo->val_seq = sequence;
252 bo->seq_valid = true;
253 } else {
254 bo->seq_valid = false;
255 }
256
257 return 0;
258}
259EXPORT_SYMBOL(ttm_bo_reserve);
260
261static void ttm_bo_ref_bug(struct kref *list_kref)
262{
263 BUG();
264}
265
266int ttm_bo_reserve(struct ttm_buffer_object *bo,
267 bool interruptible,
268 bool no_wait, bool use_sequence, uint32_t sequence)
269{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200270 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200271 int put_count = 0;
272 int ret;
273
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200274 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200275 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
276 sequence);
277 if (likely(ret == 0))
278 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200279 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200280
281 while (put_count--)
282 kref_put(&bo->list_kref, ttm_bo_ref_bug);
283
284 return ret;
285}
286
287void ttm_bo_unreserve(struct ttm_buffer_object *bo)
288{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200289 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200290
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200291 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200292 ttm_bo_add_to_lru(bo);
293 atomic_set(&bo->reserved, 0);
294 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200295 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200296}
297EXPORT_SYMBOL(ttm_bo_unreserve);
298
299/*
300 * Call bo->mutex locked.
301 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200302static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
303{
304 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200305 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200306 int ret = 0;
307 uint32_t page_flags = 0;
308
309 TTM_ASSERT_LOCKED(&bo->mutex);
310 bo->ttm = NULL;
311
Dave Airliead49f502009-07-10 22:36:26 +1000312 if (bdev->need_dma32)
313 page_flags |= TTM_PAGE_FLAG_DMA32;
314
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200315 switch (bo->type) {
316 case ttm_bo_type_device:
317 if (zero_alloc)
318 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
319 case ttm_bo_type_kernel:
320 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200321 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200322 if (unlikely(bo->ttm == NULL))
323 ret = -ENOMEM;
324 break;
325 case ttm_bo_type_user:
326 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
327 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200328 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000329 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200330 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000331 break;
332 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200333
334 ret = ttm_tt_set_user(bo->ttm, current,
335 bo->buffer_start, bo->num_pages);
336 if (unlikely(ret != 0))
337 ttm_tt_destroy(bo->ttm);
338 break;
339 default:
340 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
341 ret = -EINVAL;
342 break;
343 }
344
345 return ret;
346}
347
348static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
349 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000350 bool evict, bool interruptible,
351 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200352{
353 struct ttm_bo_device *bdev = bo->bdev;
354 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
355 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
356 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
357 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
358 int ret = 0;
359
360 if (old_is_pci || new_is_pci ||
361 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
362 ttm_bo_unmap_virtual(bo);
363
364 /*
365 * Create and bind a ttm if required.
366 */
367
368 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
369 ret = ttm_bo_add_ttm(bo, false);
370 if (ret)
371 goto out_err;
372
373 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
374 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200375 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200376
377 if (mem->mem_type != TTM_PL_SYSTEM) {
378 ret = ttm_tt_bind(bo->ttm, mem);
379 if (ret)
380 goto out_err;
381 }
382
383 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100384 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200385 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200386 goto moved;
387 }
388
389 }
390
Dave Airliee024e112009-06-24 09:48:08 +1000391 if (bdev->driver->move_notify)
392 bdev->driver->move_notify(bo, mem);
393
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200394 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
395 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000396 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200397 else if (bdev->driver->move)
398 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000399 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200400 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000401 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200402
403 if (ret)
404 goto out_err;
405
406moved:
407 if (bo->evicted) {
408 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
409 if (ret)
410 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
411 bo->evicted = false;
412 }
413
414 if (bo->mem.mm_node) {
415 spin_lock(&bo->lock);
Ben Skeggsd961db72010-08-05 10:48:18 +1000416 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200417 bdev->man[bo->mem.mem_type].gpu_offset;
418 bo->cur_placement = bo->mem.placement;
419 spin_unlock(&bo->lock);
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100420 } else
421 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200422
423 return 0;
424
425out_err:
426 new_man = &bdev->man[bo->mem.mem_type];
427 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
428 ttm_tt_unbind(bo->ttm);
429 ttm_tt_destroy(bo->ttm);
430 bo->ttm = NULL;
431 }
432
433 return ret;
434}
435
436/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200437 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200438 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200439 * This is the place to put in driver specific hooks to release
440 * driver private resources.
441 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200442 */
443
444static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
445{
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200446 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200447 ttm_tt_unbind(bo->ttm);
448 ttm_tt_destroy(bo->ttm);
449 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200450 }
451
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200452 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200453
454 atomic_set(&bo->reserved, 0);
455 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200456}
457
458
459/**
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200460 * If bo idle, remove from delayed- and lru lists, and unref.
461 * If not idle, and already on delayed list, do nothing.
462 * If not idle, and not on delayed list, put on delayed list,
463 * up the list_kref and schedule a delayed list check.
464 */
465
466static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
467{
468 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200469 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200470 struct ttm_bo_driver *driver = bdev->driver;
471 int ret;
472
473 spin_lock(&bo->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200474retry:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200475 (void) ttm_bo_wait(bo, false, false, !remove_all);
476
477 if (!bo->sync_obj) {
478 int put_count;
479
480 spin_unlock(&bo->lock);
481
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200482 spin_lock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200483 ret = ttm_bo_reserve_locked(bo, false, !remove_all, false, 0);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100484
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200485 /**
486 * Someone else has the object reserved. Bail and retry.
487 */
488
489 if (unlikely(ret == -EBUSY)) {
490 spin_unlock(&glob->lru_lock);
491 spin_lock(&bo->lock);
492 goto requeue;
493 }
494
495 /**
496 * We can re-check for sync object without taking
497 * the bo::lock since setting the sync object requires
498 * also bo::reserved. A busy object at this point may
499 * be caused by another thread starting an accelerated
500 * eviction.
501 */
502
503 if (unlikely(bo->sync_obj)) {
504 atomic_set(&bo->reserved, 0);
505 wake_up_all(&bo->event_queue);
506 spin_unlock(&glob->lru_lock);
507 spin_lock(&bo->lock);
508 if (remove_all)
509 goto retry;
510 else
511 goto requeue;
512 }
513
514 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200515
516 if (!list_empty(&bo->ddestroy)) {
517 list_del_init(&bo->ddestroy);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100518 ++put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200519 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200520 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200521 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200522
523 while (put_count--)
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100524 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200525
526 return 0;
527 }
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200528requeue:
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200529 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200530 if (list_empty(&bo->ddestroy)) {
531 void *sync_obj = bo->sync_obj;
532 void *sync_obj_arg = bo->sync_obj_arg;
533
534 kref_get(&bo->list_kref);
535 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200536 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200537 spin_unlock(&bo->lock);
538
539 if (sync_obj)
540 driver->sync_obj_flush(sync_obj, sync_obj_arg);
541 schedule_delayed_work(&bdev->wq,
542 ((HZ / 100) < 1) ? 1 : HZ / 100);
543 ret = 0;
544
545 } else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200546 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200547 spin_unlock(&bo->lock);
548 ret = -EBUSY;
549 }
550
551 return ret;
552}
553
554/**
555 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
556 * encountered buffers.
557 */
558
559static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
560{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200561 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100562 struct ttm_buffer_object *entry = NULL;
563 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200564
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200565 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100566 if (list_empty(&bdev->ddestroy))
567 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200568
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100569 entry = list_first_entry(&bdev->ddestroy,
570 struct ttm_buffer_object, ddestroy);
571 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200572
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100573 for (;;) {
574 struct ttm_buffer_object *nentry = NULL;
575
576 if (entry->ddestroy.next != &bdev->ddestroy) {
577 nentry = list_first_entry(&entry->ddestroy,
578 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200579 kref_get(&nentry->list_kref);
580 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200581
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200582 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200583 ret = ttm_bo_cleanup_refs(entry, remove_all);
584 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100585 entry = nentry;
586
587 if (ret || !entry)
588 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200589
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200590 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100591 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200592 break;
593 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200594
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100595out_unlock:
596 spin_unlock(&glob->lru_lock);
597out:
598 if (entry)
599 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200600 return ret;
601}
602
603static void ttm_bo_delayed_workqueue(struct work_struct *work)
604{
605 struct ttm_bo_device *bdev =
606 container_of(work, struct ttm_bo_device, wq.work);
607
608 if (ttm_bo_delayed_delete(bdev, false)) {
609 schedule_delayed_work(&bdev->wq,
610 ((HZ / 100) < 1) ? 1 : HZ / 100);
611 }
612}
613
614static void ttm_bo_release(struct kref *kref)
615{
616 struct ttm_buffer_object *bo =
617 container_of(kref, struct ttm_buffer_object, kref);
618 struct ttm_bo_device *bdev = bo->bdev;
619
620 if (likely(bo->vm_node != NULL)) {
621 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
622 drm_mm_put_block(bo->vm_node);
623 bo->vm_node = NULL;
624 }
625 write_unlock(&bdev->vm_lock);
626 ttm_bo_cleanup_refs(bo, false);
627 kref_put(&bo->list_kref, ttm_bo_release_list);
628 write_lock(&bdev->vm_lock);
629}
630
631void ttm_bo_unref(struct ttm_buffer_object **p_bo)
632{
633 struct ttm_buffer_object *bo = *p_bo;
634 struct ttm_bo_device *bdev = bo->bdev;
635
636 *p_bo = NULL;
637 write_lock(&bdev->vm_lock);
638 kref_put(&bo->kref, ttm_bo_release);
639 write_unlock(&bdev->vm_lock);
640}
641EXPORT_SYMBOL(ttm_bo_unref);
642
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400643int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
644{
645 return cancel_delayed_work_sync(&bdev->wq);
646}
647EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
648
649void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
650{
651 if (resched)
652 schedule_delayed_work(&bdev->wq,
653 ((HZ / 100) < 1) ? 1 : HZ / 100);
654}
655EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
656
Jerome Glisseca262a9992009-12-08 15:33:32 +0100657static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000658 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200659{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200660 struct ttm_bo_device *bdev = bo->bdev;
661 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100662 struct ttm_placement placement;
663 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200664
665 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000666 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200667 spin_unlock(&bo->lock);
668
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200669 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100670 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200671 printk(KERN_ERR TTM_PFX
672 "Failed to expire sync object before "
673 "buffer eviction.\n");
674 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200675 goto out;
676 }
677
678 BUG_ON(!atomic_read(&bo->reserved));
679
680 evict_mem = bo->mem;
681 evict_mem.mm_node = NULL;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200682 evict_mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200683
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100684 placement.fpfn = 0;
685 placement.lpfn = 0;
686 placement.num_placement = 0;
687 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100688 bdev->driver->evict_flags(bo, &placement);
689 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000690 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200691 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100692 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200693 printk(KERN_ERR TTM_PFX
694 "Failed to find memory space for "
695 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100696 ttm_bo_mem_space_debug(bo, &placement);
697 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200698 goto out;
699 }
700
701 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000702 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200703 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100704 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200705 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000706 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200707 goto out;
708 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200709 bo->evicted = true;
710out:
711 return ret;
712}
713
Jerome Glisseca262a9992009-12-08 15:33:32 +0100714static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
715 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000716 bool interruptible, bool no_wait_reserve,
717 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100718{
719 struct ttm_bo_global *glob = bdev->glob;
720 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
721 struct ttm_buffer_object *bo;
722 int ret, put_count = 0;
723
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100724retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100725 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100726 if (list_empty(&man->lru)) {
727 spin_unlock(&glob->lru_lock);
728 return -EBUSY;
729 }
730
Jerome Glisseca262a9992009-12-08 15:33:32 +0100731 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
732 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100733
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000734 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100735
736 if (unlikely(ret == -EBUSY)) {
737 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000738 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100739 ret = ttm_bo_wait_unreserved(bo, interruptible);
740
741 kref_put(&bo->list_kref, ttm_bo_release_list);
742
743 /**
744 * We *need* to retry after releasing the lru lock.
745 */
746
747 if (unlikely(ret != 0))
748 return ret;
749 goto retry;
750 }
751
752 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100753 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100754
755 BUG_ON(ret != 0);
756
Jerome Glisseca262a9992009-12-08 15:33:32 +0100757 while (put_count--)
758 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100759
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000760 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100761 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100762
Jerome Glisseca262a9992009-12-08 15:33:32 +0100763 kref_put(&bo->list_kref, ttm_bo_release_list);
764 return ret;
765}
766
Ben Skeggs42311ff2010-08-04 12:07:08 +1000767void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
768{
Ben Skeggsd961db72010-08-05 10:48:18 +1000769 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000770
Ben Skeggsd961db72010-08-05 10:48:18 +1000771 if (mem->mm_node)
772 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000773}
774EXPORT_SYMBOL(ttm_bo_mem_put);
775
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200776/**
777 * Repeatedly evict memory from the LRU for @mem_type until we create enough
778 * space, or we've evicted everything and there isn't enough space.
779 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100780static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
781 uint32_t mem_type,
782 struct ttm_placement *placement,
783 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000784 bool interruptible,
785 bool no_wait_reserve,
786 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200787{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100788 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200789 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200790 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200791 int ret;
792
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200793 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000794 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200795 if (unlikely(ret != 0))
796 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000797 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100798 break;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200799 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100800 if (list_empty(&man->lru)) {
801 spin_unlock(&glob->lru_lock);
802 break;
803 }
804 spin_unlock(&glob->lru_lock);
805 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000806 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100807 if (unlikely(ret != 0))
808 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200809 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000810 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200811 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200812 mem->mem_type = mem_type;
813 return 0;
814}
815
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200816static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
817 uint32_t cur_placement,
818 uint32_t proposed_placement)
819{
820 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
821 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
822
823 /**
824 * Keep current caching if possible.
825 */
826
827 if ((cur_placement & caching) != 0)
828 result |= (cur_placement & caching);
829 else if ((man->default_caching & caching) != 0)
830 result |= man->default_caching;
831 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
832 result |= TTM_PL_FLAG_CACHED;
833 else if ((TTM_PL_FLAG_WC & caching) != 0)
834 result |= TTM_PL_FLAG_WC;
835 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
836 result |= TTM_PL_FLAG_UNCACHED;
837
838 return result;
839}
840
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200841static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
842 bool disallow_fixed,
843 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200844 uint32_t proposed_placement,
845 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200846{
847 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
848
849 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
850 return false;
851
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200852 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200853 return false;
854
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200855 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200856 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200857
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200858 cur_flags |= (proposed_placement & man->available_caching);
859
860 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200861 return true;
862}
863
864/**
865 * Creates space for memory region @mem according to its type.
866 *
867 * This function first searches for free space in compatible memory types in
868 * the priority order defined by the driver. If free space isn't found, then
869 * ttm_bo_mem_force_space is attempted in priority order to evict and find
870 * space.
871 */
872int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100873 struct ttm_placement *placement,
874 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000875 bool interruptible, bool no_wait_reserve,
876 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877{
878 struct ttm_bo_device *bdev = bo->bdev;
879 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880 uint32_t mem_type = TTM_PL_SYSTEM;
881 uint32_t cur_flags = 0;
882 bool type_found = false;
883 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100884 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100885 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200886
887 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000888 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100889 ret = ttm_mem_type_from_flags(placement->placement[i],
890 &mem_type);
891 if (ret)
892 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200893 man = &bdev->man[mem_type];
894
895 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100896 bo->type == ttm_bo_type_user,
897 mem_type,
898 placement->placement[i],
899 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200900
901 if (!type_ok)
902 continue;
903
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200904 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
905 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100906 /*
907 * Use the access and other non-mapping-related flag bits from
908 * the memory placement flags to the current flags
909 */
910 ttm_flag_masked(&cur_flags, placement->placement[i],
911 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200912
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200913 if (mem_type == TTM_PL_SYSTEM)
914 break;
915
916 if (man->has_type && man->use_type) {
917 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000918 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100919 if (unlikely(ret))
920 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200921 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000922 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923 break;
924 }
925
Ben Skeggsd961db72010-08-05 10:48:18 +1000926 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200927 mem->mem_type = mem_type;
928 mem->placement = cur_flags;
929 return 0;
930 }
931
932 if (!type_found)
933 return -EINVAL;
934
Dave Airlieb6637522009-12-14 14:51:35 +1000935 for (i = 0; i < placement->num_busy_placement; ++i) {
936 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100937 &mem_type);
938 if (ret)
939 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200940 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200941 if (!man->has_type)
942 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200943 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100944 bo->type == ttm_bo_type_user,
945 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000946 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100947 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200948 continue;
949
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200950 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
951 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100952 /*
953 * Use the access and other non-mapping-related flag bits from
954 * the memory placement flags to the current flags
955 */
Dave Airlieb6637522009-12-14 14:51:35 +1000956 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100957 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200958
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100959
960 if (mem_type == TTM_PL_SYSTEM) {
961 mem->mem_type = mem_type;
962 mem->placement = cur_flags;
963 mem->mm_node = NULL;
964 return 0;
965 }
966
Jerome Glisseca262a9992009-12-08 15:33:32 +0100967 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000968 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200969 if (ret == 0 && mem->mm_node) {
970 mem->placement = cur_flags;
971 return 0;
972 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100973 if (ret == -ERESTARTSYS)
974 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200975 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100976 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200977 return ret;
978}
979EXPORT_SYMBOL(ttm_bo_mem_space);
980
981int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
982{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200983 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
984 return -EBUSY;
985
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100986 return wait_event_interruptible(bo->event_queue,
987 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000989EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200990
991int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100992 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000993 bool interruptible, bool no_wait_reserve,
994 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200995{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200996 int ret = 0;
997 struct ttm_mem_reg mem;
998
999 BUG_ON(!atomic_read(&bo->reserved));
1000
1001 /*
1002 * FIXME: It's possible to pipeline buffer moves.
1003 * Have the driver move function wait for idle when necessary,
1004 * instead of doing it here.
1005 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001007 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001008 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001009 if (ret)
1010 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001011 mem.num_pages = bo->num_pages;
1012 mem.size = mem.num_pages << PAGE_SHIFT;
1013 mem.page_alignment = bo->mem.page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001014 mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 /*
1016 * Determine where to move the buffer.
1017 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001018 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019 if (ret)
1020 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001021 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001022out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001023 if (ret && mem.mm_node)
1024 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001025 return ret;
1026}
1027
Jerome Glisseca262a9992009-12-08 15:33:32 +01001028static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001029 struct ttm_mem_reg *mem)
1030{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001031 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001032
Ben Skeggsd961db72010-08-05 10:48:18 +10001033 if (mem->mm_node && placement->lpfn != 0 &&
1034 (mem->start < placement->fpfn ||
1035 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001036 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{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001279 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001280 int ret = -EINVAL;
1281
1282 if (mem_type >= TTM_NUM_MEM_TYPES) {
1283 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1284 return ret;
1285 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001286 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001287
1288 if (!man->has_type) {
1289 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1290 "memory manager type %u\n", mem_type);
1291 return ret;
1292 }
1293
1294 man->use_type = false;
1295 man->has_type = false;
1296
1297 ret = 0;
1298 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001299 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001300
Ben Skeggsd961db72010-08-05 10:48:18 +10001301 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302 }
1303
1304 return ret;
1305}
1306EXPORT_SYMBOL(ttm_bo_clean_mm);
1307
1308int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1309{
1310 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1311
1312 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1313 printk(KERN_ERR TTM_PFX
1314 "Illegal memory manager memory type %u.\n",
1315 mem_type);
1316 return -EINVAL;
1317 }
1318
1319 if (!man->has_type) {
1320 printk(KERN_ERR TTM_PFX
1321 "Memory type %u has not been initialized.\n",
1322 mem_type);
1323 return 0;
1324 }
1325
Jerome Glisseca262a9992009-12-08 15:33:32 +01001326 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001327}
1328EXPORT_SYMBOL(ttm_bo_evict_mm);
1329
1330int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001331 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001332{
1333 int ret = -EINVAL;
1334 struct ttm_mem_type_manager *man;
1335
1336 if (type >= TTM_NUM_MEM_TYPES) {
1337 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1338 return ret;
1339 }
1340
1341 man = &bdev->man[type];
1342 if (man->has_type) {
1343 printk(KERN_ERR TTM_PFX
1344 "Memory manager already initialized for type %d\n",
1345 type);
1346 return ret;
1347 }
1348
1349 ret = bdev->driver->init_mem_type(bdev, type, man);
1350 if (ret)
1351 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001352 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001353
1354 ret = 0;
1355 if (type != TTM_PL_SYSTEM) {
1356 if (!p_size) {
1357 printk(KERN_ERR TTM_PFX
1358 "Zero size memory manager type %d\n",
1359 type);
1360 return ret;
1361 }
Ben Skeggsd961db72010-08-05 10:48:18 +10001362
1363 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001364 if (ret)
1365 return ret;
1366 }
1367 man->has_type = true;
1368 man->use_type = true;
1369 man->size = p_size;
1370
1371 INIT_LIST_HEAD(&man->lru);
1372
1373 return 0;
1374}
1375EXPORT_SYMBOL(ttm_bo_init_mm);
1376
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001377static void ttm_bo_global_kobj_release(struct kobject *kobj)
1378{
1379 struct ttm_bo_global *glob =
1380 container_of(kobj, struct ttm_bo_global, kobj);
1381
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001382 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1383 __free_page(glob->dummy_read_page);
1384 kfree(glob);
1385}
1386
Dave Airlieba4420c2010-03-09 10:56:52 +10001387void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001388{
1389 struct ttm_bo_global *glob = ref->object;
1390
1391 kobject_del(&glob->kobj);
1392 kobject_put(&glob->kobj);
1393}
1394EXPORT_SYMBOL(ttm_bo_global_release);
1395
Dave Airlieba4420c2010-03-09 10:56:52 +10001396int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001397{
1398 struct ttm_bo_global_ref *bo_ref =
1399 container_of(ref, struct ttm_bo_global_ref, ref);
1400 struct ttm_bo_global *glob = ref->object;
1401 int ret;
1402
1403 mutex_init(&glob->device_list_mutex);
1404 spin_lock_init(&glob->lru_lock);
1405 glob->mem_glob = bo_ref->mem_glob;
1406 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1407
1408 if (unlikely(glob->dummy_read_page == NULL)) {
1409 ret = -ENOMEM;
1410 goto out_no_drp;
1411 }
1412
1413 INIT_LIST_HEAD(&glob->swap_lru);
1414 INIT_LIST_HEAD(&glob->device_list);
1415
1416 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1417 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1418 if (unlikely(ret != 0)) {
1419 printk(KERN_ERR TTM_PFX
1420 "Could not register buffer object swapout.\n");
1421 goto out_no_shrink;
1422 }
1423
1424 glob->ttm_bo_extra_size =
1425 ttm_round_pot(sizeof(struct ttm_tt)) +
1426 ttm_round_pot(sizeof(struct ttm_backend));
1427
1428 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1429 ttm_round_pot(sizeof(struct ttm_buffer_object));
1430
1431 atomic_set(&glob->bo_count, 0);
1432
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001433 ret = kobject_init_and_add(
1434 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001435 if (unlikely(ret != 0))
1436 kobject_put(&glob->kobj);
1437 return ret;
1438out_no_shrink:
1439 __free_page(glob->dummy_read_page);
1440out_no_drp:
1441 kfree(glob);
1442 return ret;
1443}
1444EXPORT_SYMBOL(ttm_bo_global_init);
1445
1446
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001447int ttm_bo_device_release(struct ttm_bo_device *bdev)
1448{
1449 int ret = 0;
1450 unsigned i = TTM_NUM_MEM_TYPES;
1451 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001452 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001453
1454 while (i--) {
1455 man = &bdev->man[i];
1456 if (man->has_type) {
1457 man->use_type = false;
1458 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1459 ret = -EBUSY;
1460 printk(KERN_ERR TTM_PFX
1461 "DRM memory manager type %d "
1462 "is not clean.\n", i);
1463 }
1464 man->has_type = false;
1465 }
1466 }
1467
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001468 mutex_lock(&glob->device_list_mutex);
1469 list_del(&bdev->device_list);
1470 mutex_unlock(&glob->device_list_mutex);
1471
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001472 if (!cancel_delayed_work(&bdev->wq))
1473 flush_scheduled_work();
1474
1475 while (ttm_bo_delayed_delete(bdev, true))
1476 ;
1477
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001478 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001479 if (list_empty(&bdev->ddestroy))
1480 TTM_DEBUG("Delayed destroy list was clean\n");
1481
1482 if (list_empty(&bdev->man[0].lru))
1483 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001484 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001485
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001486 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1487 write_lock(&bdev->vm_lock);
1488 drm_mm_takedown(&bdev->addr_space_mm);
1489 write_unlock(&bdev->vm_lock);
1490
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001491 return ret;
1492}
1493EXPORT_SYMBOL(ttm_bo_device_release);
1494
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001495int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001496 struct ttm_bo_global *glob,
1497 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001498 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001499 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500{
1501 int ret = -EINVAL;
1502
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001503 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001504 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001505
1506 memset(bdev->man, 0, sizeof(bdev->man));
1507
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001508 /*
1509 * Initialize the system memory buffer type.
1510 * Other types need to be driver / IOCTL initialized.
1511 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001512 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001513 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001514 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001515
1516 bdev->addr_space_rb = RB_ROOT;
1517 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1518 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001519 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001520
1521 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1522 bdev->nice_mode = true;
1523 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001524 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001525 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001526 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001527
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001528 mutex_lock(&glob->device_list_mutex);
1529 list_add_tail(&bdev->device_list, &glob->device_list);
1530 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531
1532 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001533out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001535out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536 return ret;
1537}
1538EXPORT_SYMBOL(ttm_bo_device_init);
1539
1540/*
1541 * buffer object vm functions.
1542 */
1543
1544bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1545{
1546 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1547
1548 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1549 if (mem->mem_type == TTM_PL_SYSTEM)
1550 return false;
1551
1552 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1553 return false;
1554
1555 if (mem->placement & TTM_PL_FLAG_CACHED)
1556 return false;
1557 }
1558 return true;
1559}
1560
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001561void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1562{
1563 struct ttm_bo_device *bdev = bo->bdev;
1564 loff_t offset = (loff_t) bo->addr_space_offset;
1565 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1566
1567 if (!bdev->dev_mapping)
1568 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001569 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Jerome Glisse82c5da62010-04-09 14:39:23 +02001570 ttm_mem_io_free(bdev, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001571}
Dave Airliee024e112009-06-24 09:48:08 +10001572EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001573
1574static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1575{
1576 struct ttm_bo_device *bdev = bo->bdev;
1577 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1578 struct rb_node *parent = NULL;
1579 struct ttm_buffer_object *cur_bo;
1580 unsigned long offset = bo->vm_node->start;
1581 unsigned long cur_offset;
1582
1583 while (*cur) {
1584 parent = *cur;
1585 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1586 cur_offset = cur_bo->vm_node->start;
1587 if (offset < cur_offset)
1588 cur = &parent->rb_left;
1589 else if (offset > cur_offset)
1590 cur = &parent->rb_right;
1591 else
1592 BUG();
1593 }
1594
1595 rb_link_node(&bo->vm_rb, parent, cur);
1596 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1597}
1598
1599/**
1600 * ttm_bo_setup_vm:
1601 *
1602 * @bo: the buffer to allocate address space for
1603 *
1604 * Allocate address space in the drm device so that applications
1605 * can mmap the buffer and access the contents. This only
1606 * applies to ttm_bo_type_device objects as others are not
1607 * placed in the drm device address space.
1608 */
1609
1610static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1611{
1612 struct ttm_bo_device *bdev = bo->bdev;
1613 int ret;
1614
1615retry_pre_get:
1616 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1617 if (unlikely(ret != 0))
1618 return ret;
1619
1620 write_lock(&bdev->vm_lock);
1621 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1622 bo->mem.num_pages, 0, 0);
1623
1624 if (unlikely(bo->vm_node == NULL)) {
1625 ret = -ENOMEM;
1626 goto out_unlock;
1627 }
1628
1629 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1630 bo->mem.num_pages, 0);
1631
1632 if (unlikely(bo->vm_node == NULL)) {
1633 write_unlock(&bdev->vm_lock);
1634 goto retry_pre_get;
1635 }
1636
1637 ttm_bo_vm_insert_rb(bo);
1638 write_unlock(&bdev->vm_lock);
1639 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1640
1641 return 0;
1642out_unlock:
1643 write_unlock(&bdev->vm_lock);
1644 return ret;
1645}
1646
1647int ttm_bo_wait(struct ttm_buffer_object *bo,
1648 bool lazy, bool interruptible, bool no_wait)
1649{
1650 struct ttm_bo_driver *driver = bo->bdev->driver;
1651 void *sync_obj;
1652 void *sync_obj_arg;
1653 int ret = 0;
1654
1655 if (likely(bo->sync_obj == NULL))
1656 return 0;
1657
1658 while (bo->sync_obj) {
1659
1660 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1661 void *tmp_obj = bo->sync_obj;
1662 bo->sync_obj = NULL;
1663 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1664 spin_unlock(&bo->lock);
1665 driver->sync_obj_unref(&tmp_obj);
1666 spin_lock(&bo->lock);
1667 continue;
1668 }
1669
1670 if (no_wait)
1671 return -EBUSY;
1672
1673 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1674 sync_obj_arg = bo->sync_obj_arg;
1675 spin_unlock(&bo->lock);
1676 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1677 lazy, interruptible);
1678 if (unlikely(ret != 0)) {
1679 driver->sync_obj_unref(&sync_obj);
1680 spin_lock(&bo->lock);
1681 return ret;
1682 }
1683 spin_lock(&bo->lock);
1684 if (likely(bo->sync_obj == sync_obj &&
1685 bo->sync_obj_arg == sync_obj_arg)) {
1686 void *tmp_obj = bo->sync_obj;
1687 bo->sync_obj = NULL;
1688 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1689 &bo->priv_flags);
1690 spin_unlock(&bo->lock);
1691 driver->sync_obj_unref(&sync_obj);
1692 driver->sync_obj_unref(&tmp_obj);
1693 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001694 } else {
1695 spin_unlock(&bo->lock);
1696 driver->sync_obj_unref(&sync_obj);
1697 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001698 }
1699 }
1700 return 0;
1701}
1702EXPORT_SYMBOL(ttm_bo_wait);
1703
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001704int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1705{
1706 int ret = 0;
1707
1708 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001709 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001710 */
1711
1712 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1713 if (unlikely(ret != 0))
1714 return ret;
1715 spin_lock(&bo->lock);
1716 ret = ttm_bo_wait(bo, false, true, no_wait);
1717 spin_unlock(&bo->lock);
1718 if (likely(ret == 0))
1719 atomic_inc(&bo->cpu_writers);
1720 ttm_bo_unreserve(bo);
1721 return ret;
1722}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001723EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001724
1725void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1726{
1727 if (atomic_dec_and_test(&bo->cpu_writers))
1728 wake_up_all(&bo->event_queue);
1729}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001730EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001731
1732/**
1733 * A buffer object shrink method that tries to swap out the first
1734 * buffer object on the bo_global::swap_lru list.
1735 */
1736
1737static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1738{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001739 struct ttm_bo_global *glob =
1740 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001741 struct ttm_buffer_object *bo;
1742 int ret = -EBUSY;
1743 int put_count;
1744 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1745
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001746 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001747 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001748 if (unlikely(list_empty(&glob->swap_lru))) {
1749 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001750 return -EBUSY;
1751 }
1752
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001753 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001754 struct ttm_buffer_object, swap);
1755 kref_get(&bo->list_kref);
1756
1757 /**
1758 * Reserve buffer. Since we unlock while sleeping, we need
1759 * to re-check that nobody removed us from the swap-list while
1760 * we slept.
1761 */
1762
1763 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1764 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001765 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001766 ttm_bo_wait_unreserved(bo, false);
1767 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001768 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001769 }
1770 }
1771
1772 BUG_ON(ret != 0);
1773 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001774 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001775
1776 while (put_count--)
1777 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1778
1779 /**
1780 * Wait for GPU, then move to system cached.
1781 */
1782
1783 spin_lock(&bo->lock);
1784 ret = ttm_bo_wait(bo, false, false, false);
1785 spin_unlock(&bo->lock);
1786
1787 if (unlikely(ret != 0))
1788 goto out;
1789
1790 if ((bo->mem.placement & swap_placement) != swap_placement) {
1791 struct ttm_mem_reg evict_mem;
1792
1793 evict_mem = bo->mem;
1794 evict_mem.mm_node = NULL;
1795 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1796 evict_mem.mem_type = TTM_PL_SYSTEM;
1797
1798 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001799 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800 if (unlikely(ret != 0))
1801 goto out;
1802 }
1803
1804 ttm_bo_unmap_virtual(bo);
1805
1806 /**
1807 * Swap out. Buffer will be swapped in again as soon as
1808 * anyone tries to access a ttm page.
1809 */
1810
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001811 if (bo->bdev->driver->swap_notify)
1812 bo->bdev->driver->swap_notify(bo);
1813
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001814 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1815out:
1816
1817 /**
1818 *
1819 * Unreserve without putting on LRU to avoid swapping out an
1820 * already swapped buffer.
1821 */
1822
1823 atomic_set(&bo->reserved, 0);
1824 wake_up_all(&bo->event_queue);
1825 kref_put(&bo->list_kref, ttm_bo_release_list);
1826 return ret;
1827}
1828
1829void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1830{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001831 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001832 ;
1833}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001834EXPORT_SYMBOL(ttm_bo_swapout_all);