blob: 6059771d506e37af8f2b17642e4f6e00adc437a2 [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);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000491 wake_up_all(&bo->event_queue);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200492
493 /*
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000494 * Since the final reference to this bo may not be dropped by
495 * the current task we have to put a memory barrier here to make
496 * sure the changes done in this function are always visible.
497 *
498 * This function only needs protection against the final kref_put.
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200499 */
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000500 smp_mb__before_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200501}
502
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200503static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200504{
505 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200506 struct ttm_bo_global *glob = bo->glob;
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100507 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000508 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200509 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200510 int ret;
511
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100512 spin_lock(&glob->lru_lock);
513 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
514
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000515 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200516 (void) ttm_bo_wait(bo, false, false, true);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100517 if (!ret && !bo->sync_obj) {
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000518 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200519 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200520
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200521 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200522 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200523
Dave Airlied6ea8882010-11-22 13:24:40 +1000524 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200525
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200526 return;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200527 }
Thomas Hellstromaa123262010-11-02 13:21:47 +0000528 if (bo->sync_obj)
529 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100530 spin_unlock(&bdev->fence_lock);
531
532 if (!ret) {
533 atomic_set(&bo->reserved, 0);
534 wake_up_all(&bo->event_queue);
535 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200536
537 kref_get(&bo->list_kref);
538 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
539 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200540
Thomas Hellstromaa123262010-11-02 13:21:47 +0000541 if (sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +0000542 driver->sync_obj_flush(sync_obj);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000543 driver->sync_obj_unref(&sync_obj);
544 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200545 schedule_delayed_work(&bdev->wq,
546 ((HZ / 100) < 1) ? 1 : HZ / 100);
547}
548
549/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000550 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200551 * If bo idle, remove from delayed- and lru lists, and unref.
552 * If not idle, do nothing.
553 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000554 * Must be called with lru_lock and reservation held, this function
555 * will drop both before returning.
556 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200557 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200558 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
559 */
560
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000561static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
562 bool interruptible,
563 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200564{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000565 struct ttm_bo_device *bdev = bo->bdev;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000566 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200567 struct ttm_bo_global *glob = bo->glob;
568 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000569 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200570
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000571 spin_lock(&bdev->fence_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000572 ret = ttm_bo_wait(bo, false, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200573
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000574 if (ret && !no_wait_gpu) {
575 void *sync_obj;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200576
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000577 /*
578 * Take a reference to the fence and unreserve,
579 * at this point the buffer should be dead, so
580 * no new sync objects can be attached.
581 */
582 sync_obj = driver->sync_obj_ref(&bo->sync_obj);
583 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100584
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200585 atomic_set(&bo->reserved, 0);
586 wake_up_all(&bo->event_queue);
587 spin_unlock(&glob->lru_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000588
589 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
590 driver->sync_obj_unref(&sync_obj);
591 if (ret)
592 return ret;
593
594 /*
595 * remove sync_obj with ttm_bo_wait, the wait should be
596 * finished, and no new wait object should have been added.
597 */
598 spin_lock(&bdev->fence_lock);
599 ret = ttm_bo_wait(bo, false, false, true);
600 WARN_ON(ret);
601 spin_unlock(&bdev->fence_lock);
602 if (ret)
603 return ret;
604
605 spin_lock(&glob->lru_lock);
606 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
607
608 /*
609 * We raced, and lost, someone else holds the reservation now,
610 * and is probably busy in ttm_bo_cleanup_memtype_use.
611 *
612 * Even if it's not the case, because we finished waiting any
613 * delayed destruction would succeed, so just return success
614 * here.
615 */
616 if (ret) {
617 spin_unlock(&glob->lru_lock);
618 return 0;
619 }
620 } else
621 spin_unlock(&bdev->fence_lock);
622
623 if (ret || unlikely(list_empty(&bo->ddestroy))) {
624 atomic_set(&bo->reserved, 0);
625 wake_up_all(&bo->event_queue);
626 spin_unlock(&glob->lru_lock);
627 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200628 }
629
630 put_count = ttm_bo_del_from_lru(bo);
631 list_del_init(&bo->ddestroy);
632 ++put_count;
633
634 spin_unlock(&glob->lru_lock);
635 ttm_bo_cleanup_memtype_use(bo);
636
Dave Airlied6ea8882010-11-22 13:24:40 +1000637 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200638
639 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200640}
641
642/**
643 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
644 * encountered buffers.
645 */
646
647static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
648{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200649 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100650 struct ttm_buffer_object *entry = NULL;
651 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200652
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200653 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100654 if (list_empty(&bdev->ddestroy))
655 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100657 entry = list_first_entry(&bdev->ddestroy,
658 struct ttm_buffer_object, ddestroy);
659 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200660
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100661 for (;;) {
662 struct ttm_buffer_object *nentry = NULL;
663
664 if (entry->ddestroy.next != &bdev->ddestroy) {
665 nentry = list_first_entry(&entry->ddestroy,
666 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200667 kref_get(&nentry->list_kref);
668 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200669
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000670 ret = ttm_bo_reserve_locked(entry, false, !remove_all, false, 0);
671 if (!ret)
672 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
673 !remove_all);
674 else
675 spin_unlock(&glob->lru_lock);
676
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200677 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100678 entry = nentry;
679
680 if (ret || !entry)
681 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200682
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200683 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100684 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200685 break;
686 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200687
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100688out_unlock:
689 spin_unlock(&glob->lru_lock);
690out:
691 if (entry)
692 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200693 return ret;
694}
695
696static void ttm_bo_delayed_workqueue(struct work_struct *work)
697{
698 struct ttm_bo_device *bdev =
699 container_of(work, struct ttm_bo_device, wq.work);
700
701 if (ttm_bo_delayed_delete(bdev, false)) {
702 schedule_delayed_work(&bdev->wq,
703 ((HZ / 100) < 1) ? 1 : HZ / 100);
704 }
705}
706
707static void ttm_bo_release(struct kref *kref)
708{
709 struct ttm_buffer_object *bo =
710 container_of(kref, struct ttm_buffer_object, kref);
711 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100712 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200713
Thomas Hellstrom82fe50b2012-11-21 14:53:21 +0000714 write_lock(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200715 if (likely(bo->vm_node != NULL)) {
716 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
717 drm_mm_put_block(bo->vm_node);
718 bo->vm_node = NULL;
719 }
720 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100721 ttm_mem_io_lock(man, false);
722 ttm_mem_io_free_vm(bo);
723 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200724 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200725 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200726}
727
728void ttm_bo_unref(struct ttm_buffer_object **p_bo)
729{
730 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200731
732 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200733 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200734}
735EXPORT_SYMBOL(ttm_bo_unref);
736
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400737int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
738{
739 return cancel_delayed_work_sync(&bdev->wq);
740}
741EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
742
743void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
744{
745 if (resched)
746 schedule_delayed_work(&bdev->wq,
747 ((HZ / 100) < 1) ? 1 : HZ / 100);
748}
749EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
750
Jerome Glisseca262a9992009-12-08 15:33:32 +0100751static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000752 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200753{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200754 struct ttm_bo_device *bdev = bo->bdev;
755 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100756 struct ttm_placement placement;
757 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200758
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000759 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200760 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000761 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200762
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200763 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100764 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700765 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200766 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200767 goto out;
768 }
769
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200770 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200771
772 evict_mem = bo->mem;
773 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100774 evict_mem.bus.io_reserved_vm = false;
775 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200776
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100777 placement.fpfn = 0;
778 placement.lpfn = 0;
779 placement.num_placement = 0;
780 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100781 bdev->driver->evict_flags(bo, &placement);
782 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000783 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200784 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100785 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700786 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
787 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100788 ttm_bo_mem_space_debug(bo, &placement);
789 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200790 goto out;
791 }
792
793 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000794 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200795 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100796 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700797 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000798 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200799 goto out;
800 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200801 bo->evicted = true;
802out:
803 return ret;
804}
805
Jerome Glisseca262a9992009-12-08 15:33:32 +0100806static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
807 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000808 bool interruptible, bool no_wait_reserve,
809 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100810{
811 struct ttm_bo_global *glob = bdev->glob;
812 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
813 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000814 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100815
816 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000817 list_for_each_entry(bo, &man->lru, lru) {
818 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
819 if (!ret)
820 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100821 }
822
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000823 if (ret) {
824 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000825 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200826 }
827
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000828 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100829
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000830 if (!list_empty(&bo->ddestroy)) {
831 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
832 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100833 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000834 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100835 }
836
837 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100838 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100839
840 BUG_ON(ret != 0);
841
Dave Airlied6ea8882010-11-22 13:24:40 +1000842 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100843
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000844 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100845 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100846
Jerome Glisseca262a9992009-12-08 15:33:32 +0100847 kref_put(&bo->list_kref, ttm_bo_release_list);
848 return ret;
849}
850
Ben Skeggs42311ff2010-08-04 12:07:08 +1000851void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
852{
Ben Skeggsd961db72010-08-05 10:48:18 +1000853 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000854
Ben Skeggsd961db72010-08-05 10:48:18 +1000855 if (mem->mm_node)
856 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000857}
858EXPORT_SYMBOL(ttm_bo_mem_put);
859
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200860/**
861 * Repeatedly evict memory from the LRU for @mem_type until we create enough
862 * space, or we've evicted everything and there isn't enough space.
863 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100864static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
865 uint32_t mem_type,
866 struct ttm_placement *placement,
867 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000868 bool interruptible,
869 bool no_wait_reserve,
870 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200871{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100872 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200873 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200874 int ret;
875
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200876 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000877 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200878 if (unlikely(ret != 0))
879 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000880 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100881 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100882 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000883 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100884 if (unlikely(ret != 0))
885 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200886 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000887 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200888 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200889 mem->mem_type = mem_type;
890 return 0;
891}
892
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200893static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
894 uint32_t cur_placement,
895 uint32_t proposed_placement)
896{
897 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
898 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
899
900 /**
901 * Keep current caching if possible.
902 */
903
904 if ((cur_placement & caching) != 0)
905 result |= (cur_placement & caching);
906 else if ((man->default_caching & caching) != 0)
907 result |= man->default_caching;
908 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
909 result |= TTM_PL_FLAG_CACHED;
910 else if ((TTM_PL_FLAG_WC & caching) != 0)
911 result |= TTM_PL_FLAG_WC;
912 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
913 result |= TTM_PL_FLAG_UNCACHED;
914
915 return result;
916}
917
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200918static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200919 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200920 uint32_t proposed_placement,
921 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922{
923 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
924
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200925 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926 return false;
927
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200928 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200930
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200931 cur_flags |= (proposed_placement & man->available_caching);
932
933 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200934 return true;
935}
936
937/**
938 * Creates space for memory region @mem according to its type.
939 *
940 * This function first searches for free space in compatible memory types in
941 * the priority order defined by the driver. If free space isn't found, then
942 * ttm_bo_mem_force_space is attempted in priority order to evict and find
943 * space.
944 */
945int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100946 struct ttm_placement *placement,
947 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000948 bool interruptible, bool no_wait_reserve,
949 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200950{
951 struct ttm_bo_device *bdev = bo->bdev;
952 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953 uint32_t mem_type = TTM_PL_SYSTEM;
954 uint32_t cur_flags = 0;
955 bool type_found = false;
956 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100957 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100958 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200959
960 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000961 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100962 ret = ttm_mem_type_from_flags(placement->placement[i],
963 &mem_type);
964 if (ret)
965 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200966 man = &bdev->man[mem_type];
967
968 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100969 mem_type,
970 placement->placement[i],
971 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200972
973 if (!type_ok)
974 continue;
975
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200976 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
977 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100978 /*
979 * Use the access and other non-mapping-related flag bits from
980 * the memory placement flags to the current flags
981 */
982 ttm_flag_masked(&cur_flags, placement->placement[i],
983 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200984
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200985 if (mem_type == TTM_PL_SYSTEM)
986 break;
987
988 if (man->has_type && man->use_type) {
989 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000990 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100991 if (unlikely(ret))
992 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200993 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000994 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200995 break;
996 }
997
Ben Skeggsd961db72010-08-05 10:48:18 +1000998 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200999 mem->mem_type = mem_type;
1000 mem->placement = cur_flags;
1001 return 0;
1002 }
1003
1004 if (!type_found)
1005 return -EINVAL;
1006
Dave Airlieb6637522009-12-14 14:51:35 +10001007 for (i = 0; i < placement->num_busy_placement; ++i) {
1008 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001009 &mem_type);
1010 if (ret)
1011 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001012 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 if (!man->has_type)
1014 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001016 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001017 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001018 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019 continue;
1020
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001021 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1022 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001023 /*
1024 * Use the access and other non-mapping-related flag bits from
1025 * the memory placement flags to the current flags
1026 */
Dave Airlieb6637522009-12-14 14:51:35 +10001027 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001028 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001029
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001030
1031 if (mem_type == TTM_PL_SYSTEM) {
1032 mem->mem_type = mem_type;
1033 mem->placement = cur_flags;
1034 mem->mm_node = NULL;
1035 return 0;
1036 }
1037
Jerome Glisseca262a9992009-12-08 15:33:32 +01001038 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001039 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001040 if (ret == 0 && mem->mm_node) {
1041 mem->placement = cur_flags;
1042 return 0;
1043 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001044 if (ret == -ERESTARTSYS)
1045 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001047 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001048 return ret;
1049}
1050EXPORT_SYMBOL(ttm_bo_mem_space);
1051
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001053 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001054 bool interruptible, bool no_wait_reserve,
1055 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001056{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001057 int ret = 0;
1058 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001059 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001061 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001062
1063 /*
1064 * FIXME: It's possible to pipeline buffer moves.
1065 * Have the driver move function wait for idle when necessary,
1066 * instead of doing it here.
1067 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001068 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001069 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001070 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001071 if (ret)
1072 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001073 mem.num_pages = bo->num_pages;
1074 mem.size = mem.num_pages << PAGE_SHIFT;
1075 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001076 mem.bus.io_reserved_vm = false;
1077 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001078 /*
1079 * Determine where to move the buffer.
1080 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001081 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082 if (ret)
1083 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001084 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001085out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001086 if (ret && mem.mm_node)
1087 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001088 return ret;
1089}
1090
Jerome Glisseca262a9992009-12-08 15:33:32 +01001091static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092 struct ttm_mem_reg *mem)
1093{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001094 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001095
Ben Skeggsd961db72010-08-05 10:48:18 +10001096 if (mem->mm_node && placement->lpfn != 0 &&
1097 (mem->start < placement->fpfn ||
1098 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001099 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001100
Jerome Glisseca262a9992009-12-08 15:33:32 +01001101 for (i = 0; i < placement->num_placement; i++) {
1102 if ((placement->placement[i] & mem->placement &
1103 TTM_PL_MASK_CACHING) &&
1104 (placement->placement[i] & mem->placement &
1105 TTM_PL_MASK_MEM))
1106 return i;
1107 }
1108 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001109}
1110
Jerome Glisse09855ac2009-12-10 17:16:27 +01001111int ttm_bo_validate(struct ttm_buffer_object *bo,
1112 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001113 bool interruptible, bool no_wait_reserve,
1114 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001115{
1116 int ret;
1117
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001118 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001119 /* Check that range is valid */
1120 if (placement->lpfn || placement->fpfn)
1121 if (placement->fpfn > placement->lpfn ||
1122 (placement->lpfn - placement->fpfn) < bo->num_pages)
1123 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001124 /*
1125 * Check whether we need to move buffer.
1126 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001127 ret = ttm_bo_mem_compat(placement, &bo->mem);
1128 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001129 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001130 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001131 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001132 } else {
1133 /*
1134 * Use the access and other non-mapping-related flag bits from
1135 * the compatible memory placement flags to the active flags
1136 */
1137 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1138 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001139 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001140 /*
1141 * We might need to add a TTM.
1142 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1144 ret = ttm_bo_add_ttm(bo, true);
1145 if (ret)
1146 return ret;
1147 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001148 return 0;
1149}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001150EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001151
Jerome Glisse09855ac2009-12-10 17:16:27 +01001152int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1153 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001154{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001155 BUG_ON((placement->fpfn || placement->lpfn) &&
1156 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001157
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158 return 0;
1159}
1160
Jerome Glisse09855ac2009-12-10 17:16:27 +01001161int ttm_bo_init(struct ttm_bo_device *bdev,
1162 struct ttm_buffer_object *bo,
1163 unsigned long size,
1164 enum ttm_bo_type type,
1165 struct ttm_placement *placement,
1166 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001167 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001168 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001169 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001170 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001171 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001172{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001173 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001174 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001175 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1176
1177 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1178 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001179 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001180 if (destroy)
1181 (*destroy)(bo);
1182 else
1183 kfree(bo);
1184 return -ENOMEM;
1185 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001186
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1188 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001189 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001190 if (destroy)
1191 (*destroy)(bo);
1192 else
1193 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001194 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001195 return -EINVAL;
1196 }
1197 bo->destroy = destroy;
1198
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199 kref_init(&bo->kref);
1200 kref_init(&bo->list_kref);
1201 atomic_set(&bo->cpu_writers, 0);
1202 atomic_set(&bo->reserved, 1);
1203 init_waitqueue_head(&bo->event_queue);
1204 INIT_LIST_HEAD(&bo->lru);
1205 INIT_LIST_HEAD(&bo->ddestroy);
1206 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001207 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001208 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001209 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001210 bo->type = type;
1211 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001212 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001213 bo->mem.mem_type = TTM_PL_SYSTEM;
1214 bo->mem.num_pages = bo->num_pages;
1215 bo->mem.mm_node = NULL;
1216 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001217 bo->mem.bus.io_reserved_vm = false;
1218 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219 bo->priv_flags = 0;
1220 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1221 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001222 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001224 bo->sg = sg;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001225 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001226
Jerome Glisse09855ac2009-12-10 17:16:27 +01001227 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228 if (unlikely(ret != 0))
1229 goto out_err;
1230
1231 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001232 * For ttm_bo_type_device buffers, allocate
1233 * address space from the device.
1234 */
Dave Airlie129b78b2012-04-02 11:46:06 +01001235 if (bo->type == ttm_bo_type_device ||
1236 bo->type == ttm_bo_type_sg) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001237 ret = ttm_bo_setup_vm(bo);
1238 if (ret)
1239 goto out_err;
1240 }
1241
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001242 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001243 if (ret)
1244 goto out_err;
1245
1246 ttm_bo_unreserve(bo);
1247 return 0;
1248
1249out_err:
1250 ttm_bo_unreserve(bo);
1251 ttm_bo_unref(&bo);
1252
1253 return ret;
1254}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001255EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001257size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1258 unsigned long bo_size,
1259 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001260{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001261 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1262 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001263
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001264 size += ttm_round_pot(struct_size);
1265 size += PAGE_ALIGN(npages * sizeof(void *));
1266 size += ttm_round_pot(sizeof(struct ttm_tt));
1267 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001268}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001269EXPORT_SYMBOL(ttm_bo_acc_size);
1270
1271size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1272 unsigned long bo_size,
1273 unsigned struct_size)
1274{
1275 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1276 size_t size = 0;
1277
1278 size += ttm_round_pot(struct_size);
1279 size += PAGE_ALIGN(npages * sizeof(void *));
1280 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1281 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1282 return size;
1283}
1284EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001285
Jerome Glisse09855ac2009-12-10 17:16:27 +01001286int ttm_bo_create(struct ttm_bo_device *bdev,
1287 unsigned long size,
1288 enum ttm_bo_type type,
1289 struct ttm_placement *placement,
1290 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001291 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001292 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001293 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001294{
1295 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001296 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001297 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001300 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001301 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302
Thomas Hellstroma393c732012-06-12 13:28:42 +02001303 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001304 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001305 interruptible, persistent_swap_storage, acc_size,
1306 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001307 if (likely(ret == 0))
1308 *p_bo = bo;
1309
1310 return ret;
1311}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001312EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001313
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001314static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001315 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001316{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001317 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001318 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001319 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001320
1321 /*
1322 * Can't use standard list traversal since we're unlocking.
1323 */
1324
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001325 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001326 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001327 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001328 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001329 if (ret) {
1330 if (allow_errors) {
1331 return ret;
1332 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001333 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001334 }
1335 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001336 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001337 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001338 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001339 return 0;
1340}
1341
1342int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1343{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001344 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001345 int ret = -EINVAL;
1346
1347 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001348 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001349 return ret;
1350 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001351 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001352
1353 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001354 pr_err("Trying to take down uninitialized memory manager type %u\n",
1355 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001356 return ret;
1357 }
1358
1359 man->use_type = false;
1360 man->has_type = false;
1361
1362 ret = 0;
1363 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001364 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001365
Ben Skeggsd961db72010-08-05 10:48:18 +10001366 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001367 }
1368
1369 return ret;
1370}
1371EXPORT_SYMBOL(ttm_bo_clean_mm);
1372
1373int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1374{
1375 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1376
1377 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001378 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001379 return -EINVAL;
1380 }
1381
1382 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001383 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001384 return 0;
1385 }
1386
Jerome Glisseca262a9992009-12-08 15:33:32 +01001387 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001388}
1389EXPORT_SYMBOL(ttm_bo_evict_mm);
1390
1391int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001392 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001393{
1394 int ret = -EINVAL;
1395 struct ttm_mem_type_manager *man;
1396
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001397 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001398 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001399 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001400 man->io_reserve_fastpath = true;
1401 man->use_io_reserve_lru = false;
1402 mutex_init(&man->io_reserve_mutex);
1403 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001404
1405 ret = bdev->driver->init_mem_type(bdev, type, man);
1406 if (ret)
1407 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001408 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001409
1410 ret = 0;
1411 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001412 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001413 if (ret)
1414 return ret;
1415 }
1416 man->has_type = true;
1417 man->use_type = true;
1418 man->size = p_size;
1419
1420 INIT_LIST_HEAD(&man->lru);
1421
1422 return 0;
1423}
1424EXPORT_SYMBOL(ttm_bo_init_mm);
1425
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001426static void ttm_bo_global_kobj_release(struct kobject *kobj)
1427{
1428 struct ttm_bo_global *glob =
1429 container_of(kobj, struct ttm_bo_global, kobj);
1430
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001431 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1432 __free_page(glob->dummy_read_page);
1433 kfree(glob);
1434}
1435
Dave Airlieba4420c2010-03-09 10:56:52 +10001436void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001437{
1438 struct ttm_bo_global *glob = ref->object;
1439
1440 kobject_del(&glob->kobj);
1441 kobject_put(&glob->kobj);
1442}
1443EXPORT_SYMBOL(ttm_bo_global_release);
1444
Dave Airlieba4420c2010-03-09 10:56:52 +10001445int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001446{
1447 struct ttm_bo_global_ref *bo_ref =
1448 container_of(ref, struct ttm_bo_global_ref, ref);
1449 struct ttm_bo_global *glob = ref->object;
1450 int ret;
1451
1452 mutex_init(&glob->device_list_mutex);
1453 spin_lock_init(&glob->lru_lock);
1454 glob->mem_glob = bo_ref->mem_glob;
1455 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1456
1457 if (unlikely(glob->dummy_read_page == NULL)) {
1458 ret = -ENOMEM;
1459 goto out_no_drp;
1460 }
1461
1462 INIT_LIST_HEAD(&glob->swap_lru);
1463 INIT_LIST_HEAD(&glob->device_list);
1464
1465 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1466 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1467 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001468 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001469 goto out_no_shrink;
1470 }
1471
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001472 atomic_set(&glob->bo_count, 0);
1473
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001474 ret = kobject_init_and_add(
1475 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001476 if (unlikely(ret != 0))
1477 kobject_put(&glob->kobj);
1478 return ret;
1479out_no_shrink:
1480 __free_page(glob->dummy_read_page);
1481out_no_drp:
1482 kfree(glob);
1483 return ret;
1484}
1485EXPORT_SYMBOL(ttm_bo_global_init);
1486
1487
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488int ttm_bo_device_release(struct ttm_bo_device *bdev)
1489{
1490 int ret = 0;
1491 unsigned i = TTM_NUM_MEM_TYPES;
1492 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001493 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001494
1495 while (i--) {
1496 man = &bdev->man[i];
1497 if (man->has_type) {
1498 man->use_type = false;
1499 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1500 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001501 pr_err("DRM memory manager type %d is not clean\n",
1502 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001503 }
1504 man->has_type = false;
1505 }
1506 }
1507
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001508 mutex_lock(&glob->device_list_mutex);
1509 list_del(&bdev->device_list);
1510 mutex_unlock(&glob->device_list_mutex);
1511
Tejun Heof094cfc2010-12-24 15:59:06 +01001512 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001513
1514 while (ttm_bo_delayed_delete(bdev, true))
1515 ;
1516
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001517 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001518 if (list_empty(&bdev->ddestroy))
1519 TTM_DEBUG("Delayed destroy list was clean\n");
1520
1521 if (list_empty(&bdev->man[0].lru))
1522 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001523 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001524
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1526 write_lock(&bdev->vm_lock);
1527 drm_mm_takedown(&bdev->addr_space_mm);
1528 write_unlock(&bdev->vm_lock);
1529
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001530 return ret;
1531}
1532EXPORT_SYMBOL(ttm_bo_device_release);
1533
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001535 struct ttm_bo_global *glob,
1536 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001537 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001538 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001539{
1540 int ret = -EINVAL;
1541
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001542 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001543 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001544
1545 memset(bdev->man, 0, sizeof(bdev->man));
1546
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001547 /*
1548 * Initialize the system memory buffer type.
1549 * Other types need to be driver / IOCTL initialized.
1550 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001551 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001552 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001553 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001554
1555 bdev->addr_space_rb = RB_ROOT;
1556 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1557 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001558 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001559
1560 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001561 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001562 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001563 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001564 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001565 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001566 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001567 mutex_lock(&glob->device_list_mutex);
1568 list_add_tail(&bdev->device_list, &glob->device_list);
1569 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001570
1571 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001572out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001573 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001574out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575 return ret;
1576}
1577EXPORT_SYMBOL(ttm_bo_device_init);
1578
1579/*
1580 * buffer object vm functions.
1581 */
1582
1583bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1584{
1585 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1586
1587 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1588 if (mem->mem_type == TTM_PL_SYSTEM)
1589 return false;
1590
1591 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1592 return false;
1593
1594 if (mem->placement & TTM_PL_FLAG_CACHED)
1595 return false;
1596 }
1597 return true;
1598}
1599
Thomas Hellstromeba67092010-11-11 09:41:57 +01001600void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001601{
1602 struct ttm_bo_device *bdev = bo->bdev;
1603 loff_t offset = (loff_t) bo->addr_space_offset;
1604 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1605
1606 if (!bdev->dev_mapping)
1607 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001608 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001609 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001610}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001611
1612void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1613{
1614 struct ttm_bo_device *bdev = bo->bdev;
1615 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1616
1617 ttm_mem_io_lock(man, false);
1618 ttm_bo_unmap_virtual_locked(bo);
1619 ttm_mem_io_unlock(man);
1620}
1621
1622
Dave Airliee024e112009-06-24 09:48:08 +10001623EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001624
1625static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1626{
1627 struct ttm_bo_device *bdev = bo->bdev;
1628 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1629 struct rb_node *parent = NULL;
1630 struct ttm_buffer_object *cur_bo;
1631 unsigned long offset = bo->vm_node->start;
1632 unsigned long cur_offset;
1633
1634 while (*cur) {
1635 parent = *cur;
1636 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1637 cur_offset = cur_bo->vm_node->start;
1638 if (offset < cur_offset)
1639 cur = &parent->rb_left;
1640 else if (offset > cur_offset)
1641 cur = &parent->rb_right;
1642 else
1643 BUG();
1644 }
1645
1646 rb_link_node(&bo->vm_rb, parent, cur);
1647 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1648}
1649
1650/**
1651 * ttm_bo_setup_vm:
1652 *
1653 * @bo: the buffer to allocate address space for
1654 *
1655 * Allocate address space in the drm device so that applications
1656 * can mmap the buffer and access the contents. This only
1657 * applies to ttm_bo_type_device objects as others are not
1658 * placed in the drm device address space.
1659 */
1660
1661static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1662{
1663 struct ttm_bo_device *bdev = bo->bdev;
1664 int ret;
1665
1666retry_pre_get:
1667 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1668 if (unlikely(ret != 0))
1669 return ret;
1670
1671 write_lock(&bdev->vm_lock);
1672 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1673 bo->mem.num_pages, 0, 0);
1674
1675 if (unlikely(bo->vm_node == NULL)) {
1676 ret = -ENOMEM;
1677 goto out_unlock;
1678 }
1679
1680 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1681 bo->mem.num_pages, 0);
1682
1683 if (unlikely(bo->vm_node == NULL)) {
1684 write_unlock(&bdev->vm_lock);
1685 goto retry_pre_get;
1686 }
1687
1688 ttm_bo_vm_insert_rb(bo);
1689 write_unlock(&bdev->vm_lock);
1690 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1691
1692 return 0;
1693out_unlock:
1694 write_unlock(&bdev->vm_lock);
1695 return ret;
1696}
1697
1698int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001699 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001700{
1701 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001702 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001703 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001704 int ret = 0;
1705
Dave Airlie1717c0e2011-10-27 18:28:37 +02001706 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001707 return 0;
1708
Dave Airlie1717c0e2011-10-27 18:28:37 +02001709 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001710
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001711 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001712 void *tmp_obj = bo->sync_obj;
1713 bo->sync_obj = NULL;
1714 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1715 spin_unlock(&bdev->fence_lock);
1716 driver->sync_obj_unref(&tmp_obj);
1717 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001718 continue;
1719 }
1720
1721 if (no_wait)
1722 return -EBUSY;
1723
Dave Airlie1717c0e2011-10-27 18:28:37 +02001724 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001725 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001726 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001727 lazy, interruptible);
1728 if (unlikely(ret != 0)) {
1729 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001730 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001731 return ret;
1732 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001733 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001734 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001735 void *tmp_obj = bo->sync_obj;
1736 bo->sync_obj = NULL;
1737 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1738 &bo->priv_flags);
1739 spin_unlock(&bdev->fence_lock);
1740 driver->sync_obj_unref(&sync_obj);
1741 driver->sync_obj_unref(&tmp_obj);
1742 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001743 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001744 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001745 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001746 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001747 }
1748 }
1749 return 0;
1750}
1751EXPORT_SYMBOL(ttm_bo_wait);
1752
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001753int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1754{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001755 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001756 int ret = 0;
1757
1758 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001759 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001760 */
1761
1762 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1763 if (unlikely(ret != 0))
1764 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001765 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001766 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001767 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001768 if (likely(ret == 0))
1769 atomic_inc(&bo->cpu_writers);
1770 ttm_bo_unreserve(bo);
1771 return ret;
1772}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001773EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001774
1775void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1776{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001777 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001778}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001779EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001780
1781/**
1782 * A buffer object shrink method that tries to swap out the first
1783 * buffer object on the bo_global::swap_lru list.
1784 */
1785
1786static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1787{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001788 struct ttm_bo_global *glob =
1789 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001790 struct ttm_buffer_object *bo;
1791 int ret = -EBUSY;
1792 int put_count;
1793 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1794
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001795 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001796 list_for_each_entry(bo, &glob->swap_lru, swap) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001797 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001798 if (!ret)
1799 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800 }
1801
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001802 if (ret) {
1803 spin_unlock(&glob->lru_lock);
1804 return ret;
1805 }
1806
1807 kref_get(&bo->list_kref);
1808
1809 if (!list_empty(&bo->ddestroy)) {
1810 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1811 kref_put(&bo->list_kref, ttm_bo_release_list);
1812 return ret;
1813 }
1814
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001815 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001816 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001817
Dave Airlied6ea8882010-11-22 13:24:40 +10001818 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001819
1820 /**
1821 * Wait for GPU, then move to system cached.
1822 */
1823
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001824 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001825 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001826 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001827
1828 if (unlikely(ret != 0))
1829 goto out;
1830
1831 if ((bo->mem.placement & swap_placement) != swap_placement) {
1832 struct ttm_mem_reg evict_mem;
1833
1834 evict_mem = bo->mem;
1835 evict_mem.mm_node = NULL;
1836 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1837 evict_mem.mem_type = TTM_PL_SYSTEM;
1838
1839 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001840 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001841 if (unlikely(ret != 0))
1842 goto out;
1843 }
1844
1845 ttm_bo_unmap_virtual(bo);
1846
1847 /**
1848 * Swap out. Buffer will be swapped in again as soon as
1849 * anyone tries to access a ttm page.
1850 */
1851
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001852 if (bo->bdev->driver->swap_notify)
1853 bo->bdev->driver->swap_notify(bo);
1854
Jan Engelhardt5df23972011-04-04 01:25:18 +02001855 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001856out:
1857
1858 /**
1859 *
1860 * Unreserve without putting on LRU to avoid swapping out an
1861 * already swapped buffer.
1862 */
1863
1864 atomic_set(&bo->reserved, 0);
1865 wake_up_all(&bo->event_queue);
1866 kref_put(&bo->list_kref, ttm_bo_release_list);
1867 return ret;
1868}
1869
1870void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1871{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001872 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001873 ;
1874}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001875EXPORT_SYMBOL(ttm_bo_swapout_all);