blob: 309a72ee7fb4a1a00c11ca68d40625918a83b817 [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{
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;
233 struct ttm_mem_type_manager *man;
234
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
Christian Königab749612016-01-11 15:35:20 +0100240 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
241 list_del_init(&bo->swap);
242 list_del_init(&bo->lru);
243
244 } else {
245 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG))
246 list_move_tail(&bo->swap, &bo->glob->swap_lru);
247
248 man = &bdev->man[bo->mem.mem_type];
249 list_move_tail(&bo->lru, &man->lru);
250 }
251}
252EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
253
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254/*
255 * Call bo->mutex locked.
256 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200257static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
258{
259 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200260 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200261 int ret = 0;
262 uint32_t page_flags = 0;
263
264 TTM_ASSERT_LOCKED(&bo->mutex);
265 bo->ttm = NULL;
266
Dave Airliead49f502009-07-10 22:36:26 +1000267 if (bdev->need_dma32)
268 page_flags |= TTM_PAGE_FLAG_DMA32;
269
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200270 switch (bo->type) {
271 case ttm_bo_type_device:
272 if (zero_alloc)
273 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
274 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400275 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
276 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200277 if (unlikely(bo->ttm == NULL))
278 ret = -ENOMEM;
279 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100280 case ttm_bo_type_sg:
281 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
282 page_flags | TTM_PAGE_FLAG_SG,
283 glob->dummy_read_page);
284 if (unlikely(bo->ttm == NULL)) {
285 ret = -ENOMEM;
286 break;
287 }
288 bo->ttm->sg = bo->sg;
289 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200290 default:
Joe Perches25d04792012-03-16 21:43:50 -0700291 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200292 ret = -EINVAL;
293 break;
294 }
295
296 return ret;
297}
298
299static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
300 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000301 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000302 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200303{
304 struct ttm_bo_device *bdev = bo->bdev;
305 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
306 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
307 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
308 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
309 int ret = 0;
310
311 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100312 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
313 ret = ttm_mem_io_lock(old_man, true);
314 if (unlikely(ret != 0))
315 goto out_err;
316 ttm_bo_unmap_virtual_locked(bo);
317 ttm_mem_io_unlock(old_man);
318 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200319
320 /*
321 * Create and bind a ttm if required.
322 */
323
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000324 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
325 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000326 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
327 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000328 if (ret)
329 goto out_err;
330 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200331
332 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
333 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200334 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200335
336 if (mem->mem_type != TTM_PL_SYSTEM) {
337 ret = ttm_tt_bind(bo->ttm, mem);
338 if (ret)
339 goto out_err;
340 }
341
342 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000343 if (bdev->driver->move_notify)
344 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100345 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200346 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200347 goto moved;
348 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200349 }
350
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000351 if (bdev->driver->move_notify)
352 bdev->driver->move_notify(bo, mem);
353
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200354 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
355 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000356 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200357 else if (bdev->driver->move)
358 ret = bdev->driver->move(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000359 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360 else
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000361 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200362
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000363 if (ret) {
364 if (bdev->driver->move_notify) {
365 struct ttm_mem_reg tmp_mem = *mem;
366 *mem = bo->mem;
367 bo->mem = tmp_mem;
368 bdev->driver->move_notify(bo, mem);
369 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000370 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000371 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200372
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000373 goto out_err;
374 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500375
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200376moved:
377 if (bo->evicted) {
Rob Clark9ef75062014-03-12 10:59:37 -0400378 if (bdev->driver->invalidate_caches) {
379 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
380 if (ret)
381 pr_err("Can not flush read caches\n");
382 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200383 bo->evicted = false;
384 }
385
386 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000387 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200388 bdev->man[bo->mem.mem_type].gpu_offset;
389 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100390 } else
391 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200392
393 return 0;
394
395out_err:
396 new_man = &bdev->man[bo->mem.mem_type];
397 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
398 ttm_tt_unbind(bo->ttm);
399 ttm_tt_destroy(bo->ttm);
400 bo->ttm = NULL;
401 }
402
403 return ret;
404}
405
406/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200407 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200408 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200409 * This is the place to put in driver specific hooks to release
410 * driver private resources.
411 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200412 */
413
414static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
415{
Jerome Glissedc97b342011-11-18 11:47:03 -0500416 if (bo->bdev->driver->move_notify)
417 bo->bdev->driver->move_notify(bo, NULL);
418
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200419 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200420 ttm_tt_unbind(bo->ttm);
421 ttm_tt_destroy(bo->ttm);
422 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200423 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200424 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200425
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200426 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200427}
428
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200429static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
430{
431 struct reservation_object_list *fobj;
432 struct fence *fence;
433 int i;
434
435 fobj = reservation_object_get_list(bo->resv);
436 fence = reservation_object_get_excl(bo->resv);
437 if (fence && !fence->ops->signaled)
438 fence_enable_sw_signaling(fence);
439
440 for (i = 0; fobj && i < fobj->shared_count; ++i) {
441 fence = rcu_dereference_protected(fobj->shared[i],
442 reservation_object_held(bo->resv));
443
444 if (!fence->ops->signaled)
445 fence_enable_sw_signaling(fence);
446 }
447}
448
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200449static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200450{
451 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200452 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200453 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200454 int ret;
455
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100456 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200457 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100458
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700459 if (!ret) {
Christian König8aa6d4f2016-04-06 11:12:04 +0200460 if (!ttm_bo_wait(bo, false, true)) {
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100461 put_count = ttm_bo_del_from_lru(bo);
462
463 spin_unlock(&glob->lru_lock);
464 ttm_bo_cleanup_memtype_use(bo);
465
466 ttm_bo_list_ref_sub(bo, put_count, true);
467
468 return;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200469 } else
470 ttm_bo_flush_all_fences(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700471
472 /*
473 * Make NO_EVICT bos immediately available to
474 * shrinkers, now that they are queued for
475 * destruction.
476 */
477 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
478 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
479 ttm_bo_add_to_lru(bo);
480 }
481
Thomas Hellstromc7523082014-02-20 11:36:25 +0100482 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700483 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200484
485 kref_get(&bo->list_kref);
486 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
487 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200488
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200489 schedule_delayed_work(&bdev->wq,
490 ((HZ / 100) < 1) ? 1 : HZ / 100);
491}
492
493/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000494 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200495 * If bo idle, remove from delayed- and lru lists, and unref.
496 * If not idle, do nothing.
497 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000498 * Must be called with lru_lock and reservation held, this function
499 * will drop both before returning.
500 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200501 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200502 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
503 */
504
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000505static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
506 bool interruptible,
507 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200508{
509 struct ttm_bo_global *glob = bo->glob;
510 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000511 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200512
Christian König8aa6d4f2016-04-06 11:12:04 +0200513 ret = ttm_bo_wait(bo, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200514
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000515 if (ret && !no_wait_gpu) {
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200516 long lret;
517 ww_mutex_unlock(&bo->resv->lock);
518 spin_unlock(&glob->lru_lock);
519
520 lret = reservation_object_wait_timeout_rcu(bo->resv,
521 true,
522 interruptible,
523 30 * HZ);
524
525 if (lret < 0)
526 return lret;
527 else if (lret == 0)
528 return -EBUSY;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000529
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000530 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200531 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000532
533 /*
534 * We raced, and lost, someone else holds the reservation now,
535 * and is probably busy in ttm_bo_cleanup_memtype_use.
536 *
537 * Even if it's not the case, because we finished waiting any
538 * delayed destruction would succeed, so just return success
539 * here.
540 */
541 if (ret) {
542 spin_unlock(&glob->lru_lock);
543 return 0;
544 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100545
546 /*
547 * remove sync_obj with ttm_bo_wait, the wait should be
548 * finished, and no new wait object should have been added.
549 */
Christian König8aa6d4f2016-04-06 11:12:04 +0200550 ret = ttm_bo_wait(bo, false, true);
Maarten Lankhorst70401382014-01-21 13:07:01 +0100551 WARN_ON(ret);
552 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000553
554 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100555 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000556 spin_unlock(&glob->lru_lock);
557 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200558 }
559
560 put_count = ttm_bo_del_from_lru(bo);
561 list_del_init(&bo->ddestroy);
562 ++put_count;
563
564 spin_unlock(&glob->lru_lock);
565 ttm_bo_cleanup_memtype_use(bo);
566
Dave Airlied6ea8882010-11-22 13:24:40 +1000567 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200568
569 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200570}
571
572/**
573 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
574 * encountered buffers.
575 */
576
577static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
578{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200579 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100580 struct ttm_buffer_object *entry = NULL;
581 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200582
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200583 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100584 if (list_empty(&bdev->ddestroy))
585 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200586
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100587 entry = list_first_entry(&bdev->ddestroy,
588 struct ttm_buffer_object, ddestroy);
589 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200590
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100591 for (;;) {
592 struct ttm_buffer_object *nentry = NULL;
593
594 if (entry->ddestroy.next != &bdev->ddestroy) {
595 nentry = list_first_entry(&entry->ddestroy,
596 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200597 kref_get(&nentry->list_kref);
598 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200599
Christian Königdfd5e502016-04-06 11:12:03 +0200600 ret = __ttm_bo_reserve(entry, false, true, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100601 if (remove_all && ret) {
602 spin_unlock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200603 ret = __ttm_bo_reserve(entry, false, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100604 spin_lock(&glob->lru_lock);
605 }
606
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000607 if (!ret)
608 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
609 !remove_all);
610 else
611 spin_unlock(&glob->lru_lock);
612
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200613 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100614 entry = nentry;
615
616 if (ret || !entry)
617 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200618
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200619 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100620 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200621 break;
622 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200623
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100624out_unlock:
625 spin_unlock(&glob->lru_lock);
626out:
627 if (entry)
628 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200629 return ret;
630}
631
632static void ttm_bo_delayed_workqueue(struct work_struct *work)
633{
634 struct ttm_bo_device *bdev =
635 container_of(work, struct ttm_bo_device, wq.work);
636
637 if (ttm_bo_delayed_delete(bdev, false)) {
638 schedule_delayed_work(&bdev->wq,
639 ((HZ / 100) < 1) ? 1 : HZ / 100);
640 }
641}
642
643static void ttm_bo_release(struct kref *kref)
644{
645 struct ttm_buffer_object *bo =
646 container_of(kref, struct ttm_buffer_object, kref);
647 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100648 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649
David Herrmann72525b32013-07-24 21:08:53 +0200650 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100651 ttm_mem_io_lock(man, false);
652 ttm_mem_io_free_vm(bo);
653 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200654 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200655 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656}
657
658void ttm_bo_unref(struct ttm_buffer_object **p_bo)
659{
660 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200661
662 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200663 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200664}
665EXPORT_SYMBOL(ttm_bo_unref);
666
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400667int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
668{
669 return cancel_delayed_work_sync(&bdev->wq);
670}
671EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
672
673void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
674{
675 if (resched)
676 schedule_delayed_work(&bdev->wq,
677 ((HZ / 100) < 1) ? 1 : HZ / 100);
678}
679EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
680
Jerome Glisseca262a9992009-12-08 15:33:32 +0100681static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000682 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200683{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200684 struct ttm_bo_device *bdev = bo->bdev;
685 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100686 struct ttm_placement placement;
687 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200688
Christian König8aa6d4f2016-04-06 11:12:04 +0200689 ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200690
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200691 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100692 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700693 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200694 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200695 goto out;
696 }
697
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200698 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200699
700 evict_mem = bo->mem;
701 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100702 evict_mem.bus.io_reserved_vm = false;
703 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200704
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100705 placement.num_placement = 0;
706 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100707 bdev->driver->evict_flags(bo, &placement);
708 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000709 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200710 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100711 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700712 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
713 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100714 ttm_bo_mem_space_debug(bo, &placement);
715 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200716 goto out;
717 }
718
719 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000720 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200721 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100722 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700723 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000724 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200725 goto out;
726 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200727 bo->evicted = true;
728out:
729 return ret;
730}
731
Jerome Glisseca262a9992009-12-08 15:33:32 +0100732static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
733 uint32_t mem_type,
Michel Dänzere3001802014-10-09 15:02:59 +0900734 const struct ttm_place *place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000735 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000736 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100737{
738 struct ttm_bo_global *glob = bdev->glob;
739 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
740 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000741 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100742
743 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000744 list_for_each_entry(bo, &man->lru, lru) {
Christian Königdfd5e502016-04-06 11:12:03 +0200745 ret = __ttm_bo_reserve(bo, false, true, NULL);
Michel Dänzere3001802014-10-09 15:02:59 +0900746 if (!ret) {
747 if (place && (place->fpfn || place->lpfn)) {
748 /* Don't evict this BO if it's outside of the
749 * requested placement range
750 */
751 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
752 (place->lpfn && place->lpfn <= bo->mem.start)) {
753 __ttm_bo_unreserve(bo);
754 ret = -EBUSY;
755 continue;
756 }
757 }
758
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000759 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900760 }
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100761 }
762
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000763 if (ret) {
764 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000765 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200766 }
767
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000768 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100769
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000770 if (!list_empty(&bo->ddestroy)) {
771 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
772 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100773 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000774 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100775 }
776
777 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100778 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100779
780 BUG_ON(ret != 0);
781
Dave Airlied6ea8882010-11-22 13:24:40 +1000782 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100783
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000784 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100785 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100786
Jerome Glisseca262a9992009-12-08 15:33:32 +0100787 kref_put(&bo->list_kref, ttm_bo_release_list);
788 return ret;
789}
790
Ben Skeggs42311ff2010-08-04 12:07:08 +1000791void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
792{
Ben Skeggsd961db72010-08-05 10:48:18 +1000793 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000794
Ben Skeggsd961db72010-08-05 10:48:18 +1000795 if (mem->mm_node)
796 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000797}
798EXPORT_SYMBOL(ttm_bo_mem_put);
799
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200800/**
801 * Repeatedly evict memory from the LRU for @mem_type until we create enough
802 * space, or we've evicted everything and there isn't enough space.
803 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100804static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
805 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200806 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100807 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000808 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000809 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200810{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100811 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200812 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200813 int ret;
814
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200815 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200816 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200817 if (unlikely(ret != 0))
818 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000819 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100820 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900821 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000822 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100823 if (unlikely(ret != 0))
824 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200825 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000826 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200827 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200828 mem->mem_type = mem_type;
829 return 0;
830}
831
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200832static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
833 uint32_t cur_placement,
834 uint32_t proposed_placement)
835{
836 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
837 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
838
839 /**
840 * Keep current caching if possible.
841 */
842
843 if ((cur_placement & caching) != 0)
844 result |= (cur_placement & caching);
845 else if ((man->default_caching & caching) != 0)
846 result |= man->default_caching;
847 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
848 result |= TTM_PL_FLAG_CACHED;
849 else if ((TTM_PL_FLAG_WC & caching) != 0)
850 result |= TTM_PL_FLAG_WC;
851 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
852 result |= TTM_PL_FLAG_UNCACHED;
853
854 return result;
855}
856
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200857static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200858 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200859 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200860 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200861{
862 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
863
Christian Königf1217ed2014-08-27 13:16:04 +0200864 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865 return false;
866
Christian Königf1217ed2014-08-27 13:16:04 +0200867 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200868 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200869
Christian Königf1217ed2014-08-27 13:16:04 +0200870 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200871
872 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200873 return true;
874}
875
876/**
877 * Creates space for memory region @mem according to its type.
878 *
879 * This function first searches for free space in compatible memory types in
880 * the priority order defined by the driver. If free space isn't found, then
881 * ttm_bo_mem_force_space is attempted in priority order to evict and find
882 * space.
883 */
884int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100885 struct ttm_placement *placement,
886 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000887 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000888 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200889{
890 struct ttm_bo_device *bdev = bo->bdev;
891 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200892 uint32_t mem_type = TTM_PL_SYSTEM;
893 uint32_t cur_flags = 0;
894 bool type_found = false;
895 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100896 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100897 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898
899 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000900 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200901 const struct ttm_place *place = &placement->placement[i];
902
903 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100904 if (ret)
905 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200906 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700907 if (!man->has_type || !man->use_type)
908 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200909
Christian Königf1217ed2014-08-27 13:16:04 +0200910 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100911 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200912
913 if (!type_ok)
914 continue;
915
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700916 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200917 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
918 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100919 /*
920 * Use the access and other non-mapping-related flag bits from
921 * the memory placement flags to the current flags
922 */
Christian Königf1217ed2014-08-27 13:16:04 +0200923 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100924 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200925
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926 if (mem_type == TTM_PL_SYSTEM)
927 break;
928
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700929 ret = (*man->func->get_node)(man, bo, place, mem);
930 if (unlikely(ret))
931 return ret;
932
Ben Skeggsd961db72010-08-05 10:48:18 +1000933 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200934 break;
935 }
936
Ben Skeggsd961db72010-08-05 10:48:18 +1000937 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938 mem->mem_type = mem_type;
939 mem->placement = cur_flags;
940 return 0;
941 }
942
Dave Airlieb6637522009-12-14 14:51:35 +1000943 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200944 const struct ttm_place *place = &placement->busy_placement[i];
945
946 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100947 if (ret)
948 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200949 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700950 if (!man->has_type || !man->use_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200952 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953 continue;
954
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700955 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200956 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
957 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100958 /*
959 * Use the access and other non-mapping-related flag bits from
960 * the memory placement flags to the current flags
961 */
Christian Königf1217ed2014-08-27 13:16:04 +0200962 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100963 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200964
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100965 if (mem_type == TTM_PL_SYSTEM) {
966 mem->mem_type = mem_type;
967 mem->placement = cur_flags;
968 mem->mm_node = NULL;
969 return 0;
970 }
971
Christian Königf1217ed2014-08-27 13:16:04 +0200972 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000973 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200974 if (ret == 0 && mem->mm_node) {
975 mem->placement = cur_flags;
976 return 0;
977 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100978 if (ret == -ERESTARTSYS)
979 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200980 }
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700981
982 if (!type_found) {
983 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
984 return -EINVAL;
985 }
986
987 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988}
989EXPORT_SYMBOL(ttm_bo_mem_space);
990
Rashika Kheria6e87fa42014-01-06 22:12:58 +0530991static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100992 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000993 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000994 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200995{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200996 int ret = 0;
997 struct ttm_mem_reg mem;
998
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200999 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001000
1001 /*
Christian König5ee7b412016-04-06 11:12:02 +02001002 * Don't wait for the BO on initial allocation. This is important when
1003 * the BO has an imported reservation object.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001004 */
Christian König5ee7b412016-04-06 11:12:02 +02001005 if (bo->mem.mem_type != TTM_PL_SYSTEM || bo->ttm != NULL) {
1006 /*
1007 * FIXME: It's possible to pipeline buffer moves.
1008 * Have the driver move function wait for idle when necessary,
1009 * instead of doing it here.
1010 */
Christian König8aa6d4f2016-04-06 11:12:04 +02001011 ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
Christian König5ee7b412016-04-06 11:12:02 +02001012 if (ret)
1013 return ret;
1014 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 mem.num_pages = bo->num_pages;
1016 mem.size = mem.num_pages << PAGE_SHIFT;
1017 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001018 mem.bus.io_reserved_vm = false;
1019 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001020 /*
1021 * Determine where to move the buffer.
1022 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001023 ret = ttm_bo_mem_space(bo, placement, &mem,
1024 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001025 if (ret)
1026 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001027 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1028 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001029out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001030 if (ret && mem.mm_node)
1031 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001032 return ret;
1033}
1034
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001035static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1036 struct ttm_mem_reg *mem,
1037 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001038{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001039 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001040
Jerome Glisseca262a9992009-12-08 15:33:32 +01001041 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001042 const struct ttm_place *heap = &placement->placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001043 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001044 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001045 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001046 continue;
1047
1048 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001049 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1050 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1051 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001052 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001053
1054 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001055 const struct ttm_place *heap = &placement->busy_placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001056 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001057 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001058 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001059 continue;
1060
1061 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001062 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1063 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1064 return true;
1065 }
1066
1067 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001068}
1069
Jerome Glisse09855ac2009-12-10 17:16:27 +01001070int ttm_bo_validate(struct ttm_buffer_object *bo,
1071 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001072 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001073 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074{
1075 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001076 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001078 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001079 /*
1080 * Check whether we need to move buffer.
1081 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001082 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001083 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1084 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001085 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001087 } else {
1088 /*
1089 * Use the access and other non-mapping-related flag bits from
1090 * the compatible memory placement flags to the active flags
1091 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001092 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001093 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001094 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001095 /*
1096 * We might need to add a TTM.
1097 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1099 ret = ttm_bo_add_ttm(bo, true);
1100 if (ret)
1101 return ret;
1102 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001103 return 0;
1104}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001105EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106
Jerome Glisse09855ac2009-12-10 17:16:27 +01001107int ttm_bo_init(struct ttm_bo_device *bdev,
1108 struct ttm_buffer_object *bo,
1109 unsigned long size,
1110 enum ttm_bo_type type,
1111 struct ttm_placement *placement,
1112 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001113 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001114 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001115 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001116 struct sg_table *sg,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001117 struct reservation_object *resv,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001118 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001119{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001120 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001121 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001122 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001123 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001124
1125 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1126 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001127 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001128 if (destroy)
1129 (*destroy)(bo);
1130 else
1131 kfree(bo);
1132 return -ENOMEM;
1133 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001134
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1136 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001137 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001138 if (destroy)
1139 (*destroy)(bo);
1140 else
1141 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001142 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143 return -EINVAL;
1144 }
1145 bo->destroy = destroy;
1146
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001147 kref_init(&bo->kref);
1148 kref_init(&bo->list_kref);
1149 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001150 INIT_LIST_HEAD(&bo->lru);
1151 INIT_LIST_HEAD(&bo->ddestroy);
1152 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001153 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001154 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001155 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001156 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001157 bo->type = type;
1158 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001159 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001160 bo->mem.mem_type = TTM_PL_SYSTEM;
1161 bo->mem.num_pages = bo->num_pages;
1162 bo->mem.mm_node = NULL;
1163 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001164 bo->mem.bus.io_reserved_vm = false;
1165 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001166 bo->priv_flags = 0;
1167 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001168 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001169 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001170 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001171 if (resv) {
1172 bo->resv = resv;
1173 lockdep_assert_held(&bo->resv->lock.base);
1174 } else {
1175 bo->resv = &bo->ttm_resv;
1176 reservation_object_init(&bo->ttm_resv);
1177 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001178 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001179 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001180
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001181 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001182 * For ttm_bo_type_device buffers, allocate
1183 * address space from the device.
1184 */
Christian Königf1217ed2014-08-27 13:16:04 +02001185 if (bo->type == ttm_bo_type_device ||
1186 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001187 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1188 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001189
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001190 /* passed reservation objects should already be locked,
1191 * since otherwise lockdep will be angered in radeon.
1192 */
1193 if (!resv) {
1194 locked = ww_mutex_trylock(&bo->resv->lock);
1195 WARN_ON(!locked);
1196 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001197
1198 if (likely(!ret))
1199 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200
Christian König33d48cf2016-01-11 15:35:18 +01001201 if (!resv) {
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001202 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203
Christian König33d48cf2016-01-11 15:35:18 +01001204 } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1205 spin_lock(&bo->glob->lru_lock);
1206 ttm_bo_add_to_lru(bo);
1207 spin_unlock(&bo->glob->lru_lock);
1208 }
1209
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001210 if (unlikely(ret))
1211 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212
1213 return ret;
1214}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001215EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001216
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001217size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1218 unsigned long bo_size,
1219 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001220{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001221 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1222 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001224 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001225 size += ttm_round_pot(npages * sizeof(void *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001226 size += ttm_round_pot(sizeof(struct ttm_tt));
1227 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001229EXPORT_SYMBOL(ttm_bo_acc_size);
1230
1231size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1232 unsigned long bo_size,
1233 unsigned struct_size)
1234{
1235 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1236 size_t size = 0;
1237
1238 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001239 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001240 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1241 return size;
1242}
1243EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001244
Jerome Glisse09855ac2009-12-10 17:16:27 +01001245int ttm_bo_create(struct ttm_bo_device *bdev,
1246 unsigned long size,
1247 enum ttm_bo_type type,
1248 struct ttm_placement *placement,
1249 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001250 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001251 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001252 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001253{
1254 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001255 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001256 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001257
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001258 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001259 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001260 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001261
Thomas Hellstroma393c732012-06-12 13:28:42 +02001262 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001263 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001264 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001265 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001266 if (likely(ret == 0))
1267 *p_bo = bo;
1268
1269 return ret;
1270}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001271EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001273static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001274 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001275{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001276 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001277 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001278 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001279
1280 /*
1281 * Can't use standard list traversal since we're unlocking.
1282 */
1283
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001284 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001285 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001286 spin_unlock(&glob->lru_lock);
Michel Dänzere3001802014-10-09 15:02:59 +09001287 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001288 if (ret) {
1289 if (allow_errors) {
1290 return ret;
1291 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001292 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001293 }
1294 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001295 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001296 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001297 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298 return 0;
1299}
1300
1301int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1302{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001303 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001304 int ret = -EINVAL;
1305
1306 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001307 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001308 return ret;
1309 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001310 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001311
1312 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001313 pr_err("Trying to take down uninitialized memory manager type %u\n",
1314 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001315 return ret;
1316 }
1317
1318 man->use_type = false;
1319 man->has_type = false;
1320
1321 ret = 0;
1322 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001323 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001324
Ben Skeggsd961db72010-08-05 10:48:18 +10001325 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001326 }
1327
1328 return ret;
1329}
1330EXPORT_SYMBOL(ttm_bo_clean_mm);
1331
1332int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1333{
1334 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1335
1336 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001337 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001338 return -EINVAL;
1339 }
1340
1341 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001342 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001343 return 0;
1344 }
1345
Jerome Glisseca262a9992009-12-08 15:33:32 +01001346 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001347}
1348EXPORT_SYMBOL(ttm_bo_evict_mm);
1349
1350int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001351 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001352{
1353 int ret = -EINVAL;
1354 struct ttm_mem_type_manager *man;
1355
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001356 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001357 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001358 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001359 man->io_reserve_fastpath = true;
1360 man->use_io_reserve_lru = false;
1361 mutex_init(&man->io_reserve_mutex);
1362 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001363
1364 ret = bdev->driver->init_mem_type(bdev, type, man);
1365 if (ret)
1366 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001367 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001368
1369 ret = 0;
1370 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001371 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001372 if (ret)
1373 return ret;
1374 }
1375 man->has_type = true;
1376 man->use_type = true;
1377 man->size = p_size;
1378
1379 INIT_LIST_HEAD(&man->lru);
1380
1381 return 0;
1382}
1383EXPORT_SYMBOL(ttm_bo_init_mm);
1384
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001385static void ttm_bo_global_kobj_release(struct kobject *kobj)
1386{
1387 struct ttm_bo_global *glob =
1388 container_of(kobj, struct ttm_bo_global, kobj);
1389
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001390 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1391 __free_page(glob->dummy_read_page);
1392 kfree(glob);
1393}
1394
Dave Airlieba4420c2010-03-09 10:56:52 +10001395void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001396{
1397 struct ttm_bo_global *glob = ref->object;
1398
1399 kobject_del(&glob->kobj);
1400 kobject_put(&glob->kobj);
1401}
1402EXPORT_SYMBOL(ttm_bo_global_release);
1403
Dave Airlieba4420c2010-03-09 10:56:52 +10001404int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001405{
1406 struct ttm_bo_global_ref *bo_ref =
1407 container_of(ref, struct ttm_bo_global_ref, ref);
1408 struct ttm_bo_global *glob = ref->object;
1409 int ret;
1410
1411 mutex_init(&glob->device_list_mutex);
1412 spin_lock_init(&glob->lru_lock);
1413 glob->mem_glob = bo_ref->mem_glob;
1414 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1415
1416 if (unlikely(glob->dummy_read_page == NULL)) {
1417 ret = -ENOMEM;
1418 goto out_no_drp;
1419 }
1420
1421 INIT_LIST_HEAD(&glob->swap_lru);
1422 INIT_LIST_HEAD(&glob->device_list);
1423
1424 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1425 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1426 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001427 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001428 goto out_no_shrink;
1429 }
1430
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001431 atomic_set(&glob->bo_count, 0);
1432
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001433 ret = kobject_init_and_add(
1434 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001435 if (unlikely(ret != 0))
1436 kobject_put(&glob->kobj);
1437 return ret;
1438out_no_shrink:
1439 __free_page(glob->dummy_read_page);
1440out_no_drp:
1441 kfree(glob);
1442 return ret;
1443}
1444EXPORT_SYMBOL(ttm_bo_global_init);
1445
1446
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001447int ttm_bo_device_release(struct ttm_bo_device *bdev)
1448{
1449 int ret = 0;
1450 unsigned i = TTM_NUM_MEM_TYPES;
1451 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001452 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001453
1454 while (i--) {
1455 man = &bdev->man[i];
1456 if (man->has_type) {
1457 man->use_type = false;
1458 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1459 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001460 pr_err("DRM memory manager type %d is not clean\n",
1461 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001462 }
1463 man->has_type = false;
1464 }
1465 }
1466
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001467 mutex_lock(&glob->device_list_mutex);
1468 list_del(&bdev->device_list);
1469 mutex_unlock(&glob->device_list_mutex);
1470
Tejun Heof094cfc2010-12-24 15:59:06 +01001471 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001472
1473 while (ttm_bo_delayed_delete(bdev, true))
1474 ;
1475
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001476 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001477 if (list_empty(&bdev->ddestroy))
1478 TTM_DEBUG("Delayed destroy list was clean\n");
1479
1480 if (list_empty(&bdev->man[0].lru))
1481 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001482 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001483
David Herrmann72525b32013-07-24 21:08:53 +02001484 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001485
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001486 return ret;
1487}
1488EXPORT_SYMBOL(ttm_bo_device_release);
1489
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001490int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001491 struct ttm_bo_global *glob,
1492 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001493 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001494 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001495 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001496{
1497 int ret = -EINVAL;
1498
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001499 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001500
1501 memset(bdev->man, 0, sizeof(bdev->man));
1502
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001503 /*
1504 * Initialize the system memory buffer type.
1505 * Other types need to be driver / IOCTL initialized.
1506 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001507 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001508 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001509 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510
David Herrmann72525b32013-07-24 21:08:53 +02001511 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1512 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001513 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001514 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001515 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001516 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001517 bdev->need_dma32 = need_dma32;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001518 mutex_lock(&glob->device_list_mutex);
1519 list_add_tail(&bdev->device_list, &glob->device_list);
1520 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001521
1522 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001523out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001524 return ret;
1525}
1526EXPORT_SYMBOL(ttm_bo_device_init);
1527
1528/*
1529 * buffer object vm functions.
1530 */
1531
1532bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1533{
1534 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1535
1536 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1537 if (mem->mem_type == TTM_PL_SYSTEM)
1538 return false;
1539
1540 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1541 return false;
1542
1543 if (mem->placement & TTM_PL_FLAG_CACHED)
1544 return false;
1545 }
1546 return true;
1547}
1548
Thomas Hellstromeba67092010-11-11 09:41:57 +01001549void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001550{
1551 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001552
David Herrmann51335df2013-07-24 21:10:03 +02001553 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001554 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001555}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001556
1557void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1558{
1559 struct ttm_bo_device *bdev = bo->bdev;
1560 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1561
1562 ttm_mem_io_lock(man, false);
1563 ttm_bo_unmap_virtual_locked(bo);
1564 ttm_mem_io_unlock(man);
1565}
1566
1567
Dave Airliee024e112009-06-24 09:48:08 +10001568EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001569
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001570int ttm_bo_wait(struct ttm_buffer_object *bo,
Christian König8aa6d4f2016-04-06 11:12:04 +02001571 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001572{
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001573 struct reservation_object_list *fobj;
1574 struct reservation_object *resv;
1575 struct fence *excl;
1576 long timeout = 15 * HZ;
1577 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001578
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001579 resv = bo->resv;
1580 fobj = reservation_object_get_list(resv);
1581 excl = reservation_object_get_excl(resv);
1582 if (excl) {
1583 if (!fence_is_signaled(excl)) {
1584 if (no_wait)
1585 return -EBUSY;
Maarten Lankhorst70401382014-01-21 13:07:01 +01001586
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001587 timeout = fence_wait_timeout(excl,
1588 interruptible, timeout);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001589 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001590 }
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001591
1592 for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
1593 struct fence *fence;
1594 fence = rcu_dereference_protected(fobj->shared[i],
1595 reservation_object_held(resv));
1596
1597 if (!fence_is_signaled(fence)) {
1598 if (no_wait)
1599 return -EBUSY;
1600
1601 timeout = fence_wait_timeout(fence,
1602 interruptible, timeout);
1603 }
1604 }
1605
1606 if (timeout < 0)
1607 return timeout;
1608
1609 if (timeout == 0)
1610 return -EBUSY;
1611
1612 reservation_object_add_excl_fence(resv, NULL);
1613 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1614 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001615}
1616EXPORT_SYMBOL(ttm_bo_wait);
1617
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001618int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1619{
1620 int ret = 0;
1621
1622 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001623 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001624 */
1625
Christian Königdfd5e502016-04-06 11:12:03 +02001626 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001627 if (unlikely(ret != 0))
1628 return ret;
Christian König8aa6d4f2016-04-06 11:12:04 +02001629 ret = ttm_bo_wait(bo, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001630 if (likely(ret == 0))
1631 atomic_inc(&bo->cpu_writers);
1632 ttm_bo_unreserve(bo);
1633 return ret;
1634}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001635EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001636
1637void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1638{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001639 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001640}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001641EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001642
1643/**
1644 * A buffer object shrink method that tries to swap out the first
1645 * buffer object on the bo_global::swap_lru list.
1646 */
1647
1648static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1649{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001650 struct ttm_bo_global *glob =
1651 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001652 struct ttm_buffer_object *bo;
1653 int ret = -EBUSY;
1654 int put_count;
1655 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1656
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001657 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001658 list_for_each_entry(bo, &glob->swap_lru, swap) {
Christian Königdfd5e502016-04-06 11:12:03 +02001659 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001660 if (!ret)
1661 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001662 }
1663
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001664 if (ret) {
1665 spin_unlock(&glob->lru_lock);
1666 return ret;
1667 }
1668
1669 kref_get(&bo->list_kref);
1670
1671 if (!list_empty(&bo->ddestroy)) {
1672 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1673 kref_put(&bo->list_kref, ttm_bo_release_list);
1674 return ret;
1675 }
1676
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001677 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001678 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001679
Dave Airlied6ea8882010-11-22 13:24:40 +10001680 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001681
1682 /**
1683 * Wait for GPU, then move to system cached.
1684 */
1685
Christian König8aa6d4f2016-04-06 11:12:04 +02001686 ret = ttm_bo_wait(bo, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001687
1688 if (unlikely(ret != 0))
1689 goto out;
1690
1691 if ((bo->mem.placement & swap_placement) != swap_placement) {
1692 struct ttm_mem_reg evict_mem;
1693
1694 evict_mem = bo->mem;
1695 evict_mem.mm_node = NULL;
1696 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1697 evict_mem.mem_type = TTM_PL_SYSTEM;
1698
1699 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001700 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001701 if (unlikely(ret != 0))
1702 goto out;
1703 }
1704
1705 ttm_bo_unmap_virtual(bo);
1706
1707 /**
1708 * Swap out. Buffer will be swapped in again as soon as
1709 * anyone tries to access a ttm page.
1710 */
1711
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001712 if (bo->bdev->driver->swap_notify)
1713 bo->bdev->driver->swap_notify(bo);
1714
Jan Engelhardt5df23972011-04-04 01:25:18 +02001715 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001716out:
1717
1718 /**
1719 *
1720 * Unreserve without putting on LRU to avoid swapping out an
1721 * already swapped buffer.
1722 */
1723
Thomas Hellstromc7523082014-02-20 11:36:25 +01001724 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001725 kref_put(&bo->list_kref, ttm_bo_release_list);
1726 return ret;
1727}
1728
1729void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1730{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001731 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001732 ;
1733}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001734EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001735
1736/**
1737 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1738 * unreserved
1739 *
1740 * @bo: Pointer to buffer
1741 */
1742int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1743{
1744 int ret;
1745
1746 /*
1747 * In the absense of a wait_unlocked API,
1748 * Use the bo::wu_mutex to avoid triggering livelocks due to
1749 * concurrent use of this function. Note that this use of
1750 * bo::wu_mutex can go away if we change locking order to
1751 * mmap_sem -> bo::reserve.
1752 */
1753 ret = mutex_lock_interruptible(&bo->wu_mutex);
1754 if (unlikely(ret != 0))
1755 return -ERESTARTSYS;
1756 if (!ww_mutex_is_locked(&bo->resv->lock))
1757 goto out_unlock;
Christian Königdfd5e502016-04-06 11:12:03 +02001758 ret = __ttm_bo_reserve(bo, true, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001759 if (unlikely(ret != 0))
1760 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001761 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001762
1763out_unlock:
1764 mutex_unlock(&bo->wu_mutex);
1765 return ret;
1766}