blob: 4f1bc948bda04b507d760a010d3d4180b2f1aca9 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/jiffies.h>
37#include <linux/slab.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/file.h>
41#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020043
44#define TTM_ASSERT_LOCKED(param)
45#define TTM_DEBUG(fmt, arg...)
46#define TTM_BO_HASH_ORDER 13
47
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020048static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020049static void ttm_bo_global_kobj_release(struct kobject *kobj);
50
51static struct attribute ttm_bo_count = {
52 .name = "bo_count",
53 .mode = S_IRUGO
54};
55
Christian Königf1217ed2014-08-27 13:16:04 +020056static inline int ttm_mem_type_from_place(const struct ttm_place *place,
57 uint32_t *mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010058{
59 int i;
60
61 for (i = 0; i <= TTM_PL_PRIV5; i++)
Christian Königf1217ed2014-08-27 13:16:04 +020062 if (place->flags & (1 << i)) {
Jerome Glissefb53f862009-12-09 21:55:10 +010063 *mem_type = i;
64 return 0;
65 }
66 return -EINVAL;
67}
68
Jerome Glisse5012f502009-12-10 18:07:26 +010069static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010070{
Jerome Glisse5012f502009-12-10 18:07:26 +010071 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
72
Joe Perches25d04792012-03-16 21:43:50 -070073 pr_err(" has_type: %d\n", man->has_type);
74 pr_err(" use_type: %d\n", man->use_type);
75 pr_err(" flags: 0x%08X\n", man->flags);
76 pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
77 pr_err(" size: %llu\n", man->size);
78 pr_err(" available_caching: 0x%08X\n", man->available_caching);
79 pr_err(" default_caching: 0x%08X\n", man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100080 if (mem_type != TTM_PL_SYSTEM)
81 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010082}
83
84static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
85 struct ttm_placement *placement)
86{
Jerome Glissefb53f862009-12-09 21:55:10 +010087 int i, ret, mem_type;
88
Joe Perches25d04792012-03-16 21:43:50 -070089 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
90 bo, bo->mem.num_pages, bo->mem.size >> 10,
91 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010092 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +020093 ret = ttm_mem_type_from_place(&placement->placement[i],
Jerome Glissefb53f862009-12-09 21:55:10 +010094 &mem_type);
95 if (ret)
96 return;
Joe Perches25d04792012-03-16 21:43:50 -070097 pr_err(" placement[%d]=0x%08X (%d)\n",
Christian Königf1217ed2014-08-27 13:16:04 +020098 i, placement->placement[i].flags, mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +010099 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100100 }
101}
102
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200103static ssize_t ttm_bo_global_show(struct kobject *kobj,
104 struct attribute *attr,
105 char *buffer)
106{
107 struct ttm_bo_global *glob =
108 container_of(kobj, struct ttm_bo_global, kobj);
109
110 return snprintf(buffer, PAGE_SIZE, "%lu\n",
111 (unsigned long) atomic_read(&glob->bo_count));
112}
113
114static struct attribute *ttm_bo_global_attrs[] = {
115 &ttm_bo_count,
116 NULL
117};
118
Emese Revfy52cf25d2010-01-19 02:58:23 +0100119static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200120 .show = &ttm_bo_global_show
121};
122
123static struct kobj_type ttm_bo_glob_kobj_type = {
124 .release = &ttm_bo_global_kobj_release,
125 .sysfs_ops = &ttm_bo_global_ops,
126 .default_attrs = ttm_bo_global_attrs
127};
128
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200129
130static inline uint32_t ttm_bo_type_flags(unsigned type)
131{
132 return 1 << (type);
133}
134
135static void ttm_bo_release_list(struct kref *list_kref)
136{
137 struct ttm_buffer_object *bo =
138 container_of(list_kref, struct ttm_buffer_object, list_kref);
139 struct ttm_bo_device *bdev = bo->bdev;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500140 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200141
142 BUG_ON(atomic_read(&bo->list_kref.refcount));
143 BUG_ON(atomic_read(&bo->kref.refcount));
144 BUG_ON(atomic_read(&bo->cpu_writers));
145 BUG_ON(bo->sync_obj != NULL);
146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
149
150 if (bo->ttm)
151 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200152 atomic_dec(&bo->glob->bo_count);
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
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200406static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200407{
408 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200409 struct ttm_bo_global *glob = bo->glob;
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100410 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000411 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200412 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200413 int ret;
414
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100415 spin_lock(&glob->lru_lock);
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200416 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100417
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000418 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200419 (void) ttm_bo_wait(bo, false, false, true);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100420 if (!ret && !bo->sync_obj) {
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000421 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200422 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200423
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200424 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200425 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200426
Dave Airlied6ea8882010-11-22 13:24:40 +1000427 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200428
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200429 return;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200430 }
Thomas Hellstromaa123262010-11-02 13:21:47 +0000431 if (bo->sync_obj)
432 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100433 spin_unlock(&bdev->fence_lock);
434
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700435 if (!ret) {
436
437 /*
438 * Make NO_EVICT bos immediately available to
439 * shrinkers, now that they are queued for
440 * destruction.
441 */
442 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
443 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
444 ttm_bo_add_to_lru(bo);
445 }
446
Thomas Hellstromc7523082014-02-20 11:36:25 +0100447 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700448 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200449
450 kref_get(&bo->list_kref);
451 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
452 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200453
Thomas Hellstromaa123262010-11-02 13:21:47 +0000454 if (sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +0000455 driver->sync_obj_flush(sync_obj);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000456 driver->sync_obj_unref(&sync_obj);
457 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200458 schedule_delayed_work(&bdev->wq,
459 ((HZ / 100) < 1) ? 1 : HZ / 100);
460}
461
462/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000463 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200464 * If bo idle, remove from delayed- and lru lists, and unref.
465 * If not idle, do nothing.
466 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000467 * Must be called with lru_lock and reservation held, this function
468 * will drop both before returning.
469 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200470 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200471 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
472 */
473
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000474static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
475 bool interruptible,
476 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200477{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000478 struct ttm_bo_device *bdev = bo->bdev;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000479 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200480 struct ttm_bo_global *glob = bo->glob;
481 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000482 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200483
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000484 spin_lock(&bdev->fence_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000485 ret = ttm_bo_wait(bo, false, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200486
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000487 if (ret && !no_wait_gpu) {
488 void *sync_obj;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200489
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000490 /*
491 * Take a reference to the fence and unreserve,
492 * at this point the buffer should be dead, so
493 * no new sync objects can be attached.
494 */
Maarten Lankhorst0953e762012-12-19 18:21:10 +0100495 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000496 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100497
Thomas Hellstromc7523082014-02-20 11:36:25 +0100498 __ttm_bo_unreserve(bo);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200499 spin_unlock(&glob->lru_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000500
501 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
502 driver->sync_obj_unref(&sync_obj);
503 if (ret)
504 return ret;
505
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000506 spin_lock(&glob->lru_lock);
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200507 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000508
509 /*
510 * We raced, and lost, someone else holds the reservation now,
511 * and is probably busy in ttm_bo_cleanup_memtype_use.
512 *
513 * Even if it's not the case, because we finished waiting any
514 * delayed destruction would succeed, so just return success
515 * here.
516 */
517 if (ret) {
518 spin_unlock(&glob->lru_lock);
519 return 0;
520 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100521
522 /*
523 * remove sync_obj with ttm_bo_wait, the wait should be
524 * finished, and no new wait object should have been added.
525 */
526 spin_lock(&bdev->fence_lock);
527 ret = ttm_bo_wait(bo, false, false, true);
528 WARN_ON(ret);
529 }
530 spin_unlock(&bdev->fence_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000531
532 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100533 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000534 spin_unlock(&glob->lru_lock);
535 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200536 }
537
538 put_count = ttm_bo_del_from_lru(bo);
539 list_del_init(&bo->ddestroy);
540 ++put_count;
541
542 spin_unlock(&glob->lru_lock);
543 ttm_bo_cleanup_memtype_use(bo);
544
Dave Airlied6ea8882010-11-22 13:24:40 +1000545 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200546
547 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200548}
549
550/**
551 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
552 * encountered buffers.
553 */
554
555static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
556{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200557 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100558 struct ttm_buffer_object *entry = NULL;
559 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200560
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200561 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100562 if (list_empty(&bdev->ddestroy))
563 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200564
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100565 entry = list_first_entry(&bdev->ddestroy,
566 struct ttm_buffer_object, ddestroy);
567 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200568
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100569 for (;;) {
570 struct ttm_buffer_object *nentry = NULL;
571
572 if (entry->ddestroy.next != &bdev->ddestroy) {
573 nentry = list_first_entry(&entry->ddestroy,
574 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200575 kref_get(&nentry->list_kref);
576 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200577
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200578 ret = __ttm_bo_reserve(entry, false, true, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100579 if (remove_all && ret) {
580 spin_unlock(&glob->lru_lock);
Thomas Hellstromc7523082014-02-20 11:36:25 +0100581 ret = __ttm_bo_reserve(entry, false, false,
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200582 false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100583 spin_lock(&glob->lru_lock);
584 }
585
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000586 if (!ret)
587 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
588 !remove_all);
589 else
590 spin_unlock(&glob->lru_lock);
591
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200592 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100593 entry = nentry;
594
595 if (ret || !entry)
596 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200597
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200598 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100599 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200600 break;
601 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200602
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100603out_unlock:
604 spin_unlock(&glob->lru_lock);
605out:
606 if (entry)
607 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200608 return ret;
609}
610
611static void ttm_bo_delayed_workqueue(struct work_struct *work)
612{
613 struct ttm_bo_device *bdev =
614 container_of(work, struct ttm_bo_device, wq.work);
615
616 if (ttm_bo_delayed_delete(bdev, false)) {
617 schedule_delayed_work(&bdev->wq,
618 ((HZ / 100) < 1) ? 1 : HZ / 100);
619 }
620}
621
622static void ttm_bo_release(struct kref *kref)
623{
624 struct ttm_buffer_object *bo =
625 container_of(kref, struct ttm_buffer_object, kref);
626 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100627 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200628
David Herrmann72525b32013-07-24 21:08:53 +0200629 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100630 ttm_mem_io_lock(man, false);
631 ttm_mem_io_free_vm(bo);
632 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200633 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200634 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200635}
636
637void ttm_bo_unref(struct ttm_buffer_object **p_bo)
638{
639 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200640
641 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200642 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200643}
644EXPORT_SYMBOL(ttm_bo_unref);
645
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400646int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
647{
648 return cancel_delayed_work_sync(&bdev->wq);
649}
650EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
651
652void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
653{
654 if (resched)
655 schedule_delayed_work(&bdev->wq,
656 ((HZ / 100) < 1) ? 1 : HZ / 100);
657}
658EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
659
Jerome Glisseca262a9992009-12-08 15:33:32 +0100660static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000661 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200662{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200663 struct ttm_bo_device *bdev = bo->bdev;
664 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100665 struct ttm_placement placement;
666 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200667
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000668 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200669 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000670 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200671
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200672 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100673 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700674 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200675 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200676 goto out;
677 }
678
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200679 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200680
681 evict_mem = bo->mem;
682 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100683 evict_mem.bus.io_reserved_vm = false;
684 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200685
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100686 placement.num_placement = 0;
687 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100688 bdev->driver->evict_flags(bo, &placement);
689 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000690 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200691 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100692 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700693 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
694 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100695 ttm_bo_mem_space_debug(bo, &placement);
696 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200697 goto out;
698 }
699
700 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000701 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200702 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100703 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700704 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000705 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200706 goto out;
707 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200708 bo->evicted = true;
709out:
710 return ret;
711}
712
Jerome Glisseca262a9992009-12-08 15:33:32 +0100713static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
714 uint32_t mem_type,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000715 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000716 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100717{
718 struct ttm_bo_global *glob = bdev->glob;
719 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
720 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000721 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100722
723 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000724 list_for_each_entry(bo, &man->lru, lru) {
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200725 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000726 if (!ret)
727 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100728 }
729
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000730 if (ret) {
731 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000732 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200733 }
734
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000735 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100736
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000737 if (!list_empty(&bo->ddestroy)) {
738 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
739 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100740 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000741 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100742 }
743
744 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100745 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100746
747 BUG_ON(ret != 0);
748
Dave Airlied6ea8882010-11-22 13:24:40 +1000749 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100750
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000751 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100752 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100753
Jerome Glisseca262a9992009-12-08 15:33:32 +0100754 kref_put(&bo->list_kref, ttm_bo_release_list);
755 return ret;
756}
757
Ben Skeggs42311ff2010-08-04 12:07:08 +1000758void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
759{
Ben Skeggsd961db72010-08-05 10:48:18 +1000760 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000761
Ben Skeggsd961db72010-08-05 10:48:18 +1000762 if (mem->mm_node)
763 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000764}
765EXPORT_SYMBOL(ttm_bo_mem_put);
766
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200767/**
768 * Repeatedly evict memory from the LRU for @mem_type until we create enough
769 * space, or we've evicted everything and there isn't enough space.
770 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100771static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
772 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200773 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100774 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000775 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000776 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200777{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100778 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200779 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200780 int ret;
781
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200782 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200783 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200784 if (unlikely(ret != 0))
785 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000786 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100787 break;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000788 ret = ttm_mem_evict_first(bdev, mem_type,
789 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100790 if (unlikely(ret != 0))
791 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200792 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000793 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200794 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200795 mem->mem_type = mem_type;
796 return 0;
797}
798
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200799static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
800 uint32_t cur_placement,
801 uint32_t proposed_placement)
802{
803 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
804 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
805
806 /**
807 * Keep current caching if possible.
808 */
809
810 if ((cur_placement & caching) != 0)
811 result |= (cur_placement & caching);
812 else if ((man->default_caching & caching) != 0)
813 result |= man->default_caching;
814 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
815 result |= TTM_PL_FLAG_CACHED;
816 else if ((TTM_PL_FLAG_WC & caching) != 0)
817 result |= TTM_PL_FLAG_WC;
818 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
819 result |= TTM_PL_FLAG_UNCACHED;
820
821 return result;
822}
823
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200824static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200825 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200826 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200827 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200828{
829 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
830
Christian Königf1217ed2014-08-27 13:16:04 +0200831 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200832 return false;
833
Christian Königf1217ed2014-08-27 13:16:04 +0200834 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200835 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200836
Christian Königf1217ed2014-08-27 13:16:04 +0200837 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200838
839 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200840 return true;
841}
842
843/**
844 * Creates space for memory region @mem according to its type.
845 *
846 * This function first searches for free space in compatible memory types in
847 * the priority order defined by the driver. If free space isn't found, then
848 * ttm_bo_mem_force_space is attempted in priority order to evict and find
849 * space.
850 */
851int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100852 struct ttm_placement *placement,
853 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000854 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000855 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200856{
857 struct ttm_bo_device *bdev = bo->bdev;
858 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200859 uint32_t mem_type = TTM_PL_SYSTEM;
860 uint32_t cur_flags = 0;
861 bool type_found = false;
862 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100863 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100864 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865
866 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000867 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200868 const struct ttm_place *place = &placement->placement[i];
869
870 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100871 if (ret)
872 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200873 man = &bdev->man[mem_type];
874
Christian Königf1217ed2014-08-27 13:16:04 +0200875 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100876 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877
878 if (!type_ok)
879 continue;
880
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200881 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
882 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100883 /*
884 * Use the access and other non-mapping-related flag bits from
885 * the memory placement flags to the current flags
886 */
Christian Königf1217ed2014-08-27 13:16:04 +0200887 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100888 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200889
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200890 if (mem_type == TTM_PL_SYSTEM)
891 break;
892
893 if (man->has_type && man->use_type) {
894 type_found = true;
Christian Königf1217ed2014-08-27 13:16:04 +0200895 ret = (*man->func->get_node)(man, bo, place, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100896 if (unlikely(ret))
897 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000899 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200900 break;
901 }
902
Ben Skeggsd961db72010-08-05 10:48:18 +1000903 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200904 mem->mem_type = mem_type;
905 mem->placement = cur_flags;
906 return 0;
907 }
908
909 if (!type_found)
910 return -EINVAL;
911
Dave Airlieb6637522009-12-14 14:51:35 +1000912 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200913 const struct ttm_place *place = &placement->busy_placement[i];
914
915 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100916 if (ret)
917 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200918 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200919 if (!man->has_type)
920 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200921 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922 continue;
923
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200924 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
925 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100926 /*
927 * Use the access and other non-mapping-related flag bits from
928 * the memory placement flags to the current flags
929 */
Christian Königf1217ed2014-08-27 13:16:04 +0200930 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100931 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200932
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100933 if (mem_type == TTM_PL_SYSTEM) {
934 mem->mem_type = mem_type;
935 mem->placement = cur_flags;
936 mem->mm_node = NULL;
937 return 0;
938 }
939
Christian Königf1217ed2014-08-27 13:16:04 +0200940 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000941 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200942 if (ret == 0 && mem->mm_node) {
943 mem->placement = cur_flags;
944 return 0;
945 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100946 if (ret == -ERESTARTSYS)
947 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200948 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100949 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200950 return ret;
951}
952EXPORT_SYMBOL(ttm_bo_mem_space);
953
Rashika Kheria6e87fa42014-01-06 22:12:58 +0530954static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100955 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000956 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000957 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200958{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200959 int ret = 0;
960 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000961 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200962
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200963 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964
965 /*
966 * FIXME: It's possible to pipeline buffer moves.
967 * Have the driver move function wait for idle when necessary,
968 * instead of doing it here.
969 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000970 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200971 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000972 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200973 if (ret)
974 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200975 mem.num_pages = bo->num_pages;
976 mem.size = mem.num_pages << PAGE_SHIFT;
977 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100978 mem.bus.io_reserved_vm = false;
979 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200980 /*
981 * Determine where to move the buffer.
982 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000983 ret = ttm_bo_mem_space(bo, placement, &mem,
984 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200985 if (ret)
986 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000987 ret = ttm_bo_handle_move_mem(bo, &mem, false,
988 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +1000990 if (ret && mem.mm_node)
991 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200992 return ret;
993}
994
Thomas Hellstrom59c8e662013-10-28 02:02:19 -0700995static bool ttm_bo_mem_compat(struct ttm_placement *placement,
996 struct ttm_mem_reg *mem,
997 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200998{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100999 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001000
Jerome Glisseca262a9992009-12-08 15:33:32 +01001001 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001002 const struct ttm_place *heap = &placement->placement[i];
1003 if (mem->mm_node && heap->lpfn != 0 &&
1004 (mem->start < heap->fpfn ||
1005 mem->start + mem->num_pages > heap->lpfn))
1006 continue;
1007
1008 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001009 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1010 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1011 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001012 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001013
1014 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001015 const struct ttm_place *heap = &placement->busy_placement[i];
1016 if (mem->mm_node && heap->lpfn != 0 &&
1017 (mem->start < heap->fpfn ||
1018 mem->start + mem->num_pages > heap->lpfn))
1019 continue;
1020
1021 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001022 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1023 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1024 return true;
1025 }
1026
1027 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001028}
1029
Jerome Glisse09855ac2009-12-10 17:16:27 +01001030int ttm_bo_validate(struct ttm_buffer_object *bo,
1031 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001032 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001033 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001034{
1035 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001036 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001038 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001039 /*
1040 * Check whether we need to move buffer.
1041 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001042 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001043 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1044 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001045 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001047 } else {
1048 /*
1049 * Use the access and other non-mapping-related flag bits from
1050 * the compatible memory placement flags to the active flags
1051 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001052 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001053 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001054 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001055 /*
1056 * We might need to add a TTM.
1057 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001058 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1059 ret = ttm_bo_add_ttm(bo, true);
1060 if (ret)
1061 return ret;
1062 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001063 return 0;
1064}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001065EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001066
Jerome Glisse09855ac2009-12-10 17:16:27 +01001067int ttm_bo_init(struct ttm_bo_device *bdev,
1068 struct ttm_buffer_object *bo,
1069 unsigned long size,
1070 enum ttm_bo_type type,
1071 struct ttm_placement *placement,
1072 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001073 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001074 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001075 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001076 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001077 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001078{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001079 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001080 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001081 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001082 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001083
1084 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1085 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001086 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001087 if (destroy)
1088 (*destroy)(bo);
1089 else
1090 kfree(bo);
1091 return -ENOMEM;
1092 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001094 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1095 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001096 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001097 if (destroy)
1098 (*destroy)(bo);
1099 else
1100 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001101 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102 return -EINVAL;
1103 }
1104 bo->destroy = destroy;
1105
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106 kref_init(&bo->kref);
1107 kref_init(&bo->list_kref);
1108 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001109 INIT_LIST_HEAD(&bo->lru);
1110 INIT_LIST_HEAD(&bo->ddestroy);
1111 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001112 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001113 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001114 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001115 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001116 bo->type = type;
1117 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001118 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001119 bo->mem.mem_type = TTM_PL_SYSTEM;
1120 bo->mem.num_pages = bo->num_pages;
1121 bo->mem.mm_node = NULL;
1122 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001123 bo->mem.bus.io_reserved_vm = false;
1124 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125 bo->priv_flags = 0;
1126 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001127 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001128 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001129 bo->sg = sg;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001130 bo->resv = &bo->ttm_resv;
1131 reservation_object_init(bo->resv);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001132 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001133 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001134
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001136 * For ttm_bo_type_device buffers, allocate
1137 * address space from the device.
1138 */
Christian Königf1217ed2014-08-27 13:16:04 +02001139 if (bo->type == ttm_bo_type_device ||
1140 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001141 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1142 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001144 locked = ww_mutex_trylock(&bo->resv->lock);
1145 WARN_ON(!locked);
1146
1147 if (likely(!ret))
1148 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149
1150 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001151
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001152 if (unlikely(ret))
1153 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001154
1155 return ret;
1156}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001157EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001159size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1160 unsigned long bo_size,
1161 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001162{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001163 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1164 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001165
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001166 size += ttm_round_pot(struct_size);
1167 size += PAGE_ALIGN(npages * sizeof(void *));
1168 size += ttm_round_pot(sizeof(struct ttm_tt));
1169 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001170}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001171EXPORT_SYMBOL(ttm_bo_acc_size);
1172
1173size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1174 unsigned long bo_size,
1175 unsigned struct_size)
1176{
1177 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1178 size_t size = 0;
1179
1180 size += ttm_round_pot(struct_size);
1181 size += PAGE_ALIGN(npages * sizeof(void *));
1182 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1183 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1184 return size;
1185}
1186EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187
Jerome Glisse09855ac2009-12-10 17:16:27 +01001188int ttm_bo_create(struct ttm_bo_device *bdev,
1189 unsigned long size,
1190 enum ttm_bo_type type,
1191 struct ttm_placement *placement,
1192 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001193 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001194 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001195 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196{
1197 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001198 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001199 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001201 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001202 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001204
Thomas Hellstroma393c732012-06-12 13:28:42 +02001205 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001206 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001207 interruptible, persistent_swap_storage, acc_size,
1208 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209 if (likely(ret == 0))
1210 *p_bo = bo;
1211
1212 return ret;
1213}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001214EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001216static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001217 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001218{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001219 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001220 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001222
1223 /*
1224 * Can't use standard list traversal since we're unlocking.
1225 */
1226
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001227 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001228 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001229 spin_unlock(&glob->lru_lock);
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001230 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001231 if (ret) {
1232 if (allow_errors) {
1233 return ret;
1234 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001235 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001236 }
1237 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001238 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001239 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001240 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001241 return 0;
1242}
1243
1244int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1245{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001246 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001247 int ret = -EINVAL;
1248
1249 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001250 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251 return ret;
1252 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001253 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001254
1255 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001256 pr_err("Trying to take down uninitialized memory manager type %u\n",
1257 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001258 return ret;
1259 }
1260
1261 man->use_type = false;
1262 man->has_type = false;
1263
1264 ret = 0;
1265 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001266 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001267
Ben Skeggsd961db72010-08-05 10:48:18 +10001268 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269 }
1270
1271 return ret;
1272}
1273EXPORT_SYMBOL(ttm_bo_clean_mm);
1274
1275int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1276{
1277 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1278
1279 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001280 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001281 return -EINVAL;
1282 }
1283
1284 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001285 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001286 return 0;
1287 }
1288
Jerome Glisseca262a9992009-12-08 15:33:32 +01001289 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001290}
1291EXPORT_SYMBOL(ttm_bo_evict_mm);
1292
1293int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001294 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001295{
1296 int ret = -EINVAL;
1297 struct ttm_mem_type_manager *man;
1298
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001299 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001300 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001301 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001302 man->io_reserve_fastpath = true;
1303 man->use_io_reserve_lru = false;
1304 mutex_init(&man->io_reserve_mutex);
1305 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306
1307 ret = bdev->driver->init_mem_type(bdev, type, man);
1308 if (ret)
1309 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001310 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001311
1312 ret = 0;
1313 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001314 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001315 if (ret)
1316 return ret;
1317 }
1318 man->has_type = true;
1319 man->use_type = true;
1320 man->size = p_size;
1321
1322 INIT_LIST_HEAD(&man->lru);
1323
1324 return 0;
1325}
1326EXPORT_SYMBOL(ttm_bo_init_mm);
1327
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001328static void ttm_bo_global_kobj_release(struct kobject *kobj)
1329{
1330 struct ttm_bo_global *glob =
1331 container_of(kobj, struct ttm_bo_global, kobj);
1332
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001333 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1334 __free_page(glob->dummy_read_page);
1335 kfree(glob);
1336}
1337
Dave Airlieba4420c2010-03-09 10:56:52 +10001338void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001339{
1340 struct ttm_bo_global *glob = ref->object;
1341
1342 kobject_del(&glob->kobj);
1343 kobject_put(&glob->kobj);
1344}
1345EXPORT_SYMBOL(ttm_bo_global_release);
1346
Dave Airlieba4420c2010-03-09 10:56:52 +10001347int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001348{
1349 struct ttm_bo_global_ref *bo_ref =
1350 container_of(ref, struct ttm_bo_global_ref, ref);
1351 struct ttm_bo_global *glob = ref->object;
1352 int ret;
1353
1354 mutex_init(&glob->device_list_mutex);
1355 spin_lock_init(&glob->lru_lock);
1356 glob->mem_glob = bo_ref->mem_glob;
1357 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1358
1359 if (unlikely(glob->dummy_read_page == NULL)) {
1360 ret = -ENOMEM;
1361 goto out_no_drp;
1362 }
1363
1364 INIT_LIST_HEAD(&glob->swap_lru);
1365 INIT_LIST_HEAD(&glob->device_list);
1366
1367 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1368 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1369 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001370 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001371 goto out_no_shrink;
1372 }
1373
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001374 atomic_set(&glob->bo_count, 0);
1375
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001376 ret = kobject_init_and_add(
1377 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001378 if (unlikely(ret != 0))
1379 kobject_put(&glob->kobj);
1380 return ret;
1381out_no_shrink:
1382 __free_page(glob->dummy_read_page);
1383out_no_drp:
1384 kfree(glob);
1385 return ret;
1386}
1387EXPORT_SYMBOL(ttm_bo_global_init);
1388
1389
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001390int ttm_bo_device_release(struct ttm_bo_device *bdev)
1391{
1392 int ret = 0;
1393 unsigned i = TTM_NUM_MEM_TYPES;
1394 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001395 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001396
1397 while (i--) {
1398 man = &bdev->man[i];
1399 if (man->has_type) {
1400 man->use_type = false;
1401 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1402 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001403 pr_err("DRM memory manager type %d is not clean\n",
1404 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001405 }
1406 man->has_type = false;
1407 }
1408 }
1409
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001410 mutex_lock(&glob->device_list_mutex);
1411 list_del(&bdev->device_list);
1412 mutex_unlock(&glob->device_list_mutex);
1413
Tejun Heof094cfc2010-12-24 15:59:06 +01001414 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001415
1416 while (ttm_bo_delayed_delete(bdev, true))
1417 ;
1418
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001419 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001420 if (list_empty(&bdev->ddestroy))
1421 TTM_DEBUG("Delayed destroy list was clean\n");
1422
1423 if (list_empty(&bdev->man[0].lru))
1424 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001425 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001426
David Herrmann72525b32013-07-24 21:08:53 +02001427 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001428
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001429 return ret;
1430}
1431EXPORT_SYMBOL(ttm_bo_device_release);
1432
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001433int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001434 struct ttm_bo_global *glob,
1435 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001436 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001437 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001438 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001439{
1440 int ret = -EINVAL;
1441
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001442 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001443
1444 memset(bdev->man, 0, sizeof(bdev->man));
1445
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001446 /*
1447 * Initialize the system memory buffer type.
1448 * Other types need to be driver / IOCTL initialized.
1449 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001450 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001451 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001452 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001453
David Herrmann72525b32013-07-24 21:08:53 +02001454 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1455 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001456 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001457 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001458 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001459 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001460 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001461 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001462 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001463 mutex_lock(&glob->device_list_mutex);
1464 list_add_tail(&bdev->device_list, &glob->device_list);
1465 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001466
1467 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001468out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001469 return ret;
1470}
1471EXPORT_SYMBOL(ttm_bo_device_init);
1472
1473/*
1474 * buffer object vm functions.
1475 */
1476
1477bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1478{
1479 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1480
1481 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1482 if (mem->mem_type == TTM_PL_SYSTEM)
1483 return false;
1484
1485 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1486 return false;
1487
1488 if (mem->placement & TTM_PL_FLAG_CACHED)
1489 return false;
1490 }
1491 return true;
1492}
1493
Thomas Hellstromeba67092010-11-11 09:41:57 +01001494void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001495{
1496 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497
David Herrmann51335df2013-07-24 21:10:03 +02001498 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001499 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001501
1502void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1503{
1504 struct ttm_bo_device *bdev = bo->bdev;
1505 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1506
1507 ttm_mem_io_lock(man, false);
1508 ttm_bo_unmap_virtual_locked(bo);
1509 ttm_mem_io_unlock(man);
1510}
1511
1512
Dave Airliee024e112009-06-24 09:48:08 +10001513EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001514
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001515
1516int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001517 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001518{
1519 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001520 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001521 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522 int ret = 0;
1523
Maarten Lankhorst70401382014-01-21 13:07:01 +01001524 lockdep_assert_held(&bo->resv->lock.base);
1525
Dave Airlie1717c0e2011-10-27 18:28:37 +02001526 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001527 return 0;
1528
Dave Airlie1717c0e2011-10-27 18:28:37 +02001529 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001530
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001531 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001532 void *tmp_obj = bo->sync_obj;
1533 bo->sync_obj = NULL;
1534 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1535 spin_unlock(&bdev->fence_lock);
1536 driver->sync_obj_unref(&tmp_obj);
1537 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538 continue;
1539 }
1540
1541 if (no_wait)
1542 return -EBUSY;
1543
Dave Airlie1717c0e2011-10-27 18:28:37 +02001544 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001545 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001546 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001547 lazy, interruptible);
1548 if (unlikely(ret != 0)) {
1549 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001550 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001551 return ret;
1552 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001553 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001554 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001555 void *tmp_obj = bo->sync_obj;
1556 bo->sync_obj = NULL;
1557 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1558 &bo->priv_flags);
1559 spin_unlock(&bdev->fence_lock);
1560 driver->sync_obj_unref(&sync_obj);
1561 driver->sync_obj_unref(&tmp_obj);
1562 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001563 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001564 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001565 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001566 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001567 }
1568 }
1569 return 0;
1570}
1571EXPORT_SYMBOL(ttm_bo_wait);
1572
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001573int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1574{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001575 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001576 int ret = 0;
1577
1578 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001579 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001580 */
1581
Martin Kepplinger0eff2a22014-06-15 02:10:39 +02001582 ret = ttm_bo_reserve(bo, true, no_wait, false, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001583 if (unlikely(ret != 0))
1584 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001585 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001586 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001587 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001588 if (likely(ret == 0))
1589 atomic_inc(&bo->cpu_writers);
1590 ttm_bo_unreserve(bo);
1591 return ret;
1592}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001593EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001594
1595void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1596{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001597 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001598}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001599EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001600
1601/**
1602 * A buffer object shrink method that tries to swap out the first
1603 * buffer object on the bo_global::swap_lru list.
1604 */
1605
1606static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1607{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001608 struct ttm_bo_global *glob =
1609 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001610 struct ttm_buffer_object *bo;
1611 int ret = -EBUSY;
1612 int put_count;
1613 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1614
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001615 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001616 list_for_each_entry(bo, &glob->swap_lru, swap) {
Martin Kepplinger0eff2a22014-06-15 02:10:39 +02001617 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001618 if (!ret)
1619 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001620 }
1621
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001622 if (ret) {
1623 spin_unlock(&glob->lru_lock);
1624 return ret;
1625 }
1626
1627 kref_get(&bo->list_kref);
1628
1629 if (!list_empty(&bo->ddestroy)) {
1630 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1631 kref_put(&bo->list_kref, ttm_bo_release_list);
1632 return ret;
1633 }
1634
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001635 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001636 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001637
Dave Airlied6ea8882010-11-22 13:24:40 +10001638 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001639
1640 /**
1641 * Wait for GPU, then move to system cached.
1642 */
1643
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001644 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001645 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001646 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001647
1648 if (unlikely(ret != 0))
1649 goto out;
1650
1651 if ((bo->mem.placement & swap_placement) != swap_placement) {
1652 struct ttm_mem_reg evict_mem;
1653
1654 evict_mem = bo->mem;
1655 evict_mem.mm_node = NULL;
1656 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1657 evict_mem.mem_type = TTM_PL_SYSTEM;
1658
1659 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001660 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001661 if (unlikely(ret != 0))
1662 goto out;
1663 }
1664
1665 ttm_bo_unmap_virtual(bo);
1666
1667 /**
1668 * Swap out. Buffer will be swapped in again as soon as
1669 * anyone tries to access a ttm page.
1670 */
1671
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001672 if (bo->bdev->driver->swap_notify)
1673 bo->bdev->driver->swap_notify(bo);
1674
Jan Engelhardt5df23972011-04-04 01:25:18 +02001675 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001676out:
1677
1678 /**
1679 *
1680 * Unreserve without putting on LRU to avoid swapping out an
1681 * already swapped buffer.
1682 */
1683
Thomas Hellstromc7523082014-02-20 11:36:25 +01001684 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001685 kref_put(&bo->list_kref, ttm_bo_release_list);
1686 return ret;
1687}
1688
1689void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1690{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001691 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001692 ;
1693}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001694EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001695
1696/**
1697 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1698 * unreserved
1699 *
1700 * @bo: Pointer to buffer
1701 */
1702int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1703{
1704 int ret;
1705
1706 /*
1707 * In the absense of a wait_unlocked API,
1708 * Use the bo::wu_mutex to avoid triggering livelocks due to
1709 * concurrent use of this function. Note that this use of
1710 * bo::wu_mutex can go away if we change locking order to
1711 * mmap_sem -> bo::reserve.
1712 */
1713 ret = mutex_lock_interruptible(&bo->wu_mutex);
1714 if (unlikely(ret != 0))
1715 return -ERESTARTSYS;
1716 if (!ww_mutex_is_locked(&bo->resv->lock))
1717 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001718 ret = __ttm_bo_reserve(bo, true, false, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001719 if (unlikely(ret != 0))
1720 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001721 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001722
1723out_unlock:
1724 mutex_unlock(&bo->wu_mutex);
1725 return ret;
1726}