blob: a71cf98c655fa7083c932683261c4c0517b25a8a [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/jiffies.h>
37#include <linux/slab.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/file.h>
41#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +020043#include <linux/reservation.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020044
45#define TTM_ASSERT_LOCKED(param)
46#define TTM_DEBUG(fmt, arg...)
47#define TTM_BO_HASH_ORDER 13
48
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020049static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020050static void ttm_bo_global_kobj_release(struct kobject *kobj);
51
52static struct attribute ttm_bo_count = {
53 .name = "bo_count",
54 .mode = S_IRUGO
55};
56
Christian Königf1217ed2014-08-27 13:16:04 +020057static inline int ttm_mem_type_from_place(const struct ttm_place *place,
58 uint32_t *mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010059{
60 int i;
61
62 for (i = 0; i <= TTM_PL_PRIV5; i++)
Christian Königf1217ed2014-08-27 13:16:04 +020063 if (place->flags & (1 << i)) {
Jerome Glissefb53f862009-12-09 21:55:10 +010064 *mem_type = i;
65 return 0;
66 }
67 return -EINVAL;
68}
69
Jerome Glisse5012f502009-12-10 18:07:26 +010070static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010071{
Jerome Glisse5012f502009-12-10 18:07:26 +010072 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
73
Joe Perches25d04792012-03-16 21:43:50 -070074 pr_err(" has_type: %d\n", man->has_type);
75 pr_err(" use_type: %d\n", man->use_type);
76 pr_err(" flags: 0x%08X\n", man->flags);
Alex Deucher54c4cd62015-03-04 00:18:38 -050077 pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
Joe Perches25d04792012-03-16 21:43:50 -070078 pr_err(" size: %llu\n", man->size);
79 pr_err(" available_caching: 0x%08X\n", man->available_caching);
80 pr_err(" default_caching: 0x%08X\n", man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100081 if (mem_type != TTM_PL_SYSTEM)
82 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010083}
84
85static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
86 struct ttm_placement *placement)
87{
Jerome Glissefb53f862009-12-09 21:55:10 +010088 int i, ret, mem_type;
89
Joe Perches25d04792012-03-16 21:43:50 -070090 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
91 bo, bo->mem.num_pages, bo->mem.size >> 10,
92 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010093 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +020094 ret = ttm_mem_type_from_place(&placement->placement[i],
Jerome Glissefb53f862009-12-09 21:55:10 +010095 &mem_type);
96 if (ret)
97 return;
Joe Perches25d04792012-03-16 21:43:50 -070098 pr_err(" placement[%d]=0x%08X (%d)\n",
Christian Königf1217ed2014-08-27 13:16:04 +020099 i, placement->placement[i].flags, mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +0100100 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100101 }
102}
103
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200104static ssize_t ttm_bo_global_show(struct kobject *kobj,
105 struct attribute *attr,
106 char *buffer)
107{
108 struct ttm_bo_global *glob =
109 container_of(kobj, struct ttm_bo_global, kobj);
110
111 return snprintf(buffer, PAGE_SIZE, "%lu\n",
112 (unsigned long) atomic_read(&glob->bo_count));
113}
114
115static struct attribute *ttm_bo_global_attrs[] = {
116 &ttm_bo_count,
117 NULL
118};
119
Emese Revfy52cf25d2010-01-19 02:58:23 +0100120static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200121 .show = &ttm_bo_global_show
122};
123
124static struct kobj_type ttm_bo_glob_kobj_type = {
125 .release = &ttm_bo_global_kobj_release,
126 .sysfs_ops = &ttm_bo_global_ops,
127 .default_attrs = ttm_bo_global_attrs
128};
129
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200130
131static inline uint32_t ttm_bo_type_flags(unsigned type)
132{
133 return 1 << (type);
134}
135
136static void ttm_bo_release_list(struct kref *list_kref)
137{
138 struct ttm_buffer_object *bo =
139 container_of(list_kref, struct ttm_buffer_object, list_kref);
140 struct ttm_bo_device *bdev = bo->bdev;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500141 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200142
143 BUG_ON(atomic_read(&bo->list_kref.refcount));
144 BUG_ON(atomic_read(&bo->kref.refcount));
145 BUG_ON(atomic_read(&bo->cpu_writers));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
149
150 if (bo->ttm)
151 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200152 atomic_dec(&bo->glob->bo_count);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200153 if (bo->resv == &bo->ttm_resv)
154 reservation_object_fini(&bo->ttm_resv);
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800155 mutex_destroy(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 if (bo->destroy)
157 bo->destroy(bo);
158 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200159 kfree(bo);
160 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500161 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200162}
163
Dave Airlied6ea8882010-11-22 13:24:40 +1000164void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165{
166 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200167
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200168 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169
170 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
171
172 BUG_ON(!list_empty(&bo->lru));
173
Christian König98c28722016-04-06 11:12:07 +0200174 list_add(&bo->lru, bdev->driver->lru_tail(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200175 kref_get(&bo->list_kref);
176
Christian Königed704a42016-01-11 15:35:19 +0100177 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
Christian König98c28722016-04-06 11:12:07 +0200178 list_add(&bo->swap, bdev->driver->swap_lru_tail(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200179 kref_get(&bo->list_kref);
180 }
181 }
182}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200183EXPORT_SYMBOL(ttm_bo_add_to_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200184
Dave Airlied6ea8882010-11-22 13:24:40 +1000185int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200186{
Christian Königc3ea5762016-04-06 11:12:06 +0200187 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200188 int put_count = 0;
189
Christian Königc3ea5762016-04-06 11:12:06 +0200190 if (bdev->driver->lru_removal)
191 bdev->driver->lru_removal(bo);
192
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200193 if (!list_empty(&bo->swap)) {
194 list_del_init(&bo->swap);
195 ++put_count;
196 }
197 if (!list_empty(&bo->lru)) {
198 list_del_init(&bo->lru);
199 ++put_count;
200 }
201
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200202 return put_count;
203}
204
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200205static void ttm_bo_ref_bug(struct kref *list_kref)
206{
207 BUG();
208}
209
Dave Airlied6ea8882010-11-22 13:24:40 +1000210void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
211 bool never_free)
212{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100213 kref_sub(&bo->list_kref, count,
214 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000215}
216
Maarten Lankhorst34820322013-06-27 13:48:24 +0200217void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200218{
Maarten Lankhorst34820322013-06-27 13:48:24 +0200219 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200220
Maarten Lankhorst34820322013-06-27 13:48:24 +0200221 spin_lock(&bo->glob->lru_lock);
222 put_count = ttm_bo_del_from_lru(bo);
223 spin_unlock(&bo->glob->lru_lock);
224 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200225}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200226EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200227
Christian Königab749612016-01-11 15:35:20 +0100228void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
229{
230 struct ttm_bo_device *bdev = bo->bdev;
Flora Cui56fc3502016-04-20 10:23:47 +0800231 int put_count = 0;
Christian Königab749612016-01-11 15:35:20 +0100232
233 lockdep_assert_held(&bo->resv->lock.base);
234
Christian Königc3ea5762016-04-06 11:12:06 +0200235 if (bdev->driver->lru_removal)
236 bdev->driver->lru_removal(bo);
237
Flora Cui56fc3502016-04-20 10:23:47 +0800238 put_count = ttm_bo_del_from_lru(bo);
239 ttm_bo_list_ref_sub(bo, put_count, true);
240 ttm_bo_add_to_lru(bo);
Christian Königab749612016-01-11 15:35:20 +0100241}
242EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
243
Christian König98c28722016-04-06 11:12:07 +0200244struct list_head *ttm_bo_default_lru_tail(struct ttm_buffer_object *bo)
245{
246 return bo->bdev->man[bo->mem.mem_type].lru.prev;
247}
248EXPORT_SYMBOL(ttm_bo_default_lru_tail);
249
250struct list_head *ttm_bo_default_swap_lru_tail(struct ttm_buffer_object *bo)
251{
252 return bo->glob->swap_lru.prev;
253}
254EXPORT_SYMBOL(ttm_bo_default_swap_lru_tail);
255
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200256/*
257 * Call bo->mutex locked.
258 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200259static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
260{
261 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200262 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200263 int ret = 0;
264 uint32_t page_flags = 0;
265
266 TTM_ASSERT_LOCKED(&bo->mutex);
267 bo->ttm = NULL;
268
Dave Airliead49f502009-07-10 22:36:26 +1000269 if (bdev->need_dma32)
270 page_flags |= TTM_PAGE_FLAG_DMA32;
271
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200272 switch (bo->type) {
273 case ttm_bo_type_device:
274 if (zero_alloc)
275 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
276 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400277 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
278 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200279 if (unlikely(bo->ttm == NULL))
280 ret = -ENOMEM;
281 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100282 case ttm_bo_type_sg:
283 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
284 page_flags | TTM_PAGE_FLAG_SG,
285 glob->dummy_read_page);
286 if (unlikely(bo->ttm == NULL)) {
287 ret = -ENOMEM;
288 break;
289 }
290 bo->ttm->sg = bo->sg;
291 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200292 default:
Joe Perches25d04792012-03-16 21:43:50 -0700293 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200294 ret = -EINVAL;
295 break;
296 }
297
298 return ret;
299}
300
301static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
302 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000303 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000304 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200305{
306 struct ttm_bo_device *bdev = bo->bdev;
307 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
308 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
309 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
310 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
311 int ret = 0;
312
313 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100314 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
315 ret = ttm_mem_io_lock(old_man, true);
316 if (unlikely(ret != 0))
317 goto out_err;
318 ttm_bo_unmap_virtual_locked(bo);
319 ttm_mem_io_unlock(old_man);
320 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200321
322 /*
323 * Create and bind a ttm if required.
324 */
325
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000326 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
327 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000328 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
329 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000330 if (ret)
331 goto out_err;
332 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200333
334 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
335 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200336 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200337
338 if (mem->mem_type != TTM_PL_SYSTEM) {
339 ret = ttm_tt_bind(bo->ttm, mem);
340 if (ret)
341 goto out_err;
342 }
343
344 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000345 if (bdev->driver->move_notify)
346 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100347 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200348 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200349 goto moved;
350 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200351 }
352
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000353 if (bdev->driver->move_notify)
354 bdev->driver->move_notify(bo, mem);
355
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200356 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
357 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000358 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200359 else if (bdev->driver->move)
360 ret = bdev->driver->move(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000361 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200362 else
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000363 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200364
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000365 if (ret) {
366 if (bdev->driver->move_notify) {
367 struct ttm_mem_reg tmp_mem = *mem;
368 *mem = bo->mem;
369 bo->mem = tmp_mem;
370 bdev->driver->move_notify(bo, mem);
371 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000372 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000373 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200374
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000375 goto out_err;
376 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500377
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200378moved:
379 if (bo->evicted) {
Rob Clark9ef75062014-03-12 10:59:37 -0400380 if (bdev->driver->invalidate_caches) {
381 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
382 if (ret)
383 pr_err("Can not flush read caches\n");
384 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200385 bo->evicted = false;
386 }
387
388 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000389 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390 bdev->man[bo->mem.mem_type].gpu_offset;
391 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100392 } else
393 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200394
395 return 0;
396
397out_err:
398 new_man = &bdev->man[bo->mem.mem_type];
399 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
400 ttm_tt_unbind(bo->ttm);
401 ttm_tt_destroy(bo->ttm);
402 bo->ttm = NULL;
403 }
404
405 return ret;
406}
407
408/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200409 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200410 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200411 * This is the place to put in driver specific hooks to release
412 * driver private resources.
413 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200414 */
415
416static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
417{
Jerome Glissedc97b342011-11-18 11:47:03 -0500418 if (bo->bdev->driver->move_notify)
419 bo->bdev->driver->move_notify(bo, NULL);
420
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200421 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200422 ttm_tt_unbind(bo->ttm);
423 ttm_tt_destroy(bo->ttm);
424 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200425 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200426 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200427
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200428 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200429}
430
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200431static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
432{
433 struct reservation_object_list *fobj;
434 struct fence *fence;
435 int i;
436
437 fobj = reservation_object_get_list(bo->resv);
438 fence = reservation_object_get_excl(bo->resv);
439 if (fence && !fence->ops->signaled)
440 fence_enable_sw_signaling(fence);
441
442 for (i = 0; fobj && i < fobj->shared_count; ++i) {
443 fence = rcu_dereference_protected(fobj->shared[i],
444 reservation_object_held(bo->resv));
445
446 if (!fence->ops->signaled)
447 fence_enable_sw_signaling(fence);
448 }
449}
450
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200451static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200452{
453 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200454 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200455 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200456 int ret;
457
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100458 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200459 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100460
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700461 if (!ret) {
Christian König8aa6d4f2016-04-06 11:12:04 +0200462 if (!ttm_bo_wait(bo, false, true)) {
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100463 put_count = ttm_bo_del_from_lru(bo);
464
465 spin_unlock(&glob->lru_lock);
466 ttm_bo_cleanup_memtype_use(bo);
467
468 ttm_bo_list_ref_sub(bo, put_count, true);
469
470 return;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200471 } else
472 ttm_bo_flush_all_fences(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700473
474 /*
475 * Make NO_EVICT bos immediately available to
476 * shrinkers, now that they are queued for
477 * destruction.
478 */
479 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
480 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
481 ttm_bo_add_to_lru(bo);
482 }
483
Thomas Hellstromc7523082014-02-20 11:36:25 +0100484 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700485 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200486
487 kref_get(&bo->list_kref);
488 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
489 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200490
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200491 schedule_delayed_work(&bdev->wq,
492 ((HZ / 100) < 1) ? 1 : HZ / 100);
493}
494
495/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000496 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200497 * If bo idle, remove from delayed- and lru lists, and unref.
498 * If not idle, do nothing.
499 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000500 * Must be called with lru_lock and reservation held, this function
501 * will drop both before returning.
502 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200503 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200504 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
505 */
506
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000507static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
508 bool interruptible,
509 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200510{
511 struct ttm_bo_global *glob = bo->glob;
512 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000513 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200514
Christian König8aa6d4f2016-04-06 11:12:04 +0200515 ret = ttm_bo_wait(bo, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200516
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000517 if (ret && !no_wait_gpu) {
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200518 long lret;
519 ww_mutex_unlock(&bo->resv->lock);
520 spin_unlock(&glob->lru_lock);
521
522 lret = reservation_object_wait_timeout_rcu(bo->resv,
523 true,
524 interruptible,
525 30 * HZ);
526
527 if (lret < 0)
528 return lret;
529 else if (lret == 0)
530 return -EBUSY;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000531
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000532 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200533 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000534
535 /*
536 * We raced, and lost, someone else holds the reservation now,
537 * and is probably busy in ttm_bo_cleanup_memtype_use.
538 *
539 * Even if it's not the case, because we finished waiting any
540 * delayed destruction would succeed, so just return success
541 * here.
542 */
543 if (ret) {
544 spin_unlock(&glob->lru_lock);
545 return 0;
546 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100547
548 /*
549 * remove sync_obj with ttm_bo_wait, the wait should be
550 * finished, and no new wait object should have been added.
551 */
Christian König8aa6d4f2016-04-06 11:12:04 +0200552 ret = ttm_bo_wait(bo, false, true);
Maarten Lankhorst70401382014-01-21 13:07:01 +0100553 WARN_ON(ret);
554 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000555
556 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100557 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000558 spin_unlock(&glob->lru_lock);
559 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200560 }
561
562 put_count = ttm_bo_del_from_lru(bo);
563 list_del_init(&bo->ddestroy);
564 ++put_count;
565
566 spin_unlock(&glob->lru_lock);
567 ttm_bo_cleanup_memtype_use(bo);
568
Dave Airlied6ea8882010-11-22 13:24:40 +1000569 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200570
571 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200572}
573
574/**
575 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
576 * encountered buffers.
577 */
578
579static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
580{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200581 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100582 struct ttm_buffer_object *entry = NULL;
583 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200584
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200585 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100586 if (list_empty(&bdev->ddestroy))
587 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200588
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100589 entry = list_first_entry(&bdev->ddestroy,
590 struct ttm_buffer_object, ddestroy);
591 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200592
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100593 for (;;) {
594 struct ttm_buffer_object *nentry = NULL;
595
596 if (entry->ddestroy.next != &bdev->ddestroy) {
597 nentry = list_first_entry(&entry->ddestroy,
598 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200599 kref_get(&nentry->list_kref);
600 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200601
Christian Königdfd5e502016-04-06 11:12:03 +0200602 ret = __ttm_bo_reserve(entry, false, true, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100603 if (remove_all && ret) {
604 spin_unlock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200605 ret = __ttm_bo_reserve(entry, false, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100606 spin_lock(&glob->lru_lock);
607 }
608
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000609 if (!ret)
610 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
611 !remove_all);
612 else
613 spin_unlock(&glob->lru_lock);
614
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200615 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100616 entry = nentry;
617
618 if (ret || !entry)
619 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200620
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200621 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100622 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200623 break;
624 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200625
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100626out_unlock:
627 spin_unlock(&glob->lru_lock);
628out:
629 if (entry)
630 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200631 return ret;
632}
633
634static void ttm_bo_delayed_workqueue(struct work_struct *work)
635{
636 struct ttm_bo_device *bdev =
637 container_of(work, struct ttm_bo_device, wq.work);
638
639 if (ttm_bo_delayed_delete(bdev, false)) {
640 schedule_delayed_work(&bdev->wq,
641 ((HZ / 100) < 1) ? 1 : HZ / 100);
642 }
643}
644
645static void ttm_bo_release(struct kref *kref)
646{
647 struct ttm_buffer_object *bo =
648 container_of(kref, struct ttm_buffer_object, kref);
649 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100650 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200651
David Herrmann72525b32013-07-24 21:08:53 +0200652 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100653 ttm_mem_io_lock(man, false);
654 ttm_mem_io_free_vm(bo);
655 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200656 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200657 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200658}
659
660void ttm_bo_unref(struct ttm_buffer_object **p_bo)
661{
662 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200663
664 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200665 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200666}
667EXPORT_SYMBOL(ttm_bo_unref);
668
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400669int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
670{
671 return cancel_delayed_work_sync(&bdev->wq);
672}
673EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
674
675void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
676{
677 if (resched)
678 schedule_delayed_work(&bdev->wq,
679 ((HZ / 100) < 1) ? 1 : HZ / 100);
680}
681EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
682
Jerome Glisseca262a9992009-12-08 15:33:32 +0100683static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000684 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200685{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200686 struct ttm_bo_device *bdev = bo->bdev;
687 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100688 struct ttm_placement placement;
689 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200690
Christian König8aa6d4f2016-04-06 11:12:04 +0200691 ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200692
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200693 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100694 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700695 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200696 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200697 goto out;
698 }
699
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200700 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200701
702 evict_mem = bo->mem;
703 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100704 evict_mem.bus.io_reserved_vm = false;
705 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200706
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100707 placement.num_placement = 0;
708 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100709 bdev->driver->evict_flags(bo, &placement);
710 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000711 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200712 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100713 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700714 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
715 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100716 ttm_bo_mem_space_debug(bo, &placement);
717 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200718 goto out;
719 }
720
721 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000722 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200723 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100724 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700725 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000726 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200727 goto out;
728 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200729 bo->evicted = true;
730out:
731 return ret;
732}
733
Jerome Glisseca262a9992009-12-08 15:33:32 +0100734static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
735 uint32_t mem_type,
Michel Dänzere3001802014-10-09 15:02:59 +0900736 const struct ttm_place *place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000737 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000738 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100739{
740 struct ttm_bo_global *glob = bdev->glob;
741 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
742 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000743 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100744
745 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000746 list_for_each_entry(bo, &man->lru, lru) {
Christian Königdfd5e502016-04-06 11:12:03 +0200747 ret = __ttm_bo_reserve(bo, false, true, NULL);
Michel Dänzere3001802014-10-09 15:02:59 +0900748 if (!ret) {
749 if (place && (place->fpfn || place->lpfn)) {
750 /* Don't evict this BO if it's outside of the
751 * requested placement range
752 */
753 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
754 (place->lpfn && place->lpfn <= bo->mem.start)) {
755 __ttm_bo_unreserve(bo);
756 ret = -EBUSY;
757 continue;
758 }
759 }
760
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000761 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900762 }
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100763 }
764
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000765 if (ret) {
766 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000767 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200768 }
769
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000770 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100771
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000772 if (!list_empty(&bo->ddestroy)) {
773 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
774 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100775 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000776 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100777 }
778
779 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100780 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100781
782 BUG_ON(ret != 0);
783
Dave Airlied6ea8882010-11-22 13:24:40 +1000784 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100785
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000786 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100787 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100788
Jerome Glisseca262a9992009-12-08 15:33:32 +0100789 kref_put(&bo->list_kref, ttm_bo_release_list);
790 return ret;
791}
792
Ben Skeggs42311ff2010-08-04 12:07:08 +1000793void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
794{
Ben Skeggsd961db72010-08-05 10:48:18 +1000795 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000796
Ben Skeggsd961db72010-08-05 10:48:18 +1000797 if (mem->mm_node)
798 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000799}
800EXPORT_SYMBOL(ttm_bo_mem_put);
801
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200802/**
803 * Repeatedly evict memory from the LRU for @mem_type until we create enough
804 * space, or we've evicted everything and there isn't enough space.
805 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100806static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
807 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200808 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100809 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000810 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000811 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200812{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100813 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200814 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200815 int ret;
816
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200817 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200818 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200819 if (unlikely(ret != 0))
820 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000821 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100822 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900823 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000824 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100825 if (unlikely(ret != 0))
826 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200827 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000828 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200829 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200830 mem->mem_type = mem_type;
831 return 0;
832}
833
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200834static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
835 uint32_t cur_placement,
836 uint32_t proposed_placement)
837{
838 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
839 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
840
841 /**
842 * Keep current caching if possible.
843 */
844
845 if ((cur_placement & caching) != 0)
846 result |= (cur_placement & caching);
847 else if ((man->default_caching & caching) != 0)
848 result |= man->default_caching;
849 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
850 result |= TTM_PL_FLAG_CACHED;
851 else if ((TTM_PL_FLAG_WC & caching) != 0)
852 result |= TTM_PL_FLAG_WC;
853 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
854 result |= TTM_PL_FLAG_UNCACHED;
855
856 return result;
857}
858
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200859static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200860 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200861 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200862 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200863{
864 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
865
Christian Königf1217ed2014-08-27 13:16:04 +0200866 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867 return false;
868
Christian Königf1217ed2014-08-27 13:16:04 +0200869 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200870 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200871
Christian Königf1217ed2014-08-27 13:16:04 +0200872 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200873
874 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200875 return true;
876}
877
878/**
879 * Creates space for memory region @mem according to its type.
880 *
881 * This function first searches for free space in compatible memory types in
882 * the priority order defined by the driver. If free space isn't found, then
883 * ttm_bo_mem_force_space is attempted in priority order to evict and find
884 * space.
885 */
886int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100887 struct ttm_placement *placement,
888 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000889 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000890 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200891{
892 struct ttm_bo_device *bdev = bo->bdev;
893 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200894 uint32_t mem_type = TTM_PL_SYSTEM;
895 uint32_t cur_flags = 0;
896 bool type_found = false;
897 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100898 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100899 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200900
901 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000902 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200903 const struct ttm_place *place = &placement->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 Hellstrome30f3962015-09-14 01:24:41 -0700909 if (!man->has_type || !man->use_type)
910 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200911
Christian Königf1217ed2014-08-27 13:16:04 +0200912 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100913 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200914
915 if (!type_ok)
916 continue;
917
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700918 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200919 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
920 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100921 /*
922 * Use the access and other non-mapping-related flag bits from
923 * the memory placement flags to the current flags
924 */
Christian Königf1217ed2014-08-27 13:16:04 +0200925 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100926 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200927
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200928 if (mem_type == TTM_PL_SYSTEM)
929 break;
930
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700931 ret = (*man->func->get_node)(man, bo, place, mem);
932 if (unlikely(ret))
933 return ret;
934
Ben Skeggsd961db72010-08-05 10:48:18 +1000935 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200936 break;
937 }
938
Ben Skeggsd961db72010-08-05 10:48:18 +1000939 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200940 mem->mem_type = mem_type;
941 mem->placement = cur_flags;
942 return 0;
943 }
944
Dave Airlieb6637522009-12-14 14:51:35 +1000945 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200946 const struct ttm_place *place = &placement->busy_placement[i];
947
948 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100949 if (ret)
950 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700952 if (!man->has_type || !man->use_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200954 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200955 continue;
956
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700957 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200958 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
959 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100960 /*
961 * Use the access and other non-mapping-related flag bits from
962 * the memory placement flags to the current flags
963 */
Christian Königf1217ed2014-08-27 13:16:04 +0200964 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100965 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200966
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100967 if (mem_type == TTM_PL_SYSTEM) {
968 mem->mem_type = mem_type;
969 mem->placement = cur_flags;
970 mem->mm_node = NULL;
971 return 0;
972 }
973
Christian Königf1217ed2014-08-27 13:16:04 +0200974 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000975 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976 if (ret == 0 && mem->mm_node) {
977 mem->placement = cur_flags;
978 return 0;
979 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100980 if (ret == -ERESTARTSYS)
981 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200982 }
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700983
984 if (!type_found) {
985 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
986 return -EINVAL;
987 }
988
989 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200990}
991EXPORT_SYMBOL(ttm_bo_mem_space);
992
Rashika Kheria6e87fa42014-01-06 22:12:58 +0530993static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100994 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000995 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000996 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200998 int ret = 0;
999 struct ttm_mem_reg mem;
1000
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001001 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001002
1003 /*
Christian König5ee7b412016-04-06 11:12:02 +02001004 * Don't wait for the BO on initial allocation. This is important when
1005 * the BO has an imported reservation object.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006 */
Christian König5ee7b412016-04-06 11:12:02 +02001007 if (bo->mem.mem_type != TTM_PL_SYSTEM || bo->ttm != NULL) {
1008 /*
1009 * FIXME: It's possible to pipeline buffer moves.
1010 * Have the driver move function wait for idle when necessary,
1011 * instead of doing it here.
1012 */
Christian König8aa6d4f2016-04-06 11:12:04 +02001013 ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
Christian König5ee7b412016-04-06 11:12:02 +02001014 if (ret)
1015 return ret;
1016 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001017 mem.num_pages = bo->num_pages;
1018 mem.size = mem.num_pages << PAGE_SHIFT;
1019 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001020 mem.bus.io_reserved_vm = false;
1021 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001022 /*
1023 * Determine where to move the buffer.
1024 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001025 ret = ttm_bo_mem_space(bo, placement, &mem,
1026 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001027 if (ret)
1028 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001029 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1030 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001031out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001032 if (ret && mem.mm_node)
1033 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001034 return ret;
1035}
1036
Sinclair Yeh94477bf2016-06-29 12:58:49 -07001037bool ttm_bo_mem_compat(struct ttm_placement *placement,
1038 struct ttm_mem_reg *mem,
1039 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001040{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001041 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001042
Jerome Glisseca262a9992009-12-08 15:33:32 +01001043 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001044 const struct ttm_place *heap = &placement->placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001045 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001046 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001047 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001048 continue;
1049
1050 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001051 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1052 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1053 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001054 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001055
1056 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001057 const struct ttm_place *heap = &placement->busy_placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001058 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001059 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001060 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001061 continue;
1062
1063 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001064 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1065 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1066 return true;
1067 }
1068
1069 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001070}
Sinclair Yeh94477bf2016-06-29 12:58:49 -07001071EXPORT_SYMBOL(ttm_bo_mem_compat);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001072
Jerome Glisse09855ac2009-12-10 17:16:27 +01001073int ttm_bo_validate(struct ttm_buffer_object *bo,
1074 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001075 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001076 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077{
1078 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001079 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001080
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001081 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082 /*
1083 * Check whether we need to move buffer.
1084 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001085 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001086 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1087 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001088 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001090 } else {
1091 /*
1092 * Use the access and other non-mapping-related flag bits from
1093 * the compatible memory placement flags to the active flags
1094 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001095 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001096 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001097 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098 /*
1099 * We might need to add a TTM.
1100 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1102 ret = ttm_bo_add_ttm(bo, true);
1103 if (ret)
1104 return ret;
1105 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106 return 0;
1107}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001108EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001109
Jerome Glisse09855ac2009-12-10 17:16:27 +01001110int ttm_bo_init(struct ttm_bo_device *bdev,
1111 struct ttm_buffer_object *bo,
1112 unsigned long size,
1113 enum ttm_bo_type type,
1114 struct ttm_placement *placement,
1115 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001116 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001117 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001118 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001119 struct sg_table *sg,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001120 struct reservation_object *resv,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001121 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001122{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001123 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001124 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001125 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001126 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001127
1128 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1129 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001130 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001131 if (destroy)
1132 (*destroy)(bo);
1133 else
1134 kfree(bo);
1135 return -ENOMEM;
1136 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001137
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001138 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1139 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001140 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001141 if (destroy)
1142 (*destroy)(bo);
1143 else
1144 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001145 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146 return -EINVAL;
1147 }
1148 bo->destroy = destroy;
1149
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001150 kref_init(&bo->kref);
1151 kref_init(&bo->list_kref);
1152 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 INIT_LIST_HEAD(&bo->lru);
1154 INIT_LIST_HEAD(&bo->ddestroy);
1155 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001156 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001157 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001159 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001160 bo->type = type;
1161 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001162 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001163 bo->mem.mem_type = TTM_PL_SYSTEM;
1164 bo->mem.num_pages = bo->num_pages;
1165 bo->mem.mm_node = NULL;
1166 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001167 bo->mem.bus.io_reserved_vm = false;
1168 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001169 bo->priv_flags = 0;
1170 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001171 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001172 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001173 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001174 if (resv) {
1175 bo->resv = resv;
1176 lockdep_assert_held(&bo->resv->lock.base);
1177 } else {
1178 bo->resv = &bo->ttm_resv;
1179 reservation_object_init(&bo->ttm_resv);
1180 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001181 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001182 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001183
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001184 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185 * For ttm_bo_type_device buffers, allocate
1186 * address space from the device.
1187 */
Christian Königf1217ed2014-08-27 13:16:04 +02001188 if (bo->type == ttm_bo_type_device ||
1189 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001190 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1191 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001192
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001193 /* passed reservation objects should already be locked,
1194 * since otherwise lockdep will be angered in radeon.
1195 */
1196 if (!resv) {
1197 locked = ww_mutex_trylock(&bo->resv->lock);
1198 WARN_ON(!locked);
1199 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001200
1201 if (likely(!ret))
1202 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203
Christian König33d48cf2016-01-11 15:35:18 +01001204 if (!resv) {
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001205 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001206
Christian König33d48cf2016-01-11 15:35:18 +01001207 } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1208 spin_lock(&bo->glob->lru_lock);
1209 ttm_bo_add_to_lru(bo);
1210 spin_unlock(&bo->glob->lru_lock);
1211 }
1212
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001213 if (unlikely(ret))
1214 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215
1216 return ret;
1217}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001218EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001220size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1221 unsigned long bo_size,
1222 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001224 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1225 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001226
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001227 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001228 size += ttm_round_pot(npages * sizeof(void *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001229 size += ttm_round_pot(sizeof(struct ttm_tt));
1230 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001232EXPORT_SYMBOL(ttm_bo_acc_size);
1233
1234size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1235 unsigned long bo_size,
1236 unsigned struct_size)
1237{
1238 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1239 size_t size = 0;
1240
1241 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001242 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001243 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1244 return size;
1245}
1246EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001247
Jerome Glisse09855ac2009-12-10 17:16:27 +01001248int ttm_bo_create(struct ttm_bo_device *bdev,
1249 unsigned long size,
1250 enum ttm_bo_type type,
1251 struct ttm_placement *placement,
1252 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001253 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001254 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001255 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256{
1257 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001258 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001259 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001260
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001261 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001262 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001263 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001264
Thomas Hellstroma393c732012-06-12 13:28:42 +02001265 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001266 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001267 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001268 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269 if (likely(ret == 0))
1270 *p_bo = bo;
1271
1272 return ret;
1273}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001274EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001275
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001276static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001277 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001278{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001279 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001280 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001281 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001282
1283 /*
1284 * Can't use standard list traversal since we're unlocking.
1285 */
1286
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001287 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001288 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001289 spin_unlock(&glob->lru_lock);
Michel Dänzere3001802014-10-09 15:02:59 +09001290 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001291 if (ret) {
1292 if (allow_errors) {
1293 return ret;
1294 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001295 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001296 }
1297 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001298 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001300 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001301 return 0;
1302}
1303
1304int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1305{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001306 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001307 int ret = -EINVAL;
1308
1309 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001310 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001311 return ret;
1312 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001313 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001314
1315 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001316 pr_err("Trying to take down uninitialized memory manager type %u\n",
1317 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001318 return ret;
1319 }
1320
1321 man->use_type = false;
1322 man->has_type = false;
1323
1324 ret = 0;
1325 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001326 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001327
Ben Skeggsd961db72010-08-05 10:48:18 +10001328 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001329 }
1330
1331 return ret;
1332}
1333EXPORT_SYMBOL(ttm_bo_clean_mm);
1334
1335int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1336{
1337 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1338
1339 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001340 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001341 return -EINVAL;
1342 }
1343
1344 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001345 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001346 return 0;
1347 }
1348
Jerome Glisseca262a9992009-12-08 15:33:32 +01001349 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001350}
1351EXPORT_SYMBOL(ttm_bo_evict_mm);
1352
1353int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001354 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001355{
1356 int ret = -EINVAL;
1357 struct ttm_mem_type_manager *man;
1358
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001359 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001360 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001361 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001362 man->io_reserve_fastpath = true;
1363 man->use_io_reserve_lru = false;
1364 mutex_init(&man->io_reserve_mutex);
1365 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001366
1367 ret = bdev->driver->init_mem_type(bdev, type, man);
1368 if (ret)
1369 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001370 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001371
1372 ret = 0;
1373 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001374 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001375 if (ret)
1376 return ret;
1377 }
1378 man->has_type = true;
1379 man->use_type = true;
1380 man->size = p_size;
1381
1382 INIT_LIST_HEAD(&man->lru);
1383
1384 return 0;
1385}
1386EXPORT_SYMBOL(ttm_bo_init_mm);
1387
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001388static void ttm_bo_global_kobj_release(struct kobject *kobj)
1389{
1390 struct ttm_bo_global *glob =
1391 container_of(kobj, struct ttm_bo_global, kobj);
1392
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001393 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1394 __free_page(glob->dummy_read_page);
1395 kfree(glob);
1396}
1397
Dave Airlieba4420c2010-03-09 10:56:52 +10001398void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001399{
1400 struct ttm_bo_global *glob = ref->object;
1401
1402 kobject_del(&glob->kobj);
1403 kobject_put(&glob->kobj);
1404}
1405EXPORT_SYMBOL(ttm_bo_global_release);
1406
Dave Airlieba4420c2010-03-09 10:56:52 +10001407int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001408{
1409 struct ttm_bo_global_ref *bo_ref =
1410 container_of(ref, struct ttm_bo_global_ref, ref);
1411 struct ttm_bo_global *glob = ref->object;
1412 int ret;
1413
1414 mutex_init(&glob->device_list_mutex);
1415 spin_lock_init(&glob->lru_lock);
1416 glob->mem_glob = bo_ref->mem_glob;
1417 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1418
1419 if (unlikely(glob->dummy_read_page == NULL)) {
1420 ret = -ENOMEM;
1421 goto out_no_drp;
1422 }
1423
1424 INIT_LIST_HEAD(&glob->swap_lru);
1425 INIT_LIST_HEAD(&glob->device_list);
1426
1427 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1428 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1429 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001430 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001431 goto out_no_shrink;
1432 }
1433
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001434 atomic_set(&glob->bo_count, 0);
1435
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001436 ret = kobject_init_and_add(
1437 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001438 if (unlikely(ret != 0))
1439 kobject_put(&glob->kobj);
1440 return ret;
1441out_no_shrink:
1442 __free_page(glob->dummy_read_page);
1443out_no_drp:
1444 kfree(glob);
1445 return ret;
1446}
1447EXPORT_SYMBOL(ttm_bo_global_init);
1448
1449
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001450int ttm_bo_device_release(struct ttm_bo_device *bdev)
1451{
1452 int ret = 0;
1453 unsigned i = TTM_NUM_MEM_TYPES;
1454 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001455 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001456
1457 while (i--) {
1458 man = &bdev->man[i];
1459 if (man->has_type) {
1460 man->use_type = false;
1461 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1462 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001463 pr_err("DRM memory manager type %d is not clean\n",
1464 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001465 }
1466 man->has_type = false;
1467 }
1468 }
1469
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001470 mutex_lock(&glob->device_list_mutex);
1471 list_del(&bdev->device_list);
1472 mutex_unlock(&glob->device_list_mutex);
1473
Tejun Heof094cfc2010-12-24 15:59:06 +01001474 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001475
1476 while (ttm_bo_delayed_delete(bdev, true))
1477 ;
1478
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001479 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001480 if (list_empty(&bdev->ddestroy))
1481 TTM_DEBUG("Delayed destroy list was clean\n");
1482
1483 if (list_empty(&bdev->man[0].lru))
1484 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001485 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001486
David Herrmann72525b32013-07-24 21:08:53 +02001487 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001489 return ret;
1490}
1491EXPORT_SYMBOL(ttm_bo_device_release);
1492
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001493int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001494 struct ttm_bo_global *glob,
1495 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001496 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001497 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001498 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001499{
1500 int ret = -EINVAL;
1501
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001502 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001503
1504 memset(bdev->man, 0, sizeof(bdev->man));
1505
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001506 /*
1507 * Initialize the system memory buffer type.
1508 * Other types need to be driver / IOCTL initialized.
1509 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001510 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001511 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001512 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001513
David Herrmann72525b32013-07-24 21:08:53 +02001514 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1515 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001516 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001518 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001519 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001520 bdev->need_dma32 = need_dma32;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001521 mutex_lock(&glob->device_list_mutex);
1522 list_add_tail(&bdev->device_list, &glob->device_list);
1523 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001524
1525 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001526out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001527 return ret;
1528}
1529EXPORT_SYMBOL(ttm_bo_device_init);
1530
1531/*
1532 * buffer object vm functions.
1533 */
1534
1535bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1536{
1537 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1538
1539 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1540 if (mem->mem_type == TTM_PL_SYSTEM)
1541 return false;
1542
1543 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1544 return false;
1545
1546 if (mem->placement & TTM_PL_FLAG_CACHED)
1547 return false;
1548 }
1549 return true;
1550}
1551
Thomas Hellstromeba67092010-11-11 09:41:57 +01001552void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553{
1554 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001555
David Herrmann51335df2013-07-24 21:10:03 +02001556 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001557 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001558}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001559
1560void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1561{
1562 struct ttm_bo_device *bdev = bo->bdev;
1563 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1564
1565 ttm_mem_io_lock(man, false);
1566 ttm_bo_unmap_virtual_locked(bo);
1567 ttm_mem_io_unlock(man);
1568}
1569
1570
Dave Airliee024e112009-06-24 09:48:08 +10001571EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001572
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001573int ttm_bo_wait(struct ttm_buffer_object *bo,
Christian König8aa6d4f2016-04-06 11:12:04 +02001574 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575{
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001576 struct reservation_object_list *fobj;
1577 struct reservation_object *resv;
1578 struct fence *excl;
1579 long timeout = 15 * HZ;
1580 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001581
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001582 resv = bo->resv;
1583 fobj = reservation_object_get_list(resv);
1584 excl = reservation_object_get_excl(resv);
1585 if (excl) {
1586 if (!fence_is_signaled(excl)) {
1587 if (no_wait)
1588 return -EBUSY;
Maarten Lankhorst70401382014-01-21 13:07:01 +01001589
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001590 timeout = fence_wait_timeout(excl,
1591 interruptible, timeout);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001592 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001593 }
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001594
1595 for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
1596 struct fence *fence;
1597 fence = rcu_dereference_protected(fobj->shared[i],
1598 reservation_object_held(resv));
1599
1600 if (!fence_is_signaled(fence)) {
1601 if (no_wait)
1602 return -EBUSY;
1603
1604 timeout = fence_wait_timeout(fence,
1605 interruptible, timeout);
1606 }
1607 }
1608
1609 if (timeout < 0)
1610 return timeout;
1611
1612 if (timeout == 0)
1613 return -EBUSY;
1614
1615 reservation_object_add_excl_fence(resv, NULL);
1616 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1617 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001618}
1619EXPORT_SYMBOL(ttm_bo_wait);
1620
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001621int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1622{
1623 int ret = 0;
1624
1625 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001626 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001627 */
1628
Christian Königdfd5e502016-04-06 11:12:03 +02001629 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001630 if (unlikely(ret != 0))
1631 return ret;
Christian König8aa6d4f2016-04-06 11:12:04 +02001632 ret = ttm_bo_wait(bo, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001633 if (likely(ret == 0))
1634 atomic_inc(&bo->cpu_writers);
1635 ttm_bo_unreserve(bo);
1636 return ret;
1637}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001638EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001639
1640void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1641{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001642 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001643}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001644EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001645
1646/**
1647 * A buffer object shrink method that tries to swap out the first
1648 * buffer object on the bo_global::swap_lru list.
1649 */
1650
1651static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1652{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001653 struct ttm_bo_global *glob =
1654 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001655 struct ttm_buffer_object *bo;
1656 int ret = -EBUSY;
1657 int put_count;
1658 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1659
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001660 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001661 list_for_each_entry(bo, &glob->swap_lru, swap) {
Christian Königdfd5e502016-04-06 11:12:03 +02001662 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001663 if (!ret)
1664 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001665 }
1666
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001667 if (ret) {
1668 spin_unlock(&glob->lru_lock);
1669 return ret;
1670 }
1671
1672 kref_get(&bo->list_kref);
1673
1674 if (!list_empty(&bo->ddestroy)) {
1675 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1676 kref_put(&bo->list_kref, ttm_bo_release_list);
1677 return ret;
1678 }
1679
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001680 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001681 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001682
Dave Airlied6ea8882010-11-22 13:24:40 +10001683 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001684
1685 /**
1686 * Wait for GPU, then move to system cached.
1687 */
1688
Christian König8aa6d4f2016-04-06 11:12:04 +02001689 ret = ttm_bo_wait(bo, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001690
1691 if (unlikely(ret != 0))
1692 goto out;
1693
1694 if ((bo->mem.placement & swap_placement) != swap_placement) {
1695 struct ttm_mem_reg evict_mem;
1696
1697 evict_mem = bo->mem;
1698 evict_mem.mm_node = NULL;
1699 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1700 evict_mem.mem_type = TTM_PL_SYSTEM;
1701
1702 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001703 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001704 if (unlikely(ret != 0))
1705 goto out;
1706 }
1707
1708 ttm_bo_unmap_virtual(bo);
1709
1710 /**
1711 * Swap out. Buffer will be swapped in again as soon as
1712 * anyone tries to access a ttm page.
1713 */
1714
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001715 if (bo->bdev->driver->swap_notify)
1716 bo->bdev->driver->swap_notify(bo);
1717
Jan Engelhardt5df23972011-04-04 01:25:18 +02001718 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001719out:
1720
1721 /**
1722 *
1723 * Unreserve without putting on LRU to avoid swapping out an
1724 * already swapped buffer.
1725 */
1726
Thomas Hellstromc7523082014-02-20 11:36:25 +01001727 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001728 kref_put(&bo->list_kref, ttm_bo_release_list);
1729 return ret;
1730}
1731
1732void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1733{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001734 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001735 ;
1736}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001737EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001738
1739/**
1740 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1741 * unreserved
1742 *
1743 * @bo: Pointer to buffer
1744 */
1745int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1746{
1747 int ret;
1748
1749 /*
1750 * In the absense of a wait_unlocked API,
1751 * Use the bo::wu_mutex to avoid triggering livelocks due to
1752 * concurrent use of this function. Note that this use of
1753 * bo::wu_mutex can go away if we change locking order to
1754 * mmap_sem -> bo::reserve.
1755 */
1756 ret = mutex_lock_interruptible(&bo->wu_mutex);
1757 if (unlikely(ret != 0))
1758 return -ERESTARTSYS;
1759 if (!ww_mutex_is_locked(&bo->resv->lock))
1760 goto out_unlock;
Christian Königdfd5e502016-04-06 11:12:03 +02001761 ret = __ttm_bo_reserve(bo, true, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001762 if (unlikely(ret != 0))
1763 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001764 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001765
1766out_unlock:
1767 mutex_unlock(&bo->wu_mutex);
1768 return ret;
1769}