blob: 75f04b5f8c0900923eb318dc4f41628776c5e74f [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/jiffies.h>
37#include <linux/slab.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/file.h>
41#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +020043#include <linux/reservation.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020044
45#define TTM_ASSERT_LOCKED(param)
46#define TTM_DEBUG(fmt, arg...)
47#define TTM_BO_HASH_ORDER 13
48
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020049static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020050static void ttm_bo_global_kobj_release(struct kobject *kobj);
51
52static struct attribute ttm_bo_count = {
53 .name = "bo_count",
54 .mode = S_IRUGO
55};
56
Christian Königf1217ed2014-08-27 13:16:04 +020057static inline int ttm_mem_type_from_place(const struct ttm_place *place,
58 uint32_t *mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010059{
60 int i;
61
62 for (i = 0; i <= TTM_PL_PRIV5; i++)
Christian Königf1217ed2014-08-27 13:16:04 +020063 if (place->flags & (1 << i)) {
Jerome Glissefb53f862009-12-09 21:55:10 +010064 *mem_type = i;
65 return 0;
66 }
67 return -EINVAL;
68}
69
Jerome Glisse5012f502009-12-10 18:07:26 +010070static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010071{
Jerome Glisse5012f502009-12-10 18:07:26 +010072 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
73
Joe Perches25d04792012-03-16 21:43:50 -070074 pr_err(" has_type: %d\n", man->has_type);
75 pr_err(" use_type: %d\n", man->use_type);
76 pr_err(" flags: 0x%08X\n", man->flags);
Alex Deucher54c4cd62015-03-04 00:18:38 -050077 pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
Joe Perches25d04792012-03-16 21:43:50 -070078 pr_err(" size: %llu\n", man->size);
79 pr_err(" available_caching: 0x%08X\n", man->available_caching);
80 pr_err(" default_caching: 0x%08X\n", man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100081 if (mem_type != TTM_PL_SYSTEM)
82 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010083}
84
85static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
86 struct ttm_placement *placement)
87{
Jerome Glissefb53f862009-12-09 21:55:10 +010088 int i, ret, mem_type;
89
Joe Perches25d04792012-03-16 21:43:50 -070090 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
91 bo, bo->mem.num_pages, bo->mem.size >> 10,
92 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010093 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +020094 ret = ttm_mem_type_from_place(&placement->placement[i],
Jerome Glissefb53f862009-12-09 21:55:10 +010095 &mem_type);
96 if (ret)
97 return;
Joe Perches25d04792012-03-16 21:43:50 -070098 pr_err(" placement[%d]=0x%08X (%d)\n",
Christian Königf1217ed2014-08-27 13:16:04 +020099 i, placement->placement[i].flags, mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +0100100 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100101 }
102}
103
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200104static ssize_t ttm_bo_global_show(struct kobject *kobj,
105 struct attribute *attr,
106 char *buffer)
107{
108 struct ttm_bo_global *glob =
109 container_of(kobj, struct ttm_bo_global, kobj);
110
111 return snprintf(buffer, PAGE_SIZE, "%lu\n",
112 (unsigned long) atomic_read(&glob->bo_count));
113}
114
115static struct attribute *ttm_bo_global_attrs[] = {
116 &ttm_bo_count,
117 NULL
118};
119
Emese Revfy52cf25d2010-01-19 02:58:23 +0100120static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200121 .show = &ttm_bo_global_show
122};
123
124static struct kobj_type ttm_bo_glob_kobj_type = {
125 .release = &ttm_bo_global_kobj_release,
126 .sysfs_ops = &ttm_bo_global_ops,
127 .default_attrs = ttm_bo_global_attrs
128};
129
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200130
131static inline uint32_t ttm_bo_type_flags(unsigned type)
132{
133 return 1 << (type);
134}
135
136static void ttm_bo_release_list(struct kref *list_kref)
137{
138 struct ttm_buffer_object *bo =
139 container_of(list_kref, struct ttm_buffer_object, list_kref);
140 struct ttm_bo_device *bdev = bo->bdev;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500141 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200142
143 BUG_ON(atomic_read(&bo->list_kref.refcount));
144 BUG_ON(atomic_read(&bo->kref.refcount));
145 BUG_ON(atomic_read(&bo->cpu_writers));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
149
150 if (bo->ttm)
151 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200152 atomic_dec(&bo->glob->bo_count);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200153 if (bo->resv == &bo->ttm_resv)
154 reservation_object_fini(&bo->ttm_resv);
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800155 mutex_destroy(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 if (bo->destroy)
157 bo->destroy(bo);
158 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200159 kfree(bo);
160 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500161 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200162}
163
Dave Airlied6ea8882010-11-22 13:24:40 +1000164void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165{
166 struct ttm_bo_device *bdev = bo->bdev;
167 struct ttm_mem_type_manager *man;
168
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200169 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200170
171 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
172
173 BUG_ON(!list_empty(&bo->lru));
174
175 man = &bdev->man[bo->mem.mem_type];
176 list_add_tail(&bo->lru, &man->lru);
177 kref_get(&bo->list_kref);
178
Christian Königed704a42016-01-11 15:35:19 +0100179 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200180 list_add_tail(&bo->swap, &bo->glob->swap_lru);
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{
189 int put_count = 0;
190
191 if (!list_empty(&bo->swap)) {
192 list_del_init(&bo->swap);
193 ++put_count;
194 }
195 if (!list_empty(&bo->lru)) {
196 list_del_init(&bo->lru);
197 ++put_count;
198 }
199
200 /*
201 * TODO: Add a driver hook to delete from
202 * driver-specific LRU's here.
203 */
204
205 return put_count;
206}
207
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200208static void ttm_bo_ref_bug(struct kref *list_kref)
209{
210 BUG();
211}
212
Dave Airlied6ea8882010-11-22 13:24:40 +1000213void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
214 bool never_free)
215{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100216 kref_sub(&bo->list_kref, count,
217 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000218}
219
Maarten Lankhorst34820322013-06-27 13:48:24 +0200220void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200221{
Maarten Lankhorst34820322013-06-27 13:48:24 +0200222 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200223
Maarten Lankhorst34820322013-06-27 13:48:24 +0200224 spin_lock(&bo->glob->lru_lock);
225 put_count = ttm_bo_del_from_lru(bo);
226 spin_unlock(&bo->glob->lru_lock);
227 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200228}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200229EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200230
Christian Königab749612016-01-11 15:35:20 +0100231void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
232{
233 struct ttm_bo_device *bdev = bo->bdev;
234 struct ttm_mem_type_manager *man;
235
236 lockdep_assert_held(&bo->resv->lock.base);
237
238 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
239 list_del_init(&bo->swap);
240 list_del_init(&bo->lru);
241
242 } else {
243 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG))
244 list_move_tail(&bo->swap, &bo->glob->swap_lru);
245
246 man = &bdev->man[bo->mem.mem_type];
247 list_move_tail(&bo->lru, &man->lru);
248 }
249}
250EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
251
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200252/*
253 * Call bo->mutex locked.
254 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200255static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
256{
257 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200258 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200259 int ret = 0;
260 uint32_t page_flags = 0;
261
262 TTM_ASSERT_LOCKED(&bo->mutex);
263 bo->ttm = NULL;
264
Dave Airliead49f502009-07-10 22:36:26 +1000265 if (bdev->need_dma32)
266 page_flags |= TTM_PAGE_FLAG_DMA32;
267
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200268 switch (bo->type) {
269 case ttm_bo_type_device:
270 if (zero_alloc)
271 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
272 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400273 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
274 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200275 if (unlikely(bo->ttm == NULL))
276 ret = -ENOMEM;
277 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100278 case ttm_bo_type_sg:
279 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
280 page_flags | TTM_PAGE_FLAG_SG,
281 glob->dummy_read_page);
282 if (unlikely(bo->ttm == NULL)) {
283 ret = -ENOMEM;
284 break;
285 }
286 bo->ttm->sg = bo->sg;
287 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200288 default:
Joe Perches25d04792012-03-16 21:43:50 -0700289 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200290 ret = -EINVAL;
291 break;
292 }
293
294 return ret;
295}
296
297static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
298 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000299 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000300 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200301{
302 struct ttm_bo_device *bdev = bo->bdev;
303 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
304 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
305 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
306 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
307 int ret = 0;
308
309 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100310 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
311 ret = ttm_mem_io_lock(old_man, true);
312 if (unlikely(ret != 0))
313 goto out_err;
314 ttm_bo_unmap_virtual_locked(bo);
315 ttm_mem_io_unlock(old_man);
316 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200317
318 /*
319 * Create and bind a ttm if required.
320 */
321
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000322 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
323 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000324 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
325 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000326 if (ret)
327 goto out_err;
328 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200329
330 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
331 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200332 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200333
334 if (mem->mem_type != TTM_PL_SYSTEM) {
335 ret = ttm_tt_bind(bo->ttm, mem);
336 if (ret)
337 goto out_err;
338 }
339
340 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000341 if (bdev->driver->move_notify)
342 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100343 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200345 goto moved;
346 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200347 }
348
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000349 if (bdev->driver->move_notify)
350 bdev->driver->move_notify(bo, mem);
351
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200352 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
353 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000354 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200355 else if (bdev->driver->move)
356 ret = bdev->driver->move(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000357 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200358 else
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000359 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000361 if (ret) {
362 if (bdev->driver->move_notify) {
363 struct ttm_mem_reg tmp_mem = *mem;
364 *mem = bo->mem;
365 bo->mem = tmp_mem;
366 bdev->driver->move_notify(bo, mem);
367 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000368 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000369 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200370
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000371 goto out_err;
372 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500373
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200374moved:
375 if (bo->evicted) {
Rob Clark9ef75062014-03-12 10:59:37 -0400376 if (bdev->driver->invalidate_caches) {
377 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
378 if (ret)
379 pr_err("Can not flush read caches\n");
380 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200381 bo->evicted = false;
382 }
383
384 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000385 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200386 bdev->man[bo->mem.mem_type].gpu_offset;
387 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100388 } else
389 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390
391 return 0;
392
393out_err:
394 new_man = &bdev->man[bo->mem.mem_type];
395 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
396 ttm_tt_unbind(bo->ttm);
397 ttm_tt_destroy(bo->ttm);
398 bo->ttm = NULL;
399 }
400
401 return ret;
402}
403
404/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200405 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200406 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200407 * This is the place to put in driver specific hooks to release
408 * driver private resources.
409 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200410 */
411
412static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
413{
Jerome Glissedc97b342011-11-18 11:47:03 -0500414 if (bo->bdev->driver->move_notify)
415 bo->bdev->driver->move_notify(bo, NULL);
416
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200417 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200418 ttm_tt_unbind(bo->ttm);
419 ttm_tt_destroy(bo->ttm);
420 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200421 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200422 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200423
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200424 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200425}
426
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200427static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
428{
429 struct reservation_object_list *fobj;
430 struct fence *fence;
431 int i;
432
433 fobj = reservation_object_get_list(bo->resv);
434 fence = reservation_object_get_excl(bo->resv);
435 if (fence && !fence->ops->signaled)
436 fence_enable_sw_signaling(fence);
437
438 for (i = 0; fobj && i < fobj->shared_count; ++i) {
439 fence = rcu_dereference_protected(fobj->shared[i],
440 reservation_object_held(bo->resv));
441
442 if (!fence->ops->signaled)
443 fence_enable_sw_signaling(fence);
444 }
445}
446
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200447static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200448{
449 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200450 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200451 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200452 int ret;
453
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100454 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200455 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100456
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700457 if (!ret) {
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200458 if (!ttm_bo_wait(bo, false, false, true)) {
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100459 put_count = ttm_bo_del_from_lru(bo);
460
461 spin_unlock(&glob->lru_lock);
462 ttm_bo_cleanup_memtype_use(bo);
463
464 ttm_bo_list_ref_sub(bo, put_count, true);
465
466 return;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200467 } else
468 ttm_bo_flush_all_fences(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700469
470 /*
471 * Make NO_EVICT bos immediately available to
472 * shrinkers, now that they are queued for
473 * destruction.
474 */
475 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
476 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
477 ttm_bo_add_to_lru(bo);
478 }
479
Thomas Hellstromc7523082014-02-20 11:36:25 +0100480 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700481 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200482
483 kref_get(&bo->list_kref);
484 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
485 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200486
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200487 schedule_delayed_work(&bdev->wq,
488 ((HZ / 100) < 1) ? 1 : HZ / 100);
489}
490
491/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000492 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200493 * If bo idle, remove from delayed- and lru lists, and unref.
494 * If not idle, do nothing.
495 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000496 * Must be called with lru_lock and reservation held, this function
497 * will drop both before returning.
498 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200499 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200500 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
501 */
502
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000503static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
504 bool interruptible,
505 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200506{
507 struct ttm_bo_global *glob = bo->glob;
508 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000509 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200510
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000511 ret = ttm_bo_wait(bo, false, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200512
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000513 if (ret && !no_wait_gpu) {
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200514 long lret;
515 ww_mutex_unlock(&bo->resv->lock);
516 spin_unlock(&glob->lru_lock);
517
518 lret = reservation_object_wait_timeout_rcu(bo->resv,
519 true,
520 interruptible,
521 30 * HZ);
522
523 if (lret < 0)
524 return lret;
525 else if (lret == 0)
526 return -EBUSY;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000527
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000528 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200529 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000530
531 /*
532 * We raced, and lost, someone else holds the reservation now,
533 * and is probably busy in ttm_bo_cleanup_memtype_use.
534 *
535 * Even if it's not the case, because we finished waiting any
536 * delayed destruction would succeed, so just return success
537 * here.
538 */
539 if (ret) {
540 spin_unlock(&glob->lru_lock);
541 return 0;
542 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100543
544 /*
545 * remove sync_obj with ttm_bo_wait, the wait should be
546 * finished, and no new wait object should have been added.
547 */
Maarten Lankhorst70401382014-01-21 13:07:01 +0100548 ret = ttm_bo_wait(bo, false, false, true);
549 WARN_ON(ret);
550 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000551
552 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100553 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000554 spin_unlock(&glob->lru_lock);
555 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200556 }
557
558 put_count = ttm_bo_del_from_lru(bo);
559 list_del_init(&bo->ddestroy);
560 ++put_count;
561
562 spin_unlock(&glob->lru_lock);
563 ttm_bo_cleanup_memtype_use(bo);
564
Dave Airlied6ea8882010-11-22 13:24:40 +1000565 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200566
567 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200568}
569
570/**
571 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
572 * encountered buffers.
573 */
574
575static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
576{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200577 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100578 struct ttm_buffer_object *entry = NULL;
579 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200580
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200581 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100582 if (list_empty(&bdev->ddestroy))
583 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200584
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100585 entry = list_first_entry(&bdev->ddestroy,
586 struct ttm_buffer_object, ddestroy);
587 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200588
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100589 for (;;) {
590 struct ttm_buffer_object *nentry = NULL;
591
592 if (entry->ddestroy.next != &bdev->ddestroy) {
593 nentry = list_first_entry(&entry->ddestroy,
594 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200595 kref_get(&nentry->list_kref);
596 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200597
Christian Königdfd5e502016-04-06 11:12:03 +0200598 ret = __ttm_bo_reserve(entry, false, true, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100599 if (remove_all && ret) {
600 spin_unlock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200601 ret = __ttm_bo_reserve(entry, false, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100602 spin_lock(&glob->lru_lock);
603 }
604
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000605 if (!ret)
606 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
607 !remove_all);
608 else
609 spin_unlock(&glob->lru_lock);
610
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200611 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100612 entry = nentry;
613
614 if (ret || !entry)
615 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200617 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100618 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200619 break;
620 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200621
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100622out_unlock:
623 spin_unlock(&glob->lru_lock);
624out:
625 if (entry)
626 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200627 return ret;
628}
629
630static void ttm_bo_delayed_workqueue(struct work_struct *work)
631{
632 struct ttm_bo_device *bdev =
633 container_of(work, struct ttm_bo_device, wq.work);
634
635 if (ttm_bo_delayed_delete(bdev, false)) {
636 schedule_delayed_work(&bdev->wq,
637 ((HZ / 100) < 1) ? 1 : HZ / 100);
638 }
639}
640
641static void ttm_bo_release(struct kref *kref)
642{
643 struct ttm_buffer_object *bo =
644 container_of(kref, struct ttm_buffer_object, kref);
645 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100646 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200647
David Herrmann72525b32013-07-24 21:08:53 +0200648 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100649 ttm_mem_io_lock(man, false);
650 ttm_mem_io_free_vm(bo);
651 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200652 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200653 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200654}
655
656void ttm_bo_unref(struct ttm_buffer_object **p_bo)
657{
658 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200659
660 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200661 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200662}
663EXPORT_SYMBOL(ttm_bo_unref);
664
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400665int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
666{
667 return cancel_delayed_work_sync(&bdev->wq);
668}
669EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
670
671void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
672{
673 if (resched)
674 schedule_delayed_work(&bdev->wq,
675 ((HZ / 100) < 1) ? 1 : HZ / 100);
676}
677EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
678
Jerome Glisseca262a9992009-12-08 15:33:32 +0100679static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000680 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200681{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200682 struct ttm_bo_device *bdev = bo->bdev;
683 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100684 struct ttm_placement placement;
685 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200686
Dave Airlie1717c0e2011-10-27 18:28:37 +0200687 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200688
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200689 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100690 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700691 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200692 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200693 goto out;
694 }
695
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200696 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200697
698 evict_mem = bo->mem;
699 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100700 evict_mem.bus.io_reserved_vm = false;
701 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200702
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100703 placement.num_placement = 0;
704 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100705 bdev->driver->evict_flags(bo, &placement);
706 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000707 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200708 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100709 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700710 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
711 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100712 ttm_bo_mem_space_debug(bo, &placement);
713 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200714 goto out;
715 }
716
717 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000718 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200719 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100720 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700721 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000722 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200723 goto out;
724 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200725 bo->evicted = true;
726out:
727 return ret;
728}
729
Jerome Glisseca262a9992009-12-08 15:33:32 +0100730static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
731 uint32_t mem_type,
Michel Dänzere3001802014-10-09 15:02:59 +0900732 const struct ttm_place *place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000733 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000734 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100735{
736 struct ttm_bo_global *glob = bdev->glob;
737 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
738 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000739 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100740
741 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000742 list_for_each_entry(bo, &man->lru, lru) {
Christian Königdfd5e502016-04-06 11:12:03 +0200743 ret = __ttm_bo_reserve(bo, false, true, NULL);
Michel Dänzere3001802014-10-09 15:02:59 +0900744 if (!ret) {
745 if (place && (place->fpfn || place->lpfn)) {
746 /* Don't evict this BO if it's outside of the
747 * requested placement range
748 */
749 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
750 (place->lpfn && place->lpfn <= bo->mem.start)) {
751 __ttm_bo_unreserve(bo);
752 ret = -EBUSY;
753 continue;
754 }
755 }
756
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000757 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900758 }
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100759 }
760
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000761 if (ret) {
762 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000763 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200764 }
765
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000766 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100767
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000768 if (!list_empty(&bo->ddestroy)) {
769 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
770 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100771 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000772 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100773 }
774
775 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100776 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100777
778 BUG_ON(ret != 0);
779
Dave Airlied6ea8882010-11-22 13:24:40 +1000780 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100781
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000782 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100783 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100784
Jerome Glisseca262a9992009-12-08 15:33:32 +0100785 kref_put(&bo->list_kref, ttm_bo_release_list);
786 return ret;
787}
788
Ben Skeggs42311ff2010-08-04 12:07:08 +1000789void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
790{
Ben Skeggsd961db72010-08-05 10:48:18 +1000791 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000792
Ben Skeggsd961db72010-08-05 10:48:18 +1000793 if (mem->mm_node)
794 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000795}
796EXPORT_SYMBOL(ttm_bo_mem_put);
797
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200798/**
799 * Repeatedly evict memory from the LRU for @mem_type until we create enough
800 * space, or we've evicted everything and there isn't enough space.
801 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100802static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
803 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200804 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100805 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000806 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000807 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200808{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100809 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200810 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200811 int ret;
812
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200813 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200814 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200815 if (unlikely(ret != 0))
816 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000817 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100818 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900819 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000820 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100821 if (unlikely(ret != 0))
822 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200823 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000824 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200825 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200826 mem->mem_type = mem_type;
827 return 0;
828}
829
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200830static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
831 uint32_t cur_placement,
832 uint32_t proposed_placement)
833{
834 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
835 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
836
837 /**
838 * Keep current caching if possible.
839 */
840
841 if ((cur_placement & caching) != 0)
842 result |= (cur_placement & caching);
843 else if ((man->default_caching & caching) != 0)
844 result |= man->default_caching;
845 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
846 result |= TTM_PL_FLAG_CACHED;
847 else if ((TTM_PL_FLAG_WC & caching) != 0)
848 result |= TTM_PL_FLAG_WC;
849 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
850 result |= TTM_PL_FLAG_UNCACHED;
851
852 return result;
853}
854
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200855static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200856 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200857 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200858 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200859{
860 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
861
Christian Königf1217ed2014-08-27 13:16:04 +0200862 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200863 return false;
864
Christian Königf1217ed2014-08-27 13:16:04 +0200865 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200866 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867
Christian Königf1217ed2014-08-27 13:16:04 +0200868 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200869
870 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200871 return true;
872}
873
874/**
875 * Creates space for memory region @mem according to its type.
876 *
877 * This function first searches for free space in compatible memory types in
878 * the priority order defined by the driver. If free space isn't found, then
879 * ttm_bo_mem_force_space is attempted in priority order to evict and find
880 * space.
881 */
882int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100883 struct ttm_placement *placement,
884 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000885 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000886 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200887{
888 struct ttm_bo_device *bdev = bo->bdev;
889 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200890 uint32_t mem_type = TTM_PL_SYSTEM;
891 uint32_t cur_flags = 0;
892 bool type_found = false;
893 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100894 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100895 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200896
897 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000898 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200899 const struct ttm_place *place = &placement->placement[i];
900
901 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100902 if (ret)
903 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200904 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700905 if (!man->has_type || !man->use_type)
906 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200907
Christian Königf1217ed2014-08-27 13:16:04 +0200908 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100909 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200910
911 if (!type_ok)
912 continue;
913
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700914 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200915 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
916 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100917 /*
918 * Use the access and other non-mapping-related flag bits from
919 * the memory placement flags to the current flags
920 */
Christian Königf1217ed2014-08-27 13:16:04 +0200921 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100922 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200923
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200924 if (mem_type == TTM_PL_SYSTEM)
925 break;
926
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700927 ret = (*man->func->get_node)(man, bo, place, mem);
928 if (unlikely(ret))
929 return ret;
930
Ben Skeggsd961db72010-08-05 10:48:18 +1000931 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200932 break;
933 }
934
Ben Skeggsd961db72010-08-05 10:48:18 +1000935 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200936 mem->mem_type = mem_type;
937 mem->placement = cur_flags;
938 return 0;
939 }
940
Dave Airlieb6637522009-12-14 14:51:35 +1000941 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200942 const struct ttm_place *place = &placement->busy_placement[i];
943
944 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100945 if (ret)
946 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200947 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700948 if (!man->has_type || !man->use_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200949 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200950 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951 continue;
952
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700953 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200954 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
955 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100956 /*
957 * Use the access and other non-mapping-related flag bits from
958 * the memory placement flags to the current flags
959 */
Christian Königf1217ed2014-08-27 13:16:04 +0200960 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100961 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200962
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100963 if (mem_type == TTM_PL_SYSTEM) {
964 mem->mem_type = mem_type;
965 mem->placement = cur_flags;
966 mem->mm_node = NULL;
967 return 0;
968 }
969
Christian Königf1217ed2014-08-27 13:16:04 +0200970 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000971 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200972 if (ret == 0 && mem->mm_node) {
973 mem->placement = cur_flags;
974 return 0;
975 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100976 if (ret == -ERESTARTSYS)
977 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200978 }
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700979
980 if (!type_found) {
981 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
982 return -EINVAL;
983 }
984
985 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200986}
987EXPORT_SYMBOL(ttm_bo_mem_space);
988
Rashika Kheria6e87fa42014-01-06 22:12:58 +0530989static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100990 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000991 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000992 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200993{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200994 int ret = 0;
995 struct ttm_mem_reg mem;
996
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200997 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200998
999 /*
Christian König5ee7b412016-04-06 11:12:02 +02001000 * Don't wait for the BO on initial allocation. This is important when
1001 * the BO has an imported reservation object.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001002 */
Christian König5ee7b412016-04-06 11:12:02 +02001003 if (bo->mem.mem_type != TTM_PL_SYSTEM || bo->ttm != NULL) {
1004 /*
1005 * FIXME: It's possible to pipeline buffer moves.
1006 * Have the driver move function wait for idle when necessary,
1007 * instead of doing it here.
1008 */
1009 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
1010 if (ret)
1011 return ret;
1012 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 mem.num_pages = bo->num_pages;
1014 mem.size = mem.num_pages << PAGE_SHIFT;
1015 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001016 mem.bus.io_reserved_vm = false;
1017 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001018 /*
1019 * Determine where to move the buffer.
1020 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001021 ret = ttm_bo_mem_space(bo, placement, &mem,
1022 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001023 if (ret)
1024 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001025 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1026 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001027out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001028 if (ret && mem.mm_node)
1029 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001030 return ret;
1031}
1032
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001033static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1034 struct ttm_mem_reg *mem,
1035 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001037 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001038
Jerome Glisseca262a9992009-12-08 15:33:32 +01001039 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001040 const struct ttm_place *heap = &placement->placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001041 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001042 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001043 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001044 continue;
1045
1046 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001047 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1048 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1049 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001050 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001051
1052 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001053 const struct ttm_place *heap = &placement->busy_placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001054 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001055 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001056 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001057 continue;
1058
1059 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001060 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1061 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1062 return true;
1063 }
1064
1065 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001066}
1067
Jerome Glisse09855ac2009-12-10 17:16:27 +01001068int ttm_bo_validate(struct ttm_buffer_object *bo,
1069 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001070 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001071 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001072{
1073 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001074 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001075
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001076 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077 /*
1078 * Check whether we need to move buffer.
1079 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001080 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001081 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1082 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001083 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001084 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001085 } else {
1086 /*
1087 * Use the access and other non-mapping-related flag bits from
1088 * the compatible memory placement flags to the active flags
1089 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001090 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001091 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093 /*
1094 * We might need to add a TTM.
1095 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001096 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1097 ret = ttm_bo_add_ttm(bo, true);
1098 if (ret)
1099 return ret;
1100 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101 return 0;
1102}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001103EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001104
Jerome Glisse09855ac2009-12-10 17:16:27 +01001105int ttm_bo_init(struct ttm_bo_device *bdev,
1106 struct ttm_buffer_object *bo,
1107 unsigned long size,
1108 enum ttm_bo_type type,
1109 struct ttm_placement *placement,
1110 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001111 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001112 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001113 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001114 struct sg_table *sg,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001115 struct reservation_object *resv,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001116 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001118 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001119 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001120 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001121 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001122
1123 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1124 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001125 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001126 if (destroy)
1127 (*destroy)(bo);
1128 else
1129 kfree(bo);
1130 return -ENOMEM;
1131 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001132
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001133 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1134 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001135 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001136 if (destroy)
1137 (*destroy)(bo);
1138 else
1139 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001140 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001141 return -EINVAL;
1142 }
1143 bo->destroy = destroy;
1144
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001145 kref_init(&bo->kref);
1146 kref_init(&bo->list_kref);
1147 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001148 INIT_LIST_HEAD(&bo->lru);
1149 INIT_LIST_HEAD(&bo->ddestroy);
1150 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001151 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001152 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001154 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001155 bo->type = type;
1156 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001157 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158 bo->mem.mem_type = TTM_PL_SYSTEM;
1159 bo->mem.num_pages = bo->num_pages;
1160 bo->mem.mm_node = NULL;
1161 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001162 bo->mem.bus.io_reserved_vm = false;
1163 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001164 bo->priv_flags = 0;
1165 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001166 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001167 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001168 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001169 if (resv) {
1170 bo->resv = resv;
1171 lockdep_assert_held(&bo->resv->lock.base);
1172 } else {
1173 bo->resv = &bo->ttm_resv;
1174 reservation_object_init(&bo->ttm_resv);
1175 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001176 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001177 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001180 * For ttm_bo_type_device buffers, allocate
1181 * address space from the device.
1182 */
Christian Königf1217ed2014-08-27 13:16:04 +02001183 if (bo->type == ttm_bo_type_device ||
1184 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001185 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1186 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001188 /* passed reservation objects should already be locked,
1189 * since otherwise lockdep will be angered in radeon.
1190 */
1191 if (!resv) {
1192 locked = ww_mutex_trylock(&bo->resv->lock);
1193 WARN_ON(!locked);
1194 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001195
1196 if (likely(!ret))
1197 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001198
Christian König33d48cf2016-01-11 15:35:18 +01001199 if (!resv) {
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001200 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001201
Christian König33d48cf2016-01-11 15:35:18 +01001202 } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1203 spin_lock(&bo->glob->lru_lock);
1204 ttm_bo_add_to_lru(bo);
1205 spin_unlock(&bo->glob->lru_lock);
1206 }
1207
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001208 if (unlikely(ret))
1209 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001210
1211 return ret;
1212}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001213EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001214
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001215size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1216 unsigned long bo_size,
1217 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001218{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001219 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1220 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001222 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001223 size += ttm_round_pot(npages * sizeof(void *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001224 size += ttm_round_pot(sizeof(struct ttm_tt));
1225 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001226}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001227EXPORT_SYMBOL(ttm_bo_acc_size);
1228
1229size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1230 unsigned long bo_size,
1231 unsigned struct_size)
1232{
1233 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1234 size_t size = 0;
1235
1236 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001237 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001238 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1239 return size;
1240}
1241EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001242
Jerome Glisse09855ac2009-12-10 17:16:27 +01001243int ttm_bo_create(struct ttm_bo_device *bdev,
1244 unsigned long size,
1245 enum ttm_bo_type type,
1246 struct ttm_placement *placement,
1247 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001248 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001249 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001250 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251{
1252 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001253 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001254 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001255
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001257 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001258 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001259
Thomas Hellstroma393c732012-06-12 13:28:42 +02001260 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001261 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001262 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001263 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001264 if (likely(ret == 0))
1265 *p_bo = bo;
1266
1267 return ret;
1268}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001269EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001270
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001272 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001273{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001274 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001275 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001276 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001277
1278 /*
1279 * Can't use standard list traversal since we're unlocking.
1280 */
1281
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001282 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001283 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001284 spin_unlock(&glob->lru_lock);
Michel Dänzere3001802014-10-09 15:02:59 +09001285 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001286 if (ret) {
1287 if (allow_errors) {
1288 return ret;
1289 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001290 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001291 }
1292 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001293 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001294 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001295 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001296 return 0;
1297}
1298
1299int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1300{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001301 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302 int ret = -EINVAL;
1303
1304 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001305 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306 return ret;
1307 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001308 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001309
1310 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001311 pr_err("Trying to take down uninitialized memory manager type %u\n",
1312 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001313 return ret;
1314 }
1315
1316 man->use_type = false;
1317 man->has_type = false;
1318
1319 ret = 0;
1320 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001321 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001322
Ben Skeggsd961db72010-08-05 10:48:18 +10001323 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001324 }
1325
1326 return ret;
1327}
1328EXPORT_SYMBOL(ttm_bo_clean_mm);
1329
1330int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1331{
1332 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1333
1334 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001335 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001336 return -EINVAL;
1337 }
1338
1339 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001340 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001341 return 0;
1342 }
1343
Jerome Glisseca262a9992009-12-08 15:33:32 +01001344 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001345}
1346EXPORT_SYMBOL(ttm_bo_evict_mm);
1347
1348int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001349 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001350{
1351 int ret = -EINVAL;
1352 struct ttm_mem_type_manager *man;
1353
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001354 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001355 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001356 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001357 man->io_reserve_fastpath = true;
1358 man->use_io_reserve_lru = false;
1359 mutex_init(&man->io_reserve_mutex);
1360 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001361
1362 ret = bdev->driver->init_mem_type(bdev, type, man);
1363 if (ret)
1364 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001365 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001366
1367 ret = 0;
1368 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001369 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001370 if (ret)
1371 return ret;
1372 }
1373 man->has_type = true;
1374 man->use_type = true;
1375 man->size = p_size;
1376
1377 INIT_LIST_HEAD(&man->lru);
1378
1379 return 0;
1380}
1381EXPORT_SYMBOL(ttm_bo_init_mm);
1382
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001383static void ttm_bo_global_kobj_release(struct kobject *kobj)
1384{
1385 struct ttm_bo_global *glob =
1386 container_of(kobj, struct ttm_bo_global, kobj);
1387
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001388 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1389 __free_page(glob->dummy_read_page);
1390 kfree(glob);
1391}
1392
Dave Airlieba4420c2010-03-09 10:56:52 +10001393void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001394{
1395 struct ttm_bo_global *glob = ref->object;
1396
1397 kobject_del(&glob->kobj);
1398 kobject_put(&glob->kobj);
1399}
1400EXPORT_SYMBOL(ttm_bo_global_release);
1401
Dave Airlieba4420c2010-03-09 10:56:52 +10001402int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001403{
1404 struct ttm_bo_global_ref *bo_ref =
1405 container_of(ref, struct ttm_bo_global_ref, ref);
1406 struct ttm_bo_global *glob = ref->object;
1407 int ret;
1408
1409 mutex_init(&glob->device_list_mutex);
1410 spin_lock_init(&glob->lru_lock);
1411 glob->mem_glob = bo_ref->mem_glob;
1412 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1413
1414 if (unlikely(glob->dummy_read_page == NULL)) {
1415 ret = -ENOMEM;
1416 goto out_no_drp;
1417 }
1418
1419 INIT_LIST_HEAD(&glob->swap_lru);
1420 INIT_LIST_HEAD(&glob->device_list);
1421
1422 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1423 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1424 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001425 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001426 goto out_no_shrink;
1427 }
1428
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001429 atomic_set(&glob->bo_count, 0);
1430
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001431 ret = kobject_init_and_add(
1432 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001433 if (unlikely(ret != 0))
1434 kobject_put(&glob->kobj);
1435 return ret;
1436out_no_shrink:
1437 __free_page(glob->dummy_read_page);
1438out_no_drp:
1439 kfree(glob);
1440 return ret;
1441}
1442EXPORT_SYMBOL(ttm_bo_global_init);
1443
1444
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001445int ttm_bo_device_release(struct ttm_bo_device *bdev)
1446{
1447 int ret = 0;
1448 unsigned i = TTM_NUM_MEM_TYPES;
1449 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001450 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001451
1452 while (i--) {
1453 man = &bdev->man[i];
1454 if (man->has_type) {
1455 man->use_type = false;
1456 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1457 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001458 pr_err("DRM memory manager type %d is not clean\n",
1459 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001460 }
1461 man->has_type = false;
1462 }
1463 }
1464
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001465 mutex_lock(&glob->device_list_mutex);
1466 list_del(&bdev->device_list);
1467 mutex_unlock(&glob->device_list_mutex);
1468
Tejun Heof094cfc2010-12-24 15:59:06 +01001469 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001470
1471 while (ttm_bo_delayed_delete(bdev, true))
1472 ;
1473
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001474 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001475 if (list_empty(&bdev->ddestroy))
1476 TTM_DEBUG("Delayed destroy list was clean\n");
1477
1478 if (list_empty(&bdev->man[0].lru))
1479 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001480 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001481
David Herrmann72525b32013-07-24 21:08:53 +02001482 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001483
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001484 return ret;
1485}
1486EXPORT_SYMBOL(ttm_bo_device_release);
1487
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001489 struct ttm_bo_global *glob,
1490 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001491 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001492 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001493 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001494{
1495 int ret = -EINVAL;
1496
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001498
1499 memset(bdev->man, 0, sizeof(bdev->man));
1500
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001501 /*
1502 * Initialize the system memory buffer type.
1503 * Other types need to be driver / IOCTL initialized.
1504 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001505 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001506 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001507 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001508
David Herrmann72525b32013-07-24 21:08:53 +02001509 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1510 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001511 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001512 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001513 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001514 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001515 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001516 bdev->val_seq = 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001517 mutex_lock(&glob->device_list_mutex);
1518 list_add_tail(&bdev->device_list, &glob->device_list);
1519 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001520
1521 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001522out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001523 return ret;
1524}
1525EXPORT_SYMBOL(ttm_bo_device_init);
1526
1527/*
1528 * buffer object vm functions.
1529 */
1530
1531bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1532{
1533 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1534
1535 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1536 if (mem->mem_type == TTM_PL_SYSTEM)
1537 return false;
1538
1539 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1540 return false;
1541
1542 if (mem->placement & TTM_PL_FLAG_CACHED)
1543 return false;
1544 }
1545 return true;
1546}
1547
Thomas Hellstromeba67092010-11-11 09:41:57 +01001548void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001549{
1550 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001551
David Herrmann51335df2013-07-24 21:10:03 +02001552 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001553 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001554}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001555
1556void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1557{
1558 struct ttm_bo_device *bdev = bo->bdev;
1559 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1560
1561 ttm_mem_io_lock(man, false);
1562 ttm_bo_unmap_virtual_locked(bo);
1563 ttm_mem_io_unlock(man);
1564}
1565
1566
Dave Airliee024e112009-06-24 09:48:08 +10001567EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001568
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001569int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001570 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001571{
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001572 struct reservation_object_list *fobj;
1573 struct reservation_object *resv;
1574 struct fence *excl;
1575 long timeout = 15 * HZ;
1576 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001577
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001578 resv = bo->resv;
1579 fobj = reservation_object_get_list(resv);
1580 excl = reservation_object_get_excl(resv);
1581 if (excl) {
1582 if (!fence_is_signaled(excl)) {
1583 if (no_wait)
1584 return -EBUSY;
Maarten Lankhorst70401382014-01-21 13:07:01 +01001585
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001586 timeout = fence_wait_timeout(excl,
1587 interruptible, timeout);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001588 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001589 }
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001590
1591 for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
1592 struct fence *fence;
1593 fence = rcu_dereference_protected(fobj->shared[i],
1594 reservation_object_held(resv));
1595
1596 if (!fence_is_signaled(fence)) {
1597 if (no_wait)
1598 return -EBUSY;
1599
1600 timeout = fence_wait_timeout(fence,
1601 interruptible, timeout);
1602 }
1603 }
1604
1605 if (timeout < 0)
1606 return timeout;
1607
1608 if (timeout == 0)
1609 return -EBUSY;
1610
1611 reservation_object_add_excl_fence(resv, NULL);
1612 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1613 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001614}
1615EXPORT_SYMBOL(ttm_bo_wait);
1616
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001617int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1618{
1619 int ret = 0;
1620
1621 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001622 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001623 */
1624
Christian Königdfd5e502016-04-06 11:12:03 +02001625 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001626 if (unlikely(ret != 0))
1627 return ret;
Dave Airlie1717c0e2011-10-27 18:28:37 +02001628 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001629 if (likely(ret == 0))
1630 atomic_inc(&bo->cpu_writers);
1631 ttm_bo_unreserve(bo);
1632 return ret;
1633}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001634EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001635
1636void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1637{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001638 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001639}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001640EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001641
1642/**
1643 * A buffer object shrink method that tries to swap out the first
1644 * buffer object on the bo_global::swap_lru list.
1645 */
1646
1647static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1648{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001649 struct ttm_bo_global *glob =
1650 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001651 struct ttm_buffer_object *bo;
1652 int ret = -EBUSY;
1653 int put_count;
1654 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1655
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001656 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001657 list_for_each_entry(bo, &glob->swap_lru, swap) {
Christian Königdfd5e502016-04-06 11:12:03 +02001658 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001659 if (!ret)
1660 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001661 }
1662
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001663 if (ret) {
1664 spin_unlock(&glob->lru_lock);
1665 return ret;
1666 }
1667
1668 kref_get(&bo->list_kref);
1669
1670 if (!list_empty(&bo->ddestroy)) {
1671 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1672 kref_put(&bo->list_kref, ttm_bo_release_list);
1673 return ret;
1674 }
1675
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001676 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001677 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001678
Dave Airlied6ea8882010-11-22 13:24:40 +10001679 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001680
1681 /**
1682 * Wait for GPU, then move to system cached.
1683 */
1684
Dave Airlie1717c0e2011-10-27 18:28:37 +02001685 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001686
1687 if (unlikely(ret != 0))
1688 goto out;
1689
1690 if ((bo->mem.placement & swap_placement) != swap_placement) {
1691 struct ttm_mem_reg evict_mem;
1692
1693 evict_mem = bo->mem;
1694 evict_mem.mm_node = NULL;
1695 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1696 evict_mem.mem_type = TTM_PL_SYSTEM;
1697
1698 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001699 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001700 if (unlikely(ret != 0))
1701 goto out;
1702 }
1703
1704 ttm_bo_unmap_virtual(bo);
1705
1706 /**
1707 * Swap out. Buffer will be swapped in again as soon as
1708 * anyone tries to access a ttm page.
1709 */
1710
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001711 if (bo->bdev->driver->swap_notify)
1712 bo->bdev->driver->swap_notify(bo);
1713
Jan Engelhardt5df23972011-04-04 01:25:18 +02001714 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001715out:
1716
1717 /**
1718 *
1719 * Unreserve without putting on LRU to avoid swapping out an
1720 * already swapped buffer.
1721 */
1722
Thomas Hellstromc7523082014-02-20 11:36:25 +01001723 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001724 kref_put(&bo->list_kref, ttm_bo_release_list);
1725 return ret;
1726}
1727
1728void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1729{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001730 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001731 ;
1732}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001733EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001734
1735/**
1736 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1737 * unreserved
1738 *
1739 * @bo: Pointer to buffer
1740 */
1741int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1742{
1743 int ret;
1744
1745 /*
1746 * In the absense of a wait_unlocked API,
1747 * Use the bo::wu_mutex to avoid triggering livelocks due to
1748 * concurrent use of this function. Note that this use of
1749 * bo::wu_mutex can go away if we change locking order to
1750 * mmap_sem -> bo::reserve.
1751 */
1752 ret = mutex_lock_interruptible(&bo->wu_mutex);
1753 if (unlikely(ret != 0))
1754 return -ERESTARTSYS;
1755 if (!ww_mutex_is_locked(&bo->resv->lock))
1756 goto out_unlock;
Christian Königdfd5e502016-04-06 11:12:03 +02001757 ret = __ttm_bo_reserve(bo, true, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001758 if (unlikely(ret != 0))
1759 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001760 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001761
1762out_unlock:
1763 mutex_unlock(&bo->wu_mutex);
1764 return ret;
1765}