blob: 9ef893d5da88aae53be7a050f4b164d1ee0c1721 [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 Hellstromba4e7d92009-06-10 15:20:19 +0200226 if (use_sequence && bo->seq_valid &&
227 (sequence - bo->val_seq < (1 << 31))) {
228 return -EAGAIN;
229 }
230
231 if (no_wait)
232 return -EBUSY;
233
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200234 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200235 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200236 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200237
238 if (unlikely(ret))
239 return ret;
240 }
241
242 if (use_sequence) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100243 /**
244 * Wake up waiters that may need to recheck for deadlock,
245 * if we decreased the sequence number.
246 */
247 if (unlikely((bo->val_seq - sequence < (1 << 31))
248 || !bo->seq_valid))
249 wake_up_all(&bo->event_queue);
250
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200251 bo->val_seq = sequence;
252 bo->seq_valid = true;
253 } else {
254 bo->seq_valid = false;
255 }
256
257 return 0;
258}
259EXPORT_SYMBOL(ttm_bo_reserve);
260
261static void ttm_bo_ref_bug(struct kref *list_kref)
262{
263 BUG();
264}
265
Dave Airlied6ea8882010-11-22 13:24:40 +1000266void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
267 bool never_free)
268{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100269 kref_sub(&bo->list_kref, count,
270 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000271}
272
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200273int ttm_bo_reserve(struct ttm_buffer_object *bo,
274 bool interruptible,
275 bool no_wait, bool use_sequence, uint32_t sequence)
276{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200277 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200278 int put_count = 0;
279 int ret;
280
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200281 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200282 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
283 sequence);
284 if (likely(ret == 0))
285 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200286 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200287
Dave Airlied6ea8882010-11-22 13:24:40 +1000288 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200289
290 return ret;
291}
292
293void ttm_bo_unreserve(struct ttm_buffer_object *bo)
294{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200295 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200296
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200297 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200298 ttm_bo_add_to_lru(bo);
299 atomic_set(&bo->reserved, 0);
300 wake_up_all(&bo->event_queue);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200301 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200302}
303EXPORT_SYMBOL(ttm_bo_unreserve);
304
305/*
306 * Call bo->mutex locked.
307 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200308static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
309{
310 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200311 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200312 int ret = 0;
313 uint32_t page_flags = 0;
314
315 TTM_ASSERT_LOCKED(&bo->mutex);
316 bo->ttm = NULL;
317
Dave Airliead49f502009-07-10 22:36:26 +1000318 if (bdev->need_dma32)
319 page_flags |= TTM_PAGE_FLAG_DMA32;
320
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200321 switch (bo->type) {
322 case ttm_bo_type_device:
323 if (zero_alloc)
324 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
325 case ttm_bo_type_kernel:
326 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200327 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200328 if (unlikely(bo->ttm == NULL))
329 ret = -ENOMEM;
330 break;
331 case ttm_bo_type_user:
332 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
333 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200334 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000335 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200336 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000337 break;
338 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200339
340 ret = ttm_tt_set_user(bo->ttm, current,
341 bo->buffer_start, bo->num_pages);
342 if (unlikely(ret != 0))
343 ttm_tt_destroy(bo->ttm);
344 break;
345 default:
346 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
347 ret = -EINVAL;
348 break;
349 }
350
351 return ret;
352}
353
354static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
355 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000356 bool evict, bool interruptible,
357 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200358{
359 struct ttm_bo_device *bdev = bo->bdev;
360 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
361 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
362 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
363 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
364 int ret = 0;
365
366 if (old_is_pci || new_is_pci ||
367 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0))
368 ttm_bo_unmap_virtual(bo);
369
370 /*
371 * Create and bind a ttm if required.
372 */
373
374 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) {
375 ret = ttm_bo_add_ttm(bo, false);
376 if (ret)
377 goto out_err;
378
379 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
380 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200381 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200382
383 if (mem->mem_type != TTM_PL_SYSTEM) {
384 ret = ttm_tt_bind(bo->ttm, mem);
385 if (ret)
386 goto out_err;
387 }
388
389 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100390 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200391 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200392 goto moved;
393 }
394
395 }
396
Dave Airliee024e112009-06-24 09:48:08 +1000397 if (bdev->driver->move_notify)
398 bdev->driver->move_notify(bo, mem);
399
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200400 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
401 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000402 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200403 else if (bdev->driver->move)
404 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000405 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200406 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000407 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200408
409 if (ret)
410 goto out_err;
411
412moved:
413 if (bo->evicted) {
414 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
415 if (ret)
416 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
417 bo->evicted = false;
418 }
419
420 if (bo->mem.mm_node) {
421 spin_lock(&bo->lock);
Ben Skeggsd961db72010-08-05 10:48:18 +1000422 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200423 bdev->man[bo->mem.mem_type].gpu_offset;
424 bo->cur_placement = bo->mem.placement;
425 spin_unlock(&bo->lock);
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100426 } else
427 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200428
429 return 0;
430
431out_err:
432 new_man = &bdev->man[bo->mem.mem_type];
433 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
434 ttm_tt_unbind(bo->ttm);
435 ttm_tt_destroy(bo->ttm);
436 bo->ttm = NULL;
437 }
438
439 return ret;
440}
441
442/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200443 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200444 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200445 * This is the place to put in driver specific hooks to release
446 * driver private resources.
447 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200448 */
449
450static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
451{
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200452 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200453 ttm_tt_unbind(bo->ttm);
454 ttm_tt_destroy(bo->ttm);
455 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200456 }
457
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200458 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200459
460 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200461
462 /*
463 * Make processes trying to reserve really pick it up.
464 */
465 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200466 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200467}
468
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200469static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200470{
471 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200472 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200473 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000474 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200475 void *sync_obj_arg;
476 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200477 int ret;
478
479 spin_lock(&bo->lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200480 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200481 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200482
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200483 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100484
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200485 /**
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200486 * Lock inversion between bo::reserve and bo::lock here,
487 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200488 */
489
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200490 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200491
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200492 if (unlikely(ret == -EBUSY))
493 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200494
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200495 spin_unlock(&bo->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200496 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200497
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200498 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200499 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200500
Dave Airlied6ea8882010-11-22 13:24:40 +1000501 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200502
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200503 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200504 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200505 spin_lock(&glob->lru_lock);
506 }
507queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200508 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000509 if (bo->sync_obj)
510 sync_obj = driver->sync_obj_ref(bo->sync_obj);
511 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200512
513 kref_get(&bo->list_kref);
514 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
515 spin_unlock(&glob->lru_lock);
516 spin_unlock(&bo->lock);
517
Thomas Hellstromaa123262010-11-02 13:21:47 +0000518 if (sync_obj) {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200519 driver->sync_obj_flush(sync_obj, sync_obj_arg);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000520 driver->sync_obj_unref(&sync_obj);
521 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200522 schedule_delayed_work(&bdev->wq,
523 ((HZ / 100) < 1) ? 1 : HZ / 100);
524}
525
526/**
527 * function ttm_bo_cleanup_refs
528 * If bo idle, remove from delayed- and lru lists, and unref.
529 * If not idle, do nothing.
530 *
531 * @interruptible Any sleeps should occur interruptibly.
532 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
533 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
534 */
535
536static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
537 bool interruptible,
538 bool no_wait_reserve,
539 bool no_wait_gpu)
540{
541 struct ttm_bo_global *glob = bo->glob;
542 int put_count;
543 int ret = 0;
544
545retry:
546 spin_lock(&bo->lock);
547 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
548 spin_unlock(&bo->lock);
549
550 if (unlikely(ret != 0))
551 return ret;
552
553 spin_lock(&glob->lru_lock);
554 ret = ttm_bo_reserve_locked(bo, interruptible,
555 no_wait_reserve, false, 0);
556
557 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200558 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200559 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200560 }
561
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200562 /**
563 * We can re-check for sync object without taking
564 * the bo::lock since setting the sync object requires
565 * also bo::reserved. A busy object at this point may
566 * be caused by another thread recently starting an accelerated
567 * eviction.
568 */
569
570 if (unlikely(bo->sync_obj)) {
571 atomic_set(&bo->reserved, 0);
572 wake_up_all(&bo->event_queue);
573 spin_unlock(&glob->lru_lock);
574 goto retry;
575 }
576
577 put_count = ttm_bo_del_from_lru(bo);
578 list_del_init(&bo->ddestroy);
579 ++put_count;
580
581 spin_unlock(&glob->lru_lock);
582 ttm_bo_cleanup_memtype_use(bo);
583
Dave Airlied6ea8882010-11-22 13:24:40 +1000584 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200585
586 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200587}
588
589/**
590 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
591 * encountered buffers.
592 */
593
594static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
595{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200596 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100597 struct ttm_buffer_object *entry = NULL;
598 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200599
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200600 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100601 if (list_empty(&bdev->ddestroy))
602 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200603
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100604 entry = list_first_entry(&bdev->ddestroy,
605 struct ttm_buffer_object, ddestroy);
606 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200607
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100608 for (;;) {
609 struct ttm_buffer_object *nentry = NULL;
610
611 if (entry->ddestroy.next != &bdev->ddestroy) {
612 nentry = list_first_entry(&entry->ddestroy,
613 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200614 kref_get(&nentry->list_kref);
615 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200617 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200618 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
619 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200620 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100621 entry = nentry;
622
623 if (ret || !entry)
624 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200625
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200626 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100627 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200628 break;
629 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200630
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100631out_unlock:
632 spin_unlock(&glob->lru_lock);
633out:
634 if (entry)
635 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200636 return ret;
637}
638
639static void ttm_bo_delayed_workqueue(struct work_struct *work)
640{
641 struct ttm_bo_device *bdev =
642 container_of(work, struct ttm_bo_device, wq.work);
643
644 if (ttm_bo_delayed_delete(bdev, false)) {
645 schedule_delayed_work(&bdev->wq,
646 ((HZ / 100) < 1) ? 1 : HZ / 100);
647 }
648}
649
650static void ttm_bo_release(struct kref *kref)
651{
652 struct ttm_buffer_object *bo =
653 container_of(kref, struct ttm_buffer_object, kref);
654 struct ttm_bo_device *bdev = bo->bdev;
655
656 if (likely(bo->vm_node != NULL)) {
657 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
658 drm_mm_put_block(bo->vm_node);
659 bo->vm_node = NULL;
660 }
661 write_unlock(&bdev->vm_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200662 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200663 kref_put(&bo->list_kref, ttm_bo_release_list);
664 write_lock(&bdev->vm_lock);
665}
666
667void ttm_bo_unref(struct ttm_buffer_object **p_bo)
668{
669 struct ttm_buffer_object *bo = *p_bo;
670 struct ttm_bo_device *bdev = bo->bdev;
671
672 *p_bo = NULL;
673 write_lock(&bdev->vm_lock);
674 kref_put(&bo->kref, ttm_bo_release);
675 write_unlock(&bdev->vm_lock);
676}
677EXPORT_SYMBOL(ttm_bo_unref);
678
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400679int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
680{
681 return cancel_delayed_work_sync(&bdev->wq);
682}
683EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
684
685void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
686{
687 if (resched)
688 schedule_delayed_work(&bdev->wq,
689 ((HZ / 100) < 1) ? 1 : HZ / 100);
690}
691EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
692
Jerome Glisseca262a9992009-12-08 15:33:32 +0100693static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000694 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200695{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200696 struct ttm_bo_device *bdev = bo->bdev;
697 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100698 struct ttm_placement placement;
699 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200700
701 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000702 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200703 spin_unlock(&bo->lock);
704
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200705 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100706 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200707 printk(KERN_ERR TTM_PFX
708 "Failed to expire sync object before "
709 "buffer eviction.\n");
710 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200711 goto out;
712 }
713
714 BUG_ON(!atomic_read(&bo->reserved));
715
716 evict_mem = bo->mem;
717 evict_mem.mm_node = NULL;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200718 evict_mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200719
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100720 placement.fpfn = 0;
721 placement.lpfn = 0;
722 placement.num_placement = 0;
723 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100724 bdev->driver->evict_flags(bo, &placement);
725 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000726 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200727 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100728 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200729 printk(KERN_ERR TTM_PFX
730 "Failed to find memory space for "
731 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100732 ttm_bo_mem_space_debug(bo, &placement);
733 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200734 goto out;
735 }
736
737 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000738 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200739 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100740 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200741 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000742 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200743 goto out;
744 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200745 bo->evicted = true;
746out:
747 return ret;
748}
749
Jerome Glisseca262a9992009-12-08 15:33:32 +0100750static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
751 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000752 bool interruptible, bool no_wait_reserve,
753 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100754{
755 struct ttm_bo_global *glob = bdev->glob;
756 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
757 struct ttm_buffer_object *bo;
758 int ret, put_count = 0;
759
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100760retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100761 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100762 if (list_empty(&man->lru)) {
763 spin_unlock(&glob->lru_lock);
764 return -EBUSY;
765 }
766
Jerome Glisseca262a9992009-12-08 15:33:32 +0100767 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
768 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100769
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200770 if (!list_empty(&bo->ddestroy)) {
771 spin_unlock(&glob->lru_lock);
772 ret = ttm_bo_cleanup_refs(bo, interruptible,
773 no_wait_reserve, no_wait_gpu);
774 kref_put(&bo->list_kref, ttm_bo_release_list);
775
776 if (likely(ret == 0 || ret == -ERESTARTSYS))
777 return ret;
778
779 goto retry;
780 }
781
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000782 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100783
784 if (unlikely(ret == -EBUSY)) {
785 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000786 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100787 ret = ttm_bo_wait_unreserved(bo, interruptible);
788
789 kref_put(&bo->list_kref, ttm_bo_release_list);
790
791 /**
792 * We *need* to retry after releasing the lru lock.
793 */
794
795 if (unlikely(ret != 0))
796 return ret;
797 goto retry;
798 }
799
800 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100801 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100802
803 BUG_ON(ret != 0);
804
Dave Airlied6ea8882010-11-22 13:24:40 +1000805 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100806
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000807 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100808 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100809
Jerome Glisseca262a9992009-12-08 15:33:32 +0100810 kref_put(&bo->list_kref, ttm_bo_release_list);
811 return ret;
812}
813
Ben Skeggs42311ff2010-08-04 12:07:08 +1000814void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
815{
Ben Skeggsd961db72010-08-05 10:48:18 +1000816 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000817
Ben Skeggsd961db72010-08-05 10:48:18 +1000818 if (mem->mm_node)
819 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000820}
821EXPORT_SYMBOL(ttm_bo_mem_put);
822
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200823/**
824 * Repeatedly evict memory from the LRU for @mem_type until we create enough
825 * space, or we've evicted everything and there isn't enough space.
826 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100827static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
828 uint32_t mem_type,
829 struct ttm_placement *placement,
830 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000831 bool interruptible,
832 bool no_wait_reserve,
833 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200834{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100835 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200836 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200837 int ret;
838
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200839 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000840 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200841 if (unlikely(ret != 0))
842 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000843 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100844 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100845 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000846 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100847 if (unlikely(ret != 0))
848 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200849 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000850 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200851 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200852 mem->mem_type = mem_type;
853 return 0;
854}
855
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200856static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
857 uint32_t cur_placement,
858 uint32_t proposed_placement)
859{
860 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
861 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
862
863 /**
864 * Keep current caching if possible.
865 */
866
867 if ((cur_placement & caching) != 0)
868 result |= (cur_placement & caching);
869 else if ((man->default_caching & caching) != 0)
870 result |= man->default_caching;
871 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
872 result |= TTM_PL_FLAG_CACHED;
873 else if ((TTM_PL_FLAG_WC & caching) != 0)
874 result |= TTM_PL_FLAG_WC;
875 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
876 result |= TTM_PL_FLAG_UNCACHED;
877
878 return result;
879}
880
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200881static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
882 bool disallow_fixed,
883 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200884 uint32_t proposed_placement,
885 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200886{
887 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
888
889 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
890 return false;
891
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200892 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200893 return false;
894
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200895 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200896 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200897
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200898 cur_flags |= (proposed_placement & man->available_caching);
899
900 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200901 return true;
902}
903
904/**
905 * Creates space for memory region @mem according to its type.
906 *
907 * This function first searches for free space in compatible memory types in
908 * the priority order defined by the driver. If free space isn't found, then
909 * ttm_bo_mem_force_space is attempted in priority order to evict and find
910 * space.
911 */
912int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100913 struct ttm_placement *placement,
914 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000915 bool interruptible, bool no_wait_reserve,
916 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200917{
918 struct ttm_bo_device *bdev = bo->bdev;
919 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200920 uint32_t mem_type = TTM_PL_SYSTEM;
921 uint32_t cur_flags = 0;
922 bool type_found = false;
923 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100924 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100925 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926
927 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000928 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100929 ret = ttm_mem_type_from_flags(placement->placement[i],
930 &mem_type);
931 if (ret)
932 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200933 man = &bdev->man[mem_type];
934
935 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100936 bo->type == ttm_bo_type_user,
937 mem_type,
938 placement->placement[i],
939 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200940
941 if (!type_ok)
942 continue;
943
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200944 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
945 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100946 /*
947 * Use the access and other non-mapping-related flag bits from
948 * the memory placement flags to the current flags
949 */
950 ttm_flag_masked(&cur_flags, placement->placement[i],
951 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200952
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953 if (mem_type == TTM_PL_SYSTEM)
954 break;
955
956 if (man->has_type && man->use_type) {
957 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000958 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100959 if (unlikely(ret))
960 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200961 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000962 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200963 break;
964 }
965
Ben Skeggsd961db72010-08-05 10:48:18 +1000966 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200967 mem->mem_type = mem_type;
968 mem->placement = cur_flags;
969 return 0;
970 }
971
972 if (!type_found)
973 return -EINVAL;
974
Dave Airlieb6637522009-12-14 14:51:35 +1000975 for (i = 0; i < placement->num_busy_placement; ++i) {
976 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100977 &mem_type);
978 if (ret)
979 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200980 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200981 if (!man->has_type)
982 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200983 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100984 bo->type == ttm_bo_type_user,
985 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000986 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100987 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988 continue;
989
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200990 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
991 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100992 /*
993 * Use the access and other non-mapping-related flag bits from
994 * the memory placement flags to the current flags
995 */
Dave Airlieb6637522009-12-14 14:51:35 +1000996 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100997 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200998
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100999
1000 if (mem_type == TTM_PL_SYSTEM) {
1001 mem->mem_type = mem_type;
1002 mem->placement = cur_flags;
1003 mem->mm_node = NULL;
1004 return 0;
1005 }
1006
Jerome Glisseca262a9992009-12-08 15:33:32 +01001007 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001008 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001009 if (ret == 0 && mem->mm_node) {
1010 mem->placement = cur_flags;
1011 return 0;
1012 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001013 if (ret == -ERESTARTSYS)
1014 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001016 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001017 return ret;
1018}
1019EXPORT_SYMBOL(ttm_bo_mem_space);
1020
1021int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1022{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001023 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1024 return -EBUSY;
1025
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001026 return wait_event_interruptible(bo->event_queue,
1027 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001028}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001029EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001030
1031int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001032 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001033 bool interruptible, bool no_wait_reserve,
1034 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001035{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036 int ret = 0;
1037 struct ttm_mem_reg mem;
1038
1039 BUG_ON(!atomic_read(&bo->reserved));
1040
1041 /*
1042 * FIXME: It's possible to pipeline buffer moves.
1043 * Have the driver move function wait for idle when necessary,
1044 * instead of doing it here.
1045 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046 spin_lock(&bo->lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001047 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001048 spin_unlock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049 if (ret)
1050 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001051 mem.num_pages = bo->num_pages;
1052 mem.size = mem.num_pages << PAGE_SHIFT;
1053 mem.page_alignment = bo->mem.page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001054 mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001055 /*
1056 * Determine where to move the buffer.
1057 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001058 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001059 if (ret)
1060 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001061 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001062out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001063 if (ret && mem.mm_node)
1064 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001065 return ret;
1066}
1067
Jerome Glisseca262a9992009-12-08 15:33:32 +01001068static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001069 struct ttm_mem_reg *mem)
1070{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001071 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001072
Ben Skeggsd961db72010-08-05 10:48:18 +10001073 if (mem->mm_node && placement->lpfn != 0 &&
1074 (mem->start < placement->fpfn ||
1075 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001076 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077
Jerome Glisseca262a9992009-12-08 15:33:32 +01001078 for (i = 0; i < placement->num_placement; i++) {
1079 if ((placement->placement[i] & mem->placement &
1080 TTM_PL_MASK_CACHING) &&
1081 (placement->placement[i] & mem->placement &
1082 TTM_PL_MASK_MEM))
1083 return i;
1084 }
1085 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086}
1087
Jerome Glisse09855ac2009-12-10 17:16:27 +01001088int ttm_bo_validate(struct ttm_buffer_object *bo,
1089 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001090 bool interruptible, bool no_wait_reserve,
1091 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092{
1093 int ret;
1094
1095 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001096 /* Check that range is valid */
1097 if (placement->lpfn || placement->fpfn)
1098 if (placement->fpfn > placement->lpfn ||
1099 (placement->lpfn - placement->fpfn) < bo->num_pages)
1100 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101 /*
1102 * Check whether we need to move buffer.
1103 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001104 ret = ttm_bo_mem_compat(placement, &bo->mem);
1105 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001106 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001107 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001108 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001109 } else {
1110 /*
1111 * Use the access and other non-mapping-related flag bits from
1112 * the compatible memory placement flags to the active flags
1113 */
1114 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1115 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001116 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117 /*
1118 * We might need to add a TTM.
1119 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001120 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1121 ret = ttm_bo_add_ttm(bo, true);
1122 if (ret)
1123 return ret;
1124 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125 return 0;
1126}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001127EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001128
Jerome Glisse09855ac2009-12-10 17:16:27 +01001129int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1130 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001131{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001132 BUG_ON((placement->fpfn || placement->lpfn) &&
1133 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001134
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 return 0;
1136}
1137
Jerome Glisse09855ac2009-12-10 17:16:27 +01001138int ttm_bo_init(struct ttm_bo_device *bdev,
1139 struct ttm_buffer_object *bo,
1140 unsigned long size,
1141 enum ttm_bo_type type,
1142 struct ttm_placement *placement,
1143 uint32_t page_alignment,
1144 unsigned long buffer_start,
1145 bool interruptible,
1146 struct file *persistant_swap_storage,
1147 size_t acc_size,
1148 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001150 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001151 unsigned long num_pages;
1152
1153 size += buffer_start & ~PAGE_MASK;
1154 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1155 if (num_pages == 0) {
1156 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001157 if (destroy)
1158 (*destroy)(bo);
1159 else
1160 kfree(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161 return -EINVAL;
1162 }
1163 bo->destroy = destroy;
1164
1165 spin_lock_init(&bo->lock);
1166 kref_init(&bo->kref);
1167 kref_init(&bo->list_kref);
1168 atomic_set(&bo->cpu_writers, 0);
1169 atomic_set(&bo->reserved, 1);
1170 init_waitqueue_head(&bo->event_queue);
1171 INIT_LIST_HEAD(&bo->lru);
1172 INIT_LIST_HEAD(&bo->ddestroy);
1173 INIT_LIST_HEAD(&bo->swap);
1174 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001175 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001176 bo->type = type;
1177 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001178 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 bo->mem.mem_type = TTM_PL_SYSTEM;
1180 bo->mem.num_pages = bo->num_pages;
1181 bo->mem.mm_node = NULL;
1182 bo->mem.page_alignment = page_alignment;
Jerome Glisse82c5da62010-04-09 14:39:23 +02001183 bo->mem.bus.io_reserved = false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001184 bo->buffer_start = buffer_start & PAGE_MASK;
1185 bo->priv_flags = 0;
1186 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1187 bo->seq_valid = false;
1188 bo->persistant_swap_storage = persistant_swap_storage;
1189 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001190 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001191
Jerome Glisse09855ac2009-12-10 17:16:27 +01001192 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001193 if (unlikely(ret != 0))
1194 goto out_err;
1195
1196 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001197 * For ttm_bo_type_device buffers, allocate
1198 * address space from the device.
1199 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200 if (bo->type == ttm_bo_type_device) {
1201 ret = ttm_bo_setup_vm(bo);
1202 if (ret)
1203 goto out_err;
1204 }
1205
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001206 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001207 if (ret)
1208 goto out_err;
1209
1210 ttm_bo_unreserve(bo);
1211 return 0;
1212
1213out_err:
1214 ttm_bo_unreserve(bo);
1215 ttm_bo_unref(&bo);
1216
1217 return ret;
1218}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001219EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001220
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001221static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001222 unsigned long num_pages)
1223{
1224 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1225 PAGE_MASK;
1226
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001227 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228}
1229
Jerome Glisse09855ac2009-12-10 17:16:27 +01001230int ttm_bo_create(struct ttm_bo_device *bdev,
1231 unsigned long size,
1232 enum ttm_bo_type type,
1233 struct ttm_placement *placement,
1234 uint32_t page_alignment,
1235 unsigned long buffer_start,
1236 bool interruptible,
1237 struct file *persistant_swap_storage,
1238 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001239{
1240 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001241 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001242 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001243
1244 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001245 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001246 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001247 if (unlikely(ret != 0))
1248 return ret;
1249
1250 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1251
1252 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001253 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001254 return -ENOMEM;
1255 }
1256
Jerome Glisse09855ac2009-12-10 17:16:27 +01001257 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1258 buffer_start, interruptible,
1259 persistant_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001260 if (likely(ret == 0))
1261 *p_bo = bo;
1262
1263 return ret;
1264}
1265
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001266static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001267 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001268{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001269 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001270 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272
1273 /*
1274 * Can't use standard list traversal since we're unlocking.
1275 */
1276
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001277 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001278 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001279 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001280 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001281 if (ret) {
1282 if (allow_errors) {
1283 return ret;
1284 } else {
1285 printk(KERN_ERR TTM_PFX
1286 "Cleanup eviction failed\n");
1287 }
1288 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001289 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001290 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001291 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001292 return 0;
1293}
1294
1295int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1296{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001297 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298 int ret = -EINVAL;
1299
1300 if (mem_type >= TTM_NUM_MEM_TYPES) {
1301 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1302 return ret;
1303 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001304 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001305
1306 if (!man->has_type) {
1307 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1308 "memory manager type %u\n", mem_type);
1309 return ret;
1310 }
1311
1312 man->use_type = false;
1313 man->has_type = false;
1314
1315 ret = 0;
1316 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001317 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001318
Ben Skeggsd961db72010-08-05 10:48:18 +10001319 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001320 }
1321
1322 return ret;
1323}
1324EXPORT_SYMBOL(ttm_bo_clean_mm);
1325
1326int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1327{
1328 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1329
1330 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1331 printk(KERN_ERR TTM_PFX
1332 "Illegal memory manager memory type %u.\n",
1333 mem_type);
1334 return -EINVAL;
1335 }
1336
1337 if (!man->has_type) {
1338 printk(KERN_ERR TTM_PFX
1339 "Memory type %u has not been initialized.\n",
1340 mem_type);
1341 return 0;
1342 }
1343
Jerome Glisseca262a9992009-12-08 15:33:32 +01001344 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001345}
1346EXPORT_SYMBOL(ttm_bo_evict_mm);
1347
1348int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001349 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001350{
1351 int ret = -EINVAL;
1352 struct ttm_mem_type_manager *man;
1353
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001354 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001355 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001356 BUG_ON(man->has_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001357
1358 ret = bdev->driver->init_mem_type(bdev, type, man);
1359 if (ret)
1360 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001361 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001362
1363 ret = 0;
1364 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001365 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001366 if (ret)
1367 return ret;
1368 }
1369 man->has_type = true;
1370 man->use_type = true;
1371 man->size = p_size;
1372
1373 INIT_LIST_HEAD(&man->lru);
1374
1375 return 0;
1376}
1377EXPORT_SYMBOL(ttm_bo_init_mm);
1378
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001379static void ttm_bo_global_kobj_release(struct kobject *kobj)
1380{
1381 struct ttm_bo_global *glob =
1382 container_of(kobj, struct ttm_bo_global, kobj);
1383
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001384 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1385 __free_page(glob->dummy_read_page);
1386 kfree(glob);
1387}
1388
Dave Airlieba4420c2010-03-09 10:56:52 +10001389void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001390{
1391 struct ttm_bo_global *glob = ref->object;
1392
1393 kobject_del(&glob->kobj);
1394 kobject_put(&glob->kobj);
1395}
1396EXPORT_SYMBOL(ttm_bo_global_release);
1397
Dave Airlieba4420c2010-03-09 10:56:52 +10001398int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001399{
1400 struct ttm_bo_global_ref *bo_ref =
1401 container_of(ref, struct ttm_bo_global_ref, ref);
1402 struct ttm_bo_global *glob = ref->object;
1403 int ret;
1404
1405 mutex_init(&glob->device_list_mutex);
1406 spin_lock_init(&glob->lru_lock);
1407 glob->mem_glob = bo_ref->mem_glob;
1408 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1409
1410 if (unlikely(glob->dummy_read_page == NULL)) {
1411 ret = -ENOMEM;
1412 goto out_no_drp;
1413 }
1414
1415 INIT_LIST_HEAD(&glob->swap_lru);
1416 INIT_LIST_HEAD(&glob->device_list);
1417
1418 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1419 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1420 if (unlikely(ret != 0)) {
1421 printk(KERN_ERR TTM_PFX
1422 "Could not register buffer object swapout.\n");
1423 goto out_no_shrink;
1424 }
1425
1426 glob->ttm_bo_extra_size =
1427 ttm_round_pot(sizeof(struct ttm_tt)) +
1428 ttm_round_pot(sizeof(struct ttm_backend));
1429
1430 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1431 ttm_round_pot(sizeof(struct ttm_buffer_object));
1432
1433 atomic_set(&glob->bo_count, 0);
1434
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001435 ret = kobject_init_and_add(
1436 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001437 if (unlikely(ret != 0))
1438 kobject_put(&glob->kobj);
1439 return ret;
1440out_no_shrink:
1441 __free_page(glob->dummy_read_page);
1442out_no_drp:
1443 kfree(glob);
1444 return ret;
1445}
1446EXPORT_SYMBOL(ttm_bo_global_init);
1447
1448
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001449int ttm_bo_device_release(struct ttm_bo_device *bdev)
1450{
1451 int ret = 0;
1452 unsigned i = TTM_NUM_MEM_TYPES;
1453 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001454 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001455
1456 while (i--) {
1457 man = &bdev->man[i];
1458 if (man->has_type) {
1459 man->use_type = false;
1460 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1461 ret = -EBUSY;
1462 printk(KERN_ERR TTM_PFX
1463 "DRM memory manager type %d "
1464 "is not clean.\n", i);
1465 }
1466 man->has_type = false;
1467 }
1468 }
1469
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001470 mutex_lock(&glob->device_list_mutex);
1471 list_del(&bdev->device_list);
1472 mutex_unlock(&glob->device_list_mutex);
1473
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001474 if (!cancel_delayed_work(&bdev->wq))
1475 flush_scheduled_work();
1476
1477 while (ttm_bo_delayed_delete(bdev, true))
1478 ;
1479
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001480 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001481 if (list_empty(&bdev->ddestroy))
1482 TTM_DEBUG("Delayed destroy list was clean\n");
1483
1484 if (list_empty(&bdev->man[0].lru))
1485 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001486 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001487
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1489 write_lock(&bdev->vm_lock);
1490 drm_mm_takedown(&bdev->addr_space_mm);
1491 write_unlock(&bdev->vm_lock);
1492
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001493 return ret;
1494}
1495EXPORT_SYMBOL(ttm_bo_device_release);
1496
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001498 struct ttm_bo_global *glob,
1499 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001500 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001501 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001502{
1503 int ret = -EINVAL;
1504
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001505 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001506 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001507
1508 memset(bdev->man, 0, sizeof(bdev->man));
1509
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510 /*
1511 * Initialize the system memory buffer type.
1512 * Other types need to be driver / IOCTL initialized.
1513 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001514 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001515 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001516 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517
1518 bdev->addr_space_rb = RB_ROOT;
1519 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1520 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001521 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522
1523 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1524 bdev->nice_mode = true;
1525 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001527 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001528 bdev->need_dma32 = need_dma32;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001530 mutex_lock(&glob->device_list_mutex);
1531 list_add_tail(&bdev->device_list, &glob->device_list);
1532 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001533
1534 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001535out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001537out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538 return ret;
1539}
1540EXPORT_SYMBOL(ttm_bo_device_init);
1541
1542/*
1543 * buffer object vm functions.
1544 */
1545
1546bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1547{
1548 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1549
1550 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1551 if (mem->mem_type == TTM_PL_SYSTEM)
1552 return false;
1553
1554 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1555 return false;
1556
1557 if (mem->placement & TTM_PL_FLAG_CACHED)
1558 return false;
1559 }
1560 return true;
1561}
1562
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1564{
1565 struct ttm_bo_device *bdev = bo->bdev;
1566 loff_t offset = (loff_t) bo->addr_space_offset;
1567 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1568
1569 if (!bdev->dev_mapping)
1570 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001571 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Jerome Glisse82c5da62010-04-09 14:39:23 +02001572 ttm_mem_io_free(bdev, &bo->mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001573}
Dave Airliee024e112009-06-24 09:48:08 +10001574EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575
1576static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1577{
1578 struct ttm_bo_device *bdev = bo->bdev;
1579 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1580 struct rb_node *parent = NULL;
1581 struct ttm_buffer_object *cur_bo;
1582 unsigned long offset = bo->vm_node->start;
1583 unsigned long cur_offset;
1584
1585 while (*cur) {
1586 parent = *cur;
1587 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1588 cur_offset = cur_bo->vm_node->start;
1589 if (offset < cur_offset)
1590 cur = &parent->rb_left;
1591 else if (offset > cur_offset)
1592 cur = &parent->rb_right;
1593 else
1594 BUG();
1595 }
1596
1597 rb_link_node(&bo->vm_rb, parent, cur);
1598 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1599}
1600
1601/**
1602 * ttm_bo_setup_vm:
1603 *
1604 * @bo: the buffer to allocate address space for
1605 *
1606 * Allocate address space in the drm device so that applications
1607 * can mmap the buffer and access the contents. This only
1608 * applies to ttm_bo_type_device objects as others are not
1609 * placed in the drm device address space.
1610 */
1611
1612static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1613{
1614 struct ttm_bo_device *bdev = bo->bdev;
1615 int ret;
1616
1617retry_pre_get:
1618 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1619 if (unlikely(ret != 0))
1620 return ret;
1621
1622 write_lock(&bdev->vm_lock);
1623 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1624 bo->mem.num_pages, 0, 0);
1625
1626 if (unlikely(bo->vm_node == NULL)) {
1627 ret = -ENOMEM;
1628 goto out_unlock;
1629 }
1630
1631 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1632 bo->mem.num_pages, 0);
1633
1634 if (unlikely(bo->vm_node == NULL)) {
1635 write_unlock(&bdev->vm_lock);
1636 goto retry_pre_get;
1637 }
1638
1639 ttm_bo_vm_insert_rb(bo);
1640 write_unlock(&bdev->vm_lock);
1641 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1642
1643 return 0;
1644out_unlock:
1645 write_unlock(&bdev->vm_lock);
1646 return ret;
1647}
1648
1649int ttm_bo_wait(struct ttm_buffer_object *bo,
1650 bool lazy, bool interruptible, bool no_wait)
1651{
1652 struct ttm_bo_driver *driver = bo->bdev->driver;
1653 void *sync_obj;
1654 void *sync_obj_arg;
1655 int ret = 0;
1656
1657 if (likely(bo->sync_obj == NULL))
1658 return 0;
1659
1660 while (bo->sync_obj) {
1661
1662 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1663 void *tmp_obj = bo->sync_obj;
1664 bo->sync_obj = NULL;
1665 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1666 spin_unlock(&bo->lock);
1667 driver->sync_obj_unref(&tmp_obj);
1668 spin_lock(&bo->lock);
1669 continue;
1670 }
1671
1672 if (no_wait)
1673 return -EBUSY;
1674
1675 sync_obj = driver->sync_obj_ref(bo->sync_obj);
1676 sync_obj_arg = bo->sync_obj_arg;
1677 spin_unlock(&bo->lock);
1678 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1679 lazy, interruptible);
1680 if (unlikely(ret != 0)) {
1681 driver->sync_obj_unref(&sync_obj);
1682 spin_lock(&bo->lock);
1683 return ret;
1684 }
1685 spin_lock(&bo->lock);
1686 if (likely(bo->sync_obj == sync_obj &&
1687 bo->sync_obj_arg == sync_obj_arg)) {
1688 void *tmp_obj = bo->sync_obj;
1689 bo->sync_obj = NULL;
1690 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1691 &bo->priv_flags);
1692 spin_unlock(&bo->lock);
1693 driver->sync_obj_unref(&sync_obj);
1694 driver->sync_obj_unref(&tmp_obj);
1695 spin_lock(&bo->lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001696 } else {
1697 spin_unlock(&bo->lock);
1698 driver->sync_obj_unref(&sync_obj);
1699 spin_lock(&bo->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001700 }
1701 }
1702 return 0;
1703}
1704EXPORT_SYMBOL(ttm_bo_wait);
1705
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001706int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1707{
1708 int ret = 0;
1709
1710 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001711 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001712 */
1713
1714 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1715 if (unlikely(ret != 0))
1716 return ret;
1717 spin_lock(&bo->lock);
1718 ret = ttm_bo_wait(bo, false, true, no_wait);
1719 spin_unlock(&bo->lock);
1720 if (likely(ret == 0))
1721 atomic_inc(&bo->cpu_writers);
1722 ttm_bo_unreserve(bo);
1723 return ret;
1724}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001725EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001726
1727void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1728{
1729 if (atomic_dec_and_test(&bo->cpu_writers))
1730 wake_up_all(&bo->event_queue);
1731}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001732EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001733
1734/**
1735 * A buffer object shrink method that tries to swap out the first
1736 * buffer object on the bo_global::swap_lru list.
1737 */
1738
1739static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1740{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001741 struct ttm_bo_global *glob =
1742 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001743 struct ttm_buffer_object *bo;
1744 int ret = -EBUSY;
1745 int put_count;
1746 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1747
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001748 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001749 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001750 if (unlikely(list_empty(&glob->swap_lru))) {
1751 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001752 return -EBUSY;
1753 }
1754
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001755 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001756 struct ttm_buffer_object, swap);
1757 kref_get(&bo->list_kref);
1758
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001759 if (!list_empty(&bo->ddestroy)) {
1760 spin_unlock(&glob->lru_lock);
1761 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1762 kref_put(&bo->list_kref, ttm_bo_release_list);
1763 continue;
1764 }
1765
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001766 /**
1767 * Reserve buffer. Since we unlock while sleeping, we need
1768 * to re-check that nobody removed us from the swap-list while
1769 * we slept.
1770 */
1771
1772 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1773 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001774 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001775 ttm_bo_wait_unreserved(bo, false);
1776 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001777 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001778 }
1779 }
1780
1781 BUG_ON(ret != 0);
1782 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001783 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001784
Dave Airlied6ea8882010-11-22 13:24:40 +10001785 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001786
1787 /**
1788 * Wait for GPU, then move to system cached.
1789 */
1790
1791 spin_lock(&bo->lock);
1792 ret = ttm_bo_wait(bo, false, false, false);
1793 spin_unlock(&bo->lock);
1794
1795 if (unlikely(ret != 0))
1796 goto out;
1797
1798 if ((bo->mem.placement & swap_placement) != swap_placement) {
1799 struct ttm_mem_reg evict_mem;
1800
1801 evict_mem = bo->mem;
1802 evict_mem.mm_node = NULL;
1803 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1804 evict_mem.mem_type = TTM_PL_SYSTEM;
1805
1806 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001807 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001808 if (unlikely(ret != 0))
1809 goto out;
1810 }
1811
1812 ttm_bo_unmap_virtual(bo);
1813
1814 /**
1815 * Swap out. Buffer will be swapped in again as soon as
1816 * anyone tries to access a ttm page.
1817 */
1818
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001819 if (bo->bdev->driver->swap_notify)
1820 bo->bdev->driver->swap_notify(bo);
1821
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001822 ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage);
1823out:
1824
1825 /**
1826 *
1827 * Unreserve without putting on LRU to avoid swapping out an
1828 * already swapped buffer.
1829 */
1830
1831 atomic_set(&bo->reserved, 0);
1832 wake_up_all(&bo->event_queue);
1833 kref_put(&bo->list_kref, ttm_bo_release_list);
1834 return ret;
1835}
1836
1837void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1838{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001839 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001840 ;
1841}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001842EXPORT_SYMBOL(ttm_bo_swapout_all);