blob: 7c3a57de8187c603628758c8650448f889de550f [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 Sharma60063492011-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
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000407 if (bdev->driver->move_notify)
408 bdev->driver->move_notify(bo, mem);
409
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200410 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
411 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000412 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200413 else if (bdev->driver->move)
414 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000415 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200416 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000417 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200418
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000419 if (ret) {
420 if (bdev->driver->move_notify) {
421 struct ttm_mem_reg tmp_mem = *mem;
422 *mem = bo->mem;
423 bo->mem = tmp_mem;
424 bdev->driver->move_notify(bo, mem);
425 bo->mem = *mem;
426 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200427
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000428 goto out_err;
429 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500430
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200431moved:
432 if (bo->evicted) {
433 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
434 if (ret)
435 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
436 bo->evicted = false;
437 }
438
439 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000440 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200441 bdev->man[bo->mem.mem_type].gpu_offset;
442 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100443 } else
444 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200445
446 return 0;
447
448out_err:
449 new_man = &bdev->man[bo->mem.mem_type];
450 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
451 ttm_tt_unbind(bo->ttm);
452 ttm_tt_destroy(bo->ttm);
453 bo->ttm = NULL;
454 }
455
456 return ret;
457}
458
459/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200460 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200461 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200462 * This is the place to put in driver specific hooks to release
463 * driver private resources.
464 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200465 */
466
467static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
468{
Jerome Glissedc97b342011-11-18 11:47:03 -0500469 if (bo->bdev->driver->move_notify)
470 bo->bdev->driver->move_notify(bo, NULL);
471
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200472 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200473 ttm_tt_unbind(bo->ttm);
474 ttm_tt_destroy(bo->ttm);
475 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200476 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200477 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200478
479 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200480
481 /*
482 * Make processes trying to reserve really pick it up.
483 */
484 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200485 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200486}
487
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200488static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200489{
490 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200491 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200492 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000493 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200494 void *sync_obj_arg;
495 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200496 int ret;
497
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000498 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200499 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200500 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200501
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200502 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100503
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200504 /**
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000505 * Lock inversion between bo:reserve and bdev::fence_lock here,
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200506 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200507 */
508
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200509 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200510
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200511 if (unlikely(ret == -EBUSY))
512 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200513
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000514 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200515 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200516
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200517 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200518 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200519
Dave Airlied6ea8882010-11-22 13:24:40 +1000520 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200521
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200522 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200523 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200524 spin_lock(&glob->lru_lock);
525 }
526queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200527 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000528 if (bo->sync_obj)
529 sync_obj = driver->sync_obj_ref(bo->sync_obj);
530 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200531
532 kref_get(&bo->list_kref);
533 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
534 spin_unlock(&glob->lru_lock);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000535 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200536
Thomas Hellstromaa123262010-11-02 13:21:47 +0000537 if (sync_obj) {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200538 driver->sync_obj_flush(sync_obj, sync_obj_arg);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000539 driver->sync_obj_unref(&sync_obj);
540 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200541 schedule_delayed_work(&bdev->wq,
542 ((HZ / 100) < 1) ? 1 : HZ / 100);
543}
544
545/**
546 * function ttm_bo_cleanup_refs
547 * If bo idle, remove from delayed- and lru lists, and unref.
548 * If not idle, do nothing.
549 *
550 * @interruptible Any sleeps should occur interruptibly.
551 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
552 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
553 */
554
555static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
556 bool interruptible,
557 bool no_wait_reserve,
558 bool no_wait_gpu)
559{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000560 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200561 struct ttm_bo_global *glob = bo->glob;
562 int put_count;
563 int ret = 0;
564
565retry:
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000566 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200567 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000568 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200569
570 if (unlikely(ret != 0))
571 return ret;
572
573 spin_lock(&glob->lru_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100574
575 if (unlikely(list_empty(&bo->ddestroy))) {
576 spin_unlock(&glob->lru_lock);
577 return 0;
578 }
579
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200580 ret = ttm_bo_reserve_locked(bo, interruptible,
581 no_wait_reserve, false, 0);
582
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100583 if (unlikely(ret != 0)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200584 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200585 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200586 }
587
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200588 /**
589 * We can re-check for sync object without taking
590 * the bo::lock since setting the sync object requires
591 * also bo::reserved. A busy object at this point may
592 * be caused by another thread recently starting an accelerated
593 * eviction.
594 */
595
596 if (unlikely(bo->sync_obj)) {
597 atomic_set(&bo->reserved, 0);
598 wake_up_all(&bo->event_queue);
599 spin_unlock(&glob->lru_lock);
600 goto retry;
601 }
602
603 put_count = ttm_bo_del_from_lru(bo);
604 list_del_init(&bo->ddestroy);
605 ++put_count;
606
607 spin_unlock(&glob->lru_lock);
608 ttm_bo_cleanup_memtype_use(bo);
609
Dave Airlied6ea8882010-11-22 13:24:40 +1000610 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200611
612 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200613}
614
615/**
616 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
617 * encountered buffers.
618 */
619
620static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
621{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200622 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100623 struct ttm_buffer_object *entry = NULL;
624 int ret = 0;
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(&bdev->ddestroy))
628 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200629
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100630 entry = list_first_entry(&bdev->ddestroy,
631 struct ttm_buffer_object, ddestroy);
632 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200633
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100634 for (;;) {
635 struct ttm_buffer_object *nentry = NULL;
636
637 if (entry->ddestroy.next != &bdev->ddestroy) {
638 nentry = list_first_entry(&entry->ddestroy,
639 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200640 kref_get(&nentry->list_kref);
641 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200642
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200643 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200644 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
645 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200646 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100647 entry = nentry;
648
649 if (ret || !entry)
650 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200651
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200652 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100653 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200654 break;
655 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100657out_unlock:
658 spin_unlock(&glob->lru_lock);
659out:
660 if (entry)
661 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200662 return ret;
663}
664
665static void ttm_bo_delayed_workqueue(struct work_struct *work)
666{
667 struct ttm_bo_device *bdev =
668 container_of(work, struct ttm_bo_device, wq.work);
669
670 if (ttm_bo_delayed_delete(bdev, false)) {
671 schedule_delayed_work(&bdev->wq,
672 ((HZ / 100) < 1) ? 1 : HZ / 100);
673 }
674}
675
676static void ttm_bo_release(struct kref *kref)
677{
678 struct ttm_buffer_object *bo =
679 container_of(kref, struct ttm_buffer_object, kref);
680 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100681 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200682
683 if (likely(bo->vm_node != NULL)) {
684 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
685 drm_mm_put_block(bo->vm_node);
686 bo->vm_node = NULL;
687 }
688 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100689 ttm_mem_io_lock(man, false);
690 ttm_mem_io_free_vm(bo);
691 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200692 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200693 kref_put(&bo->list_kref, ttm_bo_release_list);
694 write_lock(&bdev->vm_lock);
695}
696
697void ttm_bo_unref(struct ttm_buffer_object **p_bo)
698{
699 struct ttm_buffer_object *bo = *p_bo;
700 struct ttm_bo_device *bdev = bo->bdev;
701
702 *p_bo = NULL;
703 write_lock(&bdev->vm_lock);
704 kref_put(&bo->kref, ttm_bo_release);
705 write_unlock(&bdev->vm_lock);
706}
707EXPORT_SYMBOL(ttm_bo_unref);
708
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400709int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
710{
711 return cancel_delayed_work_sync(&bdev->wq);
712}
713EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
714
715void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
716{
717 if (resched)
718 schedule_delayed_work(&bdev->wq,
719 ((HZ / 100) < 1) ? 1 : HZ / 100);
720}
721EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
722
Jerome Glisseca262a9992009-12-08 15:33:32 +0100723static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000724 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200725{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200726 struct ttm_bo_device *bdev = bo->bdev;
727 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100728 struct ttm_placement placement;
729 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200730
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000731 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200732 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000733 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200734
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200735 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100736 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200737 printk(KERN_ERR TTM_PFX
738 "Failed to expire sync object before "
739 "buffer eviction.\n");
740 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200741 goto out;
742 }
743
744 BUG_ON(!atomic_read(&bo->reserved));
745
746 evict_mem = bo->mem;
747 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100748 evict_mem.bus.io_reserved_vm = false;
749 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200750
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100751 placement.fpfn = 0;
752 placement.lpfn = 0;
753 placement.num_placement = 0;
754 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100755 bdev->driver->evict_flags(bo, &placement);
756 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000757 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200758 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100759 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200760 printk(KERN_ERR TTM_PFX
761 "Failed to find memory space for "
762 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100763 ttm_bo_mem_space_debug(bo, &placement);
764 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200765 goto out;
766 }
767
768 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000769 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200770 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100771 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200772 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000773 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200774 goto out;
775 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200776 bo->evicted = true;
777out:
778 return ret;
779}
780
Jerome Glisseca262a9992009-12-08 15:33:32 +0100781static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
782 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000783 bool interruptible, bool no_wait_reserve,
784 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100785{
786 struct ttm_bo_global *glob = bdev->glob;
787 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
788 struct ttm_buffer_object *bo;
789 int ret, put_count = 0;
790
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100791retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100792 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100793 if (list_empty(&man->lru)) {
794 spin_unlock(&glob->lru_lock);
795 return -EBUSY;
796 }
797
Jerome Glisseca262a9992009-12-08 15:33:32 +0100798 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
799 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100800
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200801 if (!list_empty(&bo->ddestroy)) {
802 spin_unlock(&glob->lru_lock);
803 ret = ttm_bo_cleanup_refs(bo, interruptible,
804 no_wait_reserve, no_wait_gpu);
805 kref_put(&bo->list_kref, ttm_bo_release_list);
806
807 if (likely(ret == 0 || ret == -ERESTARTSYS))
808 return ret;
809
810 goto retry;
811 }
812
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000813 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100814
815 if (unlikely(ret == -EBUSY)) {
816 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000817 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100818 ret = ttm_bo_wait_unreserved(bo, interruptible);
819
820 kref_put(&bo->list_kref, ttm_bo_release_list);
821
822 /**
823 * We *need* to retry after releasing the lru lock.
824 */
825
826 if (unlikely(ret != 0))
827 return ret;
828 goto retry;
829 }
830
831 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100832 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100833
834 BUG_ON(ret != 0);
835
Dave Airlied6ea8882010-11-22 13:24:40 +1000836 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100837
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000838 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100839 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100840
Jerome Glisseca262a9992009-12-08 15:33:32 +0100841 kref_put(&bo->list_kref, ttm_bo_release_list);
842 return ret;
843}
844
Ben Skeggs42311ff2010-08-04 12:07:08 +1000845void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
846{
Ben Skeggsd961db72010-08-05 10:48:18 +1000847 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000848
Ben Skeggsd961db72010-08-05 10:48:18 +1000849 if (mem->mm_node)
850 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000851}
852EXPORT_SYMBOL(ttm_bo_mem_put);
853
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200854/**
855 * Repeatedly evict memory from the LRU for @mem_type until we create enough
856 * space, or we've evicted everything and there isn't enough space.
857 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100858static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
859 uint32_t mem_type,
860 struct ttm_placement *placement,
861 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000862 bool interruptible,
863 bool no_wait_reserve,
864 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100866 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200868 int ret;
869
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200870 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000871 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200872 if (unlikely(ret != 0))
873 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000874 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100875 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100876 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000877 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100878 if (unlikely(ret != 0))
879 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000881 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200883 mem->mem_type = mem_type;
884 return 0;
885}
886
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200887static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
888 uint32_t cur_placement,
889 uint32_t proposed_placement)
890{
891 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
892 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
893
894 /**
895 * Keep current caching if possible.
896 */
897
898 if ((cur_placement & caching) != 0)
899 result |= (cur_placement & caching);
900 else if ((man->default_caching & caching) != 0)
901 result |= man->default_caching;
902 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
903 result |= TTM_PL_FLAG_CACHED;
904 else if ((TTM_PL_FLAG_WC & caching) != 0)
905 result |= TTM_PL_FLAG_WC;
906 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
907 result |= TTM_PL_FLAG_UNCACHED;
908
909 return result;
910}
911
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200912static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200913 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200914 uint32_t proposed_placement,
915 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200916{
917 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
918
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200919 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200920 return false;
921
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200922 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200924
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200925 cur_flags |= (proposed_placement & man->available_caching);
926
927 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200928 return true;
929}
930
931/**
932 * Creates space for memory region @mem according to its type.
933 *
934 * This function first searches for free space in compatible memory types in
935 * the priority order defined by the driver. If free space isn't found, then
936 * ttm_bo_mem_force_space is attempted in priority order to evict and find
937 * space.
938 */
939int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100940 struct ttm_placement *placement,
941 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000942 bool interruptible, bool no_wait_reserve,
943 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200944{
945 struct ttm_bo_device *bdev = bo->bdev;
946 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200947 uint32_t mem_type = TTM_PL_SYSTEM;
948 uint32_t cur_flags = 0;
949 bool type_found = false;
950 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100951 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100952 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953
954 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000955 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100956 ret = ttm_mem_type_from_flags(placement->placement[i],
957 &mem_type);
958 if (ret)
959 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200960 man = &bdev->man[mem_type];
961
962 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100963 mem_type,
964 placement->placement[i],
965 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200966
967 if (!type_ok)
968 continue;
969
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200970 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
971 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100972 /*
973 * Use the access and other non-mapping-related flag bits from
974 * the memory placement flags to the current flags
975 */
976 ttm_flag_masked(&cur_flags, placement->placement[i],
977 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200978
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200979 if (mem_type == TTM_PL_SYSTEM)
980 break;
981
982 if (man->has_type && man->use_type) {
983 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000984 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100985 if (unlikely(ret))
986 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200987 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000988 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989 break;
990 }
991
Ben Skeggsd961db72010-08-05 10:48:18 +1000992 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200993 mem->mem_type = mem_type;
994 mem->placement = cur_flags;
995 return 0;
996 }
997
998 if (!type_found)
999 return -EINVAL;
1000
Dave Airlieb6637522009-12-14 14:51:35 +10001001 for (i = 0; i < placement->num_busy_placement; ++i) {
1002 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001003 &mem_type);
1004 if (ret)
1005 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001007 if (!man->has_type)
1008 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001009 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001010 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001011 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001012 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 continue;
1014
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001015 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1016 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001017 /*
1018 * Use the access and other non-mapping-related flag bits from
1019 * the memory placement flags to the current flags
1020 */
Dave Airlieb6637522009-12-14 14:51:35 +10001021 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001022 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001023
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001024
1025 if (mem_type == TTM_PL_SYSTEM) {
1026 mem->mem_type = mem_type;
1027 mem->placement = cur_flags;
1028 mem->mm_node = NULL;
1029 return 0;
1030 }
1031
Jerome Glisseca262a9992009-12-08 15:33:32 +01001032 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001033 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001034 if (ret == 0 && mem->mm_node) {
1035 mem->placement = cur_flags;
1036 return 0;
1037 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001038 if (ret == -ERESTARTSYS)
1039 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001040 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001041 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001042 return ret;
1043}
1044EXPORT_SYMBOL(ttm_bo_mem_space);
1045
1046int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1047{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001048 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1049 return -EBUSY;
1050
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001051 return wait_event_interruptible(bo->event_queue,
1052 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001053}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001054EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001055
1056int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001057 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001058 bool interruptible, bool no_wait_reserve,
1059 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001061 int ret = 0;
1062 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001063 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001064
1065 BUG_ON(!atomic_read(&bo->reserved));
1066
1067 /*
1068 * FIXME: It's possible to pipeline buffer moves.
1069 * Have the driver move function wait for idle when necessary,
1070 * instead of doing it here.
1071 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001072 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001073 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001074 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001075 if (ret)
1076 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077 mem.num_pages = bo->num_pages;
1078 mem.size = mem.num_pages << PAGE_SHIFT;
1079 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001080 mem.bus.io_reserved_vm = false;
1081 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082 /*
1083 * Determine where to move the buffer.
1084 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001085 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086 if (ret)
1087 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001088 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001090 if (ret && mem.mm_node)
1091 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092 return ret;
1093}
1094
Jerome Glisseca262a9992009-12-08 15:33:32 +01001095static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001096 struct ttm_mem_reg *mem)
1097{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001098 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001099
Ben Skeggsd961db72010-08-05 10:48:18 +10001100 if (mem->mm_node && placement->lpfn != 0 &&
1101 (mem->start < placement->fpfn ||
1102 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001103 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001104
Jerome Glisseca262a9992009-12-08 15:33:32 +01001105 for (i = 0; i < placement->num_placement; i++) {
1106 if ((placement->placement[i] & mem->placement &
1107 TTM_PL_MASK_CACHING) &&
1108 (placement->placement[i] & mem->placement &
1109 TTM_PL_MASK_MEM))
1110 return i;
1111 }
1112 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113}
1114
Jerome Glisse09855ac2009-12-10 17:16:27 +01001115int ttm_bo_validate(struct ttm_buffer_object *bo,
1116 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001117 bool interruptible, bool no_wait_reserve,
1118 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001119{
1120 int ret;
1121
1122 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001123 /* Check that range is valid */
1124 if (placement->lpfn || placement->fpfn)
1125 if (placement->fpfn > placement->lpfn ||
1126 (placement->lpfn - placement->fpfn) < bo->num_pages)
1127 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001128 /*
1129 * Check whether we need to move buffer.
1130 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001131 ret = ttm_bo_mem_compat(placement, &bo->mem);
1132 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001133 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001134 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001136 } else {
1137 /*
1138 * Use the access and other non-mapping-related flag bits from
1139 * the compatible memory placement flags to the active flags
1140 */
1141 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1142 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 /*
1145 * We might need to add a TTM.
1146 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001147 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1148 ret = ttm_bo_add_ttm(bo, true);
1149 if (ret)
1150 return ret;
1151 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152 return 0;
1153}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001154EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001155
Jerome Glisse09855ac2009-12-10 17:16:27 +01001156int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1157 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001159 BUG_ON((placement->fpfn || placement->lpfn) &&
1160 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001162 return 0;
1163}
1164
Jerome Glisse09855ac2009-12-10 17:16:27 +01001165int ttm_bo_init(struct ttm_bo_device *bdev,
1166 struct ttm_buffer_object *bo,
1167 unsigned long size,
1168 enum ttm_bo_type type,
1169 struct ttm_placement *placement,
1170 uint32_t page_alignment,
1171 unsigned long buffer_start,
1172 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001173 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001174 size_t acc_size,
1175 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001176{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001177 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001179 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1180
1181 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1182 if (ret) {
1183 printk(KERN_ERR TTM_PFX "Out of kernel memory.\n");
1184 if (destroy)
1185 (*destroy)(bo);
1186 else
1187 kfree(bo);
1188 return -ENOMEM;
1189 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001190
1191 size += buffer_start & ~PAGE_MASK;
1192 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1193 if (num_pages == 0) {
1194 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001195 if (destroy)
1196 (*destroy)(bo);
1197 else
1198 kfree(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199 return -EINVAL;
1200 }
1201 bo->destroy = destroy;
1202
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203 kref_init(&bo->kref);
1204 kref_init(&bo->list_kref);
1205 atomic_set(&bo->cpu_writers, 0);
1206 atomic_set(&bo->reserved, 1);
1207 init_waitqueue_head(&bo->event_queue);
1208 INIT_LIST_HEAD(&bo->lru);
1209 INIT_LIST_HEAD(&bo->ddestroy);
1210 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001211 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001213 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001214 bo->type = type;
1215 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001216 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001217 bo->mem.mem_type = TTM_PL_SYSTEM;
1218 bo->mem.num_pages = bo->num_pages;
1219 bo->mem.mm_node = NULL;
1220 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001221 bo->mem.bus.io_reserved_vm = false;
1222 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223 bo->buffer_start = buffer_start & PAGE_MASK;
1224 bo->priv_flags = 0;
1225 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1226 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001227 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001229 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001230
Jerome Glisse09855ac2009-12-10 17:16:27 +01001231 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001232 if (unlikely(ret != 0))
1233 goto out_err;
1234
1235 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001236 * For ttm_bo_type_device buffers, allocate
1237 * address space from the device.
1238 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001239 if (bo->type == ttm_bo_type_device) {
1240 ret = ttm_bo_setup_vm(bo);
1241 if (ret)
1242 goto out_err;
1243 }
1244
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001245 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001246 if (ret)
1247 goto out_err;
1248
1249 ttm_bo_unreserve(bo);
1250 return 0;
1251
1252out_err:
1253 ttm_bo_unreserve(bo);
1254 ttm_bo_unref(&bo);
1255
1256 return ret;
1257}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001258EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001259
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001260size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1261 unsigned long bo_size,
1262 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001263{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001264 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1265 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001266
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001267 size += ttm_round_pot(struct_size);
1268 size += PAGE_ALIGN(npages * sizeof(void *));
1269 size += ttm_round_pot(sizeof(struct ttm_tt));
1270 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001272EXPORT_SYMBOL(ttm_bo_acc_size);
1273
1274size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1275 unsigned long bo_size,
1276 unsigned struct_size)
1277{
1278 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1279 size_t size = 0;
1280
1281 size += ttm_round_pot(struct_size);
1282 size += PAGE_ALIGN(npages * sizeof(void *));
1283 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1284 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1285 return size;
1286}
1287EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001288
Jerome Glisse09855ac2009-12-10 17:16:27 +01001289int ttm_bo_create(struct ttm_bo_device *bdev,
1290 unsigned long size,
1291 enum ttm_bo_type type,
1292 struct ttm_placement *placement,
1293 uint32_t page_alignment,
1294 unsigned long buffer_start,
1295 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001296 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001297 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298{
1299 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001300 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001301 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001302 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001303
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001304 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001305 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306 if (unlikely(ret != 0))
1307 return ret;
1308
1309 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1310
1311 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001312 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001313 return -ENOMEM;
1314 }
1315
Jerome Glisse09855ac2009-12-10 17:16:27 +01001316 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1317 buffer_start, interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001318 persistent_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001319 if (likely(ret == 0))
1320 *p_bo = bo;
1321
1322 return ret;
1323}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001324EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001325
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001326static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001327 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001328{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001329 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001330 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001331 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001332
1333 /*
1334 * Can't use standard list traversal since we're unlocking.
1335 */
1336
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001337 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001338 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001339 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001340 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001341 if (ret) {
1342 if (allow_errors) {
1343 return ret;
1344 } else {
1345 printk(KERN_ERR TTM_PFX
1346 "Cleanup eviction failed\n");
1347 }
1348 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001349 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001350 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001351 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001352 return 0;
1353}
1354
1355int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1356{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001357 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001358 int ret = -EINVAL;
1359
1360 if (mem_type >= TTM_NUM_MEM_TYPES) {
1361 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1362 return ret;
1363 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001364 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001365
1366 if (!man->has_type) {
1367 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1368 "memory manager type %u\n", mem_type);
1369 return ret;
1370 }
1371
1372 man->use_type = false;
1373 man->has_type = false;
1374
1375 ret = 0;
1376 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001377 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001378
Ben Skeggsd961db72010-08-05 10:48:18 +10001379 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001380 }
1381
1382 return ret;
1383}
1384EXPORT_SYMBOL(ttm_bo_clean_mm);
1385
1386int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1387{
1388 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1389
1390 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1391 printk(KERN_ERR TTM_PFX
1392 "Illegal memory manager memory type %u.\n",
1393 mem_type);
1394 return -EINVAL;
1395 }
1396
1397 if (!man->has_type) {
1398 printk(KERN_ERR TTM_PFX
1399 "Memory type %u has not been initialized.\n",
1400 mem_type);
1401 return 0;
1402 }
1403
Jerome Glisseca262a9992009-12-08 15:33:32 +01001404 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001405}
1406EXPORT_SYMBOL(ttm_bo_evict_mm);
1407
1408int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001409 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001410{
1411 int ret = -EINVAL;
1412 struct ttm_mem_type_manager *man;
1413
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001414 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001415 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001416 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001417 man->io_reserve_fastpath = true;
1418 man->use_io_reserve_lru = false;
1419 mutex_init(&man->io_reserve_mutex);
1420 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001421
1422 ret = bdev->driver->init_mem_type(bdev, type, man);
1423 if (ret)
1424 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001425 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001426
1427 ret = 0;
1428 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001429 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001430 if (ret)
1431 return ret;
1432 }
1433 man->has_type = true;
1434 man->use_type = true;
1435 man->size = p_size;
1436
1437 INIT_LIST_HEAD(&man->lru);
1438
1439 return 0;
1440}
1441EXPORT_SYMBOL(ttm_bo_init_mm);
1442
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001443static void ttm_bo_global_kobj_release(struct kobject *kobj)
1444{
1445 struct ttm_bo_global *glob =
1446 container_of(kobj, struct ttm_bo_global, kobj);
1447
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001448 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1449 __free_page(glob->dummy_read_page);
1450 kfree(glob);
1451}
1452
Dave Airlieba4420c2010-03-09 10:56:52 +10001453void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001454{
1455 struct ttm_bo_global *glob = ref->object;
1456
1457 kobject_del(&glob->kobj);
1458 kobject_put(&glob->kobj);
1459}
1460EXPORT_SYMBOL(ttm_bo_global_release);
1461
Dave Airlieba4420c2010-03-09 10:56:52 +10001462int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001463{
1464 struct ttm_bo_global_ref *bo_ref =
1465 container_of(ref, struct ttm_bo_global_ref, ref);
1466 struct ttm_bo_global *glob = ref->object;
1467 int ret;
1468
1469 mutex_init(&glob->device_list_mutex);
1470 spin_lock_init(&glob->lru_lock);
1471 glob->mem_glob = bo_ref->mem_glob;
1472 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1473
1474 if (unlikely(glob->dummy_read_page == NULL)) {
1475 ret = -ENOMEM;
1476 goto out_no_drp;
1477 }
1478
1479 INIT_LIST_HEAD(&glob->swap_lru);
1480 INIT_LIST_HEAD(&glob->device_list);
1481
1482 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1483 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1484 if (unlikely(ret != 0)) {
1485 printk(KERN_ERR TTM_PFX
1486 "Could not register buffer object swapout.\n");
1487 goto out_no_shrink;
1488 }
1489
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001490 atomic_set(&glob->bo_count, 0);
1491
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001492 ret = kobject_init_and_add(
1493 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001494 if (unlikely(ret != 0))
1495 kobject_put(&glob->kobj);
1496 return ret;
1497out_no_shrink:
1498 __free_page(glob->dummy_read_page);
1499out_no_drp:
1500 kfree(glob);
1501 return ret;
1502}
1503EXPORT_SYMBOL(ttm_bo_global_init);
1504
1505
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001506int ttm_bo_device_release(struct ttm_bo_device *bdev)
1507{
1508 int ret = 0;
1509 unsigned i = TTM_NUM_MEM_TYPES;
1510 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001511 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001512
1513 while (i--) {
1514 man = &bdev->man[i];
1515 if (man->has_type) {
1516 man->use_type = false;
1517 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1518 ret = -EBUSY;
1519 printk(KERN_ERR TTM_PFX
1520 "DRM memory manager type %d "
1521 "is not clean.\n", i);
1522 }
1523 man->has_type = false;
1524 }
1525 }
1526
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001527 mutex_lock(&glob->device_list_mutex);
1528 list_del(&bdev->device_list);
1529 mutex_unlock(&glob->device_list_mutex);
1530
Tejun Heof094cfc2010-12-24 15:59:06 +01001531 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001532
1533 while (ttm_bo_delayed_delete(bdev, true))
1534 ;
1535
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001536 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001537 if (list_empty(&bdev->ddestroy))
1538 TTM_DEBUG("Delayed destroy list was clean\n");
1539
1540 if (list_empty(&bdev->man[0].lru))
1541 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001542 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001543
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001544 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1545 write_lock(&bdev->vm_lock);
1546 drm_mm_takedown(&bdev->addr_space_mm);
1547 write_unlock(&bdev->vm_lock);
1548
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001549 return ret;
1550}
1551EXPORT_SYMBOL(ttm_bo_device_release);
1552
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001554 struct ttm_bo_global *glob,
1555 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001556 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001557 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001558{
1559 int ret = -EINVAL;
1560
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001561 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001562 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563
1564 memset(bdev->man, 0, sizeof(bdev->man));
1565
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001566 /*
1567 * Initialize the system memory buffer type.
1568 * Other types need to be driver / IOCTL initialized.
1569 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001570 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001571 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001572 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001573
1574 bdev->addr_space_rb = RB_ROOT;
1575 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1576 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001577 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001578
1579 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1580 bdev->nice_mode = true;
1581 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001582 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001583 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001584 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001585 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001586 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001587 mutex_lock(&glob->device_list_mutex);
1588 list_add_tail(&bdev->device_list, &glob->device_list);
1589 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001590
1591 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001592out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001593 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001594out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001595 return ret;
1596}
1597EXPORT_SYMBOL(ttm_bo_device_init);
1598
1599/*
1600 * buffer object vm functions.
1601 */
1602
1603bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1604{
1605 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1606
1607 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1608 if (mem->mem_type == TTM_PL_SYSTEM)
1609 return false;
1610
1611 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1612 return false;
1613
1614 if (mem->placement & TTM_PL_FLAG_CACHED)
1615 return false;
1616 }
1617 return true;
1618}
1619
Thomas Hellstromeba67092010-11-11 09:41:57 +01001620void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001621{
1622 struct ttm_bo_device *bdev = bo->bdev;
1623 loff_t offset = (loff_t) bo->addr_space_offset;
1624 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1625
1626 if (!bdev->dev_mapping)
1627 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001628 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001629 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001630}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001631
1632void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1633{
1634 struct ttm_bo_device *bdev = bo->bdev;
1635 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1636
1637 ttm_mem_io_lock(man, false);
1638 ttm_bo_unmap_virtual_locked(bo);
1639 ttm_mem_io_unlock(man);
1640}
1641
1642
Dave Airliee024e112009-06-24 09:48:08 +10001643EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001644
1645static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1646{
1647 struct ttm_bo_device *bdev = bo->bdev;
1648 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1649 struct rb_node *parent = NULL;
1650 struct ttm_buffer_object *cur_bo;
1651 unsigned long offset = bo->vm_node->start;
1652 unsigned long cur_offset;
1653
1654 while (*cur) {
1655 parent = *cur;
1656 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1657 cur_offset = cur_bo->vm_node->start;
1658 if (offset < cur_offset)
1659 cur = &parent->rb_left;
1660 else if (offset > cur_offset)
1661 cur = &parent->rb_right;
1662 else
1663 BUG();
1664 }
1665
1666 rb_link_node(&bo->vm_rb, parent, cur);
1667 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1668}
1669
1670/**
1671 * ttm_bo_setup_vm:
1672 *
1673 * @bo: the buffer to allocate address space for
1674 *
1675 * Allocate address space in the drm device so that applications
1676 * can mmap the buffer and access the contents. This only
1677 * applies to ttm_bo_type_device objects as others are not
1678 * placed in the drm device address space.
1679 */
1680
1681static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1682{
1683 struct ttm_bo_device *bdev = bo->bdev;
1684 int ret;
1685
1686retry_pre_get:
1687 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1688 if (unlikely(ret != 0))
1689 return ret;
1690
1691 write_lock(&bdev->vm_lock);
1692 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1693 bo->mem.num_pages, 0, 0);
1694
1695 if (unlikely(bo->vm_node == NULL)) {
1696 ret = -ENOMEM;
1697 goto out_unlock;
1698 }
1699
1700 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1701 bo->mem.num_pages, 0);
1702
1703 if (unlikely(bo->vm_node == NULL)) {
1704 write_unlock(&bdev->vm_lock);
1705 goto retry_pre_get;
1706 }
1707
1708 ttm_bo_vm_insert_rb(bo);
1709 write_unlock(&bdev->vm_lock);
1710 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1711
1712 return 0;
1713out_unlock:
1714 write_unlock(&bdev->vm_lock);
1715 return ret;
1716}
1717
1718int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001719 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001720{
1721 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001722 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001723 void *sync_obj;
1724 void *sync_obj_arg;
1725 int ret = 0;
1726
Dave Airlie1717c0e2011-10-27 18:28:37 +02001727 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001728 return 0;
1729
Dave Airlie1717c0e2011-10-27 18:28:37 +02001730 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001731
Dave Airlie1717c0e2011-10-27 18:28:37 +02001732 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1733 void *tmp_obj = bo->sync_obj;
1734 bo->sync_obj = NULL;
1735 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1736 spin_unlock(&bdev->fence_lock);
1737 driver->sync_obj_unref(&tmp_obj);
1738 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001739 continue;
1740 }
1741
1742 if (no_wait)
1743 return -EBUSY;
1744
Dave Airlie1717c0e2011-10-27 18:28:37 +02001745 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001746 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001747 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001748 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1749 lazy, interruptible);
1750 if (unlikely(ret != 0)) {
1751 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001752 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001753 return ret;
1754 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001755 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001756 if (likely(bo->sync_obj == sync_obj &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001757 bo->sync_obj_arg == sync_obj_arg)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001758 void *tmp_obj = bo->sync_obj;
1759 bo->sync_obj = NULL;
1760 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1761 &bo->priv_flags);
1762 spin_unlock(&bdev->fence_lock);
1763 driver->sync_obj_unref(&sync_obj);
1764 driver->sync_obj_unref(&tmp_obj);
1765 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001766 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001767 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001768 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001769 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001770 }
1771 }
1772 return 0;
1773}
1774EXPORT_SYMBOL(ttm_bo_wait);
1775
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001776int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1777{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001778 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001779 int ret = 0;
1780
1781 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001782 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001783 */
1784
1785 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1786 if (unlikely(ret != 0))
1787 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001788 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001789 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001790 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001791 if (likely(ret == 0))
1792 atomic_inc(&bo->cpu_writers);
1793 ttm_bo_unreserve(bo);
1794 return ret;
1795}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001796EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001797
1798void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1799{
1800 if (atomic_dec_and_test(&bo->cpu_writers))
1801 wake_up_all(&bo->event_queue);
1802}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001803EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001804
1805/**
1806 * A buffer object shrink method that tries to swap out the first
1807 * buffer object on the bo_global::swap_lru list.
1808 */
1809
1810static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1811{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001812 struct ttm_bo_global *glob =
1813 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001814 struct ttm_buffer_object *bo;
1815 int ret = -EBUSY;
1816 int put_count;
1817 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1818
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001819 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001820 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001821 if (unlikely(list_empty(&glob->swap_lru))) {
1822 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001823 return -EBUSY;
1824 }
1825
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001826 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001827 struct ttm_buffer_object, swap);
1828 kref_get(&bo->list_kref);
1829
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001830 if (!list_empty(&bo->ddestroy)) {
1831 spin_unlock(&glob->lru_lock);
1832 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1833 kref_put(&bo->list_kref, ttm_bo_release_list);
1834 continue;
1835 }
1836
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001837 /**
1838 * Reserve buffer. Since we unlock while sleeping, we need
1839 * to re-check that nobody removed us from the swap-list while
1840 * we slept.
1841 */
1842
1843 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1844 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001845 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001846 ttm_bo_wait_unreserved(bo, false);
1847 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001848 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001849 }
1850 }
1851
1852 BUG_ON(ret != 0);
1853 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001854 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001855
Dave Airlied6ea8882010-11-22 13:24:40 +10001856 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001857
1858 /**
1859 * Wait for GPU, then move to system cached.
1860 */
1861
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001862 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001863 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001864 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001865
1866 if (unlikely(ret != 0))
1867 goto out;
1868
1869 if ((bo->mem.placement & swap_placement) != swap_placement) {
1870 struct ttm_mem_reg evict_mem;
1871
1872 evict_mem = bo->mem;
1873 evict_mem.mm_node = NULL;
1874 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1875 evict_mem.mem_type = TTM_PL_SYSTEM;
1876
1877 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001878 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001879 if (unlikely(ret != 0))
1880 goto out;
1881 }
1882
1883 ttm_bo_unmap_virtual(bo);
1884
1885 /**
1886 * Swap out. Buffer will be swapped in again as soon as
1887 * anyone tries to access a ttm page.
1888 */
1889
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001890 if (bo->bdev->driver->swap_notify)
1891 bo->bdev->driver->swap_notify(bo);
1892
Jan Engelhardt5df23972011-04-04 01:25:18 +02001893 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001894out:
1895
1896 /**
1897 *
1898 * Unreserve without putting on LRU to avoid swapping out an
1899 * already swapped buffer.
1900 */
1901
1902 atomic_set(&bo->reserved, 0);
1903 wake_up_all(&bo->event_queue);
1904 kref_put(&bo->list_kref, ttm_bo_release_list);
1905 return ret;
1906}
1907
1908void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1909{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001910 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001911 ;
1912}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001913EXPORT_SYMBOL(ttm_bo_swapout_all);