blob: 195386f16ca4a390ccca8b0a29d5aec2258d2538 [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 Hellstrom15205fb2013-10-10 11:09:03 -0700418 if (!ret) {
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100419 (void) ttm_bo_wait(bo, false, false, true);
420
421 if (!bo->sync_obj) {
422 put_count = ttm_bo_del_from_lru(bo);
423
424 spin_unlock(&glob->lru_lock);
425 ttm_bo_cleanup_memtype_use(bo);
426
427 ttm_bo_list_ref_sub(bo, put_count, true);
428
429 return;
430 }
431 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700432
433 /*
434 * Make NO_EVICT bos immediately available to
435 * shrinkers, now that they are queued for
436 * destruction.
437 */
438 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
439 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
440 ttm_bo_add_to_lru(bo);
441 }
442
Thomas Hellstromc7523082014-02-20 11:36:25 +0100443 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700444 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200445
446 kref_get(&bo->list_kref);
447 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
448 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200449
Thomas Hellstromaa123262010-11-02 13:21:47 +0000450 if (sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +0000451 driver->sync_obj_flush(sync_obj);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000452 driver->sync_obj_unref(&sync_obj);
453 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200454 schedule_delayed_work(&bdev->wq,
455 ((HZ / 100) < 1) ? 1 : HZ / 100);
456}
457
458/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000459 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200460 * If bo idle, remove from delayed- and lru lists, and unref.
461 * If not idle, do nothing.
462 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000463 * Must be called with lru_lock and reservation held, this function
464 * will drop both before returning.
465 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200466 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200467 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
468 */
469
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000470static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
471 bool interruptible,
472 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200473{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000474 struct ttm_bo_device *bdev = bo->bdev;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000475 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200476 struct ttm_bo_global *glob = bo->glob;
477 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000478 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200479
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000480 ret = ttm_bo_wait(bo, false, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200481
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000482 if (ret && !no_wait_gpu) {
483 void *sync_obj;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200484
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000485 /*
486 * Take a reference to the fence and unreserve,
487 * at this point the buffer should be dead, so
488 * no new sync objects can be attached.
489 */
Maarten Lankhorst0953e762012-12-19 18:21:10 +0100490 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100491
Thomas Hellstromc7523082014-02-20 11:36:25 +0100492 __ttm_bo_unreserve(bo);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200493 spin_unlock(&glob->lru_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000494
495 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
496 driver->sync_obj_unref(&sync_obj);
497 if (ret)
498 return ret;
499
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000500 spin_lock(&glob->lru_lock);
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200501 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000502
503 /*
504 * We raced, and lost, someone else holds the reservation now,
505 * and is probably busy in ttm_bo_cleanup_memtype_use.
506 *
507 * Even if it's not the case, because we finished waiting any
508 * delayed destruction would succeed, so just return success
509 * here.
510 */
511 if (ret) {
512 spin_unlock(&glob->lru_lock);
513 return 0;
514 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100515
516 /*
517 * remove sync_obj with ttm_bo_wait, the wait should be
518 * finished, and no new wait object should have been added.
519 */
Maarten Lankhorst70401382014-01-21 13:07:01 +0100520 ret = ttm_bo_wait(bo, false, false, true);
521 WARN_ON(ret);
522 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000523
524 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100525 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000526 spin_unlock(&glob->lru_lock);
527 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200528 }
529
530 put_count = ttm_bo_del_from_lru(bo);
531 list_del_init(&bo->ddestroy);
532 ++put_count;
533
534 spin_unlock(&glob->lru_lock);
535 ttm_bo_cleanup_memtype_use(bo);
536
Dave Airlied6ea8882010-11-22 13:24:40 +1000537 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200538
539 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200540}
541
542/**
543 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
544 * encountered buffers.
545 */
546
547static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
548{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200549 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100550 struct ttm_buffer_object *entry = NULL;
551 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200552
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200553 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100554 if (list_empty(&bdev->ddestroy))
555 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200556
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100557 entry = list_first_entry(&bdev->ddestroy,
558 struct ttm_buffer_object, ddestroy);
559 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200560
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100561 for (;;) {
562 struct ttm_buffer_object *nentry = NULL;
563
564 if (entry->ddestroy.next != &bdev->ddestroy) {
565 nentry = list_first_entry(&entry->ddestroy,
566 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200567 kref_get(&nentry->list_kref);
568 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200569
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200570 ret = __ttm_bo_reserve(entry, false, true, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100571 if (remove_all && ret) {
572 spin_unlock(&glob->lru_lock);
Thomas Hellstromc7523082014-02-20 11:36:25 +0100573 ret = __ttm_bo_reserve(entry, false, false,
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200574 false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100575 spin_lock(&glob->lru_lock);
576 }
577
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000578 if (!ret)
579 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
580 !remove_all);
581 else
582 spin_unlock(&glob->lru_lock);
583
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200584 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100585 entry = nentry;
586
587 if (ret || !entry)
588 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200589
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200590 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100591 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200592 break;
593 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200594
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100595out_unlock:
596 spin_unlock(&glob->lru_lock);
597out:
598 if (entry)
599 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200600 return ret;
601}
602
603static void ttm_bo_delayed_workqueue(struct work_struct *work)
604{
605 struct ttm_bo_device *bdev =
606 container_of(work, struct ttm_bo_device, wq.work);
607
608 if (ttm_bo_delayed_delete(bdev, false)) {
609 schedule_delayed_work(&bdev->wq,
610 ((HZ / 100) < 1) ? 1 : HZ / 100);
611 }
612}
613
614static void ttm_bo_release(struct kref *kref)
615{
616 struct ttm_buffer_object *bo =
617 container_of(kref, struct ttm_buffer_object, kref);
618 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100619 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200620
David Herrmann72525b32013-07-24 21:08:53 +0200621 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100622 ttm_mem_io_lock(man, false);
623 ttm_mem_io_free_vm(bo);
624 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200625 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200626 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200627}
628
629void ttm_bo_unref(struct ttm_buffer_object **p_bo)
630{
631 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200632
633 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200634 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200635}
636EXPORT_SYMBOL(ttm_bo_unref);
637
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400638int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
639{
640 return cancel_delayed_work_sync(&bdev->wq);
641}
642EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
643
644void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
645{
646 if (resched)
647 schedule_delayed_work(&bdev->wq,
648 ((HZ / 100) < 1) ? 1 : HZ / 100);
649}
650EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
651
Jerome Glisseca262a9992009-12-08 15:33:32 +0100652static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000653 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200654{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200655 struct ttm_bo_device *bdev = bo->bdev;
656 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100657 struct ttm_placement placement;
658 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200659
Dave Airlie1717c0e2011-10-27 18:28:37 +0200660 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200661
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200662 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100663 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700664 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200665 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200666 goto out;
667 }
668
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200669 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200670
671 evict_mem = bo->mem;
672 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100673 evict_mem.bus.io_reserved_vm = false;
674 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200675
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100676 placement.num_placement = 0;
677 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100678 bdev->driver->evict_flags(bo, &placement);
679 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000680 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200681 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100682 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700683 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
684 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100685 ttm_bo_mem_space_debug(bo, &placement);
686 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200687 goto out;
688 }
689
690 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000691 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200692 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100693 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700694 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000695 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200696 goto out;
697 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200698 bo->evicted = true;
699out:
700 return ret;
701}
702
Jerome Glisseca262a9992009-12-08 15:33:32 +0100703static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
704 uint32_t mem_type,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000705 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000706 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100707{
708 struct ttm_bo_global *glob = bdev->glob;
709 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
710 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000711 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100712
713 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000714 list_for_each_entry(bo, &man->lru, lru) {
Martin Kepplinger0eff2a22014-06-15 02:10:39 +0200715 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000716 if (!ret)
717 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100718 }
719
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000720 if (ret) {
721 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000722 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200723 }
724
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000725 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100726
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000727 if (!list_empty(&bo->ddestroy)) {
728 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
729 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100730 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000731 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100732 }
733
734 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100735 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100736
737 BUG_ON(ret != 0);
738
Dave Airlied6ea8882010-11-22 13:24:40 +1000739 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100740
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000741 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100742 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100743
Jerome Glisseca262a9992009-12-08 15:33:32 +0100744 kref_put(&bo->list_kref, ttm_bo_release_list);
745 return ret;
746}
747
Ben Skeggs42311ff2010-08-04 12:07:08 +1000748void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
749{
Ben Skeggsd961db72010-08-05 10:48:18 +1000750 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000751
Ben Skeggsd961db72010-08-05 10:48:18 +1000752 if (mem->mm_node)
753 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000754}
755EXPORT_SYMBOL(ttm_bo_mem_put);
756
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200757/**
758 * Repeatedly evict memory from the LRU for @mem_type until we create enough
759 * space, or we've evicted everything and there isn't enough space.
760 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100761static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
762 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200763 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100764 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000765 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000766 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200767{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100768 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200769 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200770 int ret;
771
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200772 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200773 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200774 if (unlikely(ret != 0))
775 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000776 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100777 break;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000778 ret = ttm_mem_evict_first(bdev, mem_type,
779 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100780 if (unlikely(ret != 0))
781 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200782 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000783 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200784 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200785 mem->mem_type = mem_type;
786 return 0;
787}
788
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200789static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
790 uint32_t cur_placement,
791 uint32_t proposed_placement)
792{
793 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
794 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
795
796 /**
797 * Keep current caching if possible.
798 */
799
800 if ((cur_placement & caching) != 0)
801 result |= (cur_placement & caching);
802 else if ((man->default_caching & caching) != 0)
803 result |= man->default_caching;
804 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
805 result |= TTM_PL_FLAG_CACHED;
806 else if ((TTM_PL_FLAG_WC & caching) != 0)
807 result |= TTM_PL_FLAG_WC;
808 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
809 result |= TTM_PL_FLAG_UNCACHED;
810
811 return result;
812}
813
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200814static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200815 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200816 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200817 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200818{
819 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
820
Christian Königf1217ed2014-08-27 13:16:04 +0200821 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200822 return false;
823
Christian Königf1217ed2014-08-27 13:16:04 +0200824 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200825 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200826
Christian Königf1217ed2014-08-27 13:16:04 +0200827 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200828
829 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200830 return true;
831}
832
833/**
834 * Creates space for memory region @mem according to its type.
835 *
836 * This function first searches for free space in compatible memory types in
837 * the priority order defined by the driver. If free space isn't found, then
838 * ttm_bo_mem_force_space is attempted in priority order to evict and find
839 * space.
840 */
841int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100842 struct ttm_placement *placement,
843 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000844 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000845 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200846{
847 struct ttm_bo_device *bdev = bo->bdev;
848 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200849 uint32_t mem_type = TTM_PL_SYSTEM;
850 uint32_t cur_flags = 0;
851 bool type_found = false;
852 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100853 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100854 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200855
856 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000857 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200858 const struct ttm_place *place = &placement->placement[i];
859
860 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100861 if (ret)
862 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200863 man = &bdev->man[mem_type];
864
Christian Königf1217ed2014-08-27 13:16:04 +0200865 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100866 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867
868 if (!type_ok)
869 continue;
870
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200871 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
872 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100873 /*
874 * Use the access and other non-mapping-related flag bits from
875 * the memory placement flags to the current flags
876 */
Christian Königf1217ed2014-08-27 13:16:04 +0200877 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100878 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200879
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880 if (mem_type == TTM_PL_SYSTEM)
881 break;
882
883 if (man->has_type && man->use_type) {
884 type_found = true;
Christian Königf1217ed2014-08-27 13:16:04 +0200885 ret = (*man->func->get_node)(man, bo, place, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100886 if (unlikely(ret))
887 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200888 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000889 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200890 break;
891 }
892
Ben Skeggsd961db72010-08-05 10:48:18 +1000893 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200894 mem->mem_type = mem_type;
895 mem->placement = cur_flags;
896 return 0;
897 }
898
899 if (!type_found)
900 return -EINVAL;
901
Dave Airlieb6637522009-12-14 14:51:35 +1000902 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200903 const struct ttm_place *place = &placement->busy_placement[i];
904
905 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100906 if (ret)
907 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200908 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200909 if (!man->has_type)
910 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200911 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200912 continue;
913
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200914 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
915 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100916 /*
917 * Use the access and other non-mapping-related flag bits from
918 * the memory placement flags to the current flags
919 */
Christian Königf1217ed2014-08-27 13:16:04 +0200920 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100921 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200922
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100923 if (mem_type == TTM_PL_SYSTEM) {
924 mem->mem_type = mem_type;
925 mem->placement = cur_flags;
926 mem->mm_node = NULL;
927 return 0;
928 }
929
Christian Königf1217ed2014-08-27 13:16:04 +0200930 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000931 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200932 if (ret == 0 && mem->mm_node) {
933 mem->placement = cur_flags;
934 return 0;
935 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100936 if (ret == -ERESTARTSYS)
937 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100939 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200940 return ret;
941}
942EXPORT_SYMBOL(ttm_bo_mem_space);
943
Rashika Kheria6e87fa42014-01-06 22:12:58 +0530944static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100945 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000946 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000947 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200948{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200949 int ret = 0;
950 struct ttm_mem_reg mem;
951
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200952 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953
954 /*
955 * FIXME: It's possible to pipeline buffer moves.
956 * Have the driver move function wait for idle when necessary,
957 * instead of doing it here.
958 */
Dave Airlie1717c0e2011-10-27 18:28:37 +0200959 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200960 if (ret)
961 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200962 mem.num_pages = bo->num_pages;
963 mem.size = mem.num_pages << PAGE_SHIFT;
964 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100965 mem.bus.io_reserved_vm = false;
966 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200967 /*
968 * Determine where to move the buffer.
969 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000970 ret = ttm_bo_mem_space(bo, placement, &mem,
971 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200972 if (ret)
973 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000974 ret = ttm_bo_handle_move_mem(bo, &mem, false,
975 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +1000977 if (ret && mem.mm_node)
978 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200979 return ret;
980}
981
Thomas Hellstrom59c8e662013-10-28 02:02:19 -0700982static bool ttm_bo_mem_compat(struct ttm_placement *placement,
983 struct ttm_mem_reg *mem,
984 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200985{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100986 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +0100987
Jerome Glisseca262a9992009-12-08 15:33:32 +0100988 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +0200989 const struct ttm_place *heap = &placement->placement[i];
990 if (mem->mm_node && heap->lpfn != 0 &&
991 (mem->start < heap->fpfn ||
992 mem->start + mem->num_pages > heap->lpfn))
993 continue;
994
995 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -0700996 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
997 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
998 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100999 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001000
1001 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001002 const struct ttm_place *heap = &placement->busy_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;
1012 }
1013
1014 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015}
1016
Jerome Glisse09855ac2009-12-10 17:16:27 +01001017int ttm_bo_validate(struct ttm_buffer_object *bo,
1018 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001019 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001020 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001021{
1022 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001023 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001024
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001025 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001026 /*
1027 * Check whether we need to move buffer.
1028 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001029 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001030 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1031 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001032 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001033 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001034 } else {
1035 /*
1036 * Use the access and other non-mapping-related flag bits from
1037 * the compatible memory placement flags to the active flags
1038 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001039 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001040 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001041 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001042 /*
1043 * We might need to add a TTM.
1044 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001045 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1046 ret = ttm_bo_add_ttm(bo, true);
1047 if (ret)
1048 return ret;
1049 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050 return 0;
1051}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001052EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001053
Jerome Glisse09855ac2009-12-10 17:16:27 +01001054int ttm_bo_init(struct ttm_bo_device *bdev,
1055 struct ttm_buffer_object *bo,
1056 unsigned long size,
1057 enum ttm_bo_type type,
1058 struct ttm_placement *placement,
1059 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001060 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001061 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001062 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001063 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001064 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001065{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001066 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001067 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001068 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001069 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001070
1071 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1072 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001073 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001074 if (destroy)
1075 (*destroy)(bo);
1076 else
1077 kfree(bo);
1078 return -ENOMEM;
1079 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001080
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001081 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1082 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001083 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001084 if (destroy)
1085 (*destroy)(bo);
1086 else
1087 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001088 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089 return -EINVAL;
1090 }
1091 bo->destroy = destroy;
1092
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093 kref_init(&bo->kref);
1094 kref_init(&bo->list_kref);
1095 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001096 INIT_LIST_HEAD(&bo->lru);
1097 INIT_LIST_HEAD(&bo->ddestroy);
1098 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001099 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001100 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001102 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001103 bo->type = type;
1104 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001105 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106 bo->mem.mem_type = TTM_PL_SYSTEM;
1107 bo->mem.num_pages = bo->num_pages;
1108 bo->mem.mm_node = NULL;
1109 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001110 bo->mem.bus.io_reserved_vm = false;
1111 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001112 bo->priv_flags = 0;
1113 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001114 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001115 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001116 bo->sg = sg;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001117 bo->resv = &bo->ttm_resv;
1118 reservation_object_init(bo->resv);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001119 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001120 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001121
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001122 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001123 * For ttm_bo_type_device buffers, allocate
1124 * address space from the device.
1125 */
Christian Königf1217ed2014-08-27 13:16:04 +02001126 if (bo->type == ttm_bo_type_device ||
1127 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001128 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1129 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001130
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001131 locked = ww_mutex_trylock(&bo->resv->lock);
1132 WARN_ON(!locked);
1133
1134 if (likely(!ret))
1135 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001136
1137 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001138
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001139 if (unlikely(ret))
1140 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001141
1142 return ret;
1143}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001144EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001145
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001146size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1147 unsigned long bo_size,
1148 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001150 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1151 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001153 size += ttm_round_pot(struct_size);
1154 size += PAGE_ALIGN(npages * sizeof(void *));
1155 size += ttm_round_pot(sizeof(struct ttm_tt));
1156 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001157}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001158EXPORT_SYMBOL(ttm_bo_acc_size);
1159
1160size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1161 unsigned long bo_size,
1162 unsigned struct_size)
1163{
1164 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1165 size_t size = 0;
1166
1167 size += ttm_round_pot(struct_size);
1168 size += PAGE_ALIGN(npages * sizeof(void *));
1169 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1170 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1171 return size;
1172}
1173EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001174
Jerome Glisse09855ac2009-12-10 17:16:27 +01001175int ttm_bo_create(struct ttm_bo_device *bdev,
1176 unsigned long size,
1177 enum ttm_bo_type type,
1178 struct ttm_placement *placement,
1179 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001180 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001181 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001182 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001183{
1184 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001185 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001186 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001188 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001189 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001190 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001191
Thomas Hellstroma393c732012-06-12 13:28:42 +02001192 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001193 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001194 interruptible, persistent_swap_storage, acc_size,
1195 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196 if (likely(ret == 0))
1197 *p_bo = bo;
1198
1199 return ret;
1200}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001201EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001202
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001204 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001205{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001206 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001207 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001208 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209
1210 /*
1211 * Can't use standard list traversal since we're unlocking.
1212 */
1213
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001214 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001215 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001216 spin_unlock(&glob->lru_lock);
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001217 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001218 if (ret) {
1219 if (allow_errors) {
1220 return ret;
1221 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001222 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001223 }
1224 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001225 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001226 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001227 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228 return 0;
1229}
1230
1231int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1232{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001233 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001234 int ret = -EINVAL;
1235
1236 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001237 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001238 return ret;
1239 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001240 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001241
1242 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001243 pr_err("Trying to take down uninitialized memory manager type %u\n",
1244 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001245 return ret;
1246 }
1247
1248 man->use_type = false;
1249 man->has_type = false;
1250
1251 ret = 0;
1252 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001253 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001254
Ben Skeggsd961db72010-08-05 10:48:18 +10001255 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256 }
1257
1258 return ret;
1259}
1260EXPORT_SYMBOL(ttm_bo_clean_mm);
1261
1262int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1263{
1264 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1265
1266 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001267 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001268 return -EINVAL;
1269 }
1270
1271 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001272 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001273 return 0;
1274 }
1275
Jerome Glisseca262a9992009-12-08 15:33:32 +01001276 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001277}
1278EXPORT_SYMBOL(ttm_bo_evict_mm);
1279
1280int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001281 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001282{
1283 int ret = -EINVAL;
1284 struct ttm_mem_type_manager *man;
1285
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001286 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001287 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001288 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001289 man->io_reserve_fastpath = true;
1290 man->use_io_reserve_lru = false;
1291 mutex_init(&man->io_reserve_mutex);
1292 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001293
1294 ret = bdev->driver->init_mem_type(bdev, type, man);
1295 if (ret)
1296 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001297 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298
1299 ret = 0;
1300 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001301 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302 if (ret)
1303 return ret;
1304 }
1305 man->has_type = true;
1306 man->use_type = true;
1307 man->size = p_size;
1308
1309 INIT_LIST_HEAD(&man->lru);
1310
1311 return 0;
1312}
1313EXPORT_SYMBOL(ttm_bo_init_mm);
1314
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001315static void ttm_bo_global_kobj_release(struct kobject *kobj)
1316{
1317 struct ttm_bo_global *glob =
1318 container_of(kobj, struct ttm_bo_global, kobj);
1319
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001320 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1321 __free_page(glob->dummy_read_page);
1322 kfree(glob);
1323}
1324
Dave Airlieba4420c2010-03-09 10:56:52 +10001325void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001326{
1327 struct ttm_bo_global *glob = ref->object;
1328
1329 kobject_del(&glob->kobj);
1330 kobject_put(&glob->kobj);
1331}
1332EXPORT_SYMBOL(ttm_bo_global_release);
1333
Dave Airlieba4420c2010-03-09 10:56:52 +10001334int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001335{
1336 struct ttm_bo_global_ref *bo_ref =
1337 container_of(ref, struct ttm_bo_global_ref, ref);
1338 struct ttm_bo_global *glob = ref->object;
1339 int ret;
1340
1341 mutex_init(&glob->device_list_mutex);
1342 spin_lock_init(&glob->lru_lock);
1343 glob->mem_glob = bo_ref->mem_glob;
1344 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1345
1346 if (unlikely(glob->dummy_read_page == NULL)) {
1347 ret = -ENOMEM;
1348 goto out_no_drp;
1349 }
1350
1351 INIT_LIST_HEAD(&glob->swap_lru);
1352 INIT_LIST_HEAD(&glob->device_list);
1353
1354 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1355 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1356 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001357 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001358 goto out_no_shrink;
1359 }
1360
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001361 atomic_set(&glob->bo_count, 0);
1362
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001363 ret = kobject_init_and_add(
1364 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001365 if (unlikely(ret != 0))
1366 kobject_put(&glob->kobj);
1367 return ret;
1368out_no_shrink:
1369 __free_page(glob->dummy_read_page);
1370out_no_drp:
1371 kfree(glob);
1372 return ret;
1373}
1374EXPORT_SYMBOL(ttm_bo_global_init);
1375
1376
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001377int ttm_bo_device_release(struct ttm_bo_device *bdev)
1378{
1379 int ret = 0;
1380 unsigned i = TTM_NUM_MEM_TYPES;
1381 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001382 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001383
1384 while (i--) {
1385 man = &bdev->man[i];
1386 if (man->has_type) {
1387 man->use_type = false;
1388 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1389 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001390 pr_err("DRM memory manager type %d is not clean\n",
1391 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001392 }
1393 man->has_type = false;
1394 }
1395 }
1396
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001397 mutex_lock(&glob->device_list_mutex);
1398 list_del(&bdev->device_list);
1399 mutex_unlock(&glob->device_list_mutex);
1400
Tejun Heof094cfc2010-12-24 15:59:06 +01001401 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001402
1403 while (ttm_bo_delayed_delete(bdev, true))
1404 ;
1405
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001406 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001407 if (list_empty(&bdev->ddestroy))
1408 TTM_DEBUG("Delayed destroy list was clean\n");
1409
1410 if (list_empty(&bdev->man[0].lru))
1411 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001412 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001413
David Herrmann72525b32013-07-24 21:08:53 +02001414 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001415
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001416 return ret;
1417}
1418EXPORT_SYMBOL(ttm_bo_device_release);
1419
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001420int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001421 struct ttm_bo_global *glob,
1422 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001423 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001424 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001425 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001426{
1427 int ret = -EINVAL;
1428
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001429 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001430
1431 memset(bdev->man, 0, sizeof(bdev->man));
1432
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001433 /*
1434 * Initialize the system memory buffer type.
1435 * Other types need to be driver / IOCTL initialized.
1436 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001437 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001438 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001439 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001440
David Herrmann72525b32013-07-24 21:08:53 +02001441 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1442 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001443 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001444 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001445 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001446 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001447 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001448 bdev->val_seq = 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001449 mutex_lock(&glob->device_list_mutex);
1450 list_add_tail(&bdev->device_list, &glob->device_list);
1451 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001452
1453 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001454out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001455 return ret;
1456}
1457EXPORT_SYMBOL(ttm_bo_device_init);
1458
1459/*
1460 * buffer object vm functions.
1461 */
1462
1463bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1464{
1465 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1466
1467 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1468 if (mem->mem_type == TTM_PL_SYSTEM)
1469 return false;
1470
1471 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1472 return false;
1473
1474 if (mem->placement & TTM_PL_FLAG_CACHED)
1475 return false;
1476 }
1477 return true;
1478}
1479
Thomas Hellstromeba67092010-11-11 09:41:57 +01001480void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001481{
1482 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001483
David Herrmann51335df2013-07-24 21:10:03 +02001484 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001485 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001486}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001487
1488void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1489{
1490 struct ttm_bo_device *bdev = bo->bdev;
1491 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1492
1493 ttm_mem_io_lock(man, false);
1494 ttm_bo_unmap_virtual_locked(bo);
1495 ttm_mem_io_unlock(man);
1496}
1497
1498
Dave Airliee024e112009-06-24 09:48:08 +10001499EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001501
1502int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001503 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001504{
1505 struct ttm_bo_driver *driver = bo->bdev->driver;
1506 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001507 int ret = 0;
1508
Maarten Lankhorst70401382014-01-21 13:07:01 +01001509 lockdep_assert_held(&bo->resv->lock.base);
1510
Dave Airlie1717c0e2011-10-27 18:28:37 +02001511 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001512 return 0;
1513
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +01001514 if (bo->sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001515 if (driver->sync_obj_signaled(bo->sync_obj)) {
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +01001516 driver->sync_obj_unref(&bo->sync_obj);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001517 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +01001518 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001519 }
1520
1521 if (no_wait)
1522 return -EBUSY;
1523
Dave Airlie1717c0e2011-10-27 18:28:37 +02001524 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001525 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526 lazy, interruptible);
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +01001527
1528 if (likely(ret == 0)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001529 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1530 &bo->priv_flags);
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +01001531 driver->sync_obj_unref(&bo->sync_obj);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001532 }
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +01001533 driver->sync_obj_unref(&sync_obj);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534 }
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +01001535 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536}
1537EXPORT_SYMBOL(ttm_bo_wait);
1538
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001539int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1540{
1541 int ret = 0;
1542
1543 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001544 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001545 */
1546
Martin Kepplinger0eff2a22014-06-15 02:10:39 +02001547 ret = ttm_bo_reserve(bo, true, no_wait, false, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001548 if (unlikely(ret != 0))
1549 return ret;
Dave Airlie1717c0e2011-10-27 18:28:37 +02001550 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001551 if (likely(ret == 0))
1552 atomic_inc(&bo->cpu_writers);
1553 ttm_bo_unreserve(bo);
1554 return ret;
1555}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001556EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001557
1558void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1559{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001560 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001561}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001562EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563
1564/**
1565 * A buffer object shrink method that tries to swap out the first
1566 * buffer object on the bo_global::swap_lru list.
1567 */
1568
1569static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1570{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001571 struct ttm_bo_global *glob =
1572 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001573 struct ttm_buffer_object *bo;
1574 int ret = -EBUSY;
1575 int put_count;
1576 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1577
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001578 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001579 list_for_each_entry(bo, &glob->swap_lru, swap) {
Martin Kepplinger0eff2a22014-06-15 02:10:39 +02001580 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001581 if (!ret)
1582 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001583 }
1584
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001585 if (ret) {
1586 spin_unlock(&glob->lru_lock);
1587 return ret;
1588 }
1589
1590 kref_get(&bo->list_kref);
1591
1592 if (!list_empty(&bo->ddestroy)) {
1593 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1594 kref_put(&bo->list_kref, ttm_bo_release_list);
1595 return ret;
1596 }
1597
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001598 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001599 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001600
Dave Airlied6ea8882010-11-22 13:24:40 +10001601 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001602
1603 /**
1604 * Wait for GPU, then move to system cached.
1605 */
1606
Dave Airlie1717c0e2011-10-27 18:28:37 +02001607 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001608
1609 if (unlikely(ret != 0))
1610 goto out;
1611
1612 if ((bo->mem.placement & swap_placement) != swap_placement) {
1613 struct ttm_mem_reg evict_mem;
1614
1615 evict_mem = bo->mem;
1616 evict_mem.mm_node = NULL;
1617 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1618 evict_mem.mem_type = TTM_PL_SYSTEM;
1619
1620 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001621 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001622 if (unlikely(ret != 0))
1623 goto out;
1624 }
1625
1626 ttm_bo_unmap_virtual(bo);
1627
1628 /**
1629 * Swap out. Buffer will be swapped in again as soon as
1630 * anyone tries to access a ttm page.
1631 */
1632
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001633 if (bo->bdev->driver->swap_notify)
1634 bo->bdev->driver->swap_notify(bo);
1635
Jan Engelhardt5df23972011-04-04 01:25:18 +02001636 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001637out:
1638
1639 /**
1640 *
1641 * Unreserve without putting on LRU to avoid swapping out an
1642 * already swapped buffer.
1643 */
1644
Thomas Hellstromc7523082014-02-20 11:36:25 +01001645 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001646 kref_put(&bo->list_kref, ttm_bo_release_list);
1647 return ret;
1648}
1649
1650void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1651{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001652 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001653 ;
1654}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001655EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001656
1657/**
1658 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1659 * unreserved
1660 *
1661 * @bo: Pointer to buffer
1662 */
1663int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1664{
1665 int ret;
1666
1667 /*
1668 * In the absense of a wait_unlocked API,
1669 * Use the bo::wu_mutex to avoid triggering livelocks due to
1670 * concurrent use of this function. Note that this use of
1671 * bo::wu_mutex can go away if we change locking order to
1672 * mmap_sem -> bo::reserve.
1673 */
1674 ret = mutex_lock_interruptible(&bo->wu_mutex);
1675 if (unlikely(ret != 0))
1676 return -ERESTARTSYS;
1677 if (!ww_mutex_is_locked(&bo->resv->lock))
1678 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001679 ret = __ttm_bo_reserve(bo, true, false, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001680 if (unlikely(ret != 0))
1681 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001682 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001683
1684out_unlock:
1685 mutex_unlock(&bo->wu_mutex);
1686 return ret;
1687}