blob: 8d9b7de2561339b03e2b191e45454eadfd454668 [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>
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +020043#include <linux/reservation.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020044
45#define TTM_ASSERT_LOCKED(param)
46#define TTM_DEBUG(fmt, arg...)
47#define TTM_BO_HASH_ORDER 13
48
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
Christian Königf1217ed2014-08-27 13:16:04 +020057static inline int ttm_mem_type_from_place(const struct ttm_place *place,
58 uint32_t *mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010059{
60 int i;
61
62 for (i = 0; i <= TTM_PL_PRIV5; i++)
Christian Königf1217ed2014-08-27 13:16:04 +020063 if (place->flags & (1 << i)) {
Jerome Glissefb53f862009-12-09 21:55:10 +010064 *mem_type = i;
65 return 0;
66 }
67 return -EINVAL;
68}
69
Jerome Glisse5012f502009-12-10 18:07:26 +010070static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010071{
Jerome Glisse5012f502009-12-10 18:07:26 +010072 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
73
Joe Perches25d04792012-03-16 21:43:50 -070074 pr_err(" has_type: %d\n", man->has_type);
75 pr_err(" use_type: %d\n", man->use_type);
76 pr_err(" flags: 0x%08X\n", man->flags);
Alex Deucher54c4cd62015-03-04 00:18:38 -050077 pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
Joe Perches25d04792012-03-16 21:43:50 -070078 pr_err(" size: %llu\n", man->size);
79 pr_err(" available_caching: 0x%08X\n", man->available_caching);
80 pr_err(" default_caching: 0x%08X\n", man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100081 if (mem_type != TTM_PL_SYSTEM)
82 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010083}
84
85static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
86 struct ttm_placement *placement)
87{
Jerome Glissefb53f862009-12-09 21:55:10 +010088 int i, ret, mem_type;
89
Joe Perches25d04792012-03-16 21:43:50 -070090 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
91 bo, bo->mem.num_pages, bo->mem.size >> 10,
92 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010093 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +020094 ret = ttm_mem_type_from_place(&placement->placement[i],
Jerome Glissefb53f862009-12-09 21:55:10 +010095 &mem_type);
96 if (ret)
97 return;
Joe Perches25d04792012-03-16 21:43:50 -070098 pr_err(" placement[%d]=0x%08X (%d)\n",
Christian Königf1217ed2014-08-27 13:16:04 +020099 i, placement->placement[i].flags, mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +0100100 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100101 }
102}
103
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200104static ssize_t ttm_bo_global_show(struct kobject *kobj,
105 struct attribute *attr,
106 char *buffer)
107{
108 struct ttm_bo_global *glob =
109 container_of(kobj, struct ttm_bo_global, kobj);
110
111 return snprintf(buffer, PAGE_SIZE, "%lu\n",
112 (unsigned long) atomic_read(&glob->bo_count));
113}
114
115static struct attribute *ttm_bo_global_attrs[] = {
116 &ttm_bo_count,
117 NULL
118};
119
Emese Revfy52cf25d2010-01-19 02:58:23 +0100120static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200121 .show = &ttm_bo_global_show
122};
123
124static struct kobj_type ttm_bo_glob_kobj_type = {
125 .release = &ttm_bo_global_kobj_release,
126 .sysfs_ops = &ttm_bo_global_ops,
127 .default_attrs = ttm_bo_global_attrs
128};
129
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200130
131static inline uint32_t ttm_bo_type_flags(unsigned type)
132{
133 return 1 << (type);
134}
135
136static void ttm_bo_release_list(struct kref *list_kref)
137{
138 struct ttm_buffer_object *bo =
139 container_of(list_kref, struct ttm_buffer_object, list_kref);
140 struct ttm_bo_device *bdev = bo->bdev;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500141 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200142
143 BUG_ON(atomic_read(&bo->list_kref.refcount));
144 BUG_ON(atomic_read(&bo->kref.refcount));
145 BUG_ON(atomic_read(&bo->cpu_writers));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200146 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);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200153 if (bo->resv == &bo->ttm_resv)
154 reservation_object_fini(&bo->ttm_resv);
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800155 mutex_destroy(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 if (bo->destroy)
157 bo->destroy(bo);
158 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200159 kfree(bo);
160 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500161 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200162}
163
Dave Airlied6ea8882010-11-22 13:24:40 +1000164void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165{
166 struct ttm_bo_device *bdev = bo->bdev;
167 struct ttm_mem_type_manager *man;
168
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200169 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200170
171 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
172
173 BUG_ON(!list_empty(&bo->lru));
174
175 man = &bdev->man[bo->mem.mem_type];
176 list_add_tail(&bo->lru, &man->lru);
177 kref_get(&bo->list_kref);
178
179 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200180 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200181 kref_get(&bo->list_kref);
182 }
183 }
184}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200185EXPORT_SYMBOL(ttm_bo_add_to_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200186
Dave Airlied6ea8882010-11-22 13:24:40 +1000187int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200188{
189 int put_count = 0;
190
191 if (!list_empty(&bo->swap)) {
192 list_del_init(&bo->swap);
193 ++put_count;
194 }
195 if (!list_empty(&bo->lru)) {
196 list_del_init(&bo->lru);
197 ++put_count;
198 }
199
200 /*
201 * TODO: Add a driver hook to delete from
202 * driver-specific LRU's here.
203 */
204
205 return put_count;
206}
207
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200208static void ttm_bo_ref_bug(struct kref *list_kref)
209{
210 BUG();
211}
212
Dave Airlied6ea8882010-11-22 13:24:40 +1000213void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
214 bool never_free)
215{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100216 kref_sub(&bo->list_kref, count,
217 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000218}
219
Maarten Lankhorst34820322013-06-27 13:48:24 +0200220void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200221{
Maarten Lankhorst34820322013-06-27 13:48:24 +0200222 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200223
Maarten Lankhorst34820322013-06-27 13:48:24 +0200224 spin_lock(&bo->glob->lru_lock);
225 put_count = ttm_bo_del_from_lru(bo);
226 spin_unlock(&bo->glob->lru_lock);
227 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200228}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200229EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200230
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200231/*
232 * Call bo->mutex locked.
233 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200234static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
235{
236 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200237 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200238 int ret = 0;
239 uint32_t page_flags = 0;
240
241 TTM_ASSERT_LOCKED(&bo->mutex);
242 bo->ttm = NULL;
243
Dave Airliead49f502009-07-10 22:36:26 +1000244 if (bdev->need_dma32)
245 page_flags |= TTM_PAGE_FLAG_DMA32;
246
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200247 switch (bo->type) {
248 case ttm_bo_type_device:
249 if (zero_alloc)
250 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
251 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400252 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
253 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254 if (unlikely(bo->ttm == NULL))
255 ret = -ENOMEM;
256 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100257 case ttm_bo_type_sg:
258 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
259 page_flags | TTM_PAGE_FLAG_SG,
260 glob->dummy_read_page);
261 if (unlikely(bo->ttm == NULL)) {
262 ret = -ENOMEM;
263 break;
264 }
265 bo->ttm->sg = bo->sg;
266 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200267 default:
Joe Perches25d04792012-03-16 21:43:50 -0700268 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200269 ret = -EINVAL;
270 break;
271 }
272
273 return ret;
274}
275
276static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
277 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000278 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000279 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200280{
281 struct ttm_bo_device *bdev = bo->bdev;
282 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
283 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
284 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
285 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
286 int ret = 0;
287
288 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100289 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
290 ret = ttm_mem_io_lock(old_man, true);
291 if (unlikely(ret != 0))
292 goto out_err;
293 ttm_bo_unmap_virtual_locked(bo);
294 ttm_mem_io_unlock(old_man);
295 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200296
297 /*
298 * Create and bind a ttm if required.
299 */
300
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000301 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
302 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000303 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
304 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000305 if (ret)
306 goto out_err;
307 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200308
309 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
310 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200311 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200312
313 if (mem->mem_type != TTM_PL_SYSTEM) {
314 ret = ttm_tt_bind(bo->ttm, mem);
315 if (ret)
316 goto out_err;
317 }
318
319 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000320 if (bdev->driver->move_notify)
321 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100322 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200324 goto moved;
325 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326 }
327
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000328 if (bdev->driver->move_notify)
329 bdev->driver->move_notify(bo, mem);
330
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200331 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
332 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000333 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200334 else if (bdev->driver->move)
335 ret = bdev->driver->move(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000336 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200337 else
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000338 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200339
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000340 if (ret) {
341 if (bdev->driver->move_notify) {
342 struct ttm_mem_reg tmp_mem = *mem;
343 *mem = bo->mem;
344 bo->mem = tmp_mem;
345 bdev->driver->move_notify(bo, mem);
346 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000347 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000348 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200349
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000350 goto out_err;
351 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500352
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200353moved:
354 if (bo->evicted) {
Rob Clark9ef75062014-03-12 10:59:37 -0400355 if (bdev->driver->invalidate_caches) {
356 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
357 if (ret)
358 pr_err("Can not flush read caches\n");
359 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360 bo->evicted = false;
361 }
362
363 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000364 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200365 bdev->man[bo->mem.mem_type].gpu_offset;
366 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100367 } else
368 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200369
370 return 0;
371
372out_err:
373 new_man = &bdev->man[bo->mem.mem_type];
374 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
375 ttm_tt_unbind(bo->ttm);
376 ttm_tt_destroy(bo->ttm);
377 bo->ttm = NULL;
378 }
379
380 return ret;
381}
382
383/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200384 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200385 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200386 * This is the place to put in driver specific hooks to release
387 * driver private resources.
388 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200389 */
390
391static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
392{
Jerome Glissedc97b342011-11-18 11:47:03 -0500393 if (bo->bdev->driver->move_notify)
394 bo->bdev->driver->move_notify(bo, NULL);
395
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200396 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200397 ttm_tt_unbind(bo->ttm);
398 ttm_tt_destroy(bo->ttm);
399 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200400 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200401 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200402
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200403 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200404}
405
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200406static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
407{
408 struct reservation_object_list *fobj;
409 struct fence *fence;
410 int i;
411
412 fobj = reservation_object_get_list(bo->resv);
413 fence = reservation_object_get_excl(bo->resv);
414 if (fence && !fence->ops->signaled)
415 fence_enable_sw_signaling(fence);
416
417 for (i = 0; fobj && i < fobj->shared_count; ++i) {
418 fence = rcu_dereference_protected(fobj->shared[i],
419 reservation_object_held(bo->resv));
420
421 if (!fence->ops->signaled)
422 fence_enable_sw_signaling(fence);
423 }
424}
425
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200426static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200427{
428 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200429 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200430 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200431 int ret;
432
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100433 spin_lock(&glob->lru_lock);
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200434 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100435
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700436 if (!ret) {
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200437 if (!ttm_bo_wait(bo, false, false, true)) {
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100438 put_count = ttm_bo_del_from_lru(bo);
439
440 spin_unlock(&glob->lru_lock);
441 ttm_bo_cleanup_memtype_use(bo);
442
443 ttm_bo_list_ref_sub(bo, put_count, true);
444
445 return;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200446 } else
447 ttm_bo_flush_all_fences(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700448
449 /*
450 * Make NO_EVICT bos immediately available to
451 * shrinkers, now that they are queued for
452 * destruction.
453 */
454 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
455 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
456 ttm_bo_add_to_lru(bo);
457 }
458
Thomas Hellstromc7523082014-02-20 11:36:25 +0100459 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700460 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200461
462 kref_get(&bo->list_kref);
463 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
464 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200465
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200466 schedule_delayed_work(&bdev->wq,
467 ((HZ / 100) < 1) ? 1 : HZ / 100);
468}
469
470/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000471 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200472 * If bo idle, remove from delayed- and lru lists, and unref.
473 * If not idle, do nothing.
474 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000475 * Must be called with lru_lock and reservation held, this function
476 * will drop both before returning.
477 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200478 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200479 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
480 */
481
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000482static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
483 bool interruptible,
484 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200485{
486 struct ttm_bo_global *glob = bo->glob;
487 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000488 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200489
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000490 ret = ttm_bo_wait(bo, false, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200491
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000492 if (ret && !no_wait_gpu) {
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200493 long lret;
494 ww_mutex_unlock(&bo->resv->lock);
495 spin_unlock(&glob->lru_lock);
496
497 lret = reservation_object_wait_timeout_rcu(bo->resv,
498 true,
499 interruptible,
500 30 * HZ);
501
502 if (lret < 0)
503 return lret;
504 else if (lret == 0)
505 return -EBUSY;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000506
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000507 spin_lock(&glob->lru_lock);
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200508 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000509
510 /*
511 * We raced, and lost, someone else holds the reservation now,
512 * and is probably busy in ttm_bo_cleanup_memtype_use.
513 *
514 * Even if it's not the case, because we finished waiting any
515 * delayed destruction would succeed, so just return success
516 * here.
517 */
518 if (ret) {
519 spin_unlock(&glob->lru_lock);
520 return 0;
521 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100522
523 /*
524 * remove sync_obj with ttm_bo_wait, the wait should be
525 * finished, and no new wait object should have been added.
526 */
Maarten Lankhorst70401382014-01-21 13:07:01 +0100527 ret = ttm_bo_wait(bo, false, false, true);
528 WARN_ON(ret);
529 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000530
531 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100532 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000533 spin_unlock(&glob->lru_lock);
534 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200535 }
536
537 put_count = ttm_bo_del_from_lru(bo);
538 list_del_init(&bo->ddestroy);
539 ++put_count;
540
541 spin_unlock(&glob->lru_lock);
542 ttm_bo_cleanup_memtype_use(bo);
543
Dave Airlied6ea8882010-11-22 13:24:40 +1000544 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200545
546 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200547}
548
549/**
550 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
551 * encountered buffers.
552 */
553
554static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
555{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200556 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100557 struct ttm_buffer_object *entry = NULL;
558 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200559
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200560 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100561 if (list_empty(&bdev->ddestroy))
562 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200563
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100564 entry = list_first_entry(&bdev->ddestroy,
565 struct ttm_buffer_object, ddestroy);
566 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200567
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100568 for (;;) {
569 struct ttm_buffer_object *nentry = NULL;
570
571 if (entry->ddestroy.next != &bdev->ddestroy) {
572 nentry = list_first_entry(&entry->ddestroy,
573 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200574 kref_get(&nentry->list_kref);
575 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200576
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200577 ret = __ttm_bo_reserve(entry, false, true, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100578 if (remove_all && ret) {
579 spin_unlock(&glob->lru_lock);
Thomas Hellstromc7523082014-02-20 11:36:25 +0100580 ret = __ttm_bo_reserve(entry, false, false,
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200581 false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100582 spin_lock(&glob->lru_lock);
583 }
584
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000585 if (!ret)
586 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
587 !remove_all);
588 else
589 spin_unlock(&glob->lru_lock);
590
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200591 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100592 entry = nentry;
593
594 if (ret || !entry)
595 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200596
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200597 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100598 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200599 break;
600 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200601
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100602out_unlock:
603 spin_unlock(&glob->lru_lock);
604out:
605 if (entry)
606 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200607 return ret;
608}
609
610static void ttm_bo_delayed_workqueue(struct work_struct *work)
611{
612 struct ttm_bo_device *bdev =
613 container_of(work, struct ttm_bo_device, wq.work);
614
615 if (ttm_bo_delayed_delete(bdev, false)) {
616 schedule_delayed_work(&bdev->wq,
617 ((HZ / 100) < 1) ? 1 : HZ / 100);
618 }
619}
620
621static void ttm_bo_release(struct kref *kref)
622{
623 struct ttm_buffer_object *bo =
624 container_of(kref, struct ttm_buffer_object, kref);
625 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100626 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200627
David Herrmann72525b32013-07-24 21:08:53 +0200628 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100629 ttm_mem_io_lock(man, false);
630 ttm_mem_io_free_vm(bo);
631 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200632 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200633 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200634}
635
636void ttm_bo_unref(struct ttm_buffer_object **p_bo)
637{
638 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200639
640 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200641 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200642}
643EXPORT_SYMBOL(ttm_bo_unref);
644
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400645int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
646{
647 return cancel_delayed_work_sync(&bdev->wq);
648}
649EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
650
651void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
652{
653 if (resched)
654 schedule_delayed_work(&bdev->wq,
655 ((HZ / 100) < 1) ? 1 : HZ / 100);
656}
657EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
658
Jerome Glisseca262a9992009-12-08 15:33:32 +0100659static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000660 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200661{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200662 struct ttm_bo_device *bdev = bo->bdev;
663 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100664 struct ttm_placement placement;
665 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200666
Dave Airlie1717c0e2011-10-27 18:28:37 +0200667 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200668
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200669 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100670 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700671 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200672 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200673 goto out;
674 }
675
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200676 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200677
678 evict_mem = bo->mem;
679 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100680 evict_mem.bus.io_reserved_vm = false;
681 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200682
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100683 placement.num_placement = 0;
684 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100685 bdev->driver->evict_flags(bo, &placement);
686 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000687 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200688 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100689 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700690 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
691 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100692 ttm_bo_mem_space_debug(bo, &placement);
693 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200694 goto out;
695 }
696
697 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000698 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200699 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100700 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700701 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000702 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200703 goto out;
704 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200705 bo->evicted = true;
706out:
707 return ret;
708}
709
Jerome Glisseca262a9992009-12-08 15:33:32 +0100710static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
711 uint32_t mem_type,
Michel Dänzere3001802014-10-09 15:02:59 +0900712 const struct ttm_place *place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000713 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000714 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100715{
716 struct ttm_bo_global *glob = bdev->glob;
717 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
718 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000719 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100720
721 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000722 list_for_each_entry(bo, &man->lru, lru) {
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200723 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Michel Dänzere3001802014-10-09 15:02:59 +0900724 if (!ret) {
725 if (place && (place->fpfn || place->lpfn)) {
726 /* Don't evict this BO if it's outside of the
727 * requested placement range
728 */
729 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
730 (place->lpfn && place->lpfn <= bo->mem.start)) {
731 __ttm_bo_unreserve(bo);
732 ret = -EBUSY;
733 continue;
734 }
735 }
736
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000737 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900738 }
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100739 }
740
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000741 if (ret) {
742 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000743 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200744 }
745
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000746 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100747
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000748 if (!list_empty(&bo->ddestroy)) {
749 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
750 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100751 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000752 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100753 }
754
755 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100756 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100757
758 BUG_ON(ret != 0);
759
Dave Airlied6ea8882010-11-22 13:24:40 +1000760 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100761
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000762 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100763 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100764
Jerome Glisseca262a9992009-12-08 15:33:32 +0100765 kref_put(&bo->list_kref, ttm_bo_release_list);
766 return ret;
767}
768
Ben Skeggs42311ff2010-08-04 12:07:08 +1000769void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
770{
Ben Skeggsd961db72010-08-05 10:48:18 +1000771 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000772
Ben Skeggsd961db72010-08-05 10:48:18 +1000773 if (mem->mm_node)
774 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000775}
776EXPORT_SYMBOL(ttm_bo_mem_put);
777
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200778/**
779 * Repeatedly evict memory from the LRU for @mem_type until we create enough
780 * space, or we've evicted everything and there isn't enough space.
781 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100782static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
783 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200784 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100785 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000786 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000787 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200788{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100789 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200790 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200791 int ret;
792
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200793 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200794 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200795 if (unlikely(ret != 0))
796 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000797 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100798 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900799 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000800 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100801 if (unlikely(ret != 0))
802 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200803 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000804 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200805 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200806 mem->mem_type = mem_type;
807 return 0;
808}
809
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200810static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
811 uint32_t cur_placement,
812 uint32_t proposed_placement)
813{
814 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
815 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
816
817 /**
818 * Keep current caching if possible.
819 */
820
821 if ((cur_placement & caching) != 0)
822 result |= (cur_placement & caching);
823 else if ((man->default_caching & caching) != 0)
824 result |= man->default_caching;
825 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
826 result |= TTM_PL_FLAG_CACHED;
827 else if ((TTM_PL_FLAG_WC & caching) != 0)
828 result |= TTM_PL_FLAG_WC;
829 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
830 result |= TTM_PL_FLAG_UNCACHED;
831
832 return result;
833}
834
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200835static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200836 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200837 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200838 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200839{
840 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
841
Christian Königf1217ed2014-08-27 13:16:04 +0200842 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200843 return false;
844
Christian Königf1217ed2014-08-27 13:16:04 +0200845 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200846 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200847
Christian Königf1217ed2014-08-27 13:16:04 +0200848 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200849
850 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200851 return true;
852}
853
854/**
855 * Creates space for memory region @mem according to its type.
856 *
857 * This function first searches for free space in compatible memory types in
858 * the priority order defined by the driver. If free space isn't found, then
859 * ttm_bo_mem_force_space is attempted in priority order to evict and find
860 * space.
861 */
862int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100863 struct ttm_placement *placement,
864 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000865 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000866 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867{
868 struct ttm_bo_device *bdev = bo->bdev;
869 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200870 uint32_t mem_type = TTM_PL_SYSTEM;
871 uint32_t cur_flags = 0;
872 bool type_found = false;
873 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100874 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100875 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200876
877 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000878 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200879 const struct ttm_place *place = &placement->placement[i];
880
881 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100882 if (ret)
883 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200884 man = &bdev->man[mem_type];
885
Christian Königf1217ed2014-08-27 13:16:04 +0200886 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100887 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200888
889 if (!type_ok)
890 continue;
891
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200892 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
893 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100894 /*
895 * Use the access and other non-mapping-related flag bits from
896 * the memory placement flags to the current flags
897 */
Christian Königf1217ed2014-08-27 13:16:04 +0200898 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100899 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200900
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200901 if (mem_type == TTM_PL_SYSTEM)
902 break;
903
904 if (man->has_type && man->use_type) {
905 type_found = true;
Christian Königf1217ed2014-08-27 13:16:04 +0200906 ret = (*man->func->get_node)(man, bo, place, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100907 if (unlikely(ret))
908 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200909 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000910 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200911 break;
912 }
913
Ben Skeggsd961db72010-08-05 10:48:18 +1000914 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200915 mem->mem_type = mem_type;
916 mem->placement = cur_flags;
917 return 0;
918 }
919
920 if (!type_found)
921 return -EINVAL;
922
Dave Airlieb6637522009-12-14 14:51:35 +1000923 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200924 const struct ttm_place *place = &placement->busy_placement[i];
925
926 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100927 if (ret)
928 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200930 if (!man->has_type)
931 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200932 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200933 continue;
934
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200935 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
936 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100937 /*
938 * Use the access and other non-mapping-related flag bits from
939 * the memory placement flags to the current flags
940 */
Christian Königf1217ed2014-08-27 13:16:04 +0200941 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100942 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200943
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100944 if (mem_type == TTM_PL_SYSTEM) {
945 mem->mem_type = mem_type;
946 mem->placement = cur_flags;
947 mem->mm_node = NULL;
948 return 0;
949 }
950
Christian Königf1217ed2014-08-27 13:16:04 +0200951 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000952 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953 if (ret == 0 && mem->mm_node) {
954 mem->placement = cur_flags;
955 return 0;
956 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100957 if (ret == -ERESTARTSYS)
958 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200959 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100960 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200961 return ret;
962}
963EXPORT_SYMBOL(ttm_bo_mem_space);
964
Rashika Kheria6e87fa42014-01-06 22:12:58 +0530965static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100966 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000967 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000968 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200969{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970 int ret = 0;
971 struct ttm_mem_reg mem;
972
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200973 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200974
975 /*
976 * FIXME: It's possible to pipeline buffer moves.
977 * Have the driver move function wait for idle when necessary,
978 * instead of doing it here.
979 */
Dave Airlie1717c0e2011-10-27 18:28:37 +0200980 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200981 if (ret)
982 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200983 mem.num_pages = bo->num_pages;
984 mem.size = mem.num_pages << PAGE_SHIFT;
985 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100986 mem.bus.io_reserved_vm = false;
987 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988 /*
989 * Determine where to move the buffer.
990 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000991 ret = ttm_bo_mem_space(bo, placement, &mem,
992 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200993 if (ret)
994 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000995 ret = ttm_bo_handle_move_mem(bo, &mem, false,
996 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +1000998 if (ret && mem.mm_node)
999 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001000 return ret;
1001}
1002
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001003static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1004 struct ttm_mem_reg *mem,
1005 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001007 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001008
Jerome Glisseca262a9992009-12-08 15:33:32 +01001009 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001010 const struct ttm_place *heap = &placement->placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001011 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001012 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001013 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001014 continue;
1015
1016 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001017 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1018 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1019 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001020 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001021
1022 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001023 const struct ttm_place *heap = &placement->busy_placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001024 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001025 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001026 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001027 continue;
1028
1029 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001030 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1031 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1032 return true;
1033 }
1034
1035 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036}
1037
Jerome Glisse09855ac2009-12-10 17:16:27 +01001038int ttm_bo_validate(struct ttm_buffer_object *bo,
1039 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001040 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001041 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001042{
1043 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001044 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001045
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001046 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001047 /*
1048 * Check whether we need to move buffer.
1049 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001050 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001051 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1052 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001053 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001054 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001055 } else {
1056 /*
1057 * Use the access and other non-mapping-related flag bits from
1058 * the compatible memory placement flags to the active flags
1059 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001060 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001061 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001062 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001063 /*
1064 * We might need to add a TTM.
1065 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001066 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1067 ret = ttm_bo_add_ttm(bo, true);
1068 if (ret)
1069 return ret;
1070 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001071 return 0;
1072}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001073EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074
Jerome Glisse09855ac2009-12-10 17:16:27 +01001075int ttm_bo_init(struct ttm_bo_device *bdev,
1076 struct ttm_buffer_object *bo,
1077 unsigned long size,
1078 enum ttm_bo_type type,
1079 struct ttm_placement *placement,
1080 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001081 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001082 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001083 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001084 struct sg_table *sg,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001085 struct reservation_object *resv,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001086 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001087{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001088 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001090 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001091 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001092
1093 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1094 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001095 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001096 if (destroy)
1097 (*destroy)(bo);
1098 else
1099 kfree(bo);
1100 return -ENOMEM;
1101 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001103 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1104 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001105 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001106 if (destroy)
1107 (*destroy)(bo);
1108 else
1109 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001110 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001111 return -EINVAL;
1112 }
1113 bo->destroy = destroy;
1114
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001115 kref_init(&bo->kref);
1116 kref_init(&bo->list_kref);
1117 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001118 INIT_LIST_HEAD(&bo->lru);
1119 INIT_LIST_HEAD(&bo->ddestroy);
1120 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001121 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001122 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001123 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001124 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125 bo->type = type;
1126 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001127 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001128 bo->mem.mem_type = TTM_PL_SYSTEM;
1129 bo->mem.num_pages = bo->num_pages;
1130 bo->mem.mm_node = NULL;
1131 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001132 bo->mem.bus.io_reserved_vm = false;
1133 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001134 bo->priv_flags = 0;
1135 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001136 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001137 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001138 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001139 if (resv) {
1140 bo->resv = resv;
1141 lockdep_assert_held(&bo->resv->lock.base);
1142 } else {
1143 bo->resv = &bo->ttm_resv;
1144 reservation_object_init(&bo->ttm_resv);
1145 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001146 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001147 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001148
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001150 * For ttm_bo_type_device buffers, allocate
1151 * address space from the device.
1152 */
Christian Königf1217ed2014-08-27 13:16:04 +02001153 if (bo->type == ttm_bo_type_device ||
1154 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001155 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1156 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001157
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001158 /* passed reservation objects should already be locked,
1159 * since otherwise lockdep will be angered in radeon.
1160 */
1161 if (!resv) {
1162 locked = ww_mutex_trylock(&bo->resv->lock);
1163 WARN_ON(!locked);
1164 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001165
1166 if (likely(!ret))
1167 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001168
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001169 if (!resv)
1170 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001171
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001172 if (unlikely(ret))
1173 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001174
1175 return ret;
1176}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001177EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001179size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1180 unsigned long bo_size,
1181 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001182{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001183 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1184 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001186 size += ttm_round_pot(struct_size);
1187 size += PAGE_ALIGN(npages * sizeof(void *));
1188 size += ttm_round_pot(sizeof(struct ttm_tt));
1189 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001190}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001191EXPORT_SYMBOL(ttm_bo_acc_size);
1192
1193size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1194 unsigned long bo_size,
1195 unsigned struct_size)
1196{
1197 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1198 size_t size = 0;
1199
1200 size += ttm_round_pot(struct_size);
1201 size += PAGE_ALIGN(npages * sizeof(void *));
1202 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1203 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1204 return size;
1205}
1206EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001207
Jerome Glisse09855ac2009-12-10 17:16:27 +01001208int ttm_bo_create(struct ttm_bo_device *bdev,
1209 unsigned long size,
1210 enum ttm_bo_type type,
1211 struct ttm_placement *placement,
1212 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001213 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001214 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001215 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001216{
1217 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001218 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001219 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001220
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001222 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001224
Thomas Hellstroma393c732012-06-12 13:28:42 +02001225 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001226 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001227 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001228 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001229 if (likely(ret == 0))
1230 *p_bo = bo;
1231
1232 return ret;
1233}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001234EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001235
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001236static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001237 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001238{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001239 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001240 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001241 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001242
1243 /*
1244 * Can't use standard list traversal since we're unlocking.
1245 */
1246
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001247 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001248 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001249 spin_unlock(&glob->lru_lock);
Michel Dänzere3001802014-10-09 15:02:59 +09001250 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001251 if (ret) {
1252 if (allow_errors) {
1253 return ret;
1254 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001255 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001256 }
1257 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001258 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001259 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001260 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001261 return 0;
1262}
1263
1264int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1265{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001266 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001267 int ret = -EINVAL;
1268
1269 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001270 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271 return ret;
1272 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001273 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001274
1275 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001276 pr_err("Trying to take down uninitialized memory manager type %u\n",
1277 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001278 return ret;
1279 }
1280
1281 man->use_type = false;
1282 man->has_type = false;
1283
1284 ret = 0;
1285 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001286 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001287
Ben Skeggsd961db72010-08-05 10:48:18 +10001288 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001289 }
1290
1291 return ret;
1292}
1293EXPORT_SYMBOL(ttm_bo_clean_mm);
1294
1295int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1296{
1297 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1298
1299 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001300 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001301 return -EINVAL;
1302 }
1303
1304 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001305 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306 return 0;
1307 }
1308
Jerome Glisseca262a9992009-12-08 15:33:32 +01001309 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001310}
1311EXPORT_SYMBOL(ttm_bo_evict_mm);
1312
1313int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001314 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001315{
1316 int ret = -EINVAL;
1317 struct ttm_mem_type_manager *man;
1318
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001319 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001320 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001321 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001322 man->io_reserve_fastpath = true;
1323 man->use_io_reserve_lru = false;
1324 mutex_init(&man->io_reserve_mutex);
1325 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001326
1327 ret = bdev->driver->init_mem_type(bdev, type, man);
1328 if (ret)
1329 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001330 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001331
1332 ret = 0;
1333 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001334 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001335 if (ret)
1336 return ret;
1337 }
1338 man->has_type = true;
1339 man->use_type = true;
1340 man->size = p_size;
1341
1342 INIT_LIST_HEAD(&man->lru);
1343
1344 return 0;
1345}
1346EXPORT_SYMBOL(ttm_bo_init_mm);
1347
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001348static void ttm_bo_global_kobj_release(struct kobject *kobj)
1349{
1350 struct ttm_bo_global *glob =
1351 container_of(kobj, struct ttm_bo_global, kobj);
1352
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001353 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1354 __free_page(glob->dummy_read_page);
1355 kfree(glob);
1356}
1357
Dave Airlieba4420c2010-03-09 10:56:52 +10001358void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001359{
1360 struct ttm_bo_global *glob = ref->object;
1361
1362 kobject_del(&glob->kobj);
1363 kobject_put(&glob->kobj);
1364}
1365EXPORT_SYMBOL(ttm_bo_global_release);
1366
Dave Airlieba4420c2010-03-09 10:56:52 +10001367int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001368{
1369 struct ttm_bo_global_ref *bo_ref =
1370 container_of(ref, struct ttm_bo_global_ref, ref);
1371 struct ttm_bo_global *glob = ref->object;
1372 int ret;
1373
1374 mutex_init(&glob->device_list_mutex);
1375 spin_lock_init(&glob->lru_lock);
1376 glob->mem_glob = bo_ref->mem_glob;
1377 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1378
1379 if (unlikely(glob->dummy_read_page == NULL)) {
1380 ret = -ENOMEM;
1381 goto out_no_drp;
1382 }
1383
1384 INIT_LIST_HEAD(&glob->swap_lru);
1385 INIT_LIST_HEAD(&glob->device_list);
1386
1387 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1388 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1389 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001390 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001391 goto out_no_shrink;
1392 }
1393
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001394 atomic_set(&glob->bo_count, 0);
1395
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001396 ret = kobject_init_and_add(
1397 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001398 if (unlikely(ret != 0))
1399 kobject_put(&glob->kobj);
1400 return ret;
1401out_no_shrink:
1402 __free_page(glob->dummy_read_page);
1403out_no_drp:
1404 kfree(glob);
1405 return ret;
1406}
1407EXPORT_SYMBOL(ttm_bo_global_init);
1408
1409
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001410int ttm_bo_device_release(struct ttm_bo_device *bdev)
1411{
1412 int ret = 0;
1413 unsigned i = TTM_NUM_MEM_TYPES;
1414 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001415 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001416
1417 while (i--) {
1418 man = &bdev->man[i];
1419 if (man->has_type) {
1420 man->use_type = false;
1421 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1422 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001423 pr_err("DRM memory manager type %d is not clean\n",
1424 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001425 }
1426 man->has_type = false;
1427 }
1428 }
1429
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001430 mutex_lock(&glob->device_list_mutex);
1431 list_del(&bdev->device_list);
1432 mutex_unlock(&glob->device_list_mutex);
1433
Tejun Heof094cfc2010-12-24 15:59:06 +01001434 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001435
1436 while (ttm_bo_delayed_delete(bdev, true))
1437 ;
1438
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001439 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001440 if (list_empty(&bdev->ddestroy))
1441 TTM_DEBUG("Delayed destroy list was clean\n");
1442
1443 if (list_empty(&bdev->man[0].lru))
1444 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001445 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001446
David Herrmann72525b32013-07-24 21:08:53 +02001447 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001448
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001449 return ret;
1450}
1451EXPORT_SYMBOL(ttm_bo_device_release);
1452
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001453int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001454 struct ttm_bo_global *glob,
1455 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001456 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001457 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001458 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001459{
1460 int ret = -EINVAL;
1461
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001462 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001463
1464 memset(bdev->man, 0, sizeof(bdev->man));
1465
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001466 /*
1467 * Initialize the system memory buffer type.
1468 * Other types need to be driver / IOCTL initialized.
1469 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001470 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001471 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001472 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001473
David Herrmann72525b32013-07-24 21:08:53 +02001474 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1475 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001476 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001477 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001478 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001479 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001480 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001481 bdev->val_seq = 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001482 mutex_lock(&glob->device_list_mutex);
1483 list_add_tail(&bdev->device_list, &glob->device_list);
1484 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001485
1486 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001487out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488 return ret;
1489}
1490EXPORT_SYMBOL(ttm_bo_device_init);
1491
1492/*
1493 * buffer object vm functions.
1494 */
1495
1496bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1497{
1498 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1499
1500 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1501 if (mem->mem_type == TTM_PL_SYSTEM)
1502 return false;
1503
1504 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1505 return false;
1506
1507 if (mem->placement & TTM_PL_FLAG_CACHED)
1508 return false;
1509 }
1510 return true;
1511}
1512
Thomas Hellstromeba67092010-11-11 09:41:57 +01001513void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001514{
1515 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001516
David Herrmann51335df2013-07-24 21:10:03 +02001517 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001518 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001519}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001520
1521void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1522{
1523 struct ttm_bo_device *bdev = bo->bdev;
1524 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1525
1526 ttm_mem_io_lock(man, false);
1527 ttm_bo_unmap_virtual_locked(bo);
1528 ttm_mem_io_unlock(man);
1529}
1530
1531
Dave Airliee024e112009-06-24 09:48:08 +10001532EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001533
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001535 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536{
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001537 struct reservation_object_list *fobj;
1538 struct reservation_object *resv;
1539 struct fence *excl;
1540 long timeout = 15 * HZ;
1541 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001542
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001543 resv = bo->resv;
1544 fobj = reservation_object_get_list(resv);
1545 excl = reservation_object_get_excl(resv);
1546 if (excl) {
1547 if (!fence_is_signaled(excl)) {
1548 if (no_wait)
1549 return -EBUSY;
Maarten Lankhorst70401382014-01-21 13:07:01 +01001550
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001551 timeout = fence_wait_timeout(excl,
1552 interruptible, timeout);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001554 }
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001555
1556 for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
1557 struct fence *fence;
1558 fence = rcu_dereference_protected(fobj->shared[i],
1559 reservation_object_held(resv));
1560
1561 if (!fence_is_signaled(fence)) {
1562 if (no_wait)
1563 return -EBUSY;
1564
1565 timeout = fence_wait_timeout(fence,
1566 interruptible, timeout);
1567 }
1568 }
1569
1570 if (timeout < 0)
1571 return timeout;
1572
1573 if (timeout == 0)
1574 return -EBUSY;
1575
1576 reservation_object_add_excl_fence(resv, NULL);
1577 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1578 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001579}
1580EXPORT_SYMBOL(ttm_bo_wait);
1581
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001582int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1583{
1584 int ret = 0;
1585
1586 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001587 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001588 */
1589
Martin Kepplinger0eff2a22014-06-15 02:10:39 +02001590 ret = ttm_bo_reserve(bo, true, no_wait, false, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001591 if (unlikely(ret != 0))
1592 return ret;
Dave Airlie1717c0e2011-10-27 18:28:37 +02001593 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001594 if (likely(ret == 0))
1595 atomic_inc(&bo->cpu_writers);
1596 ttm_bo_unreserve(bo);
1597 return ret;
1598}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001599EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001600
1601void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1602{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001603 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001604}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001605EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001606
1607/**
1608 * A buffer object shrink method that tries to swap out the first
1609 * buffer object on the bo_global::swap_lru list.
1610 */
1611
1612static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1613{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001614 struct ttm_bo_global *glob =
1615 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001616 struct ttm_buffer_object *bo;
1617 int ret = -EBUSY;
1618 int put_count;
1619 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1620
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001621 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001622 list_for_each_entry(bo, &glob->swap_lru, swap) {
Martin Kepplinger0eff2a22014-06-15 02:10:39 +02001623 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001624 if (!ret)
1625 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001626 }
1627
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001628 if (ret) {
1629 spin_unlock(&glob->lru_lock);
1630 return ret;
1631 }
1632
1633 kref_get(&bo->list_kref);
1634
1635 if (!list_empty(&bo->ddestroy)) {
1636 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1637 kref_put(&bo->list_kref, ttm_bo_release_list);
1638 return ret;
1639 }
1640
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001641 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001642 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001643
Dave Airlied6ea8882010-11-22 13:24:40 +10001644 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001645
1646 /**
1647 * Wait for GPU, then move to system cached.
1648 */
1649
Dave Airlie1717c0e2011-10-27 18:28:37 +02001650 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001651
1652 if (unlikely(ret != 0))
1653 goto out;
1654
1655 if ((bo->mem.placement & swap_placement) != swap_placement) {
1656 struct ttm_mem_reg evict_mem;
1657
1658 evict_mem = bo->mem;
1659 evict_mem.mm_node = NULL;
1660 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1661 evict_mem.mem_type = TTM_PL_SYSTEM;
1662
1663 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001664 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001665 if (unlikely(ret != 0))
1666 goto out;
1667 }
1668
1669 ttm_bo_unmap_virtual(bo);
1670
1671 /**
1672 * Swap out. Buffer will be swapped in again as soon as
1673 * anyone tries to access a ttm page.
1674 */
1675
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001676 if (bo->bdev->driver->swap_notify)
1677 bo->bdev->driver->swap_notify(bo);
1678
Jan Engelhardt5df23972011-04-04 01:25:18 +02001679 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001680out:
1681
1682 /**
1683 *
1684 * Unreserve without putting on LRU to avoid swapping out an
1685 * already swapped buffer.
1686 */
1687
Thomas Hellstromc7523082014-02-20 11:36:25 +01001688 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001689 kref_put(&bo->list_kref, ttm_bo_release_list);
1690 return ret;
1691}
1692
1693void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1694{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001695 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001696 ;
1697}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001698EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001699
1700/**
1701 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1702 * unreserved
1703 *
1704 * @bo: Pointer to buffer
1705 */
1706int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1707{
1708 int ret;
1709
1710 /*
1711 * In the absense of a wait_unlocked API,
1712 * Use the bo::wu_mutex to avoid triggering livelocks due to
1713 * concurrent use of this function. Note that this use of
1714 * bo::wu_mutex can go away if we change locking order to
1715 * mmap_sem -> bo::reserve.
1716 */
1717 ret = mutex_lock_interruptible(&bo->wu_mutex);
1718 if (unlikely(ret != 0))
1719 return -ERESTARTSYS;
1720 if (!ww_mutex_is_locked(&bo->resv->lock))
1721 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001722 ret = __ttm_bo_reserve(bo, true, false, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001723 if (unlikely(ret != 0))
1724 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001725 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001726
1727out_unlock:
1728 mutex_unlock(&bo->wu_mutex);
1729 return ret;
1730}