blob: 8c5c64d52af284310ff1e70b94d2bb3356c13955 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/jiffies.h>
37#include <linux/slab.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/file.h>
41#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +020043#include <linux/reservation.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020044
45#define TTM_ASSERT_LOCKED(param)
46#define TTM_DEBUG(fmt, arg...)
47#define TTM_BO_HASH_ORDER 13
48
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020049static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020050static void ttm_bo_global_kobj_release(struct kobject *kobj);
51
52static struct attribute ttm_bo_count = {
53 .name = "bo_count",
54 .mode = S_IRUGO
55};
56
Christian Königf1217ed2014-08-27 13:16:04 +020057static inline int ttm_mem_type_from_place(const struct ttm_place *place,
58 uint32_t *mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010059{
Christian König5d98d0bc2016-09-12 13:16:16 +020060 int pos;
Jerome Glissefb53f862009-12-09 21:55:10 +010061
Christian König5d98d0bc2016-09-12 13:16:16 +020062 pos = ffs(place->flags & TTM_PL_MASK_MEM);
63 if (unlikely(!pos))
64 return -EINVAL;
65
66 *mem_type = pos - 1;
67 return 0;
Jerome Glissefb53f862009-12-09 21:55:10 +010068}
69
Jerome Glisse5012f502009-12-10 18:07:26 +010070static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010071{
Jerome Glisse5012f502009-12-10 18:07:26 +010072 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
73
Joe Perches25d04792012-03-16 21:43:50 -070074 pr_err(" has_type: %d\n", man->has_type);
75 pr_err(" use_type: %d\n", man->use_type);
76 pr_err(" flags: 0x%08X\n", man->flags);
Alex Deucher54c4cd62015-03-04 00:18:38 -050077 pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
Joe Perches25d04792012-03-16 21:43:50 -070078 pr_err(" size: %llu\n", man->size);
79 pr_err(" available_caching: 0x%08X\n", man->available_caching);
80 pr_err(" default_caching: 0x%08X\n", man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100081 if (mem_type != TTM_PL_SYSTEM)
82 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010083}
84
85static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
86 struct ttm_placement *placement)
87{
Jerome Glissefb53f862009-12-09 21:55:10 +010088 int i, ret, mem_type;
89
Joe Perches25d04792012-03-16 21:43:50 -070090 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
91 bo, bo->mem.num_pages, bo->mem.size >> 10,
92 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010093 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +020094 ret = ttm_mem_type_from_place(&placement->placement[i],
Jerome Glissefb53f862009-12-09 21:55:10 +010095 &mem_type);
96 if (ret)
97 return;
Joe Perches25d04792012-03-16 21:43:50 -070098 pr_err(" placement[%d]=0x%08X (%d)\n",
Christian Königf1217ed2014-08-27 13:16:04 +020099 i, placement->placement[i].flags, mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +0100100 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100101 }
102}
103
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200104static ssize_t ttm_bo_global_show(struct kobject *kobj,
105 struct attribute *attr,
106 char *buffer)
107{
108 struct ttm_bo_global *glob =
109 container_of(kobj, struct ttm_bo_global, kobj);
110
111 return snprintf(buffer, PAGE_SIZE, "%lu\n",
112 (unsigned long) atomic_read(&glob->bo_count));
113}
114
115static struct attribute *ttm_bo_global_attrs[] = {
116 &ttm_bo_count,
117 NULL
118};
119
Emese Revfy52cf25d2010-01-19 02:58:23 +0100120static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200121 .show = &ttm_bo_global_show
122};
123
124static struct kobj_type ttm_bo_glob_kobj_type = {
125 .release = &ttm_bo_global_kobj_release,
126 .sysfs_ops = &ttm_bo_global_ops,
127 .default_attrs = ttm_bo_global_attrs
128};
129
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200130
131static inline uint32_t ttm_bo_type_flags(unsigned type)
132{
133 return 1 << (type);
134}
135
136static void ttm_bo_release_list(struct kref *list_kref)
137{
138 struct ttm_buffer_object *bo =
139 container_of(list_kref, struct ttm_buffer_object, list_kref);
140 struct ttm_bo_device *bdev = bo->bdev;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500141 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200142
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));
Christian König4279cb12016-06-06 10:17:51 +0200149 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200150 atomic_dec(&bo->glob->bo_count);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100151 dma_fence_put(bo->moving);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200152 if (bo->resv == &bo->ttm_resv)
153 reservation_object_fini(&bo->ttm_resv);
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800154 mutex_destroy(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200155 if (bo->destroy)
156 bo->destroy(bo);
157 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200158 kfree(bo);
159 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500160 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200161}
162
Dave Airlied6ea8882010-11-22 13:24:40 +1000163void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164{
165 struct ttm_bo_device *bdev = bo->bdev;
Christian König260498f2017-01-12 11:50:13 +0100166 struct ttm_mem_type_manager *man;
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önig260498f2017-01-12 11:50:13 +0100174 man = &bdev->man[bo->mem.mem_type];
175 list_add_tail(&bo->lru, &man->lru[bo->priority]);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200176 kref_get(&bo->list_kref);
177
Christian Königed704a42016-01-11 15:35:19 +0100178 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
Christian König260498f2017-01-12 11:50:13 +0100179 list_add_tail(&bo->swap,
180 &bo->glob->swap_lru[bo->priority]);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200181 kref_get(&bo->list_kref);
182 }
183 }
184}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200185EXPORT_SYMBOL(ttm_bo_add_to_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200186
Dave Airlied6ea8882010-11-22 13:24:40 +1000187int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200188{
Christian Königc3ea5762016-04-06 11:12:06 +0200189 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200190 int put_count = 0;
191
Christian Königc3ea5762016-04-06 11:12:06 +0200192 if (bdev->driver->lru_removal)
193 bdev->driver->lru_removal(bo);
194
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200195 if (!list_empty(&bo->swap)) {
196 list_del_init(&bo->swap);
197 ++put_count;
198 }
199 if (!list_empty(&bo->lru)) {
200 list_del_init(&bo->lru);
201 ++put_count;
202 }
203
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200204 return put_count;
205}
206
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200207static void ttm_bo_ref_bug(struct kref *list_kref)
208{
209 BUG();
210}
211
Dave Airlied6ea8882010-11-22 13:24:40 +1000212void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
213 bool never_free)
214{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100215 kref_sub(&bo->list_kref, count,
216 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000217}
218
Maarten Lankhorst34820322013-06-27 13:48:24 +0200219void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200220{
Maarten Lankhorst34820322013-06-27 13:48:24 +0200221 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200222
Maarten Lankhorst34820322013-06-27 13:48:24 +0200223 spin_lock(&bo->glob->lru_lock);
224 put_count = ttm_bo_del_from_lru(bo);
225 spin_unlock(&bo->glob->lru_lock);
226 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200227}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200228EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200229
Christian Königab749612016-01-11 15:35:20 +0100230void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
231{
232 struct ttm_bo_device *bdev = bo->bdev;
Flora Cui56fc3502016-04-20 10:23:47 +0800233 int put_count = 0;
Christian Königab749612016-01-11 15:35:20 +0100234
235 lockdep_assert_held(&bo->resv->lock.base);
236
Christian Königc3ea5762016-04-06 11:12:06 +0200237 if (bdev->driver->lru_removal)
238 bdev->driver->lru_removal(bo);
239
Flora Cui56fc3502016-04-20 10:23:47 +0800240 put_count = ttm_bo_del_from_lru(bo);
241 ttm_bo_list_ref_sub(bo, put_count, true);
242 ttm_bo_add_to_lru(bo);
Christian Königab749612016-01-11 15:35:20 +0100243}
244EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
245
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200246/*
247 * Call bo->mutex locked.
248 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200249static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
250{
251 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200252 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200253 int ret = 0;
254 uint32_t page_flags = 0;
255
256 TTM_ASSERT_LOCKED(&bo->mutex);
257 bo->ttm = NULL;
258
Dave Airliead49f502009-07-10 22:36:26 +1000259 if (bdev->need_dma32)
260 page_flags |= TTM_PAGE_FLAG_DMA32;
261
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200262 switch (bo->type) {
263 case ttm_bo_type_device:
264 if (zero_alloc)
265 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
266 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400267 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
268 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200269 if (unlikely(bo->ttm == NULL))
270 ret = -ENOMEM;
271 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100272 case ttm_bo_type_sg:
273 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
274 page_flags | TTM_PAGE_FLAG_SG,
275 glob->dummy_read_page);
276 if (unlikely(bo->ttm == NULL)) {
277 ret = -ENOMEM;
278 break;
279 }
280 bo->ttm->sg = bo->sg;
281 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200282 default:
Joe Perches25d04792012-03-16 21:43:50 -0700283 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200284 ret = -EINVAL;
285 break;
286 }
287
288 return ret;
289}
290
291static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
292 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000293 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000294 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200295{
296 struct ttm_bo_device *bdev = bo->bdev;
297 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
298 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
299 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
300 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
301 int ret = 0;
302
303 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100304 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
305 ret = ttm_mem_io_lock(old_man, true);
306 if (unlikely(ret != 0))
307 goto out_err;
308 ttm_bo_unmap_virtual_locked(bo);
309 ttm_mem_io_unlock(old_man);
310 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200311
312 /*
313 * Create and bind a ttm if required.
314 */
315
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000316 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
317 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000318 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
319 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000320 if (ret)
321 goto out_err;
322 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323
324 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
325 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200326 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200327
328 if (mem->mem_type != TTM_PL_SYSTEM) {
329 ret = ttm_tt_bind(bo->ttm, mem);
330 if (ret)
331 goto out_err;
332 }
333
334 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000335 if (bdev->driver->move_notify)
Nicolai Hähnle66257db2016-12-15 17:23:49 +0100336 bdev->driver->move_notify(bo, evict, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100337 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200338 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200339 goto moved;
340 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200341 }
342
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000343 if (bdev->driver->move_notify)
Nicolai Hähnle66257db2016-12-15 17:23:49 +0100344 bdev->driver->move_notify(bo, evict, mem);
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000345
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200346 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
347 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900348 ret = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200349 else if (bdev->driver->move)
350 ret = bdev->driver->move(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000351 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200352 else
Michel Dänzer4499f2a2016-08-08 12:28:26 +0900353 ret = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200354
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000355 if (ret) {
356 if (bdev->driver->move_notify) {
357 struct ttm_mem_reg tmp_mem = *mem;
358 *mem = bo->mem;
359 bo->mem = tmp_mem;
Nicolai Hähnle66257db2016-12-15 17:23:49 +0100360 bdev->driver->move_notify(bo, false, mem);
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000361 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000362 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000363 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200364
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000365 goto out_err;
366 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500367
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200368moved:
369 if (bo->evicted) {
Rob Clark9ef75062014-03-12 10:59:37 -0400370 if (bdev->driver->invalidate_caches) {
371 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
372 if (ret)
373 pr_err("Can not flush read caches\n");
374 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200375 bo->evicted = false;
376 }
377
378 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000379 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200380 bdev->man[bo->mem.mem_type].gpu_offset;
381 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100382 } else
383 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200384
385 return 0;
386
387out_err:
388 new_man = &bdev->man[bo->mem.mem_type];
Christian König4279cb12016-06-06 10:17:51 +0200389 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390 ttm_tt_destroy(bo->ttm);
391 bo->ttm = NULL;
392 }
393
394 return ret;
395}
396
397/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200398 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200399 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200400 * This is the place to put in driver specific hooks to release
401 * driver private resources.
402 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200403 */
404
405static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
406{
Jerome Glissedc97b342011-11-18 11:47:03 -0500407 if (bo->bdev->driver->move_notify)
Nicolai Hähnle66257db2016-12-15 17:23:49 +0100408 bo->bdev->driver->move_notify(bo, false, NULL);
Jerome Glissedc97b342011-11-18 11:47:03 -0500409
Christian König4279cb12016-06-06 10:17:51 +0200410 ttm_tt_destroy(bo->ttm);
411 bo->ttm = NULL;
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200412 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200413
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200414 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200415}
416
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200417static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
418{
419 struct reservation_object_list *fobj;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100420 struct dma_fence *fence;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200421 int i;
422
423 fobj = reservation_object_get_list(bo->resv);
424 fence = reservation_object_get_excl(bo->resv);
425 if (fence && !fence->ops->signaled)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100426 dma_fence_enable_sw_signaling(fence);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200427
428 for (i = 0; fobj && i < fobj->shared_count; ++i) {
429 fence = rcu_dereference_protected(fobj->shared[i],
430 reservation_object_held(bo->resv));
431
432 if (!fence->ops->signaled)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100433 dma_fence_enable_sw_signaling(fence);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200434 }
435}
436
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200437static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200438{
439 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200440 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200441 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200442 int ret;
443
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100444 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200445 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100446
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700447 if (!ret) {
Christian König8aa6d4f2016-04-06 11:12:04 +0200448 if (!ttm_bo_wait(bo, false, true)) {
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100449 put_count = ttm_bo_del_from_lru(bo);
450
451 spin_unlock(&glob->lru_lock);
452 ttm_bo_cleanup_memtype_use(bo);
453
454 ttm_bo_list_ref_sub(bo, put_count, true);
455
456 return;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200457 } else
458 ttm_bo_flush_all_fences(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700459
460 /*
461 * Make NO_EVICT bos immediately available to
462 * shrinkers, now that they are queued for
463 * destruction.
464 */
465 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
466 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
467 ttm_bo_add_to_lru(bo);
468 }
469
Thomas Hellstromc7523082014-02-20 11:36:25 +0100470 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700471 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200472
473 kref_get(&bo->list_kref);
474 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
475 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200476
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200477 schedule_delayed_work(&bdev->wq,
478 ((HZ / 100) < 1) ? 1 : HZ / 100);
479}
480
481/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000482 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200483 * If bo idle, remove from delayed- and lru lists, and unref.
484 * If not idle, do nothing.
485 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000486 * Must be called with lru_lock and reservation held, this function
487 * will drop both before returning.
488 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200489 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200490 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
491 */
492
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000493static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
494 bool interruptible,
495 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200496{
497 struct ttm_bo_global *glob = bo->glob;
498 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000499 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200500
Christian König8aa6d4f2016-04-06 11:12:04 +0200501 ret = ttm_bo_wait(bo, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200502
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000503 if (ret && !no_wait_gpu) {
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200504 long lret;
505 ww_mutex_unlock(&bo->resv->lock);
506 spin_unlock(&glob->lru_lock);
507
508 lret = reservation_object_wait_timeout_rcu(bo->resv,
509 true,
510 interruptible,
511 30 * HZ);
512
513 if (lret < 0)
514 return lret;
515 else if (lret == 0)
516 return -EBUSY;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000517
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000518 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200519 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000520
521 /*
522 * We raced, and lost, someone else holds the reservation now,
523 * and is probably busy in ttm_bo_cleanup_memtype_use.
524 *
525 * Even if it's not the case, because we finished waiting any
526 * delayed destruction would succeed, so just return success
527 * here.
528 */
529 if (ret) {
530 spin_unlock(&glob->lru_lock);
531 return 0;
532 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100533
534 /*
535 * remove sync_obj with ttm_bo_wait, the wait should be
536 * finished, and no new wait object should have been added.
537 */
Christian König8aa6d4f2016-04-06 11:12:04 +0200538 ret = ttm_bo_wait(bo, false, true);
Maarten Lankhorst70401382014-01-21 13:07:01 +0100539 WARN_ON(ret);
540 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000541
542 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100543 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000544 spin_unlock(&glob->lru_lock);
545 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200546 }
547
548 put_count = ttm_bo_del_from_lru(bo);
549 list_del_init(&bo->ddestroy);
550 ++put_count;
551
552 spin_unlock(&glob->lru_lock);
553 ttm_bo_cleanup_memtype_use(bo);
554
Dave Airlied6ea8882010-11-22 13:24:40 +1000555 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200556
557 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200558}
559
560/**
561 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
562 * encountered buffers.
563 */
564
565static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
566{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200567 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100568 struct ttm_buffer_object *entry = NULL;
569 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200570
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200571 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100572 if (list_empty(&bdev->ddestroy))
573 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200574
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100575 entry = list_first_entry(&bdev->ddestroy,
576 struct ttm_buffer_object, ddestroy);
577 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200578
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100579 for (;;) {
580 struct ttm_buffer_object *nentry = NULL;
581
582 if (entry->ddestroy.next != &bdev->ddestroy) {
583 nentry = list_first_entry(&entry->ddestroy,
584 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200585 kref_get(&nentry->list_kref);
586 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200587
Christian Königdfd5e502016-04-06 11:12:03 +0200588 ret = __ttm_bo_reserve(entry, false, true, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100589 if (remove_all && ret) {
590 spin_unlock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200591 ret = __ttm_bo_reserve(entry, false, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100592 spin_lock(&glob->lru_lock);
593 }
594
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000595 if (!ret)
596 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
597 !remove_all);
598 else
599 spin_unlock(&glob->lru_lock);
600
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200601 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100602 entry = nentry;
603
604 if (ret || !entry)
605 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200606
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200607 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100608 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200609 break;
610 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200611
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100612out_unlock:
613 spin_unlock(&glob->lru_lock);
614out:
615 if (entry)
616 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200617 return ret;
618}
619
620static void ttm_bo_delayed_workqueue(struct work_struct *work)
621{
622 struct ttm_bo_device *bdev =
623 container_of(work, struct ttm_bo_device, wq.work);
624
625 if (ttm_bo_delayed_delete(bdev, false)) {
626 schedule_delayed_work(&bdev->wq,
627 ((HZ / 100) < 1) ? 1 : HZ / 100);
628 }
629}
630
631static void ttm_bo_release(struct kref *kref)
632{
633 struct ttm_buffer_object *bo =
634 container_of(kref, struct ttm_buffer_object, kref);
635 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100636 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200637
David Herrmann72525b32013-07-24 21:08:53 +0200638 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100639 ttm_mem_io_lock(man, false);
640 ttm_mem_io_free_vm(bo);
641 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200642 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200643 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200644}
645
646void ttm_bo_unref(struct ttm_buffer_object **p_bo)
647{
648 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649
650 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200651 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200652}
653EXPORT_SYMBOL(ttm_bo_unref);
654
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400655int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
656{
657 return cancel_delayed_work_sync(&bdev->wq);
658}
659EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
660
661void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
662{
663 if (resched)
664 schedule_delayed_work(&bdev->wq,
665 ((HZ / 100) < 1) ? 1 : HZ / 100);
666}
667EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
668
Jerome Glisseca262a9992009-12-08 15:33:32 +0100669static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000670 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200671{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200672 struct ttm_bo_device *bdev = bo->bdev;
673 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100674 struct ttm_placement placement;
675 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200676
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200677 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200678
679 evict_mem = bo->mem;
680 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100681 evict_mem.bus.io_reserved_vm = false;
682 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200683
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100684 placement.num_placement = 0;
685 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100686 bdev->driver->evict_flags(bo, &placement);
687 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000688 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200689 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100690 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700691 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
692 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100693 ttm_bo_mem_space_debug(bo, &placement);
694 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200695 goto out;
696 }
697
698 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000699 no_wait_gpu);
Christian König17d33bc2016-06-06 10:17:56 +0200700 if (unlikely(ret)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100701 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700702 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000703 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200704 goto out;
705 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200706 bo->evicted = true;
707out:
708 return ret;
709}
710
Christian Königa2ab19fe2016-08-30 17:26:04 +0200711bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
712 const struct ttm_place *place)
713{
714 /* Don't evict this BO if it's outside of the
715 * requested placement range
716 */
717 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
718 (place->lpfn && place->lpfn <= bo->mem.start))
719 return false;
720
721 return true;
722}
723EXPORT_SYMBOL(ttm_bo_eviction_valuable);
724
Jerome Glisseca262a9992009-12-08 15:33:32 +0100725static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
726 uint32_t mem_type,
Michel Dänzere3001802014-10-09 15:02:59 +0900727 const struct ttm_place *place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000728 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000729 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100730{
731 struct ttm_bo_global *glob = bdev->glob;
732 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
733 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000734 int ret = -EBUSY, put_count;
Christian Königcf6c4672017-01-10 14:08:28 +0100735 unsigned i;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100736
737 spin_lock(&glob->lru_lock);
Christian Königcf6c4672017-01-10 14:08:28 +0100738 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
739 list_for_each_entry(bo, &man->lru[i], lru) {
740 ret = __ttm_bo_reserve(bo, false, true, NULL);
741 if (ret)
742 continue;
Michel Dänzere3001802014-10-09 15:02:59 +0900743
Christian Königcf6c4672017-01-10 14:08:28 +0100744 if (place && !bdev->driver->eviction_valuable(bo,
745 place)) {
746 __ttm_bo_unreserve(bo);
747 ret = -EBUSY;
748 continue;
749 }
750
751 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900752 }
Christian Königa2ab19fe2016-08-30 17:26:04 +0200753
Christian Königcf6c4672017-01-10 14:08:28 +0100754 if (!ret)
755 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100756 }
757
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000758 if (ret) {
759 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000760 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200761 }
762
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000763 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100764
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000765 if (!list_empty(&bo->ddestroy)) {
766 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
767 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100768 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000769 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100770 }
771
772 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100773 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100774
775 BUG_ON(ret != 0);
776
Dave Airlied6ea8882010-11-22 13:24:40 +1000777 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100778
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000779 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100780 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100781
Jerome Glisseca262a9992009-12-08 15:33:32 +0100782 kref_put(&bo->list_kref, ttm_bo_release_list);
783 return ret;
784}
785
Ben Skeggs42311ff2010-08-04 12:07:08 +1000786void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
787{
Ben Skeggsd961db72010-08-05 10:48:18 +1000788 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000789
Ben Skeggsd961db72010-08-05 10:48:18 +1000790 if (mem->mm_node)
791 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000792}
793EXPORT_SYMBOL(ttm_bo_mem_put);
794
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200795/**
Christian König3ddf4ad2016-06-15 13:44:03 +0200796 * Add the last move fence to the BO and reserve a new shared slot.
797 */
798static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
799 struct ttm_mem_type_manager *man,
800 struct ttm_mem_reg *mem)
801{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100802 struct dma_fence *fence;
Christian König3ddf4ad2016-06-15 13:44:03 +0200803 int ret;
804
805 spin_lock(&man->move_lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100806 fence = dma_fence_get(man->move);
Christian König3ddf4ad2016-06-15 13:44:03 +0200807 spin_unlock(&man->move_lock);
808
809 if (fence) {
810 reservation_object_add_shared_fence(bo->resv, fence);
811
812 ret = reservation_object_reserve_shared(bo->resv);
813 if (unlikely(ret))
814 return ret;
815
Chris Wilsonf54d1862016-10-25 13:00:45 +0100816 dma_fence_put(bo->moving);
Christian König3ddf4ad2016-06-15 13:44:03 +0200817 bo->moving = fence;
818 }
819
820 return 0;
821}
822
823/**
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200824 * Repeatedly evict memory from the LRU for @mem_type until we create enough
825 * space, or we've evicted everything and there isn't enough space.
826 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100827static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
828 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200829 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100830 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000831 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000832 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200833{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100834 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200835 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200836 int ret;
837
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200838 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200839 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200840 if (unlikely(ret != 0))
841 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000842 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100843 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900844 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000845 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100846 if (unlikely(ret != 0))
847 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200848 } while (1);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200849 mem->mem_type = mem_type;
Christian König3ddf4ad2016-06-15 13:44:03 +0200850 return ttm_bo_add_move_fence(bo, man, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200851}
852
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200853static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
854 uint32_t cur_placement,
855 uint32_t proposed_placement)
856{
857 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
858 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
859
860 /**
861 * Keep current caching if possible.
862 */
863
864 if ((cur_placement & caching) != 0)
865 result |= (cur_placement & caching);
866 else if ((man->default_caching & caching) != 0)
867 result |= man->default_caching;
868 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
869 result |= TTM_PL_FLAG_CACHED;
870 else if ((TTM_PL_FLAG_WC & caching) != 0)
871 result |= TTM_PL_FLAG_WC;
872 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
873 result |= TTM_PL_FLAG_UNCACHED;
874
875 return result;
876}
877
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200878static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200879 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200880 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200881 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882{
883 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
884
Christian Königf1217ed2014-08-27 13:16:04 +0200885 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200886 return false;
887
Christian Königf1217ed2014-08-27 13:16:04 +0200888 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200889 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200890
Christian Königf1217ed2014-08-27 13:16:04 +0200891 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200892
893 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200894 return true;
895}
896
897/**
898 * Creates space for memory region @mem according to its type.
899 *
900 * This function first searches for free space in compatible memory types in
901 * the priority order defined by the driver. If free space isn't found, then
902 * ttm_bo_mem_force_space is attempted in priority order to evict and find
903 * space.
904 */
905int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100906 struct ttm_placement *placement,
907 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000908 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000909 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200910{
911 struct ttm_bo_device *bdev = bo->bdev;
912 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200913 uint32_t mem_type = TTM_PL_SYSTEM;
914 uint32_t cur_flags = 0;
915 bool type_found = false;
916 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100917 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100918 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200919
Christian König3ddf4ad2016-06-15 13:44:03 +0200920 ret = reservation_object_reserve_shared(bo->resv);
921 if (unlikely(ret))
922 return ret;
923
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200924 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000925 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200926 const struct ttm_place *place = &placement->placement[i];
927
928 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100929 if (ret)
930 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200931 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700932 if (!man->has_type || !man->use_type)
933 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200934
Christian Königf1217ed2014-08-27 13:16:04 +0200935 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100936 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200937
938 if (!type_ok)
939 continue;
940
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700941 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200942 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
943 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100944 /*
945 * Use the access and other non-mapping-related flag bits from
946 * the memory placement flags to the current flags
947 */
Christian Königf1217ed2014-08-27 13:16:04 +0200948 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100949 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200950
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951 if (mem_type == TTM_PL_SYSTEM)
952 break;
953
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700954 ret = (*man->func->get_node)(man, bo, place, mem);
955 if (unlikely(ret))
956 return ret;
Christian König3ddf4ad2016-06-15 13:44:03 +0200957
958 if (mem->mm_node) {
959 ret = ttm_bo_add_move_fence(bo, man, mem);
960 if (unlikely(ret)) {
961 (*man->func->put_node)(man, mem);
962 return ret;
963 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964 break;
Christian König3ddf4ad2016-06-15 13:44:03 +0200965 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200966 }
967
Ben Skeggsd961db72010-08-05 10:48:18 +1000968 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200969 mem->mem_type = mem_type;
970 mem->placement = cur_flags;
971 return 0;
972 }
973
Dave Airlieb6637522009-12-14 14:51:35 +1000974 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200975 const struct ttm_place *place = &placement->busy_placement[i];
976
977 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100978 if (ret)
979 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200980 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700981 if (!man->has_type || !man->use_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200982 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200983 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200984 continue;
985
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700986 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200987 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
988 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100989 /*
990 * Use the access and other non-mapping-related flag bits from
991 * the memory placement flags to the current flags
992 */
Christian Königf1217ed2014-08-27 13:16:04 +0200993 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100994 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200995
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100996 if (mem_type == TTM_PL_SYSTEM) {
997 mem->mem_type = mem_type;
998 mem->placement = cur_flags;
999 mem->mm_node = NULL;
1000 return 0;
1001 }
1002
Christian Königf1217ed2014-08-27 13:16:04 +02001003 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001004 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005 if (ret == 0 && mem->mm_node) {
1006 mem->placement = cur_flags;
1007 return 0;
1008 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001009 if (ret == -ERESTARTSYS)
1010 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001011 }
Thomas Hellstrome30f3962015-09-14 01:24:41 -07001012
1013 if (!type_found) {
1014 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
1015 return -EINVAL;
1016 }
1017
1018 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001019}
1020EXPORT_SYMBOL(ttm_bo_mem_space);
1021
Rashika Kheria6e87fa42014-01-06 22:12:58 +05301022static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001023 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001024 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001025 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001026{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001027 int ret = 0;
1028 struct ttm_mem_reg mem;
1029
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001030 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001031
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001032 mem.num_pages = bo->num_pages;
1033 mem.size = mem.num_pages << PAGE_SHIFT;
1034 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001035 mem.bus.io_reserved_vm = false;
1036 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037 /*
1038 * Determine where to move the buffer.
1039 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001040 ret = ttm_bo_mem_space(bo, placement, &mem,
1041 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001042 if (ret)
1043 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001044 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1045 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001047 if (ret && mem.mm_node)
1048 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049 return ret;
1050}
1051
Sinclair Yeh94477bf2016-06-29 12:58:49 -07001052bool ttm_bo_mem_compat(struct ttm_placement *placement,
1053 struct ttm_mem_reg *mem,
1054 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001055{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001056 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001057
Jerome Glisseca262a9992009-12-08 15:33:32 +01001058 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001059 const struct ttm_place *heap = &placement->placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001060 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001061 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001062 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001063 continue;
1064
1065 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001066 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1067 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1068 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001069 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001070
1071 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001072 const struct ttm_place *heap = &placement->busy_placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001073 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001074 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001075 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001076 continue;
1077
1078 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001079 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1080 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1081 return true;
1082 }
1083
1084 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001085}
Sinclair Yeh94477bf2016-06-29 12:58:49 -07001086EXPORT_SYMBOL(ttm_bo_mem_compat);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001087
Jerome Glisse09855ac2009-12-10 17:16:27 +01001088int ttm_bo_validate(struct ttm_buffer_object *bo,
1089 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001090 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001091 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092{
1093 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001094 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001095
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001096 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001097 /*
1098 * Check whether we need to move buffer.
1099 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001100 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001101 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1102 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001103 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001104 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001105 } else {
1106 /*
1107 * Use the access and other non-mapping-related flag bits from
1108 * the compatible memory placement flags to the active flags
1109 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001110 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001111 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001112 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113 /*
1114 * We might need to add a TTM.
1115 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001116 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1117 ret = ttm_bo_add_ttm(bo, true);
1118 if (ret)
1119 return ret;
1120 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001121 return 0;
1122}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001123EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001124
Jerome Glisse09855ac2009-12-10 17:16:27 +01001125int ttm_bo_init(struct ttm_bo_device *bdev,
1126 struct ttm_buffer_object *bo,
1127 unsigned long size,
1128 enum ttm_bo_type type,
1129 struct ttm_placement *placement,
1130 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001131 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001132 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001133 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001134 struct sg_table *sg,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001135 struct reservation_object *resv,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001136 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001137{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001138 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001139 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001140 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001141 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001142
1143 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1144 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001145 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001146 if (destroy)
1147 (*destroy)(bo);
1148 else
1149 kfree(bo);
1150 return -ENOMEM;
1151 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1154 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001155 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001156 if (destroy)
1157 (*destroy)(bo);
1158 else
1159 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001160 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161 return -EINVAL;
1162 }
1163 bo->destroy = destroy;
1164
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001165 kref_init(&bo->kref);
1166 kref_init(&bo->list_kref);
1167 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001168 INIT_LIST_HEAD(&bo->lru);
1169 INIT_LIST_HEAD(&bo->ddestroy);
1170 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001171 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001172 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001173 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001174 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001175 bo->type = type;
1176 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001177 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178 bo->mem.mem_type = TTM_PL_SYSTEM;
1179 bo->mem.num_pages = bo->num_pages;
1180 bo->mem.mm_node = NULL;
1181 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001182 bo->mem.bus.io_reserved_vm = false;
1183 bo->mem.bus.io_reserved_count = 0;
Christian König5bc73062016-06-15 13:44:01 +02001184 bo->moving = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001186 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001188 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001189 if (resv) {
1190 bo->resv = resv;
1191 lockdep_assert_held(&bo->resv->lock.base);
1192 } else {
1193 bo->resv = &bo->ttm_resv;
1194 reservation_object_init(&bo->ttm_resv);
1195 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001196 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001197 drm_vma_node_reset(&bo->vma_node);
Christian Königcf6c4672017-01-10 14:08:28 +01001198 bo->priority = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001201 * For ttm_bo_type_device buffers, allocate
1202 * address space from the device.
1203 */
Christian Königf1217ed2014-08-27 13:16:04 +02001204 if (bo->type == ttm_bo_type_device ||
1205 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001206 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1207 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001208
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001209 /* passed reservation objects should already be locked,
1210 * since otherwise lockdep will be angered in radeon.
1211 */
1212 if (!resv) {
1213 locked = ww_mutex_trylock(&bo->resv->lock);
1214 WARN_ON(!locked);
1215 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001216
1217 if (likely(!ret))
1218 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219
Christian König33d48cf2016-01-11 15:35:18 +01001220 if (!resv) {
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001221 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001222
Christian König33d48cf2016-01-11 15:35:18 +01001223 } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1224 spin_lock(&bo->glob->lru_lock);
1225 ttm_bo_add_to_lru(bo);
1226 spin_unlock(&bo->glob->lru_lock);
1227 }
1228
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001229 if (unlikely(ret))
1230 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231
1232 return ret;
1233}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001234EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001235
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001236size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1237 unsigned long bo_size,
1238 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001239{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001240 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1241 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001242
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001243 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001244 size += ttm_round_pot(npages * sizeof(void *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001245 size += ttm_round_pot(sizeof(struct ttm_tt));
1246 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001247}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001248EXPORT_SYMBOL(ttm_bo_acc_size);
1249
1250size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1251 unsigned long bo_size,
1252 unsigned struct_size)
1253{
1254 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1255 size_t size = 0;
1256
1257 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001258 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001259 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1260 return size;
1261}
1262EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001263
Jerome Glisse09855ac2009-12-10 17:16:27 +01001264int ttm_bo_create(struct ttm_bo_device *bdev,
1265 unsigned long size,
1266 enum ttm_bo_type type,
1267 struct ttm_placement *placement,
1268 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001269 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001270 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001271 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272{
1273 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001274 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001275 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001276
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001277 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001278 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001279 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001280
Thomas Hellstroma393c732012-06-12 13:28:42 +02001281 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001282 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001283 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001284 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001285 if (likely(ret == 0))
1286 *p_bo = bo;
1287
1288 return ret;
1289}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001290EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001292static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Christian König2ee7fc92017-01-06 19:16:07 +01001293 unsigned mem_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001294{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001295 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001296 struct ttm_bo_global *glob = bdev->glob;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001297 struct dma_fence *fence;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298 int ret;
Christian Königcf6c4672017-01-10 14:08:28 +01001299 unsigned i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001300
1301 /*
1302 * Can't use standard list traversal since we're unlocking.
1303 */
1304
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001305 spin_lock(&glob->lru_lock);
Christian Königcf6c4672017-01-10 14:08:28 +01001306 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1307 while (!list_empty(&man->lru[i])) {
1308 spin_unlock(&glob->lru_lock);
1309 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
1310 if (ret)
1311 return ret;
1312 spin_lock(&glob->lru_lock);
1313 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001314 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001315 spin_unlock(&glob->lru_lock);
Christian Königaff98ba2016-06-22 14:16:28 +02001316
1317 spin_lock(&man->move_lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001318 fence = dma_fence_get(man->move);
Christian Königaff98ba2016-06-22 14:16:28 +02001319 spin_unlock(&man->move_lock);
1320
1321 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001322 ret = dma_fence_wait(fence, false);
1323 dma_fence_put(fence);
Christian König2ee7fc92017-01-06 19:16:07 +01001324 if (ret)
1325 return ret;
Christian Königaff98ba2016-06-22 14:16:28 +02001326 }
1327
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001328 return 0;
1329}
1330
1331int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1332{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001333 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001334 int ret = -EINVAL;
1335
1336 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001337 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001338 return ret;
1339 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001340 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001341
1342 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001343 pr_err("Trying to take down uninitialized memory manager type %u\n",
1344 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001345 return ret;
1346 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01001347 dma_fence_put(man->move);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001348
1349 man->use_type = false;
1350 man->has_type = false;
1351
1352 ret = 0;
1353 if (mem_type > 0) {
Christian König2ee7fc92017-01-06 19:16:07 +01001354 ret = ttm_bo_force_list_clean(bdev, mem_type);
1355 if (ret) {
1356 pr_err("Cleanup eviction failed\n");
1357 return ret;
1358 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001359
Ben Skeggsd961db72010-08-05 10:48:18 +10001360 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001361 }
1362
1363 return ret;
1364}
1365EXPORT_SYMBOL(ttm_bo_clean_mm);
1366
1367int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1368{
1369 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1370
1371 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001372 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001373 return -EINVAL;
1374 }
1375
1376 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001377 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001378 return 0;
1379 }
1380
Christian König2ee7fc92017-01-06 19:16:07 +01001381 return ttm_bo_force_list_clean(bdev, mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001382}
1383EXPORT_SYMBOL(ttm_bo_evict_mm);
1384
1385int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001386 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001387{
1388 int ret = -EINVAL;
1389 struct ttm_mem_type_manager *man;
Christian Königcf6c4672017-01-10 14:08:28 +01001390 unsigned i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001391
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001392 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001393 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001394 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001395 man->io_reserve_fastpath = true;
1396 man->use_io_reserve_lru = false;
1397 mutex_init(&man->io_reserve_mutex);
Christian König3ddf4ad2016-06-15 13:44:03 +02001398 spin_lock_init(&man->move_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001399 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001400
1401 ret = bdev->driver->init_mem_type(bdev, type, man);
1402 if (ret)
1403 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001404 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001405
1406 ret = 0;
1407 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001408 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001409 if (ret)
1410 return ret;
1411 }
1412 man->has_type = true;
1413 man->use_type = true;
1414 man->size = p_size;
1415
Christian Königcf6c4672017-01-10 14:08:28 +01001416 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1417 INIT_LIST_HEAD(&man->lru[i]);
Christian König3ddf4ad2016-06-15 13:44:03 +02001418 man->move = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001419
1420 return 0;
1421}
1422EXPORT_SYMBOL(ttm_bo_init_mm);
1423
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001424static void ttm_bo_global_kobj_release(struct kobject *kobj)
1425{
1426 struct ttm_bo_global *glob =
1427 container_of(kobj, struct ttm_bo_global, kobj);
1428
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001429 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1430 __free_page(glob->dummy_read_page);
1431 kfree(glob);
1432}
1433
Dave Airlieba4420c2010-03-09 10:56:52 +10001434void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001435{
1436 struct ttm_bo_global *glob = ref->object;
1437
1438 kobject_del(&glob->kobj);
1439 kobject_put(&glob->kobj);
1440}
1441EXPORT_SYMBOL(ttm_bo_global_release);
1442
Dave Airlieba4420c2010-03-09 10:56:52 +10001443int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001444{
1445 struct ttm_bo_global_ref *bo_ref =
1446 container_of(ref, struct ttm_bo_global_ref, ref);
1447 struct ttm_bo_global *glob = ref->object;
1448 int ret;
Christian Königcf6c4672017-01-10 14:08:28 +01001449 unsigned i;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001450
1451 mutex_init(&glob->device_list_mutex);
1452 spin_lock_init(&glob->lru_lock);
1453 glob->mem_glob = bo_ref->mem_glob;
1454 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1455
1456 if (unlikely(glob->dummy_read_page == NULL)) {
1457 ret = -ENOMEM;
1458 goto out_no_drp;
1459 }
1460
Christian Königcf6c4672017-01-10 14:08:28 +01001461 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1462 INIT_LIST_HEAD(&glob->swap_lru[i]);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001463 INIT_LIST_HEAD(&glob->device_list);
1464
1465 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1466 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1467 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001468 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001469 goto out_no_shrink;
1470 }
1471
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001472 atomic_set(&glob->bo_count, 0);
1473
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001474 ret = kobject_init_and_add(
1475 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001476 if (unlikely(ret != 0))
1477 kobject_put(&glob->kobj);
1478 return ret;
1479out_no_shrink:
1480 __free_page(glob->dummy_read_page);
1481out_no_drp:
1482 kfree(glob);
1483 return ret;
1484}
1485EXPORT_SYMBOL(ttm_bo_global_init);
1486
1487
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488int ttm_bo_device_release(struct ttm_bo_device *bdev)
1489{
1490 int ret = 0;
1491 unsigned i = TTM_NUM_MEM_TYPES;
1492 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001493 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001494
1495 while (i--) {
1496 man = &bdev->man[i];
1497 if (man->has_type) {
1498 man->use_type = false;
1499 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1500 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001501 pr_err("DRM memory manager type %d is not clean\n",
1502 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001503 }
1504 man->has_type = false;
1505 }
1506 }
1507
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001508 mutex_lock(&glob->device_list_mutex);
1509 list_del(&bdev->device_list);
1510 mutex_unlock(&glob->device_list_mutex);
1511
Tejun Heof094cfc2010-12-24 15:59:06 +01001512 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001513
1514 while (ttm_bo_delayed_delete(bdev, true))
1515 ;
1516
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001517 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001518 if (list_empty(&bdev->ddestroy))
1519 TTM_DEBUG("Delayed destroy list was clean\n");
1520
Christian Königcf6c4672017-01-10 14:08:28 +01001521 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1522 if (list_empty(&bdev->man[0].lru[0]))
1523 TTM_DEBUG("Swap list %d was clean\n", i);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001524 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525
David Herrmann72525b32013-07-24 21:08:53 +02001526 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001527
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001528 return ret;
1529}
1530EXPORT_SYMBOL(ttm_bo_device_release);
1531
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001532int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001533 struct ttm_bo_global *glob,
1534 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001535 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001536 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001537 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538{
1539 int ret = -EINVAL;
1540
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001541 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001542
1543 memset(bdev->man, 0, sizeof(bdev->man));
1544
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001545 /*
1546 * Initialize the system memory buffer type.
1547 * Other types need to be driver / IOCTL initialized.
1548 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001549 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001550 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001551 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001552
David Herrmann72525b32013-07-24 21:08:53 +02001553 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1554 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001555 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001556 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001557 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001558 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001559 bdev->need_dma32 = need_dma32;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001560 mutex_lock(&glob->device_list_mutex);
1561 list_add_tail(&bdev->device_list, &glob->device_list);
1562 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563
1564 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001565out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001566 return ret;
1567}
1568EXPORT_SYMBOL(ttm_bo_device_init);
1569
1570/*
1571 * buffer object vm functions.
1572 */
1573
1574bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1575{
1576 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1577
1578 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1579 if (mem->mem_type == TTM_PL_SYSTEM)
1580 return false;
1581
1582 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1583 return false;
1584
1585 if (mem->placement & TTM_PL_FLAG_CACHED)
1586 return false;
1587 }
1588 return true;
1589}
1590
Thomas Hellstromeba67092010-11-11 09:41:57 +01001591void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001592{
1593 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001594
David Herrmann51335df2013-07-24 21:10:03 +02001595 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001596 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001597}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001598
1599void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1600{
1601 struct ttm_bo_device *bdev = bo->bdev;
1602 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1603
1604 ttm_mem_io_lock(man, false);
1605 ttm_bo_unmap_virtual_locked(bo);
1606 ttm_mem_io_unlock(man);
1607}
1608
1609
Dave Airliee024e112009-06-24 09:48:08 +10001610EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001611
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001612int ttm_bo_wait(struct ttm_buffer_object *bo,
Christian König8aa6d4f2016-04-06 11:12:04 +02001613 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001614{
Christian König98a6dd92016-11-07 16:16:15 -05001615 long timeout = 15 * HZ;
1616
1617 if (no_wait) {
1618 if (reservation_object_test_signaled_rcu(bo->resv, true))
1619 return 0;
1620 else
1621 return -EBUSY;
1622 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001623
Christian Königf849c6d2016-06-15 13:44:02 +02001624 timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
1625 interruptible, timeout);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001626 if (timeout < 0)
1627 return timeout;
1628
1629 if (timeout == 0)
1630 return -EBUSY;
1631
Christian Königf849c6d2016-06-15 13:44:02 +02001632 reservation_object_add_excl_fence(bo->resv, NULL);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001633 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001634}
1635EXPORT_SYMBOL(ttm_bo_wait);
1636
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001637int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1638{
1639 int ret = 0;
1640
1641 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001642 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001643 */
1644
Christian Königdfd5e502016-04-06 11:12:03 +02001645 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001646 if (unlikely(ret != 0))
1647 return ret;
Christian König8aa6d4f2016-04-06 11:12:04 +02001648 ret = ttm_bo_wait(bo, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001649 if (likely(ret == 0))
1650 atomic_inc(&bo->cpu_writers);
1651 ttm_bo_unreserve(bo);
1652 return ret;
1653}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001654EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001655
1656void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1657{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001658 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001659}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001660EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001661
1662/**
1663 * A buffer object shrink method that tries to swap out the first
1664 * buffer object on the bo_global::swap_lru list.
1665 */
1666
1667static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1668{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001669 struct ttm_bo_global *glob =
1670 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001671 struct ttm_buffer_object *bo;
1672 int ret = -EBUSY;
1673 int put_count;
1674 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
Christian Königcf6c4672017-01-10 14:08:28 +01001675 unsigned i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001676
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001677 spin_lock(&glob->lru_lock);
Christian Königcf6c4672017-01-10 14:08:28 +01001678 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1679 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1680 ret = __ttm_bo_reserve(bo, false, true, NULL);
1681 if (!ret)
1682 break;
1683 }
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001684 if (!ret)
1685 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001686 }
1687
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001688 if (ret) {
1689 spin_unlock(&glob->lru_lock);
1690 return ret;
1691 }
1692
1693 kref_get(&bo->list_kref);
1694
1695 if (!list_empty(&bo->ddestroy)) {
1696 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1697 kref_put(&bo->list_kref, ttm_bo_release_list);
1698 return ret;
1699 }
1700
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001701 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001702 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001703
Dave Airlied6ea8882010-11-22 13:24:40 +10001704 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001705
1706 /**
Christian König61ede072016-06-06 10:17:57 +02001707 * Move to system cached
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001708 */
1709
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001710 if ((bo->mem.placement & swap_placement) != swap_placement) {
1711 struct ttm_mem_reg evict_mem;
1712
1713 evict_mem = bo->mem;
1714 evict_mem.mm_node = NULL;
1715 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1716 evict_mem.mem_type = TTM_PL_SYSTEM;
1717
1718 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001719 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001720 if (unlikely(ret != 0))
1721 goto out;
1722 }
1723
Christian König61ede072016-06-06 10:17:57 +02001724 /**
1725 * Make sure BO is idle.
1726 */
1727
1728 ret = ttm_bo_wait(bo, false, false);
1729 if (unlikely(ret != 0))
1730 goto out;
1731
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001732 ttm_bo_unmap_virtual(bo);
1733
1734 /**
1735 * Swap out. Buffer will be swapped in again as soon as
1736 * anyone tries to access a ttm page.
1737 */
1738
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001739 if (bo->bdev->driver->swap_notify)
1740 bo->bdev->driver->swap_notify(bo);
1741
Jan Engelhardt5df23972011-04-04 01:25:18 +02001742 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001743out:
1744
1745 /**
1746 *
1747 * Unreserve without putting on LRU to avoid swapping out an
1748 * already swapped buffer.
1749 */
1750
Thomas Hellstromc7523082014-02-20 11:36:25 +01001751 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001752 kref_put(&bo->list_kref, ttm_bo_release_list);
1753 return ret;
1754}
1755
1756void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1757{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001758 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001759 ;
1760}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001761EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001762
1763/**
1764 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1765 * unreserved
1766 *
1767 * @bo: Pointer to buffer
1768 */
1769int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1770{
1771 int ret;
1772
1773 /*
1774 * In the absense of a wait_unlocked API,
1775 * Use the bo::wu_mutex to avoid triggering livelocks due to
1776 * concurrent use of this function. Note that this use of
1777 * bo::wu_mutex can go away if we change locking order to
1778 * mmap_sem -> bo::reserve.
1779 */
1780 ret = mutex_lock_interruptible(&bo->wu_mutex);
1781 if (unlikely(ret != 0))
1782 return -ERESTARTSYS;
1783 if (!ww_mutex_is_locked(&bo->resv->lock))
1784 goto out_unlock;
Christian Königdfd5e502016-04-06 11:12:03 +02001785 ret = __ttm_bo_reserve(bo, true, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001786 if (unlikely(ret != 0))
1787 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001788 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001789
1790out_unlock:
1791 mutex_unlock(&bo->wu_mutex);
1792 return ret;
1793}