blob: 66707be386f74ce59cc5ef13a44d9a8058b71293 [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);
77 pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
78 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
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200470static int ttm_bo_unreserve_and_wait(struct ttm_buffer_object *bo,
471 bool interruptible)
472{
473 struct ttm_bo_global *glob = bo->glob;
474 struct reservation_object_list *fobj;
475 struct fence *excl = NULL;
476 struct fence **shared = NULL;
477 u32 shared_count = 0, i;
478 int ret = 0;
479
480 fobj = reservation_object_get_list(bo->resv);
481 if (fobj && fobj->shared_count) {
482 shared = kmalloc(sizeof(*shared) * fobj->shared_count,
483 GFP_KERNEL);
484
485 if (!shared) {
486 ret = -ENOMEM;
487 __ttm_bo_unreserve(bo);
488 spin_unlock(&glob->lru_lock);
489 return ret;
490 }
491
492 for (i = 0; i < fobj->shared_count; ++i) {
493 if (!fence_is_signaled(fobj->shared[i])) {
494 fence_get(fobj->shared[i]);
495 shared[shared_count++] = fobj->shared[i];
496 }
497 }
498 if (!shared_count) {
499 kfree(shared);
500 shared = NULL;
501 }
502 }
503
504 excl = reservation_object_get_excl(bo->resv);
505 if (excl && !fence_is_signaled(excl))
506 fence_get(excl);
507 else
508 excl = NULL;
509
510 __ttm_bo_unreserve(bo);
511 spin_unlock(&glob->lru_lock);
512
513 if (excl) {
514 ret = fence_wait(excl, interruptible);
515 fence_put(excl);
516 }
517
518 if (shared_count > 0) {
519 for (i = 0; i < shared_count; ++i) {
520 if (!ret)
521 ret = fence_wait(shared[i], interruptible);
522 fence_put(shared[i]);
523 }
524 kfree(shared);
525 }
526
527 return ret;
528}
529
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200530/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000531 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200532 * If bo idle, remove from delayed- and lru lists, and unref.
533 * If not idle, do nothing.
534 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000535 * Must be called with lru_lock and reservation held, this function
536 * will drop both before returning.
537 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200538 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200539 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
540 */
541
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000542static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
543 bool interruptible,
544 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200545{
546 struct ttm_bo_global *glob = bo->glob;
547 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000548 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200549
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000550 ret = ttm_bo_wait(bo, false, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200551
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000552 if (ret && !no_wait_gpu) {
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200553 ret = ttm_bo_unreserve_and_wait(bo, interruptible);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000554 if (ret)
555 return ret;
556
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000557 spin_lock(&glob->lru_lock);
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200558 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000559
560 /*
561 * We raced, and lost, someone else holds the reservation now,
562 * and is probably busy in ttm_bo_cleanup_memtype_use.
563 *
564 * Even if it's not the case, because we finished waiting any
565 * delayed destruction would succeed, so just return success
566 * here.
567 */
568 if (ret) {
569 spin_unlock(&glob->lru_lock);
570 return 0;
571 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100572
573 /*
574 * remove sync_obj with ttm_bo_wait, the wait should be
575 * finished, and no new wait object should have been added.
576 */
Maarten Lankhorst70401382014-01-21 13:07:01 +0100577 ret = ttm_bo_wait(bo, false, false, true);
578 WARN_ON(ret);
579 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000580
581 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100582 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000583 spin_unlock(&glob->lru_lock);
584 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200585 }
586
587 put_count = ttm_bo_del_from_lru(bo);
588 list_del_init(&bo->ddestroy);
589 ++put_count;
590
591 spin_unlock(&glob->lru_lock);
592 ttm_bo_cleanup_memtype_use(bo);
593
Dave Airlied6ea8882010-11-22 13:24:40 +1000594 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200595
596 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200597}
598
599/**
600 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
601 * encountered buffers.
602 */
603
604static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
605{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200606 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100607 struct ttm_buffer_object *entry = NULL;
608 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200609
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200610 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100611 if (list_empty(&bdev->ddestroy))
612 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200613
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100614 entry = list_first_entry(&bdev->ddestroy,
615 struct ttm_buffer_object, ddestroy);
616 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200617
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100618 for (;;) {
619 struct ttm_buffer_object *nentry = NULL;
620
621 if (entry->ddestroy.next != &bdev->ddestroy) {
622 nentry = list_first_entry(&entry->ddestroy,
623 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200624 kref_get(&nentry->list_kref);
625 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200626
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200627 ret = __ttm_bo_reserve(entry, false, true, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100628 if (remove_all && ret) {
629 spin_unlock(&glob->lru_lock);
Thomas Hellstromc7523082014-02-20 11:36:25 +0100630 ret = __ttm_bo_reserve(entry, false, false,
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200631 false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100632 spin_lock(&glob->lru_lock);
633 }
634
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000635 if (!ret)
636 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
637 !remove_all);
638 else
639 spin_unlock(&glob->lru_lock);
640
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200641 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100642 entry = nentry;
643
644 if (ret || !entry)
645 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200646
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200647 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100648 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649 break;
650 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200651
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100652out_unlock:
653 spin_unlock(&glob->lru_lock);
654out:
655 if (entry)
656 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200657 return ret;
658}
659
660static void ttm_bo_delayed_workqueue(struct work_struct *work)
661{
662 struct ttm_bo_device *bdev =
663 container_of(work, struct ttm_bo_device, wq.work);
664
665 if (ttm_bo_delayed_delete(bdev, false)) {
666 schedule_delayed_work(&bdev->wq,
667 ((HZ / 100) < 1) ? 1 : HZ / 100);
668 }
669}
670
671static void ttm_bo_release(struct kref *kref)
672{
673 struct ttm_buffer_object *bo =
674 container_of(kref, struct ttm_buffer_object, kref);
675 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100676 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200677
David Herrmann72525b32013-07-24 21:08:53 +0200678 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100679 ttm_mem_io_lock(man, false);
680 ttm_mem_io_free_vm(bo);
681 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200682 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200683 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200684}
685
686void ttm_bo_unref(struct ttm_buffer_object **p_bo)
687{
688 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200689
690 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200691 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200692}
693EXPORT_SYMBOL(ttm_bo_unref);
694
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400695int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
696{
697 return cancel_delayed_work_sync(&bdev->wq);
698}
699EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
700
701void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
702{
703 if (resched)
704 schedule_delayed_work(&bdev->wq,
705 ((HZ / 100) < 1) ? 1 : HZ / 100);
706}
707EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
708
Jerome Glisseca262a9992009-12-08 15:33:32 +0100709static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000710 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200711{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200712 struct ttm_bo_device *bdev = bo->bdev;
713 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100714 struct ttm_placement placement;
715 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200716
Dave Airlie1717c0e2011-10-27 18:28:37 +0200717 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200718
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200719 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100720 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700721 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200722 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200723 goto out;
724 }
725
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200726 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200727
728 evict_mem = bo->mem;
729 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100730 evict_mem.bus.io_reserved_vm = false;
731 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200732
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100733 placement.num_placement = 0;
734 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100735 bdev->driver->evict_flags(bo, &placement);
736 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000737 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200738 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100739 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700740 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
741 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100742 ttm_bo_mem_space_debug(bo, &placement);
743 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200744 goto out;
745 }
746
747 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000748 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200749 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100750 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700751 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000752 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200753 goto out;
754 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200755 bo->evicted = true;
756out:
757 return ret;
758}
759
Jerome Glisseca262a9992009-12-08 15:33:32 +0100760static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
761 uint32_t mem_type,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000762 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000763 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100764{
765 struct ttm_bo_global *glob = bdev->glob;
766 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
767 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000768 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100769
770 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000771 list_for_each_entry(bo, &man->lru, lru) {
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200772 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000773 if (!ret)
774 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100775 }
776
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000777 if (ret) {
778 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000779 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200780 }
781
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000782 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100783
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000784 if (!list_empty(&bo->ddestroy)) {
785 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
786 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100787 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000788 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100789 }
790
791 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100792 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100793
794 BUG_ON(ret != 0);
795
Dave Airlied6ea8882010-11-22 13:24:40 +1000796 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100797
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000798 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100799 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100800
Jerome Glisseca262a9992009-12-08 15:33:32 +0100801 kref_put(&bo->list_kref, ttm_bo_release_list);
802 return ret;
803}
804
Ben Skeggs42311ff2010-08-04 12:07:08 +1000805void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
806{
Ben Skeggsd961db72010-08-05 10:48:18 +1000807 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000808
Ben Skeggsd961db72010-08-05 10:48:18 +1000809 if (mem->mm_node)
810 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000811}
812EXPORT_SYMBOL(ttm_bo_mem_put);
813
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200814/**
815 * Repeatedly evict memory from the LRU for @mem_type until we create enough
816 * space, or we've evicted everything and there isn't enough space.
817 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100818static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
819 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200820 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100821 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000822 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000823 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200824{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100825 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200826 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200827 int ret;
828
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200829 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200830 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200831 if (unlikely(ret != 0))
832 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000833 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100834 break;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000835 ret = ttm_mem_evict_first(bdev, mem_type,
836 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100837 if (unlikely(ret != 0))
838 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200839 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000840 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200841 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200842 mem->mem_type = mem_type;
843 return 0;
844}
845
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200846static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
847 uint32_t cur_placement,
848 uint32_t proposed_placement)
849{
850 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
851 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
852
853 /**
854 * Keep current caching if possible.
855 */
856
857 if ((cur_placement & caching) != 0)
858 result |= (cur_placement & caching);
859 else if ((man->default_caching & caching) != 0)
860 result |= man->default_caching;
861 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
862 result |= TTM_PL_FLAG_CACHED;
863 else if ((TTM_PL_FLAG_WC & caching) != 0)
864 result |= TTM_PL_FLAG_WC;
865 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
866 result |= TTM_PL_FLAG_UNCACHED;
867
868 return result;
869}
870
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200871static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200872 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200873 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200874 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200875{
876 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
877
Christian Königf1217ed2014-08-27 13:16:04 +0200878 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200879 return false;
880
Christian Königf1217ed2014-08-27 13:16:04 +0200881 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200883
Christian Königf1217ed2014-08-27 13:16:04 +0200884 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200885
886 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200887 return true;
888}
889
890/**
891 * Creates space for memory region @mem according to its type.
892 *
893 * This function first searches for free space in compatible memory types in
894 * the priority order defined by the driver. If free space isn't found, then
895 * ttm_bo_mem_force_space is attempted in priority order to evict and find
896 * space.
897 */
898int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100899 struct ttm_placement *placement,
900 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000901 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000902 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200903{
904 struct ttm_bo_device *bdev = bo->bdev;
905 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200906 uint32_t mem_type = TTM_PL_SYSTEM;
907 uint32_t cur_flags = 0;
908 bool type_found = false;
909 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100910 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100911 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200912
913 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000914 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200915 const struct ttm_place *place = &placement->placement[i];
916
917 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100918 if (ret)
919 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200920 man = &bdev->man[mem_type];
921
Christian Königf1217ed2014-08-27 13:16:04 +0200922 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100923 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200924
925 if (!type_ok)
926 continue;
927
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200928 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
929 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100930 /*
931 * Use the access and other non-mapping-related flag bits from
932 * the memory placement flags to the current flags
933 */
Christian Königf1217ed2014-08-27 13:16:04 +0200934 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100935 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200936
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200937 if (mem_type == TTM_PL_SYSTEM)
938 break;
939
940 if (man->has_type && man->use_type) {
941 type_found = true;
Christian Königf1217ed2014-08-27 13:16:04 +0200942 ret = (*man->func->get_node)(man, bo, place, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100943 if (unlikely(ret))
944 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200945 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000946 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200947 break;
948 }
949
Ben Skeggsd961db72010-08-05 10:48:18 +1000950 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951 mem->mem_type = mem_type;
952 mem->placement = cur_flags;
953 return 0;
954 }
955
956 if (!type_found)
957 return -EINVAL;
958
Dave Airlieb6637522009-12-14 14:51:35 +1000959 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200960 const struct ttm_place *place = &placement->busy_placement[i];
961
962 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100963 if (ret)
964 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200965 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200966 if (!man->has_type)
967 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200968 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200969 continue;
970
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200971 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
972 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100973 /*
974 * Use the access and other non-mapping-related flag bits from
975 * the memory placement flags to the current flags
976 */
Christian Königf1217ed2014-08-27 13:16:04 +0200977 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100978 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200979
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100980 if (mem_type == TTM_PL_SYSTEM) {
981 mem->mem_type = mem_type;
982 mem->placement = cur_flags;
983 mem->mm_node = NULL;
984 return 0;
985 }
986
Christian Königf1217ed2014-08-27 13:16:04 +0200987 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000988 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989 if (ret == 0 && mem->mm_node) {
990 mem->placement = cur_flags;
991 return 0;
992 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100993 if (ret == -ERESTARTSYS)
994 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200995 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100996 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997 return ret;
998}
999EXPORT_SYMBOL(ttm_bo_mem_space);
1000
Rashika Kheria6e87fa42014-01-06 22:12:58 +05301001static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001002 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001003 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001004 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006 int ret = 0;
1007 struct ttm_mem_reg mem;
1008
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001009 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001010
1011 /*
1012 * FIXME: It's possible to pipeline buffer moves.
1013 * Have the driver move function wait for idle when necessary,
1014 * instead of doing it here.
1015 */
Dave Airlie1717c0e2011-10-27 18:28:37 +02001016 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001017 if (ret)
1018 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019 mem.num_pages = bo->num_pages;
1020 mem.size = mem.num_pages << PAGE_SHIFT;
1021 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001022 mem.bus.io_reserved_vm = false;
1023 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001024 /*
1025 * Determine where to move the buffer.
1026 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001027 ret = ttm_bo_mem_space(bo, placement, &mem,
1028 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001029 if (ret)
1030 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001031 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1032 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001033out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001034 if (ret && mem.mm_node)
1035 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036 return ret;
1037}
1038
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001039static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1040 struct ttm_mem_reg *mem,
1041 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001042{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001043 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001044
Jerome Glisseca262a9992009-12-08 15:33:32 +01001045 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001046 const struct ttm_place *heap = &placement->placement[i];
1047 if (mem->mm_node && heap->lpfn != 0 &&
1048 (mem->start < heap->fpfn ||
1049 mem->start + mem->num_pages > heap->lpfn))
1050 continue;
1051
1052 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001053 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1054 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1055 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001056 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001057
1058 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001059 const struct ttm_place *heap = &placement->busy_placement[i];
1060 if (mem->mm_node && heap->lpfn != 0 &&
1061 (mem->start < heap->fpfn ||
1062 mem->start + mem->num_pages > heap->lpfn))
1063 continue;
1064
1065 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001066 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1067 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1068 return true;
1069 }
1070
1071 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001072}
1073
Jerome Glisse09855ac2009-12-10 17:16:27 +01001074int ttm_bo_validate(struct ttm_buffer_object *bo,
1075 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001076 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001077 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001078{
1079 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001080 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001081
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001082 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001083 /*
1084 * Check whether we need to move buffer.
1085 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001086 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001087 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1088 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001089 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001090 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001091 } else {
1092 /*
1093 * Use the access and other non-mapping-related flag bits from
1094 * the compatible memory placement flags to the active flags
1095 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001096 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001097 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001099 /*
1100 * We might need to add a TTM.
1101 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1103 ret = ttm_bo_add_ttm(bo, true);
1104 if (ret)
1105 return ret;
1106 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001107 return 0;
1108}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001109EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110
Jerome Glisse09855ac2009-12-10 17:16:27 +01001111int ttm_bo_init(struct ttm_bo_device *bdev,
1112 struct ttm_buffer_object *bo,
1113 unsigned long size,
1114 enum ttm_bo_type type,
1115 struct ttm_placement *placement,
1116 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001117 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001118 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001119 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001120 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001121 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001122{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001123 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001124 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001125 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001126 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001127
1128 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1129 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001130 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001131 if (destroy)
1132 (*destroy)(bo);
1133 else
1134 kfree(bo);
1135 return -ENOMEM;
1136 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001137
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001138 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1139 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001140 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001141 if (destroy)
1142 (*destroy)(bo);
1143 else
1144 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001145 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146 return -EINVAL;
1147 }
1148 bo->destroy = destroy;
1149
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001150 kref_init(&bo->kref);
1151 kref_init(&bo->list_kref);
1152 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 INIT_LIST_HEAD(&bo->lru);
1154 INIT_LIST_HEAD(&bo->ddestroy);
1155 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001156 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001157 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001159 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001160 bo->type = type;
1161 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001162 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001163 bo->mem.mem_type = TTM_PL_SYSTEM;
1164 bo->mem.num_pages = bo->num_pages;
1165 bo->mem.mm_node = NULL;
1166 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001167 bo->mem.bus.io_reserved_vm = false;
1168 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001169 bo->priv_flags = 0;
1170 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001171 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001172 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001173 bo->sg = sg;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001174 bo->resv = &bo->ttm_resv;
1175 reservation_object_init(bo->resv);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001176 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001177 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001180 * For ttm_bo_type_device buffers, allocate
1181 * address space from the device.
1182 */
Christian Königf1217ed2014-08-27 13:16:04 +02001183 if (bo->type == ttm_bo_type_device ||
1184 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001185 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1186 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001188 locked = ww_mutex_trylock(&bo->resv->lock);
1189 WARN_ON(!locked);
1190
1191 if (likely(!ret))
1192 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001193
1194 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001195
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001196 if (unlikely(ret))
1197 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001198
1199 return ret;
1200}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001201EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001202
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001203size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1204 unsigned long bo_size,
1205 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001206{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001207 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1208 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001210 size += ttm_round_pot(struct_size);
1211 size += PAGE_ALIGN(npages * sizeof(void *));
1212 size += ttm_round_pot(sizeof(struct ttm_tt));
1213 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001214}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001215EXPORT_SYMBOL(ttm_bo_acc_size);
1216
1217size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1218 unsigned long bo_size,
1219 unsigned struct_size)
1220{
1221 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1222 size_t size = 0;
1223
1224 size += ttm_round_pot(struct_size);
1225 size += PAGE_ALIGN(npages * sizeof(void *));
1226 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1227 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1228 return size;
1229}
1230EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231
Jerome Glisse09855ac2009-12-10 17:16:27 +01001232int ttm_bo_create(struct ttm_bo_device *bdev,
1233 unsigned long size,
1234 enum ttm_bo_type type,
1235 struct ttm_placement *placement,
1236 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001237 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001238 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001239 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001240{
1241 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001242 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001243 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001244
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001245 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001246 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001247 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248
Thomas Hellstroma393c732012-06-12 13:28:42 +02001249 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001250 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001251 interruptible, persistent_swap_storage, acc_size,
1252 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001253 if (likely(ret == 0))
1254 *p_bo = bo;
1255
1256 return ret;
1257}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001258EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001259
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001260static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001261 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001262{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001263 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001264 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001265 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001266
1267 /*
1268 * Can't use standard list traversal since we're unlocking.
1269 */
1270
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001271 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001272 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001273 spin_unlock(&glob->lru_lock);
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001274 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001275 if (ret) {
1276 if (allow_errors) {
1277 return ret;
1278 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001279 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001280 }
1281 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001282 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001283 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001284 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001285 return 0;
1286}
1287
1288int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1289{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001290 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291 int ret = -EINVAL;
1292
1293 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001294 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001295 return ret;
1296 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001297 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298
1299 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001300 pr_err("Trying to take down uninitialized memory manager type %u\n",
1301 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302 return ret;
1303 }
1304
1305 man->use_type = false;
1306 man->has_type = false;
1307
1308 ret = 0;
1309 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001310 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001311
Ben Skeggsd961db72010-08-05 10:48:18 +10001312 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001313 }
1314
1315 return ret;
1316}
1317EXPORT_SYMBOL(ttm_bo_clean_mm);
1318
1319int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1320{
1321 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1322
1323 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001324 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001325 return -EINVAL;
1326 }
1327
1328 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001329 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001330 return 0;
1331 }
1332
Jerome Glisseca262a9992009-12-08 15:33:32 +01001333 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001334}
1335EXPORT_SYMBOL(ttm_bo_evict_mm);
1336
1337int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001338 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001339{
1340 int ret = -EINVAL;
1341 struct ttm_mem_type_manager *man;
1342
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001343 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001344 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001345 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001346 man->io_reserve_fastpath = true;
1347 man->use_io_reserve_lru = false;
1348 mutex_init(&man->io_reserve_mutex);
1349 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001350
1351 ret = bdev->driver->init_mem_type(bdev, type, man);
1352 if (ret)
1353 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001354 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001355
1356 ret = 0;
1357 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001358 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001359 if (ret)
1360 return ret;
1361 }
1362 man->has_type = true;
1363 man->use_type = true;
1364 man->size = p_size;
1365
1366 INIT_LIST_HEAD(&man->lru);
1367
1368 return 0;
1369}
1370EXPORT_SYMBOL(ttm_bo_init_mm);
1371
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001372static void ttm_bo_global_kobj_release(struct kobject *kobj)
1373{
1374 struct ttm_bo_global *glob =
1375 container_of(kobj, struct ttm_bo_global, kobj);
1376
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001377 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1378 __free_page(glob->dummy_read_page);
1379 kfree(glob);
1380}
1381
Dave Airlieba4420c2010-03-09 10:56:52 +10001382void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001383{
1384 struct ttm_bo_global *glob = ref->object;
1385
1386 kobject_del(&glob->kobj);
1387 kobject_put(&glob->kobj);
1388}
1389EXPORT_SYMBOL(ttm_bo_global_release);
1390
Dave Airlieba4420c2010-03-09 10:56:52 +10001391int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001392{
1393 struct ttm_bo_global_ref *bo_ref =
1394 container_of(ref, struct ttm_bo_global_ref, ref);
1395 struct ttm_bo_global *glob = ref->object;
1396 int ret;
1397
1398 mutex_init(&glob->device_list_mutex);
1399 spin_lock_init(&glob->lru_lock);
1400 glob->mem_glob = bo_ref->mem_glob;
1401 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1402
1403 if (unlikely(glob->dummy_read_page == NULL)) {
1404 ret = -ENOMEM;
1405 goto out_no_drp;
1406 }
1407
1408 INIT_LIST_HEAD(&glob->swap_lru);
1409 INIT_LIST_HEAD(&glob->device_list);
1410
1411 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1412 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1413 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001414 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001415 goto out_no_shrink;
1416 }
1417
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001418 atomic_set(&glob->bo_count, 0);
1419
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001420 ret = kobject_init_and_add(
1421 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001422 if (unlikely(ret != 0))
1423 kobject_put(&glob->kobj);
1424 return ret;
1425out_no_shrink:
1426 __free_page(glob->dummy_read_page);
1427out_no_drp:
1428 kfree(glob);
1429 return ret;
1430}
1431EXPORT_SYMBOL(ttm_bo_global_init);
1432
1433
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001434int ttm_bo_device_release(struct ttm_bo_device *bdev)
1435{
1436 int ret = 0;
1437 unsigned i = TTM_NUM_MEM_TYPES;
1438 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001439 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001440
1441 while (i--) {
1442 man = &bdev->man[i];
1443 if (man->has_type) {
1444 man->use_type = false;
1445 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1446 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001447 pr_err("DRM memory manager type %d is not clean\n",
1448 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001449 }
1450 man->has_type = false;
1451 }
1452 }
1453
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001454 mutex_lock(&glob->device_list_mutex);
1455 list_del(&bdev->device_list);
1456 mutex_unlock(&glob->device_list_mutex);
1457
Tejun Heof094cfc2010-12-24 15:59:06 +01001458 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001459
1460 while (ttm_bo_delayed_delete(bdev, true))
1461 ;
1462
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001463 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001464 if (list_empty(&bdev->ddestroy))
1465 TTM_DEBUG("Delayed destroy list was clean\n");
1466
1467 if (list_empty(&bdev->man[0].lru))
1468 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001469 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001470
David Herrmann72525b32013-07-24 21:08:53 +02001471 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001472
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001473 return ret;
1474}
1475EXPORT_SYMBOL(ttm_bo_device_release);
1476
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001477int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001478 struct ttm_bo_global *glob,
1479 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001480 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001481 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001482 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001483{
1484 int ret = -EINVAL;
1485
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001486 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001487
1488 memset(bdev->man, 0, sizeof(bdev->man));
1489
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001490 /*
1491 * Initialize the system memory buffer type.
1492 * Other types need to be driver / IOCTL initialized.
1493 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001494 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001495 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001496 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497
David Herrmann72525b32013-07-24 21:08:53 +02001498 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1499 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001501 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001502 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001503 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001504 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001505 bdev->val_seq = 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001506 mutex_lock(&glob->device_list_mutex);
1507 list_add_tail(&bdev->device_list, &glob->device_list);
1508 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001509
1510 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001511out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001512 return ret;
1513}
1514EXPORT_SYMBOL(ttm_bo_device_init);
1515
1516/*
1517 * buffer object vm functions.
1518 */
1519
1520bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1521{
1522 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1523
1524 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1525 if (mem->mem_type == TTM_PL_SYSTEM)
1526 return false;
1527
1528 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1529 return false;
1530
1531 if (mem->placement & TTM_PL_FLAG_CACHED)
1532 return false;
1533 }
1534 return true;
1535}
1536
Thomas Hellstromeba67092010-11-11 09:41:57 +01001537void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538{
1539 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001540
David Herrmann51335df2013-07-24 21:10:03 +02001541 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001542 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001543}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001544
1545void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1546{
1547 struct ttm_bo_device *bdev = bo->bdev;
1548 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1549
1550 ttm_mem_io_lock(man, false);
1551 ttm_bo_unmap_virtual_locked(bo);
1552 ttm_mem_io_unlock(man);
1553}
1554
1555
Dave Airliee024e112009-06-24 09:48:08 +10001556EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001557
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001558int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001559 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001560{
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001561 struct reservation_object_list *fobj;
1562 struct reservation_object *resv;
1563 struct fence *excl;
1564 long timeout = 15 * HZ;
1565 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001566
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001567 resv = bo->resv;
1568 fobj = reservation_object_get_list(resv);
1569 excl = reservation_object_get_excl(resv);
1570 if (excl) {
1571 if (!fence_is_signaled(excl)) {
1572 if (no_wait)
1573 return -EBUSY;
Maarten Lankhorst70401382014-01-21 13:07:01 +01001574
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001575 timeout = fence_wait_timeout(excl,
1576 interruptible, timeout);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001577 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001578 }
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001579
1580 for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
1581 struct fence *fence;
1582 fence = rcu_dereference_protected(fobj->shared[i],
1583 reservation_object_held(resv));
1584
1585 if (!fence_is_signaled(fence)) {
1586 if (no_wait)
1587 return -EBUSY;
1588
1589 timeout = fence_wait_timeout(fence,
1590 interruptible, timeout);
1591 }
1592 }
1593
1594 if (timeout < 0)
1595 return timeout;
1596
1597 if (timeout == 0)
1598 return -EBUSY;
1599
1600 reservation_object_add_excl_fence(resv, NULL);
1601 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1602 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001603}
1604EXPORT_SYMBOL(ttm_bo_wait);
1605
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001606int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1607{
1608 int ret = 0;
1609
1610 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001611 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001612 */
1613
Martin Kepplinger0eff2a22014-06-15 02:10:39 +02001614 ret = ttm_bo_reserve(bo, true, no_wait, false, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001615 if (unlikely(ret != 0))
1616 return ret;
Dave Airlie1717c0e2011-10-27 18:28:37 +02001617 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001618 if (likely(ret == 0))
1619 atomic_inc(&bo->cpu_writers);
1620 ttm_bo_unreserve(bo);
1621 return ret;
1622}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001623EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001624
1625void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1626{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001627 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001628}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001629EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001630
1631/**
1632 * A buffer object shrink method that tries to swap out the first
1633 * buffer object on the bo_global::swap_lru list.
1634 */
1635
1636static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1637{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001638 struct ttm_bo_global *glob =
1639 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001640 struct ttm_buffer_object *bo;
1641 int ret = -EBUSY;
1642 int put_count;
1643 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1644
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001645 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001646 list_for_each_entry(bo, &glob->swap_lru, swap) {
Martin Kepplinger0eff2a22014-06-15 02:10:39 +02001647 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001648 if (!ret)
1649 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001650 }
1651
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001652 if (ret) {
1653 spin_unlock(&glob->lru_lock);
1654 return ret;
1655 }
1656
1657 kref_get(&bo->list_kref);
1658
1659 if (!list_empty(&bo->ddestroy)) {
1660 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1661 kref_put(&bo->list_kref, ttm_bo_release_list);
1662 return ret;
1663 }
1664
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001665 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001666 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001667
Dave Airlied6ea8882010-11-22 13:24:40 +10001668 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001669
1670 /**
1671 * Wait for GPU, then move to system cached.
1672 */
1673
Dave Airlie1717c0e2011-10-27 18:28:37 +02001674 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001675
1676 if (unlikely(ret != 0))
1677 goto out;
1678
1679 if ((bo->mem.placement & swap_placement) != swap_placement) {
1680 struct ttm_mem_reg evict_mem;
1681
1682 evict_mem = bo->mem;
1683 evict_mem.mm_node = NULL;
1684 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1685 evict_mem.mem_type = TTM_PL_SYSTEM;
1686
1687 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001688 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001689 if (unlikely(ret != 0))
1690 goto out;
1691 }
1692
1693 ttm_bo_unmap_virtual(bo);
1694
1695 /**
1696 * Swap out. Buffer will be swapped in again as soon as
1697 * anyone tries to access a ttm page.
1698 */
1699
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001700 if (bo->bdev->driver->swap_notify)
1701 bo->bdev->driver->swap_notify(bo);
1702
Jan Engelhardt5df23972011-04-04 01:25:18 +02001703 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001704out:
1705
1706 /**
1707 *
1708 * Unreserve without putting on LRU to avoid swapping out an
1709 * already swapped buffer.
1710 */
1711
Thomas Hellstromc7523082014-02-20 11:36:25 +01001712 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001713 kref_put(&bo->list_kref, ttm_bo_release_list);
1714 return ret;
1715}
1716
1717void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1718{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001719 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001720 ;
1721}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001722EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001723
1724/**
1725 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1726 * unreserved
1727 *
1728 * @bo: Pointer to buffer
1729 */
1730int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1731{
1732 int ret;
1733
1734 /*
1735 * In the absense of a wait_unlocked API,
1736 * Use the bo::wu_mutex to avoid triggering livelocks due to
1737 * concurrent use of this function. Note that this use of
1738 * bo::wu_mutex can go away if we change locking order to
1739 * mmap_sem -> bo::reserve.
1740 */
1741 ret = mutex_lock_interruptible(&bo->wu_mutex);
1742 if (unlikely(ret != 0))
1743 return -ERESTARTSYS;
1744 if (!ww_mutex_is_locked(&bo->resv->lock))
1745 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001746 ret = __ttm_bo_reserve(bo, true, false, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001747 if (unlikely(ret != 0))
1748 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001749 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001750
1751out_unlock:
1752 mutex_unlock(&bo->wu_mutex);
1753 return ret;
1754}