blob: cf2ec562550ec3fda20abc37dc6d49b94aab6be6 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
31#include "ttm/ttm_module.h"
32#include "ttm/ttm_bo_driver.h"
33#include "ttm/ttm_placement.h"
34#include <linux/jiffies.h>
35#include <linux/slab.h>
36#include <linux/sched.h>
37#include <linux/mm.h>
38#include <linux/file.h>
39#include <linux/module.h>
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +020040#include <asm/atomic.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020041
42#define TTM_ASSERT_LOCKED(param)
43#define TTM_DEBUG(fmt, arg...)
44#define TTM_BO_HASH_ORDER 13
45
46static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020047static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020048static void ttm_bo_global_kobj_release(struct kobject *kobj);
49
50static struct attribute ttm_bo_count = {
51 .name = "bo_count",
52 .mode = S_IRUGO
53};
54
Jerome Glissefb53f862009-12-09 21:55:10 +010055static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
56{
57 int i;
58
59 for (i = 0; i <= TTM_PL_PRIV5; i++)
60 if (flags & (1 << i)) {
61 *mem_type = i;
62 return 0;
63 }
64 return -EINVAL;
65}
66
Jerome Glisse5012f502009-12-10 18:07:26 +010067static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010068{
Jerome Glisse5012f502009-12-10 18:07:26 +010069 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
70
Jerome Glissefb53f862009-12-09 21:55:10 +010071 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
72 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
73 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
74 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010075 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
Jerome Glissefb53f862009-12-09 21:55:10 +010076 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
77 man->available_caching);
78 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
79 man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100080 if (mem_type != TTM_PL_SYSTEM)
81 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010082}
83
84static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
85 struct ttm_placement *placement)
86{
Jerome Glissefb53f862009-12-09 21:55:10 +010087 int i, ret, mem_type;
88
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010089 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
Jerome Glissefb53f862009-12-09 21:55:10 +010090 bo, bo->mem.num_pages, bo->mem.size >> 10,
91 bo->mem.size >> 20);
92 for (i = 0; i < placement->num_placement; i++) {
93 ret = ttm_mem_type_from_flags(placement->placement[i],
94 &mem_type);
95 if (ret)
96 return;
Jerome Glissefb53f862009-12-09 21:55:10 +010097 printk(KERN_ERR TTM_PFX " placement[%d]=0x%08X (%d)\n",
98 i, placement->placement[i], mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +010099 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100100 }
101}
102
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200103static ssize_t ttm_bo_global_show(struct kobject *kobj,
104 struct attribute *attr,
105 char *buffer)
106{
107 struct ttm_bo_global *glob =
108 container_of(kobj, struct ttm_bo_global, kobj);
109
110 return snprintf(buffer, PAGE_SIZE, "%lu\n",
111 (unsigned long) atomic_read(&glob->bo_count));
112}
113
114static struct attribute *ttm_bo_global_attrs[] = {
115 &ttm_bo_count,
116 NULL
117};
118
Emese Revfy52cf25d2010-01-19 02:58:23 +0100119static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200120 .show = &ttm_bo_global_show
121};
122
123static struct kobj_type ttm_bo_glob_kobj_type = {
124 .release = &ttm_bo_global_kobj_release,
125 .sysfs_ops = &ttm_bo_global_ops,
126 .default_attrs = ttm_bo_global_attrs
127};
128
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200129
130static inline uint32_t ttm_bo_type_flags(unsigned type)
131{
132 return 1 << (type);
133}
134
135static void ttm_bo_release_list(struct kref *list_kref)
136{
137 struct ttm_buffer_object *bo =
138 container_of(list_kref, struct ttm_buffer_object, list_kref);
139 struct ttm_bo_device *bdev = bo->bdev;
140
141 BUG_ON(atomic_read(&bo->list_kref.refcount));
142 BUG_ON(atomic_read(&bo->kref.refcount));
143 BUG_ON(atomic_read(&bo->cpu_writers));
144 BUG_ON(bo->sync_obj != NULL);
145 BUG_ON(bo->mem.mm_node != NULL);
146 BUG_ON(!list_empty(&bo->lru));
147 BUG_ON(!list_empty(&bo->ddestroy));
148
149 if (bo->ttm)
150 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200151 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200152 if (bo->destroy)
153 bo->destroy(bo);
154 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200155 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 kfree(bo);
157 }
158}
159
160int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
161{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200162 if (interruptible) {
Jean Delvare965d3802010-10-09 12:36:45 +0000163 return wait_event_interruptible(bo->event_queue,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164 atomic_read(&bo->reserved) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165 } else {
166 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
Jean Delvare965d3802010-10-09 12:36:45 +0000167 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200168 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000170EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200171
Dave Airlied6ea8882010-11-22 13:24:40 +1000172void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200173{
174 struct ttm_bo_device *bdev = bo->bdev;
175 struct ttm_mem_type_manager *man;
176
177 BUG_ON(!atomic_read(&bo->reserved));
178
179 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
180
181 BUG_ON(!list_empty(&bo->lru));
182
183 man = &bdev->man[bo->mem.mem_type];
184 list_add_tail(&bo->lru, &man->lru);
185 kref_get(&bo->list_kref);
186
187 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200188 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200189 kref_get(&bo->list_kref);
190 }
191 }
192}
193
Dave Airlied6ea8882010-11-22 13:24:40 +1000194int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200195{
196 int put_count = 0;
197
198 if (!list_empty(&bo->swap)) {
199 list_del_init(&bo->swap);
200 ++put_count;
201 }
202 if (!list_empty(&bo->lru)) {
203 list_del_init(&bo->lru);
204 ++put_count;
205 }
206
207 /*
208 * TODO: Add a driver hook to delete from
209 * driver-specific LRU's here.
210 */
211
212 return put_count;
213}
214
215int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
216 bool interruptible,
217 bool no_wait, bool use_sequence, uint32_t sequence)
218{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200219 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200220 int ret;
221
222 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100223 /**
224 * Deadlock avoidance for multi-bo reserving.
225 */
Thomas Hellstrom96726fe2010-11-17 12:28:28 +0000226 if (use_sequence && bo->seq_valid) {
227 /**
228 * We've already reserved this one.
229 */
230 if (unlikely(sequence == bo->val_seq))
231 return -EDEADLK;
232 /**
233 * Already reserved by a thread that will not back
234 * off for us. We need to back off.
235 */
236 if (unlikely(sequence - bo->val_seq < (1 << 31)))
237 return -EAGAIN;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200238 }
239
240 if (no_wait)
241 return -EBUSY;
242
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200243 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200244 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200245 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200246
247 if (unlikely(ret))
248 return ret;
249 }
250
251 if (use_sequence) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100252 /**
253 * Wake up waiters that may need to recheck for deadlock,
254 * if we decreased the sequence number.
255 */
256 if (unlikely((bo->val_seq - sequence < (1 << 31))
257 || !bo->seq_valid))
258 wake_up_all(&bo->event_queue);
259
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200260 bo->val_seq = sequence;
261 bo->seq_valid = true;
262 } else {
263 bo->seq_valid = false;
264 }
265
266 return 0;
267}
268EXPORT_SYMBOL(ttm_bo_reserve);
269
270static void ttm_bo_ref_bug(struct kref *list_kref)
271{
272 BUG();
273}
274
Dave Airlied6ea8882010-11-22 13:24:40 +1000275void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
276 bool never_free)
277{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100278 kref_sub(&bo->list_kref, count,
279 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000280}
281
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200282int ttm_bo_reserve(struct ttm_buffer_object *bo,
283 bool interruptible,
284 bool no_wait, bool use_sequence, uint32_t sequence)
285{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200286 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200287 int put_count = 0;
288 int ret;
289
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200290 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200291 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
292 sequence);
293 if (likely(ret == 0))
294 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200295 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200296
Dave Airlied6ea8882010-11-22 13:24:40 +1000297 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200298
299 return ret;
300}
301
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000302void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
303{
304 ttm_bo_add_to_lru(bo);
305 atomic_set(&bo->reserved, 0);
306 wake_up_all(&bo->event_queue);
307}
308
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200309void ttm_bo_unreserve(struct ttm_buffer_object *bo)
310{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200311 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200312
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200313 spin_lock(&glob->lru_lock);
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000314 ttm_bo_unreserve_locked(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200315 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200316}
317EXPORT_SYMBOL(ttm_bo_unreserve);
318
319/*
320 * Call bo->mutex locked.
321 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200322static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
323{
324 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200325 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326 int ret = 0;
327 uint32_t page_flags = 0;
328
329 TTM_ASSERT_LOCKED(&bo->mutex);
330 bo->ttm = NULL;
331
Dave Airliead49f502009-07-10 22:36:26 +1000332 if (bdev->need_dma32)
333 page_flags |= TTM_PAGE_FLAG_DMA32;
334
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200335 switch (bo->type) {
336 case ttm_bo_type_device:
337 if (zero_alloc)
338 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
339 case ttm_bo_type_kernel:
340 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200341 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200342 if (unlikely(bo->ttm == NULL))
343 ret = -ENOMEM;
344 break;
345 case ttm_bo_type_user:
346 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
347 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200348 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000349 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200350 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000351 break;
352 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200353
354 ret = ttm_tt_set_user(bo->ttm, current,
355 bo->buffer_start, bo->num_pages);
356 if (unlikely(ret != 0))
357 ttm_tt_destroy(bo->ttm);
358 break;
359 default:
360 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
361 ret = -EINVAL;
362 break;
363 }
364
365 return ret;
366}
367
368static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
369 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000370 bool evict, bool interruptible,
371 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200372{
373 struct ttm_bo_device *bdev = bo->bdev;
374 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
375 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
376 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
377 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
378 int ret = 0;
379
380 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100381 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
382 ret = ttm_mem_io_lock(old_man, true);
383 if (unlikely(ret != 0))
384 goto out_err;
385 ttm_bo_unmap_virtual_locked(bo);
386 ttm_mem_io_unlock(old_man);
387 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200388
389 /*
390 * Create and bind a ttm if required.
391 */
392
393 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
394 ret = ttm_bo_add_ttm(bo, false);
395 if (ret)
396 goto out_err;
397
398 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
399 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200400 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200401
402 if (mem->mem_type != TTM_PL_SYSTEM) {
403 ret = ttm_tt_bind(bo->ttm, mem);
404 if (ret)
405 goto out_err;
406 }
407
408 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100409 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200410 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200411 goto moved;
412 }
413
414 }
415
Dave Airliee024e112009-06-24 09:48:08 +1000416 if (bdev->driver->move_notify)
417 bdev->driver->move_notify(bo, mem);
418
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200419 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
420 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000421 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200422 else if (bdev->driver->move)
423 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000424 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200425 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000426 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200427
428 if (ret)
429 goto out_err;
430
431moved:
432 if (bo->evicted) {
433 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
434 if (ret)
435 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
436 bo->evicted = false;
437 }
438
439 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000440 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200441 bdev->man[bo->mem.mem_type].gpu_offset;
442 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100443 } else
444 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200445
446 return 0;
447
448out_err:
449 new_man = &bdev->man[bo->mem.mem_type];
450 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
451 ttm_tt_unbind(bo->ttm);
452 ttm_tt_destroy(bo->ttm);
453 bo->ttm = NULL;
454 }
455
456 return ret;
457}
458
459/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200460 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200461 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200462 * This is the place to put in driver specific hooks to release
463 * driver private resources.
464 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200465 */
466
467static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
468{
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200469 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200470 ttm_tt_unbind(bo->ttm);
471 ttm_tt_destroy(bo->ttm);
472 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200473 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200474 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200475
476 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200477
478 /*
479 * Make processes trying to reserve really pick it up.
480 */
481 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200482 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200483}
484
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200485static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200486{
487 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200488 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200489 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000490 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200491 void *sync_obj_arg;
492 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200493 int ret;
494
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000495 spin_lock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200496 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200497 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200498
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200499 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100500
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200501 /**
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000502 * Lock inversion between bo:reserve and bdev::fence_lock here,
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200503 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200504 */
505
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200506 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200507
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200508 if (unlikely(ret == -EBUSY))
509 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200510
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000511 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200512 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200513
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200514 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200515 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200516
Dave Airlied6ea8882010-11-22 13:24:40 +1000517 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200518
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200519 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200520 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200521 spin_lock(&glob->lru_lock);
522 }
523queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200524 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000525 if (bo->sync_obj)
526 sync_obj = driver->sync_obj_ref(bo->sync_obj);
527 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200528
529 kref_get(&bo->list_kref);
530 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
531 spin_unlock(&glob->lru_lock);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000532 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200533
Thomas Hellstromaa123262010-11-02 13:21:47 +0000534 if (sync_obj) {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200535 driver->sync_obj_flush(sync_obj, sync_obj_arg);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000536 driver->sync_obj_unref(&sync_obj);
537 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200538 schedule_delayed_work(&bdev->wq,
539 ((HZ / 100) < 1) ? 1 : HZ / 100);
540}
541
542/**
543 * function ttm_bo_cleanup_refs
544 * If bo idle, remove from delayed- and lru lists, and unref.
545 * If not idle, do nothing.
546 *
547 * @interruptible Any sleeps should occur interruptibly.
548 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
549 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
550 */
551
552static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
553 bool interruptible,
554 bool no_wait_reserve,
555 bool no_wait_gpu)
556{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000557 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200558 struct ttm_bo_global *glob = bo->glob;
559 int put_count;
560 int ret = 0;
561
562retry:
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000563 spin_lock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200564 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000565 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200566
567 if (unlikely(ret != 0))
568 return ret;
569
570 spin_lock(&glob->lru_lock);
571 ret = ttm_bo_reserve_locked(bo, interruptible,
572 no_wait_reserve, false, 0);
573
574 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200575 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200576 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200577 }
578
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200579 /**
580 * We can re-check for sync object without taking
581 * the bo::lock since setting the sync object requires
582 * also bo::reserved. A busy object at this point may
583 * be caused by another thread recently starting an accelerated
584 * eviction.
585 */
586
587 if (unlikely(bo->sync_obj)) {
588 atomic_set(&bo->reserved, 0);
589 wake_up_all(&bo->event_queue);
590 spin_unlock(&glob->lru_lock);
591 goto retry;
592 }
593
594 put_count = ttm_bo_del_from_lru(bo);
595 list_del_init(&bo->ddestroy);
596 ++put_count;
597
598 spin_unlock(&glob->lru_lock);
599 ttm_bo_cleanup_memtype_use(bo);
600
Dave Airlied6ea8882010-11-22 13:24:40 +1000601 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200602
603 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200604}
605
606/**
607 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
608 * encountered buffers.
609 */
610
611static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
612{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200613 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100614 struct ttm_buffer_object *entry = NULL;
615 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200617 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100618 if (list_empty(&bdev->ddestroy))
619 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200620
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100621 entry = list_first_entry(&bdev->ddestroy,
622 struct ttm_buffer_object, ddestroy);
623 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200624
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100625 for (;;) {
626 struct ttm_buffer_object *nentry = NULL;
627
628 if (entry->ddestroy.next != &bdev->ddestroy) {
629 nentry = list_first_entry(&entry->ddestroy,
630 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200631 kref_get(&nentry->list_kref);
632 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200633
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200634 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200635 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
636 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200637 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100638 entry = nentry;
639
640 if (ret || !entry)
641 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200642
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200643 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100644 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200645 break;
646 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200647
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100648out_unlock:
649 spin_unlock(&glob->lru_lock);
650out:
651 if (entry)
652 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200653 return ret;
654}
655
656static void ttm_bo_delayed_workqueue(struct work_struct *work)
657{
658 struct ttm_bo_device *bdev =
659 container_of(work, struct ttm_bo_device, wq.work);
660
661 if (ttm_bo_delayed_delete(bdev, false)) {
662 schedule_delayed_work(&bdev->wq,
663 ((HZ / 100) < 1) ? 1 : HZ / 100);
664 }
665}
666
667static void ttm_bo_release(struct kref *kref)
668{
669 struct ttm_buffer_object *bo =
670 container_of(kref, struct ttm_buffer_object, kref);
671 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100672 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200673
674 if (likely(bo->vm_node != NULL)) {
675 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
676 drm_mm_put_block(bo->vm_node);
677 bo->vm_node = NULL;
678 }
679 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100680 ttm_mem_io_lock(man, false);
681 ttm_mem_io_free_vm(bo);
682 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200683 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200684 kref_put(&bo->list_kref, ttm_bo_release_list);
685 write_lock(&bdev->vm_lock);
686}
687
688void ttm_bo_unref(struct ttm_buffer_object **p_bo)
689{
690 struct ttm_buffer_object *bo = *p_bo;
691 struct ttm_bo_device *bdev = bo->bdev;
692
693 *p_bo = NULL;
694 write_lock(&bdev->vm_lock);
695 kref_put(&bo->kref, ttm_bo_release);
696 write_unlock(&bdev->vm_lock);
697}
698EXPORT_SYMBOL(ttm_bo_unref);
699
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400700int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
701{
702 return cancel_delayed_work_sync(&bdev->wq);
703}
704EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
705
706void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
707{
708 if (resched)
709 schedule_delayed_work(&bdev->wq,
710 ((HZ / 100) < 1) ? 1 : HZ / 100);
711}
712EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
713
Jerome Glisseca262a9992009-12-08 15:33:32 +0100714static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000715 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200716{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200717 struct ttm_bo_device *bdev = bo->bdev;
718 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100719 struct ttm_placement placement;
720 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200721
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000722 spin_lock(&bdev->fence_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000723 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000724 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200725
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200726 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100727 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200728 printk(KERN_ERR TTM_PFX
729 "Failed to expire sync object before "
730 "buffer eviction.\n");
731 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200732 goto out;
733 }
734
735 BUG_ON(!atomic_read(&bo->reserved));
736
737 evict_mem = bo->mem;
738 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100739 evict_mem.bus.io_reserved_vm = false;
740 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200741
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100742 placement.fpfn = 0;
743 placement.lpfn = 0;
744 placement.num_placement = 0;
745 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100746 bdev->driver->evict_flags(bo, &placement);
747 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000748 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200749 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100750 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200751 printk(KERN_ERR TTM_PFX
752 "Failed to find memory space for "
753 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100754 ttm_bo_mem_space_debug(bo, &placement);
755 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200756 goto out;
757 }
758
759 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000760 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200761 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100762 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200763 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000764 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200765 goto out;
766 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200767 bo->evicted = true;
768out:
769 return ret;
770}
771
Jerome Glisseca262a9992009-12-08 15:33:32 +0100772static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
773 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000774 bool interruptible, bool no_wait_reserve,
775 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100776{
777 struct ttm_bo_global *glob = bdev->glob;
778 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
779 struct ttm_buffer_object *bo;
780 int ret, put_count = 0;
781
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100782retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100783 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100784 if (list_empty(&man->lru)) {
785 spin_unlock(&glob->lru_lock);
786 return -EBUSY;
787 }
788
Jerome Glisseca262a9992009-12-08 15:33:32 +0100789 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
790 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100791
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200792 if (!list_empty(&bo->ddestroy)) {
793 spin_unlock(&glob->lru_lock);
794 ret = ttm_bo_cleanup_refs(bo, interruptible,
795 no_wait_reserve, no_wait_gpu);
796 kref_put(&bo->list_kref, ttm_bo_release_list);
797
798 if (likely(ret == 0 || ret == -ERESTARTSYS))
799 return ret;
800
801 goto retry;
802 }
803
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000804 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100805
806 if (unlikely(ret == -EBUSY)) {
807 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000808 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100809 ret = ttm_bo_wait_unreserved(bo, interruptible);
810
811 kref_put(&bo->list_kref, ttm_bo_release_list);
812
813 /**
814 * We *need* to retry after releasing the lru lock.
815 */
816
817 if (unlikely(ret != 0))
818 return ret;
819 goto retry;
820 }
821
822 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100823 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100824
825 BUG_ON(ret != 0);
826
Dave Airlied6ea8882010-11-22 13:24:40 +1000827 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100828
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000829 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100830 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100831
Jerome Glisseca262a9992009-12-08 15:33:32 +0100832 kref_put(&bo->list_kref, ttm_bo_release_list);
833 return ret;
834}
835
Ben Skeggs42311ff2010-08-04 12:07:08 +1000836void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
837{
Ben Skeggsd961db72010-08-05 10:48:18 +1000838 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000839
Ben Skeggsd961db72010-08-05 10:48:18 +1000840 if (mem->mm_node)
841 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000842}
843EXPORT_SYMBOL(ttm_bo_mem_put);
844
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200845/**
846 * Repeatedly evict memory from the LRU for @mem_type until we create enough
847 * space, or we've evicted everything and there isn't enough space.
848 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100849static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
850 uint32_t mem_type,
851 struct ttm_placement *placement,
852 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000853 bool interruptible,
854 bool no_wait_reserve,
855 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200856{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100857 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200858 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200859 int ret;
860
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200861 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000862 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200863 if (unlikely(ret != 0))
864 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000865 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100866 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100867 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000868 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100869 if (unlikely(ret != 0))
870 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200871 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000872 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200873 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200874 mem->mem_type = mem_type;
875 return 0;
876}
877
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200878static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
879 uint32_t cur_placement,
880 uint32_t proposed_placement)
881{
882 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
883 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
884
885 /**
886 * Keep current caching if possible.
887 */
888
889 if ((cur_placement & caching) != 0)
890 result |= (cur_placement & caching);
891 else if ((man->default_caching & caching) != 0)
892 result |= man->default_caching;
893 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
894 result |= TTM_PL_FLAG_CACHED;
895 else if ((TTM_PL_FLAG_WC & caching) != 0)
896 result |= TTM_PL_FLAG_WC;
897 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
898 result |= TTM_PL_FLAG_UNCACHED;
899
900 return result;
901}
902
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200903static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
904 bool disallow_fixed,
905 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200906 uint32_t proposed_placement,
907 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200908{
909 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
910
911 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
912 return false;
913
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200914 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200915 return false;
916
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200917 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200918 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200919
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200920 cur_flags |= (proposed_placement & man->available_caching);
921
922 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923 return true;
924}
925
926/**
927 * Creates space for memory region @mem according to its type.
928 *
929 * This function first searches for free space in compatible memory types in
930 * the priority order defined by the driver. If free space isn't found, then
931 * ttm_bo_mem_force_space is attempted in priority order to evict and find
932 * space.
933 */
934int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100935 struct ttm_placement *placement,
936 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000937 bool interruptible, bool no_wait_reserve,
938 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200939{
940 struct ttm_bo_device *bdev = bo->bdev;
941 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200942 uint32_t mem_type = TTM_PL_SYSTEM;
943 uint32_t cur_flags = 0;
944 bool type_found = false;
945 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100946 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100947 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200948
949 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000950 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100951 ret = ttm_mem_type_from_flags(placement->placement[i],
952 &mem_type);
953 if (ret)
954 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200955 man = &bdev->man[mem_type];
956
957 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100958 bo->type == ttm_bo_type_user,
959 mem_type,
960 placement->placement[i],
961 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200962
963 if (!type_ok)
964 continue;
965
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200966 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
967 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100968 /*
969 * Use the access and other non-mapping-related flag bits from
970 * the memory placement flags to the current flags
971 */
972 ttm_flag_masked(&cur_flags, placement->placement[i],
973 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200974
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200975 if (mem_type == TTM_PL_SYSTEM)
976 break;
977
978 if (man->has_type && man->use_type) {
979 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000980 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100981 if (unlikely(ret))
982 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200983 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000984 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200985 break;
986 }
987
Ben Skeggsd961db72010-08-05 10:48:18 +1000988 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989 mem->mem_type = mem_type;
990 mem->placement = cur_flags;
991 return 0;
992 }
993
994 if (!type_found)
995 return -EINVAL;
996
Dave Airlieb6637522009-12-14 14:51:35 +1000997 for (i = 0; i < placement->num_busy_placement; ++i) {
998 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100999 &mem_type);
1000 if (ret)
1001 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001002 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001003 if (!man->has_type)
1004 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001006 bo->type == ttm_bo_type_user,
1007 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001008 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001009 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001010 continue;
1011
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001012 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1013 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001014 /*
1015 * Use the access and other non-mapping-related flag bits from
1016 * the memory placement flags to the current flags
1017 */
Dave Airlieb6637522009-12-14 14:51:35 +10001018 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001019 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001020
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001021
1022 if (mem_type == TTM_PL_SYSTEM) {
1023 mem->mem_type = mem_type;
1024 mem->placement = cur_flags;
1025 mem->mm_node = NULL;
1026 return 0;
1027 }
1028
Jerome Glisseca262a9992009-12-08 15:33:32 +01001029 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001030 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001031 if (ret == 0 && mem->mm_node) {
1032 mem->placement = cur_flags;
1033 return 0;
1034 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001035 if (ret == -ERESTARTSYS)
1036 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001038 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001039 return ret;
1040}
1041EXPORT_SYMBOL(ttm_bo_mem_space);
1042
1043int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1044{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001045 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1046 return -EBUSY;
1047
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001048 return wait_event_interruptible(bo->event_queue,
1049 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001051EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052
1053int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001054 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001055 bool interruptible, bool no_wait_reserve,
1056 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001057{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001058 int ret = 0;
1059 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001060 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001061
1062 BUG_ON(!atomic_read(&bo->reserved));
1063
1064 /*
1065 * FIXME: It's possible to pipeline buffer moves.
1066 * Have the driver move function wait for idle when necessary,
1067 * instead of doing it here.
1068 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001069 spin_lock(&bdev->fence_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001070 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001071 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001072 if (ret)
1073 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074 mem.num_pages = bo->num_pages;
1075 mem.size = mem.num_pages << PAGE_SHIFT;
1076 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001077 mem.bus.io_reserved_vm = false;
1078 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001079 /*
1080 * Determine where to move the buffer.
1081 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001082 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001083 if (ret)
1084 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001085 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001087 if (ret && mem.mm_node)
1088 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089 return ret;
1090}
1091
Jerome Glisseca262a9992009-12-08 15:33:32 +01001092static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093 struct ttm_mem_reg *mem)
1094{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001095 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001096
Ben Skeggsd961db72010-08-05 10:48:18 +10001097 if (mem->mm_node && placement->lpfn != 0 &&
1098 (mem->start < placement->fpfn ||
1099 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001100 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101
Jerome Glisseca262a9992009-12-08 15:33:32 +01001102 for (i = 0; i < placement->num_placement; i++) {
1103 if ((placement->placement[i] & mem->placement &
1104 TTM_PL_MASK_CACHING) &&
1105 (placement->placement[i] & mem->placement &
1106 TTM_PL_MASK_MEM))
1107 return i;
1108 }
1109 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110}
1111
Jerome Glisse09855ac2009-12-10 17:16:27 +01001112int ttm_bo_validate(struct ttm_buffer_object *bo,
1113 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001114 bool interruptible, bool no_wait_reserve,
1115 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001116{
1117 int ret;
1118
1119 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001120 /* Check that range is valid */
1121 if (placement->lpfn || placement->fpfn)
1122 if (placement->fpfn > placement->lpfn ||
1123 (placement->lpfn - placement->fpfn) < bo->num_pages)
1124 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125 /*
1126 * Check whether we need to move buffer.
1127 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001128 ret = ttm_bo_mem_compat(placement, &bo->mem);
1129 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001130 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001131 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001132 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001133 } else {
1134 /*
1135 * Use the access and other non-mapping-related flag bits from
1136 * the compatible memory placement flags to the active flags
1137 */
1138 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1139 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001140 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001141 /*
1142 * We might need to add a TTM.
1143 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1145 ret = ttm_bo_add_ttm(bo, true);
1146 if (ret)
1147 return ret;
1148 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149 return 0;
1150}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001151EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152
Jerome Glisse09855ac2009-12-10 17:16:27 +01001153int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1154 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001155{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001156 BUG_ON((placement->fpfn || placement->lpfn) &&
1157 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001159 return 0;
1160}
1161
Jerome Glisse09855ac2009-12-10 17:16:27 +01001162int ttm_bo_init(struct ttm_bo_device *bdev,
1163 struct ttm_buffer_object *bo,
1164 unsigned long size,
1165 enum ttm_bo_type type,
1166 struct ttm_placement *placement,
1167 uint32_t page_alignment,
1168 unsigned long buffer_start,
1169 bool interruptible,
1170 struct file *persistant_swap_storage,
1171 size_t acc_size,
1172 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001173{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001174 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001175 unsigned long num_pages;
1176
1177 size += buffer_start & ~PAGE_MASK;
1178 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1179 if (num_pages == 0) {
1180 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001181 if (destroy)
1182 (*destroy)(bo);
1183 else
1184 kfree(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185 return -EINVAL;
1186 }
1187 bo->destroy = destroy;
1188
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001189 kref_init(&bo->kref);
1190 kref_init(&bo->list_kref);
1191 atomic_set(&bo->cpu_writers, 0);
1192 atomic_set(&bo->reserved, 1);
1193 init_waitqueue_head(&bo->event_queue);
1194 INIT_LIST_HEAD(&bo->lru);
1195 INIT_LIST_HEAD(&bo->ddestroy);
1196 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001197 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001198 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001199 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200 bo->type = type;
1201 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001202 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203 bo->mem.mem_type = TTM_PL_SYSTEM;
1204 bo->mem.num_pages = bo->num_pages;
1205 bo->mem.mm_node = NULL;
1206 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001207 bo->mem.bus.io_reserved_vm = false;
1208 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209 bo->buffer_start = buffer_start & PAGE_MASK;
1210 bo->priv_flags = 0;
1211 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1212 bo->seq_valid = false;
1213 bo->persistant_swap_storage = persistant_swap_storage;
1214 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001215 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001216
Jerome Glisse09855ac2009-12-10 17:16:27 +01001217 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001218 if (unlikely(ret != 0))
1219 goto out_err;
1220
1221 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001222 * For ttm_bo_type_device buffers, allocate
1223 * address space from the device.
1224 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001225 if (bo->type == ttm_bo_type_device) {
1226 ret = ttm_bo_setup_vm(bo);
1227 if (ret)
1228 goto out_err;
1229 }
1230
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001231 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001232 if (ret)
1233 goto out_err;
1234
1235 ttm_bo_unreserve(bo);
1236 return 0;
1237
1238out_err:
1239 ttm_bo_unreserve(bo);
1240 ttm_bo_unref(&bo);
1241
1242 return ret;
1243}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001244EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001245
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001246static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001247 unsigned long num_pages)
1248{
1249 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1250 PAGE_MASK;
1251
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001252 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001253}
1254
Jerome Glisse09855ac2009-12-10 17:16:27 +01001255int ttm_bo_create(struct ttm_bo_device *bdev,
1256 unsigned long size,
1257 enum ttm_bo_type type,
1258 struct ttm_placement *placement,
1259 uint32_t page_alignment,
1260 unsigned long buffer_start,
1261 bool interruptible,
1262 struct file *persistant_swap_storage,
1263 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001264{
1265 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001266 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001267 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001268
1269 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001270 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001271 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272 if (unlikely(ret != 0))
1273 return ret;
1274
1275 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1276
1277 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001278 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001279 return -ENOMEM;
1280 }
1281
Jerome Glisse09855ac2009-12-10 17:16:27 +01001282 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1283 buffer_start, interruptible,
1284 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001285 if (likely(ret == 0))
1286 *p_bo = bo;
1287
1288 return ret;
1289}
1290
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001292 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001293{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001294 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001295 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001296 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001297
1298 /*
1299 * Can't use standard list traversal since we're unlocking.
1300 */
1301
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001302 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001303 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001304 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001305 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001306 if (ret) {
1307 if (allow_errors) {
1308 return ret;
1309 } else {
1310 printk(KERN_ERR TTM_PFX
1311 "Cleanup eviction failed\n");
1312 }
1313 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001314 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001315 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001316 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001317 return 0;
1318}
1319
1320int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1321{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001322 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001323 int ret = -EINVAL;
1324
1325 if (mem_type >= TTM_NUM_MEM_TYPES) {
1326 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1327 return ret;
1328 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001329 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001330
1331 if (!man->has_type) {
1332 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1333 "memory manager type %u\n", mem_type);
1334 return ret;
1335 }
1336
1337 man->use_type = false;
1338 man->has_type = false;
1339
1340 ret = 0;
1341 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001342 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001343
Ben Skeggsd961db72010-08-05 10:48:18 +10001344 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001345 }
1346
1347 return ret;
1348}
1349EXPORT_SYMBOL(ttm_bo_clean_mm);
1350
1351int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1352{
1353 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1354
1355 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1356 printk(KERN_ERR TTM_PFX
1357 "Illegal memory manager memory type %u.\n",
1358 mem_type);
1359 return -EINVAL;
1360 }
1361
1362 if (!man->has_type) {
1363 printk(KERN_ERR TTM_PFX
1364 "Memory type %u has not been initialized.\n",
1365 mem_type);
1366 return 0;
1367 }
1368
Jerome Glisseca262a9992009-12-08 15:33:32 +01001369 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001370}
1371EXPORT_SYMBOL(ttm_bo_evict_mm);
1372
1373int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001374 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001375{
1376 int ret = -EINVAL;
1377 struct ttm_mem_type_manager *man;
1378
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001379 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001380 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001381 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001382 man->io_reserve_fastpath = true;
1383 man->use_io_reserve_lru = false;
1384 mutex_init(&man->io_reserve_mutex);
1385 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001386
1387 ret = bdev->driver->init_mem_type(bdev, type, man);
1388 if (ret)
1389 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001390 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001391
1392 ret = 0;
1393 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001394 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001395 if (ret)
1396 return ret;
1397 }
1398 man->has_type = true;
1399 man->use_type = true;
1400 man->size = p_size;
1401
1402 INIT_LIST_HEAD(&man->lru);
1403
1404 return 0;
1405}
1406EXPORT_SYMBOL(ttm_bo_init_mm);
1407
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001408static void ttm_bo_global_kobj_release(struct kobject *kobj)
1409{
1410 struct ttm_bo_global *glob =
1411 container_of(kobj, struct ttm_bo_global, kobj);
1412
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001413 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1414 __free_page(glob->dummy_read_page);
1415 kfree(glob);
1416}
1417
Dave Airlieba4420c2010-03-09 10:56:52 +10001418void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001419{
1420 struct ttm_bo_global *glob = ref->object;
1421
1422 kobject_del(&glob->kobj);
1423 kobject_put(&glob->kobj);
1424}
1425EXPORT_SYMBOL(ttm_bo_global_release);
1426
Dave Airlieba4420c2010-03-09 10:56:52 +10001427int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001428{
1429 struct ttm_bo_global_ref *bo_ref =
1430 container_of(ref, struct ttm_bo_global_ref, ref);
1431 struct ttm_bo_global *glob = ref->object;
1432 int ret;
1433
1434 mutex_init(&glob->device_list_mutex);
1435 spin_lock_init(&glob->lru_lock);
1436 glob->mem_glob = bo_ref->mem_glob;
1437 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1438
1439 if (unlikely(glob->dummy_read_page == NULL)) {
1440 ret = -ENOMEM;
1441 goto out_no_drp;
1442 }
1443
1444 INIT_LIST_HEAD(&glob->swap_lru);
1445 INIT_LIST_HEAD(&glob->device_list);
1446
1447 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1448 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1449 if (unlikely(ret != 0)) {
1450 printk(KERN_ERR TTM_PFX
1451 "Could not register buffer object swapout.\n");
1452 goto out_no_shrink;
1453 }
1454
1455 glob->ttm_bo_extra_size =
1456 ttm_round_pot(sizeof(struct ttm_tt)) +
1457 ttm_round_pot(sizeof(struct ttm_backend));
1458
1459 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1460 ttm_round_pot(sizeof(struct ttm_buffer_object));
1461
1462 atomic_set(&glob->bo_count, 0);
1463
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001464 ret = kobject_init_and_add(
1465 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001466 if (unlikely(ret != 0))
1467 kobject_put(&glob->kobj);
1468 return ret;
1469out_no_shrink:
1470 __free_page(glob->dummy_read_page);
1471out_no_drp:
1472 kfree(glob);
1473 return ret;
1474}
1475EXPORT_SYMBOL(ttm_bo_global_init);
1476
1477
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001478int ttm_bo_device_release(struct ttm_bo_device *bdev)
1479{
1480 int ret = 0;
1481 unsigned i = TTM_NUM_MEM_TYPES;
1482 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001483 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001484
1485 while (i--) {
1486 man = &bdev->man[i];
1487 if (man->has_type) {
1488 man->use_type = false;
1489 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1490 ret = -EBUSY;
1491 printk(KERN_ERR TTM_PFX
1492 "DRM memory manager type %d "
1493 "is not clean.\n", i);
1494 }
1495 man->has_type = false;
1496 }
1497 }
1498
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001499 mutex_lock(&glob->device_list_mutex);
1500 list_del(&bdev->device_list);
1501 mutex_unlock(&glob->device_list_mutex);
1502
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001503 if (!cancel_delayed_work(&bdev->wq))
1504 flush_scheduled_work();
1505
1506 while (ttm_bo_delayed_delete(bdev, true))
1507 ;
1508
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001509 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510 if (list_empty(&bdev->ddestroy))
1511 TTM_DEBUG("Delayed destroy list was clean\n");
1512
1513 if (list_empty(&bdev->man[0].lru))
1514 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001515 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001516
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1518 write_lock(&bdev->vm_lock);
1519 drm_mm_takedown(&bdev->addr_space_mm);
1520 write_unlock(&bdev->vm_lock);
1521
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522 return ret;
1523}
1524EXPORT_SYMBOL(ttm_bo_device_release);
1525
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001527 struct ttm_bo_global *glob,
1528 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001529 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001530 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531{
1532 int ret = -EINVAL;
1533
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001535 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536
1537 memset(bdev->man, 0, sizeof(bdev->man));
1538
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001539 /*
1540 * Initialize the system memory buffer type.
1541 * Other types need to be driver / IOCTL initialized.
1542 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001543 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001544 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001545 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001546
1547 bdev->addr_space_rb = RB_ROOT;
1548 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1549 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001550 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001551
1552 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1553 bdev->nice_mode = true;
1554 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001555 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001556 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001557 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001558 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001559 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001560 mutex_lock(&glob->device_list_mutex);
1561 list_add_tail(&bdev->device_list, &glob->device_list);
1562 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563
1564 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001565out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001566 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001567out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001568 return ret;
1569}
1570EXPORT_SYMBOL(ttm_bo_device_init);
1571
1572/*
1573 * buffer object vm functions.
1574 */
1575
1576bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1577{
1578 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1579
1580 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1581 if (mem->mem_type == TTM_PL_SYSTEM)
1582 return false;
1583
1584 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1585 return false;
1586
1587 if (mem->placement & TTM_PL_FLAG_CACHED)
1588 return false;
1589 }
1590 return true;
1591}
1592
Thomas Hellstromeba67092010-11-11 09:41:57 +01001593void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001594{
1595 struct ttm_bo_device *bdev = bo->bdev;
1596 loff_t offset = (loff_t) bo->addr_space_offset;
1597 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1598
1599 if (!bdev->dev_mapping)
1600 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001601 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001602 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001603}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001604
1605void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1606{
1607 struct ttm_bo_device *bdev = bo->bdev;
1608 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1609
1610 ttm_mem_io_lock(man, false);
1611 ttm_bo_unmap_virtual_locked(bo);
1612 ttm_mem_io_unlock(man);
1613}
1614
1615
Dave Airliee024e112009-06-24 09:48:08 +10001616EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001617
1618static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1619{
1620 struct ttm_bo_device *bdev = bo->bdev;
1621 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1622 struct rb_node *parent = NULL;
1623 struct ttm_buffer_object *cur_bo;
1624 unsigned long offset = bo->vm_node->start;
1625 unsigned long cur_offset;
1626
1627 while (*cur) {
1628 parent = *cur;
1629 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1630 cur_offset = cur_bo->vm_node->start;
1631 if (offset < cur_offset)
1632 cur = &parent->rb_left;
1633 else if (offset > cur_offset)
1634 cur = &parent->rb_right;
1635 else
1636 BUG();
1637 }
1638
1639 rb_link_node(&bo->vm_rb, parent, cur);
1640 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1641}
1642
1643/**
1644 * ttm_bo_setup_vm:
1645 *
1646 * @bo: the buffer to allocate address space for
1647 *
1648 * Allocate address space in the drm device so that applications
1649 * can mmap the buffer and access the contents. This only
1650 * applies to ttm_bo_type_device objects as others are not
1651 * placed in the drm device address space.
1652 */
1653
1654static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1655{
1656 struct ttm_bo_device *bdev = bo->bdev;
1657 int ret;
1658
1659retry_pre_get:
1660 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1661 if (unlikely(ret != 0))
1662 return ret;
1663
1664 write_lock(&bdev->vm_lock);
1665 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1666 bo->mem.num_pages, 0, 0);
1667
1668 if (unlikely(bo->vm_node == NULL)) {
1669 ret = -ENOMEM;
1670 goto out_unlock;
1671 }
1672
1673 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1674 bo->mem.num_pages, 0);
1675
1676 if (unlikely(bo->vm_node == NULL)) {
1677 write_unlock(&bdev->vm_lock);
1678 goto retry_pre_get;
1679 }
1680
1681 ttm_bo_vm_insert_rb(bo);
1682 write_unlock(&bdev->vm_lock);
1683 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1684
1685 return 0;
1686out_unlock:
1687 write_unlock(&bdev->vm_lock);
1688 return ret;
1689}
1690
1691int ttm_bo_wait(struct ttm_buffer_object *bo,
1692 bool lazy, bool interruptible, bool no_wait)
1693{
1694 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001695 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001696 void *sync_obj;
1697 void *sync_obj_arg;
1698 int ret = 0;
1699
1700 if (likely(bo->sync_obj == NULL))
1701 return 0;
1702
1703 while (bo->sync_obj) {
1704
1705 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1706 void *tmp_obj = bo->sync_obj;
1707 bo->sync_obj = NULL;
1708 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001709 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001710 driver->sync_obj_unref(&tmp_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001711 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001712 continue;
1713 }
1714
1715 if (no_wait)
1716 return -EBUSY;
1717
1718 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1719 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001720 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001721 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1722 lazy, interruptible);
1723 if (unlikely(ret != 0)) {
1724 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001725 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001726 return ret;
1727 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001728 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001729 if (likely(bo->sync_obj == sync_obj &&
1730 bo->sync_obj_arg == sync_obj_arg)) {
1731 void *tmp_obj = bo->sync_obj;
1732 bo->sync_obj = NULL;
1733 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1734 &bo->priv_flags);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001735 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001736 driver->sync_obj_unref(&sync_obj);
1737 driver->sync_obj_unref(&tmp_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001738 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001739 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001740 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001741 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001742 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001743 }
1744 }
1745 return 0;
1746}
1747EXPORT_SYMBOL(ttm_bo_wait);
1748
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001749int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1750{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001751 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001752 int ret = 0;
1753
1754 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001755 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001756 */
1757
1758 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1759 if (unlikely(ret != 0))
1760 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001761 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001762 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001763 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001764 if (likely(ret == 0))
1765 atomic_inc(&bo->cpu_writers);
1766 ttm_bo_unreserve(bo);
1767 return ret;
1768}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001769EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001770
1771void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1772{
1773 if (atomic_dec_and_test(&bo->cpu_writers))
1774 wake_up_all(&bo->event_queue);
1775}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001776EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001777
1778/**
1779 * A buffer object shrink method that tries to swap out the first
1780 * buffer object on the bo_global::swap_lru list.
1781 */
1782
1783static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1784{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001785 struct ttm_bo_global *glob =
1786 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001787 struct ttm_buffer_object *bo;
1788 int ret = -EBUSY;
1789 int put_count;
1790 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1791
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001792 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001793 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001794 if (unlikely(list_empty(&glob->swap_lru))) {
1795 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001796 return -EBUSY;
1797 }
1798
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001799 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800 struct ttm_buffer_object, swap);
1801 kref_get(&bo->list_kref);
1802
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001803 if (!list_empty(&bo->ddestroy)) {
1804 spin_unlock(&glob->lru_lock);
1805 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1806 kref_put(&bo->list_kref, ttm_bo_release_list);
1807 continue;
1808 }
1809
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001810 /**
1811 * Reserve buffer. Since we unlock while sleeping, we need
1812 * to re-check that nobody removed us from the swap-list while
1813 * we slept.
1814 */
1815
1816 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1817 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001818 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001819 ttm_bo_wait_unreserved(bo, false);
1820 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001821 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001822 }
1823 }
1824
1825 BUG_ON(ret != 0);
1826 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001827 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001828
Dave Airlied6ea8882010-11-22 13:24:40 +10001829 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001830
1831 /**
1832 * Wait for GPU, then move to system cached.
1833 */
1834
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001835 spin_lock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001836 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001837 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001838
1839 if (unlikely(ret != 0))
1840 goto out;
1841
1842 if ((bo->mem.placement & swap_placement) != swap_placement) {
1843 struct ttm_mem_reg evict_mem;
1844
1845 evict_mem = bo->mem;
1846 evict_mem.mm_node = NULL;
1847 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1848 evict_mem.mem_type = TTM_PL_SYSTEM;
1849
1850 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001851 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001852 if (unlikely(ret != 0))
1853 goto out;
1854 }
1855
1856 ttm_bo_unmap_virtual(bo);
1857
1858 /**
1859 * Swap out. Buffer will be swapped in again as soon as
1860 * anyone tries to access a ttm page.
1861 */
1862
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001863 if (bo->bdev->driver->swap_notify)
1864 bo->bdev->driver->swap_notify(bo);
1865
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001866 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1867out:
1868
1869 /**
1870 *
1871 * Unreserve without putting on LRU to avoid swapping out an
1872 * already swapped buffer.
1873 */
1874
1875 atomic_set(&bo->reserved, 0);
1876 wake_up_all(&bo->event_queue);
1877 kref_put(&bo->list_kref, ttm_bo_release_list);
1878 return ret;
1879}
1880
1881void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1882{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001883 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001884 ;
1885}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001886EXPORT_SYMBOL(ttm_bo_swapout_all);