blob: af7b57a47fbc30e8a890f99d91eaa3632e4e5c45 [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);
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010082 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
Jerome Glissefb53f862009-12-09 21:55:10 +010083 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
84 man->available_caching);
85 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
86 man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100087 if (mem_type != TTM_PL_SYSTEM)
88 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010089}
90
91static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
92 struct ttm_placement *placement)
93{
Jerome Glissefb53f862009-12-09 21:55:10 +010094 int i, ret, mem_type;
95
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010096 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
Jerome Glissefb53f862009-12-09 21:55:10 +010097 bo, bo->mem.num_pages, bo->mem.size >> 10,
98 bo->mem.size >> 20);
99 for (i = 0; i < placement->num_placement; i++) {
100 ret = ttm_mem_type_from_flags(placement->placement[i],
101 &mem_type);
102 if (ret)
103 return;
Jerome Glissefb53f862009-12-09 21:55:10 +0100104 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
105 i, placement->placement[i], mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +0100106 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100107 }
108}
109
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200110static ssize_t ttm_bo_global_show(struct kobject *kobj,
111 struct attribute *attr,
112 char *buffer)
113{
114 struct ttm_bo_global *glob =
115 container_of(kobj, struct ttm_bo_global, kobj);
116
117 return snprintf(buffer, PAGE_SIZE, "%lu\n",
118 (unsigned long) atomic_read(&glob->bo_count));
119}
120
121static struct attribute *ttm_bo_global_attrs[] = {
122 &ttm_bo_count,
123 NULL
124};
125
Emese Revfy52cf25d2010-01-19 02:58:23 +0100126static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200127 .show = &ttm_bo_global_show
128};
129
130static struct kobj_type ttm_bo_glob_kobj_type = {
131 .release = &ttm_bo_global_kobj_release,
132 .sysfs_ops = &ttm_bo_global_ops,
133 .default_attrs = ttm_bo_global_attrs
134};
135
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200136
137static inline uint32_t ttm_bo_type_flags(unsigned type)
138{
139 return 1 << (type);
140}
141
142static void ttm_bo_release_list(struct kref *list_kref)
143{
144 struct ttm_buffer_object *bo =
145 container_of(list_kref, struct ttm_buffer_object, list_kref);
146 struct ttm_bo_device *bdev = bo->bdev;
147
148 BUG_ON(atomic_read(&bo->list_kref.refcount));
149 BUG_ON(atomic_read(&bo->kref.refcount));
150 BUG_ON(atomic_read(&bo->cpu_writers));
151 BUG_ON(bo->sync_obj != NULL);
152 BUG_ON(bo->mem.mm_node != NULL);
153 BUG_ON(!list_empty(&bo->lru));
154 BUG_ON(!list_empty(&bo->ddestroy));
155
156 if (bo->ttm)
157 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200158 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200159 if (bo->destroy)
160 bo->destroy(bo);
161 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200162 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200163 kfree(bo);
164 }
165}
166
167int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
168{
169
170 if (interruptible) {
171 int ret = 0;
172
173 ret = wait_event_interruptible(bo->event_queue,
174 atomic_read(&bo->reserved) == 0);
175 if (unlikely(ret != 0))
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100176 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200177 } else {
178 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
179 }
180 return 0;
181}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000182EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200183
184static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
185{
186 struct ttm_bo_device *bdev = bo->bdev;
187 struct ttm_mem_type_manager *man;
188
189 BUG_ON(!atomic_read(&bo->reserved));
190
191 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
192
193 BUG_ON(!list_empty(&bo->lru));
194
195 man = &bdev->man[bo->mem.mem_type];
196 list_add_tail(&bo->lru, &man->lru);
197 kref_get(&bo->list_kref);
198
199 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200200 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200201 kref_get(&bo->list_kref);
202 }
203 }
204}
205
206/**
207 * Call with the lru_lock held.
208 */
209
210static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
211{
212 int put_count = 0;
213
214 if (!list_empty(&bo->swap)) {
215 list_del_init(&bo->swap);
216 ++put_count;
217 }
218 if (!list_empty(&bo->lru)) {
219 list_del_init(&bo->lru);
220 ++put_count;
221 }
222
223 /*
224 * TODO: Add a driver hook to delete from
225 * driver-specific LRU's here.
226 */
227
228 return put_count;
229}
230
231int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
232 bool interruptible,
233 bool no_wait, bool use_sequence, uint32_t sequence)
234{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200235 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200236 int ret;
237
238 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
239 if (use_sequence && bo->seq_valid &&
240 (sequence - bo->val_seq < (1 << 31))) {
241 return -EAGAIN;
242 }
243
244 if (no_wait)
245 return -EBUSY;
246
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200247 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200248 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200249 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200250
251 if (unlikely(ret))
252 return ret;
253 }
254
255 if (use_sequence) {
256 bo->val_seq = sequence;
257 bo->seq_valid = true;
258 } else {
259 bo->seq_valid = false;
260 }
261
262 return 0;
263}
264EXPORT_SYMBOL(ttm_bo_reserve);
265
266static void ttm_bo_ref_bug(struct kref *list_kref)
267{
268 BUG();
269}
270
271int ttm_bo_reserve(struct ttm_buffer_object *bo,
272 bool interruptible,
273 bool no_wait, bool use_sequence, uint32_t sequence)
274{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200275 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200276 int put_count = 0;
277 int ret;
278
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200279 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200280 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
281 sequence);
282 if (likely(ret == 0))
283 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200284 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200285
286 while (put_count--)
287 kref_put(&bo->list_kref, ttm_bo_ref_bug);
288
289 return ret;
290}
291
292void ttm_bo_unreserve(struct ttm_buffer_object *bo)
293{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200294 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200295
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200296 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200297 ttm_bo_add_to_lru(bo);
298 atomic_set(&bo->reserved, 0);
299 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200300 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200301}
302EXPORT_SYMBOL(ttm_bo_unreserve);
303
304/*
305 * Call bo->mutex locked.
306 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200307static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
308{
309 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200310 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200311 int ret = 0;
312 uint32_t page_flags = 0;
313
314 TTM_ASSERT_LOCKED(&bo->mutex);
315 bo->ttm = NULL;
316
Dave Airliead49f502009-07-10 22:36:26 +1000317 if (bdev->need_dma32)
318 page_flags |= TTM_PAGE_FLAG_DMA32;
319
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200320 switch (bo->type) {
321 case ttm_bo_type_device:
322 if (zero_alloc)
323 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
324 case ttm_bo_type_kernel:
325 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200326 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200327 if (unlikely(bo->ttm == NULL))
328 ret = -ENOMEM;
329 break;
330 case ttm_bo_type_user:
331 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
332 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200333 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000334 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200335 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000336 break;
337 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200338
339 ret = ttm_tt_set_user(bo->ttm, current,
340 bo->buffer_start, bo->num_pages);
341 if (unlikely(ret != 0))
342 ttm_tt_destroy(bo->ttm);
343 break;
344 default:
345 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
346 ret = -EINVAL;
347 break;
348 }
349
350 return ret;
351}
352
353static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
354 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000355 bool evict, bool interruptible,
356 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200357{
358 struct ttm_bo_device *bdev = bo->bdev;
359 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
360 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
361 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
362 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
363 int ret = 0;
364
365 if (old_is_pci || new_is_pci ||
366 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
367 ttm_bo_unmap_virtual(bo);
368
369 /*
370 * Create and bind a ttm if required.
371 */
372
373 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
374 ret = ttm_bo_add_ttm(bo, false);
375 if (ret)
376 goto out_err;
377
378 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
379 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200380 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200381
382 if (mem->mem_type != TTM_PL_SYSTEM) {
383 ret = ttm_tt_bind(bo->ttm, mem);
384 if (ret)
385 goto out_err;
386 }
387
388 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100389 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200391 goto moved;
392 }
393
394 }
395
Dave Airliee024e112009-06-24 09:48:08 +1000396 if (bdev->driver->move_notify)
397 bdev->driver->move_notify(bo, mem);
398
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200399 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
400 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000401 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200402 else if (bdev->driver->move)
403 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000404 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200405 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000406 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200407
408 if (ret)
409 goto out_err;
410
411moved:
412 if (bo->evicted) {
413 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
414 if (ret)
415 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
416 bo->evicted = false;
417 }
418
419 if (bo->mem.mm_node) {
420 spin_lock(&bo->lock);
Ben Skeggsd961db72010-08-05 10:48:18 +1000421 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200422 bdev->man[bo->mem.mem_type].gpu_offset;
423 bo->cur_placement = bo->mem.placement;
424 spin_unlock(&bo->lock);
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100425 } else
426 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200427
428 return 0;
429
430out_err:
431 new_man = &bdev->man[bo->mem.mem_type];
432 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
433 ttm_tt_unbind(bo->ttm);
434 ttm_tt_destroy(bo->ttm);
435 bo->ttm = NULL;
436 }
437
438 return ret;
439}
440
441/**
442 * If bo idle, remove from delayed- and lru lists, and unref.
443 * If not idle, and already on delayed list, do nothing.
444 * If not idle, and not on delayed list, put on delayed list,
445 * up the list_kref and schedule a delayed list check.
446 */
447
448static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all)
449{
450 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200451 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200452 struct ttm_bo_driver *driver = bdev->driver;
453 int ret;
454
455 spin_lock(&bo->lock);
456 (void) ttm_bo_wait(bo, false, false, !remove_all);
457
458 if (!bo->sync_obj) {
459 int put_count;
460
461 spin_unlock(&bo->lock);
462
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200463 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100464 put_count = ttm_bo_del_from_lru(bo);
465
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200466 ret = ttm_bo_reserve_locked(bo, false, false, false, 0);
467 BUG_ON(ret);
468 if (bo->ttm)
469 ttm_tt_unbind(bo->ttm);
470
471 if (!list_empty(&bo->ddestroy)) {
472 list_del_init(&bo->ddestroy);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100473 ++put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200474 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200475 spin_unlock(&glob->lru_lock);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000476 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200477
478 atomic_set(&bo->reserved, 0);
479
480 while (put_count--)
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100481 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200482
483 return 0;
484 }
485
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200486 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200487 if (list_empty(&bo->ddestroy)) {
488 void *sync_obj = bo->sync_obj;
489 void *sync_obj_arg = bo->sync_obj_arg;
490
491 kref_get(&bo->list_kref);
492 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200493 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200494 spin_unlock(&bo->lock);
495
496 if (sync_obj)
497 driver->sync_obj_flush(sync_obj, sync_obj_arg);
498 schedule_delayed_work(&bdev->wq,
499 ((HZ / 100) < 1) ? 1 : HZ / 100);
500 ret = 0;
501
502 } else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200503 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200504 spin_unlock(&bo->lock);
505 ret = -EBUSY;
506 }
507
508 return ret;
509}
510
511/**
512 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
513 * encountered buffers.
514 */
515
516static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
517{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200518 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100519 struct ttm_buffer_object *entry = NULL;
520 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200521
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200522 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100523 if (list_empty(&bdev->ddestroy))
524 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200525
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100526 entry = list_first_entry(&bdev->ddestroy,
527 struct ttm_buffer_object, ddestroy);
528 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200529
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100530 for (;;) {
531 struct ttm_buffer_object *nentry = NULL;
532
533 if (entry->ddestroy.next != &bdev->ddestroy) {
534 nentry = list_first_entry(&entry->ddestroy,
535 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200536 kref_get(&nentry->list_kref);
537 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200538
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200539 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200540 ret = ttm_bo_cleanup_refs(entry, remove_all);
541 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100542 entry = nentry;
543
544 if (ret || !entry)
545 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200546
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200547 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100548 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200549 break;
550 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200551
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100552out_unlock:
553 spin_unlock(&glob->lru_lock);
554out:
555 if (entry)
556 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200557 return ret;
558}
559
560static void ttm_bo_delayed_workqueue(struct work_struct *work)
561{
562 struct ttm_bo_device *bdev =
563 container_of(work, struct ttm_bo_device, wq.work);
564
565 if (ttm_bo_delayed_delete(bdev, false)) {
566 schedule_delayed_work(&bdev->wq,
567 ((HZ / 100) < 1) ? 1 : HZ / 100);
568 }
569}
570
571static void ttm_bo_release(struct kref *kref)
572{
573 struct ttm_buffer_object *bo =
574 container_of(kref, struct ttm_buffer_object, kref);
575 struct ttm_bo_device *bdev = bo->bdev;
576
577 if (likely(bo->vm_node != NULL)) {
578 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
579 drm_mm_put_block(bo->vm_node);
580 bo->vm_node = NULL;
581 }
582 write_unlock(&bdev->vm_lock);
583 ttm_bo_cleanup_refs(bo, false);
584 kref_put(&bo->list_kref, ttm_bo_release_list);
585 write_lock(&bdev->vm_lock);
586}
587
588void ttm_bo_unref(struct ttm_buffer_object **p_bo)
589{
590 struct ttm_buffer_object *bo = *p_bo;
591 struct ttm_bo_device *bdev = bo->bdev;
592
593 *p_bo = NULL;
594 write_lock(&bdev->vm_lock);
595 kref_put(&bo->kref, ttm_bo_release);
596 write_unlock(&bdev->vm_lock);
597}
598EXPORT_SYMBOL(ttm_bo_unref);
599
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400600int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
601{
602 return cancel_delayed_work_sync(&bdev->wq);
603}
604EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
605
606void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
607{
608 if (resched)
609 schedule_delayed_work(&bdev->wq,
610 ((HZ / 100) < 1) ? 1 : HZ / 100);
611}
612EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
613
Jerome Glisseca262a9992009-12-08 15:33:32 +0100614static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000615 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200617 struct ttm_bo_device *bdev = bo->bdev;
618 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100619 struct ttm_placement placement;
620 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200621
622 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000623 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200624 spin_unlock(&bo->lock);
625
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200626 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100627 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200628 printk(KERN_ERR TTM_PFX
629 "Failed to expire sync object before "
630 "buffer eviction.\n");
631 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200632 goto out;
633 }
634
635 BUG_ON(!atomic_read(&bo->reserved));
636
637 evict_mem = bo->mem;
638 evict_mem.mm_node = NULL;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200639 evict_mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200640
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100641 placement.fpfn = 0;
642 placement.lpfn = 0;
643 placement.num_placement = 0;
644 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100645 bdev->driver->evict_flags(bo, &placement);
646 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000647 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200648 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100649 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200650 printk(KERN_ERR TTM_PFX
651 "Failed to find memory space for "
652 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100653 ttm_bo_mem_space_debug(bo, &placement);
654 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200655 goto out;
656 }
657
658 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000659 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200660 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100661 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200662 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000663 ttm_bo_mem_put(bo, &evict_mem);
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,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000673 bool interruptible, bool no_wait_reserve,
674 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100675{
676 struct ttm_bo_global *glob = bdev->glob;
677 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
678 struct ttm_buffer_object *bo;
679 int ret, put_count = 0;
680
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100681retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100682 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100683 if (list_empty(&man->lru)) {
684 spin_unlock(&glob->lru_lock);
685 return -EBUSY;
686 }
687
Jerome Glisseca262a9992009-12-08 15:33:32 +0100688 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
689 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100690
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000691 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100692
693 if (unlikely(ret == -EBUSY)) {
694 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000695 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100696 ret = ttm_bo_wait_unreserved(bo, interruptible);
697
698 kref_put(&bo->list_kref, ttm_bo_release_list);
699
700 /**
701 * We *need* to retry after releasing the lru lock.
702 */
703
704 if (unlikely(ret != 0))
705 return ret;
706 goto retry;
707 }
708
709 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100710 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100711
712 BUG_ON(ret != 0);
713
Jerome Glisseca262a9992009-12-08 15:33:32 +0100714 while (put_count--)
715 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100716
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000717 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100718 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100719
Jerome Glisseca262a9992009-12-08 15:33:32 +0100720 kref_put(&bo->list_kref, ttm_bo_release_list);
721 return ret;
722}
723
Ben Skeggs42311ff2010-08-04 12:07:08 +1000724void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
725{
Ben Skeggsd961db72010-08-05 10:48:18 +1000726 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000727
Ben Skeggsd961db72010-08-05 10:48:18 +1000728 if (mem->mm_node)
729 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000730}
731EXPORT_SYMBOL(ttm_bo_mem_put);
732
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200733/**
734 * Repeatedly evict memory from the LRU for @mem_type until we create enough
735 * space, or we've evicted everything and there isn't enough space.
736 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100737static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
738 uint32_t mem_type,
739 struct ttm_placement *placement,
740 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000741 bool interruptible,
742 bool no_wait_reserve,
743 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200744{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100745 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200746 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200747 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200748 int ret;
749
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200750 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000751 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200752 if (unlikely(ret != 0))
753 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000754 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100755 break;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200756 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100757 if (list_empty(&man->lru)) {
758 spin_unlock(&glob->lru_lock);
759 break;
760 }
761 spin_unlock(&glob->lru_lock);
762 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000763 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100764 if (unlikely(ret != 0))
765 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200766 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000767 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200768 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200769 mem->mem_type = mem_type;
770 return 0;
771}
772
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200773static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
774 uint32_t cur_placement,
775 uint32_t proposed_placement)
776{
777 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
778 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
779
780 /**
781 * Keep current caching if possible.
782 */
783
784 if ((cur_placement & caching) != 0)
785 result |= (cur_placement & caching);
786 else if ((man->default_caching & caching) != 0)
787 result |= man->default_caching;
788 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
789 result |= TTM_PL_FLAG_CACHED;
790 else if ((TTM_PL_FLAG_WC & caching) != 0)
791 result |= TTM_PL_FLAG_WC;
792 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
793 result |= TTM_PL_FLAG_UNCACHED;
794
795 return result;
796}
797
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200798static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
799 bool disallow_fixed,
800 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200801 uint32_t proposed_placement,
802 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200803{
804 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
805
806 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
807 return false;
808
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200809 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200810 return false;
811
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200812 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200813 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200814
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200815 cur_flags |= (proposed_placement & man->available_caching);
816
817 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200818 return true;
819}
820
821/**
822 * Creates space for memory region @mem according to its type.
823 *
824 * This function first searches for free space in compatible memory types in
825 * the priority order defined by the driver. If free space isn't found, then
826 * ttm_bo_mem_force_space is attempted in priority order to evict and find
827 * space.
828 */
829int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100830 struct ttm_placement *placement,
831 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000832 bool interruptible, bool no_wait_reserve,
833 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200834{
835 struct ttm_bo_device *bdev = bo->bdev;
836 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200837 uint32_t mem_type = TTM_PL_SYSTEM;
838 uint32_t cur_flags = 0;
839 bool type_found = false;
840 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100841 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100842 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200843
844 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000845 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100846 ret = ttm_mem_type_from_flags(placement->placement[i],
847 &mem_type);
848 if (ret)
849 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200850 man = &bdev->man[mem_type];
851
852 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100853 bo->type == ttm_bo_type_user,
854 mem_type,
855 placement->placement[i],
856 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200857
858 if (!type_ok)
859 continue;
860
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200861 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
862 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100863 /*
864 * Use the access and other non-mapping-related flag bits from
865 * the memory placement flags to the current flags
866 */
867 ttm_flag_masked(&cur_flags, placement->placement[i],
868 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200869
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200870 if (mem_type == TTM_PL_SYSTEM)
871 break;
872
873 if (man->has_type && man->use_type) {
874 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000875 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100876 if (unlikely(ret))
877 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200878 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000879 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880 break;
881 }
882
Ben Skeggsd961db72010-08-05 10:48:18 +1000883 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200884 mem->mem_type = mem_type;
885 mem->placement = cur_flags;
886 return 0;
887 }
888
889 if (!type_found)
890 return -EINVAL;
891
Dave Airlieb6637522009-12-14 14:51:35 +1000892 for (i = 0; i < placement->num_busy_placement; ++i) {
893 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100894 &mem_type);
895 if (ret)
896 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200897 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898 if (!man->has_type)
899 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200900 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100901 bo->type == ttm_bo_type_user,
902 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000903 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100904 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200905 continue;
906
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200907 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
908 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100909 /*
910 * Use the access and other non-mapping-related flag bits from
911 * the memory placement flags to the current flags
912 */
Dave Airlieb6637522009-12-14 14:51:35 +1000913 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100914 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200915
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100916
917 if (mem_type == TTM_PL_SYSTEM) {
918 mem->mem_type = mem_type;
919 mem->placement = cur_flags;
920 mem->mm_node = NULL;
921 return 0;
922 }
923
Jerome Glisseca262a9992009-12-08 15:33:32 +0100924 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000925 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926 if (ret == 0 && mem->mm_node) {
927 mem->placement = cur_flags;
928 return 0;
929 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100930 if (ret == -ERESTARTSYS)
931 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200932 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100933 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200934 return ret;
935}
936EXPORT_SYMBOL(ttm_bo_mem_space);
937
938int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
939{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200940 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
941 return -EBUSY;
942
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100943 return wait_event_interruptible(bo->event_queue,
944 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200945}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000946EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200947
948int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100949 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000950 bool interruptible, bool no_wait_reserve,
951 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200952{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953 int ret = 0;
954 struct ttm_mem_reg mem;
955
956 BUG_ON(!atomic_read(&bo->reserved));
957
958 /*
959 * FIXME: It's possible to pipeline buffer moves.
960 * Have the driver move function wait for idle when necessary,
961 * instead of doing it here.
962 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200963 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000964 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200965 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200966 if (ret)
967 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200968 mem.num_pages = bo->num_pages;
969 mem.size = mem.num_pages << PAGE_SHIFT;
970 mem.page_alignment = bo->mem.page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200971 mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200972 /*
973 * Determine where to move the buffer.
974 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000975 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976 if (ret)
977 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000978 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200979out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +1000980 if (ret && mem.mm_node)
981 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200982 return ret;
983}
984
Jerome Glisseca262a9992009-12-08 15:33:32 +0100985static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200986 struct ttm_mem_reg *mem)
987{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100988 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +0100989
Ben Skeggsd961db72010-08-05 10:48:18 +1000990 if (mem->mm_node && placement->lpfn != 0 &&
991 (mem->start < placement->fpfn ||
992 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +0100993 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200994
Jerome Glisseca262a9992009-12-08 15:33:32 +0100995 for (i = 0; i < placement->num_placement; i++) {
996 if ((placement->placement[i] & mem->placement &
997 TTM_PL_MASK_CACHING) &&
998 (placement->placement[i] & mem->placement &
999 TTM_PL_MASK_MEM))
1000 return i;
1001 }
1002 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001003}
1004
Jerome Glisse09855ac2009-12-10 17:16:27 +01001005int ttm_bo_validate(struct ttm_buffer_object *bo,
1006 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001007 bool interruptible, bool no_wait_reserve,
1008 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001009{
1010 int ret;
1011
1012 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001013 /* Check that range is valid */
1014 if (placement->lpfn || placement->fpfn)
1015 if (placement->fpfn > placement->lpfn ||
1016 (placement->lpfn - placement->fpfn) < bo->num_pages)
1017 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001018 /*
1019 * Check whether we need to move buffer.
1020 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001021 ret = ttm_bo_mem_compat(placement, &bo->mem);
1022 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001023 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001024 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001025 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001026 } else {
1027 /*
1028 * Use the access and other non-mapping-related flag bits from
1029 * the compatible memory placement flags to the active flags
1030 */
1031 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1032 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001033 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001034 /*
1035 * We might need to add a TTM.
1036 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1038 ret = ttm_bo_add_ttm(bo, true);
1039 if (ret)
1040 return ret;
1041 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001042 return 0;
1043}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001044EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001045
Jerome Glisse09855ac2009-12-10 17:16:27 +01001046int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1047 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001048{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001049 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050
Jerome Glisse09855ac2009-12-10 17:16:27 +01001051 if (placement->fpfn || placement->lpfn) {
1052 if (bo->mem.num_pages > (placement->lpfn - placement->fpfn)) {
1053 printk(KERN_ERR TTM_PFX "Page number range to small "
1054 "Need %lu pages, range is [%u, %u]\n",
1055 bo->mem.num_pages, placement->fpfn,
1056 placement->lpfn);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001057 return -EINVAL;
1058 }
Jerome Glisse09855ac2009-12-10 17:16:27 +01001059 }
1060 for (i = 0; i < placement->num_placement; i++) {
1061 if (!capable(CAP_SYS_ADMIN)) {
1062 if (placement->placement[i] & TTM_PL_FLAG_NO_EVICT) {
1063 printk(KERN_ERR TTM_PFX "Need to be root to "
1064 "modify NO_EVICT status.\n");
1065 return -EINVAL;
1066 }
1067 }
1068 }
1069 for (i = 0; i < placement->num_busy_placement; i++) {
1070 if (!capable(CAP_SYS_ADMIN)) {
1071 if (placement->busy_placement[i] & TTM_PL_FLAG_NO_EVICT) {
1072 printk(KERN_ERR TTM_PFX "Need to be root to "
1073 "modify NO_EVICT status.\n");
1074 return -EINVAL;
1075 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001076 }
1077 }
1078 return 0;
1079}
1080
Jerome Glisse09855ac2009-12-10 17:16:27 +01001081int ttm_bo_init(struct ttm_bo_device *bdev,
1082 struct ttm_buffer_object *bo,
1083 unsigned long size,
1084 enum ttm_bo_type type,
1085 struct ttm_placement *placement,
1086 uint32_t page_alignment,
1087 unsigned long buffer_start,
1088 bool interruptible,
1089 struct file *persistant_swap_storage,
1090 size_t acc_size,
1091 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001093 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001094 unsigned long num_pages;
1095
1096 size += buffer_start & ~PAGE_MASK;
1097 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1098 if (num_pages == 0) {
1099 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
1100 return -EINVAL;
1101 }
1102 bo->destroy = destroy;
1103
1104 spin_lock_init(&bo->lock);
1105 kref_init(&bo->kref);
1106 kref_init(&bo->list_kref);
1107 atomic_set(&bo->cpu_writers, 0);
1108 atomic_set(&bo->reserved, 1);
1109 init_waitqueue_head(&bo->event_queue);
1110 INIT_LIST_HEAD(&bo->lru);
1111 INIT_LIST_HEAD(&bo->ddestroy);
1112 INIT_LIST_HEAD(&bo->swap);
1113 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001114 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001115 bo->type = type;
1116 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001117 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001118 bo->mem.mem_type = TTM_PL_SYSTEM;
1119 bo->mem.num_pages = bo->num_pages;
1120 bo->mem.mm_node = NULL;
1121 bo->mem.page_alignment = page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001122 bo->mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001123 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 Glisse9d87fa22010-04-07 10:21:19 +00001145 ret = ttm_bo_validate(bo, placement, interruptible, false, 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 Glisse9d87fa22010-04-07 10:21:19 +00001219 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001220 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{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001236 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001237 int ret = -EINVAL;
1238
1239 if (mem_type >= TTM_NUM_MEM_TYPES) {
1240 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1241 return ret;
1242 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001243 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001244
1245 if (!man->has_type) {
1246 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1247 "memory manager type %u\n", mem_type);
1248 return ret;
1249 }
1250
1251 man->use_type = false;
1252 man->has_type = false;
1253
1254 ret = 0;
1255 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001256 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001257
Ben Skeggsd961db72010-08-05 10:48:18 +10001258 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001259 }
1260
1261 return ret;
1262}
1263EXPORT_SYMBOL(ttm_bo_clean_mm);
1264
1265int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1266{
1267 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1268
1269 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1270 printk(KERN_ERR TTM_PFX
1271 "Illegal memory manager memory type %u.\n",
1272 mem_type);
1273 return -EINVAL;
1274 }
1275
1276 if (!man->has_type) {
1277 printk(KERN_ERR TTM_PFX
1278 "Memory type %u has not been initialized.\n",
1279 mem_type);
1280 return 0;
1281 }
1282
Jerome Glisseca262a9992009-12-08 15:33:32 +01001283 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001284}
1285EXPORT_SYMBOL(ttm_bo_evict_mm);
1286
1287int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001288 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001289{
1290 int ret = -EINVAL;
1291 struct ttm_mem_type_manager *man;
1292
1293 if (type >= TTM_NUM_MEM_TYPES) {
1294 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type);
1295 return ret;
1296 }
1297
1298 man = &bdev->man[type];
1299 if (man->has_type) {
1300 printk(KERN_ERR TTM_PFX
1301 "Memory manager already initialized for type %d\n",
1302 type);
1303 return ret;
1304 }
1305
1306 ret = bdev->driver->init_mem_type(bdev, type, man);
1307 if (ret)
1308 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001309 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001310
1311 ret = 0;
1312 if (type != TTM_PL_SYSTEM) {
1313 if (!p_size) {
1314 printk(KERN_ERR TTM_PFX
1315 "Zero size memory manager type %d\n",
1316 type);
1317 return ret;
1318 }
Ben Skeggsd961db72010-08-05 10:48:18 +10001319
1320 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001321 if (ret)
1322 return ret;
1323 }
1324 man->has_type = true;
1325 man->use_type = true;
1326 man->size = p_size;
1327
1328 INIT_LIST_HEAD(&man->lru);
1329
1330 return 0;
1331}
1332EXPORT_SYMBOL(ttm_bo_init_mm);
1333
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001334static void ttm_bo_global_kobj_release(struct kobject *kobj)
1335{
1336 struct ttm_bo_global *glob =
1337 container_of(kobj, struct ttm_bo_global, kobj);
1338
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001339 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1340 __free_page(glob->dummy_read_page);
1341 kfree(glob);
1342}
1343
Dave Airlieba4420c2010-03-09 10:56:52 +10001344void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001345{
1346 struct ttm_bo_global *glob = ref->object;
1347
1348 kobject_del(&glob->kobj);
1349 kobject_put(&glob->kobj);
1350}
1351EXPORT_SYMBOL(ttm_bo_global_release);
1352
Dave Airlieba4420c2010-03-09 10:56:52 +10001353int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001354{
1355 struct ttm_bo_global_ref *bo_ref =
1356 container_of(ref, struct ttm_bo_global_ref, ref);
1357 struct ttm_bo_global *glob = ref->object;
1358 int ret;
1359
1360 mutex_init(&glob->device_list_mutex);
1361 spin_lock_init(&glob->lru_lock);
1362 glob->mem_glob = bo_ref->mem_glob;
1363 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1364
1365 if (unlikely(glob->dummy_read_page == NULL)) {
1366 ret = -ENOMEM;
1367 goto out_no_drp;
1368 }
1369
1370 INIT_LIST_HEAD(&glob->swap_lru);
1371 INIT_LIST_HEAD(&glob->device_list);
1372
1373 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1374 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1375 if (unlikely(ret != 0)) {
1376 printk(KERN_ERR TTM_PFX
1377 "Could not register buffer object swapout.\n");
1378 goto out_no_shrink;
1379 }
1380
1381 glob->ttm_bo_extra_size =
1382 ttm_round_pot(sizeof(struct ttm_tt)) +
1383 ttm_round_pot(sizeof(struct ttm_backend));
1384
1385 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1386 ttm_round_pot(sizeof(struct ttm_buffer_object));
1387
1388 atomic_set(&glob->bo_count, 0);
1389
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001390 ret = kobject_init_and_add(
1391 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001392 if (unlikely(ret != 0))
1393 kobject_put(&glob->kobj);
1394 return ret;
1395out_no_shrink:
1396 __free_page(glob->dummy_read_page);
1397out_no_drp:
1398 kfree(glob);
1399 return ret;
1400}
1401EXPORT_SYMBOL(ttm_bo_global_init);
1402
1403
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001404int ttm_bo_device_release(struct ttm_bo_device *bdev)
1405{
1406 int ret = 0;
1407 unsigned i = TTM_NUM_MEM_TYPES;
1408 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001409 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001410
1411 while (i--) {
1412 man = &bdev->man[i];
1413 if (man->has_type) {
1414 man->use_type = false;
1415 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1416 ret = -EBUSY;
1417 printk(KERN_ERR TTM_PFX
1418 "DRM memory manager type %d "
1419 "is not clean.\n", i);
1420 }
1421 man->has_type = false;
1422 }
1423 }
1424
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001425 mutex_lock(&glob->device_list_mutex);
1426 list_del(&bdev->device_list);
1427 mutex_unlock(&glob->device_list_mutex);
1428
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001429 if (!cancel_delayed_work(&bdev->wq))
1430 flush_scheduled_work();
1431
1432 while (ttm_bo_delayed_delete(bdev, true))
1433 ;
1434
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001435 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001436 if (list_empty(&bdev->ddestroy))
1437 TTM_DEBUG("Delayed destroy list was clean\n");
1438
1439 if (list_empty(&bdev->man[0].lru))
1440 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001441 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001442
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001443 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1444 write_lock(&bdev->vm_lock);
1445 drm_mm_takedown(&bdev->addr_space_mm);
1446 write_unlock(&bdev->vm_lock);
1447
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001448 return ret;
1449}
1450EXPORT_SYMBOL(ttm_bo_device_release);
1451
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001452int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001453 struct ttm_bo_global *glob,
1454 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001455 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001456 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001457{
1458 int ret = -EINVAL;
1459
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001460 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001461 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001462
1463 memset(bdev->man, 0, sizeof(bdev->man));
1464
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001465 /*
1466 * Initialize the system memory buffer type.
1467 * Other types need to be driver / IOCTL initialized.
1468 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001469 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001470 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001471 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001472
1473 bdev->addr_space_rb = RB_ROOT;
1474 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1475 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001476 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001477
1478 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1479 bdev->nice_mode = true;
1480 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001481 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001482 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001483 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001484
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001485 mutex_lock(&glob->device_list_mutex);
1486 list_add_tail(&bdev->device_list, &glob->device_list);
1487 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488
1489 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001490out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001491 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001492out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001493 return ret;
1494}
1495EXPORT_SYMBOL(ttm_bo_device_init);
1496
1497/*
1498 * buffer object vm functions.
1499 */
1500
1501bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1502{
1503 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1504
1505 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1506 if (mem->mem_type == TTM_PL_SYSTEM)
1507 return false;
1508
1509 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1510 return false;
1511
1512 if (mem->placement & TTM_PL_FLAG_CACHED)
1513 return false;
1514 }
1515 return true;
1516}
1517
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001518void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1519{
1520 struct ttm_bo_device *bdev = bo->bdev;
1521 loff_t offset = (loff_t) bo->addr_space_offset;
1522 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1523
1524 if (!bdev->dev_mapping)
1525 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Jerome Glisse82c5da62010-04-09 14:39:23 +02001527 ttm_mem_io_free(bdev, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001528}
Dave Airliee024e112009-06-24 09:48:08 +10001529EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001530
1531static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1532{
1533 struct ttm_bo_device *bdev = bo->bdev;
1534 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1535 struct rb_node *parent = NULL;
1536 struct ttm_buffer_object *cur_bo;
1537 unsigned long offset = bo->vm_node->start;
1538 unsigned long cur_offset;
1539
1540 while (*cur) {
1541 parent = *cur;
1542 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1543 cur_offset = cur_bo->vm_node->start;
1544 if (offset < cur_offset)
1545 cur = &parent->rb_left;
1546 else if (offset > cur_offset)
1547 cur = &parent->rb_right;
1548 else
1549 BUG();
1550 }
1551
1552 rb_link_node(&bo->vm_rb, parent, cur);
1553 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1554}
1555
1556/**
1557 * ttm_bo_setup_vm:
1558 *
1559 * @bo: the buffer to allocate address space for
1560 *
1561 * Allocate address space in the drm device so that applications
1562 * can mmap the buffer and access the contents. This only
1563 * applies to ttm_bo_type_device objects as others are not
1564 * placed in the drm device address space.
1565 */
1566
1567static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1568{
1569 struct ttm_bo_device *bdev = bo->bdev;
1570 int ret;
1571
1572retry_pre_get:
1573 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1574 if (unlikely(ret != 0))
1575 return ret;
1576
1577 write_lock(&bdev->vm_lock);
1578 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1579 bo->mem.num_pages, 0, 0);
1580
1581 if (unlikely(bo->vm_node == NULL)) {
1582 ret = -ENOMEM;
1583 goto out_unlock;
1584 }
1585
1586 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1587 bo->mem.num_pages, 0);
1588
1589 if (unlikely(bo->vm_node == NULL)) {
1590 write_unlock(&bdev->vm_lock);
1591 goto retry_pre_get;
1592 }
1593
1594 ttm_bo_vm_insert_rb(bo);
1595 write_unlock(&bdev->vm_lock);
1596 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1597
1598 return 0;
1599out_unlock:
1600 write_unlock(&bdev->vm_lock);
1601 return ret;
1602}
1603
1604int ttm_bo_wait(struct ttm_buffer_object *bo,
1605 bool lazy, bool interruptible, bool no_wait)
1606{
1607 struct ttm_bo_driver *driver = bo->bdev->driver;
1608 void *sync_obj;
1609 void *sync_obj_arg;
1610 int ret = 0;
1611
1612 if (likely(bo->sync_obj == NULL))
1613 return 0;
1614
1615 while (bo->sync_obj) {
1616
1617 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1618 void *tmp_obj = bo->sync_obj;
1619 bo->sync_obj = NULL;
1620 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1621 spin_unlock(&bo->lock);
1622 driver->sync_obj_unref(&tmp_obj);
1623 spin_lock(&bo->lock);
1624 continue;
1625 }
1626
1627 if (no_wait)
1628 return -EBUSY;
1629
1630 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1631 sync_obj_arg = bo->sync_obj_arg;
1632 spin_unlock(&bo->lock);
1633 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1634 lazy, interruptible);
1635 if (unlikely(ret != 0)) {
1636 driver->sync_obj_unref(&sync_obj);
1637 spin_lock(&bo->lock);
1638 return ret;
1639 }
1640 spin_lock(&bo->lock);
1641 if (likely(bo->sync_obj == sync_obj &&
1642 bo->sync_obj_arg == sync_obj_arg)) {
1643 void *tmp_obj = bo->sync_obj;
1644 bo->sync_obj = NULL;
1645 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1646 &bo->priv_flags);
1647 spin_unlock(&bo->lock);
1648 driver->sync_obj_unref(&sync_obj);
1649 driver->sync_obj_unref(&tmp_obj);
1650 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001651 } else {
1652 spin_unlock(&bo->lock);
1653 driver->sync_obj_unref(&sync_obj);
1654 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001655 }
1656 }
1657 return 0;
1658}
1659EXPORT_SYMBOL(ttm_bo_wait);
1660
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001661int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1662{
1663 int ret = 0;
1664
1665 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001666 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001667 */
1668
1669 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1670 if (unlikely(ret != 0))
1671 return ret;
1672 spin_lock(&bo->lock);
1673 ret = ttm_bo_wait(bo, false, true, no_wait);
1674 spin_unlock(&bo->lock);
1675 if (likely(ret == 0))
1676 atomic_inc(&bo->cpu_writers);
1677 ttm_bo_unreserve(bo);
1678 return ret;
1679}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001680EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001681
1682void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1683{
1684 if (atomic_dec_and_test(&bo->cpu_writers))
1685 wake_up_all(&bo->event_queue);
1686}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001687EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001688
1689/**
1690 * A buffer object shrink method that tries to swap out the first
1691 * buffer object on the bo_global::swap_lru list.
1692 */
1693
1694static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1695{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001696 struct ttm_bo_global *glob =
1697 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001698 struct ttm_buffer_object *bo;
1699 int ret = -EBUSY;
1700 int put_count;
1701 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1702
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001703 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001704 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001705 if (unlikely(list_empty(&glob->swap_lru))) {
1706 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001707 return -EBUSY;
1708 }
1709
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001710 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001711 struct ttm_buffer_object, swap);
1712 kref_get(&bo->list_kref);
1713
1714 /**
1715 * Reserve buffer. Since we unlock while sleeping, we need
1716 * to re-check that nobody removed us from the swap-list while
1717 * we slept.
1718 */
1719
1720 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1721 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001722 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001723 ttm_bo_wait_unreserved(bo, false);
1724 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001725 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001726 }
1727 }
1728
1729 BUG_ON(ret != 0);
1730 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001731 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001732
1733 while (put_count--)
1734 kref_put(&bo->list_kref, ttm_bo_ref_bug);
1735
1736 /**
1737 * Wait for GPU, then move to system cached.
1738 */
1739
1740 spin_lock(&bo->lock);
1741 ret = ttm_bo_wait(bo, false, false, false);
1742 spin_unlock(&bo->lock);
1743
1744 if (unlikely(ret != 0))
1745 goto out;
1746
1747 if ((bo->mem.placement & swap_placement) != swap_placement) {
1748 struct ttm_mem_reg evict_mem;
1749
1750 evict_mem = bo->mem;
1751 evict_mem.mm_node = NULL;
1752 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1753 evict_mem.mem_type = TTM_PL_SYSTEM;
1754
1755 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001756 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001757 if (unlikely(ret != 0))
1758 goto out;
1759 }
1760
1761 ttm_bo_unmap_virtual(bo);
1762
1763 /**
1764 * Swap out. Buffer will be swapped in again as soon as
1765 * anyone tries to access a ttm page.
1766 */
1767
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001768 if (bo->bdev->driver->swap_notify)
1769 bo->bdev->driver->swap_notify(bo);
1770
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001771 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1772out:
1773
1774 /**
1775 *
1776 * Unreserve without putting on LRU to avoid swapping out an
1777 * already swapped buffer.
1778 */
1779
1780 atomic_set(&bo->reserved, 0);
1781 wake_up_all(&bo->event_queue);
1782 kref_put(&bo->list_kref, ttm_bo_release_list);
1783 return ret;
1784}
1785
1786void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1787{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001788 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001789 ;
1790}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001791EXPORT_SYMBOL(ttm_bo_swapout_all);