blob: 5f61f133b4193a0f0bbea747dd32f599dc66fb7e [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
699 if (likely(bo->vm_node != NULL)) {
700 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
701 drm_mm_put_block(bo->vm_node);
702 bo->vm_node = NULL;
703 }
704 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100705 ttm_mem_io_lock(man, false);
706 ttm_mem_io_free_vm(bo);
707 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200708 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200709 kref_put(&bo->list_kref, ttm_bo_release_list);
710 write_lock(&bdev->vm_lock);
711}
712
713void ttm_bo_unref(struct ttm_buffer_object **p_bo)
714{
715 struct ttm_buffer_object *bo = *p_bo;
716 struct ttm_bo_device *bdev = bo->bdev;
717
718 *p_bo = NULL;
719 write_lock(&bdev->vm_lock);
720 kref_put(&bo->kref, ttm_bo_release);
721 write_unlock(&bdev->vm_lock);
722}
723EXPORT_SYMBOL(ttm_bo_unref);
724
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400725int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
726{
727 return cancel_delayed_work_sync(&bdev->wq);
728}
729EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
730
731void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
732{
733 if (resched)
734 schedule_delayed_work(&bdev->wq,
735 ((HZ / 100) < 1) ? 1 : HZ / 100);
736}
737EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
738
Jerome Glisseca262a9992009-12-08 15:33:32 +0100739static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000740 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200741{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200742 struct ttm_bo_device *bdev = bo->bdev;
743 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100744 struct ttm_placement placement;
745 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200746
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000747 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200748 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000749 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200750
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200751 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100752 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700753 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200754 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200755 goto out;
756 }
757
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200758 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200759
760 evict_mem = bo->mem;
761 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100762 evict_mem.bus.io_reserved_vm = false;
763 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200764
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100765 placement.fpfn = 0;
766 placement.lpfn = 0;
767 placement.num_placement = 0;
768 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100769 bdev->driver->evict_flags(bo, &placement);
770 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000771 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200772 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100773 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700774 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
775 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100776 ttm_bo_mem_space_debug(bo, &placement);
777 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200778 goto out;
779 }
780
781 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000782 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200783 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100784 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700785 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000786 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200787 goto out;
788 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200789 bo->evicted = true;
790out:
791 return ret;
792}
793
Jerome Glisseca262a9992009-12-08 15:33:32 +0100794static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
795 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000796 bool interruptible, bool no_wait_reserve,
797 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100798{
799 struct ttm_bo_global *glob = bdev->glob;
800 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
801 struct ttm_buffer_object *bo;
802 int ret, put_count = 0;
803
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100804retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100805 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100806 if (list_empty(&man->lru)) {
807 spin_unlock(&glob->lru_lock);
808 return -EBUSY;
809 }
810
Jerome Glisseca262a9992009-12-08 15:33:32 +0100811 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
812 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100813
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200814 if (!list_empty(&bo->ddestroy)) {
815 spin_unlock(&glob->lru_lock);
816 ret = ttm_bo_cleanup_refs(bo, interruptible,
817 no_wait_reserve, no_wait_gpu);
818 kref_put(&bo->list_kref, ttm_bo_release_list);
819
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000820 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200821 }
822
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000823 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100824
825 if (unlikely(ret == -EBUSY)) {
826 spin_unlock(&glob->lru_lock);
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000827 if (likely(!no_wait_reserve))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100828 ret = ttm_bo_wait_unreserved(bo, interruptible);
829
830 kref_put(&bo->list_kref, ttm_bo_release_list);
831
832 /**
833 * We *need* to retry after releasing the lru lock.
834 */
835
836 if (unlikely(ret != 0))
837 return ret;
838 goto retry;
839 }
840
841 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100842 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100843
844 BUG_ON(ret != 0);
845
Dave Airlied6ea8882010-11-22 13:24:40 +1000846 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100847
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000848 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100849 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100850
Jerome Glisseca262a9992009-12-08 15:33:32 +0100851 kref_put(&bo->list_kref, ttm_bo_release_list);
852 return ret;
853}
854
Ben Skeggs42311ff2010-08-04 12:07:08 +1000855void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
856{
Ben Skeggsd961db72010-08-05 10:48:18 +1000857 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000858
Ben Skeggsd961db72010-08-05 10:48:18 +1000859 if (mem->mm_node)
860 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000861}
862EXPORT_SYMBOL(ttm_bo_mem_put);
863
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200864/**
865 * Repeatedly evict memory from the LRU for @mem_type until we create enough
866 * space, or we've evicted everything and there isn't enough space.
867 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100868static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
869 uint32_t mem_type,
870 struct ttm_placement *placement,
871 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000872 bool interruptible,
873 bool no_wait_reserve,
874 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200875{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100876 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200878 int ret;
879
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000881 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882 if (unlikely(ret != 0))
883 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000884 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100885 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100886 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000887 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100888 if (unlikely(ret != 0))
889 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200890 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000891 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200892 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200893 mem->mem_type = mem_type;
894 return 0;
895}
896
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200897static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
898 uint32_t cur_placement,
899 uint32_t proposed_placement)
900{
901 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
902 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
903
904 /**
905 * Keep current caching if possible.
906 */
907
908 if ((cur_placement & caching) != 0)
909 result |= (cur_placement & caching);
910 else if ((man->default_caching & caching) != 0)
911 result |= man->default_caching;
912 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
913 result |= TTM_PL_FLAG_CACHED;
914 else if ((TTM_PL_FLAG_WC & caching) != 0)
915 result |= TTM_PL_FLAG_WC;
916 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
917 result |= TTM_PL_FLAG_UNCACHED;
918
919 return result;
920}
921
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200924 uint32_t proposed_placement,
925 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926{
927 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
928
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200929 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200930 return false;
931
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200932 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200933 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200934
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200935 cur_flags |= (proposed_placement & man->available_caching);
936
937 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938 return true;
939}
940
941/**
942 * Creates space for memory region @mem according to its type.
943 *
944 * This function first searches for free space in compatible memory types in
945 * the priority order defined by the driver. If free space isn't found, then
946 * ttm_bo_mem_force_space is attempted in priority order to evict and find
947 * space.
948 */
949int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100950 struct ttm_placement *placement,
951 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000952 bool interruptible, bool no_wait_reserve,
953 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200954{
955 struct ttm_bo_device *bdev = bo->bdev;
956 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200957 uint32_t mem_type = TTM_PL_SYSTEM;
958 uint32_t cur_flags = 0;
959 bool type_found = false;
960 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100961 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100962 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200963
964 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000965 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100966 ret = ttm_mem_type_from_flags(placement->placement[i],
967 &mem_type);
968 if (ret)
969 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970 man = &bdev->man[mem_type];
971
972 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100973 mem_type,
974 placement->placement[i],
975 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976
977 if (!type_ok)
978 continue;
979
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200980 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
981 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100982 /*
983 * Use the access and other non-mapping-related flag bits from
984 * the memory placement flags to the current flags
985 */
986 ttm_flag_masked(&cur_flags, placement->placement[i],
987 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200988
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989 if (mem_type == TTM_PL_SYSTEM)
990 break;
991
992 if (man->has_type && man->use_type) {
993 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000994 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100995 if (unlikely(ret))
996 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000998 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200999 break;
1000 }
1001
Ben Skeggsd961db72010-08-05 10:48:18 +10001002 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001003 mem->mem_type = mem_type;
1004 mem->placement = cur_flags;
1005 return 0;
1006 }
1007
1008 if (!type_found)
1009 return -EINVAL;
1010
Dave Airlieb6637522009-12-14 14:51:35 +10001011 for (i = 0; i < placement->num_busy_placement; ++i) {
1012 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001013 &mem_type);
1014 if (ret)
1015 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001016 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001017 if (!man->has_type)
1018 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001020 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001021 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001022 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001023 continue;
1024
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001025 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1026 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001027 /*
1028 * Use the access and other non-mapping-related flag bits from
1029 * the memory placement flags to the current flags
1030 */
Dave Airlieb6637522009-12-14 14:51:35 +10001031 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001032 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001033
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001034
1035 if (mem_type == TTM_PL_SYSTEM) {
1036 mem->mem_type = mem_type;
1037 mem->placement = cur_flags;
1038 mem->mm_node = NULL;
1039 return 0;
1040 }
1041
Jerome Glisseca262a9992009-12-08 15:33:32 +01001042 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001043 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044 if (ret == 0 && mem->mm_node) {
1045 mem->placement = cur_flags;
1046 return 0;
1047 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001048 if (ret == -ERESTARTSYS)
1049 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001051 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052 return ret;
1053}
1054EXPORT_SYMBOL(ttm_bo_mem_space);
1055
1056int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1057{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001058 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1059 return -EBUSY;
1060
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001061 return wait_event_interruptible(bo->event_queue,
1062 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001063}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001064EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001065
1066int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001067 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001068 bool interruptible, bool no_wait_reserve,
1069 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001070{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001071 int ret = 0;
1072 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001073 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001075 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001076
1077 /*
1078 * FIXME: It's possible to pipeline buffer moves.
1079 * Have the driver move function wait for idle when necessary,
1080 * instead of doing it here.
1081 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001082 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001083 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001084 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001085 if (ret)
1086 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001087 mem.num_pages = bo->num_pages;
1088 mem.size = mem.num_pages << PAGE_SHIFT;
1089 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001090 mem.bus.io_reserved_vm = false;
1091 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092 /*
1093 * Determine where to move the buffer.
1094 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001095 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001096 if (ret)
1097 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001098 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001099out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001100 if (ret && mem.mm_node)
1101 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102 return ret;
1103}
1104
Jerome Glisseca262a9992009-12-08 15:33:32 +01001105static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106 struct ttm_mem_reg *mem)
1107{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001108 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001109
Ben Skeggsd961db72010-08-05 10:48:18 +10001110 if (mem->mm_node && placement->lpfn != 0 &&
1111 (mem->start < placement->fpfn ||
1112 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001113 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001114
Jerome Glisseca262a9992009-12-08 15:33:32 +01001115 for (i = 0; i < placement->num_placement; i++) {
1116 if ((placement->placement[i] & mem->placement &
1117 TTM_PL_MASK_CACHING) &&
1118 (placement->placement[i] & mem->placement &
1119 TTM_PL_MASK_MEM))
1120 return i;
1121 }
1122 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001123}
1124
Jerome Glisse09855ac2009-12-10 17:16:27 +01001125int ttm_bo_validate(struct ttm_buffer_object *bo,
1126 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001127 bool interruptible, bool no_wait_reserve,
1128 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001129{
1130 int ret;
1131
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001132 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001133 /* Check that range is valid */
1134 if (placement->lpfn || placement->fpfn)
1135 if (placement->fpfn > placement->lpfn ||
1136 (placement->lpfn - placement->fpfn) < bo->num_pages)
1137 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001138 /*
1139 * Check whether we need to move buffer.
1140 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001141 ret = ttm_bo_mem_compat(placement, &bo->mem);
1142 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001143 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001144 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001145 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001146 } else {
1147 /*
1148 * Use the access and other non-mapping-related flag bits from
1149 * the compatible memory placement flags to the active flags
1150 */
1151 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1152 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001154 /*
1155 * We might need to add a TTM.
1156 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001157 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1158 ret = ttm_bo_add_ttm(bo, true);
1159 if (ret)
1160 return ret;
1161 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001162 return 0;
1163}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001164EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001165
Jerome Glisse09855ac2009-12-10 17:16:27 +01001166int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1167 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001168{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001169 BUG_ON((placement->fpfn || placement->lpfn) &&
1170 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001171
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001172 return 0;
1173}
1174
Jerome Glisse09855ac2009-12-10 17:16:27 +01001175int ttm_bo_init(struct ttm_bo_device *bdev,
1176 struct ttm_buffer_object *bo,
1177 unsigned long size,
1178 enum ttm_bo_type type,
1179 struct ttm_placement *placement,
1180 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001181 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001182 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001183 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001184 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001185 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001186{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001187 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001188 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001189 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1190
1191 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1192 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001193 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001194 if (destroy)
1195 (*destroy)(bo);
1196 else
1197 kfree(bo);
1198 return -ENOMEM;
1199 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001201 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1202 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001203 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001204 if (destroy)
1205 (*destroy)(bo);
1206 else
1207 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001208 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209 return -EINVAL;
1210 }
1211 bo->destroy = destroy;
1212
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001213 kref_init(&bo->kref);
1214 kref_init(&bo->list_kref);
1215 atomic_set(&bo->cpu_writers, 0);
1216 atomic_set(&bo->reserved, 1);
1217 init_waitqueue_head(&bo->event_queue);
1218 INIT_LIST_HEAD(&bo->lru);
1219 INIT_LIST_HEAD(&bo->ddestroy);
1220 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001221 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001222 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001223 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001224 bo->type = type;
1225 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001226 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001227 bo->mem.mem_type = TTM_PL_SYSTEM;
1228 bo->mem.num_pages = bo->num_pages;
1229 bo->mem.mm_node = NULL;
1230 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001231 bo->mem.bus.io_reserved_vm = false;
1232 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001233 bo->priv_flags = 0;
1234 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1235 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001236 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001237 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001238 bo->sg = sg;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001239 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001240
Jerome Glisse09855ac2009-12-10 17:16:27 +01001241 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001242 if (unlikely(ret != 0))
1243 goto out_err;
1244
1245 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001246 * For ttm_bo_type_device buffers, allocate
1247 * address space from the device.
1248 */
Dave Airlie129b78b2012-04-02 11:46:06 +01001249 if (bo->type == ttm_bo_type_device ||
1250 bo->type == ttm_bo_type_sg) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251 ret = ttm_bo_setup_vm(bo);
1252 if (ret)
1253 goto out_err;
1254 }
1255
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001256 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001257 if (ret)
1258 goto out_err;
1259
1260 ttm_bo_unreserve(bo);
1261 return 0;
1262
1263out_err:
1264 ttm_bo_unreserve(bo);
1265 ttm_bo_unref(&bo);
1266
1267 return ret;
1268}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001269EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001270
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001271size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1272 unsigned long bo_size,
1273 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001274{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001275 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1276 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001277
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001278 size += ttm_round_pot(struct_size);
1279 size += PAGE_ALIGN(npages * sizeof(void *));
1280 size += ttm_round_pot(sizeof(struct ttm_tt));
1281 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001282}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001283EXPORT_SYMBOL(ttm_bo_acc_size);
1284
1285size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1286 unsigned long bo_size,
1287 unsigned struct_size)
1288{
1289 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1290 size_t size = 0;
1291
1292 size += ttm_round_pot(struct_size);
1293 size += PAGE_ALIGN(npages * sizeof(void *));
1294 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1295 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1296 return size;
1297}
1298EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299
Jerome Glisse09855ac2009-12-10 17:16:27 +01001300int ttm_bo_create(struct ttm_bo_device *bdev,
1301 unsigned long size,
1302 enum ttm_bo_type type,
1303 struct ttm_placement *placement,
1304 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001305 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001306 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001307 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001308{
1309 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001310 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001311 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001312
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001313 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001314 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001315 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001316
Thomas Hellstroma393c732012-06-12 13:28:42 +02001317 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001318 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001319 interruptible, persistent_swap_storage, acc_size,
1320 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001321 if (likely(ret == 0))
1322 *p_bo = bo;
1323
1324 return ret;
1325}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001326EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001327
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001328static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001329 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001330{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001331 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001332 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001333 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001334
1335 /*
1336 * Can't use standard list traversal since we're unlocking.
1337 */
1338
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001339 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001340 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001341 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001342 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001343 if (ret) {
1344 if (allow_errors) {
1345 return ret;
1346 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001347 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001348 }
1349 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001350 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001351 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001352 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001353 return 0;
1354}
1355
1356int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1357{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001358 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001359 int ret = -EINVAL;
1360
1361 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001362 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001363 return ret;
1364 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001365 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001366
1367 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001368 pr_err("Trying to take down uninitialized memory manager type %u\n",
1369 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001370 return ret;
1371 }
1372
1373 man->use_type = false;
1374 man->has_type = false;
1375
1376 ret = 0;
1377 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001378 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001379
Ben Skeggsd961db72010-08-05 10:48:18 +10001380 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001381 }
1382
1383 return ret;
1384}
1385EXPORT_SYMBOL(ttm_bo_clean_mm);
1386
1387int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1388{
1389 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1390
1391 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001392 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001393 return -EINVAL;
1394 }
1395
1396 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001397 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001398 return 0;
1399 }
1400
Jerome Glisseca262a9992009-12-08 15:33:32 +01001401 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001402}
1403EXPORT_SYMBOL(ttm_bo_evict_mm);
1404
1405int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001406 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001407{
1408 int ret = -EINVAL;
1409 struct ttm_mem_type_manager *man;
1410
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001411 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001412 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001413 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001414 man->io_reserve_fastpath = true;
1415 man->use_io_reserve_lru = false;
1416 mutex_init(&man->io_reserve_mutex);
1417 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001418
1419 ret = bdev->driver->init_mem_type(bdev, type, man);
1420 if (ret)
1421 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001422 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001423
1424 ret = 0;
1425 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001426 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001427 if (ret)
1428 return ret;
1429 }
1430 man->has_type = true;
1431 man->use_type = true;
1432 man->size = p_size;
1433
1434 INIT_LIST_HEAD(&man->lru);
1435
1436 return 0;
1437}
1438EXPORT_SYMBOL(ttm_bo_init_mm);
1439
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001440static void ttm_bo_global_kobj_release(struct kobject *kobj)
1441{
1442 struct ttm_bo_global *glob =
1443 container_of(kobj, struct ttm_bo_global, kobj);
1444
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001445 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1446 __free_page(glob->dummy_read_page);
1447 kfree(glob);
1448}
1449
Dave Airlieba4420c2010-03-09 10:56:52 +10001450void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001451{
1452 struct ttm_bo_global *glob = ref->object;
1453
1454 kobject_del(&glob->kobj);
1455 kobject_put(&glob->kobj);
1456}
1457EXPORT_SYMBOL(ttm_bo_global_release);
1458
Dave Airlieba4420c2010-03-09 10:56:52 +10001459int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001460{
1461 struct ttm_bo_global_ref *bo_ref =
1462 container_of(ref, struct ttm_bo_global_ref, ref);
1463 struct ttm_bo_global *glob = ref->object;
1464 int ret;
1465
1466 mutex_init(&glob->device_list_mutex);
1467 spin_lock_init(&glob->lru_lock);
1468 glob->mem_glob = bo_ref->mem_glob;
1469 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1470
1471 if (unlikely(glob->dummy_read_page == NULL)) {
1472 ret = -ENOMEM;
1473 goto out_no_drp;
1474 }
1475
1476 INIT_LIST_HEAD(&glob->swap_lru);
1477 INIT_LIST_HEAD(&glob->device_list);
1478
1479 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1480 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1481 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001482 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001483 goto out_no_shrink;
1484 }
1485
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001486 atomic_set(&glob->bo_count, 0);
1487
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001488 ret = kobject_init_and_add(
1489 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001490 if (unlikely(ret != 0))
1491 kobject_put(&glob->kobj);
1492 return ret;
1493out_no_shrink:
1494 __free_page(glob->dummy_read_page);
1495out_no_drp:
1496 kfree(glob);
1497 return ret;
1498}
1499EXPORT_SYMBOL(ttm_bo_global_init);
1500
1501
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001502int ttm_bo_device_release(struct ttm_bo_device *bdev)
1503{
1504 int ret = 0;
1505 unsigned i = TTM_NUM_MEM_TYPES;
1506 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001507 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001508
1509 while (i--) {
1510 man = &bdev->man[i];
1511 if (man->has_type) {
1512 man->use_type = false;
1513 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1514 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001515 pr_err("DRM memory manager type %d is not clean\n",
1516 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517 }
1518 man->has_type = false;
1519 }
1520 }
1521
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001522 mutex_lock(&glob->device_list_mutex);
1523 list_del(&bdev->device_list);
1524 mutex_unlock(&glob->device_list_mutex);
1525
Tejun Heof094cfc2010-12-24 15:59:06 +01001526 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001527
1528 while (ttm_bo_delayed_delete(bdev, true))
1529 ;
1530
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001531 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001532 if (list_empty(&bdev->ddestroy))
1533 TTM_DEBUG("Delayed destroy list was clean\n");
1534
1535 if (list_empty(&bdev->man[0].lru))
1536 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001537 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001539 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1540 write_lock(&bdev->vm_lock);
1541 drm_mm_takedown(&bdev->addr_space_mm);
1542 write_unlock(&bdev->vm_lock);
1543
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001544 return ret;
1545}
1546EXPORT_SYMBOL(ttm_bo_device_release);
1547
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001548int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001549 struct ttm_bo_global *glob,
1550 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001551 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001552 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553{
1554 int ret = -EINVAL;
1555
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001556 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001557 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001558
1559 memset(bdev->man, 0, sizeof(bdev->man));
1560
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001561 /*
1562 * Initialize the system memory buffer type.
1563 * Other types need to be driver / IOCTL initialized.
1564 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001565 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001566 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001567 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001568
1569 bdev->addr_space_rb = RB_ROOT;
1570 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1571 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001572 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001573
1574 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001576 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001577 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001578 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001579 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001580 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001581 mutex_lock(&glob->device_list_mutex);
1582 list_add_tail(&bdev->device_list, &glob->device_list);
1583 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001584
1585 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001586out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001587 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001588out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001589 return ret;
1590}
1591EXPORT_SYMBOL(ttm_bo_device_init);
1592
1593/*
1594 * buffer object vm functions.
1595 */
1596
1597bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1598{
1599 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1600
1601 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1602 if (mem->mem_type == TTM_PL_SYSTEM)
1603 return false;
1604
1605 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1606 return false;
1607
1608 if (mem->placement & TTM_PL_FLAG_CACHED)
1609 return false;
1610 }
1611 return true;
1612}
1613
Thomas Hellstromeba67092010-11-11 09:41:57 +01001614void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001615{
1616 struct ttm_bo_device *bdev = bo->bdev;
1617 loff_t offset = (loff_t) bo->addr_space_offset;
1618 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1619
1620 if (!bdev->dev_mapping)
1621 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001622 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001623 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001624}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001625
1626void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1627{
1628 struct ttm_bo_device *bdev = bo->bdev;
1629 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1630
1631 ttm_mem_io_lock(man, false);
1632 ttm_bo_unmap_virtual_locked(bo);
1633 ttm_mem_io_unlock(man);
1634}
1635
1636
Dave Airliee024e112009-06-24 09:48:08 +10001637EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001638
1639static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1640{
1641 struct ttm_bo_device *bdev = bo->bdev;
1642 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1643 struct rb_node *parent = NULL;
1644 struct ttm_buffer_object *cur_bo;
1645 unsigned long offset = bo->vm_node->start;
1646 unsigned long cur_offset;
1647
1648 while (*cur) {
1649 parent = *cur;
1650 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1651 cur_offset = cur_bo->vm_node->start;
1652 if (offset < cur_offset)
1653 cur = &parent->rb_left;
1654 else if (offset > cur_offset)
1655 cur = &parent->rb_right;
1656 else
1657 BUG();
1658 }
1659
1660 rb_link_node(&bo->vm_rb, parent, cur);
1661 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1662}
1663
1664/**
1665 * ttm_bo_setup_vm:
1666 *
1667 * @bo: the buffer to allocate address space for
1668 *
1669 * Allocate address space in the drm device so that applications
1670 * can mmap the buffer and access the contents. This only
1671 * applies to ttm_bo_type_device objects as others are not
1672 * placed in the drm device address space.
1673 */
1674
1675static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1676{
1677 struct ttm_bo_device *bdev = bo->bdev;
1678 int ret;
1679
1680retry_pre_get:
1681 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1682 if (unlikely(ret != 0))
1683 return ret;
1684
1685 write_lock(&bdev->vm_lock);
1686 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1687 bo->mem.num_pages, 0, 0);
1688
1689 if (unlikely(bo->vm_node == NULL)) {
1690 ret = -ENOMEM;
1691 goto out_unlock;
1692 }
1693
1694 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1695 bo->mem.num_pages, 0);
1696
1697 if (unlikely(bo->vm_node == NULL)) {
1698 write_unlock(&bdev->vm_lock);
1699 goto retry_pre_get;
1700 }
1701
1702 ttm_bo_vm_insert_rb(bo);
1703 write_unlock(&bdev->vm_lock);
1704 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1705
1706 return 0;
1707out_unlock:
1708 write_unlock(&bdev->vm_lock);
1709 return ret;
1710}
1711
1712int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001713 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001714{
1715 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001716 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001717 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001718 int ret = 0;
1719
Dave Airlie1717c0e2011-10-27 18:28:37 +02001720 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001721 return 0;
1722
Dave Airlie1717c0e2011-10-27 18:28:37 +02001723 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001724
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001725 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001726 void *tmp_obj = bo->sync_obj;
1727 bo->sync_obj = NULL;
1728 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1729 spin_unlock(&bdev->fence_lock);
1730 driver->sync_obj_unref(&tmp_obj);
1731 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001732 continue;
1733 }
1734
1735 if (no_wait)
1736 return -EBUSY;
1737
Dave Airlie1717c0e2011-10-27 18:28:37 +02001738 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001739 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001740 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001741 lazy, interruptible);
1742 if (unlikely(ret != 0)) {
1743 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001744 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001745 return ret;
1746 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001747 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001748 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001749 void *tmp_obj = bo->sync_obj;
1750 bo->sync_obj = NULL;
1751 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1752 &bo->priv_flags);
1753 spin_unlock(&bdev->fence_lock);
1754 driver->sync_obj_unref(&sync_obj);
1755 driver->sync_obj_unref(&tmp_obj);
1756 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001757 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001758 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001759 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001760 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001761 }
1762 }
1763 return 0;
1764}
1765EXPORT_SYMBOL(ttm_bo_wait);
1766
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001767int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1768{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001769 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001770 int ret = 0;
1771
1772 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001773 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001774 */
1775
1776 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1777 if (unlikely(ret != 0))
1778 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001779 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001780 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001781 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001782 if (likely(ret == 0))
1783 atomic_inc(&bo->cpu_writers);
1784 ttm_bo_unreserve(bo);
1785 return ret;
1786}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001787EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001788
1789void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1790{
1791 if (atomic_dec_and_test(&bo->cpu_writers))
1792 wake_up_all(&bo->event_queue);
1793}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001794EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001795
1796/**
1797 * A buffer object shrink method that tries to swap out the first
1798 * buffer object on the bo_global::swap_lru list.
1799 */
1800
1801static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1802{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001803 struct ttm_bo_global *glob =
1804 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001805 struct ttm_buffer_object *bo;
1806 int ret = -EBUSY;
1807 int put_count;
1808 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1809
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001810 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001811 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001812 if (unlikely(list_empty(&glob->swap_lru))) {
1813 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001814 return -EBUSY;
1815 }
1816
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001817 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001818 struct ttm_buffer_object, swap);
1819 kref_get(&bo->list_kref);
1820
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001821 if (!list_empty(&bo->ddestroy)) {
1822 spin_unlock(&glob->lru_lock);
1823 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1824 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma8ff3ee2012-06-01 15:39:11 +02001825 spin_lock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001826 continue;
1827 }
1828
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001829 /**
1830 * Reserve buffer. Since we unlock while sleeping, we need
1831 * to re-check that nobody removed us from the swap-list while
1832 * we slept.
1833 */
1834
1835 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1836 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001837 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001838 ttm_bo_wait_unreserved(bo, false);
1839 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001840 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001841 }
1842 }
1843
1844 BUG_ON(ret != 0);
1845 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001846 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001847
Dave Airlied6ea8882010-11-22 13:24:40 +10001848 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001849
1850 /**
1851 * Wait for GPU, then move to system cached.
1852 */
1853
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001854 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001855 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001856 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001857
1858 if (unlikely(ret != 0))
1859 goto out;
1860
1861 if ((bo->mem.placement & swap_placement) != swap_placement) {
1862 struct ttm_mem_reg evict_mem;
1863
1864 evict_mem = bo->mem;
1865 evict_mem.mm_node = NULL;
1866 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1867 evict_mem.mem_type = TTM_PL_SYSTEM;
1868
1869 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001870 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001871 if (unlikely(ret != 0))
1872 goto out;
1873 }
1874
1875 ttm_bo_unmap_virtual(bo);
1876
1877 /**
1878 * Swap out. Buffer will be swapped in again as soon as
1879 * anyone tries to access a ttm page.
1880 */
1881
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001882 if (bo->bdev->driver->swap_notify)
1883 bo->bdev->driver->swap_notify(bo);
1884
Jan Engelhardt5df23972011-04-04 01:25:18 +02001885 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001886out:
1887
1888 /**
1889 *
1890 * Unreserve without putting on LRU to avoid swapping out an
1891 * already swapped buffer.
1892 */
1893
1894 atomic_set(&bo->reserved, 0);
1895 wake_up_all(&bo->event_queue);
1896 kref_put(&bo->list_kref, ttm_bo_release_list);
1897 return ret;
1898}
1899
1900void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1901{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001902 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001903 ;
1904}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001905EXPORT_SYMBOL(ttm_bo_swapout_all);