blob: a6d7fcb99c0bd01228bfe5b47bd4c9c526b1a844 [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
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200397static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
398{
399 struct reservation_object_list *fobj;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100400 struct dma_fence *fence;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200401 int i;
402
403 fobj = reservation_object_get_list(bo->resv);
404 fence = reservation_object_get_excl(bo->resv);
405 if (fence && !fence->ops->signaled)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100406 dma_fence_enable_sw_signaling(fence);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200407
408 for (i = 0; fobj && i < fobj->shared_count; ++i) {
409 fence = rcu_dereference_protected(fobj->shared[i],
410 reservation_object_held(bo->resv));
411
412 if (!fence->ops->signaled)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100413 dma_fence_enable_sw_signaling(fence);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200414 }
415}
416
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200417static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200418{
419 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200420 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200421 int ret;
422
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100423 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200424 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100425
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700426 if (!ret) {
Christian König8aa6d4f2016-04-06 11:12:04 +0200427 if (!ttm_bo_wait(bo, false, true)) {
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100428 ttm_bo_del_from_lru(bo);
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100429 spin_unlock(&glob->lru_lock);
430 ttm_bo_cleanup_memtype_use(bo);
431
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100432 return;
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +0200433 } else
434 ttm_bo_flush_all_fences(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700435
436 /*
437 * Make NO_EVICT bos immediately available to
438 * shrinkers, now that they are queued for
439 * destruction.
440 */
441 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
442 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
443 ttm_bo_add_to_lru(bo);
444 }
445
Thomas Hellstromc7523082014-02-20 11:36:25 +0100446 __ttm_bo_unreserve(bo);
Thomas Hellstrom15205fb2013-10-10 11:09:03 -0700447 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200448
449 kref_get(&bo->list_kref);
450 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
451 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200452
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200453 schedule_delayed_work(&bdev->wq,
454 ((HZ / 100) < 1) ? 1 : HZ / 100);
455}
456
457/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000458 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200459 * If bo idle, remove from delayed- and lru lists, and unref.
460 * If not idle, do nothing.
461 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000462 * Must be called with lru_lock and reservation held, this function
463 * will drop both before returning.
464 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200465 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200466 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
467 */
468
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000469static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
470 bool interruptible,
471 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200472{
473 struct ttm_bo_global *glob = bo->glob;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000474 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200475
Christian König8aa6d4f2016-04-06 11:12:04 +0200476 ret = ttm_bo_wait(bo, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200477
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000478 if (ret && !no_wait_gpu) {
Maarten Lankhorst472db7a2014-05-14 15:42:29 +0200479 long lret;
480 ww_mutex_unlock(&bo->resv->lock);
481 spin_unlock(&glob->lru_lock);
482
483 lret = reservation_object_wait_timeout_rcu(bo->resv,
484 true,
485 interruptible,
486 30 * HZ);
487
488 if (lret < 0)
489 return lret;
490 else if (lret == 0)
491 return -EBUSY;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000492
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000493 spin_lock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200494 ret = __ttm_bo_reserve(bo, false, true, NULL);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000495
496 /*
497 * We raced, and lost, someone else holds the reservation now,
498 * and is probably busy in ttm_bo_cleanup_memtype_use.
499 *
500 * Even if it's not the case, because we finished waiting any
501 * delayed destruction would succeed, so just return success
502 * here.
503 */
504 if (ret) {
505 spin_unlock(&glob->lru_lock);
506 return 0;
507 }
Maarten Lankhorst70401382014-01-21 13:07:01 +0100508
509 /*
510 * remove sync_obj with ttm_bo_wait, the wait should be
511 * finished, and no new wait object should have been added.
512 */
Christian König8aa6d4f2016-04-06 11:12:04 +0200513 ret = ttm_bo_wait(bo, false, true);
Maarten Lankhorst70401382014-01-21 13:07:01 +0100514 WARN_ON(ret);
515 }
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000516
517 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Thomas Hellstromc7523082014-02-20 11:36:25 +0100518 __ttm_bo_unreserve(bo);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000519 spin_unlock(&glob->lru_lock);
520 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200521 }
522
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100523 ttm_bo_del_from_lru(bo);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200524 list_del_init(&bo->ddestroy);
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100525 kref_put(&bo->list_kref, ttm_bo_ref_bug);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200526
527 spin_unlock(&glob->lru_lock);
528 ttm_bo_cleanup_memtype_use(bo);
529
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200530 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200531}
532
533/**
534 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
535 * encountered buffers.
536 */
537
538static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
539{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200540 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100541 struct ttm_buffer_object *entry = NULL;
542 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200543
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200544 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100545 if (list_empty(&bdev->ddestroy))
546 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200547
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100548 entry = list_first_entry(&bdev->ddestroy,
549 struct ttm_buffer_object, ddestroy);
550 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200551
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100552 for (;;) {
553 struct ttm_buffer_object *nentry = NULL;
554
555 if (entry->ddestroy.next != &bdev->ddestroy) {
556 nentry = list_first_entry(&entry->ddestroy,
557 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200558 kref_get(&nentry->list_kref);
559 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200560
Christian Königdfd5e502016-04-06 11:12:03 +0200561 ret = __ttm_bo_reserve(entry, false, true, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100562 if (remove_all && ret) {
563 spin_unlock(&glob->lru_lock);
Christian Königdfd5e502016-04-06 11:12:03 +0200564 ret = __ttm_bo_reserve(entry, false, false, NULL);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100565 spin_lock(&glob->lru_lock);
566 }
567
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000568 if (!ret)
569 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
570 !remove_all);
571 else
572 spin_unlock(&glob->lru_lock);
573
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200574 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100575 entry = nentry;
576
577 if (ret || !entry)
578 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200579
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200580 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100581 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200582 break;
583 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200584
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100585out_unlock:
586 spin_unlock(&glob->lru_lock);
587out:
588 if (entry)
589 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200590 return ret;
591}
592
593static void ttm_bo_delayed_workqueue(struct work_struct *work)
594{
595 struct ttm_bo_device *bdev =
596 container_of(work, struct ttm_bo_device, wq.work);
597
598 if (ttm_bo_delayed_delete(bdev, false)) {
599 schedule_delayed_work(&bdev->wq,
600 ((HZ / 100) < 1) ? 1 : HZ / 100);
601 }
602}
603
604static void ttm_bo_release(struct kref *kref)
605{
606 struct ttm_buffer_object *bo =
607 container_of(kref, struct ttm_buffer_object, kref);
608 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100609 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200610
David Herrmann72525b32013-07-24 21:08:53 +0200611 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100612 ttm_mem_io_lock(man, false);
613 ttm_mem_io_free_vm(bo);
614 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200615 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200617}
618
619void ttm_bo_unref(struct ttm_buffer_object **p_bo)
620{
621 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200622
623 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200624 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200625}
626EXPORT_SYMBOL(ttm_bo_unref);
627
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400628int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
629{
630 return cancel_delayed_work_sync(&bdev->wq);
631}
632EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
633
634void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
635{
636 if (resched)
637 schedule_delayed_work(&bdev->wq,
638 ((HZ / 100) < 1) ? 1 : HZ / 100);
639}
640EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
641
Jerome Glisseca262a9992009-12-08 15:33:32 +0100642static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000643 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200644{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200645 struct ttm_bo_device *bdev = bo->bdev;
646 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100647 struct ttm_placement placement;
648 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649
Maarten Lankhorst009a9da2013-06-27 13:48:25 +0200650 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200651
652 evict_mem = bo->mem;
653 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100654 evict_mem.bus.io_reserved_vm = false;
655 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200656
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100657 placement.num_placement = 0;
658 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100659 bdev->driver->evict_flags(bo, &placement);
660 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000661 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200662 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100663 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700664 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
665 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100666 ttm_bo_mem_space_debug(bo, &placement);
667 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200668 goto out;
669 }
670
671 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000672 no_wait_gpu);
Christian König17d33bc2016-06-06 10:17:56 +0200673 if (unlikely(ret)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100674 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700675 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000676 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200677 goto out;
678 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200679 bo->evicted = true;
680out:
681 return ret;
682}
683
Christian Königa2ab19fe2016-08-30 17:26:04 +0200684bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
685 const struct ttm_place *place)
686{
687 /* Don't evict this BO if it's outside of the
688 * requested placement range
689 */
690 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
691 (place->lpfn && place->lpfn <= bo->mem.start))
692 return false;
693
694 return true;
695}
696EXPORT_SYMBOL(ttm_bo_eviction_valuable);
697
Jerome Glisseca262a9992009-12-08 15:33:32 +0100698static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
699 uint32_t mem_type,
Michel Dänzere3001802014-10-09 15:02:59 +0900700 const struct ttm_place *place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000701 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000702 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100703{
704 struct ttm_bo_global *glob = bdev->glob;
705 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
706 struct ttm_buffer_object *bo;
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100707 int ret = -EBUSY;
Christian Königcf6c4672017-01-10 14:08:28 +0100708 unsigned i;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100709
710 spin_lock(&glob->lru_lock);
Christian Königcf6c4672017-01-10 14:08:28 +0100711 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
712 list_for_each_entry(bo, &man->lru[i], lru) {
713 ret = __ttm_bo_reserve(bo, false, true, NULL);
714 if (ret)
715 continue;
Michel Dänzere3001802014-10-09 15:02:59 +0900716
Christian Königcf6c4672017-01-10 14:08:28 +0100717 if (place && !bdev->driver->eviction_valuable(bo,
718 place)) {
719 __ttm_bo_unreserve(bo);
720 ret = -EBUSY;
721 continue;
722 }
723
724 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900725 }
Christian Königa2ab19fe2016-08-30 17:26:04 +0200726
Christian Königcf6c4672017-01-10 14:08:28 +0100727 if (!ret)
728 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100729 }
730
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000731 if (ret) {
732 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000733 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200734 }
735
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000736 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100737
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000738 if (!list_empty(&bo->ddestroy)) {
739 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
740 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100741 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000742 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100743 }
744
Peter Zijlstrabdfafc42016-11-14 17:34:19 +0100745 ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100746 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100747
748 BUG_ON(ret != 0);
749
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000750 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100751 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100752
Jerome Glisseca262a9992009-12-08 15:33:32 +0100753 kref_put(&bo->list_kref, ttm_bo_release_list);
754 return ret;
755}
756
Ben Skeggs42311ff2010-08-04 12:07:08 +1000757void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
758{
Ben Skeggsd961db72010-08-05 10:48:18 +1000759 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000760
Ben Skeggsd961db72010-08-05 10:48:18 +1000761 if (mem->mm_node)
762 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000763}
764EXPORT_SYMBOL(ttm_bo_mem_put);
765
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200766/**
Christian König3ddf4ad2016-06-15 13:44:03 +0200767 * Add the last move fence to the BO and reserve a new shared slot.
768 */
769static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
770 struct ttm_mem_type_manager *man,
771 struct ttm_mem_reg *mem)
772{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100773 struct dma_fence *fence;
Christian König3ddf4ad2016-06-15 13:44:03 +0200774 int ret;
775
776 spin_lock(&man->move_lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100777 fence = dma_fence_get(man->move);
Christian König3ddf4ad2016-06-15 13:44:03 +0200778 spin_unlock(&man->move_lock);
779
780 if (fence) {
781 reservation_object_add_shared_fence(bo->resv, fence);
782
783 ret = reservation_object_reserve_shared(bo->resv);
784 if (unlikely(ret))
785 return ret;
786
Chris Wilsonf54d1862016-10-25 13:00:45 +0100787 dma_fence_put(bo->moving);
Christian König3ddf4ad2016-06-15 13:44:03 +0200788 bo->moving = fence;
789 }
790
791 return 0;
792}
793
794/**
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200795 * Repeatedly evict memory from the LRU for @mem_type until we create enough
796 * space, or we've evicted everything and there isn't enough space.
797 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100798static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
799 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200800 const struct ttm_place *place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100801 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000802 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000803 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200804{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100805 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200806 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200807 int ret;
808
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200809 do {
Christian Königf1217ed2014-08-27 13:16:04 +0200810 ret = (*man->func->get_node)(man, bo, place, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200811 if (unlikely(ret != 0))
812 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000813 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100814 break;
Michel Dänzere3001802014-10-09 15:02:59 +0900815 ret = ttm_mem_evict_first(bdev, mem_type, place,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000816 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100817 if (unlikely(ret != 0))
818 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200819 } while (1);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200820 mem->mem_type = mem_type;
Christian König3ddf4ad2016-06-15 13:44:03 +0200821 return ttm_bo_add_move_fence(bo, man, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200822}
823
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200824static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
825 uint32_t cur_placement,
826 uint32_t proposed_placement)
827{
828 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
829 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
830
831 /**
832 * Keep current caching if possible.
833 */
834
835 if ((cur_placement & caching) != 0)
836 result |= (cur_placement & caching);
837 else if ((man->default_caching & caching) != 0)
838 result |= man->default_caching;
839 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
840 result |= TTM_PL_FLAG_CACHED;
841 else if ((TTM_PL_FLAG_WC & caching) != 0)
842 result |= TTM_PL_FLAG_WC;
843 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
844 result |= TTM_PL_FLAG_UNCACHED;
845
846 return result;
847}
848
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200849static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200850 uint32_t mem_type,
Christian Königf1217ed2014-08-27 13:16:04 +0200851 const struct ttm_place *place,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200852 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200853{
854 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
855
Christian Königf1217ed2014-08-27 13:16:04 +0200856 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200857 return false;
858
Christian Königf1217ed2014-08-27 13:16:04 +0200859 if ((place->flags & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200860 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200861
Christian Königf1217ed2014-08-27 13:16:04 +0200862 cur_flags |= (place->flags & man->available_caching);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200863
864 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865 return true;
866}
867
868/**
869 * Creates space for memory region @mem according to its type.
870 *
871 * This function first searches for free space in compatible memory types in
872 * the priority order defined by the driver. If free space isn't found, then
873 * ttm_bo_mem_force_space is attempted in priority order to evict and find
874 * space.
875 */
876int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100877 struct ttm_placement *placement,
878 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000879 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000880 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200881{
882 struct ttm_bo_device *bdev = bo->bdev;
883 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200884 uint32_t mem_type = TTM_PL_SYSTEM;
885 uint32_t cur_flags = 0;
886 bool type_found = false;
887 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100888 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100889 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200890
Christian König3ddf4ad2016-06-15 13:44:03 +0200891 ret = reservation_object_reserve_shared(bo->resv);
892 if (unlikely(ret))
893 return ret;
894
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200895 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000896 for (i = 0; i < placement->num_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200897 const struct ttm_place *place = &placement->placement[i];
898
899 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100900 if (ret)
901 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200902 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700903 if (!man->has_type || !man->use_type)
904 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200905
Christian Königf1217ed2014-08-27 13:16:04 +0200906 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100907 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200908
909 if (!type_ok)
910 continue;
911
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700912 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200913 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
914 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100915 /*
916 * Use the access and other non-mapping-related flag bits from
917 * the memory placement flags to the current flags
918 */
Christian Königf1217ed2014-08-27 13:16:04 +0200919 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100920 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200921
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922 if (mem_type == TTM_PL_SYSTEM)
923 break;
924
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700925 ret = (*man->func->get_node)(man, bo, place, mem);
926 if (unlikely(ret))
927 return ret;
Christian König3ddf4ad2016-06-15 13:44:03 +0200928
929 if (mem->mm_node) {
930 ret = ttm_bo_add_move_fence(bo, man, mem);
931 if (unlikely(ret)) {
932 (*man->func->put_node)(man, mem);
933 return ret;
934 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200935 break;
Christian König3ddf4ad2016-06-15 13:44:03 +0200936 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200937 }
938
Ben Skeggsd961db72010-08-05 10:48:18 +1000939 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200940 mem->mem_type = mem_type;
941 mem->placement = cur_flags;
942 return 0;
943 }
944
Dave Airlieb6637522009-12-14 14:51:35 +1000945 for (i = 0; i < placement->num_busy_placement; ++i) {
Christian Königf1217ed2014-08-27 13:16:04 +0200946 const struct ttm_place *place = &placement->busy_placement[i];
947
948 ret = ttm_mem_type_from_place(place, &mem_type);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100949 if (ret)
950 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951 man = &bdev->man[mem_type];
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700952 if (!man->has_type || !man->use_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953 continue;
Christian Königf1217ed2014-08-27 13:16:04 +0200954 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200955 continue;
956
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700957 type_found = true;
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200958 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
959 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100960 /*
961 * Use the access and other non-mapping-related flag bits from
962 * the memory placement flags to the current flags
963 */
Christian Königf1217ed2014-08-27 13:16:04 +0200964 ttm_flag_masked(&cur_flags, place->flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100965 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200966
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100967 if (mem_type == TTM_PL_SYSTEM) {
968 mem->mem_type = mem_type;
969 mem->placement = cur_flags;
970 mem->mm_node = NULL;
971 return 0;
972 }
973
Christian Königf1217ed2014-08-27 13:16:04 +0200974 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000975 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976 if (ret == 0 && mem->mm_node) {
977 mem->placement = cur_flags;
978 return 0;
979 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100980 if (ret == -ERESTARTSYS)
981 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200982 }
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700983
984 if (!type_found) {
Joe Perches8dfe1622017-02-28 04:55:54 -0800985 pr_err(TTM_PFX "No compatible memory type found\n");
Thomas Hellstrome30f3962015-09-14 01:24:41 -0700986 return -EINVAL;
987 }
988
989 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200990}
991EXPORT_SYMBOL(ttm_bo_mem_space);
992
Rashika Kheria6e87fa42014-01-06 22:12:58 +0530993static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100994 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000995 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000996 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200998 int ret = 0;
999 struct ttm_mem_reg mem;
1000
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001001 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001002
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001003 mem.num_pages = bo->num_pages;
1004 mem.size = mem.num_pages << PAGE_SHIFT;
1005 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001006 mem.bus.io_reserved_vm = false;
1007 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001008 /*
1009 * Determine where to move the buffer.
1010 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001011 ret = ttm_bo_mem_space(bo, placement, &mem,
1012 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 if (ret)
1014 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001015 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1016 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001017out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001018 if (ret && mem.mm_node)
1019 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001020 return ret;
1021}
1022
Christian König018b7fc2017-03-29 11:47:04 +02001023static bool ttm_bo_places_compat(const struct ttm_place *places,
1024 unsigned num_placement,
1025 struct ttm_mem_reg *mem,
1026 uint32_t *new_flags)
1027{
1028 unsigned i;
1029
1030 for (i = 0; i < num_placement; i++) {
1031 const struct ttm_place *heap = &places[i];
1032
1033 if (mem->mm_node && (mem->start < heap->fpfn ||
1034 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1035 continue;
1036
1037 *new_flags = heap->flags;
1038 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
Christian Königc8b26bd12017-03-29 12:13:54 +02001039 (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
1040 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1041 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
Christian König018b7fc2017-03-29 11:47:04 +02001042 return true;
1043 }
1044 return false;
1045}
1046
Sinclair Yeh94477bf2016-06-29 12:58:49 -07001047bool ttm_bo_mem_compat(struct ttm_placement *placement,
1048 struct ttm_mem_reg *mem,
1049 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050{
Christian König018b7fc2017-03-29 11:47:04 +02001051 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1052 mem, new_flags))
1053 return true;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001054
Christian König018b7fc2017-03-29 11:47:04 +02001055 if ((placement->busy_placement != placement->placement ||
1056 placement->num_busy_placement > placement->num_placement) &&
1057 ttm_bo_places_compat(placement->busy_placement,
1058 placement->num_busy_placement,
1059 mem, new_flags))
1060 return true;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001061
1062 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001063}
Sinclair Yeh94477bf2016-06-29 12:58:49 -07001064EXPORT_SYMBOL(ttm_bo_mem_compat);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001065
Jerome Glisse09855ac2009-12-10 17:16:27 +01001066int ttm_bo_validate(struct ttm_buffer_object *bo,
1067 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001068 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001069 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001070{
1071 int ret;
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001072 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001073
Maarten Lankhorst009a9da2013-06-27 13:48:25 +02001074 lockdep_assert_held(&bo->resv->lock.base);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001075 /*
1076 * Check whether we need to move buffer.
1077 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001078 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001079 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1080 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001081 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001083 } else {
1084 /*
1085 * Use the access and other non-mapping-related flag bits from
1086 * the compatible memory placement flags to the active flags
1087 */
Thomas Hellstrom59c8e662013-10-28 02:02:19 -07001088 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001089 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001090 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001091 /*
1092 * We might need to add a TTM.
1093 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001094 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1095 ret = ttm_bo_add_ttm(bo, true);
1096 if (ret)
1097 return ret;
1098 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001099 return 0;
1100}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001101EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102
Nicolai Hähnleca9cf68d2017-02-16 10:56:40 +01001103int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1104 struct ttm_buffer_object *bo,
1105 unsigned long size,
1106 enum ttm_bo_type type,
1107 struct ttm_placement *placement,
1108 uint32_t page_alignment,
1109 bool interruptible,
1110 struct file *persistent_swap_storage,
1111 size_t acc_size,
1112 struct sg_table *sg,
1113 struct reservation_object *resv,
1114 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001115{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001116 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001118 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001119 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001120
1121 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1122 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001123 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001124 if (destroy)
1125 (*destroy)(bo);
1126 else
1127 kfree(bo);
1128 return -ENOMEM;
1129 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001130
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001131 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1132 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001133 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001134 if (destroy)
1135 (*destroy)(bo);
1136 else
1137 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001138 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001139 return -EINVAL;
1140 }
1141 bo->destroy = destroy;
1142
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143 kref_init(&bo->kref);
1144 kref_init(&bo->list_kref);
1145 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146 INIT_LIST_HEAD(&bo->lru);
1147 INIT_LIST_HEAD(&bo->ddestroy);
1148 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001149 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001150 mutex_init(&bo->wu_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001151 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001152 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 bo->type = type;
1154 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001155 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001156 bo->mem.mem_type = TTM_PL_SYSTEM;
1157 bo->mem.num_pages = bo->num_pages;
1158 bo->mem.mm_node = NULL;
1159 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001160 bo->mem.bus.io_reserved_vm = false;
1161 bo->mem.bus.io_reserved_count = 0;
Christian König5bc73062016-06-15 13:44:01 +02001162 bo->moving = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001163 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001164 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001165 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001166 bo->sg = sg;
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001167 if (resv) {
1168 bo->resv = resv;
1169 lockdep_assert_held(&bo->resv->lock.base);
1170 } else {
1171 bo->resv = &bo->ttm_resv;
1172 reservation_object_init(&bo->ttm_resv);
1173 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001174 atomic_inc(&bo->glob->bo_count);
David Herrmann72525b32013-07-24 21:08:53 +02001175 drm_vma_node_reset(&bo->vma_node);
Christian Königcf6c4672017-01-10 14:08:28 +01001176 bo->priority = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001177
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001178 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179 * For ttm_bo_type_device buffers, allocate
1180 * address space from the device.
1181 */
Christian Königf1217ed2014-08-27 13:16:04 +02001182 if (bo->type == ttm_bo_type_device ||
1183 bo->type == ttm_bo_type_sg)
David Herrmannabf19032013-07-25 14:08:51 +02001184 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1185 bo->mem.num_pages);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001186
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001187 /* passed reservation objects should already be locked,
1188 * since otherwise lockdep will be angered in radeon.
1189 */
1190 if (!resv) {
1191 locked = ww_mutex_trylock(&bo->resv->lock);
1192 WARN_ON(!locked);
1193 }
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001194
1195 if (likely(!ret))
1196 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001197
Nicolai Hähnlec2c139c2017-02-14 09:37:12 +01001198 if (unlikely(ret)) {
Nicolai Hähnleca9cf68d2017-02-16 10:56:40 +01001199 if (!resv)
1200 ttm_bo_unreserve(bo);
1201
Nicolai Hähnlec2c139c2017-02-14 09:37:12 +01001202 ttm_bo_unref(&bo);
1203 return ret;
1204 }
1205
1206 if (resv && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
Christian König33d48cf2016-01-11 15:35:18 +01001207 spin_lock(&bo->glob->lru_lock);
1208 ttm_bo_add_to_lru(bo);
1209 spin_unlock(&bo->glob->lru_lock);
1210 }
1211
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212 return ret;
1213}
Nicolai Hähnleca9cf68d2017-02-16 10:56:40 +01001214EXPORT_SYMBOL(ttm_bo_init_reserved);
1215
1216int ttm_bo_init(struct ttm_bo_device *bdev,
1217 struct ttm_buffer_object *bo,
1218 unsigned long size,
1219 enum ttm_bo_type type,
1220 struct ttm_placement *placement,
1221 uint32_t page_alignment,
1222 bool interruptible,
1223 struct file *persistent_swap_storage,
1224 size_t acc_size,
1225 struct sg_table *sg,
1226 struct reservation_object *resv,
1227 void (*destroy) (struct ttm_buffer_object *))
1228{
1229 int ret;
1230
1231 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1232 page_alignment, interruptible,
1233 persistent_swap_storage, acc_size,
1234 sg, resv, destroy);
1235 if (ret)
1236 return ret;
1237
1238 if (!resv)
1239 ttm_bo_unreserve(bo);
1240
1241 return 0;
1242}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001243EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001244
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001245size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1246 unsigned long bo_size,
1247 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001249 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1250 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001252 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001253 size += ttm_round_pot(npages * sizeof(void *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001254 size += ttm_round_pot(sizeof(struct ttm_tt));
1255 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001257EXPORT_SYMBOL(ttm_bo_acc_size);
1258
1259size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1260 unsigned long bo_size,
1261 unsigned struct_size)
1262{
1263 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1264 size_t size = 0;
1265
1266 size += ttm_round_pot(struct_size);
Felix Kuehling85621632016-04-07 21:42:17 -04001267 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001268 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1269 return size;
1270}
1271EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001272
Jerome Glisse09855ac2009-12-10 17:16:27 +01001273int ttm_bo_create(struct ttm_bo_device *bdev,
1274 unsigned long size,
1275 enum ttm_bo_type type,
1276 struct ttm_placement *placement,
1277 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001278 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001279 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001280 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001281{
1282 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001283 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001284 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001285
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001286 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001287 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001288 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001289
Thomas Hellstroma393c732012-06-12 13:28:42 +02001290 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001291 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001292 interruptible, persistent_swap_storage, acc_size,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +01001293 NULL, NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001294 if (likely(ret == 0))
1295 *p_bo = bo;
1296
1297 return ret;
1298}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001299EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001300
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001301static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Christian König2ee7fc92017-01-06 19:16:07 +01001302 unsigned mem_type)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001303{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001304 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001305 struct ttm_bo_global *glob = bdev->glob;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001306 struct dma_fence *fence;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001307 int ret;
Christian Königcf6c4672017-01-10 14:08:28 +01001308 unsigned i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001309
1310 /*
1311 * Can't use standard list traversal since we're unlocking.
1312 */
1313
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001314 spin_lock(&glob->lru_lock);
Christian Königcf6c4672017-01-10 14:08:28 +01001315 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1316 while (!list_empty(&man->lru[i])) {
1317 spin_unlock(&glob->lru_lock);
1318 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
1319 if (ret)
Jerome Glisseca262a9992009-12-08 15:33:32 +01001320 return ret;
Christian Königcf6c4672017-01-10 14:08:28 +01001321 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001322 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001323 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001324 spin_unlock(&glob->lru_lock);
Christian Königaff98ba2016-06-22 14:16:28 +02001325
1326 spin_lock(&man->move_lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001327 fence = dma_fence_get(man->move);
Christian Königaff98ba2016-06-22 14:16:28 +02001328 spin_unlock(&man->move_lock);
1329
1330 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01001331 ret = dma_fence_wait(fence, false);
1332 dma_fence_put(fence);
Christian König2ee7fc92017-01-06 19:16:07 +01001333 if (ret)
1334 return ret;
Christian Königaff98ba2016-06-22 14:16:28 +02001335 }
1336
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001337 return 0;
1338}
1339
1340int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1341{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001342 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001343 int ret = -EINVAL;
1344
1345 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001346 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001347 return ret;
1348 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001349 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001350
1351 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001352 pr_err("Trying to take down uninitialized memory manager type %u\n",
1353 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001354 return ret;
1355 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01001356 dma_fence_put(man->move);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001357
1358 man->use_type = false;
1359 man->has_type = false;
1360
1361 ret = 0;
1362 if (mem_type > 0) {
Christian König2ee7fc92017-01-06 19:16:07 +01001363 ret = ttm_bo_force_list_clean(bdev, mem_type);
1364 if (ret) {
1365 pr_err("Cleanup eviction failed\n");
1366 return ret;
1367 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001368
Ben Skeggsd961db72010-08-05 10:48:18 +10001369 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001370 }
1371
1372 return ret;
1373}
1374EXPORT_SYMBOL(ttm_bo_clean_mm);
1375
1376int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1377{
1378 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1379
1380 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001381 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001382 return -EINVAL;
1383 }
1384
1385 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001386 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001387 return 0;
1388 }
1389
Christian König2ee7fc92017-01-06 19:16:07 +01001390 return ttm_bo_force_list_clean(bdev, mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001391}
1392EXPORT_SYMBOL(ttm_bo_evict_mm);
1393
1394int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001395 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001396{
Huang Ruiaef1ba52017-04-17 15:32:52 +08001397 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001398 struct ttm_mem_type_manager *man;
Christian Königcf6c4672017-01-10 14:08:28 +01001399 unsigned i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001400
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001401 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001402 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001403 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001404 man->io_reserve_fastpath = true;
1405 man->use_io_reserve_lru = false;
1406 mutex_init(&man->io_reserve_mutex);
Christian König3ddf4ad2016-06-15 13:44:03 +02001407 spin_lock_init(&man->move_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001408 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001409
1410 ret = bdev->driver->init_mem_type(bdev, type, man);
1411 if (ret)
1412 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001413 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001414
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001415 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001416 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001417 if (ret)
1418 return ret;
1419 }
1420 man->has_type = true;
1421 man->use_type = true;
1422 man->size = p_size;
1423
Christian Königcf6c4672017-01-10 14:08:28 +01001424 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1425 INIT_LIST_HEAD(&man->lru[i]);
Christian König3ddf4ad2016-06-15 13:44:03 +02001426 man->move = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001427
1428 return 0;
1429}
1430EXPORT_SYMBOL(ttm_bo_init_mm);
1431
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001432static void ttm_bo_global_kobj_release(struct kobject *kobj)
1433{
1434 struct ttm_bo_global *glob =
1435 container_of(kobj, struct ttm_bo_global, kobj);
1436
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001437 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1438 __free_page(glob->dummy_read_page);
1439 kfree(glob);
1440}
1441
Dave Airlieba4420c2010-03-09 10:56:52 +10001442void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001443{
1444 struct ttm_bo_global *glob = ref->object;
1445
1446 kobject_del(&glob->kobj);
1447 kobject_put(&glob->kobj);
1448}
1449EXPORT_SYMBOL(ttm_bo_global_release);
1450
Dave Airlieba4420c2010-03-09 10:56:52 +10001451int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001452{
1453 struct ttm_bo_global_ref *bo_ref =
1454 container_of(ref, struct ttm_bo_global_ref, ref);
1455 struct ttm_bo_global *glob = ref->object;
1456 int ret;
Christian Königcf6c4672017-01-10 14:08:28 +01001457 unsigned i;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001458
1459 mutex_init(&glob->device_list_mutex);
1460 spin_lock_init(&glob->lru_lock);
1461 glob->mem_glob = bo_ref->mem_glob;
1462 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1463
1464 if (unlikely(glob->dummy_read_page == NULL)) {
1465 ret = -ENOMEM;
1466 goto out_no_drp;
1467 }
1468
Christian Königcf6c4672017-01-10 14:08:28 +01001469 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1470 INIT_LIST_HEAD(&glob->swap_lru[i]);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001471 INIT_LIST_HEAD(&glob->device_list);
1472
1473 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1474 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1475 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001476 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001477 goto out_no_shrink;
1478 }
1479
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001480 atomic_set(&glob->bo_count, 0);
1481
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001482 ret = kobject_init_and_add(
1483 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001484 if (unlikely(ret != 0))
1485 kobject_put(&glob->kobj);
1486 return ret;
1487out_no_shrink:
1488 __free_page(glob->dummy_read_page);
1489out_no_drp:
1490 kfree(glob);
1491 return ret;
1492}
1493EXPORT_SYMBOL(ttm_bo_global_init);
1494
1495
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001496int ttm_bo_device_release(struct ttm_bo_device *bdev)
1497{
1498 int ret = 0;
1499 unsigned i = TTM_NUM_MEM_TYPES;
1500 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001501 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001502
1503 while (i--) {
1504 man = &bdev->man[i];
1505 if (man->has_type) {
1506 man->use_type = false;
1507 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1508 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001509 pr_err("DRM memory manager type %d is not clean\n",
1510 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001511 }
1512 man->has_type = false;
1513 }
1514 }
1515
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001516 mutex_lock(&glob->device_list_mutex);
1517 list_del(&bdev->device_list);
1518 mutex_unlock(&glob->device_list_mutex);
1519
Tejun Heof094cfc2010-12-24 15:59:06 +01001520 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001521
1522 while (ttm_bo_delayed_delete(bdev, true))
1523 ;
1524
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001525 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526 if (list_empty(&bdev->ddestroy))
1527 TTM_DEBUG("Delayed destroy list was clean\n");
1528
Christian Königcf6c4672017-01-10 14:08:28 +01001529 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1530 if (list_empty(&bdev->man[0].lru[0]))
1531 TTM_DEBUG("Swap list %d was clean\n", i);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001532 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001533
David Herrmann72525b32013-07-24 21:08:53 +02001534 drm_vma_offset_manager_destroy(&bdev->vma_manager);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001535
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001536 return ret;
1537}
1538EXPORT_SYMBOL(ttm_bo_device_release);
1539
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001540int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001541 struct ttm_bo_global *glob,
1542 struct ttm_bo_driver *driver,
David Herrmann44d847b2013-08-13 19:10:30 +02001543 struct address_space *mapping,
Dave Airlie51c8b402009-08-20 13:38:04 +10001544 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001545 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001546{
1547 int ret = -EINVAL;
1548
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001549 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001550
1551 memset(bdev->man, 0, sizeof(bdev->man));
1552
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553 /*
1554 * Initialize the system memory buffer type.
1555 * Other types need to be driver / IOCTL initialized.
1556 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001557 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001558 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001559 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001560
David Herrmann72525b32013-07-24 21:08:53 +02001561 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1562 0x10000000);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001564 INIT_LIST_HEAD(&bdev->ddestroy);
David Herrmann44d847b2013-08-13 19:10:30 +02001565 bdev->dev_mapping = mapping;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001566 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001567 bdev->need_dma32 = need_dma32;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001568 mutex_lock(&glob->device_list_mutex);
1569 list_add_tail(&bdev->device_list, &glob->device_list);
1570 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001571
1572 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001573out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001574 return ret;
1575}
1576EXPORT_SYMBOL(ttm_bo_device_init);
1577
1578/*
1579 * buffer object vm functions.
1580 */
1581
1582bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1583{
1584 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1585
1586 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1587 if (mem->mem_type == TTM_PL_SYSTEM)
1588 return false;
1589
1590 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1591 return false;
1592
1593 if (mem->placement & TTM_PL_FLAG_CACHED)
1594 return false;
1595 }
1596 return true;
1597}
1598
Thomas Hellstromeba67092010-11-11 09:41:57 +01001599void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001600{
1601 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001602
David Herrmann51335df2013-07-24 21:10:03 +02001603 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001604 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001605}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001606
1607void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1608{
1609 struct ttm_bo_device *bdev = bo->bdev;
1610 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1611
1612 ttm_mem_io_lock(man, false);
1613 ttm_bo_unmap_virtual_locked(bo);
1614 ttm_mem_io_unlock(man);
1615}
1616
1617
Dave Airliee024e112009-06-24 09:48:08 +10001618EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001619
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001620int ttm_bo_wait(struct ttm_buffer_object *bo,
Christian König8aa6d4f2016-04-06 11:12:04 +02001621 bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001622{
Christian König98a6dd92016-11-07 16:16:15 -05001623 long timeout = 15 * HZ;
1624
1625 if (no_wait) {
1626 if (reservation_object_test_signaled_rcu(bo->resv, true))
1627 return 0;
1628 else
1629 return -EBUSY;
1630 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001631
Christian Königf849c6d2016-06-15 13:44:02 +02001632 timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
1633 interruptible, timeout);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001634 if (timeout < 0)
1635 return timeout;
1636
1637 if (timeout == 0)
1638 return -EBUSY;
1639
Christian Königf849c6d2016-06-15 13:44:02 +02001640 reservation_object_add_excl_fence(bo->resv, NULL);
Maarten Lankhorstf2c24b82014-04-02 17:14:48 +02001641 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001642}
1643EXPORT_SYMBOL(ttm_bo_wait);
1644
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001645int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1646{
1647 int ret = 0;
1648
1649 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001650 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001651 */
1652
Christian Königdfd5e502016-04-06 11:12:03 +02001653 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001654 if (unlikely(ret != 0))
1655 return ret;
Christian König8aa6d4f2016-04-06 11:12:04 +02001656 ret = ttm_bo_wait(bo, true, no_wait);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001657 if (likely(ret == 0))
1658 atomic_inc(&bo->cpu_writers);
1659 ttm_bo_unreserve(bo);
1660 return ret;
1661}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001662EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001663
1664void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1665{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001666 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001667}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001668EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001669
1670/**
1671 * A buffer object shrink method that tries to swap out the first
1672 * buffer object on the bo_global::swap_lru list.
1673 */
1674
1675static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1676{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001677 struct ttm_bo_global *glob =
1678 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001679 struct ttm_buffer_object *bo;
1680 int ret = -EBUSY;
Christian Königcf6c4672017-01-10 14:08:28 +01001681 unsigned i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001682
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001683 spin_lock(&glob->lru_lock);
Christian Königcf6c4672017-01-10 14:08:28 +01001684 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1685 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1686 ret = __ttm_bo_reserve(bo, false, true, NULL);
1687 if (!ret)
1688 break;
1689 }
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001690 if (!ret)
1691 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001692 }
1693
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001694 if (ret) {
1695 spin_unlock(&glob->lru_lock);
1696 return ret;
1697 }
1698
1699 kref_get(&bo->list_kref);
1700
1701 if (!list_empty(&bo->ddestroy)) {
1702 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1703 kref_put(&bo->list_kref, ttm_bo_release_list);
1704 return ret;
1705 }
1706
Peter Zijlstrabdfafc42016-11-14 17:34:19 +01001707 ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001708 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001709
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001710 /**
Christian König61ede072016-06-06 10:17:57 +02001711 * Move to system cached
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001712 */
1713
Michel Dänzer239ac652017-01-25 17:21:31 +09001714 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1715 bo->ttm->caching_state != tt_cached) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001716 struct ttm_mem_reg evict_mem;
1717
1718 evict_mem = bo->mem;
1719 evict_mem.mm_node = NULL;
1720 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1721 evict_mem.mem_type = TTM_PL_SYSTEM;
1722
1723 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001724 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001725 if (unlikely(ret != 0))
1726 goto out;
1727 }
1728
Christian König61ede072016-06-06 10:17:57 +02001729 /**
1730 * Make sure BO is idle.
1731 */
1732
1733 ret = ttm_bo_wait(bo, false, false);
1734 if (unlikely(ret != 0))
1735 goto out;
1736
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001737 ttm_bo_unmap_virtual(bo);
1738
1739 /**
1740 * Swap out. Buffer will be swapped in again as soon as
1741 * anyone tries to access a ttm page.
1742 */
1743
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001744 if (bo->bdev->driver->swap_notify)
1745 bo->bdev->driver->swap_notify(bo);
1746
Jan Engelhardt5df23972011-04-04 01:25:18 +02001747 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001748out:
1749
1750 /**
1751 *
1752 * Unreserve without putting on LRU to avoid swapping out an
1753 * already swapped buffer.
1754 */
1755
Thomas Hellstromc7523082014-02-20 11:36:25 +01001756 __ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001757 kref_put(&bo->list_kref, ttm_bo_release_list);
1758 return ret;
1759}
1760
1761void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1762{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001763 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001764 ;
1765}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001766EXPORT_SYMBOL(ttm_bo_swapout_all);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001767
1768/**
1769 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1770 * unreserved
1771 *
1772 * @bo: Pointer to buffer
1773 */
1774int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1775{
1776 int ret;
1777
1778 /*
1779 * In the absense of a wait_unlocked API,
1780 * Use the bo::wu_mutex to avoid triggering livelocks due to
1781 * concurrent use of this function. Note that this use of
1782 * bo::wu_mutex can go away if we change locking order to
1783 * mmap_sem -> bo::reserve.
1784 */
1785 ret = mutex_lock_interruptible(&bo->wu_mutex);
1786 if (unlikely(ret != 0))
1787 return -ERESTARTSYS;
1788 if (!ww_mutex_is_locked(&bo->resv->lock))
1789 goto out_unlock;
Christian Königdfd5e502016-04-06 11:12:03 +02001790 ret = __ttm_bo_reserve(bo, true, false, NULL);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001791 if (unlikely(ret != 0))
1792 goto out_unlock;
Thomas Hellstromc7523082014-02-20 11:36:25 +01001793 __ttm_bo_unreserve(bo);
Thomas Hellstromc58f0092013-11-14 10:49:05 -08001794
1795out_unlock:
1796 mutex_unlock(&bo->wu_mutex);
1797 return ret;
1798}