blob: b7781453bfd17728a3522520c46706b86f53e372 [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;
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100503 struct ttm_bo_driver *driver = bdev->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
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100508 spin_lock(&glob->lru_lock);
509 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
510
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000511 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200512 (void) ttm_bo_wait(bo, false, false, true);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100513 if (!ret && !bo->sync_obj) {
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 Hellstrome1efc9b2010-10-19 09:01:01 +0200523 }
Thomas Hellstromaa123262010-11-02 13:21:47 +0000524 if (bo->sync_obj)
525 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100526 spin_unlock(&bdev->fence_lock);
527
528 if (!ret) {
529 atomic_set(&bo->reserved, 0);
530 wake_up_all(&bo->event_queue);
531 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200532
533 kref_get(&bo->list_kref);
534 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
535 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200536
Thomas Hellstromaa123262010-11-02 13:21:47 +0000537 if (sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +0000538 driver->sync_obj_flush(sync_obj);
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
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000573retry_reserve:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200574 spin_lock(&glob->lru_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100575
576 if (unlikely(list_empty(&bo->ddestroy))) {
577 spin_unlock(&glob->lru_lock);
578 return 0;
579 }
580
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000581 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200582
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000583 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200584 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000585 if (likely(!no_wait_reserve))
586 ret = ttm_bo_wait_unreserved(bo, interruptible);
587 if (unlikely(ret != 0))
588 return ret;
589
590 goto retry_reserve;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200591 }
592
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000593 BUG_ON(ret != 0);
594
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200595 /**
596 * We can re-check for sync object without taking
597 * the bo::lock since setting the sync object requires
598 * also bo::reserved. A busy object at this point may
599 * be caused by another thread recently starting an accelerated
600 * eviction.
601 */
602
603 if (unlikely(bo->sync_obj)) {
604 atomic_set(&bo->reserved, 0);
605 wake_up_all(&bo->event_queue);
606 spin_unlock(&glob->lru_lock);
607 goto retry;
608 }
609
610 put_count = ttm_bo_del_from_lru(bo);
611 list_del_init(&bo->ddestroy);
612 ++put_count;
613
614 spin_unlock(&glob->lru_lock);
615 ttm_bo_cleanup_memtype_use(bo);
616
Dave Airlied6ea8882010-11-22 13:24:40 +1000617 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200618
619 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200620}
621
622/**
623 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
624 * encountered buffers.
625 */
626
627static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
628{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200629 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100630 struct ttm_buffer_object *entry = NULL;
631 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200632
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200633 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100634 if (list_empty(&bdev->ddestroy))
635 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200636
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100637 entry = list_first_entry(&bdev->ddestroy,
638 struct ttm_buffer_object, ddestroy);
639 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200640
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100641 for (;;) {
642 struct ttm_buffer_object *nentry = NULL;
643
644 if (entry->ddestroy.next != &bdev->ddestroy) {
645 nentry = list_first_entry(&entry->ddestroy,
646 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200647 kref_get(&nentry->list_kref);
648 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200650 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200651 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
652 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200653 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100654 entry = nentry;
655
656 if (ret || !entry)
657 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200658
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200659 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100660 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200661 break;
662 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200663
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100664out_unlock:
665 spin_unlock(&glob->lru_lock);
666out:
667 if (entry)
668 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200669 return ret;
670}
671
672static void ttm_bo_delayed_workqueue(struct work_struct *work)
673{
674 struct ttm_bo_device *bdev =
675 container_of(work, struct ttm_bo_device, wq.work);
676
677 if (ttm_bo_delayed_delete(bdev, false)) {
678 schedule_delayed_work(&bdev->wq,
679 ((HZ / 100) < 1) ? 1 : HZ / 100);
680 }
681}
682
683static void ttm_bo_release(struct kref *kref)
684{
685 struct ttm_buffer_object *bo =
686 container_of(kref, struct ttm_buffer_object, kref);
687 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100688 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200689
Thomas Hellstrom82fe50b2012-11-21 14:53:21 +0000690 write_lock(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200691 if (likely(bo->vm_node != NULL)) {
692 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
693 drm_mm_put_block(bo->vm_node);
694 bo->vm_node = NULL;
695 }
696 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100697 ttm_mem_io_lock(man, false);
698 ttm_mem_io_free_vm(bo);
699 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200700 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200701 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200702}
703
704void ttm_bo_unref(struct ttm_buffer_object **p_bo)
705{
706 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200707
708 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200709 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200710}
711EXPORT_SYMBOL(ttm_bo_unref);
712
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400713int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
714{
715 return cancel_delayed_work_sync(&bdev->wq);
716}
717EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
718
719void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
720{
721 if (resched)
722 schedule_delayed_work(&bdev->wq,
723 ((HZ / 100) < 1) ? 1 : HZ / 100);
724}
725EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
726
Jerome Glisseca262a9992009-12-08 15:33:32 +0100727static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000728 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200729{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200730 struct ttm_bo_device *bdev = bo->bdev;
731 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100732 struct ttm_placement placement;
733 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200734
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000735 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200736 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000737 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200738
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200739 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100740 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700741 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200742 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200743 goto out;
744 }
745
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200746 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200747
748 evict_mem = bo->mem;
749 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100750 evict_mem.bus.io_reserved_vm = false;
751 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200752
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100753 placement.fpfn = 0;
754 placement.lpfn = 0;
755 placement.num_placement = 0;
756 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100757 bdev->driver->evict_flags(bo, &placement);
758 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000759 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200760 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100761 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700762 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
763 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100764 ttm_bo_mem_space_debug(bo, &placement);
765 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200766 goto out;
767 }
768
769 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000770 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200771 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100772 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700773 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000774 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200775 goto out;
776 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200777 bo->evicted = true;
778out:
779 return ret;
780}
781
Jerome Glisseca262a9992009-12-08 15:33:32 +0100782static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
783 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000784 bool interruptible, bool no_wait_reserve,
785 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100786{
787 struct ttm_bo_global *glob = bdev->glob;
788 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
789 struct ttm_buffer_object *bo;
790 int ret, put_count = 0;
791
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100792retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100793 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100794 if (list_empty(&man->lru)) {
795 spin_unlock(&glob->lru_lock);
796 return -EBUSY;
797 }
798
Jerome Glisseca262a9992009-12-08 15:33:32 +0100799 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
800 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100801
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200802 if (!list_empty(&bo->ddestroy)) {
803 spin_unlock(&glob->lru_lock);
804 ret = ttm_bo_cleanup_refs(bo, interruptible,
805 no_wait_reserve, no_wait_gpu);
806 kref_put(&bo->list_kref, ttm_bo_release_list);
807
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000808 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200809 }
810
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000811 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100812
813 if (unlikely(ret == -EBUSY)) {
814 spin_unlock(&glob->lru_lock);
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000815 if (likely(!no_wait_reserve))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100816 ret = ttm_bo_wait_unreserved(bo, interruptible);
817
818 kref_put(&bo->list_kref, ttm_bo_release_list);
819
820 /**
821 * We *need* to retry after releasing the lru lock.
822 */
823
824 if (unlikely(ret != 0))
825 return ret;
826 goto retry;
827 }
828
829 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100830 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100831
832 BUG_ON(ret != 0);
833
Dave Airlied6ea8882010-11-22 13:24:40 +1000834 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100835
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000836 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100837 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100838
Jerome Glisseca262a9992009-12-08 15:33:32 +0100839 kref_put(&bo->list_kref, ttm_bo_release_list);
840 return ret;
841}
842
Ben Skeggs42311ff2010-08-04 12:07:08 +1000843void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
844{
Ben Skeggsd961db72010-08-05 10:48:18 +1000845 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000846
Ben Skeggsd961db72010-08-05 10:48:18 +1000847 if (mem->mm_node)
848 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000849}
850EXPORT_SYMBOL(ttm_bo_mem_put);
851
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200852/**
853 * Repeatedly evict memory from the LRU for @mem_type until we create enough
854 * space, or we've evicted everything and there isn't enough space.
855 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100856static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
857 uint32_t mem_type,
858 struct ttm_placement *placement,
859 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000860 bool interruptible,
861 bool no_wait_reserve,
862 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200863{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100864 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200866 int ret;
867
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200868 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000869 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200870 if (unlikely(ret != 0))
871 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000872 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100873 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100874 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000875 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100876 if (unlikely(ret != 0))
877 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200878 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000879 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200881 mem->mem_type = mem_type;
882 return 0;
883}
884
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200885static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
886 uint32_t cur_placement,
887 uint32_t proposed_placement)
888{
889 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
890 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
891
892 /**
893 * Keep current caching if possible.
894 */
895
896 if ((cur_placement & caching) != 0)
897 result |= (cur_placement & caching);
898 else if ((man->default_caching & caching) != 0)
899 result |= man->default_caching;
900 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
901 result |= TTM_PL_FLAG_CACHED;
902 else if ((TTM_PL_FLAG_WC & caching) != 0)
903 result |= TTM_PL_FLAG_WC;
904 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
905 result |= TTM_PL_FLAG_UNCACHED;
906
907 return result;
908}
909
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200910static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200911 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200912 uint32_t proposed_placement,
913 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200914{
915 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
916
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200917 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200918 return false;
919
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200920 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200921 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200923 cur_flags |= (proposed_placement & man->available_caching);
924
925 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926 return true;
927}
928
929/**
930 * Creates space for memory region @mem according to its type.
931 *
932 * This function first searches for free space in compatible memory types in
933 * the priority order defined by the driver. If free space isn't found, then
934 * ttm_bo_mem_force_space is attempted in priority order to evict and find
935 * space.
936 */
937int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100938 struct ttm_placement *placement,
939 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000940 bool interruptible, bool no_wait_reserve,
941 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200942{
943 struct ttm_bo_device *bdev = bo->bdev;
944 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200945 uint32_t mem_type = TTM_PL_SYSTEM;
946 uint32_t cur_flags = 0;
947 bool type_found = false;
948 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100949 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100950 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951
952 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000953 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100954 ret = ttm_mem_type_from_flags(placement->placement[i],
955 &mem_type);
956 if (ret)
957 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200958 man = &bdev->man[mem_type];
959
960 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100961 mem_type,
962 placement->placement[i],
963 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964
965 if (!type_ok)
966 continue;
967
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200968 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
969 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100970 /*
971 * Use the access and other non-mapping-related flag bits from
972 * the memory placement flags to the current flags
973 */
974 ttm_flag_masked(&cur_flags, placement->placement[i],
975 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200976
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200977 if (mem_type == TTM_PL_SYSTEM)
978 break;
979
980 if (man->has_type && man->use_type) {
981 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000982 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100983 if (unlikely(ret))
984 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200985 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000986 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200987 break;
988 }
989
Ben Skeggsd961db72010-08-05 10:48:18 +1000990 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200991 mem->mem_type = mem_type;
992 mem->placement = cur_flags;
993 return 0;
994 }
995
996 if (!type_found)
997 return -EINVAL;
998
Dave Airlieb6637522009-12-14 14:51:35 +1000999 for (i = 0; i < placement->num_busy_placement; ++i) {
1000 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001001 &mem_type);
1002 if (ret)
1003 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001004 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005 if (!man->has_type)
1006 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001007 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001008 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001009 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001010 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001011 continue;
1012
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001013 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1014 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001015 /*
1016 * Use the access and other non-mapping-related flag bits from
1017 * the memory placement flags to the current flags
1018 */
Dave Airlieb6637522009-12-14 14:51:35 +10001019 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001020 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001021
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001022
1023 if (mem_type == TTM_PL_SYSTEM) {
1024 mem->mem_type = mem_type;
1025 mem->placement = cur_flags;
1026 mem->mm_node = NULL;
1027 return 0;
1028 }
1029
Jerome Glisseca262a9992009-12-08 15:33:32 +01001030 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001031 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001032 if (ret == 0 && mem->mm_node) {
1033 mem->placement = cur_flags;
1034 return 0;
1035 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001036 if (ret == -ERESTARTSYS)
1037 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001038 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001039 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001040 return ret;
1041}
1042EXPORT_SYMBOL(ttm_bo_mem_space);
1043
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001045 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001046 bool interruptible, bool no_wait_reserve,
1047 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001048{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049 int ret = 0;
1050 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001051 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001053 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001054
1055 /*
1056 * FIXME: It's possible to pipeline buffer moves.
1057 * Have the driver move function wait for idle when necessary,
1058 * instead of doing it here.
1059 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001060 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001061 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001062 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001063 if (ret)
1064 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001065 mem.num_pages = bo->num_pages;
1066 mem.size = mem.num_pages << PAGE_SHIFT;
1067 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001068 mem.bus.io_reserved_vm = false;
1069 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001070 /*
1071 * Determine where to move the buffer.
1072 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001073 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074 if (ret)
1075 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001076 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001078 if (ret && mem.mm_node)
1079 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001080 return ret;
1081}
1082
Jerome Glisseca262a9992009-12-08 15:33:32 +01001083static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001084 struct ttm_mem_reg *mem)
1085{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001086 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001087
Ben Skeggsd961db72010-08-05 10:48:18 +10001088 if (mem->mm_node && placement->lpfn != 0 &&
1089 (mem->start < placement->fpfn ||
1090 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001091 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092
Jerome Glisseca262a9992009-12-08 15:33:32 +01001093 for (i = 0; i < placement->num_placement; i++) {
1094 if ((placement->placement[i] & mem->placement &
1095 TTM_PL_MASK_CACHING) &&
1096 (placement->placement[i] & mem->placement &
1097 TTM_PL_MASK_MEM))
1098 return i;
1099 }
1100 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101}
1102
Jerome Glisse09855ac2009-12-10 17:16:27 +01001103int ttm_bo_validate(struct ttm_buffer_object *bo,
1104 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001105 bool interruptible, bool no_wait_reserve,
1106 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001107{
1108 int ret;
1109
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001110 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001111 /* Check that range is valid */
1112 if (placement->lpfn || placement->fpfn)
1113 if (placement->fpfn > placement->lpfn ||
1114 (placement->lpfn - placement->fpfn) < bo->num_pages)
1115 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001116 /*
1117 * Check whether we need to move buffer.
1118 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001119 ret = ttm_bo_mem_compat(placement, &bo->mem);
1120 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001121 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001122 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001123 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001124 } else {
1125 /*
1126 * Use the access and other non-mapping-related flag bits from
1127 * the compatible memory placement flags to the active flags
1128 */
1129 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1130 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001131 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001132 /*
1133 * We might need to add a TTM.
1134 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1136 ret = ttm_bo_add_ttm(bo, true);
1137 if (ret)
1138 return ret;
1139 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001140 return 0;
1141}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001142EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143
Jerome Glisse09855ac2009-12-10 17:16:27 +01001144int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1145 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001147 BUG_ON((placement->fpfn || placement->lpfn) &&
1148 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001150 return 0;
1151}
1152
Jerome Glisse09855ac2009-12-10 17:16:27 +01001153int ttm_bo_init(struct ttm_bo_device *bdev,
1154 struct ttm_buffer_object *bo,
1155 unsigned long size,
1156 enum ttm_bo_type type,
1157 struct ttm_placement *placement,
1158 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001159 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001160 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001161 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001162 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001163 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001164{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001165 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001166 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001167 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1168
1169 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1170 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001171 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001172 if (destroy)
1173 (*destroy)(bo);
1174 else
1175 kfree(bo);
1176 return -ENOMEM;
1177 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1180 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001181 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001182 if (destroy)
1183 (*destroy)(bo);
1184 else
1185 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001186 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187 return -EINVAL;
1188 }
1189 bo->destroy = destroy;
1190
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001191 kref_init(&bo->kref);
1192 kref_init(&bo->list_kref);
1193 atomic_set(&bo->cpu_writers, 0);
1194 atomic_set(&bo->reserved, 1);
1195 init_waitqueue_head(&bo->event_queue);
1196 INIT_LIST_HEAD(&bo->lru);
1197 INIT_LIST_HEAD(&bo->ddestroy);
1198 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001199 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001201 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001202 bo->type = type;
1203 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001204 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001205 bo->mem.mem_type = TTM_PL_SYSTEM;
1206 bo->mem.num_pages = bo->num_pages;
1207 bo->mem.mm_node = NULL;
1208 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001209 bo->mem.bus.io_reserved_vm = false;
1210 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001211 bo->priv_flags = 0;
1212 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1213 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001214 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001216 bo->sg = sg;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001217 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001218
Jerome Glisse09855ac2009-12-10 17:16:27 +01001219 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001220 if (unlikely(ret != 0))
1221 goto out_err;
1222
1223 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001224 * For ttm_bo_type_device buffers, allocate
1225 * address space from the device.
1226 */
Dave Airlie129b78b2012-04-02 11:46:06 +01001227 if (bo->type == ttm_bo_type_device ||
1228 bo->type == ttm_bo_type_sg) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001229 ret = ttm_bo_setup_vm(bo);
1230 if (ret)
1231 goto out_err;
1232 }
1233
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001234 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001235 if (ret)
1236 goto out_err;
1237
1238 ttm_bo_unreserve(bo);
1239 return 0;
1240
1241out_err:
1242 ttm_bo_unreserve(bo);
1243 ttm_bo_unref(&bo);
1244
1245 return ret;
1246}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001247EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001249size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1250 unsigned long bo_size,
1251 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001252{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001253 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1254 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001255
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001256 size += ttm_round_pot(struct_size);
1257 size += PAGE_ALIGN(npages * sizeof(void *));
1258 size += ttm_round_pot(sizeof(struct ttm_tt));
1259 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001260}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001261EXPORT_SYMBOL(ttm_bo_acc_size);
1262
1263size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1264 unsigned long bo_size,
1265 unsigned struct_size)
1266{
1267 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1268 size_t size = 0;
1269
1270 size += ttm_round_pot(struct_size);
1271 size += PAGE_ALIGN(npages * sizeof(void *));
1272 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1273 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1274 return size;
1275}
1276EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001277
Jerome Glisse09855ac2009-12-10 17:16:27 +01001278int ttm_bo_create(struct ttm_bo_device *bdev,
1279 unsigned long size,
1280 enum ttm_bo_type type,
1281 struct ttm_placement *placement,
1282 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001283 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001284 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001285 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001286{
1287 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001288 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001289 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001290
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001292 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001293 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001294
Thomas Hellstroma393c732012-06-12 13:28:42 +02001295 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001296 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001297 interruptible, persistent_swap_storage, acc_size,
1298 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299 if (likely(ret == 0))
1300 *p_bo = bo;
1301
1302 return ret;
1303}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001304EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001305
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001307 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001308{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001309 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001310 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001311 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001312
1313 /*
1314 * Can't use standard list traversal since we're unlocking.
1315 */
1316
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001317 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001318 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001319 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001320 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001321 if (ret) {
1322 if (allow_errors) {
1323 return ret;
1324 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001325 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001326 }
1327 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001328 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001329 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001330 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001331 return 0;
1332}
1333
1334int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1335{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001336 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001337 int ret = -EINVAL;
1338
1339 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001340 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001341 return ret;
1342 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001343 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001344
1345 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001346 pr_err("Trying to take down uninitialized memory manager type %u\n",
1347 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001348 return ret;
1349 }
1350
1351 man->use_type = false;
1352 man->has_type = false;
1353
1354 ret = 0;
1355 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001356 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001357
Ben Skeggsd961db72010-08-05 10:48:18 +10001358 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001359 }
1360
1361 return ret;
1362}
1363EXPORT_SYMBOL(ttm_bo_clean_mm);
1364
1365int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1366{
1367 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1368
1369 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001370 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001371 return -EINVAL;
1372 }
1373
1374 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001375 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001376 return 0;
1377 }
1378
Jerome Glisseca262a9992009-12-08 15:33:32 +01001379 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001380}
1381EXPORT_SYMBOL(ttm_bo_evict_mm);
1382
1383int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001384 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001385{
1386 int ret = -EINVAL;
1387 struct ttm_mem_type_manager *man;
1388
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001389 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001390 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001391 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001392 man->io_reserve_fastpath = true;
1393 man->use_io_reserve_lru = false;
1394 mutex_init(&man->io_reserve_mutex);
1395 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001396
1397 ret = bdev->driver->init_mem_type(bdev, type, man);
1398 if (ret)
1399 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001400 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001401
1402 ret = 0;
1403 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001404 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001405 if (ret)
1406 return ret;
1407 }
1408 man->has_type = true;
1409 man->use_type = true;
1410 man->size = p_size;
1411
1412 INIT_LIST_HEAD(&man->lru);
1413
1414 return 0;
1415}
1416EXPORT_SYMBOL(ttm_bo_init_mm);
1417
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001418static void ttm_bo_global_kobj_release(struct kobject *kobj)
1419{
1420 struct ttm_bo_global *glob =
1421 container_of(kobj, struct ttm_bo_global, kobj);
1422
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001423 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1424 __free_page(glob->dummy_read_page);
1425 kfree(glob);
1426}
1427
Dave Airlieba4420c2010-03-09 10:56:52 +10001428void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001429{
1430 struct ttm_bo_global *glob = ref->object;
1431
1432 kobject_del(&glob->kobj);
1433 kobject_put(&glob->kobj);
1434}
1435EXPORT_SYMBOL(ttm_bo_global_release);
1436
Dave Airlieba4420c2010-03-09 10:56:52 +10001437int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001438{
1439 struct ttm_bo_global_ref *bo_ref =
1440 container_of(ref, struct ttm_bo_global_ref, ref);
1441 struct ttm_bo_global *glob = ref->object;
1442 int ret;
1443
1444 mutex_init(&glob->device_list_mutex);
1445 spin_lock_init(&glob->lru_lock);
1446 glob->mem_glob = bo_ref->mem_glob;
1447 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1448
1449 if (unlikely(glob->dummy_read_page == NULL)) {
1450 ret = -ENOMEM;
1451 goto out_no_drp;
1452 }
1453
1454 INIT_LIST_HEAD(&glob->swap_lru);
1455 INIT_LIST_HEAD(&glob->device_list);
1456
1457 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1458 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1459 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001460 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001461 goto out_no_shrink;
1462 }
1463
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001464 atomic_set(&glob->bo_count, 0);
1465
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001466 ret = kobject_init_and_add(
1467 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001468 if (unlikely(ret != 0))
1469 kobject_put(&glob->kobj);
1470 return ret;
1471out_no_shrink:
1472 __free_page(glob->dummy_read_page);
1473out_no_drp:
1474 kfree(glob);
1475 return ret;
1476}
1477EXPORT_SYMBOL(ttm_bo_global_init);
1478
1479
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001480int ttm_bo_device_release(struct ttm_bo_device *bdev)
1481{
1482 int ret = 0;
1483 unsigned i = TTM_NUM_MEM_TYPES;
1484 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001485 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001486
1487 while (i--) {
1488 man = &bdev->man[i];
1489 if (man->has_type) {
1490 man->use_type = false;
1491 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1492 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001493 pr_err("DRM memory manager type %d is not clean\n",
1494 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001495 }
1496 man->has_type = false;
1497 }
1498 }
1499
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001500 mutex_lock(&glob->device_list_mutex);
1501 list_del(&bdev->device_list);
1502 mutex_unlock(&glob->device_list_mutex);
1503
Tejun Heof094cfc2010-12-24 15:59:06 +01001504 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001505
1506 while (ttm_bo_delayed_delete(bdev, true))
1507 ;
1508
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001509 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510 if (list_empty(&bdev->ddestroy))
1511 TTM_DEBUG("Delayed destroy list was clean\n");
1512
1513 if (list_empty(&bdev->man[0].lru))
1514 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001515 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001516
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1518 write_lock(&bdev->vm_lock);
1519 drm_mm_takedown(&bdev->addr_space_mm);
1520 write_unlock(&bdev->vm_lock);
1521
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522 return ret;
1523}
1524EXPORT_SYMBOL(ttm_bo_device_release);
1525
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001527 struct ttm_bo_global *glob,
1528 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001529 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001530 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531{
1532 int ret = -EINVAL;
1533
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001535 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536
1537 memset(bdev->man, 0, sizeof(bdev->man));
1538
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001539 /*
1540 * Initialize the system memory buffer type.
1541 * Other types need to be driver / IOCTL initialized.
1542 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001543 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001544 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001545 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001546
1547 bdev->addr_space_rb = RB_ROOT;
1548 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1549 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001550 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001551
1552 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001554 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001555 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001556 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001557 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001558 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001559 mutex_lock(&glob->device_list_mutex);
1560 list_add_tail(&bdev->device_list, &glob->device_list);
1561 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001562
1563 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001564out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001565 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001566out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001567 return ret;
1568}
1569EXPORT_SYMBOL(ttm_bo_device_init);
1570
1571/*
1572 * buffer object vm functions.
1573 */
1574
1575bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1576{
1577 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1578
1579 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1580 if (mem->mem_type == TTM_PL_SYSTEM)
1581 return false;
1582
1583 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1584 return false;
1585
1586 if (mem->placement & TTM_PL_FLAG_CACHED)
1587 return false;
1588 }
1589 return true;
1590}
1591
Thomas Hellstromeba67092010-11-11 09:41:57 +01001592void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001593{
1594 struct ttm_bo_device *bdev = bo->bdev;
1595 loff_t offset = (loff_t) bo->addr_space_offset;
1596 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1597
1598 if (!bdev->dev_mapping)
1599 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001600 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001601 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001602}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001603
1604void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1605{
1606 struct ttm_bo_device *bdev = bo->bdev;
1607 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1608
1609 ttm_mem_io_lock(man, false);
1610 ttm_bo_unmap_virtual_locked(bo);
1611 ttm_mem_io_unlock(man);
1612}
1613
1614
Dave Airliee024e112009-06-24 09:48:08 +10001615EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001616
1617static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1618{
1619 struct ttm_bo_device *bdev = bo->bdev;
1620 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1621 struct rb_node *parent = NULL;
1622 struct ttm_buffer_object *cur_bo;
1623 unsigned long offset = bo->vm_node->start;
1624 unsigned long cur_offset;
1625
1626 while (*cur) {
1627 parent = *cur;
1628 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1629 cur_offset = cur_bo->vm_node->start;
1630 if (offset < cur_offset)
1631 cur = &parent->rb_left;
1632 else if (offset > cur_offset)
1633 cur = &parent->rb_right;
1634 else
1635 BUG();
1636 }
1637
1638 rb_link_node(&bo->vm_rb, parent, cur);
1639 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1640}
1641
1642/**
1643 * ttm_bo_setup_vm:
1644 *
1645 * @bo: the buffer to allocate address space for
1646 *
1647 * Allocate address space in the drm device so that applications
1648 * can mmap the buffer and access the contents. This only
1649 * applies to ttm_bo_type_device objects as others are not
1650 * placed in the drm device address space.
1651 */
1652
1653static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1654{
1655 struct ttm_bo_device *bdev = bo->bdev;
1656 int ret;
1657
1658retry_pre_get:
1659 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1660 if (unlikely(ret != 0))
1661 return ret;
1662
1663 write_lock(&bdev->vm_lock);
1664 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1665 bo->mem.num_pages, 0, 0);
1666
1667 if (unlikely(bo->vm_node == NULL)) {
1668 ret = -ENOMEM;
1669 goto out_unlock;
1670 }
1671
1672 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1673 bo->mem.num_pages, 0);
1674
1675 if (unlikely(bo->vm_node == NULL)) {
1676 write_unlock(&bdev->vm_lock);
1677 goto retry_pre_get;
1678 }
1679
1680 ttm_bo_vm_insert_rb(bo);
1681 write_unlock(&bdev->vm_lock);
1682 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1683
1684 return 0;
1685out_unlock:
1686 write_unlock(&bdev->vm_lock);
1687 return ret;
1688}
1689
1690int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001691 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001692{
1693 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001694 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001695 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001696 int ret = 0;
1697
Dave Airlie1717c0e2011-10-27 18:28:37 +02001698 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001699 return 0;
1700
Dave Airlie1717c0e2011-10-27 18:28:37 +02001701 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001702
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001703 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001704 void *tmp_obj = bo->sync_obj;
1705 bo->sync_obj = NULL;
1706 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1707 spin_unlock(&bdev->fence_lock);
1708 driver->sync_obj_unref(&tmp_obj);
1709 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001710 continue;
1711 }
1712
1713 if (no_wait)
1714 return -EBUSY;
1715
Dave Airlie1717c0e2011-10-27 18:28:37 +02001716 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001717 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001718 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001719 lazy, interruptible);
1720 if (unlikely(ret != 0)) {
1721 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001722 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001723 return ret;
1724 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001725 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001726 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001727 void *tmp_obj = bo->sync_obj;
1728 bo->sync_obj = NULL;
1729 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1730 &bo->priv_flags);
1731 spin_unlock(&bdev->fence_lock);
1732 driver->sync_obj_unref(&sync_obj);
1733 driver->sync_obj_unref(&tmp_obj);
1734 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001735 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001736 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001737 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001738 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001739 }
1740 }
1741 return 0;
1742}
1743EXPORT_SYMBOL(ttm_bo_wait);
1744
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001745int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1746{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001747 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001748 int ret = 0;
1749
1750 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001751 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001752 */
1753
1754 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1755 if (unlikely(ret != 0))
1756 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001757 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001758 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001759 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001760 if (likely(ret == 0))
1761 atomic_inc(&bo->cpu_writers);
1762 ttm_bo_unreserve(bo);
1763 return ret;
1764}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001765EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001766
1767void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1768{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001769 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001770}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001771EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001772
1773/**
1774 * A buffer object shrink method that tries to swap out the first
1775 * buffer object on the bo_global::swap_lru list.
1776 */
1777
1778static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1779{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001780 struct ttm_bo_global *glob =
1781 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001782 struct ttm_buffer_object *bo;
1783 int ret = -EBUSY;
1784 int put_count;
1785 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1786
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001787 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001788 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001789 if (unlikely(list_empty(&glob->swap_lru))) {
1790 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001791 return -EBUSY;
1792 }
1793
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001794 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001795 struct ttm_buffer_object, swap);
1796 kref_get(&bo->list_kref);
1797
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001798 if (!list_empty(&bo->ddestroy)) {
1799 spin_unlock(&glob->lru_lock);
1800 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1801 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma8ff3ee2012-06-01 15:39:11 +02001802 spin_lock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001803 continue;
1804 }
1805
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001806 /**
1807 * Reserve buffer. Since we unlock while sleeping, we need
1808 * to re-check that nobody removed us from the swap-list while
1809 * we slept.
1810 */
1811
1812 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1813 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001814 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001815 ttm_bo_wait_unreserved(bo, false);
1816 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001817 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001818 }
1819 }
1820
1821 BUG_ON(ret != 0);
1822 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001823 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001824
Dave Airlied6ea8882010-11-22 13:24:40 +10001825 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001826
1827 /**
1828 * Wait for GPU, then move to system cached.
1829 */
1830
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001831 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001832 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001833 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001834
1835 if (unlikely(ret != 0))
1836 goto out;
1837
1838 if ((bo->mem.placement & swap_placement) != swap_placement) {
1839 struct ttm_mem_reg evict_mem;
1840
1841 evict_mem = bo->mem;
1842 evict_mem.mm_node = NULL;
1843 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1844 evict_mem.mem_type = TTM_PL_SYSTEM;
1845
1846 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001847 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001848 if (unlikely(ret != 0))
1849 goto out;
1850 }
1851
1852 ttm_bo_unmap_virtual(bo);
1853
1854 /**
1855 * Swap out. Buffer will be swapped in again as soon as
1856 * anyone tries to access a ttm page.
1857 */
1858
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001859 if (bo->bdev->driver->swap_notify)
1860 bo->bdev->driver->swap_notify(bo);
1861
Jan Engelhardt5df23972011-04-04 01:25:18 +02001862 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001863out:
1864
1865 /**
1866 *
1867 * Unreserve without putting on LRU to avoid swapping out an
1868 * already swapped buffer.
1869 */
1870
1871 atomic_set(&bo->reserved, 0);
1872 wake_up_all(&bo->event_queue);
1873 kref_put(&bo->list_kref, ttm_bo_release_list);
1874 return ret;
1875}
1876
1877void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1878{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001879 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001880 ;
1881}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001882EXPORT_SYMBOL(ttm_bo_swapout_all);