blob: 340dfb11959d3f3d40067972a7d1cf8e54e1dd7c [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)) {
227 if (use_sequence && bo->seq_valid &&
228 (sequence - bo->val_seq < (1 << 31))) {
229 return -EAGAIN;
230 }
231
232 if (no_wait)
233 return -EBUSY;
234
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200235 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200236 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200237 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200238
239 if (unlikely(ret))
240 return ret;
241 }
242
243 if (use_sequence) {
244 bo->val_seq = sequence;
245 bo->seq_valid = true;
246 } else {
247 bo->seq_valid = false;
248 }
249
250 return 0;
251}
252EXPORT_SYMBOL(ttm_bo_reserve);
253
254static void ttm_bo_ref_bug(struct kref *list_kref)
255{
256 BUG();
257}
258
259int ttm_bo_reserve(struct ttm_buffer_object *bo,
260 bool interruptible,
261 bool no_wait, bool use_sequence, uint32_t sequence)
262{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200263 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200264 int put_count = 0;
265 int ret;
266
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200267 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200268 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
269 sequence);
270 if (likely(ret == 0))
271 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200272 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200273
274 while (put_count--)
275 kref_put(&bo->list_kref, ttm_bo_ref_bug);
276
277 return ret;
278}
279
280void ttm_bo_unreserve(struct ttm_buffer_object *bo)
281{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200282 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200283
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200284 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200285 ttm_bo_add_to_lru(bo);
286 atomic_set(&bo->reserved, 0);
287 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200288 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200289}
290EXPORT_SYMBOL(ttm_bo_unreserve);
291
292/*
293 * Call bo->mutex locked.
294 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200295static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
296{
297 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200298 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200299 int ret = 0;
300 uint32_t page_flags = 0;
301
302 TTM_ASSERT_LOCKED(&bo->mutex);
303 bo->ttm = NULL;
304
Dave Airliead49f502009-07-10 22:36:26 +1000305 if (bdev->need_dma32)
306 page_flags |= TTM_PAGE_FLAG_DMA32;
307
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200308 switch (bo->type) {
309 case ttm_bo_type_device:
310 if (zero_alloc)
311 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
312 case ttm_bo_type_kernel:
313 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200314 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200315 if (unlikely(bo->ttm == NULL))
316 ret = -ENOMEM;
317 break;
318 case ttm_bo_type_user:
319 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
320 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200321 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000322 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000324 break;
325 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326
327 ret = ttm_tt_set_user(bo->ttm, current,
328 bo->buffer_start, bo->num_pages);
329 if (unlikely(ret != 0))
330 ttm_tt_destroy(bo->ttm);
331 break;
332 default:
333 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
334 ret = -EINVAL;
335 break;
336 }
337
338 return ret;
339}
340
341static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
342 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000343 bool evict, bool interruptible,
344 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200345{
346 struct ttm_bo_device *bdev = bo->bdev;
347 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
348 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
349 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
350 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
351 int ret = 0;
352
353 if (old_is_pci || new_is_pci ||
354 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
355 ttm_bo_unmap_virtual(bo);
356
357 /*
358 * Create and bind a ttm if required.
359 */
360
361 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
362 ret = ttm_bo_add_ttm(bo, false);
363 if (ret)
364 goto out_err;
365
366 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
367 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200368 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200369
370 if (mem->mem_type != TTM_PL_SYSTEM) {
371 ret = ttm_tt_bind(bo->ttm, mem);
372 if (ret)
373 goto out_err;
374 }
375
376 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100377 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200378 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200379 goto moved;
380 }
381
382 }
383
Dave Airliee024e112009-06-24 09:48:08 +1000384 if (bdev->driver->move_notify)
385 bdev->driver->move_notify(bo, mem);
386
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200387 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
388 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000389 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390 else if (bdev->driver->move)
391 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000392 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200393 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000394 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200395
396 if (ret)
397 goto out_err;
398
399moved:
400 if (bo->evicted) {
401 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
402 if (ret)
403 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
404 bo->evicted = false;
405 }
406
407 if (bo->mem.mm_node) {
408 spin_lock(&bo->lock);
Ben Skeggsd961db72010-08-05 10:48:18 +1000409 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200410 bdev->man[bo->mem.mem_type].gpu_offset;
411 bo->cur_placement = bo->mem.placement;
412 spin_unlock(&bo->lock);
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100413 } else
414 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200415
416 return 0;
417
418out_err:
419 new_man = &bdev->man[bo->mem.mem_type];
420 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
421 ttm_tt_unbind(bo->ttm);
422 ttm_tt_destroy(bo->ttm);
423 bo->ttm = NULL;
424 }
425
426 return ret;
427}
428
429/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200430 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200431 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200432 * This is the place to put in driver specific hooks to release
433 * driver private resources.
434 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200435 */
436
437static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
438{
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200439 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200440 ttm_tt_unbind(bo->ttm);
441 ttm_tt_destroy(bo->ttm);
442 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200443 }
444
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200445 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200446
447 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200448
449 /*
450 * Make processes trying to reserve really pick it up.
451 */
452 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200453 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200454}
455
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200456static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200457{
458 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200459 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200460 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000461 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200462 void *sync_obj_arg;
463 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200464 int ret;
465
466 spin_lock(&bo->lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200467 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200468 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200469
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200470 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100471
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200472 /**
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200473 * Lock inversion between bo::reserve and bo::lock here,
474 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200475 */
476
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200477 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200478
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200479 if (unlikely(ret == -EBUSY))
480 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200481
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200482 spin_unlock(&bo->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200483 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200484
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200485 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200486 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200487
488 while (put_count--)
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100489 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200490
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200491 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200492 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200493 spin_lock(&glob->lru_lock);
494 }
495queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200496 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000497 if (bo->sync_obj)
498 sync_obj = driver->sync_obj_ref(bo->sync_obj);
499 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200500
501 kref_get(&bo->list_kref);
502 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
503 spin_unlock(&glob->lru_lock);
504 spin_unlock(&bo->lock);
505
Thomas Hellstromaa123262010-11-02 13:21:47 +0000506 if (sync_obj) {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200507 driver->sync_obj_flush(sync_obj, sync_obj_arg);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000508 driver->sync_obj_unref(&sync_obj);
509 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200510 schedule_delayed_work(&bdev->wq,
511 ((HZ / 100) < 1) ? 1 : HZ / 100);
512}
513
514/**
515 * function ttm_bo_cleanup_refs
516 * If bo idle, remove from delayed- and lru lists, and unref.
517 * If not idle, do nothing.
518 *
519 * @interruptible Any sleeps should occur interruptibly.
520 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
521 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
522 */
523
524static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
525 bool interruptible,
526 bool no_wait_reserve,
527 bool no_wait_gpu)
528{
529 struct ttm_bo_global *glob = bo->glob;
530 int put_count;
531 int ret = 0;
532
533retry:
534 spin_lock(&bo->lock);
535 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
536 spin_unlock(&bo->lock);
537
538 if (unlikely(ret != 0))
539 return ret;
540
541 spin_lock(&glob->lru_lock);
542 ret = ttm_bo_reserve_locked(bo, interruptible,
543 no_wait_reserve, false, 0);
544
545 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200546 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200547 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200548 }
549
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200550 /**
551 * We can re-check for sync object without taking
552 * the bo::lock since setting the sync object requires
553 * also bo::reserved. A busy object at this point may
554 * be caused by another thread recently starting an accelerated
555 * eviction.
556 */
557
558 if (unlikely(bo->sync_obj)) {
559 atomic_set(&bo->reserved, 0);
560 wake_up_all(&bo->event_queue);
561 spin_unlock(&glob->lru_lock);
562 goto retry;
563 }
564
565 put_count = ttm_bo_del_from_lru(bo);
566 list_del_init(&bo->ddestroy);
567 ++put_count;
568
569 spin_unlock(&glob->lru_lock);
570 ttm_bo_cleanup_memtype_use(bo);
571
572 while (put_count--)
573 kref_put(&bo->list_kref, ttm_bo_ref_bug);
574
575 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200576}
577
578/**
579 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
580 * encountered buffers.
581 */
582
583static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
584{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200585 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100586 struct ttm_buffer_object *entry = NULL;
587 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200588
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200589 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100590 if (list_empty(&bdev->ddestroy))
591 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200592
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100593 entry = list_first_entry(&bdev->ddestroy,
594 struct ttm_buffer_object, ddestroy);
595 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200596
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100597 for (;;) {
598 struct ttm_buffer_object *nentry = NULL;
599
600 if (entry->ddestroy.next != &bdev->ddestroy) {
601 nentry = list_first_entry(&entry->ddestroy,
602 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200603 kref_get(&nentry->list_kref);
604 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200605
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200606 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200607 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
608 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200609 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100610 entry = nentry;
611
612 if (ret || !entry)
613 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200614
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200615 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100616 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200617 break;
618 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200619
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100620out_unlock:
621 spin_unlock(&glob->lru_lock);
622out:
623 if (entry)
624 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200625 return ret;
626}
627
628static void ttm_bo_delayed_workqueue(struct work_struct *work)
629{
630 struct ttm_bo_device *bdev =
631 container_of(work, struct ttm_bo_device, wq.work);
632
633 if (ttm_bo_delayed_delete(bdev, false)) {
634 schedule_delayed_work(&bdev->wq,
635 ((HZ / 100) < 1) ? 1 : HZ / 100);
636 }
637}
638
639static void ttm_bo_release(struct kref *kref)
640{
641 struct ttm_buffer_object *bo =
642 container_of(kref, struct ttm_buffer_object, kref);
643 struct ttm_bo_device *bdev = bo->bdev;
644
645 if (likely(bo->vm_node != NULL)) {
646 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
647 drm_mm_put_block(bo->vm_node);
648 bo->vm_node = NULL;
649 }
650 write_unlock(&bdev->vm_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200651 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200652 kref_put(&bo->list_kref, ttm_bo_release_list);
653 write_lock(&bdev->vm_lock);
654}
655
656void ttm_bo_unref(struct ttm_buffer_object **p_bo)
657{
658 struct ttm_buffer_object *bo = *p_bo;
659 struct ttm_bo_device *bdev = bo->bdev;
660
661 *p_bo = NULL;
662 write_lock(&bdev->vm_lock);
663 kref_put(&bo->kref, ttm_bo_release);
664 write_unlock(&bdev->vm_lock);
665}
666EXPORT_SYMBOL(ttm_bo_unref);
667
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400668int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
669{
670 return cancel_delayed_work_sync(&bdev->wq);
671}
672EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
673
674void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
675{
676 if (resched)
677 schedule_delayed_work(&bdev->wq,
678 ((HZ / 100) < 1) ? 1 : HZ / 100);
679}
680EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
681
Jerome Glisseca262a9992009-12-08 15:33:32 +0100682static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000683 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200684{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200685 struct ttm_bo_device *bdev = bo->bdev;
686 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100687 struct ttm_placement placement;
688 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200689
690 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000691 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200692 spin_unlock(&bo->lock);
693
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200694 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100695 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200696 printk(KERN_ERR TTM_PFX
697 "Failed to expire sync object before "
698 "buffer eviction.\n");
699 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200700 goto out;
701 }
702
703 BUG_ON(!atomic_read(&bo->reserved));
704
705 evict_mem = bo->mem;
706 evict_mem.mm_node = NULL;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200707 evict_mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200708
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100709 placement.fpfn = 0;
710 placement.lpfn = 0;
711 placement.num_placement = 0;
712 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100713 bdev->driver->evict_flags(bo, &placement);
714 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000715 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200716 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100717 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200718 printk(KERN_ERR TTM_PFX
719 "Failed to find memory space for "
720 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100721 ttm_bo_mem_space_debug(bo, &placement);
722 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200723 goto out;
724 }
725
726 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000727 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200728 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100729 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200730 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000731 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200732 goto out;
733 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200734 bo->evicted = true;
735out:
736 return ret;
737}
738
Jerome Glisseca262a9992009-12-08 15:33:32 +0100739static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
740 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000741 bool interruptible, bool no_wait_reserve,
742 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100743{
744 struct ttm_bo_global *glob = bdev->glob;
745 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
746 struct ttm_buffer_object *bo;
747 int ret, put_count = 0;
748
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100749retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100750 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100751 if (list_empty(&man->lru)) {
752 spin_unlock(&glob->lru_lock);
753 return -EBUSY;
754 }
755
Jerome Glisseca262a9992009-12-08 15:33:32 +0100756 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
757 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100758
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200759 if (!list_empty(&bo->ddestroy)) {
760 spin_unlock(&glob->lru_lock);
761 ret = ttm_bo_cleanup_refs(bo, interruptible,
762 no_wait_reserve, no_wait_gpu);
763 kref_put(&bo->list_kref, ttm_bo_release_list);
764
765 if (likely(ret == 0 || ret == -ERESTARTSYS))
766 return ret;
767
768 goto retry;
769 }
770
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000771 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100772
773 if (unlikely(ret == -EBUSY)) {
774 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000775 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100776 ret = ttm_bo_wait_unreserved(bo, interruptible);
777
778 kref_put(&bo->list_kref, ttm_bo_release_list);
779
780 /**
781 * We *need* to retry after releasing the lru lock.
782 */
783
784 if (unlikely(ret != 0))
785 return ret;
786 goto retry;
787 }
788
789 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100790 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100791
792 BUG_ON(ret != 0);
793
Jerome Glisseca262a9992009-12-08 15:33:32 +0100794 while (put_count--)
795 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100796
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000797 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100798 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100799
Jerome Glisseca262a9992009-12-08 15:33:32 +0100800 kref_put(&bo->list_kref, ttm_bo_release_list);
801 return ret;
802}
803
Ben Skeggs42311ff2010-08-04 12:07:08 +1000804void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
805{
Ben Skeggsd961db72010-08-05 10:48:18 +1000806 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000807
Ben Skeggsd961db72010-08-05 10:48:18 +1000808 if (mem->mm_node)
809 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000810}
811EXPORT_SYMBOL(ttm_bo_mem_put);
812
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200813/**
814 * Repeatedly evict memory from the LRU for @mem_type until we create enough
815 * space, or we've evicted everything and there isn't enough space.
816 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100817static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
818 uint32_t mem_type,
819 struct ttm_placement *placement,
820 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000821 bool interruptible,
822 bool no_wait_reserve,
823 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200824{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100825 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200826 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200827 int ret;
828
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200829 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000830 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200831 if (unlikely(ret != 0))
832 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000833 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100834 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100835 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000836 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100837 if (unlikely(ret != 0))
838 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200839 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000840 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200841 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200842 mem->mem_type = mem_type;
843 return 0;
844}
845
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200846static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
847 uint32_t cur_placement,
848 uint32_t proposed_placement)
849{
850 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
851 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
852
853 /**
854 * Keep current caching if possible.
855 */
856
857 if ((cur_placement & caching) != 0)
858 result |= (cur_placement & caching);
859 else if ((man->default_caching & caching) != 0)
860 result |= man->default_caching;
861 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
862 result |= TTM_PL_FLAG_CACHED;
863 else if ((TTM_PL_FLAG_WC & caching) != 0)
864 result |= TTM_PL_FLAG_WC;
865 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
866 result |= TTM_PL_FLAG_UNCACHED;
867
868 return result;
869}
870
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200871static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
872 bool disallow_fixed,
873 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200874 uint32_t proposed_placement,
875 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200876{
877 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
878
879 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
880 return false;
881
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200882 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200883 return false;
884
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200885 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200886 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200887
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200888 cur_flags |= (proposed_placement & man->available_caching);
889
890 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200891 return true;
892}
893
894/**
895 * Creates space for memory region @mem according to its type.
896 *
897 * This function first searches for free space in compatible memory types in
898 * the priority order defined by the driver. If free space isn't found, then
899 * ttm_bo_mem_force_space is attempted in priority order to evict and find
900 * space.
901 */
902int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100903 struct ttm_placement *placement,
904 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000905 bool interruptible, bool no_wait_reserve,
906 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200907{
908 struct ttm_bo_device *bdev = bo->bdev;
909 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200910 uint32_t mem_type = TTM_PL_SYSTEM;
911 uint32_t cur_flags = 0;
912 bool type_found = false;
913 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100914 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100915 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200916
917 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000918 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100919 ret = ttm_mem_type_from_flags(placement->placement[i],
920 &mem_type);
921 if (ret)
922 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923 man = &bdev->man[mem_type];
924
925 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100926 bo->type == ttm_bo_type_user,
927 mem_type,
928 placement->placement[i],
929 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200930
931 if (!type_ok)
932 continue;
933
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200934 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
935 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100936 /*
937 * Use the access and other non-mapping-related flag bits from
938 * the memory placement flags to the current flags
939 */
940 ttm_flag_masked(&cur_flags, placement->placement[i],
941 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200942
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200943 if (mem_type == TTM_PL_SYSTEM)
944 break;
945
946 if (man->has_type && man->use_type) {
947 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000948 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100949 if (unlikely(ret))
950 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000952 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953 break;
954 }
955
Ben Skeggsd961db72010-08-05 10:48:18 +1000956 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200957 mem->mem_type = mem_type;
958 mem->placement = cur_flags;
959 return 0;
960 }
961
962 if (!type_found)
963 return -EINVAL;
964
Dave Airlieb6637522009-12-14 14:51:35 +1000965 for (i = 0; i < placement->num_busy_placement; ++i) {
966 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100967 &mem_type);
968 if (ret)
969 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200971 if (!man->has_type)
972 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200973 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100974 bo->type == ttm_bo_type_user,
975 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000976 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100977 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200978 continue;
979
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200980 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
981 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100982 /*
983 * Use the access and other non-mapping-related flag bits from
984 * the memory placement flags to the current flags
985 */
Dave Airlieb6637522009-12-14 14:51:35 +1000986 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100987 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200988
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100989
990 if (mem_type == TTM_PL_SYSTEM) {
991 mem->mem_type = mem_type;
992 mem->placement = cur_flags;
993 mem->mm_node = NULL;
994 return 0;
995 }
996
Jerome Glisseca262a9992009-12-08 15:33:32 +0100997 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000998 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200999 if (ret == 0 && mem->mm_node) {
1000 mem->placement = cur_flags;
1001 return 0;
1002 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001003 if (ret == -ERESTARTSYS)
1004 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001006 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001007 return ret;
1008}
1009EXPORT_SYMBOL(ttm_bo_mem_space);
1010
1011int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1012{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1014 return -EBUSY;
1015
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001016 return wait_event_interruptible(bo->event_queue,
1017 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001018}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001019EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001020
1021int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001022 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001023 bool interruptible, bool no_wait_reserve,
1024 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001025{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001026 int ret = 0;
1027 struct ttm_mem_reg mem;
1028
1029 BUG_ON(!atomic_read(&bo->reserved));
1030
1031 /*
1032 * FIXME: It's possible to pipeline buffer moves.
1033 * Have the driver move function wait for idle when necessary,
1034 * instead of doing it here.
1035 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001037 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001038 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001039 if (ret)
1040 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001041 mem.num_pages = bo->num_pages;
1042 mem.size = mem.num_pages << PAGE_SHIFT;
1043 mem.page_alignment = bo->mem.page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001044 mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001045 /*
1046 * Determine where to move the buffer.
1047 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001048 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049 if (ret)
1050 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001051 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001053 if (ret && mem.mm_node)
1054 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001055 return ret;
1056}
1057
Jerome Glisseca262a9992009-12-08 15:33:32 +01001058static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001059 struct ttm_mem_reg *mem)
1060{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001061 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001062
Ben Skeggsd961db72010-08-05 10:48:18 +10001063 if (mem->mm_node && placement->lpfn != 0 &&
1064 (mem->start < placement->fpfn ||
1065 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001066 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001067
Jerome Glisseca262a9992009-12-08 15:33:32 +01001068 for (i = 0; i < placement->num_placement; i++) {
1069 if ((placement->placement[i] & mem->placement &
1070 TTM_PL_MASK_CACHING) &&
1071 (placement->placement[i] & mem->placement &
1072 TTM_PL_MASK_MEM))
1073 return i;
1074 }
1075 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001076}
1077
Jerome Glisse09855ac2009-12-10 17:16:27 +01001078int ttm_bo_validate(struct ttm_buffer_object *bo,
1079 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001080 bool interruptible, bool no_wait_reserve,
1081 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082{
1083 int ret;
1084
1085 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001086 /* Check that range is valid */
1087 if (placement->lpfn || placement->fpfn)
1088 if (placement->fpfn > placement->lpfn ||
1089 (placement->lpfn - placement->fpfn) < bo->num_pages)
1090 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001091 /*
1092 * Check whether we need to move buffer.
1093 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001094 ret = ttm_bo_mem_compat(placement, &bo->mem);
1095 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001096 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001097 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001099 } else {
1100 /*
1101 * Use the access and other non-mapping-related flag bits from
1102 * the compatible memory placement flags to the active flags
1103 */
1104 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1105 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001107 /*
1108 * We might need to add a TTM.
1109 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1111 ret = ttm_bo_add_ttm(bo, true);
1112 if (ret)
1113 return ret;
1114 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001115 return 0;
1116}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001117EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001118
Jerome Glisse09855ac2009-12-10 17:16:27 +01001119int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1120 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001121{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001122 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001123
Jerome Glisse09855ac2009-12-10 17:16:27 +01001124 if (placement->fpfn || placement->lpfn) {
1125 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1126 printk(KERN_ERR TTM_PFX "Page number range to small "
1127 "Need %lu pages, range is [%u, %u]\n",
1128 bo->mem.num_pages, placement->fpfn,
1129 placement->lpfn);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001130 return -EINVAL;
1131 }
Jerome Glisse09855ac2009-12-10 17:16:27 +01001132 }
1133 for (i = 0; i < placement->num_placement; i++) {
1134 if (!capable(CAP_SYS_ADMIN)) {
1135 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1136 printk(KERN_ERR TTM_PFX "Need to be root to "
1137 "modify NO_EVICT status.\n");
1138 return -EINVAL;
1139 }
1140 }
1141 }
1142 for (i = 0; i < placement->num_busy_placement; i++) {
1143 if (!capable(CAP_SYS_ADMIN)) {
1144 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1145 printk(KERN_ERR TTM_PFX "Need to be root to "
1146 "modify NO_EVICT status.\n");
1147 return -EINVAL;
1148 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149 }
1150 }
1151 return 0;
1152}
1153
Jerome Glisse09855ac2009-12-10 17:16:27 +01001154int ttm_bo_init(struct ttm_bo_device *bdev,
1155 struct ttm_buffer_object *bo,
1156 unsigned long size,
1157 enum ttm_bo_type type,
1158 struct ttm_placement *placement,
1159 uint32_t page_alignment,
1160 unsigned long buffer_start,
1161 bool interruptible,
1162 struct file *persistant_swap_storage,
1163 size_t acc_size,
1164 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001165{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001166 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001167 unsigned long num_pages;
1168
1169 size += buffer_start & ~PAGE_MASK;
1170 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1171 if (num_pages == 0) {
1172 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1173 return -EINVAL;
1174 }
1175 bo->destroy = destroy;
1176
1177 spin_lock_init(&bo->lock);
1178 kref_init(&bo->kref);
1179 kref_init(&bo->list_kref);
1180 atomic_set(&bo->cpu_writers, 0);
1181 atomic_set(&bo->reserved, 1);
1182 init_waitqueue_head(&bo->event_queue);
1183 INIT_LIST_HEAD(&bo->lru);
1184 INIT_LIST_HEAD(&bo->ddestroy);
1185 INIT_LIST_HEAD(&bo->swap);
1186 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001187 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001188 bo->type = type;
1189 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001190 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001191 bo->mem.mem_type = TTM_PL_SYSTEM;
1192 bo->mem.num_pages = bo->num_pages;
1193 bo->mem.mm_node = NULL;
1194 bo->mem.page_alignment = page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001195 bo->mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196 bo->buffer_start = buffer_start & PAGE_MASK;
1197 bo->priv_flags = 0;
1198 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1199 bo->seq_valid = false;
1200 bo->persistant_swap_storage = persistant_swap_storage;
1201 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001202 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203
Jerome Glisse09855ac2009-12-10 17:16:27 +01001204 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001205 if (unlikely(ret != 0))
1206 goto out_err;
1207
1208 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209 * For ttm_bo_type_device buffers, allocate
1210 * address space from the device.
1211 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212 if (bo->type == ttm_bo_type_device) {
1213 ret = ttm_bo_setup_vm(bo);
1214 if (ret)
1215 goto out_err;
1216 }
1217
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001218 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219 if (ret)
1220 goto out_err;
1221
1222 ttm_bo_unreserve(bo);
1223 return 0;
1224
1225out_err:
1226 ttm_bo_unreserve(bo);
1227 ttm_bo_unref(&bo);
1228
1229 return ret;
1230}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001231EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001232
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001233static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001234 unsigned long num_pages)
1235{
1236 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1237 PAGE_MASK;
1238
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001239 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001240}
1241
Jerome Glisse09855ac2009-12-10 17:16:27 +01001242int ttm_bo_create(struct ttm_bo_device *bdev,
1243 unsigned long size,
1244 enum ttm_bo_type type,
1245 struct ttm_placement *placement,
1246 uint32_t page_alignment,
1247 unsigned long buffer_start,
1248 bool interruptible,
1249 struct file *persistant_swap_storage,
1250 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251{
1252 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001253 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001254 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001255
1256 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001257 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001258 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001259 if (unlikely(ret != 0))
1260 return ret;
1261
1262 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1263
1264 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001265 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001266 return -ENOMEM;
1267 }
1268
Jerome Glisse09855ac2009-12-10 17:16:27 +01001269 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1270 buffer_start, interruptible,
1271 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272 if (likely(ret == 0))
1273 *p_bo = bo;
1274
1275 return ret;
1276}
1277
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001278static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001279 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001280{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001281 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001282 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001283 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001284
1285 /*
1286 * Can't use standard list traversal since we're unlocking.
1287 */
1288
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001289 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001290 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001291 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001292 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001293 if (ret) {
1294 if (allow_errors) {
1295 return ret;
1296 } else {
1297 printk(KERN_ERR TTM_PFX
1298 "Cleanup eviction failed\n");
1299 }
1300 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001301 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001303 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001304 return 0;
1305}
1306
1307int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1308{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001309 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001310 int ret = -EINVAL;
1311
1312 if (mem_type >= TTM_NUM_MEM_TYPES) {
1313 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1314 return ret;
1315 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001316 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001317
1318 if (!man->has_type) {
1319 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1320 "memory manager type %u\n", mem_type);
1321 return ret;
1322 }
1323
1324 man->use_type = false;
1325 man->has_type = false;
1326
1327 ret = 0;
1328 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001329 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001330
Ben Skeggsd961db72010-08-05 10:48:18 +10001331 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001332 }
1333
1334 return ret;
1335}
1336EXPORT_SYMBOL(ttm_bo_clean_mm);
1337
1338int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1339{
1340 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1341
1342 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1343 printk(KERN_ERR TTM_PFX
1344 "Illegal memory manager memory type %u.\n",
1345 mem_type);
1346 return -EINVAL;
1347 }
1348
1349 if (!man->has_type) {
1350 printk(KERN_ERR TTM_PFX
1351 "Memory type %u has not been initialized.\n",
1352 mem_type);
1353 return 0;
1354 }
1355
Jerome Glisseca262a9992009-12-08 15:33:32 +01001356 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001357}
1358EXPORT_SYMBOL(ttm_bo_evict_mm);
1359
1360int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001361 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001362{
1363 int ret = -EINVAL;
1364 struct ttm_mem_type_manager *man;
1365
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001366 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001367 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001368 BUG_ON(man->has_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001369
1370 ret = bdev->driver->init_mem_type(bdev, type, man);
1371 if (ret)
1372 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001373 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001374
1375 ret = 0;
1376 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001377 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001378 if (ret)
1379 return ret;
1380 }
1381 man->has_type = true;
1382 man->use_type = true;
1383 man->size = p_size;
1384
1385 INIT_LIST_HEAD(&man->lru);
1386
1387 return 0;
1388}
1389EXPORT_SYMBOL(ttm_bo_init_mm);
1390
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001391static void ttm_bo_global_kobj_release(struct kobject *kobj)
1392{
1393 struct ttm_bo_global *glob =
1394 container_of(kobj, struct ttm_bo_global, kobj);
1395
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001396 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1397 __free_page(glob->dummy_read_page);
1398 kfree(glob);
1399}
1400
Dave Airlieba4420c2010-03-09 10:56:52 +10001401void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001402{
1403 struct ttm_bo_global *glob = ref->object;
1404
1405 kobject_del(&glob->kobj);
1406 kobject_put(&glob->kobj);
1407}
1408EXPORT_SYMBOL(ttm_bo_global_release);
1409
Dave Airlieba4420c2010-03-09 10:56:52 +10001410int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001411{
1412 struct ttm_bo_global_ref *bo_ref =
1413 container_of(ref, struct ttm_bo_global_ref, ref);
1414 struct ttm_bo_global *glob = ref->object;
1415 int ret;
1416
1417 mutex_init(&glob->device_list_mutex);
1418 spin_lock_init(&glob->lru_lock);
1419 glob->mem_glob = bo_ref->mem_glob;
1420 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1421
1422 if (unlikely(glob->dummy_read_page == NULL)) {
1423 ret = -ENOMEM;
1424 goto out_no_drp;
1425 }
1426
1427 INIT_LIST_HEAD(&glob->swap_lru);
1428 INIT_LIST_HEAD(&glob->device_list);
1429
1430 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1431 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1432 if (unlikely(ret != 0)) {
1433 printk(KERN_ERR TTM_PFX
1434 "Could not register buffer object swapout.\n");
1435 goto out_no_shrink;
1436 }
1437
1438 glob->ttm_bo_extra_size =
1439 ttm_round_pot(sizeof(struct ttm_tt)) +
1440 ttm_round_pot(sizeof(struct ttm_backend));
1441
1442 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1443 ttm_round_pot(sizeof(struct ttm_buffer_object));
1444
1445 atomic_set(&glob->bo_count, 0);
1446
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001447 ret = kobject_init_and_add(
1448 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001449 if (unlikely(ret != 0))
1450 kobject_put(&glob->kobj);
1451 return ret;
1452out_no_shrink:
1453 __free_page(glob->dummy_read_page);
1454out_no_drp:
1455 kfree(glob);
1456 return ret;
1457}
1458EXPORT_SYMBOL(ttm_bo_global_init);
1459
1460
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001461int ttm_bo_device_release(struct ttm_bo_device *bdev)
1462{
1463 int ret = 0;
1464 unsigned i = TTM_NUM_MEM_TYPES;
1465 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001466 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001467
1468 while (i--) {
1469 man = &bdev->man[i];
1470 if (man->has_type) {
1471 man->use_type = false;
1472 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1473 ret = -EBUSY;
1474 printk(KERN_ERR TTM_PFX
1475 "DRM memory manager type %d "
1476 "is not clean.\n", i);
1477 }
1478 man->has_type = false;
1479 }
1480 }
1481
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001482 mutex_lock(&glob->device_list_mutex);
1483 list_del(&bdev->device_list);
1484 mutex_unlock(&glob->device_list_mutex);
1485
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001486 if (!cancel_delayed_work(&bdev->wq))
1487 flush_scheduled_work();
1488
1489 while (ttm_bo_delayed_delete(bdev, true))
1490 ;
1491
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001492 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001493 if (list_empty(&bdev->ddestroy))
1494 TTM_DEBUG("Delayed destroy list was clean\n");
1495
1496 if (list_empty(&bdev->man[0].lru))
1497 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001498 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001499
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1501 write_lock(&bdev->vm_lock);
1502 drm_mm_takedown(&bdev->addr_space_mm);
1503 write_unlock(&bdev->vm_lock);
1504
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001505 return ret;
1506}
1507EXPORT_SYMBOL(ttm_bo_device_release);
1508
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001509int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001510 struct ttm_bo_global *glob,
1511 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001512 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001513 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001514{
1515 int ret = -EINVAL;
1516
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001518 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001519
1520 memset(bdev->man, 0, sizeof(bdev->man));
1521
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522 /*
1523 * Initialize the system memory buffer type.
1524 * Other types need to be driver / IOCTL initialized.
1525 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001526 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001527 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001528 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529
1530 bdev->addr_space_rb = RB_ROOT;
1531 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1532 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001533 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534
1535 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1536 bdev->nice_mode = true;
1537 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001539 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001540 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001541
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001542 mutex_lock(&glob->device_list_mutex);
1543 list_add_tail(&bdev->device_list, &glob->device_list);
1544 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001545
1546 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001547out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001548 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001549out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001550 return ret;
1551}
1552EXPORT_SYMBOL(ttm_bo_device_init);
1553
1554/*
1555 * buffer object vm functions.
1556 */
1557
1558bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1559{
1560 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1561
1562 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1563 if (mem->mem_type == TTM_PL_SYSTEM)
1564 return false;
1565
1566 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1567 return false;
1568
1569 if (mem->placement & TTM_PL_FLAG_CACHED)
1570 return false;
1571 }
1572 return true;
1573}
1574
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1576{
1577 struct ttm_bo_device *bdev = bo->bdev;
1578 loff_t offset = (loff_t) bo->addr_space_offset;
1579 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1580
1581 if (!bdev->dev_mapping)
1582 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001583 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Jerome Glisse82c5da62010-04-09 14:39:23 +02001584 ttm_mem_io_free(bdev, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001585}
Dave Airliee024e112009-06-24 09:48:08 +10001586EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001587
1588static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1589{
1590 struct ttm_bo_device *bdev = bo->bdev;
1591 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1592 struct rb_node *parent = NULL;
1593 struct ttm_buffer_object *cur_bo;
1594 unsigned long offset = bo->vm_node->start;
1595 unsigned long cur_offset;
1596
1597 while (*cur) {
1598 parent = *cur;
1599 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1600 cur_offset = cur_bo->vm_node->start;
1601 if (offset < cur_offset)
1602 cur = &parent->rb_left;
1603 else if (offset > cur_offset)
1604 cur = &parent->rb_right;
1605 else
1606 BUG();
1607 }
1608
1609 rb_link_node(&bo->vm_rb, parent, cur);
1610 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1611}
1612
1613/**
1614 * ttm_bo_setup_vm:
1615 *
1616 * @bo: the buffer to allocate address space for
1617 *
1618 * Allocate address space in the drm device so that applications
1619 * can mmap the buffer and access the contents. This only
1620 * applies to ttm_bo_type_device objects as others are not
1621 * placed in the drm device address space.
1622 */
1623
1624static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1625{
1626 struct ttm_bo_device *bdev = bo->bdev;
1627 int ret;
1628
1629retry_pre_get:
1630 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1631 if (unlikely(ret != 0))
1632 return ret;
1633
1634 write_lock(&bdev->vm_lock);
1635 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1636 bo->mem.num_pages, 0, 0);
1637
1638 if (unlikely(bo->vm_node == NULL)) {
1639 ret = -ENOMEM;
1640 goto out_unlock;
1641 }
1642
1643 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1644 bo->mem.num_pages, 0);
1645
1646 if (unlikely(bo->vm_node == NULL)) {
1647 write_unlock(&bdev->vm_lock);
1648 goto retry_pre_get;
1649 }
1650
1651 ttm_bo_vm_insert_rb(bo);
1652 write_unlock(&bdev->vm_lock);
1653 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1654
1655 return 0;
1656out_unlock:
1657 write_unlock(&bdev->vm_lock);
1658 return ret;
1659}
1660
1661int ttm_bo_wait(struct ttm_buffer_object *bo,
1662 bool lazy, bool interruptible, bool no_wait)
1663{
1664 struct ttm_bo_driver *driver = bo->bdev->driver;
1665 void *sync_obj;
1666 void *sync_obj_arg;
1667 int ret = 0;
1668
1669 if (likely(bo->sync_obj == NULL))
1670 return 0;
1671
1672 while (bo->sync_obj) {
1673
1674 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1675 void *tmp_obj = bo->sync_obj;
1676 bo->sync_obj = NULL;
1677 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1678 spin_unlock(&bo->lock);
1679 driver->sync_obj_unref(&tmp_obj);
1680 spin_lock(&bo->lock);
1681 continue;
1682 }
1683
1684 if (no_wait)
1685 return -EBUSY;
1686
1687 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1688 sync_obj_arg = bo->sync_obj_arg;
1689 spin_unlock(&bo->lock);
1690 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1691 lazy, interruptible);
1692 if (unlikely(ret != 0)) {
1693 driver->sync_obj_unref(&sync_obj);
1694 spin_lock(&bo->lock);
1695 return ret;
1696 }
1697 spin_lock(&bo->lock);
1698 if (likely(bo->sync_obj == sync_obj &&
1699 bo->sync_obj_arg == sync_obj_arg)) {
1700 void *tmp_obj = bo->sync_obj;
1701 bo->sync_obj = NULL;
1702 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1703 &bo->priv_flags);
1704 spin_unlock(&bo->lock);
1705 driver->sync_obj_unref(&sync_obj);
1706 driver->sync_obj_unref(&tmp_obj);
1707 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001708 } else {
1709 spin_unlock(&bo->lock);
1710 driver->sync_obj_unref(&sync_obj);
1711 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001712 }
1713 }
1714 return 0;
1715}
1716EXPORT_SYMBOL(ttm_bo_wait);
1717
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001718int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1719{
1720 int ret = 0;
1721
1722 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001723 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001724 */
1725
1726 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1727 if (unlikely(ret != 0))
1728 return ret;
1729 spin_lock(&bo->lock);
1730 ret = ttm_bo_wait(bo, false, true, no_wait);
1731 spin_unlock(&bo->lock);
1732 if (likely(ret == 0))
1733 atomic_inc(&bo->cpu_writers);
1734 ttm_bo_unreserve(bo);
1735 return ret;
1736}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001737EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001738
1739void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1740{
1741 if (atomic_dec_and_test(&bo->cpu_writers))
1742 wake_up_all(&bo->event_queue);
1743}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001744EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001745
1746/**
1747 * A buffer object shrink method that tries to swap out the first
1748 * buffer object on the bo_global::swap_lru list.
1749 */
1750
1751static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1752{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001753 struct ttm_bo_global *glob =
1754 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001755 struct ttm_buffer_object *bo;
1756 int ret = -EBUSY;
1757 int put_count;
1758 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1759
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001760 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001761 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001762 if (unlikely(list_empty(&glob->swap_lru))) {
1763 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001764 return -EBUSY;
1765 }
1766
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001767 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001768 struct ttm_buffer_object, swap);
1769 kref_get(&bo->list_kref);
1770
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001771 if (!list_empty(&bo->ddestroy)) {
1772 spin_unlock(&glob->lru_lock);
1773 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1774 kref_put(&bo->list_kref, ttm_bo_release_list);
1775 continue;
1776 }
1777
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001778 /**
1779 * Reserve buffer. Since we unlock while sleeping, we need
1780 * to re-check that nobody removed us from the swap-list while
1781 * we slept.
1782 */
1783
1784 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1785 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001786 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001787 ttm_bo_wait_unreserved(bo, false);
1788 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001789 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001790 }
1791 }
1792
1793 BUG_ON(ret != 0);
1794 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001795 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001796
1797 while (put_count--)
1798 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1799
1800 /**
1801 * Wait for GPU, then move to system cached.
1802 */
1803
1804 spin_lock(&bo->lock);
1805 ret = ttm_bo_wait(bo, false, false, false);
1806 spin_unlock(&bo->lock);
1807
1808 if (unlikely(ret != 0))
1809 goto out;
1810
1811 if ((bo->mem.placement & swap_placement) != swap_placement) {
1812 struct ttm_mem_reg evict_mem;
1813
1814 evict_mem = bo->mem;
1815 evict_mem.mm_node = NULL;
1816 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1817 evict_mem.mem_type = TTM_PL_SYSTEM;
1818
1819 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001820 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001821 if (unlikely(ret != 0))
1822 goto out;
1823 }
1824
1825 ttm_bo_unmap_virtual(bo);
1826
1827 /**
1828 * Swap out. Buffer will be swapped in again as soon as
1829 * anyone tries to access a ttm page.
1830 */
1831
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001832 if (bo->bdev->driver->swap_notify)
1833 bo->bdev->driver->swap_notify(bo);
1834
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001835 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1836out:
1837
1838 /**
1839 *
1840 * Unreserve without putting on LRU to avoid swapping out an
1841 * already swapped buffer.
1842 */
1843
1844 atomic_set(&bo->reserved, 0);
1845 wake_up_all(&bo->event_queue);
1846 kref_put(&bo->list_kref, ttm_bo_release_list);
1847 return ret;
1848}
1849
1850void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1851{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001852 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001853 ;
1854}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001855EXPORT_SYMBOL(ttm_bo_swapout_all);