blob: f561eead057d40bb1bea4fa1e0b4277e000aa0e7 [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 */
30
31#include "ttm/ttm_module.h"
32#include "ttm/ttm_bo_driver.h"
33#include "ttm/ttm_placement.h"
34#include <linux/jiffies.h>
35#include <linux/slab.h>
36#include <linux/sched.h>
37#include <linux/mm.h>
38#include <linux/file.h>
39#include <linux/module.h>
40
41#define TTM_ASSERT_LOCKED(param)
42#define TTM_DEBUG(fmt, arg...)
43#define TTM_BO_HASH_ORDER 13
44
45static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020046static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020047static void ttm_bo_global_kobj_release(struct kobject *kobj);
48
49static struct attribute ttm_bo_count = {
50 .name = "bo_count",
51 .mode = S_IRUGO
52};
53
Jerome Glissefb53f862009-12-09 21:55:10 +010054static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
55{
56 int i;
57
58 for (i = 0; i <= TTM_PL_PRIV5; i++)
59 if (flags & (1 << i)) {
60 *mem_type = i;
61 return 0;
62 }
63 return -EINVAL;
64}
65
Jerome Glisse5012f502009-12-10 18:07:26 +010066static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010067{
Jerome Glisse5012f502009-12-10 18:07:26 +010068 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
69
Jerome Glissefb53f862009-12-09 21:55:10 +010070 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
71 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
72 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
73 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010074 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
Jerome Glissefb53f862009-12-09 21:55:10 +010075 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
76 man->available_caching);
77 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
78 man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100079 if (mem_type != TTM_PL_SYSTEM)
80 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010081}
82
83static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
84 struct ttm_placement *placement)
85{
Jerome Glissefb53f862009-12-09 21:55:10 +010086 int i, ret, mem_type;
87
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010088 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
Jerome Glissefb53f862009-12-09 21:55:10 +010089 bo, bo->mem.num_pages, bo->mem.size >> 10,
90 bo->mem.size >> 20);
91 for (i = 0; i < placement->num_placement; i++) {
92 ret = ttm_mem_type_from_flags(placement->placement[i],
93 &mem_type);
94 if (ret)
95 return;
Jerome Glissefb53f862009-12-09 21:55:10 +010096 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
97 i, placement->placement[i], mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +010098 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +010099 }
100}
101
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200102static ssize_t ttm_bo_global_show(struct kobject *kobj,
103 struct attribute *attr,
104 char *buffer)
105{
106 struct ttm_bo_global *glob =
107 container_of(kobj, struct ttm_bo_global, kobj);
108
109 return snprintf(buffer, PAGE_SIZE, "%lu\n",
110 (unsigned long) atomic_read(&glob->bo_count));
111}
112
113static struct attribute *ttm_bo_global_attrs[] = {
114 &ttm_bo_count,
115 NULL
116};
117
Emese Revfy52cf25d2010-01-19 02:58:23 +0100118static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200119 .show = &ttm_bo_global_show
120};
121
122static struct kobj_type ttm_bo_glob_kobj_type = {
123 .release = &ttm_bo_global_kobj_release,
124 .sysfs_ops = &ttm_bo_global_ops,
125 .default_attrs = ttm_bo_global_attrs
126};
127
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200128
129static inline uint32_t ttm_bo_type_flags(unsigned type)
130{
131 return 1 << (type);
132}
133
134static void ttm_bo_release_list(struct kref *list_kref)
135{
136 struct ttm_buffer_object *bo =
137 container_of(list_kref, struct ttm_buffer_object, list_kref);
138 struct ttm_bo_device *bdev = bo->bdev;
139
140 BUG_ON(atomic_read(&bo->list_kref.refcount));
141 BUG_ON(atomic_read(&bo->kref.refcount));
142 BUG_ON(atomic_read(&bo->cpu_writers));
143 BUG_ON(bo->sync_obj != NULL);
144 BUG_ON(bo->mem.mm_node != NULL);
145 BUG_ON(!list_empty(&bo->lru));
146 BUG_ON(!list_empty(&bo->ddestroy));
147
148 if (bo->ttm)
149 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200150 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200151 if (bo->destroy)
152 bo->destroy(bo);
153 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200154 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200155 kfree(bo);
156 }
157}
158
159int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
160{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200161 if (interruptible) {
Jean Delvare965d3802010-10-09 12:36:45 +0000162 return wait_event_interruptible(bo->event_queue,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200163 atomic_read(&bo->reserved) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164 } else {
165 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
Jean Delvare965d3802010-10-09 12:36:45 +0000166 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200167 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200168}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000169EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200170
171static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
172{
173 struct ttm_bo_device *bdev = bo->bdev;
174 struct ttm_mem_type_manager *man;
175
176 BUG_ON(!atomic_read(&bo->reserved));
177
178 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
179
180 BUG_ON(!list_empty(&bo->lru));
181
182 man = &bdev->man[bo->mem.mem_type];
183 list_add_tail(&bo->lru, &man->lru);
184 kref_get(&bo->list_kref);
185
186 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200187 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200188 kref_get(&bo->list_kref);
189 }
190 }
191}
192
193/**
194 * Call with the lru_lock held.
195 */
196
197static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
198{
199 int put_count = 0;
200
201 if (!list_empty(&bo->swap)) {
202 list_del_init(&bo->swap);
203 ++put_count;
204 }
205 if (!list_empty(&bo->lru)) {
206 list_del_init(&bo->lru);
207 ++put_count;
208 }
209
210 /*
211 * TODO: Add a driver hook to delete from
212 * driver-specific LRU's here.
213 */
214
215 return put_count;
216}
217
218int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
219 bool interruptible,
220 bool no_wait, bool use_sequence, uint32_t sequence)
221{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200222 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200223 int ret;
224
225 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
226 if (use_sequence && bo->seq_valid &&
227 (sequence - bo->val_seq < (1 << 31))) {
228 return -EAGAIN;
229 }
230
231 if (no_wait)
232 return -EBUSY;
233
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200234 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200235 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200236 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200237
238 if (unlikely(ret))
239 return ret;
240 }
241
242 if (use_sequence) {
243 bo->val_seq = sequence;
244 bo->seq_valid = true;
245 } else {
246 bo->seq_valid = false;
247 }
248
249 return 0;
250}
251EXPORT_SYMBOL(ttm_bo_reserve);
252
253static void ttm_bo_ref_bug(struct kref *list_kref)
254{
255 BUG();
256}
257
258int ttm_bo_reserve(struct ttm_buffer_object *bo,
259 bool interruptible,
260 bool no_wait, bool use_sequence, uint32_t sequence)
261{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200262 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200263 int put_count = 0;
264 int ret;
265
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200266 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200267 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
268 sequence);
269 if (likely(ret == 0))
270 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200271 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200272
273 while (put_count--)
274 kref_put(&bo->list_kref, ttm_bo_ref_bug);
275
276 return ret;
277}
278
279void ttm_bo_unreserve(struct ttm_buffer_object *bo)
280{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200281 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200282
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200283 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200284 ttm_bo_add_to_lru(bo);
285 atomic_set(&bo->reserved, 0);
286 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200287 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200288}
289EXPORT_SYMBOL(ttm_bo_unreserve);
290
291/*
292 * Call bo->mutex locked.
293 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200294static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
295{
296 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200297 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200298 int ret = 0;
299 uint32_t page_flags = 0;
300
301 TTM_ASSERT_LOCKED(&bo->mutex);
302 bo->ttm = NULL;
303
Dave Airliead49f502009-07-10 22:36:26 +1000304 if (bdev->need_dma32)
305 page_flags |= TTM_PAGE_FLAG_DMA32;
306
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200307 switch (bo->type) {
308 case ttm_bo_type_device:
309 if (zero_alloc)
310 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
311 case ttm_bo_type_kernel:
312 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200313 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200314 if (unlikely(bo->ttm == NULL))
315 ret = -ENOMEM;
316 break;
317 case ttm_bo_type_user:
318 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
319 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200320 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000321 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200322 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000323 break;
324 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200325
326 ret = ttm_tt_set_user(bo->ttm, current,
327 bo->buffer_start, bo->num_pages);
328 if (unlikely(ret != 0))
329 ttm_tt_destroy(bo->ttm);
330 break;
331 default:
332 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
333 ret = -EINVAL;
334 break;
335 }
336
337 return ret;
338}
339
340static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
341 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000342 bool evict, bool interruptible,
343 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344{
345 struct ttm_bo_device *bdev = bo->bdev;
346 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
347 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
348 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
349 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
350 int ret = 0;
351
352 if (old_is_pci || new_is_pci ||
353 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
354 ttm_bo_unmap_virtual(bo);
355
356 /*
357 * Create and bind a ttm if required.
358 */
359
360 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
361 ret = ttm_bo_add_ttm(bo, false);
362 if (ret)
363 goto out_err;
364
365 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
366 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200367 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200368
369 if (mem->mem_type != TTM_PL_SYSTEM) {
370 ret = ttm_tt_bind(bo->ttm, mem);
371 if (ret)
372 goto out_err;
373 }
374
375 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100376 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200377 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200378 goto moved;
379 }
380
381 }
382
Dave Airliee024e112009-06-24 09:48:08 +1000383 if (bdev->driver->move_notify)
384 bdev->driver->move_notify(bo, mem);
385
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200386 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
387 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000388 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200389 else if (bdev->driver->move)
390 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000391 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200392 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000393 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200394
395 if (ret)
396 goto out_err;
397
398moved:
399 if (bo->evicted) {
400 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
401 if (ret)
402 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
403 bo->evicted = false;
404 }
405
406 if (bo->mem.mm_node) {
407 spin_lock(&bo->lock);
Ben Skeggsd961db72010-08-05 10:48:18 +1000408 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200409 bdev->man[bo->mem.mem_type].gpu_offset;
410 bo->cur_placement = bo->mem.placement;
411 spin_unlock(&bo->lock);
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100412 } else
413 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200414
415 return 0;
416
417out_err:
418 new_man = &bdev->man[bo->mem.mem_type];
419 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
420 ttm_tt_unbind(bo->ttm);
421 ttm_tt_destroy(bo->ttm);
422 bo->ttm = NULL;
423 }
424
425 return ret;
426}
427
428/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200429 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200430 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200431 * This is the place to put in driver specific hooks to release
432 * driver private resources.
433 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200434 */
435
436static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
437{
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200438 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200439 ttm_tt_unbind(bo->ttm);
440 ttm_tt_destroy(bo->ttm);
441 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200442 }
443
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200444 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200445
446 atomic_set(&bo->reserved, 0);
447 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200448}
449
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200450static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200451{
452 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200453 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200454 struct ttm_bo_driver *driver;
455 void *sync_obj;
456 void *sync_obj_arg;
457 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200458 int ret;
459
460 spin_lock(&bo->lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200461 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200462 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200463
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200464 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100465
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200466 /**
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200467 * Lock inversion between bo::reserve and bo::lock here,
468 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200469 */
470
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200471 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200472
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200473 if (unlikely(ret == -EBUSY))
474 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200475
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200476 spin_unlock(&bo->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200477 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200478
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200479 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200480 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200481
482 while (put_count--)
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100483 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200484
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200485 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200486 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200487 spin_lock(&glob->lru_lock);
488 }
489queue:
490 sync_obj = bo->sync_obj;
491 sync_obj_arg = bo->sync_obj_arg;
492 driver = bdev->driver;
493
494 kref_get(&bo->list_kref);
495 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
496 spin_unlock(&glob->lru_lock);
497 spin_unlock(&bo->lock);
498
499 if (sync_obj)
500 driver->sync_obj_flush(sync_obj, sync_obj_arg);
501 schedule_delayed_work(&bdev->wq,
502 ((HZ / 100) < 1) ? 1 : HZ / 100);
503}
504
505/**
506 * function ttm_bo_cleanup_refs
507 * If bo idle, remove from delayed- and lru lists, and unref.
508 * If not idle, do nothing.
509 *
510 * @interruptible Any sleeps should occur interruptibly.
511 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
512 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
513 */
514
515static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
516 bool interruptible,
517 bool no_wait_reserve,
518 bool no_wait_gpu)
519{
520 struct ttm_bo_global *glob = bo->glob;
521 int put_count;
522 int ret = 0;
523
524retry:
525 spin_lock(&bo->lock);
526 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
527 spin_unlock(&bo->lock);
528
529 if (unlikely(ret != 0))
530 return ret;
531
532 spin_lock(&glob->lru_lock);
533 ret = ttm_bo_reserve_locked(bo, interruptible,
534 no_wait_reserve, false, 0);
535
536 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200537 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200538 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200539 }
540
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200541 /**
542 * We can re-check for sync object without taking
543 * the bo::lock since setting the sync object requires
544 * also bo::reserved. A busy object at this point may
545 * be caused by another thread recently starting an accelerated
546 * eviction.
547 */
548
549 if (unlikely(bo->sync_obj)) {
550 atomic_set(&bo->reserved, 0);
551 wake_up_all(&bo->event_queue);
552 spin_unlock(&glob->lru_lock);
553 goto retry;
554 }
555
556 put_count = ttm_bo_del_from_lru(bo);
557 list_del_init(&bo->ddestroy);
558 ++put_count;
559
560 spin_unlock(&glob->lru_lock);
561 ttm_bo_cleanup_memtype_use(bo);
562
563 while (put_count--)
564 kref_put(&bo->list_kref, ttm_bo_ref_bug);
565
566 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200567}
568
569/**
570 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
571 * encountered buffers.
572 */
573
574static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
575{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200576 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100577 struct ttm_buffer_object *entry = NULL;
578 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200579
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200580 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100581 if (list_empty(&bdev->ddestroy))
582 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200583
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100584 entry = list_first_entry(&bdev->ddestroy,
585 struct ttm_buffer_object, ddestroy);
586 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200587
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100588 for (;;) {
589 struct ttm_buffer_object *nentry = NULL;
590
591 if (entry->ddestroy.next != &bdev->ddestroy) {
592 nentry = list_first_entry(&entry->ddestroy,
593 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200594 kref_get(&nentry->list_kref);
595 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200596
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200597 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200598 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
599 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200600 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100601 entry = nentry;
602
603 if (ret || !entry)
604 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200605
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200606 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100607 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200608 break;
609 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200610
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100611out_unlock:
612 spin_unlock(&glob->lru_lock);
613out:
614 if (entry)
615 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616 return ret;
617}
618
619static void ttm_bo_delayed_workqueue(struct work_struct *work)
620{
621 struct ttm_bo_device *bdev =
622 container_of(work, struct ttm_bo_device, wq.work);
623
624 if (ttm_bo_delayed_delete(bdev, false)) {
625 schedule_delayed_work(&bdev->wq,
626 ((HZ / 100) < 1) ? 1 : HZ / 100);
627 }
628}
629
630static void ttm_bo_release(struct kref *kref)
631{
632 struct ttm_buffer_object *bo =
633 container_of(kref, struct ttm_buffer_object, kref);
634 struct ttm_bo_device *bdev = bo->bdev;
635
636 if (likely(bo->vm_node != NULL)) {
637 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
638 drm_mm_put_block(bo->vm_node);
639 bo->vm_node = NULL;
640 }
641 write_unlock(&bdev->vm_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200642 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200643 kref_put(&bo->list_kref, ttm_bo_release_list);
644 write_lock(&bdev->vm_lock);
645}
646
647void ttm_bo_unref(struct ttm_buffer_object **p_bo)
648{
649 struct ttm_buffer_object *bo = *p_bo;
650 struct ttm_bo_device *bdev = bo->bdev;
651
652 *p_bo = NULL;
653 write_lock(&bdev->vm_lock);
654 kref_put(&bo->kref, ttm_bo_release);
655 write_unlock(&bdev->vm_lock);
656}
657EXPORT_SYMBOL(ttm_bo_unref);
658
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400659int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
660{
661 return cancel_delayed_work_sync(&bdev->wq);
662}
663EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
664
665void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
666{
667 if (resched)
668 schedule_delayed_work(&bdev->wq,
669 ((HZ / 100) < 1) ? 1 : HZ / 100);
670}
671EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
672
Jerome Glisseca262a9992009-12-08 15:33:32 +0100673static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000674 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200675{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200676 struct ttm_bo_device *bdev = bo->bdev;
677 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100678 struct ttm_placement placement;
679 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200680
681 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000682 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200683 spin_unlock(&bo->lock);
684
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200685 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100686 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200687 printk(KERN_ERR TTM_PFX
688 "Failed to expire sync object before "
689 "buffer eviction.\n");
690 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200691 goto out;
692 }
693
694 BUG_ON(!atomic_read(&bo->reserved));
695
696 evict_mem = bo->mem;
697 evict_mem.mm_node = NULL;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200698 evict_mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200699
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100700 placement.fpfn = 0;
701 placement.lpfn = 0;
702 placement.num_placement = 0;
703 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100704 bdev->driver->evict_flags(bo, &placement);
705 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000706 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200707 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100708 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200709 printk(KERN_ERR TTM_PFX
710 "Failed to find memory space for "
711 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100712 ttm_bo_mem_space_debug(bo, &placement);
713 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200714 goto out;
715 }
716
717 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000718 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200719 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100720 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200721 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000722 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200723 goto out;
724 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200725 bo->evicted = true;
726out:
727 return ret;
728}
729
Jerome Glisseca262a9992009-12-08 15:33:32 +0100730static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
731 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000732 bool interruptible, bool no_wait_reserve,
733 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100734{
735 struct ttm_bo_global *glob = bdev->glob;
736 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
737 struct ttm_buffer_object *bo;
738 int ret, put_count = 0;
739
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100740retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100741 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100742 if (list_empty(&man->lru)) {
743 spin_unlock(&glob->lru_lock);
744 return -EBUSY;
745 }
746
Jerome Glisseca262a9992009-12-08 15:33:32 +0100747 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
748 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100749
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200750 if (!list_empty(&bo->ddestroy)) {
751 spin_unlock(&glob->lru_lock);
752 ret = ttm_bo_cleanup_refs(bo, interruptible,
753 no_wait_reserve, no_wait_gpu);
754 kref_put(&bo->list_kref, ttm_bo_release_list);
755
756 if (likely(ret == 0 || ret == -ERESTARTSYS))
757 return ret;
758
759 goto retry;
760 }
761
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000762 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100763
764 if (unlikely(ret == -EBUSY)) {
765 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000766 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100767 ret = ttm_bo_wait_unreserved(bo, interruptible);
768
769 kref_put(&bo->list_kref, ttm_bo_release_list);
770
771 /**
772 * We *need* to retry after releasing the lru lock.
773 */
774
775 if (unlikely(ret != 0))
776 return ret;
777 goto retry;
778 }
779
780 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100781 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100782
783 BUG_ON(ret != 0);
784
Jerome Glisseca262a9992009-12-08 15:33:32 +0100785 while (put_count--)
786 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100787
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000788 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100789 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100790
Jerome Glisseca262a9992009-12-08 15:33:32 +0100791 kref_put(&bo->list_kref, ttm_bo_release_list);
792 return ret;
793}
794
Ben Skeggs42311ff2010-08-04 12:07:08 +1000795void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
796{
Ben Skeggsd961db72010-08-05 10:48:18 +1000797 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000798
Ben Skeggsd961db72010-08-05 10:48:18 +1000799 if (mem->mm_node)
800 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000801}
802EXPORT_SYMBOL(ttm_bo_mem_put);
803
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200804/**
805 * Repeatedly evict memory from the LRU for @mem_type until we create enough
806 * space, or we've evicted everything and there isn't enough space.
807 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100808static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
809 uint32_t mem_type,
810 struct ttm_placement *placement,
811 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000812 bool interruptible,
813 bool no_wait_reserve,
814 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200815{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100816 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200817 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200818 int ret;
819
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200820 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000821 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200822 if (unlikely(ret != 0))
823 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000824 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100825 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100826 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000827 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100828 if (unlikely(ret != 0))
829 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200830 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000831 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200832 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200833 mem->mem_type = mem_type;
834 return 0;
835}
836
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200837static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
838 uint32_t cur_placement,
839 uint32_t proposed_placement)
840{
841 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
842 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
843
844 /**
845 * Keep current caching if possible.
846 */
847
848 if ((cur_placement & caching) != 0)
849 result |= (cur_placement & caching);
850 else if ((man->default_caching & caching) != 0)
851 result |= man->default_caching;
852 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
853 result |= TTM_PL_FLAG_CACHED;
854 else if ((TTM_PL_FLAG_WC & caching) != 0)
855 result |= TTM_PL_FLAG_WC;
856 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
857 result |= TTM_PL_FLAG_UNCACHED;
858
859 return result;
860}
861
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200862static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
863 bool disallow_fixed,
864 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200865 uint32_t proposed_placement,
866 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867{
868 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
869
870 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
871 return false;
872
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200873 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200874 return false;
875
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200876 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200878
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200879 cur_flags |= (proposed_placement & man->available_caching);
880
881 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882 return true;
883}
884
885/**
886 * Creates space for memory region @mem according to its type.
887 *
888 * This function first searches for free space in compatible memory types in
889 * the priority order defined by the driver. If free space isn't found, then
890 * ttm_bo_mem_force_space is attempted in priority order to evict and find
891 * space.
892 */
893int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100894 struct ttm_placement *placement,
895 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000896 bool interruptible, bool no_wait_reserve,
897 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898{
899 struct ttm_bo_device *bdev = bo->bdev;
900 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200901 uint32_t mem_type = TTM_PL_SYSTEM;
902 uint32_t cur_flags = 0;
903 bool type_found = false;
904 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100905 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100906 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200907
908 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000909 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100910 ret = ttm_mem_type_from_flags(placement->placement[i],
911 &mem_type);
912 if (ret)
913 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200914 man = &bdev->man[mem_type];
915
916 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100917 bo->type == ttm_bo_type_user,
918 mem_type,
919 placement->placement[i],
920 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200921
922 if (!type_ok)
923 continue;
924
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200925 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
926 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100927 /*
928 * Use the access and other non-mapping-related flag bits from
929 * the memory placement flags to the current flags
930 */
931 ttm_flag_masked(&cur_flags, placement->placement[i],
932 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200933
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200934 if (mem_type == TTM_PL_SYSTEM)
935 break;
936
937 if (man->has_type && man->use_type) {
938 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000939 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100940 if (unlikely(ret))
941 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200942 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000943 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200944 break;
945 }
946
Ben Skeggsd961db72010-08-05 10:48:18 +1000947 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200948 mem->mem_type = mem_type;
949 mem->placement = cur_flags;
950 return 0;
951 }
952
953 if (!type_found)
954 return -EINVAL;
955
Dave Airlieb6637522009-12-14 14:51:35 +1000956 for (i = 0; i < placement->num_busy_placement; ++i) {
957 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100958 &mem_type);
959 if (ret)
960 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200961 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200962 if (!man->has_type)
963 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100965 bo->type == ttm_bo_type_user,
966 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000967 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100968 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200969 continue;
970
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200971 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
972 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100973 /*
974 * Use the access and other non-mapping-related flag bits from
975 * the memory placement flags to the current flags
976 */
Dave Airlieb6637522009-12-14 14:51:35 +1000977 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100978 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200979
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100980
981 if (mem_type == TTM_PL_SYSTEM) {
982 mem->mem_type = mem_type;
983 mem->placement = cur_flags;
984 mem->mm_node = NULL;
985 return 0;
986 }
987
Jerome Glisseca262a9992009-12-08 15:33:32 +0100988 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000989 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200990 if (ret == 0 && mem->mm_node) {
991 mem->placement = cur_flags;
992 return 0;
993 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100994 if (ret == -ERESTARTSYS)
995 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200996 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100997 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200998 return ret;
999}
1000EXPORT_SYMBOL(ttm_bo_mem_space);
1001
1002int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1003{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001004 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1005 return -EBUSY;
1006
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001007 return wait_event_interruptible(bo->event_queue,
1008 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001009}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001010EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001011
1012int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001013 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001014 bool interruptible, bool no_wait_reserve,
1015 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001016{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001017 int ret = 0;
1018 struct ttm_mem_reg mem;
1019
1020 BUG_ON(!atomic_read(&bo->reserved));
1021
1022 /*
1023 * FIXME: It's possible to pipeline buffer moves.
1024 * Have the driver move function wait for idle when necessary,
1025 * instead of doing it here.
1026 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001027 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001028 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001029 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001030 if (ret)
1031 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001032 mem.num_pages = bo->num_pages;
1033 mem.size = mem.num_pages << PAGE_SHIFT;
1034 mem.page_alignment = bo->mem.page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001035 mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036 /*
1037 * Determine where to move the buffer.
1038 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001039 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001040 if (ret)
1041 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001042 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001043out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001044 if (ret && mem.mm_node)
1045 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046 return ret;
1047}
1048
Jerome Glisseca262a9992009-12-08 15:33:32 +01001049static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050 struct ttm_mem_reg *mem)
1051{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001052 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001053
Ben Skeggsd961db72010-08-05 10:48:18 +10001054 if (mem->mm_node && placement->lpfn != 0 &&
1055 (mem->start < placement->fpfn ||
1056 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001057 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001058
Jerome Glisseca262a9992009-12-08 15:33:32 +01001059 for (i = 0; i < placement->num_placement; i++) {
1060 if ((placement->placement[i] & mem->placement &
1061 TTM_PL_MASK_CACHING) &&
1062 (placement->placement[i] & mem->placement &
1063 TTM_PL_MASK_MEM))
1064 return i;
1065 }
1066 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001067}
1068
Jerome Glisse09855ac2009-12-10 17:16:27 +01001069int ttm_bo_validate(struct ttm_buffer_object *bo,
1070 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001071 bool interruptible, bool no_wait_reserve,
1072 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001073{
1074 int ret;
1075
1076 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001077 /* Check that range is valid */
1078 if (placement->lpfn || placement->fpfn)
1079 if (placement->fpfn > placement->lpfn ||
1080 (placement->lpfn - placement->fpfn) < bo->num_pages)
1081 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082 /*
1083 * Check whether we need to move buffer.
1084 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001085 ret = ttm_bo_mem_compat(placement, &bo->mem);
1086 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001087 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001088 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001090 } else {
1091 /*
1092 * Use the access and other non-mapping-related flag bits from
1093 * the compatible memory placement flags to the active flags
1094 */
1095 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1096 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001097 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098 /*
1099 * We might need to add a TTM.
1100 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1102 ret = ttm_bo_add_ttm(bo, true);
1103 if (ret)
1104 return ret;
1105 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106 return 0;
1107}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001108EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001109
Jerome Glisse09855ac2009-12-10 17:16:27 +01001110int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1111 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001112{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001113 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001114
Jerome Glisse09855ac2009-12-10 17:16:27 +01001115 if (placement->fpfn || placement->lpfn) {
1116 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1117 printk(KERN_ERR TTM_PFX "Page number range to small "
1118 "Need %lu pages, range is [%u, %u]\n",
1119 bo->mem.num_pages, placement->fpfn,
1120 placement->lpfn);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001121 return -EINVAL;
1122 }
Jerome Glisse09855ac2009-12-10 17:16:27 +01001123 }
1124 for (i = 0; i < placement->num_placement; i++) {
1125 if (!capable(CAP_SYS_ADMIN)) {
1126 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1127 printk(KERN_ERR TTM_PFX "Need to be root to "
1128 "modify NO_EVICT status.\n");
1129 return -EINVAL;
1130 }
1131 }
1132 }
1133 for (i = 0; i < placement->num_busy_placement; i++) {
1134 if (!capable(CAP_SYS_ADMIN)) {
1135 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1136 printk(KERN_ERR TTM_PFX "Need to be root to "
1137 "modify NO_EVICT status.\n");
1138 return -EINVAL;
1139 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001140 }
1141 }
1142 return 0;
1143}
1144
Jerome Glisse09855ac2009-12-10 17:16:27 +01001145int ttm_bo_init(struct ttm_bo_device *bdev,
1146 struct ttm_buffer_object *bo,
1147 unsigned long size,
1148 enum ttm_bo_type type,
1149 struct ttm_placement *placement,
1150 uint32_t page_alignment,
1151 unsigned long buffer_start,
1152 bool interruptible,
1153 struct file *persistant_swap_storage,
1154 size_t acc_size,
1155 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001156{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001157 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158 unsigned long num_pages;
1159
1160 size += buffer_start & ~PAGE_MASK;
1161 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1162 if (num_pages == 0) {
1163 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1164 return -EINVAL;
1165 }
1166 bo->destroy = destroy;
1167
1168 spin_lock_init(&bo->lock);
1169 kref_init(&bo->kref);
1170 kref_init(&bo->list_kref);
1171 atomic_set(&bo->cpu_writers, 0);
1172 atomic_set(&bo->reserved, 1);
1173 init_waitqueue_head(&bo->event_queue);
1174 INIT_LIST_HEAD(&bo->lru);
1175 INIT_LIST_HEAD(&bo->ddestroy);
1176 INIT_LIST_HEAD(&bo->swap);
1177 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001178 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 bo->type = type;
1180 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001181 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001182 bo->mem.mem_type = TTM_PL_SYSTEM;
1183 bo->mem.num_pages = bo->num_pages;
1184 bo->mem.mm_node = NULL;
1185 bo->mem.page_alignment = page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001186 bo->mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187 bo->buffer_start = buffer_start & PAGE_MASK;
1188 bo->priv_flags = 0;
1189 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1190 bo->seq_valid = false;
1191 bo->persistant_swap_storage = persistant_swap_storage;
1192 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001193 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001194
Jerome Glisse09855ac2009-12-10 17:16:27 +01001195 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196 if (unlikely(ret != 0))
1197 goto out_err;
1198
1199 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200 * For ttm_bo_type_device buffers, allocate
1201 * address space from the device.
1202 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203 if (bo->type == ttm_bo_type_device) {
1204 ret = ttm_bo_setup_vm(bo);
1205 if (ret)
1206 goto out_err;
1207 }
1208
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001209 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001210 if (ret)
1211 goto out_err;
1212
1213 ttm_bo_unreserve(bo);
1214 return 0;
1215
1216out_err:
1217 ttm_bo_unreserve(bo);
1218 ttm_bo_unref(&bo);
1219
1220 return ret;
1221}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001222EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001224static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001225 unsigned long num_pages)
1226{
1227 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1228 PAGE_MASK;
1229
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001230 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231}
1232
Jerome Glisse09855ac2009-12-10 17:16:27 +01001233int ttm_bo_create(struct ttm_bo_device *bdev,
1234 unsigned long size,
1235 enum ttm_bo_type type,
1236 struct ttm_placement *placement,
1237 uint32_t page_alignment,
1238 unsigned long buffer_start,
1239 bool interruptible,
1240 struct file *persistant_swap_storage,
1241 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001242{
1243 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001244 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001245 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001246
1247 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001248 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001249 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001250 if (unlikely(ret != 0))
1251 return ret;
1252
1253 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1254
1255 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001256 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001257 return -ENOMEM;
1258 }
1259
Jerome Glisse09855ac2009-12-10 17:16:27 +01001260 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1261 buffer_start, interruptible,
1262 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001263 if (likely(ret == 0))
1264 *p_bo = bo;
1265
1266 return ret;
1267}
1268
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001270 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001272 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001273 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001274 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001275
1276 /*
1277 * Can't use standard list traversal since we're unlocking.
1278 */
1279
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001280 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001281 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001282 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001283 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001284 if (ret) {
1285 if (allow_errors) {
1286 return ret;
1287 } else {
1288 printk(KERN_ERR TTM_PFX
1289 "Cleanup eviction failed\n");
1290 }
1291 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001292 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001293 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001294 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001295 return 0;
1296}
1297
1298int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1299{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001300 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001301 int ret = -EINVAL;
1302
1303 if (mem_type >= TTM_NUM_MEM_TYPES) {
1304 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1305 return ret;
1306 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001307 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001308
1309 if (!man->has_type) {
1310 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1311 "memory manager type %u\n", mem_type);
1312 return ret;
1313 }
1314
1315 man->use_type = false;
1316 man->has_type = false;
1317
1318 ret = 0;
1319 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001320 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001321
Ben Skeggsd961db72010-08-05 10:48:18 +10001322 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001323 }
1324
1325 return ret;
1326}
1327EXPORT_SYMBOL(ttm_bo_clean_mm);
1328
1329int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1330{
1331 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1332
1333 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1334 printk(KERN_ERR TTM_PFX
1335 "Illegal memory manager memory type %u.\n",
1336 mem_type);
1337 return -EINVAL;
1338 }
1339
1340 if (!man->has_type) {
1341 printk(KERN_ERR TTM_PFX
1342 "Memory type %u has not been initialized.\n",
1343 mem_type);
1344 return 0;
1345 }
1346
Jerome Glisseca262a9992009-12-08 15:33:32 +01001347 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001348}
1349EXPORT_SYMBOL(ttm_bo_evict_mm);
1350
1351int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001352 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001353{
1354 int ret = -EINVAL;
1355 struct ttm_mem_type_manager *man;
1356
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001357 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001358 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001359 BUG_ON(man->has_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001360
1361 ret = bdev->driver->init_mem_type(bdev, type, man);
1362 if (ret)
1363 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001364 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001365
1366 ret = 0;
1367 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001368 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001369 if (ret)
1370 return ret;
1371 }
1372 man->has_type = true;
1373 man->use_type = true;
1374 man->size = p_size;
1375
1376 INIT_LIST_HEAD(&man->lru);
1377
1378 return 0;
1379}
1380EXPORT_SYMBOL(ttm_bo_init_mm);
1381
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001382static void ttm_bo_global_kobj_release(struct kobject *kobj)
1383{
1384 struct ttm_bo_global *glob =
1385 container_of(kobj, struct ttm_bo_global, kobj);
1386
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001387 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1388 __free_page(glob->dummy_read_page);
1389 kfree(glob);
1390}
1391
Dave Airlieba4420c2010-03-09 10:56:52 +10001392void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001393{
1394 struct ttm_bo_global *glob = ref->object;
1395
1396 kobject_del(&glob->kobj);
1397 kobject_put(&glob->kobj);
1398}
1399EXPORT_SYMBOL(ttm_bo_global_release);
1400
Dave Airlieba4420c2010-03-09 10:56:52 +10001401int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001402{
1403 struct ttm_bo_global_ref *bo_ref =
1404 container_of(ref, struct ttm_bo_global_ref, ref);
1405 struct ttm_bo_global *glob = ref->object;
1406 int ret;
1407
1408 mutex_init(&glob->device_list_mutex);
1409 spin_lock_init(&glob->lru_lock);
1410 glob->mem_glob = bo_ref->mem_glob;
1411 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1412
1413 if (unlikely(glob->dummy_read_page == NULL)) {
1414 ret = -ENOMEM;
1415 goto out_no_drp;
1416 }
1417
1418 INIT_LIST_HEAD(&glob->swap_lru);
1419 INIT_LIST_HEAD(&glob->device_list);
1420
1421 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1422 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1423 if (unlikely(ret != 0)) {
1424 printk(KERN_ERR TTM_PFX
1425 "Could not register buffer object swapout.\n");
1426 goto out_no_shrink;
1427 }
1428
1429 glob->ttm_bo_extra_size =
1430 ttm_round_pot(sizeof(struct ttm_tt)) +
1431 ttm_round_pot(sizeof(struct ttm_backend));
1432
1433 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1434 ttm_round_pot(sizeof(struct ttm_buffer_object));
1435
1436 atomic_set(&glob->bo_count, 0);
1437
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001438 ret = kobject_init_and_add(
1439 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001440 if (unlikely(ret != 0))
1441 kobject_put(&glob->kobj);
1442 return ret;
1443out_no_shrink:
1444 __free_page(glob->dummy_read_page);
1445out_no_drp:
1446 kfree(glob);
1447 return ret;
1448}
1449EXPORT_SYMBOL(ttm_bo_global_init);
1450
1451
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001452int ttm_bo_device_release(struct ttm_bo_device *bdev)
1453{
1454 int ret = 0;
1455 unsigned i = TTM_NUM_MEM_TYPES;
1456 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001457 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001458
1459 while (i--) {
1460 man = &bdev->man[i];
1461 if (man->has_type) {
1462 man->use_type = false;
1463 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1464 ret = -EBUSY;
1465 printk(KERN_ERR TTM_PFX
1466 "DRM memory manager type %d "
1467 "is not clean.\n", i);
1468 }
1469 man->has_type = false;
1470 }
1471 }
1472
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001473 mutex_lock(&glob->device_list_mutex);
1474 list_del(&bdev->device_list);
1475 mutex_unlock(&glob->device_list_mutex);
1476
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001477 if (!cancel_delayed_work(&bdev->wq))
1478 flush_scheduled_work();
1479
1480 while (ttm_bo_delayed_delete(bdev, true))
1481 ;
1482
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001483 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001484 if (list_empty(&bdev->ddestroy))
1485 TTM_DEBUG("Delayed destroy list was clean\n");
1486
1487 if (list_empty(&bdev->man[0].lru))
1488 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001489 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001490
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001491 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1492 write_lock(&bdev->vm_lock);
1493 drm_mm_takedown(&bdev->addr_space_mm);
1494 write_unlock(&bdev->vm_lock);
1495
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001496 return ret;
1497}
1498EXPORT_SYMBOL(ttm_bo_device_release);
1499
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001501 struct ttm_bo_global *glob,
1502 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001503 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001504 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001505{
1506 int ret = -EINVAL;
1507
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001508 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001509 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510
1511 memset(bdev->man, 0, sizeof(bdev->man));
1512
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001513 /*
1514 * Initialize the system memory buffer type.
1515 * Other types need to be driver / IOCTL initialized.
1516 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001517 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001518 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001519 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001520
1521 bdev->addr_space_rb = RB_ROOT;
1522 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1523 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001524 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525
1526 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1527 bdev->nice_mode = true;
1528 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001530 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001531 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001532
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001533 mutex_lock(&glob->device_list_mutex);
1534 list_add_tail(&bdev->device_list, &glob->device_list);
1535 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536
1537 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001538out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001539 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001540out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001541 return ret;
1542}
1543EXPORT_SYMBOL(ttm_bo_device_init);
1544
1545/*
1546 * buffer object vm functions.
1547 */
1548
1549bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1550{
1551 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1552
1553 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1554 if (mem->mem_type == TTM_PL_SYSTEM)
1555 return false;
1556
1557 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1558 return false;
1559
1560 if (mem->placement & TTM_PL_FLAG_CACHED)
1561 return false;
1562 }
1563 return true;
1564}
1565
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001566void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1567{
1568 struct ttm_bo_device *bdev = bo->bdev;
1569 loff_t offset = (loff_t) bo->addr_space_offset;
1570 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1571
1572 if (!bdev->dev_mapping)
1573 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001574 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Jerome Glisse82c5da62010-04-09 14:39:23 +02001575 ttm_mem_io_free(bdev, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001576}
Dave Airliee024e112009-06-24 09:48:08 +10001577EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001578
1579static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1580{
1581 struct ttm_bo_device *bdev = bo->bdev;
1582 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1583 struct rb_node *parent = NULL;
1584 struct ttm_buffer_object *cur_bo;
1585 unsigned long offset = bo->vm_node->start;
1586 unsigned long cur_offset;
1587
1588 while (*cur) {
1589 parent = *cur;
1590 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1591 cur_offset = cur_bo->vm_node->start;
1592 if (offset < cur_offset)
1593 cur = &parent->rb_left;
1594 else if (offset > cur_offset)
1595 cur = &parent->rb_right;
1596 else
1597 BUG();
1598 }
1599
1600 rb_link_node(&bo->vm_rb, parent, cur);
1601 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1602}
1603
1604/**
1605 * ttm_bo_setup_vm:
1606 *
1607 * @bo: the buffer to allocate address space for
1608 *
1609 * Allocate address space in the drm device so that applications
1610 * can mmap the buffer and access the contents. This only
1611 * applies to ttm_bo_type_device objects as others are not
1612 * placed in the drm device address space.
1613 */
1614
1615static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1616{
1617 struct ttm_bo_device *bdev = bo->bdev;
1618 int ret;
1619
1620retry_pre_get:
1621 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1622 if (unlikely(ret != 0))
1623 return ret;
1624
1625 write_lock(&bdev->vm_lock);
1626 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1627 bo->mem.num_pages, 0, 0);
1628
1629 if (unlikely(bo->vm_node == NULL)) {
1630 ret = -ENOMEM;
1631 goto out_unlock;
1632 }
1633
1634 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1635 bo->mem.num_pages, 0);
1636
1637 if (unlikely(bo->vm_node == NULL)) {
1638 write_unlock(&bdev->vm_lock);
1639 goto retry_pre_get;
1640 }
1641
1642 ttm_bo_vm_insert_rb(bo);
1643 write_unlock(&bdev->vm_lock);
1644 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1645
1646 return 0;
1647out_unlock:
1648 write_unlock(&bdev->vm_lock);
1649 return ret;
1650}
1651
1652int ttm_bo_wait(struct ttm_buffer_object *bo,
1653 bool lazy, bool interruptible, bool no_wait)
1654{
1655 struct ttm_bo_driver *driver = bo->bdev->driver;
1656 void *sync_obj;
1657 void *sync_obj_arg;
1658 int ret = 0;
1659
1660 if (likely(bo->sync_obj == NULL))
1661 return 0;
1662
1663 while (bo->sync_obj) {
1664
1665 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1666 void *tmp_obj = bo->sync_obj;
1667 bo->sync_obj = NULL;
1668 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1669 spin_unlock(&bo->lock);
1670 driver->sync_obj_unref(&tmp_obj);
1671 spin_lock(&bo->lock);
1672 continue;
1673 }
1674
1675 if (no_wait)
1676 return -EBUSY;
1677
1678 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1679 sync_obj_arg = bo->sync_obj_arg;
1680 spin_unlock(&bo->lock);
1681 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1682 lazy, interruptible);
1683 if (unlikely(ret != 0)) {
1684 driver->sync_obj_unref(&sync_obj);
1685 spin_lock(&bo->lock);
1686 return ret;
1687 }
1688 spin_lock(&bo->lock);
1689 if (likely(bo->sync_obj == sync_obj &&
1690 bo->sync_obj_arg == sync_obj_arg)) {
1691 void *tmp_obj = bo->sync_obj;
1692 bo->sync_obj = NULL;
1693 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1694 &bo->priv_flags);
1695 spin_unlock(&bo->lock);
1696 driver->sync_obj_unref(&sync_obj);
1697 driver->sync_obj_unref(&tmp_obj);
1698 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001699 } else {
1700 spin_unlock(&bo->lock);
1701 driver->sync_obj_unref(&sync_obj);
1702 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001703 }
1704 }
1705 return 0;
1706}
1707EXPORT_SYMBOL(ttm_bo_wait);
1708
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001709int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1710{
1711 int ret = 0;
1712
1713 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001714 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001715 */
1716
1717 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1718 if (unlikely(ret != 0))
1719 return ret;
1720 spin_lock(&bo->lock);
1721 ret = ttm_bo_wait(bo, false, true, no_wait);
1722 spin_unlock(&bo->lock);
1723 if (likely(ret == 0))
1724 atomic_inc(&bo->cpu_writers);
1725 ttm_bo_unreserve(bo);
1726 return ret;
1727}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001728EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001729
1730void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1731{
1732 if (atomic_dec_and_test(&bo->cpu_writers))
1733 wake_up_all(&bo->event_queue);
1734}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001735EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001736
1737/**
1738 * A buffer object shrink method that tries to swap out the first
1739 * buffer object on the bo_global::swap_lru list.
1740 */
1741
1742static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1743{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001744 struct ttm_bo_global *glob =
1745 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001746 struct ttm_buffer_object *bo;
1747 int ret = -EBUSY;
1748 int put_count;
1749 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1750
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001751 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001752 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001753 if (unlikely(list_empty(&glob->swap_lru))) {
1754 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001755 return -EBUSY;
1756 }
1757
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001758 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001759 struct ttm_buffer_object, swap);
1760 kref_get(&bo->list_kref);
1761
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001762 if (!list_empty(&bo->ddestroy)) {
1763 spin_unlock(&glob->lru_lock);
1764 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1765 kref_put(&bo->list_kref, ttm_bo_release_list);
1766 continue;
1767 }
1768
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001769 /**
1770 * Reserve buffer. Since we unlock while sleeping, we need
1771 * to re-check that nobody removed us from the swap-list while
1772 * we slept.
1773 */
1774
1775 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1776 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001777 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001778 ttm_bo_wait_unreserved(bo, false);
1779 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001780 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001781 }
1782 }
1783
1784 BUG_ON(ret != 0);
1785 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001786 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001787
1788 while (put_count--)
1789 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1790
1791 /**
1792 * Wait for GPU, then move to system cached.
1793 */
1794
1795 spin_lock(&bo->lock);
1796 ret = ttm_bo_wait(bo, false, false, false);
1797 spin_unlock(&bo->lock);
1798
1799 if (unlikely(ret != 0))
1800 goto out;
1801
1802 if ((bo->mem.placement & swap_placement) != swap_placement) {
1803 struct ttm_mem_reg evict_mem;
1804
1805 evict_mem = bo->mem;
1806 evict_mem.mm_node = NULL;
1807 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1808 evict_mem.mem_type = TTM_PL_SYSTEM;
1809
1810 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001811 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001812 if (unlikely(ret != 0))
1813 goto out;
1814 }
1815
1816 ttm_bo_unmap_virtual(bo);
1817
1818 /**
1819 * Swap out. Buffer will be swapped in again as soon as
1820 * anyone tries to access a ttm page.
1821 */
1822
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001823 if (bo->bdev->driver->swap_notify)
1824 bo->bdev->driver->swap_notify(bo);
1825
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001826 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1827out:
1828
1829 /**
1830 *
1831 * Unreserve without putting on LRU to avoid swapping out an
1832 * already swapped buffer.
1833 */
1834
1835 atomic_set(&bo->reserved, 0);
1836 wake_up_all(&bo->event_queue);
1837 kref_put(&bo->list_kref, ttm_bo_release_list);
1838 return ret;
1839}
1840
1841void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1842{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001843 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001844 ;
1845}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001846EXPORT_SYMBOL(ttm_bo_swapout_all);