blob: 934a96a78540c57a525d722f25480012be3449f8 [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
172static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
173{
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
194/**
195 * Call with the lru_lock held.
196 */
197
198static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
199{
200 int put_count = 0;
201
202 if (!list_empty(&bo->swap)) {
203 list_del_init(&bo->swap);
204 ++put_count;
205 }
206 if (!list_empty(&bo->lru)) {
207 list_del_init(&bo->lru);
208 ++put_count;
209 }
210
211 /*
212 * TODO: Add a driver hook to delete from
213 * driver-specific LRU's here.
214 */
215
216 return put_count;
217}
218
219int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
220 bool interruptible,
221 bool no_wait, bool use_sequence, uint32_t sequence)
222{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200223 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200224 int ret;
225
226 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100227 /**
228 * Deadlock avoidance for multi-bo reserving.
229 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200230 if (use_sequence && bo->seq_valid &&
231 (sequence - bo->val_seq < (1 << 31))) {
232 return -EAGAIN;
233 }
234
235 if (no_wait)
236 return -EBUSY;
237
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200238 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200239 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200240 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200241
242 if (unlikely(ret))
243 return ret;
244 }
245
246 if (use_sequence) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100247 /**
248 * Wake up waiters that may need to recheck for deadlock,
249 * if we decreased the sequence number.
250 */
251 if (unlikely((bo->val_seq - sequence < (1 << 31))
252 || !bo->seq_valid))
253 wake_up_all(&bo->event_queue);
254
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200255 bo->val_seq = sequence;
256 bo->seq_valid = true;
257 } else {
258 bo->seq_valid = false;
259 }
260
261 return 0;
262}
263EXPORT_SYMBOL(ttm_bo_reserve);
264
265static void ttm_bo_ref_bug(struct kref *list_kref)
266{
267 BUG();
268}
269
270int ttm_bo_reserve(struct ttm_buffer_object *bo,
271 bool interruptible,
272 bool no_wait, bool use_sequence, uint32_t sequence)
273{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200274 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200275 int put_count = 0;
276 int ret;
277
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200278 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200279 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
280 sequence);
281 if (likely(ret == 0))
282 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200283 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200284
285 while (put_count--)
286 kref_put(&bo->list_kref, ttm_bo_ref_bug);
287
288 return ret;
289}
290
291void ttm_bo_unreserve(struct ttm_buffer_object *bo)
292{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200293 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200294
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200295 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200296 ttm_bo_add_to_lru(bo);
297 atomic_set(&bo->reserved, 0);
298 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200299 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200300}
301EXPORT_SYMBOL(ttm_bo_unreserve);
302
303/*
304 * Call bo->mutex locked.
305 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200306static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
307{
308 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200309 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200310 int ret = 0;
311 uint32_t page_flags = 0;
312
313 TTM_ASSERT_LOCKED(&bo->mutex);
314 bo->ttm = NULL;
315
Dave Airliead49f502009-07-10 22:36:26 +1000316 if (bdev->need_dma32)
317 page_flags |= TTM_PAGE_FLAG_DMA32;
318
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200319 switch (bo->type) {
320 case ttm_bo_type_device:
321 if (zero_alloc)
322 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
323 case ttm_bo_type_kernel:
324 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200325 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326 if (unlikely(bo->ttm == NULL))
327 ret = -ENOMEM;
328 break;
329 case ttm_bo_type_user:
330 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
331 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200332 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000333 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200334 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000335 break;
336 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200337
338 ret = ttm_tt_set_user(bo->ttm, current,
339 bo->buffer_start, bo->num_pages);
340 if (unlikely(ret != 0))
341 ttm_tt_destroy(bo->ttm);
342 break;
343 default:
344 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
345 ret = -EINVAL;
346 break;
347 }
348
349 return ret;
350}
351
352static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
353 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000354 bool evict, bool interruptible,
355 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200356{
357 struct ttm_bo_device *bdev = bo->bdev;
358 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
359 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
360 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
361 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
362 int ret = 0;
363
364 if (old_is_pci || new_is_pci ||
365 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
366 ttm_bo_unmap_virtual(bo);
367
368 /*
369 * Create and bind a ttm if required.
370 */
371
372 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
373 ret = ttm_bo_add_ttm(bo, false);
374 if (ret)
375 goto out_err;
376
377 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
378 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200379 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200380
381 if (mem->mem_type != TTM_PL_SYSTEM) {
382 ret = ttm_tt_bind(bo->ttm, mem);
383 if (ret)
384 goto out_err;
385 }
386
387 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100388 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200389 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390 goto moved;
391 }
392
393 }
394
Dave Airliee024e112009-06-24 09:48:08 +1000395 if (bdev->driver->move_notify)
396 bdev->driver->move_notify(bo, mem);
397
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200398 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
399 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000400 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200401 else if (bdev->driver->move)
402 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000403 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200404 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000405 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200406
407 if (ret)
408 goto out_err;
409
410moved:
411 if (bo->evicted) {
412 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
413 if (ret)
414 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
415 bo->evicted = false;
416 }
417
418 if (bo->mem.mm_node) {
419 spin_lock(&bo->lock);
Ben Skeggsd961db72010-08-05 10:48:18 +1000420 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200421 bdev->man[bo->mem.mem_type].gpu_offset;
422 bo->cur_placement = bo->mem.placement;
423 spin_unlock(&bo->lock);
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100424 } else
425 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200426
427 return 0;
428
429out_err:
430 new_man = &bdev->man[bo->mem.mem_type];
431 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
432 ttm_tt_unbind(bo->ttm);
433 ttm_tt_destroy(bo->ttm);
434 bo->ttm = NULL;
435 }
436
437 return ret;
438}
439
440/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200441 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200442 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200443 * This is the place to put in driver specific hooks to release
444 * driver private resources.
445 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200446 */
447
448static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
449{
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200450 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200451 ttm_tt_unbind(bo->ttm);
452 ttm_tt_destroy(bo->ttm);
453 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200454 }
455
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200456 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200457
458 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200459
460 /*
461 * Make processes trying to reserve really pick it up.
462 */
463 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200464 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200465}
466
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200467static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200468{
469 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200470 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200471 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000472 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200473 void *sync_obj_arg;
474 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200475 int ret;
476
477 spin_lock(&bo->lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200478 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200479 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200480
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200481 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100482
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200483 /**
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200484 * Lock inversion between bo::reserve and bo::lock here,
485 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200486 */
487
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200488 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200489
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200490 if (unlikely(ret == -EBUSY))
491 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200492
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200493 spin_unlock(&bo->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200494 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200495
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200496 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200497 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200498
499 while (put_count--)
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100500 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200501
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200502 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200503 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200504 spin_lock(&glob->lru_lock);
505 }
506queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200507 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000508 if (bo->sync_obj)
509 sync_obj = driver->sync_obj_ref(bo->sync_obj);
510 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200511
512 kref_get(&bo->list_kref);
513 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
514 spin_unlock(&glob->lru_lock);
515 spin_unlock(&bo->lock);
516
Thomas Hellstromaa123262010-11-02 13:21:47 +0000517 if (sync_obj) {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200518 driver->sync_obj_flush(sync_obj, sync_obj_arg);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000519 driver->sync_obj_unref(&sync_obj);
520 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200521 schedule_delayed_work(&bdev->wq,
522 ((HZ / 100) < 1) ? 1 : HZ / 100);
523}
524
525/**
526 * function ttm_bo_cleanup_refs
527 * If bo idle, remove from delayed- and lru lists, and unref.
528 * If not idle, do nothing.
529 *
530 * @interruptible Any sleeps should occur interruptibly.
531 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
532 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
533 */
534
535static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
536 bool interruptible,
537 bool no_wait_reserve,
538 bool no_wait_gpu)
539{
540 struct ttm_bo_global *glob = bo->glob;
541 int put_count;
542 int ret = 0;
543
544retry:
545 spin_lock(&bo->lock);
546 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
547 spin_unlock(&bo->lock);
548
549 if (unlikely(ret != 0))
550 return ret;
551
552 spin_lock(&glob->lru_lock);
553 ret = ttm_bo_reserve_locked(bo, interruptible,
554 no_wait_reserve, false, 0);
555
556 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200557 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200558 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200559 }
560
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200561 /**
562 * We can re-check for sync object without taking
563 * the bo::lock since setting the sync object requires
564 * also bo::reserved. A busy object at this point may
565 * be caused by another thread recently starting an accelerated
566 * eviction.
567 */
568
569 if (unlikely(bo->sync_obj)) {
570 atomic_set(&bo->reserved, 0);
571 wake_up_all(&bo->event_queue);
572 spin_unlock(&glob->lru_lock);
573 goto retry;
574 }
575
576 put_count = ttm_bo_del_from_lru(bo);
577 list_del_init(&bo->ddestroy);
578 ++put_count;
579
580 spin_unlock(&glob->lru_lock);
581 ttm_bo_cleanup_memtype_use(bo);
582
583 while (put_count--)
584 kref_put(&bo->list_kref, ttm_bo_ref_bug);
585
586 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200587}
588
589/**
590 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
591 * encountered buffers.
592 */
593
594static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
595{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200596 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100597 struct ttm_buffer_object *entry = NULL;
598 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200599
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200600 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100601 if (list_empty(&bdev->ddestroy))
602 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200603
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100604 entry = list_first_entry(&bdev->ddestroy,
605 struct ttm_buffer_object, ddestroy);
606 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200607
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100608 for (;;) {
609 struct ttm_buffer_object *nentry = NULL;
610
611 if (entry->ddestroy.next != &bdev->ddestroy) {
612 nentry = list_first_entry(&entry->ddestroy,
613 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200614 kref_get(&nentry->list_kref);
615 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200617 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200618 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
619 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200620 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100621 entry = nentry;
622
623 if (ret || !entry)
624 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200625
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200626 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100627 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200628 break;
629 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200630
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100631out_unlock:
632 spin_unlock(&glob->lru_lock);
633out:
634 if (entry)
635 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200636 return ret;
637}
638
639static void ttm_bo_delayed_workqueue(struct work_struct *work)
640{
641 struct ttm_bo_device *bdev =
642 container_of(work, struct ttm_bo_device, wq.work);
643
644 if (ttm_bo_delayed_delete(bdev, false)) {
645 schedule_delayed_work(&bdev->wq,
646 ((HZ / 100) < 1) ? 1 : HZ / 100);
647 }
648}
649
650static void ttm_bo_release(struct kref *kref)
651{
652 struct ttm_buffer_object *bo =
653 container_of(kref, struct ttm_buffer_object, kref);
654 struct ttm_bo_device *bdev = bo->bdev;
655
656 if (likely(bo->vm_node != NULL)) {
657 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
658 drm_mm_put_block(bo->vm_node);
659 bo->vm_node = NULL;
660 }
661 write_unlock(&bdev->vm_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200662 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200663 kref_put(&bo->list_kref, ttm_bo_release_list);
664 write_lock(&bdev->vm_lock);
665}
666
667void ttm_bo_unref(struct ttm_buffer_object **p_bo)
668{
669 struct ttm_buffer_object *bo = *p_bo;
670 struct ttm_bo_device *bdev = bo->bdev;
671
672 *p_bo = NULL;
673 write_lock(&bdev->vm_lock);
674 kref_put(&bo->kref, ttm_bo_release);
675 write_unlock(&bdev->vm_lock);
676}
677EXPORT_SYMBOL(ttm_bo_unref);
678
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400679int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
680{
681 return cancel_delayed_work_sync(&bdev->wq);
682}
683EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
684
685void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
686{
687 if (resched)
688 schedule_delayed_work(&bdev->wq,
689 ((HZ / 100) < 1) ? 1 : HZ / 100);
690}
691EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
692
Jerome Glisseca262a9992009-12-08 15:33:32 +0100693static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000694 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200695{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200696 struct ttm_bo_device *bdev = bo->bdev;
697 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100698 struct ttm_placement placement;
699 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200700
701 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000702 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200703 spin_unlock(&bo->lock);
704
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200705 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100706 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200707 printk(KERN_ERR TTM_PFX
708 "Failed to expire sync object before "
709 "buffer eviction.\n");
710 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200711 goto out;
712 }
713
714 BUG_ON(!atomic_read(&bo->reserved));
715
716 evict_mem = bo->mem;
717 evict_mem.mm_node = NULL;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200718 evict_mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200719
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100720 placement.fpfn = 0;
721 placement.lpfn = 0;
722 placement.num_placement = 0;
723 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100724 bdev->driver->evict_flags(bo, &placement);
725 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000726 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200727 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100728 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200729 printk(KERN_ERR TTM_PFX
730 "Failed to find memory space for "
731 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100732 ttm_bo_mem_space_debug(bo, &placement);
733 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200734 goto out;
735 }
736
737 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000738 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200739 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100740 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200741 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000742 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200743 goto out;
744 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200745 bo->evicted = true;
746out:
747 return ret;
748}
749
Jerome Glisseca262a9992009-12-08 15:33:32 +0100750static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
751 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000752 bool interruptible, bool no_wait_reserve,
753 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100754{
755 struct ttm_bo_global *glob = bdev->glob;
756 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
757 struct ttm_buffer_object *bo;
758 int ret, put_count = 0;
759
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100760retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100761 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100762 if (list_empty(&man->lru)) {
763 spin_unlock(&glob->lru_lock);
764 return -EBUSY;
765 }
766
Jerome Glisseca262a9992009-12-08 15:33:32 +0100767 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
768 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100769
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200770 if (!list_empty(&bo->ddestroy)) {
771 spin_unlock(&glob->lru_lock);
772 ret = ttm_bo_cleanup_refs(bo, interruptible,
773 no_wait_reserve, no_wait_gpu);
774 kref_put(&bo->list_kref, ttm_bo_release_list);
775
776 if (likely(ret == 0 || ret == -ERESTARTSYS))
777 return ret;
778
779 goto retry;
780 }
781
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000782 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100783
784 if (unlikely(ret == -EBUSY)) {
785 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000786 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100787 ret = ttm_bo_wait_unreserved(bo, interruptible);
788
789 kref_put(&bo->list_kref, ttm_bo_release_list);
790
791 /**
792 * We *need* to retry after releasing the lru lock.
793 */
794
795 if (unlikely(ret != 0))
796 return ret;
797 goto retry;
798 }
799
800 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100801 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100802
803 BUG_ON(ret != 0);
804
Jerome Glisseca262a9992009-12-08 15:33:32 +0100805 while (put_count--)
806 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100807
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000808 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100809 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100810
Jerome Glisseca262a9992009-12-08 15:33:32 +0100811 kref_put(&bo->list_kref, ttm_bo_release_list);
812 return ret;
813}
814
Ben Skeggs42311ff2010-08-04 12:07:08 +1000815void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
816{
Ben Skeggsd961db72010-08-05 10:48:18 +1000817 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000818
Ben Skeggsd961db72010-08-05 10:48:18 +1000819 if (mem->mm_node)
820 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000821}
822EXPORT_SYMBOL(ttm_bo_mem_put);
823
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200824/**
825 * Repeatedly evict memory from the LRU for @mem_type until we create enough
826 * space, or we've evicted everything and there isn't enough space.
827 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100828static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
829 uint32_t mem_type,
830 struct ttm_placement *placement,
831 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000832 bool interruptible,
833 bool no_wait_reserve,
834 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200835{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100836 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200837 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200838 int ret;
839
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200840 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000841 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200842 if (unlikely(ret != 0))
843 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000844 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100845 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100846 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000847 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100848 if (unlikely(ret != 0))
849 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200850 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000851 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200852 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200853 mem->mem_type = mem_type;
854 return 0;
855}
856
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200857static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
858 uint32_t cur_placement,
859 uint32_t proposed_placement)
860{
861 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
862 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
863
864 /**
865 * Keep current caching if possible.
866 */
867
868 if ((cur_placement & caching) != 0)
869 result |= (cur_placement & caching);
870 else if ((man->default_caching & caching) != 0)
871 result |= man->default_caching;
872 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
873 result |= TTM_PL_FLAG_CACHED;
874 else if ((TTM_PL_FLAG_WC & caching) != 0)
875 result |= TTM_PL_FLAG_WC;
876 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
877 result |= TTM_PL_FLAG_UNCACHED;
878
879 return result;
880}
881
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
883 bool disallow_fixed,
884 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200885 uint32_t proposed_placement,
886 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200887{
888 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
889
890 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
891 return false;
892
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200893 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200894 return false;
895
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200896 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200897 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200899 cur_flags |= (proposed_placement & man->available_caching);
900
901 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200902 return true;
903}
904
905/**
906 * Creates space for memory region @mem according to its type.
907 *
908 * This function first searches for free space in compatible memory types in
909 * the priority order defined by the driver. If free space isn't found, then
910 * ttm_bo_mem_force_space is attempted in priority order to evict and find
911 * space.
912 */
913int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100914 struct ttm_placement *placement,
915 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000916 bool interruptible, bool no_wait_reserve,
917 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200918{
919 struct ttm_bo_device *bdev = bo->bdev;
920 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200921 uint32_t mem_type = TTM_PL_SYSTEM;
922 uint32_t cur_flags = 0;
923 bool type_found = false;
924 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100925 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100926 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200927
928 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000929 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100930 ret = ttm_mem_type_from_flags(placement->placement[i],
931 &mem_type);
932 if (ret)
933 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200934 man = &bdev->man[mem_type];
935
936 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100937 bo->type == ttm_bo_type_user,
938 mem_type,
939 placement->placement[i],
940 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200941
942 if (!type_ok)
943 continue;
944
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200945 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
946 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100947 /*
948 * Use the access and other non-mapping-related flag bits from
949 * the memory placement flags to the current flags
950 */
951 ttm_flag_masked(&cur_flags, placement->placement[i],
952 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200953
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200954 if (mem_type == TTM_PL_SYSTEM)
955 break;
956
957 if (man->has_type && man->use_type) {
958 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000959 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100960 if (unlikely(ret))
961 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200962 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000963 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964 break;
965 }
966
Ben Skeggsd961db72010-08-05 10:48:18 +1000967 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200968 mem->mem_type = mem_type;
969 mem->placement = cur_flags;
970 return 0;
971 }
972
973 if (!type_found)
974 return -EINVAL;
975
Dave Airlieb6637522009-12-14 14:51:35 +1000976 for (i = 0; i < placement->num_busy_placement; ++i) {
977 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100978 &mem_type);
979 if (ret)
980 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200981 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200982 if (!man->has_type)
983 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200984 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100985 bo->type == ttm_bo_type_user,
986 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000987 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100988 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989 continue;
990
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200991 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
992 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100993 /*
994 * Use the access and other non-mapping-related flag bits from
995 * the memory placement flags to the current flags
996 */
Dave Airlieb6637522009-12-14 14:51:35 +1000997 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100998 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200999
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001000
1001 if (mem_type == TTM_PL_SYSTEM) {
1002 mem->mem_type = mem_type;
1003 mem->placement = cur_flags;
1004 mem->mm_node = NULL;
1005 return 0;
1006 }
1007
Jerome Glisseca262a9992009-12-08 15:33:32 +01001008 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001009 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001010 if (ret == 0 && mem->mm_node) {
1011 mem->placement = cur_flags;
1012 return 0;
1013 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001014 if (ret == -ERESTARTSYS)
1015 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001016 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001017 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001018 return ret;
1019}
1020EXPORT_SYMBOL(ttm_bo_mem_space);
1021
1022int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1023{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001024 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1025 return -EBUSY;
1026
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001027 return wait_event_interruptible(bo->event_queue,
1028 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001029}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001030EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001031
1032int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001033 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001034 bool interruptible, bool no_wait_reserve,
1035 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037 int ret = 0;
1038 struct ttm_mem_reg mem;
1039
1040 BUG_ON(!atomic_read(&bo->reserved));
1041
1042 /*
1043 * FIXME: It's possible to pipeline buffer moves.
1044 * Have the driver move function wait for idle when necessary,
1045 * instead of doing it here.
1046 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001047 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001048 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050 if (ret)
1051 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052 mem.num_pages = bo->num_pages;
1053 mem.size = mem.num_pages << PAGE_SHIFT;
1054 mem.page_alignment = bo->mem.page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001055 mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001056 /*
1057 * Determine where to move the buffer.
1058 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001059 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060 if (ret)
1061 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001062 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001063out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001064 if (ret && mem.mm_node)
1065 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001066 return ret;
1067}
1068
Jerome Glisseca262a9992009-12-08 15:33:32 +01001069static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001070 struct ttm_mem_reg *mem)
1071{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001072 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001073
Ben Skeggsd961db72010-08-05 10:48:18 +10001074 if (mem->mm_node && placement->lpfn != 0 &&
1075 (mem->start < placement->fpfn ||
1076 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001077 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001078
Jerome Glisseca262a9992009-12-08 15:33:32 +01001079 for (i = 0; i < placement->num_placement; i++) {
1080 if ((placement->placement[i] & mem->placement &
1081 TTM_PL_MASK_CACHING) &&
1082 (placement->placement[i] & mem->placement &
1083 TTM_PL_MASK_MEM))
1084 return i;
1085 }
1086 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001087}
1088
Jerome Glisse09855ac2009-12-10 17:16:27 +01001089int ttm_bo_validate(struct ttm_buffer_object *bo,
1090 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001091 bool interruptible, bool no_wait_reserve,
1092 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093{
1094 int ret;
1095
1096 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001097 /* Check that range is valid */
1098 if (placement->lpfn || placement->fpfn)
1099 if (placement->fpfn > placement->lpfn ||
1100 (placement->lpfn - placement->fpfn) < bo->num_pages)
1101 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102 /*
1103 * Check whether we need to move buffer.
1104 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001105 ret = ttm_bo_mem_compat(placement, &bo->mem);
1106 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001107 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001108 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001109 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001110 } else {
1111 /*
1112 * Use the access and other non-mapping-related flag bits from
1113 * the compatible memory placement flags to the active flags
1114 */
1115 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1116 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001118 /*
1119 * We might need to add a TTM.
1120 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001121 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1122 ret = ttm_bo_add_ttm(bo, true);
1123 if (ret)
1124 return ret;
1125 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001126 return 0;
1127}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001128EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001129
Jerome Glisse09855ac2009-12-10 17:16:27 +01001130int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1131 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001132{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001133 BUG_ON((placement->fpfn || placement->lpfn) &&
1134 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001136 return 0;
1137}
1138
Jerome Glisse09855ac2009-12-10 17:16:27 +01001139int ttm_bo_init(struct ttm_bo_device *bdev,
1140 struct ttm_buffer_object *bo,
1141 unsigned long size,
1142 enum ttm_bo_type type,
1143 struct ttm_placement *placement,
1144 uint32_t page_alignment,
1145 unsigned long buffer_start,
1146 bool interruptible,
1147 struct file *persistant_swap_storage,
1148 size_t acc_size,
1149 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001150{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001151 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152 unsigned long num_pages;
1153
1154 size += buffer_start & ~PAGE_MASK;
1155 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1156 if (num_pages == 0) {
1157 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001158 if (destroy)
1159 (*destroy)(bo);
1160 else
1161 kfree(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001162 return -EINVAL;
1163 }
1164 bo->destroy = destroy;
1165
1166 spin_lock_init(&bo->lock);
1167 kref_init(&bo->kref);
1168 kref_init(&bo->list_kref);
1169 atomic_set(&bo->cpu_writers, 0);
1170 atomic_set(&bo->reserved, 1);
1171 init_waitqueue_head(&bo->event_queue);
1172 INIT_LIST_HEAD(&bo->lru);
1173 INIT_LIST_HEAD(&bo->ddestroy);
1174 INIT_LIST_HEAD(&bo->swap);
1175 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001176 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001177 bo->type = type;
1178 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001179 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001180 bo->mem.mem_type = TTM_PL_SYSTEM;
1181 bo->mem.num_pages = bo->num_pages;
1182 bo->mem.mm_node = NULL;
1183 bo->mem.page_alignment = page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001184 bo->mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185 bo->buffer_start = buffer_start & PAGE_MASK;
1186 bo->priv_flags = 0;
1187 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1188 bo->seq_valid = false;
1189 bo->persistant_swap_storage = persistant_swap_storage;
1190 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001191 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001192
Jerome Glisse09855ac2009-12-10 17:16:27 +01001193 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001194 if (unlikely(ret != 0))
1195 goto out_err;
1196
1197 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001198 * For ttm_bo_type_device buffers, allocate
1199 * address space from the device.
1200 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001201 if (bo->type == ttm_bo_type_device) {
1202 ret = ttm_bo_setup_vm(bo);
1203 if (ret)
1204 goto out_err;
1205 }
1206
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001207 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001208 if (ret)
1209 goto out_err;
1210
1211 ttm_bo_unreserve(bo);
1212 return 0;
1213
1214out_err:
1215 ttm_bo_unreserve(bo);
1216 ttm_bo_unref(&bo);
1217
1218 return ret;
1219}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001220EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001222static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223 unsigned long num_pages)
1224{
1225 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1226 PAGE_MASK;
1227
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001228 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001229}
1230
Jerome Glisse09855ac2009-12-10 17:16:27 +01001231int ttm_bo_create(struct ttm_bo_device *bdev,
1232 unsigned long size,
1233 enum ttm_bo_type type,
1234 struct ttm_placement *placement,
1235 uint32_t page_alignment,
1236 unsigned long buffer_start,
1237 bool interruptible,
1238 struct file *persistant_swap_storage,
1239 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001240{
1241 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001242 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001243 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001244
1245 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001246 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001247 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248 if (unlikely(ret != 0))
1249 return ret;
1250
1251 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1252
1253 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001254 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001255 return -ENOMEM;
1256 }
1257
Jerome Glisse09855ac2009-12-10 17:16:27 +01001258 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1259 buffer_start, interruptible,
1260 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001261 if (likely(ret == 0))
1262 *p_bo = bo;
1263
1264 return ret;
1265}
1266
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001267static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001268 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001270 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001271 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001273
1274 /*
1275 * Can't use standard list traversal since we're unlocking.
1276 */
1277
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001278 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001279 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001280 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001281 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001282 if (ret) {
1283 if (allow_errors) {
1284 return ret;
1285 } else {
1286 printk(KERN_ERR TTM_PFX
1287 "Cleanup eviction failed\n");
1288 }
1289 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001290 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001292 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001293 return 0;
1294}
1295
1296int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1297{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001298 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299 int ret = -EINVAL;
1300
1301 if (mem_type >= TTM_NUM_MEM_TYPES) {
1302 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1303 return ret;
1304 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001305 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306
1307 if (!man->has_type) {
1308 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1309 "memory manager type %u\n", mem_type);
1310 return ret;
1311 }
1312
1313 man->use_type = false;
1314 man->has_type = false;
1315
1316 ret = 0;
1317 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001318 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001319
Ben Skeggsd961db72010-08-05 10:48:18 +10001320 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001321 }
1322
1323 return ret;
1324}
1325EXPORT_SYMBOL(ttm_bo_clean_mm);
1326
1327int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1328{
1329 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1330
1331 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1332 printk(KERN_ERR TTM_PFX
1333 "Illegal memory manager memory type %u.\n",
1334 mem_type);
1335 return -EINVAL;
1336 }
1337
1338 if (!man->has_type) {
1339 printk(KERN_ERR TTM_PFX
1340 "Memory type %u has not been initialized.\n",
1341 mem_type);
1342 return 0;
1343 }
1344
Jerome Glisseca262a9992009-12-08 15:33:32 +01001345 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001346}
1347EXPORT_SYMBOL(ttm_bo_evict_mm);
1348
1349int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001350 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001351{
1352 int ret = -EINVAL;
1353 struct ttm_mem_type_manager *man;
1354
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001355 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001356 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001357 BUG_ON(man->has_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001358
1359 ret = bdev->driver->init_mem_type(bdev, type, man);
1360 if (ret)
1361 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001362 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001363
1364 ret = 0;
1365 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001366 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001367 if (ret)
1368 return ret;
1369 }
1370 man->has_type = true;
1371 man->use_type = true;
1372 man->size = p_size;
1373
1374 INIT_LIST_HEAD(&man->lru);
1375
1376 return 0;
1377}
1378EXPORT_SYMBOL(ttm_bo_init_mm);
1379
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001380static void ttm_bo_global_kobj_release(struct kobject *kobj)
1381{
1382 struct ttm_bo_global *glob =
1383 container_of(kobj, struct ttm_bo_global, kobj);
1384
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001385 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1386 __free_page(glob->dummy_read_page);
1387 kfree(glob);
1388}
1389
Dave Airlieba4420c2010-03-09 10:56:52 +10001390void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001391{
1392 struct ttm_bo_global *glob = ref->object;
1393
1394 kobject_del(&glob->kobj);
1395 kobject_put(&glob->kobj);
1396}
1397EXPORT_SYMBOL(ttm_bo_global_release);
1398
Dave Airlieba4420c2010-03-09 10:56:52 +10001399int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001400{
1401 struct ttm_bo_global_ref *bo_ref =
1402 container_of(ref, struct ttm_bo_global_ref, ref);
1403 struct ttm_bo_global *glob = ref->object;
1404 int ret;
1405
1406 mutex_init(&glob->device_list_mutex);
1407 spin_lock_init(&glob->lru_lock);
1408 glob->mem_glob = bo_ref->mem_glob;
1409 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1410
1411 if (unlikely(glob->dummy_read_page == NULL)) {
1412 ret = -ENOMEM;
1413 goto out_no_drp;
1414 }
1415
1416 INIT_LIST_HEAD(&glob->swap_lru);
1417 INIT_LIST_HEAD(&glob->device_list);
1418
1419 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1420 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1421 if (unlikely(ret != 0)) {
1422 printk(KERN_ERR TTM_PFX
1423 "Could not register buffer object swapout.\n");
1424 goto out_no_shrink;
1425 }
1426
1427 glob->ttm_bo_extra_size =
1428 ttm_round_pot(sizeof(struct ttm_tt)) +
1429 ttm_round_pot(sizeof(struct ttm_backend));
1430
1431 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1432 ttm_round_pot(sizeof(struct ttm_buffer_object));
1433
1434 atomic_set(&glob->bo_count, 0);
1435
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001436 ret = kobject_init_and_add(
1437 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001438 if (unlikely(ret != 0))
1439 kobject_put(&glob->kobj);
1440 return ret;
1441out_no_shrink:
1442 __free_page(glob->dummy_read_page);
1443out_no_drp:
1444 kfree(glob);
1445 return ret;
1446}
1447EXPORT_SYMBOL(ttm_bo_global_init);
1448
1449
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001450int ttm_bo_device_release(struct ttm_bo_device *bdev)
1451{
1452 int ret = 0;
1453 unsigned i = TTM_NUM_MEM_TYPES;
1454 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001455 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001456
1457 while (i--) {
1458 man = &bdev->man[i];
1459 if (man->has_type) {
1460 man->use_type = false;
1461 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1462 ret = -EBUSY;
1463 printk(KERN_ERR TTM_PFX
1464 "DRM memory manager type %d "
1465 "is not clean.\n", i);
1466 }
1467 man->has_type = false;
1468 }
1469 }
1470
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001471 mutex_lock(&glob->device_list_mutex);
1472 list_del(&bdev->device_list);
1473 mutex_unlock(&glob->device_list_mutex);
1474
Tejun Heof094cfc2010-12-24 15:59:06 +01001475 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001476
1477 while (ttm_bo_delayed_delete(bdev, true))
1478 ;
1479
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001480 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001481 if (list_empty(&bdev->ddestroy))
1482 TTM_DEBUG("Delayed destroy list was clean\n");
1483
1484 if (list_empty(&bdev->man[0].lru))
1485 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001486 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001487
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1489 write_lock(&bdev->vm_lock);
1490 drm_mm_takedown(&bdev->addr_space_mm);
1491 write_unlock(&bdev->vm_lock);
1492
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001493 return ret;
1494}
1495EXPORT_SYMBOL(ttm_bo_device_release);
1496
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001498 struct ttm_bo_global *glob,
1499 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001500 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001501 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001502{
1503 int ret = -EINVAL;
1504
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001505 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001506 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001507
1508 memset(bdev->man, 0, sizeof(bdev->man));
1509
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510 /*
1511 * Initialize the system memory buffer type.
1512 * Other types need to be driver / IOCTL initialized.
1513 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001514 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001515 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001516 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517
1518 bdev->addr_space_rb = RB_ROOT;
1519 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1520 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001521 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522
1523 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1524 bdev->nice_mode = true;
1525 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001527 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001528 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001530 mutex_lock(&glob->device_list_mutex);
1531 list_add_tail(&bdev->device_list, &glob->device_list);
1532 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001533
1534 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001535out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001537out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538 return ret;
1539}
1540EXPORT_SYMBOL(ttm_bo_device_init);
1541
1542/*
1543 * buffer object vm functions.
1544 */
1545
1546bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1547{
1548 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1549
1550 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1551 if (mem->mem_type == TTM_PL_SYSTEM)
1552 return false;
1553
1554 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1555 return false;
1556
1557 if (mem->placement & TTM_PL_FLAG_CACHED)
1558 return false;
1559 }
1560 return true;
1561}
1562
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1564{
1565 struct ttm_bo_device *bdev = bo->bdev;
1566 loff_t offset = (loff_t) bo->addr_space_offset;
1567 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1568
1569 if (!bdev->dev_mapping)
1570 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001571 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Jerome Glisse82c5da62010-04-09 14:39:23 +02001572 ttm_mem_io_free(bdev, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001573}
Dave Airliee024e112009-06-24 09:48:08 +10001574EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575
1576static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1577{
1578 struct ttm_bo_device *bdev = bo->bdev;
1579 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1580 struct rb_node *parent = NULL;
1581 struct ttm_buffer_object *cur_bo;
1582 unsigned long offset = bo->vm_node->start;
1583 unsigned long cur_offset;
1584
1585 while (*cur) {
1586 parent = *cur;
1587 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1588 cur_offset = cur_bo->vm_node->start;
1589 if (offset < cur_offset)
1590 cur = &parent->rb_left;
1591 else if (offset > cur_offset)
1592 cur = &parent->rb_right;
1593 else
1594 BUG();
1595 }
1596
1597 rb_link_node(&bo->vm_rb, parent, cur);
1598 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1599}
1600
1601/**
1602 * ttm_bo_setup_vm:
1603 *
1604 * @bo: the buffer to allocate address space for
1605 *
1606 * Allocate address space in the drm device so that applications
1607 * can mmap the buffer and access the contents. This only
1608 * applies to ttm_bo_type_device objects as others are not
1609 * placed in the drm device address space.
1610 */
1611
1612static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1613{
1614 struct ttm_bo_device *bdev = bo->bdev;
1615 int ret;
1616
1617retry_pre_get:
1618 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1619 if (unlikely(ret != 0))
1620 return ret;
1621
1622 write_lock(&bdev->vm_lock);
1623 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1624 bo->mem.num_pages, 0, 0);
1625
1626 if (unlikely(bo->vm_node == NULL)) {
1627 ret = -ENOMEM;
1628 goto out_unlock;
1629 }
1630
1631 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1632 bo->mem.num_pages, 0);
1633
1634 if (unlikely(bo->vm_node == NULL)) {
1635 write_unlock(&bdev->vm_lock);
1636 goto retry_pre_get;
1637 }
1638
1639 ttm_bo_vm_insert_rb(bo);
1640 write_unlock(&bdev->vm_lock);
1641 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1642
1643 return 0;
1644out_unlock:
1645 write_unlock(&bdev->vm_lock);
1646 return ret;
1647}
1648
1649int ttm_bo_wait(struct ttm_buffer_object *bo,
1650 bool lazy, bool interruptible, bool no_wait)
1651{
1652 struct ttm_bo_driver *driver = bo->bdev->driver;
1653 void *sync_obj;
1654 void *sync_obj_arg;
1655 int ret = 0;
1656
1657 if (likely(bo->sync_obj == NULL))
1658 return 0;
1659
1660 while (bo->sync_obj) {
1661
1662 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1663 void *tmp_obj = bo->sync_obj;
1664 bo->sync_obj = NULL;
1665 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1666 spin_unlock(&bo->lock);
1667 driver->sync_obj_unref(&tmp_obj);
1668 spin_lock(&bo->lock);
1669 continue;
1670 }
1671
1672 if (no_wait)
1673 return -EBUSY;
1674
1675 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1676 sync_obj_arg = bo->sync_obj_arg;
1677 spin_unlock(&bo->lock);
1678 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1679 lazy, interruptible);
1680 if (unlikely(ret != 0)) {
1681 driver->sync_obj_unref(&sync_obj);
1682 spin_lock(&bo->lock);
1683 return ret;
1684 }
1685 spin_lock(&bo->lock);
1686 if (likely(bo->sync_obj == sync_obj &&
1687 bo->sync_obj_arg == sync_obj_arg)) {
1688 void *tmp_obj = bo->sync_obj;
1689 bo->sync_obj = NULL;
1690 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1691 &bo->priv_flags);
1692 spin_unlock(&bo->lock);
1693 driver->sync_obj_unref(&sync_obj);
1694 driver->sync_obj_unref(&tmp_obj);
1695 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001696 } else {
1697 spin_unlock(&bo->lock);
1698 driver->sync_obj_unref(&sync_obj);
1699 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001700 }
1701 }
1702 return 0;
1703}
1704EXPORT_SYMBOL(ttm_bo_wait);
1705
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001706int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1707{
1708 int ret = 0;
1709
1710 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001711 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001712 */
1713
1714 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1715 if (unlikely(ret != 0))
1716 return ret;
1717 spin_lock(&bo->lock);
1718 ret = ttm_bo_wait(bo, false, true, no_wait);
1719 spin_unlock(&bo->lock);
1720 if (likely(ret == 0))
1721 atomic_inc(&bo->cpu_writers);
1722 ttm_bo_unreserve(bo);
1723 return ret;
1724}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001725EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001726
1727void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1728{
1729 if (atomic_dec_and_test(&bo->cpu_writers))
1730 wake_up_all(&bo->event_queue);
1731}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001732EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001733
1734/**
1735 * A buffer object shrink method that tries to swap out the first
1736 * buffer object on the bo_global::swap_lru list.
1737 */
1738
1739static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1740{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001741 struct ttm_bo_global *glob =
1742 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001743 struct ttm_buffer_object *bo;
1744 int ret = -EBUSY;
1745 int put_count;
1746 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1747
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001748 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001749 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001750 if (unlikely(list_empty(&glob->swap_lru))) {
1751 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001752 return -EBUSY;
1753 }
1754
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001755 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001756 struct ttm_buffer_object, swap);
1757 kref_get(&bo->list_kref);
1758
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001759 if (!list_empty(&bo->ddestroy)) {
1760 spin_unlock(&glob->lru_lock);
1761 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1762 kref_put(&bo->list_kref, ttm_bo_release_list);
1763 continue;
1764 }
1765
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001766 /**
1767 * Reserve buffer. Since we unlock while sleeping, we need
1768 * to re-check that nobody removed us from the swap-list while
1769 * we slept.
1770 */
1771
1772 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1773 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001774 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001775 ttm_bo_wait_unreserved(bo, false);
1776 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001777 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001778 }
1779 }
1780
1781 BUG_ON(ret != 0);
1782 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001783 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001784
1785 while (put_count--)
1786 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1787
1788 /**
1789 * Wait for GPU, then move to system cached.
1790 */
1791
1792 spin_lock(&bo->lock);
1793 ret = ttm_bo_wait(bo, false, false, false);
1794 spin_unlock(&bo->lock);
1795
1796 if (unlikely(ret != 0))
1797 goto out;
1798
1799 if ((bo->mem.placement & swap_placement) != swap_placement) {
1800 struct ttm_mem_reg evict_mem;
1801
1802 evict_mem = bo->mem;
1803 evict_mem.mm_node = NULL;
1804 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1805 evict_mem.mem_type = TTM_PL_SYSTEM;
1806
1807 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001808 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001809 if (unlikely(ret != 0))
1810 goto out;
1811 }
1812
1813 ttm_bo_unmap_virtual(bo);
1814
1815 /**
1816 * Swap out. Buffer will be swapped in again as soon as
1817 * anyone tries to access a ttm page.
1818 */
1819
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001820 if (bo->bdev->driver->swap_notify)
1821 bo->bdev->driver->swap_notify(bo);
1822
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001823 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1824out:
1825
1826 /**
1827 *
1828 * Unreserve without putting on LRU to avoid swapping out an
1829 * already swapped buffer.
1830 */
1831
1832 atomic_set(&bo->reserved, 0);
1833 wake_up_all(&bo->event_queue);
1834 kref_put(&bo->list_kref, ttm_bo_release_list);
1835 return ret;
1836}
1837
1838void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1839{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001840 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001841 ;
1842}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001843EXPORT_SYMBOL(ttm_bo_swapout_all);