blob: d3463ebc0b25cf22b421cbd0e62a7df46b7311d4 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/jiffies.h>
37#include <linux/slab.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/file.h>
41#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +020043#include <linux/reservation.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020044
45#define TTM_ASSERT_LOCKED(param)
46#define TTM_DEBUG(fmt, arg...)
47#define TTM_BO_HASH_ORDER 13
48
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020049static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020050static void ttm_bo_global_kobj_release(struct kobject *kobj);
51
52static struct attribute ttm_bo_count = {
53 .name = "bo_count",
54 .mode = S_IRUGO
55};
56
Christian Königf1217ed2014-08-27 13:16:04 +020057static inline int ttm_mem_type_from_place(const struct ttm_place *place,
58 uint32_t *mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010059{
Christian König5d98d0bc2016-09-12 13:16:16 +020060 int pos;
Jerome Glissefb53f862009-12-09 21:55:10 +010061
Christian König5d98d0bc2016-09-12 13:16:16 +020062 pos = ffs(place->flags & TTM_PL_MASK_MEM);
63 if (unlikely(!pos))
64 return -EINVAL;
65
66 *mem_type = pos - 1;
67 return 0;
Jerome Glissefb53f862009-12-09 21:55:10 +010068}
69
Jerome Glisse5012f502009-12-10 18:07:26 +010070static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010071{
Jerome Glisse5012f502009-12-10 18:07:26 +010072 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
73
Joe Perches25d04792012-03-16 21:43:50 -070074 pr_err(" has_type: %d\n", man->has_type);
75 pr_err(" use_type: %d\n", man->use_type);
76 pr_err(" flags: 0x%08X\n", man->flags);
Alex Deucher54c4cd62015-03-04 00:18:38 -050077 pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
Joe Perches25d04792012-03-16 21:43:50 -070078 pr_err(" size: %llu\n", man->size);
79 pr_err(" available_caching: 0x%08X\n", man->available_caching);
80 pr_err(" default_caching: 0x%08X\n", man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100081 if (mem_type != TTM_PL_SYSTEM)
82 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010083}
84
85static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
86 struct ttm_placement *placement)
87{
Jerome Glissefb53f862009-12-09 21:55:10 +010088 int i, ret, mem_type;
89
Joe Perches25d04792012-03-16 21:43:50 -070090 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
91 bo, bo->mem.num_pages, bo->mem.size >> 10,
92 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010093 for (i = 0; i < placement->num_placement; i++) {
Christian Königf1217ed2014-08-27 13:16:04 +020094 ret = ttm_mem_type_from_place(&placement->placement[i],
Jerome Glissefb53f862009-12-09 21:55:10 +010095 &mem_type);
96 if (ret)
97 return;
Joe Perches25d04792012-03-16 21:43:50 -070098 pr_err(" placement[%d]=0x%08X (%d)\n",
Christian Königf1217ed2014-08-27 13:16:04 +020099 i, placement->placement[i].flags, mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +0100100 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100101 }
102}
103
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200104static ssize_t ttm_bo_global_show(struct kobject *kobj,
105 struct attribute *attr,
106 char *buffer)
107{
108 struct ttm_bo_global *glob =
109 container_of(kobj, struct ttm_bo_global, kobj);
110
111 return snprintf(buffer, PAGE_SIZE, "%lu\n",
112 (unsigned long) atomic_read(&glob->bo_count));
113}
114
115static struct attribute *ttm_bo_global_attrs[] = {
116 &ttm_bo_count,
117 NULL
118};
119
Emese Revfy52cf25d2010-01-19 02:58:23 +0100120static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200121 .show = &ttm_bo_global_show
122};
123
124static struct kobj_type ttm_bo_glob_kobj_type = {
125 .release = &ttm_bo_global_kobj_release,
126 .sysfs_ops = &ttm_bo_global_ops,
127 .default_attrs = ttm_bo_global_attrs
128};
129
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200130
131static inline uint32_t ttm_bo_type_flags(unsigned type)
132{
133 return 1 << (type);
134}
135
136static void ttm_bo_release_list(struct kref *list_kref)
137{
138 struct ttm_buffer_object *bo =
139 container_of(list_kref, struct ttm_buffer_object, list_kref);
140 struct ttm_bo_device *bdev = bo->bdev;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500141 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200142
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100143 BUG_ON(kref_read(&bo->list_kref));
144 BUG_ON(kref_read(&bo->kref));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200145 BUG_ON(atomic_read(&bo->cpu_writers));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
Christian König4279cb12016-06-06 10:17:51 +0200149 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200150 atomic_dec(&bo->glob->bo_count);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100151 dma_fence_put(bo->moving);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200152 if (bo->resv == &bo->ttm_resv)
153 reservation_object_fini(&bo->ttm_resv);
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800154 mutex_destroy(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200155 if (bo->destroy)
156 bo->destroy(bo);
157 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200158 kfree(bo);
159 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500160 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200161}
162
Dave Airlied6ea8882010-11-22 13:24:40 +1000163void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164{
165 struct ttm_bo_device *bdev = bo->bdev;
Christian König260498f2017-01-12 11:50:13 +0100166 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200167
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200168 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169
170 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
171
172 BUG_ON(!list_empty(&bo->lru));
173
Christian König260498f2017-01-12 11:50:13 +0100174 man = &bdev->man[bo->mem.mem_type];
175 list_add_tail(&bo->lru, &man->lru[bo->priority]);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200176 kref_get(&bo->list_kref);
177
Christian Königed704a42016-01-11 15:35:19 +0100178 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
Christian König260498f2017-01-12 11:50:13 +0100179 list_add_tail(&bo->swap,
180 &bo->glob->swap_lru[bo->priority]);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200181 kref_get(&bo->list_kref);
182 }
183 }
184}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200185EXPORT_SYMBOL(ttm_bo_add_to_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200186
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100187static void ttm_bo_ref_bug(struct kref *list_kref)
188{
189 BUG();
190}
191
192void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200193{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200194 if (!list_empty(&bo->swap)) {
195 list_del_init(&bo->swap);
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100196 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200197 }
198 if (!list_empty(&bo->lru)) {
199 list_del_init(&bo->lru);
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100200 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200201 }
202
Christian König896d6302017-01-12 11:54:11 +0100203 /*
204 * TODO: Add a driver hook to delete from
205 * driver-specific LRU's here.
206 */
Dave Airlied6ea8882010-11-22 13:24:40 +1000207}
208
Maarten Lankhorst34820322013-06-27 13:48:24 +0200209void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200210{
Maarten Lankhorst34820322013-06-27 13:48:24 +0200211 spin_lock(&bo->glob->lru_lock);
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100212 ttm_bo_del_from_lru(bo);
Maarten Lankhorst34820322013-06-27 13:48:24 +0200213 spin_unlock(&bo->glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200214}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200215EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200216
Christian Königab749612016-01-11 15:35:20 +0100217void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
218{
Christian Königab749612016-01-11 15:35:20 +0100219 lockdep_assert_held(&bo->resv->lock.base);
220
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100221 ttm_bo_del_from_lru(bo);
Flora Cui56fc3502016-04-20 10:23:47 +0800222 ttm_bo_add_to_lru(bo);
Christian Königab749612016-01-11 15:35:20 +0100223}
224EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
225
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200226/*
227 * Call bo->mutex locked.
228 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200229static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
230{
231 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200232 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200233 int ret = 0;
234 uint32_t page_flags = 0;
235
236 TTM_ASSERT_LOCKED(&bo->mutex);
237 bo->ttm = NULL;
238
Dave Airliead49f502009-07-10 22:36:26 +1000239 if (bdev->need_dma32)
240 page_flags |= TTM_PAGE_FLAG_DMA32;
241
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200242 switch (bo->type) {
243 case ttm_bo_type_device:
244 if (zero_alloc)
245 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
246 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400247 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
248 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200249 if (unlikely(bo->ttm == NULL))
250 ret = -ENOMEM;
251 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100252 case ttm_bo_type_sg:
253 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
254 page_flags | TTM_PAGE_FLAG_SG,
255 glob->dummy_read_page);
256 if (unlikely(bo->ttm == NULL)) {
257 ret = -ENOMEM;
258 break;
259 }
260 bo->ttm->sg = bo->sg;
261 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200262 default:
Joe Perches25d04792012-03-16 21:43:50 -0700263 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200264 ret = -EINVAL;
265 break;
266 }
267
268 return ret;
269}
270
271static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
272 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000273 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000274 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200275{
276 struct ttm_bo_device *bdev = bo->bdev;
277 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
278 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
279 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
280 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
281 int ret = 0;
282
283 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100284 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
285 ret = ttm_mem_io_lock(old_man, true);
286 if (unlikely(ret != 0))
287 goto out_err;
288 ttm_bo_unmap_virtual_locked(bo);
289 ttm_mem_io_unlock(old_man);
290 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200291
292 /*
293 * Create and bind a ttm if required.
294 */
295
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000296 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
297 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000298 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
299 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000300 if (ret)
301 goto out_err;
302 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200303
304 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
305 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200306 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200307
308 if (mem->mem_type != TTM_PL_SYSTEM) {
309 ret = ttm_tt_bind(bo->ttm, mem);
310 if (ret)
311 goto out_err;
312 }
313
314 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000315 if (bdev->driver->move_notify)
Nicolai Hähnle66257db2016-12-15 17:23:49 +0100316 bdev->driver->move_notify(bo, evict, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100317 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200318 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200319 goto moved;
320 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200321 }
322
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000323 if (bdev->driver->move_notify)
Nicolai Hähnle66257db2016-12-15 17:23:49 +0100324 bdev->driver->move_notify(bo, evict, mem);
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000325
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
327 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900328 ret = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200329 else if (bdev->driver->move)
330 ret = bdev->driver->move(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000331 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200332 else
Michel Dänzer4499f2a2016-08-08 12:28:26 +0900333 ret = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200334
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000335 if (ret) {
336 if (bdev->driver->move_notify) {
337 struct ttm_mem_reg tmp_mem = *mem;
338 *mem = bo->mem;
339 bo->mem = tmp_mem;
Nicolai Hähnle66257db2016-12-15 17:23:49 +0100340 bdev->driver->move_notify(bo, false, mem);
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000341 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000342 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000343 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200344
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000345 goto out_err;
346 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500347
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200348moved:
349 if (bo->evicted) {
Rob Clark9ef75062014-03-12 10:59:37 -0400350 if (bdev->driver->invalidate_caches) {
351 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
352 if (ret)
353 pr_err("Can not flush read caches\n");
354 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200355 bo->evicted = false;
356 }
357
358 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000359 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360 bdev->man[bo->mem.mem_type].gpu_offset;
361 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100362 } else
363 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200364
365 return 0;
366
367out_err:
368 new_man = &bdev->man[bo->mem.mem_type];
Christian König4279cb12016-06-06 10:17:51 +0200369 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200370 ttm_tt_destroy(bo->ttm);
371 bo->ttm = NULL;
372 }
373
374 return ret;
375}
376
377/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200378 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200379 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200380 * This is the place to put in driver specific hooks to release
381 * driver private resources.
382 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200383 */
384
385static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
386{
Jerome Glissedc97b342011-11-18 11:47:03 -0500387 if (bo->bdev->driver->move_notify)
Nicolai Hähnle66257db2016-12-15 17:23:49 +0100388 bo->bdev->driver->move_notify(bo, false, NULL);
Jerome Glissedc97b342011-11-18 11:47:03 -0500389
Christian König4279cb12016-06-06 10:17:51 +0200390 ttm_tt_destroy(bo->ttm);
391 bo->ttm = NULL;
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200392 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200393
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200394 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200395}
396
Christian König841e7632017-07-20 20:55:06 +0200397static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
398{
399 int r;
400
401 if (bo->resv == &bo->ttm_resv)
402 return 0;
403
404 reservation_object_init(&bo->ttm_resv);
405 BUG_ON(reservation_object_lock(&bo->ttm_resv, NULL));
406
407 r = reservation_object_copy_fences(&bo->ttm_resv, bo->resv);
408 if (r) {
409 reservation_object_unlock(&bo->ttm_resv);
410 reservation_object_fini(&bo->ttm_resv);
411 }
412
413 return r;
414}
415
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200416static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
417{
418 struct reservation_object_list *fobj;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100419 struct dma_fence *fence;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200420 int i;
421
Christian König841e7632017-07-20 20:55:06 +0200422 fobj = reservation_object_get_list(&bo->ttm_resv);
423 fence = reservation_object_get_excl(&bo->ttm_resv);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200424 if (fence && !fence->ops->signaled)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100425 dma_fence_enable_sw_signaling(fence);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200426
427 for (i = 0; fobj && i < fobj->shared_count; ++i) {
428 fence = rcu_dereference_protected(fobj->shared[i],
429 reservation_object_held(bo->resv));
430
431 if (!fence->ops->signaled)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100432 dma_fence_enable_sw_signaling(fence);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200433 }
434}
435
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200436static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200437{
438 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200439 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200440 int ret;
441
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100442 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200443 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100444
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700445 if (!ret) {
Christian König8aa6d4f2016-04-06 11:12:04 +0200446 if (!ttm_bo_wait(bo, false, true)) {
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100447 ttm_bo_del_from_lru(bo);
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100448 spin_unlock(&glob->lru_lock);
449 ttm_bo_cleanup_memtype_use(bo);
450
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100451 return;
Christian König841e7632017-07-20 20:55:06 +0200452 }
453
454 ret = ttm_bo_individualize_resv(bo);
455 if (ret) {
456 /* Last resort, if we fail to allocate memory for the
457 * fences block for the BO to become idle and free it.
458 */
459 spin_unlock(&glob->lru_lock);
460 ttm_bo_wait(bo, true, true);
461 ttm_bo_cleanup_memtype_use(bo);
462 return;
463 }
464 ttm_bo_flush_all_fences(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700465
466 /*
467 * Make NO_EVICT bos immediately available to
468 * shrinkers, now that they are queued for
469 * destruction.
470 */
471 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
472 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
473 ttm_bo_add_to_lru(bo);
474 }
475
Christian König841e7632017-07-20 20:55:06 +0200476 if (bo->resv != &bo->ttm_resv)
477 reservation_object_unlock(&bo->ttm_resv);
Thomas Hellstromc7523082014-02-20 11:36:25 +0100478 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700479 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200480
481 kref_get(&bo->list_kref);
482 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
483 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200484
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200485 schedule_delayed_work(&bdev->wq,
486 ((HZ / 100) < 1) ? 1 : HZ / 100);
487}
488
489/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000490 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200491 * If bo idle, remove from delayed- and lru lists, and unref.
492 * If not idle, do nothing.
493 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000494 * Must be called with lru_lock and reservation held, this function
495 * will drop both before returning.
496 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200497 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200498 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
499 */
500
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000501static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
502 bool interruptible,
503 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200504{
505 struct ttm_bo_global *glob = bo->glob;
Christian König841e7632017-07-20 20:55:06 +0200506 struct reservation_object *resv;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000507 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200508
Christian König841e7632017-07-20 20:55:06 +0200509 if (unlikely(list_empty(&bo->ddestroy)))
510 resv = bo->resv;
511 else
512 resv = &bo->ttm_resv;
513
514 if (reservation_object_test_signaled_rcu(resv, true))
515 ret = 0;
516 else
517 ret = -EBUSY;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200518
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000519 if (ret && !no_wait_gpu) {
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200520 long lret;
521 ww_mutex_unlock(&bo->resv->lock);
522 spin_unlock(&glob->lru_lock);
523
Christian König841e7632017-07-20 20:55:06 +0200524 lret = reservation_object_wait_timeout_rcu(resv, true,
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200525 interruptible,
526 30 * HZ);
527
528 if (lret < 0)
529 return lret;
530 else if (lret == 0)
531 return -EBUSY;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000532
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000533 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200534 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000535
536 /*
537 * We raced, and lost, someone else holds the reservation now,
538 * and is probably busy in ttm_bo_cleanup_memtype_use.
539 *
540 * Even if it's not the case, because we finished waiting any
541 * delayed destruction would succeed, so just return success
542 * here.
543 */
544 if (ret) {
545 spin_unlock(&glob->lru_lock);
546 return 0;
547 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100548 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000549
550 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100551 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000552 spin_unlock(&glob->lru_lock);
553 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200554 }
555
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100556 ttm_bo_del_from_lru(bo);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200557 list_del_init(&bo->ddestroy);
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100558 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200559
560 spin_unlock(&glob->lru_lock);
561 ttm_bo_cleanup_memtype_use(bo);
562
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200563 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200564}
565
566/**
567 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
568 * encountered buffers.
569 */
570
571static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
572{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200573 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100574 struct ttm_buffer_object *entry = NULL;
575 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200576
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200577 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100578 if (list_empty(&bdev->ddestroy))
579 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200580
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100581 entry = list_first_entry(&bdev->ddestroy,
582 struct ttm_buffer_object, ddestroy);
583 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200584
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100585 for (;;) {
586 struct ttm_buffer_object *nentry = NULL;
587
588 if (entry->ddestroy.next != &bdev->ddestroy) {
589 nentry = list_first_entry(&entry->ddestroy,
590 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200591 kref_get(&nentry->list_kref);
592 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200593
Christian Königdfd5e502016-04-06 11:12:03 +0200594 ret = __ttm_bo_reserve(entry, false, true, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100595 if (remove_all && ret) {
596 spin_unlock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200597 ret = __ttm_bo_reserve(entry, false, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100598 spin_lock(&glob->lru_lock);
599 }
600
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000601 if (!ret)
602 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
603 !remove_all);
604 else
605 spin_unlock(&glob->lru_lock);
606
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200607 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100608 entry = nentry;
609
610 if (ret || !entry)
611 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200612
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200613 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100614 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200615 break;
616 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200617
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100618out_unlock:
619 spin_unlock(&glob->lru_lock);
620out:
621 if (entry)
622 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200623 return ret;
624}
625
626static void ttm_bo_delayed_workqueue(struct work_struct *work)
627{
628 struct ttm_bo_device *bdev =
629 container_of(work, struct ttm_bo_device, wq.work);
630
631 if (ttm_bo_delayed_delete(bdev, false)) {
632 schedule_delayed_work(&bdev->wq,
633 ((HZ / 100) < 1) ? 1 : HZ / 100);
634 }
635}
636
637static void ttm_bo_release(struct kref *kref)
638{
639 struct ttm_buffer_object *bo =
640 container_of(kref, struct ttm_buffer_object, kref);
641 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100642 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200643
David Herrmann72525b32013-07-24 21:08:53 +0200644 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100645 ttm_mem_io_lock(man, false);
646 ttm_mem_io_free_vm(bo);
647 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200648 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200650}
651
652void ttm_bo_unref(struct ttm_buffer_object **p_bo)
653{
654 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200655
656 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200657 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200658}
659EXPORT_SYMBOL(ttm_bo_unref);
660
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400661int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
662{
663 return cancel_delayed_work_sync(&bdev->wq);
664}
665EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
666
667void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
668{
669 if (resched)
670 schedule_delayed_work(&bdev->wq,
671 ((HZ / 100) < 1) ? 1 : HZ / 100);
672}
673EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
674
Jerome Glisseca262a9992009-12-08 15:33:32 +0100675static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000676 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200677{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200678 struct ttm_bo_device *bdev = bo->bdev;
679 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100680 struct ttm_placement placement;
681 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200682
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200683 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200684
685 evict_mem = bo->mem;
686 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100687 evict_mem.bus.io_reserved_vm = false;
688 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200689
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100690 placement.num_placement = 0;
691 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100692 bdev->driver->evict_flags(bo, &placement);
693 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000694 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200695 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100696 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700697 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
698 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100699 ttm_bo_mem_space_debug(bo, &placement);
700 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200701 goto out;
702 }
703
704 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000705 no_wait_gpu);
Christian König17d33bc2016-06-06 10:17:56 +0200706 if (unlikely(ret)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100707 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700708 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000709 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200710 goto out;
711 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200712 bo->evicted = true;
713out:
714 return ret;
715}
716
Christian Königa2ab19fe2016-08-30 17:26:04 +0200717bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
718 const struct ttm_place *place)
719{
720 /* Don't evict this BO if it's outside of the
721 * requested placement range
722 */
723 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
724 (place->lpfn && place->lpfn <= bo->mem.start))
725 return false;
726
727 return true;
728}
729EXPORT_SYMBOL(ttm_bo_eviction_valuable);
730
Jerome Glisseca262a9992009-12-08 15:33:32 +0100731static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
732 uint32_t mem_type,
Michel Dänzere3001802014-10-09 15:02:59 +0900733 const struct ttm_place *place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000734 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000735 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100736{
737 struct ttm_bo_global *glob = bdev->glob;
738 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
739 struct ttm_buffer_object *bo;
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100740 int ret = -EBUSY;
Christian Königcf6c4672017-01-10 14:08:28 +0100741 unsigned i;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100742
743 spin_lock(&glob->lru_lock);
Christian Königcf6c4672017-01-10 14:08:28 +0100744 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
745 list_for_each_entry(bo, &man->lru[i], lru) {
746 ret = __ttm_bo_reserve(bo, false, true, NULL);
747 if (ret)
748 continue;
Michel Dänzere3001802014-10-09 15:02:59 +0900749
Christian Königcf6c4672017-01-10 14:08:28 +0100750 if (place && !bdev->driver->eviction_valuable(bo,
751 place)) {
752 __ttm_bo_unreserve(bo);
753 ret = -EBUSY;
754 continue;
755 }
756
757 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900758 }
Christian Königa2ab19fe2016-08-30 17:26:04 +0200759
Christian Königcf6c4672017-01-10 14:08:28 +0100760 if (!ret)
761 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100762 }
763
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000764 if (ret) {
765 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000766 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200767 }
768
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000769 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100770
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000771 if (!list_empty(&bo->ddestroy)) {
772 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
773 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100774 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000775 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100776 }
777
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100778 ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100779 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100780
781 BUG_ON(ret != 0);
782
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000783 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100784 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100785
Jerome Glisseca262a9992009-12-08 15:33:32 +0100786 kref_put(&bo->list_kref, ttm_bo_release_list);
787 return ret;
788}
789
Ben Skeggs42311ff2010-08-04 12:07:08 +1000790void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
791{
Ben Skeggsd961db72010-08-05 10:48:18 +1000792 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000793
Ben Skeggsd961db72010-08-05 10:48:18 +1000794 if (mem->mm_node)
795 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000796}
797EXPORT_SYMBOL(ttm_bo_mem_put);
798
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200799/**
Christian König3ddf4ad2016-06-15 13:44:03 +0200800 * Add the last move fence to the BO and reserve a new shared slot.
801 */
802static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
803 struct ttm_mem_type_manager *man,
804 struct ttm_mem_reg *mem)
805{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100806 struct dma_fence *fence;
Christian König3ddf4ad2016-06-15 13:44:03 +0200807 int ret;
808
809 spin_lock(&man->move_lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100810 fence = dma_fence_get(man->move);
Christian König3ddf4ad2016-06-15 13:44:03 +0200811 spin_unlock(&man->move_lock);
812
813 if (fence) {
814 reservation_object_add_shared_fence(bo->resv, fence);
815
816 ret = reservation_object_reserve_shared(bo->resv);
817 if (unlikely(ret))
818 return ret;
819
Chris Wilsonf54d1862016-10-25 13:00:45 +0100820 dma_fence_put(bo->moving);
Christian König3ddf4ad2016-06-15 13:44:03 +0200821 bo->moving = fence;
822 }
823
824 return 0;
825}
826
827/**
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200828 * Repeatedly evict memory from the LRU for @mem_type until we create enough
829 * space, or we've evicted everything and there isn't enough space.
830 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100831static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
832 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200833 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100834 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000835 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000836 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200837{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100838 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200839 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200840 int ret;
841
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200842 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200843 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200844 if (unlikely(ret != 0))
845 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000846 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100847 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900848 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000849 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100850 if (unlikely(ret != 0))
851 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200852 } while (1);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200853 mem->mem_type = mem_type;
Christian König3ddf4ad2016-06-15 13:44:03 +0200854 return ttm_bo_add_move_fence(bo, man, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200855}
856
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200857static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
858 uint32_t cur_placement,
859 uint32_t proposed_placement)
860{
861 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
862 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
863
864 /**
865 * Keep current caching if possible.
866 */
867
868 if ((cur_placement & caching) != 0)
869 result |= (cur_placement & caching);
870 else if ((man->default_caching & caching) != 0)
871 result |= man->default_caching;
872 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
873 result |= TTM_PL_FLAG_CACHED;
874 else if ((TTM_PL_FLAG_WC & caching) != 0)
875 result |= TTM_PL_FLAG_WC;
876 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
877 result |= TTM_PL_FLAG_UNCACHED;
878
879 return result;
880}
881
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200883 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200884 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200885 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200886{
887 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
888
Christian Königf1217ed2014-08-27 13:16:04 +0200889 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200890 return false;
891
Christian Königf1217ed2014-08-27 13:16:04 +0200892 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200893 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200894
Christian Königf1217ed2014-08-27 13:16:04 +0200895 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200896
897 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898 return true;
899}
900
901/**
902 * Creates space for memory region @mem according to its type.
903 *
904 * This function first searches for free space in compatible memory types in
905 * the priority order defined by the driver. If free space isn't found, then
906 * ttm_bo_mem_force_space is attempted in priority order to evict and find
907 * space.
908 */
909int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100910 struct ttm_placement *placement,
911 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000912 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000913 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200914{
915 struct ttm_bo_device *bdev = bo->bdev;
916 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200917 uint32_t mem_type = TTM_PL_SYSTEM;
918 uint32_t cur_flags = 0;
919 bool type_found = false;
920 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100921 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100922 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923
Christian König3ddf4ad2016-06-15 13:44:03 +0200924 ret = reservation_object_reserve_shared(bo->resv);
925 if (unlikely(ret))
926 return ret;
927
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200928 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000929 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200930 const struct ttm_place *place = &placement->placement[i];
931
932 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100933 if (ret)
934 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200935 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700936 if (!man->has_type || !man->use_type)
937 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938
Christian Königf1217ed2014-08-27 13:16:04 +0200939 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100940 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200941
942 if (!type_ok)
943 continue;
944
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700945 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200946 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
947 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100948 /*
949 * Use the access and other non-mapping-related flag bits from
950 * the memory placement flags to the current flags
951 */
Christian Königf1217ed2014-08-27 13:16:04 +0200952 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100953 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200954
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200955 if (mem_type == TTM_PL_SYSTEM)
956 break;
957
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700958 ret = (*man->func->get_node)(man, bo, place, mem);
959 if (unlikely(ret))
960 return ret;
Christian König3ddf4ad2016-06-15 13:44:03 +0200961
962 if (mem->mm_node) {
963 ret = ttm_bo_add_move_fence(bo, man, mem);
964 if (unlikely(ret)) {
965 (*man->func->put_node)(man, mem);
966 return ret;
967 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200968 break;
Christian König3ddf4ad2016-06-15 13:44:03 +0200969 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970 }
971
Ben Skeggsd961db72010-08-05 10:48:18 +1000972 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200973 mem->mem_type = mem_type;
974 mem->placement = cur_flags;
975 return 0;
976 }
977
Dave Airlieb6637522009-12-14 14:51:35 +1000978 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200979 const struct ttm_place *place = &placement->busy_placement[i];
980
981 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100982 if (ret)
983 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200984 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700985 if (!man->has_type || !man->use_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200986 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200987 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988 continue;
989
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700990 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200991 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
992 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100993 /*
994 * Use the access and other non-mapping-related flag bits from
995 * the memory placement flags to the current flags
996 */
Christian Königf1217ed2014-08-27 13:16:04 +0200997 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100998 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200999
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001000 if (mem_type == TTM_PL_SYSTEM) {
1001 mem->mem_type = mem_type;
1002 mem->placement = cur_flags;
1003 mem->mm_node = NULL;
1004 return 0;
1005 }
1006
Christian Königf1217ed2014-08-27 13:16:04 +02001007 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001008 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001009 if (ret == 0 && mem->mm_node) {
1010 mem->placement = cur_flags;
1011 return 0;
1012 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001013 if (ret == -ERESTARTSYS)
1014 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 }
Thomas Hellstrome30f3962015-09-14 01:24:41 -07001016
1017 if (!type_found) {
Joe Perches8dfe1622017-02-28 04:55:54 -08001018 pr_err(TTM_PFX "No compatible memory type found\n");
Thomas Hellstrome30f3962015-09-14 01:24:41 -07001019 return -EINVAL;
1020 }
1021
1022 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001023}
1024EXPORT_SYMBOL(ttm_bo_mem_space);
1025
Rashika Kheria6e87fa42014-01-06 22:12:58 +05301026static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001027 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001028 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001029 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001030{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001031 int ret = 0;
1032 struct ttm_mem_reg mem;
1033
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001034 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001035
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001036 mem.num_pages = bo->num_pages;
1037 mem.size = mem.num_pages << PAGE_SHIFT;
1038 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001039 mem.bus.io_reserved_vm = false;
1040 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001041 /*
1042 * Determine where to move the buffer.
1043 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001044 ret = ttm_bo_mem_space(bo, placement, &mem,
1045 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046 if (ret)
1047 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001048 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1049 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001051 if (ret && mem.mm_node)
1052 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001053 return ret;
1054}
1055
Christian König018b7fc2017-03-29 11:47:04 +02001056static bool ttm_bo_places_compat(const struct ttm_place *places,
1057 unsigned num_placement,
1058 struct ttm_mem_reg *mem,
1059 uint32_t *new_flags)
1060{
1061 unsigned i;
1062
1063 for (i = 0; i < num_placement; i++) {
1064 const struct ttm_place *heap = &places[i];
1065
1066 if (mem->mm_node && (mem->start < heap->fpfn ||
1067 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1068 continue;
1069
1070 *new_flags = heap->flags;
1071 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
Christian Königc8b26bd12017-03-29 12:13:54 +02001072 (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
1073 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1074 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
Christian König018b7fc2017-03-29 11:47:04 +02001075 return true;
1076 }
1077 return false;
1078}
1079
Sinclair Yeh94477bf2016-06-29 12:58:49 -07001080bool ttm_bo_mem_compat(struct ttm_placement *placement,
1081 struct ttm_mem_reg *mem,
1082 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001083{
Christian König018b7fc2017-03-29 11:47:04 +02001084 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1085 mem, new_flags))
1086 return true;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001087
Christian König018b7fc2017-03-29 11:47:04 +02001088 if ((placement->busy_placement != placement->placement ||
1089 placement->num_busy_placement > placement->num_placement) &&
1090 ttm_bo_places_compat(placement->busy_placement,
1091 placement->num_busy_placement,
1092 mem, new_flags))
1093 return true;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001094
1095 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001096}
Sinclair Yeh94477bf2016-06-29 12:58:49 -07001097EXPORT_SYMBOL(ttm_bo_mem_compat);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098
Jerome Glisse09855ac2009-12-10 17:16:27 +01001099int ttm_bo_validate(struct ttm_buffer_object *bo,
1100 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001101 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001102 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001103{
1104 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001105 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001107 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001108 /*
1109 * Check whether we need to move buffer.
1110 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001111 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001112 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1113 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001114 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001115 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001116 } else {
1117 /*
1118 * Use the access and other non-mapping-related flag bits from
1119 * the compatible memory placement flags to the active flags
1120 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001121 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001122 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001123 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001124 /*
1125 * We might need to add a TTM.
1126 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001127 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1128 ret = ttm_bo_add_ttm(bo, true);
1129 if (ret)
1130 return ret;
1131 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001132 return 0;
1133}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001134EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135
Nicolai Hähnleca9cf68d2017-02-16 10:56:40 +01001136int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1137 struct ttm_buffer_object *bo,
1138 unsigned long size,
1139 enum ttm_bo_type type,
1140 struct ttm_placement *placement,
1141 uint32_t page_alignment,
1142 bool interruptible,
1143 struct file *persistent_swap_storage,
1144 size_t acc_size,
1145 struct sg_table *sg,
1146 struct reservation_object *resv,
1147 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001148{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001149 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001150 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001151 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001152 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001153
1154 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1155 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001156 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001157 if (destroy)
1158 (*destroy)(bo);
1159 else
1160 kfree(bo);
1161 return -ENOMEM;
1162 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001163
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001164 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1165 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001166 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001167 if (destroy)
1168 (*destroy)(bo);
1169 else
1170 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001171 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001172 return -EINVAL;
1173 }
1174 bo->destroy = destroy;
1175
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001176 kref_init(&bo->kref);
1177 kref_init(&bo->list_kref);
1178 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 INIT_LIST_HEAD(&bo->lru);
1180 INIT_LIST_HEAD(&bo->ddestroy);
1181 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001182 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001183 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001184 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001185 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001186 bo->type = type;
1187 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001188 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001189 bo->mem.mem_type = TTM_PL_SYSTEM;
1190 bo->mem.num_pages = bo->num_pages;
1191 bo->mem.mm_node = NULL;
1192 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001193 bo->mem.bus.io_reserved_vm = false;
1194 bo->mem.bus.io_reserved_count = 0;
Christian König5bc73062016-06-15 13:44:01 +02001195 bo->moving = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001197 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001198 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001199 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001200 if (resv) {
1201 bo->resv = resv;
1202 lockdep_assert_held(&bo->resv->lock.base);
1203 } else {
1204 bo->resv = &bo->ttm_resv;
1205 reservation_object_init(&bo->ttm_resv);
1206 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001207 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001208 drm_vma_node_reset(&bo->vma_node);
Christian Königcf6c4672017-01-10 14:08:28 +01001209 bo->priority = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001210
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001211 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212 * For ttm_bo_type_device buffers, allocate
1213 * address space from the device.
1214 */
Christian Königf1217ed2014-08-27 13:16:04 +02001215 if (bo->type == ttm_bo_type_device ||
1216 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001217 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1218 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001220 /* passed reservation objects should already be locked,
1221 * since otherwise lockdep will be angered in radeon.
1222 */
1223 if (!resv) {
1224 locked = ww_mutex_trylock(&bo->resv->lock);
1225 WARN_ON(!locked);
1226 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001227
1228 if (likely(!ret))
1229 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001230
Nicolai Hähnlec2c139c2017-02-14 09:37:12 +01001231 if (unlikely(ret)) {
Nicolai Hähnleca9cf68d2017-02-16 10:56:40 +01001232 if (!resv)
1233 ttm_bo_unreserve(bo);
1234
Nicolai Hähnlec2c139c2017-02-14 09:37:12 +01001235 ttm_bo_unref(&bo);
1236 return ret;
1237 }
1238
1239 if (resv && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
Christian König33d48cf2016-01-11 15:35:18 +01001240 spin_lock(&bo->glob->lru_lock);
1241 ttm_bo_add_to_lru(bo);
1242 spin_unlock(&bo->glob->lru_lock);
1243 }
1244
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001245 return ret;
1246}
Nicolai Hähnleca9cf68d2017-02-16 10:56:40 +01001247EXPORT_SYMBOL(ttm_bo_init_reserved);
1248
1249int ttm_bo_init(struct ttm_bo_device *bdev,
1250 struct ttm_buffer_object *bo,
1251 unsigned long size,
1252 enum ttm_bo_type type,
1253 struct ttm_placement *placement,
1254 uint32_t page_alignment,
1255 bool interruptible,
1256 struct file *persistent_swap_storage,
1257 size_t acc_size,
1258 struct sg_table *sg,
1259 struct reservation_object *resv,
1260 void (*destroy) (struct ttm_buffer_object *))
1261{
1262 int ret;
1263
1264 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1265 page_alignment, interruptible,
1266 persistent_swap_storage, acc_size,
1267 sg, resv, destroy);
1268 if (ret)
1269 return ret;
1270
1271 if (!resv)
1272 ttm_bo_unreserve(bo);
1273
1274 return 0;
1275}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001276EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001277
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001278size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1279 unsigned long bo_size,
1280 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001281{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001282 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1283 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001284
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001285 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001286 size += ttm_round_pot(npages * sizeof(void *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001287 size += ttm_round_pot(sizeof(struct ttm_tt));
1288 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001289}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001290EXPORT_SYMBOL(ttm_bo_acc_size);
1291
1292size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1293 unsigned long bo_size,
1294 unsigned struct_size)
1295{
1296 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1297 size_t size = 0;
1298
1299 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001300 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001301 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1302 return size;
1303}
1304EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001305
Jerome Glisse09855ac2009-12-10 17:16:27 +01001306int ttm_bo_create(struct ttm_bo_device *bdev,
1307 unsigned long size,
1308 enum ttm_bo_type type,
1309 struct ttm_placement *placement,
1310 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001311 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001312 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001313 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001314{
1315 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001316 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001317 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001318
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001319 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001320 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001321 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001322
Thomas Hellstroma393c732012-06-12 13:28:42 +02001323 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001324 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001325 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001326 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001327 if (likely(ret == 0))
1328 *p_bo = bo;
1329
1330 return ret;
1331}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001332EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001333
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001334static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Christian König2ee7fc92017-01-06 19:16:07 +01001335 unsigned mem_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001336{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001337 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001338 struct ttm_bo_global *glob = bdev->glob;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001339 struct dma_fence *fence;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001340 int ret;
Christian Königcf6c4672017-01-10 14:08:28 +01001341 unsigned i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001342
1343 /*
1344 * Can't use standard list traversal since we're unlocking.
1345 */
1346
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001347 spin_lock(&glob->lru_lock);
Christian Königcf6c4672017-01-10 14:08:28 +01001348 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1349 while (!list_empty(&man->lru[i])) {
1350 spin_unlock(&glob->lru_lock);
1351 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
1352 if (ret)
Jerome Glisseca262a9992009-12-08 15:33:32 +01001353 return ret;
Christian Königcf6c4672017-01-10 14:08:28 +01001354 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001355 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001356 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001357 spin_unlock(&glob->lru_lock);
Christian Königaff98ba2016-06-22 14:16:28 +02001358
1359 spin_lock(&man->move_lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001360 fence = dma_fence_get(man->move);
Christian Königaff98ba2016-06-22 14:16:28 +02001361 spin_unlock(&man->move_lock);
1362
1363 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001364 ret = dma_fence_wait(fence, false);
1365 dma_fence_put(fence);
Christian König2ee7fc92017-01-06 19:16:07 +01001366 if (ret)
1367 return ret;
Christian Königaff98ba2016-06-22 14:16:28 +02001368 }
1369
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001370 return 0;
1371}
1372
1373int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1374{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001375 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001376 int ret = -EINVAL;
1377
1378 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001379 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001380 return ret;
1381 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001382 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001383
1384 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001385 pr_err("Trying to take down uninitialized memory manager type %u\n",
1386 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001387 return ret;
1388 }
1389
1390 man->use_type = false;
1391 man->has_type = false;
1392
1393 ret = 0;
1394 if (mem_type > 0) {
Christian König2ee7fc92017-01-06 19:16:07 +01001395 ret = ttm_bo_force_list_clean(bdev, mem_type);
1396 if (ret) {
1397 pr_err("Cleanup eviction failed\n");
1398 return ret;
1399 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001400
Ben Skeggsd961db72010-08-05 10:48:18 +10001401 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001402 }
1403
John Brooks8046e192017-07-03 14:05:34 -04001404 dma_fence_put(man->move);
1405 man->move = NULL;
1406
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001407 return ret;
1408}
1409EXPORT_SYMBOL(ttm_bo_clean_mm);
1410
1411int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1412{
1413 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1414
1415 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001416 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001417 return -EINVAL;
1418 }
1419
1420 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001421 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001422 return 0;
1423 }
1424
Christian König2ee7fc92017-01-06 19:16:07 +01001425 return ttm_bo_force_list_clean(bdev, mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001426}
1427EXPORT_SYMBOL(ttm_bo_evict_mm);
1428
1429int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001430 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001431{
Huang Ruiaef1ba52017-04-17 15:32:52 +08001432 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001433 struct ttm_mem_type_manager *man;
Christian Königcf6c4672017-01-10 14:08:28 +01001434 unsigned i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001435
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001436 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001437 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001438 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001439 man->io_reserve_fastpath = true;
1440 man->use_io_reserve_lru = false;
1441 mutex_init(&man->io_reserve_mutex);
Christian König3ddf4ad2016-06-15 13:44:03 +02001442 spin_lock_init(&man->move_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001443 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001444
1445 ret = bdev->driver->init_mem_type(bdev, type, man);
1446 if (ret)
1447 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001448 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001449
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001450 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001451 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001452 if (ret)
1453 return ret;
1454 }
1455 man->has_type = true;
1456 man->use_type = true;
1457 man->size = p_size;
1458
Christian Königcf6c4672017-01-10 14:08:28 +01001459 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1460 INIT_LIST_HEAD(&man->lru[i]);
Christian König3ddf4ad2016-06-15 13:44:03 +02001461 man->move = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001462
1463 return 0;
1464}
1465EXPORT_SYMBOL(ttm_bo_init_mm);
1466
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001467static void ttm_bo_global_kobj_release(struct kobject *kobj)
1468{
1469 struct ttm_bo_global *glob =
1470 container_of(kobj, struct ttm_bo_global, kobj);
1471
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001472 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1473 __free_page(glob->dummy_read_page);
1474 kfree(glob);
1475}
1476
Dave Airlieba4420c2010-03-09 10:56:52 +10001477void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001478{
1479 struct ttm_bo_global *glob = ref->object;
1480
1481 kobject_del(&glob->kobj);
1482 kobject_put(&glob->kobj);
1483}
1484EXPORT_SYMBOL(ttm_bo_global_release);
1485
Dave Airlieba4420c2010-03-09 10:56:52 +10001486int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001487{
1488 struct ttm_bo_global_ref *bo_ref =
1489 container_of(ref, struct ttm_bo_global_ref, ref);
1490 struct ttm_bo_global *glob = ref->object;
1491 int ret;
Christian Königcf6c4672017-01-10 14:08:28 +01001492 unsigned i;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001493
1494 mutex_init(&glob->device_list_mutex);
1495 spin_lock_init(&glob->lru_lock);
1496 glob->mem_glob = bo_ref->mem_glob;
1497 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1498
1499 if (unlikely(glob->dummy_read_page == NULL)) {
1500 ret = -ENOMEM;
1501 goto out_no_drp;
1502 }
1503
Christian Königcf6c4672017-01-10 14:08:28 +01001504 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1505 INIT_LIST_HEAD(&glob->swap_lru[i]);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001506 INIT_LIST_HEAD(&glob->device_list);
1507
1508 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1509 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1510 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001511 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001512 goto out_no_shrink;
1513 }
1514
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001515 atomic_set(&glob->bo_count, 0);
1516
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001517 ret = kobject_init_and_add(
1518 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001519 if (unlikely(ret != 0))
1520 kobject_put(&glob->kobj);
1521 return ret;
1522out_no_shrink:
1523 __free_page(glob->dummy_read_page);
1524out_no_drp:
1525 kfree(glob);
1526 return ret;
1527}
1528EXPORT_SYMBOL(ttm_bo_global_init);
1529
1530
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531int ttm_bo_device_release(struct ttm_bo_device *bdev)
1532{
1533 int ret = 0;
1534 unsigned i = TTM_NUM_MEM_TYPES;
1535 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001536 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001537
1538 while (i--) {
1539 man = &bdev->man[i];
1540 if (man->has_type) {
1541 man->use_type = false;
1542 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1543 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001544 pr_err("DRM memory manager type %d is not clean\n",
1545 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001546 }
1547 man->has_type = false;
1548 }
1549 }
1550
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001551 mutex_lock(&glob->device_list_mutex);
1552 list_del(&bdev->device_list);
1553 mutex_unlock(&glob->device_list_mutex);
1554
Tejun Heof094cfc2010-12-24 15:59:06 +01001555 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001556
1557 while (ttm_bo_delayed_delete(bdev, true))
1558 ;
1559
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001560 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001561 if (list_empty(&bdev->ddestroy))
1562 TTM_DEBUG("Delayed destroy list was clean\n");
1563
Christian Königcf6c4672017-01-10 14:08:28 +01001564 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1565 if (list_empty(&bdev->man[0].lru[0]))
1566 TTM_DEBUG("Swap list %d was clean\n", i);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001567 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001568
David Herrmann72525b32013-07-24 21:08:53 +02001569 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001570
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001571 return ret;
1572}
1573EXPORT_SYMBOL(ttm_bo_device_release);
1574
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001576 struct ttm_bo_global *glob,
1577 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001578 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001579 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001580 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001581{
1582 int ret = -EINVAL;
1583
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001584 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001585
1586 memset(bdev->man, 0, sizeof(bdev->man));
1587
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001588 /*
1589 * Initialize the system memory buffer type.
1590 * Other types need to be driver / IOCTL initialized.
1591 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001592 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001593 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001594 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001595
David Herrmann72525b32013-07-24 21:08:53 +02001596 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1597 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001598 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001599 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001600 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001601 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001602 bdev->need_dma32 = need_dma32;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001603 mutex_lock(&glob->device_list_mutex);
1604 list_add_tail(&bdev->device_list, &glob->device_list);
1605 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001606
1607 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001608out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001609 return ret;
1610}
1611EXPORT_SYMBOL(ttm_bo_device_init);
1612
1613/*
1614 * buffer object vm functions.
1615 */
1616
1617bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1618{
1619 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1620
1621 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1622 if (mem->mem_type == TTM_PL_SYSTEM)
1623 return false;
1624
1625 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1626 return false;
1627
1628 if (mem->placement & TTM_PL_FLAG_CACHED)
1629 return false;
1630 }
1631 return true;
1632}
1633
Thomas Hellstromeba67092010-11-11 09:41:57 +01001634void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001635{
1636 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001637
David Herrmann51335df2013-07-24 21:10:03 +02001638 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001639 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001640}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001641
1642void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1643{
1644 struct ttm_bo_device *bdev = bo->bdev;
1645 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1646
1647 ttm_mem_io_lock(man, false);
1648 ttm_bo_unmap_virtual_locked(bo);
1649 ttm_mem_io_unlock(man);
1650}
1651
1652
Dave Airliee024e112009-06-24 09:48:08 +10001653EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001654
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001655int ttm_bo_wait(struct ttm_buffer_object *bo,
Christian König8aa6d4f2016-04-06 11:12:04 +02001656 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001657{
Christian König98a6dd92016-11-07 16:16:15 -05001658 long timeout = 15 * HZ;
1659
1660 if (no_wait) {
1661 if (reservation_object_test_signaled_rcu(bo->resv, true))
1662 return 0;
1663 else
1664 return -EBUSY;
1665 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001666
Christian Königf849c6d2016-06-15 13:44:02 +02001667 timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
1668 interruptible, timeout);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001669 if (timeout < 0)
1670 return timeout;
1671
1672 if (timeout == 0)
1673 return -EBUSY;
1674
Christian Königf849c6d2016-06-15 13:44:02 +02001675 reservation_object_add_excl_fence(bo->resv, NULL);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001676 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001677}
1678EXPORT_SYMBOL(ttm_bo_wait);
1679
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001680int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1681{
1682 int ret = 0;
1683
1684 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001685 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001686 */
1687
Christian Königdfd5e502016-04-06 11:12:03 +02001688 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001689 if (unlikely(ret != 0))
1690 return ret;
Christian König8aa6d4f2016-04-06 11:12:04 +02001691 ret = ttm_bo_wait(bo, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001692 if (likely(ret == 0))
1693 atomic_inc(&bo->cpu_writers);
1694 ttm_bo_unreserve(bo);
1695 return ret;
1696}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001697EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001698
1699void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1700{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001701 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001702}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001703EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001704
1705/**
1706 * A buffer object shrink method that tries to swap out the first
1707 * buffer object on the bo_global::swap_lru list.
1708 */
1709
1710static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1711{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001712 struct ttm_bo_global *glob =
1713 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001714 struct ttm_buffer_object *bo;
1715 int ret = -EBUSY;
Christian Königcf6c4672017-01-10 14:08:28 +01001716 unsigned i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001717
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001718 spin_lock(&glob->lru_lock);
Christian Königcf6c4672017-01-10 14:08:28 +01001719 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1720 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1721 ret = __ttm_bo_reserve(bo, false, true, NULL);
1722 if (!ret)
1723 break;
1724 }
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001725 if (!ret)
1726 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001727 }
1728
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001729 if (ret) {
1730 spin_unlock(&glob->lru_lock);
1731 return ret;
1732 }
1733
1734 kref_get(&bo->list_kref);
1735
1736 if (!list_empty(&bo->ddestroy)) {
1737 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1738 kref_put(&bo->list_kref, ttm_bo_release_list);
1739 return ret;
1740 }
1741
Peter Zijlstrabdfafc42016-11-14 17:34:19 +01001742 ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001743 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001744
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001745 /**
Christian König61ede072016-06-06 10:17:57 +02001746 * Move to system cached
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001747 */
1748
Michel Dänzer239ac652017-01-25 17:21:31 +09001749 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1750 bo->ttm->caching_state != tt_cached) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001751 struct ttm_mem_reg evict_mem;
1752
1753 evict_mem = bo->mem;
1754 evict_mem.mm_node = NULL;
1755 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1756 evict_mem.mem_type = TTM_PL_SYSTEM;
1757
1758 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001759 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001760 if (unlikely(ret != 0))
1761 goto out;
1762 }
1763
Christian König61ede072016-06-06 10:17:57 +02001764 /**
1765 * Make sure BO is idle.
1766 */
1767
1768 ret = ttm_bo_wait(bo, false, false);
1769 if (unlikely(ret != 0))
1770 goto out;
1771
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001772 ttm_bo_unmap_virtual(bo);
1773
1774 /**
1775 * Swap out. Buffer will be swapped in again as soon as
1776 * anyone tries to access a ttm page.
1777 */
1778
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001779 if (bo->bdev->driver->swap_notify)
1780 bo->bdev->driver->swap_notify(bo);
1781
Jan Engelhardt5df23972011-04-04 01:25:18 +02001782 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001783out:
1784
1785 /**
1786 *
1787 * Unreserve without putting on LRU to avoid swapping out an
1788 * already swapped buffer.
1789 */
1790
Thomas Hellstromc7523082014-02-20 11:36:25 +01001791 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001792 kref_put(&bo->list_kref, ttm_bo_release_list);
1793 return ret;
1794}
1795
1796void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1797{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001798 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001799 ;
1800}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001801EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001802
1803/**
1804 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1805 * unreserved
1806 *
1807 * @bo: Pointer to buffer
1808 */
1809int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1810{
1811 int ret;
1812
1813 /*
1814 * In the absense of a wait_unlocked API,
1815 * Use the bo::wu_mutex to avoid triggering livelocks due to
1816 * concurrent use of this function. Note that this use of
1817 * bo::wu_mutex can go away if we change locking order to
1818 * mmap_sem -> bo::reserve.
1819 */
1820 ret = mutex_lock_interruptible(&bo->wu_mutex);
1821 if (unlikely(ret != 0))
1822 return -ERESTARTSYS;
1823 if (!ww_mutex_is_locked(&bo->resv->lock))
1824 goto out_unlock;
Christian Königdfd5e502016-04-06 11:12:03 +02001825 ret = __ttm_bo_reserve(bo, true, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001826 if (unlikely(ret != 0))
1827 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001828 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001829
1830out_unlock:
1831 mutex_unlock(&bo->wu_mutex);
1832 return ret;
1833}