blob: 5d8750830dc37adb02694148cee1106efc4e1749 [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>
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +020040#include <asm/atomic.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020041
42#define TTM_ASSERT_LOCKED(param)
43#define TTM_DEBUG(fmt, arg...)
44#define TTM_BO_HASH_ORDER 13
45
46static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020047static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020048static void ttm_bo_global_kobj_release(struct kobject *kobj);
49
50static struct attribute ttm_bo_count = {
51 .name = "bo_count",
52 .mode = S_IRUGO
53};
54
Jerome Glissefb53f862009-12-09 21:55:10 +010055static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
56{
57 int i;
58
59 for (i = 0; i <= TTM_PL_PRIV5; i++)
60 if (flags & (1 << i)) {
61 *mem_type = i;
62 return 0;
63 }
64 return -EINVAL;
65}
66
Jerome Glisse5012f502009-12-10 18:07:26 +010067static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010068{
Jerome Glisse5012f502009-12-10 18:07:26 +010069 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
70
Jerome Glissefb53f862009-12-09 21:55:10 +010071 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
72 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
73 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
74 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010075 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
Jerome Glissefb53f862009-12-09 21:55:10 +010076 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
77 man->available_caching);
78 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
79 man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100080 if (mem_type != TTM_PL_SYSTEM)
81 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010082}
83
84static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
85 struct ttm_placement *placement)
86{
Jerome Glissefb53f862009-12-09 21:55:10 +010087 int i, ret, mem_type;
88
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010089 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
Jerome Glissefb53f862009-12-09 21:55:10 +010090 bo, bo->mem.num_pages, bo->mem.size >> 10,
91 bo->mem.size >> 20);
92 for (i = 0; i < placement->num_placement; i++) {
93 ret = ttm_mem_type_from_flags(placement->placement[i],
94 &mem_type);
95 if (ret)
96 return;
Jerome Glissefb53f862009-12-09 21:55:10 +010097 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
98 i, placement->placement[i], mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +010099 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100100 }
101}
102
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200103static ssize_t ttm_bo_global_show(struct kobject *kobj,
104 struct attribute *attr,
105 char *buffer)
106{
107 struct ttm_bo_global *glob =
108 container_of(kobj, struct ttm_bo_global, kobj);
109
110 return snprintf(buffer, PAGE_SIZE, "%lu\n",
111 (unsigned long) atomic_read(&glob->bo_count));
112}
113
114static struct attribute *ttm_bo_global_attrs[] = {
115 &ttm_bo_count,
116 NULL
117};
118
Emese Revfy52cf25d2010-01-19 02:58:23 +0100119static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200120 .show = &ttm_bo_global_show
121};
122
123static struct kobj_type ttm_bo_glob_kobj_type = {
124 .release = &ttm_bo_global_kobj_release,
125 .sysfs_ops = &ttm_bo_global_ops,
126 .default_attrs = ttm_bo_global_attrs
127};
128
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200129
130static inline uint32_t ttm_bo_type_flags(unsigned type)
131{
132 return 1 << (type);
133}
134
135static void ttm_bo_release_list(struct kref *list_kref)
136{
137 struct ttm_buffer_object *bo =
138 container_of(list_kref, struct ttm_buffer_object, list_kref);
139 struct ttm_bo_device *bdev = bo->bdev;
140
141 BUG_ON(atomic_read(&bo->list_kref.refcount));
142 BUG_ON(atomic_read(&bo->kref.refcount));
143 BUG_ON(atomic_read(&bo->cpu_writers));
144 BUG_ON(bo->sync_obj != NULL);
145 BUG_ON(bo->mem.mm_node != NULL);
146 BUG_ON(!list_empty(&bo->lru));
147 BUG_ON(!list_empty(&bo->ddestroy));
148
149 if (bo->ttm)
150 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200151 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200152 if (bo->destroy)
153 bo->destroy(bo);
154 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200155 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 kfree(bo);
157 }
158}
159
160int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
161{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200162 if (interruptible) {
Jean Delvare965d3802010-10-09 12:36:45 +0000163 return wait_event_interruptible(bo->event_queue,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164 atomic_read(&bo->reserved) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165 } else {
166 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
Jean Delvare965d3802010-10-09 12:36:45 +0000167 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200168 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000170EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200171
Dave Airlied6ea8882010-11-22 13:24:40 +1000172void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200173{
174 struct ttm_bo_device *bdev = bo->bdev;
175 struct ttm_mem_type_manager *man;
176
177 BUG_ON(!atomic_read(&bo->reserved));
178
179 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
180
181 BUG_ON(!list_empty(&bo->lru));
182
183 man = &bdev->man[bo->mem.mem_type];
184 list_add_tail(&bo->lru, &man->lru);
185 kref_get(&bo->list_kref);
186
187 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200188 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200189 kref_get(&bo->list_kref);
190 }
191 }
192}
193
Dave Airlied6ea8882010-11-22 13:24:40 +1000194int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200195{
196 int put_count = 0;
197
198 if (!list_empty(&bo->swap)) {
199 list_del_init(&bo->swap);
200 ++put_count;
201 }
202 if (!list_empty(&bo->lru)) {
203 list_del_init(&bo->lru);
204 ++put_count;
205 }
206
207 /*
208 * TODO: Add a driver hook to delete from
209 * driver-specific LRU's here.
210 */
211
212 return put_count;
213}
214
215int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
216 bool interruptible,
217 bool no_wait, bool use_sequence, uint32_t sequence)
218{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200219 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200220 int ret;
221
222 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100223 /**
224 * Deadlock avoidance for multi-bo reserving.
225 */
Thomas Hellstrom96726fe2010-11-17 12:28:28 +0000226 if (use_sequence && bo->seq_valid) {
227 /**
228 * We've already reserved this one.
229 */
230 if (unlikely(sequence == bo->val_seq))
231 return -EDEADLK;
232 /**
233 * Already reserved by a thread that will not back
234 * off for us. We need to back off.
235 */
236 if (unlikely(sequence - bo->val_seq < (1 << 31)))
237 return -EAGAIN;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200238 }
239
240 if (no_wait)
241 return -EBUSY;
242
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200243 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200244 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200245 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200246
247 if (unlikely(ret))
248 return ret;
249 }
250
251 if (use_sequence) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100252 /**
253 * Wake up waiters that may need to recheck for deadlock,
254 * if we decreased the sequence number.
255 */
256 if (unlikely((bo->val_seq - sequence < (1 << 31))
257 || !bo->seq_valid))
258 wake_up_all(&bo->event_queue);
259
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200260 bo->val_seq = sequence;
261 bo->seq_valid = true;
262 } else {
263 bo->seq_valid = false;
264 }
265
266 return 0;
267}
268EXPORT_SYMBOL(ttm_bo_reserve);
269
270static void ttm_bo_ref_bug(struct kref *list_kref)
271{
272 BUG();
273}
274
Dave Airlied6ea8882010-11-22 13:24:40 +1000275void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
276 bool never_free)
277{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100278 kref_sub(&bo->list_kref, count,
279 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000280}
281
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200282int ttm_bo_reserve(struct ttm_buffer_object *bo,
283 bool interruptible,
284 bool no_wait, bool use_sequence, uint32_t sequence)
285{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200286 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200287 int put_count = 0;
288 int ret;
289
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200290 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200291 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
292 sequence);
293 if (likely(ret == 0))
294 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200295 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200296
Dave Airlied6ea8882010-11-22 13:24:40 +1000297 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200298
299 return ret;
300}
301
302void ttm_bo_unreserve(struct ttm_buffer_object *bo)
303{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200304 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200305
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200306 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200307 ttm_bo_add_to_lru(bo);
308 atomic_set(&bo->reserved, 0);
309 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200310 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200311}
312EXPORT_SYMBOL(ttm_bo_unreserve);
313
314/*
315 * Call bo->mutex locked.
316 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200317static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
318{
319 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200320 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200321 int ret = 0;
322 uint32_t page_flags = 0;
323
324 TTM_ASSERT_LOCKED(&bo->mutex);
325 bo->ttm = NULL;
326
Dave Airliead49f502009-07-10 22:36:26 +1000327 if (bdev->need_dma32)
328 page_flags |= TTM_PAGE_FLAG_DMA32;
329
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200330 switch (bo->type) {
331 case ttm_bo_type_device:
332 if (zero_alloc)
333 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
334 case ttm_bo_type_kernel:
335 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200336 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200337 if (unlikely(bo->ttm == NULL))
338 ret = -ENOMEM;
339 break;
340 case ttm_bo_type_user:
341 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
342 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200343 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000344 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200345 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000346 break;
347 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200348
349 ret = ttm_tt_set_user(bo->ttm, current,
350 bo->buffer_start, bo->num_pages);
351 if (unlikely(ret != 0))
352 ttm_tt_destroy(bo->ttm);
353 break;
354 default:
355 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
356 ret = -EINVAL;
357 break;
358 }
359
360 return ret;
361}
362
363static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
364 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000365 bool evict, bool interruptible,
366 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200367{
368 struct ttm_bo_device *bdev = bo->bdev;
369 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
370 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
371 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
372 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
373 int ret = 0;
374
375 if (old_is_pci || new_is_pci ||
376 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
377 ttm_bo_unmap_virtual(bo);
378
379 /*
380 * Create and bind a ttm if required.
381 */
382
383 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
384 ret = ttm_bo_add_ttm(bo, false);
385 if (ret)
386 goto out_err;
387
388 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
389 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200390 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200391
392 if (mem->mem_type != TTM_PL_SYSTEM) {
393 ret = ttm_tt_bind(bo->ttm, mem);
394 if (ret)
395 goto out_err;
396 }
397
398 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100399 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200400 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200401 goto moved;
402 }
403
404 }
405
Dave Airliee024e112009-06-24 09:48:08 +1000406 if (bdev->driver->move_notify)
407 bdev->driver->move_notify(bo, mem);
408
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200409 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
410 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000411 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200412 else if (bdev->driver->move)
413 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000414 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200415 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000416 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200417
418 if (ret)
419 goto out_err;
420
421moved:
422 if (bo->evicted) {
423 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
424 if (ret)
425 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
426 bo->evicted = false;
427 }
428
429 if (bo->mem.mm_node) {
430 spin_lock(&bo->lock);
Ben Skeggsd961db72010-08-05 10:48:18 +1000431 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200432 bdev->man[bo->mem.mem_type].gpu_offset;
433 bo->cur_placement = bo->mem.placement;
434 spin_unlock(&bo->lock);
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100435 } else
436 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200437
438 return 0;
439
440out_err:
441 new_man = &bdev->man[bo->mem.mem_type];
442 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
443 ttm_tt_unbind(bo->ttm);
444 ttm_tt_destroy(bo->ttm);
445 bo->ttm = NULL;
446 }
447
448 return ret;
449}
450
451/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200452 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200453 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200454 * This is the place to put in driver specific hooks to release
455 * driver private resources.
456 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200457 */
458
459static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
460{
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200461 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200462 ttm_tt_unbind(bo->ttm);
463 ttm_tt_destroy(bo->ttm);
464 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200465 }
466
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200467 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200468
469 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200470
471 /*
472 * Make processes trying to reserve really pick it up.
473 */
474 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200475 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200476}
477
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200478static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200479{
480 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200481 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200482 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000483 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200484 void *sync_obj_arg;
485 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200486 int ret;
487
488 spin_lock(&bo->lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200489 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200490 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200491
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200492 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100493
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200494 /**
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200495 * Lock inversion between bo::reserve and bo::lock here,
496 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200497 */
498
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200499 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200500
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200501 if (unlikely(ret == -EBUSY))
502 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200503
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200504 spin_unlock(&bo->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200505 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200506
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200507 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200508 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200509
Dave Airlied6ea8882010-11-22 13:24:40 +1000510 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200511
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200512 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200513 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200514 spin_lock(&glob->lru_lock);
515 }
516queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200517 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000518 if (bo->sync_obj)
519 sync_obj = driver->sync_obj_ref(bo->sync_obj);
520 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200521
522 kref_get(&bo->list_kref);
523 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
524 spin_unlock(&glob->lru_lock);
525 spin_unlock(&bo->lock);
526
Thomas Hellstromaa123262010-11-02 13:21:47 +0000527 if (sync_obj) {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200528 driver->sync_obj_flush(sync_obj, sync_obj_arg);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000529 driver->sync_obj_unref(&sync_obj);
530 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200531 schedule_delayed_work(&bdev->wq,
532 ((HZ / 100) < 1) ? 1 : HZ / 100);
533}
534
535/**
536 * function ttm_bo_cleanup_refs
537 * If bo idle, remove from delayed- and lru lists, and unref.
538 * If not idle, do nothing.
539 *
540 * @interruptible Any sleeps should occur interruptibly.
541 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
542 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
543 */
544
545static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
546 bool interruptible,
547 bool no_wait_reserve,
548 bool no_wait_gpu)
549{
550 struct ttm_bo_global *glob = bo->glob;
551 int put_count;
552 int ret = 0;
553
554retry:
555 spin_lock(&bo->lock);
556 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
557 spin_unlock(&bo->lock);
558
559 if (unlikely(ret != 0))
560 return ret;
561
562 spin_lock(&glob->lru_lock);
563 ret = ttm_bo_reserve_locked(bo, interruptible,
564 no_wait_reserve, false, 0);
565
566 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200567 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200568 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200569 }
570
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200571 /**
572 * We can re-check for sync object without taking
573 * the bo::lock since setting the sync object requires
574 * also bo::reserved. A busy object at this point may
575 * be caused by another thread recently starting an accelerated
576 * eviction.
577 */
578
579 if (unlikely(bo->sync_obj)) {
580 atomic_set(&bo->reserved, 0);
581 wake_up_all(&bo->event_queue);
582 spin_unlock(&glob->lru_lock);
583 goto retry;
584 }
585
586 put_count = ttm_bo_del_from_lru(bo);
587 list_del_init(&bo->ddestroy);
588 ++put_count;
589
590 spin_unlock(&glob->lru_lock);
591 ttm_bo_cleanup_memtype_use(bo);
592
Dave Airlied6ea8882010-11-22 13:24:40 +1000593 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200594
595 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200596}
597
598/**
599 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
600 * encountered buffers.
601 */
602
603static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
604{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200605 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100606 struct ttm_buffer_object *entry = NULL;
607 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200608
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200609 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100610 if (list_empty(&bdev->ddestroy))
611 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200612
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100613 entry = list_first_entry(&bdev->ddestroy,
614 struct ttm_buffer_object, ddestroy);
615 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100617 for (;;) {
618 struct ttm_buffer_object *nentry = NULL;
619
620 if (entry->ddestroy.next != &bdev->ddestroy) {
621 nentry = list_first_entry(&entry->ddestroy,
622 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200623 kref_get(&nentry->list_kref);
624 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200625
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200626 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200627 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
628 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200629 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100630 entry = nentry;
631
632 if (ret || !entry)
633 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200634
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200635 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100636 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200637 break;
638 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200639
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100640out_unlock:
641 spin_unlock(&glob->lru_lock);
642out:
643 if (entry)
644 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200645 return ret;
646}
647
648static void ttm_bo_delayed_workqueue(struct work_struct *work)
649{
650 struct ttm_bo_device *bdev =
651 container_of(work, struct ttm_bo_device, wq.work);
652
653 if (ttm_bo_delayed_delete(bdev, false)) {
654 schedule_delayed_work(&bdev->wq,
655 ((HZ / 100) < 1) ? 1 : HZ / 100);
656 }
657}
658
659static void ttm_bo_release(struct kref *kref)
660{
661 struct ttm_buffer_object *bo =
662 container_of(kref, struct ttm_buffer_object, kref);
663 struct ttm_bo_device *bdev = bo->bdev;
664
665 if (likely(bo->vm_node != NULL)) {
666 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
667 drm_mm_put_block(bo->vm_node);
668 bo->vm_node = NULL;
669 }
670 write_unlock(&bdev->vm_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200671 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200672 kref_put(&bo->list_kref, ttm_bo_release_list);
673 write_lock(&bdev->vm_lock);
674}
675
676void ttm_bo_unref(struct ttm_buffer_object **p_bo)
677{
678 struct ttm_buffer_object *bo = *p_bo;
679 struct ttm_bo_device *bdev = bo->bdev;
680
681 *p_bo = NULL;
682 write_lock(&bdev->vm_lock);
683 kref_put(&bo->kref, ttm_bo_release);
684 write_unlock(&bdev->vm_lock);
685}
686EXPORT_SYMBOL(ttm_bo_unref);
687
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400688int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
689{
690 return cancel_delayed_work_sync(&bdev->wq);
691}
692EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
693
694void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
695{
696 if (resched)
697 schedule_delayed_work(&bdev->wq,
698 ((HZ / 100) < 1) ? 1 : HZ / 100);
699}
700EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
701
Jerome Glisseca262a9992009-12-08 15:33:32 +0100702static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000703 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200704{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200705 struct ttm_bo_device *bdev = bo->bdev;
706 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100707 struct ttm_placement placement;
708 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200709
710 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000711 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200712 spin_unlock(&bo->lock);
713
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200714 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100715 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200716 printk(KERN_ERR TTM_PFX
717 "Failed to expire sync object before "
718 "buffer eviction.\n");
719 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200720 goto out;
721 }
722
723 BUG_ON(!atomic_read(&bo->reserved));
724
725 evict_mem = bo->mem;
726 evict_mem.mm_node = NULL;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200727 evict_mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200728
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100729 placement.fpfn = 0;
730 placement.lpfn = 0;
731 placement.num_placement = 0;
732 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100733 bdev->driver->evict_flags(bo, &placement);
734 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000735 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200736 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100737 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200738 printk(KERN_ERR TTM_PFX
739 "Failed to find memory space for "
740 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100741 ttm_bo_mem_space_debug(bo, &placement);
742 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200743 goto out;
744 }
745
746 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000747 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200748 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100749 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200750 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000751 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200752 goto out;
753 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200754 bo->evicted = true;
755out:
756 return ret;
757}
758
Jerome Glisseca262a9992009-12-08 15:33:32 +0100759static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
760 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000761 bool interruptible, bool no_wait_reserve,
762 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100763{
764 struct ttm_bo_global *glob = bdev->glob;
765 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
766 struct ttm_buffer_object *bo;
767 int ret, put_count = 0;
768
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100769retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100770 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100771 if (list_empty(&man->lru)) {
772 spin_unlock(&glob->lru_lock);
773 return -EBUSY;
774 }
775
Jerome Glisseca262a9992009-12-08 15:33:32 +0100776 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
777 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100778
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200779 if (!list_empty(&bo->ddestroy)) {
780 spin_unlock(&glob->lru_lock);
781 ret = ttm_bo_cleanup_refs(bo, interruptible,
782 no_wait_reserve, no_wait_gpu);
783 kref_put(&bo->list_kref, ttm_bo_release_list);
784
785 if (likely(ret == 0 || ret == -ERESTARTSYS))
786 return ret;
787
788 goto retry;
789 }
790
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000791 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100792
793 if (unlikely(ret == -EBUSY)) {
794 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000795 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100796 ret = ttm_bo_wait_unreserved(bo, interruptible);
797
798 kref_put(&bo->list_kref, ttm_bo_release_list);
799
800 /**
801 * We *need* to retry after releasing the lru lock.
802 */
803
804 if (unlikely(ret != 0))
805 return ret;
806 goto retry;
807 }
808
809 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100810 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100811
812 BUG_ON(ret != 0);
813
Dave Airlied6ea8882010-11-22 13:24:40 +1000814 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100815
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000816 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100817 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100818
Jerome Glisseca262a9992009-12-08 15:33:32 +0100819 kref_put(&bo->list_kref, ttm_bo_release_list);
820 return ret;
821}
822
Ben Skeggs42311ff2010-08-04 12:07:08 +1000823void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
824{
Ben Skeggsd961db72010-08-05 10:48:18 +1000825 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000826
Ben Skeggsd961db72010-08-05 10:48:18 +1000827 if (mem->mm_node)
828 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000829}
830EXPORT_SYMBOL(ttm_bo_mem_put);
831
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200832/**
833 * Repeatedly evict memory from the LRU for @mem_type until we create enough
834 * space, or we've evicted everything and there isn't enough space.
835 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100836static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
837 uint32_t mem_type,
838 struct ttm_placement *placement,
839 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000840 bool interruptible,
841 bool no_wait_reserve,
842 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200843{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100844 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200845 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200846 int ret;
847
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200848 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000849 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200850 if (unlikely(ret != 0))
851 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000852 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100853 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100854 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000855 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100856 if (unlikely(ret != 0))
857 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200858 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000859 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200860 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200861 mem->mem_type = mem_type;
862 return 0;
863}
864
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200865static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
866 uint32_t cur_placement,
867 uint32_t proposed_placement)
868{
869 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
870 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
871
872 /**
873 * Keep current caching if possible.
874 */
875
876 if ((cur_placement & caching) != 0)
877 result |= (cur_placement & caching);
878 else if ((man->default_caching & caching) != 0)
879 result |= man->default_caching;
880 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
881 result |= TTM_PL_FLAG_CACHED;
882 else if ((TTM_PL_FLAG_WC & caching) != 0)
883 result |= TTM_PL_FLAG_WC;
884 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
885 result |= TTM_PL_FLAG_UNCACHED;
886
887 return result;
888}
889
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200890static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
891 bool disallow_fixed,
892 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200893 uint32_t proposed_placement,
894 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200895{
896 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
897
898 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
899 return false;
900
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200901 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200902 return false;
903
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200904 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200905 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200906
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200907 cur_flags |= (proposed_placement & man->available_caching);
908
909 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200910 return true;
911}
912
913/**
914 * Creates space for memory region @mem according to its type.
915 *
916 * This function first searches for free space in compatible memory types in
917 * the priority order defined by the driver. If free space isn't found, then
918 * ttm_bo_mem_force_space is attempted in priority order to evict and find
919 * space.
920 */
921int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100922 struct ttm_placement *placement,
923 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000924 bool interruptible, bool no_wait_reserve,
925 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926{
927 struct ttm_bo_device *bdev = bo->bdev;
928 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929 uint32_t mem_type = TTM_PL_SYSTEM;
930 uint32_t cur_flags = 0;
931 bool type_found = false;
932 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100933 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100934 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200935
936 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000937 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100938 ret = ttm_mem_type_from_flags(placement->placement[i],
939 &mem_type);
940 if (ret)
941 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200942 man = &bdev->man[mem_type];
943
944 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100945 bo->type == ttm_bo_type_user,
946 mem_type,
947 placement->placement[i],
948 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200949
950 if (!type_ok)
951 continue;
952
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200953 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
954 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100955 /*
956 * Use the access and other non-mapping-related flag bits from
957 * the memory placement flags to the current flags
958 */
959 ttm_flag_masked(&cur_flags, placement->placement[i],
960 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200961
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200962 if (mem_type == TTM_PL_SYSTEM)
963 break;
964
965 if (man->has_type && man->use_type) {
966 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000967 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100968 if (unlikely(ret))
969 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000971 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200972 break;
973 }
974
Ben Skeggsd961db72010-08-05 10:48:18 +1000975 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976 mem->mem_type = mem_type;
977 mem->placement = cur_flags;
978 return 0;
979 }
980
981 if (!type_found)
982 return -EINVAL;
983
Dave Airlieb6637522009-12-14 14:51:35 +1000984 for (i = 0; i < placement->num_busy_placement; ++i) {
985 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100986 &mem_type);
987 if (ret)
988 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200990 if (!man->has_type)
991 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200992 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100993 bo->type == ttm_bo_type_user,
994 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000995 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100996 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997 continue;
998
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200999 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1000 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001001 /*
1002 * Use the access and other non-mapping-related flag bits from
1003 * the memory placement flags to the current flags
1004 */
Dave Airlieb6637522009-12-14 14:51:35 +10001005 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001006 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001007
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001008
1009 if (mem_type == TTM_PL_SYSTEM) {
1010 mem->mem_type = mem_type;
1011 mem->placement = cur_flags;
1012 mem->mm_node = NULL;
1013 return 0;
1014 }
1015
Jerome Glisseca262a9992009-12-08 15:33:32 +01001016 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001017 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001018 if (ret == 0 && mem->mm_node) {
1019 mem->placement = cur_flags;
1020 return 0;
1021 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001022 if (ret == -ERESTARTSYS)
1023 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001024 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001025 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001026 return ret;
1027}
1028EXPORT_SYMBOL(ttm_bo_mem_space);
1029
1030int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1031{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001032 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1033 return -EBUSY;
1034
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001035 return wait_event_interruptible(bo->event_queue,
1036 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001038EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001039
1040int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001041 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001042 bool interruptible, bool no_wait_reserve,
1043 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001045 int ret = 0;
1046 struct ttm_mem_reg mem;
1047
1048 BUG_ON(!atomic_read(&bo->reserved));
1049
1050 /*
1051 * FIXME: It's possible to pipeline buffer moves.
1052 * Have the driver move function wait for idle when necessary,
1053 * instead of doing it here.
1054 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001055 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001056 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001057 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001058 if (ret)
1059 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060 mem.num_pages = bo->num_pages;
1061 mem.size = mem.num_pages << PAGE_SHIFT;
1062 mem.page_alignment = bo->mem.page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001063 mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001064 /*
1065 * Determine where to move the buffer.
1066 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001067 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001068 if (ret)
1069 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001070 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001071out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001072 if (ret && mem.mm_node)
1073 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074 return ret;
1075}
1076
Jerome Glisseca262a9992009-12-08 15:33:32 +01001077static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001078 struct ttm_mem_reg *mem)
1079{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001080 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001081
Ben Skeggsd961db72010-08-05 10:48:18 +10001082 if (mem->mm_node && placement->lpfn != 0 &&
1083 (mem->start < placement->fpfn ||
1084 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001085 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086
Jerome Glisseca262a9992009-12-08 15:33:32 +01001087 for (i = 0; i < placement->num_placement; i++) {
1088 if ((placement->placement[i] & mem->placement &
1089 TTM_PL_MASK_CACHING) &&
1090 (placement->placement[i] & mem->placement &
1091 TTM_PL_MASK_MEM))
1092 return i;
1093 }
1094 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001095}
1096
Jerome Glisse09855ac2009-12-10 17:16:27 +01001097int ttm_bo_validate(struct ttm_buffer_object *bo,
1098 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001099 bool interruptible, bool no_wait_reserve,
1100 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101{
1102 int ret;
1103
1104 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001105 /* Check that range is valid */
1106 if (placement->lpfn || placement->fpfn)
1107 if (placement->fpfn > placement->lpfn ||
1108 (placement->lpfn - placement->fpfn) < bo->num_pages)
1109 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110 /*
1111 * Check whether we need to move buffer.
1112 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001113 ret = ttm_bo_mem_compat(placement, &bo->mem);
1114 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001115 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001116 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001118 } else {
1119 /*
1120 * Use the access and other non-mapping-related flag bits from
1121 * the compatible memory placement flags to the active flags
1122 */
1123 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1124 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001126 /*
1127 * We might need to add a TTM.
1128 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001129 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1130 ret = ttm_bo_add_ttm(bo, true);
1131 if (ret)
1132 return ret;
1133 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001134 return 0;
1135}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001136EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001137
Jerome Glisse09855ac2009-12-10 17:16:27 +01001138int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1139 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001140{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001141 BUG_ON((placement->fpfn || placement->lpfn) &&
1142 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 return 0;
1145}
1146
Jerome Glisse09855ac2009-12-10 17:16:27 +01001147int ttm_bo_init(struct ttm_bo_device *bdev,
1148 struct ttm_buffer_object *bo,
1149 unsigned long size,
1150 enum ttm_bo_type type,
1151 struct ttm_placement *placement,
1152 uint32_t page_alignment,
1153 unsigned long buffer_start,
1154 bool interruptible,
1155 struct file *persistant_swap_storage,
1156 size_t acc_size,
1157 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001159 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001160 unsigned long num_pages;
1161
1162 size += buffer_start & ~PAGE_MASK;
1163 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1164 if (num_pages == 0) {
1165 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001166 if (destroy)
1167 (*destroy)(bo);
1168 else
1169 kfree(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001170 return -EINVAL;
1171 }
1172 bo->destroy = destroy;
1173
1174 spin_lock_init(&bo->lock);
1175 kref_init(&bo->kref);
1176 kref_init(&bo->list_kref);
1177 atomic_set(&bo->cpu_writers, 0);
1178 atomic_set(&bo->reserved, 1);
1179 init_waitqueue_head(&bo->event_queue);
1180 INIT_LIST_HEAD(&bo->lru);
1181 INIT_LIST_HEAD(&bo->ddestroy);
1182 INIT_LIST_HEAD(&bo->swap);
1183 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001184 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185 bo->type = type;
1186 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001187 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001188 bo->mem.mem_type = TTM_PL_SYSTEM;
1189 bo->mem.num_pages = bo->num_pages;
1190 bo->mem.mm_node = NULL;
1191 bo->mem.page_alignment = page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001192 bo->mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001193 bo->buffer_start = buffer_start & PAGE_MASK;
1194 bo->priv_flags = 0;
1195 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1196 bo->seq_valid = false;
1197 bo->persistant_swap_storage = persistant_swap_storage;
1198 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001199 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200
Jerome Glisse09855ac2009-12-10 17:16:27 +01001201 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001202 if (unlikely(ret != 0))
1203 goto out_err;
1204
1205 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001206 * For ttm_bo_type_device buffers, allocate
1207 * address space from the device.
1208 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209 if (bo->type == ttm_bo_type_device) {
1210 ret = ttm_bo_setup_vm(bo);
1211 if (ret)
1212 goto out_err;
1213 }
1214
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001215 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001216 if (ret)
1217 goto out_err;
1218
1219 ttm_bo_unreserve(bo);
1220 return 0;
1221
1222out_err:
1223 ttm_bo_unreserve(bo);
1224 ttm_bo_unref(&bo);
1225
1226 return ret;
1227}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001228EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001229
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001230static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231 unsigned long num_pages)
1232{
1233 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1234 PAGE_MASK;
1235
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001236 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001237}
1238
Jerome Glisse09855ac2009-12-10 17:16:27 +01001239int ttm_bo_create(struct ttm_bo_device *bdev,
1240 unsigned long size,
1241 enum ttm_bo_type type,
1242 struct ttm_placement *placement,
1243 uint32_t page_alignment,
1244 unsigned long buffer_start,
1245 bool interruptible,
1246 struct file *persistant_swap_storage,
1247 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248{
1249 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001250 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001251 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001252
1253 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001254 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001255 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256 if (unlikely(ret != 0))
1257 return ret;
1258
1259 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1260
1261 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001262 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001263 return -ENOMEM;
1264 }
1265
Jerome Glisse09855ac2009-12-10 17:16:27 +01001266 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1267 buffer_start, interruptible,
1268 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269 if (likely(ret == 0))
1270 *p_bo = bo;
1271
1272 return ret;
1273}
1274
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001275static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001276 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001277{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001278 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001279 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001280 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001281
1282 /*
1283 * Can't use standard list traversal since we're unlocking.
1284 */
1285
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001286 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001287 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001288 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001289 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001290 if (ret) {
1291 if (allow_errors) {
1292 return ret;
1293 } else {
1294 printk(KERN_ERR TTM_PFX
1295 "Cleanup eviction failed\n");
1296 }
1297 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001298 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001300 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001301 return 0;
1302}
1303
1304int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1305{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001306 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001307 int ret = -EINVAL;
1308
1309 if (mem_type >= TTM_NUM_MEM_TYPES) {
1310 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1311 return ret;
1312 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001313 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001314
1315 if (!man->has_type) {
1316 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1317 "memory manager type %u\n", mem_type);
1318 return ret;
1319 }
1320
1321 man->use_type = false;
1322 man->has_type = false;
1323
1324 ret = 0;
1325 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001326 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001327
Ben Skeggsd961db72010-08-05 10:48:18 +10001328 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001329 }
1330
1331 return ret;
1332}
1333EXPORT_SYMBOL(ttm_bo_clean_mm);
1334
1335int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1336{
1337 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1338
1339 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1340 printk(KERN_ERR TTM_PFX
1341 "Illegal memory manager memory type %u.\n",
1342 mem_type);
1343 return -EINVAL;
1344 }
1345
1346 if (!man->has_type) {
1347 printk(KERN_ERR TTM_PFX
1348 "Memory type %u has not been initialized.\n",
1349 mem_type);
1350 return 0;
1351 }
1352
Jerome Glisseca262a9992009-12-08 15:33:32 +01001353 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001354}
1355EXPORT_SYMBOL(ttm_bo_evict_mm);
1356
1357int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001358 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001359{
1360 int ret = -EINVAL;
1361 struct ttm_mem_type_manager *man;
1362
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001363 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001364 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001365 BUG_ON(man->has_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001366
1367 ret = bdev->driver->init_mem_type(bdev, type, man);
1368 if (ret)
1369 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001370 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001371
1372 ret = 0;
1373 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001374 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001375 if (ret)
1376 return ret;
1377 }
1378 man->has_type = true;
1379 man->use_type = true;
1380 man->size = p_size;
1381
1382 INIT_LIST_HEAD(&man->lru);
1383
1384 return 0;
1385}
1386EXPORT_SYMBOL(ttm_bo_init_mm);
1387
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001388static void ttm_bo_global_kobj_release(struct kobject *kobj)
1389{
1390 struct ttm_bo_global *glob =
1391 container_of(kobj, struct ttm_bo_global, kobj);
1392
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001393 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1394 __free_page(glob->dummy_read_page);
1395 kfree(glob);
1396}
1397
Dave Airlieba4420c2010-03-09 10:56:52 +10001398void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001399{
1400 struct ttm_bo_global *glob = ref->object;
1401
1402 kobject_del(&glob->kobj);
1403 kobject_put(&glob->kobj);
1404}
1405EXPORT_SYMBOL(ttm_bo_global_release);
1406
Dave Airlieba4420c2010-03-09 10:56:52 +10001407int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001408{
1409 struct ttm_bo_global_ref *bo_ref =
1410 container_of(ref, struct ttm_bo_global_ref, ref);
1411 struct ttm_bo_global *glob = ref->object;
1412 int ret;
1413
1414 mutex_init(&glob->device_list_mutex);
1415 spin_lock_init(&glob->lru_lock);
1416 glob->mem_glob = bo_ref->mem_glob;
1417 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1418
1419 if (unlikely(glob->dummy_read_page == NULL)) {
1420 ret = -ENOMEM;
1421 goto out_no_drp;
1422 }
1423
1424 INIT_LIST_HEAD(&glob->swap_lru);
1425 INIT_LIST_HEAD(&glob->device_list);
1426
1427 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1428 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1429 if (unlikely(ret != 0)) {
1430 printk(KERN_ERR TTM_PFX
1431 "Could not register buffer object swapout.\n");
1432 goto out_no_shrink;
1433 }
1434
1435 glob->ttm_bo_extra_size =
1436 ttm_round_pot(sizeof(struct ttm_tt)) +
1437 ttm_round_pot(sizeof(struct ttm_backend));
1438
1439 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1440 ttm_round_pot(sizeof(struct ttm_buffer_object));
1441
1442 atomic_set(&glob->bo_count, 0);
1443
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001444 ret = kobject_init_and_add(
1445 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001446 if (unlikely(ret != 0))
1447 kobject_put(&glob->kobj);
1448 return ret;
1449out_no_shrink:
1450 __free_page(glob->dummy_read_page);
1451out_no_drp:
1452 kfree(glob);
1453 return ret;
1454}
1455EXPORT_SYMBOL(ttm_bo_global_init);
1456
1457
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001458int ttm_bo_device_release(struct ttm_bo_device *bdev)
1459{
1460 int ret = 0;
1461 unsigned i = TTM_NUM_MEM_TYPES;
1462 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001463 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001464
1465 while (i--) {
1466 man = &bdev->man[i];
1467 if (man->has_type) {
1468 man->use_type = false;
1469 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1470 ret = -EBUSY;
1471 printk(KERN_ERR TTM_PFX
1472 "DRM memory manager type %d "
1473 "is not clean.\n", i);
1474 }
1475 man->has_type = false;
1476 }
1477 }
1478
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001479 mutex_lock(&glob->device_list_mutex);
1480 list_del(&bdev->device_list);
1481 mutex_unlock(&glob->device_list_mutex);
1482
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001483 if (!cancel_delayed_work(&bdev->wq))
1484 flush_scheduled_work();
1485
1486 while (ttm_bo_delayed_delete(bdev, true))
1487 ;
1488
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001489 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001490 if (list_empty(&bdev->ddestroy))
1491 TTM_DEBUG("Delayed destroy list was clean\n");
1492
1493 if (list_empty(&bdev->man[0].lru))
1494 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001495 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001496
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1498 write_lock(&bdev->vm_lock);
1499 drm_mm_takedown(&bdev->addr_space_mm);
1500 write_unlock(&bdev->vm_lock);
1501
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001502 return ret;
1503}
1504EXPORT_SYMBOL(ttm_bo_device_release);
1505
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001506int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001507 struct ttm_bo_global *glob,
1508 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001509 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001510 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001511{
1512 int ret = -EINVAL;
1513
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001514 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001515 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001516
1517 memset(bdev->man, 0, sizeof(bdev->man));
1518
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001519 /*
1520 * Initialize the system memory buffer type.
1521 * Other types need to be driver / IOCTL initialized.
1522 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001523 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001524 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001525 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526
1527 bdev->addr_space_rb = RB_ROOT;
1528 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1529 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001530 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531
1532 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1533 bdev->nice_mode = true;
1534 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001535 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001536 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001537 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001539 mutex_lock(&glob->device_list_mutex);
1540 list_add_tail(&bdev->device_list, &glob->device_list);
1541 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001542
1543 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001544out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001545 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001546out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001547 return ret;
1548}
1549EXPORT_SYMBOL(ttm_bo_device_init);
1550
1551/*
1552 * buffer object vm functions.
1553 */
1554
1555bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1556{
1557 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1558
1559 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1560 if (mem->mem_type == TTM_PL_SYSTEM)
1561 return false;
1562
1563 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1564 return false;
1565
1566 if (mem->placement & TTM_PL_FLAG_CACHED)
1567 return false;
1568 }
1569 return true;
1570}
1571
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001572void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1573{
1574 struct ttm_bo_device *bdev = bo->bdev;
1575 loff_t offset = (loff_t) bo->addr_space_offset;
1576 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1577
1578 if (!bdev->dev_mapping)
1579 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001580 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Jerome Glisse82c5da62010-04-09 14:39:23 +02001581 ttm_mem_io_free(bdev, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001582}
Dave Airliee024e112009-06-24 09:48:08 +10001583EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001584
1585static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1586{
1587 struct ttm_bo_device *bdev = bo->bdev;
1588 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1589 struct rb_node *parent = NULL;
1590 struct ttm_buffer_object *cur_bo;
1591 unsigned long offset = bo->vm_node->start;
1592 unsigned long cur_offset;
1593
1594 while (*cur) {
1595 parent = *cur;
1596 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1597 cur_offset = cur_bo->vm_node->start;
1598 if (offset < cur_offset)
1599 cur = &parent->rb_left;
1600 else if (offset > cur_offset)
1601 cur = &parent->rb_right;
1602 else
1603 BUG();
1604 }
1605
1606 rb_link_node(&bo->vm_rb, parent, cur);
1607 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1608}
1609
1610/**
1611 * ttm_bo_setup_vm:
1612 *
1613 * @bo: the buffer to allocate address space for
1614 *
1615 * Allocate address space in the drm device so that applications
1616 * can mmap the buffer and access the contents. This only
1617 * applies to ttm_bo_type_device objects as others are not
1618 * placed in the drm device address space.
1619 */
1620
1621static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1622{
1623 struct ttm_bo_device *bdev = bo->bdev;
1624 int ret;
1625
1626retry_pre_get:
1627 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1628 if (unlikely(ret != 0))
1629 return ret;
1630
1631 write_lock(&bdev->vm_lock);
1632 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1633 bo->mem.num_pages, 0, 0);
1634
1635 if (unlikely(bo->vm_node == NULL)) {
1636 ret = -ENOMEM;
1637 goto out_unlock;
1638 }
1639
1640 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1641 bo->mem.num_pages, 0);
1642
1643 if (unlikely(bo->vm_node == NULL)) {
1644 write_unlock(&bdev->vm_lock);
1645 goto retry_pre_get;
1646 }
1647
1648 ttm_bo_vm_insert_rb(bo);
1649 write_unlock(&bdev->vm_lock);
1650 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1651
1652 return 0;
1653out_unlock:
1654 write_unlock(&bdev->vm_lock);
1655 return ret;
1656}
1657
1658int ttm_bo_wait(struct ttm_buffer_object *bo,
1659 bool lazy, bool interruptible, bool no_wait)
1660{
1661 struct ttm_bo_driver *driver = bo->bdev->driver;
1662 void *sync_obj;
1663 void *sync_obj_arg;
1664 int ret = 0;
1665
1666 if (likely(bo->sync_obj == NULL))
1667 return 0;
1668
1669 while (bo->sync_obj) {
1670
1671 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1672 void *tmp_obj = bo->sync_obj;
1673 bo->sync_obj = NULL;
1674 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1675 spin_unlock(&bo->lock);
1676 driver->sync_obj_unref(&tmp_obj);
1677 spin_lock(&bo->lock);
1678 continue;
1679 }
1680
1681 if (no_wait)
1682 return -EBUSY;
1683
1684 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1685 sync_obj_arg = bo->sync_obj_arg;
1686 spin_unlock(&bo->lock);
1687 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1688 lazy, interruptible);
1689 if (unlikely(ret != 0)) {
1690 driver->sync_obj_unref(&sync_obj);
1691 spin_lock(&bo->lock);
1692 return ret;
1693 }
1694 spin_lock(&bo->lock);
1695 if (likely(bo->sync_obj == sync_obj &&
1696 bo->sync_obj_arg == sync_obj_arg)) {
1697 void *tmp_obj = bo->sync_obj;
1698 bo->sync_obj = NULL;
1699 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1700 &bo->priv_flags);
1701 spin_unlock(&bo->lock);
1702 driver->sync_obj_unref(&sync_obj);
1703 driver->sync_obj_unref(&tmp_obj);
1704 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001705 } else {
1706 spin_unlock(&bo->lock);
1707 driver->sync_obj_unref(&sync_obj);
1708 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001709 }
1710 }
1711 return 0;
1712}
1713EXPORT_SYMBOL(ttm_bo_wait);
1714
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001715int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1716{
1717 int ret = 0;
1718
1719 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001720 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001721 */
1722
1723 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1724 if (unlikely(ret != 0))
1725 return ret;
1726 spin_lock(&bo->lock);
1727 ret = ttm_bo_wait(bo, false, true, no_wait);
1728 spin_unlock(&bo->lock);
1729 if (likely(ret == 0))
1730 atomic_inc(&bo->cpu_writers);
1731 ttm_bo_unreserve(bo);
1732 return ret;
1733}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001734EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001735
1736void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1737{
1738 if (atomic_dec_and_test(&bo->cpu_writers))
1739 wake_up_all(&bo->event_queue);
1740}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001741EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001742
1743/**
1744 * A buffer object shrink method that tries to swap out the first
1745 * buffer object on the bo_global::swap_lru list.
1746 */
1747
1748static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1749{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001750 struct ttm_bo_global *glob =
1751 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001752 struct ttm_buffer_object *bo;
1753 int ret = -EBUSY;
1754 int put_count;
1755 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1756
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001757 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001758 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001759 if (unlikely(list_empty(&glob->swap_lru))) {
1760 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001761 return -EBUSY;
1762 }
1763
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001764 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001765 struct ttm_buffer_object, swap);
1766 kref_get(&bo->list_kref);
1767
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001768 if (!list_empty(&bo->ddestroy)) {
1769 spin_unlock(&glob->lru_lock);
1770 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1771 kref_put(&bo->list_kref, ttm_bo_release_list);
1772 continue;
1773 }
1774
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001775 /**
1776 * Reserve buffer. Since we unlock while sleeping, we need
1777 * to re-check that nobody removed us from the swap-list while
1778 * we slept.
1779 */
1780
1781 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1782 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001783 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001784 ttm_bo_wait_unreserved(bo, false);
1785 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001786 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001787 }
1788 }
1789
1790 BUG_ON(ret != 0);
1791 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001792 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001793
Dave Airlied6ea8882010-11-22 13:24:40 +10001794 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001795
1796 /**
1797 * Wait for GPU, then move to system cached.
1798 */
1799
1800 spin_lock(&bo->lock);
1801 ret = ttm_bo_wait(bo, false, false, false);
1802 spin_unlock(&bo->lock);
1803
1804 if (unlikely(ret != 0))
1805 goto out;
1806
1807 if ((bo->mem.placement & swap_placement) != swap_placement) {
1808 struct ttm_mem_reg evict_mem;
1809
1810 evict_mem = bo->mem;
1811 evict_mem.mm_node = NULL;
1812 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1813 evict_mem.mem_type = TTM_PL_SYSTEM;
1814
1815 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001816 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001817 if (unlikely(ret != 0))
1818 goto out;
1819 }
1820
1821 ttm_bo_unmap_virtual(bo);
1822
1823 /**
1824 * Swap out. Buffer will be swapped in again as soon as
1825 * anyone tries to access a ttm page.
1826 */
1827
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001828 if (bo->bdev->driver->swap_notify)
1829 bo->bdev->driver->swap_notify(bo);
1830
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001831 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1832out:
1833
1834 /**
1835 *
1836 * Unreserve without putting on LRU to avoid swapping out an
1837 * already swapped buffer.
1838 */
1839
1840 atomic_set(&bo->reserved, 0);
1841 wake_up_all(&bo->event_queue);
1842 kref_put(&bo->list_kref, ttm_bo_release_list);
1843 return ret;
1844}
1845
1846void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1847{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001848 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001849 ;
1850}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001851EXPORT_SYMBOL(ttm_bo_swapout_all);