blob: 0bb0f5f713e6cf9166a1b91eb19ab81b85206716 [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 Sharma60063492011-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);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200502 (void) ttm_bo_wait(bo, false, false, true);
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);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200570 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000571 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200572
573 if (unlikely(ret != 0))
574 return ret;
575
576 spin_lock(&glob->lru_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100577
578 if (unlikely(list_empty(&bo->ddestroy))) {
579 spin_unlock(&glob->lru_lock);
580 return 0;
581 }
582
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200583 ret = ttm_bo_reserve_locked(bo, interruptible,
584 no_wait_reserve, false, 0);
585
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100586 if (unlikely(ret != 0)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200587 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200588 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200589 }
590
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200591 /**
592 * We can re-check for sync object without taking
593 * the bo::lock since setting the sync object requires
594 * also bo::reserved. A busy object at this point may
595 * be caused by another thread recently starting an accelerated
596 * eviction.
597 */
598
599 if (unlikely(bo->sync_obj)) {
600 atomic_set(&bo->reserved, 0);
601 wake_up_all(&bo->event_queue);
602 spin_unlock(&glob->lru_lock);
603 goto retry;
604 }
605
606 put_count = ttm_bo_del_from_lru(bo);
607 list_del_init(&bo->ddestroy);
608 ++put_count;
609
610 spin_unlock(&glob->lru_lock);
611 ttm_bo_cleanup_memtype_use(bo);
612
Dave Airlied6ea8882010-11-22 13:24:40 +1000613 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200614
615 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200616}
617
618/**
619 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
620 * encountered buffers.
621 */
622
623static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
624{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200625 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100626 struct ttm_buffer_object *entry = NULL;
627 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200628
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200629 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100630 if (list_empty(&bdev->ddestroy))
631 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200632
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100633 entry = list_first_entry(&bdev->ddestroy,
634 struct ttm_buffer_object, ddestroy);
635 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200636
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100637 for (;;) {
638 struct ttm_buffer_object *nentry = NULL;
639
640 if (entry->ddestroy.next != &bdev->ddestroy) {
641 nentry = list_first_entry(&entry->ddestroy,
642 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200643 kref_get(&nentry->list_kref);
644 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200645
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200646 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200647 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
648 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200649 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100650 entry = nentry;
651
652 if (ret || !entry)
653 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200654
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200655 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100656 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200657 break;
658 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200659
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100660out_unlock:
661 spin_unlock(&glob->lru_lock);
662out:
663 if (entry)
664 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200665 return ret;
666}
667
668static void ttm_bo_delayed_workqueue(struct work_struct *work)
669{
670 struct ttm_bo_device *bdev =
671 container_of(work, struct ttm_bo_device, wq.work);
672
673 if (ttm_bo_delayed_delete(bdev, false)) {
674 schedule_delayed_work(&bdev->wq,
675 ((HZ / 100) < 1) ? 1 : HZ / 100);
676 }
677}
678
679static void ttm_bo_release(struct kref *kref)
680{
681 struct ttm_buffer_object *bo =
682 container_of(kref, struct ttm_buffer_object, kref);
683 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100684 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200685
686 if (likely(bo->vm_node != NULL)) {
687 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
688 drm_mm_put_block(bo->vm_node);
689 bo->vm_node = NULL;
690 }
691 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100692 ttm_mem_io_lock(man, false);
693 ttm_mem_io_free_vm(bo);
694 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200695 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200696 kref_put(&bo->list_kref, ttm_bo_release_list);
697 write_lock(&bdev->vm_lock);
698}
699
700void ttm_bo_unref(struct ttm_buffer_object **p_bo)
701{
702 struct ttm_buffer_object *bo = *p_bo;
703 struct ttm_bo_device *bdev = bo->bdev;
704
705 *p_bo = NULL;
706 write_lock(&bdev->vm_lock);
707 kref_put(&bo->kref, ttm_bo_release);
708 write_unlock(&bdev->vm_lock);
709}
710EXPORT_SYMBOL(ttm_bo_unref);
711
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400712int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
713{
714 return cancel_delayed_work_sync(&bdev->wq);
715}
716EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
717
718void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
719{
720 if (resched)
721 schedule_delayed_work(&bdev->wq,
722 ((HZ / 100) < 1) ? 1 : HZ / 100);
723}
724EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
725
Jerome Glisseca262a9992009-12-08 15:33:32 +0100726static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000727 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200728{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200729 struct ttm_bo_device *bdev = bo->bdev;
730 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100731 struct ttm_placement placement;
732 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200733
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000734 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200735 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000736 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200737
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200738 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100739 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200740 printk(KERN_ERR TTM_PFX
741 "Failed to expire sync object before "
742 "buffer eviction.\n");
743 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200744 goto out;
745 }
746
747 BUG_ON(!atomic_read(&bo->reserved));
748
749 evict_mem = bo->mem;
750 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100751 evict_mem.bus.io_reserved_vm = false;
752 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200753
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100754 placement.fpfn = 0;
755 placement.lpfn = 0;
756 placement.num_placement = 0;
757 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100758 bdev->driver->evict_flags(bo, &placement);
759 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000760 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200761 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100762 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200763 printk(KERN_ERR TTM_PFX
764 "Failed to find memory space for "
765 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100766 ttm_bo_mem_space_debug(bo, &placement);
767 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200768 goto out;
769 }
770
771 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000772 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200773 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100774 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200775 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000776 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200777 goto out;
778 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200779 bo->evicted = true;
780out:
781 return ret;
782}
783
Jerome Glisseca262a9992009-12-08 15:33:32 +0100784static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
785 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000786 bool interruptible, bool no_wait_reserve,
787 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100788{
789 struct ttm_bo_global *glob = bdev->glob;
790 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
791 struct ttm_buffer_object *bo;
792 int ret, put_count = 0;
793
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100794retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100795 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100796 if (list_empty(&man->lru)) {
797 spin_unlock(&glob->lru_lock);
798 return -EBUSY;
799 }
800
Jerome Glisseca262a9992009-12-08 15:33:32 +0100801 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
802 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100803
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200804 if (!list_empty(&bo->ddestroy)) {
805 spin_unlock(&glob->lru_lock);
806 ret = ttm_bo_cleanup_refs(bo, interruptible,
807 no_wait_reserve, no_wait_gpu);
808 kref_put(&bo->list_kref, ttm_bo_release_list);
809
810 if (likely(ret == 0 || ret == -ERESTARTSYS))
811 return ret;
812
813 goto retry;
814 }
815
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000816 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100817
818 if (unlikely(ret == -EBUSY)) {
819 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000820 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100821 ret = ttm_bo_wait_unreserved(bo, interruptible);
822
823 kref_put(&bo->list_kref, ttm_bo_release_list);
824
825 /**
826 * We *need* to retry after releasing the lru lock.
827 */
828
829 if (unlikely(ret != 0))
830 return ret;
831 goto retry;
832 }
833
834 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100835 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100836
837 BUG_ON(ret != 0);
838
Dave Airlied6ea8882010-11-22 13:24:40 +1000839 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100840
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000841 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100842 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100843
Jerome Glisseca262a9992009-12-08 15:33:32 +0100844 kref_put(&bo->list_kref, ttm_bo_release_list);
845 return ret;
846}
847
Ben Skeggs42311ff2010-08-04 12:07:08 +1000848void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
849{
Ben Skeggsd961db72010-08-05 10:48:18 +1000850 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000851
Ben Skeggsd961db72010-08-05 10:48:18 +1000852 if (mem->mm_node)
853 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000854}
855EXPORT_SYMBOL(ttm_bo_mem_put);
856
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200857/**
858 * Repeatedly evict memory from the LRU for @mem_type until we create enough
859 * space, or we've evicted everything and there isn't enough space.
860 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100861static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
862 uint32_t mem_type,
863 struct ttm_placement *placement,
864 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000865 bool interruptible,
866 bool no_wait_reserve,
867 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200868{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100869 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200870 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200871 int ret;
872
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200873 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000874 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200875 if (unlikely(ret != 0))
876 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000877 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100878 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100879 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000880 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100881 if (unlikely(ret != 0))
882 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200883 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000884 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200885 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200886 mem->mem_type = mem_type;
887 return 0;
888}
889
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200890static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
891 uint32_t cur_placement,
892 uint32_t proposed_placement)
893{
894 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
895 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
896
897 /**
898 * Keep current caching if possible.
899 */
900
901 if ((cur_placement & caching) != 0)
902 result |= (cur_placement & caching);
903 else if ((man->default_caching & caching) != 0)
904 result |= man->default_caching;
905 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
906 result |= TTM_PL_FLAG_CACHED;
907 else if ((TTM_PL_FLAG_WC & caching) != 0)
908 result |= TTM_PL_FLAG_WC;
909 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
910 result |= TTM_PL_FLAG_UNCACHED;
911
912 return result;
913}
914
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200915static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
916 bool disallow_fixed,
917 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200918 uint32_t proposed_placement,
919 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200920{
921 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
922
923 if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
924 return false;
925
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200926 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200927 return false;
928
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200929 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200930 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200931
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200932 cur_flags |= (proposed_placement & man->available_caching);
933
934 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200935 return true;
936}
937
938/**
939 * Creates space for memory region @mem according to its type.
940 *
941 * This function first searches for free space in compatible memory types in
942 * the priority order defined by the driver. If free space isn't found, then
943 * ttm_bo_mem_force_space is attempted in priority order to evict and find
944 * space.
945 */
946int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100947 struct ttm_placement *placement,
948 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000949 bool interruptible, bool no_wait_reserve,
950 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951{
952 struct ttm_bo_device *bdev = bo->bdev;
953 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200954 uint32_t mem_type = TTM_PL_SYSTEM;
955 uint32_t cur_flags = 0;
956 bool type_found = false;
957 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100958 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100959 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200960
961 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000962 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100963 ret = ttm_mem_type_from_flags(placement->placement[i],
964 &mem_type);
965 if (ret)
966 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200967 man = &bdev->man[mem_type];
968
969 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100970 bo->type == ttm_bo_type_user,
971 mem_type,
972 placement->placement[i],
973 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200974
975 if (!type_ok)
976 continue;
977
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200978 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
979 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100980 /*
981 * Use the access and other non-mapping-related flag bits from
982 * the memory placement flags to the current flags
983 */
984 ttm_flag_masked(&cur_flags, placement->placement[i],
985 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200986
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200987 if (mem_type == TTM_PL_SYSTEM)
988 break;
989
990 if (man->has_type && man->use_type) {
991 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000992 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100993 if (unlikely(ret))
994 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200995 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000996 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200997 break;
998 }
999
Ben Skeggsd961db72010-08-05 10:48:18 +10001000 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001001 mem->mem_type = mem_type;
1002 mem->placement = cur_flags;
1003 return 0;
1004 }
1005
1006 if (!type_found)
1007 return -EINVAL;
1008
Dave Airlieb6637522009-12-14 14:51:35 +10001009 for (i = 0; i < placement->num_busy_placement; ++i) {
1010 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001011 &mem_type);
1012 if (ret)
1013 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001014 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 if (!man->has_type)
1016 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001017 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001018 bo->type == ttm_bo_type_user,
1019 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001020 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001021 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001022 continue;
1023
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001024 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1025 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001026 /*
1027 * Use the access and other non-mapping-related flag bits from
1028 * the memory placement flags to the current flags
1029 */
Dave Airlieb6637522009-12-14 14:51:35 +10001030 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001031 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001032
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001033
1034 if (mem_type == TTM_PL_SYSTEM) {
1035 mem->mem_type = mem_type;
1036 mem->placement = cur_flags;
1037 mem->mm_node = NULL;
1038 return 0;
1039 }
1040
Jerome Glisseca262a9992009-12-08 15:33:32 +01001041 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001042 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001043 if (ret == 0 && mem->mm_node) {
1044 mem->placement = cur_flags;
1045 return 0;
1046 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001047 if (ret == -ERESTARTSYS)
1048 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001050 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001051 return ret;
1052}
1053EXPORT_SYMBOL(ttm_bo_mem_space);
1054
1055int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1056{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001057 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1058 return -EBUSY;
1059
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001060 return wait_event_interruptible(bo->event_queue,
1061 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001062}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001063EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001064
1065int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001066 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001067 bool interruptible, bool no_wait_reserve,
1068 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001069{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001070 int ret = 0;
1071 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001072 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001073
1074 BUG_ON(!atomic_read(&bo->reserved));
1075
1076 /*
1077 * FIXME: It's possible to pipeline buffer moves.
1078 * Have the driver move function wait for idle when necessary,
1079 * instead of doing it here.
1080 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001081 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001082 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001083 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001084 if (ret)
1085 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086 mem.num_pages = bo->num_pages;
1087 mem.size = mem.num_pages << PAGE_SHIFT;
1088 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001089 mem.bus.io_reserved_vm = false;
1090 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001091 /*
1092 * Determine where to move the buffer.
1093 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001094 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001095 if (ret)
1096 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001097 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001099 if (ret && mem.mm_node)
1100 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101 return ret;
1102}
1103
Jerome Glisseca262a9992009-12-08 15:33:32 +01001104static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001105 struct ttm_mem_reg *mem)
1106{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001107 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001108
Ben Skeggsd961db72010-08-05 10:48:18 +10001109 if (mem->mm_node && placement->lpfn != 0 &&
1110 (mem->start < placement->fpfn ||
1111 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001112 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113
Jerome Glisseca262a9992009-12-08 15:33:32 +01001114 for (i = 0; i < placement->num_placement; i++) {
1115 if ((placement->placement[i] & mem->placement &
1116 TTM_PL_MASK_CACHING) &&
1117 (placement->placement[i] & mem->placement &
1118 TTM_PL_MASK_MEM))
1119 return i;
1120 }
1121 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001122}
1123
Jerome Glisse09855ac2009-12-10 17:16:27 +01001124int ttm_bo_validate(struct ttm_buffer_object *bo,
1125 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001126 bool interruptible, bool no_wait_reserve,
1127 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001128{
1129 int ret;
1130
1131 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001132 /* Check that range is valid */
1133 if (placement->lpfn || placement->fpfn)
1134 if (placement->fpfn > placement->lpfn ||
1135 (placement->lpfn - placement->fpfn) < bo->num_pages)
1136 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001137 /*
1138 * Check whether we need to move buffer.
1139 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001140 ret = ttm_bo_mem_compat(placement, &bo->mem);
1141 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001142 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001143 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001145 } else {
1146 /*
1147 * Use the access and other non-mapping-related flag bits from
1148 * the compatible memory placement flags to the active flags
1149 */
1150 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1151 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 /*
1154 * We might need to add a TTM.
1155 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001156 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1157 ret = ttm_bo_add_ttm(bo, true);
1158 if (ret)
1159 return ret;
1160 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161 return 0;
1162}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001163EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001164
Jerome Glisse09855ac2009-12-10 17:16:27 +01001165int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1166 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001167{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001168 BUG_ON((placement->fpfn || placement->lpfn) &&
1169 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001170
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001171 return 0;
1172}
1173
Jerome Glisse09855ac2009-12-10 17:16:27 +01001174int ttm_bo_init(struct ttm_bo_device *bdev,
1175 struct ttm_buffer_object *bo,
1176 unsigned long size,
1177 enum ttm_bo_type type,
1178 struct ttm_placement *placement,
1179 uint32_t page_alignment,
1180 unsigned long buffer_start,
1181 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001182 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001183 size_t acc_size,
1184 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001186 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187 unsigned long num_pages;
1188
1189 size += buffer_start & ~PAGE_MASK;
1190 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1191 if (num_pages == 0) {
1192 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001193 if (destroy)
1194 (*destroy)(bo);
1195 else
1196 kfree(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001197 return -EINVAL;
1198 }
1199 bo->destroy = destroy;
1200
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001201 kref_init(&bo->kref);
1202 kref_init(&bo->list_kref);
1203 atomic_set(&bo->cpu_writers, 0);
1204 atomic_set(&bo->reserved, 1);
1205 init_waitqueue_head(&bo->event_queue);
1206 INIT_LIST_HEAD(&bo->lru);
1207 INIT_LIST_HEAD(&bo->ddestroy);
1208 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001209 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001210 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001211 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212 bo->type = type;
1213 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001214 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215 bo->mem.mem_type = TTM_PL_SYSTEM;
1216 bo->mem.num_pages = bo->num_pages;
1217 bo->mem.mm_node = NULL;
1218 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001219 bo->mem.bus.io_reserved_vm = false;
1220 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221 bo->buffer_start = buffer_start & PAGE_MASK;
1222 bo->priv_flags = 0;
1223 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1224 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001225 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001226 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001227 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228
Jerome Glisse09855ac2009-12-10 17:16:27 +01001229 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001230 if (unlikely(ret != 0))
1231 goto out_err;
1232
1233 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001234 * For ttm_bo_type_device buffers, allocate
1235 * address space from the device.
1236 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001237 if (bo->type == ttm_bo_type_device) {
1238 ret = ttm_bo_setup_vm(bo);
1239 if (ret)
1240 goto out_err;
1241 }
1242
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001243 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001244 if (ret)
1245 goto out_err;
1246
1247 ttm_bo_unreserve(bo);
1248 return 0;
1249
1250out_err:
1251 ttm_bo_unreserve(bo);
1252 ttm_bo_unref(&bo);
1253
1254 return ret;
1255}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001256EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001257
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001258static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001259 unsigned long num_pages)
1260{
1261 size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
1262 PAGE_MASK;
1263
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001264 return glob->ttm_bo_size + 2 * page_array_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001265}
1266
Jerome Glisse09855ac2009-12-10 17:16:27 +01001267int ttm_bo_create(struct ttm_bo_device *bdev,
1268 unsigned long size,
1269 enum ttm_bo_type type,
1270 struct ttm_placement *placement,
1271 uint32_t page_alignment,
1272 unsigned long buffer_start,
1273 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001274 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001275 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001276{
1277 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001278 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001279 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001280
1281 size_t acc_size =
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001282 ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001283 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001284 if (unlikely(ret != 0))
1285 return ret;
1286
1287 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1288
1289 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001290 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291 return -ENOMEM;
1292 }
1293
Jerome Glisse09855ac2009-12-10 17:16:27 +01001294 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1295 buffer_start, interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001296 persistent_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001297 if (likely(ret == 0))
1298 *p_bo = bo;
1299
1300 return ret;
1301}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001302EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001303
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001304static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001305 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001306{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001307 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001308 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001309 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001310
1311 /*
1312 * Can't use standard list traversal since we're unlocking.
1313 */
1314
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001315 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001316 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001317 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001318 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001319 if (ret) {
1320 if (allow_errors) {
1321 return ret;
1322 } else {
1323 printk(KERN_ERR TTM_PFX
1324 "Cleanup eviction failed\n");
1325 }
1326 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001327 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001328 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001329 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001330 return 0;
1331}
1332
1333int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1334{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001335 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001336 int ret = -EINVAL;
1337
1338 if (mem_type >= TTM_NUM_MEM_TYPES) {
1339 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1340 return ret;
1341 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001342 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001343
1344 if (!man->has_type) {
1345 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1346 "memory manager type %u\n", mem_type);
1347 return ret;
1348 }
1349
1350 man->use_type = false;
1351 man->has_type = false;
1352
1353 ret = 0;
1354 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001355 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001356
Ben Skeggsd961db72010-08-05 10:48:18 +10001357 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001358 }
1359
1360 return ret;
1361}
1362EXPORT_SYMBOL(ttm_bo_clean_mm);
1363
1364int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1365{
1366 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1367
1368 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1369 printk(KERN_ERR TTM_PFX
1370 "Illegal memory manager memory type %u.\n",
1371 mem_type);
1372 return -EINVAL;
1373 }
1374
1375 if (!man->has_type) {
1376 printk(KERN_ERR TTM_PFX
1377 "Memory type %u has not been initialized.\n",
1378 mem_type);
1379 return 0;
1380 }
1381
Jerome Glisseca262a9992009-12-08 15:33:32 +01001382 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001383}
1384EXPORT_SYMBOL(ttm_bo_evict_mm);
1385
1386int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001387 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001388{
1389 int ret = -EINVAL;
1390 struct ttm_mem_type_manager *man;
1391
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001392 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001393 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001394 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001395 man->io_reserve_fastpath = true;
1396 man->use_io_reserve_lru = false;
1397 mutex_init(&man->io_reserve_mutex);
1398 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001399
1400 ret = bdev->driver->init_mem_type(bdev, type, man);
1401 if (ret)
1402 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001403 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001404
1405 ret = 0;
1406 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001407 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001408 if (ret)
1409 return ret;
1410 }
1411 man->has_type = true;
1412 man->use_type = true;
1413 man->size = p_size;
1414
1415 INIT_LIST_HEAD(&man->lru);
1416
1417 return 0;
1418}
1419EXPORT_SYMBOL(ttm_bo_init_mm);
1420
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001421static void ttm_bo_global_kobj_release(struct kobject *kobj)
1422{
1423 struct ttm_bo_global *glob =
1424 container_of(kobj, struct ttm_bo_global, kobj);
1425
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001426 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1427 __free_page(glob->dummy_read_page);
1428 kfree(glob);
1429}
1430
Dave Airlieba4420c2010-03-09 10:56:52 +10001431void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001432{
1433 struct ttm_bo_global *glob = ref->object;
1434
1435 kobject_del(&glob->kobj);
1436 kobject_put(&glob->kobj);
1437}
1438EXPORT_SYMBOL(ttm_bo_global_release);
1439
Dave Airlieba4420c2010-03-09 10:56:52 +10001440int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001441{
1442 struct ttm_bo_global_ref *bo_ref =
1443 container_of(ref, struct ttm_bo_global_ref, ref);
1444 struct ttm_bo_global *glob = ref->object;
1445 int ret;
1446
1447 mutex_init(&glob->device_list_mutex);
1448 spin_lock_init(&glob->lru_lock);
1449 glob->mem_glob = bo_ref->mem_glob;
1450 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1451
1452 if (unlikely(glob->dummy_read_page == NULL)) {
1453 ret = -ENOMEM;
1454 goto out_no_drp;
1455 }
1456
1457 INIT_LIST_HEAD(&glob->swap_lru);
1458 INIT_LIST_HEAD(&glob->device_list);
1459
1460 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1461 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1462 if (unlikely(ret != 0)) {
1463 printk(KERN_ERR TTM_PFX
1464 "Could not register buffer object swapout.\n");
1465 goto out_no_shrink;
1466 }
1467
1468 glob->ttm_bo_extra_size =
1469 ttm_round_pot(sizeof(struct ttm_tt)) +
1470 ttm_round_pot(sizeof(struct ttm_backend));
1471
1472 glob->ttm_bo_size = glob->ttm_bo_extra_size +
1473 ttm_round_pot(sizeof(struct ttm_buffer_object));
1474
1475 atomic_set(&glob->bo_count, 0);
1476
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001477 ret = kobject_init_and_add(
1478 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001479 if (unlikely(ret != 0))
1480 kobject_put(&glob->kobj);
1481 return ret;
1482out_no_shrink:
1483 __free_page(glob->dummy_read_page);
1484out_no_drp:
1485 kfree(glob);
1486 return ret;
1487}
1488EXPORT_SYMBOL(ttm_bo_global_init);
1489
1490
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001491int ttm_bo_device_release(struct ttm_bo_device *bdev)
1492{
1493 int ret = 0;
1494 unsigned i = TTM_NUM_MEM_TYPES;
1495 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001496 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001497
1498 while (i--) {
1499 man = &bdev->man[i];
1500 if (man->has_type) {
1501 man->use_type = false;
1502 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1503 ret = -EBUSY;
1504 printk(KERN_ERR TTM_PFX
1505 "DRM memory manager type %d "
1506 "is not clean.\n", i);
1507 }
1508 man->has_type = false;
1509 }
1510 }
1511
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001512 mutex_lock(&glob->device_list_mutex);
1513 list_del(&bdev->device_list);
1514 mutex_unlock(&glob->device_list_mutex);
1515
Tejun Heof094cfc2010-12-24 15:59:06 +01001516 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001517
1518 while (ttm_bo_delayed_delete(bdev, true))
1519 ;
1520
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001521 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001522 if (list_empty(&bdev->ddestroy))
1523 TTM_DEBUG("Delayed destroy list was clean\n");
1524
1525 if (list_empty(&bdev->man[0].lru))
1526 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001527 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001528
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001529 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1530 write_lock(&bdev->vm_lock);
1531 drm_mm_takedown(&bdev->addr_space_mm);
1532 write_unlock(&bdev->vm_lock);
1533
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534 return ret;
1535}
1536EXPORT_SYMBOL(ttm_bo_device_release);
1537
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001539 struct ttm_bo_global *glob,
1540 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001541 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001542 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001543{
1544 int ret = -EINVAL;
1545
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001546 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001547 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001548
1549 memset(bdev->man, 0, sizeof(bdev->man));
1550
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001551 /*
1552 * Initialize the system memory buffer type.
1553 * Other types need to be driver / IOCTL initialized.
1554 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001555 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001556 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001557 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001558
1559 bdev->addr_space_rb = RB_ROOT;
1560 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1561 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001562 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001563
1564 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1565 bdev->nice_mode = true;
1566 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001567 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001568 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001569 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001570 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001571 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001572 mutex_lock(&glob->device_list_mutex);
1573 list_add_tail(&bdev->device_list, &glob->device_list);
1574 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575
1576 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001577out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001578 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001579out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001580 return ret;
1581}
1582EXPORT_SYMBOL(ttm_bo_device_init);
1583
1584/*
1585 * buffer object vm functions.
1586 */
1587
1588bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1589{
1590 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1591
1592 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1593 if (mem->mem_type == TTM_PL_SYSTEM)
1594 return false;
1595
1596 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1597 return false;
1598
1599 if (mem->placement & TTM_PL_FLAG_CACHED)
1600 return false;
1601 }
1602 return true;
1603}
1604
Thomas Hellstromeba67092010-11-11 09:41:57 +01001605void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001606{
1607 struct ttm_bo_device *bdev = bo->bdev;
1608 loff_t offset = (loff_t) bo->addr_space_offset;
1609 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1610
1611 if (!bdev->dev_mapping)
1612 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001613 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001614 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001615}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001616
1617void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1618{
1619 struct ttm_bo_device *bdev = bo->bdev;
1620 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1621
1622 ttm_mem_io_lock(man, false);
1623 ttm_bo_unmap_virtual_locked(bo);
1624 ttm_mem_io_unlock(man);
1625}
1626
1627
Dave Airliee024e112009-06-24 09:48:08 +10001628EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001629
1630static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1631{
1632 struct ttm_bo_device *bdev = bo->bdev;
1633 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1634 struct rb_node *parent = NULL;
1635 struct ttm_buffer_object *cur_bo;
1636 unsigned long offset = bo->vm_node->start;
1637 unsigned long cur_offset;
1638
1639 while (*cur) {
1640 parent = *cur;
1641 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1642 cur_offset = cur_bo->vm_node->start;
1643 if (offset < cur_offset)
1644 cur = &parent->rb_left;
1645 else if (offset > cur_offset)
1646 cur = &parent->rb_right;
1647 else
1648 BUG();
1649 }
1650
1651 rb_link_node(&bo->vm_rb, parent, cur);
1652 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1653}
1654
1655/**
1656 * ttm_bo_setup_vm:
1657 *
1658 * @bo: the buffer to allocate address space for
1659 *
1660 * Allocate address space in the drm device so that applications
1661 * can mmap the buffer and access the contents. This only
1662 * applies to ttm_bo_type_device objects as others are not
1663 * placed in the drm device address space.
1664 */
1665
1666static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1667{
1668 struct ttm_bo_device *bdev = bo->bdev;
1669 int ret;
1670
1671retry_pre_get:
1672 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1673 if (unlikely(ret != 0))
1674 return ret;
1675
1676 write_lock(&bdev->vm_lock);
1677 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1678 bo->mem.num_pages, 0, 0);
1679
1680 if (unlikely(bo->vm_node == NULL)) {
1681 ret = -ENOMEM;
1682 goto out_unlock;
1683 }
1684
1685 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1686 bo->mem.num_pages, 0);
1687
1688 if (unlikely(bo->vm_node == NULL)) {
1689 write_unlock(&bdev->vm_lock);
1690 goto retry_pre_get;
1691 }
1692
1693 ttm_bo_vm_insert_rb(bo);
1694 write_unlock(&bdev->vm_lock);
1695 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1696
1697 return 0;
1698out_unlock:
1699 write_unlock(&bdev->vm_lock);
1700 return ret;
1701}
1702
1703int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001704 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001705{
1706 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001707 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001708 void *sync_obj;
1709 void *sync_obj_arg;
1710 int ret = 0;
1711
Dave Airlie1717c0e2011-10-27 18:28:37 +02001712 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001713 return 0;
1714
Dave Airlie1717c0e2011-10-27 18:28:37 +02001715 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001716
Dave Airlie1717c0e2011-10-27 18:28:37 +02001717 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1718 void *tmp_obj = bo->sync_obj;
1719 bo->sync_obj = NULL;
1720 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1721 spin_unlock(&bdev->fence_lock);
1722 driver->sync_obj_unref(&tmp_obj);
1723 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001724 continue;
1725 }
1726
1727 if (no_wait)
1728 return -EBUSY;
1729
Dave Airlie1717c0e2011-10-27 18:28:37 +02001730 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001731 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001732 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001733 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1734 lazy, interruptible);
1735 if (unlikely(ret != 0)) {
1736 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001737 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001738 return ret;
1739 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001740 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001741 if (likely(bo->sync_obj == sync_obj &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001742 bo->sync_obj_arg == sync_obj_arg)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001743 void *tmp_obj = bo->sync_obj;
1744 bo->sync_obj = NULL;
1745 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1746 &bo->priv_flags);
1747 spin_unlock(&bdev->fence_lock);
1748 driver->sync_obj_unref(&sync_obj);
1749 driver->sync_obj_unref(&tmp_obj);
1750 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001751 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001752 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001753 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001754 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001755 }
1756 }
1757 return 0;
1758}
1759EXPORT_SYMBOL(ttm_bo_wait);
1760
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001761int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1762{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001763 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001764 int ret = 0;
1765
1766 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001767 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001768 */
1769
1770 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1771 if (unlikely(ret != 0))
1772 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001773 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001774 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001775 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001776 if (likely(ret == 0))
1777 atomic_inc(&bo->cpu_writers);
1778 ttm_bo_unreserve(bo);
1779 return ret;
1780}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001781EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001782
1783void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1784{
1785 if (atomic_dec_and_test(&bo->cpu_writers))
1786 wake_up_all(&bo->event_queue);
1787}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001788EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001789
1790/**
1791 * A buffer object shrink method that tries to swap out the first
1792 * buffer object on the bo_global::swap_lru list.
1793 */
1794
1795static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1796{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001797 struct ttm_bo_global *glob =
1798 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001799 struct ttm_buffer_object *bo;
1800 int ret = -EBUSY;
1801 int put_count;
1802 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1803
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001804 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001805 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001806 if (unlikely(list_empty(&glob->swap_lru))) {
1807 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001808 return -EBUSY;
1809 }
1810
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001811 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001812 struct ttm_buffer_object, swap);
1813 kref_get(&bo->list_kref);
1814
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001815 if (!list_empty(&bo->ddestroy)) {
1816 spin_unlock(&glob->lru_lock);
1817 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1818 kref_put(&bo->list_kref, ttm_bo_release_list);
1819 continue;
1820 }
1821
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001822 /**
1823 * Reserve buffer. Since we unlock while sleeping, we need
1824 * to re-check that nobody removed us from the swap-list while
1825 * we slept.
1826 */
1827
1828 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1829 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001830 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001831 ttm_bo_wait_unreserved(bo, false);
1832 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001833 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001834 }
1835 }
1836
1837 BUG_ON(ret != 0);
1838 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001839 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001840
Dave Airlied6ea8882010-11-22 13:24:40 +10001841 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001842
1843 /**
1844 * Wait for GPU, then move to system cached.
1845 */
1846
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001847 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001848 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001849 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001850
1851 if (unlikely(ret != 0))
1852 goto out;
1853
1854 if ((bo->mem.placement & swap_placement) != swap_placement) {
1855 struct ttm_mem_reg evict_mem;
1856
1857 evict_mem = bo->mem;
1858 evict_mem.mm_node = NULL;
1859 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1860 evict_mem.mem_type = TTM_PL_SYSTEM;
1861
1862 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001863 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001864 if (unlikely(ret != 0))
1865 goto out;
1866 }
1867
1868 ttm_bo_unmap_virtual(bo);
1869
1870 /**
1871 * Swap out. Buffer will be swapped in again as soon as
1872 * anyone tries to access a ttm page.
1873 */
1874
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001875 if (bo->bdev->driver->swap_notify)
1876 bo->bdev->driver->swap_notify(bo);
1877
Jan Engelhardt5df23972011-04-04 01:25:18 +02001878 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001879out:
1880
1881 /**
1882 *
1883 * Unreserve without putting on LRU to avoid swapping out an
1884 * already swapped buffer.
1885 */
1886
1887 atomic_set(&bo->reserved, 0);
1888 wake_up_all(&bo->event_queue);
1889 kref_put(&bo->list_kref, ttm_bo_release_list);
1890 return ret;
1891}
1892
1893void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1894{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001895 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001896 ;
1897}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001898EXPORT_SYMBOL(ttm_bo_swapout_all);