blob: a8a27f51e4191a7524e0d2af0be116b7bcecdb7b [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>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020043
44#define TTM_ASSERT_LOCKED(param)
45#define TTM_DEBUG(fmt, arg...)
46#define TTM_BO_HASH_ORDER 13
47
48static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
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
Jerome Glissefb53f862009-12-09 21:55:10 +010057static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
58{
59 int i;
60
61 for (i = 0; i <= TTM_PL_PRIV5; i++)
62 if (flags & (1 << i)) {
63 *mem_type = i;
64 return 0;
65 }
66 return -EINVAL;
67}
68
Jerome Glisse5012f502009-12-10 18:07:26 +010069static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010070{
Jerome Glisse5012f502009-12-10 18:07:26 +010071 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
72
Joe Perches25d04792012-03-16 21:43:50 -070073 pr_err(" has_type: %d\n", man->has_type);
74 pr_err(" use_type: %d\n", man->use_type);
75 pr_err(" flags: 0x%08X\n", man->flags);
76 pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
77 pr_err(" size: %llu\n", man->size);
78 pr_err(" available_caching: 0x%08X\n", man->available_caching);
79 pr_err(" default_caching: 0x%08X\n", man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100080 if (mem_type != TTM_PL_SYSTEM)
81 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010082}
83
84static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
85 struct ttm_placement *placement)
86{
Jerome Glissefb53f862009-12-09 21:55:10 +010087 int i, ret, mem_type;
88
Joe Perches25d04792012-03-16 21:43:50 -070089 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
90 bo, bo->mem.num_pages, bo->mem.size >> 10,
91 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010092 for (i = 0; i < placement->num_placement; i++) {
93 ret = ttm_mem_type_from_flags(placement->placement[i],
94 &mem_type);
95 if (ret)
96 return;
Joe Perches25d04792012-03-16 21:43:50 -070097 pr_err(" placement[%d]=0x%08X (%d)\n",
98 i, placement->placement[i], mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +010099 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100100 }
101}
102
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200103static ssize_t ttm_bo_global_show(struct kobject *kobj,
104 struct attribute *attr,
105 char *buffer)
106{
107 struct ttm_bo_global *glob =
108 container_of(kobj, struct ttm_bo_global, kobj);
109
110 return snprintf(buffer, PAGE_SIZE, "%lu\n",
111 (unsigned long) atomic_read(&glob->bo_count));
112}
113
114static struct attribute *ttm_bo_global_attrs[] = {
115 &ttm_bo_count,
116 NULL
117};
118
Emese Revfy52cf25d2010-01-19 02:58:23 +0100119static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200120 .show = &ttm_bo_global_show
121};
122
123static struct kobj_type ttm_bo_glob_kobj_type = {
124 .release = &ttm_bo_global_kobj_release,
125 .sysfs_ops = &ttm_bo_global_ops,
126 .default_attrs = ttm_bo_global_attrs
127};
128
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200129
130static inline uint32_t ttm_bo_type_flags(unsigned type)
131{
132 return 1 << (type);
133}
134
135static void ttm_bo_release_list(struct kref *list_kref)
136{
137 struct ttm_buffer_object *bo =
138 container_of(list_kref, struct ttm_buffer_object, list_kref);
139 struct ttm_bo_device *bdev = bo->bdev;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500140 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200141
142 BUG_ON(atomic_read(&bo->list_kref.refcount));
143 BUG_ON(atomic_read(&bo->kref.refcount));
144 BUG_ON(atomic_read(&bo->cpu_writers));
145 BUG_ON(bo->sync_obj != NULL);
146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
149
150 if (bo->ttm)
151 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200152 atomic_dec(&bo->glob->bo_count);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200153 if (bo->resv == &bo->ttm_resv)
154 reservation_object_fini(&bo->ttm_resv);
155
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 if (bo->destroy)
157 bo->destroy(bo);
158 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200159 kfree(bo);
160 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500161 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200162}
163
Dave Airlied6ea8882010-11-22 13:24:40 +1000164void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165{
166 struct ttm_bo_device *bdev = bo->bdev;
167 struct ttm_mem_type_manager *man;
168
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200169 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200170
171 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
172
173 BUG_ON(!list_empty(&bo->lru));
174
175 man = &bdev->man[bo->mem.mem_type];
176 list_add_tail(&bo->lru, &man->lru);
177 kref_get(&bo->list_kref);
178
179 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200180 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200181 kref_get(&bo->list_kref);
182 }
183 }
184}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200185EXPORT_SYMBOL(ttm_bo_add_to_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200186
Dave Airlied6ea8882010-11-22 13:24:40 +1000187int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200188{
189 int put_count = 0;
190
191 if (!list_empty(&bo->swap)) {
192 list_del_init(&bo->swap);
193 ++put_count;
194 }
195 if (!list_empty(&bo->lru)) {
196 list_del_init(&bo->lru);
197 ++put_count;
198 }
199
200 /*
201 * TODO: Add a driver hook to delete from
202 * driver-specific LRU's here.
203 */
204
205 return put_count;
206}
207
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200208static void ttm_bo_ref_bug(struct kref *list_kref)
209{
210 BUG();
211}
212
Dave Airlied6ea8882010-11-22 13:24:40 +1000213void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
214 bool never_free)
215{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100216 kref_sub(&bo->list_kref, count,
217 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000218}
219
Maarten Lankhorst34820322013-06-27 13:48:24 +0200220void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200221{
Maarten Lankhorst34820322013-06-27 13:48:24 +0200222 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200223
Maarten Lankhorst34820322013-06-27 13:48:24 +0200224 spin_lock(&bo->glob->lru_lock);
225 put_count = ttm_bo_del_from_lru(bo);
226 spin_unlock(&bo->glob->lru_lock);
227 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200228}
Maarten Lankhorst34820322013-06-27 13:48:24 +0200229EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200230
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200231/*
232 * Call bo->mutex locked.
233 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200234static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
235{
236 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200237 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200238 int ret = 0;
239 uint32_t page_flags = 0;
240
241 TTM_ASSERT_LOCKED(&bo->mutex);
242 bo->ttm = NULL;
243
Dave Airliead49f502009-07-10 22:36:26 +1000244 if (bdev->need_dma32)
245 page_flags |= TTM_PAGE_FLAG_DMA32;
246
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200247 switch (bo->type) {
248 case ttm_bo_type_device:
249 if (zero_alloc)
250 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
251 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400252 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
253 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254 if (unlikely(bo->ttm == NULL))
255 ret = -ENOMEM;
256 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100257 case ttm_bo_type_sg:
258 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
259 page_flags | TTM_PAGE_FLAG_SG,
260 glob->dummy_read_page);
261 if (unlikely(bo->ttm == NULL)) {
262 ret = -ENOMEM;
263 break;
264 }
265 bo->ttm->sg = bo->sg;
266 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200267 default:
Joe Perches25d04792012-03-16 21:43:50 -0700268 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200269 ret = -EINVAL;
270 break;
271 }
272
273 return ret;
274}
275
276static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
277 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000278 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000279 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200280{
281 struct ttm_bo_device *bdev = bo->bdev;
282 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
283 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
284 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
285 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
286 int ret = 0;
287
288 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100289 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
290 ret = ttm_mem_io_lock(old_man, true);
291 if (unlikely(ret != 0))
292 goto out_err;
293 ttm_bo_unmap_virtual_locked(bo);
294 ttm_mem_io_unlock(old_man);
295 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200296
297 /*
298 * Create and bind a ttm if required.
299 */
300
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000301 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
302 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000303 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
304 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000305 if (ret)
306 goto out_err;
307 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200308
309 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
310 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200311 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200312
313 if (mem->mem_type != TTM_PL_SYSTEM) {
314 ret = ttm_tt_bind(bo->ttm, mem);
315 if (ret)
316 goto out_err;
317 }
318
319 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000320 if (bdev->driver->move_notify)
321 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100322 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200324 goto moved;
325 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326 }
327
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000328 if (bdev->driver->move_notify)
329 bdev->driver->move_notify(bo, mem);
330
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200331 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
332 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000333 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200334 else if (bdev->driver->move)
335 ret = bdev->driver->move(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000336 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200337 else
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000338 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200339
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000340 if (ret) {
341 if (bdev->driver->move_notify) {
342 struct ttm_mem_reg tmp_mem = *mem;
343 *mem = bo->mem;
344 bo->mem = tmp_mem;
345 bdev->driver->move_notify(bo, mem);
346 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000347 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000348 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200349
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000350 goto out_err;
351 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500352
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200353moved:
354 if (bo->evicted) {
355 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
356 if (ret)
Joe Perches25d04792012-03-16 21:43:50 -0700357 pr_err("Can not flush read caches\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200358 bo->evicted = false;
359 }
360
361 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000362 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200363 bdev->man[bo->mem.mem_type].gpu_offset;
364 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100365 } else
366 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200367
368 return 0;
369
370out_err:
371 new_man = &bdev->man[bo->mem.mem_type];
372 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
373 ttm_tt_unbind(bo->ttm);
374 ttm_tt_destroy(bo->ttm);
375 bo->ttm = NULL;
376 }
377
378 return ret;
379}
380
381/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200382 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200383 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200384 * This is the place to put in driver specific hooks to release
385 * driver private resources.
386 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200387 */
388
389static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
390{
Jerome Glissedc97b342011-11-18 11:47:03 -0500391 if (bo->bdev->driver->move_notify)
392 bo->bdev->driver->move_notify(bo, NULL);
393
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200394 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200395 ttm_tt_unbind(bo->ttm);
396 ttm_tt_destroy(bo->ttm);
397 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200398 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200399 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200400
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200401 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200402}
403
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200404static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200405{
406 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200407 struct ttm_bo_global *glob = bo->glob;
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100408 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000409 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200410 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200411 int ret;
412
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100413 spin_lock(&glob->lru_lock);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100414 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100415
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000416 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200417 (void) ttm_bo_wait(bo, false, false, true);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100418 if (!ret && !bo->sync_obj) {
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000419 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200420 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200421
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200422 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200423 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200424
Dave Airlied6ea8882010-11-22 13:24:40 +1000425 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200426
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200427 return;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200428 }
Thomas Hellstromaa123262010-11-02 13:21:47 +0000429 if (bo->sync_obj)
430 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100431 spin_unlock(&bdev->fence_lock);
432
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200433 if (!ret)
434 ww_mutex_unlock(&bo->resv->lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200435
436 kref_get(&bo->list_kref);
437 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
438 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200439
Thomas Hellstromaa123262010-11-02 13:21:47 +0000440 if (sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +0000441 driver->sync_obj_flush(sync_obj);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000442 driver->sync_obj_unref(&sync_obj);
443 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200444 schedule_delayed_work(&bdev->wq,
445 ((HZ / 100) < 1) ? 1 : HZ / 100);
446}
447
448/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000449 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200450 * If bo idle, remove from delayed- and lru lists, and unref.
451 * If not idle, do nothing.
452 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000453 * Must be called with lru_lock and reservation held, this function
454 * will drop both before returning.
455 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200456 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200457 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
458 */
459
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000460static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
461 bool interruptible,
462 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200463{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000464 struct ttm_bo_device *bdev = bo->bdev;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000465 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200466 struct ttm_bo_global *glob = bo->glob;
467 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000468 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200469
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000470 spin_lock(&bdev->fence_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000471 ret = ttm_bo_wait(bo, false, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200472
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000473 if (ret && !no_wait_gpu) {
474 void *sync_obj;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200475
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000476 /*
477 * Take a reference to the fence and unreserve,
478 * at this point the buffer should be dead, so
479 * no new sync objects can be attached.
480 */
Maarten Lankhorst0953e762012-12-19 18:21:10 +0100481 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000482 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100483
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200484 ww_mutex_unlock(&bo->resv->lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200485 spin_unlock(&glob->lru_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000486
487 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
488 driver->sync_obj_unref(&sync_obj);
489 if (ret)
490 return ret;
491
492 /*
493 * remove sync_obj with ttm_bo_wait, the wait should be
494 * finished, and no new wait object should have been added.
495 */
496 spin_lock(&bdev->fence_lock);
497 ret = ttm_bo_wait(bo, false, false, true);
498 WARN_ON(ret);
499 spin_unlock(&bdev->fence_lock);
500 if (ret)
501 return ret;
502
503 spin_lock(&glob->lru_lock);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100504 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000505
506 /*
507 * We raced, and lost, someone else holds the reservation now,
508 * and is probably busy in ttm_bo_cleanup_memtype_use.
509 *
510 * Even if it's not the case, because we finished waiting any
511 * delayed destruction would succeed, so just return success
512 * here.
513 */
514 if (ret) {
515 spin_unlock(&glob->lru_lock);
516 return 0;
517 }
518 } else
519 spin_unlock(&bdev->fence_lock);
520
521 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200522 ww_mutex_unlock(&bo->resv->lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000523 spin_unlock(&glob->lru_lock);
524 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200525 }
526
527 put_count = ttm_bo_del_from_lru(bo);
528 list_del_init(&bo->ddestroy);
529 ++put_count;
530
531 spin_unlock(&glob->lru_lock);
532 ttm_bo_cleanup_memtype_use(bo);
533
Dave Airlied6ea8882010-11-22 13:24:40 +1000534 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200535
536 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200537}
538
539/**
540 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
541 * encountered buffers.
542 */
543
544static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
545{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200546 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100547 struct ttm_buffer_object *entry = NULL;
548 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200549
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200550 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100551 if (list_empty(&bdev->ddestroy))
552 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200553
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100554 entry = list_first_entry(&bdev->ddestroy,
555 struct ttm_buffer_object, ddestroy);
556 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200557
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100558 for (;;) {
559 struct ttm_buffer_object *nentry = NULL;
560
561 if (entry->ddestroy.next != &bdev->ddestroy) {
562 nentry = list_first_entry(&entry->ddestroy,
563 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200564 kref_get(&nentry->list_kref);
565 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200566
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100567 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
568 if (remove_all && ret) {
569 spin_unlock(&glob->lru_lock);
570 ret = ttm_bo_reserve_nolru(entry, false, false,
571 false, 0);
572 spin_lock(&glob->lru_lock);
573 }
574
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000575 if (!ret)
576 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
577 !remove_all);
578 else
579 spin_unlock(&glob->lru_lock);
580
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200581 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100582 entry = nentry;
583
584 if (ret || !entry)
585 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200586
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200587 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100588 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200589 break;
590 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200591
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100592out_unlock:
593 spin_unlock(&glob->lru_lock);
594out:
595 if (entry)
596 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200597 return ret;
598}
599
600static void ttm_bo_delayed_workqueue(struct work_struct *work)
601{
602 struct ttm_bo_device *bdev =
603 container_of(work, struct ttm_bo_device, wq.work);
604
605 if (ttm_bo_delayed_delete(bdev, false)) {
606 schedule_delayed_work(&bdev->wq,
607 ((HZ / 100) < 1) ? 1 : HZ / 100);
608 }
609}
610
611static void ttm_bo_release(struct kref *kref)
612{
613 struct ttm_buffer_object *bo =
614 container_of(kref, struct ttm_buffer_object, kref);
615 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100616 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200617
Thomas Hellstrom82fe50b2012-11-21 14:53:21 +0000618 write_lock(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200619 if (likely(bo->vm_node != NULL)) {
620 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
621 drm_mm_put_block(bo->vm_node);
622 bo->vm_node = NULL;
623 }
624 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100625 ttm_mem_io_lock(man, false);
626 ttm_mem_io_free_vm(bo);
627 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200628 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200629 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200630}
631
632void ttm_bo_unref(struct ttm_buffer_object **p_bo)
633{
634 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200635
636 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200637 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200638}
639EXPORT_SYMBOL(ttm_bo_unref);
640
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400641int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
642{
643 return cancel_delayed_work_sync(&bdev->wq);
644}
645EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
646
647void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
648{
649 if (resched)
650 schedule_delayed_work(&bdev->wq,
651 ((HZ / 100) < 1) ? 1 : HZ / 100);
652}
653EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
654
Jerome Glisseca262a9992009-12-08 15:33:32 +0100655static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000656 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200657{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200658 struct ttm_bo_device *bdev = bo->bdev;
659 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100660 struct ttm_placement placement;
661 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200662
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000663 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200664 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000665 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200666
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200667 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100668 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700669 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200670 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200671 goto out;
672 }
673
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200674 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200675
676 evict_mem = bo->mem;
677 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100678 evict_mem.bus.io_reserved_vm = false;
679 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200680
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100681 placement.fpfn = 0;
682 placement.lpfn = 0;
683 placement.num_placement = 0;
684 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100685 bdev->driver->evict_flags(bo, &placement);
686 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000687 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200688 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100689 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700690 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
691 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100692 ttm_bo_mem_space_debug(bo, &placement);
693 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200694 goto out;
695 }
696
697 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000698 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200699 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100700 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700701 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000702 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200703 goto out;
704 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200705 bo->evicted = true;
706out:
707 return ret;
708}
709
Jerome Glisseca262a9992009-12-08 15:33:32 +0100710static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
711 uint32_t mem_type,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000712 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000713 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100714{
715 struct ttm_bo_global *glob = bdev->glob;
716 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
717 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000718 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100719
720 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000721 list_for_each_entry(bo, &man->lru, lru) {
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100722 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000723 if (!ret)
724 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100725 }
726
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000727 if (ret) {
728 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000729 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200730 }
731
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000732 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100733
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000734 if (!list_empty(&bo->ddestroy)) {
735 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
736 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100737 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000738 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100739 }
740
741 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100742 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100743
744 BUG_ON(ret != 0);
745
Dave Airlied6ea8882010-11-22 13:24:40 +1000746 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100747
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000748 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100749 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100750
Jerome Glisseca262a9992009-12-08 15:33:32 +0100751 kref_put(&bo->list_kref, ttm_bo_release_list);
752 return ret;
753}
754
Ben Skeggs42311ff2010-08-04 12:07:08 +1000755void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
756{
Ben Skeggsd961db72010-08-05 10:48:18 +1000757 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000758
Ben Skeggsd961db72010-08-05 10:48:18 +1000759 if (mem->mm_node)
760 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000761}
762EXPORT_SYMBOL(ttm_bo_mem_put);
763
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200764/**
765 * Repeatedly evict memory from the LRU for @mem_type until we create enough
766 * space, or we've evicted everything and there isn't enough space.
767 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100768static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
769 uint32_t mem_type,
770 struct ttm_placement *placement,
771 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000772 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000773 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200774{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100775 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200776 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200777 int ret;
778
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200779 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000780 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200781 if (unlikely(ret != 0))
782 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000783 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100784 break;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000785 ret = ttm_mem_evict_first(bdev, mem_type,
786 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100787 if (unlikely(ret != 0))
788 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200789 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000790 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200791 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200792 mem->mem_type = mem_type;
793 return 0;
794}
795
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200796static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
797 uint32_t cur_placement,
798 uint32_t proposed_placement)
799{
800 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
801 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
802
803 /**
804 * Keep current caching if possible.
805 */
806
807 if ((cur_placement & caching) != 0)
808 result |= (cur_placement & caching);
809 else if ((man->default_caching & caching) != 0)
810 result |= man->default_caching;
811 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
812 result |= TTM_PL_FLAG_CACHED;
813 else if ((TTM_PL_FLAG_WC & caching) != 0)
814 result |= TTM_PL_FLAG_WC;
815 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
816 result |= TTM_PL_FLAG_UNCACHED;
817
818 return result;
819}
820
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200821static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200822 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200823 uint32_t proposed_placement,
824 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200825{
826 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
827
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200828 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200829 return false;
830
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200831 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200832 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200833
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200834 cur_flags |= (proposed_placement & man->available_caching);
835
836 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200837 return true;
838}
839
840/**
841 * Creates space for memory region @mem according to its type.
842 *
843 * This function first searches for free space in compatible memory types in
844 * the priority order defined by the driver. If free space isn't found, then
845 * ttm_bo_mem_force_space is attempted in priority order to evict and find
846 * space.
847 */
848int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100849 struct ttm_placement *placement,
850 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000851 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000852 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200853{
854 struct ttm_bo_device *bdev = bo->bdev;
855 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200856 uint32_t mem_type = TTM_PL_SYSTEM;
857 uint32_t cur_flags = 0;
858 bool type_found = false;
859 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100860 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100861 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200862
863 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000864 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100865 ret = ttm_mem_type_from_flags(placement->placement[i],
866 &mem_type);
867 if (ret)
868 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200869 man = &bdev->man[mem_type];
870
871 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100872 mem_type,
873 placement->placement[i],
874 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200875
876 if (!type_ok)
877 continue;
878
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200879 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
880 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100881 /*
882 * Use the access and other non-mapping-related flag bits from
883 * the memory placement flags to the current flags
884 */
885 ttm_flag_masked(&cur_flags, placement->placement[i],
886 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200887
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200888 if (mem_type == TTM_PL_SYSTEM)
889 break;
890
891 if (man->has_type && man->use_type) {
892 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000893 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100894 if (unlikely(ret))
895 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200896 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000897 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898 break;
899 }
900
Ben Skeggsd961db72010-08-05 10:48:18 +1000901 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200902 mem->mem_type = mem_type;
903 mem->placement = cur_flags;
904 return 0;
905 }
906
907 if (!type_found)
908 return -EINVAL;
909
Dave Airlieb6637522009-12-14 14:51:35 +1000910 for (i = 0; i < placement->num_busy_placement; ++i) {
911 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100912 &mem_type);
913 if (ret)
914 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200915 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200916 if (!man->has_type)
917 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200918 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100919 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000920 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100921 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922 continue;
923
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200924 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
925 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100926 /*
927 * Use the access and other non-mapping-related flag bits from
928 * the memory placement flags to the current flags
929 */
Dave Airlieb6637522009-12-14 14:51:35 +1000930 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100931 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200932
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +0100933
934 if (mem_type == TTM_PL_SYSTEM) {
935 mem->mem_type = mem_type;
936 mem->placement = cur_flags;
937 mem->mm_node = NULL;
938 return 0;
939 }
940
Jerome Glisseca262a9992009-12-08 15:33:32 +0100941 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000942 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200943 if (ret == 0 && mem->mm_node) {
944 mem->placement = cur_flags;
945 return 0;
946 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100947 if (ret == -ERESTARTSYS)
948 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200949 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100950 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951 return ret;
952}
953EXPORT_SYMBOL(ttm_bo_mem_space);
954
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200955int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100956 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000957 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000958 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200959{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200960 int ret = 0;
961 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000962 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200963
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200964 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200965
966 /*
967 * FIXME: It's possible to pipeline buffer moves.
968 * Have the driver move function wait for idle when necessary,
969 * instead of doing it here.
970 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000971 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200972 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000973 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200974 if (ret)
975 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200976 mem.num_pages = bo->num_pages;
977 mem.size = mem.num_pages << PAGE_SHIFT;
978 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100979 mem.bus.io_reserved_vm = false;
980 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200981 /*
982 * Determine where to move the buffer.
983 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000984 ret = ttm_bo_mem_space(bo, placement, &mem,
985 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200986 if (ret)
987 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000988 ret = ttm_bo_handle_move_mem(bo, &mem, false,
989 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200990out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +1000991 if (ret && mem.mm_node)
992 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200993 return ret;
994}
995
Jerome Glisseca262a9992009-12-08 15:33:32 +0100996static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997 struct ttm_mem_reg *mem)
998{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100999 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001000
Ben Skeggsd961db72010-08-05 10:48:18 +10001001 if (mem->mm_node && placement->lpfn != 0 &&
1002 (mem->start < placement->fpfn ||
1003 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001004 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005
Jerome Glisseca262a9992009-12-08 15:33:32 +01001006 for (i = 0; i < placement->num_placement; i++) {
1007 if ((placement->placement[i] & mem->placement &
1008 TTM_PL_MASK_CACHING) &&
1009 (placement->placement[i] & mem->placement &
1010 TTM_PL_MASK_MEM))
1011 return i;
1012 }
1013 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001014}
1015
Jerome Glisse09855ac2009-12-10 17:16:27 +01001016int ttm_bo_validate(struct ttm_buffer_object *bo,
1017 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001018 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001019 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001020{
1021 int ret;
1022
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001023 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001024 /* Check that range is valid */
1025 if (placement->lpfn || placement->fpfn)
1026 if (placement->fpfn > placement->lpfn ||
1027 (placement->lpfn - placement->fpfn) < bo->num_pages)
1028 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001029 /*
1030 * Check whether we need to move buffer.
1031 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001032 ret = ttm_bo_mem_compat(placement, &bo->mem);
1033 if (ret < 0) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001034 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1035 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001036 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001038 } else {
1039 /*
1040 * Use the access and other non-mapping-related flag bits from
1041 * the compatible memory placement flags to the active flags
1042 */
1043 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1044 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001045 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046 /*
1047 * We might need to add a TTM.
1048 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1050 ret = ttm_bo_add_ttm(bo, true);
1051 if (ret)
1052 return ret;
1053 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001054 return 0;
1055}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001056EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001057
Jerome Glisse09855ac2009-12-10 17:16:27 +01001058int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1059 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001061 BUG_ON((placement->fpfn || placement->lpfn) &&
1062 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001063
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001064 return 0;
1065}
1066
Jerome Glisse09855ac2009-12-10 17:16:27 +01001067int ttm_bo_init(struct ttm_bo_device *bdev,
1068 struct ttm_buffer_object *bo,
1069 unsigned long size,
1070 enum ttm_bo_type type,
1071 struct ttm_placement *placement,
1072 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001073 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001074 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001075 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001076 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001077 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001078{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001079 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001080 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001081 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001082 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001083
1084 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1085 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001086 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001087 if (destroy)
1088 (*destroy)(bo);
1089 else
1090 kfree(bo);
1091 return -ENOMEM;
1092 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001093
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001094 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1095 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001096 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001097 if (destroy)
1098 (*destroy)(bo);
1099 else
1100 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001101 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102 return -EINVAL;
1103 }
1104 bo->destroy = destroy;
1105
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001106 kref_init(&bo->kref);
1107 kref_init(&bo->list_kref);
1108 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001109 INIT_LIST_HEAD(&bo->lru);
1110 INIT_LIST_HEAD(&bo->ddestroy);
1111 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001112 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001114 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001115 bo->type = type;
1116 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001117 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001118 bo->mem.mem_type = TTM_PL_SYSTEM;
1119 bo->mem.num_pages = bo->num_pages;
1120 bo->mem.mm_node = NULL;
1121 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001122 bo->mem.bus.io_reserved_vm = false;
1123 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001124 bo->priv_flags = 0;
1125 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001126 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001127 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001128 bo->sg = sg;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001129 bo->resv = &bo->ttm_resv;
1130 reservation_object_init(bo->resv);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001131 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001132
Jerome Glisse09855ac2009-12-10 17:16:27 +01001133 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001134
1135 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001136 * For ttm_bo_type_device buffers, allocate
1137 * address space from the device.
1138 */
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001139 if (likely(!ret) &&
1140 (bo->type == ttm_bo_type_device ||
1141 bo->type == ttm_bo_type_sg))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001142 ret = ttm_bo_setup_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001144 locked = ww_mutex_trylock(&bo->resv->lock);
1145 WARN_ON(!locked);
1146
1147 if (likely(!ret))
1148 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149
1150 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001151
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001152 if (unlikely(ret))
1153 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001154
1155 return ret;
1156}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001157EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001159size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1160 unsigned long bo_size,
1161 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001162{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001163 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1164 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001165
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001166 size += ttm_round_pot(struct_size);
1167 size += PAGE_ALIGN(npages * sizeof(void *));
1168 size += ttm_round_pot(sizeof(struct ttm_tt));
1169 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001170}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001171EXPORT_SYMBOL(ttm_bo_acc_size);
1172
1173size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1174 unsigned long bo_size,
1175 unsigned struct_size)
1176{
1177 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1178 size_t size = 0;
1179
1180 size += ttm_round_pot(struct_size);
1181 size += PAGE_ALIGN(npages * sizeof(void *));
1182 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1183 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1184 return size;
1185}
1186EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187
Jerome Glisse09855ac2009-12-10 17:16:27 +01001188int ttm_bo_create(struct ttm_bo_device *bdev,
1189 unsigned long size,
1190 enum ttm_bo_type type,
1191 struct ttm_placement *placement,
1192 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001193 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001194 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001195 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196{
1197 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001198 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001199 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001201 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001202 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001203 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001204
Thomas Hellstroma393c732012-06-12 13:28:42 +02001205 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001206 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001207 interruptible, persistent_swap_storage, acc_size,
1208 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209 if (likely(ret == 0))
1210 *p_bo = bo;
1211
1212 return ret;
1213}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001214EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001216static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001217 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001218{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001219 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001220 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001222
1223 /*
1224 * Can't use standard list traversal since we're unlocking.
1225 */
1226
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001227 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001228 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001229 spin_unlock(&glob->lru_lock);
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001230 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001231 if (ret) {
1232 if (allow_errors) {
1233 return ret;
1234 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001235 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001236 }
1237 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001238 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001239 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001240 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001241 return 0;
1242}
1243
1244int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1245{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001246 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001247 int ret = -EINVAL;
1248
1249 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001250 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251 return ret;
1252 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001253 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001254
1255 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001256 pr_err("Trying to take down uninitialized memory manager type %u\n",
1257 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001258 return ret;
1259 }
1260
1261 man->use_type = false;
1262 man->has_type = false;
1263
1264 ret = 0;
1265 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001266 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001267
Ben Skeggsd961db72010-08-05 10:48:18 +10001268 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269 }
1270
1271 return ret;
1272}
1273EXPORT_SYMBOL(ttm_bo_clean_mm);
1274
1275int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1276{
1277 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1278
1279 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001280 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001281 return -EINVAL;
1282 }
1283
1284 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001285 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001286 return 0;
1287 }
1288
Jerome Glisseca262a9992009-12-08 15:33:32 +01001289 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001290}
1291EXPORT_SYMBOL(ttm_bo_evict_mm);
1292
1293int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001294 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001295{
1296 int ret = -EINVAL;
1297 struct ttm_mem_type_manager *man;
1298
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001299 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001300 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001301 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001302 man->io_reserve_fastpath = true;
1303 man->use_io_reserve_lru = false;
1304 mutex_init(&man->io_reserve_mutex);
1305 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306
1307 ret = bdev->driver->init_mem_type(bdev, type, man);
1308 if (ret)
1309 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001310 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001311
1312 ret = 0;
1313 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001314 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001315 if (ret)
1316 return ret;
1317 }
1318 man->has_type = true;
1319 man->use_type = true;
1320 man->size = p_size;
1321
1322 INIT_LIST_HEAD(&man->lru);
1323
1324 return 0;
1325}
1326EXPORT_SYMBOL(ttm_bo_init_mm);
1327
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001328static void ttm_bo_global_kobj_release(struct kobject *kobj)
1329{
1330 struct ttm_bo_global *glob =
1331 container_of(kobj, struct ttm_bo_global, kobj);
1332
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001333 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1334 __free_page(glob->dummy_read_page);
1335 kfree(glob);
1336}
1337
Dave Airlieba4420c2010-03-09 10:56:52 +10001338void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001339{
1340 struct ttm_bo_global *glob = ref->object;
1341
1342 kobject_del(&glob->kobj);
1343 kobject_put(&glob->kobj);
1344}
1345EXPORT_SYMBOL(ttm_bo_global_release);
1346
Dave Airlieba4420c2010-03-09 10:56:52 +10001347int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001348{
1349 struct ttm_bo_global_ref *bo_ref =
1350 container_of(ref, struct ttm_bo_global_ref, ref);
1351 struct ttm_bo_global *glob = ref->object;
1352 int ret;
1353
1354 mutex_init(&glob->device_list_mutex);
1355 spin_lock_init(&glob->lru_lock);
1356 glob->mem_glob = bo_ref->mem_glob;
1357 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1358
1359 if (unlikely(glob->dummy_read_page == NULL)) {
1360 ret = -ENOMEM;
1361 goto out_no_drp;
1362 }
1363
1364 INIT_LIST_HEAD(&glob->swap_lru);
1365 INIT_LIST_HEAD(&glob->device_list);
1366
1367 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1368 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1369 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001370 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001371 goto out_no_shrink;
1372 }
1373
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001374 atomic_set(&glob->bo_count, 0);
1375
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001376 ret = kobject_init_and_add(
1377 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001378 if (unlikely(ret != 0))
1379 kobject_put(&glob->kobj);
1380 return ret;
1381out_no_shrink:
1382 __free_page(glob->dummy_read_page);
1383out_no_drp:
1384 kfree(glob);
1385 return ret;
1386}
1387EXPORT_SYMBOL(ttm_bo_global_init);
1388
1389
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001390int ttm_bo_device_release(struct ttm_bo_device *bdev)
1391{
1392 int ret = 0;
1393 unsigned i = TTM_NUM_MEM_TYPES;
1394 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001395 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001396
1397 while (i--) {
1398 man = &bdev->man[i];
1399 if (man->has_type) {
1400 man->use_type = false;
1401 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1402 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001403 pr_err("DRM memory manager type %d is not clean\n",
1404 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001405 }
1406 man->has_type = false;
1407 }
1408 }
1409
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001410 mutex_lock(&glob->device_list_mutex);
1411 list_del(&bdev->device_list);
1412 mutex_unlock(&glob->device_list_mutex);
1413
Tejun Heof094cfc2010-12-24 15:59:06 +01001414 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001415
1416 while (ttm_bo_delayed_delete(bdev, true))
1417 ;
1418
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001419 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001420 if (list_empty(&bdev->ddestroy))
1421 TTM_DEBUG("Delayed destroy list was clean\n");
1422
1423 if (list_empty(&bdev->man[0].lru))
1424 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001425 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001426
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001427 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1428 write_lock(&bdev->vm_lock);
1429 drm_mm_takedown(&bdev->addr_space_mm);
1430 write_unlock(&bdev->vm_lock);
1431
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001432 return ret;
1433}
1434EXPORT_SYMBOL(ttm_bo_device_release);
1435
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001436int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001437 struct ttm_bo_global *glob,
1438 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001439 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001440 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001441{
1442 int ret = -EINVAL;
1443
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001444 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001445 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001446
1447 memset(bdev->man, 0, sizeof(bdev->man));
1448
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001449 /*
1450 * Initialize the system memory buffer type.
1451 * Other types need to be driver / IOCTL initialized.
1452 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001453 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001454 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001455 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001456
1457 bdev->addr_space_rb = RB_ROOT;
1458 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1459 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001460 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001461
1462 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001463 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001464 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001465 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001466 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001467 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001468 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001469 mutex_lock(&glob->device_list_mutex);
1470 list_add_tail(&bdev->device_list, &glob->device_list);
1471 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001472
1473 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001474out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001475 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001476out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001477 return ret;
1478}
1479EXPORT_SYMBOL(ttm_bo_device_init);
1480
1481/*
1482 * buffer object vm functions.
1483 */
1484
1485bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1486{
1487 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1488
1489 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1490 if (mem->mem_type == TTM_PL_SYSTEM)
1491 return false;
1492
1493 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1494 return false;
1495
1496 if (mem->placement & TTM_PL_FLAG_CACHED)
1497 return false;
1498 }
1499 return true;
1500}
1501
Thomas Hellstromeba67092010-11-11 09:41:57 +01001502void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001503{
1504 struct ttm_bo_device *bdev = bo->bdev;
1505 loff_t offset = (loff_t) bo->addr_space_offset;
1506 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1507
1508 if (!bdev->dev_mapping)
1509 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001510 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001511 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001512}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001513
1514void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1515{
1516 struct ttm_bo_device *bdev = bo->bdev;
1517 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1518
1519 ttm_mem_io_lock(man, false);
1520 ttm_bo_unmap_virtual_locked(bo);
1521 ttm_mem_io_unlock(man);
1522}
1523
1524
Dave Airliee024e112009-06-24 09:48:08 +10001525EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526
1527static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1528{
1529 struct ttm_bo_device *bdev = bo->bdev;
1530 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1531 struct rb_node *parent = NULL;
1532 struct ttm_buffer_object *cur_bo;
1533 unsigned long offset = bo->vm_node->start;
1534 unsigned long cur_offset;
1535
1536 while (*cur) {
1537 parent = *cur;
1538 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1539 cur_offset = cur_bo->vm_node->start;
1540 if (offset < cur_offset)
1541 cur = &parent->rb_left;
1542 else if (offset > cur_offset)
1543 cur = &parent->rb_right;
1544 else
1545 BUG();
1546 }
1547
1548 rb_link_node(&bo->vm_rb, parent, cur);
1549 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1550}
1551
1552/**
1553 * ttm_bo_setup_vm:
1554 *
1555 * @bo: the buffer to allocate address space for
1556 *
1557 * Allocate address space in the drm device so that applications
1558 * can mmap the buffer and access the contents. This only
1559 * applies to ttm_bo_type_device objects as others are not
1560 * placed in the drm device address space.
1561 */
1562
1563static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1564{
1565 struct ttm_bo_device *bdev = bo->bdev;
1566 int ret;
1567
1568retry_pre_get:
1569 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1570 if (unlikely(ret != 0))
1571 return ret;
1572
1573 write_lock(&bdev->vm_lock);
1574 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1575 bo->mem.num_pages, 0, 0);
1576
1577 if (unlikely(bo->vm_node == NULL)) {
1578 ret = -ENOMEM;
1579 goto out_unlock;
1580 }
1581
1582 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1583 bo->mem.num_pages, 0);
1584
1585 if (unlikely(bo->vm_node == NULL)) {
1586 write_unlock(&bdev->vm_lock);
1587 goto retry_pre_get;
1588 }
1589
1590 ttm_bo_vm_insert_rb(bo);
1591 write_unlock(&bdev->vm_lock);
1592 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1593
1594 return 0;
1595out_unlock:
1596 write_unlock(&bdev->vm_lock);
1597 return ret;
1598}
1599
1600int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001601 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001602{
1603 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001604 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001605 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001606 int ret = 0;
1607
Dave Airlie1717c0e2011-10-27 18:28:37 +02001608 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001609 return 0;
1610
Dave Airlie1717c0e2011-10-27 18:28:37 +02001611 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001612
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001613 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001614 void *tmp_obj = bo->sync_obj;
1615 bo->sync_obj = NULL;
1616 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1617 spin_unlock(&bdev->fence_lock);
1618 driver->sync_obj_unref(&tmp_obj);
1619 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001620 continue;
1621 }
1622
1623 if (no_wait)
1624 return -EBUSY;
1625
Dave Airlie1717c0e2011-10-27 18:28:37 +02001626 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001627 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001628 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001629 lazy, interruptible);
1630 if (unlikely(ret != 0)) {
1631 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001632 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001633 return ret;
1634 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001635 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001636 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001637 void *tmp_obj = bo->sync_obj;
1638 bo->sync_obj = NULL;
1639 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1640 &bo->priv_flags);
1641 spin_unlock(&bdev->fence_lock);
1642 driver->sync_obj_unref(&sync_obj);
1643 driver->sync_obj_unref(&tmp_obj);
1644 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001645 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001646 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001647 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001648 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001649 }
1650 }
1651 return 0;
1652}
1653EXPORT_SYMBOL(ttm_bo_wait);
1654
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001655int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1656{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001657 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001658 int ret = 0;
1659
1660 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001661 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001662 */
1663
1664 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1665 if (unlikely(ret != 0))
1666 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001667 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001668 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001669 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001670 if (likely(ret == 0))
1671 atomic_inc(&bo->cpu_writers);
1672 ttm_bo_unreserve(bo);
1673 return ret;
1674}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001675EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001676
1677void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1678{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001679 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001680}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001681EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001682
1683/**
1684 * A buffer object shrink method that tries to swap out the first
1685 * buffer object on the bo_global::swap_lru list.
1686 */
1687
1688static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1689{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001690 struct ttm_bo_global *glob =
1691 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001692 struct ttm_buffer_object *bo;
1693 int ret = -EBUSY;
1694 int put_count;
1695 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1696
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001697 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001698 list_for_each_entry(bo, &glob->swap_lru, swap) {
Maarten Lankhorst63d0a412013-01-15 14:56:37 +01001699 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001700 if (!ret)
1701 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001702 }
1703
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001704 if (ret) {
1705 spin_unlock(&glob->lru_lock);
1706 return ret;
1707 }
1708
1709 kref_get(&bo->list_kref);
1710
1711 if (!list_empty(&bo->ddestroy)) {
1712 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1713 kref_put(&bo->list_kref, ttm_bo_release_list);
1714 return ret;
1715 }
1716
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001717 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001718 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001719
Dave Airlied6ea8882010-11-22 13:24:40 +10001720 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001721
1722 /**
1723 * Wait for GPU, then move to system cached.
1724 */
1725
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001726 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001727 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001728 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001729
1730 if (unlikely(ret != 0))
1731 goto out;
1732
1733 if ((bo->mem.placement & swap_placement) != swap_placement) {
1734 struct ttm_mem_reg evict_mem;
1735
1736 evict_mem = bo->mem;
1737 evict_mem.mm_node = NULL;
1738 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1739 evict_mem.mem_type = TTM_PL_SYSTEM;
1740
1741 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001742 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001743 if (unlikely(ret != 0))
1744 goto out;
1745 }
1746
1747 ttm_bo_unmap_virtual(bo);
1748
1749 /**
1750 * Swap out. Buffer will be swapped in again as soon as
1751 * anyone tries to access a ttm page.
1752 */
1753
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001754 if (bo->bdev->driver->swap_notify)
1755 bo->bdev->driver->swap_notify(bo);
1756
Jan Engelhardt5df23972011-04-04 01:25:18 +02001757 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001758out:
1759
1760 /**
1761 *
1762 * Unreserve without putting on LRU to avoid swapping out an
1763 * already swapped buffer.
1764 */
1765
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001766 ww_mutex_unlock(&bo->resv->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001767 kref_put(&bo->list_kref, ttm_bo_release_list);
1768 return ret;
1769}
1770
1771void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1772{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001773 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001774 ;
1775}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001776EXPORT_SYMBOL(ttm_bo_swapout_all);