blob: 384116afe5b7f4e0d47797253cc12fb0bd417770 [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>
Arun Sharma600634972011-07-26 16:09:06 -070040#include <linux/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);
Marcin Slusarz7c4c3962011-08-22 21:17:57 +0000356 if (unlikely(ret != 0)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200357 ttm_tt_destroy(bo->ttm);
Marcin Slusarz7c4c3962011-08-22 21:17:57 +0000358 bo->ttm = NULL;
359 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360 break;
361 default:
362 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
363 ret = -EINVAL;
364 break;
365 }
366
367 return ret;
368}
369
370static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
371 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000372 bool evict, bool interruptible,
373 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200374{
375 struct ttm_bo_device *bdev = bo->bdev;
376 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
377 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
378 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
379 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
380 int ret = 0;
381
382 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100383 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
384 ret = ttm_mem_io_lock(old_man, true);
385 if (unlikely(ret != 0))
386 goto out_err;
387 ttm_bo_unmap_virtual_locked(bo);
388 ttm_mem_io_unlock(old_man);
389 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390
391 /*
392 * Create and bind a ttm if required.
393 */
394
395 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
396 ret = ttm_bo_add_ttm(bo, false);
397 if (ret)
398 goto out_err;
399
400 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
401 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200402 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200403
404 if (mem->mem_type != TTM_PL_SYSTEM) {
405 ret = ttm_tt_bind(bo->ttm, mem);
406 if (ret)
407 goto out_err;
408 }
409
410 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000411 if (bdev->driver->move_notify)
412 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100413 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200414 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200415 goto moved;
416 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200417 }
418
Dave Airliee024e112009-06-24 09:48:08 +1000419 if (bdev->driver->move_notify)
420 bdev->driver->move_notify(bo, mem);
421
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200422 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
423 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000424 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200425 else if (bdev->driver->move)
426 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000427 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200428 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000429 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200430
431 if (ret)
432 goto out_err;
433
434moved:
435 if (bo->evicted) {
436 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
437 if (ret)
438 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
439 bo->evicted = false;
440 }
441
442 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000443 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200444 bdev->man[bo->mem.mem_type].gpu_offset;
445 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100446 } else
447 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200448
449 return 0;
450
451out_err:
452 new_man = &bdev->man[bo->mem.mem_type];
453 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
454 ttm_tt_unbind(bo->ttm);
455 ttm_tt_destroy(bo->ttm);
456 bo->ttm = NULL;
457 }
458
459 return ret;
460}
461
462/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200463 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200464 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200465 * This is the place to put in driver specific hooks to release
466 * driver private resources.
467 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200468 */
469
470static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
471{
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200472 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200473 ttm_tt_unbind(bo->ttm);
474 ttm_tt_destroy(bo->ttm);
475 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200476 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200477 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200478
479 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200480
481 /*
482 * Make processes trying to reserve really pick it up.
483 */
484 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200485 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200486}
487
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200488static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200489{
490 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200491 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200492 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000493 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200494 void *sync_obj_arg;
495 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200496 int ret;
497
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000498 spin_lock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200499 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200500 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200501
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200502 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100503
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200504 /**
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000505 * Lock inversion between bo:reserve and bdev::fence_lock here,
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200506 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200507 */
508
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200509 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200510
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200511 if (unlikely(ret == -EBUSY))
512 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200513
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000514 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200515 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200516
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200517 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200518 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200519
Dave Airlied6ea8882010-11-22 13:24:40 +1000520 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200521
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200522 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200523 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200524 spin_lock(&glob->lru_lock);
525 }
526queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200527 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000528 if (bo->sync_obj)
529 sync_obj = driver->sync_obj_ref(bo->sync_obj);
530 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200531
532 kref_get(&bo->list_kref);
533 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
534 spin_unlock(&glob->lru_lock);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000535 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200536
Thomas Hellstromaa123262010-11-02 13:21:47 +0000537 if (sync_obj) {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200538 driver->sync_obj_flush(sync_obj, sync_obj_arg);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000539 driver->sync_obj_unref(&sync_obj);
540 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200541 schedule_delayed_work(&bdev->wq,
542 ((HZ / 100) < 1) ? 1 : HZ / 100);
543}
544
545/**
546 * function ttm_bo_cleanup_refs
547 * If bo idle, remove from delayed- and lru lists, and unref.
548 * If not idle, do nothing.
549 *
550 * @interruptible Any sleeps should occur interruptibly.
551 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
552 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
553 */
554
555static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
556 bool interruptible,
557 bool no_wait_reserve,
558 bool no_wait_gpu)
559{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000560 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200561 struct ttm_bo_global *glob = bo->glob;
562 int put_count;
563 int ret = 0;
564
565retry:
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000566 spin_lock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200567 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000568 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200569
570 if (unlikely(ret != 0))
571 return ret;
572
573 spin_lock(&glob->lru_lock);
574 ret = ttm_bo_reserve_locked(bo, interruptible,
575 no_wait_reserve, false, 0);
576
577 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200578 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200579 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200580 }
581
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200582 /**
583 * We can re-check for sync object without taking
584 * the bo::lock since setting the sync object requires
585 * also bo::reserved. A busy object at this point may
586 * be caused by another thread recently starting an accelerated
587 * eviction.
588 */
589
590 if (unlikely(bo->sync_obj)) {
591 atomic_set(&bo->reserved, 0);
592 wake_up_all(&bo->event_queue);
593 spin_unlock(&glob->lru_lock);
594 goto retry;
595 }
596
597 put_count = ttm_bo_del_from_lru(bo);
598 list_del_init(&bo->ddestroy);
599 ++put_count;
600
601 spin_unlock(&glob->lru_lock);
602 ttm_bo_cleanup_memtype_use(bo);
603
Dave Airlied6ea8882010-11-22 13:24:40 +1000604 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200605
606 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200607}
608
609/**
610 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
611 * encountered buffers.
612 */
613
614static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
615{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200616 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100617 struct ttm_buffer_object *entry = NULL;
618 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200619
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200620 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100621 if (list_empty(&bdev->ddestroy))
622 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200623
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100624 entry = list_first_entry(&bdev->ddestroy,
625 struct ttm_buffer_object, ddestroy);
626 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200627
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100628 for (;;) {
629 struct ttm_buffer_object *nentry = NULL;
630
631 if (entry->ddestroy.next != &bdev->ddestroy) {
632 nentry = list_first_entry(&entry->ddestroy,
633 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200634 kref_get(&nentry->list_kref);
635 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200636
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200637 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200638 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
639 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200640 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100641 entry = nentry;
642
643 if (ret || !entry)
644 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200645
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200646 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100647 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200648 break;
649 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200650
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100651out_unlock:
652 spin_unlock(&glob->lru_lock);
653out:
654 if (entry)
655 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656 return ret;
657}
658
659static void ttm_bo_delayed_workqueue(struct work_struct *work)
660{
661 struct ttm_bo_device *bdev =
662 container_of(work, struct ttm_bo_device, wq.work);
663
664 if (ttm_bo_delayed_delete(bdev, false)) {
665 schedule_delayed_work(&bdev->wq,
666 ((HZ / 100) < 1) ? 1 : HZ / 100);
667 }
668}
669
670static void ttm_bo_release(struct kref *kref)
671{
672 struct ttm_buffer_object *bo =
673 container_of(kref, struct ttm_buffer_object, kref);
674 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100675 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200676
677 if (likely(bo->vm_node != NULL)) {
678 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
679 drm_mm_put_block(bo->vm_node);
680 bo->vm_node = NULL;
681 }
682 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100683 ttm_mem_io_lock(man, false);
684 ttm_mem_io_free_vm(bo);
685 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200686 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200687 kref_put(&bo->list_kref, ttm_bo_release_list);
688 write_lock(&bdev->vm_lock);
689}
690
691void ttm_bo_unref(struct ttm_buffer_object **p_bo)
692{
693 struct ttm_buffer_object *bo = *p_bo;
694 struct ttm_bo_device *bdev = bo->bdev;
695
696 *p_bo = NULL;
697 write_lock(&bdev->vm_lock);
698 kref_put(&bo->kref, ttm_bo_release);
699 write_unlock(&bdev->vm_lock);
700}
701EXPORT_SYMBOL(ttm_bo_unref);
702
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400703int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
704{
705 return cancel_delayed_work_sync(&bdev->wq);
706}
707EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
708
709void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
710{
711 if (resched)
712 schedule_delayed_work(&bdev->wq,
713 ((HZ / 100) < 1) ? 1 : HZ / 100);
714}
715EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
716
Jerome Glisseca262a9992009-12-08 15:33:32 +0100717static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000718 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200719{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200720 struct ttm_bo_device *bdev = bo->bdev;
721 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100722 struct ttm_placement placement;
723 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200724
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000725 spin_lock(&bdev->fence_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000726 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000727 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200728
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200729 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100730 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200731 printk(KERN_ERR TTM_PFX
732 "Failed to expire sync object before "
733 "buffer eviction.\n");
734 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200735 goto out;
736 }
737
738 BUG_ON(!atomic_read(&bo->reserved));
739
740 evict_mem = bo->mem;
741 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100742 evict_mem.bus.io_reserved_vm = false;
743 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200744
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100745 placement.fpfn = 0;
746 placement.lpfn = 0;
747 placement.num_placement = 0;
748 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100749 bdev->driver->evict_flags(bo, &placement);
750 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000751 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200752 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100753 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200754 printk(KERN_ERR TTM_PFX
755 "Failed to find memory space for "
756 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100757 ttm_bo_mem_space_debug(bo, &placement);
758 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200759 goto out;
760 }
761
762 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000763 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200764 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100765 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200766 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000767 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200768 goto out;
769 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200770 bo->evicted = true;
771out:
772 return ret;
773}
774
Jerome Glisseca262a9992009-12-08 15:33:32 +0100775static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
776 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000777 bool interruptible, bool no_wait_reserve,
778 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100779{
780 struct ttm_bo_global *glob = bdev->glob;
781 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
782 struct ttm_buffer_object *bo;
783 int ret, put_count = 0;
784
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100785retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100786 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100787 if (list_empty(&man->lru)) {
788 spin_unlock(&glob->lru_lock);
789 return -EBUSY;
790 }
791
Jerome Glisseca262a9992009-12-08 15:33:32 +0100792 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
793 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100794
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200795 if (!list_empty(&bo->ddestroy)) {
796 spin_unlock(&glob->lru_lock);
797 ret = ttm_bo_cleanup_refs(bo, interruptible,
798 no_wait_reserve, no_wait_gpu);
799 kref_put(&bo->list_kref, ttm_bo_release_list);
800
801 if (likely(ret == 0 || ret == -ERESTARTSYS))
802 return ret;
803
804 goto retry;
805 }
806
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000807 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100808
809 if (unlikely(ret == -EBUSY)) {
810 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000811 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100812 ret = ttm_bo_wait_unreserved(bo, interruptible);
813
814 kref_put(&bo->list_kref, ttm_bo_release_list);
815
816 /**
817 * We *need* to retry after releasing the lru lock.
818 */
819
820 if (unlikely(ret != 0))
821 return ret;
822 goto retry;
823 }
824
825 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100826 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100827
828 BUG_ON(ret != 0);
829
Dave Airlied6ea8882010-11-22 13:24:40 +1000830 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100831
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000832 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100833 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100834
Jerome Glisseca262a9992009-12-08 15:33:32 +0100835 kref_put(&bo->list_kref, ttm_bo_release_list);
836 return ret;
837}
838
Ben Skeggs42311ff2010-08-04 12:07:08 +1000839void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
840{
Ben Skeggsd961db72010-08-05 10:48:18 +1000841 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000842
Ben Skeggsd961db72010-08-05 10:48:18 +1000843 if (mem->mm_node)
844 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000845}
846EXPORT_SYMBOL(ttm_bo_mem_put);
847
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200848/**
849 * Repeatedly evict memory from the LRU for @mem_type until we create enough
850 * space, or we've evicted everything and there isn't enough space.
851 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100852static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
853 uint32_t mem_type,
854 struct ttm_placement *placement,
855 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000856 bool interruptible,
857 bool no_wait_reserve,
858 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200859{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100860 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200861 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200862 int ret;
863
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200864 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000865 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200866 if (unlikely(ret != 0))
867 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000868 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100869 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100870 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000871 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100872 if (unlikely(ret != 0))
873 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200874 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000875 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200876 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877 mem->mem_type = mem_type;
878 return 0;
879}
880
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200881static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
882 uint32_t cur_placement,
883 uint32_t proposed_placement)
884{
885 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
886 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
887
888 /**
889 * Keep current caching if possible.
890 */
891
892 if ((cur_placement & caching) != 0)
893 result |= (cur_placement & caching);
894 else if ((man->default_caching & caching) != 0)
895 result |= man->default_caching;
896 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
897 result |= TTM_PL_FLAG_CACHED;
898 else if ((TTM_PL_FLAG_WC & caching) != 0)
899 result |= TTM_PL_FLAG_WC;
900 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
901 result |= TTM_PL_FLAG_UNCACHED;
902
903 return result;
904}
905
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200906static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
907 bool disallow_fixed,
908 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200909 uint32_t proposed_placement,
910 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200911{
912 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
913
914 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
915 return false;
916
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200917 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200918 return false;
919
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200920 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200921 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200923 cur_flags |= (proposed_placement & man->available_caching);
924
925 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926 return true;
927}
928
929/**
930 * Creates space for memory region @mem according to its type.
931 *
932 * This function first searches for free space in compatible memory types in
933 * the priority order defined by the driver. If free space isn't found, then
934 * ttm_bo_mem_force_space is attempted in priority order to evict and find
935 * space.
936 */
937int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100938 struct ttm_placement *placement,
939 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000940 bool interruptible, bool no_wait_reserve,
941 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200942{
943 struct ttm_bo_device *bdev = bo->bdev;
944 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200945 uint32_t mem_type = TTM_PL_SYSTEM;
946 uint32_t cur_flags = 0;
947 bool type_found = false;
948 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100949 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100950 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951
952 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000953 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100954 ret = ttm_mem_type_from_flags(placement->placement[i],
955 &mem_type);
956 if (ret)
957 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200958 man = &bdev->man[mem_type];
959
960 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100961 bo->type == ttm_bo_type_user,
962 mem_type,
963 placement->placement[i],
964 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200965
966 if (!type_ok)
967 continue;
968
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200969 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
970 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100971 /*
972 * Use the access and other non-mapping-related flag bits from
973 * the memory placement flags to the current flags
974 */
975 ttm_flag_masked(&cur_flags, placement->placement[i],
976 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200977
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200978 if (mem_type == TTM_PL_SYSTEM)
979 break;
980
981 if (man->has_type && man->use_type) {
982 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000983 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100984 if (unlikely(ret))
985 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200986 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000987 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988 break;
989 }
990
Ben Skeggsd961db72010-08-05 10:48:18 +1000991 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200992 mem->mem_type = mem_type;
993 mem->placement = cur_flags;
994 return 0;
995 }
996
997 if (!type_found)
998 return -EINVAL;
999
Dave Airlieb6637522009-12-14 14:51:35 +10001000 for (i = 0; i < placement->num_busy_placement; ++i) {
1001 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001002 &mem_type);
1003 if (ret)
1004 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006 if (!man->has_type)
1007 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001008 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001009 bo->type == ttm_bo_type_user,
1010 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001011 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001012 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 continue;
1014
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001015 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1016 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001017 /*
1018 * Use the access and other non-mapping-related flag bits from
1019 * the memory placement flags to the current flags
1020 */
Dave Airlieb6637522009-12-14 14:51:35 +10001021 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001022 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001023
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001024
1025 if (mem_type == TTM_PL_SYSTEM) {
1026 mem->mem_type = mem_type;
1027 mem->placement = cur_flags;
1028 mem->mm_node = NULL;
1029 return 0;
1030 }
1031
Jerome Glisseca262a9992009-12-08 15:33:32 +01001032 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001033 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001034 if (ret == 0 && mem->mm_node) {
1035 mem->placement = cur_flags;
1036 return 0;
1037 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001038 if (ret == -ERESTARTSYS)
1039 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001040 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001041 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001042 return ret;
1043}
1044EXPORT_SYMBOL(ttm_bo_mem_space);
1045
1046int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1047{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001048 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1049 return -EBUSY;
1050
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001051 return wait_event_interruptible(bo->event_queue,
1052 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001053}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001054EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001055
1056int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001057 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001058 bool interruptible, bool no_wait_reserve,
1059 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001061 int ret = 0;
1062 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001063 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001064
1065 BUG_ON(!atomic_read(&bo->reserved));
1066
1067 /*
1068 * FIXME: It's possible to pipeline buffer moves.
1069 * Have the driver move function wait for idle when necessary,
1070 * instead of doing it here.
1071 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001072 spin_lock(&bdev->fence_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001073 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001074 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001075 if (ret)
1076 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077 mem.num_pages = bo->num_pages;
1078 mem.size = mem.num_pages << PAGE_SHIFT;
1079 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001080 mem.bus.io_reserved_vm = false;
1081 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082 /*
1083 * Determine where to move the buffer.
1084 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001085 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086 if (ret)
1087 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001088 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001090 if (ret && mem.mm_node)
1091 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092 return ret;
1093}
1094
Jerome Glisseca262a9992009-12-08 15:33:32 +01001095static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001096 struct ttm_mem_reg *mem)
1097{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001098 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001099
Ben Skeggsd961db72010-08-05 10:48:18 +10001100 if (mem->mm_node && placement->lpfn != 0 &&
1101 (mem->start < placement->fpfn ||
1102 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001103 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001104
Jerome Glisseca262a9992009-12-08 15:33:32 +01001105 for (i = 0; i < placement->num_placement; i++) {
1106 if ((placement->placement[i] & mem->placement &
1107 TTM_PL_MASK_CACHING) &&
1108 (placement->placement[i] & mem->placement &
1109 TTM_PL_MASK_MEM))
1110 return i;
1111 }
1112 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113}
1114
Jerome Glisse09855ac2009-12-10 17:16:27 +01001115int ttm_bo_validate(struct ttm_buffer_object *bo,
1116 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001117 bool interruptible, bool no_wait_reserve,
1118 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001119{
1120 int ret;
1121
1122 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001123 /* Check that range is valid */
1124 if (placement->lpfn || placement->fpfn)
1125 if (placement->fpfn > placement->lpfn ||
1126 (placement->lpfn - placement->fpfn) < bo->num_pages)
1127 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001128 /*
1129 * Check whether we need to move buffer.
1130 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001131 ret = ttm_bo_mem_compat(placement, &bo->mem);
1132 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001133 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001134 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001136 } else {
1137 /*
1138 * Use the access and other non-mapping-related flag bits from
1139 * the compatible memory placement flags to the active flags
1140 */
1141 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1142 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 /*
1145 * We might need to add a TTM.
1146 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001147 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1148 ret = ttm_bo_add_ttm(bo, true);
1149 if (ret)
1150 return ret;
1151 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152 return 0;
1153}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001154EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001155
Jerome Glisse09855ac2009-12-10 17:16:27 +01001156int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1157 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001159 BUG_ON((placement->fpfn || placement->lpfn) &&
1160 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001162 return 0;
1163}
1164
Jerome Glisse09855ac2009-12-10 17:16:27 +01001165int ttm_bo_init(struct ttm_bo_device *bdev,
1166 struct ttm_buffer_object *bo,
1167 unsigned long size,
1168 enum ttm_bo_type type,
1169 struct ttm_placement *placement,
1170 uint32_t page_alignment,
1171 unsigned long buffer_start,
1172 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001173 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001174 size_t acc_size,
1175 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001176{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001177 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178 unsigned long num_pages;
1179
1180 size += buffer_start & ~PAGE_MASK;
1181 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1182 if (num_pages == 0) {
1183 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001184 if (destroy)
1185 (*destroy)(bo);
1186 else
1187 kfree(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001188 return -EINVAL;
1189 }
1190 bo->destroy = destroy;
1191
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001192 kref_init(&bo->kref);
1193 kref_init(&bo->list_kref);
1194 atomic_set(&bo->cpu_writers, 0);
1195 atomic_set(&bo->reserved, 1);
1196 init_waitqueue_head(&bo->event_queue);
1197 INIT_LIST_HEAD(&bo->lru);
1198 INIT_LIST_HEAD(&bo->ddestroy);
1199 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001200 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001201 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001202 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203 bo->type = type;
1204 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001205 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001206 bo->mem.mem_type = TTM_PL_SYSTEM;
1207 bo->mem.num_pages = bo->num_pages;
1208 bo->mem.mm_node = NULL;
1209 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001210 bo->mem.bus.io_reserved_vm = false;
1211 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212 bo->buffer_start = buffer_start & PAGE_MASK;
1213 bo->priv_flags = 0;
1214 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1215 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001216 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001217 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001218 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219
Jerome Glisse09855ac2009-12-10 17:16:27 +01001220 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221 if (unlikely(ret != 0))
1222 goto out_err;
1223
1224 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001225 * For ttm_bo_type_device buffers, allocate
1226 * address space from the device.
1227 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228 if (bo->type == ttm_bo_type_device) {
1229 ret = ttm_bo_setup_vm(bo);
1230 if (ret)
1231 goto out_err;
1232 }
1233
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001234 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001235 if (ret)
1236 goto out_err;
1237
1238 ttm_bo_unreserve(bo);
1239 return 0;
1240
1241out_err:
1242 ttm_bo_unreserve(bo);
1243 ttm_bo_unref(&bo);
1244
1245 return ret;
1246}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001247EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001249static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001250 unsigned long num_pages)
1251{
1252 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1253 PAGE_MASK;
1254
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001255 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256}
1257
Jerome Glisse09855ac2009-12-10 17:16:27 +01001258int ttm_bo_create(struct ttm_bo_device *bdev,
1259 unsigned long size,
1260 enum ttm_bo_type type,
1261 struct ttm_placement *placement,
1262 uint32_t page_alignment,
1263 unsigned long buffer_start,
1264 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001265 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001266 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001267{
1268 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001269 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001270 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271
1272 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001273 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001274 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001275 if (unlikely(ret != 0))
1276 return ret;
1277
1278 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1279
1280 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001281 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001282 return -ENOMEM;
1283 }
1284
Jerome Glisse09855ac2009-12-10 17:16:27 +01001285 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1286 buffer_start, interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001287 persistent_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001288 if (likely(ret == 0))
1289 *p_bo = bo;
1290
1291 return ret;
1292}
1293
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001294static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001295 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001296{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001297 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001298 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001300
1301 /*
1302 * Can't use standard list traversal since we're unlocking.
1303 */
1304
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001305 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001306 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001307 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001308 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001309 if (ret) {
1310 if (allow_errors) {
1311 return ret;
1312 } else {
1313 printk(KERN_ERR TTM_PFX
1314 "Cleanup eviction failed\n");
1315 }
1316 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001317 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001318 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001319 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001320 return 0;
1321}
1322
1323int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1324{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001325 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001326 int ret = -EINVAL;
1327
1328 if (mem_type >= TTM_NUM_MEM_TYPES) {
1329 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1330 return ret;
1331 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001332 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001333
1334 if (!man->has_type) {
1335 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1336 "memory manager type %u\n", mem_type);
1337 return ret;
1338 }
1339
1340 man->use_type = false;
1341 man->has_type = false;
1342
1343 ret = 0;
1344 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001345 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001346
Ben Skeggsd961db72010-08-05 10:48:18 +10001347 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001348 }
1349
1350 return ret;
1351}
1352EXPORT_SYMBOL(ttm_bo_clean_mm);
1353
1354int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1355{
1356 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1357
1358 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1359 printk(KERN_ERR TTM_PFX
1360 "Illegal memory manager memory type %u.\n",
1361 mem_type);
1362 return -EINVAL;
1363 }
1364
1365 if (!man->has_type) {
1366 printk(KERN_ERR TTM_PFX
1367 "Memory type %u has not been initialized.\n",
1368 mem_type);
1369 return 0;
1370 }
1371
Jerome Glisseca262a9992009-12-08 15:33:32 +01001372 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001373}
1374EXPORT_SYMBOL(ttm_bo_evict_mm);
1375
1376int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001377 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001378{
1379 int ret = -EINVAL;
1380 struct ttm_mem_type_manager *man;
1381
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001382 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001383 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001384 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001385 man->io_reserve_fastpath = true;
1386 man->use_io_reserve_lru = false;
1387 mutex_init(&man->io_reserve_mutex);
1388 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001389
1390 ret = bdev->driver->init_mem_type(bdev, type, man);
1391 if (ret)
1392 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001393 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001394
1395 ret = 0;
1396 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001397 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001398 if (ret)
1399 return ret;
1400 }
1401 man->has_type = true;
1402 man->use_type = true;
1403 man->size = p_size;
1404
1405 INIT_LIST_HEAD(&man->lru);
1406
1407 return 0;
1408}
1409EXPORT_SYMBOL(ttm_bo_init_mm);
1410
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001411static void ttm_bo_global_kobj_release(struct kobject *kobj)
1412{
1413 struct ttm_bo_global *glob =
1414 container_of(kobj, struct ttm_bo_global, kobj);
1415
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001416 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1417 __free_page(glob->dummy_read_page);
1418 kfree(glob);
1419}
1420
Dave Airlieba4420c2010-03-09 10:56:52 +10001421void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001422{
1423 struct ttm_bo_global *glob = ref->object;
1424
1425 kobject_del(&glob->kobj);
1426 kobject_put(&glob->kobj);
1427}
1428EXPORT_SYMBOL(ttm_bo_global_release);
1429
Dave Airlieba4420c2010-03-09 10:56:52 +10001430int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001431{
1432 struct ttm_bo_global_ref *bo_ref =
1433 container_of(ref, struct ttm_bo_global_ref, ref);
1434 struct ttm_bo_global *glob = ref->object;
1435 int ret;
1436
1437 mutex_init(&glob->device_list_mutex);
1438 spin_lock_init(&glob->lru_lock);
1439 glob->mem_glob = bo_ref->mem_glob;
1440 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1441
1442 if (unlikely(glob->dummy_read_page == NULL)) {
1443 ret = -ENOMEM;
1444 goto out_no_drp;
1445 }
1446
1447 INIT_LIST_HEAD(&glob->swap_lru);
1448 INIT_LIST_HEAD(&glob->device_list);
1449
1450 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1451 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1452 if (unlikely(ret != 0)) {
1453 printk(KERN_ERR TTM_PFX
1454 "Could not register buffer object swapout.\n");
1455 goto out_no_shrink;
1456 }
1457
1458 glob->ttm_bo_extra_size =
1459 ttm_round_pot(sizeof(struct ttm_tt)) +
1460 ttm_round_pot(sizeof(struct ttm_backend));
1461
1462 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1463 ttm_round_pot(sizeof(struct ttm_buffer_object));
1464
1465 atomic_set(&glob->bo_count, 0);
1466
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001467 ret = kobject_init_and_add(
1468 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001469 if (unlikely(ret != 0))
1470 kobject_put(&glob->kobj);
1471 return ret;
1472out_no_shrink:
1473 __free_page(glob->dummy_read_page);
1474out_no_drp:
1475 kfree(glob);
1476 return ret;
1477}
1478EXPORT_SYMBOL(ttm_bo_global_init);
1479
1480
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001481int ttm_bo_device_release(struct ttm_bo_device *bdev)
1482{
1483 int ret = 0;
1484 unsigned i = TTM_NUM_MEM_TYPES;
1485 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001486 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001487
1488 while (i--) {
1489 man = &bdev->man[i];
1490 if (man->has_type) {
1491 man->use_type = false;
1492 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1493 ret = -EBUSY;
1494 printk(KERN_ERR TTM_PFX
1495 "DRM memory manager type %d "
1496 "is not clean.\n", i);
1497 }
1498 man->has_type = false;
1499 }
1500 }
1501
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001502 mutex_lock(&glob->device_list_mutex);
1503 list_del(&bdev->device_list);
1504 mutex_unlock(&glob->device_list_mutex);
1505
Tejun Heof094cfc2010-12-24 15:59:06 +01001506 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001507
1508 while (ttm_bo_delayed_delete(bdev, true))
1509 ;
1510
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001511 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001512 if (list_empty(&bdev->ddestroy))
1513 TTM_DEBUG("Delayed destroy list was clean\n");
1514
1515 if (list_empty(&bdev->man[0].lru))
1516 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001517 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001518
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001519 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1520 write_lock(&bdev->vm_lock);
1521 drm_mm_takedown(&bdev->addr_space_mm);
1522 write_unlock(&bdev->vm_lock);
1523
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001524 return ret;
1525}
1526EXPORT_SYMBOL(ttm_bo_device_release);
1527
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001528int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001529 struct ttm_bo_global *glob,
1530 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001531 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001532 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001533{
1534 int ret = -EINVAL;
1535
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001537 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538
1539 memset(bdev->man, 0, sizeof(bdev->man));
1540
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001541 /*
1542 * Initialize the system memory buffer type.
1543 * Other types need to be driver / IOCTL initialized.
1544 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001545 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001546 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001547 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001548
1549 bdev->addr_space_rb = RB_ROOT;
1550 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1551 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001552 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553
1554 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1555 bdev->nice_mode = true;
1556 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001557 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001558 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001559 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001560 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001561 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001562 mutex_lock(&glob->device_list_mutex);
1563 list_add_tail(&bdev->device_list, &glob->device_list);
1564 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001565
1566 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001567out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001568 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001569out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001570 return ret;
1571}
1572EXPORT_SYMBOL(ttm_bo_device_init);
1573
1574/*
1575 * buffer object vm functions.
1576 */
1577
1578bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1579{
1580 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1581
1582 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1583 if (mem->mem_type == TTM_PL_SYSTEM)
1584 return false;
1585
1586 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1587 return false;
1588
1589 if (mem->placement & TTM_PL_FLAG_CACHED)
1590 return false;
1591 }
1592 return true;
1593}
1594
Thomas Hellstromeba67092010-11-11 09:41:57 +01001595void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001596{
1597 struct ttm_bo_device *bdev = bo->bdev;
1598 loff_t offset = (loff_t) bo->addr_space_offset;
1599 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1600
1601 if (!bdev->dev_mapping)
1602 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001603 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001604 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001605}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001606
1607void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1608{
1609 struct ttm_bo_device *bdev = bo->bdev;
1610 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1611
1612 ttm_mem_io_lock(man, false);
1613 ttm_bo_unmap_virtual_locked(bo);
1614 ttm_mem_io_unlock(man);
1615}
1616
1617
Dave Airliee024e112009-06-24 09:48:08 +10001618EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001619
1620static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1621{
1622 struct ttm_bo_device *bdev = bo->bdev;
1623 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1624 struct rb_node *parent = NULL;
1625 struct ttm_buffer_object *cur_bo;
1626 unsigned long offset = bo->vm_node->start;
1627 unsigned long cur_offset;
1628
1629 while (*cur) {
1630 parent = *cur;
1631 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1632 cur_offset = cur_bo->vm_node->start;
1633 if (offset < cur_offset)
1634 cur = &parent->rb_left;
1635 else if (offset > cur_offset)
1636 cur = &parent->rb_right;
1637 else
1638 BUG();
1639 }
1640
1641 rb_link_node(&bo->vm_rb, parent, cur);
1642 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1643}
1644
1645/**
1646 * ttm_bo_setup_vm:
1647 *
1648 * @bo: the buffer to allocate address space for
1649 *
1650 * Allocate address space in the drm device so that applications
1651 * can mmap the buffer and access the contents. This only
1652 * applies to ttm_bo_type_device objects as others are not
1653 * placed in the drm device address space.
1654 */
1655
1656static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1657{
1658 struct ttm_bo_device *bdev = bo->bdev;
1659 int ret;
1660
1661retry_pre_get:
1662 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1663 if (unlikely(ret != 0))
1664 return ret;
1665
1666 write_lock(&bdev->vm_lock);
1667 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1668 bo->mem.num_pages, 0, 0);
1669
1670 if (unlikely(bo->vm_node == NULL)) {
1671 ret = -ENOMEM;
1672 goto out_unlock;
1673 }
1674
1675 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1676 bo->mem.num_pages, 0);
1677
1678 if (unlikely(bo->vm_node == NULL)) {
1679 write_unlock(&bdev->vm_lock);
1680 goto retry_pre_get;
1681 }
1682
1683 ttm_bo_vm_insert_rb(bo);
1684 write_unlock(&bdev->vm_lock);
1685 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1686
1687 return 0;
1688out_unlock:
1689 write_unlock(&bdev->vm_lock);
1690 return ret;
1691}
1692
1693int ttm_bo_wait(struct ttm_buffer_object *bo,
1694 bool lazy, bool interruptible, bool no_wait)
1695{
1696 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001697 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001698 void *sync_obj;
1699 void *sync_obj_arg;
1700 int ret = 0;
1701
1702 if (likely(bo->sync_obj == NULL))
1703 return 0;
1704
1705 while (bo->sync_obj) {
1706
1707 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1708 void *tmp_obj = bo->sync_obj;
1709 bo->sync_obj = NULL;
1710 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001711 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001712 driver->sync_obj_unref(&tmp_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001713 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001714 continue;
1715 }
1716
1717 if (no_wait)
1718 return -EBUSY;
1719
1720 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1721 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001722 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001723 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1724 lazy, interruptible);
1725 if (unlikely(ret != 0)) {
1726 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001727 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001728 return ret;
1729 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001730 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001731 if (likely(bo->sync_obj == sync_obj &&
1732 bo->sync_obj_arg == sync_obj_arg)) {
1733 void *tmp_obj = bo->sync_obj;
1734 bo->sync_obj = NULL;
1735 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1736 &bo->priv_flags);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001737 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001738 driver->sync_obj_unref(&sync_obj);
1739 driver->sync_obj_unref(&tmp_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001740 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001741 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001742 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001743 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001744 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001745 }
1746 }
1747 return 0;
1748}
1749EXPORT_SYMBOL(ttm_bo_wait);
1750
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001751int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1752{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001753 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001754 int ret = 0;
1755
1756 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001757 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001758 */
1759
1760 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1761 if (unlikely(ret != 0))
1762 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001763 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001764 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001765 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001766 if (likely(ret == 0))
1767 atomic_inc(&bo->cpu_writers);
1768 ttm_bo_unreserve(bo);
1769 return ret;
1770}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001771EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001772
1773void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1774{
1775 if (atomic_dec_and_test(&bo->cpu_writers))
1776 wake_up_all(&bo->event_queue);
1777}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001778EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001779
1780/**
1781 * A buffer object shrink method that tries to swap out the first
1782 * buffer object on the bo_global::swap_lru list.
1783 */
1784
1785static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1786{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001787 struct ttm_bo_global *glob =
1788 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001789 struct ttm_buffer_object *bo;
1790 int ret = -EBUSY;
1791 int put_count;
1792 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1793
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001794 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001795 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001796 if (unlikely(list_empty(&glob->swap_lru))) {
1797 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001798 return -EBUSY;
1799 }
1800
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001801 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001802 struct ttm_buffer_object, swap);
1803 kref_get(&bo->list_kref);
1804
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001805 if (!list_empty(&bo->ddestroy)) {
1806 spin_unlock(&glob->lru_lock);
1807 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1808 kref_put(&bo->list_kref, ttm_bo_release_list);
1809 continue;
1810 }
1811
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001812 /**
1813 * Reserve buffer. Since we unlock while sleeping, we need
1814 * to re-check that nobody removed us from the swap-list while
1815 * we slept.
1816 */
1817
1818 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1819 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001820 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001821 ttm_bo_wait_unreserved(bo, false);
1822 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001823 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001824 }
1825 }
1826
1827 BUG_ON(ret != 0);
1828 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001829 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001830
Dave Airlied6ea8882010-11-22 13:24:40 +10001831 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001832
1833 /**
1834 * Wait for GPU, then move to system cached.
1835 */
1836
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001837 spin_lock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001838 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001839 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001840
1841 if (unlikely(ret != 0))
1842 goto out;
1843
1844 if ((bo->mem.placement & swap_placement) != swap_placement) {
1845 struct ttm_mem_reg evict_mem;
1846
1847 evict_mem = bo->mem;
1848 evict_mem.mm_node = NULL;
1849 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1850 evict_mem.mem_type = TTM_PL_SYSTEM;
1851
1852 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001853 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001854 if (unlikely(ret != 0))
1855 goto out;
1856 }
1857
1858 ttm_bo_unmap_virtual(bo);
1859
1860 /**
1861 * Swap out. Buffer will be swapped in again as soon as
1862 * anyone tries to access a ttm page.
1863 */
1864
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001865 if (bo->bdev->driver->swap_notify)
1866 bo->bdev->driver->swap_notify(bo);
1867
Jan Engelhardt5df23972011-04-04 01:25:18 +02001868 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001869out:
1870
1871 /**
1872 *
1873 * Unreserve without putting on LRU to avoid swapping out an
1874 * already swapped buffer.
1875 */
1876
1877 atomic_set(&bo->reserved, 0);
1878 wake_up_all(&bo->event_queue);
1879 kref_put(&bo->list_kref, ttm_bo_release_list);
1880 return ret;
1881}
1882
1883void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1884{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001885 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001886 ;
1887}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001888EXPORT_SYMBOL(ttm_bo_swapout_all);