blob: ffc6cb55c78c149a1c53314847e2cf8076050a29 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/jiffies.h>
37#include <linux/slab.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/file.h>
41#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +020043#include <linux/reservation.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020044
45#define TTM_ASSERT_LOCKED(param)
46#define TTM_DEBUG(fmt, arg...)
47#define TTM_BO_HASH_ORDER 13
48
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020049static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020050static void ttm_bo_global_kobj_release(struct kobject *kobj);
51
52static struct attribute ttm_bo_count = {
53 .name = "bo_count",
54 .mode = S_IRUGO
55};
56
Christian Königf1217ed2014-08-27 13:16:04 +020057static inline int ttm_mem_type_from_place(const struct ttm_place *place,
58 uint32_t *mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010059{
Christian König5d98d0bc2016-09-12 13:16:16 +020060 int pos;
Jerome Glissefb53f862009-12-09 21:55:10 +010061
Christian König5d98d0bc2016-09-12 13:16:16 +020062 pos = ffs(place->flags & TTM_PL_MASK_MEM);
63 if (unlikely(!pos))
64 return -EINVAL;
65
66 *mem_type = pos - 1;
67 return 0;
Jerome Glissefb53f862009-12-09 21:55:10 +010068}
69
Jerome Glisse5012f502009-12-10 18:07:26 +010070static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010071{
Jerome Glisse5012f502009-12-10 18:07:26 +010072 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
73
Joe Perches25d04792012-03-16 21:43:50 -070074 pr_err(" has_type: %d\n", man->has_type);
75 pr_err(" use_type: %d\n", man->use_type);
76 pr_err(" flags: 0x%08X\n", man->flags);
Alex Deucher54c4cd62015-03-04 00:18:38 -050077 pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
Joe Perches25d04792012-03-16 21:43:50 -070078 pr_err(" size: %llu\n", man->size);
79 pr_err(" available_caching: 0x%08X\n", man->available_caching);
80 pr_err(" default_caching: 0x%08X\n", man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100081 if (mem_type != TTM_PL_SYSTEM)
82 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010083}
84
85static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
86 struct ttm_placement *placement)
87{
Jerome Glissefb53f862009-12-09 21:55:10 +010088 int i, ret, mem_type;
89
Joe Perches25d04792012-03-16 21:43:50 -070090 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
91 bo, bo->mem.num_pages, bo->mem.size >> 10,
92 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010093 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +020094 ret = ttm_mem_type_from_place(&placement->placement[i],
Jerome Glissefb53f862009-12-09 21:55:10 +010095 &mem_type);
96 if (ret)
97 return;
Joe Perches25d04792012-03-16 21:43:50 -070098 pr_err(" placement[%d]=0x%08X (%d)\n",
Christian Königf1217ed2014-08-27 13:16:04 +020099 i, placement->placement[i].flags, mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +0100100 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100101 }
102}
103
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200104static ssize_t ttm_bo_global_show(struct kobject *kobj,
105 struct attribute *attr,
106 char *buffer)
107{
108 struct ttm_bo_global *glob =
109 container_of(kobj, struct ttm_bo_global, kobj);
110
111 return snprintf(buffer, PAGE_SIZE, "%lu\n",
112 (unsigned long) atomic_read(&glob->bo_count));
113}
114
115static struct attribute *ttm_bo_global_attrs[] = {
116 &ttm_bo_count,
117 NULL
118};
119
Emese Revfy52cf25d2010-01-19 02:58:23 +0100120static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200121 .show = &ttm_bo_global_show
122};
123
124static struct kobj_type ttm_bo_glob_kobj_type = {
125 .release = &ttm_bo_global_kobj_release,
126 .sysfs_ops = &ttm_bo_global_ops,
127 .default_attrs = ttm_bo_global_attrs
128};
129
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200130
131static inline uint32_t ttm_bo_type_flags(unsigned type)
132{
133 return 1 << (type);
134}
135
136static void ttm_bo_release_list(struct kref *list_kref)
137{
138 struct ttm_buffer_object *bo =
139 container_of(list_kref, struct ttm_buffer_object, list_kref);
140 struct ttm_bo_device *bdev = bo->bdev;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500141 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200142
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100143 BUG_ON(kref_read(&bo->list_kref));
144 BUG_ON(kref_read(&bo->kref));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200145 BUG_ON(atomic_read(&bo->cpu_writers));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
Christian König4279cb12016-06-06 10:17:51 +0200149 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200150 atomic_dec(&bo->glob->bo_count);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100151 dma_fence_put(bo->moving);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200152 if (bo->resv == &bo->ttm_resv)
153 reservation_object_fini(&bo->ttm_resv);
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800154 mutex_destroy(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200155 if (bo->destroy)
156 bo->destroy(bo);
157 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200158 kfree(bo);
159 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500160 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200161}
162
Dave Airlied6ea8882010-11-22 13:24:40 +1000163void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164{
165 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200166
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200167 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200168
169 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
170
171 BUG_ON(!list_empty(&bo->lru));
172
Christian König98c28722016-04-06 11:12:07 +0200173 list_add(&bo->lru, bdev->driver->lru_tail(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200174 kref_get(&bo->list_kref);
175
Christian Königed704a42016-01-11 15:35:19 +0100176 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
Christian König98c28722016-04-06 11:12:07 +0200177 list_add(&bo->swap, bdev->driver->swap_lru_tail(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200178 kref_get(&bo->list_kref);
179 }
180 }
181}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200182EXPORT_SYMBOL(ttm_bo_add_to_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200183
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100184static void ttm_bo_ref_bug(struct kref *list_kref)
185{
186 BUG();
187}
188
189void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200190{
Christian Königc3ea5762016-04-06 11:12:06 +0200191 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200192
Christian Königc3ea5762016-04-06 11:12:06 +0200193 if (bdev->driver->lru_removal)
194 bdev->driver->lru_removal(bo);
195
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200196 if (!list_empty(&bo->swap)) {
197 list_del_init(&bo->swap);
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100198 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200199 }
200 if (!list_empty(&bo->lru)) {
201 list_del_init(&bo->lru);
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100202 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200203 }
Dave Airlied6ea8882010-11-22 13:24:40 +1000204}
205
Maarten Lankhorst34820322013-06-27 13:48:24 +0200206void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200207{
Maarten Lankhorst34820322013-06-27 13:48:24 +0200208 spin_lock(&bo->glob->lru_lock);
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100209 ttm_bo_del_from_lru(bo);
Maarten Lankhorst34820322013-06-27 13:48:24 +0200210 spin_unlock(&bo->glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200211}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200212EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200213
Christian Königab749612016-01-11 15:35:20 +0100214void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
215{
216 struct ttm_bo_device *bdev = bo->bdev;
Christian Königab749612016-01-11 15:35:20 +0100217
218 lockdep_assert_held(&bo->resv->lock.base);
219
Christian Königc3ea5762016-04-06 11:12:06 +0200220 if (bdev->driver->lru_removal)
221 bdev->driver->lru_removal(bo);
222
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100223 ttm_bo_del_from_lru(bo);
Flora Cui56fc3502016-04-20 10:23:47 +0800224 ttm_bo_add_to_lru(bo);
Christian Königab749612016-01-11 15:35:20 +0100225}
226EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
227
Christian König98c28722016-04-06 11:12:07 +0200228struct list_head *ttm_bo_default_lru_tail(struct ttm_buffer_object *bo)
229{
230 return bo->bdev->man[bo->mem.mem_type].lru.prev;
231}
232EXPORT_SYMBOL(ttm_bo_default_lru_tail);
233
234struct list_head *ttm_bo_default_swap_lru_tail(struct ttm_buffer_object *bo)
235{
236 return bo->glob->swap_lru.prev;
237}
238EXPORT_SYMBOL(ttm_bo_default_swap_lru_tail);
239
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200240/*
241 * Call bo->mutex locked.
242 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200243static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
244{
245 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200246 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200247 int ret = 0;
248 uint32_t page_flags = 0;
249
250 TTM_ASSERT_LOCKED(&bo->mutex);
251 bo->ttm = NULL;
252
Dave Airliead49f502009-07-10 22:36:26 +1000253 if (bdev->need_dma32)
254 page_flags |= TTM_PAGE_FLAG_DMA32;
255
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200256 switch (bo->type) {
257 case ttm_bo_type_device:
258 if (zero_alloc)
259 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
260 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400261 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
262 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200263 if (unlikely(bo->ttm == NULL))
264 ret = -ENOMEM;
265 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100266 case ttm_bo_type_sg:
267 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
268 page_flags | TTM_PAGE_FLAG_SG,
269 glob->dummy_read_page);
270 if (unlikely(bo->ttm == NULL)) {
271 ret = -ENOMEM;
272 break;
273 }
274 bo->ttm->sg = bo->sg;
275 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200276 default:
Joe Perches25d04792012-03-16 21:43:50 -0700277 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200278 ret = -EINVAL;
279 break;
280 }
281
282 return ret;
283}
284
285static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
286 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000287 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000288 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200289{
290 struct ttm_bo_device *bdev = bo->bdev;
291 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
292 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
293 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
294 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
295 int ret = 0;
296
297 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100298 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
299 ret = ttm_mem_io_lock(old_man, true);
300 if (unlikely(ret != 0))
301 goto out_err;
302 ttm_bo_unmap_virtual_locked(bo);
303 ttm_mem_io_unlock(old_man);
304 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200305
306 /*
307 * Create and bind a ttm if required.
308 */
309
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000310 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
311 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000312 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
313 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000314 if (ret)
315 goto out_err;
316 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200317
318 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
319 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200320 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200321
322 if (mem->mem_type != TTM_PL_SYSTEM) {
323 ret = ttm_tt_bind(bo->ttm, mem);
324 if (ret)
325 goto out_err;
326 }
327
328 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000329 if (bdev->driver->move_notify)
330 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100331 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200332 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200333 goto moved;
334 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200335 }
336
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000337 if (bdev->driver->move_notify)
338 bdev->driver->move_notify(bo, mem);
339
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200340 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
341 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900342 ret = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200343 else if (bdev->driver->move)
344 ret = bdev->driver->move(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000345 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200346 else
Michel Dänzer4499f2a2016-08-08 12:28:26 +0900347 ret = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200348
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000349 if (ret) {
350 if (bdev->driver->move_notify) {
351 struct ttm_mem_reg tmp_mem = *mem;
352 *mem = bo->mem;
353 bo->mem = tmp_mem;
354 bdev->driver->move_notify(bo, mem);
355 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000356 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000357 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200358
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000359 goto out_err;
360 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500361
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200362moved:
363 if (bo->evicted) {
Rob Clark9ef75062014-03-12 10:59:37 -0400364 if (bdev->driver->invalidate_caches) {
365 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
366 if (ret)
367 pr_err("Can not flush read caches\n");
368 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200369 bo->evicted = false;
370 }
371
372 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000373 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200374 bdev->man[bo->mem.mem_type].gpu_offset;
375 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100376 } else
377 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200378
379 return 0;
380
381out_err:
382 new_man = &bdev->man[bo->mem.mem_type];
Christian König4279cb12016-06-06 10:17:51 +0200383 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200384 ttm_tt_destroy(bo->ttm);
385 bo->ttm = NULL;
386 }
387
388 return ret;
389}
390
391/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200392 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200393 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200394 * This is the place to put in driver specific hooks to release
395 * driver private resources.
396 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200397 */
398
399static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
400{
Jerome Glissedc97b342011-11-18 11:47:03 -0500401 if (bo->bdev->driver->move_notify)
402 bo->bdev->driver->move_notify(bo, NULL);
403
Christian König4279cb12016-06-06 10:17:51 +0200404 ttm_tt_destroy(bo->ttm);
405 bo->ttm = NULL;
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200406 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200407
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200408 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200409}
410
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200411static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
412{
413 struct reservation_object_list *fobj;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100414 struct dma_fence *fence;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200415 int i;
416
417 fobj = reservation_object_get_list(bo->resv);
418 fence = reservation_object_get_excl(bo->resv);
419 if (fence && !fence->ops->signaled)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100420 dma_fence_enable_sw_signaling(fence);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200421
422 for (i = 0; fobj && i < fobj->shared_count; ++i) {
423 fence = rcu_dereference_protected(fobj->shared[i],
424 reservation_object_held(bo->resv));
425
426 if (!fence->ops->signaled)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100427 dma_fence_enable_sw_signaling(fence);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200428 }
429}
430
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200431static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200432{
433 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200434 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200435 int ret;
436
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100437 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200438 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100439
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700440 if (!ret) {
Christian König8aa6d4f2016-04-06 11:12:04 +0200441 if (!ttm_bo_wait(bo, false, true)) {
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100442 ttm_bo_del_from_lru(bo);
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100443 spin_unlock(&glob->lru_lock);
444 ttm_bo_cleanup_memtype_use(bo);
445
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100446 return;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200447 } else
448 ttm_bo_flush_all_fences(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700449
450 /*
451 * Make NO_EVICT bos immediately available to
452 * shrinkers, now that they are queued for
453 * destruction.
454 */
455 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
456 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
457 ttm_bo_add_to_lru(bo);
458 }
459
Thomas Hellstromc7523082014-02-20 11:36:25 +0100460 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700461 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200462
463 kref_get(&bo->list_kref);
464 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
465 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200466
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200467 schedule_delayed_work(&bdev->wq,
468 ((HZ / 100) < 1) ? 1 : HZ / 100);
469}
470
471/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000472 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200473 * If bo idle, remove from delayed- and lru lists, and unref.
474 * If not idle, do nothing.
475 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000476 * Must be called with lru_lock and reservation held, this function
477 * will drop both before returning.
478 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200479 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200480 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
481 */
482
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000483static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
484 bool interruptible,
485 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200486{
487 struct ttm_bo_global *glob = bo->glob;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000488 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200489
Christian König8aa6d4f2016-04-06 11:12:04 +0200490 ret = ttm_bo_wait(bo, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200491
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000492 if (ret && !no_wait_gpu) {
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200493 long lret;
494 ww_mutex_unlock(&bo->resv->lock);
495 spin_unlock(&glob->lru_lock);
496
497 lret = reservation_object_wait_timeout_rcu(bo->resv,
498 true,
499 interruptible,
500 30 * HZ);
501
502 if (lret < 0)
503 return lret;
504 else if (lret == 0)
505 return -EBUSY;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000506
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000507 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200508 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000509
510 /*
511 * We raced, and lost, someone else holds the reservation now,
512 * and is probably busy in ttm_bo_cleanup_memtype_use.
513 *
514 * Even if it's not the case, because we finished waiting any
515 * delayed destruction would succeed, so just return success
516 * here.
517 */
518 if (ret) {
519 spin_unlock(&glob->lru_lock);
520 return 0;
521 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100522
523 /*
524 * remove sync_obj with ttm_bo_wait, the wait should be
525 * finished, and no new wait object should have been added.
526 */
Christian König8aa6d4f2016-04-06 11:12:04 +0200527 ret = ttm_bo_wait(bo, false, true);
Maarten Lankhorst70401382014-01-21 13:07:01 +0100528 WARN_ON(ret);
529 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000530
531 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100532 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000533 spin_unlock(&glob->lru_lock);
534 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200535 }
536
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100537 ttm_bo_del_from_lru(bo);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200538 list_del_init(&bo->ddestroy);
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100539 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200540
541 spin_unlock(&glob->lru_lock);
542 ttm_bo_cleanup_memtype_use(bo);
543
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200544 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200545}
546
547/**
548 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
549 * encountered buffers.
550 */
551
552static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
553{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200554 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100555 struct ttm_buffer_object *entry = NULL;
556 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200557
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200558 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100559 if (list_empty(&bdev->ddestroy))
560 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200561
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100562 entry = list_first_entry(&bdev->ddestroy,
563 struct ttm_buffer_object, ddestroy);
564 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200565
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100566 for (;;) {
567 struct ttm_buffer_object *nentry = NULL;
568
569 if (entry->ddestroy.next != &bdev->ddestroy) {
570 nentry = list_first_entry(&entry->ddestroy,
571 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200572 kref_get(&nentry->list_kref);
573 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200574
Christian Königdfd5e502016-04-06 11:12:03 +0200575 ret = __ttm_bo_reserve(entry, false, true, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100576 if (remove_all && ret) {
577 spin_unlock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200578 ret = __ttm_bo_reserve(entry, false, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100579 spin_lock(&glob->lru_lock);
580 }
581
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000582 if (!ret)
583 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
584 !remove_all);
585 else
586 spin_unlock(&glob->lru_lock);
587
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200588 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100589 entry = nentry;
590
591 if (ret || !entry)
592 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200593
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200594 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100595 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200596 break;
597 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200598
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100599out_unlock:
600 spin_unlock(&glob->lru_lock);
601out:
602 if (entry)
603 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200604 return ret;
605}
606
607static void ttm_bo_delayed_workqueue(struct work_struct *work)
608{
609 struct ttm_bo_device *bdev =
610 container_of(work, struct ttm_bo_device, wq.work);
611
612 if (ttm_bo_delayed_delete(bdev, false)) {
613 schedule_delayed_work(&bdev->wq,
614 ((HZ / 100) < 1) ? 1 : HZ / 100);
615 }
616}
617
618static void ttm_bo_release(struct kref *kref)
619{
620 struct ttm_buffer_object *bo =
621 container_of(kref, struct ttm_buffer_object, kref);
622 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100623 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200624
David Herrmann72525b32013-07-24 21:08:53 +0200625 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100626 ttm_mem_io_lock(man, false);
627 ttm_mem_io_free_vm(bo);
628 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200629 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200630 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200631}
632
633void ttm_bo_unref(struct ttm_buffer_object **p_bo)
634{
635 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200636
637 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200638 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200639}
640EXPORT_SYMBOL(ttm_bo_unref);
641
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400642int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
643{
644 return cancel_delayed_work_sync(&bdev->wq);
645}
646EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
647
648void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
649{
650 if (resched)
651 schedule_delayed_work(&bdev->wq,
652 ((HZ / 100) < 1) ? 1 : HZ / 100);
653}
654EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
655
Jerome Glisseca262a9992009-12-08 15:33:32 +0100656static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000657 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200658{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200659 struct ttm_bo_device *bdev = bo->bdev;
660 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100661 struct ttm_placement placement;
662 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200663
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200664 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200665
666 evict_mem = bo->mem;
667 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100668 evict_mem.bus.io_reserved_vm = false;
669 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200670
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100671 placement.num_placement = 0;
672 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100673 bdev->driver->evict_flags(bo, &placement);
674 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000675 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200676 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100677 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700678 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
679 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100680 ttm_bo_mem_space_debug(bo, &placement);
681 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200682 goto out;
683 }
684
685 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000686 no_wait_gpu);
Christian König17d33bc2016-06-06 10:17:56 +0200687 if (unlikely(ret)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100688 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700689 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000690 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200691 goto out;
692 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200693 bo->evicted = true;
694out:
695 return ret;
696}
697
Christian Königa2ab19fe2016-08-30 17:26:04 +0200698bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
699 const struct ttm_place *place)
700{
701 /* Don't evict this BO if it's outside of the
702 * requested placement range
703 */
704 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
705 (place->lpfn && place->lpfn <= bo->mem.start))
706 return false;
707
708 return true;
709}
710EXPORT_SYMBOL(ttm_bo_eviction_valuable);
711
Jerome Glisseca262a9992009-12-08 15:33:32 +0100712static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
713 uint32_t mem_type,
Michel Dänzere3001802014-10-09 15:02:59 +0900714 const struct ttm_place *place,
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;
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100721 int ret = -EBUSY;
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) {
Christian Königdfd5e502016-04-06 11:12:03 +0200725 ret = __ttm_bo_reserve(bo, false, true, NULL);
Christian Königa2ab19fe2016-08-30 17:26:04 +0200726 if (ret)
727 continue;
Michel Dänzere3001802014-10-09 15:02:59 +0900728
Christian Königa2ab19fe2016-08-30 17:26:04 +0200729 if (place && !bdev->driver->eviction_valuable(bo, place)) {
730 __ttm_bo_unreserve(bo);
731 ret = -EBUSY;
732 continue;
Michel Dänzere3001802014-10-09 15:02:59 +0900733 }
Christian Königa2ab19fe2016-08-30 17:26:04 +0200734
735 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100736 }
737
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000738 if (ret) {
739 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000740 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200741 }
742
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000743 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100744
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000745 if (!list_empty(&bo->ddestroy)) {
746 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
747 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100748 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000749 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100750 }
751
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100752 ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100753 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100754
755 BUG_ON(ret != 0);
756
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000757 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100758 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100759
Jerome Glisseca262a9992009-12-08 15:33:32 +0100760 kref_put(&bo->list_kref, ttm_bo_release_list);
761 return ret;
762}
763
Ben Skeggs42311ff2010-08-04 12:07:08 +1000764void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
765{
Ben Skeggsd961db72010-08-05 10:48:18 +1000766 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000767
Ben Skeggsd961db72010-08-05 10:48:18 +1000768 if (mem->mm_node)
769 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000770}
771EXPORT_SYMBOL(ttm_bo_mem_put);
772
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200773/**
Christian König3ddf4ad2016-06-15 13:44:03 +0200774 * Add the last move fence to the BO and reserve a new shared slot.
775 */
776static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
777 struct ttm_mem_type_manager *man,
778 struct ttm_mem_reg *mem)
779{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100780 struct dma_fence *fence;
Christian König3ddf4ad2016-06-15 13:44:03 +0200781 int ret;
782
783 spin_lock(&man->move_lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100784 fence = dma_fence_get(man->move);
Christian König3ddf4ad2016-06-15 13:44:03 +0200785 spin_unlock(&man->move_lock);
786
787 if (fence) {
788 reservation_object_add_shared_fence(bo->resv, fence);
789
790 ret = reservation_object_reserve_shared(bo->resv);
791 if (unlikely(ret))
792 return ret;
793
Chris Wilsonf54d1862016-10-25 13:00:45 +0100794 dma_fence_put(bo->moving);
Christian König3ddf4ad2016-06-15 13:44:03 +0200795 bo->moving = fence;
796 }
797
798 return 0;
799}
800
801/**
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200802 * Repeatedly evict memory from the LRU for @mem_type until we create enough
803 * space, or we've evicted everything and there isn't enough space.
804 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100805static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
806 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200807 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100808 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000809 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000810 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200811{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100812 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200813 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200814 int ret;
815
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200816 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200817 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200818 if (unlikely(ret != 0))
819 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000820 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100821 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900822 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000823 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100824 if (unlikely(ret != 0))
825 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200826 } while (1);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200827 mem->mem_type = mem_type;
Christian König3ddf4ad2016-06-15 13:44:03 +0200828 return ttm_bo_add_move_fence(bo, man, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200829}
830
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200831static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
832 uint32_t cur_placement,
833 uint32_t proposed_placement)
834{
835 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
836 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
837
838 /**
839 * Keep current caching if possible.
840 */
841
842 if ((cur_placement & caching) != 0)
843 result |= (cur_placement & caching);
844 else if ((man->default_caching & caching) != 0)
845 result |= man->default_caching;
846 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
847 result |= TTM_PL_FLAG_CACHED;
848 else if ((TTM_PL_FLAG_WC & caching) != 0)
849 result |= TTM_PL_FLAG_WC;
850 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
851 result |= TTM_PL_FLAG_UNCACHED;
852
853 return result;
854}
855
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200856static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200857 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200858 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200859 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200860{
861 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
862
Christian Königf1217ed2014-08-27 13:16:04 +0200863 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200864 return false;
865
Christian Königf1217ed2014-08-27 13:16:04 +0200866 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200868
Christian Königf1217ed2014-08-27 13:16:04 +0200869 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200870
871 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200872 return true;
873}
874
875/**
876 * Creates space for memory region @mem according to its type.
877 *
878 * This function first searches for free space in compatible memory types in
879 * the priority order defined by the driver. If free space isn't found, then
880 * ttm_bo_mem_force_space is attempted in priority order to evict and find
881 * space.
882 */
883int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100884 struct ttm_placement *placement,
885 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000886 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000887 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200888{
889 struct ttm_bo_device *bdev = bo->bdev;
890 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200891 uint32_t mem_type = TTM_PL_SYSTEM;
892 uint32_t cur_flags = 0;
893 bool type_found = false;
894 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100895 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100896 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200897
Christian König3ddf4ad2016-06-15 13:44:03 +0200898 ret = reservation_object_reserve_shared(bo->resv);
899 if (unlikely(ret))
900 return ret;
901
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200902 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000903 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200904 const struct ttm_place *place = &placement->placement[i];
905
906 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100907 if (ret)
908 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200909 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700910 if (!man->has_type || !man->use_type)
911 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200912
Christian Königf1217ed2014-08-27 13:16:04 +0200913 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100914 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200915
916 if (!type_ok)
917 continue;
918
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700919 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200920 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
921 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100922 /*
923 * Use the access and other non-mapping-related flag bits from
924 * the memory placement flags to the current flags
925 */
Christian Königf1217ed2014-08-27 13:16:04 +0200926 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100927 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200928
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929 if (mem_type == TTM_PL_SYSTEM)
930 break;
931
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700932 ret = (*man->func->get_node)(man, bo, place, mem);
933 if (unlikely(ret))
934 return ret;
Christian König3ddf4ad2016-06-15 13:44:03 +0200935
936 if (mem->mm_node) {
937 ret = ttm_bo_add_move_fence(bo, man, mem);
938 if (unlikely(ret)) {
939 (*man->func->put_node)(man, mem);
940 return ret;
941 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200942 break;
Christian König3ddf4ad2016-06-15 13:44:03 +0200943 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200944 }
945
Ben Skeggsd961db72010-08-05 10:48:18 +1000946 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200947 mem->mem_type = mem_type;
948 mem->placement = cur_flags;
949 return 0;
950 }
951
Dave Airlieb6637522009-12-14 14:51:35 +1000952 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200953 const struct ttm_place *place = &placement->busy_placement[i];
954
955 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100956 if (ret)
957 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200958 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700959 if (!man->has_type || !man->use_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200960 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200961 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200962 continue;
963
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700964 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200965 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
966 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100967 /*
968 * Use the access and other non-mapping-related flag bits from
969 * the memory placement flags to the current flags
970 */
Christian Königf1217ed2014-08-27 13:16:04 +0200971 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100972 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200973
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100974 if (mem_type == TTM_PL_SYSTEM) {
975 mem->mem_type = mem_type;
976 mem->placement = cur_flags;
977 mem->mm_node = NULL;
978 return 0;
979 }
980
Christian Königf1217ed2014-08-27 13:16:04 +0200981 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000982 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200983 if (ret == 0 && mem->mm_node) {
984 mem->placement = cur_flags;
985 return 0;
986 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100987 if (ret == -ERESTARTSYS)
988 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989 }
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700990
991 if (!type_found) {
992 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
993 return -EINVAL;
994 }
995
996 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997}
998EXPORT_SYMBOL(ttm_bo_mem_space);
999
Rashika Kheria6e87fa42014-01-06 22:12:58 +05301000static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001001 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001002 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001003 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001004{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005 int ret = 0;
1006 struct ttm_mem_reg mem;
1007
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001008 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001009
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001010 mem.num_pages = bo->num_pages;
1011 mem.size = mem.num_pages << PAGE_SHIFT;
1012 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001013 mem.bus.io_reserved_vm = false;
1014 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 /*
1016 * Determine where to move the buffer.
1017 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001018 ret = ttm_bo_mem_space(bo, placement, &mem,
1019 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001020 if (ret)
1021 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001022 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1023 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001024out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001025 if (ret && mem.mm_node)
1026 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001027 return ret;
1028}
1029
Sinclair Yeh94477bf2016-06-29 12:58:49 -07001030bool ttm_bo_mem_compat(struct ttm_placement *placement,
1031 struct ttm_mem_reg *mem,
1032 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001033{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001034 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001035
Jerome Glisseca262a9992009-12-08 15:33:32 +01001036 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001037 const struct ttm_place *heap = &placement->placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001038 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001039 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001040 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001041 continue;
1042
1043 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001044 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1045 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1046 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001047 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001048
1049 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001050 const struct ttm_place *heap = &placement->busy_placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001051 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001052 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001053 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001054 continue;
1055
1056 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001057 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1058 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1059 return true;
1060 }
1061
1062 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001063}
Sinclair Yeh94477bf2016-06-29 12:58:49 -07001064EXPORT_SYMBOL(ttm_bo_mem_compat);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001065
Jerome Glisse09855ac2009-12-10 17:16:27 +01001066int ttm_bo_validate(struct ttm_buffer_object *bo,
1067 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001068 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001069 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001070{
1071 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001072 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001073
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001074 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001075 /*
1076 * Check whether we need to move buffer.
1077 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001078 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001079 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1080 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001081 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001083 } else {
1084 /*
1085 * Use the access and other non-mapping-related flag bits from
1086 * the compatible memory placement flags to the active flags
1087 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001088 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001089 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001090 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001091 /*
1092 * We might need to add a TTM.
1093 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001094 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1095 ret = ttm_bo_add_ttm(bo, true);
1096 if (ret)
1097 return ret;
1098 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001099 return 0;
1100}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001101EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102
Jerome Glisse09855ac2009-12-10 17:16:27 +01001103int ttm_bo_init(struct ttm_bo_device *bdev,
1104 struct ttm_buffer_object *bo,
1105 unsigned long size,
1106 enum ttm_bo_type type,
1107 struct ttm_placement *placement,
1108 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001109 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001110 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001111 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001112 struct sg_table *sg,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001113 struct reservation_object *resv,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001114 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001115{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001116 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001118 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001119 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001120
1121 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1122 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001123 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001124 if (destroy)
1125 (*destroy)(bo);
1126 else
1127 kfree(bo);
1128 return -ENOMEM;
1129 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001130
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001131 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1132 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001133 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001134 if (destroy)
1135 (*destroy)(bo);
1136 else
1137 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001138 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001139 return -EINVAL;
1140 }
1141 bo->destroy = destroy;
1142
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143 kref_init(&bo->kref);
1144 kref_init(&bo->list_kref);
1145 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146 INIT_LIST_HEAD(&bo->lru);
1147 INIT_LIST_HEAD(&bo->ddestroy);
1148 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001149 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001150 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001151 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001152 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 bo->type = type;
1154 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001155 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001156 bo->mem.mem_type = TTM_PL_SYSTEM;
1157 bo->mem.num_pages = bo->num_pages;
1158 bo->mem.mm_node = NULL;
1159 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001160 bo->mem.bus.io_reserved_vm = false;
1161 bo->mem.bus.io_reserved_count = 0;
Christian König5bc73062016-06-15 13:44:01 +02001162 bo->moving = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001163 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001164 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001165 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001166 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001167 if (resv) {
1168 bo->resv = resv;
1169 lockdep_assert_held(&bo->resv->lock.base);
1170 } else {
1171 bo->resv = &bo->ttm_resv;
1172 reservation_object_init(&bo->ttm_resv);
1173 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001174 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001175 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001176
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001177 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178 * For ttm_bo_type_device buffers, allocate
1179 * address space from the device.
1180 */
Christian Königf1217ed2014-08-27 13:16:04 +02001181 if (bo->type == ttm_bo_type_device ||
1182 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001183 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1184 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001186 /* passed reservation objects should already be locked,
1187 * since otherwise lockdep will be angered in radeon.
1188 */
1189 if (!resv) {
1190 locked = ww_mutex_trylock(&bo->resv->lock);
1191 WARN_ON(!locked);
1192 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001193
1194 if (likely(!ret))
1195 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196
Christian König33d48cf2016-01-11 15:35:18 +01001197 if (!resv) {
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001198 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199
Christian König33d48cf2016-01-11 15:35:18 +01001200 } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1201 spin_lock(&bo->glob->lru_lock);
1202 ttm_bo_add_to_lru(bo);
1203 spin_unlock(&bo->glob->lru_lock);
1204 }
1205
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001206 if (unlikely(ret))
1207 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001208
1209 return ret;
1210}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001211EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001213size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1214 unsigned long bo_size,
1215 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001216{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001217 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1218 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001220 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001221 size += ttm_round_pot(npages * sizeof(void *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001222 size += ttm_round_pot(sizeof(struct ttm_tt));
1223 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001224}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001225EXPORT_SYMBOL(ttm_bo_acc_size);
1226
1227size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1228 unsigned long bo_size,
1229 unsigned struct_size)
1230{
1231 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1232 size_t size = 0;
1233
1234 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001235 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001236 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1237 return size;
1238}
1239EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001240
Jerome Glisse09855ac2009-12-10 17:16:27 +01001241int ttm_bo_create(struct ttm_bo_device *bdev,
1242 unsigned long size,
1243 enum ttm_bo_type type,
1244 struct ttm_placement *placement,
1245 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001246 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001247 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001248 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001249{
1250 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001251 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001252 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001253
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001254 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001255 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001257
Thomas Hellstroma393c732012-06-12 13:28:42 +02001258 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001259 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001260 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001261 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001262 if (likely(ret == 0))
1263 *p_bo = bo;
1264
1265 return ret;
1266}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001267EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001268
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001270 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001272 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001273 struct ttm_bo_global *glob = bdev->glob;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001274 struct dma_fence *fence;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001275 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001276
1277 /*
1278 * Can't use standard list traversal since we're unlocking.
1279 */
1280
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001281 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001282 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001283 spin_unlock(&glob->lru_lock);
Michel Dänzere3001802014-10-09 15:02:59 +09001284 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001285 if (ret) {
1286 if (allow_errors) {
1287 return ret;
1288 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001289 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001290 }
1291 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001292 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001293 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001294 spin_unlock(&glob->lru_lock);
Christian Königaff98ba2016-06-22 14:16:28 +02001295
1296 spin_lock(&man->move_lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001297 fence = dma_fence_get(man->move);
Christian Königaff98ba2016-06-22 14:16:28 +02001298 spin_unlock(&man->move_lock);
1299
1300 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001301 ret = dma_fence_wait(fence, false);
1302 dma_fence_put(fence);
Christian Königaff98ba2016-06-22 14:16:28 +02001303 if (ret) {
1304 if (allow_errors) {
1305 return ret;
1306 } else {
1307 pr_err("Cleanup eviction failed\n");
1308 }
1309 }
1310 }
1311
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001312 return 0;
1313}
1314
1315int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1316{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001317 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001318 int ret = -EINVAL;
1319
1320 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001321 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001322 return ret;
1323 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001324 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001325
1326 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001327 pr_err("Trying to take down uninitialized memory manager type %u\n",
1328 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001329 return ret;
1330 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01001331 dma_fence_put(man->move);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001332
1333 man->use_type = false;
1334 man->has_type = false;
1335
1336 ret = 0;
1337 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001338 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001339
Ben Skeggsd961db72010-08-05 10:48:18 +10001340 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001341 }
1342
1343 return ret;
1344}
1345EXPORT_SYMBOL(ttm_bo_clean_mm);
1346
1347int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1348{
1349 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1350
1351 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001352 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001353 return -EINVAL;
1354 }
1355
1356 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001357 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001358 return 0;
1359 }
1360
Jerome Glisseca262a9992009-12-08 15:33:32 +01001361 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001362}
1363EXPORT_SYMBOL(ttm_bo_evict_mm);
1364
1365int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001366 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001367{
1368 int ret = -EINVAL;
1369 struct ttm_mem_type_manager *man;
1370
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001371 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001372 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001373 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001374 man->io_reserve_fastpath = true;
1375 man->use_io_reserve_lru = false;
1376 mutex_init(&man->io_reserve_mutex);
Christian König3ddf4ad2016-06-15 13:44:03 +02001377 spin_lock_init(&man->move_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001378 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001379
1380 ret = bdev->driver->init_mem_type(bdev, type, man);
1381 if (ret)
1382 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001383 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001384
1385 ret = 0;
1386 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001387 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001388 if (ret)
1389 return ret;
1390 }
1391 man->has_type = true;
1392 man->use_type = true;
1393 man->size = p_size;
1394
1395 INIT_LIST_HEAD(&man->lru);
Christian König3ddf4ad2016-06-15 13:44:03 +02001396 man->move = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001397
1398 return 0;
1399}
1400EXPORT_SYMBOL(ttm_bo_init_mm);
1401
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001402static void ttm_bo_global_kobj_release(struct kobject *kobj)
1403{
1404 struct ttm_bo_global *glob =
1405 container_of(kobj, struct ttm_bo_global, kobj);
1406
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001407 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1408 __free_page(glob->dummy_read_page);
1409 kfree(glob);
1410}
1411
Dave Airlieba4420c2010-03-09 10:56:52 +10001412void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001413{
1414 struct ttm_bo_global *glob = ref->object;
1415
1416 kobject_del(&glob->kobj);
1417 kobject_put(&glob->kobj);
1418}
1419EXPORT_SYMBOL(ttm_bo_global_release);
1420
Dave Airlieba4420c2010-03-09 10:56:52 +10001421int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001422{
1423 struct ttm_bo_global_ref *bo_ref =
1424 container_of(ref, struct ttm_bo_global_ref, ref);
1425 struct ttm_bo_global *glob = ref->object;
1426 int ret;
1427
1428 mutex_init(&glob->device_list_mutex);
1429 spin_lock_init(&glob->lru_lock);
1430 glob->mem_glob = bo_ref->mem_glob;
1431 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1432
1433 if (unlikely(glob->dummy_read_page == NULL)) {
1434 ret = -ENOMEM;
1435 goto out_no_drp;
1436 }
1437
1438 INIT_LIST_HEAD(&glob->swap_lru);
1439 INIT_LIST_HEAD(&glob->device_list);
1440
1441 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1442 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1443 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001444 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001445 goto out_no_shrink;
1446 }
1447
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001448 atomic_set(&glob->bo_count, 0);
1449
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001450 ret = kobject_init_and_add(
1451 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001452 if (unlikely(ret != 0))
1453 kobject_put(&glob->kobj);
1454 return ret;
1455out_no_shrink:
1456 __free_page(glob->dummy_read_page);
1457out_no_drp:
1458 kfree(glob);
1459 return ret;
1460}
1461EXPORT_SYMBOL(ttm_bo_global_init);
1462
1463
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001464int ttm_bo_device_release(struct ttm_bo_device *bdev)
1465{
1466 int ret = 0;
1467 unsigned i = TTM_NUM_MEM_TYPES;
1468 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001469 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001470
1471 while (i--) {
1472 man = &bdev->man[i];
1473 if (man->has_type) {
1474 man->use_type = false;
1475 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1476 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001477 pr_err("DRM memory manager type %d is not clean\n",
1478 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001479 }
1480 man->has_type = false;
1481 }
1482 }
1483
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001484 mutex_lock(&glob->device_list_mutex);
1485 list_del(&bdev->device_list);
1486 mutex_unlock(&glob->device_list_mutex);
1487
Tejun Heof094cfc2010-12-24 15:59:06 +01001488 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001489
1490 while (ttm_bo_delayed_delete(bdev, true))
1491 ;
1492
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001493 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001494 if (list_empty(&bdev->ddestroy))
1495 TTM_DEBUG("Delayed destroy list was clean\n");
1496
1497 if (list_empty(&bdev->man[0].lru))
1498 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001499 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500
David Herrmann72525b32013-07-24 21:08:53 +02001501 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001502
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001503 return ret;
1504}
1505EXPORT_SYMBOL(ttm_bo_device_release);
1506
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001507int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001508 struct ttm_bo_global *glob,
1509 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001510 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001511 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001512 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001513{
1514 int ret = -EINVAL;
1515
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001516 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517
1518 memset(bdev->man, 0, sizeof(bdev->man));
1519
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001520 /*
1521 * Initialize the system memory buffer type.
1522 * Other types need to be driver / IOCTL initialized.
1523 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001524 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001526 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001527
David Herrmann72525b32013-07-24 21:08:53 +02001528 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1529 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001530 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001532 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001533 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001534 bdev->need_dma32 = need_dma32;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001535 mutex_lock(&glob->device_list_mutex);
1536 list_add_tail(&bdev->device_list, &glob->device_list);
1537 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538
1539 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001540out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001541 return ret;
1542}
1543EXPORT_SYMBOL(ttm_bo_device_init);
1544
1545/*
1546 * buffer object vm functions.
1547 */
1548
1549bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1550{
1551 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1552
1553 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1554 if (mem->mem_type == TTM_PL_SYSTEM)
1555 return false;
1556
1557 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1558 return false;
1559
1560 if (mem->placement & TTM_PL_FLAG_CACHED)
1561 return false;
1562 }
1563 return true;
1564}
1565
Thomas Hellstromeba67092010-11-11 09:41:57 +01001566void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001567{
1568 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001569
David Herrmann51335df2013-07-24 21:10:03 +02001570 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001571 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001572}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001573
1574void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1575{
1576 struct ttm_bo_device *bdev = bo->bdev;
1577 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1578
1579 ttm_mem_io_lock(man, false);
1580 ttm_bo_unmap_virtual_locked(bo);
1581 ttm_mem_io_unlock(man);
1582}
1583
1584
Dave Airliee024e112009-06-24 09:48:08 +10001585EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001586
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001587int ttm_bo_wait(struct ttm_buffer_object *bo,
Christian König8aa6d4f2016-04-06 11:12:04 +02001588 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001589{
Christian König98a6dd92016-11-07 16:16:15 -05001590 long timeout = 15 * HZ;
1591
1592 if (no_wait) {
1593 if (reservation_object_test_signaled_rcu(bo->resv, true))
1594 return 0;
1595 else
1596 return -EBUSY;
1597 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001598
Christian Königf849c6d2016-06-15 13:44:02 +02001599 timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
1600 interruptible, timeout);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001601 if (timeout < 0)
1602 return timeout;
1603
1604 if (timeout == 0)
1605 return -EBUSY;
1606
Christian Königf849c6d2016-06-15 13:44:02 +02001607 reservation_object_add_excl_fence(bo->resv, NULL);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001608 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001609}
1610EXPORT_SYMBOL(ttm_bo_wait);
1611
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001612int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1613{
1614 int ret = 0;
1615
1616 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001617 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001618 */
1619
Christian Königdfd5e502016-04-06 11:12:03 +02001620 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001621 if (unlikely(ret != 0))
1622 return ret;
Christian König8aa6d4f2016-04-06 11:12:04 +02001623 ret = ttm_bo_wait(bo, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001624 if (likely(ret == 0))
1625 atomic_inc(&bo->cpu_writers);
1626 ttm_bo_unreserve(bo);
1627 return ret;
1628}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001629EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001630
1631void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1632{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001633 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001634}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001635EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001636
1637/**
1638 * A buffer object shrink method that tries to swap out the first
1639 * buffer object on the bo_global::swap_lru list.
1640 */
1641
1642static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1643{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001644 struct ttm_bo_global *glob =
1645 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001646 struct ttm_buffer_object *bo;
1647 int ret = -EBUSY;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001648 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1649
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001650 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001651 list_for_each_entry(bo, &glob->swap_lru, swap) {
Christian Königdfd5e502016-04-06 11:12:03 +02001652 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001653 if (!ret)
1654 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001655 }
1656
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001657 if (ret) {
1658 spin_unlock(&glob->lru_lock);
1659 return ret;
1660 }
1661
1662 kref_get(&bo->list_kref);
1663
1664 if (!list_empty(&bo->ddestroy)) {
1665 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1666 kref_put(&bo->list_kref, ttm_bo_release_list);
1667 return ret;
1668 }
1669
Peter Zijlstrabdfafc42016-11-14 17:34:19 +01001670 ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001671 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001672
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001673 /**
Christian König61ede072016-06-06 10:17:57 +02001674 * Move to system cached
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001675 */
1676
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001677 if ((bo->mem.placement & swap_placement) != swap_placement) {
1678 struct ttm_mem_reg evict_mem;
1679
1680 evict_mem = bo->mem;
1681 evict_mem.mm_node = NULL;
1682 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1683 evict_mem.mem_type = TTM_PL_SYSTEM;
1684
1685 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001686 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001687 if (unlikely(ret != 0))
1688 goto out;
1689 }
1690
Christian König61ede072016-06-06 10:17:57 +02001691 /**
1692 * Make sure BO is idle.
1693 */
1694
1695 ret = ttm_bo_wait(bo, false, false);
1696 if (unlikely(ret != 0))
1697 goto out;
1698
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001699 ttm_bo_unmap_virtual(bo);
1700
1701 /**
1702 * Swap out. Buffer will be swapped in again as soon as
1703 * anyone tries to access a ttm page.
1704 */
1705
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001706 if (bo->bdev->driver->swap_notify)
1707 bo->bdev->driver->swap_notify(bo);
1708
Jan Engelhardt5df23972011-04-04 01:25:18 +02001709 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001710out:
1711
1712 /**
1713 *
1714 * Unreserve without putting on LRU to avoid swapping out an
1715 * already swapped buffer.
1716 */
1717
Thomas Hellstromc7523082014-02-20 11:36:25 +01001718 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001719 kref_put(&bo->list_kref, ttm_bo_release_list);
1720 return ret;
1721}
1722
1723void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1724{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001725 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001726 ;
1727}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001728EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001729
1730/**
1731 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1732 * unreserved
1733 *
1734 * @bo: Pointer to buffer
1735 */
1736int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1737{
1738 int ret;
1739
1740 /*
1741 * In the absense of a wait_unlocked API,
1742 * Use the bo::wu_mutex to avoid triggering livelocks due to
1743 * concurrent use of this function. Note that this use of
1744 * bo::wu_mutex can go away if we change locking order to
1745 * mmap_sem -> bo::reserve.
1746 */
1747 ret = mutex_lock_interruptible(&bo->wu_mutex);
1748 if (unlikely(ret != 0))
1749 return -ERESTARTSYS;
1750 if (!ww_mutex_is_locked(&bo->resv->lock))
1751 goto out_unlock;
Christian Königdfd5e502016-04-06 11:12:03 +02001752 ret = __ttm_bo_reserve(bo, true, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001753 if (unlikely(ret != 0))
1754 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001755 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001756
1757out_unlock:
1758 mutex_unlock(&bo->wu_mutex);
1759 return ret;
1760}