blob: a1cb783c7131c56de8aa19cf5afb6a668e5e4345 [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
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200458static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200459{
460 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200461 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200462 struct ttm_bo_driver *driver;
463 void *sync_obj;
464 void *sync_obj_arg;
465 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200466 int ret;
467
468 spin_lock(&bo->lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200469 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200470 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200471
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200472 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100473
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200474 /**
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200475 * Lock inversion between bo::reserve and bo::lock here,
476 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200477 */
478
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200479 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200480
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200481 if (unlikely(ret == -EBUSY))
482 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200483
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200484 spin_unlock(&bo->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200485 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200486
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200487 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200488 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200489
490 while (put_count--)
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100491 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200492
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200493 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200494 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200495 spin_lock(&glob->lru_lock);
496 }
497queue:
498 sync_obj = bo->sync_obj;
499 sync_obj_arg = bo->sync_obj_arg;
500 driver = bdev->driver;
501
502 kref_get(&bo->list_kref);
503 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
504 spin_unlock(&glob->lru_lock);
505 spin_unlock(&bo->lock);
506
507 if (sync_obj)
508 driver->sync_obj_flush(sync_obj, sync_obj_arg);
509 schedule_delayed_work(&bdev->wq,
510 ((HZ / 100) < 1) ? 1 : HZ / 100);
511}
512
513/**
514 * function ttm_bo_cleanup_refs
515 * If bo idle, remove from delayed- and lru lists, and unref.
516 * If not idle, do nothing.
517 *
518 * @interruptible Any sleeps should occur interruptibly.
519 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
520 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
521 */
522
523static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
524 bool interruptible,
525 bool no_wait_reserve,
526 bool no_wait_gpu)
527{
528 struct ttm_bo_global *glob = bo->glob;
529 int put_count;
530 int ret = 0;
531
532retry:
533 spin_lock(&bo->lock);
534 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
535 spin_unlock(&bo->lock);
536
537 if (unlikely(ret != 0))
538 return ret;
539
540 spin_lock(&glob->lru_lock);
541 ret = ttm_bo_reserve_locked(bo, interruptible,
542 no_wait_reserve, false, 0);
543
544 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200545 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200546 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200547 }
548
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200549 /**
550 * We can re-check for sync object without taking
551 * the bo::lock since setting the sync object requires
552 * also bo::reserved. A busy object at this point may
553 * be caused by another thread recently starting an accelerated
554 * eviction.
555 */
556
557 if (unlikely(bo->sync_obj)) {
558 atomic_set(&bo->reserved, 0);
559 wake_up_all(&bo->event_queue);
560 spin_unlock(&glob->lru_lock);
561 goto retry;
562 }
563
564 put_count = ttm_bo_del_from_lru(bo);
565 list_del_init(&bo->ddestroy);
566 ++put_count;
567
568 spin_unlock(&glob->lru_lock);
569 ttm_bo_cleanup_memtype_use(bo);
570
571 while (put_count--)
572 kref_put(&bo->list_kref, ttm_bo_ref_bug);
573
574 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200575}
576
577/**
578 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
579 * encountered buffers.
580 */
581
582static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
583{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200584 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100585 struct ttm_buffer_object *entry = NULL;
586 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200587
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200588 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100589 if (list_empty(&bdev->ddestroy))
590 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200591
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100592 entry = list_first_entry(&bdev->ddestroy,
593 struct ttm_buffer_object, ddestroy);
594 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200595
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100596 for (;;) {
597 struct ttm_buffer_object *nentry = NULL;
598
599 if (entry->ddestroy.next != &bdev->ddestroy) {
600 nentry = list_first_entry(&entry->ddestroy,
601 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200602 kref_get(&nentry->list_kref);
603 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200604
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200605 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200606 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
607 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200608 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100609 entry = nentry;
610
611 if (ret || !entry)
612 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200613
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200614 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100615 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616 break;
617 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200618
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100619out_unlock:
620 spin_unlock(&glob->lru_lock);
621out:
622 if (entry)
623 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200624 return ret;
625}
626
627static void ttm_bo_delayed_workqueue(struct work_struct *work)
628{
629 struct ttm_bo_device *bdev =
630 container_of(work, struct ttm_bo_device, wq.work);
631
632 if (ttm_bo_delayed_delete(bdev, false)) {
633 schedule_delayed_work(&bdev->wq,
634 ((HZ / 100) < 1) ? 1 : HZ / 100);
635 }
636}
637
638static void ttm_bo_release(struct kref *kref)
639{
640 struct ttm_buffer_object *bo =
641 container_of(kref, struct ttm_buffer_object, kref);
642 struct ttm_bo_device *bdev = bo->bdev;
643
644 if (likely(bo->vm_node != NULL)) {
645 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
646 drm_mm_put_block(bo->vm_node);
647 bo->vm_node = NULL;
648 }
649 write_unlock(&bdev->vm_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200650 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200651 kref_put(&bo->list_kref, ttm_bo_release_list);
652 write_lock(&bdev->vm_lock);
653}
654
655void ttm_bo_unref(struct ttm_buffer_object **p_bo)
656{
657 struct ttm_buffer_object *bo = *p_bo;
658 struct ttm_bo_device *bdev = bo->bdev;
659
660 *p_bo = NULL;
661 write_lock(&bdev->vm_lock);
662 kref_put(&bo->kref, ttm_bo_release);
663 write_unlock(&bdev->vm_lock);
664}
665EXPORT_SYMBOL(ttm_bo_unref);
666
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400667int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
668{
669 return cancel_delayed_work_sync(&bdev->wq);
670}
671EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
672
673void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
674{
675 if (resched)
676 schedule_delayed_work(&bdev->wq,
677 ((HZ / 100) < 1) ? 1 : HZ / 100);
678}
679EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
680
Jerome Glisseca262a9992009-12-08 15:33:32 +0100681static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000682 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200683{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200684 struct ttm_bo_device *bdev = bo->bdev;
685 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100686 struct ttm_placement placement;
687 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200688
689 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000690 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200691 spin_unlock(&bo->lock);
692
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200693 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100694 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200695 printk(KERN_ERR TTM_PFX
696 "Failed to expire sync object before "
697 "buffer eviction.\n");
698 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200699 goto out;
700 }
701
702 BUG_ON(!atomic_read(&bo->reserved));
703
704 evict_mem = bo->mem;
705 evict_mem.mm_node = NULL;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200706 evict_mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200707
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100708 placement.fpfn = 0;
709 placement.lpfn = 0;
710 placement.num_placement = 0;
711 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100712 bdev->driver->evict_flags(bo, &placement);
713 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000714 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200715 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100716 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200717 printk(KERN_ERR TTM_PFX
718 "Failed to find memory space for "
719 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100720 ttm_bo_mem_space_debug(bo, &placement);
721 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200722 goto out;
723 }
724
725 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000726 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200727 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100728 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200729 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000730 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200731 goto out;
732 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200733 bo->evicted = true;
734out:
735 return ret;
736}
737
Jerome Glisseca262a9992009-12-08 15:33:32 +0100738static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
739 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000740 bool interruptible, bool no_wait_reserve,
741 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100742{
743 struct ttm_bo_global *glob = bdev->glob;
744 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
745 struct ttm_buffer_object *bo;
746 int ret, put_count = 0;
747
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100748retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100749 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100750 if (list_empty(&man->lru)) {
751 spin_unlock(&glob->lru_lock);
752 return -EBUSY;
753 }
754
Jerome Glisseca262a9992009-12-08 15:33:32 +0100755 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
756 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100757
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200758 if (!list_empty(&bo->ddestroy)) {
759 spin_unlock(&glob->lru_lock);
760 ret = ttm_bo_cleanup_refs(bo, interruptible,
761 no_wait_reserve, no_wait_gpu);
762 kref_put(&bo->list_kref, ttm_bo_release_list);
763
764 if (likely(ret == 0 || ret == -ERESTARTSYS))
765 return ret;
766
767 goto retry;
768 }
769
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000770 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100771
772 if (unlikely(ret == -EBUSY)) {
773 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000774 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100775 ret = ttm_bo_wait_unreserved(bo, interruptible);
776
777 kref_put(&bo->list_kref, ttm_bo_release_list);
778
779 /**
780 * We *need* to retry after releasing the lru lock.
781 */
782
783 if (unlikely(ret != 0))
784 return ret;
785 goto retry;
786 }
787
788 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100789 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100790
791 BUG_ON(ret != 0);
792
Jerome Glisseca262a9992009-12-08 15:33:32 +0100793 while (put_count--)
794 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100795
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000796 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100797 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100798
Jerome Glisseca262a9992009-12-08 15:33:32 +0100799 kref_put(&bo->list_kref, ttm_bo_release_list);
800 return ret;
801}
802
Ben Skeggs42311ff2010-08-04 12:07:08 +1000803void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
804{
Ben Skeggsd961db72010-08-05 10:48:18 +1000805 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000806
Ben Skeggsd961db72010-08-05 10:48:18 +1000807 if (mem->mm_node)
808 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000809}
810EXPORT_SYMBOL(ttm_bo_mem_put);
811
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200812/**
813 * Repeatedly evict memory from the LRU for @mem_type until we create enough
814 * space, or we've evicted everything and there isn't enough space.
815 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100816static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
817 uint32_t mem_type,
818 struct ttm_placement *placement,
819 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000820 bool interruptible,
821 bool no_wait_reserve,
822 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200823{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100824 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200825 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200826 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200827 int ret;
828
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200829 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000830 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200831 if (unlikely(ret != 0))
832 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000833 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100834 break;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200835 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100836 if (list_empty(&man->lru)) {
837 spin_unlock(&glob->lru_lock);
838 break;
839 }
840 spin_unlock(&glob->lru_lock);
841 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000842 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100843 if (unlikely(ret != 0))
844 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200845 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000846 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200847 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200848 mem->mem_type = mem_type;
849 return 0;
850}
851
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200852static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
853 uint32_t cur_placement,
854 uint32_t proposed_placement)
855{
856 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
857 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
858
859 /**
860 * Keep current caching if possible.
861 */
862
863 if ((cur_placement & caching) != 0)
864 result |= (cur_placement & caching);
865 else if ((man->default_caching & caching) != 0)
866 result |= man->default_caching;
867 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
868 result |= TTM_PL_FLAG_CACHED;
869 else if ((TTM_PL_FLAG_WC & caching) != 0)
870 result |= TTM_PL_FLAG_WC;
871 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
872 result |= TTM_PL_FLAG_UNCACHED;
873
874 return result;
875}
876
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
878 bool disallow_fixed,
879 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200880 uint32_t proposed_placement,
881 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882{
883 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
884
885 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
886 return false;
887
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200888 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200889 return false;
890
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200891 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200892 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200893
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200894 cur_flags |= (proposed_placement & man->available_caching);
895
896 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200897 return true;
898}
899
900/**
901 * Creates space for memory region @mem according to its type.
902 *
903 * This function first searches for free space in compatible memory types in
904 * the priority order defined by the driver. If free space isn't found, then
905 * ttm_bo_mem_force_space is attempted in priority order to evict and find
906 * space.
907 */
908int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100909 struct ttm_placement *placement,
910 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000911 bool interruptible, bool no_wait_reserve,
912 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200913{
914 struct ttm_bo_device *bdev = bo->bdev;
915 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200916 uint32_t mem_type = TTM_PL_SYSTEM;
917 uint32_t cur_flags = 0;
918 bool type_found = false;
919 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100920 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100921 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922
923 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000924 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100925 ret = ttm_mem_type_from_flags(placement->placement[i],
926 &mem_type);
927 if (ret)
928 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929 man = &bdev->man[mem_type];
930
931 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100932 bo->type == ttm_bo_type_user,
933 mem_type,
934 placement->placement[i],
935 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200936
937 if (!type_ok)
938 continue;
939
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200940 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
941 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100942 /*
943 * Use the access and other non-mapping-related flag bits from
944 * the memory placement flags to the current flags
945 */
946 ttm_flag_masked(&cur_flags, placement->placement[i],
947 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200948
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200949 if (mem_type == TTM_PL_SYSTEM)
950 break;
951
952 if (man->has_type && man->use_type) {
953 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000954 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100955 if (unlikely(ret))
956 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200957 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000958 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200959 break;
960 }
961
Ben Skeggsd961db72010-08-05 10:48:18 +1000962 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200963 mem->mem_type = mem_type;
964 mem->placement = cur_flags;
965 return 0;
966 }
967
968 if (!type_found)
969 return -EINVAL;
970
Dave Airlieb6637522009-12-14 14:51:35 +1000971 for (i = 0; i < placement->num_busy_placement; ++i) {
972 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100973 &mem_type);
974 if (ret)
975 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200977 if (!man->has_type)
978 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200979 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100980 bo->type == ttm_bo_type_user,
981 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000982 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100983 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200984 continue;
985
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200986 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
987 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100988 /*
989 * Use the access and other non-mapping-related flag bits from
990 * the memory placement flags to the current flags
991 */
Dave Airlieb6637522009-12-14 14:51:35 +1000992 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100993 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200994
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100995
996 if (mem_type == TTM_PL_SYSTEM) {
997 mem->mem_type = mem_type;
998 mem->placement = cur_flags;
999 mem->mm_node = NULL;
1000 return 0;
1001 }
1002
Jerome Glisseca262a9992009-12-08 15:33:32 +01001003 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001004 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005 if (ret == 0 && mem->mm_node) {
1006 mem->placement = cur_flags;
1007 return 0;
1008 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001009 if (ret == -ERESTARTSYS)
1010 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001011 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001012 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 return ret;
1014}
1015EXPORT_SYMBOL(ttm_bo_mem_space);
1016
1017int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1018{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1020 return -EBUSY;
1021
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001022 return wait_event_interruptible(bo->event_queue,
1023 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001024}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001025EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001026
1027int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001028 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001029 bool interruptible, bool no_wait_reserve,
1030 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001031{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001032 int ret = 0;
1033 struct ttm_mem_reg mem;
1034
1035 BUG_ON(!atomic_read(&bo->reserved));
1036
1037 /*
1038 * FIXME: It's possible to pipeline buffer moves.
1039 * Have the driver move function wait for idle when necessary,
1040 * instead of doing it here.
1041 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001042 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001043 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001045 if (ret)
1046 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001047 mem.num_pages = bo->num_pages;
1048 mem.size = mem.num_pages << PAGE_SHIFT;
1049 mem.page_alignment = bo->mem.page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001050 mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001051 /*
1052 * Determine where to move the buffer.
1053 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001054 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001055 if (ret)
1056 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001057 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001058out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001059 if (ret && mem.mm_node)
1060 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001061 return ret;
1062}
1063
Jerome Glisseca262a9992009-12-08 15:33:32 +01001064static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001065 struct ttm_mem_reg *mem)
1066{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001067 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001068
Ben Skeggsd961db72010-08-05 10:48:18 +10001069 if (mem->mm_node && placement->lpfn != 0 &&
1070 (mem->start < placement->fpfn ||
1071 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001072 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001073
Jerome Glisseca262a9992009-12-08 15:33:32 +01001074 for (i = 0; i < placement->num_placement; i++) {
1075 if ((placement->placement[i] & mem->placement &
1076 TTM_PL_MASK_CACHING) &&
1077 (placement->placement[i] & mem->placement &
1078 TTM_PL_MASK_MEM))
1079 return i;
1080 }
1081 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082}
1083
Jerome Glisse09855ac2009-12-10 17:16:27 +01001084int ttm_bo_validate(struct ttm_buffer_object *bo,
1085 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001086 bool interruptible, bool no_wait_reserve,
1087 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001088{
1089 int ret;
1090
1091 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001092 /* Check that range is valid */
1093 if (placement->lpfn || placement->fpfn)
1094 if (placement->fpfn > placement->lpfn ||
1095 (placement->lpfn - placement->fpfn) < bo->num_pages)
1096 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001097 /*
1098 * Check whether we need to move buffer.
1099 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001100 ret = ttm_bo_mem_compat(placement, &bo->mem);
1101 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001102 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001103 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001104 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001105 } else {
1106 /*
1107 * Use the access and other non-mapping-related flag bits from
1108 * the compatible memory placement flags to the active flags
1109 */
1110 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1111 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001112 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113 /*
1114 * We might need to add a TTM.
1115 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001116 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1117 ret = ttm_bo_add_ttm(bo, true);
1118 if (ret)
1119 return ret;
1120 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001121 return 0;
1122}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001123EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001124
Jerome Glisse09855ac2009-12-10 17:16:27 +01001125int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1126 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001127{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001128 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001129
Jerome Glisse09855ac2009-12-10 17:16:27 +01001130 if (placement->fpfn || placement->lpfn) {
1131 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1132 printk(KERN_ERR TTM_PFX "Page number range to small "
1133 "Need %lu pages, range is [%u, %u]\n",
1134 bo->mem.num_pages, placement->fpfn,
1135 placement->lpfn);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001136 return -EINVAL;
1137 }
Jerome Glisse09855ac2009-12-10 17:16:27 +01001138 }
1139 for (i = 0; i < placement->num_placement; i++) {
1140 if (!capable(CAP_SYS_ADMIN)) {
1141 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1142 printk(KERN_ERR TTM_PFX "Need to be root to "
1143 "modify NO_EVICT status.\n");
1144 return -EINVAL;
1145 }
1146 }
1147 }
1148 for (i = 0; i < placement->num_busy_placement; i++) {
1149 if (!capable(CAP_SYS_ADMIN)) {
1150 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1151 printk(KERN_ERR TTM_PFX "Need to be root to "
1152 "modify NO_EVICT status.\n");
1153 return -EINVAL;
1154 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001155 }
1156 }
1157 return 0;
1158}
1159
Jerome Glisse09855ac2009-12-10 17:16:27 +01001160int ttm_bo_init(struct ttm_bo_device *bdev,
1161 struct ttm_buffer_object *bo,
1162 unsigned long size,
1163 enum ttm_bo_type type,
1164 struct ttm_placement *placement,
1165 uint32_t page_alignment,
1166 unsigned long buffer_start,
1167 bool interruptible,
1168 struct file *persistant_swap_storage,
1169 size_t acc_size,
1170 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001171{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001172 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001173 unsigned long num_pages;
1174
1175 size += buffer_start & ~PAGE_MASK;
1176 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1177 if (num_pages == 0) {
1178 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1179 return -EINVAL;
1180 }
1181 bo->destroy = destroy;
1182
1183 spin_lock_init(&bo->lock);
1184 kref_init(&bo->kref);
1185 kref_init(&bo->list_kref);
1186 atomic_set(&bo->cpu_writers, 0);
1187 atomic_set(&bo->reserved, 1);
1188 init_waitqueue_head(&bo->event_queue);
1189 INIT_LIST_HEAD(&bo->lru);
1190 INIT_LIST_HEAD(&bo->ddestroy);
1191 INIT_LIST_HEAD(&bo->swap);
1192 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001193 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001194 bo->type = type;
1195 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001196 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001197 bo->mem.mem_type = TTM_PL_SYSTEM;
1198 bo->mem.num_pages = bo->num_pages;
1199 bo->mem.mm_node = NULL;
1200 bo->mem.page_alignment = page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001201 bo->mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001202 bo->buffer_start = buffer_start & PAGE_MASK;
1203 bo->priv_flags = 0;
1204 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1205 bo->seq_valid = false;
1206 bo->persistant_swap_storage = persistant_swap_storage;
1207 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001208 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209
Jerome Glisse09855ac2009-12-10 17:16:27 +01001210 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001211 if (unlikely(ret != 0))
1212 goto out_err;
1213
1214 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215 * For ttm_bo_type_device buffers, allocate
1216 * address space from the device.
1217 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001218 if (bo->type == ttm_bo_type_device) {
1219 ret = ttm_bo_setup_vm(bo);
1220 if (ret)
1221 goto out_err;
1222 }
1223
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001224 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001225 if (ret)
1226 goto out_err;
1227
1228 ttm_bo_unreserve(bo);
1229 return 0;
1230
1231out_err:
1232 ttm_bo_unreserve(bo);
1233 ttm_bo_unref(&bo);
1234
1235 return ret;
1236}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001237EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001238
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001239static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001240 unsigned long num_pages)
1241{
1242 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1243 PAGE_MASK;
1244
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001245 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001246}
1247
Jerome Glisse09855ac2009-12-10 17:16:27 +01001248int ttm_bo_create(struct ttm_bo_device *bdev,
1249 unsigned long size,
1250 enum ttm_bo_type type,
1251 struct ttm_placement *placement,
1252 uint32_t page_alignment,
1253 unsigned long buffer_start,
1254 bool interruptible,
1255 struct file *persistant_swap_storage,
1256 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001257{
1258 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001259 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001260 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001261
1262 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001263 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001264 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001265 if (unlikely(ret != 0))
1266 return ret;
1267
1268 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1269
1270 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001271 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272 return -ENOMEM;
1273 }
1274
Jerome Glisse09855ac2009-12-10 17:16:27 +01001275 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1276 buffer_start, interruptible,
1277 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001278 if (likely(ret == 0))
1279 *p_bo = bo;
1280
1281 return ret;
1282}
1283
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001284static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001285 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001286{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001287 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001288 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001289 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001290
1291 /*
1292 * Can't use standard list traversal since we're unlocking.
1293 */
1294
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001295 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001296 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001297 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001298 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001299 if (ret) {
1300 if (allow_errors) {
1301 return ret;
1302 } else {
1303 printk(KERN_ERR TTM_PFX
1304 "Cleanup eviction failed\n");
1305 }
1306 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001307 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001308 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001309 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001310 return 0;
1311}
1312
1313int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1314{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001315 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001316 int ret = -EINVAL;
1317
1318 if (mem_type >= TTM_NUM_MEM_TYPES) {
1319 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1320 return ret;
1321 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001322 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001323
1324 if (!man->has_type) {
1325 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1326 "memory manager type %u\n", mem_type);
1327 return ret;
1328 }
1329
1330 man->use_type = false;
1331 man->has_type = false;
1332
1333 ret = 0;
1334 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001335 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001336
Ben Skeggsd961db72010-08-05 10:48:18 +10001337 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001338 }
1339
1340 return ret;
1341}
1342EXPORT_SYMBOL(ttm_bo_clean_mm);
1343
1344int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1345{
1346 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1347
1348 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1349 printk(KERN_ERR TTM_PFX
1350 "Illegal memory manager memory type %u.\n",
1351 mem_type);
1352 return -EINVAL;
1353 }
1354
1355 if (!man->has_type) {
1356 printk(KERN_ERR TTM_PFX
1357 "Memory type %u has not been initialized.\n",
1358 mem_type);
1359 return 0;
1360 }
1361
Jerome Glisseca262a9992009-12-08 15:33:32 +01001362 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001363}
1364EXPORT_SYMBOL(ttm_bo_evict_mm);
1365
1366int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001367 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001368{
1369 int ret = -EINVAL;
1370 struct ttm_mem_type_manager *man;
1371
1372 if (type >= TTM_NUM_MEM_TYPES) {
1373 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1374 return ret;
1375 }
1376
1377 man = &bdev->man[type];
1378 if (man->has_type) {
1379 printk(KERN_ERR TTM_PFX
1380 "Memory manager already initialized for type %d\n",
1381 type);
1382 return ret;
1383 }
1384
1385 ret = bdev->driver->init_mem_type(bdev, type, man);
1386 if (ret)
1387 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001388 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001389
1390 ret = 0;
1391 if (type != TTM_PL_SYSTEM) {
1392 if (!p_size) {
1393 printk(KERN_ERR TTM_PFX
1394 "Zero size memory manager type %d\n",
1395 type);
1396 return ret;
1397 }
Ben Skeggsd961db72010-08-05 10:48:18 +10001398
1399 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001400 if (ret)
1401 return ret;
1402 }
1403 man->has_type = true;
1404 man->use_type = true;
1405 man->size = p_size;
1406
1407 INIT_LIST_HEAD(&man->lru);
1408
1409 return 0;
1410}
1411EXPORT_SYMBOL(ttm_bo_init_mm);
1412
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001413static void ttm_bo_global_kobj_release(struct kobject *kobj)
1414{
1415 struct ttm_bo_global *glob =
1416 container_of(kobj, struct ttm_bo_global, kobj);
1417
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001418 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1419 __free_page(glob->dummy_read_page);
1420 kfree(glob);
1421}
1422
Dave Airlieba4420c2010-03-09 10:56:52 +10001423void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001424{
1425 struct ttm_bo_global *glob = ref->object;
1426
1427 kobject_del(&glob->kobj);
1428 kobject_put(&glob->kobj);
1429}
1430EXPORT_SYMBOL(ttm_bo_global_release);
1431
Dave Airlieba4420c2010-03-09 10:56:52 +10001432int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001433{
1434 struct ttm_bo_global_ref *bo_ref =
1435 container_of(ref, struct ttm_bo_global_ref, ref);
1436 struct ttm_bo_global *glob = ref->object;
1437 int ret;
1438
1439 mutex_init(&glob->device_list_mutex);
1440 spin_lock_init(&glob->lru_lock);
1441 glob->mem_glob = bo_ref->mem_glob;
1442 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1443
1444 if (unlikely(glob->dummy_read_page == NULL)) {
1445 ret = -ENOMEM;
1446 goto out_no_drp;
1447 }
1448
1449 INIT_LIST_HEAD(&glob->swap_lru);
1450 INIT_LIST_HEAD(&glob->device_list);
1451
1452 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1453 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1454 if (unlikely(ret != 0)) {
1455 printk(KERN_ERR TTM_PFX
1456 "Could not register buffer object swapout.\n");
1457 goto out_no_shrink;
1458 }
1459
1460 glob->ttm_bo_extra_size =
1461 ttm_round_pot(sizeof(struct ttm_tt)) +
1462 ttm_round_pot(sizeof(struct ttm_backend));
1463
1464 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1465 ttm_round_pot(sizeof(struct ttm_buffer_object));
1466
1467 atomic_set(&glob->bo_count, 0);
1468
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001469 ret = kobject_init_and_add(
1470 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001471 if (unlikely(ret != 0))
1472 kobject_put(&glob->kobj);
1473 return ret;
1474out_no_shrink:
1475 __free_page(glob->dummy_read_page);
1476out_no_drp:
1477 kfree(glob);
1478 return ret;
1479}
1480EXPORT_SYMBOL(ttm_bo_global_init);
1481
1482
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001483int ttm_bo_device_release(struct ttm_bo_device *bdev)
1484{
1485 int ret = 0;
1486 unsigned i = TTM_NUM_MEM_TYPES;
1487 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001488 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001489
1490 while (i--) {
1491 man = &bdev->man[i];
1492 if (man->has_type) {
1493 man->use_type = false;
1494 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1495 ret = -EBUSY;
1496 printk(KERN_ERR TTM_PFX
1497 "DRM memory manager type %d "
1498 "is not clean.\n", i);
1499 }
1500 man->has_type = false;
1501 }
1502 }
1503
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001504 mutex_lock(&glob->device_list_mutex);
1505 list_del(&bdev->device_list);
1506 mutex_unlock(&glob->device_list_mutex);
1507
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001508 if (!cancel_delayed_work(&bdev->wq))
1509 flush_scheduled_work();
1510
1511 while (ttm_bo_delayed_delete(bdev, true))
1512 ;
1513
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001514 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001515 if (list_empty(&bdev->ddestroy))
1516 TTM_DEBUG("Delayed destroy list was clean\n");
1517
1518 if (list_empty(&bdev->man[0].lru))
1519 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001520 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001521
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1523 write_lock(&bdev->vm_lock);
1524 drm_mm_takedown(&bdev->addr_space_mm);
1525 write_unlock(&bdev->vm_lock);
1526
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001527 return ret;
1528}
1529EXPORT_SYMBOL(ttm_bo_device_release);
1530
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001532 struct ttm_bo_global *glob,
1533 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001534 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001535 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536{
1537 int ret = -EINVAL;
1538
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001539 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001540 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001541
1542 memset(bdev->man, 0, sizeof(bdev->man));
1543
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001544 /*
1545 * Initialize the system memory buffer type.
1546 * Other types need to be driver / IOCTL initialized.
1547 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001548 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001549 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001550 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001551
1552 bdev->addr_space_rb = RB_ROOT;
1553 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1554 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001555 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001556
1557 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1558 bdev->nice_mode = true;
1559 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001560 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001561 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001562 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001564 mutex_lock(&glob->device_list_mutex);
1565 list_add_tail(&bdev->device_list, &glob->device_list);
1566 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001567
1568 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001569out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001570 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001571out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001572 return ret;
1573}
1574EXPORT_SYMBOL(ttm_bo_device_init);
1575
1576/*
1577 * buffer object vm functions.
1578 */
1579
1580bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1581{
1582 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1583
1584 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1585 if (mem->mem_type == TTM_PL_SYSTEM)
1586 return false;
1587
1588 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1589 return false;
1590
1591 if (mem->placement & TTM_PL_FLAG_CACHED)
1592 return false;
1593 }
1594 return true;
1595}
1596
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001597void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1598{
1599 struct ttm_bo_device *bdev = bo->bdev;
1600 loff_t offset = (loff_t) bo->addr_space_offset;
1601 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1602
1603 if (!bdev->dev_mapping)
1604 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001605 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Jerome Glisse82c5da62010-04-09 14:39:23 +02001606 ttm_mem_io_free(bdev, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001607}
Dave Airliee024e112009-06-24 09:48:08 +10001608EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001609
1610static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1611{
1612 struct ttm_bo_device *bdev = bo->bdev;
1613 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1614 struct rb_node *parent = NULL;
1615 struct ttm_buffer_object *cur_bo;
1616 unsigned long offset = bo->vm_node->start;
1617 unsigned long cur_offset;
1618
1619 while (*cur) {
1620 parent = *cur;
1621 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1622 cur_offset = cur_bo->vm_node->start;
1623 if (offset < cur_offset)
1624 cur = &parent->rb_left;
1625 else if (offset > cur_offset)
1626 cur = &parent->rb_right;
1627 else
1628 BUG();
1629 }
1630
1631 rb_link_node(&bo->vm_rb, parent, cur);
1632 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1633}
1634
1635/**
1636 * ttm_bo_setup_vm:
1637 *
1638 * @bo: the buffer to allocate address space for
1639 *
1640 * Allocate address space in the drm device so that applications
1641 * can mmap the buffer and access the contents. This only
1642 * applies to ttm_bo_type_device objects as others are not
1643 * placed in the drm device address space.
1644 */
1645
1646static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1647{
1648 struct ttm_bo_device *bdev = bo->bdev;
1649 int ret;
1650
1651retry_pre_get:
1652 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1653 if (unlikely(ret != 0))
1654 return ret;
1655
1656 write_lock(&bdev->vm_lock);
1657 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1658 bo->mem.num_pages, 0, 0);
1659
1660 if (unlikely(bo->vm_node == NULL)) {
1661 ret = -ENOMEM;
1662 goto out_unlock;
1663 }
1664
1665 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1666 bo->mem.num_pages, 0);
1667
1668 if (unlikely(bo->vm_node == NULL)) {
1669 write_unlock(&bdev->vm_lock);
1670 goto retry_pre_get;
1671 }
1672
1673 ttm_bo_vm_insert_rb(bo);
1674 write_unlock(&bdev->vm_lock);
1675 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1676
1677 return 0;
1678out_unlock:
1679 write_unlock(&bdev->vm_lock);
1680 return ret;
1681}
1682
1683int ttm_bo_wait(struct ttm_buffer_object *bo,
1684 bool lazy, bool interruptible, bool no_wait)
1685{
1686 struct ttm_bo_driver *driver = bo->bdev->driver;
1687 void *sync_obj;
1688 void *sync_obj_arg;
1689 int ret = 0;
1690
1691 if (likely(bo->sync_obj == NULL))
1692 return 0;
1693
1694 while (bo->sync_obj) {
1695
1696 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1697 void *tmp_obj = bo->sync_obj;
1698 bo->sync_obj = NULL;
1699 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1700 spin_unlock(&bo->lock);
1701 driver->sync_obj_unref(&tmp_obj);
1702 spin_lock(&bo->lock);
1703 continue;
1704 }
1705
1706 if (no_wait)
1707 return -EBUSY;
1708
1709 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1710 sync_obj_arg = bo->sync_obj_arg;
1711 spin_unlock(&bo->lock);
1712 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1713 lazy, interruptible);
1714 if (unlikely(ret != 0)) {
1715 driver->sync_obj_unref(&sync_obj);
1716 spin_lock(&bo->lock);
1717 return ret;
1718 }
1719 spin_lock(&bo->lock);
1720 if (likely(bo->sync_obj == sync_obj &&
1721 bo->sync_obj_arg == sync_obj_arg)) {
1722 void *tmp_obj = bo->sync_obj;
1723 bo->sync_obj = NULL;
1724 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1725 &bo->priv_flags);
1726 spin_unlock(&bo->lock);
1727 driver->sync_obj_unref(&sync_obj);
1728 driver->sync_obj_unref(&tmp_obj);
1729 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001730 } else {
1731 spin_unlock(&bo->lock);
1732 driver->sync_obj_unref(&sync_obj);
1733 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001734 }
1735 }
1736 return 0;
1737}
1738EXPORT_SYMBOL(ttm_bo_wait);
1739
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001740int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1741{
1742 int ret = 0;
1743
1744 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001745 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001746 */
1747
1748 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1749 if (unlikely(ret != 0))
1750 return ret;
1751 spin_lock(&bo->lock);
1752 ret = ttm_bo_wait(bo, false, true, no_wait);
1753 spin_unlock(&bo->lock);
1754 if (likely(ret == 0))
1755 atomic_inc(&bo->cpu_writers);
1756 ttm_bo_unreserve(bo);
1757 return ret;
1758}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001759EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001760
1761void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1762{
1763 if (atomic_dec_and_test(&bo->cpu_writers))
1764 wake_up_all(&bo->event_queue);
1765}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001766EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001767
1768/**
1769 * A buffer object shrink method that tries to swap out the first
1770 * buffer object on the bo_global::swap_lru list.
1771 */
1772
1773static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1774{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001775 struct ttm_bo_global *glob =
1776 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001777 struct ttm_buffer_object *bo;
1778 int ret = -EBUSY;
1779 int put_count;
1780 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1781
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001782 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001783 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001784 if (unlikely(list_empty(&glob->swap_lru))) {
1785 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001786 return -EBUSY;
1787 }
1788
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001789 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001790 struct ttm_buffer_object, swap);
1791 kref_get(&bo->list_kref);
1792
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001793 if (!list_empty(&bo->ddestroy)) {
1794 spin_unlock(&glob->lru_lock);
1795 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1796 kref_put(&bo->list_kref, ttm_bo_release_list);
1797 continue;
1798 }
1799
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800 /**
1801 * Reserve buffer. Since we unlock while sleeping, we need
1802 * to re-check that nobody removed us from the swap-list while
1803 * we slept.
1804 */
1805
1806 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1807 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001808 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001809 ttm_bo_wait_unreserved(bo, false);
1810 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001811 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001812 }
1813 }
1814
1815 BUG_ON(ret != 0);
1816 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001817 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001818
1819 while (put_count--)
1820 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1821
1822 /**
1823 * Wait for GPU, then move to system cached.
1824 */
1825
1826 spin_lock(&bo->lock);
1827 ret = ttm_bo_wait(bo, false, false, false);
1828 spin_unlock(&bo->lock);
1829
1830 if (unlikely(ret != 0))
1831 goto out;
1832
1833 if ((bo->mem.placement & swap_placement) != swap_placement) {
1834 struct ttm_mem_reg evict_mem;
1835
1836 evict_mem = bo->mem;
1837 evict_mem.mm_node = NULL;
1838 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1839 evict_mem.mem_type = TTM_PL_SYSTEM;
1840
1841 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001842 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001843 if (unlikely(ret != 0))
1844 goto out;
1845 }
1846
1847 ttm_bo_unmap_virtual(bo);
1848
1849 /**
1850 * Swap out. Buffer will be swapped in again as soon as
1851 * anyone tries to access a ttm page.
1852 */
1853
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001854 if (bo->bdev->driver->swap_notify)
1855 bo->bdev->driver->swap_notify(bo);
1856
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001857 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1858out:
1859
1860 /**
1861 *
1862 * Unreserve without putting on LRU to avoid swapping out an
1863 * already swapped buffer.
1864 */
1865
1866 atomic_set(&bo->reserved, 0);
1867 wake_up_all(&bo->event_queue);
1868 kref_put(&bo->list_kref, ttm_bo_release_list);
1869 return ret;
1870}
1871
1872void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1873{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001874 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001875 ;
1876}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001877EXPORT_SYMBOL(ttm_bo_swapout_all);