blob: 7426fe59108e65485b5bd45f5c2aa1c462518b63 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/jiffies.h>
37#include <linux/slab.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/file.h>
41#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020043
44#define TTM_ASSERT_LOCKED(param)
45#define TTM_DEBUG(fmt, arg...)
46#define TTM_BO_HASH_ORDER 13
47
48static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020049static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020050static void ttm_bo_global_kobj_release(struct kobject *kobj);
51
52static struct attribute ttm_bo_count = {
53 .name = "bo_count",
54 .mode = S_IRUGO
55};
56
Jerome Glissefb53f862009-12-09 21:55:10 +010057static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
58{
59 int i;
60
61 for (i = 0; i <= TTM_PL_PRIV5; i++)
62 if (flags & (1 << i)) {
63 *mem_type = i;
64 return 0;
65 }
66 return -EINVAL;
67}
68
Jerome Glisse5012f502009-12-10 18:07:26 +010069static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010070{
Jerome Glisse5012f502009-12-10 18:07:26 +010071 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
72
Joe Perches25d04792012-03-16 21:43:50 -070073 pr_err(" has_type: %d\n", man->has_type);
74 pr_err(" use_type: %d\n", man->use_type);
75 pr_err(" flags: 0x%08X\n", man->flags);
76 pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
77 pr_err(" size: %llu\n", man->size);
78 pr_err(" available_caching: 0x%08X\n", man->available_caching);
79 pr_err(" default_caching: 0x%08X\n", man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100080 if (mem_type != TTM_PL_SYSTEM)
81 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010082}
83
84static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
85 struct ttm_placement *placement)
86{
Jerome Glissefb53f862009-12-09 21:55:10 +010087 int i, ret, mem_type;
88
Joe Perches25d04792012-03-16 21:43:50 -070089 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
90 bo, bo->mem.num_pages, bo->mem.size >> 10,
91 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010092 for (i = 0; i < placement->num_placement; i++) {
93 ret = ttm_mem_type_from_flags(placement->placement[i],
94 &mem_type);
95 if (ret)
96 return;
Joe Perches25d04792012-03-16 21:43:50 -070097 pr_err(" placement[%d]=0x%08X (%d)\n",
98 i, placement->placement[i], mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +010099 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100100 }
101}
102
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200103static ssize_t ttm_bo_global_show(struct kobject *kobj,
104 struct attribute *attr,
105 char *buffer)
106{
107 struct ttm_bo_global *glob =
108 container_of(kobj, struct ttm_bo_global, kobj);
109
110 return snprintf(buffer, PAGE_SIZE, "%lu\n",
111 (unsigned long) atomic_read(&glob->bo_count));
112}
113
114static struct attribute *ttm_bo_global_attrs[] = {
115 &ttm_bo_count,
116 NULL
117};
118
Emese Revfy52cf25d2010-01-19 02:58:23 +0100119static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200120 .show = &ttm_bo_global_show
121};
122
123static struct kobj_type ttm_bo_glob_kobj_type = {
124 .release = &ttm_bo_global_kobj_release,
125 .sysfs_ops = &ttm_bo_global_ops,
126 .default_attrs = ttm_bo_global_attrs
127};
128
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200129
130static inline uint32_t ttm_bo_type_flags(unsigned type)
131{
132 return 1 << (type);
133}
134
135static void ttm_bo_release_list(struct kref *list_kref)
136{
137 struct ttm_buffer_object *bo =
138 container_of(list_kref, struct ttm_buffer_object, list_kref);
139 struct ttm_bo_device *bdev = bo->bdev;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500140 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200141
142 BUG_ON(atomic_read(&bo->list_kref.refcount));
143 BUG_ON(atomic_read(&bo->kref.refcount));
144 BUG_ON(atomic_read(&bo->cpu_writers));
145 BUG_ON(bo->sync_obj != NULL);
146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
149
150 if (bo->ttm)
151 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200152 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200153 if (bo->destroy)
154 bo->destroy(bo);
155 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 kfree(bo);
157 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500158 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200159}
160
161int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
162{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200163 if (interruptible) {
Jean Delvare965d3802010-10-09 12:36:45 +0000164 return wait_event_interruptible(bo->event_queue,
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200165 !ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200166 } else {
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200167 wait_event(bo->event_queue, !ttm_bo_is_reserved(bo));
Jean Delvare965d3802010-10-09 12:36:45 +0000168 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200170}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000171EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200172
Dave Airlied6ea8882010-11-22 13:24:40 +1000173void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200174{
175 struct ttm_bo_device *bdev = bo->bdev;
176 struct ttm_mem_type_manager *man;
177
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200178 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200179
180 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
181
182 BUG_ON(!list_empty(&bo->lru));
183
184 man = &bdev->man[bo->mem.mem_type];
185 list_add_tail(&bo->lru, &man->lru);
186 kref_get(&bo->list_kref);
187
188 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200189 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200190 kref_get(&bo->list_kref);
191 }
192 }
193}
194
Dave Airlied6ea8882010-11-22 13:24:40 +1000195int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200196{
197 int put_count = 0;
198
199 if (!list_empty(&bo->swap)) {
200 list_del_init(&bo->swap);
201 ++put_count;
202 }
203 if (!list_empty(&bo->lru)) {
204 list_del_init(&bo->lru);
205 ++put_count;
206 }
207
208 /*
209 * TODO: Add a driver hook to delete from
210 * driver-specific LRU's here.
211 */
212
213 return put_count;
214}
215
216int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
217 bool interruptible,
218 bool no_wait, bool use_sequence, uint32_t sequence)
219{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200220 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200221 int ret;
222
Thomas Hellstrom6c1e9632012-11-06 11:31:51 +0000223 while (unlikely(atomic_read(&bo->reserved) != 0)) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100224 /**
225 * Deadlock avoidance for multi-bo reserving.
226 */
Thomas Hellstrom96726fe2010-11-17 12:28:28 +0000227 if (use_sequence && bo->seq_valid) {
228 /**
229 * We've already reserved this one.
230 */
231 if (unlikely(sequence == bo->val_seq))
232 return -EDEADLK;
233 /**
234 * Already reserved by a thread that will not back
235 * off for us. We need to back off.
236 */
237 if (unlikely(sequence - bo->val_seq < (1 << 31)))
238 return -EAGAIN;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200239 }
240
241 if (no_wait)
242 return -EBUSY;
243
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200244 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200245 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200246 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200247
248 if (unlikely(ret))
249 return ret;
250 }
251
Thomas Hellstrom6c1e9632012-11-06 11:31:51 +0000252 atomic_set(&bo->reserved, 1);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200253 if (use_sequence) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100254 /**
255 * Wake up waiters that may need to recheck for deadlock,
256 * if we decreased the sequence number.
257 */
258 if (unlikely((bo->val_seq - sequence < (1 << 31))
259 || !bo->seq_valid))
260 wake_up_all(&bo->event_queue);
261
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200262 bo->val_seq = sequence;
263 bo->seq_valid = true;
264 } else {
265 bo->seq_valid = false;
266 }
267
268 return 0;
269}
270EXPORT_SYMBOL(ttm_bo_reserve);
271
272static void ttm_bo_ref_bug(struct kref *list_kref)
273{
274 BUG();
275}
276
Dave Airlied6ea8882010-11-22 13:24:40 +1000277void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
278 bool never_free)
279{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100280 kref_sub(&bo->list_kref, count,
281 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000282}
283
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200284int ttm_bo_reserve(struct ttm_buffer_object *bo,
285 bool interruptible,
286 bool no_wait, bool use_sequence, uint32_t sequence)
287{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200288 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200289 int put_count = 0;
290 int ret;
291
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200292 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200293 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
294 sequence);
295 if (likely(ret == 0))
296 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200297 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200298
Dave Airlied6ea8882010-11-22 13:24:40 +1000299 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200300
301 return ret;
302}
303
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000304void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
305{
306 ttm_bo_add_to_lru(bo);
307 atomic_set(&bo->reserved, 0);
308 wake_up_all(&bo->event_queue);
309}
310
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200311void ttm_bo_unreserve(struct ttm_buffer_object *bo)
312{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200313 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200314
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200315 spin_lock(&glob->lru_lock);
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000316 ttm_bo_unreserve_locked(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200317 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200318}
319EXPORT_SYMBOL(ttm_bo_unreserve);
320
321/*
322 * Call bo->mutex locked.
323 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200324static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
325{
326 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200327 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200328 int ret = 0;
329 uint32_t page_flags = 0;
330
331 TTM_ASSERT_LOCKED(&bo->mutex);
332 bo->ttm = NULL;
333
Dave Airliead49f502009-07-10 22:36:26 +1000334 if (bdev->need_dma32)
335 page_flags |= TTM_PAGE_FLAG_DMA32;
336
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200337 switch (bo->type) {
338 case ttm_bo_type_device:
339 if (zero_alloc)
340 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
341 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400342 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
343 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344 if (unlikely(bo->ttm == NULL))
345 ret = -ENOMEM;
346 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100347 case ttm_bo_type_sg:
348 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
349 page_flags | TTM_PAGE_FLAG_SG,
350 glob->dummy_read_page);
351 if (unlikely(bo->ttm == NULL)) {
352 ret = -ENOMEM;
353 break;
354 }
355 bo->ttm->sg = bo->sg;
356 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200357 default:
Joe Perches25d04792012-03-16 21:43:50 -0700358 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200359 ret = -EINVAL;
360 break;
361 }
362
363 return ret;
364}
365
366static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
367 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000368 bool evict, bool interruptible,
369 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200370{
371 struct ttm_bo_device *bdev = bo->bdev;
372 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
373 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
374 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
375 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
376 int ret = 0;
377
378 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100379 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
380 ret = ttm_mem_io_lock(old_man, true);
381 if (unlikely(ret != 0))
382 goto out_err;
383 ttm_bo_unmap_virtual_locked(bo);
384 ttm_mem_io_unlock(old_man);
385 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200386
387 /*
388 * Create and bind a ttm if required.
389 */
390
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000391 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
392 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000393 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
394 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000395 if (ret)
396 goto out_err;
397 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200398
399 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
400 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200401 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200402
403 if (mem->mem_type != TTM_PL_SYSTEM) {
404 ret = ttm_tt_bind(bo->ttm, mem);
405 if (ret)
406 goto out_err;
407 }
408
409 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000410 if (bdev->driver->move_notify)
411 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100412 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200413 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200414 goto moved;
415 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200416 }
417
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000418 if (bdev->driver->move_notify)
419 bdev->driver->move_notify(bo, mem);
420
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200421 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
422 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000423 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200424 else if (bdev->driver->move)
425 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000426 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200427 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000428 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200429
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000430 if (ret) {
431 if (bdev->driver->move_notify) {
432 struct ttm_mem_reg tmp_mem = *mem;
433 *mem = bo->mem;
434 bo->mem = tmp_mem;
435 bdev->driver->move_notify(bo, mem);
436 bo->mem = *mem;
437 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200438
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000439 goto out_err;
440 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500441
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200442moved:
443 if (bo->evicted) {
444 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
445 if (ret)
Joe Perches25d04792012-03-16 21:43:50 -0700446 pr_err("Can not flush read caches\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200447 bo->evicted = false;
448 }
449
450 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000451 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200452 bdev->man[bo->mem.mem_type].gpu_offset;
453 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100454 } else
455 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200456
457 return 0;
458
459out_err:
460 new_man = &bdev->man[bo->mem.mem_type];
461 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
462 ttm_tt_unbind(bo->ttm);
463 ttm_tt_destroy(bo->ttm);
464 bo->ttm = NULL;
465 }
466
467 return ret;
468}
469
470/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200471 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200472 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200473 * This is the place to put in driver specific hooks to release
474 * driver private resources.
475 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200476 */
477
478static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
479{
Jerome Glissedc97b342011-11-18 11:47:03 -0500480 if (bo->bdev->driver->move_notify)
481 bo->bdev->driver->move_notify(bo, NULL);
482
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200483 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200484 ttm_tt_unbind(bo->ttm);
485 ttm_tt_destroy(bo->ttm);
486 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200487 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200488 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200489
490 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200491
492 /*
493 * Make processes trying to reserve really pick it up.
494 */
495 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200496 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200497}
498
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200499static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200500{
501 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200502 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200503 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000504 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200505 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200506 int ret;
507
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000508 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200509 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200510 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200511
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200512 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100513
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200514 /**
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000515 * Lock inversion between bo:reserve and bdev::fence_lock here,
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200516 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200517 */
518
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200519 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200520
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200521 if (unlikely(ret == -EBUSY))
522 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200523
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000524 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200525 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200526
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200527 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200528 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200529
Dave Airlied6ea8882010-11-22 13:24:40 +1000530 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200531
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200532 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200533 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200534 spin_lock(&glob->lru_lock);
535 }
536queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200537 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000538 if (bo->sync_obj)
539 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200540
541 kref_get(&bo->list_kref);
542 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
543 spin_unlock(&glob->lru_lock);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000544 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200545
Thomas Hellstromaa123262010-11-02 13:21:47 +0000546 if (sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +0000547 driver->sync_obj_flush(sync_obj);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000548 driver->sync_obj_unref(&sync_obj);
549 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200550 schedule_delayed_work(&bdev->wq,
551 ((HZ / 100) < 1) ? 1 : HZ / 100);
552}
553
554/**
555 * function ttm_bo_cleanup_refs
556 * If bo idle, remove from delayed- and lru lists, and unref.
557 * If not idle, do nothing.
558 *
559 * @interruptible Any sleeps should occur interruptibly.
560 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
561 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
562 */
563
564static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
565 bool interruptible,
566 bool no_wait_reserve,
567 bool no_wait_gpu)
568{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000569 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200570 struct ttm_bo_global *glob = bo->glob;
571 int put_count;
572 int ret = 0;
573
574retry:
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000575 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200576 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000577 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200578
579 if (unlikely(ret != 0))
580 return ret;
581
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000582retry_reserve:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200583 spin_lock(&glob->lru_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100584
585 if (unlikely(list_empty(&bo->ddestroy))) {
586 spin_unlock(&glob->lru_lock);
587 return 0;
588 }
589
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000590 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200591
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000592 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200593 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000594 if (likely(!no_wait_reserve))
595 ret = ttm_bo_wait_unreserved(bo, interruptible);
596 if (unlikely(ret != 0))
597 return ret;
598
599 goto retry_reserve;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200600 }
601
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000602 BUG_ON(ret != 0);
603
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200604 /**
605 * We can re-check for sync object without taking
606 * the bo::lock since setting the sync object requires
607 * also bo::reserved. A busy object at this point may
608 * be caused by another thread recently starting an accelerated
609 * eviction.
610 */
611
612 if (unlikely(bo->sync_obj)) {
613 atomic_set(&bo->reserved, 0);
614 wake_up_all(&bo->event_queue);
615 spin_unlock(&glob->lru_lock);
616 goto retry;
617 }
618
619 put_count = ttm_bo_del_from_lru(bo);
620 list_del_init(&bo->ddestroy);
621 ++put_count;
622
623 spin_unlock(&glob->lru_lock);
624 ttm_bo_cleanup_memtype_use(bo);
625
Dave Airlied6ea8882010-11-22 13:24:40 +1000626 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200627
628 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200629}
630
631/**
632 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
633 * encountered buffers.
634 */
635
636static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
637{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200638 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100639 struct ttm_buffer_object *entry = NULL;
640 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200641
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200642 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100643 if (list_empty(&bdev->ddestroy))
644 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200645
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100646 entry = list_first_entry(&bdev->ddestroy,
647 struct ttm_buffer_object, ddestroy);
648 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100650 for (;;) {
651 struct ttm_buffer_object *nentry = NULL;
652
653 if (entry->ddestroy.next != &bdev->ddestroy) {
654 nentry = list_first_entry(&entry->ddestroy,
655 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656 kref_get(&nentry->list_kref);
657 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200658
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200659 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200660 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
661 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200662 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100663 entry = nentry;
664
665 if (ret || !entry)
666 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200667
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200668 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100669 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200670 break;
671 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200672
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100673out_unlock:
674 spin_unlock(&glob->lru_lock);
675out:
676 if (entry)
677 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200678 return ret;
679}
680
681static void ttm_bo_delayed_workqueue(struct work_struct *work)
682{
683 struct ttm_bo_device *bdev =
684 container_of(work, struct ttm_bo_device, wq.work);
685
686 if (ttm_bo_delayed_delete(bdev, false)) {
687 schedule_delayed_work(&bdev->wq,
688 ((HZ / 100) < 1) ? 1 : HZ / 100);
689 }
690}
691
692static void ttm_bo_release(struct kref *kref)
693{
694 struct ttm_buffer_object *bo =
695 container_of(kref, struct ttm_buffer_object, kref);
696 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100697 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200698
699 if (likely(bo->vm_node != NULL)) {
700 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
701 drm_mm_put_block(bo->vm_node);
702 bo->vm_node = NULL;
703 }
704 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100705 ttm_mem_io_lock(man, false);
706 ttm_mem_io_free_vm(bo);
707 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200708 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200709 kref_put(&bo->list_kref, ttm_bo_release_list);
710 write_lock(&bdev->vm_lock);
711}
712
713void ttm_bo_unref(struct ttm_buffer_object **p_bo)
714{
715 struct ttm_buffer_object *bo = *p_bo;
716 struct ttm_bo_device *bdev = bo->bdev;
717
718 *p_bo = NULL;
719 write_lock(&bdev->vm_lock);
720 kref_put(&bo->kref, ttm_bo_release);
721 write_unlock(&bdev->vm_lock);
722}
723EXPORT_SYMBOL(ttm_bo_unref);
724
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400725int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
726{
727 return cancel_delayed_work_sync(&bdev->wq);
728}
729EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
730
731void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
732{
733 if (resched)
734 schedule_delayed_work(&bdev->wq,
735 ((HZ / 100) < 1) ? 1 : HZ / 100);
736}
737EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
738
Jerome Glisseca262a9992009-12-08 15:33:32 +0100739static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000740 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200741{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200742 struct ttm_bo_device *bdev = bo->bdev;
743 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100744 struct ttm_placement placement;
745 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200746
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000747 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200748 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000749 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200750
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200751 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100752 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700753 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200754 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200755 goto out;
756 }
757
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200758 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200759
760 evict_mem = bo->mem;
761 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100762 evict_mem.bus.io_reserved_vm = false;
763 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200764
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100765 placement.fpfn = 0;
766 placement.lpfn = 0;
767 placement.num_placement = 0;
768 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100769 bdev->driver->evict_flags(bo, &placement);
770 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000771 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200772 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100773 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700774 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
775 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100776 ttm_bo_mem_space_debug(bo, &placement);
777 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200778 goto out;
779 }
780
781 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000782 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200783 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100784 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700785 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000786 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200787 goto out;
788 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200789 bo->evicted = true;
790out:
791 return ret;
792}
793
Jerome Glisseca262a9992009-12-08 15:33:32 +0100794static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
795 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000796 bool interruptible, bool no_wait_reserve,
797 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100798{
799 struct ttm_bo_global *glob = bdev->glob;
800 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
801 struct ttm_buffer_object *bo;
802 int ret, put_count = 0;
803
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100804retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100805 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100806 if (list_empty(&man->lru)) {
807 spin_unlock(&glob->lru_lock);
808 return -EBUSY;
809 }
810
Jerome Glisseca262a9992009-12-08 15:33:32 +0100811 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
812 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100813
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200814 if (!list_empty(&bo->ddestroy)) {
815 spin_unlock(&glob->lru_lock);
816 ret = ttm_bo_cleanup_refs(bo, interruptible,
817 no_wait_reserve, no_wait_gpu);
818 kref_put(&bo->list_kref, ttm_bo_release_list);
819
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000820 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200821 }
822
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000823 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100824
825 if (unlikely(ret == -EBUSY)) {
826 spin_unlock(&glob->lru_lock);
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000827 if (likely(!no_wait_reserve))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100828 ret = ttm_bo_wait_unreserved(bo, interruptible);
829
830 kref_put(&bo->list_kref, ttm_bo_release_list);
831
832 /**
833 * We *need* to retry after releasing the lru lock.
834 */
835
836 if (unlikely(ret != 0))
837 return ret;
838 goto retry;
839 }
840
841 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100842 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100843
844 BUG_ON(ret != 0);
845
Dave Airlied6ea8882010-11-22 13:24:40 +1000846 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100847
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000848 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100849 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100850
Jerome Glisseca262a9992009-12-08 15:33:32 +0100851 kref_put(&bo->list_kref, ttm_bo_release_list);
852 return ret;
853}
854
Ben Skeggs42311ff2010-08-04 12:07:08 +1000855void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
856{
Ben Skeggsd961db72010-08-05 10:48:18 +1000857 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000858
Ben Skeggsd961db72010-08-05 10:48:18 +1000859 if (mem->mm_node)
860 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000861}
862EXPORT_SYMBOL(ttm_bo_mem_put);
863
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200864/**
865 * Repeatedly evict memory from the LRU for @mem_type until we create enough
866 * space, or we've evicted everything and there isn't enough space.
867 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100868static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
869 uint32_t mem_type,
870 struct ttm_placement *placement,
871 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000872 bool interruptible,
873 bool no_wait_reserve,
874 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200875{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100876 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200878 int ret;
879
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000881 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882 if (unlikely(ret != 0))
883 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000884 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100885 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100886 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000887 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100888 if (unlikely(ret != 0))
889 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200890 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000891 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200892 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200893 mem->mem_type = mem_type;
894 return 0;
895}
896
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200897static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
898 uint32_t cur_placement,
899 uint32_t proposed_placement)
900{
901 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
902 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
903
904 /**
905 * Keep current caching if possible.
906 */
907
908 if ((cur_placement & caching) != 0)
909 result |= (cur_placement & caching);
910 else if ((man->default_caching & caching) != 0)
911 result |= man->default_caching;
912 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
913 result |= TTM_PL_FLAG_CACHED;
914 else if ((TTM_PL_FLAG_WC & caching) != 0)
915 result |= TTM_PL_FLAG_WC;
916 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
917 result |= TTM_PL_FLAG_UNCACHED;
918
919 return result;
920}
921
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200924 uint32_t proposed_placement,
925 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926{
927 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
928
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200929 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200930 return false;
931
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200932 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200933 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200934
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200935 cur_flags |= (proposed_placement & man->available_caching);
936
937 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938 return true;
939}
940
941/**
942 * Creates space for memory region @mem according to its type.
943 *
944 * This function first searches for free space in compatible memory types in
945 * the priority order defined by the driver. If free space isn't found, then
946 * ttm_bo_mem_force_space is attempted in priority order to evict and find
947 * space.
948 */
949int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100950 struct ttm_placement *placement,
951 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000952 bool interruptible, bool no_wait_reserve,
953 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200954{
955 struct ttm_bo_device *bdev = bo->bdev;
956 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200957 uint32_t mem_type = TTM_PL_SYSTEM;
958 uint32_t cur_flags = 0;
959 bool type_found = false;
960 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100961 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100962 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200963
964 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000965 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100966 ret = ttm_mem_type_from_flags(placement->placement[i],
967 &mem_type);
968 if (ret)
969 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970 man = &bdev->man[mem_type];
971
972 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100973 mem_type,
974 placement->placement[i],
975 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976
977 if (!type_ok)
978 continue;
979
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200980 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
981 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100982 /*
983 * Use the access and other non-mapping-related flag bits from
984 * the memory placement flags to the current flags
985 */
986 ttm_flag_masked(&cur_flags, placement->placement[i],
987 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200988
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989 if (mem_type == TTM_PL_SYSTEM)
990 break;
991
992 if (man->has_type && man->use_type) {
993 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000994 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100995 if (unlikely(ret))
996 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000998 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200999 break;
1000 }
1001
Ben Skeggsd961db72010-08-05 10:48:18 +10001002 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001003 mem->mem_type = mem_type;
1004 mem->placement = cur_flags;
1005 return 0;
1006 }
1007
1008 if (!type_found)
1009 return -EINVAL;
1010
Dave Airlieb6637522009-12-14 14:51:35 +10001011 for (i = 0; i < placement->num_busy_placement; ++i) {
1012 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001013 &mem_type);
1014 if (ret)
1015 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001016 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001017 if (!man->has_type)
1018 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001020 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001021 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001022 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001023 continue;
1024
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001025 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1026 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001027 /*
1028 * Use the access and other non-mapping-related flag bits from
1029 * the memory placement flags to the current flags
1030 */
Dave Airlieb6637522009-12-14 14:51:35 +10001031 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001032 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001033
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001034
1035 if (mem_type == TTM_PL_SYSTEM) {
1036 mem->mem_type = mem_type;
1037 mem->placement = cur_flags;
1038 mem->mm_node = NULL;
1039 return 0;
1040 }
1041
Jerome Glisseca262a9992009-12-08 15:33:32 +01001042 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001043 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044 if (ret == 0 && mem->mm_node) {
1045 mem->placement = cur_flags;
1046 return 0;
1047 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001048 if (ret == -ERESTARTSYS)
1049 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001051 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052 return ret;
1053}
1054EXPORT_SYMBOL(ttm_bo_mem_space);
1055
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001056int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001057 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001058 bool interruptible, bool no_wait_reserve,
1059 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001061 int ret = 0;
1062 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001063 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001064
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001065 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001066
1067 /*
1068 * FIXME: It's possible to pipeline buffer moves.
1069 * Have the driver move function wait for idle when necessary,
1070 * instead of doing it here.
1071 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001072 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001073 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001074 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001075 if (ret)
1076 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077 mem.num_pages = bo->num_pages;
1078 mem.size = mem.num_pages << PAGE_SHIFT;
1079 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001080 mem.bus.io_reserved_vm = false;
1081 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082 /*
1083 * Determine where to move the buffer.
1084 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001085 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086 if (ret)
1087 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001088 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001090 if (ret && mem.mm_node)
1091 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092 return ret;
1093}
1094
Jerome Glisseca262a9992009-12-08 15:33:32 +01001095static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001096 struct ttm_mem_reg *mem)
1097{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001098 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001099
Ben Skeggsd961db72010-08-05 10:48:18 +10001100 if (mem->mm_node && placement->lpfn != 0 &&
1101 (mem->start < placement->fpfn ||
1102 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001103 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001104
Jerome Glisseca262a9992009-12-08 15:33:32 +01001105 for (i = 0; i < placement->num_placement; i++) {
1106 if ((placement->placement[i] & mem->placement &
1107 TTM_PL_MASK_CACHING) &&
1108 (placement->placement[i] & mem->placement &
1109 TTM_PL_MASK_MEM))
1110 return i;
1111 }
1112 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113}
1114
Jerome Glisse09855ac2009-12-10 17:16:27 +01001115int ttm_bo_validate(struct ttm_buffer_object *bo,
1116 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001117 bool interruptible, bool no_wait_reserve,
1118 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001119{
1120 int ret;
1121
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001122 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001123 /* Check that range is valid */
1124 if (placement->lpfn || placement->fpfn)
1125 if (placement->fpfn > placement->lpfn ||
1126 (placement->lpfn - placement->fpfn) < bo->num_pages)
1127 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001128 /*
1129 * Check whether we need to move buffer.
1130 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001131 ret = ttm_bo_mem_compat(placement, &bo->mem);
1132 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001133 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001134 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001136 } else {
1137 /*
1138 * Use the access and other non-mapping-related flag bits from
1139 * the compatible memory placement flags to the active flags
1140 */
1141 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1142 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 /*
1145 * We might need to add a TTM.
1146 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001147 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1148 ret = ttm_bo_add_ttm(bo, true);
1149 if (ret)
1150 return ret;
1151 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152 return 0;
1153}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001154EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001155
Jerome Glisse09855ac2009-12-10 17:16:27 +01001156int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1157 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001159 BUG_ON((placement->fpfn || placement->lpfn) &&
1160 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001162 return 0;
1163}
1164
Jerome Glisse09855ac2009-12-10 17:16:27 +01001165int ttm_bo_init(struct ttm_bo_device *bdev,
1166 struct ttm_buffer_object *bo,
1167 unsigned long size,
1168 enum ttm_bo_type type,
1169 struct ttm_placement *placement,
1170 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001171 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001172 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001173 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001174 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001175 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001176{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001177 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001179 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1180
1181 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1182 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001183 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001184 if (destroy)
1185 (*destroy)(bo);
1186 else
1187 kfree(bo);
1188 return -ENOMEM;
1189 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001190
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001191 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1192 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001193 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001194 if (destroy)
1195 (*destroy)(bo);
1196 else
1197 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001198 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199 return -EINVAL;
1200 }
1201 bo->destroy = destroy;
1202
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203 kref_init(&bo->kref);
1204 kref_init(&bo->list_kref);
1205 atomic_set(&bo->cpu_writers, 0);
1206 atomic_set(&bo->reserved, 1);
1207 init_waitqueue_head(&bo->event_queue);
1208 INIT_LIST_HEAD(&bo->lru);
1209 INIT_LIST_HEAD(&bo->ddestroy);
1210 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001211 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001213 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001214 bo->type = type;
1215 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001216 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001217 bo->mem.mem_type = TTM_PL_SYSTEM;
1218 bo->mem.num_pages = bo->num_pages;
1219 bo->mem.mm_node = NULL;
1220 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001221 bo->mem.bus.io_reserved_vm = false;
1222 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223 bo->priv_flags = 0;
1224 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1225 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001226 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001227 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001228 bo->sg = sg;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001229 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001230
Jerome Glisse09855ac2009-12-10 17:16:27 +01001231 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001232 if (unlikely(ret != 0))
1233 goto out_err;
1234
1235 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001236 * For ttm_bo_type_device buffers, allocate
1237 * address space from the device.
1238 */
Dave Airlie129b78b2012-04-02 11:46:06 +01001239 if (bo->type == ttm_bo_type_device ||
1240 bo->type == ttm_bo_type_sg) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001241 ret = ttm_bo_setup_vm(bo);
1242 if (ret)
1243 goto out_err;
1244 }
1245
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001246 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001247 if (ret)
1248 goto out_err;
1249
1250 ttm_bo_unreserve(bo);
1251 return 0;
1252
1253out_err:
1254 ttm_bo_unreserve(bo);
1255 ttm_bo_unref(&bo);
1256
1257 return ret;
1258}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001259EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001260
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001261size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1262 unsigned long bo_size,
1263 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001264{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001265 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1266 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001267
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001268 size += ttm_round_pot(struct_size);
1269 size += PAGE_ALIGN(npages * sizeof(void *));
1270 size += ttm_round_pot(sizeof(struct ttm_tt));
1271 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001273EXPORT_SYMBOL(ttm_bo_acc_size);
1274
1275size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1276 unsigned long bo_size,
1277 unsigned struct_size)
1278{
1279 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1280 size_t size = 0;
1281
1282 size += ttm_round_pot(struct_size);
1283 size += PAGE_ALIGN(npages * sizeof(void *));
1284 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1285 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1286 return size;
1287}
1288EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001289
Jerome Glisse09855ac2009-12-10 17:16:27 +01001290int ttm_bo_create(struct ttm_bo_device *bdev,
1291 unsigned long size,
1292 enum ttm_bo_type type,
1293 struct ttm_placement *placement,
1294 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001295 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001296 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001297 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298{
1299 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001300 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001301 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001303 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001304 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001305 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306
Thomas Hellstroma393c732012-06-12 13:28:42 +02001307 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001308 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001309 interruptible, persistent_swap_storage, acc_size,
1310 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001311 if (likely(ret == 0))
1312 *p_bo = bo;
1313
1314 return ret;
1315}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001316EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001317
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001318static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001319 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001320{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001321 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001322 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001323 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001324
1325 /*
1326 * Can't use standard list traversal since we're unlocking.
1327 */
1328
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001329 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001330 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001331 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001332 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001333 if (ret) {
1334 if (allow_errors) {
1335 return ret;
1336 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001337 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001338 }
1339 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001340 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001341 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001342 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001343 return 0;
1344}
1345
1346int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1347{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001348 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001349 int ret = -EINVAL;
1350
1351 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001352 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001353 return ret;
1354 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001355 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001356
1357 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001358 pr_err("Trying to take down uninitialized memory manager type %u\n",
1359 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001360 return ret;
1361 }
1362
1363 man->use_type = false;
1364 man->has_type = false;
1365
1366 ret = 0;
1367 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001368 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001369
Ben Skeggsd961db72010-08-05 10:48:18 +10001370 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001371 }
1372
1373 return ret;
1374}
1375EXPORT_SYMBOL(ttm_bo_clean_mm);
1376
1377int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1378{
1379 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1380
1381 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001382 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001383 return -EINVAL;
1384 }
1385
1386 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001387 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001388 return 0;
1389 }
1390
Jerome Glisseca262a9992009-12-08 15:33:32 +01001391 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001392}
1393EXPORT_SYMBOL(ttm_bo_evict_mm);
1394
1395int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001396 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001397{
1398 int ret = -EINVAL;
1399 struct ttm_mem_type_manager *man;
1400
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001401 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001402 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001403 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001404 man->io_reserve_fastpath = true;
1405 man->use_io_reserve_lru = false;
1406 mutex_init(&man->io_reserve_mutex);
1407 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001408
1409 ret = bdev->driver->init_mem_type(bdev, type, man);
1410 if (ret)
1411 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001412 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001413
1414 ret = 0;
1415 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001416 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001417 if (ret)
1418 return ret;
1419 }
1420 man->has_type = true;
1421 man->use_type = true;
1422 man->size = p_size;
1423
1424 INIT_LIST_HEAD(&man->lru);
1425
1426 return 0;
1427}
1428EXPORT_SYMBOL(ttm_bo_init_mm);
1429
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001430static void ttm_bo_global_kobj_release(struct kobject *kobj)
1431{
1432 struct ttm_bo_global *glob =
1433 container_of(kobj, struct ttm_bo_global, kobj);
1434
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001435 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1436 __free_page(glob->dummy_read_page);
1437 kfree(glob);
1438}
1439
Dave Airlieba4420c2010-03-09 10:56:52 +10001440void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001441{
1442 struct ttm_bo_global *glob = ref->object;
1443
1444 kobject_del(&glob->kobj);
1445 kobject_put(&glob->kobj);
1446}
1447EXPORT_SYMBOL(ttm_bo_global_release);
1448
Dave Airlieba4420c2010-03-09 10:56:52 +10001449int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001450{
1451 struct ttm_bo_global_ref *bo_ref =
1452 container_of(ref, struct ttm_bo_global_ref, ref);
1453 struct ttm_bo_global *glob = ref->object;
1454 int ret;
1455
1456 mutex_init(&glob->device_list_mutex);
1457 spin_lock_init(&glob->lru_lock);
1458 glob->mem_glob = bo_ref->mem_glob;
1459 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1460
1461 if (unlikely(glob->dummy_read_page == NULL)) {
1462 ret = -ENOMEM;
1463 goto out_no_drp;
1464 }
1465
1466 INIT_LIST_HEAD(&glob->swap_lru);
1467 INIT_LIST_HEAD(&glob->device_list);
1468
1469 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1470 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1471 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001472 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001473 goto out_no_shrink;
1474 }
1475
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001476 atomic_set(&glob->bo_count, 0);
1477
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001478 ret = kobject_init_and_add(
1479 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001480 if (unlikely(ret != 0))
1481 kobject_put(&glob->kobj);
1482 return ret;
1483out_no_shrink:
1484 __free_page(glob->dummy_read_page);
1485out_no_drp:
1486 kfree(glob);
1487 return ret;
1488}
1489EXPORT_SYMBOL(ttm_bo_global_init);
1490
1491
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001492int ttm_bo_device_release(struct ttm_bo_device *bdev)
1493{
1494 int ret = 0;
1495 unsigned i = TTM_NUM_MEM_TYPES;
1496 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001497 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001498
1499 while (i--) {
1500 man = &bdev->man[i];
1501 if (man->has_type) {
1502 man->use_type = false;
1503 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1504 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001505 pr_err("DRM memory manager type %d is not clean\n",
1506 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001507 }
1508 man->has_type = false;
1509 }
1510 }
1511
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001512 mutex_lock(&glob->device_list_mutex);
1513 list_del(&bdev->device_list);
1514 mutex_unlock(&glob->device_list_mutex);
1515
Tejun Heof094cfc2010-12-24 15:59:06 +01001516 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517
1518 while (ttm_bo_delayed_delete(bdev, true))
1519 ;
1520
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001521 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522 if (list_empty(&bdev->ddestroy))
1523 TTM_DEBUG("Delayed destroy list was clean\n");
1524
1525 if (list_empty(&bdev->man[0].lru))
1526 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001527 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001528
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1530 write_lock(&bdev->vm_lock);
1531 drm_mm_takedown(&bdev->addr_space_mm);
1532 write_unlock(&bdev->vm_lock);
1533
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534 return ret;
1535}
1536EXPORT_SYMBOL(ttm_bo_device_release);
1537
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001539 struct ttm_bo_global *glob,
1540 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001541 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001542 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001543{
1544 int ret = -EINVAL;
1545
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001546 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001547 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001548
1549 memset(bdev->man, 0, sizeof(bdev->man));
1550
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001551 /*
1552 * Initialize the system memory buffer type.
1553 * Other types need to be driver / IOCTL initialized.
1554 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001555 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001556 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001557 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001558
1559 bdev->addr_space_rb = RB_ROOT;
1560 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1561 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001562 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563
1564 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001565 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001566 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001567 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001568 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001569 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001570 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001571 mutex_lock(&glob->device_list_mutex);
1572 list_add_tail(&bdev->device_list, &glob->device_list);
1573 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001574
1575 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001576out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001577 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001578out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001579 return ret;
1580}
1581EXPORT_SYMBOL(ttm_bo_device_init);
1582
1583/*
1584 * buffer object vm functions.
1585 */
1586
1587bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1588{
1589 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1590
1591 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1592 if (mem->mem_type == TTM_PL_SYSTEM)
1593 return false;
1594
1595 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1596 return false;
1597
1598 if (mem->placement & TTM_PL_FLAG_CACHED)
1599 return false;
1600 }
1601 return true;
1602}
1603
Thomas Hellstromeba67092010-11-11 09:41:57 +01001604void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001605{
1606 struct ttm_bo_device *bdev = bo->bdev;
1607 loff_t offset = (loff_t) bo->addr_space_offset;
1608 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1609
1610 if (!bdev->dev_mapping)
1611 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001612 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001613 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001614}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001615
1616void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1617{
1618 struct ttm_bo_device *bdev = bo->bdev;
1619 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1620
1621 ttm_mem_io_lock(man, false);
1622 ttm_bo_unmap_virtual_locked(bo);
1623 ttm_mem_io_unlock(man);
1624}
1625
1626
Dave Airliee024e112009-06-24 09:48:08 +10001627EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001628
1629static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1630{
1631 struct ttm_bo_device *bdev = bo->bdev;
1632 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1633 struct rb_node *parent = NULL;
1634 struct ttm_buffer_object *cur_bo;
1635 unsigned long offset = bo->vm_node->start;
1636 unsigned long cur_offset;
1637
1638 while (*cur) {
1639 parent = *cur;
1640 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1641 cur_offset = cur_bo->vm_node->start;
1642 if (offset < cur_offset)
1643 cur = &parent->rb_left;
1644 else if (offset > cur_offset)
1645 cur = &parent->rb_right;
1646 else
1647 BUG();
1648 }
1649
1650 rb_link_node(&bo->vm_rb, parent, cur);
1651 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1652}
1653
1654/**
1655 * ttm_bo_setup_vm:
1656 *
1657 * @bo: the buffer to allocate address space for
1658 *
1659 * Allocate address space in the drm device so that applications
1660 * can mmap the buffer and access the contents. This only
1661 * applies to ttm_bo_type_device objects as others are not
1662 * placed in the drm device address space.
1663 */
1664
1665static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1666{
1667 struct ttm_bo_device *bdev = bo->bdev;
1668 int ret;
1669
1670retry_pre_get:
1671 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1672 if (unlikely(ret != 0))
1673 return ret;
1674
1675 write_lock(&bdev->vm_lock);
1676 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1677 bo->mem.num_pages, 0, 0);
1678
1679 if (unlikely(bo->vm_node == NULL)) {
1680 ret = -ENOMEM;
1681 goto out_unlock;
1682 }
1683
1684 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1685 bo->mem.num_pages, 0);
1686
1687 if (unlikely(bo->vm_node == NULL)) {
1688 write_unlock(&bdev->vm_lock);
1689 goto retry_pre_get;
1690 }
1691
1692 ttm_bo_vm_insert_rb(bo);
1693 write_unlock(&bdev->vm_lock);
1694 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1695
1696 return 0;
1697out_unlock:
1698 write_unlock(&bdev->vm_lock);
1699 return ret;
1700}
1701
1702int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001703 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001704{
1705 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001706 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001707 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001708 int ret = 0;
1709
Dave Airlie1717c0e2011-10-27 18:28:37 +02001710 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001711 return 0;
1712
Dave Airlie1717c0e2011-10-27 18:28:37 +02001713 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001714
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001715 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001716 void *tmp_obj = bo->sync_obj;
1717 bo->sync_obj = NULL;
1718 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1719 spin_unlock(&bdev->fence_lock);
1720 driver->sync_obj_unref(&tmp_obj);
1721 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001722 continue;
1723 }
1724
1725 if (no_wait)
1726 return -EBUSY;
1727
Dave Airlie1717c0e2011-10-27 18:28:37 +02001728 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001729 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001730 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001731 lazy, interruptible);
1732 if (unlikely(ret != 0)) {
1733 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001734 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001735 return ret;
1736 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001737 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001738 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001739 void *tmp_obj = bo->sync_obj;
1740 bo->sync_obj = NULL;
1741 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1742 &bo->priv_flags);
1743 spin_unlock(&bdev->fence_lock);
1744 driver->sync_obj_unref(&sync_obj);
1745 driver->sync_obj_unref(&tmp_obj);
1746 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001747 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001748 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001749 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 }
1752 }
1753 return 0;
1754}
1755EXPORT_SYMBOL(ttm_bo_wait);
1756
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001757int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1758{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001759 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001760 int ret = 0;
1761
1762 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001763 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001764 */
1765
1766 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1767 if (unlikely(ret != 0))
1768 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001769 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001770 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001771 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001772 if (likely(ret == 0))
1773 atomic_inc(&bo->cpu_writers);
1774 ttm_bo_unreserve(bo);
1775 return ret;
1776}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001777EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001778
1779void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1780{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001781 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001782}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001783EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001784
1785/**
1786 * A buffer object shrink method that tries to swap out the first
1787 * buffer object on the bo_global::swap_lru list.
1788 */
1789
1790static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1791{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001792 struct ttm_bo_global *glob =
1793 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001794 struct ttm_buffer_object *bo;
1795 int ret = -EBUSY;
1796 int put_count;
1797 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1798
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001799 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001801 if (unlikely(list_empty(&glob->swap_lru))) {
1802 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001803 return -EBUSY;
1804 }
1805
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001806 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001807 struct ttm_buffer_object, swap);
1808 kref_get(&bo->list_kref);
1809
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001810 if (!list_empty(&bo->ddestroy)) {
1811 spin_unlock(&glob->lru_lock);
1812 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1813 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma8ff3ee2012-06-01 15:39:11 +02001814 spin_lock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001815 continue;
1816 }
1817
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001818 /**
1819 * Reserve buffer. Since we unlock while sleeping, we need
1820 * to re-check that nobody removed us from the swap-list while
1821 * we slept.
1822 */
1823
1824 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1825 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001826 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001827 ttm_bo_wait_unreserved(bo, false);
1828 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001829 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001830 }
1831 }
1832
1833 BUG_ON(ret != 0);
1834 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001835 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001836
Dave Airlied6ea8882010-11-22 13:24:40 +10001837 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001838
1839 /**
1840 * Wait for GPU, then move to system cached.
1841 */
1842
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001843 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001844 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001845 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001846
1847 if (unlikely(ret != 0))
1848 goto out;
1849
1850 if ((bo->mem.placement & swap_placement) != swap_placement) {
1851 struct ttm_mem_reg evict_mem;
1852
1853 evict_mem = bo->mem;
1854 evict_mem.mm_node = NULL;
1855 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1856 evict_mem.mem_type = TTM_PL_SYSTEM;
1857
1858 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001859 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001860 if (unlikely(ret != 0))
1861 goto out;
1862 }
1863
1864 ttm_bo_unmap_virtual(bo);
1865
1866 /**
1867 * Swap out. Buffer will be swapped in again as soon as
1868 * anyone tries to access a ttm page.
1869 */
1870
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001871 if (bo->bdev->driver->swap_notify)
1872 bo->bdev->driver->swap_notify(bo);
1873
Jan Engelhardt5df23972011-04-04 01:25:18 +02001874 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001875out:
1876
1877 /**
1878 *
1879 * Unreserve without putting on LRU to avoid swapping out an
1880 * already swapped buffer.
1881 */
1882
1883 atomic_set(&bo->reserved, 0);
1884 wake_up_all(&bo->event_queue);
1885 kref_put(&bo->list_kref, ttm_bo_release_list);
1886 return ret;
1887}
1888
1889void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1890{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001891 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001892 ;
1893}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001894EXPORT_SYMBOL(ttm_bo_swapout_all);