blob: 9a479885bf5902a983400d56f3cf6b079a1c41ff [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;
814 int ret, put_count = 0;
815
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100816retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100817 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100818 if (list_empty(&man->lru)) {
819 spin_unlock(&glob->lru_lock);
820 return -EBUSY;
821 }
822
Jerome Glisseca262a9992009-12-08 15:33:32 +0100823 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
824 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100825
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200826 if (!list_empty(&bo->ddestroy)) {
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000827 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait_reserve, false, 0);
828 if (!ret)
829 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
830 no_wait_gpu);
831 else
832 spin_unlock(&glob->lru_lock);
833
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200834 kref_put(&bo->list_kref, ttm_bo_release_list);
835
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000836 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200837 }
838
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000839 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100840
841 if (unlikely(ret == -EBUSY)) {
842 spin_unlock(&glob->lru_lock);
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000843 if (likely(!no_wait_reserve))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100844 ret = ttm_bo_wait_unreserved(bo, interruptible);
845
846 kref_put(&bo->list_kref, ttm_bo_release_list);
847
848 /**
849 * We *need* to retry after releasing the lru lock.
850 */
851
852 if (unlikely(ret != 0))
853 return ret;
854 goto retry;
855 }
856
857 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100858 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100859
860 BUG_ON(ret != 0);
861
Dave Airlied6ea8882010-11-22 13:24:40 +1000862 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100863
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000864 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100865 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100866
Jerome Glisseca262a9992009-12-08 15:33:32 +0100867 kref_put(&bo->list_kref, ttm_bo_release_list);
868 return ret;
869}
870
Ben Skeggs42311ff2010-08-04 12:07:08 +1000871void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
872{
Ben Skeggsd961db72010-08-05 10:48:18 +1000873 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000874
Ben Skeggsd961db72010-08-05 10:48:18 +1000875 if (mem->mm_node)
876 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000877}
878EXPORT_SYMBOL(ttm_bo_mem_put);
879
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880/**
881 * Repeatedly evict memory from the LRU for @mem_type until we create enough
882 * space, or we've evicted everything and there isn't enough space.
883 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100884static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
885 uint32_t mem_type,
886 struct ttm_placement *placement,
887 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000888 bool interruptible,
889 bool no_wait_reserve,
890 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200891{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100892 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200893 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200894 int ret;
895
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200896 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000897 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898 if (unlikely(ret != 0))
899 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000900 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100901 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100902 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000903 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100904 if (unlikely(ret != 0))
905 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200906 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000907 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200908 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200909 mem->mem_type = mem_type;
910 return 0;
911}
912
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200913static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
914 uint32_t cur_placement,
915 uint32_t proposed_placement)
916{
917 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
918 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
919
920 /**
921 * Keep current caching if possible.
922 */
923
924 if ((cur_placement & caching) != 0)
925 result |= (cur_placement & caching);
926 else if ((man->default_caching & caching) != 0)
927 result |= man->default_caching;
928 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
929 result |= TTM_PL_FLAG_CACHED;
930 else if ((TTM_PL_FLAG_WC & caching) != 0)
931 result |= TTM_PL_FLAG_WC;
932 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
933 result |= TTM_PL_FLAG_UNCACHED;
934
935 return result;
936}
937
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200939 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200940 uint32_t proposed_placement,
941 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200942{
943 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
944
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200945 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200946 return false;
947
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200948 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200949 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200950
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200951 cur_flags |= (proposed_placement & man->available_caching);
952
953 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200954 return true;
955}
956
957/**
958 * Creates space for memory region @mem according to its type.
959 *
960 * This function first searches for free space in compatible memory types in
961 * the priority order defined by the driver. If free space isn't found, then
962 * ttm_bo_mem_force_space is attempted in priority order to evict and find
963 * space.
964 */
965int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100966 struct ttm_placement *placement,
967 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000968 bool interruptible, bool no_wait_reserve,
969 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970{
971 struct ttm_bo_device *bdev = bo->bdev;
972 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200973 uint32_t mem_type = TTM_PL_SYSTEM;
974 uint32_t cur_flags = 0;
975 bool type_found = false;
976 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100977 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100978 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200979
980 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000981 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100982 ret = ttm_mem_type_from_flags(placement->placement[i],
983 &mem_type);
984 if (ret)
985 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200986 man = &bdev->man[mem_type];
987
988 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100989 mem_type,
990 placement->placement[i],
991 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200992
993 if (!type_ok)
994 continue;
995
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200996 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
997 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100998 /*
999 * Use the access and other non-mapping-related flag bits from
1000 * the memory placement flags to the current flags
1001 */
1002 ttm_flag_masked(&cur_flags, placement->placement[i],
1003 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001004
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005 if (mem_type == TTM_PL_SYSTEM)
1006 break;
1007
1008 if (man->has_type && man->use_type) {
1009 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +10001010 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001011 if (unlikely(ret))
1012 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 }
Ben Skeggsd961db72010-08-05 10:48:18 +10001014 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 break;
1016 }
1017
Ben Skeggsd961db72010-08-05 10:48:18 +10001018 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019 mem->mem_type = mem_type;
1020 mem->placement = cur_flags;
1021 return 0;
1022 }
1023
1024 if (!type_found)
1025 return -EINVAL;
1026
Dave Airlieb6637522009-12-14 14:51:35 +10001027 for (i = 0; i < placement->num_busy_placement; ++i) {
1028 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001029 &mem_type);
1030 if (ret)
1031 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001032 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001033 if (!man->has_type)
1034 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001035 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001036 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001037 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001038 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001039 continue;
1040
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001041 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1042 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001043 /*
1044 * Use the access and other non-mapping-related flag bits from
1045 * the memory placement flags to the current flags
1046 */
Dave Airlieb6637522009-12-14 14:51:35 +10001047 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001048 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001049
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001050
1051 if (mem_type == TTM_PL_SYSTEM) {
1052 mem->mem_type = mem_type;
1053 mem->placement = cur_flags;
1054 mem->mm_node = NULL;
1055 return 0;
1056 }
1057
Jerome Glisseca262a9992009-12-08 15:33:32 +01001058 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001059 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060 if (ret == 0 && mem->mm_node) {
1061 mem->placement = cur_flags;
1062 return 0;
1063 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001064 if (ret == -ERESTARTSYS)
1065 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001066 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001067 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001068 return ret;
1069}
1070EXPORT_SYMBOL(ttm_bo_mem_space);
1071
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001072int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001073 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001074 bool interruptible, bool no_wait_reserve,
1075 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001076{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077 int ret = 0;
1078 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001079 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001080
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001081 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082
1083 /*
1084 * FIXME: It's possible to pipeline buffer moves.
1085 * Have the driver move function wait for idle when necessary,
1086 * instead of doing it here.
1087 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001088 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001089 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001090 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001091 if (ret)
1092 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093 mem.num_pages = bo->num_pages;
1094 mem.size = mem.num_pages << PAGE_SHIFT;
1095 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001096 mem.bus.io_reserved_vm = false;
1097 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098 /*
1099 * Determine where to move the buffer.
1100 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001101 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102 if (ret)
1103 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001104 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001105out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001106 if (ret && mem.mm_node)
1107 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001108 return ret;
1109}
1110
Jerome Glisseca262a9992009-12-08 15:33:32 +01001111static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001112 struct ttm_mem_reg *mem)
1113{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001114 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001115
Ben Skeggsd961db72010-08-05 10:48:18 +10001116 if (mem->mm_node && placement->lpfn != 0 &&
1117 (mem->start < placement->fpfn ||
1118 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001119 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001120
Jerome Glisseca262a9992009-12-08 15:33:32 +01001121 for (i = 0; i < placement->num_placement; i++) {
1122 if ((placement->placement[i] & mem->placement &
1123 TTM_PL_MASK_CACHING) &&
1124 (placement->placement[i] & mem->placement &
1125 TTM_PL_MASK_MEM))
1126 return i;
1127 }
1128 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001129}
1130
Jerome Glisse09855ac2009-12-10 17:16:27 +01001131int ttm_bo_validate(struct ttm_buffer_object *bo,
1132 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001133 bool interruptible, bool no_wait_reserve,
1134 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135{
1136 int ret;
1137
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001138 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001139 /* Check that range is valid */
1140 if (placement->lpfn || placement->fpfn)
1141 if (placement->fpfn > placement->lpfn ||
1142 (placement->lpfn - placement->fpfn) < bo->num_pages)
1143 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 /*
1145 * Check whether we need to move buffer.
1146 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001147 ret = ttm_bo_mem_compat(placement, &bo->mem);
1148 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001149 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001150 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001151 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001152 } else {
1153 /*
1154 * Use the access and other non-mapping-related flag bits from
1155 * the compatible memory placement flags to the active flags
1156 */
1157 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1158 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001159 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001160 /*
1161 * We might need to add a TTM.
1162 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001163 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1164 ret = ttm_bo_add_ttm(bo, true);
1165 if (ret)
1166 return ret;
1167 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001168 return 0;
1169}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001170EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001171
Jerome Glisse09855ac2009-12-10 17:16:27 +01001172int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1173 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001174{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001175 BUG_ON((placement->fpfn || placement->lpfn) &&
1176 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001177
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178 return 0;
1179}
1180
Jerome Glisse09855ac2009-12-10 17:16:27 +01001181int ttm_bo_init(struct ttm_bo_device *bdev,
1182 struct ttm_buffer_object *bo,
1183 unsigned long size,
1184 enum ttm_bo_type type,
1185 struct ttm_placement *placement,
1186 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001187 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001188 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001189 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001190 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001191 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001192{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001193 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001194 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001195 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1196
1197 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1198 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001199 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001200 if (destroy)
1201 (*destroy)(bo);
1202 else
1203 kfree(bo);
1204 return -ENOMEM;
1205 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001206
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001207 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1208 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001209 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001210 if (destroy)
1211 (*destroy)(bo);
1212 else
1213 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001214 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215 return -EINVAL;
1216 }
1217 bo->destroy = destroy;
1218
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219 kref_init(&bo->kref);
1220 kref_init(&bo->list_kref);
1221 atomic_set(&bo->cpu_writers, 0);
1222 atomic_set(&bo->reserved, 1);
1223 init_waitqueue_head(&bo->event_queue);
1224 INIT_LIST_HEAD(&bo->lru);
1225 INIT_LIST_HEAD(&bo->ddestroy);
1226 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001227 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001229 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001230 bo->type = type;
1231 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001232 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001233 bo->mem.mem_type = TTM_PL_SYSTEM;
1234 bo->mem.num_pages = bo->num_pages;
1235 bo->mem.mm_node = NULL;
1236 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001237 bo->mem.bus.io_reserved_vm = false;
1238 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001239 bo->priv_flags = 0;
1240 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1241 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001242 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001243 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001244 bo->sg = sg;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001245 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001246
Jerome Glisse09855ac2009-12-10 17:16:27 +01001247 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248 if (unlikely(ret != 0))
1249 goto out_err;
1250
1251 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001252 * For ttm_bo_type_device buffers, allocate
1253 * address space from the device.
1254 */
Dave Airlie129b78b2012-04-02 11:46:06 +01001255 if (bo->type == ttm_bo_type_device ||
1256 bo->type == ttm_bo_type_sg) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001257 ret = ttm_bo_setup_vm(bo);
1258 if (ret)
1259 goto out_err;
1260 }
1261
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001262 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001263 if (ret)
1264 goto out_err;
1265
1266 ttm_bo_unreserve(bo);
1267 return 0;
1268
1269out_err:
1270 ttm_bo_unreserve(bo);
1271 ttm_bo_unref(&bo);
1272
1273 return ret;
1274}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001275EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001276
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001277size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1278 unsigned long bo_size,
1279 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001280{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001281 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1282 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001283
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001284 size += ttm_round_pot(struct_size);
1285 size += PAGE_ALIGN(npages * sizeof(void *));
1286 size += ttm_round_pot(sizeof(struct ttm_tt));
1287 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001288}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001289EXPORT_SYMBOL(ttm_bo_acc_size);
1290
1291size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1292 unsigned long bo_size,
1293 unsigned struct_size)
1294{
1295 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1296 size_t size = 0;
1297
1298 size += ttm_round_pot(struct_size);
1299 size += PAGE_ALIGN(npages * sizeof(void *));
1300 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1301 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1302 return size;
1303}
1304EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001305
Jerome Glisse09855ac2009-12-10 17:16:27 +01001306int ttm_bo_create(struct ttm_bo_device *bdev,
1307 unsigned long size,
1308 enum ttm_bo_type type,
1309 struct ttm_placement *placement,
1310 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001311 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001312 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001313 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001314{
1315 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001316 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001317 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001318
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001319 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001320 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001321 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001322
Thomas Hellstroma393c732012-06-12 13:28:42 +02001323 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001324 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001325 interruptible, persistent_swap_storage, acc_size,
1326 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001327 if (likely(ret == 0))
1328 *p_bo = bo;
1329
1330 return ret;
1331}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001332EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001333
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001334static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001335 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001336{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001337 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001338 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001339 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001340
1341 /*
1342 * Can't use standard list traversal since we're unlocking.
1343 */
1344
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001345 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001346 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001347 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001348 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001349 if (ret) {
1350 if (allow_errors) {
1351 return ret;
1352 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001353 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001354 }
1355 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001356 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001357 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001358 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001359 return 0;
1360}
1361
1362int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1363{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001364 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001365 int ret = -EINVAL;
1366
1367 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001368 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001369 return ret;
1370 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001371 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001372
1373 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001374 pr_err("Trying to take down uninitialized memory manager type %u\n",
1375 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001376 return ret;
1377 }
1378
1379 man->use_type = false;
1380 man->has_type = false;
1381
1382 ret = 0;
1383 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001384 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001385
Ben Skeggsd961db72010-08-05 10:48:18 +10001386 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001387 }
1388
1389 return ret;
1390}
1391EXPORT_SYMBOL(ttm_bo_clean_mm);
1392
1393int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1394{
1395 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1396
1397 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001398 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001399 return -EINVAL;
1400 }
1401
1402 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001403 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001404 return 0;
1405 }
1406
Jerome Glisseca262a9992009-12-08 15:33:32 +01001407 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001408}
1409EXPORT_SYMBOL(ttm_bo_evict_mm);
1410
1411int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001412 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001413{
1414 int ret = -EINVAL;
1415 struct ttm_mem_type_manager *man;
1416
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001417 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001418 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001419 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001420 man->io_reserve_fastpath = true;
1421 man->use_io_reserve_lru = false;
1422 mutex_init(&man->io_reserve_mutex);
1423 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001424
1425 ret = bdev->driver->init_mem_type(bdev, type, man);
1426 if (ret)
1427 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001428 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001429
1430 ret = 0;
1431 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001432 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001433 if (ret)
1434 return ret;
1435 }
1436 man->has_type = true;
1437 man->use_type = true;
1438 man->size = p_size;
1439
1440 INIT_LIST_HEAD(&man->lru);
1441
1442 return 0;
1443}
1444EXPORT_SYMBOL(ttm_bo_init_mm);
1445
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001446static void ttm_bo_global_kobj_release(struct kobject *kobj)
1447{
1448 struct ttm_bo_global *glob =
1449 container_of(kobj, struct ttm_bo_global, kobj);
1450
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001451 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1452 __free_page(glob->dummy_read_page);
1453 kfree(glob);
1454}
1455
Dave Airlieba4420c2010-03-09 10:56:52 +10001456void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001457{
1458 struct ttm_bo_global *glob = ref->object;
1459
1460 kobject_del(&glob->kobj);
1461 kobject_put(&glob->kobj);
1462}
1463EXPORT_SYMBOL(ttm_bo_global_release);
1464
Dave Airlieba4420c2010-03-09 10:56:52 +10001465int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001466{
1467 struct ttm_bo_global_ref *bo_ref =
1468 container_of(ref, struct ttm_bo_global_ref, ref);
1469 struct ttm_bo_global *glob = ref->object;
1470 int ret;
1471
1472 mutex_init(&glob->device_list_mutex);
1473 spin_lock_init(&glob->lru_lock);
1474 glob->mem_glob = bo_ref->mem_glob;
1475 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1476
1477 if (unlikely(glob->dummy_read_page == NULL)) {
1478 ret = -ENOMEM;
1479 goto out_no_drp;
1480 }
1481
1482 INIT_LIST_HEAD(&glob->swap_lru);
1483 INIT_LIST_HEAD(&glob->device_list);
1484
1485 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1486 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1487 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001488 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001489 goto out_no_shrink;
1490 }
1491
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001492 atomic_set(&glob->bo_count, 0);
1493
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001494 ret = kobject_init_and_add(
1495 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001496 if (unlikely(ret != 0))
1497 kobject_put(&glob->kobj);
1498 return ret;
1499out_no_shrink:
1500 __free_page(glob->dummy_read_page);
1501out_no_drp:
1502 kfree(glob);
1503 return ret;
1504}
1505EXPORT_SYMBOL(ttm_bo_global_init);
1506
1507
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001508int ttm_bo_device_release(struct ttm_bo_device *bdev)
1509{
1510 int ret = 0;
1511 unsigned i = TTM_NUM_MEM_TYPES;
1512 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001513 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001514
1515 while (i--) {
1516 man = &bdev->man[i];
1517 if (man->has_type) {
1518 man->use_type = false;
1519 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1520 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001521 pr_err("DRM memory manager type %d is not clean\n",
1522 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001523 }
1524 man->has_type = false;
1525 }
1526 }
1527
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001528 mutex_lock(&glob->device_list_mutex);
1529 list_del(&bdev->device_list);
1530 mutex_unlock(&glob->device_list_mutex);
1531
Tejun Heof094cfc2010-12-24 15:59:06 +01001532 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001533
1534 while (ttm_bo_delayed_delete(bdev, true))
1535 ;
1536
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001537 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538 if (list_empty(&bdev->ddestroy))
1539 TTM_DEBUG("Delayed destroy list was clean\n");
1540
1541 if (list_empty(&bdev->man[0].lru))
1542 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001543 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001544
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001545 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1546 write_lock(&bdev->vm_lock);
1547 drm_mm_takedown(&bdev->addr_space_mm);
1548 write_unlock(&bdev->vm_lock);
1549
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001550 return ret;
1551}
1552EXPORT_SYMBOL(ttm_bo_device_release);
1553
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001554int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001555 struct ttm_bo_global *glob,
1556 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001557 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001558 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001559{
1560 int ret = -EINVAL;
1561
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001562 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001564
1565 memset(bdev->man, 0, sizeof(bdev->man));
1566
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001567 /*
1568 * Initialize the system memory buffer type.
1569 * Other types need to be driver / IOCTL initialized.
1570 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001571 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001572 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001573 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001574
1575 bdev->addr_space_rb = RB_ROOT;
1576 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1577 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001578 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001579
1580 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001581 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001582 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001583 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001584 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001585 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001586 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001587 mutex_lock(&glob->device_list_mutex);
1588 list_add_tail(&bdev->device_list, &glob->device_list);
1589 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001590
1591 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001592out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001593 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001594out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001595 return ret;
1596}
1597EXPORT_SYMBOL(ttm_bo_device_init);
1598
1599/*
1600 * buffer object vm functions.
1601 */
1602
1603bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1604{
1605 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1606
1607 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1608 if (mem->mem_type == TTM_PL_SYSTEM)
1609 return false;
1610
1611 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1612 return false;
1613
1614 if (mem->placement & TTM_PL_FLAG_CACHED)
1615 return false;
1616 }
1617 return true;
1618}
1619
Thomas Hellstromeba67092010-11-11 09:41:57 +01001620void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001621{
1622 struct ttm_bo_device *bdev = bo->bdev;
1623 loff_t offset = (loff_t) bo->addr_space_offset;
1624 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1625
1626 if (!bdev->dev_mapping)
1627 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001628 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001629 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001630}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001631
1632void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1633{
1634 struct ttm_bo_device *bdev = bo->bdev;
1635 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1636
1637 ttm_mem_io_lock(man, false);
1638 ttm_bo_unmap_virtual_locked(bo);
1639 ttm_mem_io_unlock(man);
1640}
1641
1642
Dave Airliee024e112009-06-24 09:48:08 +10001643EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001644
1645static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1646{
1647 struct ttm_bo_device *bdev = bo->bdev;
1648 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1649 struct rb_node *parent = NULL;
1650 struct ttm_buffer_object *cur_bo;
1651 unsigned long offset = bo->vm_node->start;
1652 unsigned long cur_offset;
1653
1654 while (*cur) {
1655 parent = *cur;
1656 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1657 cur_offset = cur_bo->vm_node->start;
1658 if (offset < cur_offset)
1659 cur = &parent->rb_left;
1660 else if (offset > cur_offset)
1661 cur = &parent->rb_right;
1662 else
1663 BUG();
1664 }
1665
1666 rb_link_node(&bo->vm_rb, parent, cur);
1667 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1668}
1669
1670/**
1671 * ttm_bo_setup_vm:
1672 *
1673 * @bo: the buffer to allocate address space for
1674 *
1675 * Allocate address space in the drm device so that applications
1676 * can mmap the buffer and access the contents. This only
1677 * applies to ttm_bo_type_device objects as others are not
1678 * placed in the drm device address space.
1679 */
1680
1681static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1682{
1683 struct ttm_bo_device *bdev = bo->bdev;
1684 int ret;
1685
1686retry_pre_get:
1687 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1688 if (unlikely(ret != 0))
1689 return ret;
1690
1691 write_lock(&bdev->vm_lock);
1692 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1693 bo->mem.num_pages, 0, 0);
1694
1695 if (unlikely(bo->vm_node == NULL)) {
1696 ret = -ENOMEM;
1697 goto out_unlock;
1698 }
1699
1700 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1701 bo->mem.num_pages, 0);
1702
1703 if (unlikely(bo->vm_node == NULL)) {
1704 write_unlock(&bdev->vm_lock);
1705 goto retry_pre_get;
1706 }
1707
1708 ttm_bo_vm_insert_rb(bo);
1709 write_unlock(&bdev->vm_lock);
1710 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1711
1712 return 0;
1713out_unlock:
1714 write_unlock(&bdev->vm_lock);
1715 return ret;
1716}
1717
1718int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001719 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001720{
1721 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001722 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001723 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001724 int ret = 0;
1725
Dave Airlie1717c0e2011-10-27 18:28:37 +02001726 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001727 return 0;
1728
Dave Airlie1717c0e2011-10-27 18:28:37 +02001729 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001730
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001731 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001732 void *tmp_obj = bo->sync_obj;
1733 bo->sync_obj = NULL;
1734 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1735 spin_unlock(&bdev->fence_lock);
1736 driver->sync_obj_unref(&tmp_obj);
1737 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001738 continue;
1739 }
1740
1741 if (no_wait)
1742 return -EBUSY;
1743
Dave Airlie1717c0e2011-10-27 18:28:37 +02001744 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001745 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001746 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001747 lazy, interruptible);
1748 if (unlikely(ret != 0)) {
1749 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001750 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001751 return ret;
1752 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001753 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001754 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001755 void *tmp_obj = bo->sync_obj;
1756 bo->sync_obj = NULL;
1757 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1758 &bo->priv_flags);
1759 spin_unlock(&bdev->fence_lock);
1760 driver->sync_obj_unref(&sync_obj);
1761 driver->sync_obj_unref(&tmp_obj);
1762 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001763 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001764 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001765 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001766 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001767 }
1768 }
1769 return 0;
1770}
1771EXPORT_SYMBOL(ttm_bo_wait);
1772
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001773int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1774{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001775 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001776 int ret = 0;
1777
1778 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001779 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001780 */
1781
1782 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1783 if (unlikely(ret != 0))
1784 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001785 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001786 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001787 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001788 if (likely(ret == 0))
1789 atomic_inc(&bo->cpu_writers);
1790 ttm_bo_unreserve(bo);
1791 return ret;
1792}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001793EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001794
1795void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1796{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001797 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001798}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001799EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800
1801/**
1802 * A buffer object shrink method that tries to swap out the first
1803 * buffer object on the bo_global::swap_lru list.
1804 */
1805
1806static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1807{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001808 struct ttm_bo_global *glob =
1809 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001810 struct ttm_buffer_object *bo;
1811 int ret = -EBUSY;
1812 int put_count;
1813 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1814
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001815 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001816 list_for_each_entry(bo, &glob->swap_lru, swap) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001817 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001818 if (!ret)
1819 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001820 }
1821
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001822 if (ret) {
1823 spin_unlock(&glob->lru_lock);
1824 return ret;
1825 }
1826
1827 kref_get(&bo->list_kref);
1828
1829 if (!list_empty(&bo->ddestroy)) {
1830 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1831 kref_put(&bo->list_kref, ttm_bo_release_list);
1832 return ret;
1833 }
1834
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001835 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001836 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001837
Dave Airlied6ea8882010-11-22 13:24:40 +10001838 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001839
1840 /**
1841 * Wait for GPU, then move to system cached.
1842 */
1843
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001844 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001845 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001846 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001847
1848 if (unlikely(ret != 0))
1849 goto out;
1850
1851 if ((bo->mem.placement & swap_placement) != swap_placement) {
1852 struct ttm_mem_reg evict_mem;
1853
1854 evict_mem = bo->mem;
1855 evict_mem.mm_node = NULL;
1856 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1857 evict_mem.mem_type = TTM_PL_SYSTEM;
1858
1859 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001860 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001861 if (unlikely(ret != 0))
1862 goto out;
1863 }
1864
1865 ttm_bo_unmap_virtual(bo);
1866
1867 /**
1868 * Swap out. Buffer will be swapped in again as soon as
1869 * anyone tries to access a ttm page.
1870 */
1871
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001872 if (bo->bdev->driver->swap_notify)
1873 bo->bdev->driver->swap_notify(bo);
1874
Jan Engelhardt5df23972011-04-04 01:25:18 +02001875 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001876out:
1877
1878 /**
1879 *
1880 * Unreserve without putting on LRU to avoid swapping out an
1881 * already swapped buffer.
1882 */
1883
1884 atomic_set(&bo->reserved, 0);
1885 wake_up_all(&bo->event_queue);
1886 kref_put(&bo->list_kref, ttm_bo_release_list);
1887 return ret;
1888}
1889
1890void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1891{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001892 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001893 ;
1894}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001895EXPORT_SYMBOL(ttm_bo_swapout_all);