blob: 2c54c3d414b3cb956e7cf953ab6a610bc5e6e82e [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
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/jiffies.h>
37#include <linux/slab.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/file.h>
41#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020043
44#define TTM_ASSERT_LOCKED(param)
45#define TTM_DEBUG(fmt, arg...)
46#define TTM_BO_HASH_ORDER 13
47
48static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020049static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020050static void ttm_bo_global_kobj_release(struct kobject *kobj);
51
52static struct attribute ttm_bo_count = {
53 .name = "bo_count",
54 .mode = S_IRUGO
55};
56
Jerome Glissefb53f862009-12-09 21:55:10 +010057static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
58{
59 int i;
60
61 for (i = 0; i <= TTM_PL_PRIV5; i++)
62 if (flags & (1 << i)) {
63 *mem_type = i;
64 return 0;
65 }
66 return -EINVAL;
67}
68
Jerome Glisse5012f502009-12-10 18:07:26 +010069static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010070{
Jerome Glisse5012f502009-12-10 18:07:26 +010071 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
72
Joe Perches25d04792012-03-16 21:43:50 -070073 pr_err(" has_type: %d\n", man->has_type);
74 pr_err(" use_type: %d\n", man->use_type);
75 pr_err(" flags: 0x%08X\n", man->flags);
76 pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
77 pr_err(" size: %llu\n", man->size);
78 pr_err(" available_caching: 0x%08X\n", man->available_caching);
79 pr_err(" default_caching: 0x%08X\n", 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
Joe Perches25d04792012-03-16 21:43:50 -070089 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
90 bo, bo->mem.num_pages, bo->mem.size >> 10,
91 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010092 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;
Joe Perches25d04792012-03-16 21:43:50 -070097 pr_err(" 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,
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200165 !ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200166 } else {
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200167 wait_event(bo->event_queue, !ttm_bo_is_reserved(bo));
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
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200178 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200179
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
Thomas Hellstrom6c1e9632012-11-06 11:31:51 +0000223 while (unlikely(atomic_read(&bo->reserved) != 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
Thomas Hellstrom6c1e9632012-11-06 11:31:51 +0000252 atomic_set(&bo->reserved, 1);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200253 if (use_sequence) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100254 /**
255 * Wake up waiters that may need to recheck for deadlock,
256 * if we decreased the sequence number.
257 */
258 if (unlikely((bo->val_seq - sequence < (1 << 31))
259 || !bo->seq_valid))
260 wake_up_all(&bo->event_queue);
261
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200262 bo->val_seq = sequence;
263 bo->seq_valid = true;
264 } else {
265 bo->seq_valid = false;
266 }
267
268 return 0;
269}
270EXPORT_SYMBOL(ttm_bo_reserve);
271
272static void ttm_bo_ref_bug(struct kref *list_kref)
273{
274 BUG();
275}
276
Dave Airlied6ea8882010-11-22 13:24:40 +1000277void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
278 bool never_free)
279{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100280 kref_sub(&bo->list_kref, count,
281 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000282}
283
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200284int ttm_bo_reserve(struct ttm_buffer_object *bo,
285 bool interruptible,
286 bool no_wait, bool use_sequence, uint32_t sequence)
287{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200288 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200289 int put_count = 0;
290 int ret;
291
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200292 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200293 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
294 sequence);
295 if (likely(ret == 0))
296 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200297 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200298
Dave Airlied6ea8882010-11-22 13:24:40 +1000299 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200300
301 return ret;
302}
303
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000304void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
305{
306 ttm_bo_add_to_lru(bo);
307 atomic_set(&bo->reserved, 0);
308 wake_up_all(&bo->event_queue);
309}
310
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200311void ttm_bo_unreserve(struct ttm_buffer_object *bo)
312{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200313 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200314
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200315 spin_lock(&glob->lru_lock);
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000316 ttm_bo_unreserve_locked(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200317 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200318}
319EXPORT_SYMBOL(ttm_bo_unreserve);
320
321/*
322 * Call bo->mutex locked.
323 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200324static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
325{
326 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200327 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200328 int ret = 0;
329 uint32_t page_flags = 0;
330
331 TTM_ASSERT_LOCKED(&bo->mutex);
332 bo->ttm = NULL;
333
Dave Airliead49f502009-07-10 22:36:26 +1000334 if (bdev->need_dma32)
335 page_flags |= TTM_PAGE_FLAG_DMA32;
336
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200337 switch (bo->type) {
338 case ttm_bo_type_device:
339 if (zero_alloc)
340 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
341 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400342 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
343 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344 if (unlikely(bo->ttm == NULL))
345 ret = -ENOMEM;
346 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100347 case ttm_bo_type_sg:
348 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
349 page_flags | TTM_PAGE_FLAG_SG,
350 glob->dummy_read_page);
351 if (unlikely(bo->ttm == NULL)) {
352 ret = -ENOMEM;
353 break;
354 }
355 bo->ttm->sg = bo->sg;
356 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200357 default:
Joe Perches25d04792012-03-16 21:43:50 -0700358 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200359 ret = -EINVAL;
360 break;
361 }
362
363 return ret;
364}
365
366static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
367 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000368 bool evict, bool interruptible,
369 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200370{
371 struct ttm_bo_device *bdev = bo->bdev;
372 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
373 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
374 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
375 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
376 int ret = 0;
377
378 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100379 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
380 ret = ttm_mem_io_lock(old_man, true);
381 if (unlikely(ret != 0))
382 goto out_err;
383 ttm_bo_unmap_virtual_locked(bo);
384 ttm_mem_io_unlock(old_man);
385 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200386
387 /*
388 * Create and bind a ttm if required.
389 */
390
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000391 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
392 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000393 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
394 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000395 if (ret)
396 goto out_err;
397 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200398
399 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
400 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200401 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200402
403 if (mem->mem_type != TTM_PL_SYSTEM) {
404 ret = ttm_tt_bind(bo->ttm, mem);
405 if (ret)
406 goto out_err;
407 }
408
409 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000410 if (bdev->driver->move_notify)
411 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100412 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200413 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200414 goto moved;
415 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200416 }
417
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000418 if (bdev->driver->move_notify)
419 bdev->driver->move_notify(bo, mem);
420
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200421 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
422 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000423 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200424 else if (bdev->driver->move)
425 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000426 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200427 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000428 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200429
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000430 if (ret) {
431 if (bdev->driver->move_notify) {
432 struct ttm_mem_reg tmp_mem = *mem;
433 *mem = bo->mem;
434 bo->mem = tmp_mem;
435 bdev->driver->move_notify(bo, mem);
436 bo->mem = *mem;
437 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200438
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000439 goto out_err;
440 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500441
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200442moved:
443 if (bo->evicted) {
444 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
445 if (ret)
Joe Perches25d04792012-03-16 21:43:50 -0700446 pr_err("Can not flush read caches\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200447 bo->evicted = false;
448 }
449
450 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000451 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200452 bdev->man[bo->mem.mem_type].gpu_offset;
453 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100454 } else
455 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200456
457 return 0;
458
459out_err:
460 new_man = &bdev->man[bo->mem.mem_type];
461 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
462 ttm_tt_unbind(bo->ttm);
463 ttm_tt_destroy(bo->ttm);
464 bo->ttm = NULL;
465 }
466
467 return ret;
468}
469
470/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200471 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200472 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200473 * This is the place to put in driver specific hooks to release
474 * driver private resources.
475 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200476 */
477
478static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
479{
Jerome Glissedc97b342011-11-18 11:47:03 -0500480 if (bo->bdev->driver->move_notify)
481 bo->bdev->driver->move_notify(bo, NULL);
482
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200483 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200484 ttm_tt_unbind(bo->ttm);
485 ttm_tt_destroy(bo->ttm);
486 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200487 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200488 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200489
490 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200491
492 /*
493 * Make processes trying to reserve really pick it up.
494 */
495 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200496 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200497}
498
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200499static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200500{
501 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200502 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200503 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000504 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200505 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200506 int ret;
507
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000508 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200509 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200510 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200511
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200512 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100513
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200514 /**
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000515 * Lock inversion between bo:reserve and bdev::fence_lock here,
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200516 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200517 */
518
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200519 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200520
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200521 if (unlikely(ret == -EBUSY))
522 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200523
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000524 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200525 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200526
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200527 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200528 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200529
Dave Airlied6ea8882010-11-22 13:24:40 +1000530 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200531
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200532 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200533 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200534 spin_lock(&glob->lru_lock);
535 }
536queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200537 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000538 if (bo->sync_obj)
539 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200540
541 kref_get(&bo->list_kref);
542 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
543 spin_unlock(&glob->lru_lock);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000544 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200545
Thomas Hellstromaa123262010-11-02 13:21:47 +0000546 if (sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +0000547 driver->sync_obj_flush(sync_obj);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000548 driver->sync_obj_unref(&sync_obj);
549 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200550 schedule_delayed_work(&bdev->wq,
551 ((HZ / 100) < 1) ? 1 : HZ / 100);
552}
553
554/**
555 * function ttm_bo_cleanup_refs
556 * If bo idle, remove from delayed- and lru lists, and unref.
557 * If not idle, do nothing.
558 *
559 * @interruptible Any sleeps should occur interruptibly.
560 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
561 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
562 */
563
564static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
565 bool interruptible,
566 bool no_wait_reserve,
567 bool no_wait_gpu)
568{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000569 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200570 struct ttm_bo_global *glob = bo->glob;
571 int put_count;
572 int ret = 0;
573
574retry:
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000575 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200576 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000577 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200578
579 if (unlikely(ret != 0))
580 return ret;
581
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000582retry_reserve:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200583 spin_lock(&glob->lru_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100584
585 if (unlikely(list_empty(&bo->ddestroy))) {
586 spin_unlock(&glob->lru_lock);
587 return 0;
588 }
589
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000590 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200591
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000592 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200593 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000594 if (likely(!no_wait_reserve))
595 ret = ttm_bo_wait_unreserved(bo, interruptible);
596 if (unlikely(ret != 0))
597 return ret;
598
599 goto retry_reserve;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200600 }
601
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000602 BUG_ON(ret != 0);
603
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200604 /**
605 * We can re-check for sync object without taking
606 * the bo::lock since setting the sync object requires
607 * also bo::reserved. A busy object at this point may
608 * be caused by another thread recently starting an accelerated
609 * eviction.
610 */
611
612 if (unlikely(bo->sync_obj)) {
613 atomic_set(&bo->reserved, 0);
614 wake_up_all(&bo->event_queue);
615 spin_unlock(&glob->lru_lock);
616 goto retry;
617 }
618
619 put_count = ttm_bo_del_from_lru(bo);
620 list_del_init(&bo->ddestroy);
621 ++put_count;
622
623 spin_unlock(&glob->lru_lock);
624 ttm_bo_cleanup_memtype_use(bo);
625
Dave Airlied6ea8882010-11-22 13:24:40 +1000626 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200627
628 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200629}
630
631/**
632 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
633 * encountered buffers.
634 */
635
636static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
637{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200638 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100639 struct ttm_buffer_object *entry = NULL;
640 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200641
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200642 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100643 if (list_empty(&bdev->ddestroy))
644 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200645
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100646 entry = list_first_entry(&bdev->ddestroy,
647 struct ttm_buffer_object, ddestroy);
648 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100650 for (;;) {
651 struct ttm_buffer_object *nentry = NULL;
652
653 if (entry->ddestroy.next != &bdev->ddestroy) {
654 nentry = list_first_entry(&entry->ddestroy,
655 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656 kref_get(&nentry->list_kref);
657 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200658
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200659 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200660 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
661 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200662 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100663 entry = nentry;
664
665 if (ret || !entry)
666 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200667
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200668 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100669 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200670 break;
671 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200672
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100673out_unlock:
674 spin_unlock(&glob->lru_lock);
675out:
676 if (entry)
677 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200678 return ret;
679}
680
681static void ttm_bo_delayed_workqueue(struct work_struct *work)
682{
683 struct ttm_bo_device *bdev =
684 container_of(work, struct ttm_bo_device, wq.work);
685
686 if (ttm_bo_delayed_delete(bdev, false)) {
687 schedule_delayed_work(&bdev->wq,
688 ((HZ / 100) < 1) ? 1 : HZ / 100);
689 }
690}
691
692static void ttm_bo_release(struct kref *kref)
693{
694 struct ttm_buffer_object *bo =
695 container_of(kref, struct ttm_buffer_object, kref);
696 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100697 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200698
Thomas Hellstrom82fe50b2012-11-21 14:53:21 +0000699 write_lock(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200700 if (likely(bo->vm_node != NULL)) {
701 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
702 drm_mm_put_block(bo->vm_node);
703 bo->vm_node = NULL;
704 }
705 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100706 ttm_mem_io_lock(man, false);
707 ttm_mem_io_free_vm(bo);
708 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200709 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200710 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200711}
712
713void ttm_bo_unref(struct ttm_buffer_object **p_bo)
714{
715 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200716
717 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200718 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200719}
720EXPORT_SYMBOL(ttm_bo_unref);
721
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400722int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
723{
724 return cancel_delayed_work_sync(&bdev->wq);
725}
726EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
727
728void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
729{
730 if (resched)
731 schedule_delayed_work(&bdev->wq,
732 ((HZ / 100) < 1) ? 1 : HZ / 100);
733}
734EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
735
Jerome Glisseca262a9992009-12-08 15:33:32 +0100736static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000737 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200738{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200739 struct ttm_bo_device *bdev = bo->bdev;
740 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100741 struct ttm_placement placement;
742 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200743
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000744 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200745 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000746 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200747
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200748 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100749 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700750 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200751 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200752 goto out;
753 }
754
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200755 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200756
757 evict_mem = bo->mem;
758 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100759 evict_mem.bus.io_reserved_vm = false;
760 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200761
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100762 placement.fpfn = 0;
763 placement.lpfn = 0;
764 placement.num_placement = 0;
765 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100766 bdev->driver->evict_flags(bo, &placement);
767 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000768 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200769 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100770 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700771 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
772 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100773 ttm_bo_mem_space_debug(bo, &placement);
774 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200775 goto out;
776 }
777
778 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000779 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200780 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100781 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700782 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000783 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200784 goto out;
785 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200786 bo->evicted = true;
787out:
788 return ret;
789}
790
Jerome Glisseca262a9992009-12-08 15:33:32 +0100791static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
792 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000793 bool interruptible, bool no_wait_reserve,
794 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100795{
796 struct ttm_bo_global *glob = bdev->glob;
797 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
798 struct ttm_buffer_object *bo;
799 int ret, put_count = 0;
800
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100801retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100802 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100803 if (list_empty(&man->lru)) {
804 spin_unlock(&glob->lru_lock);
805 return -EBUSY;
806 }
807
Jerome Glisseca262a9992009-12-08 15:33:32 +0100808 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
809 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100810
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200811 if (!list_empty(&bo->ddestroy)) {
812 spin_unlock(&glob->lru_lock);
813 ret = ttm_bo_cleanup_refs(bo, interruptible,
814 no_wait_reserve, no_wait_gpu);
815 kref_put(&bo->list_kref, ttm_bo_release_list);
816
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000817 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200818 }
819
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000820 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100821
822 if (unlikely(ret == -EBUSY)) {
823 spin_unlock(&glob->lru_lock);
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000824 if (likely(!no_wait_reserve))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100825 ret = ttm_bo_wait_unreserved(bo, interruptible);
826
827 kref_put(&bo->list_kref, ttm_bo_release_list);
828
829 /**
830 * We *need* to retry after releasing the lru lock.
831 */
832
833 if (unlikely(ret != 0))
834 return ret;
835 goto retry;
836 }
837
838 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100839 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100840
841 BUG_ON(ret != 0);
842
Dave Airlied6ea8882010-11-22 13:24:40 +1000843 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100844
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000845 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100846 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100847
Jerome Glisseca262a9992009-12-08 15:33:32 +0100848 kref_put(&bo->list_kref, ttm_bo_release_list);
849 return ret;
850}
851
Ben Skeggs42311ff2010-08-04 12:07:08 +1000852void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
853{
Ben Skeggsd961db72010-08-05 10:48:18 +1000854 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000855
Ben Skeggsd961db72010-08-05 10:48:18 +1000856 if (mem->mm_node)
857 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000858}
859EXPORT_SYMBOL(ttm_bo_mem_put);
860
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200861/**
862 * Repeatedly evict memory from the LRU for @mem_type until we create enough
863 * space, or we've evicted everything and there isn't enough space.
864 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100865static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
866 uint32_t mem_type,
867 struct ttm_placement *placement,
868 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000869 bool interruptible,
870 bool no_wait_reserve,
871 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200872{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100873 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200874 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200875 int ret;
876
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000878 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200879 if (unlikely(ret != 0))
880 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000881 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100882 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100883 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000884 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100885 if (unlikely(ret != 0))
886 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200887 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000888 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200889 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200890 mem->mem_type = mem_type;
891 return 0;
892}
893
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200894static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
895 uint32_t cur_placement,
896 uint32_t proposed_placement)
897{
898 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
899 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
900
901 /**
902 * Keep current caching if possible.
903 */
904
905 if ((cur_placement & caching) != 0)
906 result |= (cur_placement & caching);
907 else if ((man->default_caching & caching) != 0)
908 result |= man->default_caching;
909 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
910 result |= TTM_PL_FLAG_CACHED;
911 else if ((TTM_PL_FLAG_WC & caching) != 0)
912 result |= TTM_PL_FLAG_WC;
913 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
914 result |= TTM_PL_FLAG_UNCACHED;
915
916 return result;
917}
918
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200919static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200920 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200921 uint32_t proposed_placement,
922 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923{
924 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
925
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200926 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200927 return false;
928
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200929 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200930 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200931
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200932 cur_flags |= (proposed_placement & man->available_caching);
933
934 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200935 return true;
936}
937
938/**
939 * Creates space for memory region @mem according to its type.
940 *
941 * This function first searches for free space in compatible memory types in
942 * the priority order defined by the driver. If free space isn't found, then
943 * ttm_bo_mem_force_space is attempted in priority order to evict and find
944 * space.
945 */
946int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100947 struct ttm_placement *placement,
948 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000949 bool interruptible, bool no_wait_reserve,
950 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951{
952 struct ttm_bo_device *bdev = bo->bdev;
953 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200954 uint32_t mem_type = TTM_PL_SYSTEM;
955 uint32_t cur_flags = 0;
956 bool type_found = false;
957 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100958 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100959 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200960
961 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000962 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100963 ret = ttm_mem_type_from_flags(placement->placement[i],
964 &mem_type);
965 if (ret)
966 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200967 man = &bdev->man[mem_type];
968
969 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100970 mem_type,
971 placement->placement[i],
972 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200973
974 if (!type_ok)
975 continue;
976
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200977 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
978 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100979 /*
980 * Use the access and other non-mapping-related flag bits from
981 * the memory placement flags to the current flags
982 */
983 ttm_flag_masked(&cur_flags, placement->placement[i],
984 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200985
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200986 if (mem_type == TTM_PL_SYSTEM)
987 break;
988
989 if (man->has_type && man->use_type) {
990 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000991 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100992 if (unlikely(ret))
993 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200994 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000995 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200996 break;
997 }
998
Ben Skeggsd961db72010-08-05 10:48:18 +1000999 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001000 mem->mem_type = mem_type;
1001 mem->placement = cur_flags;
1002 return 0;
1003 }
1004
1005 if (!type_found)
1006 return -EINVAL;
1007
Dave Airlieb6637522009-12-14 14:51:35 +10001008 for (i = 0; i < placement->num_busy_placement; ++i) {
1009 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001010 &mem_type);
1011 if (ret)
1012 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001014 if (!man->has_type)
1015 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001016 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001017 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001018 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001019 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001020 continue;
1021
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001022 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1023 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001024 /*
1025 * Use the access and other non-mapping-related flag bits from
1026 * the memory placement flags to the current flags
1027 */
Dave Airlieb6637522009-12-14 14:51:35 +10001028 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001029 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001030
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001031
1032 if (mem_type == TTM_PL_SYSTEM) {
1033 mem->mem_type = mem_type;
1034 mem->placement = cur_flags;
1035 mem->mm_node = NULL;
1036 return 0;
1037 }
1038
Jerome Glisseca262a9992009-12-08 15:33:32 +01001039 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001040 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001041 if (ret == 0 && mem->mm_node) {
1042 mem->placement = cur_flags;
1043 return 0;
1044 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001045 if (ret == -ERESTARTSYS)
1046 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001047 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001048 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049 return ret;
1050}
1051EXPORT_SYMBOL(ttm_bo_mem_space);
1052
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001053int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001054 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001055 bool interruptible, bool no_wait_reserve,
1056 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001057{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001058 int ret = 0;
1059 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001060 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001061
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001062 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001063
1064 /*
1065 * FIXME: It's possible to pipeline buffer moves.
1066 * Have the driver move function wait for idle when necessary,
1067 * instead of doing it here.
1068 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001069 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001070 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001071 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001072 if (ret)
1073 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074 mem.num_pages = bo->num_pages;
1075 mem.size = mem.num_pages << PAGE_SHIFT;
1076 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001077 mem.bus.io_reserved_vm = false;
1078 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001079 /*
1080 * Determine where to move the buffer.
1081 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001082 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001083 if (ret)
1084 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001085 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001087 if (ret && mem.mm_node)
1088 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089 return ret;
1090}
1091
Jerome Glisseca262a9992009-12-08 15:33:32 +01001092static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093 struct ttm_mem_reg *mem)
1094{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001095 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001096
Ben Skeggsd961db72010-08-05 10:48:18 +10001097 if (mem->mm_node && placement->lpfn != 0 &&
1098 (mem->start < placement->fpfn ||
1099 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001100 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101
Jerome Glisseca262a9992009-12-08 15:33:32 +01001102 for (i = 0; i < placement->num_placement; i++) {
1103 if ((placement->placement[i] & mem->placement &
1104 TTM_PL_MASK_CACHING) &&
1105 (placement->placement[i] & mem->placement &
1106 TTM_PL_MASK_MEM))
1107 return i;
1108 }
1109 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110}
1111
Jerome Glisse09855ac2009-12-10 17:16:27 +01001112int ttm_bo_validate(struct ttm_buffer_object *bo,
1113 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001114 bool interruptible, bool no_wait_reserve,
1115 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001116{
1117 int ret;
1118
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001119 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001120 /* Check that range is valid */
1121 if (placement->lpfn || placement->fpfn)
1122 if (placement->fpfn > placement->lpfn ||
1123 (placement->lpfn - placement->fpfn) < bo->num_pages)
1124 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125 /*
1126 * Check whether we need to move buffer.
1127 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001128 ret = ttm_bo_mem_compat(placement, &bo->mem);
1129 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001130 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001131 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001132 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001133 } else {
1134 /*
1135 * Use the access and other non-mapping-related flag bits from
1136 * the compatible memory placement flags to the active flags
1137 */
1138 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1139 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001140 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001141 /*
1142 * We might need to add a TTM.
1143 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1145 ret = ttm_bo_add_ttm(bo, true);
1146 if (ret)
1147 return ret;
1148 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149 return 0;
1150}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001151EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152
Jerome Glisse09855ac2009-12-10 17:16:27 +01001153int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1154 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001155{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001156 BUG_ON((placement->fpfn || placement->lpfn) &&
1157 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001159 return 0;
1160}
1161
Jerome Glisse09855ac2009-12-10 17:16:27 +01001162int ttm_bo_init(struct ttm_bo_device *bdev,
1163 struct ttm_buffer_object *bo,
1164 unsigned long size,
1165 enum ttm_bo_type type,
1166 struct ttm_placement *placement,
1167 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001168 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001169 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001170 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001171 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001172 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001173{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001174 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001175 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001176 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1177
1178 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1179 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001180 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001181 if (destroy)
1182 (*destroy)(bo);
1183 else
1184 kfree(bo);
1185 return -ENOMEM;
1186 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001188 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1189 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001190 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001191 if (destroy)
1192 (*destroy)(bo);
1193 else
1194 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001195 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196 return -EINVAL;
1197 }
1198 bo->destroy = destroy;
1199
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200 kref_init(&bo->kref);
1201 kref_init(&bo->list_kref);
1202 atomic_set(&bo->cpu_writers, 0);
1203 atomic_set(&bo->reserved, 1);
1204 init_waitqueue_head(&bo->event_queue);
1205 INIT_LIST_HEAD(&bo->lru);
1206 INIT_LIST_HEAD(&bo->ddestroy);
1207 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001208 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001210 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001211 bo->type = type;
1212 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001213 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001214 bo->mem.mem_type = TTM_PL_SYSTEM;
1215 bo->mem.num_pages = bo->num_pages;
1216 bo->mem.mm_node = NULL;
1217 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001218 bo->mem.bus.io_reserved_vm = false;
1219 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001220 bo->priv_flags = 0;
1221 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1222 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001223 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001224 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001225 bo->sg = sg;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001226 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001227
Jerome Glisse09855ac2009-12-10 17:16:27 +01001228 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001229 if (unlikely(ret != 0))
1230 goto out_err;
1231
1232 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001233 * For ttm_bo_type_device buffers, allocate
1234 * address space from the device.
1235 */
Dave Airlie129b78b2012-04-02 11:46:06 +01001236 if (bo->type == ttm_bo_type_device ||
1237 bo->type == ttm_bo_type_sg) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001238 ret = ttm_bo_setup_vm(bo);
1239 if (ret)
1240 goto out_err;
1241 }
1242
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001243 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001244 if (ret)
1245 goto out_err;
1246
1247 ttm_bo_unreserve(bo);
1248 return 0;
1249
1250out_err:
1251 ttm_bo_unreserve(bo);
1252 ttm_bo_unref(&bo);
1253
1254 return ret;
1255}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001256EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001257
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001258size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1259 unsigned long bo_size,
1260 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001261{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001262 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1263 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001264
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001265 size += ttm_round_pot(struct_size);
1266 size += PAGE_ALIGN(npages * sizeof(void *));
1267 size += ttm_round_pot(sizeof(struct ttm_tt));
1268 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001270EXPORT_SYMBOL(ttm_bo_acc_size);
1271
1272size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1273 unsigned long bo_size,
1274 unsigned struct_size)
1275{
1276 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1277 size_t size = 0;
1278
1279 size += ttm_round_pot(struct_size);
1280 size += PAGE_ALIGN(npages * sizeof(void *));
1281 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1282 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1283 return size;
1284}
1285EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001286
Jerome Glisse09855ac2009-12-10 17:16:27 +01001287int ttm_bo_create(struct ttm_bo_device *bdev,
1288 unsigned long size,
1289 enum ttm_bo_type type,
1290 struct ttm_placement *placement,
1291 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001292 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001293 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001294 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001295{
1296 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001297 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001298 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001300 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001301 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001303
Thomas Hellstroma393c732012-06-12 13:28:42 +02001304 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001305 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001306 interruptible, persistent_swap_storage, acc_size,
1307 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001308 if (likely(ret == 0))
1309 *p_bo = bo;
1310
1311 return ret;
1312}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001313EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001314
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001315static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001316 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001317{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001318 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001319 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001320 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001321
1322 /*
1323 * Can't use standard list traversal since we're unlocking.
1324 */
1325
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001326 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001327 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001328 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001329 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001330 if (ret) {
1331 if (allow_errors) {
1332 return ret;
1333 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001334 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001335 }
1336 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001337 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001338 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001339 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001340 return 0;
1341}
1342
1343int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1344{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001345 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001346 int ret = -EINVAL;
1347
1348 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001349 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001350 return ret;
1351 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001352 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001353
1354 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001355 pr_err("Trying to take down uninitialized memory manager type %u\n",
1356 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001357 return ret;
1358 }
1359
1360 man->use_type = false;
1361 man->has_type = false;
1362
1363 ret = 0;
1364 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001365 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001366
Ben Skeggsd961db72010-08-05 10:48:18 +10001367 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001368 }
1369
1370 return ret;
1371}
1372EXPORT_SYMBOL(ttm_bo_clean_mm);
1373
1374int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1375{
1376 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1377
1378 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001379 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001380 return -EINVAL;
1381 }
1382
1383 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001384 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001385 return 0;
1386 }
1387
Jerome Glisseca262a9992009-12-08 15:33:32 +01001388 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001389}
1390EXPORT_SYMBOL(ttm_bo_evict_mm);
1391
1392int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001393 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001394{
1395 int ret = -EINVAL;
1396 struct ttm_mem_type_manager *man;
1397
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001398 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001399 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001400 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001401 man->io_reserve_fastpath = true;
1402 man->use_io_reserve_lru = false;
1403 mutex_init(&man->io_reserve_mutex);
1404 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001405
1406 ret = bdev->driver->init_mem_type(bdev, type, man);
1407 if (ret)
1408 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001409 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001410
1411 ret = 0;
1412 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001413 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001414 if (ret)
1415 return ret;
1416 }
1417 man->has_type = true;
1418 man->use_type = true;
1419 man->size = p_size;
1420
1421 INIT_LIST_HEAD(&man->lru);
1422
1423 return 0;
1424}
1425EXPORT_SYMBOL(ttm_bo_init_mm);
1426
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001427static void ttm_bo_global_kobj_release(struct kobject *kobj)
1428{
1429 struct ttm_bo_global *glob =
1430 container_of(kobj, struct ttm_bo_global, kobj);
1431
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001432 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1433 __free_page(glob->dummy_read_page);
1434 kfree(glob);
1435}
1436
Dave Airlieba4420c2010-03-09 10:56:52 +10001437void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001438{
1439 struct ttm_bo_global *glob = ref->object;
1440
1441 kobject_del(&glob->kobj);
1442 kobject_put(&glob->kobj);
1443}
1444EXPORT_SYMBOL(ttm_bo_global_release);
1445
Dave Airlieba4420c2010-03-09 10:56:52 +10001446int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001447{
1448 struct ttm_bo_global_ref *bo_ref =
1449 container_of(ref, struct ttm_bo_global_ref, ref);
1450 struct ttm_bo_global *glob = ref->object;
1451 int ret;
1452
1453 mutex_init(&glob->device_list_mutex);
1454 spin_lock_init(&glob->lru_lock);
1455 glob->mem_glob = bo_ref->mem_glob;
1456 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1457
1458 if (unlikely(glob->dummy_read_page == NULL)) {
1459 ret = -ENOMEM;
1460 goto out_no_drp;
1461 }
1462
1463 INIT_LIST_HEAD(&glob->swap_lru);
1464 INIT_LIST_HEAD(&glob->device_list);
1465
1466 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1467 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1468 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001469 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001470 goto out_no_shrink;
1471 }
1472
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001473 atomic_set(&glob->bo_count, 0);
1474
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001475 ret = kobject_init_and_add(
1476 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001477 if (unlikely(ret != 0))
1478 kobject_put(&glob->kobj);
1479 return ret;
1480out_no_shrink:
1481 __free_page(glob->dummy_read_page);
1482out_no_drp:
1483 kfree(glob);
1484 return ret;
1485}
1486EXPORT_SYMBOL(ttm_bo_global_init);
1487
1488
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001489int ttm_bo_device_release(struct ttm_bo_device *bdev)
1490{
1491 int ret = 0;
1492 unsigned i = TTM_NUM_MEM_TYPES;
1493 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001494 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001495
1496 while (i--) {
1497 man = &bdev->man[i];
1498 if (man->has_type) {
1499 man->use_type = false;
1500 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1501 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001502 pr_err("DRM memory manager type %d is not clean\n",
1503 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001504 }
1505 man->has_type = false;
1506 }
1507 }
1508
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001509 mutex_lock(&glob->device_list_mutex);
1510 list_del(&bdev->device_list);
1511 mutex_unlock(&glob->device_list_mutex);
1512
Tejun Heof094cfc2010-12-24 15:59:06 +01001513 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001514
1515 while (ttm_bo_delayed_delete(bdev, true))
1516 ;
1517
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001518 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001519 if (list_empty(&bdev->ddestroy))
1520 TTM_DEBUG("Delayed destroy list was clean\n");
1521
1522 if (list_empty(&bdev->man[0].lru))
1523 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001524 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1527 write_lock(&bdev->vm_lock);
1528 drm_mm_takedown(&bdev->addr_space_mm);
1529 write_unlock(&bdev->vm_lock);
1530
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531 return ret;
1532}
1533EXPORT_SYMBOL(ttm_bo_device_release);
1534
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001535int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001536 struct ttm_bo_global *glob,
1537 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001538 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001539 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001540{
1541 int ret = -EINVAL;
1542
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001543 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001544 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001545
1546 memset(bdev->man, 0, sizeof(bdev->man));
1547
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001548 /*
1549 * Initialize the system memory buffer type.
1550 * Other types need to be driver / IOCTL initialized.
1551 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001552 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001554 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001555
1556 bdev->addr_space_rb = RB_ROOT;
1557 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1558 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001559 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001560
1561 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001562 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001564 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001565 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001566 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001567 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001568 mutex_lock(&glob->device_list_mutex);
1569 list_add_tail(&bdev->device_list, &glob->device_list);
1570 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001571
1572 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001573out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001574 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001575out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001576 return ret;
1577}
1578EXPORT_SYMBOL(ttm_bo_device_init);
1579
1580/*
1581 * buffer object vm functions.
1582 */
1583
1584bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1585{
1586 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1587
1588 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1589 if (mem->mem_type == TTM_PL_SYSTEM)
1590 return false;
1591
1592 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1593 return false;
1594
1595 if (mem->placement & TTM_PL_FLAG_CACHED)
1596 return false;
1597 }
1598 return true;
1599}
1600
Thomas Hellstromeba67092010-11-11 09:41:57 +01001601void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001602{
1603 struct ttm_bo_device *bdev = bo->bdev;
1604 loff_t offset = (loff_t) bo->addr_space_offset;
1605 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1606
1607 if (!bdev->dev_mapping)
1608 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001609 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001610 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001611}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001612
1613void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1614{
1615 struct ttm_bo_device *bdev = bo->bdev;
1616 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1617
1618 ttm_mem_io_lock(man, false);
1619 ttm_bo_unmap_virtual_locked(bo);
1620 ttm_mem_io_unlock(man);
1621}
1622
1623
Dave Airliee024e112009-06-24 09:48:08 +10001624EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001625
1626static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1627{
1628 struct ttm_bo_device *bdev = bo->bdev;
1629 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1630 struct rb_node *parent = NULL;
1631 struct ttm_buffer_object *cur_bo;
1632 unsigned long offset = bo->vm_node->start;
1633 unsigned long cur_offset;
1634
1635 while (*cur) {
1636 parent = *cur;
1637 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1638 cur_offset = cur_bo->vm_node->start;
1639 if (offset < cur_offset)
1640 cur = &parent->rb_left;
1641 else if (offset > cur_offset)
1642 cur = &parent->rb_right;
1643 else
1644 BUG();
1645 }
1646
1647 rb_link_node(&bo->vm_rb, parent, cur);
1648 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1649}
1650
1651/**
1652 * ttm_bo_setup_vm:
1653 *
1654 * @bo: the buffer to allocate address space for
1655 *
1656 * Allocate address space in the drm device so that applications
1657 * can mmap the buffer and access the contents. This only
1658 * applies to ttm_bo_type_device objects as others are not
1659 * placed in the drm device address space.
1660 */
1661
1662static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1663{
1664 struct ttm_bo_device *bdev = bo->bdev;
1665 int ret;
1666
1667retry_pre_get:
1668 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1669 if (unlikely(ret != 0))
1670 return ret;
1671
1672 write_lock(&bdev->vm_lock);
1673 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1674 bo->mem.num_pages, 0, 0);
1675
1676 if (unlikely(bo->vm_node == NULL)) {
1677 ret = -ENOMEM;
1678 goto out_unlock;
1679 }
1680
1681 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1682 bo->mem.num_pages, 0);
1683
1684 if (unlikely(bo->vm_node == NULL)) {
1685 write_unlock(&bdev->vm_lock);
1686 goto retry_pre_get;
1687 }
1688
1689 ttm_bo_vm_insert_rb(bo);
1690 write_unlock(&bdev->vm_lock);
1691 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1692
1693 return 0;
1694out_unlock:
1695 write_unlock(&bdev->vm_lock);
1696 return ret;
1697}
1698
1699int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001700 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001701{
1702 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001703 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001704 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001705 int ret = 0;
1706
Dave Airlie1717c0e2011-10-27 18:28:37 +02001707 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001708 return 0;
1709
Dave Airlie1717c0e2011-10-27 18:28:37 +02001710 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001711
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001712 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001713 void *tmp_obj = bo->sync_obj;
1714 bo->sync_obj = NULL;
1715 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1716 spin_unlock(&bdev->fence_lock);
1717 driver->sync_obj_unref(&tmp_obj);
1718 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001719 continue;
1720 }
1721
1722 if (no_wait)
1723 return -EBUSY;
1724
Dave Airlie1717c0e2011-10-27 18:28:37 +02001725 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001726 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001727 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001728 lazy, interruptible);
1729 if (unlikely(ret != 0)) {
1730 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001731 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001732 return ret;
1733 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001734 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001735 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001736 void *tmp_obj = bo->sync_obj;
1737 bo->sync_obj = NULL;
1738 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1739 &bo->priv_flags);
1740 spin_unlock(&bdev->fence_lock);
1741 driver->sync_obj_unref(&sync_obj);
1742 driver->sync_obj_unref(&tmp_obj);
1743 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001744 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001745 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001746 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001747 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001748 }
1749 }
1750 return 0;
1751}
1752EXPORT_SYMBOL(ttm_bo_wait);
1753
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001754int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1755{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001756 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001757 int ret = 0;
1758
1759 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001760 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001761 */
1762
1763 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1764 if (unlikely(ret != 0))
1765 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001766 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001767 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001768 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001769 if (likely(ret == 0))
1770 atomic_inc(&bo->cpu_writers);
1771 ttm_bo_unreserve(bo);
1772 return ret;
1773}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001774EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001775
1776void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1777{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001778 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001779}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001780EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001781
1782/**
1783 * A buffer object shrink method that tries to swap out the first
1784 * buffer object on the bo_global::swap_lru list.
1785 */
1786
1787static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1788{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001789 struct ttm_bo_global *glob =
1790 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001791 struct ttm_buffer_object *bo;
1792 int ret = -EBUSY;
1793 int put_count;
1794 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1795
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001796 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001797 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001798 if (unlikely(list_empty(&glob->swap_lru))) {
1799 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800 return -EBUSY;
1801 }
1802
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001803 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001804 struct ttm_buffer_object, swap);
1805 kref_get(&bo->list_kref);
1806
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001807 if (!list_empty(&bo->ddestroy)) {
1808 spin_unlock(&glob->lru_lock);
1809 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1810 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma8ff3ee2012-06-01 15:39:11 +02001811 spin_lock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001812 continue;
1813 }
1814
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001815 /**
1816 * Reserve buffer. Since we unlock while sleeping, we need
1817 * to re-check that nobody removed us from the swap-list while
1818 * we slept.
1819 */
1820
1821 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1822 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001823 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001824 ttm_bo_wait_unreserved(bo, false);
1825 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001826 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001827 }
1828 }
1829
1830 BUG_ON(ret != 0);
1831 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001832 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001833
Dave Airlied6ea8882010-11-22 13:24:40 +10001834 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001835
1836 /**
1837 * Wait for GPU, then move to system cached.
1838 */
1839
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001840 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001841 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001842 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001843
1844 if (unlikely(ret != 0))
1845 goto out;
1846
1847 if ((bo->mem.placement & swap_placement) != swap_placement) {
1848 struct ttm_mem_reg evict_mem;
1849
1850 evict_mem = bo->mem;
1851 evict_mem.mm_node = NULL;
1852 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1853 evict_mem.mem_type = TTM_PL_SYSTEM;
1854
1855 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001856 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001857 if (unlikely(ret != 0))
1858 goto out;
1859 }
1860
1861 ttm_bo_unmap_virtual(bo);
1862
1863 /**
1864 * Swap out. Buffer will be swapped in again as soon as
1865 * anyone tries to access a ttm page.
1866 */
1867
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001868 if (bo->bdev->driver->swap_notify)
1869 bo->bdev->driver->swap_notify(bo);
1870
Jan Engelhardt5df23972011-04-04 01:25:18 +02001871 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001872out:
1873
1874 /**
1875 *
1876 * Unreserve without putting on LRU to avoid swapping out an
1877 * already swapped buffer.
1878 */
1879
1880 atomic_set(&bo->reserved, 0);
1881 wake_up_all(&bo->event_queue);
1882 kref_put(&bo->list_kref, ttm_bo_release_list);
1883 return ret;
1884}
1885
1886void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1887{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001888 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001889 ;
1890}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001891EXPORT_SYMBOL(ttm_bo_swapout_all);