blob: 28cd5352f8d075a5c7906882f0957339546da053 [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
Christian König4279cb12016-06-06 10:17:51 +0200150 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200151 atomic_dec(&bo->glob->bo_count);
Christian König5bc73062016-06-15 13:44:01 +0200152 fence_put(bo->moving);
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
Christian König77dfc282016-06-06 10:17:54 +0200363 ret = ttm_bo_move_memcpy(bo, evict, interruptible,
364 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200365
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000366 if (ret) {
367 if (bdev->driver->move_notify) {
368 struct ttm_mem_reg tmp_mem = *mem;
369 *mem = bo->mem;
370 bo->mem = tmp_mem;
371 bdev->driver->move_notify(bo, mem);
372 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000373 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000374 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200375
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000376 goto out_err;
377 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500378
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200379moved:
380 if (bo->evicted) {
Rob Clark9ef75062014-03-12 10:59:37 -0400381 if (bdev->driver->invalidate_caches) {
382 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
383 if (ret)
384 pr_err("Can not flush read caches\n");
385 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200386 bo->evicted = false;
387 }
388
389 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000390 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200391 bdev->man[bo->mem.mem_type].gpu_offset;
392 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100393 } else
394 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200395
396 return 0;
397
398out_err:
399 new_man = &bdev->man[bo->mem.mem_type];
Christian König4279cb12016-06-06 10:17:51 +0200400 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200401 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
Christian König4279cb12016-06-06 10:17:51 +0200421 ttm_tt_destroy(bo->ttm);
422 bo->ttm = NULL;
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200423 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200424
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200425 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200426}
427
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200428static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
429{
430 struct reservation_object_list *fobj;
431 struct fence *fence;
432 int i;
433
434 fobj = reservation_object_get_list(bo->resv);
435 fence = reservation_object_get_excl(bo->resv);
436 if (fence && !fence->ops->signaled)
437 fence_enable_sw_signaling(fence);
438
439 for (i = 0; fobj && i < fobj->shared_count; ++i) {
440 fence = rcu_dereference_protected(fobj->shared[i],
441 reservation_object_held(bo->resv));
442
443 if (!fence->ops->signaled)
444 fence_enable_sw_signaling(fence);
445 }
446}
447
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200448static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200449{
450 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200451 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200452 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200453 int ret;
454
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100455 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200456 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100457
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700458 if (!ret) {
Christian König8aa6d4f2016-04-06 11:12:04 +0200459 if (!ttm_bo_wait(bo, false, true)) {
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100460 put_count = ttm_bo_del_from_lru(bo);
461
462 spin_unlock(&glob->lru_lock);
463 ttm_bo_cleanup_memtype_use(bo);
464
465 ttm_bo_list_ref_sub(bo, put_count, true);
466
467 return;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200468 } else
469 ttm_bo_flush_all_fences(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700470
471 /*
472 * Make NO_EVICT bos immediately available to
473 * shrinkers, now that they are queued for
474 * destruction.
475 */
476 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
477 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
478 ttm_bo_add_to_lru(bo);
479 }
480
Thomas Hellstromc7523082014-02-20 11:36:25 +0100481 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700482 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200483
484 kref_get(&bo->list_kref);
485 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
486 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200487
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200488 schedule_delayed_work(&bdev->wq,
489 ((HZ / 100) < 1) ? 1 : HZ / 100);
490}
491
492/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000493 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200494 * If bo idle, remove from delayed- and lru lists, and unref.
495 * If not idle, do nothing.
496 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000497 * Must be called with lru_lock and reservation held, this function
498 * will drop both before returning.
499 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200500 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200501 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
502 */
503
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000504static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
505 bool interruptible,
506 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200507{
508 struct ttm_bo_global *glob = bo->glob;
509 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000510 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200511
Christian König8aa6d4f2016-04-06 11:12:04 +0200512 ret = ttm_bo_wait(bo, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200513
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000514 if (ret && !no_wait_gpu) {
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200515 long lret;
516 ww_mutex_unlock(&bo->resv->lock);
517 spin_unlock(&glob->lru_lock);
518
519 lret = reservation_object_wait_timeout_rcu(bo->resv,
520 true,
521 interruptible,
522 30 * HZ);
523
524 if (lret < 0)
525 return lret;
526 else if (lret == 0)
527 return -EBUSY;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000528
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000529 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200530 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000531
532 /*
533 * We raced, and lost, someone else holds the reservation now,
534 * and is probably busy in ttm_bo_cleanup_memtype_use.
535 *
536 * Even if it's not the case, because we finished waiting any
537 * delayed destruction would succeed, so just return success
538 * here.
539 */
540 if (ret) {
541 spin_unlock(&glob->lru_lock);
542 return 0;
543 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100544
545 /*
546 * remove sync_obj with ttm_bo_wait, the wait should be
547 * finished, and no new wait object should have been added.
548 */
Christian König8aa6d4f2016-04-06 11:12:04 +0200549 ret = ttm_bo_wait(bo, false, true);
Maarten Lankhorst70401382014-01-21 13:07:01 +0100550 WARN_ON(ret);
551 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000552
553 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100554 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000555 spin_unlock(&glob->lru_lock);
556 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200557 }
558
559 put_count = ttm_bo_del_from_lru(bo);
560 list_del_init(&bo->ddestroy);
561 ++put_count;
562
563 spin_unlock(&glob->lru_lock);
564 ttm_bo_cleanup_memtype_use(bo);
565
Dave Airlied6ea8882010-11-22 13:24:40 +1000566 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200567
568 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200569}
570
571/**
572 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
573 * encountered buffers.
574 */
575
576static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
577{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200578 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100579 struct ttm_buffer_object *entry = NULL;
580 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200581
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200582 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100583 if (list_empty(&bdev->ddestroy))
584 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200585
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100586 entry = list_first_entry(&bdev->ddestroy,
587 struct ttm_buffer_object, ddestroy);
588 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200589
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100590 for (;;) {
591 struct ttm_buffer_object *nentry = NULL;
592
593 if (entry->ddestroy.next != &bdev->ddestroy) {
594 nentry = list_first_entry(&entry->ddestroy,
595 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200596 kref_get(&nentry->list_kref);
597 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200598
Christian Königdfd5e502016-04-06 11:12:03 +0200599 ret = __ttm_bo_reserve(entry, false, true, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100600 if (remove_all && ret) {
601 spin_unlock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200602 ret = __ttm_bo_reserve(entry, false, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100603 spin_lock(&glob->lru_lock);
604 }
605
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000606 if (!ret)
607 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
608 !remove_all);
609 else
610 spin_unlock(&glob->lru_lock);
611
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200612 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100613 entry = nentry;
614
615 if (ret || !entry)
616 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200617
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200618 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100619 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200620 break;
621 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200622
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100623out_unlock:
624 spin_unlock(&glob->lru_lock);
625out:
626 if (entry)
627 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200628 return ret;
629}
630
631static void ttm_bo_delayed_workqueue(struct work_struct *work)
632{
633 struct ttm_bo_device *bdev =
634 container_of(work, struct ttm_bo_device, wq.work);
635
636 if (ttm_bo_delayed_delete(bdev, false)) {
637 schedule_delayed_work(&bdev->wq,
638 ((HZ / 100) < 1) ? 1 : HZ / 100);
639 }
640}
641
642static void ttm_bo_release(struct kref *kref)
643{
644 struct ttm_buffer_object *bo =
645 container_of(kref, struct ttm_buffer_object, kref);
646 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100647 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200648
David Herrmann72525b32013-07-24 21:08:53 +0200649 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100650 ttm_mem_io_lock(man, false);
651 ttm_mem_io_free_vm(bo);
652 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200653 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200654 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200655}
656
657void ttm_bo_unref(struct ttm_buffer_object **p_bo)
658{
659 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200660
661 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200662 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200663}
664EXPORT_SYMBOL(ttm_bo_unref);
665
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400666int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
667{
668 return cancel_delayed_work_sync(&bdev->wq);
669}
670EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
671
672void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
673{
674 if (resched)
675 schedule_delayed_work(&bdev->wq,
676 ((HZ / 100) < 1) ? 1 : HZ / 100);
677}
678EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
679
Jerome Glisseca262a9992009-12-08 15:33:32 +0100680static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000681 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200682{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200683 struct ttm_bo_device *bdev = bo->bdev;
684 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100685 struct ttm_placement placement;
686 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200687
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200688 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200689
690 evict_mem = bo->mem;
691 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100692 evict_mem.bus.io_reserved_vm = false;
693 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200694
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100695 placement.num_placement = 0;
696 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100697 bdev->driver->evict_flags(bo, &placement);
698 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000699 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200700 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100701 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700702 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
703 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100704 ttm_bo_mem_space_debug(bo, &placement);
705 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200706 goto out;
707 }
708
709 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000710 no_wait_gpu);
Christian König17d33bc2016-06-06 10:17:56 +0200711 if (unlikely(ret)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100712 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700713 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000714 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200715 goto out;
716 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200717 bo->evicted = true;
718out:
719 return ret;
720}
721
Jerome Glisseca262a9992009-12-08 15:33:32 +0100722static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
723 uint32_t mem_type,
Michel Dänzere3001802014-10-09 15:02:59 +0900724 const struct ttm_place *place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000725 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000726 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100727{
728 struct ttm_bo_global *glob = bdev->glob;
729 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
730 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000731 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100732
733 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000734 list_for_each_entry(bo, &man->lru, lru) {
Christian Königdfd5e502016-04-06 11:12:03 +0200735 ret = __ttm_bo_reserve(bo, false, true, NULL);
Michel Dänzere3001802014-10-09 15:02:59 +0900736 if (!ret) {
737 if (place && (place->fpfn || place->lpfn)) {
738 /* Don't evict this BO if it's outside of the
739 * requested placement range
740 */
741 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
742 (place->lpfn && place->lpfn <= bo->mem.start)) {
743 __ttm_bo_unreserve(bo);
744 ret = -EBUSY;
745 continue;
746 }
747 }
748
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000749 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900750 }
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100751 }
752
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000753 if (ret) {
754 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000755 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200756 }
757
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000758 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100759
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000760 if (!list_empty(&bo->ddestroy)) {
761 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
762 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100763 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000764 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100765 }
766
767 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100768 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100769
770 BUG_ON(ret != 0);
771
Dave Airlied6ea8882010-11-22 13:24:40 +1000772 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100773
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000774 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100775 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100776
Jerome Glisseca262a9992009-12-08 15:33:32 +0100777 kref_put(&bo->list_kref, ttm_bo_release_list);
778 return ret;
779}
780
Ben Skeggs42311ff2010-08-04 12:07:08 +1000781void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
782{
Ben Skeggsd961db72010-08-05 10:48:18 +1000783 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000784
Ben Skeggsd961db72010-08-05 10:48:18 +1000785 if (mem->mm_node)
786 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000787}
788EXPORT_SYMBOL(ttm_bo_mem_put);
789
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200790/**
791 * Repeatedly evict memory from the LRU for @mem_type until we create enough
792 * space, or we've evicted everything and there isn't enough space.
793 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100794static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
795 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200796 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100797 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000798 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000799 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200800{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100801 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200802 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200803 int ret;
804
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200805 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200806 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200807 if (unlikely(ret != 0))
808 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000809 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100810 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900811 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000812 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100813 if (unlikely(ret != 0))
814 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200815 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000816 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200817 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200818 mem->mem_type = mem_type;
819 return 0;
820}
821
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200822static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
823 uint32_t cur_placement,
824 uint32_t proposed_placement)
825{
826 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
827 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
828
829 /**
830 * Keep current caching if possible.
831 */
832
833 if ((cur_placement & caching) != 0)
834 result |= (cur_placement & caching);
835 else if ((man->default_caching & caching) != 0)
836 result |= man->default_caching;
837 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
838 result |= TTM_PL_FLAG_CACHED;
839 else if ((TTM_PL_FLAG_WC & caching) != 0)
840 result |= TTM_PL_FLAG_WC;
841 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
842 result |= TTM_PL_FLAG_UNCACHED;
843
844 return result;
845}
846
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200847static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200848 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200849 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200850 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200851{
852 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
853
Christian Königf1217ed2014-08-27 13:16:04 +0200854 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200855 return false;
856
Christian Königf1217ed2014-08-27 13:16:04 +0200857 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200858 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200859
Christian Königf1217ed2014-08-27 13:16:04 +0200860 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200861
862 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200863 return true;
864}
865
866/**
867 * Creates space for memory region @mem according to its type.
868 *
869 * This function first searches for free space in compatible memory types in
870 * the priority order defined by the driver. If free space isn't found, then
871 * ttm_bo_mem_force_space is attempted in priority order to evict and find
872 * space.
873 */
874int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100875 struct ttm_placement *placement,
876 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000877 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000878 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200879{
880 struct ttm_bo_device *bdev = bo->bdev;
881 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882 uint32_t mem_type = TTM_PL_SYSTEM;
883 uint32_t cur_flags = 0;
884 bool type_found = false;
885 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100886 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100887 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200888
889 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000890 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200891 const struct ttm_place *place = &placement->placement[i];
892
893 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100894 if (ret)
895 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200896 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700897 if (!man->has_type || !man->use_type)
898 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200899
Christian Königf1217ed2014-08-27 13:16:04 +0200900 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100901 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200902
903 if (!type_ok)
904 continue;
905
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700906 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200907 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
908 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100909 /*
910 * Use the access and other non-mapping-related flag bits from
911 * the memory placement flags to the current flags
912 */
Christian Königf1217ed2014-08-27 13:16:04 +0200913 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100914 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200915
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200916 if (mem_type == TTM_PL_SYSTEM)
917 break;
918
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700919 ret = (*man->func->get_node)(man, bo, place, mem);
920 if (unlikely(ret))
921 return ret;
922
Ben Skeggsd961db72010-08-05 10:48:18 +1000923 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200924 break;
925 }
926
Ben Skeggsd961db72010-08-05 10:48:18 +1000927 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200928 mem->mem_type = mem_type;
929 mem->placement = cur_flags;
930 return 0;
931 }
932
Dave Airlieb6637522009-12-14 14:51:35 +1000933 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200934 const struct ttm_place *place = &placement->busy_placement[i];
935
936 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100937 if (ret)
938 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200939 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700940 if (!man->has_type || !man->use_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200941 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200942 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200943 continue;
944
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700945 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200946 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
947 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100948 /*
949 * Use the access and other non-mapping-related flag bits from
950 * the memory placement flags to the current flags
951 */
Christian Königf1217ed2014-08-27 13:16:04 +0200952 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100953 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200954
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100955 if (mem_type == TTM_PL_SYSTEM) {
956 mem->mem_type = mem_type;
957 mem->placement = cur_flags;
958 mem->mm_node = NULL;
959 return 0;
960 }
961
Christian Königf1217ed2014-08-27 13:16:04 +0200962 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000963 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964 if (ret == 0 && mem->mm_node) {
965 mem->placement = cur_flags;
966 return 0;
967 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100968 if (ret == -ERESTARTSYS)
969 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970 }
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700971
972 if (!type_found) {
973 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
974 return -EINVAL;
975 }
976
977 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200978}
979EXPORT_SYMBOL(ttm_bo_mem_space);
980
Rashika Kheria6e87fa42014-01-06 22:12:58 +0530981static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100982 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000983 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000984 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200985{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200986 int ret = 0;
987 struct ttm_mem_reg mem;
988
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200989 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200990
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200991 mem.num_pages = bo->num_pages;
992 mem.size = mem.num_pages << PAGE_SHIFT;
993 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100994 mem.bus.io_reserved_vm = false;
995 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200996 /*
997 * Determine where to move the buffer.
998 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000999 ret = ttm_bo_mem_space(bo, placement, &mem,
1000 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001001 if (ret)
1002 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001003 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1004 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001006 if (ret && mem.mm_node)
1007 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001008 return ret;
1009}
1010
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001011static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1012 struct ttm_mem_reg *mem,
1013 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001014{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001015 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001016
Jerome Glisseca262a9992009-12-08 15:33:32 +01001017 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001018 const struct ttm_place *heap = &placement->placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001019 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001020 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001021 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001022 continue;
1023
1024 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001025 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1026 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1027 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001028 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001029
1030 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001031 const struct ttm_place *heap = &placement->busy_placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001032 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001033 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001034 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001035 continue;
1036
1037 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001038 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1039 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1040 return true;
1041 }
1042
1043 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044}
1045
Jerome Glisse09855ac2009-12-10 17:16:27 +01001046int ttm_bo_validate(struct ttm_buffer_object *bo,
1047 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001048 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001049 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050{
1051 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001052 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001053
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001054 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001055 /*
1056 * Check whether we need to move buffer.
1057 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001058 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001059 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1060 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001061 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001062 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001063 } else {
1064 /*
1065 * Use the access and other non-mapping-related flag bits from
1066 * the compatible memory placement flags to the active flags
1067 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001068 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001069 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001070 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001071 /*
1072 * We might need to add a TTM.
1073 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1075 ret = ttm_bo_add_ttm(bo, true);
1076 if (ret)
1077 return ret;
1078 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001079 return 0;
1080}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001081EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082
Jerome Glisse09855ac2009-12-10 17:16:27 +01001083int ttm_bo_init(struct ttm_bo_device *bdev,
1084 struct ttm_buffer_object *bo,
1085 unsigned long size,
1086 enum ttm_bo_type type,
1087 struct ttm_placement *placement,
1088 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001089 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001090 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001091 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001092 struct sg_table *sg,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001093 struct reservation_object *resv,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001094 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001095{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001096 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001097 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001098 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001099 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001100
1101 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1102 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001103 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001104 if (destroy)
1105 (*destroy)(bo);
1106 else
1107 kfree(bo);
1108 return -ENOMEM;
1109 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001111 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1112 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001113 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001114 if (destroy)
1115 (*destroy)(bo);
1116 else
1117 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001118 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001119 return -EINVAL;
1120 }
1121 bo->destroy = destroy;
1122
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001123 kref_init(&bo->kref);
1124 kref_init(&bo->list_kref);
1125 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001126 INIT_LIST_HEAD(&bo->lru);
1127 INIT_LIST_HEAD(&bo->ddestroy);
1128 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001129 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001130 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001131 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001132 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001133 bo->type = type;
1134 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001135 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001136 bo->mem.mem_type = TTM_PL_SYSTEM;
1137 bo->mem.num_pages = bo->num_pages;
1138 bo->mem.mm_node = NULL;
1139 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001140 bo->mem.bus.io_reserved_vm = false;
1141 bo->mem.bus.io_reserved_count = 0;
Christian König5bc73062016-06-15 13:44:01 +02001142 bo->moving = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001144 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001145 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001146 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001147 if (resv) {
1148 bo->resv = resv;
1149 lockdep_assert_held(&bo->resv->lock.base);
1150 } else {
1151 bo->resv = &bo->ttm_resv;
1152 reservation_object_init(&bo->ttm_resv);
1153 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001154 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001155 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001156
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001157 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158 * For ttm_bo_type_device buffers, allocate
1159 * address space from the device.
1160 */
Christian Königf1217ed2014-08-27 13:16:04 +02001161 if (bo->type == ttm_bo_type_device ||
1162 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001163 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1164 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001165
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001166 /* passed reservation objects should already be locked,
1167 * since otherwise lockdep will be angered in radeon.
1168 */
1169 if (!resv) {
1170 locked = ww_mutex_trylock(&bo->resv->lock);
1171 WARN_ON(!locked);
1172 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001173
1174 if (likely(!ret))
1175 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001176
Christian König33d48cf2016-01-11 15:35:18 +01001177 if (!resv) {
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001178 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179
Christian König33d48cf2016-01-11 15:35:18 +01001180 } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1181 spin_lock(&bo->glob->lru_lock);
1182 ttm_bo_add_to_lru(bo);
1183 spin_unlock(&bo->glob->lru_lock);
1184 }
1185
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001186 if (unlikely(ret))
1187 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001188
1189 return ret;
1190}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001191EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001192
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001193size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1194 unsigned long bo_size,
1195 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001197 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1198 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001200 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001201 size += ttm_round_pot(npages * sizeof(void *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001202 size += ttm_round_pot(sizeof(struct ttm_tt));
1203 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001204}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001205EXPORT_SYMBOL(ttm_bo_acc_size);
1206
1207size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1208 unsigned long bo_size,
1209 unsigned struct_size)
1210{
1211 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1212 size_t size = 0;
1213
1214 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001215 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001216 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1217 return size;
1218}
1219EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001220
Jerome Glisse09855ac2009-12-10 17:16:27 +01001221int ttm_bo_create(struct ttm_bo_device *bdev,
1222 unsigned long size,
1223 enum ttm_bo_type type,
1224 struct ttm_placement *placement,
1225 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001226 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001227 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001228 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001229{
1230 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001231 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001232 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001233
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001234 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001235 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001236 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001237
Thomas Hellstroma393c732012-06-12 13:28:42 +02001238 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001239 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001240 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001241 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001242 if (likely(ret == 0))
1243 *p_bo = bo;
1244
1245 return ret;
1246}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001247EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001249static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001250 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001252 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001253 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001254 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001255
1256 /*
1257 * Can't use standard list traversal since we're unlocking.
1258 */
1259
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001260 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001261 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001262 spin_unlock(&glob->lru_lock);
Michel Dänzere3001802014-10-09 15:02:59 +09001263 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001264 if (ret) {
1265 if (allow_errors) {
1266 return ret;
1267 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001268 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001269 }
1270 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001271 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001273 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001274 return 0;
1275}
1276
1277int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1278{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001279 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001280 int ret = -EINVAL;
1281
1282 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001283 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001284 return ret;
1285 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001286 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001287
1288 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001289 pr_err("Trying to take down uninitialized memory manager type %u\n",
1290 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291 return ret;
1292 }
1293
1294 man->use_type = false;
1295 man->has_type = false;
1296
1297 ret = 0;
1298 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001299 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001300
Ben Skeggsd961db72010-08-05 10:48:18 +10001301 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302 }
1303
1304 return ret;
1305}
1306EXPORT_SYMBOL(ttm_bo_clean_mm);
1307
1308int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1309{
1310 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1311
1312 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001313 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001314 return -EINVAL;
1315 }
1316
1317 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001318 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001319 return 0;
1320 }
1321
Jerome Glisseca262a9992009-12-08 15:33:32 +01001322 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001323}
1324EXPORT_SYMBOL(ttm_bo_evict_mm);
1325
1326int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001327 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001328{
1329 int ret = -EINVAL;
1330 struct ttm_mem_type_manager *man;
1331
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001332 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001333 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001334 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001335 man->io_reserve_fastpath = true;
1336 man->use_io_reserve_lru = false;
1337 mutex_init(&man->io_reserve_mutex);
1338 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001339
1340 ret = bdev->driver->init_mem_type(bdev, type, man);
1341 if (ret)
1342 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001343 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001344
1345 ret = 0;
1346 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001347 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001348 if (ret)
1349 return ret;
1350 }
1351 man->has_type = true;
1352 man->use_type = true;
1353 man->size = p_size;
1354
1355 INIT_LIST_HEAD(&man->lru);
1356
1357 return 0;
1358}
1359EXPORT_SYMBOL(ttm_bo_init_mm);
1360
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001361static void ttm_bo_global_kobj_release(struct kobject *kobj)
1362{
1363 struct ttm_bo_global *glob =
1364 container_of(kobj, struct ttm_bo_global, kobj);
1365
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001366 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1367 __free_page(glob->dummy_read_page);
1368 kfree(glob);
1369}
1370
Dave Airlieba4420c2010-03-09 10:56:52 +10001371void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001372{
1373 struct ttm_bo_global *glob = ref->object;
1374
1375 kobject_del(&glob->kobj);
1376 kobject_put(&glob->kobj);
1377}
1378EXPORT_SYMBOL(ttm_bo_global_release);
1379
Dave Airlieba4420c2010-03-09 10:56:52 +10001380int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001381{
1382 struct ttm_bo_global_ref *bo_ref =
1383 container_of(ref, struct ttm_bo_global_ref, ref);
1384 struct ttm_bo_global *glob = ref->object;
1385 int ret;
1386
1387 mutex_init(&glob->device_list_mutex);
1388 spin_lock_init(&glob->lru_lock);
1389 glob->mem_glob = bo_ref->mem_glob;
1390 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1391
1392 if (unlikely(glob->dummy_read_page == NULL)) {
1393 ret = -ENOMEM;
1394 goto out_no_drp;
1395 }
1396
1397 INIT_LIST_HEAD(&glob->swap_lru);
1398 INIT_LIST_HEAD(&glob->device_list);
1399
1400 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1401 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1402 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001403 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001404 goto out_no_shrink;
1405 }
1406
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001407 atomic_set(&glob->bo_count, 0);
1408
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001409 ret = kobject_init_and_add(
1410 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001411 if (unlikely(ret != 0))
1412 kobject_put(&glob->kobj);
1413 return ret;
1414out_no_shrink:
1415 __free_page(glob->dummy_read_page);
1416out_no_drp:
1417 kfree(glob);
1418 return ret;
1419}
1420EXPORT_SYMBOL(ttm_bo_global_init);
1421
1422
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001423int ttm_bo_device_release(struct ttm_bo_device *bdev)
1424{
1425 int ret = 0;
1426 unsigned i = TTM_NUM_MEM_TYPES;
1427 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001428 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001429
1430 while (i--) {
1431 man = &bdev->man[i];
1432 if (man->has_type) {
1433 man->use_type = false;
1434 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1435 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001436 pr_err("DRM memory manager type %d is not clean\n",
1437 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001438 }
1439 man->has_type = false;
1440 }
1441 }
1442
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001443 mutex_lock(&glob->device_list_mutex);
1444 list_del(&bdev->device_list);
1445 mutex_unlock(&glob->device_list_mutex);
1446
Tejun Heof094cfc2010-12-24 15:59:06 +01001447 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001448
1449 while (ttm_bo_delayed_delete(bdev, true))
1450 ;
1451
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001452 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001453 if (list_empty(&bdev->ddestroy))
1454 TTM_DEBUG("Delayed destroy list was clean\n");
1455
1456 if (list_empty(&bdev->man[0].lru))
1457 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001458 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001459
David Herrmann72525b32013-07-24 21:08:53 +02001460 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001461
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001462 return ret;
1463}
1464EXPORT_SYMBOL(ttm_bo_device_release);
1465
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001466int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001467 struct ttm_bo_global *glob,
1468 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001469 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001470 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001471 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001472{
1473 int ret = -EINVAL;
1474
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001475 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001476
1477 memset(bdev->man, 0, sizeof(bdev->man));
1478
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001479 /*
1480 * Initialize the system memory buffer type.
1481 * Other types need to be driver / IOCTL initialized.
1482 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001483 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001484 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001485 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001486
David Herrmann72525b32013-07-24 21:08:53 +02001487 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1488 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001489 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001490 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001491 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001492 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001493 bdev->need_dma32 = need_dma32;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001494 mutex_lock(&glob->device_list_mutex);
1495 list_add_tail(&bdev->device_list, &glob->device_list);
1496 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497
1498 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001499out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500 return ret;
1501}
1502EXPORT_SYMBOL(ttm_bo_device_init);
1503
1504/*
1505 * buffer object vm functions.
1506 */
1507
1508bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1509{
1510 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1511
1512 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1513 if (mem->mem_type == TTM_PL_SYSTEM)
1514 return false;
1515
1516 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1517 return false;
1518
1519 if (mem->placement & TTM_PL_FLAG_CACHED)
1520 return false;
1521 }
1522 return true;
1523}
1524
Thomas Hellstromeba67092010-11-11 09:41:57 +01001525void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526{
1527 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001528
David Herrmann51335df2013-07-24 21:10:03 +02001529 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001530 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001532
1533void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1534{
1535 struct ttm_bo_device *bdev = bo->bdev;
1536 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1537
1538 ttm_mem_io_lock(man, false);
1539 ttm_bo_unmap_virtual_locked(bo);
1540 ttm_mem_io_unlock(man);
1541}
1542
1543
Dave Airliee024e112009-06-24 09:48:08 +10001544EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001545
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001546int ttm_bo_wait(struct ttm_buffer_object *bo,
Christian König8aa6d4f2016-04-06 11:12:04 +02001547 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001548{
Christian Königf849c6d2016-06-15 13:44:02 +02001549 long timeout = no_wait ? 0 : 15 * HZ;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001550
Christian Königf849c6d2016-06-15 13:44:02 +02001551 timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
1552 interruptible, timeout);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001553 if (timeout < 0)
1554 return timeout;
1555
1556 if (timeout == 0)
1557 return -EBUSY;
1558
Christian Königf849c6d2016-06-15 13:44:02 +02001559 reservation_object_add_excl_fence(bo->resv, NULL);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001560 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001561}
1562EXPORT_SYMBOL(ttm_bo_wait);
1563
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001564int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1565{
1566 int ret = 0;
1567
1568 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001569 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001570 */
1571
Christian Königdfd5e502016-04-06 11:12:03 +02001572 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001573 if (unlikely(ret != 0))
1574 return ret;
Christian König8aa6d4f2016-04-06 11:12:04 +02001575 ret = ttm_bo_wait(bo, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001576 if (likely(ret == 0))
1577 atomic_inc(&bo->cpu_writers);
1578 ttm_bo_unreserve(bo);
1579 return ret;
1580}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001581EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001582
1583void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1584{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001585 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001586}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001587EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001588
1589/**
1590 * A buffer object shrink method that tries to swap out the first
1591 * buffer object on the bo_global::swap_lru list.
1592 */
1593
1594static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1595{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001596 struct ttm_bo_global *glob =
1597 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001598 struct ttm_buffer_object *bo;
1599 int ret = -EBUSY;
1600 int put_count;
1601 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1602
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001603 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001604 list_for_each_entry(bo, &glob->swap_lru, swap) {
Christian Königdfd5e502016-04-06 11:12:03 +02001605 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001606 if (!ret)
1607 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001608 }
1609
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001610 if (ret) {
1611 spin_unlock(&glob->lru_lock);
1612 return ret;
1613 }
1614
1615 kref_get(&bo->list_kref);
1616
1617 if (!list_empty(&bo->ddestroy)) {
1618 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1619 kref_put(&bo->list_kref, ttm_bo_release_list);
1620 return ret;
1621 }
1622
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001623 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001624 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001625
Dave Airlied6ea8882010-11-22 13:24:40 +10001626 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001627
1628 /**
Christian König61ede072016-06-06 10:17:57 +02001629 * Move to system cached
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001630 */
1631
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001632 if ((bo->mem.placement & swap_placement) != swap_placement) {
1633 struct ttm_mem_reg evict_mem;
1634
1635 evict_mem = bo->mem;
1636 evict_mem.mm_node = NULL;
1637 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1638 evict_mem.mem_type = TTM_PL_SYSTEM;
1639
1640 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001641 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001642 if (unlikely(ret != 0))
1643 goto out;
1644 }
1645
Christian König61ede072016-06-06 10:17:57 +02001646 /**
1647 * Make sure BO is idle.
1648 */
1649
1650 ret = ttm_bo_wait(bo, false, false);
1651 if (unlikely(ret != 0))
1652 goto out;
1653
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001654 ttm_bo_unmap_virtual(bo);
1655
1656 /**
1657 * Swap out. Buffer will be swapped in again as soon as
1658 * anyone tries to access a ttm page.
1659 */
1660
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001661 if (bo->bdev->driver->swap_notify)
1662 bo->bdev->driver->swap_notify(bo);
1663
Jan Engelhardt5df23972011-04-04 01:25:18 +02001664 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001665out:
1666
1667 /**
1668 *
1669 * Unreserve without putting on LRU to avoid swapping out an
1670 * already swapped buffer.
1671 */
1672
Thomas Hellstromc7523082014-02-20 11:36:25 +01001673 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001674 kref_put(&bo->list_kref, ttm_bo_release_list);
1675 return ret;
1676}
1677
1678void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1679{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001680 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001681 ;
1682}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001683EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001684
1685/**
1686 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1687 * unreserved
1688 *
1689 * @bo: Pointer to buffer
1690 */
1691int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1692{
1693 int ret;
1694
1695 /*
1696 * In the absense of a wait_unlocked API,
1697 * Use the bo::wu_mutex to avoid triggering livelocks due to
1698 * concurrent use of this function. Note that this use of
1699 * bo::wu_mutex can go away if we change locking order to
1700 * mmap_sem -> bo::reserve.
1701 */
1702 ret = mutex_lock_interruptible(&bo->wu_mutex);
1703 if (unlikely(ret != 0))
1704 return -ERESTARTSYS;
1705 if (!ww_mutex_is_locked(&bo->resv->lock))
1706 goto out_unlock;
Christian Königdfd5e502016-04-06 11:12:03 +02001707 ret = __ttm_bo_reserve(bo, true, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001708 if (unlikely(ret != 0))
1709 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001710 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001711
1712out_unlock:
1713 mutex_unlock(&bo->wu_mutex);
1714 return ret;
1715}