blob: 301e2371c34f5c260e95c9a1fc7b2122329497cf [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;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200167
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200168 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169
170 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
171
172 BUG_ON(!list_empty(&bo->lru));
173
Christian König98c28722016-04-06 11:12:07 +0200174 list_add(&bo->lru, bdev->driver->lru_tail(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200175 kref_get(&bo->list_kref);
176
Christian Königed704a42016-01-11 15:35:19 +0100177 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
Christian König98c28722016-04-06 11:12:07 +0200178 list_add(&bo->swap, bdev->driver->swap_lru_tail(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200179 kref_get(&bo->list_kref);
180 }
181 }
182}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200183EXPORT_SYMBOL(ttm_bo_add_to_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200184
Dave Airlied6ea8882010-11-22 13:24:40 +1000185int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200186{
Christian Königc3ea5762016-04-06 11:12:06 +0200187 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200188 int put_count = 0;
189
Christian Königc3ea5762016-04-06 11:12:06 +0200190 if (bdev->driver->lru_removal)
191 bdev->driver->lru_removal(bo);
192
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200193 if (!list_empty(&bo->swap)) {
194 list_del_init(&bo->swap);
195 ++put_count;
196 }
197 if (!list_empty(&bo->lru)) {
198 list_del_init(&bo->lru);
199 ++put_count;
200 }
201
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200202 return put_count;
203}
204
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200205static void ttm_bo_ref_bug(struct kref *list_kref)
206{
207 BUG();
208}
209
Dave Airlied6ea8882010-11-22 13:24:40 +1000210void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
211 bool never_free)
212{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100213 kref_sub(&bo->list_kref, count,
214 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000215}
216
Maarten Lankhorst34820322013-06-27 13:48:24 +0200217void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200218{
Maarten Lankhorst34820322013-06-27 13:48:24 +0200219 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200220
Maarten Lankhorst34820322013-06-27 13:48:24 +0200221 spin_lock(&bo->glob->lru_lock);
222 put_count = ttm_bo_del_from_lru(bo);
223 spin_unlock(&bo->glob->lru_lock);
224 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200225}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200226EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200227
Christian Königab749612016-01-11 15:35:20 +0100228void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
229{
230 struct ttm_bo_device *bdev = bo->bdev;
Christian Königab749612016-01-11 15:35:20 +0100231
232 lockdep_assert_held(&bo->resv->lock.base);
233
Christian Königc3ea5762016-04-06 11:12:06 +0200234 if (bdev->driver->lru_removal)
235 bdev->driver->lru_removal(bo);
236
Christian Königab749612016-01-11 15:35:20 +0100237 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
238 list_del_init(&bo->swap);
239 list_del_init(&bo->lru);
240
241 } else {
Christian König98c28722016-04-06 11:12:07 +0200242 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
243 list_del(&bo->swap);
244 list_add(&bo->swap, bdev->driver->swap_lru_tail(bo));
245 }
Christian Königab749612016-01-11 15:35:20 +0100246
Christian König98c28722016-04-06 11:12:07 +0200247 list_del(&bo->lru);
248 list_add(&bo->lru, bdev->driver->lru_tail(bo));
Christian Königab749612016-01-11 15:35:20 +0100249 }
250}
251EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
252
Christian König98c28722016-04-06 11:12:07 +0200253struct list_head *ttm_bo_default_lru_tail(struct ttm_buffer_object *bo)
254{
255 return bo->bdev->man[bo->mem.mem_type].lru.prev;
256}
257EXPORT_SYMBOL(ttm_bo_default_lru_tail);
258
259struct list_head *ttm_bo_default_swap_lru_tail(struct ttm_buffer_object *bo)
260{
261 return bo->glob->swap_lru.prev;
262}
263EXPORT_SYMBOL(ttm_bo_default_swap_lru_tail);
264
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200265/*
266 * Call bo->mutex locked.
267 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200268static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
269{
270 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200271 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200272 int ret = 0;
273 uint32_t page_flags = 0;
274
275 TTM_ASSERT_LOCKED(&bo->mutex);
276 bo->ttm = NULL;
277
Dave Airliead49f502009-07-10 22:36:26 +1000278 if (bdev->need_dma32)
279 page_flags |= TTM_PAGE_FLAG_DMA32;
280
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200281 switch (bo->type) {
282 case ttm_bo_type_device:
283 if (zero_alloc)
284 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
285 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400286 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
287 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200288 if (unlikely(bo->ttm == NULL))
289 ret = -ENOMEM;
290 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100291 case ttm_bo_type_sg:
292 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
293 page_flags | TTM_PAGE_FLAG_SG,
294 glob->dummy_read_page);
295 if (unlikely(bo->ttm == NULL)) {
296 ret = -ENOMEM;
297 break;
298 }
299 bo->ttm->sg = bo->sg;
300 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200301 default:
Joe Perches25d04792012-03-16 21:43:50 -0700302 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200303 ret = -EINVAL;
304 break;
305 }
306
307 return ret;
308}
309
310static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
311 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000312 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000313 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200314{
315 struct ttm_bo_device *bdev = bo->bdev;
316 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
317 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
318 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
319 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
320 int ret = 0;
321
322 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100323 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
324 ret = ttm_mem_io_lock(old_man, true);
325 if (unlikely(ret != 0))
326 goto out_err;
327 ttm_bo_unmap_virtual_locked(bo);
328 ttm_mem_io_unlock(old_man);
329 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200330
331 /*
332 * Create and bind a ttm if required.
333 */
334
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000335 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
336 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000337 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
338 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000339 if (ret)
340 goto out_err;
341 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200342
343 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
344 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200345 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200346
347 if (mem->mem_type != TTM_PL_SYSTEM) {
348 ret = ttm_tt_bind(bo->ttm, mem);
349 if (ret)
350 goto out_err;
351 }
352
353 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000354 if (bdev->driver->move_notify)
355 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100356 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200357 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200358 goto moved;
359 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360 }
361
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000362 if (bdev->driver->move_notify)
363 bdev->driver->move_notify(bo, mem);
364
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200365 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
366 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000367 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200368 else if (bdev->driver->move)
369 ret = bdev->driver->move(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000370 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200371 else
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000372 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200373
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000374 if (ret) {
375 if (bdev->driver->move_notify) {
376 struct ttm_mem_reg tmp_mem = *mem;
377 *mem = bo->mem;
378 bo->mem = tmp_mem;
379 bdev->driver->move_notify(bo, mem);
380 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000381 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000382 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200383
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000384 goto out_err;
385 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500386
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200387moved:
388 if (bo->evicted) {
Rob Clark9ef75062014-03-12 10:59:37 -0400389 if (bdev->driver->invalidate_caches) {
390 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
391 if (ret)
392 pr_err("Can not flush read caches\n");
393 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200394 bo->evicted = false;
395 }
396
397 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000398 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200399 bdev->man[bo->mem.mem_type].gpu_offset;
400 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100401 } else
402 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200403
404 return 0;
405
406out_err:
407 new_man = &bdev->man[bo->mem.mem_type];
408 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
409 ttm_tt_unbind(bo->ttm);
410 ttm_tt_destroy(bo->ttm);
411 bo->ttm = NULL;
412 }
413
414 return ret;
415}
416
417/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200418 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200419 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200420 * This is the place to put in driver specific hooks to release
421 * driver private resources.
422 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200423 */
424
425static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
426{
Jerome Glissedc97b342011-11-18 11:47:03 -0500427 if (bo->bdev->driver->move_notify)
428 bo->bdev->driver->move_notify(bo, NULL);
429
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200430 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200431 ttm_tt_unbind(bo->ttm);
432 ttm_tt_destroy(bo->ttm);
433 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200434 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200435 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200436
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200437 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200438}
439
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200440static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
441{
442 struct reservation_object_list *fobj;
443 struct fence *fence;
444 int i;
445
446 fobj = reservation_object_get_list(bo->resv);
447 fence = reservation_object_get_excl(bo->resv);
448 if (fence && !fence->ops->signaled)
449 fence_enable_sw_signaling(fence);
450
451 for (i = 0; fobj && i < fobj->shared_count; ++i) {
452 fence = rcu_dereference_protected(fobj->shared[i],
453 reservation_object_held(bo->resv));
454
455 if (!fence->ops->signaled)
456 fence_enable_sw_signaling(fence);
457 }
458}
459
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200460static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200461{
462 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200463 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200464 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200465 int ret;
466
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100467 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200468 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100469
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700470 if (!ret) {
Christian König8aa6d4f2016-04-06 11:12:04 +0200471 if (!ttm_bo_wait(bo, false, true)) {
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100472 put_count = ttm_bo_del_from_lru(bo);
473
474 spin_unlock(&glob->lru_lock);
475 ttm_bo_cleanup_memtype_use(bo);
476
477 ttm_bo_list_ref_sub(bo, put_count, true);
478
479 return;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200480 } else
481 ttm_bo_flush_all_fences(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700482
483 /*
484 * Make NO_EVICT bos immediately available to
485 * shrinkers, now that they are queued for
486 * destruction.
487 */
488 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
489 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
490 ttm_bo_add_to_lru(bo);
491 }
492
Thomas Hellstromc7523082014-02-20 11:36:25 +0100493 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700494 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200495
496 kref_get(&bo->list_kref);
497 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
498 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200499
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200500 schedule_delayed_work(&bdev->wq,
501 ((HZ / 100) < 1) ? 1 : HZ / 100);
502}
503
504/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000505 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200506 * If bo idle, remove from delayed- and lru lists, and unref.
507 * If not idle, do nothing.
508 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000509 * Must be called with lru_lock and reservation held, this function
510 * will drop both before returning.
511 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200512 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200513 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
514 */
515
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000516static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
517 bool interruptible,
518 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200519{
520 struct ttm_bo_global *glob = bo->glob;
521 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000522 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200523
Christian König8aa6d4f2016-04-06 11:12:04 +0200524 ret = ttm_bo_wait(bo, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200525
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000526 if (ret && !no_wait_gpu) {
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200527 long lret;
528 ww_mutex_unlock(&bo->resv->lock);
529 spin_unlock(&glob->lru_lock);
530
531 lret = reservation_object_wait_timeout_rcu(bo->resv,
532 true,
533 interruptible,
534 30 * HZ);
535
536 if (lret < 0)
537 return lret;
538 else if (lret == 0)
539 return -EBUSY;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000540
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000541 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200542 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000543
544 /*
545 * We raced, and lost, someone else holds the reservation now,
546 * and is probably busy in ttm_bo_cleanup_memtype_use.
547 *
548 * Even if it's not the case, because we finished waiting any
549 * delayed destruction would succeed, so just return success
550 * here.
551 */
552 if (ret) {
553 spin_unlock(&glob->lru_lock);
554 return 0;
555 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100556
557 /*
558 * remove sync_obj with ttm_bo_wait, the wait should be
559 * finished, and no new wait object should have been added.
560 */
Christian König8aa6d4f2016-04-06 11:12:04 +0200561 ret = ttm_bo_wait(bo, false, true);
Maarten Lankhorst70401382014-01-21 13:07:01 +0100562 WARN_ON(ret);
563 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000564
565 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100566 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000567 spin_unlock(&glob->lru_lock);
568 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200569 }
570
571 put_count = ttm_bo_del_from_lru(bo);
572 list_del_init(&bo->ddestroy);
573 ++put_count;
574
575 spin_unlock(&glob->lru_lock);
576 ttm_bo_cleanup_memtype_use(bo);
577
Dave Airlied6ea8882010-11-22 13:24:40 +1000578 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200579
580 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200581}
582
583/**
584 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
585 * encountered buffers.
586 */
587
588static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
589{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200590 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100591 struct ttm_buffer_object *entry = NULL;
592 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200593
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200594 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100595 if (list_empty(&bdev->ddestroy))
596 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200597
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100598 entry = list_first_entry(&bdev->ddestroy,
599 struct ttm_buffer_object, ddestroy);
600 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200601
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100602 for (;;) {
603 struct ttm_buffer_object *nentry = NULL;
604
605 if (entry->ddestroy.next != &bdev->ddestroy) {
606 nentry = list_first_entry(&entry->ddestroy,
607 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200608 kref_get(&nentry->list_kref);
609 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200610
Christian Königdfd5e502016-04-06 11:12:03 +0200611 ret = __ttm_bo_reserve(entry, false, true, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100612 if (remove_all && ret) {
613 spin_unlock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200614 ret = __ttm_bo_reserve(entry, false, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100615 spin_lock(&glob->lru_lock);
616 }
617
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000618 if (!ret)
619 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
620 !remove_all);
621 else
622 spin_unlock(&glob->lru_lock);
623
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200624 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100625 entry = nentry;
626
627 if (ret || !entry)
628 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200629
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200630 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100631 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200632 break;
633 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200634
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100635out_unlock:
636 spin_unlock(&glob->lru_lock);
637out:
638 if (entry)
639 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200640 return ret;
641}
642
643static void ttm_bo_delayed_workqueue(struct work_struct *work)
644{
645 struct ttm_bo_device *bdev =
646 container_of(work, struct ttm_bo_device, wq.work);
647
648 if (ttm_bo_delayed_delete(bdev, false)) {
649 schedule_delayed_work(&bdev->wq,
650 ((HZ / 100) < 1) ? 1 : HZ / 100);
651 }
652}
653
654static void ttm_bo_release(struct kref *kref)
655{
656 struct ttm_buffer_object *bo =
657 container_of(kref, struct ttm_buffer_object, kref);
658 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100659 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200660
David Herrmann72525b32013-07-24 21:08:53 +0200661 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100662 ttm_mem_io_lock(man, false);
663 ttm_mem_io_free_vm(bo);
664 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200665 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200666 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200667}
668
669void ttm_bo_unref(struct ttm_buffer_object **p_bo)
670{
671 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200672
673 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200674 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200675}
676EXPORT_SYMBOL(ttm_bo_unref);
677
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400678int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
679{
680 return cancel_delayed_work_sync(&bdev->wq);
681}
682EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
683
684void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
685{
686 if (resched)
687 schedule_delayed_work(&bdev->wq,
688 ((HZ / 100) < 1) ? 1 : HZ / 100);
689}
690EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
691
Jerome Glisseca262a9992009-12-08 15:33:32 +0100692static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000693 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200694{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200695 struct ttm_bo_device *bdev = bo->bdev;
696 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100697 struct ttm_placement placement;
698 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200699
Christian König8aa6d4f2016-04-06 11:12:04 +0200700 ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200701
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200702 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100703 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700704 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200705 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200706 goto out;
707 }
708
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200709 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200710
711 evict_mem = bo->mem;
712 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100713 evict_mem.bus.io_reserved_vm = false;
714 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200715
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100716 placement.num_placement = 0;
717 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100718 bdev->driver->evict_flags(bo, &placement);
719 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000720 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200721 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100722 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700723 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
724 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100725 ttm_bo_mem_space_debug(bo, &placement);
726 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200727 goto out;
728 }
729
730 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000731 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200732 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100733 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700734 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000735 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200736 goto out;
737 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200738 bo->evicted = true;
739out:
740 return ret;
741}
742
Jerome Glisseca262a9992009-12-08 15:33:32 +0100743static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
744 uint32_t mem_type,
Michel Dänzere3001802014-10-09 15:02:59 +0900745 const struct ttm_place *place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000746 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000747 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100748{
749 struct ttm_bo_global *glob = bdev->glob;
750 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
751 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000752 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100753
754 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000755 list_for_each_entry(bo, &man->lru, lru) {
Christian Königdfd5e502016-04-06 11:12:03 +0200756 ret = __ttm_bo_reserve(bo, false, true, NULL);
Michel Dänzere3001802014-10-09 15:02:59 +0900757 if (!ret) {
758 if (place && (place->fpfn || place->lpfn)) {
759 /* Don't evict this BO if it's outside of the
760 * requested placement range
761 */
762 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
763 (place->lpfn && place->lpfn <= bo->mem.start)) {
764 __ttm_bo_unreserve(bo);
765 ret = -EBUSY;
766 continue;
767 }
768 }
769
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000770 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900771 }
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100772 }
773
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000774 if (ret) {
775 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000776 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200777 }
778
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000779 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100780
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000781 if (!list_empty(&bo->ddestroy)) {
782 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
783 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100784 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000785 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100786 }
787
788 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100789 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100790
791 BUG_ON(ret != 0);
792
Dave Airlied6ea8882010-11-22 13:24:40 +1000793 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100794
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000795 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100796 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100797
Jerome Glisseca262a9992009-12-08 15:33:32 +0100798 kref_put(&bo->list_kref, ttm_bo_release_list);
799 return ret;
800}
801
Ben Skeggs42311ff2010-08-04 12:07:08 +1000802void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
803{
Ben Skeggsd961db72010-08-05 10:48:18 +1000804 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000805
Ben Skeggsd961db72010-08-05 10:48:18 +1000806 if (mem->mm_node)
807 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000808}
809EXPORT_SYMBOL(ttm_bo_mem_put);
810
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200811/**
812 * Repeatedly evict memory from the LRU for @mem_type until we create enough
813 * space, or we've evicted everything and there isn't enough space.
814 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100815static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
816 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200817 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100818 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000819 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000820 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200821{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100822 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200823 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200824 int ret;
825
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200826 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200827 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200828 if (unlikely(ret != 0))
829 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000830 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100831 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900832 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000833 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100834 if (unlikely(ret != 0))
835 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200836 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000837 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200838 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200839 mem->mem_type = mem_type;
840 return 0;
841}
842
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200843static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
844 uint32_t cur_placement,
845 uint32_t proposed_placement)
846{
847 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
848 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
849
850 /**
851 * Keep current caching if possible.
852 */
853
854 if ((cur_placement & caching) != 0)
855 result |= (cur_placement & caching);
856 else if ((man->default_caching & caching) != 0)
857 result |= man->default_caching;
858 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
859 result |= TTM_PL_FLAG_CACHED;
860 else if ((TTM_PL_FLAG_WC & caching) != 0)
861 result |= TTM_PL_FLAG_WC;
862 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
863 result |= TTM_PL_FLAG_UNCACHED;
864
865 return result;
866}
867
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200868static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200869 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200870 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200871 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200872{
873 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
874
Christian Königf1217ed2014-08-27 13:16:04 +0200875 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200876 return false;
877
Christian Königf1217ed2014-08-27 13:16:04 +0200878 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200879 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880
Christian Königf1217ed2014-08-27 13:16:04 +0200881 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200882
883 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200884 return true;
885}
886
887/**
888 * Creates space for memory region @mem according to its type.
889 *
890 * This function first searches for free space in compatible memory types in
891 * the priority order defined by the driver. If free space isn't found, then
892 * ttm_bo_mem_force_space is attempted in priority order to evict and find
893 * space.
894 */
895int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100896 struct ttm_placement *placement,
897 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000898 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000899 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200900{
901 struct ttm_bo_device *bdev = bo->bdev;
902 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200903 uint32_t mem_type = TTM_PL_SYSTEM;
904 uint32_t cur_flags = 0;
905 bool type_found = false;
906 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100907 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100908 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200909
910 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000911 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200912 const struct ttm_place *place = &placement->placement[i];
913
914 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100915 if (ret)
916 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200917 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700918 if (!man->has_type || !man->use_type)
919 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200920
Christian Königf1217ed2014-08-27 13:16:04 +0200921 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100922 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923
924 if (!type_ok)
925 continue;
926
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700927 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200928 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
929 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100930 /*
931 * Use the access and other non-mapping-related flag bits from
932 * the memory placement flags to the current flags
933 */
Christian Königf1217ed2014-08-27 13:16:04 +0200934 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100935 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200936
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200937 if (mem_type == TTM_PL_SYSTEM)
938 break;
939
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700940 ret = (*man->func->get_node)(man, bo, place, mem);
941 if (unlikely(ret))
942 return ret;
943
Ben Skeggsd961db72010-08-05 10:48:18 +1000944 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200945 break;
946 }
947
Ben Skeggsd961db72010-08-05 10:48:18 +1000948 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200949 mem->mem_type = mem_type;
950 mem->placement = cur_flags;
951 return 0;
952 }
953
Dave Airlieb6637522009-12-14 14:51:35 +1000954 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200955 const struct ttm_place *place = &placement->busy_placement[i];
956
957 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100958 if (ret)
959 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200960 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700961 if (!man->has_type || !man->use_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200962 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200963 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964 continue;
965
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700966 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200967 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
968 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100969 /*
970 * Use the access and other non-mapping-related flag bits from
971 * the memory placement flags to the current flags
972 */
Christian Königf1217ed2014-08-27 13:16:04 +0200973 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100974 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200975
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100976 if (mem_type == TTM_PL_SYSTEM) {
977 mem->mem_type = mem_type;
978 mem->placement = cur_flags;
979 mem->mm_node = NULL;
980 return 0;
981 }
982
Christian Königf1217ed2014-08-27 13:16:04 +0200983 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000984 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200985 if (ret == 0 && mem->mm_node) {
986 mem->placement = cur_flags;
987 return 0;
988 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100989 if (ret == -ERESTARTSYS)
990 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200991 }
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700992
993 if (!type_found) {
994 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
995 return -EINVAL;
996 }
997
998 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200999}
1000EXPORT_SYMBOL(ttm_bo_mem_space);
1001
Rashika Kheria6e87fa42014-01-06 22:12:58 +05301002static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001003 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001004 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001005 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001006{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001007 int ret = 0;
1008 struct ttm_mem_reg mem;
1009
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001010 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001011
1012 /*
Christian König5ee7b412016-04-06 11:12:02 +02001013 * Don't wait for the BO on initial allocation. This is important when
1014 * the BO has an imported reservation object.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 */
Christian König5ee7b412016-04-06 11:12:02 +02001016 if (bo->mem.mem_type != TTM_PL_SYSTEM || bo->ttm != NULL) {
1017 /*
1018 * FIXME: It's possible to pipeline buffer moves.
1019 * Have the driver move function wait for idle when necessary,
1020 * instead of doing it here.
1021 */
Christian König8aa6d4f2016-04-06 11:12:04 +02001022 ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
Christian König5ee7b412016-04-06 11:12:02 +02001023 if (ret)
1024 return ret;
1025 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001026 mem.num_pages = bo->num_pages;
1027 mem.size = mem.num_pages << PAGE_SHIFT;
1028 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001029 mem.bus.io_reserved_vm = false;
1030 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001031 /*
1032 * Determine where to move the buffer.
1033 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001034 ret = ttm_bo_mem_space(bo, placement, &mem,
1035 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036 if (ret)
1037 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001038 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1039 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001040out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001041 if (ret && mem.mm_node)
1042 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001043 return ret;
1044}
1045
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001046static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1047 struct ttm_mem_reg *mem,
1048 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001050 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001051
Jerome Glisseca262a9992009-12-08 15:33:32 +01001052 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001053 const struct ttm_place *heap = &placement->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;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001063 }
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001064
1065 for (i = 0; i < placement->num_busy_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +02001066 const struct ttm_place *heap = &placement->busy_placement[i];
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001067 if (mem->mm_node &&
Christian Königf1217ed2014-08-27 13:16:04 +02001068 (mem->start < heap->fpfn ||
Michel Dänzer9ace2ef2014-10-09 15:03:03 +09001069 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
Christian Königf1217ed2014-08-27 13:16:04 +02001070 continue;
1071
1072 *new_flags = heap->flags;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001073 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1074 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1075 return true;
1076 }
1077
1078 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001079}
1080
Jerome Glisse09855ac2009-12-10 17:16:27 +01001081int ttm_bo_validate(struct ttm_buffer_object *bo,
1082 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001083 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001084 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001085{
1086 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001087 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001088
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001089 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001090 /*
1091 * Check whether we need to move buffer.
1092 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001093 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001094 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1095 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001096 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001097 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001098 } else {
1099 /*
1100 * Use the access and other non-mapping-related flag bits from
1101 * the compatible memory placement flags to the active flags
1102 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001103 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001104 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001105 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106 /*
1107 * We might need to add a TTM.
1108 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001109 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1110 ret = ttm_bo_add_ttm(bo, true);
1111 if (ret)
1112 return ret;
1113 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001114 return 0;
1115}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001116EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117
Jerome Glisse09855ac2009-12-10 17:16:27 +01001118int ttm_bo_init(struct ttm_bo_device *bdev,
1119 struct ttm_buffer_object *bo,
1120 unsigned long size,
1121 enum ttm_bo_type type,
1122 struct ttm_placement *placement,
1123 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001124 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001125 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001126 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001127 struct sg_table *sg,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001128 struct reservation_object *resv,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001129 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001130{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001131 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001132 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001133 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001134 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001135
1136 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1137 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001138 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001139 if (destroy)
1140 (*destroy)(bo);
1141 else
1142 kfree(bo);
1143 return -ENOMEM;
1144 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001145
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1147 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001148 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001149 if (destroy)
1150 (*destroy)(bo);
1151 else
1152 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001153 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001154 return -EINVAL;
1155 }
1156 bo->destroy = destroy;
1157
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158 kref_init(&bo->kref);
1159 kref_init(&bo->list_kref);
1160 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161 INIT_LIST_HEAD(&bo->lru);
1162 INIT_LIST_HEAD(&bo->ddestroy);
1163 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001164 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001165 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001166 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001167 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001168 bo->type = type;
1169 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001170 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001171 bo->mem.mem_type = TTM_PL_SYSTEM;
1172 bo->mem.num_pages = bo->num_pages;
1173 bo->mem.mm_node = NULL;
1174 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001175 bo->mem.bus.io_reserved_vm = false;
1176 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001177 bo->priv_flags = 0;
1178 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001179 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001180 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001181 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001182 if (resv) {
1183 bo->resv = resv;
1184 lockdep_assert_held(&bo->resv->lock.base);
1185 } else {
1186 bo->resv = &bo->ttm_resv;
1187 reservation_object_init(&bo->ttm_resv);
1188 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001189 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001190 drm_vma_node_reset(&bo->vma_node);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001191
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001192 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001193 * For ttm_bo_type_device buffers, allocate
1194 * address space from the device.
1195 */
Christian Königf1217ed2014-08-27 13:16:04 +02001196 if (bo->type == ttm_bo_type_device ||
1197 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001198 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1199 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001201 /* passed reservation objects should already be locked,
1202 * since otherwise lockdep will be angered in radeon.
1203 */
1204 if (!resv) {
1205 locked = ww_mutex_trylock(&bo->resv->lock);
1206 WARN_ON(!locked);
1207 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001208
1209 if (likely(!ret))
1210 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001211
Christian König33d48cf2016-01-11 15:35:18 +01001212 if (!resv) {
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001213 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001214
Christian König33d48cf2016-01-11 15:35:18 +01001215 } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1216 spin_lock(&bo->glob->lru_lock);
1217 ttm_bo_add_to_lru(bo);
1218 spin_unlock(&bo->glob->lru_lock);
1219 }
1220
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001221 if (unlikely(ret))
1222 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223
1224 return ret;
1225}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001226EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001227
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001228size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1229 unsigned long bo_size,
1230 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001232 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1233 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001234
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001235 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001236 size += ttm_round_pot(npages * sizeof(void *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001237 size += ttm_round_pot(sizeof(struct ttm_tt));
1238 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001239}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001240EXPORT_SYMBOL(ttm_bo_acc_size);
1241
1242size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1243 unsigned long bo_size,
1244 unsigned struct_size)
1245{
1246 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1247 size_t size = 0;
1248
1249 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001250 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001251 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1252 return size;
1253}
1254EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001255
Jerome Glisse09855ac2009-12-10 17:16:27 +01001256int ttm_bo_create(struct ttm_bo_device *bdev,
1257 unsigned long size,
1258 enum ttm_bo_type type,
1259 struct ttm_placement *placement,
1260 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001261 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001262 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001263 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001264{
1265 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001266 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001267 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001268
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001270 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272
Thomas Hellstroma393c732012-06-12 13:28:42 +02001273 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001274 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001275 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001276 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001277 if (likely(ret == 0))
1278 *p_bo = bo;
1279
1280 return ret;
1281}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001282EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001283
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001284static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001285 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001286{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001287 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001288 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001289 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001290
1291 /*
1292 * Can't use standard list traversal since we're unlocking.
1293 */
1294
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001295 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001296 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001297 spin_unlock(&glob->lru_lock);
Michel Dänzere3001802014-10-09 15:02:59 +09001298 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001299 if (ret) {
1300 if (allow_errors) {
1301 return ret;
1302 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001303 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001304 }
1305 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001306 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001307 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001308 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001309 return 0;
1310}
1311
1312int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1313{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001314 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001315 int ret = -EINVAL;
1316
1317 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001318 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001319 return ret;
1320 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001321 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001322
1323 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001324 pr_err("Trying to take down uninitialized memory manager type %u\n",
1325 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001326 return ret;
1327 }
1328
1329 man->use_type = false;
1330 man->has_type = false;
1331
1332 ret = 0;
1333 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001334 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001335
Ben Skeggsd961db72010-08-05 10:48:18 +10001336 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001337 }
1338
1339 return ret;
1340}
1341EXPORT_SYMBOL(ttm_bo_clean_mm);
1342
1343int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1344{
1345 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1346
1347 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001348 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001349 return -EINVAL;
1350 }
1351
1352 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001353 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001354 return 0;
1355 }
1356
Jerome Glisseca262a9992009-12-08 15:33:32 +01001357 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001358}
1359EXPORT_SYMBOL(ttm_bo_evict_mm);
1360
1361int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001362 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001363{
1364 int ret = -EINVAL;
1365 struct ttm_mem_type_manager *man;
1366
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001367 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001368 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001369 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001370 man->io_reserve_fastpath = true;
1371 man->use_io_reserve_lru = false;
1372 mutex_init(&man->io_reserve_mutex);
1373 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001374
1375 ret = bdev->driver->init_mem_type(bdev, type, man);
1376 if (ret)
1377 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001378 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001379
1380 ret = 0;
1381 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001382 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001383 if (ret)
1384 return ret;
1385 }
1386 man->has_type = true;
1387 man->use_type = true;
1388 man->size = p_size;
1389
1390 INIT_LIST_HEAD(&man->lru);
1391
1392 return 0;
1393}
1394EXPORT_SYMBOL(ttm_bo_init_mm);
1395
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001396static void ttm_bo_global_kobj_release(struct kobject *kobj)
1397{
1398 struct ttm_bo_global *glob =
1399 container_of(kobj, struct ttm_bo_global, kobj);
1400
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001401 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1402 __free_page(glob->dummy_read_page);
1403 kfree(glob);
1404}
1405
Dave Airlieba4420c2010-03-09 10:56:52 +10001406void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001407{
1408 struct ttm_bo_global *glob = ref->object;
1409
1410 kobject_del(&glob->kobj);
1411 kobject_put(&glob->kobj);
1412}
1413EXPORT_SYMBOL(ttm_bo_global_release);
1414
Dave Airlieba4420c2010-03-09 10:56:52 +10001415int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001416{
1417 struct ttm_bo_global_ref *bo_ref =
1418 container_of(ref, struct ttm_bo_global_ref, ref);
1419 struct ttm_bo_global *glob = ref->object;
1420 int ret;
1421
1422 mutex_init(&glob->device_list_mutex);
1423 spin_lock_init(&glob->lru_lock);
1424 glob->mem_glob = bo_ref->mem_glob;
1425 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1426
1427 if (unlikely(glob->dummy_read_page == NULL)) {
1428 ret = -ENOMEM;
1429 goto out_no_drp;
1430 }
1431
1432 INIT_LIST_HEAD(&glob->swap_lru);
1433 INIT_LIST_HEAD(&glob->device_list);
1434
1435 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1436 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1437 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001438 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001439 goto out_no_shrink;
1440 }
1441
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001442 atomic_set(&glob->bo_count, 0);
1443
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001444 ret = kobject_init_and_add(
1445 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001446 if (unlikely(ret != 0))
1447 kobject_put(&glob->kobj);
1448 return ret;
1449out_no_shrink:
1450 __free_page(glob->dummy_read_page);
1451out_no_drp:
1452 kfree(glob);
1453 return ret;
1454}
1455EXPORT_SYMBOL(ttm_bo_global_init);
1456
1457
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001458int ttm_bo_device_release(struct ttm_bo_device *bdev)
1459{
1460 int ret = 0;
1461 unsigned i = TTM_NUM_MEM_TYPES;
1462 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001463 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001464
1465 while (i--) {
1466 man = &bdev->man[i];
1467 if (man->has_type) {
1468 man->use_type = false;
1469 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1470 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001471 pr_err("DRM memory manager type %d is not clean\n",
1472 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001473 }
1474 man->has_type = false;
1475 }
1476 }
1477
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001478 mutex_lock(&glob->device_list_mutex);
1479 list_del(&bdev->device_list);
1480 mutex_unlock(&glob->device_list_mutex);
1481
Tejun Heof094cfc2010-12-24 15:59:06 +01001482 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001483
1484 while (ttm_bo_delayed_delete(bdev, true))
1485 ;
1486
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001487 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488 if (list_empty(&bdev->ddestroy))
1489 TTM_DEBUG("Delayed destroy list was clean\n");
1490
1491 if (list_empty(&bdev->man[0].lru))
1492 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001493 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001494
David Herrmann72525b32013-07-24 21:08:53 +02001495 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001496
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497 return ret;
1498}
1499EXPORT_SYMBOL(ttm_bo_device_release);
1500
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001501int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001502 struct ttm_bo_global *glob,
1503 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001504 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001505 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001506 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001507{
1508 int ret = -EINVAL;
1509
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001511
1512 memset(bdev->man, 0, sizeof(bdev->man));
1513
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001514 /*
1515 * Initialize the system memory buffer type.
1516 * Other types need to be driver / IOCTL initialized.
1517 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001518 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001519 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001520 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001521
David Herrmann72525b32013-07-24 21:08:53 +02001522 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1523 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001524 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001526 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001527 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001528 bdev->need_dma32 = need_dma32;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001529 mutex_lock(&glob->device_list_mutex);
1530 list_add_tail(&bdev->device_list, &glob->device_list);
1531 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001532
1533 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001534out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001535 return ret;
1536}
1537EXPORT_SYMBOL(ttm_bo_device_init);
1538
1539/*
1540 * buffer object vm functions.
1541 */
1542
1543bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1544{
1545 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1546
1547 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1548 if (mem->mem_type == TTM_PL_SYSTEM)
1549 return false;
1550
1551 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1552 return false;
1553
1554 if (mem->placement & TTM_PL_FLAG_CACHED)
1555 return false;
1556 }
1557 return true;
1558}
1559
Thomas Hellstromeba67092010-11-11 09:41:57 +01001560void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001561{
1562 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563
David Herrmann51335df2013-07-24 21:10:03 +02001564 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001565 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001566}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001567
1568void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1569{
1570 struct ttm_bo_device *bdev = bo->bdev;
1571 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1572
1573 ttm_mem_io_lock(man, false);
1574 ttm_bo_unmap_virtual_locked(bo);
1575 ttm_mem_io_unlock(man);
1576}
1577
1578
Dave Airliee024e112009-06-24 09:48:08 +10001579EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001580
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001581int ttm_bo_wait(struct ttm_buffer_object *bo,
Christian König8aa6d4f2016-04-06 11:12:04 +02001582 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001583{
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001584 struct reservation_object_list *fobj;
1585 struct reservation_object *resv;
1586 struct fence *excl;
1587 long timeout = 15 * HZ;
1588 int i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001589
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001590 resv = bo->resv;
1591 fobj = reservation_object_get_list(resv);
1592 excl = reservation_object_get_excl(resv);
1593 if (excl) {
1594 if (!fence_is_signaled(excl)) {
1595 if (no_wait)
1596 return -EBUSY;
Maarten Lankhorst70401382014-01-21 13:07:01 +01001597
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001598 timeout = fence_wait_timeout(excl,
1599 interruptible, timeout);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001600 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001601 }
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001602
1603 for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
1604 struct fence *fence;
1605 fence = rcu_dereference_protected(fobj->shared[i],
1606 reservation_object_held(resv));
1607
1608 if (!fence_is_signaled(fence)) {
1609 if (no_wait)
1610 return -EBUSY;
1611
1612 timeout = fence_wait_timeout(fence,
1613 interruptible, timeout);
1614 }
1615 }
1616
1617 if (timeout < 0)
1618 return timeout;
1619
1620 if (timeout == 0)
1621 return -EBUSY;
1622
1623 reservation_object_add_excl_fence(resv, NULL);
1624 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1625 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001626}
1627EXPORT_SYMBOL(ttm_bo_wait);
1628
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001629int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1630{
1631 int ret = 0;
1632
1633 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001634 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001635 */
1636
Christian Königdfd5e502016-04-06 11:12:03 +02001637 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001638 if (unlikely(ret != 0))
1639 return ret;
Christian König8aa6d4f2016-04-06 11:12:04 +02001640 ret = ttm_bo_wait(bo, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001641 if (likely(ret == 0))
1642 atomic_inc(&bo->cpu_writers);
1643 ttm_bo_unreserve(bo);
1644 return ret;
1645}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001646EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001647
1648void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1649{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001650 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001651}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001652EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001653
1654/**
1655 * A buffer object shrink method that tries to swap out the first
1656 * buffer object on the bo_global::swap_lru list.
1657 */
1658
1659static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1660{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001661 struct ttm_bo_global *glob =
1662 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001663 struct ttm_buffer_object *bo;
1664 int ret = -EBUSY;
1665 int put_count;
1666 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1667
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001668 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001669 list_for_each_entry(bo, &glob->swap_lru, swap) {
Christian Königdfd5e502016-04-06 11:12:03 +02001670 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001671 if (!ret)
1672 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001673 }
1674
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001675 if (ret) {
1676 spin_unlock(&glob->lru_lock);
1677 return ret;
1678 }
1679
1680 kref_get(&bo->list_kref);
1681
1682 if (!list_empty(&bo->ddestroy)) {
1683 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1684 kref_put(&bo->list_kref, ttm_bo_release_list);
1685 return ret;
1686 }
1687
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001688 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001689 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001690
Dave Airlied6ea8882010-11-22 13:24:40 +10001691 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001692
1693 /**
1694 * Wait for GPU, then move to system cached.
1695 */
1696
Christian König8aa6d4f2016-04-06 11:12:04 +02001697 ret = ttm_bo_wait(bo, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001698
1699 if (unlikely(ret != 0))
1700 goto out;
1701
1702 if ((bo->mem.placement & swap_placement) != swap_placement) {
1703 struct ttm_mem_reg evict_mem;
1704
1705 evict_mem = bo->mem;
1706 evict_mem.mm_node = NULL;
1707 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1708 evict_mem.mem_type = TTM_PL_SYSTEM;
1709
1710 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001711 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001712 if (unlikely(ret != 0))
1713 goto out;
1714 }
1715
1716 ttm_bo_unmap_virtual(bo);
1717
1718 /**
1719 * Swap out. Buffer will be swapped in again as soon as
1720 * anyone tries to access a ttm page.
1721 */
1722
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001723 if (bo->bdev->driver->swap_notify)
1724 bo->bdev->driver->swap_notify(bo);
1725
Jan Engelhardt5df23972011-04-04 01:25:18 +02001726 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001727out:
1728
1729 /**
1730 *
1731 * Unreserve without putting on LRU to avoid swapping out an
1732 * already swapped buffer.
1733 */
1734
Thomas Hellstromc7523082014-02-20 11:36:25 +01001735 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001736 kref_put(&bo->list_kref, ttm_bo_release_list);
1737 return ret;
1738}
1739
1740void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1741{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001742 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001743 ;
1744}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001745EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001746
1747/**
1748 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1749 * unreserved
1750 *
1751 * @bo: Pointer to buffer
1752 */
1753int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1754{
1755 int ret;
1756
1757 /*
1758 * In the absense of a wait_unlocked API,
1759 * Use the bo::wu_mutex to avoid triggering livelocks due to
1760 * concurrent use of this function. Note that this use of
1761 * bo::wu_mutex can go away if we change locking order to
1762 * mmap_sem -> bo::reserve.
1763 */
1764 ret = mutex_lock_interruptible(&bo->wu_mutex);
1765 if (unlikely(ret != 0))
1766 return -ERESTARTSYS;
1767 if (!ww_mutex_is_locked(&bo->resv->lock))
1768 goto out_unlock;
Christian Königdfd5e502016-04-06 11:12:03 +02001769 ret = __ttm_bo_reserve(bo, true, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001770 if (unlikely(ret != 0))
1771 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001772 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001773
1774out_unlock:
1775 mutex_unlock(&bo->wu_mutex);
1776 return ret;
1777}