blob: 5d931690e0e1e954d783d93ec0fc9f5d947723fa [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/**
Christian König3ddf4ad2016-06-15 13:44:03 +0200791 * Add the last move fence to the BO and reserve a new shared slot.
792 */
793static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
794 struct ttm_mem_type_manager *man,
795 struct ttm_mem_reg *mem)
796{
797 struct fence *fence;
798 int ret;
799
800 spin_lock(&man->move_lock);
801 fence = fence_get(man->move);
802 spin_unlock(&man->move_lock);
803
804 if (fence) {
805 reservation_object_add_shared_fence(bo->resv, fence);
806
807 ret = reservation_object_reserve_shared(bo->resv);
808 if (unlikely(ret))
809 return ret;
810
811 fence_put(bo->moving);
812 bo->moving = fence;
813 }
814
815 return 0;
816}
817
818/**
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200819 * Repeatedly evict memory from the LRU for @mem_type until we create enough
820 * space, or we've evicted everything and there isn't enough space.
821 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100822static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
823 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200824 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100825 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000826 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000827 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200828{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100829 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200830 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200831 int ret;
832
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200833 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200834 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200835 if (unlikely(ret != 0))
836 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000837 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100838 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900839 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000840 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100841 if (unlikely(ret != 0))
842 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200843 } while (1);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200844 mem->mem_type = mem_type;
Christian König3ddf4ad2016-06-15 13:44:03 +0200845 return ttm_bo_add_move_fence(bo, man, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200846}
847
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200848static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
849 uint32_t cur_placement,
850 uint32_t proposed_placement)
851{
852 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
853 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
854
855 /**
856 * Keep current caching if possible.
857 */
858
859 if ((cur_placement & caching) != 0)
860 result |= (cur_placement & caching);
861 else if ((man->default_caching & caching) != 0)
862 result |= man->default_caching;
863 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
864 result |= TTM_PL_FLAG_CACHED;
865 else if ((TTM_PL_FLAG_WC & caching) != 0)
866 result |= TTM_PL_FLAG_WC;
867 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
868 result |= TTM_PL_FLAG_UNCACHED;
869
870 return result;
871}
872
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200873static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200874 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200875 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200876 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877{
878 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
879
Christian Königf1217ed2014-08-27 13:16:04 +0200880 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200881 return false;
882
Christian Königf1217ed2014-08-27 13:16:04 +0200883 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200884 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200885
Christian Königf1217ed2014-08-27 13:16:04 +0200886 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200887
888 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200889 return true;
890}
891
892/**
893 * Creates space for memory region @mem according to its type.
894 *
895 * This function first searches for free space in compatible memory types in
896 * the priority order defined by the driver. If free space isn't found, then
897 * ttm_bo_mem_force_space is attempted in priority order to evict and find
898 * space.
899 */
900int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100901 struct ttm_placement *placement,
902 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000903 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000904 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200905{
906 struct ttm_bo_device *bdev = bo->bdev;
907 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200908 uint32_t mem_type = TTM_PL_SYSTEM;
909 uint32_t cur_flags = 0;
910 bool type_found = false;
911 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100912 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100913 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200914
Christian König3ddf4ad2016-06-15 13:44:03 +0200915 ret = reservation_object_reserve_shared(bo->resv);
916 if (unlikely(ret))
917 return ret;
918
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200919 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000920 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200921 const struct ttm_place *place = &placement->placement[i];
922
923 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100924 if (ret)
925 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700927 if (!man->has_type || !man->use_type)
928 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929
Christian Königf1217ed2014-08-27 13:16:04 +0200930 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100931 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200932
933 if (!type_ok)
934 continue;
935
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700936 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200937 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
938 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100939 /*
940 * Use the access and other non-mapping-related flag bits from
941 * the memory placement flags to the current flags
942 */
Christian Königf1217ed2014-08-27 13:16:04 +0200943 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100944 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200945
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200946 if (mem_type == TTM_PL_SYSTEM)
947 break;
948
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700949 ret = (*man->func->get_node)(man, bo, place, mem);
950 if (unlikely(ret))
951 return ret;
Christian König3ddf4ad2016-06-15 13:44:03 +0200952
953 if (mem->mm_node) {
954 ret = ttm_bo_add_move_fence(bo, man, mem);
955 if (unlikely(ret)) {
956 (*man->func->put_node)(man, mem);
957 return ret;
958 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200959 break;
Christian König3ddf4ad2016-06-15 13:44:03 +0200960 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200961 }
962
Ben Skeggsd961db72010-08-05 10:48:18 +1000963 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964 mem->mem_type = mem_type;
965 mem->placement = cur_flags;
966 return 0;
967 }
968
Dave Airlieb6637522009-12-14 14:51:35 +1000969 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200970 const struct ttm_place *place = &placement->busy_placement[i];
971
972 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100973 if (ret)
974 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200975 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700976 if (!man->has_type || !man->use_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200977 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200978 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200979 continue;
980
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700981 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200982 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
983 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100984 /*
985 * Use the access and other non-mapping-related flag bits from
986 * the memory placement flags to the current flags
987 */
Christian Königf1217ed2014-08-27 13:16:04 +0200988 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100989 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200990
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100991 if (mem_type == TTM_PL_SYSTEM) {
992 mem->mem_type = mem_type;
993 mem->placement = cur_flags;
994 mem->mm_node = NULL;
995 return 0;
996 }
997
Christian Königf1217ed2014-08-27 13:16:04 +0200998 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000999 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001000 if (ret == 0 && mem->mm_node) {
1001 mem->placement = cur_flags;
1002 return 0;
1003 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001004 if (ret == -ERESTARTSYS)
1005 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006 }
Thomas Hellstrome30f3962015-09-14 01:24:41 -07001007
1008 if (!type_found) {
1009 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
1010 return -EINVAL;
1011 }
1012
1013 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001014}
1015EXPORT_SYMBOL(ttm_bo_mem_space);
1016
Rashika Kheria6e87fa42014-01-06 22:12:58 +05301017static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001018 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001019 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001020 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001021{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001022 int ret = 0;
1023 struct ttm_mem_reg mem;
1024
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001025 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001026
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001027 mem.num_pages = bo->num_pages;
1028 mem.size = mem.num_pages << PAGE_SHIFT;
1029 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001030 mem.bus.io_reserved_vm = false;
1031 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001032 /*
1033 * Determine where to move the buffer.
1034 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001035 ret = ttm_bo_mem_space(bo, placement, &mem,
1036 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037 if (ret)
1038 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001039 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1040 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001041out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001042 if (ret && mem.mm_node)
1043 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044 return ret;
1045}
1046
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001047static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1048 struct ttm_mem_reg *mem,
1049 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001051 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001052
Jerome Glisseca262a9992009-12-08 15:33:32 +01001053 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001054 const struct ttm_place *heap = &placement->placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001055 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001056 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001057 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001058 continue;
1059
1060 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001061 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1062 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1063 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001064 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001065
1066 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001067 const struct ttm_place *heap = &placement->busy_placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001068 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001069 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001070 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001071 continue;
1072
1073 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001074 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1075 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1076 return true;
1077 }
1078
1079 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001080}
1081
Jerome Glisse09855ac2009-12-10 17:16:27 +01001082int ttm_bo_validate(struct ttm_buffer_object *bo,
1083 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001084 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001085 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086{
1087 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001088 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001089
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001090 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001091 /*
1092 * Check whether we need to move buffer.
1093 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001094 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001095 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1096 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001097 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001099 } else {
1100 /*
1101 * Use the access and other non-mapping-related flag bits from
1102 * the compatible memory placement flags to the active flags
1103 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001104 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001105 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001107 /*
1108 * We might need to add a TTM.
1109 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1111 ret = ttm_bo_add_ttm(bo, true);
1112 if (ret)
1113 return ret;
1114 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001115 return 0;
1116}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001117EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001118
Jerome Glisse09855ac2009-12-10 17:16:27 +01001119int ttm_bo_init(struct ttm_bo_device *bdev,
1120 struct ttm_buffer_object *bo,
1121 unsigned long size,
1122 enum ttm_bo_type type,
1123 struct ttm_placement *placement,
1124 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001125 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001126 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001127 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001128 struct sg_table *sg,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001129 struct reservation_object *resv,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001130 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001131{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001132 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001133 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001134 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001135 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001136
1137 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1138 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001139 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001140 if (destroy)
1141 (*destroy)(bo);
1142 else
1143 kfree(bo);
1144 return -ENOMEM;
1145 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001147 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1148 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001149 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001150 if (destroy)
1151 (*destroy)(bo);
1152 else
1153 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001154 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001155 return -EINVAL;
1156 }
1157 bo->destroy = destroy;
1158
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001159 kref_init(&bo->kref);
1160 kref_init(&bo->list_kref);
1161 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001162 INIT_LIST_HEAD(&bo->lru);
1163 INIT_LIST_HEAD(&bo->ddestroy);
1164 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001165 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001166 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001167 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001168 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001169 bo->type = type;
1170 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001171 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001172 bo->mem.mem_type = TTM_PL_SYSTEM;
1173 bo->mem.num_pages = bo->num_pages;
1174 bo->mem.mm_node = NULL;
1175 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001176 bo->mem.bus.io_reserved_vm = false;
1177 bo->mem.bus.io_reserved_count = 0;
Christian König5bc73062016-06-15 13:44:01 +02001178 bo->moving = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001180 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001181 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001182 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001183 if (resv) {
1184 bo->resv = resv;
1185 lockdep_assert_held(&bo->resv->lock.base);
1186 } else {
1187 bo->resv = &bo->ttm_resv;
1188 reservation_object_init(&bo->ttm_resv);
1189 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001190 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001191 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001192
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001193 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001194 * For ttm_bo_type_device buffers, allocate
1195 * address space from the device.
1196 */
Christian Königf1217ed2014-08-27 13:16:04 +02001197 if (bo->type == ttm_bo_type_device ||
1198 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001199 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1200 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001201
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001202 /* passed reservation objects should already be locked,
1203 * since otherwise lockdep will be angered in radeon.
1204 */
1205 if (!resv) {
1206 locked = ww_mutex_trylock(&bo->resv->lock);
1207 WARN_ON(!locked);
1208 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001209
1210 if (likely(!ret))
1211 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212
Christian König33d48cf2016-01-11 15:35:18 +01001213 if (!resv) {
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001214 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215
Christian König33d48cf2016-01-11 15:35:18 +01001216 } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1217 spin_lock(&bo->glob->lru_lock);
1218 ttm_bo_add_to_lru(bo);
1219 spin_unlock(&bo->glob->lru_lock);
1220 }
1221
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001222 if (unlikely(ret))
1223 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001224
1225 return ret;
1226}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001227EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001229size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1230 unsigned long bo_size,
1231 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001232{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001233 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1234 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001235
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001236 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001237 size += ttm_round_pot(npages * sizeof(void *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001238 size += ttm_round_pot(sizeof(struct ttm_tt));
1239 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001240}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001241EXPORT_SYMBOL(ttm_bo_acc_size);
1242
1243size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1244 unsigned long bo_size,
1245 unsigned struct_size)
1246{
1247 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1248 size_t size = 0;
1249
1250 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001251 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001252 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1253 return size;
1254}
1255EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256
Jerome Glisse09855ac2009-12-10 17:16:27 +01001257int ttm_bo_create(struct ttm_bo_device *bdev,
1258 unsigned long size,
1259 enum ttm_bo_type type,
1260 struct ttm_placement *placement,
1261 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001262 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001263 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001264 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001265{
1266 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001267 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001268 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001270 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001271 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001273
Thomas Hellstroma393c732012-06-12 13:28:42 +02001274 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001275 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001276 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001277 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001278 if (likely(ret == 0))
1279 *p_bo = bo;
1280
1281 return ret;
1282}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001283EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001284
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001285static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001286 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001287{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001288 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001289 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001290 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291
1292 /*
1293 * Can't use standard list traversal since we're unlocking.
1294 */
1295
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001296 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001297 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001298 spin_unlock(&glob->lru_lock);
Michel Dänzere3001802014-10-09 15:02:59 +09001299 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001300 if (ret) {
1301 if (allow_errors) {
1302 return ret;
1303 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001304 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001305 }
1306 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001307 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001308 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001309 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001310 return 0;
1311}
1312
1313int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1314{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001315 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001316 int ret = -EINVAL;
1317
1318 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001319 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001320 return ret;
1321 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001322 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001323
1324 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001325 pr_err("Trying to take down uninitialized memory manager type %u\n",
1326 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001327 return ret;
1328 }
Christian König3ddf4ad2016-06-15 13:44:03 +02001329 fence_put(man->move);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001330
1331 man->use_type = false;
1332 man->has_type = false;
1333
1334 ret = 0;
1335 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001336 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001337
Ben Skeggsd961db72010-08-05 10:48:18 +10001338 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001339 }
1340
1341 return ret;
1342}
1343EXPORT_SYMBOL(ttm_bo_clean_mm);
1344
1345int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1346{
1347 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1348
1349 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001350 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001351 return -EINVAL;
1352 }
1353
1354 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001355 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001356 return 0;
1357 }
1358
Jerome Glisseca262a9992009-12-08 15:33:32 +01001359 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001360}
1361EXPORT_SYMBOL(ttm_bo_evict_mm);
1362
1363int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001364 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001365{
1366 int ret = -EINVAL;
1367 struct ttm_mem_type_manager *man;
1368
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001369 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001370 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001371 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001372 man->io_reserve_fastpath = true;
1373 man->use_io_reserve_lru = false;
1374 mutex_init(&man->io_reserve_mutex);
Christian König3ddf4ad2016-06-15 13:44:03 +02001375 spin_lock_init(&man->move_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001376 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001377
1378 ret = bdev->driver->init_mem_type(bdev, type, man);
1379 if (ret)
1380 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001381 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001382
1383 ret = 0;
1384 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001385 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001386 if (ret)
1387 return ret;
1388 }
1389 man->has_type = true;
1390 man->use_type = true;
1391 man->size = p_size;
1392
1393 INIT_LIST_HEAD(&man->lru);
Christian König3ddf4ad2016-06-15 13:44:03 +02001394 man->move = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001395
1396 return 0;
1397}
1398EXPORT_SYMBOL(ttm_bo_init_mm);
1399
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001400static void ttm_bo_global_kobj_release(struct kobject *kobj)
1401{
1402 struct ttm_bo_global *glob =
1403 container_of(kobj, struct ttm_bo_global, kobj);
1404
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001405 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1406 __free_page(glob->dummy_read_page);
1407 kfree(glob);
1408}
1409
Dave Airlieba4420c2010-03-09 10:56:52 +10001410void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001411{
1412 struct ttm_bo_global *glob = ref->object;
1413
1414 kobject_del(&glob->kobj);
1415 kobject_put(&glob->kobj);
1416}
1417EXPORT_SYMBOL(ttm_bo_global_release);
1418
Dave Airlieba4420c2010-03-09 10:56:52 +10001419int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001420{
1421 struct ttm_bo_global_ref *bo_ref =
1422 container_of(ref, struct ttm_bo_global_ref, ref);
1423 struct ttm_bo_global *glob = ref->object;
1424 int ret;
1425
1426 mutex_init(&glob->device_list_mutex);
1427 spin_lock_init(&glob->lru_lock);
1428 glob->mem_glob = bo_ref->mem_glob;
1429 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1430
1431 if (unlikely(glob->dummy_read_page == NULL)) {
1432 ret = -ENOMEM;
1433 goto out_no_drp;
1434 }
1435
1436 INIT_LIST_HEAD(&glob->swap_lru);
1437 INIT_LIST_HEAD(&glob->device_list);
1438
1439 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1440 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1441 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001442 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001443 goto out_no_shrink;
1444 }
1445
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001446 atomic_set(&glob->bo_count, 0);
1447
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001448 ret = kobject_init_and_add(
1449 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001450 if (unlikely(ret != 0))
1451 kobject_put(&glob->kobj);
1452 return ret;
1453out_no_shrink:
1454 __free_page(glob->dummy_read_page);
1455out_no_drp:
1456 kfree(glob);
1457 return ret;
1458}
1459EXPORT_SYMBOL(ttm_bo_global_init);
1460
1461
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001462int ttm_bo_device_release(struct ttm_bo_device *bdev)
1463{
1464 int ret = 0;
1465 unsigned i = TTM_NUM_MEM_TYPES;
1466 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001467 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001468
1469 while (i--) {
1470 man = &bdev->man[i];
1471 if (man->has_type) {
1472 man->use_type = false;
1473 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1474 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001475 pr_err("DRM memory manager type %d is not clean\n",
1476 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001477 }
1478 man->has_type = false;
1479 }
1480 }
1481
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001482 mutex_lock(&glob->device_list_mutex);
1483 list_del(&bdev->device_list);
1484 mutex_unlock(&glob->device_list_mutex);
1485
Tejun Heof094cfc2010-12-24 15:59:06 +01001486 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001487
1488 while (ttm_bo_delayed_delete(bdev, true))
1489 ;
1490
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001491 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001492 if (list_empty(&bdev->ddestroy))
1493 TTM_DEBUG("Delayed destroy list was clean\n");
1494
1495 if (list_empty(&bdev->man[0].lru))
1496 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001497 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001498
David Herrmann72525b32013-07-24 21:08:53 +02001499 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001501 return ret;
1502}
1503EXPORT_SYMBOL(ttm_bo_device_release);
1504
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001505int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001506 struct ttm_bo_global *glob,
1507 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001508 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001509 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001510 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001511{
1512 int ret = -EINVAL;
1513
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001514 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001515
1516 memset(bdev->man, 0, sizeof(bdev->man));
1517
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001518 /*
1519 * Initialize the system memory buffer type.
1520 * Other types need to be driver / IOCTL initialized.
1521 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001522 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001523 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001524 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525
David Herrmann72525b32013-07-24 21:08:53 +02001526 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1527 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001528 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001530 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001531 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001532 bdev->need_dma32 = need_dma32;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001533 mutex_lock(&glob->device_list_mutex);
1534 list_add_tail(&bdev->device_list, &glob->device_list);
1535 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536
1537 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001538out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001539 return ret;
1540}
1541EXPORT_SYMBOL(ttm_bo_device_init);
1542
1543/*
1544 * buffer object vm functions.
1545 */
1546
1547bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1548{
1549 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1550
1551 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1552 if (mem->mem_type == TTM_PL_SYSTEM)
1553 return false;
1554
1555 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1556 return false;
1557
1558 if (mem->placement & TTM_PL_FLAG_CACHED)
1559 return false;
1560 }
1561 return true;
1562}
1563
Thomas Hellstromeba67092010-11-11 09:41:57 +01001564void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001565{
1566 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001567
David Herrmann51335df2013-07-24 21:10:03 +02001568 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001569 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001570}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001571
1572void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1573{
1574 struct ttm_bo_device *bdev = bo->bdev;
1575 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1576
1577 ttm_mem_io_lock(man, false);
1578 ttm_bo_unmap_virtual_locked(bo);
1579 ttm_mem_io_unlock(man);
1580}
1581
1582
Dave Airliee024e112009-06-24 09:48:08 +10001583EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001584
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001585int ttm_bo_wait(struct ttm_buffer_object *bo,
Christian König8aa6d4f2016-04-06 11:12:04 +02001586 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001587{
Christian Königf849c6d2016-06-15 13:44:02 +02001588 long timeout = no_wait ? 0 : 15 * HZ;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001589
Christian Königf849c6d2016-06-15 13:44:02 +02001590 timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
1591 interruptible, timeout);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001592 if (timeout < 0)
1593 return timeout;
1594
1595 if (timeout == 0)
1596 return -EBUSY;
1597
Christian Königf849c6d2016-06-15 13:44:02 +02001598 reservation_object_add_excl_fence(bo->resv, NULL);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001599 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001600}
1601EXPORT_SYMBOL(ttm_bo_wait);
1602
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001603int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1604{
1605 int ret = 0;
1606
1607 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001608 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001609 */
1610
Christian Königdfd5e502016-04-06 11:12:03 +02001611 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001612 if (unlikely(ret != 0))
1613 return ret;
Christian König8aa6d4f2016-04-06 11:12:04 +02001614 ret = ttm_bo_wait(bo, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001615 if (likely(ret == 0))
1616 atomic_inc(&bo->cpu_writers);
1617 ttm_bo_unreserve(bo);
1618 return ret;
1619}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001620EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001621
1622void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1623{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001624 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001625}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001626EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001627
1628/**
1629 * A buffer object shrink method that tries to swap out the first
1630 * buffer object on the bo_global::swap_lru list.
1631 */
1632
1633static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1634{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001635 struct ttm_bo_global *glob =
1636 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001637 struct ttm_buffer_object *bo;
1638 int ret = -EBUSY;
1639 int put_count;
1640 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1641
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001642 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001643 list_for_each_entry(bo, &glob->swap_lru, swap) {
Christian Königdfd5e502016-04-06 11:12:03 +02001644 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001645 if (!ret)
1646 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001647 }
1648
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001649 if (ret) {
1650 spin_unlock(&glob->lru_lock);
1651 return ret;
1652 }
1653
1654 kref_get(&bo->list_kref);
1655
1656 if (!list_empty(&bo->ddestroy)) {
1657 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1658 kref_put(&bo->list_kref, ttm_bo_release_list);
1659 return ret;
1660 }
1661
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001662 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001663 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001664
Dave Airlied6ea8882010-11-22 13:24:40 +10001665 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001666
1667 /**
Christian König61ede072016-06-06 10:17:57 +02001668 * Move to system cached
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001669 */
1670
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001671 if ((bo->mem.placement & swap_placement) != swap_placement) {
1672 struct ttm_mem_reg evict_mem;
1673
1674 evict_mem = bo->mem;
1675 evict_mem.mm_node = NULL;
1676 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1677 evict_mem.mem_type = TTM_PL_SYSTEM;
1678
1679 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001680 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001681 if (unlikely(ret != 0))
1682 goto out;
1683 }
1684
Christian König61ede072016-06-06 10:17:57 +02001685 /**
1686 * Make sure BO is idle.
1687 */
1688
1689 ret = ttm_bo_wait(bo, false, false);
1690 if (unlikely(ret != 0))
1691 goto out;
1692
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001693 ttm_bo_unmap_virtual(bo);
1694
1695 /**
1696 * Swap out. Buffer will be swapped in again as soon as
1697 * anyone tries to access a ttm page.
1698 */
1699
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001700 if (bo->bdev->driver->swap_notify)
1701 bo->bdev->driver->swap_notify(bo);
1702
Jan Engelhardt5df23972011-04-04 01:25:18 +02001703 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001704out:
1705
1706 /**
1707 *
1708 * Unreserve without putting on LRU to avoid swapping out an
1709 * already swapped buffer.
1710 */
1711
Thomas Hellstromc7523082014-02-20 11:36:25 +01001712 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001713 kref_put(&bo->list_kref, ttm_bo_release_list);
1714 return ret;
1715}
1716
1717void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1718{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001719 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001720 ;
1721}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001722EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001723
1724/**
1725 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1726 * unreserved
1727 *
1728 * @bo: Pointer to buffer
1729 */
1730int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1731{
1732 int ret;
1733
1734 /*
1735 * In the absense of a wait_unlocked API,
1736 * Use the bo::wu_mutex to avoid triggering livelocks due to
1737 * concurrent use of this function. Note that this use of
1738 * bo::wu_mutex can go away if we change locking order to
1739 * mmap_sem -> bo::reserve.
1740 */
1741 ret = mutex_lock_interruptible(&bo->wu_mutex);
1742 if (unlikely(ret != 0))
1743 return -ERESTARTSYS;
1744 if (!ww_mutex_is_locked(&bo->resv->lock))
1745 goto out_unlock;
Christian Königdfd5e502016-04-06 11:12:03 +02001746 ret = __ttm_bo_reserve(bo, true, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001747 if (unlikely(ret != 0))
1748 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001749 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001750
1751out_unlock:
1752 mutex_unlock(&bo->wu_mutex);
1753 return ret;
1754}