blob: 119b6e3ff9063ad5d8980f0bbf45efbd553be711 [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;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500140 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200141
142 BUG_ON(atomic_read(&bo->list_kref.refcount));
143 BUG_ON(atomic_read(&bo->kref.refcount));
144 BUG_ON(atomic_read(&bo->cpu_writers));
145 BUG_ON(bo->sync_obj != NULL);
146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
149
150 if (bo->ttm)
151 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200152 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200153 if (bo->destroy)
154 bo->destroy(bo);
155 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 kfree(bo);
157 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500158 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200159}
160
161int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
162{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200163 if (interruptible) {
Jean Delvare965d3802010-10-09 12:36:45 +0000164 return wait_event_interruptible(bo->event_queue,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165 atomic_read(&bo->reserved) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200166 } else {
167 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
Jean Delvare965d3802010-10-09 12:36:45 +0000168 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200170}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000171EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200172
Dave Airlied6ea8882010-11-22 13:24:40 +1000173void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200174{
175 struct ttm_bo_device *bdev = bo->bdev;
176 struct ttm_mem_type_manager *man;
177
178 BUG_ON(!atomic_read(&bo->reserved));
179
180 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
181
182 BUG_ON(!list_empty(&bo->lru));
183
184 man = &bdev->man[bo->mem.mem_type];
185 list_add_tail(&bo->lru, &man->lru);
186 kref_get(&bo->list_kref);
187
188 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200189 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200190 kref_get(&bo->list_kref);
191 }
192 }
193}
194
Dave Airlied6ea8882010-11-22 13:24:40 +1000195int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200196{
197 int put_count = 0;
198
199 if (!list_empty(&bo->swap)) {
200 list_del_init(&bo->swap);
201 ++put_count;
202 }
203 if (!list_empty(&bo->lru)) {
204 list_del_init(&bo->lru);
205 ++put_count;
206 }
207
208 /*
209 * TODO: Add a driver hook to delete from
210 * driver-specific LRU's here.
211 */
212
213 return put_count;
214}
215
216int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
217 bool interruptible,
218 bool no_wait, bool use_sequence, uint32_t sequence)
219{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200220 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200221 int ret;
222
223 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100224 /**
225 * Deadlock avoidance for multi-bo reserving.
226 */
Thomas Hellstrom96726fe2010-11-17 12:28:28 +0000227 if (use_sequence && bo->seq_valid) {
228 /**
229 * We've already reserved this one.
230 */
231 if (unlikely(sequence == bo->val_seq))
232 return -EDEADLK;
233 /**
234 * Already reserved by a thread that will not back
235 * off for us. We need to back off.
236 */
237 if (unlikely(sequence - bo->val_seq < (1 << 31)))
238 return -EAGAIN;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200239 }
240
241 if (no_wait)
242 return -EBUSY;
243
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200244 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200245 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200246 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200247
248 if (unlikely(ret))
249 return ret;
250 }
251
252 if (use_sequence) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100253 /**
254 * Wake up waiters that may need to recheck for deadlock,
255 * if we decreased the sequence number.
256 */
257 if (unlikely((bo->val_seq - sequence < (1 << 31))
258 || !bo->seq_valid))
259 wake_up_all(&bo->event_queue);
260
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200261 bo->val_seq = sequence;
262 bo->seq_valid = true;
263 } else {
264 bo->seq_valid = false;
265 }
266
267 return 0;
268}
269EXPORT_SYMBOL(ttm_bo_reserve);
270
271static void ttm_bo_ref_bug(struct kref *list_kref)
272{
273 BUG();
274}
275
Dave Airlied6ea8882010-11-22 13:24:40 +1000276void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
277 bool never_free)
278{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100279 kref_sub(&bo->list_kref, count,
280 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000281}
282
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200283int ttm_bo_reserve(struct ttm_buffer_object *bo,
284 bool interruptible,
285 bool no_wait, bool use_sequence, uint32_t sequence)
286{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200287 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200288 int put_count = 0;
289 int ret;
290
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200291 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200292 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
293 sequence);
294 if (likely(ret == 0))
295 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200296 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200297
Dave Airlied6ea8882010-11-22 13:24:40 +1000298 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200299
300 return ret;
301}
302
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000303void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
304{
305 ttm_bo_add_to_lru(bo);
306 atomic_set(&bo->reserved, 0);
307 wake_up_all(&bo->event_queue);
308}
309
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200310void ttm_bo_unreserve(struct ttm_buffer_object *bo)
311{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200312 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200313
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200314 spin_lock(&glob->lru_lock);
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000315 ttm_bo_unreserve_locked(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200316 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200317}
318EXPORT_SYMBOL(ttm_bo_unreserve);
319
320/*
321 * Call bo->mutex locked.
322 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
324{
325 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200326 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200327 int ret = 0;
328 uint32_t page_flags = 0;
329
330 TTM_ASSERT_LOCKED(&bo->mutex);
331 bo->ttm = NULL;
332
Dave Airliead49f502009-07-10 22:36:26 +1000333 if (bdev->need_dma32)
334 page_flags |= TTM_PAGE_FLAG_DMA32;
335
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200336 switch (bo->type) {
337 case ttm_bo_type_device:
338 if (zero_alloc)
339 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
340 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400341 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
342 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200343 if (unlikely(bo->ttm == NULL))
344 ret = -ENOMEM;
345 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200346 default:
347 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
348 ret = -EINVAL;
349 break;
350 }
351
352 return ret;
353}
354
355static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
356 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000357 bool evict, bool interruptible,
358 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200359{
360 struct ttm_bo_device *bdev = bo->bdev;
361 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
362 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
363 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
364 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
365 int ret = 0;
366
367 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100368 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
369 ret = ttm_mem_io_lock(old_man, true);
370 if (unlikely(ret != 0))
371 goto out_err;
372 ttm_bo_unmap_virtual_locked(bo);
373 ttm_mem_io_unlock(old_man);
374 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200375
376 /*
377 * Create and bind a ttm if required.
378 */
379
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000380 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
381 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000382 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
383 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000384 if (ret)
385 goto out_err;
386 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200387
388 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
389 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200390 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200391
392 if (mem->mem_type != TTM_PL_SYSTEM) {
393 ret = ttm_tt_bind(bo->ttm, mem);
394 if (ret)
395 goto out_err;
396 }
397
398 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000399 if (bdev->driver->move_notify)
400 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100401 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200402 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200403 goto moved;
404 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200405 }
406
407 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
408 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000409 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200410 else if (bdev->driver->move)
411 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000412 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200413 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000414 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200415
416 if (ret)
417 goto out_err;
418
Jerome Glissedc97b342011-11-18 11:47:03 -0500419 if (bdev->driver->move_notify)
420 bdev->driver->move_notify(bo, mem);
421
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200422moved:
423 if (bo->evicted) {
424 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
425 if (ret)
426 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
427 bo->evicted = false;
428 }
429
430 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000431 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200432 bdev->man[bo->mem.mem_type].gpu_offset;
433 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100434 } else
435 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200436
437 return 0;
438
439out_err:
440 new_man = &bdev->man[bo->mem.mem_type];
441 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
442 ttm_tt_unbind(bo->ttm);
443 ttm_tt_destroy(bo->ttm);
444 bo->ttm = NULL;
445 }
446
447 return ret;
448}
449
450/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200451 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200452 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200453 * This is the place to put in driver specific hooks to release
454 * driver private resources.
455 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200456 */
457
458static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
459{
Jerome Glissedc97b342011-11-18 11:47:03 -0500460 if (bo->bdev->driver->move_notify)
461 bo->bdev->driver->move_notify(bo, NULL);
462
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200463 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200464 ttm_tt_unbind(bo->ttm);
465 ttm_tt_destroy(bo->ttm);
466 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200467 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200468 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200469
470 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200471
472 /*
473 * Make processes trying to reserve really pick it up.
474 */
475 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200476 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200477}
478
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200479static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200480{
481 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200482 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200483 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000484 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200485 void *sync_obj_arg;
486 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200487 int ret;
488
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000489 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200490 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200491 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200492
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200493 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100494
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200495 /**
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000496 * Lock inversion between bo:reserve and bdev::fence_lock here,
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200497 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200498 */
499
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200500 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200501
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200502 if (unlikely(ret == -EBUSY))
503 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200504
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000505 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200506 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200507
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200508 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200509 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200510
Dave Airlied6ea8882010-11-22 13:24:40 +1000511 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200512
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200513 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200514 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200515 spin_lock(&glob->lru_lock);
516 }
517queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200518 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000519 if (bo->sync_obj)
520 sync_obj = driver->sync_obj_ref(bo->sync_obj);
521 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200522
523 kref_get(&bo->list_kref);
524 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
525 spin_unlock(&glob->lru_lock);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000526 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200527
Thomas Hellstromaa123262010-11-02 13:21:47 +0000528 if (sync_obj) {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200529 driver->sync_obj_flush(sync_obj, sync_obj_arg);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000530 driver->sync_obj_unref(&sync_obj);
531 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200532 schedule_delayed_work(&bdev->wq,
533 ((HZ / 100) < 1) ? 1 : HZ / 100);
534}
535
536/**
537 * function ttm_bo_cleanup_refs
538 * If bo idle, remove from delayed- and lru lists, and unref.
539 * If not idle, do nothing.
540 *
541 * @interruptible Any sleeps should occur interruptibly.
542 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
543 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
544 */
545
546static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
547 bool interruptible,
548 bool no_wait_reserve,
549 bool no_wait_gpu)
550{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000551 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200552 struct ttm_bo_global *glob = bo->glob;
553 int put_count;
554 int ret = 0;
555
556retry:
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000557 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200558 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000559 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200560
561 if (unlikely(ret != 0))
562 return ret;
563
564 spin_lock(&glob->lru_lock);
565 ret = ttm_bo_reserve_locked(bo, interruptible,
566 no_wait_reserve, false, 0);
567
568 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200569 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200570 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200571 }
572
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200573 /**
574 * We can re-check for sync object without taking
575 * the bo::lock since setting the sync object requires
576 * also bo::reserved. A busy object at this point may
577 * be caused by another thread recently starting an accelerated
578 * eviction.
579 */
580
581 if (unlikely(bo->sync_obj)) {
582 atomic_set(&bo->reserved, 0);
583 wake_up_all(&bo->event_queue);
584 spin_unlock(&glob->lru_lock);
585 goto retry;
586 }
587
588 put_count = ttm_bo_del_from_lru(bo);
589 list_del_init(&bo->ddestroy);
590 ++put_count;
591
592 spin_unlock(&glob->lru_lock);
593 ttm_bo_cleanup_memtype_use(bo);
594
Dave Airlied6ea8882010-11-22 13:24:40 +1000595 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200596
597 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200598}
599
600/**
601 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
602 * encountered buffers.
603 */
604
605static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
606{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200607 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100608 struct ttm_buffer_object *entry = NULL;
609 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200610
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200611 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100612 if (list_empty(&bdev->ddestroy))
613 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200614
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100615 entry = list_first_entry(&bdev->ddestroy,
616 struct ttm_buffer_object, ddestroy);
617 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200618
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100619 for (;;) {
620 struct ttm_buffer_object *nentry = NULL;
621
622 if (entry->ddestroy.next != &bdev->ddestroy) {
623 nentry = list_first_entry(&entry->ddestroy,
624 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200625 kref_get(&nentry->list_kref);
626 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200627
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200628 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200629 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
630 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200631 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100632 entry = nentry;
633
634 if (ret || !entry)
635 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200636
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200637 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100638 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200639 break;
640 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200641
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100642out_unlock:
643 spin_unlock(&glob->lru_lock);
644out:
645 if (entry)
646 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200647 return ret;
648}
649
650static void ttm_bo_delayed_workqueue(struct work_struct *work)
651{
652 struct ttm_bo_device *bdev =
653 container_of(work, struct ttm_bo_device, wq.work);
654
655 if (ttm_bo_delayed_delete(bdev, false)) {
656 schedule_delayed_work(&bdev->wq,
657 ((HZ / 100) < 1) ? 1 : HZ / 100);
658 }
659}
660
661static void ttm_bo_release(struct kref *kref)
662{
663 struct ttm_buffer_object *bo =
664 container_of(kref, struct ttm_buffer_object, kref);
665 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100666 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200667
668 if (likely(bo->vm_node != NULL)) {
669 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
670 drm_mm_put_block(bo->vm_node);
671 bo->vm_node = NULL;
672 }
673 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100674 ttm_mem_io_lock(man, false);
675 ttm_mem_io_free_vm(bo);
676 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200677 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200678 kref_put(&bo->list_kref, ttm_bo_release_list);
679 write_lock(&bdev->vm_lock);
680}
681
682void ttm_bo_unref(struct ttm_buffer_object **p_bo)
683{
684 struct ttm_buffer_object *bo = *p_bo;
685 struct ttm_bo_device *bdev = bo->bdev;
686
687 *p_bo = NULL;
688 write_lock(&bdev->vm_lock);
689 kref_put(&bo->kref, ttm_bo_release);
690 write_unlock(&bdev->vm_lock);
691}
692EXPORT_SYMBOL(ttm_bo_unref);
693
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400694int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
695{
696 return cancel_delayed_work_sync(&bdev->wq);
697}
698EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
699
700void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
701{
702 if (resched)
703 schedule_delayed_work(&bdev->wq,
704 ((HZ / 100) < 1) ? 1 : HZ / 100);
705}
706EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
707
Jerome Glisseca262a9992009-12-08 15:33:32 +0100708static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000709 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200710{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200711 struct ttm_bo_device *bdev = bo->bdev;
712 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100713 struct ttm_placement placement;
714 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200715
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000716 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200717 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000718 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200719
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200720 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100721 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200722 printk(KERN_ERR TTM_PFX
723 "Failed to expire sync object before "
724 "buffer eviction.\n");
725 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200726 goto out;
727 }
728
729 BUG_ON(!atomic_read(&bo->reserved));
730
731 evict_mem = bo->mem;
732 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100733 evict_mem.bus.io_reserved_vm = false;
734 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200735
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100736 placement.fpfn = 0;
737 placement.lpfn = 0;
738 placement.num_placement = 0;
739 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100740 bdev->driver->evict_flags(bo, &placement);
741 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000742 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200743 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100744 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200745 printk(KERN_ERR TTM_PFX
746 "Failed to find memory space for "
747 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100748 ttm_bo_mem_space_debug(bo, &placement);
749 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200750 goto out;
751 }
752
753 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000754 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200755 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100756 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200757 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000758 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200759 goto out;
760 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200761 bo->evicted = true;
762out:
763 return ret;
764}
765
Jerome Glisseca262a9992009-12-08 15:33:32 +0100766static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
767 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000768 bool interruptible, bool no_wait_reserve,
769 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100770{
771 struct ttm_bo_global *glob = bdev->glob;
772 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
773 struct ttm_buffer_object *bo;
774 int ret, put_count = 0;
775
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100776retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100777 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100778 if (list_empty(&man->lru)) {
779 spin_unlock(&glob->lru_lock);
780 return -EBUSY;
781 }
782
Jerome Glisseca262a9992009-12-08 15:33:32 +0100783 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
784 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100785
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200786 if (!list_empty(&bo->ddestroy)) {
787 spin_unlock(&glob->lru_lock);
788 ret = ttm_bo_cleanup_refs(bo, interruptible,
789 no_wait_reserve, no_wait_gpu);
790 kref_put(&bo->list_kref, ttm_bo_release_list);
791
792 if (likely(ret == 0 || ret == -ERESTARTSYS))
793 return ret;
794
795 goto retry;
796 }
797
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000798 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100799
800 if (unlikely(ret == -EBUSY)) {
801 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000802 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100803 ret = ttm_bo_wait_unreserved(bo, interruptible);
804
805 kref_put(&bo->list_kref, ttm_bo_release_list);
806
807 /**
808 * We *need* to retry after releasing the lru lock.
809 */
810
811 if (unlikely(ret != 0))
812 return ret;
813 goto retry;
814 }
815
816 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100817 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100818
819 BUG_ON(ret != 0);
820
Dave Airlied6ea8882010-11-22 13:24:40 +1000821 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100822
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000823 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100824 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100825
Jerome Glisseca262a9992009-12-08 15:33:32 +0100826 kref_put(&bo->list_kref, ttm_bo_release_list);
827 return ret;
828}
829
Ben Skeggs42311ff2010-08-04 12:07:08 +1000830void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
831{
Ben Skeggsd961db72010-08-05 10:48:18 +1000832 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000833
Ben Skeggsd961db72010-08-05 10:48:18 +1000834 if (mem->mm_node)
835 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000836}
837EXPORT_SYMBOL(ttm_bo_mem_put);
838
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200839/**
840 * Repeatedly evict memory from the LRU for @mem_type until we create enough
841 * space, or we've evicted everything and there isn't enough space.
842 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100843static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
844 uint32_t mem_type,
845 struct ttm_placement *placement,
846 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000847 bool interruptible,
848 bool no_wait_reserve,
849 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200850{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100851 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200852 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200853 int ret;
854
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200855 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000856 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200857 if (unlikely(ret != 0))
858 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000859 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100860 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100861 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000862 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100863 if (unlikely(ret != 0))
864 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000866 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200868 mem->mem_type = mem_type;
869 return 0;
870}
871
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200872static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
873 uint32_t cur_placement,
874 uint32_t proposed_placement)
875{
876 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
877 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
878
879 /**
880 * Keep current caching if possible.
881 */
882
883 if ((cur_placement & caching) != 0)
884 result |= (cur_placement & caching);
885 else if ((man->default_caching & caching) != 0)
886 result |= man->default_caching;
887 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
888 result |= TTM_PL_FLAG_CACHED;
889 else if ((TTM_PL_FLAG_WC & caching) != 0)
890 result |= TTM_PL_FLAG_WC;
891 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
892 result |= TTM_PL_FLAG_UNCACHED;
893
894 return result;
895}
896
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200897static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200899 uint32_t proposed_placement,
900 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200901{
902 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
903
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200904 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200905 return false;
906
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200907 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200908 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200909
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200910 cur_flags |= (proposed_placement & man->available_caching);
911
912 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200913 return true;
914}
915
916/**
917 * Creates space for memory region @mem according to its type.
918 *
919 * This function first searches for free space in compatible memory types in
920 * the priority order defined by the driver. If free space isn't found, then
921 * ttm_bo_mem_force_space is attempted in priority order to evict and find
922 * space.
923 */
924int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100925 struct ttm_placement *placement,
926 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000927 bool interruptible, bool no_wait_reserve,
928 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929{
930 struct ttm_bo_device *bdev = bo->bdev;
931 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200932 uint32_t mem_type = TTM_PL_SYSTEM;
933 uint32_t cur_flags = 0;
934 bool type_found = false;
935 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100936 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100937 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938
939 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000940 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100941 ret = ttm_mem_type_from_flags(placement->placement[i],
942 &mem_type);
943 if (ret)
944 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200945 man = &bdev->man[mem_type];
946
947 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100948 mem_type,
949 placement->placement[i],
950 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951
952 if (!type_ok)
953 continue;
954
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200955 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
956 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100957 /*
958 * Use the access and other non-mapping-related flag bits from
959 * the memory placement flags to the current flags
960 */
961 ttm_flag_masked(&cur_flags, placement->placement[i],
962 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200963
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964 if (mem_type == TTM_PL_SYSTEM)
965 break;
966
967 if (man->has_type && man->use_type) {
968 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000969 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100970 if (unlikely(ret))
971 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200972 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000973 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200974 break;
975 }
976
Ben Skeggsd961db72010-08-05 10:48:18 +1000977 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200978 mem->mem_type = mem_type;
979 mem->placement = cur_flags;
980 return 0;
981 }
982
983 if (!type_found)
984 return -EINVAL;
985
Dave Airlieb6637522009-12-14 14:51:35 +1000986 for (i = 0; i < placement->num_busy_placement; ++i) {
987 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100988 &mem_type);
989 if (ret)
990 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200991 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200992 if (!man->has_type)
993 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200994 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100995 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000996 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100997 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200998 continue;
999
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001000 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1001 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001002 /*
1003 * Use the access and other non-mapping-related flag bits from
1004 * the memory placement flags to the current flags
1005 */
Dave Airlieb6637522009-12-14 14:51:35 +10001006 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001007 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001008
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001009
1010 if (mem_type == TTM_PL_SYSTEM) {
1011 mem->mem_type = mem_type;
1012 mem->placement = cur_flags;
1013 mem->mm_node = NULL;
1014 return 0;
1015 }
1016
Jerome Glisseca262a9992009-12-08 15:33:32 +01001017 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001018 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019 if (ret == 0 && mem->mm_node) {
1020 mem->placement = cur_flags;
1021 return 0;
1022 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001023 if (ret == -ERESTARTSYS)
1024 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001025 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001026 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001027 return ret;
1028}
1029EXPORT_SYMBOL(ttm_bo_mem_space);
1030
1031int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1032{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001033 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1034 return -EBUSY;
1035
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001036 return wait_event_interruptible(bo->event_queue,
1037 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001038}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001039EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001040
1041int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001042 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001043 bool interruptible, bool no_wait_reserve,
1044 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001045{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046 int ret = 0;
1047 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001048 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049
1050 BUG_ON(!atomic_read(&bo->reserved));
1051
1052 /*
1053 * FIXME: It's possible to pipeline buffer moves.
1054 * Have the driver move function wait for idle when necessary,
1055 * instead of doing it here.
1056 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001057 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001058 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001059 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060 if (ret)
1061 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001062 mem.num_pages = bo->num_pages;
1063 mem.size = mem.num_pages << PAGE_SHIFT;
1064 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001065 mem.bus.io_reserved_vm = false;
1066 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001067 /*
1068 * Determine where to move the buffer.
1069 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001070 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001071 if (ret)
1072 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001073 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001075 if (ret && mem.mm_node)
1076 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077 return ret;
1078}
1079
Jerome Glisseca262a9992009-12-08 15:33:32 +01001080static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001081 struct ttm_mem_reg *mem)
1082{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001083 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001084
Ben Skeggsd961db72010-08-05 10:48:18 +10001085 if (mem->mm_node && placement->lpfn != 0 &&
1086 (mem->start < placement->fpfn ||
1087 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001088 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089
Jerome Glisseca262a9992009-12-08 15:33:32 +01001090 for (i = 0; i < placement->num_placement; i++) {
1091 if ((placement->placement[i] & mem->placement &
1092 TTM_PL_MASK_CACHING) &&
1093 (placement->placement[i] & mem->placement &
1094 TTM_PL_MASK_MEM))
1095 return i;
1096 }
1097 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098}
1099
Jerome Glisse09855ac2009-12-10 17:16:27 +01001100int ttm_bo_validate(struct ttm_buffer_object *bo,
1101 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001102 bool interruptible, bool no_wait_reserve,
1103 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001104{
1105 int ret;
1106
1107 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001108 /* Check that range is valid */
1109 if (placement->lpfn || placement->fpfn)
1110 if (placement->fpfn > placement->lpfn ||
1111 (placement->lpfn - placement->fpfn) < bo->num_pages)
1112 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113 /*
1114 * Check whether we need to move buffer.
1115 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001116 ret = ttm_bo_mem_compat(placement, &bo->mem);
1117 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001118 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001119 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001120 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001121 } else {
1122 /*
1123 * Use the access and other non-mapping-related flag bits from
1124 * the compatible memory placement flags to the active flags
1125 */
1126 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1127 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001128 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001129 /*
1130 * We might need to add a TTM.
1131 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001132 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1133 ret = ttm_bo_add_ttm(bo, true);
1134 if (ret)
1135 return ret;
1136 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001137 return 0;
1138}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001139EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001140
Jerome Glisse09855ac2009-12-10 17:16:27 +01001141int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1142 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001144 BUG_ON((placement->fpfn || placement->lpfn) &&
1145 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001147 return 0;
1148}
1149
Jerome Glisse09855ac2009-12-10 17:16:27 +01001150int ttm_bo_init(struct ttm_bo_device *bdev,
1151 struct ttm_buffer_object *bo,
1152 unsigned long size,
1153 enum ttm_bo_type type,
1154 struct ttm_placement *placement,
1155 uint32_t page_alignment,
1156 unsigned long buffer_start,
1157 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001158 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001159 size_t acc_size,
1160 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001162 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001163 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001164 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1165
1166 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1167 if (ret) {
1168 printk(KERN_ERR TTM_PFX "Out of kernel memory.\n");
1169 if (destroy)
1170 (*destroy)(bo);
1171 else
1172 kfree(bo);
1173 return -ENOMEM;
1174 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001175
1176 size += buffer_start & ~PAGE_MASK;
1177 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1178 if (num_pages == 0) {
1179 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001180 if (destroy)
1181 (*destroy)(bo);
1182 else
1183 kfree(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001184 return -EINVAL;
1185 }
1186 bo->destroy = destroy;
1187
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001188 kref_init(&bo->kref);
1189 kref_init(&bo->list_kref);
1190 atomic_set(&bo->cpu_writers, 0);
1191 atomic_set(&bo->reserved, 1);
1192 init_waitqueue_head(&bo->event_queue);
1193 INIT_LIST_HEAD(&bo->lru);
1194 INIT_LIST_HEAD(&bo->ddestroy);
1195 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001196 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001197 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001198 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199 bo->type = type;
1200 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001201 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001202 bo->mem.mem_type = TTM_PL_SYSTEM;
1203 bo->mem.num_pages = bo->num_pages;
1204 bo->mem.mm_node = NULL;
1205 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001206 bo->mem.bus.io_reserved_vm = false;
1207 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001208 bo->buffer_start = buffer_start & PAGE_MASK;
1209 bo->priv_flags = 0;
1210 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1211 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001212 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001213 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001214 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215
Jerome Glisse09855ac2009-12-10 17:16:27 +01001216 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001217 if (unlikely(ret != 0))
1218 goto out_err;
1219
1220 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221 * For ttm_bo_type_device buffers, allocate
1222 * address space from the device.
1223 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001224 if (bo->type == ttm_bo_type_device) {
1225 ret = ttm_bo_setup_vm(bo);
1226 if (ret)
1227 goto out_err;
1228 }
1229
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001230 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231 if (ret)
1232 goto out_err;
1233
1234 ttm_bo_unreserve(bo);
1235 return 0;
1236
1237out_err:
1238 ttm_bo_unreserve(bo);
1239 ttm_bo_unref(&bo);
1240
1241 return ret;
1242}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001243EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001244
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001245size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1246 unsigned long bo_size,
1247 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001249 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1250 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001252 size += ttm_round_pot(struct_size);
1253 size += PAGE_ALIGN(npages * sizeof(void *));
1254 size += ttm_round_pot(sizeof(struct ttm_tt));
1255 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001257EXPORT_SYMBOL(ttm_bo_acc_size);
1258
1259size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1260 unsigned long bo_size,
1261 unsigned struct_size)
1262{
1263 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1264 size_t size = 0;
1265
1266 size += ttm_round_pot(struct_size);
1267 size += PAGE_ALIGN(npages * sizeof(void *));
1268 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1269 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1270 return size;
1271}
1272EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001273
Jerome Glisse09855ac2009-12-10 17:16:27 +01001274int ttm_bo_create(struct ttm_bo_device *bdev,
1275 unsigned long size,
1276 enum ttm_bo_type type,
1277 struct ttm_placement *placement,
1278 uint32_t page_alignment,
1279 unsigned long buffer_start,
1280 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001281 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001282 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001283{
1284 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001285 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001286 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001287 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001288
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001289 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001290 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291 if (unlikely(ret != 0))
1292 return ret;
1293
1294 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1295
1296 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001297 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298 return -ENOMEM;
1299 }
1300
Jerome Glisse09855ac2009-12-10 17:16:27 +01001301 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1302 buffer_start, interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001303 persistent_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001304 if (likely(ret == 0))
1305 *p_bo = bo;
1306
1307 return ret;
1308}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001309EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001310
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001311static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001312 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001313{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001314 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001315 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001316 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001317
1318 /*
1319 * Can't use standard list traversal since we're unlocking.
1320 */
1321
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001322 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001323 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001324 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001325 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001326 if (ret) {
1327 if (allow_errors) {
1328 return ret;
1329 } else {
1330 printk(KERN_ERR TTM_PFX
1331 "Cleanup eviction failed\n");
1332 }
1333 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001334 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001335 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001336 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001337 return 0;
1338}
1339
1340int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1341{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001342 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001343 int ret = -EINVAL;
1344
1345 if (mem_type >= TTM_NUM_MEM_TYPES) {
1346 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1347 return ret;
1348 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001349 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001350
1351 if (!man->has_type) {
1352 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1353 "memory manager type %u\n", mem_type);
1354 return ret;
1355 }
1356
1357 man->use_type = false;
1358 man->has_type = false;
1359
1360 ret = 0;
1361 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001362 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001363
Ben Skeggsd961db72010-08-05 10:48:18 +10001364 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001365 }
1366
1367 return ret;
1368}
1369EXPORT_SYMBOL(ttm_bo_clean_mm);
1370
1371int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1372{
1373 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1374
1375 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1376 printk(KERN_ERR TTM_PFX
1377 "Illegal memory manager memory type %u.\n",
1378 mem_type);
1379 return -EINVAL;
1380 }
1381
1382 if (!man->has_type) {
1383 printk(KERN_ERR TTM_PFX
1384 "Memory type %u has not been initialized.\n",
1385 mem_type);
1386 return 0;
1387 }
1388
Jerome Glisseca262a9992009-12-08 15:33:32 +01001389 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001390}
1391EXPORT_SYMBOL(ttm_bo_evict_mm);
1392
1393int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001394 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001395{
1396 int ret = -EINVAL;
1397 struct ttm_mem_type_manager *man;
1398
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001399 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001400 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001401 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001402 man->io_reserve_fastpath = true;
1403 man->use_io_reserve_lru = false;
1404 mutex_init(&man->io_reserve_mutex);
1405 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001406
1407 ret = bdev->driver->init_mem_type(bdev, type, man);
1408 if (ret)
1409 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001410 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001411
1412 ret = 0;
1413 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001414 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001415 if (ret)
1416 return ret;
1417 }
1418 man->has_type = true;
1419 man->use_type = true;
1420 man->size = p_size;
1421
1422 INIT_LIST_HEAD(&man->lru);
1423
1424 return 0;
1425}
1426EXPORT_SYMBOL(ttm_bo_init_mm);
1427
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001428static void ttm_bo_global_kobj_release(struct kobject *kobj)
1429{
1430 struct ttm_bo_global *glob =
1431 container_of(kobj, struct ttm_bo_global, kobj);
1432
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001433 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1434 __free_page(glob->dummy_read_page);
1435 kfree(glob);
1436}
1437
Dave Airlieba4420c2010-03-09 10:56:52 +10001438void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001439{
1440 struct ttm_bo_global *glob = ref->object;
1441
1442 kobject_del(&glob->kobj);
1443 kobject_put(&glob->kobj);
1444}
1445EXPORT_SYMBOL(ttm_bo_global_release);
1446
Dave Airlieba4420c2010-03-09 10:56:52 +10001447int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001448{
1449 struct ttm_bo_global_ref *bo_ref =
1450 container_of(ref, struct ttm_bo_global_ref, ref);
1451 struct ttm_bo_global *glob = ref->object;
1452 int ret;
1453
1454 mutex_init(&glob->device_list_mutex);
1455 spin_lock_init(&glob->lru_lock);
1456 glob->mem_glob = bo_ref->mem_glob;
1457 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1458
1459 if (unlikely(glob->dummy_read_page == NULL)) {
1460 ret = -ENOMEM;
1461 goto out_no_drp;
1462 }
1463
1464 INIT_LIST_HEAD(&glob->swap_lru);
1465 INIT_LIST_HEAD(&glob->device_list);
1466
1467 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1468 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1469 if (unlikely(ret != 0)) {
1470 printk(KERN_ERR TTM_PFX
1471 "Could not register buffer object swapout.\n");
1472 goto out_no_shrink;
1473 }
1474
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001475 atomic_set(&glob->bo_count, 0);
1476
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001477 ret = kobject_init_and_add(
1478 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001479 if (unlikely(ret != 0))
1480 kobject_put(&glob->kobj);
1481 return ret;
1482out_no_shrink:
1483 __free_page(glob->dummy_read_page);
1484out_no_drp:
1485 kfree(glob);
1486 return ret;
1487}
1488EXPORT_SYMBOL(ttm_bo_global_init);
1489
1490
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001491int ttm_bo_device_release(struct ttm_bo_device *bdev)
1492{
1493 int ret = 0;
1494 unsigned i = TTM_NUM_MEM_TYPES;
1495 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001496 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497
1498 while (i--) {
1499 man = &bdev->man[i];
1500 if (man->has_type) {
1501 man->use_type = false;
1502 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1503 ret = -EBUSY;
1504 printk(KERN_ERR TTM_PFX
1505 "DRM memory manager type %d "
1506 "is not clean.\n", i);
1507 }
1508 man->has_type = false;
1509 }
1510 }
1511
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001512 mutex_lock(&glob->device_list_mutex);
1513 list_del(&bdev->device_list);
1514 mutex_unlock(&glob->device_list_mutex);
1515
Tejun Heof094cfc2010-12-24 15:59:06 +01001516 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517
1518 while (ttm_bo_delayed_delete(bdev, true))
1519 ;
1520
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001521 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522 if (list_empty(&bdev->ddestroy))
1523 TTM_DEBUG("Delayed destroy list was clean\n");
1524
1525 if (list_empty(&bdev->man[0].lru))
1526 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001527 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001528
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1530 write_lock(&bdev->vm_lock);
1531 drm_mm_takedown(&bdev->addr_space_mm);
1532 write_unlock(&bdev->vm_lock);
1533
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534 return ret;
1535}
1536EXPORT_SYMBOL(ttm_bo_device_release);
1537
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001539 struct ttm_bo_global *glob,
1540 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001541 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001542 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001543{
1544 int ret = -EINVAL;
1545
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001546 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001547 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001548
1549 memset(bdev->man, 0, sizeof(bdev->man));
1550
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001551 /*
1552 * Initialize the system memory buffer type.
1553 * Other types need to be driver / IOCTL initialized.
1554 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001555 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001556 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001557 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001558
1559 bdev->addr_space_rb = RB_ROOT;
1560 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1561 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001562 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563
1564 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1565 bdev->nice_mode = true;
1566 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001567 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001568 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001569 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001570 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001571 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001572 mutex_lock(&glob->device_list_mutex);
1573 list_add_tail(&bdev->device_list, &glob->device_list);
1574 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575
1576 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001577out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001578 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001579out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001580 return ret;
1581}
1582EXPORT_SYMBOL(ttm_bo_device_init);
1583
1584/*
1585 * buffer object vm functions.
1586 */
1587
1588bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1589{
1590 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1591
1592 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1593 if (mem->mem_type == TTM_PL_SYSTEM)
1594 return false;
1595
1596 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1597 return false;
1598
1599 if (mem->placement & TTM_PL_FLAG_CACHED)
1600 return false;
1601 }
1602 return true;
1603}
1604
Thomas Hellstromeba67092010-11-11 09:41:57 +01001605void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001606{
1607 struct ttm_bo_device *bdev = bo->bdev;
1608 loff_t offset = (loff_t) bo->addr_space_offset;
1609 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1610
1611 if (!bdev->dev_mapping)
1612 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001613 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001614 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001615}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001616
1617void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1618{
1619 struct ttm_bo_device *bdev = bo->bdev;
1620 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1621
1622 ttm_mem_io_lock(man, false);
1623 ttm_bo_unmap_virtual_locked(bo);
1624 ttm_mem_io_unlock(man);
1625}
1626
1627
Dave Airliee024e112009-06-24 09:48:08 +10001628EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001629
1630static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1631{
1632 struct ttm_bo_device *bdev = bo->bdev;
1633 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1634 struct rb_node *parent = NULL;
1635 struct ttm_buffer_object *cur_bo;
1636 unsigned long offset = bo->vm_node->start;
1637 unsigned long cur_offset;
1638
1639 while (*cur) {
1640 parent = *cur;
1641 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1642 cur_offset = cur_bo->vm_node->start;
1643 if (offset < cur_offset)
1644 cur = &parent->rb_left;
1645 else if (offset > cur_offset)
1646 cur = &parent->rb_right;
1647 else
1648 BUG();
1649 }
1650
1651 rb_link_node(&bo->vm_rb, parent, cur);
1652 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1653}
1654
1655/**
1656 * ttm_bo_setup_vm:
1657 *
1658 * @bo: the buffer to allocate address space for
1659 *
1660 * Allocate address space in the drm device so that applications
1661 * can mmap the buffer and access the contents. This only
1662 * applies to ttm_bo_type_device objects as others are not
1663 * placed in the drm device address space.
1664 */
1665
1666static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1667{
1668 struct ttm_bo_device *bdev = bo->bdev;
1669 int ret;
1670
1671retry_pre_get:
1672 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1673 if (unlikely(ret != 0))
1674 return ret;
1675
1676 write_lock(&bdev->vm_lock);
1677 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1678 bo->mem.num_pages, 0, 0);
1679
1680 if (unlikely(bo->vm_node == NULL)) {
1681 ret = -ENOMEM;
1682 goto out_unlock;
1683 }
1684
1685 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1686 bo->mem.num_pages, 0);
1687
1688 if (unlikely(bo->vm_node == NULL)) {
1689 write_unlock(&bdev->vm_lock);
1690 goto retry_pre_get;
1691 }
1692
1693 ttm_bo_vm_insert_rb(bo);
1694 write_unlock(&bdev->vm_lock);
1695 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1696
1697 return 0;
1698out_unlock:
1699 write_unlock(&bdev->vm_lock);
1700 return ret;
1701}
1702
1703int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001704 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001705{
1706 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001707 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001708 void *sync_obj;
1709 void *sync_obj_arg;
1710 int ret = 0;
1711
Dave Airlie1717c0e2011-10-27 18:28:37 +02001712 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001713 return 0;
1714
Dave Airlie1717c0e2011-10-27 18:28:37 +02001715 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001716
Dave Airlie1717c0e2011-10-27 18:28:37 +02001717 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1718 void *tmp_obj = bo->sync_obj;
1719 bo->sync_obj = NULL;
1720 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1721 spin_unlock(&bdev->fence_lock);
1722 driver->sync_obj_unref(&tmp_obj);
1723 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001724 continue;
1725 }
1726
1727 if (no_wait)
1728 return -EBUSY;
1729
Dave Airlie1717c0e2011-10-27 18:28:37 +02001730 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001731 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001732 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001733 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1734 lazy, interruptible);
1735 if (unlikely(ret != 0)) {
1736 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001737 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001738 return ret;
1739 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001740 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001741 if (likely(bo->sync_obj == sync_obj &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001742 bo->sync_obj_arg == sync_obj_arg)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001743 void *tmp_obj = bo->sync_obj;
1744 bo->sync_obj = NULL;
1745 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1746 &bo->priv_flags);
1747 spin_unlock(&bdev->fence_lock);
1748 driver->sync_obj_unref(&sync_obj);
1749 driver->sync_obj_unref(&tmp_obj);
1750 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001751 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001752 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001753 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001754 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001755 }
1756 }
1757 return 0;
1758}
1759EXPORT_SYMBOL(ttm_bo_wait);
1760
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001761int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1762{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001763 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001764 int ret = 0;
1765
1766 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001767 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001768 */
1769
1770 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1771 if (unlikely(ret != 0))
1772 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001773 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001774 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001775 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001776 if (likely(ret == 0))
1777 atomic_inc(&bo->cpu_writers);
1778 ttm_bo_unreserve(bo);
1779 return ret;
1780}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001781EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001782
1783void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1784{
1785 if (atomic_dec_and_test(&bo->cpu_writers))
1786 wake_up_all(&bo->event_queue);
1787}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001788EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001789
1790/**
1791 * A buffer object shrink method that tries to swap out the first
1792 * buffer object on the bo_global::swap_lru list.
1793 */
1794
1795static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1796{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001797 struct ttm_bo_global *glob =
1798 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001799 struct ttm_buffer_object *bo;
1800 int ret = -EBUSY;
1801 int put_count;
1802 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1803
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001804 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001805 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001806 if (unlikely(list_empty(&glob->swap_lru))) {
1807 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001808 return -EBUSY;
1809 }
1810
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001811 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001812 struct ttm_buffer_object, swap);
1813 kref_get(&bo->list_kref);
1814
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001815 if (!list_empty(&bo->ddestroy)) {
1816 spin_unlock(&glob->lru_lock);
1817 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1818 kref_put(&bo->list_kref, ttm_bo_release_list);
1819 continue;
1820 }
1821
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001822 /**
1823 * Reserve buffer. Since we unlock while sleeping, we need
1824 * to re-check that nobody removed us from the swap-list while
1825 * we slept.
1826 */
1827
1828 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1829 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001830 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001831 ttm_bo_wait_unreserved(bo, false);
1832 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001833 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001834 }
1835 }
1836
1837 BUG_ON(ret != 0);
1838 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001839 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001840
Dave Airlied6ea8882010-11-22 13:24:40 +10001841 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001842
1843 /**
1844 * Wait for GPU, then move to system cached.
1845 */
1846
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001847 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001848 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001849 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001850
1851 if (unlikely(ret != 0))
1852 goto out;
1853
1854 if ((bo->mem.placement & swap_placement) != swap_placement) {
1855 struct ttm_mem_reg evict_mem;
1856
1857 evict_mem = bo->mem;
1858 evict_mem.mm_node = NULL;
1859 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1860 evict_mem.mem_type = TTM_PL_SYSTEM;
1861
1862 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001863 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001864 if (unlikely(ret != 0))
1865 goto out;
1866 }
1867
1868 ttm_bo_unmap_virtual(bo);
1869
1870 /**
1871 * Swap out. Buffer will be swapped in again as soon as
1872 * anyone tries to access a ttm page.
1873 */
1874
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001875 if (bo->bdev->driver->swap_notify)
1876 bo->bdev->driver->swap_notify(bo);
1877
Jan Engelhardt5df23972011-04-04 01:25:18 +02001878 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001879out:
1880
1881 /**
1882 *
1883 * Unreserve without putting on LRU to avoid swapping out an
1884 * already swapped buffer.
1885 */
1886
1887 atomic_set(&bo->reserved, 0);
1888 wake_up_all(&bo->event_queue);
1889 kref_put(&bo->list_kref, ttm_bo_release_list);
1890 return ret;
1891}
1892
1893void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1894{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001895 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001896 ;
1897}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001898EXPORT_SYMBOL(ttm_bo_swapout_all);