blob: 50fc8e4c9a310c78b57ca4e749bc46eb1ea0a4f0 [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
31#include "ttm/ttm_module.h"
32#include "ttm/ttm_bo_driver.h"
33#include "ttm/ttm_placement.h"
34#include <linux/jiffies.h>
35#include <linux/slab.h>
36#include <linux/sched.h>
37#include <linux/mm.h>
38#include <linux/file.h>
39#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070040#include <linux/atomic.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020041
42#define TTM_ASSERT_LOCKED(param)
43#define TTM_DEBUG(fmt, arg...)
44#define TTM_BO_HASH_ORDER 13
45
46static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020047static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020048static void ttm_bo_global_kobj_release(struct kobject *kobj);
49
50static struct attribute ttm_bo_count = {
51 .name = "bo_count",
52 .mode = S_IRUGO
53};
54
Jerome Glissefb53f862009-12-09 21:55:10 +010055static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
56{
57 int i;
58
59 for (i = 0; i <= TTM_PL_PRIV5; i++)
60 if (flags & (1 << i)) {
61 *mem_type = i;
62 return 0;
63 }
64 return -EINVAL;
65}
66
Jerome Glisse5012f502009-12-10 18:07:26 +010067static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010068{
Jerome Glisse5012f502009-12-10 18:07:26 +010069 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
70
Jerome Glissefb53f862009-12-09 21:55:10 +010071 printk(KERN_ERR TTM_PFX " has_type: %d\n", man->has_type);
72 printk(KERN_ERR TTM_PFX " use_type: %d\n", man->use_type);
73 printk(KERN_ERR TTM_PFX " flags: 0x%08X\n", man->flags);
74 printk(KERN_ERR TTM_PFX " gpu_offset: 0x%08lX\n", man->gpu_offset);
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010075 printk(KERN_ERR TTM_PFX " size: %llu\n", man->size);
Jerome Glissefb53f862009-12-09 21:55:10 +010076 printk(KERN_ERR TTM_PFX " available_caching: 0x%08X\n",
77 man->available_caching);
78 printk(KERN_ERR TTM_PFX " default_caching: 0x%08X\n",
79 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
Jerome Glisseeb6d2c32009-12-10 16:15:52 +010089 printk(KERN_ERR TTM_PFX "No space for %p (%lu pages, %luK, %luM)\n",
Jerome Glissefb53f862009-12-09 21:55:10 +010090 bo, bo->mem.num_pages, bo->mem.size >> 10,
91 bo->mem.size >> 20);
92 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;
Jerome Glissefb53f862009-12-09 21:55:10 +010097 printk(KERN_ERR TTM_PFX " 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;
140
141 BUG_ON(atomic_read(&bo->list_kref.refcount));
142 BUG_ON(atomic_read(&bo->kref.refcount));
143 BUG_ON(atomic_read(&bo->cpu_writers));
144 BUG_ON(bo->sync_obj != NULL);
145 BUG_ON(bo->mem.mm_node != NULL);
146 BUG_ON(!list_empty(&bo->lru));
147 BUG_ON(!list_empty(&bo->ddestroy));
148
149 if (bo->ttm)
150 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200151 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200152 if (bo->destroy)
153 bo->destroy(bo);
154 else {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200155 ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 kfree(bo);
157 }
158}
159
160int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
161{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200162 if (interruptible) {
Jean Delvare965d3802010-10-09 12:36:45 +0000163 return wait_event_interruptible(bo->event_queue,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164 atomic_read(&bo->reserved) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165 } else {
166 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
Jean Delvare965d3802010-10-09 12:36:45 +0000167 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200168 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000170EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200171
Dave Airlied6ea8882010-11-22 13:24:40 +1000172void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200173{
174 struct ttm_bo_device *bdev = bo->bdev;
175 struct ttm_mem_type_manager *man;
176
177 BUG_ON(!atomic_read(&bo->reserved));
178
179 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
180
181 BUG_ON(!list_empty(&bo->lru));
182
183 man = &bdev->man[bo->mem.mem_type];
184 list_add_tail(&bo->lru, &man->lru);
185 kref_get(&bo->list_kref);
186
187 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200188 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200189 kref_get(&bo->list_kref);
190 }
191 }
192}
193
Dave Airlied6ea8882010-11-22 13:24:40 +1000194int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200195{
196 int put_count = 0;
197
198 if (!list_empty(&bo->swap)) {
199 list_del_init(&bo->swap);
200 ++put_count;
201 }
202 if (!list_empty(&bo->lru)) {
203 list_del_init(&bo->lru);
204 ++put_count;
205 }
206
207 /*
208 * TODO: Add a driver hook to delete from
209 * driver-specific LRU's here.
210 */
211
212 return put_count;
213}
214
215int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
216 bool interruptible,
217 bool no_wait, bool use_sequence, uint32_t sequence)
218{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200219 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200220 int ret;
221
222 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100223 /**
224 * Deadlock avoidance for multi-bo reserving.
225 */
Thomas Hellstrom96726fe2010-11-17 12:28:28 +0000226 if (use_sequence && bo->seq_valid) {
227 /**
228 * We've already reserved this one.
229 */
230 if (unlikely(sequence == bo->val_seq))
231 return -EDEADLK;
232 /**
233 * Already reserved by a thread that will not back
234 * off for us. We need to back off.
235 */
236 if (unlikely(sequence - bo->val_seq < (1 << 31)))
237 return -EAGAIN;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200238 }
239
240 if (no_wait)
241 return -EBUSY;
242
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200243 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200244 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200245 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200246
247 if (unlikely(ret))
248 return ret;
249 }
250
251 if (use_sequence) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100252 /**
253 * Wake up waiters that may need to recheck for deadlock,
254 * if we decreased the sequence number.
255 */
256 if (unlikely((bo->val_seq - sequence < (1 << 31))
257 || !bo->seq_valid))
258 wake_up_all(&bo->event_queue);
259
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200260 bo->val_seq = sequence;
261 bo->seq_valid = true;
262 } else {
263 bo->seq_valid = false;
264 }
265
266 return 0;
267}
268EXPORT_SYMBOL(ttm_bo_reserve);
269
270static void ttm_bo_ref_bug(struct kref *list_kref)
271{
272 BUG();
273}
274
Dave Airlied6ea8882010-11-22 13:24:40 +1000275void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
276 bool never_free)
277{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100278 kref_sub(&bo->list_kref, count,
279 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000280}
281
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200282int ttm_bo_reserve(struct ttm_buffer_object *bo,
283 bool interruptible,
284 bool no_wait, bool use_sequence, uint32_t sequence)
285{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200286 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200287 int put_count = 0;
288 int ret;
289
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200290 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200291 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
292 sequence);
293 if (likely(ret == 0))
294 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200295 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200296
Dave Airlied6ea8882010-11-22 13:24:40 +1000297 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200298
299 return ret;
300}
301
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000302void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
303{
304 ttm_bo_add_to_lru(bo);
305 atomic_set(&bo->reserved, 0);
306 wake_up_all(&bo->event_queue);
307}
308
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200309void ttm_bo_unreserve(struct ttm_buffer_object *bo)
310{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200311 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200312
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200313 spin_lock(&glob->lru_lock);
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000314 ttm_bo_unreserve_locked(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200315 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200316}
317EXPORT_SYMBOL(ttm_bo_unreserve);
318
319/*
320 * Call bo->mutex locked.
321 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200322static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
323{
324 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200325 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200326 int ret = 0;
327 uint32_t page_flags = 0;
328
329 TTM_ASSERT_LOCKED(&bo->mutex);
330 bo->ttm = NULL;
331
Dave Airliead49f502009-07-10 22:36:26 +1000332 if (bdev->need_dma32)
333 page_flags |= TTM_PAGE_FLAG_DMA32;
334
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200335 switch (bo->type) {
336 case ttm_bo_type_device:
337 if (zero_alloc)
338 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
339 case ttm_bo_type_kernel:
340 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200341 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200342 if (unlikely(bo->ttm == NULL))
343 ret = -ENOMEM;
344 break;
345 case ttm_bo_type_user:
346 bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
347 page_flags | TTM_PAGE_FLAG_USER,
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200348 glob->dummy_read_page);
Dave Airlie447aeb92009-12-08 09:25:45 +1000349 if (unlikely(bo->ttm == NULL)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200350 ret = -ENOMEM;
Dave Airlie447aeb92009-12-08 09:25:45 +1000351 break;
352 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200353
354 ret = ttm_tt_set_user(bo->ttm, current,
355 bo->buffer_start, bo->num_pages);
Marcin Slusarz7c4c3962011-08-22 21:17:57 +0000356 if (unlikely(ret != 0)) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200357 ttm_tt_destroy(bo->ttm);
Marcin Slusarz7c4c3962011-08-22 21:17:57 +0000358 bo->ttm = NULL;
359 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200360 break;
361 default:
362 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
363 ret = -EINVAL;
364 break;
365 }
366
367 return ret;
368}
369
370static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
371 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000372 bool evict, bool interruptible,
373 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200374{
375 struct ttm_bo_device *bdev = bo->bdev;
376 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
377 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
378 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
379 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
380 int ret = 0;
381
382 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100383 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
384 ret = ttm_mem_io_lock(old_man, true);
385 if (unlikely(ret != 0))
386 goto out_err;
387 ttm_bo_unmap_virtual_locked(bo);
388 ttm_mem_io_unlock(old_man);
389 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200390
391 /*
392 * Create and bind a ttm if required.
393 */
394
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000395 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
396 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000397 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
398 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000399 if (ret)
400 goto out_err;
401 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200402
403 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
404 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200405 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200406
407 if (mem->mem_type != TTM_PL_SYSTEM) {
408 ret = ttm_tt_bind(bo->ttm, mem);
409 if (ret)
410 goto out_err;
411 }
412
413 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000414 if (bdev->driver->move_notify)
415 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100416 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200417 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200418 goto moved;
419 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200420 }
421
Dave Airliee024e112009-06-24 09:48:08 +1000422 if (bdev->driver->move_notify)
423 bdev->driver->move_notify(bo, mem);
424
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200425 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
426 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000427 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200428 else if (bdev->driver->move)
429 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000430 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200431 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000432 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200433
434 if (ret)
435 goto out_err;
436
437moved:
438 if (bo->evicted) {
439 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
440 if (ret)
441 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
442 bo->evicted = false;
443 }
444
445 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000446 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200447 bdev->man[bo->mem.mem_type].gpu_offset;
448 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100449 } else
450 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200451
452 return 0;
453
454out_err:
455 new_man = &bdev->man[bo->mem.mem_type];
456 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
457 ttm_tt_unbind(bo->ttm);
458 ttm_tt_destroy(bo->ttm);
459 bo->ttm = NULL;
460 }
461
462 return ret;
463}
464
465/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200466 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200467 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200468 * This is the place to put in driver specific hooks to release
469 * driver private resources.
470 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200471 */
472
473static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
474{
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200475 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200476 ttm_tt_unbind(bo->ttm);
477 ttm_tt_destroy(bo->ttm);
478 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200479 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200480 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200481
482 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200483
484 /*
485 * Make processes trying to reserve really pick it up.
486 */
487 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200488 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200489}
490
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200491static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200492{
493 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200494 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200495 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000496 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200497 void *sync_obj_arg;
498 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200499 int ret;
500
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000501 spin_lock(&bdev->fence_lock);
Marek Olšákdfadbbd2011-08-13 20:32:11 +0000502 (void) ttm_bo_wait(bo, false, false, true, TTM_USAGE_READWRITE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200503 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200504
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200505 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100506
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200507 /**
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000508 * Lock inversion between bo:reserve and bdev::fence_lock here,
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200509 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200510 */
511
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200512 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200513
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200514 if (unlikely(ret == -EBUSY))
515 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200516
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000517 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200518 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200519
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200520 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200521 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200522
Dave Airlied6ea8882010-11-22 13:24:40 +1000523 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200524
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200525 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200526 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200527 spin_lock(&glob->lru_lock);
528 }
529queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200530 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000531 if (bo->sync_obj)
532 sync_obj = driver->sync_obj_ref(bo->sync_obj);
533 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200534
535 kref_get(&bo->list_kref);
536 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
537 spin_unlock(&glob->lru_lock);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000538 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200539
Thomas Hellstromaa123262010-11-02 13:21:47 +0000540 if (sync_obj) {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200541 driver->sync_obj_flush(sync_obj, sync_obj_arg);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000542 driver->sync_obj_unref(&sync_obj);
543 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200544 schedule_delayed_work(&bdev->wq,
545 ((HZ / 100) < 1) ? 1 : HZ / 100);
546}
547
548/**
549 * function ttm_bo_cleanup_refs
550 * If bo idle, remove from delayed- and lru lists, and unref.
551 * If not idle, do nothing.
552 *
553 * @interruptible Any sleeps should occur interruptibly.
554 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
555 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
556 */
557
558static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
559 bool interruptible,
560 bool no_wait_reserve,
561 bool no_wait_gpu)
562{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000563 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200564 struct ttm_bo_global *glob = bo->glob;
565 int put_count;
566 int ret = 0;
567
568retry:
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000569 spin_lock(&bdev->fence_lock);
Marek Olšákdfadbbd2011-08-13 20:32:11 +0000570 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu,
571 TTM_USAGE_READWRITE);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000572 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200573
574 if (unlikely(ret != 0))
575 return ret;
576
577 spin_lock(&glob->lru_lock);
578 ret = ttm_bo_reserve_locked(bo, interruptible,
579 no_wait_reserve, false, 0);
580
581 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200582 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200583 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200584 }
585
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200586 /**
587 * We can re-check for sync object without taking
588 * the bo::lock since setting the sync object requires
589 * also bo::reserved. A busy object at this point may
590 * be caused by another thread recently starting an accelerated
591 * eviction.
592 */
593
594 if (unlikely(bo->sync_obj)) {
595 atomic_set(&bo->reserved, 0);
596 wake_up_all(&bo->event_queue);
597 spin_unlock(&glob->lru_lock);
598 goto retry;
599 }
600
601 put_count = ttm_bo_del_from_lru(bo);
602 list_del_init(&bo->ddestroy);
603 ++put_count;
604
605 spin_unlock(&glob->lru_lock);
606 ttm_bo_cleanup_memtype_use(bo);
607
Dave Airlied6ea8882010-11-22 13:24:40 +1000608 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200609
610 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200611}
612
613/**
614 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
615 * encountered buffers.
616 */
617
618static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
619{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200620 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100621 struct ttm_buffer_object *entry = NULL;
622 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200623
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200624 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100625 if (list_empty(&bdev->ddestroy))
626 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200627
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100628 entry = list_first_entry(&bdev->ddestroy,
629 struct ttm_buffer_object, ddestroy);
630 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200631
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100632 for (;;) {
633 struct ttm_buffer_object *nentry = NULL;
634
635 if (entry->ddestroy.next != &bdev->ddestroy) {
636 nentry = list_first_entry(&entry->ddestroy,
637 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200638 kref_get(&nentry->list_kref);
639 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200640
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200641 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200642 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
643 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200644 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100645 entry = nentry;
646
647 if (ret || !entry)
648 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200650 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100651 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200652 break;
653 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200654
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100655out_unlock:
656 spin_unlock(&glob->lru_lock);
657out:
658 if (entry)
659 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200660 return ret;
661}
662
663static void ttm_bo_delayed_workqueue(struct work_struct *work)
664{
665 struct ttm_bo_device *bdev =
666 container_of(work, struct ttm_bo_device, wq.work);
667
668 if (ttm_bo_delayed_delete(bdev, false)) {
669 schedule_delayed_work(&bdev->wq,
670 ((HZ / 100) < 1) ? 1 : HZ / 100);
671 }
672}
673
674static void ttm_bo_release(struct kref *kref)
675{
676 struct ttm_buffer_object *bo =
677 container_of(kref, struct ttm_buffer_object, kref);
678 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100679 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200680
681 if (likely(bo->vm_node != NULL)) {
682 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
683 drm_mm_put_block(bo->vm_node);
684 bo->vm_node = NULL;
685 }
686 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100687 ttm_mem_io_lock(man, false);
688 ttm_mem_io_free_vm(bo);
689 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200690 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200691 kref_put(&bo->list_kref, ttm_bo_release_list);
692 write_lock(&bdev->vm_lock);
693}
694
695void ttm_bo_unref(struct ttm_buffer_object **p_bo)
696{
697 struct ttm_buffer_object *bo = *p_bo;
698 struct ttm_bo_device *bdev = bo->bdev;
699
700 *p_bo = NULL;
701 write_lock(&bdev->vm_lock);
702 kref_put(&bo->kref, ttm_bo_release);
703 write_unlock(&bdev->vm_lock);
704}
705EXPORT_SYMBOL(ttm_bo_unref);
706
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400707int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
708{
709 return cancel_delayed_work_sync(&bdev->wq);
710}
711EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
712
713void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
714{
715 if (resched)
716 schedule_delayed_work(&bdev->wq,
717 ((HZ / 100) < 1) ? 1 : HZ / 100);
718}
719EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
720
Jerome Glisseca262a9992009-12-08 15:33:32 +0100721static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000722 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200723{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200724 struct ttm_bo_device *bdev = bo->bdev;
725 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100726 struct ttm_placement placement;
727 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200728
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000729 spin_lock(&bdev->fence_lock);
Marek Olšákdfadbbd2011-08-13 20:32:11 +0000730 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu,
731 TTM_USAGE_READWRITE);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000732 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200733
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200734 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100735 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200736 printk(KERN_ERR TTM_PFX
737 "Failed to expire sync object before "
738 "buffer eviction.\n");
739 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200740 goto out;
741 }
742
743 BUG_ON(!atomic_read(&bo->reserved));
744
745 evict_mem = bo->mem;
746 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100747 evict_mem.bus.io_reserved_vm = false;
748 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200749
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100750 placement.fpfn = 0;
751 placement.lpfn = 0;
752 placement.num_placement = 0;
753 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100754 bdev->driver->evict_flags(bo, &placement);
755 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000756 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200757 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100758 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200759 printk(KERN_ERR TTM_PFX
760 "Failed to find memory space for "
761 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100762 ttm_bo_mem_space_debug(bo, &placement);
763 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200764 goto out;
765 }
766
767 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000768 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200769 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100770 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200771 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000772 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200773 goto out;
774 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200775 bo->evicted = true;
776out:
777 return ret;
778}
779
Jerome Glisseca262a9992009-12-08 15:33:32 +0100780static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
781 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000782 bool interruptible, bool no_wait_reserve,
783 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100784{
785 struct ttm_bo_global *glob = bdev->glob;
786 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
787 struct ttm_buffer_object *bo;
788 int ret, put_count = 0;
789
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100790retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100791 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100792 if (list_empty(&man->lru)) {
793 spin_unlock(&glob->lru_lock);
794 return -EBUSY;
795 }
796
Jerome Glisseca262a9992009-12-08 15:33:32 +0100797 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
798 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100799
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200800 if (!list_empty(&bo->ddestroy)) {
801 spin_unlock(&glob->lru_lock);
802 ret = ttm_bo_cleanup_refs(bo, interruptible,
803 no_wait_reserve, no_wait_gpu);
804 kref_put(&bo->list_kref, ttm_bo_release_list);
805
806 if (likely(ret == 0 || ret == -ERESTARTSYS))
807 return ret;
808
809 goto retry;
810 }
811
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000812 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100813
814 if (unlikely(ret == -EBUSY)) {
815 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000816 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100817 ret = ttm_bo_wait_unreserved(bo, interruptible);
818
819 kref_put(&bo->list_kref, ttm_bo_release_list);
820
821 /**
822 * We *need* to retry after releasing the lru lock.
823 */
824
825 if (unlikely(ret != 0))
826 return ret;
827 goto retry;
828 }
829
830 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100831 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100832
833 BUG_ON(ret != 0);
834
Dave Airlied6ea8882010-11-22 13:24:40 +1000835 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100836
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000837 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100838 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100839
Jerome Glisseca262a9992009-12-08 15:33:32 +0100840 kref_put(&bo->list_kref, ttm_bo_release_list);
841 return ret;
842}
843
Ben Skeggs42311ff2010-08-04 12:07:08 +1000844void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
845{
Ben Skeggsd961db72010-08-05 10:48:18 +1000846 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000847
Ben Skeggsd961db72010-08-05 10:48:18 +1000848 if (mem->mm_node)
849 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000850}
851EXPORT_SYMBOL(ttm_bo_mem_put);
852
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200853/**
854 * Repeatedly evict memory from the LRU for @mem_type until we create enough
855 * space, or we've evicted everything and there isn't enough space.
856 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100857static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
858 uint32_t mem_type,
859 struct ttm_placement *placement,
860 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000861 bool interruptible,
862 bool no_wait_reserve,
863 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200864{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100865 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200866 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867 int ret;
868
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200869 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000870 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200871 if (unlikely(ret != 0))
872 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000873 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100874 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100875 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000876 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100877 if (unlikely(ret != 0))
878 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200879 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000880 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200881 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200882 mem->mem_type = mem_type;
883 return 0;
884}
885
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200886static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
887 uint32_t cur_placement,
888 uint32_t proposed_placement)
889{
890 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
891 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
892
893 /**
894 * Keep current caching if possible.
895 */
896
897 if ((cur_placement & caching) != 0)
898 result |= (cur_placement & caching);
899 else if ((man->default_caching & caching) != 0)
900 result |= man->default_caching;
901 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
902 result |= TTM_PL_FLAG_CACHED;
903 else if ((TTM_PL_FLAG_WC & caching) != 0)
904 result |= TTM_PL_FLAG_WC;
905 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
906 result |= TTM_PL_FLAG_UNCACHED;
907
908 return result;
909}
910
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200911static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
912 bool disallow_fixed,
913 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200914 uint32_t proposed_placement,
915 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200916{
917 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
918
919 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
920 return false;
921
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200922 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923 return false;
924
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200925 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200927
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200928 cur_flags |= (proposed_placement & man->available_caching);
929
930 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200931 return true;
932}
933
934/**
935 * Creates space for memory region @mem according to its type.
936 *
937 * This function first searches for free space in compatible memory types in
938 * the priority order defined by the driver. If free space isn't found, then
939 * ttm_bo_mem_force_space is attempted in priority order to evict and find
940 * space.
941 */
942int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100943 struct ttm_placement *placement,
944 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000945 bool interruptible, bool no_wait_reserve,
946 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200947{
948 struct ttm_bo_device *bdev = bo->bdev;
949 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200950 uint32_t mem_type = TTM_PL_SYSTEM;
951 uint32_t cur_flags = 0;
952 bool type_found = false;
953 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100954 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100955 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200956
957 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000958 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100959 ret = ttm_mem_type_from_flags(placement->placement[i],
960 &mem_type);
961 if (ret)
962 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200963 man = &bdev->man[mem_type];
964
965 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100966 bo->type == ttm_bo_type_user,
967 mem_type,
968 placement->placement[i],
969 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200970
971 if (!type_ok)
972 continue;
973
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200974 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
975 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100976 /*
977 * Use the access and other non-mapping-related flag bits from
978 * the memory placement flags to the current flags
979 */
980 ttm_flag_masked(&cur_flags, placement->placement[i],
981 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200982
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200983 if (mem_type == TTM_PL_SYSTEM)
984 break;
985
986 if (man->has_type && man->use_type) {
987 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000988 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100989 if (unlikely(ret))
990 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200991 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000992 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200993 break;
994 }
995
Ben Skeggsd961db72010-08-05 10:48:18 +1000996 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997 mem->mem_type = mem_type;
998 mem->placement = cur_flags;
999 return 0;
1000 }
1001
1002 if (!type_found)
1003 return -EINVAL;
1004
Dave Airlieb6637522009-12-14 14:51:35 +10001005 for (i = 0; i < placement->num_busy_placement; ++i) {
1006 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001007 &mem_type);
1008 if (ret)
1009 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001010 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001011 if (!man->has_type)
1012 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001013 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001014 bo->type == ttm_bo_type_user,
1015 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001016 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001017 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001018 continue;
1019
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001020 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1021 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001022 /*
1023 * Use the access and other non-mapping-related flag bits from
1024 * the memory placement flags to the current flags
1025 */
Dave Airlieb6637522009-12-14 14:51:35 +10001026 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001027 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001028
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001029
1030 if (mem_type == TTM_PL_SYSTEM) {
1031 mem->mem_type = mem_type;
1032 mem->placement = cur_flags;
1033 mem->mm_node = NULL;
1034 return 0;
1035 }
1036
Jerome Glisseca262a9992009-12-08 15:33:32 +01001037 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001038 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001039 if (ret == 0 && mem->mm_node) {
1040 mem->placement = cur_flags;
1041 return 0;
1042 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001043 if (ret == -ERESTARTSYS)
1044 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001045 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001046 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001047 return ret;
1048}
1049EXPORT_SYMBOL(ttm_bo_mem_space);
1050
1051int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1052{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001053 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1054 return -EBUSY;
1055
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001056 return wait_event_interruptible(bo->event_queue,
1057 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001058}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001059EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060
1061int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001062 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001063 bool interruptible, bool no_wait_reserve,
1064 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001065{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001066 int ret = 0;
1067 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001068 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001069
1070 BUG_ON(!atomic_read(&bo->reserved));
1071
1072 /*
1073 * FIXME: It's possible to pipeline buffer moves.
1074 * Have the driver move function wait for idle when necessary,
1075 * instead of doing it here.
1076 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001077 spin_lock(&bdev->fence_lock);
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001078 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu,
1079 TTM_USAGE_READWRITE);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001080 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001081 if (ret)
1082 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001083 mem.num_pages = bo->num_pages;
1084 mem.size = mem.num_pages << PAGE_SHIFT;
1085 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001086 mem.bus.io_reserved_vm = false;
1087 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001088 /*
1089 * Determine where to move the buffer.
1090 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001091 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001092 if (ret)
1093 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001094 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001095out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001096 if (ret && mem.mm_node)
1097 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098 return ret;
1099}
1100
Jerome Glisseca262a9992009-12-08 15:33:32 +01001101static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102 struct ttm_mem_reg *mem)
1103{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001104 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001105
Ben Skeggsd961db72010-08-05 10:48:18 +10001106 if (mem->mm_node && placement->lpfn != 0 &&
1107 (mem->start < placement->fpfn ||
1108 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001109 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110
Jerome Glisseca262a9992009-12-08 15:33:32 +01001111 for (i = 0; i < placement->num_placement; i++) {
1112 if ((placement->placement[i] & mem->placement &
1113 TTM_PL_MASK_CACHING) &&
1114 (placement->placement[i] & mem->placement &
1115 TTM_PL_MASK_MEM))
1116 return i;
1117 }
1118 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001119}
1120
Jerome Glisse09855ac2009-12-10 17:16:27 +01001121int ttm_bo_validate(struct ttm_buffer_object *bo,
1122 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001123 bool interruptible, bool no_wait_reserve,
1124 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125{
1126 int ret;
1127
1128 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001129 /* Check that range is valid */
1130 if (placement->lpfn || placement->fpfn)
1131 if (placement->fpfn > placement->lpfn ||
1132 (placement->lpfn - placement->fpfn) < bo->num_pages)
1133 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001134 /*
1135 * Check whether we need to move buffer.
1136 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001137 ret = ttm_bo_mem_compat(placement, &bo->mem);
1138 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001139 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001140 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001141 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001142 } else {
1143 /*
1144 * Use the access and other non-mapping-related flag bits from
1145 * the compatible memory placement flags to the active flags
1146 */
1147 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1148 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001150 /*
1151 * We might need to add a TTM.
1152 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1154 ret = ttm_bo_add_ttm(bo, true);
1155 if (ret)
1156 return ret;
1157 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158 return 0;
1159}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001160EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161
Jerome Glisse09855ac2009-12-10 17:16:27 +01001162int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1163 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001164{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001165 BUG_ON((placement->fpfn || placement->lpfn) &&
1166 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001167
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001168 return 0;
1169}
1170
Jerome Glisse09855ac2009-12-10 17:16:27 +01001171int ttm_bo_init(struct ttm_bo_device *bdev,
1172 struct ttm_buffer_object *bo,
1173 unsigned long size,
1174 enum ttm_bo_type type,
1175 struct ttm_placement *placement,
1176 uint32_t page_alignment,
1177 unsigned long buffer_start,
1178 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001179 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001180 size_t acc_size,
1181 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001182{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001183 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001184 unsigned long num_pages;
1185
1186 size += buffer_start & ~PAGE_MASK;
1187 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1188 if (num_pages == 0) {
1189 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001190 if (destroy)
1191 (*destroy)(bo);
1192 else
1193 kfree(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001194 return -EINVAL;
1195 }
1196 bo->destroy = destroy;
1197
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001198 kref_init(&bo->kref);
1199 kref_init(&bo->list_kref);
1200 atomic_set(&bo->cpu_writers, 0);
1201 atomic_set(&bo->reserved, 1);
1202 init_waitqueue_head(&bo->event_queue);
1203 INIT_LIST_HEAD(&bo->lru);
1204 INIT_LIST_HEAD(&bo->ddestroy);
1205 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001206 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001207 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001208 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001209 bo->type = type;
1210 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001211 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212 bo->mem.mem_type = TTM_PL_SYSTEM;
1213 bo->mem.num_pages = bo->num_pages;
1214 bo->mem.mm_node = NULL;
1215 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001216 bo->mem.bus.io_reserved_vm = false;
1217 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001218 bo->buffer_start = buffer_start & PAGE_MASK;
1219 bo->priv_flags = 0;
1220 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1221 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001222 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001224 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001225
Jerome Glisse09855ac2009-12-10 17:16:27 +01001226 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001227 if (unlikely(ret != 0))
1228 goto out_err;
1229
1230 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231 * For ttm_bo_type_device buffers, allocate
1232 * address space from the device.
1233 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001234 if (bo->type == ttm_bo_type_device) {
1235 ret = ttm_bo_setup_vm(bo);
1236 if (ret)
1237 goto out_err;
1238 }
1239
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001240 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001241 if (ret)
1242 goto out_err;
1243
1244 ttm_bo_unreserve(bo);
1245 return 0;
1246
1247out_err:
1248 ttm_bo_unreserve(bo);
1249 ttm_bo_unref(&bo);
1250
1251 return ret;
1252}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001253EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001254
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001255static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256 unsigned long num_pages)
1257{
1258 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1259 PAGE_MASK;
1260
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001261 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001262}
1263
Jerome Glisse09855ac2009-12-10 17:16:27 +01001264int ttm_bo_create(struct ttm_bo_device *bdev,
1265 unsigned long size,
1266 enum ttm_bo_type type,
1267 struct ttm_placement *placement,
1268 uint32_t page_alignment,
1269 unsigned long buffer_start,
1270 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001271 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001272 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001273{
1274 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001275 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001276 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001277
1278 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001279 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001280 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001281 if (unlikely(ret != 0))
1282 return ret;
1283
1284 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1285
1286 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001287 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001288 return -ENOMEM;
1289 }
1290
Jerome Glisse09855ac2009-12-10 17:16:27 +01001291 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1292 buffer_start, interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001293 persistent_swap_storage, acc_size, 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,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001302 unsigned mem_type, bool allow_errors)
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;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001307
1308 /*
1309 * Can't use standard list traversal since we're unlocking.
1310 */
1311
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001312 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001313 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001314 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001315 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001316 if (ret) {
1317 if (allow_errors) {
1318 return ret;
1319 } else {
1320 printk(KERN_ERR TTM_PFX
1321 "Cleanup eviction failed\n");
1322 }
1323 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001324 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001325 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001326 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001327 return 0;
1328}
1329
1330int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1331{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001332 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001333 int ret = -EINVAL;
1334
1335 if (mem_type >= TTM_NUM_MEM_TYPES) {
1336 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1337 return ret;
1338 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001339 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001340
1341 if (!man->has_type) {
1342 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1343 "memory manager type %u\n", mem_type);
1344 return ret;
1345 }
1346
1347 man->use_type = false;
1348 man->has_type = false;
1349
1350 ret = 0;
1351 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001352 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001353
Ben Skeggsd961db72010-08-05 10:48:18 +10001354 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001355 }
1356
1357 return ret;
1358}
1359EXPORT_SYMBOL(ttm_bo_clean_mm);
1360
1361int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1362{
1363 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1364
1365 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1366 printk(KERN_ERR TTM_PFX
1367 "Illegal memory manager memory type %u.\n",
1368 mem_type);
1369 return -EINVAL;
1370 }
1371
1372 if (!man->has_type) {
1373 printk(KERN_ERR TTM_PFX
1374 "Memory type %u has not been initialized.\n",
1375 mem_type);
1376 return 0;
1377 }
1378
Jerome Glisseca262a9992009-12-08 15:33:32 +01001379 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001380}
1381EXPORT_SYMBOL(ttm_bo_evict_mm);
1382
1383int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001384 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001385{
1386 int ret = -EINVAL;
1387 struct ttm_mem_type_manager *man;
1388
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001389 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001390 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001391 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001392 man->io_reserve_fastpath = true;
1393 man->use_io_reserve_lru = false;
1394 mutex_init(&man->io_reserve_mutex);
1395 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001396
1397 ret = bdev->driver->init_mem_type(bdev, type, man);
1398 if (ret)
1399 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001400 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001401
1402 ret = 0;
1403 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001404 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001405 if (ret)
1406 return ret;
1407 }
1408 man->has_type = true;
1409 man->use_type = true;
1410 man->size = p_size;
1411
1412 INIT_LIST_HEAD(&man->lru);
1413
1414 return 0;
1415}
1416EXPORT_SYMBOL(ttm_bo_init_mm);
1417
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001418static void ttm_bo_global_kobj_release(struct kobject *kobj)
1419{
1420 struct ttm_bo_global *glob =
1421 container_of(kobj, struct ttm_bo_global, kobj);
1422
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001423 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1424 __free_page(glob->dummy_read_page);
1425 kfree(glob);
1426}
1427
Dave Airlieba4420c2010-03-09 10:56:52 +10001428void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001429{
1430 struct ttm_bo_global *glob = ref->object;
1431
1432 kobject_del(&glob->kobj);
1433 kobject_put(&glob->kobj);
1434}
1435EXPORT_SYMBOL(ttm_bo_global_release);
1436
Dave Airlieba4420c2010-03-09 10:56:52 +10001437int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001438{
1439 struct ttm_bo_global_ref *bo_ref =
1440 container_of(ref, struct ttm_bo_global_ref, ref);
1441 struct ttm_bo_global *glob = ref->object;
1442 int ret;
1443
1444 mutex_init(&glob->device_list_mutex);
1445 spin_lock_init(&glob->lru_lock);
1446 glob->mem_glob = bo_ref->mem_glob;
1447 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1448
1449 if (unlikely(glob->dummy_read_page == NULL)) {
1450 ret = -ENOMEM;
1451 goto out_no_drp;
1452 }
1453
1454 INIT_LIST_HEAD(&glob->swap_lru);
1455 INIT_LIST_HEAD(&glob->device_list);
1456
1457 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1458 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1459 if (unlikely(ret != 0)) {
1460 printk(KERN_ERR TTM_PFX
1461 "Could not register buffer object swapout.\n");
1462 goto out_no_shrink;
1463 }
1464
1465 glob->ttm_bo_extra_size =
1466 ttm_round_pot(sizeof(struct ttm_tt)) +
1467 ttm_round_pot(sizeof(struct ttm_backend));
1468
1469 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1470 ttm_round_pot(sizeof(struct ttm_buffer_object));
1471
1472 atomic_set(&glob->bo_count, 0);
1473
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001474 ret = kobject_init_and_add(
1475 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001476 if (unlikely(ret != 0))
1477 kobject_put(&glob->kobj);
1478 return ret;
1479out_no_shrink:
1480 __free_page(glob->dummy_read_page);
1481out_no_drp:
1482 kfree(glob);
1483 return ret;
1484}
1485EXPORT_SYMBOL(ttm_bo_global_init);
1486
1487
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001488int ttm_bo_device_release(struct ttm_bo_device *bdev)
1489{
1490 int ret = 0;
1491 unsigned i = TTM_NUM_MEM_TYPES;
1492 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001493 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001494
1495 while (i--) {
1496 man = &bdev->man[i];
1497 if (man->has_type) {
1498 man->use_type = false;
1499 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1500 ret = -EBUSY;
1501 printk(KERN_ERR TTM_PFX
1502 "DRM memory manager type %d "
1503 "is not clean.\n", i);
1504 }
1505 man->has_type = false;
1506 }
1507 }
1508
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001509 mutex_lock(&glob->device_list_mutex);
1510 list_del(&bdev->device_list);
1511 mutex_unlock(&glob->device_list_mutex);
1512
Tejun Heof094cfc2010-12-24 15:59:06 +01001513 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001514
1515 while (ttm_bo_delayed_delete(bdev, true))
1516 ;
1517
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001518 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001519 if (list_empty(&bdev->ddestroy))
1520 TTM_DEBUG("Delayed destroy list was clean\n");
1521
1522 if (list_empty(&bdev->man[0].lru))
1523 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001524 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1527 write_lock(&bdev->vm_lock);
1528 drm_mm_takedown(&bdev->addr_space_mm);
1529 write_unlock(&bdev->vm_lock);
1530
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531 return ret;
1532}
1533EXPORT_SYMBOL(ttm_bo_device_release);
1534
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001535int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001536 struct ttm_bo_global *glob,
1537 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001538 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001539 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001540{
1541 int ret = -EINVAL;
1542
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001543 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001544 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001545
1546 memset(bdev->man, 0, sizeof(bdev->man));
1547
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001548 /*
1549 * Initialize the system memory buffer type.
1550 * Other types need to be driver / IOCTL initialized.
1551 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001552 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001554 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001555
1556 bdev->addr_space_rb = RB_ROOT;
1557 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1558 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001559 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001560
1561 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1562 bdev->nice_mode = true;
1563 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001564 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001565 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001566 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001567 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001568 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001569 mutex_lock(&glob->device_list_mutex);
1570 list_add_tail(&bdev->device_list, &glob->device_list);
1571 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001572
1573 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001574out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001576out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001577 return ret;
1578}
1579EXPORT_SYMBOL(ttm_bo_device_init);
1580
1581/*
1582 * buffer object vm functions.
1583 */
1584
1585bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1586{
1587 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1588
1589 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1590 if (mem->mem_type == TTM_PL_SYSTEM)
1591 return false;
1592
1593 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1594 return false;
1595
1596 if (mem->placement & TTM_PL_FLAG_CACHED)
1597 return false;
1598 }
1599 return true;
1600}
1601
Thomas Hellstromeba67092010-11-11 09:41:57 +01001602void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001603{
1604 struct ttm_bo_device *bdev = bo->bdev;
1605 loff_t offset = (loff_t) bo->addr_space_offset;
1606 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1607
1608 if (!bdev->dev_mapping)
1609 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001610 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001611 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001612}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001613
1614void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1615{
1616 struct ttm_bo_device *bdev = bo->bdev;
1617 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1618
1619 ttm_mem_io_lock(man, false);
1620 ttm_bo_unmap_virtual_locked(bo);
1621 ttm_mem_io_unlock(man);
1622}
1623
1624
Dave Airliee024e112009-06-24 09:48:08 +10001625EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001626
1627static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1628{
1629 struct ttm_bo_device *bdev = bo->bdev;
1630 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1631 struct rb_node *parent = NULL;
1632 struct ttm_buffer_object *cur_bo;
1633 unsigned long offset = bo->vm_node->start;
1634 unsigned long cur_offset;
1635
1636 while (*cur) {
1637 parent = *cur;
1638 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1639 cur_offset = cur_bo->vm_node->start;
1640 if (offset < cur_offset)
1641 cur = &parent->rb_left;
1642 else if (offset > cur_offset)
1643 cur = &parent->rb_right;
1644 else
1645 BUG();
1646 }
1647
1648 rb_link_node(&bo->vm_rb, parent, cur);
1649 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1650}
1651
1652/**
1653 * ttm_bo_setup_vm:
1654 *
1655 * @bo: the buffer to allocate address space for
1656 *
1657 * Allocate address space in the drm device so that applications
1658 * can mmap the buffer and access the contents. This only
1659 * applies to ttm_bo_type_device objects as others are not
1660 * placed in the drm device address space.
1661 */
1662
1663static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1664{
1665 struct ttm_bo_device *bdev = bo->bdev;
1666 int ret;
1667
1668retry_pre_get:
1669 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1670 if (unlikely(ret != 0))
1671 return ret;
1672
1673 write_lock(&bdev->vm_lock);
1674 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1675 bo->mem.num_pages, 0, 0);
1676
1677 if (unlikely(bo->vm_node == NULL)) {
1678 ret = -ENOMEM;
1679 goto out_unlock;
1680 }
1681
1682 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1683 bo->mem.num_pages, 0);
1684
1685 if (unlikely(bo->vm_node == NULL)) {
1686 write_unlock(&bdev->vm_lock);
1687 goto retry_pre_get;
1688 }
1689
1690 ttm_bo_vm_insert_rb(bo);
1691 write_unlock(&bdev->vm_lock);
1692 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1693
1694 return 0;
1695out_unlock:
1696 write_unlock(&bdev->vm_lock);
1697 return ret;
1698}
1699
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001700static void ttm_bo_unref_sync_obj_locked(struct ttm_buffer_object *bo,
1701 void *sync_obj,
1702 void **extra_sync_obj)
1703{
1704 struct ttm_bo_device *bdev = bo->bdev;
1705 struct ttm_bo_driver *driver = bdev->driver;
1706 void *tmp_obj = NULL, *tmp_obj_read = NULL, *tmp_obj_write = NULL;
1707
1708 /* We must unref the sync obj wherever it's ref'd.
1709 * Note that if we unref bo->sync_obj, we can unref both the read
1710 * and write sync objs too, because they can't be newer than
1711 * bo->sync_obj, so they are no longer relevant. */
1712 if (sync_obj == bo->sync_obj ||
1713 sync_obj == bo->sync_obj_read) {
1714 tmp_obj_read = bo->sync_obj_read;
1715 bo->sync_obj_read = NULL;
1716 }
1717 if (sync_obj == bo->sync_obj ||
1718 sync_obj == bo->sync_obj_write) {
1719 tmp_obj_write = bo->sync_obj_write;
1720 bo->sync_obj_write = NULL;
1721 }
1722 if (sync_obj == bo->sync_obj) {
1723 tmp_obj = bo->sync_obj;
1724 bo->sync_obj = NULL;
1725 }
1726
1727 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1728 spin_unlock(&bdev->fence_lock);
1729 if (tmp_obj)
1730 driver->sync_obj_unref(&tmp_obj);
1731 if (tmp_obj_read)
1732 driver->sync_obj_unref(&tmp_obj_read);
1733 if (tmp_obj_write)
1734 driver->sync_obj_unref(&tmp_obj_write);
1735 if (extra_sync_obj)
1736 driver->sync_obj_unref(extra_sync_obj);
1737 spin_lock(&bdev->fence_lock);
1738}
1739
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001740int ttm_bo_wait(struct ttm_buffer_object *bo,
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001741 bool lazy, bool interruptible, bool no_wait,
1742 enum ttm_buffer_usage usage)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001743{
1744 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001745 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001746 void *sync_obj;
1747 void *sync_obj_arg;
1748 int ret = 0;
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001749 void **bo_sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001750
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001751 switch (usage) {
1752 case TTM_USAGE_READ:
1753 bo_sync_obj = &bo->sync_obj_read;
1754 break;
1755 case TTM_USAGE_WRITE:
1756 bo_sync_obj = &bo->sync_obj_write;
1757 break;
1758 case TTM_USAGE_READWRITE:
1759 default:
1760 bo_sync_obj = &bo->sync_obj;
1761 }
1762
1763 if (likely(*bo_sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001764 return 0;
1765
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001766 while (*bo_sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001767
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001768 if (driver->sync_obj_signaled(*bo_sync_obj, bo->sync_obj_arg)) {
1769 ttm_bo_unref_sync_obj_locked(bo, *bo_sync_obj, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001770 continue;
1771 }
1772
1773 if (no_wait)
1774 return -EBUSY;
1775
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001776 sync_obj = driver->sync_obj_ref(*bo_sync_obj);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001777 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001778 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001779 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1780 lazy, interruptible);
1781 if (unlikely(ret != 0)) {
1782 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001783 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001784 return ret;
1785 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001786 spin_lock(&bdev->fence_lock);
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001787 if (likely(*bo_sync_obj == sync_obj &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001788 bo->sync_obj_arg == sync_obj_arg)) {
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001789 ttm_bo_unref_sync_obj_locked(bo, *bo_sync_obj, &sync_obj);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001790 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001791 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001792 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001793 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001794 }
1795 }
1796 return 0;
1797}
1798EXPORT_SYMBOL(ttm_bo_wait);
1799
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001800int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1801{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001802 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001803 int ret = 0;
1804
1805 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001806 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001807 */
1808
1809 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1810 if (unlikely(ret != 0))
1811 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001812 spin_lock(&bdev->fence_lock);
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001813 ret = ttm_bo_wait(bo, false, true, no_wait, TTM_USAGE_READWRITE);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001814 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001815 if (likely(ret == 0))
1816 atomic_inc(&bo->cpu_writers);
1817 ttm_bo_unreserve(bo);
1818 return ret;
1819}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001820EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001821
1822void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1823{
1824 if (atomic_dec_and_test(&bo->cpu_writers))
1825 wake_up_all(&bo->event_queue);
1826}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001827EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001828
1829/**
1830 * A buffer object shrink method that tries to swap out the first
1831 * buffer object on the bo_global::swap_lru list.
1832 */
1833
1834static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1835{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001836 struct ttm_bo_global *glob =
1837 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001838 struct ttm_buffer_object *bo;
1839 int ret = -EBUSY;
1840 int put_count;
1841 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1842
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001843 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001844 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001845 if (unlikely(list_empty(&glob->swap_lru))) {
1846 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001847 return -EBUSY;
1848 }
1849
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001850 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001851 struct ttm_buffer_object, swap);
1852 kref_get(&bo->list_kref);
1853
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001854 if (!list_empty(&bo->ddestroy)) {
1855 spin_unlock(&glob->lru_lock);
1856 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1857 kref_put(&bo->list_kref, ttm_bo_release_list);
1858 continue;
1859 }
1860
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001861 /**
1862 * Reserve buffer. Since we unlock while sleeping, we need
1863 * to re-check that nobody removed us from the swap-list while
1864 * we slept.
1865 */
1866
1867 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1868 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001869 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001870 ttm_bo_wait_unreserved(bo, false);
1871 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001872 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001873 }
1874 }
1875
1876 BUG_ON(ret != 0);
1877 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001878 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001879
Dave Airlied6ea8882010-11-22 13:24:40 +10001880 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001881
1882 /**
1883 * Wait for GPU, then move to system cached.
1884 */
1885
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001886 spin_lock(&bo->bdev->fence_lock);
Marek Olšákdfadbbd2011-08-13 20:32:11 +00001887 ret = ttm_bo_wait(bo, false, false, false, TTM_USAGE_READWRITE);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001888 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001889
1890 if (unlikely(ret != 0))
1891 goto out;
1892
1893 if ((bo->mem.placement & swap_placement) != swap_placement) {
1894 struct ttm_mem_reg evict_mem;
1895
1896 evict_mem = bo->mem;
1897 evict_mem.mm_node = NULL;
1898 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1899 evict_mem.mem_type = TTM_PL_SYSTEM;
1900
1901 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001902 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001903 if (unlikely(ret != 0))
1904 goto out;
1905 }
1906
1907 ttm_bo_unmap_virtual(bo);
1908
1909 /**
1910 * Swap out. Buffer will be swapped in again as soon as
1911 * anyone tries to access a ttm page.
1912 */
1913
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001914 if (bo->bdev->driver->swap_notify)
1915 bo->bdev->driver->swap_notify(bo);
1916
Jan Engelhardt5df23972011-04-04 01:25:18 +02001917 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001918out:
1919
1920 /**
1921 *
1922 * Unreserve without putting on LRU to avoid swapping out an
1923 * already swapped buffer.
1924 */
1925
1926 atomic_set(&bo->reserved, 0);
1927 wake_up_all(&bo->event_queue);
1928 kref_put(&bo->list_kref, ttm_bo_release_list);
1929 return ret;
1930}
1931
1932void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1933{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001934 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001935 ;
1936}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001937EXPORT_SYMBOL(ttm_bo_swapout_all);