blob: 7927fe99d01705ed130413fcb926d884ccd9c46f [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
74static void ttm_mem_type_manager_debug(struct ttm_bo_global *glob,
75 struct ttm_mem_type_manager *man)
76{
77 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
78 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
79 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
80 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
81 printk(KERN_ERR TTM_PFX " io_offset: 0x%08lX\n", man->io_offset);
82 printk(KERN_ERR TTM_PFX " io_size: %ld\n", man->io_size);
83 printk(KERN_ERR TTM_PFX " size: %ld\n", (unsigned long)man->size);
84 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
85 man->available_caching);
86 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
87 man->default_caching);
88 spin_lock(&glob->lru_lock);
89 drm_mm_debug_table(&man->manager, TTM_PFX);
90 spin_unlock(&glob->lru_lock);
91}
92
93static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
94 struct ttm_placement *placement)
95{
96 struct ttm_bo_device *bdev = bo->bdev;
97 struct ttm_bo_global *glob = bo->glob;
98 struct ttm_mem_type_manager *man;
99 int i, ret, mem_type;
100
101 printk(KERN_ERR TTM_PFX "No space for %p (%ld pages, %ldK, %ldM)\n",
102 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;
109 man = &bdev->man[mem_type];
110 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
111 i, placement->placement[i], mem_type);
112 ttm_mem_type_manager_debug(glob, man);
113 }
114}
115
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200116static ssize_t ttm_bo_global_show(struct kobject *kobj,
117 struct attribute *attr,
118 char *buffer)
119{
120 struct ttm_bo_global *glob =
121 container_of(kobj, struct ttm_bo_global, kobj);
122
123 return snprintf(buffer, PAGE_SIZE, "%lu\n",
124 (unsigned long) atomic_read(&glob->bo_count));
125}
126
127static struct attribute *ttm_bo_global_attrs[] = {
128 &ttm_bo_count,
129 NULL
130};
131
132static struct sysfs_ops ttm_bo_global_ops = {
133 .show = &ttm_bo_global_show
134};
135
136static struct kobj_type ttm_bo_glob_kobj_type = {
137 .release = &ttm_bo_global_kobj_release,
138 .sysfs_ops = &ttm_bo_global_ops,
139 .default_attrs = ttm_bo_global_attrs
140};
141
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200142
143static inline uint32_t ttm_bo_type_flags(unsigned type)
144{
145 return 1 << (type);
146}
147
148static void ttm_bo_release_list(struct kref *list_kref)
149{
150 struct ttm_buffer_object *bo =
151 container_of(list_kref, struct ttm_buffer_object, list_kref);
152 struct ttm_bo_device *bdev = bo->bdev;
153
154 BUG_ON(atomic_read(&bo->list_kref.refcount));
155 BUG_ON(atomic_read(&bo->kref.refcount));
156 BUG_ON(atomic_read(&bo->cpu_writers));
157 BUG_ON(bo->sync_obj != NULL);
158 BUG_ON(bo->mem.mm_node != NULL);
159 BUG_ON(!list_empty(&bo->lru));
160 BUG_ON(!list_empty(&bo->ddestroy));
161
162 if (bo->ttm)
163 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200164 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165 if (bo->destroy)
166 bo->destroy(bo);
167 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200168 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169 kfree(bo);
170 }
171}
172
173int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
174{
175
176 if (interruptible) {
177 int ret = 0;
178
179 ret = wait_event_interruptible(bo->event_queue,
180 atomic_read(&bo->reserved) == 0);
181 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100182 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200183 } else {
184 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
185 }
186 return 0;
187}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000188EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200189
190static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
191{
192 struct ttm_bo_device *bdev = bo->bdev;
193 struct ttm_mem_type_manager *man;
194
195 BUG_ON(!atomic_read(&bo->reserved));
196
197 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
198
199 BUG_ON(!list_empty(&bo->lru));
200
201 man = &bdev->man[bo->mem.mem_type];
202 list_add_tail(&bo->lru, &man->lru);
203 kref_get(&bo->list_kref);
204
205 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200206 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200207 kref_get(&bo->list_kref);
208 }
209 }
210}
211
212/**
213 * Call with the lru_lock held.
214 */
215
216static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
217{
218 int put_count = 0;
219
220 if (!list_empty(&bo->swap)) {
221 list_del_init(&bo->swap);
222 ++put_count;
223 }
224 if (!list_empty(&bo->lru)) {
225 list_del_init(&bo->lru);
226 ++put_count;
227 }
228
229 /*
230 * TODO: Add a driver hook to delete from
231 * driver-specific LRU's here.
232 */
233
234 return put_count;
235}
236
237int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
238 bool interruptible,
239 bool no_wait, bool use_sequence, uint32_t sequence)
240{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200241 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200242 int ret;
243
244 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
245 if (use_sequence && bo->seq_valid &&
246 (sequence - bo->val_seq < (1 << 31))) {
247 return -EAGAIN;
248 }
249
250 if (no_wait)
251 return -EBUSY;
252
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200253 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200255 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200256
257 if (unlikely(ret))
258 return ret;
259 }
260
261 if (use_sequence) {
262 bo->val_seq = sequence;
263 bo->seq_valid = true;
264 } else {
265 bo->seq_valid = false;
266 }
267
268 return 0;
269}
270EXPORT_SYMBOL(ttm_bo_reserve);
271
272static void ttm_bo_ref_bug(struct kref *list_kref)
273{
274 BUG();
275}
276
277int ttm_bo_reserve(struct ttm_buffer_object *bo,
278 bool interruptible,
279 bool no_wait, bool use_sequence, uint32_t sequence)
280{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200281 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200282 int put_count = 0;
283 int ret;
284
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200285 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200286 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
287 sequence);
288 if (likely(ret == 0))
289 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200290 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200291
292 while (put_count--)
293 kref_put(&bo->list_kref, ttm_bo_ref_bug);
294
295 return ret;
296}
297
298void ttm_bo_unreserve(struct ttm_buffer_object *bo)
299{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200300 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200301
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200302 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200303 ttm_bo_add_to_lru(bo);
304 atomic_set(&bo->reserved, 0);
305 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200306 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200307}
308EXPORT_SYMBOL(ttm_bo_unreserve);
309
310/*
311 * Call bo->mutex locked.
312 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200313static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
314{
315 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200316 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200317 int ret = 0;
318 uint32_t page_flags = 0;
319
320 TTM_ASSERT_LOCKED(&bo->mutex);
321 bo->ttm = NULL;
322
Dave Airliead49f502009-07-10 22:36:26 +1000323 if (bdev->need_dma32)
324 page_flags |= TTM_PAGE_FLAG_DMA32;
325
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326 switch (bo->type) {
327 case ttm_bo_type_device:
328 if (zero_alloc)
329 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
330 case ttm_bo_type_kernel:
331 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200332 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200333 if (unlikely(bo->ttm == NULL))
334 ret = -ENOMEM;
335 break;
336 case ttm_bo_type_user:
337 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
338 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200339 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000340 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200341 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000342 break;
343 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344
345 ret = ttm_tt_set_user(bo->ttm, current,
346 bo->buffer_start, bo->num_pages);
347 if (unlikely(ret != 0))
348 ttm_tt_destroy(bo->ttm);
349 break;
350 default:
351 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
352 ret = -EINVAL;
353 break;
354 }
355
356 return ret;
357}
358
359static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
360 struct ttm_mem_reg *mem,
361 bool evict, bool interruptible, bool no_wait)
362{
363 struct ttm_bo_device *bdev = bo->bdev;
364 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
365 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
366 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
367 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
368 int ret = 0;
369
370 if (old_is_pci || new_is_pci ||
371 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
372 ttm_bo_unmap_virtual(bo);
373
374 /*
375 * Create and bind a ttm if required.
376 */
377
378 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
379 ret = ttm_bo_add_ttm(bo, false);
380 if (ret)
381 goto out_err;
382
383 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
384 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200385 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200386
387 if (mem->mem_type != TTM_PL_SYSTEM) {
388 ret = ttm_tt_bind(bo->ttm, mem);
389 if (ret)
390 goto out_err;
391 }
392
393 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100394 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200395 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200396 goto moved;
397 }
398
399 }
400
Dave Airliee024e112009-06-24 09:48:08 +1000401 if (bdev->driver->move_notify)
402 bdev->driver->move_notify(bo, mem);
403
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200404 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
405 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
406 ret = ttm_bo_move_ttm(bo, evict, no_wait, mem);
407 else if (bdev->driver->move)
408 ret = bdev->driver->move(bo, evict, interruptible,
409 no_wait, mem);
410 else
411 ret = ttm_bo_move_memcpy(bo, evict, no_wait, mem);
412
413 if (ret)
414 goto out_err;
415
416moved:
417 if (bo->evicted) {
418 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
419 if (ret)
420 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
421 bo->evicted = false;
422 }
423
424 if (bo->mem.mm_node) {
425 spin_lock(&bo->lock);
426 bo->offset = (bo->mem.mm_node->start << PAGE_SHIFT) +
427 bdev->man[bo->mem.mem_type].gpu_offset;
428 bo->cur_placement = bo->mem.placement;
429 spin_unlock(&bo->lock);
430 }
431
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;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200527 struct ttm_buffer_object *entry, *nentry;
528 struct list_head *list, *next;
529 int ret;
530
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200531 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200532 list_for_each_safe(list, next, &bdev->ddestroy) {
533 entry = list_entry(list, struct ttm_buffer_object, ddestroy);
534 nentry = NULL;
535
536 /*
537 * Protect the next list entry from destruction while we
538 * unlock the lru_lock.
539 */
540
541 if (next != &bdev->ddestroy) {
542 nentry = list_entry(next, struct ttm_buffer_object,
543 ddestroy);
544 kref_get(&nentry->list_kref);
545 }
546 kref_get(&entry->list_kref);
547
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200548 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200549 ret = ttm_bo_cleanup_refs(entry, remove_all);
550 kref_put(&entry->list_kref, ttm_bo_release_list);
551
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200552 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200553 if (nentry) {
554 bool next_onlist = !list_empty(next);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200555 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200556 kref_put(&nentry->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200557 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200558 /*
559 * Someone might have raced us and removed the
560 * next entry from the list. We don't bother restarting
561 * list traversal.
562 */
563
564 if (!next_onlist)
565 break;
566 }
567 if (ret)
568 break;
569 }
570 ret = !list_empty(&bdev->ddestroy);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200571 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200572
573 return ret;
574}
575
576static void ttm_bo_delayed_workqueue(struct work_struct *work)
577{
578 struct ttm_bo_device *bdev =
579 container_of(work, struct ttm_bo_device, wq.work);
580
581 if (ttm_bo_delayed_delete(bdev, false)) {
582 schedule_delayed_work(&bdev->wq,
583 ((HZ / 100) < 1) ? 1 : HZ / 100);
584 }
585}
586
587static void ttm_bo_release(struct kref *kref)
588{
589 struct ttm_buffer_object *bo =
590 container_of(kref, struct ttm_buffer_object, kref);
591 struct ttm_bo_device *bdev = bo->bdev;
592
593 if (likely(bo->vm_node != NULL)) {
594 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
595 drm_mm_put_block(bo->vm_node);
596 bo->vm_node = NULL;
597 }
598 write_unlock(&bdev->vm_lock);
599 ttm_bo_cleanup_refs(bo, false);
600 kref_put(&bo->list_kref, ttm_bo_release_list);
601 write_lock(&bdev->vm_lock);
602}
603
604void ttm_bo_unref(struct ttm_buffer_object **p_bo)
605{
606 struct ttm_buffer_object *bo = *p_bo;
607 struct ttm_bo_device *bdev = bo->bdev;
608
609 *p_bo = NULL;
610 write_lock(&bdev->vm_lock);
611 kref_put(&bo->kref, ttm_bo_release);
612 write_unlock(&bdev->vm_lock);
613}
614EXPORT_SYMBOL(ttm_bo_unref);
615
Jerome Glisseca262a9992009-12-08 15:33:32 +0100616static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
617 bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200618{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200619 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200620 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200621 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100622 struct ttm_placement placement;
623 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200624
625 spin_lock(&bo->lock);
626 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
627 spin_unlock(&bo->lock);
628
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200629 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100630 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200631 printk(KERN_ERR TTM_PFX
632 "Failed to expire sync object before "
633 "buffer eviction.\n");
634 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200635 goto out;
636 }
637
638 BUG_ON(!atomic_read(&bo->reserved));
639
640 evict_mem = bo->mem;
641 evict_mem.mm_node = NULL;
642
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100643 placement.fpfn = 0;
644 placement.lpfn = 0;
645 placement.num_placement = 0;
646 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100647 bdev->driver->evict_flags(bo, &placement);
648 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
649 no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200650 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100651 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200652 printk(KERN_ERR TTM_PFX
653 "Failed to find memory space for "
654 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100655 ttm_bo_mem_space_debug(bo, &placement);
656 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200657 goto out;
658 }
659
660 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
661 no_wait);
662 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100663 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200664 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +0100665 spin_lock(&glob->lru_lock);
666 if (evict_mem.mm_node) {
667 evict_mem.mm_node->private = NULL;
668 drm_mm_put_block(evict_mem.mm_node);
669 evict_mem.mm_node = NULL;
670 }
671 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200672 goto out;
673 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200674 bo->evicted = true;
675out:
676 return ret;
677}
678
Jerome Glisseca262a9992009-12-08 15:33:32 +0100679static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
680 uint32_t mem_type,
681 bool interruptible, bool no_wait)
682{
683 struct ttm_bo_global *glob = bdev->glob;
684 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
685 struct ttm_buffer_object *bo;
686 int ret, put_count = 0;
687
688 spin_lock(&glob->lru_lock);
689 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
690 kref_get(&bo->list_kref);
691 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, false, 0);
692 if (likely(ret == 0))
693 put_count = ttm_bo_del_from_lru(bo);
694 spin_unlock(&glob->lru_lock);
695 if (unlikely(ret != 0))
696 return ret;
697 while (put_count--)
698 kref_put(&bo->list_kref, ttm_bo_ref_bug);
699 ret = ttm_bo_evict(bo, interruptible, no_wait);
700 ttm_bo_unreserve(bo);
701 kref_put(&bo->list_kref, ttm_bo_release_list);
702 return ret;
703}
704
705static int ttm_bo_man_get_node(struct ttm_buffer_object *bo,
706 struct ttm_mem_type_manager *man,
707 struct ttm_placement *placement,
708 struct ttm_mem_reg *mem,
709 struct drm_mm_node **node)
710{
711 struct ttm_bo_global *glob = bo->glob;
712 unsigned long lpfn;
713 int ret;
714
715 lpfn = placement->lpfn;
716 if (!lpfn)
717 lpfn = man->size;
718 *node = NULL;
719 do {
720 ret = drm_mm_pre_get(&man->manager);
721 if (unlikely(ret))
722 return ret;
723
724 spin_lock(&glob->lru_lock);
725 *node = drm_mm_search_free_in_range(&man->manager,
726 mem->num_pages, mem->page_alignment,
727 placement->fpfn, lpfn, 1);
728 if (unlikely(*node == NULL)) {
729 spin_unlock(&glob->lru_lock);
730 return 0;
731 }
732 *node = drm_mm_get_block_atomic_range(*node, mem->num_pages,
733 mem->page_alignment,
734 placement->fpfn,
735 lpfn);
736 spin_unlock(&glob->lru_lock);
737 } while (*node == NULL);
738 return 0;
739}
740
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200741/**
742 * Repeatedly evict memory from the LRU for @mem_type until we create enough
743 * space, or we've evicted everything and there isn't enough space.
744 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100745static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
746 uint32_t mem_type,
747 struct ttm_placement *placement,
748 struct ttm_mem_reg *mem,
749 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200750{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100751 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200752 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200753 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Jerome Glisseca262a9992009-12-08 15:33:32 +0100754 struct drm_mm_node *node;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200755 int ret;
756
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200757 do {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100758 ret = ttm_bo_man_get_node(bo, man, placement, mem, &node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200759 if (unlikely(ret != 0))
760 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100761 if (node)
762 break;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200763 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100764 if (list_empty(&man->lru)) {
765 spin_unlock(&glob->lru_lock);
766 break;
767 }
768 spin_unlock(&glob->lru_lock);
769 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
770 no_wait);
771 if (unlikely(ret != 0))
772 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200773 } while (1);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100774 if (node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200775 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200776 mem->mm_node = node;
777 mem->mem_type = mem_type;
778 return 0;
779}
780
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200781static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
782 uint32_t cur_placement,
783 uint32_t proposed_placement)
784{
785 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
786 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
787
788 /**
789 * Keep current caching if possible.
790 */
791
792 if ((cur_placement & caching) != 0)
793 result |= (cur_placement & caching);
794 else if ((man->default_caching & caching) != 0)
795 result |= man->default_caching;
796 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
797 result |= TTM_PL_FLAG_CACHED;
798 else if ((TTM_PL_FLAG_WC & caching) != 0)
799 result |= TTM_PL_FLAG_WC;
800 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
801 result |= TTM_PL_FLAG_UNCACHED;
802
803 return result;
804}
805
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200806static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
807 bool disallow_fixed,
808 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200809 uint32_t proposed_placement,
810 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200811{
812 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
813
814 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
815 return false;
816
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200817 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200818 return false;
819
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200820 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200821 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200822
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200823 cur_flags |= (proposed_placement & man->available_caching);
824
825 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200826 return true;
827}
828
829/**
830 * Creates space for memory region @mem according to its type.
831 *
832 * This function first searches for free space in compatible memory types in
833 * the priority order defined by the driver. If free space isn't found, then
834 * ttm_bo_mem_force_space is attempted in priority order to evict and find
835 * space.
836 */
837int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100838 struct ttm_placement *placement,
839 struct ttm_mem_reg *mem,
840 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200841{
842 struct ttm_bo_device *bdev = bo->bdev;
843 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200844 uint32_t mem_type = TTM_PL_SYSTEM;
845 uint32_t cur_flags = 0;
846 bool type_found = false;
847 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100848 bool has_erestartsys = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200849 struct drm_mm_node *node = NULL;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100850 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200851
852 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000853 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100854 ret = ttm_mem_type_from_flags(placement->placement[i],
855 &mem_type);
856 if (ret)
857 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200858 man = &bdev->man[mem_type];
859
860 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100861 bo->type == ttm_bo_type_user,
862 mem_type,
863 placement->placement[i],
864 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865
866 if (!type_ok)
867 continue;
868
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200869 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
870 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100871 /*
872 * Use the access and other non-mapping-related flag bits from
873 * the memory placement flags to the current flags
874 */
875 ttm_flag_masked(&cur_flags, placement->placement[i],
876 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200877
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200878 if (mem_type == TTM_PL_SYSTEM)
879 break;
880
881 if (man->has_type && man->use_type) {
882 type_found = true;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100883 ret = ttm_bo_man_get_node(bo, man, placement, mem,
884 &node);
885 if (unlikely(ret))
886 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200887 }
888 if (node)
889 break;
890 }
891
892 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) {
893 mem->mm_node = node;
894 mem->mem_type = mem_type;
895 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100896 if (node)
897 node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898 return 0;
899 }
900
901 if (!type_found)
902 return -EINVAL;
903
Dave Airlieb6637522009-12-14 14:51:35 +1000904 for (i = 0; i < placement->num_busy_placement; ++i) {
905 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100906 &mem_type);
907 if (ret)
908 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200909 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200910 if (!man->has_type)
911 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200912 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100913 bo->type == ttm_bo_type_user,
914 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000915 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100916 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200917 continue;
918
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200919 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
920 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100921 /*
922 * Use the access and other non-mapping-related flag bits from
923 * the memory placement flags to the current flags
924 */
Dave Airlieb6637522009-12-14 14:51:35 +1000925 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100926 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200927
Jerome Glisseca262a9992009-12-08 15:33:32 +0100928 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
929 interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200930 if (ret == 0 && mem->mm_node) {
931 mem->placement = cur_flags;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100932 mem->mm_node->private = bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200933 return 0;
934 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100935 if (ret == -ERESTARTSYS)
936 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200937 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100938 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200939 return ret;
940}
941EXPORT_SYMBOL(ttm_bo_mem_space);
942
943int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
944{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200945 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
946 return -EBUSY;
947
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100948 return wait_event_interruptible(bo->event_queue,
949 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200950}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000951EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200952
953int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100954 struct ttm_placement *placement,
955 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200956{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200957 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200958 int ret = 0;
959 struct ttm_mem_reg mem;
960
961 BUG_ON(!atomic_read(&bo->reserved));
962
963 /*
964 * FIXME: It's possible to pipeline buffer moves.
965 * Have the driver move function wait for idle when necessary,
966 * instead of doing it here.
967 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200968 spin_lock(&bo->lock);
969 ret = ttm_bo_wait(bo, false, interruptible, no_wait);
970 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200971 if (ret)
972 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200973 mem.num_pages = bo->num_pages;
974 mem.size = mem.num_pages << PAGE_SHIFT;
975 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976 /*
977 * Determine where to move the buffer.
978 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100979 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200980 if (ret)
981 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200982 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200983out_unlock:
984 if (ret && mem.mm_node) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200985 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100986 mem.mm_node->private = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200987 drm_mm_put_block(mem.mm_node);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200988 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989 }
990 return ret;
991}
992
Jerome Glisseca262a9992009-12-08 15:33:32 +0100993static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200994 struct ttm_mem_reg *mem)
995{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100996 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997
Jerome Glisseca262a9992009-12-08 15:33:32 +0100998 for (i = 0; i < placement->num_placement; i++) {
999 if ((placement->placement[i] & mem->placement &
1000 TTM_PL_MASK_CACHING) &&
1001 (placement->placement[i] & mem->placement &
1002 TTM_PL_MASK_MEM))
1003 return i;
1004 }
1005 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006}
1007
Jerome Glisse09855ac2009-12-10 17:16:27 +01001008int ttm_bo_validate(struct ttm_buffer_object *bo,
1009 struct ttm_placement *placement,
1010 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001011{
1012 int ret;
1013
1014 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001015 /* Check that range is valid */
1016 if (placement->lpfn || placement->fpfn)
1017 if (placement->fpfn > placement->lpfn ||
1018 (placement->lpfn - placement->fpfn) < bo->num_pages)
1019 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001020 /*
1021 * Check whether we need to move buffer.
1022 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001023 ret = ttm_bo_mem_compat(placement, &bo->mem);
1024 if (ret < 0) {
1025 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait);
1026 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001027 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001028 } else {
1029 /*
1030 * Use the access and other non-mapping-related flag bits from
1031 * the compatible memory placement flags to the active flags
1032 */
1033 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1034 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001035 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036 /*
1037 * We might need to add a TTM.
1038 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001039 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1040 ret = ttm_bo_add_ttm(bo, true);
1041 if (ret)
1042 return ret;
1043 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044 return 0;
1045}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001046EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001047
Jerome Glisse09855ac2009-12-10 17:16:27 +01001048int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1049 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001051 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052
Jerome Glisse09855ac2009-12-10 17:16:27 +01001053 if (placement->fpfn || placement->lpfn) {
1054 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1055 printk(KERN_ERR TTM_PFX "Page number range to small "
1056 "Need %lu pages, range is [%u, %u]\n",
1057 bo->mem.num_pages, placement->fpfn,
1058 placement->lpfn);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001059 return -EINVAL;
1060 }
Jerome Glisse09855ac2009-12-10 17:16:27 +01001061 }
1062 for (i = 0; i < placement->num_placement; i++) {
1063 if (!capable(CAP_SYS_ADMIN)) {
1064 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1065 printk(KERN_ERR TTM_PFX "Need to be root to "
1066 "modify NO_EVICT status.\n");
1067 return -EINVAL;
1068 }
1069 }
1070 }
1071 for (i = 0; i < placement->num_busy_placement; i++) {
1072 if (!capable(CAP_SYS_ADMIN)) {
1073 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1074 printk(KERN_ERR TTM_PFX "Need to be root to "
1075 "modify NO_EVICT status.\n");
1076 return -EINVAL;
1077 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001078 }
1079 }
1080 return 0;
1081}
1082
Jerome Glisse09855ac2009-12-10 17:16:27 +01001083int ttm_bo_init(struct ttm_bo_device *bdev,
1084 struct ttm_buffer_object *bo,
1085 unsigned long size,
1086 enum ttm_bo_type type,
1087 struct ttm_placement *placement,
1088 uint32_t page_alignment,
1089 unsigned long buffer_start,
1090 bool interruptible,
1091 struct file *persistant_swap_storage,
1092 size_t acc_size,
1093 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001094{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001095 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001096 unsigned long num_pages;
1097
1098 size += buffer_start & ~PAGE_MASK;
1099 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1100 if (num_pages == 0) {
1101 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1102 return -EINVAL;
1103 }
1104 bo->destroy = destroy;
1105
1106 spin_lock_init(&bo->lock);
1107 kref_init(&bo->kref);
1108 kref_init(&bo->list_kref);
1109 atomic_set(&bo->cpu_writers, 0);
1110 atomic_set(&bo->reserved, 1);
1111 init_waitqueue_head(&bo->event_queue);
1112 INIT_LIST_HEAD(&bo->lru);
1113 INIT_LIST_HEAD(&bo->ddestroy);
1114 INIT_LIST_HEAD(&bo->swap);
1115 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001116 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117 bo->type = type;
1118 bo->num_pages = num_pages;
1119 bo->mem.mem_type = TTM_PL_SYSTEM;
1120 bo->mem.num_pages = bo->num_pages;
1121 bo->mem.mm_node = NULL;
1122 bo->mem.page_alignment = page_alignment;
1123 bo->buffer_start = buffer_start & PAGE_MASK;
1124 bo->priv_flags = 0;
1125 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1126 bo->seq_valid = false;
1127 bo->persistant_swap_storage = persistant_swap_storage;
1128 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001129 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001130
Jerome Glisse09855ac2009-12-10 17:16:27 +01001131 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001132 if (unlikely(ret != 0))
1133 goto out_err;
1134
1135 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001136 * For ttm_bo_type_device buffers, allocate
1137 * address space from the device.
1138 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001139 if (bo->type == ttm_bo_type_device) {
1140 ret = ttm_bo_setup_vm(bo);
1141 if (ret)
1142 goto out_err;
1143 }
1144
Jerome Glisse09855ac2009-12-10 17:16:27 +01001145 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146 if (ret)
1147 goto out_err;
1148
1149 ttm_bo_unreserve(bo);
1150 return 0;
1151
1152out_err:
1153 ttm_bo_unreserve(bo);
1154 ttm_bo_unref(&bo);
1155
1156 return ret;
1157}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001158EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001159
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001160static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161 unsigned long num_pages)
1162{
1163 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1164 PAGE_MASK;
1165
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001166 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001167}
1168
Jerome Glisse09855ac2009-12-10 17:16:27 +01001169int ttm_bo_create(struct ttm_bo_device *bdev,
1170 unsigned long size,
1171 enum ttm_bo_type type,
1172 struct ttm_placement *placement,
1173 uint32_t page_alignment,
1174 unsigned long buffer_start,
1175 bool interruptible,
1176 struct file *persistant_swap_storage,
1177 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178{
1179 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001180 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001181 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001182
1183 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001184 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001185 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001186 if (unlikely(ret != 0))
1187 return ret;
1188
1189 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1190
1191 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001192 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001193 return -ENOMEM;
1194 }
1195
Jerome Glisse09855ac2009-12-10 17:16:27 +01001196 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1197 buffer_start, interruptible,
1198 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199 if (likely(ret == 0))
1200 *p_bo = bo;
1201
1202 return ret;
1203}
1204
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001205static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001206 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001207{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001208 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001209 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001210 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001211
1212 /*
1213 * Can't use standard list traversal since we're unlocking.
1214 */
1215
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001216 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001217 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001218 spin_unlock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001219 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
1220 if (ret) {
1221 if (allow_errors) {
1222 return ret;
1223 } else {
1224 printk(KERN_ERR TTM_PFX
1225 "Cleanup eviction failed\n");
1226 }
1227 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001228 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001229 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001230 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231 return 0;
1232}
1233
1234int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1235{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001236 struct ttm_bo_global *glob = bdev->glob;
Roel Kluinc96e7c72009-08-03 14:22:53 +02001237 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001238 int ret = -EINVAL;
1239
1240 if (mem_type >= TTM_NUM_MEM_TYPES) {
1241 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1242 return ret;
1243 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001244 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001245
1246 if (!man->has_type) {
1247 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1248 "memory manager type %u\n", mem_type);
1249 return ret;
1250 }
1251
1252 man->use_type = false;
1253 man->has_type = false;
1254
1255 ret = 0;
1256 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001257 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001258
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001259 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001260 if (drm_mm_clean(&man->manager))
1261 drm_mm_takedown(&man->manager);
1262 else
1263 ret = -EBUSY;
1264
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001265 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001266 }
1267
1268 return ret;
1269}
1270EXPORT_SYMBOL(ttm_bo_clean_mm);
1271
1272int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1273{
1274 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1275
1276 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1277 printk(KERN_ERR TTM_PFX
1278 "Illegal memory manager memory type %u.\n",
1279 mem_type);
1280 return -EINVAL;
1281 }
1282
1283 if (!man->has_type) {
1284 printk(KERN_ERR TTM_PFX
1285 "Memory type %u has not been initialized.\n",
1286 mem_type);
1287 return 0;
1288 }
1289
Jerome Glisseca262a9992009-12-08 15:33:32 +01001290 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291}
1292EXPORT_SYMBOL(ttm_bo_evict_mm);
1293
1294int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001295 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001296{
1297 int ret = -EINVAL;
1298 struct ttm_mem_type_manager *man;
1299
1300 if (type >= TTM_NUM_MEM_TYPES) {
1301 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1302 return ret;
1303 }
1304
1305 man = &bdev->man[type];
1306 if (man->has_type) {
1307 printk(KERN_ERR TTM_PFX
1308 "Memory manager already initialized for type %d\n",
1309 type);
1310 return ret;
1311 }
1312
1313 ret = bdev->driver->init_mem_type(bdev, type, man);
1314 if (ret)
1315 return ret;
1316
1317 ret = 0;
1318 if (type != TTM_PL_SYSTEM) {
1319 if (!p_size) {
1320 printk(KERN_ERR TTM_PFX
1321 "Zero size memory manager type %d\n",
1322 type);
1323 return ret;
1324 }
Jerome Glisseca262a9992009-12-08 15:33:32 +01001325 ret = drm_mm_init(&man->manager, 0, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001326 if (ret)
1327 return ret;
1328 }
1329 man->has_type = true;
1330 man->use_type = true;
1331 man->size = p_size;
1332
1333 INIT_LIST_HEAD(&man->lru);
1334
1335 return 0;
1336}
1337EXPORT_SYMBOL(ttm_bo_init_mm);
1338
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001339static void ttm_bo_global_kobj_release(struct kobject *kobj)
1340{
1341 struct ttm_bo_global *glob =
1342 container_of(kobj, struct ttm_bo_global, kobj);
1343
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001344 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1345 __free_page(glob->dummy_read_page);
1346 kfree(glob);
1347}
1348
1349void ttm_bo_global_release(struct ttm_global_reference *ref)
1350{
1351 struct ttm_bo_global *glob = ref->object;
1352
1353 kobject_del(&glob->kobj);
1354 kobject_put(&glob->kobj);
1355}
1356EXPORT_SYMBOL(ttm_bo_global_release);
1357
1358int ttm_bo_global_init(struct ttm_global_reference *ref)
1359{
1360 struct ttm_bo_global_ref *bo_ref =
1361 container_of(ref, struct ttm_bo_global_ref, ref);
1362 struct ttm_bo_global *glob = ref->object;
1363 int ret;
1364
1365 mutex_init(&glob->device_list_mutex);
1366 spin_lock_init(&glob->lru_lock);
1367 glob->mem_glob = bo_ref->mem_glob;
1368 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1369
1370 if (unlikely(glob->dummy_read_page == NULL)) {
1371 ret = -ENOMEM;
1372 goto out_no_drp;
1373 }
1374
1375 INIT_LIST_HEAD(&glob->swap_lru);
1376 INIT_LIST_HEAD(&glob->device_list);
1377
1378 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1379 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1380 if (unlikely(ret != 0)) {
1381 printk(KERN_ERR TTM_PFX
1382 "Could not register buffer object swapout.\n");
1383 goto out_no_shrink;
1384 }
1385
1386 glob->ttm_bo_extra_size =
1387 ttm_round_pot(sizeof(struct ttm_tt)) +
1388 ttm_round_pot(sizeof(struct ttm_backend));
1389
1390 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1391 ttm_round_pot(sizeof(struct ttm_buffer_object));
1392
1393 atomic_set(&glob->bo_count, 0);
1394
1395 kobject_init(&glob->kobj, &ttm_bo_glob_kobj_type);
1396 ret = kobject_add(&glob->kobj, ttm_get_kobj(), "buffer_objects");
1397 if (unlikely(ret != 0))
1398 kobject_put(&glob->kobj);
1399 return ret;
1400out_no_shrink:
1401 __free_page(glob->dummy_read_page);
1402out_no_drp:
1403 kfree(glob);
1404 return ret;
1405}
1406EXPORT_SYMBOL(ttm_bo_global_init);
1407
1408
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001409int ttm_bo_device_release(struct ttm_bo_device *bdev)
1410{
1411 int ret = 0;
1412 unsigned i = TTM_NUM_MEM_TYPES;
1413 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001414 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001415
1416 while (i--) {
1417 man = &bdev->man[i];
1418 if (man->has_type) {
1419 man->use_type = false;
1420 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1421 ret = -EBUSY;
1422 printk(KERN_ERR TTM_PFX
1423 "DRM memory manager type %d "
1424 "is not clean.\n", i);
1425 }
1426 man->has_type = false;
1427 }
1428 }
1429
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001430 mutex_lock(&glob->device_list_mutex);
1431 list_del(&bdev->device_list);
1432 mutex_unlock(&glob->device_list_mutex);
1433
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001434 if (!cancel_delayed_work(&bdev->wq))
1435 flush_scheduled_work();
1436
1437 while (ttm_bo_delayed_delete(bdev, true))
1438 ;
1439
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001440 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001441 if (list_empty(&bdev->ddestroy))
1442 TTM_DEBUG("Delayed destroy list was clean\n");
1443
1444 if (list_empty(&bdev->man[0].lru))
1445 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001446 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001447
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001448 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1449 write_lock(&bdev->vm_lock);
1450 drm_mm_takedown(&bdev->addr_space_mm);
1451 write_unlock(&bdev->vm_lock);
1452
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001453 return ret;
1454}
1455EXPORT_SYMBOL(ttm_bo_device_release);
1456
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001457int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001458 struct ttm_bo_global *glob,
1459 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001460 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001461 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001462{
1463 int ret = -EINVAL;
1464
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001465 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001466 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001467
1468 memset(bdev->man, 0, sizeof(bdev->man));
1469
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001470 /*
1471 * Initialize the system memory buffer type.
1472 * Other types need to be driver / IOCTL initialized.
1473 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001474 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001475 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001476 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001477
1478 bdev->addr_space_rb = RB_ROOT;
1479 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1480 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001481 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001482
1483 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1484 bdev->nice_mode = true;
1485 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001486 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001487 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001488 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001489
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001490 mutex_lock(&glob->device_list_mutex);
1491 list_add_tail(&bdev->device_list, &glob->device_list);
1492 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001493
1494 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001495out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001496 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001497out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001498 return ret;
1499}
1500EXPORT_SYMBOL(ttm_bo_device_init);
1501
1502/*
1503 * buffer object vm functions.
1504 */
1505
1506bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1507{
1508 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1509
1510 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1511 if (mem->mem_type == TTM_PL_SYSTEM)
1512 return false;
1513
1514 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1515 return false;
1516
1517 if (mem->placement & TTM_PL_FLAG_CACHED)
1518 return false;
1519 }
1520 return true;
1521}
1522
1523int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
1524 struct ttm_mem_reg *mem,
1525 unsigned long *bus_base,
1526 unsigned long *bus_offset, unsigned long *bus_size)
1527{
1528 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1529
1530 *bus_size = 0;
1531 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1532 return -EINVAL;
1533
1534 if (ttm_mem_reg_is_pci(bdev, mem)) {
1535 *bus_offset = mem->mm_node->start << PAGE_SHIFT;
1536 *bus_size = mem->num_pages << PAGE_SHIFT;
1537 *bus_base = man->io_offset;
1538 }
1539
1540 return 0;
1541}
1542
1543void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1544{
1545 struct ttm_bo_device *bdev = bo->bdev;
1546 loff_t offset = (loff_t) bo->addr_space_offset;
1547 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1548
1549 if (!bdev->dev_mapping)
1550 return;
1551
1552 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
1553}
Dave Airliee024e112009-06-24 09:48:08 +10001554EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001555
1556static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1557{
1558 struct ttm_bo_device *bdev = bo->bdev;
1559 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1560 struct rb_node *parent = NULL;
1561 struct ttm_buffer_object *cur_bo;
1562 unsigned long offset = bo->vm_node->start;
1563 unsigned long cur_offset;
1564
1565 while (*cur) {
1566 parent = *cur;
1567 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1568 cur_offset = cur_bo->vm_node->start;
1569 if (offset < cur_offset)
1570 cur = &parent->rb_left;
1571 else if (offset > cur_offset)
1572 cur = &parent->rb_right;
1573 else
1574 BUG();
1575 }
1576
1577 rb_link_node(&bo->vm_rb, parent, cur);
1578 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1579}
1580
1581/**
1582 * ttm_bo_setup_vm:
1583 *
1584 * @bo: the buffer to allocate address space for
1585 *
1586 * Allocate address space in the drm device so that applications
1587 * can mmap the buffer and access the contents. This only
1588 * applies to ttm_bo_type_device objects as others are not
1589 * placed in the drm device address space.
1590 */
1591
1592static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1593{
1594 struct ttm_bo_device *bdev = bo->bdev;
1595 int ret;
1596
1597retry_pre_get:
1598 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1599 if (unlikely(ret != 0))
1600 return ret;
1601
1602 write_lock(&bdev->vm_lock);
1603 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1604 bo->mem.num_pages, 0, 0);
1605
1606 if (unlikely(bo->vm_node == NULL)) {
1607 ret = -ENOMEM;
1608 goto out_unlock;
1609 }
1610
1611 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1612 bo->mem.num_pages, 0);
1613
1614 if (unlikely(bo->vm_node == NULL)) {
1615 write_unlock(&bdev->vm_lock);
1616 goto retry_pre_get;
1617 }
1618
1619 ttm_bo_vm_insert_rb(bo);
1620 write_unlock(&bdev->vm_lock);
1621 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1622
1623 return 0;
1624out_unlock:
1625 write_unlock(&bdev->vm_lock);
1626 return ret;
1627}
1628
1629int ttm_bo_wait(struct ttm_buffer_object *bo,
1630 bool lazy, bool interruptible, bool no_wait)
1631{
1632 struct ttm_bo_driver *driver = bo->bdev->driver;
1633 void *sync_obj;
1634 void *sync_obj_arg;
1635 int ret = 0;
1636
1637 if (likely(bo->sync_obj == NULL))
1638 return 0;
1639
1640 while (bo->sync_obj) {
1641
1642 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1643 void *tmp_obj = bo->sync_obj;
1644 bo->sync_obj = NULL;
1645 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1646 spin_unlock(&bo->lock);
1647 driver->sync_obj_unref(&tmp_obj);
1648 spin_lock(&bo->lock);
1649 continue;
1650 }
1651
1652 if (no_wait)
1653 return -EBUSY;
1654
1655 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1656 sync_obj_arg = bo->sync_obj_arg;
1657 spin_unlock(&bo->lock);
1658 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1659 lazy, interruptible);
1660 if (unlikely(ret != 0)) {
1661 driver->sync_obj_unref(&sync_obj);
1662 spin_lock(&bo->lock);
1663 return ret;
1664 }
1665 spin_lock(&bo->lock);
1666 if (likely(bo->sync_obj == sync_obj &&
1667 bo->sync_obj_arg == sync_obj_arg)) {
1668 void *tmp_obj = bo->sync_obj;
1669 bo->sync_obj = NULL;
1670 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1671 &bo->priv_flags);
1672 spin_unlock(&bo->lock);
1673 driver->sync_obj_unref(&sync_obj);
1674 driver->sync_obj_unref(&tmp_obj);
1675 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001676 } else {
1677 spin_unlock(&bo->lock);
1678 driver->sync_obj_unref(&sync_obj);
1679 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001680 }
1681 }
1682 return 0;
1683}
1684EXPORT_SYMBOL(ttm_bo_wait);
1685
1686void ttm_bo_unblock_reservation(struct ttm_buffer_object *bo)
1687{
1688 atomic_set(&bo->reserved, 0);
1689 wake_up_all(&bo->event_queue);
1690}
1691
1692int ttm_bo_block_reservation(struct ttm_buffer_object *bo, bool interruptible,
1693 bool no_wait)
1694{
1695 int ret;
1696
1697 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
1698 if (no_wait)
1699 return -EBUSY;
1700 else if (interruptible) {
1701 ret = wait_event_interruptible
1702 (bo->event_queue, atomic_read(&bo->reserved) == 0);
1703 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001704 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001705 } else {
1706 wait_event(bo->event_queue,
1707 atomic_read(&bo->reserved) == 0);
1708 }
1709 }
1710 return 0;
1711}
1712
1713int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1714{
1715 int ret = 0;
1716
1717 /*
1718 * Using ttm_bo_reserve instead of ttm_bo_block_reservation
1719 * makes sure the lru lists are updated.
1720 */
1721
1722 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1723 if (unlikely(ret != 0))
1724 return ret;
1725 spin_lock(&bo->lock);
1726 ret = ttm_bo_wait(bo, false, true, no_wait);
1727 spin_unlock(&bo->lock);
1728 if (likely(ret == 0))
1729 atomic_inc(&bo->cpu_writers);
1730 ttm_bo_unreserve(bo);
1731 return ret;
1732}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001733EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001734
1735void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1736{
1737 if (atomic_dec_and_test(&bo->cpu_writers))
1738 wake_up_all(&bo->event_queue);
1739}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001740EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001741
1742/**
1743 * A buffer object shrink method that tries to swap out the first
1744 * buffer object on the bo_global::swap_lru list.
1745 */
1746
1747static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1748{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001749 struct ttm_bo_global *glob =
1750 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001751 struct ttm_buffer_object *bo;
1752 int ret = -EBUSY;
1753 int put_count;
1754 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1755
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001756 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001757 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001758 if (unlikely(list_empty(&glob->swap_lru))) {
1759 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001760 return -EBUSY;
1761 }
1762
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001763 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001764 struct ttm_buffer_object, swap);
1765 kref_get(&bo->list_kref);
1766
1767 /**
1768 * Reserve buffer. Since we unlock while sleeping, we need
1769 * to re-check that nobody removed us from the swap-list while
1770 * we slept.
1771 */
1772
1773 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1774 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001775 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001776 ttm_bo_wait_unreserved(bo, false);
1777 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001778 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001779 }
1780 }
1781
1782 BUG_ON(ret != 0);
1783 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001784 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001785
1786 while (put_count--)
1787 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1788
1789 /**
1790 * Wait for GPU, then move to system cached.
1791 */
1792
1793 spin_lock(&bo->lock);
1794 ret = ttm_bo_wait(bo, false, false, false);
1795 spin_unlock(&bo->lock);
1796
1797 if (unlikely(ret != 0))
1798 goto out;
1799
1800 if ((bo->mem.placement & swap_placement) != swap_placement) {
1801 struct ttm_mem_reg evict_mem;
1802
1803 evict_mem = bo->mem;
1804 evict_mem.mm_node = NULL;
1805 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1806 evict_mem.mem_type = TTM_PL_SYSTEM;
1807
1808 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1809 false, false);
1810 if (unlikely(ret != 0))
1811 goto out;
1812 }
1813
1814 ttm_bo_unmap_virtual(bo);
1815
1816 /**
1817 * Swap out. Buffer will be swapped in again as soon as
1818 * anyone tries to access a ttm page.
1819 */
1820
1821 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1822out:
1823
1824 /**
1825 *
1826 * Unreserve without putting on LRU to avoid swapping out an
1827 * already swapped buffer.
1828 */
1829
1830 atomic_set(&bo->reserved, 0);
1831 wake_up_all(&bo->event_queue);
1832 kref_put(&bo->list_kref, ttm_bo_release_list);
1833 return ret;
1834}
1835
1836void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1837{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001838 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001839 ;
1840}