blob: de7ad9991902263c487115fca48e978091c5fd75 [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;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500140 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200141
142 BUG_ON(atomic_read(&bo->list_kref.refcount));
143 BUG_ON(atomic_read(&bo->kref.refcount));
144 BUG_ON(atomic_read(&bo->cpu_writers));
145 BUG_ON(bo->sync_obj != NULL);
146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
149
150 if (bo->ttm)
151 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200152 atomic_dec(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200153 if (bo->destroy)
154 bo->destroy(bo);
155 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 kfree(bo);
157 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500158 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200159}
160
161int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
162{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200163 if (interruptible) {
Jean Delvare965d3802010-10-09 12:36:45 +0000164 return wait_event_interruptible(bo->event_queue,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165 atomic_read(&bo->reserved) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200166 } else {
167 wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
Jean Delvare965d3802010-10-09 12:36:45 +0000168 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200170}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000171EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200172
Dave Airlied6ea8882010-11-22 13:24:40 +1000173void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200174{
175 struct ttm_bo_device *bdev = bo->bdev;
176 struct ttm_mem_type_manager *man;
177
178 BUG_ON(!atomic_read(&bo->reserved));
179
180 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
181
182 BUG_ON(!list_empty(&bo->lru));
183
184 man = &bdev->man[bo->mem.mem_type];
185 list_add_tail(&bo->lru, &man->lru);
186 kref_get(&bo->list_kref);
187
188 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200189 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200190 kref_get(&bo->list_kref);
191 }
192 }
193}
194
Dave Airlied6ea8882010-11-22 13:24:40 +1000195int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200196{
197 int put_count = 0;
198
199 if (!list_empty(&bo->swap)) {
200 list_del_init(&bo->swap);
201 ++put_count;
202 }
203 if (!list_empty(&bo->lru)) {
204 list_del_init(&bo->lru);
205 ++put_count;
206 }
207
208 /*
209 * TODO: Add a driver hook to delete from
210 * driver-specific LRU's here.
211 */
212
213 return put_count;
214}
215
216int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
217 bool interruptible,
218 bool no_wait, bool use_sequence, uint32_t sequence)
219{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200220 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200221 int ret;
222
223 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100224 /**
225 * Deadlock avoidance for multi-bo reserving.
226 */
Thomas Hellstrom96726fe2010-11-17 12:28:28 +0000227 if (use_sequence && bo->seq_valid) {
228 /**
229 * We've already reserved this one.
230 */
231 if (unlikely(sequence == bo->val_seq))
232 return -EDEADLK;
233 /**
234 * Already reserved by a thread that will not back
235 * off for us. We need to back off.
236 */
237 if (unlikely(sequence - bo->val_seq < (1 << 31)))
238 return -EAGAIN;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200239 }
240
241 if (no_wait)
242 return -EBUSY;
243
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200244 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200245 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200246 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200247
248 if (unlikely(ret))
249 return ret;
250 }
251
252 if (use_sequence) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100253 /**
254 * Wake up waiters that may need to recheck for deadlock,
255 * if we decreased the sequence number.
256 */
257 if (unlikely((bo->val_seq - sequence < (1 << 31))
258 || !bo->seq_valid))
259 wake_up_all(&bo->event_queue);
260
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200261 bo->val_seq = sequence;
262 bo->seq_valid = true;
263 } else {
264 bo->seq_valid = false;
265 }
266
267 return 0;
268}
269EXPORT_SYMBOL(ttm_bo_reserve);
270
271static void ttm_bo_ref_bug(struct kref *list_kref)
272{
273 BUG();
274}
275
Dave Airlied6ea8882010-11-22 13:24:40 +1000276void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
277 bool never_free)
278{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100279 kref_sub(&bo->list_kref, count,
280 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000281}
282
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200283int ttm_bo_reserve(struct ttm_buffer_object *bo,
284 bool interruptible,
285 bool no_wait, bool use_sequence, uint32_t sequence)
286{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200287 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200288 int put_count = 0;
289 int ret;
290
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200291 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200292 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
293 sequence);
294 if (likely(ret == 0))
295 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200296 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200297
Dave Airlied6ea8882010-11-22 13:24:40 +1000298 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200299
300 return ret;
301}
302
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000303void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
304{
305 ttm_bo_add_to_lru(bo);
306 atomic_set(&bo->reserved, 0);
307 wake_up_all(&bo->event_queue);
308}
309
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200310void ttm_bo_unreserve(struct ttm_buffer_object *bo)
311{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200312 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200313
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200314 spin_lock(&glob->lru_lock);
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000315 ttm_bo_unreserve_locked(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200316 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200317}
318EXPORT_SYMBOL(ttm_bo_unreserve);
319
320/*
321 * Call bo->mutex locked.
322 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
324{
325 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200326 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200327 int ret = 0;
328 uint32_t page_flags = 0;
329
330 TTM_ASSERT_LOCKED(&bo->mutex);
331 bo->ttm = NULL;
332
Dave Airliead49f502009-07-10 22:36:26 +1000333 if (bdev->need_dma32)
334 page_flags |= TTM_PAGE_FLAG_DMA32;
335
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200336 switch (bo->type) {
337 case ttm_bo_type_device:
338 if (zero_alloc)
339 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
340 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400341 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
342 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200343 if (unlikely(bo->ttm == NULL))
344 ret = -ENOMEM;
345 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200346 default:
347 printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
348 ret = -EINVAL;
349 break;
350 }
351
352 return ret;
353}
354
355static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
356 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000357 bool evict, bool interruptible,
358 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200359{
360 struct ttm_bo_device *bdev = bo->bdev;
361 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
362 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
363 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
364 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
365 int ret = 0;
366
367 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100368 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
369 ret = ttm_mem_io_lock(old_man, true);
370 if (unlikely(ret != 0))
371 goto out_err;
372 ttm_bo_unmap_virtual_locked(bo);
373 ttm_mem_io_unlock(old_man);
374 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200375
376 /*
377 * Create and bind a ttm if required.
378 */
379
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000380 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
381 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000382 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
383 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000384 if (ret)
385 goto out_err;
386 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200387
388 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
389 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200390 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200391
392 if (mem->mem_type != TTM_PL_SYSTEM) {
393 ret = ttm_tt_bind(bo->ttm, mem);
394 if (ret)
395 goto out_err;
396 }
397
398 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000399 if (bdev->driver->move_notify)
400 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100401 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200402 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200403 goto moved;
404 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200405 }
406
Dave Airliee024e112009-06-24 09:48:08 +1000407 if (bdev->driver->move_notify)
408 bdev->driver->move_notify(bo, mem);
409
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200410 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
411 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000412 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200413 else if (bdev->driver->move)
414 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000415 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200416 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000417 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200418
419 if (ret)
420 goto out_err;
421
422moved:
423 if (bo->evicted) {
424 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
425 if (ret)
426 printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
427 bo->evicted = false;
428 }
429
430 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000431 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200432 bdev->man[bo->mem.mem_type].gpu_offset;
433 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100434 } else
435 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200436
437 return 0;
438
439out_err:
440 new_man = &bdev->man[bo->mem.mem_type];
441 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
442 ttm_tt_unbind(bo->ttm);
443 ttm_tt_destroy(bo->ttm);
444 bo->ttm = NULL;
445 }
446
447 return ret;
448}
449
450/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200451 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200452 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200453 * This is the place to put in driver specific hooks to release
454 * driver private resources.
455 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200456 */
457
458static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
459{
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200460 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200461 ttm_tt_unbind(bo->ttm);
462 ttm_tt_destroy(bo->ttm);
463 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200464 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200465 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200466
467 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200468
469 /*
470 * Make processes trying to reserve really pick it up.
471 */
472 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200473 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200474}
475
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200476static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200477{
478 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200479 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200480 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000481 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200482 void *sync_obj_arg;
483 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200484 int ret;
485
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000486 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200487 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200488 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200489
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200490 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100491
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200492 /**
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000493 * Lock inversion between bo:reserve and bdev::fence_lock here,
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200494 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200495 */
496
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200497 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200498
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200499 if (unlikely(ret == -EBUSY))
500 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200501
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000502 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200503 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200504
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200505 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200506 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200507
Dave Airlied6ea8882010-11-22 13:24:40 +1000508 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200509
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200510 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200511 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200512 spin_lock(&glob->lru_lock);
513 }
514queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200515 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000516 if (bo->sync_obj)
517 sync_obj = driver->sync_obj_ref(bo->sync_obj);
518 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200519
520 kref_get(&bo->list_kref);
521 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
522 spin_unlock(&glob->lru_lock);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000523 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200524
Thomas Hellstromaa123262010-11-02 13:21:47 +0000525 if (sync_obj) {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200526 driver->sync_obj_flush(sync_obj, sync_obj_arg);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000527 driver->sync_obj_unref(&sync_obj);
528 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200529 schedule_delayed_work(&bdev->wq,
530 ((HZ / 100) < 1) ? 1 : HZ / 100);
531}
532
533/**
534 * function ttm_bo_cleanup_refs
535 * If bo idle, remove from delayed- and lru lists, and unref.
536 * If not idle, do nothing.
537 *
538 * @interruptible Any sleeps should occur interruptibly.
539 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
540 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
541 */
542
543static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
544 bool interruptible,
545 bool no_wait_reserve,
546 bool no_wait_gpu)
547{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000548 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200549 struct ttm_bo_global *glob = bo->glob;
550 int put_count;
551 int ret = 0;
552
553retry:
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000554 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200555 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000556 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200557
558 if (unlikely(ret != 0))
559 return ret;
560
561 spin_lock(&glob->lru_lock);
562 ret = ttm_bo_reserve_locked(bo, interruptible,
563 no_wait_reserve, false, 0);
564
565 if (unlikely(ret != 0) || list_empty(&bo->ddestroy)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200566 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200567 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200568 }
569
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200570 /**
571 * We can re-check for sync object without taking
572 * the bo::lock since setting the sync object requires
573 * also bo::reserved. A busy object at this point may
574 * be caused by another thread recently starting an accelerated
575 * eviction.
576 */
577
578 if (unlikely(bo->sync_obj)) {
579 atomic_set(&bo->reserved, 0);
580 wake_up_all(&bo->event_queue);
581 spin_unlock(&glob->lru_lock);
582 goto retry;
583 }
584
585 put_count = ttm_bo_del_from_lru(bo);
586 list_del_init(&bo->ddestroy);
587 ++put_count;
588
589 spin_unlock(&glob->lru_lock);
590 ttm_bo_cleanup_memtype_use(bo);
591
Dave Airlied6ea8882010-11-22 13:24:40 +1000592 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200593
594 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200595}
596
597/**
598 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
599 * encountered buffers.
600 */
601
602static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
603{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200604 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100605 struct ttm_buffer_object *entry = NULL;
606 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200607
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200608 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100609 if (list_empty(&bdev->ddestroy))
610 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200611
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100612 entry = list_first_entry(&bdev->ddestroy,
613 struct ttm_buffer_object, ddestroy);
614 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200615
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100616 for (;;) {
617 struct ttm_buffer_object *nentry = NULL;
618
619 if (entry->ddestroy.next != &bdev->ddestroy) {
620 nentry = list_first_entry(&entry->ddestroy,
621 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200622 kref_get(&nentry->list_kref);
623 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200624
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200625 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200626 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
627 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200628 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100629 entry = nentry;
630
631 if (ret || !entry)
632 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200633
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200634 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100635 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200636 break;
637 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200638
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100639out_unlock:
640 spin_unlock(&glob->lru_lock);
641out:
642 if (entry)
643 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200644 return ret;
645}
646
647static void ttm_bo_delayed_workqueue(struct work_struct *work)
648{
649 struct ttm_bo_device *bdev =
650 container_of(work, struct ttm_bo_device, wq.work);
651
652 if (ttm_bo_delayed_delete(bdev, false)) {
653 schedule_delayed_work(&bdev->wq,
654 ((HZ / 100) < 1) ? 1 : HZ / 100);
655 }
656}
657
658static void ttm_bo_release(struct kref *kref)
659{
660 struct ttm_buffer_object *bo =
661 container_of(kref, struct ttm_buffer_object, kref);
662 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100663 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200664
665 if (likely(bo->vm_node != NULL)) {
666 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
667 drm_mm_put_block(bo->vm_node);
668 bo->vm_node = NULL;
669 }
670 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100671 ttm_mem_io_lock(man, false);
672 ttm_mem_io_free_vm(bo);
673 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200674 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200675 kref_put(&bo->list_kref, ttm_bo_release_list);
676 write_lock(&bdev->vm_lock);
677}
678
679void ttm_bo_unref(struct ttm_buffer_object **p_bo)
680{
681 struct ttm_buffer_object *bo = *p_bo;
682 struct ttm_bo_device *bdev = bo->bdev;
683
684 *p_bo = NULL;
685 write_lock(&bdev->vm_lock);
686 kref_put(&bo->kref, ttm_bo_release);
687 write_unlock(&bdev->vm_lock);
688}
689EXPORT_SYMBOL(ttm_bo_unref);
690
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400691int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
692{
693 return cancel_delayed_work_sync(&bdev->wq);
694}
695EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
696
697void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
698{
699 if (resched)
700 schedule_delayed_work(&bdev->wq,
701 ((HZ / 100) < 1) ? 1 : HZ / 100);
702}
703EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
704
Jerome Glisseca262a9992009-12-08 15:33:32 +0100705static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000706 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200707{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200708 struct ttm_bo_device *bdev = bo->bdev;
709 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100710 struct ttm_placement placement;
711 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200712
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000713 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200714 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000715 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200716
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200717 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100718 if (ret != -ERESTARTSYS) {
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200719 printk(KERN_ERR TTM_PFX
720 "Failed to expire sync object before "
721 "buffer eviction.\n");
722 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200723 goto out;
724 }
725
726 BUG_ON(!atomic_read(&bo->reserved));
727
728 evict_mem = bo->mem;
729 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100730 evict_mem.bus.io_reserved_vm = false;
731 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200732
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100733 placement.fpfn = 0;
734 placement.lpfn = 0;
735 placement.num_placement = 0;
736 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100737 bdev->driver->evict_flags(bo, &placement);
738 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000739 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200740 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100741 if (ret != -ERESTARTSYS) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200742 printk(KERN_ERR TTM_PFX
743 "Failed to find memory space for "
744 "buffer 0x%p eviction.\n", bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100745 ttm_bo_mem_space_debug(bo, &placement);
746 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200747 goto out;
748 }
749
750 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000751 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200752 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100753 if (ret != -ERESTARTSYS)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200754 printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000755 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200756 goto out;
757 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200758 bo->evicted = true;
759out:
760 return ret;
761}
762
Jerome Glisseca262a9992009-12-08 15:33:32 +0100763static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
764 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000765 bool interruptible, bool no_wait_reserve,
766 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100767{
768 struct ttm_bo_global *glob = bdev->glob;
769 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
770 struct ttm_buffer_object *bo;
771 int ret, put_count = 0;
772
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100773retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100774 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100775 if (list_empty(&man->lru)) {
776 spin_unlock(&glob->lru_lock);
777 return -EBUSY;
778 }
779
Jerome Glisseca262a9992009-12-08 15:33:32 +0100780 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
781 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100782
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200783 if (!list_empty(&bo->ddestroy)) {
784 spin_unlock(&glob->lru_lock);
785 ret = ttm_bo_cleanup_refs(bo, interruptible,
786 no_wait_reserve, no_wait_gpu);
787 kref_put(&bo->list_kref, ttm_bo_release_list);
788
789 if (likely(ret == 0 || ret == -ERESTARTSYS))
790 return ret;
791
792 goto retry;
793 }
794
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000795 ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100796
797 if (unlikely(ret == -EBUSY)) {
798 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000799 if (likely(!no_wait_gpu))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100800 ret = ttm_bo_wait_unreserved(bo, interruptible);
801
802 kref_put(&bo->list_kref, ttm_bo_release_list);
803
804 /**
805 * We *need* to retry after releasing the lru lock.
806 */
807
808 if (unlikely(ret != 0))
809 return ret;
810 goto retry;
811 }
812
813 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100814 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100815
816 BUG_ON(ret != 0);
817
Dave Airlied6ea8882010-11-22 13:24:40 +1000818 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100819
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000820 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100821 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100822
Jerome Glisseca262a9992009-12-08 15:33:32 +0100823 kref_put(&bo->list_kref, ttm_bo_release_list);
824 return ret;
825}
826
Ben Skeggs42311ff2010-08-04 12:07:08 +1000827void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
828{
Ben Skeggsd961db72010-08-05 10:48:18 +1000829 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000830
Ben Skeggsd961db72010-08-05 10:48:18 +1000831 if (mem->mm_node)
832 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000833}
834EXPORT_SYMBOL(ttm_bo_mem_put);
835
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200836/**
837 * Repeatedly evict memory from the LRU for @mem_type until we create enough
838 * space, or we've evicted everything and there isn't enough space.
839 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100840static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
841 uint32_t mem_type,
842 struct ttm_placement *placement,
843 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000844 bool interruptible,
845 bool no_wait_reserve,
846 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200847{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100848 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200849 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200850 int ret;
851
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200852 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000853 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200854 if (unlikely(ret != 0))
855 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000856 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100857 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100858 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000859 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100860 if (unlikely(ret != 0))
861 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200862 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000863 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200864 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865 mem->mem_type = mem_type;
866 return 0;
867}
868
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200869static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
870 uint32_t cur_placement,
871 uint32_t proposed_placement)
872{
873 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
874 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
875
876 /**
877 * Keep current caching if possible.
878 */
879
880 if ((cur_placement & caching) != 0)
881 result |= (cur_placement & caching);
882 else if ((man->default_caching & caching) != 0)
883 result |= man->default_caching;
884 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
885 result |= TTM_PL_FLAG_CACHED;
886 else if ((TTM_PL_FLAG_WC & caching) != 0)
887 result |= TTM_PL_FLAG_WC;
888 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
889 result |= TTM_PL_FLAG_UNCACHED;
890
891 return result;
892}
893
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200894static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200895 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200896 uint32_t proposed_placement,
897 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200898{
899 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
900
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200901 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200902 return false;
903
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200904 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200905 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200906
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200907 cur_flags |= (proposed_placement & man->available_caching);
908
909 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200910 return true;
911}
912
913/**
914 * Creates space for memory region @mem according to its type.
915 *
916 * This function first searches for free space in compatible memory types in
917 * the priority order defined by the driver. If free space isn't found, then
918 * ttm_bo_mem_force_space is attempted in priority order to evict and find
919 * space.
920 */
921int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100922 struct ttm_placement *placement,
923 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000924 bool interruptible, bool no_wait_reserve,
925 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926{
927 struct ttm_bo_device *bdev = bo->bdev;
928 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929 uint32_t mem_type = TTM_PL_SYSTEM;
930 uint32_t cur_flags = 0;
931 bool type_found = false;
932 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100933 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100934 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200935
936 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000937 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100938 ret = ttm_mem_type_from_flags(placement->placement[i],
939 &mem_type);
940 if (ret)
941 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200942 man = &bdev->man[mem_type];
943
944 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100945 mem_type,
946 placement->placement[i],
947 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200948
949 if (!type_ok)
950 continue;
951
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200952 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
953 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100954 /*
955 * Use the access and other non-mapping-related flag bits from
956 * the memory placement flags to the current flags
957 */
958 ttm_flag_masked(&cur_flags, placement->placement[i],
959 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200960
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200961 if (mem_type == TTM_PL_SYSTEM)
962 break;
963
964 if (man->has_type && man->use_type) {
965 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000966 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100967 if (unlikely(ret))
968 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200969 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000970 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200971 break;
972 }
973
Ben Skeggsd961db72010-08-05 10:48:18 +1000974 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200975 mem->mem_type = mem_type;
976 mem->placement = cur_flags;
977 return 0;
978 }
979
980 if (!type_found)
981 return -EINVAL;
982
Dave Airlieb6637522009-12-14 14:51:35 +1000983 for (i = 0; i < placement->num_busy_placement; ++i) {
984 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100985 &mem_type);
986 if (ret)
987 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200989 if (!man->has_type)
990 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200991 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100992 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +1000993 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +0100994 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200995 continue;
996
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200997 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
998 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100999 /*
1000 * Use the access and other non-mapping-related flag bits from
1001 * the memory placement flags to the current flags
1002 */
Dave Airlieb6637522009-12-14 14:51:35 +10001003 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001004 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001005
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001006
1007 if (mem_type == TTM_PL_SYSTEM) {
1008 mem->mem_type = mem_type;
1009 mem->placement = cur_flags;
1010 mem->mm_node = NULL;
1011 return 0;
1012 }
1013
Jerome Glisseca262a9992009-12-08 15:33:32 +01001014 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001015 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001016 if (ret == 0 && mem->mm_node) {
1017 mem->placement = cur_flags;
1018 return 0;
1019 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001020 if (ret == -ERESTARTSYS)
1021 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001022 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001023 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001024 return ret;
1025}
1026EXPORT_SYMBOL(ttm_bo_mem_space);
1027
1028int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1029{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001030 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1031 return -EBUSY;
1032
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001033 return wait_event_interruptible(bo->event_queue,
1034 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001035}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001036EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037
1038int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001039 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001040 bool interruptible, bool no_wait_reserve,
1041 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001042{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001043 int ret = 0;
1044 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001045 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001046
1047 BUG_ON(!atomic_read(&bo->reserved));
1048
1049 /*
1050 * FIXME: It's possible to pipeline buffer moves.
1051 * Have the driver move function wait for idle when necessary,
1052 * instead of doing it here.
1053 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001054 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001055 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001056 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001057 if (ret)
1058 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001059 mem.num_pages = bo->num_pages;
1060 mem.size = mem.num_pages << PAGE_SHIFT;
1061 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001062 mem.bus.io_reserved_vm = false;
1063 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001064 /*
1065 * Determine where to move the buffer.
1066 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001067 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001068 if (ret)
1069 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001070 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001071out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001072 if (ret && mem.mm_node)
1073 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074 return ret;
1075}
1076
Jerome Glisseca262a9992009-12-08 15:33:32 +01001077static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001078 struct ttm_mem_reg *mem)
1079{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001080 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001081
Ben Skeggsd961db72010-08-05 10:48:18 +10001082 if (mem->mm_node && placement->lpfn != 0 &&
1083 (mem->start < placement->fpfn ||
1084 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001085 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086
Jerome Glisseca262a9992009-12-08 15:33:32 +01001087 for (i = 0; i < placement->num_placement; i++) {
1088 if ((placement->placement[i] & mem->placement &
1089 TTM_PL_MASK_CACHING) &&
1090 (placement->placement[i] & mem->placement &
1091 TTM_PL_MASK_MEM))
1092 return i;
1093 }
1094 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001095}
1096
Jerome Glisse09855ac2009-12-10 17:16:27 +01001097int ttm_bo_validate(struct ttm_buffer_object *bo,
1098 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001099 bool interruptible, bool no_wait_reserve,
1100 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101{
1102 int ret;
1103
1104 BUG_ON(!atomic_read(&bo->reserved));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001105 /* Check that range is valid */
1106 if (placement->lpfn || placement->fpfn)
1107 if (placement->fpfn > placement->lpfn ||
1108 (placement->lpfn - placement->fpfn) < bo->num_pages)
1109 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110 /*
1111 * Check whether we need to move buffer.
1112 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001113 ret = ttm_bo_mem_compat(placement, &bo->mem);
1114 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001115 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001116 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001118 } else {
1119 /*
1120 * Use the access and other non-mapping-related flag bits from
1121 * the compatible memory placement flags to the active flags
1122 */
1123 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1124 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001126 /*
1127 * We might need to add a TTM.
1128 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001129 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1130 ret = ttm_bo_add_ttm(bo, true);
1131 if (ret)
1132 return ret;
1133 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001134 return 0;
1135}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001136EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001137
Jerome Glisse09855ac2009-12-10 17:16:27 +01001138int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1139 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001140{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001141 BUG_ON((placement->fpfn || placement->lpfn) &&
1142 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 return 0;
1145}
1146
Jerome Glisse09855ac2009-12-10 17:16:27 +01001147int ttm_bo_init(struct ttm_bo_device *bdev,
1148 struct ttm_buffer_object *bo,
1149 unsigned long size,
1150 enum ttm_bo_type type,
1151 struct ttm_placement *placement,
1152 uint32_t page_alignment,
1153 unsigned long buffer_start,
1154 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001155 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001156 size_t acc_size,
1157 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001158{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001159 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001160 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001161 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1162
1163 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1164 if (ret) {
1165 printk(KERN_ERR TTM_PFX "Out of kernel memory.\n");
1166 if (destroy)
1167 (*destroy)(bo);
1168 else
1169 kfree(bo);
1170 return -ENOMEM;
1171 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001172
1173 size += buffer_start & ~PAGE_MASK;
1174 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1175 if (num_pages == 0) {
1176 printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001177 if (destroy)
1178 (*destroy)(bo);
1179 else
1180 kfree(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001181 return -EINVAL;
1182 }
1183 bo->destroy = destroy;
1184
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185 kref_init(&bo->kref);
1186 kref_init(&bo->list_kref);
1187 atomic_set(&bo->cpu_writers, 0);
1188 atomic_set(&bo->reserved, 1);
1189 init_waitqueue_head(&bo->event_queue);
1190 INIT_LIST_HEAD(&bo->lru);
1191 INIT_LIST_HEAD(&bo->ddestroy);
1192 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001193 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001194 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001195 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196 bo->type = type;
1197 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001198 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199 bo->mem.mem_type = TTM_PL_SYSTEM;
1200 bo->mem.num_pages = bo->num_pages;
1201 bo->mem.mm_node = NULL;
1202 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001203 bo->mem.bus.io_reserved_vm = false;
1204 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001205 bo->buffer_start = buffer_start & PAGE_MASK;
1206 bo->priv_flags = 0;
1207 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1208 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001209 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001210 bo->acc_size = acc_size;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001211 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212
Jerome Glisse09855ac2009-12-10 17:16:27 +01001213 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001214 if (unlikely(ret != 0))
1215 goto out_err;
1216
1217 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001218 * For ttm_bo_type_device buffers, allocate
1219 * address space from the device.
1220 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221 if (bo->type == ttm_bo_type_device) {
1222 ret = ttm_bo_setup_vm(bo);
1223 if (ret)
1224 goto out_err;
1225 }
1226
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001227 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001228 if (ret)
1229 goto out_err;
1230
1231 ttm_bo_unreserve(bo);
1232 return 0;
1233
1234out_err:
1235 ttm_bo_unreserve(bo);
1236 ttm_bo_unref(&bo);
1237
1238 return ret;
1239}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001240EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001241
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001242size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1243 unsigned long bo_size,
1244 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001245{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001246 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1247 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001249 size += ttm_round_pot(struct_size);
1250 size += PAGE_ALIGN(npages * sizeof(void *));
1251 size += ttm_round_pot(sizeof(struct ttm_tt));
1252 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001253}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001254EXPORT_SYMBOL(ttm_bo_acc_size);
1255
1256size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1257 unsigned long bo_size,
1258 unsigned struct_size)
1259{
1260 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1261 size_t size = 0;
1262
1263 size += ttm_round_pot(struct_size);
1264 size += PAGE_ALIGN(npages * sizeof(void *));
1265 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1266 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1267 return size;
1268}
1269EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001270
Jerome Glisse09855ac2009-12-10 17:16:27 +01001271int ttm_bo_create(struct ttm_bo_device *bdev,
1272 unsigned long size,
1273 enum ttm_bo_type type,
1274 struct ttm_placement *placement,
1275 uint32_t page_alignment,
1276 unsigned long buffer_start,
1277 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001278 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001279 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001280{
1281 struct ttm_buffer_object *bo;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001282 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001283 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001284 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001285
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001286 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001287 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001288 if (unlikely(ret != 0))
1289 return ret;
1290
1291 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1292
1293 if (unlikely(bo == NULL)) {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +02001294 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001295 return -ENOMEM;
1296 }
1297
Jerome Glisse09855ac2009-12-10 17:16:27 +01001298 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1299 buffer_start, interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001300 persistent_swap_storage, acc_size, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001301 if (likely(ret == 0))
1302 *p_bo = bo;
1303
1304 return ret;
1305}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001306EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001307
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001308static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001309 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001310{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001311 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001312 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001313 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001314
1315 /*
1316 * Can't use standard list traversal since we're unlocking.
1317 */
1318
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001319 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001320 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001321 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001322 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001323 if (ret) {
1324 if (allow_errors) {
1325 return ret;
1326 } else {
1327 printk(KERN_ERR TTM_PFX
1328 "Cleanup eviction failed\n");
1329 }
1330 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001331 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001332 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001333 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001334 return 0;
1335}
1336
1337int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1338{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001339 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001340 int ret = -EINVAL;
1341
1342 if (mem_type >= TTM_NUM_MEM_TYPES) {
1343 printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
1344 return ret;
1345 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001346 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001347
1348 if (!man->has_type) {
1349 printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
1350 "memory manager type %u\n", mem_type);
1351 return ret;
1352 }
1353
1354 man->use_type = false;
1355 man->has_type = false;
1356
1357 ret = 0;
1358 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001359 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001360
Ben Skeggsd961db72010-08-05 10:48:18 +10001361 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001362 }
1363
1364 return ret;
1365}
1366EXPORT_SYMBOL(ttm_bo_clean_mm);
1367
1368int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1369{
1370 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1371
1372 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1373 printk(KERN_ERR TTM_PFX
1374 "Illegal memory manager memory type %u.\n",
1375 mem_type);
1376 return -EINVAL;
1377 }
1378
1379 if (!man->has_type) {
1380 printk(KERN_ERR TTM_PFX
1381 "Memory type %u has not been initialized.\n",
1382 mem_type);
1383 return 0;
1384 }
1385
Jerome Glisseca262a9992009-12-08 15:33:32 +01001386 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001387}
1388EXPORT_SYMBOL(ttm_bo_evict_mm);
1389
1390int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001391 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001392{
1393 int ret = -EINVAL;
1394 struct ttm_mem_type_manager *man;
1395
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001396 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001397 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001398 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001399 man->io_reserve_fastpath = true;
1400 man->use_io_reserve_lru = false;
1401 mutex_init(&man->io_reserve_mutex);
1402 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001403
1404 ret = bdev->driver->init_mem_type(bdev, type, man);
1405 if (ret)
1406 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001407 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001408
1409 ret = 0;
1410 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001411 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001412 if (ret)
1413 return ret;
1414 }
1415 man->has_type = true;
1416 man->use_type = true;
1417 man->size = p_size;
1418
1419 INIT_LIST_HEAD(&man->lru);
1420
1421 return 0;
1422}
1423EXPORT_SYMBOL(ttm_bo_init_mm);
1424
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001425static void ttm_bo_global_kobj_release(struct kobject *kobj)
1426{
1427 struct ttm_bo_global *glob =
1428 container_of(kobj, struct ttm_bo_global, kobj);
1429
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001430 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1431 __free_page(glob->dummy_read_page);
1432 kfree(glob);
1433}
1434
Dave Airlieba4420c2010-03-09 10:56:52 +10001435void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001436{
1437 struct ttm_bo_global *glob = ref->object;
1438
1439 kobject_del(&glob->kobj);
1440 kobject_put(&glob->kobj);
1441}
1442EXPORT_SYMBOL(ttm_bo_global_release);
1443
Dave Airlieba4420c2010-03-09 10:56:52 +10001444int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001445{
1446 struct ttm_bo_global_ref *bo_ref =
1447 container_of(ref, struct ttm_bo_global_ref, ref);
1448 struct ttm_bo_global *glob = ref->object;
1449 int ret;
1450
1451 mutex_init(&glob->device_list_mutex);
1452 spin_lock_init(&glob->lru_lock);
1453 glob->mem_glob = bo_ref->mem_glob;
1454 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1455
1456 if (unlikely(glob->dummy_read_page == NULL)) {
1457 ret = -ENOMEM;
1458 goto out_no_drp;
1459 }
1460
1461 INIT_LIST_HEAD(&glob->swap_lru);
1462 INIT_LIST_HEAD(&glob->device_list);
1463
1464 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1465 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1466 if (unlikely(ret != 0)) {
1467 printk(KERN_ERR TTM_PFX
1468 "Could not register buffer object swapout.\n");
1469 goto out_no_shrink;
1470 }
1471
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001472 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
1700int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001701 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001702{
1703 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001704 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001705 void *sync_obj;
1706 void *sync_obj_arg;
1707 int ret = 0;
1708
Dave Airlie1717c0e2011-10-27 18:28:37 +02001709 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001710 return 0;
1711
Dave Airlie1717c0e2011-10-27 18:28:37 +02001712 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001713
Dave Airlie1717c0e2011-10-27 18:28:37 +02001714 if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
1715 void *tmp_obj = bo->sync_obj;
1716 bo->sync_obj = NULL;
1717 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1718 spin_unlock(&bdev->fence_lock);
1719 driver->sync_obj_unref(&tmp_obj);
1720 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001721 continue;
1722 }
1723
1724 if (no_wait)
1725 return -EBUSY;
1726
Dave Airlie1717c0e2011-10-27 18:28:37 +02001727 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001728 sync_obj_arg = bo->sync_obj_arg;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001729 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001730 ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
1731 lazy, interruptible);
1732 if (unlikely(ret != 0)) {
1733 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001734 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001735 return ret;
1736 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001737 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001738 if (likely(bo->sync_obj == sync_obj &&
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001739 bo->sync_obj_arg == sync_obj_arg)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001740 void *tmp_obj = bo->sync_obj;
1741 bo->sync_obj = NULL;
1742 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1743 &bo->priv_flags);
1744 spin_unlock(&bdev->fence_lock);
1745 driver->sync_obj_unref(&sync_obj);
1746 driver->sync_obj_unref(&tmp_obj);
1747 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001748 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001749 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001750 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001751 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001752 }
1753 }
1754 return 0;
1755}
1756EXPORT_SYMBOL(ttm_bo_wait);
1757
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001758int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1759{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001760 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001761 int ret = 0;
1762
1763 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001764 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001765 */
1766
1767 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1768 if (unlikely(ret != 0))
1769 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001770 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001771 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001772 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001773 if (likely(ret == 0))
1774 atomic_inc(&bo->cpu_writers);
1775 ttm_bo_unreserve(bo);
1776 return ret;
1777}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001778EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001779
1780void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1781{
1782 if (atomic_dec_and_test(&bo->cpu_writers))
1783 wake_up_all(&bo->event_queue);
1784}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001785EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001786
1787/**
1788 * A buffer object shrink method that tries to swap out the first
1789 * buffer object on the bo_global::swap_lru list.
1790 */
1791
1792static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1793{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001794 struct ttm_bo_global *glob =
1795 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001796 struct ttm_buffer_object *bo;
1797 int ret = -EBUSY;
1798 int put_count;
1799 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1800
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001801 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001802 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001803 if (unlikely(list_empty(&glob->swap_lru))) {
1804 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001805 return -EBUSY;
1806 }
1807
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001808 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001809 struct ttm_buffer_object, swap);
1810 kref_get(&bo->list_kref);
1811
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001812 if (!list_empty(&bo->ddestroy)) {
1813 spin_unlock(&glob->lru_lock);
1814 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1815 kref_put(&bo->list_kref, ttm_bo_release_list);
1816 continue;
1817 }
1818
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001819 /**
1820 * Reserve buffer. Since we unlock while sleeping, we need
1821 * to re-check that nobody removed us from the swap-list while
1822 * we slept.
1823 */
1824
1825 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1826 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001827 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001828 ttm_bo_wait_unreserved(bo, false);
1829 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001830 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001831 }
1832 }
1833
1834 BUG_ON(ret != 0);
1835 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001836 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001837
Dave Airlied6ea8882010-11-22 13:24:40 +10001838 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001839
1840 /**
1841 * Wait for GPU, then move to system cached.
1842 */
1843
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001844 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001845 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001846 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001847
1848 if (unlikely(ret != 0))
1849 goto out;
1850
1851 if ((bo->mem.placement & swap_placement) != swap_placement) {
1852 struct ttm_mem_reg evict_mem;
1853
1854 evict_mem = bo->mem;
1855 evict_mem.mm_node = NULL;
1856 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1857 evict_mem.mem_type = TTM_PL_SYSTEM;
1858
1859 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001860 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001861 if (unlikely(ret != 0))
1862 goto out;
1863 }
1864
1865 ttm_bo_unmap_virtual(bo);
1866
1867 /**
1868 * Swap out. Buffer will be swapped in again as soon as
1869 * anyone tries to access a ttm page.
1870 */
1871
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001872 if (bo->bdev->driver->swap_notify)
1873 bo->bdev->driver->swap_notify(bo);
1874
Jan Engelhardt5df23972011-04-04 01:25:18 +02001875 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001876out:
1877
1878 /**
1879 *
1880 * Unreserve without putting on LRU to avoid swapping out an
1881 * already swapped buffer.
1882 */
1883
1884 atomic_set(&bo->reserved, 0);
1885 wake_up_all(&bo->event_queue);
1886 kref_put(&bo->list_kref, ttm_bo_release_list);
1887 return ret;
1888}
1889
1890void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1891{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001892 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001893 ;
1894}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001895EXPORT_SYMBOL(ttm_bo_swapout_all);