blob: 0e3754a3a303c18391538180b702a2b0e4c66399 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
Jerome Glisseca262a9992009-12-08 15:33:32 +010030/* Notes:
31 *
32 * We store bo pointer in drm_mm_node struct so we know which bo own a
33 * specific node. There is no protection on the pointer, thus to make
34 * sure things don't go berserk you have to access this pointer while
35 * holding the global lru lock and make sure anytime you free a node you
36 * reset the pointer to NULL.
37 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020038
39#include "ttm/ttm_module.h"
40#include "ttm/ttm_bo_driver.h"
41#include "ttm/ttm_placement.h"
42#include <linux/jiffies.h>
43#include <linux/slab.h>
44#include <linux/sched.h>
45#include <linux/mm.h>
46#include <linux/file.h>
47#include <linux/module.h>
48
49#define TTM_ASSERT_LOCKED(param)
50#define TTM_DEBUG(fmt, arg...)
51#define TTM_BO_HASH_ORDER 13
52
53static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020054static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020055static void ttm_bo_global_kobj_release(struct kobject *kobj);
56
57static struct attribute ttm_bo_count = {
58 .name = "bo_count",
59 .mode = S_IRUGO
60};
61
Jerome Glissefb53f862009-12-09 21:55:10 +010062static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
63{
64 int i;
65
66 for (i = 0; i <= TTM_PL_PRIV5; i++)
67 if (flags & (1 << i)) {
68 *mem_type = i;
69 return 0;
70 }
71 return -EINVAL;
72}
73
Jerome Glisse5012f502009-12-10 18:07:26 +010074static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010075{
Jerome Glisse5012f502009-12-10 18:07:26 +010076 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
77
Jerome Glissefb53f862009-12-09 21:55:10 +010078 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
79 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
80 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
81 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
82 printk(KERN_ERR TTM_PFX " io_offset: 0x%08lX\n", man->io_offset);
83 printk(KERN_ERR TTM_PFX " io_size: %ld\n", man->io_size);
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010084 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
Jerome Glissefb53f862009-12-09 21:55:10 +010085 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
86 man->available_caching);
87 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
88 man->default_caching);
Jerome Glisse5012f502009-12-10 18:07:26 +010089 if (mem_type != TTM_PL_SYSTEM) {
90 spin_lock(&bdev->glob->lru_lock);
91 drm_mm_debug_table(&man->manager, TTM_PFX);
92 spin_unlock(&bdev->glob->lru_lock);
93 }
Jerome Glissefb53f862009-12-09 21:55:10 +010094}
95
96static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
97 struct ttm_placement *placement)
98{
Jerome Glissefb53f862009-12-09 21:55:10 +010099 int i, ret, mem_type;
100
Jerome Glisseeb6d2c32009-12-10 16:15:52 +0100101 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
Jerome Glissefb53f862009-12-09 21:55:10 +0100102 bo, bo->mem.num_pages, bo->mem.size >> 10,
103 bo->mem.size >> 20);
104 for (i = 0; i < placement->num_placement; i++) {
105 ret = ttm_mem_type_from_flags(placement->placement[i],
106 &mem_type);
107 if (ret)
108 return;
Jerome Glissefb53f862009-12-09 21:55:10 +0100109 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
110 i, placement->placement[i], mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +0100111 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100112 }
113}
114
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200115static ssize_t ttm_bo_global_show(struct kobject *kobj,
116 struct attribute *attr,
117 char *buffer)
118{
119 struct ttm_bo_global *glob =
120 container_of(kobj, struct ttm_bo_global, kobj);
121
122 return snprintf(buffer, PAGE_SIZE, "%lu\n",
123 (unsigned long) atomic_read(&glob->bo_count));
124}
125
126static struct attribute *ttm_bo_global_attrs[] = {
127 &ttm_bo_count,
128 NULL
129};
130
Emese Revfy52cf25d2010-01-19 02:58:23 +0100131static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200132 .show = &ttm_bo_global_show
133};
134
135static struct kobj_type ttm_bo_glob_kobj_type = {
136 .release = &ttm_bo_global_kobj_release,
137 .sysfs_ops = &ttm_bo_global_ops,
138 .default_attrs = ttm_bo_global_attrs
139};
140
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200141
142static inline uint32_t ttm_bo_type_flags(unsigned type)
143{
144 return 1 << (type);
145}
146
147static void ttm_bo_release_list(struct kref *list_kref)
148{
149 struct ttm_buffer_object *bo =
150 container_of(list_kref, struct ttm_buffer_object, list_kref);
151 struct ttm_bo_device *bdev = bo->bdev;
152
153 BUG_ON(atomic_read(&bo->list_kref.refcount));
154 BUG_ON(atomic_read(&bo->kref.refcount));
155 BUG_ON(atomic_read(&bo->cpu_writers));
156 BUG_ON(bo->sync_obj != NULL);
157 BUG_ON(bo->mem.mm_node != NULL);
158 BUG_ON(!list_empty(&bo->lru));
159 BUG_ON(!list_empty(&bo->ddestroy));
160
161 if (bo->ttm)
162 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200163 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164 if (bo->destroy)
165 bo->destroy(bo);
166 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200167 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200168 kfree(bo);
169 }
170}
171
172int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
173{
174
175 if (interruptible) {
176 int ret = 0;
177
178 ret = wait_event_interruptible(bo->event_queue,
179 atomic_read(&bo->reserved) == 0);
180 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100181 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200182 } else {
183 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
184 }
185 return 0;
186}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000187EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200188
189static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
190{
191 struct ttm_bo_device *bdev = bo->bdev;
192 struct ttm_mem_type_manager *man;
193
194 BUG_ON(!atomic_read(&bo->reserved));
195
196 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
197
198 BUG_ON(!list_empty(&bo->lru));
199
200 man = &bdev->man[bo->mem.mem_type];
201 list_add_tail(&bo->lru, &man->lru);
202 kref_get(&bo->list_kref);
203
204 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200205 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200206 kref_get(&bo->list_kref);
207 }
208 }
209}
210
211/**
212 * Call with the lru_lock held.
213 */
214
215static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
216{
217 int put_count = 0;
218
219 if (!list_empty(&bo->swap)) {
220 list_del_init(&bo->swap);
221 ++put_count;
222 }
223 if (!list_empty(&bo->lru)) {
224 list_del_init(&bo->lru);
225 ++put_count;
226 }
227
228 /*
229 * TODO: Add a driver hook to delete from
230 * driver-specific LRU's here.
231 */
232
233 return put_count;
234}
235
236int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
237 bool interruptible,
238 bool no_wait, bool use_sequence, uint32_t sequence)
239{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200240 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200241 int ret;
242
243 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
244 if (use_sequence && bo->seq_valid &&
245 (sequence - bo->val_seq < (1 << 31))) {
246 return -EAGAIN;
247 }
248
249 if (no_wait)
250 return -EBUSY;
251
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200252 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200253 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200254 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200255
256 if (unlikely(ret))
257 return ret;
258 }
259
260 if (use_sequence) {
261 bo->val_seq = sequence;
262 bo->seq_valid = true;
263 } else {
264 bo->seq_valid = false;
265 }
266
267 return 0;
268}
269EXPORT_SYMBOL(ttm_bo_reserve);
270
271static void ttm_bo_ref_bug(struct kref *list_kref)
272{
273 BUG();
274}
275
276int ttm_bo_reserve(struct ttm_buffer_object *bo,
277 bool interruptible,
278 bool no_wait, bool use_sequence, uint32_t sequence)
279{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200280 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200281 int put_count = 0;
282 int ret;
283
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200284 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200285 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
286 sequence);
287 if (likely(ret == 0))
288 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200289 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200290
291 while (put_count--)
292 kref_put(&bo->list_kref, ttm_bo_ref_bug);
293
294 return ret;
295}
296
297void ttm_bo_unreserve(struct ttm_buffer_object *bo)
298{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200299 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200300
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200301 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200302 ttm_bo_add_to_lru(bo);
303 atomic_set(&bo->reserved, 0);
304 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200305 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200306}
307EXPORT_SYMBOL(ttm_bo_unreserve);
308
309/*
310 * Call bo->mutex locked.
311 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200312static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
313{
314 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200315 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200316 int ret = 0;
317 uint32_t page_flags = 0;
318
319 TTM_ASSERT_LOCKED(&bo->mutex);
320 bo->ttm = NULL;
321
Dave Airliead49f502009-07-10 22:36:26 +1000322 if (bdev->need_dma32)
323 page_flags |= TTM_PAGE_FLAG_DMA32;
324
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200325 switch (bo->type) {
326 case ttm_bo_type_device:
327 if (zero_alloc)
328 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
329 case ttm_bo_type_kernel:
330 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200331 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200332 if (unlikely(bo->ttm == NULL))
333 ret = -ENOMEM;
334 break;
335 case ttm_bo_type_user:
336 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
337 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200338 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000339 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200340 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000341 break;
342 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200343
344 ret = ttm_tt_set_user(bo->ttm, current,
345 bo->buffer_start, bo->num_pages);
346 if (unlikely(ret != 0))
347 ttm_tt_destroy(bo->ttm);
348 break;
349 default:
350 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
351 ret = -EINVAL;
352 break;
353 }
354
355 return ret;
356}
357
358static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
359 struct ttm_mem_reg *mem,
360 bool evict, bool interruptible, bool no_wait)
361{
362 struct ttm_bo_device *bdev = bo->bdev;
363 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
364 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
365 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
366 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
367 int ret = 0;
368
369 if (old_is_pci || new_is_pci ||
370 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
371 ttm_bo_unmap_virtual(bo);
372
373 /*
374 * Create and bind a ttm if required.
375 */
376
377 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
378 ret = ttm_bo_add_ttm(bo, false);
379 if (ret)
380 goto out_err;
381
382 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
383 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200384 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200385
386 if (mem->mem_type != TTM_PL_SYSTEM) {
387 ret = ttm_tt_bind(bo->ttm, mem);
388 if (ret)
389 goto out_err;
390 }
391
392 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100393 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200394 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200395 goto moved;
396 }
397
398 }
399
Dave Airliee024e112009-06-24 09:48:08 +1000400 if (bdev->driver->move_notify)
401 bdev->driver->move_notify(bo, mem);
402
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200403 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
404 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
405 ret = ttm_bo_move_ttm(bo, evict, no_wait, mem);
406 else if (bdev->driver->move)
407 ret = bdev->driver->move(bo, evict, interruptible,
408 no_wait, mem);
409 else
410 ret = ttm_bo_move_memcpy(bo, evict, no_wait, mem);
411
412 if (ret)
413 goto out_err;
414
415moved:
416 if (bo->evicted) {
417 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
418 if (ret)
419 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
420 bo->evicted = false;
421 }
422
423 if (bo->mem.mm_node) {
424 spin_lock(&bo->lock);
425 bo->offset = (bo->mem.mm_node->start << PAGE_SHIFT) +
426 bdev->man[bo->mem.mem_type].gpu_offset;
427 bo->cur_placement = bo->mem.placement;
428 spin_unlock(&bo->lock);
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100429 } else
430 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200431
432 return 0;
433
434out_err:
435 new_man = &bdev->man[bo->mem.mem_type];
436 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
437 ttm_tt_unbind(bo->ttm);
438 ttm_tt_destroy(bo->ttm);
439 bo->ttm = NULL;
440 }
441
442 return ret;
443}
444
445/**
446 * If bo idle, remove from delayed- and lru lists, and unref.
447 * If not idle, and already on delayed list, do nothing.
448 * If not idle, and not on delayed list, put on delayed list,
449 * up the list_kref and schedule a delayed list check.
450 */
451
452static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
453{
454 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200455 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200456 struct ttm_bo_driver *driver = bdev->driver;
457 int ret;
458
459 spin_lock(&bo->lock);
460 (void) ttm_bo_wait(bo, false, false, !remove_all);
461
462 if (!bo->sync_obj) {
463 int put_count;
464
465 spin_unlock(&bo->lock);
466
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200467 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100468 put_count = ttm_bo_del_from_lru(bo);
469
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200470 ret = ttm_bo_reserve_locked(bo, false, false, false, 0);
471 BUG_ON(ret);
472 if (bo->ttm)
473 ttm_tt_unbind(bo->ttm);
474
475 if (!list_empty(&bo->ddestroy)) {
476 list_del_init(&bo->ddestroy);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100477 ++put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200478 }
479 if (bo->mem.mm_node) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100480 bo->mem.mm_node->private = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200481 drm_mm_put_block(bo->mem.mm_node);
482 bo->mem.mm_node = NULL;
483 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200484 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200485
486 atomic_set(&bo->reserved, 0);
487
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
491 return 0;
492 }
493
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200494 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200495 if (list_empty(&bo->ddestroy)) {
496 void *sync_obj = bo->sync_obj;
497 void *sync_obj_arg = bo->sync_obj_arg;
498
499 kref_get(&bo->list_kref);
500 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200501 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200502 spin_unlock(&bo->lock);
503
504 if (sync_obj)
505 driver->sync_obj_flush(sync_obj, sync_obj_arg);
506 schedule_delayed_work(&bdev->wq,
507 ((HZ / 100) < 1) ? 1 : HZ / 100);
508 ret = 0;
509
510 } else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200511 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200512 spin_unlock(&bo->lock);
513 ret = -EBUSY;
514 }
515
516 return ret;
517}
518
519/**
520 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
521 * encountered buffers.
522 */
523
524static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
525{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200526 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100527 struct ttm_buffer_object *entry = NULL;
528 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200529
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200530 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100531 if (list_empty(&bdev->ddestroy))
532 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200533
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100534 entry = list_first_entry(&bdev->ddestroy,
535 struct ttm_buffer_object, ddestroy);
536 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200537
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100538 for (;;) {
539 struct ttm_buffer_object *nentry = NULL;
540
541 if (entry->ddestroy.next != &bdev->ddestroy) {
542 nentry = list_first_entry(&entry->ddestroy,
543 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200544 kref_get(&nentry->list_kref);
545 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200546
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200547 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200548 ret = ttm_bo_cleanup_refs(entry, remove_all);
549 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100550 entry = nentry;
551
552 if (ret || !entry)
553 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200554
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200555 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100556 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200557 break;
558 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200559
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100560out_unlock:
561 spin_unlock(&glob->lru_lock);
562out:
563 if (entry)
564 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200565 return ret;
566}
567
568static void ttm_bo_delayed_workqueue(struct work_struct *work)
569{
570 struct ttm_bo_device *bdev =
571 container_of(work, struct ttm_bo_device, wq.work);
572
573 if (ttm_bo_delayed_delete(bdev, false)) {
574 schedule_delayed_work(&bdev->wq,
575 ((HZ / 100) < 1) ? 1 : HZ / 100);
576 }
577}
578
579static void ttm_bo_release(struct kref *kref)
580{
581 struct ttm_buffer_object *bo =
582 container_of(kref, struct ttm_buffer_object, kref);
583 struct ttm_bo_device *bdev = bo->bdev;
584
585 if (likely(bo->vm_node != NULL)) {
586 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
587 drm_mm_put_block(bo->vm_node);
588 bo->vm_node = NULL;
589 }
590 write_unlock(&bdev->vm_lock);
591 ttm_bo_cleanup_refs(bo, false);
592 kref_put(&bo->list_kref, ttm_bo_release_list);
593 write_lock(&bdev->vm_lock);
594}
595
596void ttm_bo_unref(struct ttm_buffer_object **p_bo)
597{
598 struct ttm_buffer_object *bo = *p_bo;
599 struct ttm_bo_device *bdev = bo->bdev;
600
601 *p_bo = NULL;
602 write_lock(&bdev->vm_lock);
603 kref_put(&bo->kref, ttm_bo_release);
604 write_unlock(&bdev->vm_lock);
605}
606EXPORT_SYMBOL(ttm_bo_unref);
607
Jerome Glisseca262a9992009-12-08 15:33:32 +0100608static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
609 bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200610{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200611 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200612 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200613 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100614 struct ttm_placement placement;
615 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616
617 spin_lock(&bo->lock);
618 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
619 spin_unlock(&bo->lock);
620
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200621 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100622 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200623 printk(KERN_ERR TTM_PFX
624 "Failed to expire sync object before "
625 "buffer eviction.\n");
626 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200627 goto out;
628 }
629
630 BUG_ON(!atomic_read(&bo->reserved));
631
632 evict_mem = bo->mem;
633 evict_mem.mm_node = NULL;
634
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100635 placement.fpfn = 0;
636 placement.lpfn = 0;
637 placement.num_placement = 0;
638 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100639 bdev->driver->evict_flags(bo, &placement);
640 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
641 no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200642 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100643 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200644 printk(KERN_ERR TTM_PFX
645 "Failed to find memory space for "
646 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100647 ttm_bo_mem_space_debug(bo, &placement);
648 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649 goto out;
650 }
651
652 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
653 no_wait);
654 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100655 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +0100657 spin_lock(&glob->lru_lock);
658 if (evict_mem.mm_node) {
659 evict_mem.mm_node->private = NULL;
660 drm_mm_put_block(evict_mem.mm_node);
661 evict_mem.mm_node = NULL;
662 }
663 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200664 goto out;
665 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200666 bo->evicted = true;
667out:
668 return ret;
669}
670
Jerome Glisseca262a9992009-12-08 15:33:32 +0100671static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
672 uint32_t mem_type,
673 bool interruptible, bool no_wait)
674{
675 struct ttm_bo_global *glob = bdev->glob;
676 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
677 struct ttm_buffer_object *bo;
678 int ret, put_count = 0;
679
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100680retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100681 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100682 if (list_empty(&man->lru)) {
683 spin_unlock(&glob->lru_lock);
684 return -EBUSY;
685 }
686
Jerome Glisseca262a9992009-12-08 15:33:32 +0100687 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
688 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100689
690 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
691
692 if (unlikely(ret == -EBUSY)) {
693 spin_unlock(&glob->lru_lock);
694 if (likely(!no_wait))
695 ret = ttm_bo_wait_unreserved(bo, interruptible);
696
697 kref_put(&bo->list_kref, ttm_bo_release_list);
698
699 /**
700 * We *need* to retry after releasing the lru lock.
701 */
702
703 if (unlikely(ret != 0))
704 return ret;
705 goto retry;
706 }
707
708 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100709 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100710
711 BUG_ON(ret != 0);
712
Jerome Glisseca262a9992009-12-08 15:33:32 +0100713 while (put_count--)
714 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100715
Jerome Glisseca262a9992009-12-08 15:33:32 +0100716 ret = ttm_bo_evict(bo, interruptible, no_wait);
717 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100718
Jerome Glisseca262a9992009-12-08 15:33:32 +0100719 kref_put(&bo->list_kref, ttm_bo_release_list);
720 return ret;
721}
722
723static int ttm_bo_man_get_node(struct ttm_buffer_object *bo,
724 struct ttm_mem_type_manager *man,
725 struct ttm_placement *placement,
726 struct ttm_mem_reg *mem,
727 struct drm_mm_node **node)
728{
729 struct ttm_bo_global *glob = bo->glob;
730 unsigned long lpfn;
731 int ret;
732
733 lpfn = placement->lpfn;
734 if (!lpfn)
735 lpfn = man->size;
736 *node = NULL;
737 do {
738 ret = drm_mm_pre_get(&man->manager);
739 if (unlikely(ret))
740 return ret;
741
742 spin_lock(&glob->lru_lock);
743 *node = drm_mm_search_free_in_range(&man->manager,
744 mem->num_pages, mem->page_alignment,
745 placement->fpfn, lpfn, 1);
746 if (unlikely(*node == NULL)) {
747 spin_unlock(&glob->lru_lock);
748 return 0;
749 }
750 *node = drm_mm_get_block_atomic_range(*node, mem->num_pages,
751 mem->page_alignment,
752 placement->fpfn,
753 lpfn);
754 spin_unlock(&glob->lru_lock);
755 } while (*node == NULL);
756 return 0;
757}
758
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200759/**
760 * Repeatedly evict memory from the LRU for @mem_type until we create enough
761 * space, or we've evicted everything and there isn't enough space.
762 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100763static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
764 uint32_t mem_type,
765 struct ttm_placement *placement,
766 struct ttm_mem_reg *mem,
767 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200768{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100769 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200770 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200771 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Jerome Glisseca262a9992009-12-08 15:33:32 +0100772 struct drm_mm_node *node;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200773 int ret;
774
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200775 do {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100776 ret = ttm_bo_man_get_node(bo, man, placement, mem, &node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200777 if (unlikely(ret != 0))
778 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100779 if (node)
780 break;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200781 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100782 if (list_empty(&man->lru)) {
783 spin_unlock(&glob->lru_lock);
784 break;
785 }
786 spin_unlock(&glob->lru_lock);
787 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
788 no_wait);
789 if (unlikely(ret != 0))
790 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200791 } while (1);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100792 if (node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200793 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200794 mem->mm_node = node;
795 mem->mem_type = mem_type;
796 return 0;
797}
798
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200799static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
800 uint32_t cur_placement,
801 uint32_t proposed_placement)
802{
803 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
804 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
805
806 /**
807 * Keep current caching if possible.
808 */
809
810 if ((cur_placement & caching) != 0)
811 result |= (cur_placement & caching);
812 else if ((man->default_caching & caching) != 0)
813 result |= man->default_caching;
814 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
815 result |= TTM_PL_FLAG_CACHED;
816 else if ((TTM_PL_FLAG_WC & caching) != 0)
817 result |= TTM_PL_FLAG_WC;
818 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
819 result |= TTM_PL_FLAG_UNCACHED;
820
821 return result;
822}
823
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200824static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
825 bool disallow_fixed,
826 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200827 uint32_t proposed_placement,
828 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200829{
830 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
831
832 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
833 return false;
834
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200835 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200836 return false;
837
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200838 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200839 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200840
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200841 cur_flags |= (proposed_placement & man->available_caching);
842
843 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200844 return true;
845}
846
847/**
848 * Creates space for memory region @mem according to its type.
849 *
850 * This function first searches for free space in compatible memory types in
851 * the priority order defined by the driver. If free space isn't found, then
852 * ttm_bo_mem_force_space is attempted in priority order to evict and find
853 * space.
854 */
855int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100856 struct ttm_placement *placement,
857 struct ttm_mem_reg *mem,
858 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200859{
860 struct ttm_bo_device *bdev = bo->bdev;
861 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200862 uint32_t mem_type = TTM_PL_SYSTEM;
863 uint32_t cur_flags = 0;
864 bool type_found = false;
865 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100866 bool has_erestartsys = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867 struct drm_mm_node *node = NULL;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100868 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200869
870 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000871 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100872 ret = ttm_mem_type_from_flags(placement->placement[i],
873 &mem_type);
874 if (ret)
875 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200876 man = &bdev->man[mem_type];
877
878 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100879 bo->type == ttm_bo_type_user,
880 mem_type,
881 placement->placement[i],
882 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200883
884 if (!type_ok)
885 continue;
886
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200887 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
888 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100889 /*
890 * Use the access and other non-mapping-related flag bits from
891 * the memory placement flags to the current flags
892 */
893 ttm_flag_masked(&cur_flags, placement->placement[i],
894 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200895
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200896 if (mem_type == TTM_PL_SYSTEM)
897 break;
898
899 if (man->has_type && man->use_type) {
900 type_found = true;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100901 ret = ttm_bo_man_get_node(bo, man, placement, mem,
902 &node);
903 if (unlikely(ret))
904 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200905 }
906 if (node)
907 break;
908 }
909
910 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) {
911 mem->mm_node = node;
912 mem->mem_type = mem_type;
913 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100914 if (node)
915 node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200916 return 0;
917 }
918
919 if (!type_found)
920 return -EINVAL;
921
Dave Airlieb6637522009-12-14 14:51:35 +1000922 for (i = 0; i < placement->num_busy_placement; ++i) {
923 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100924 &mem_type);
925 if (ret)
926 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200927 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200928 if (!man->has_type)
929 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200930 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100931 bo->type == ttm_bo_type_user,
932 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000933 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100934 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200935 continue;
936
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200937 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
938 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100939 /*
940 * Use the access and other non-mapping-related flag bits from
941 * the memory placement flags to the current flags
942 */
Dave Airlieb6637522009-12-14 14:51:35 +1000943 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100944 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200945
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100946
947 if (mem_type == TTM_PL_SYSTEM) {
948 mem->mem_type = mem_type;
949 mem->placement = cur_flags;
950 mem->mm_node = NULL;
951 return 0;
952 }
953
Jerome Glisseca262a9992009-12-08 15:33:32 +0100954 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
955 interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200956 if (ret == 0 && mem->mm_node) {
957 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100958 mem->mm_node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200959 return 0;
960 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100961 if (ret == -ERESTARTSYS)
962 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200963 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100964 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200965 return ret;
966}
967EXPORT_SYMBOL(ttm_bo_mem_space);
968
969int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
970{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200971 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
972 return -EBUSY;
973
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100974 return wait_event_interruptible(bo->event_queue,
975 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000977EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200978
979int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100980 struct ttm_placement *placement,
981 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200982{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200983 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200984 int ret = 0;
985 struct ttm_mem_reg mem;
986
987 BUG_ON(!atomic_read(&bo->reserved));
988
989 /*
990 * FIXME: It's possible to pipeline buffer moves.
991 * Have the driver move function wait for idle when necessary,
992 * instead of doing it here.
993 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200994 spin_lock(&bo->lock);
995 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
996 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997 if (ret)
998 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200999 mem.num_pages = bo->num_pages;
1000 mem.size = mem.num_pages << PAGE_SHIFT;
1001 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001002 /*
1003 * Determine where to move the buffer.
1004 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001005 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006 if (ret)
1007 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001008 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001009out_unlock:
1010 if (ret && mem.mm_node) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001011 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001012 mem.mm_node->private = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 drm_mm_put_block(mem.mm_node);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001014 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 }
1016 return ret;
1017}
1018
Jerome Glisseca262a9992009-12-08 15:33:32 +01001019static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001020 struct ttm_mem_reg *mem)
1021{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001022 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001023 struct drm_mm_node *node = mem->mm_node;
1024
1025 if (node && placement->lpfn != 0 &&
1026 (node->start < placement->fpfn ||
1027 node->start + node->size > placement->lpfn))
1028 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001029
Jerome Glisseca262a9992009-12-08 15:33:32 +01001030 for (i = 0; i < placement->num_placement; i++) {
1031 if ((placement->placement[i] & mem->placement &
1032 TTM_PL_MASK_CACHING) &&
1033 (placement->placement[i] & mem->placement &
1034 TTM_PL_MASK_MEM))
1035 return i;
1036 }
1037 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001038}
1039
Jerome Glisse09855ac2009-12-10 17:16:27 +01001040int ttm_bo_validate(struct ttm_buffer_object *bo,
1041 struct ttm_placement *placement,
1042 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001043{
1044 int ret;
1045
1046 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001047 /* Check that range is valid */
1048 if (placement->lpfn || placement->fpfn)
1049 if (placement->fpfn > placement->lpfn ||
1050 (placement->lpfn - placement->fpfn) < bo->num_pages)
1051 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052 /*
1053 * Check whether we need to move buffer.
1054 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001055 ret = ttm_bo_mem_compat(placement, &bo->mem);
1056 if (ret < 0) {
1057 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait);
1058 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001059 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001060 } else {
1061 /*
1062 * Use the access and other non-mapping-related flag bits from
1063 * the compatible memory placement flags to the active flags
1064 */
1065 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1066 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001067 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001068 /*
1069 * We might need to add a TTM.
1070 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001071 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1072 ret = ttm_bo_add_ttm(bo, true);
1073 if (ret)
1074 return ret;
1075 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001076 return 0;
1077}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001078EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001079
Jerome Glisse09855ac2009-12-10 17:16:27 +01001080int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1081 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001083 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001084
Jerome Glisse09855ac2009-12-10 17:16:27 +01001085 if (placement->fpfn || placement->lpfn) {
1086 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1087 printk(KERN_ERR TTM_PFX "Page number range to small "
1088 "Need %lu pages, range is [%u, %u]\n",
1089 bo->mem.num_pages, placement->fpfn,
1090 placement->lpfn);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001091 return -EINVAL;
1092 }
Jerome Glisse09855ac2009-12-10 17:16:27 +01001093 }
1094 for (i = 0; i < placement->num_placement; i++) {
1095 if (!capable(CAP_SYS_ADMIN)) {
1096 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1097 printk(KERN_ERR TTM_PFX "Need to be root to "
1098 "modify NO_EVICT status.\n");
1099 return -EINVAL;
1100 }
1101 }
1102 }
1103 for (i = 0; i < placement->num_busy_placement; i++) {
1104 if (!capable(CAP_SYS_ADMIN)) {
1105 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1106 printk(KERN_ERR TTM_PFX "Need to be root to "
1107 "modify NO_EVICT status.\n");
1108 return -EINVAL;
1109 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110 }
1111 }
1112 return 0;
1113}
1114
Jerome Glisse09855ac2009-12-10 17:16:27 +01001115int ttm_bo_init(struct ttm_bo_device *bdev,
1116 struct ttm_buffer_object *bo,
1117 unsigned long size,
1118 enum ttm_bo_type type,
1119 struct ttm_placement *placement,
1120 uint32_t page_alignment,
1121 unsigned long buffer_start,
1122 bool interruptible,
1123 struct file *persistant_swap_storage,
1124 size_t acc_size,
1125 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001126{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001127 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001128 unsigned long num_pages;
1129
1130 size += buffer_start & ~PAGE_MASK;
1131 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1132 if (num_pages == 0) {
1133 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1134 return -EINVAL;
1135 }
1136 bo->destroy = destroy;
1137
1138 spin_lock_init(&bo->lock);
1139 kref_init(&bo->kref);
1140 kref_init(&bo->list_kref);
1141 atomic_set(&bo->cpu_writers, 0);
1142 atomic_set(&bo->reserved, 1);
1143 init_waitqueue_head(&bo->event_queue);
1144 INIT_LIST_HEAD(&bo->lru);
1145 INIT_LIST_HEAD(&bo->ddestroy);
1146 INIT_LIST_HEAD(&bo->swap);
1147 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001148 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149 bo->type = type;
1150 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001151 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152 bo->mem.mem_type = TTM_PL_SYSTEM;
1153 bo->mem.num_pages = bo->num_pages;
1154 bo->mem.mm_node = NULL;
1155 bo->mem.page_alignment = page_alignment;
1156 bo->buffer_start = buffer_start & PAGE_MASK;
1157 bo->priv_flags = 0;
1158 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1159 bo->seq_valid = false;
1160 bo->persistant_swap_storage = persistant_swap_storage;
1161 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001162 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001163
Jerome Glisse09855ac2009-12-10 17:16:27 +01001164 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001165 if (unlikely(ret != 0))
1166 goto out_err;
1167
1168 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001169 * For ttm_bo_type_device buffers, allocate
1170 * address space from the device.
1171 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001172 if (bo->type == ttm_bo_type_device) {
1173 ret = ttm_bo_setup_vm(bo);
1174 if (ret)
1175 goto out_err;
1176 }
1177
Jerome Glisse09855ac2009-12-10 17:16:27 +01001178 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 if (ret)
1180 goto out_err;
1181
1182 ttm_bo_unreserve(bo);
1183 return 0;
1184
1185out_err:
1186 ttm_bo_unreserve(bo);
1187 ttm_bo_unref(&bo);
1188
1189 return ret;
1190}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001191EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001192
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001193static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001194 unsigned long num_pages)
1195{
1196 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1197 PAGE_MASK;
1198
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001199 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200}
1201
Jerome Glisse09855ac2009-12-10 17:16:27 +01001202int ttm_bo_create(struct ttm_bo_device *bdev,
1203 unsigned long size,
1204 enum ttm_bo_type type,
1205 struct ttm_placement *placement,
1206 uint32_t page_alignment,
1207 unsigned long buffer_start,
1208 bool interruptible,
1209 struct file *persistant_swap_storage,
1210 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001211{
1212 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001213 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001214 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215
1216 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001217 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001218 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219 if (unlikely(ret != 0))
1220 return ret;
1221
1222 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1223
1224 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001225 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001226 return -ENOMEM;
1227 }
1228
Jerome Glisse09855ac2009-12-10 17:16:27 +01001229 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1230 buffer_start, interruptible,
1231 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001232 if (likely(ret == 0))
1233 *p_bo = bo;
1234
1235 return ret;
1236}
1237
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001238static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001239 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001240{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001241 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001242 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001243 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001244
1245 /*
1246 * Can't use standard list traversal since we're unlocking.
1247 */
1248
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001249 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001250 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001251 spin_unlock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001252 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1253 if (ret) {
1254 if (allow_errors) {
1255 return ret;
1256 } else {
1257 printk(KERN_ERR TTM_PFX
1258 "Cleanup eviction failed\n");
1259 }
1260 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001261 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001262 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001263 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001264 return 0;
1265}
1266
1267int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1268{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001269 struct ttm_bo_global *glob = bdev->glob;
Roel Kluinc96e7c72009-08-03 14:22:53 +02001270 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271 int ret = -EINVAL;
1272
1273 if (mem_type >= TTM_NUM_MEM_TYPES) {
1274 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1275 return ret;
1276 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001277 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001278
1279 if (!man->has_type) {
1280 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1281 "memory manager type %u\n", mem_type);
1282 return ret;
1283 }
1284
1285 man->use_type = false;
1286 man->has_type = false;
1287
1288 ret = 0;
1289 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001290 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001292 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001293 if (drm_mm_clean(&man->manager))
1294 drm_mm_takedown(&man->manager);
1295 else
1296 ret = -EBUSY;
1297
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001298 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299 }
1300
1301 return ret;
1302}
1303EXPORT_SYMBOL(ttm_bo_clean_mm);
1304
1305int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1306{
1307 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1308
1309 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1310 printk(KERN_ERR TTM_PFX
1311 "Illegal memory manager memory type %u.\n",
1312 mem_type);
1313 return -EINVAL;
1314 }
1315
1316 if (!man->has_type) {
1317 printk(KERN_ERR TTM_PFX
1318 "Memory type %u has not been initialized.\n",
1319 mem_type);
1320 return 0;
1321 }
1322
Jerome Glisseca262a9992009-12-08 15:33:32 +01001323 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001324}
1325EXPORT_SYMBOL(ttm_bo_evict_mm);
1326
1327int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001328 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001329{
1330 int ret = -EINVAL;
1331 struct ttm_mem_type_manager *man;
1332
1333 if (type >= TTM_NUM_MEM_TYPES) {
1334 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1335 return ret;
1336 }
1337
1338 man = &bdev->man[type];
1339 if (man->has_type) {
1340 printk(KERN_ERR TTM_PFX
1341 "Memory manager already initialized for type %d\n",
1342 type);
1343 return ret;
1344 }
1345
1346 ret = bdev->driver->init_mem_type(bdev, type, man);
1347 if (ret)
1348 return ret;
1349
1350 ret = 0;
1351 if (type != TTM_PL_SYSTEM) {
1352 if (!p_size) {
1353 printk(KERN_ERR TTM_PFX
1354 "Zero size memory manager type %d\n",
1355 type);
1356 return ret;
1357 }
Jerome Glisseca262a9992009-12-08 15:33:32 +01001358 ret = drm_mm_init(&man->manager, 0, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001359 if (ret)
1360 return ret;
1361 }
1362 man->has_type = true;
1363 man->use_type = true;
1364 man->size = p_size;
1365
1366 INIT_LIST_HEAD(&man->lru);
1367
1368 return 0;
1369}
1370EXPORT_SYMBOL(ttm_bo_init_mm);
1371
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001372static void ttm_bo_global_kobj_release(struct kobject *kobj)
1373{
1374 struct ttm_bo_global *glob =
1375 container_of(kobj, struct ttm_bo_global, kobj);
1376
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001377 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1378 __free_page(glob->dummy_read_page);
1379 kfree(glob);
1380}
1381
1382void ttm_bo_global_release(struct ttm_global_reference *ref)
1383{
1384 struct ttm_bo_global *glob = ref->object;
1385
1386 kobject_del(&glob->kobj);
1387 kobject_put(&glob->kobj);
1388}
1389EXPORT_SYMBOL(ttm_bo_global_release);
1390
1391int ttm_bo_global_init(struct ttm_global_reference *ref)
1392{
1393 struct ttm_bo_global_ref *bo_ref =
1394 container_of(ref, struct ttm_bo_global_ref, ref);
1395 struct ttm_bo_global *glob = ref->object;
1396 int ret;
1397
1398 mutex_init(&glob->device_list_mutex);
1399 spin_lock_init(&glob->lru_lock);
1400 glob->mem_glob = bo_ref->mem_glob;
1401 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1402
1403 if (unlikely(glob->dummy_read_page == NULL)) {
1404 ret = -ENOMEM;
1405 goto out_no_drp;
1406 }
1407
1408 INIT_LIST_HEAD(&glob->swap_lru);
1409 INIT_LIST_HEAD(&glob->device_list);
1410
1411 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1412 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1413 if (unlikely(ret != 0)) {
1414 printk(KERN_ERR TTM_PFX
1415 "Could not register buffer object swapout.\n");
1416 goto out_no_shrink;
1417 }
1418
1419 glob->ttm_bo_extra_size =
1420 ttm_round_pot(sizeof(struct ttm_tt)) +
1421 ttm_round_pot(sizeof(struct ttm_backend));
1422
1423 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1424 ttm_round_pot(sizeof(struct ttm_buffer_object));
1425
1426 atomic_set(&glob->bo_count, 0);
1427
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001428 ret = kobject_init_and_add(
1429 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001430 if (unlikely(ret != 0))
1431 kobject_put(&glob->kobj);
1432 return ret;
1433out_no_shrink:
1434 __free_page(glob->dummy_read_page);
1435out_no_drp:
1436 kfree(glob);
1437 return ret;
1438}
1439EXPORT_SYMBOL(ttm_bo_global_init);
1440
1441
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001442int ttm_bo_device_release(struct ttm_bo_device *bdev)
1443{
1444 int ret = 0;
1445 unsigned i = TTM_NUM_MEM_TYPES;
1446 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001447 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001448
1449 while (i--) {
1450 man = &bdev->man[i];
1451 if (man->has_type) {
1452 man->use_type = false;
1453 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1454 ret = -EBUSY;
1455 printk(KERN_ERR TTM_PFX
1456 "DRM memory manager type %d "
1457 "is not clean.\n", i);
1458 }
1459 man->has_type = false;
1460 }
1461 }
1462
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001463 mutex_lock(&glob->device_list_mutex);
1464 list_del(&bdev->device_list);
1465 mutex_unlock(&glob->device_list_mutex);
1466
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001467 if (!cancel_delayed_work(&bdev->wq))
1468 flush_scheduled_work();
1469
1470 while (ttm_bo_delayed_delete(bdev, true))
1471 ;
1472
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001473 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001474 if (list_empty(&bdev->ddestroy))
1475 TTM_DEBUG("Delayed destroy list was clean\n");
1476
1477 if (list_empty(&bdev->man[0].lru))
1478 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001479 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001480
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001481 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1482 write_lock(&bdev->vm_lock);
1483 drm_mm_takedown(&bdev->addr_space_mm);
1484 write_unlock(&bdev->vm_lock);
1485
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001486 return ret;
1487}
1488EXPORT_SYMBOL(ttm_bo_device_release);
1489
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001490int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001491 struct ttm_bo_global *glob,
1492 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001493 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001494 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001495{
1496 int ret = -EINVAL;
1497
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001498 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001499 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500
1501 memset(bdev->man, 0, sizeof(bdev->man));
1502
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001503 /*
1504 * Initialize the system memory buffer type.
1505 * Other types need to be driver / IOCTL initialized.
1506 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001507 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001508 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001509 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510
1511 bdev->addr_space_rb = RB_ROOT;
1512 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1513 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001514 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001515
1516 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1517 bdev->nice_mode = true;
1518 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001519 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001520 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001521 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001523 mutex_lock(&glob->device_list_mutex);
1524 list_add_tail(&bdev->device_list, &glob->device_list);
1525 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526
1527 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001528out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001530out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531 return ret;
1532}
1533EXPORT_SYMBOL(ttm_bo_device_init);
1534
1535/*
1536 * buffer object vm functions.
1537 */
1538
1539bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1540{
1541 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1542
1543 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1544 if (mem->mem_type == TTM_PL_SYSTEM)
1545 return false;
1546
1547 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1548 return false;
1549
1550 if (mem->placement & TTM_PL_FLAG_CACHED)
1551 return false;
1552 }
1553 return true;
1554}
1555
1556int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
1557 struct ttm_mem_reg *mem,
1558 unsigned long *bus_base,
1559 unsigned long *bus_offset, unsigned long *bus_size)
1560{
1561 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1562
1563 *bus_size = 0;
1564 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1565 return -EINVAL;
1566
1567 if (ttm_mem_reg_is_pci(bdev, mem)) {
1568 *bus_offset = mem->mm_node->start << PAGE_SHIFT;
1569 *bus_size = mem->num_pages << PAGE_SHIFT;
1570 *bus_base = man->io_offset;
1571 }
1572
1573 return 0;
1574}
1575
1576void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1577{
1578 struct ttm_bo_device *bdev = bo->bdev;
1579 loff_t offset = (loff_t) bo->addr_space_offset;
1580 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1581
1582 if (!bdev->dev_mapping)
1583 return;
1584
1585 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1586}
Dave Airliee024e112009-06-24 09:48:08 +10001587EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001588
1589static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1590{
1591 struct ttm_bo_device *bdev = bo->bdev;
1592 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1593 struct rb_node *parent = NULL;
1594 struct ttm_buffer_object *cur_bo;
1595 unsigned long offset = bo->vm_node->start;
1596 unsigned long cur_offset;
1597
1598 while (*cur) {
1599 parent = *cur;
1600 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1601 cur_offset = cur_bo->vm_node->start;
1602 if (offset < cur_offset)
1603 cur = &parent->rb_left;
1604 else if (offset > cur_offset)
1605 cur = &parent->rb_right;
1606 else
1607 BUG();
1608 }
1609
1610 rb_link_node(&bo->vm_rb, parent, cur);
1611 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1612}
1613
1614/**
1615 * ttm_bo_setup_vm:
1616 *
1617 * @bo: the buffer to allocate address space for
1618 *
1619 * Allocate address space in the drm device so that applications
1620 * can mmap the buffer and access the contents. This only
1621 * applies to ttm_bo_type_device objects as others are not
1622 * placed in the drm device address space.
1623 */
1624
1625static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1626{
1627 struct ttm_bo_device *bdev = bo->bdev;
1628 int ret;
1629
1630retry_pre_get:
1631 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1632 if (unlikely(ret != 0))
1633 return ret;
1634
1635 write_lock(&bdev->vm_lock);
1636 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1637 bo->mem.num_pages, 0, 0);
1638
1639 if (unlikely(bo->vm_node == NULL)) {
1640 ret = -ENOMEM;
1641 goto out_unlock;
1642 }
1643
1644 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1645 bo->mem.num_pages, 0);
1646
1647 if (unlikely(bo->vm_node == NULL)) {
1648 write_unlock(&bdev->vm_lock);
1649 goto retry_pre_get;
1650 }
1651
1652 ttm_bo_vm_insert_rb(bo);
1653 write_unlock(&bdev->vm_lock);
1654 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1655
1656 return 0;
1657out_unlock:
1658 write_unlock(&bdev->vm_lock);
1659 return ret;
1660}
1661
1662int ttm_bo_wait(struct ttm_buffer_object *bo,
1663 bool lazy, bool interruptible, bool no_wait)
1664{
1665 struct ttm_bo_driver *driver = bo->bdev->driver;
1666 void *sync_obj;
1667 void *sync_obj_arg;
1668 int ret = 0;
1669
1670 if (likely(bo->sync_obj == NULL))
1671 return 0;
1672
1673 while (bo->sync_obj) {
1674
1675 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1676 void *tmp_obj = bo->sync_obj;
1677 bo->sync_obj = NULL;
1678 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1679 spin_unlock(&bo->lock);
1680 driver->sync_obj_unref(&tmp_obj);
1681 spin_lock(&bo->lock);
1682 continue;
1683 }
1684
1685 if (no_wait)
1686 return -EBUSY;
1687
1688 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1689 sync_obj_arg = bo->sync_obj_arg;
1690 spin_unlock(&bo->lock);
1691 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1692 lazy, interruptible);
1693 if (unlikely(ret != 0)) {
1694 driver->sync_obj_unref(&sync_obj);
1695 spin_lock(&bo->lock);
1696 return ret;
1697 }
1698 spin_lock(&bo->lock);
1699 if (likely(bo->sync_obj == sync_obj &&
1700 bo->sync_obj_arg == sync_obj_arg)) {
1701 void *tmp_obj = bo->sync_obj;
1702 bo->sync_obj = NULL;
1703 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1704 &bo->priv_flags);
1705 spin_unlock(&bo->lock);
1706 driver->sync_obj_unref(&sync_obj);
1707 driver->sync_obj_unref(&tmp_obj);
1708 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001709 } else {
1710 spin_unlock(&bo->lock);
1711 driver->sync_obj_unref(&sync_obj);
1712 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001713 }
1714 }
1715 return 0;
1716}
1717EXPORT_SYMBOL(ttm_bo_wait);
1718
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001719int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1720{
1721 int ret = 0;
1722
1723 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001724 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001725 */
1726
1727 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1728 if (unlikely(ret != 0))
1729 return ret;
1730 spin_lock(&bo->lock);
1731 ret = ttm_bo_wait(bo, false, true, no_wait);
1732 spin_unlock(&bo->lock);
1733 if (likely(ret == 0))
1734 atomic_inc(&bo->cpu_writers);
1735 ttm_bo_unreserve(bo);
1736 return ret;
1737}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001738EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001739
1740void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1741{
1742 if (atomic_dec_and_test(&bo->cpu_writers))
1743 wake_up_all(&bo->event_queue);
1744}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001745EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001746
1747/**
1748 * A buffer object shrink method that tries to swap out the first
1749 * buffer object on the bo_global::swap_lru list.
1750 */
1751
1752static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1753{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001754 struct ttm_bo_global *glob =
1755 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001756 struct ttm_buffer_object *bo;
1757 int ret = -EBUSY;
1758 int put_count;
1759 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1760
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001761 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001762 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001763 if (unlikely(list_empty(&glob->swap_lru))) {
1764 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001765 return -EBUSY;
1766 }
1767
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001768 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001769 struct ttm_buffer_object, swap);
1770 kref_get(&bo->list_kref);
1771
1772 /**
1773 * Reserve buffer. Since we unlock while sleeping, we need
1774 * to re-check that nobody removed us from the swap-list while
1775 * we slept.
1776 */
1777
1778 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1779 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001780 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001781 ttm_bo_wait_unreserved(bo, false);
1782 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001783 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001784 }
1785 }
1786
1787 BUG_ON(ret != 0);
1788 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001789 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001790
1791 while (put_count--)
1792 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1793
1794 /**
1795 * Wait for GPU, then move to system cached.
1796 */
1797
1798 spin_lock(&bo->lock);
1799 ret = ttm_bo_wait(bo, false, false, false);
1800 spin_unlock(&bo->lock);
1801
1802 if (unlikely(ret != 0))
1803 goto out;
1804
1805 if ((bo->mem.placement & swap_placement) != swap_placement) {
1806 struct ttm_mem_reg evict_mem;
1807
1808 evict_mem = bo->mem;
1809 evict_mem.mm_node = NULL;
1810 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1811 evict_mem.mem_type = TTM_PL_SYSTEM;
1812
1813 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1814 false, false);
1815 if (unlikely(ret != 0))
1816 goto out;
1817 }
1818
1819 ttm_bo_unmap_virtual(bo);
1820
1821 /**
1822 * Swap out. Buffer will be swapped in again as soon as
1823 * anyone tries to access a ttm page.
1824 */
1825
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001826 if (bo->bdev->driver->swap_notify)
1827 bo->bdev->driver->swap_notify(bo);
1828
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001829 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1830out:
1831
1832 /**
1833 *
1834 * Unreserve without putting on LRU to avoid swapping out an
1835 * already swapped buffer.
1836 */
1837
1838 atomic_set(&bo->reserved, 0);
1839 wake_up_all(&bo->event_queue);
1840 kref_put(&bo->list_kref, ttm_bo_release_list);
1841 return ret;
1842}
1843
1844void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1845{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001846 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001847 ;
1848}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001849EXPORT_SYMBOL(ttm_bo_swapout_all);