blob: 0ac0a88860a4fee209fb600a93c5a937e89d4483 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30
Joe Perches25d04792012-03-16 21:43:50 -070031#define pr_fmt(fmt) "[TTM] " fmt
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/ttm/ttm_module.h>
34#include <drm/ttm/ttm_bo_driver.h>
35#include <drm/ttm/ttm_placement.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/jiffies.h>
37#include <linux/slab.h>
38#include <linux/sched.h>
39#include <linux/mm.h>
40#include <linux/file.h>
41#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020043
44#define TTM_ASSERT_LOCKED(param)
45#define TTM_DEBUG(fmt, arg...)
46#define TTM_BO_HASH_ORDER 13
47
48static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020049static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
Thomas Hellstroma987fca2009-08-18 16:51:56 +020050static void ttm_bo_global_kobj_release(struct kobject *kobj);
51
52static struct attribute ttm_bo_count = {
53 .name = "bo_count",
54 .mode = S_IRUGO
55};
56
Jerome Glissefb53f862009-12-09 21:55:10 +010057static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
58{
59 int i;
60
61 for (i = 0; i <= TTM_PL_PRIV5; i++)
62 if (flags & (1 << i)) {
63 *mem_type = i;
64 return 0;
65 }
66 return -EINVAL;
67}
68
Jerome Glisse5012f502009-12-10 18:07:26 +010069static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
Jerome Glissefb53f862009-12-09 21:55:10 +010070{
Jerome Glisse5012f502009-12-10 18:07:26 +010071 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
72
Joe Perches25d04792012-03-16 21:43:50 -070073 pr_err(" has_type: %d\n", man->has_type);
74 pr_err(" use_type: %d\n", man->use_type);
75 pr_err(" flags: 0x%08X\n", man->flags);
76 pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
77 pr_err(" size: %llu\n", man->size);
78 pr_err(" available_caching: 0x%08X\n", man->available_caching);
79 pr_err(" default_caching: 0x%08X\n", man->default_caching);
Ben Skeggsd961db72010-08-05 10:48:18 +100080 if (mem_type != TTM_PL_SYSTEM)
81 (*man->func->debug)(man, TTM_PFX);
Jerome Glissefb53f862009-12-09 21:55:10 +010082}
83
84static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
85 struct ttm_placement *placement)
86{
Jerome Glissefb53f862009-12-09 21:55:10 +010087 int i, ret, mem_type;
88
Joe Perches25d04792012-03-16 21:43:50 -070089 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
90 bo, bo->mem.num_pages, bo->mem.size >> 10,
91 bo->mem.size >> 20);
Jerome Glissefb53f862009-12-09 21:55:10 +010092 for (i = 0; i < placement->num_placement; i++) {
93 ret = ttm_mem_type_from_flags(placement->placement[i],
94 &mem_type);
95 if (ret)
96 return;
Joe Perches25d04792012-03-16 21:43:50 -070097 pr_err(" placement[%d]=0x%08X (%d)\n",
98 i, placement->placement[i], mem_type);
Jerome Glisse5012f502009-12-10 18:07:26 +010099 ttm_mem_type_debug(bo->bdev, mem_type);
Jerome Glissefb53f862009-12-09 21:55:10 +0100100 }
101}
102
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200103static ssize_t ttm_bo_global_show(struct kobject *kobj,
104 struct attribute *attr,
105 char *buffer)
106{
107 struct ttm_bo_global *glob =
108 container_of(kobj, struct ttm_bo_global, kobj);
109
110 return snprintf(buffer, PAGE_SIZE, "%lu\n",
111 (unsigned long) atomic_read(&glob->bo_count));
112}
113
114static struct attribute *ttm_bo_global_attrs[] = {
115 &ttm_bo_count,
116 NULL
117};
118
Emese Revfy52cf25d2010-01-19 02:58:23 +0100119static const struct sysfs_ops ttm_bo_global_ops = {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200120 .show = &ttm_bo_global_show
121};
122
123static struct kobj_type ttm_bo_glob_kobj_type = {
124 .release = &ttm_bo_global_kobj_release,
125 .sysfs_ops = &ttm_bo_global_ops,
126 .default_attrs = ttm_bo_global_attrs
127};
128
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200129
130static inline uint32_t ttm_bo_type_flags(unsigned type)
131{
132 return 1 << (type);
133}
134
135static void ttm_bo_release_list(struct kref *list_kref)
136{
137 struct ttm_buffer_object *bo =
138 container_of(list_kref, struct ttm_buffer_object, list_kref);
139 struct ttm_bo_device *bdev = bo->bdev;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500140 size_t acc_size = bo->acc_size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200141
142 BUG_ON(atomic_read(&bo->list_kref.refcount));
143 BUG_ON(atomic_read(&bo->kref.refcount));
144 BUG_ON(atomic_read(&bo->cpu_writers));
145 BUG_ON(bo->sync_obj != NULL);
146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
149
150 if (bo->ttm)
151 ttm_tt_destroy(bo->ttm);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200152 atomic_dec(&bo->glob->bo_count);
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
Maarten Lankhorstcc4c0c42013-01-15 14:57:28 +0100161static int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo,
162 bool interruptible)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200163{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200164 if (interruptible) {
Jean Delvare965d3802010-10-09 12:36:45 +0000165 return wait_event_interruptible(bo->event_queue,
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200166 !ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200167 } else {
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200168 wait_event(bo->event_queue, !ttm_bo_is_reserved(bo));
Jean Delvare965d3802010-10-09 12:36:45 +0000169 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200170 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200171}
172
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
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200178 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200179
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
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100216int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200217 bool interruptible,
218 bool no_wait, bool use_sequence, uint32_t sequence)
219{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200220 int ret;
221
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100222 while (unlikely(atomic_xchg(&bo->reserved, 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 Hellstromba4e7d92009-06-10 15:20:19 +0200243 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200244
245 if (unlikely(ret))
246 return ret;
247 }
248
249 if (use_sequence) {
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100250 bool wake_up = false;
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100251 /**
252 * Wake up waiters that may need to recheck for deadlock,
253 * if we decreased the sequence number.
254 */
255 if (unlikely((bo->val_seq - sequence < (1 << 31))
256 || !bo->seq_valid))
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100257 wake_up = true;
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100258
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100259 /*
260 * In the worst case with memory ordering these values can be
261 * seen in the wrong order. However since we call wake_up_all
262 * in that case, this will hopefully not pose a problem,
263 * and the worst case would only cause someone to accidentally
264 * hit -EAGAIN in ttm_bo_reserve when they see old value of
265 * val_seq. However this would only happen if seq_valid was
266 * written before val_seq was, and just means some slightly
267 * increased cpu usage
268 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200269 bo->val_seq = sequence;
270 bo->seq_valid = true;
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100271 if (wake_up)
272 wake_up_all(&bo->event_queue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200273 } else {
274 bo->seq_valid = false;
275 }
276
277 return 0;
278}
279EXPORT_SYMBOL(ttm_bo_reserve);
280
281static void ttm_bo_ref_bug(struct kref *list_kref)
282{
283 BUG();
284}
285
Dave Airlied6ea8882010-11-22 13:24:40 +1000286void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
287 bool never_free)
288{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100289 kref_sub(&bo->list_kref, count,
290 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000291}
292
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200293int ttm_bo_reserve(struct ttm_buffer_object *bo,
294 bool interruptible,
295 bool no_wait, bool use_sequence, uint32_t sequence)
296{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200297 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200298 int put_count = 0;
299 int ret;
300
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100301 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_sequence,
302 sequence);
303 if (likely(ret == 0)) {
304 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200305 put_count = ttm_bo_del_from_lru(bo);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100306 spin_unlock(&glob->lru_lock);
307 ttm_bo_list_ref_sub(bo, put_count, true);
308 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200309
310 return ret;
311}
312
Maarten Lankhorst5e45d7d2013-01-15 14:57:05 +0100313int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
314 bool interruptible, uint32_t sequence)
315{
316 bool wake_up = false;
317 int ret;
318
319 while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) {
320 WARN_ON(bo->seq_valid && sequence == bo->val_seq);
321
322 ret = ttm_bo_wait_unreserved(bo, interruptible);
323
324 if (unlikely(ret))
325 return ret;
326 }
327
328 if ((bo->val_seq - sequence < (1 << 31)) || !bo->seq_valid)
329 wake_up = true;
330
331 /**
332 * Wake up waiters that may need to recheck for deadlock,
333 * if we decreased the sequence number.
334 */
335 bo->val_seq = sequence;
336 bo->seq_valid = true;
337 if (wake_up)
338 wake_up_all(&bo->event_queue);
339
340 return 0;
341}
342
343int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
344 bool interruptible, uint32_t sequence)
345{
346 struct ttm_bo_global *glob = bo->glob;
347 int put_count, ret;
348
349 ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, sequence);
350 if (likely(!ret)) {
351 spin_lock(&glob->lru_lock);
352 put_count = ttm_bo_del_from_lru(bo);
353 spin_unlock(&glob->lru_lock);
354 ttm_bo_list_ref_sub(bo, put_count, true);
355 }
356 return ret;
357}
358EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
359
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000360void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
361{
362 ttm_bo_add_to_lru(bo);
363 atomic_set(&bo->reserved, 0);
364 wake_up_all(&bo->event_queue);
365}
366
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200367void ttm_bo_unreserve(struct ttm_buffer_object *bo)
368{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200369 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200370
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200371 spin_lock(&glob->lru_lock);
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000372 ttm_bo_unreserve_locked(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200373 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200374}
375EXPORT_SYMBOL(ttm_bo_unreserve);
376
377/*
378 * Call bo->mutex locked.
379 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200380static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
381{
382 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200383 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200384 int ret = 0;
385 uint32_t page_flags = 0;
386
387 TTM_ASSERT_LOCKED(&bo->mutex);
388 bo->ttm = NULL;
389
Dave Airliead49f502009-07-10 22:36:26 +1000390 if (bdev->need_dma32)
391 page_flags |= TTM_PAGE_FLAG_DMA32;
392
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200393 switch (bo->type) {
394 case ttm_bo_type_device:
395 if (zero_alloc)
396 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
397 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400398 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
399 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200400 if (unlikely(bo->ttm == NULL))
401 ret = -ENOMEM;
402 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100403 case ttm_bo_type_sg:
404 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
405 page_flags | TTM_PAGE_FLAG_SG,
406 glob->dummy_read_page);
407 if (unlikely(bo->ttm == NULL)) {
408 ret = -ENOMEM;
409 break;
410 }
411 bo->ttm->sg = bo->sg;
412 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200413 default:
Joe Perches25d04792012-03-16 21:43:50 -0700414 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200415 ret = -EINVAL;
416 break;
417 }
418
419 return ret;
420}
421
422static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
423 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000424 bool evict, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000425 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200426{
427 struct ttm_bo_device *bdev = bo->bdev;
428 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
429 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
430 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
431 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
432 int ret = 0;
433
434 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100435 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
436 ret = ttm_mem_io_lock(old_man, true);
437 if (unlikely(ret != 0))
438 goto out_err;
439 ttm_bo_unmap_virtual_locked(bo);
440 ttm_mem_io_unlock(old_man);
441 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200442
443 /*
444 * Create and bind a ttm if required.
445 */
446
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000447 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
448 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000449 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
450 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000451 if (ret)
452 goto out_err;
453 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200454
455 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
456 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200457 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200458
459 if (mem->mem_type != TTM_PL_SYSTEM) {
460 ret = ttm_tt_bind(bo->ttm, mem);
461 if (ret)
462 goto out_err;
463 }
464
465 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000466 if (bdev->driver->move_notify)
467 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100468 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200469 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200470 goto moved;
471 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200472 }
473
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000474 if (bdev->driver->move_notify)
475 bdev->driver->move_notify(bo, mem);
476
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200477 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
478 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000479 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200480 else if (bdev->driver->move)
481 ret = bdev->driver->move(bo, evict, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000482 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200483 else
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000484 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200485
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000486 if (ret) {
487 if (bdev->driver->move_notify) {
488 struct ttm_mem_reg tmp_mem = *mem;
489 *mem = bo->mem;
490 bo->mem = tmp_mem;
491 bdev->driver->move_notify(bo, mem);
492 bo->mem = *mem;
Dave Airlie014b3442013-01-16 15:58:34 +1000493 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000494 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200495
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000496 goto out_err;
497 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500498
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200499moved:
500 if (bo->evicted) {
Rob Clark9804b5d2014-03-12 10:59:37 -0400501 if (bdev->driver->invalidate_caches) {
502 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
503 if (ret)
504 pr_err("Can not flush read caches\n");
505 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200506 bo->evicted = false;
507 }
508
509 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000510 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200511 bdev->man[bo->mem.mem_type].gpu_offset;
512 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100513 } else
514 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200515
516 return 0;
517
518out_err:
519 new_man = &bdev->man[bo->mem.mem_type];
520 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
521 ttm_tt_unbind(bo->ttm);
522 ttm_tt_destroy(bo->ttm);
523 bo->ttm = NULL;
524 }
525
526 return ret;
527}
528
529/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200530 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200531 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200532 * This is the place to put in driver specific hooks to release
533 * driver private resources.
534 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200535 */
536
537static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
538{
Jerome Glissedc97b342011-11-18 11:47:03 -0500539 if (bo->bdev->driver->move_notify)
540 bo->bdev->driver->move_notify(bo, NULL);
541
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200542 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200543 ttm_tt_unbind(bo->ttm);
544 ttm_tt_destroy(bo->ttm);
545 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200546 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200547 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200548
549 atomic_set(&bo->reserved, 0);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000550 wake_up_all(&bo->event_queue);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200551
552 /*
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000553 * Since the final reference to this bo may not be dropped by
554 * the current task we have to put a memory barrier here to make
555 * sure the changes done in this function are always visible.
556 *
557 * This function only needs protection against the final kref_put.
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200558 */
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000559 smp_mb__before_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200560}
561
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200562static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200563{
564 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200565 struct ttm_bo_global *glob = bo->glob;
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100566 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000567 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200568 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200569 int ret;
570
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100571 spin_lock(&glob->lru_lock);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100572 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100573
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000574 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200575 (void) ttm_bo_wait(bo, false, false, true);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100576 if (!ret && !bo->sync_obj) {
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000577 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200578 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200579
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200580 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200581 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200582
Dave Airlied6ea8882010-11-22 13:24:40 +1000583 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200584
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200585 return;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200586 }
Thomas Hellstromaa123262010-11-02 13:21:47 +0000587 if (bo->sync_obj)
588 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100589 spin_unlock(&bdev->fence_lock);
590
591 if (!ret) {
592 atomic_set(&bo->reserved, 0);
593 wake_up_all(&bo->event_queue);
594 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200595
596 kref_get(&bo->list_kref);
597 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
598 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200599
Thomas Hellstromaa123262010-11-02 13:21:47 +0000600 if (sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +0000601 driver->sync_obj_flush(sync_obj);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000602 driver->sync_obj_unref(&sync_obj);
603 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200604 schedule_delayed_work(&bdev->wq,
605 ((HZ / 100) < 1) ? 1 : HZ / 100);
606}
607
608/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000609 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200610 * If bo idle, remove from delayed- and lru lists, and unref.
611 * If not idle, do nothing.
612 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000613 * Must be called with lru_lock and reservation held, this function
614 * will drop both before returning.
615 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200616 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200617 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
618 */
619
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000620static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
621 bool interruptible,
622 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200623{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000624 struct ttm_bo_device *bdev = bo->bdev;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000625 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200626 struct ttm_bo_global *glob = bo->glob;
627 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000628 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200629
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000630 spin_lock(&bdev->fence_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000631 ret = ttm_bo_wait(bo, false, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200632
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000633 if (ret && !no_wait_gpu) {
634 void *sync_obj;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200635
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000636 /*
637 * Take a reference to the fence and unreserve,
638 * at this point the buffer should be dead, so
639 * no new sync objects can be attached.
640 */
Maarten Lankhorst0953e762012-12-19 18:21:10 +0100641 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000642 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100643
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200644 atomic_set(&bo->reserved, 0);
645 wake_up_all(&bo->event_queue);
646 spin_unlock(&glob->lru_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000647
648 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
649 driver->sync_obj_unref(&sync_obj);
650 if (ret)
651 return ret;
652
653 /*
654 * remove sync_obj with ttm_bo_wait, the wait should be
655 * finished, and no new wait object should have been added.
656 */
657 spin_lock(&bdev->fence_lock);
658 ret = ttm_bo_wait(bo, false, false, true);
659 WARN_ON(ret);
660 spin_unlock(&bdev->fence_lock);
661 if (ret)
662 return ret;
663
664 spin_lock(&glob->lru_lock);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100665 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000666
667 /*
668 * We raced, and lost, someone else holds the reservation now,
669 * and is probably busy in ttm_bo_cleanup_memtype_use.
670 *
671 * Even if it's not the case, because we finished waiting any
672 * delayed destruction would succeed, so just return success
673 * here.
674 */
675 if (ret) {
676 spin_unlock(&glob->lru_lock);
677 return 0;
678 }
679 } else
680 spin_unlock(&bdev->fence_lock);
681
682 if (ret || unlikely(list_empty(&bo->ddestroy))) {
683 atomic_set(&bo->reserved, 0);
684 wake_up_all(&bo->event_queue);
685 spin_unlock(&glob->lru_lock);
686 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200687 }
688
689 put_count = ttm_bo_del_from_lru(bo);
690 list_del_init(&bo->ddestroy);
691 ++put_count;
692
693 spin_unlock(&glob->lru_lock);
694 ttm_bo_cleanup_memtype_use(bo);
695
Dave Airlied6ea8882010-11-22 13:24:40 +1000696 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200697
698 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200699}
700
701/**
702 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
703 * encountered buffers.
704 */
705
706static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
707{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200708 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100709 struct ttm_buffer_object *entry = NULL;
710 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200711
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200712 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100713 if (list_empty(&bdev->ddestroy))
714 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200715
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100716 entry = list_first_entry(&bdev->ddestroy,
717 struct ttm_buffer_object, ddestroy);
718 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200719
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100720 for (;;) {
721 struct ttm_buffer_object *nentry = NULL;
722
723 if (entry->ddestroy.next != &bdev->ddestroy) {
724 nentry = list_first_entry(&entry->ddestroy,
725 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200726 kref_get(&nentry->list_kref);
727 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200728
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100729 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
730 if (remove_all && ret) {
731 spin_unlock(&glob->lru_lock);
732 ret = ttm_bo_reserve_nolru(entry, false, false,
733 false, 0);
734 spin_lock(&glob->lru_lock);
735 }
736
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000737 if (!ret)
738 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
739 !remove_all);
740 else
741 spin_unlock(&glob->lru_lock);
742
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200743 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100744 entry = nentry;
745
746 if (ret || !entry)
747 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200748
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200749 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100750 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200751 break;
752 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200753
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100754out_unlock:
755 spin_unlock(&glob->lru_lock);
756out:
757 if (entry)
758 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200759 return ret;
760}
761
762static void ttm_bo_delayed_workqueue(struct work_struct *work)
763{
764 struct ttm_bo_device *bdev =
765 container_of(work, struct ttm_bo_device, wq.work);
766
767 if (ttm_bo_delayed_delete(bdev, false)) {
768 schedule_delayed_work(&bdev->wq,
769 ((HZ / 100) < 1) ? 1 : HZ / 100);
770 }
771}
772
773static void ttm_bo_release(struct kref *kref)
774{
775 struct ttm_buffer_object *bo =
776 container_of(kref, struct ttm_buffer_object, kref);
777 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100778 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200779
Thomas Hellstrom82fe50b2012-11-21 14:53:21 +0000780 write_lock(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200781 if (likely(bo->vm_node != NULL)) {
782 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
783 drm_mm_put_block(bo->vm_node);
784 bo->vm_node = NULL;
785 }
786 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100787 ttm_mem_io_lock(man, false);
788 ttm_mem_io_free_vm(bo);
789 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200790 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200791 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200792}
793
794void ttm_bo_unref(struct ttm_buffer_object **p_bo)
795{
796 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200797
798 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200799 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200800}
801EXPORT_SYMBOL(ttm_bo_unref);
802
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400803int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
804{
805 return cancel_delayed_work_sync(&bdev->wq);
806}
807EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
808
809void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
810{
811 if (resched)
812 schedule_delayed_work(&bdev->wq,
813 ((HZ / 100) < 1) ? 1 : HZ / 100);
814}
815EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
816
Jerome Glisseca262a9992009-12-08 15:33:32 +0100817static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000818 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200819{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200820 struct ttm_bo_device *bdev = bo->bdev;
821 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100822 struct ttm_placement placement;
823 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200824
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000825 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200826 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000827 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200828
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200829 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100830 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700831 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200832 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200833 goto out;
834 }
835
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200836 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200837
838 evict_mem = bo->mem;
839 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100840 evict_mem.bus.io_reserved_vm = false;
841 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200842
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100843 placement.fpfn = 0;
844 placement.lpfn = 0;
845 placement.num_placement = 0;
846 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100847 bdev->driver->evict_flags(bo, &placement);
848 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000849 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200850 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100851 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700852 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
853 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100854 ttm_bo_mem_space_debug(bo, &placement);
855 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200856 goto out;
857 }
858
859 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000860 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200861 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100862 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700863 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000864 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865 goto out;
866 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200867 bo->evicted = true;
868out:
869 return ret;
870}
871
Jerome Glisseca262a9992009-12-08 15:33:32 +0100872static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
873 uint32_t mem_type,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000874 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000875 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100876{
877 struct ttm_bo_global *glob = bdev->glob;
878 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
879 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000880 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100881
882 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000883 list_for_each_entry(bo, &man->lru, lru) {
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100884 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000885 if (!ret)
886 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100887 }
888
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000889 if (ret) {
890 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000891 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200892 }
893
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000894 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100895
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000896 if (!list_empty(&bo->ddestroy)) {
897 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
898 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100899 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000900 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100901 }
902
903 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100904 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100905
906 BUG_ON(ret != 0);
907
Dave Airlied6ea8882010-11-22 13:24:40 +1000908 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100909
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000910 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100911 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100912
Jerome Glisseca262a9992009-12-08 15:33:32 +0100913 kref_put(&bo->list_kref, ttm_bo_release_list);
914 return ret;
915}
916
Ben Skeggs42311ff2010-08-04 12:07:08 +1000917void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
918{
Ben Skeggsd961db72010-08-05 10:48:18 +1000919 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000920
Ben Skeggsd961db72010-08-05 10:48:18 +1000921 if (mem->mm_node)
922 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000923}
924EXPORT_SYMBOL(ttm_bo_mem_put);
925
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926/**
927 * Repeatedly evict memory from the LRU for @mem_type until we create enough
928 * space, or we've evicted everything and there isn't enough space.
929 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100930static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
931 uint32_t mem_type,
932 struct ttm_placement *placement,
933 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000934 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000935 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200936{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100937 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200939 int ret;
940
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200941 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000942 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200943 if (unlikely(ret != 0))
944 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000945 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100946 break;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000947 ret = ttm_mem_evict_first(bdev, mem_type,
948 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100949 if (unlikely(ret != 0))
950 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000952 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200954 mem->mem_type = mem_type;
955 return 0;
956}
957
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200958static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
959 uint32_t cur_placement,
960 uint32_t proposed_placement)
961{
962 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
963 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
964
965 /**
966 * Keep current caching if possible.
967 */
968
969 if ((cur_placement & caching) != 0)
970 result |= (cur_placement & caching);
971 else if ((man->default_caching & caching) != 0)
972 result |= man->default_caching;
973 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
974 result |= TTM_PL_FLAG_CACHED;
975 else if ((TTM_PL_FLAG_WC & caching) != 0)
976 result |= TTM_PL_FLAG_WC;
977 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
978 result |= TTM_PL_FLAG_UNCACHED;
979
980 return result;
981}
982
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200983static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200984 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200985 uint32_t proposed_placement,
986 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200987{
988 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
989
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200990 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200991 return false;
992
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200993 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200994 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200995
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200996 cur_flags |= (proposed_placement & man->available_caching);
997
998 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200999 return true;
1000}
1001
1002/**
1003 * Creates space for memory region @mem according to its type.
1004 *
1005 * This function first searches for free space in compatible memory types in
1006 * the priority order defined by the driver. If free space isn't found, then
1007 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1008 * space.
1009 */
1010int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001011 struct ttm_placement *placement,
1012 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001013 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001014 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015{
1016 struct ttm_bo_device *bdev = bo->bdev;
1017 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001018 uint32_t mem_type = TTM_PL_SYSTEM;
1019 uint32_t cur_flags = 0;
1020 bool type_found = false;
1021 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001022 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001023 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001024
1025 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +10001026 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001027 ret = ttm_mem_type_from_flags(placement->placement[i],
1028 &mem_type);
1029 if (ret)
1030 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001031 man = &bdev->man[mem_type];
1032
1033 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001034 mem_type,
1035 placement->placement[i],
1036 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001037
1038 if (!type_ok)
1039 continue;
1040
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001041 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1042 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001043 /*
1044 * Use the access and other non-mapping-related flag bits from
1045 * the memory placement flags to the current flags
1046 */
1047 ttm_flag_masked(&cur_flags, placement->placement[i],
1048 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001049
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001050 if (mem_type == TTM_PL_SYSTEM)
1051 break;
1052
1053 if (man->has_type && man->use_type) {
1054 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +10001055 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001056 if (unlikely(ret))
1057 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001058 }
Ben Skeggsd961db72010-08-05 10:48:18 +10001059 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001060 break;
1061 }
1062
Ben Skeggsd961db72010-08-05 10:48:18 +10001063 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001064 mem->mem_type = mem_type;
1065 mem->placement = cur_flags;
1066 return 0;
1067 }
1068
1069 if (!type_found)
1070 return -EINVAL;
1071
Dave Airlieb6637522009-12-14 14:51:35 +10001072 for (i = 0; i < placement->num_busy_placement; ++i) {
1073 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001074 &mem_type);
1075 if (ret)
1076 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001078 if (!man->has_type)
1079 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001080 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001081 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001082 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001083 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001084 continue;
1085
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001086 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1087 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001088 /*
1089 * Use the access and other non-mapping-related flag bits from
1090 * the memory placement flags to the current flags
1091 */
Dave Airlieb6637522009-12-14 14:51:35 +10001092 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001093 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001094
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001095
1096 if (mem_type == TTM_PL_SYSTEM) {
1097 mem->mem_type = mem_type;
1098 mem->placement = cur_flags;
1099 mem->mm_node = NULL;
1100 return 0;
1101 }
1102
Jerome Glisseca262a9992009-12-08 15:33:32 +01001103 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001104 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001105 if (ret == 0 && mem->mm_node) {
1106 mem->placement = cur_flags;
1107 return 0;
1108 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001109 if (ret == -ERESTARTSYS)
1110 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001111 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001112 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113 return ret;
1114}
1115EXPORT_SYMBOL(ttm_bo_mem_space);
1116
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001117int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001118 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001119 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001120 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001121{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001122 int ret = 0;
1123 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001124 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001125
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001126 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001127
1128 /*
1129 * FIXME: It's possible to pipeline buffer moves.
1130 * Have the driver move function wait for idle when necessary,
1131 * instead of doing it here.
1132 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001133 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001134 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001135 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001136 if (ret)
1137 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001138 mem.num_pages = bo->num_pages;
1139 mem.size = mem.num_pages << PAGE_SHIFT;
1140 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001141 mem.bus.io_reserved_vm = false;
1142 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143 /*
1144 * Determine where to move the buffer.
1145 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001146 ret = ttm_bo_mem_space(bo, placement, &mem,
1147 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001148 if (ret)
1149 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001150 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1151 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001153 if (ret && mem.mm_node)
1154 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001155 return ret;
1156}
1157
Thomas Hellstrom4f94b6e2013-10-28 02:02:19 -07001158static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1159 struct ttm_mem_reg *mem,
1160 uint32_t *new_flags)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001162 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001163
Ben Skeggsd961db72010-08-05 10:48:18 +10001164 if (mem->mm_node && placement->lpfn != 0 &&
1165 (mem->start < placement->fpfn ||
1166 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrom4f94b6e2013-10-28 02:02:19 -07001167 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001168
Jerome Glisseca262a9992009-12-08 15:33:32 +01001169 for (i = 0; i < placement->num_placement; i++) {
Thomas Hellstrom4f94b6e2013-10-28 02:02:19 -07001170 *new_flags = placement->placement[i];
1171 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1172 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1173 return true;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001174 }
Thomas Hellstrom4f94b6e2013-10-28 02:02:19 -07001175
1176 for (i = 0; i < placement->num_busy_placement; i++) {
1177 *new_flags = placement->busy_placement[i];
1178 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1179 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1180 return true;
1181 }
1182
1183 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001184}
1185
Jerome Glisse09855ac2009-12-10 17:16:27 +01001186int ttm_bo_validate(struct ttm_buffer_object *bo,
1187 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001188 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001189 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001190{
1191 int ret;
Thomas Hellstrom4f94b6e2013-10-28 02:02:19 -07001192 uint32_t new_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001193
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001194 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001195 /* Check that range is valid */
1196 if (placement->lpfn || placement->fpfn)
1197 if (placement->fpfn > placement->lpfn ||
1198 (placement->lpfn - placement->fpfn) < bo->num_pages)
1199 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200 /*
1201 * Check whether we need to move buffer.
1202 */
Thomas Hellstrom4f94b6e2013-10-28 02:02:19 -07001203 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001204 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1205 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001206 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001207 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001208 } else {
1209 /*
1210 * Use the access and other non-mapping-related flag bits from
1211 * the compatible memory placement flags to the active flags
1212 */
Thomas Hellstrom4f94b6e2013-10-28 02:02:19 -07001213 ttm_flag_masked(&bo->mem.placement, new_flags,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001214 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001215 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001216 /*
1217 * We might need to add a TTM.
1218 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1220 ret = ttm_bo_add_ttm(bo, true);
1221 if (ret)
1222 return ret;
1223 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001224 return 0;
1225}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001226EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001227
Jerome Glisse09855ac2009-12-10 17:16:27 +01001228int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1229 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001230{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001231 BUG_ON((placement->fpfn || placement->lpfn) &&
1232 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001233
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001234 return 0;
1235}
1236
Jerome Glisse09855ac2009-12-10 17:16:27 +01001237int ttm_bo_init(struct ttm_bo_device *bdev,
1238 struct ttm_buffer_object *bo,
1239 unsigned long size,
1240 enum ttm_bo_type type,
1241 struct ttm_placement *placement,
1242 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001243 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001244 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001245 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001246 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001247 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001248{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001249 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001250 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001251 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1252
1253 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1254 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001255 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001256 if (destroy)
1257 (*destroy)(bo);
1258 else
1259 kfree(bo);
1260 return -ENOMEM;
1261 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001262
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001263 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1264 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001265 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001266 if (destroy)
1267 (*destroy)(bo);
1268 else
1269 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001270 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001271 return -EINVAL;
1272 }
1273 bo->destroy = destroy;
1274
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001275 kref_init(&bo->kref);
1276 kref_init(&bo->list_kref);
1277 atomic_set(&bo->cpu_writers, 0);
1278 atomic_set(&bo->reserved, 1);
1279 init_waitqueue_head(&bo->event_queue);
1280 INIT_LIST_HEAD(&bo->lru);
1281 INIT_LIST_HEAD(&bo->ddestroy);
1282 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001283 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001284 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001285 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001286 bo->type = type;
1287 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001288 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001289 bo->mem.mem_type = TTM_PL_SYSTEM;
1290 bo->mem.num_pages = bo->num_pages;
1291 bo->mem.mm_node = NULL;
1292 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001293 bo->mem.bus.io_reserved_vm = false;
1294 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001295 bo->priv_flags = 0;
1296 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1297 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001298 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001299 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001300 bo->sg = sg;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001301 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302
Jerome Glisse09855ac2009-12-10 17:16:27 +01001303 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001304 if (unlikely(ret != 0))
1305 goto out_err;
1306
1307 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001308 * For ttm_bo_type_device buffers, allocate
1309 * address space from the device.
1310 */
Dave Airlie129b78b2012-04-02 11:46:06 +01001311 if (bo->type == ttm_bo_type_device ||
1312 bo->type == ttm_bo_type_sg) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001313 ret = ttm_bo_setup_vm(bo);
1314 if (ret)
1315 goto out_err;
1316 }
1317
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001318 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001319 if (ret)
1320 goto out_err;
1321
1322 ttm_bo_unreserve(bo);
1323 return 0;
1324
1325out_err:
1326 ttm_bo_unreserve(bo);
1327 ttm_bo_unref(&bo);
1328
1329 return ret;
1330}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001331EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001332
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001333size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1334 unsigned long bo_size,
1335 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001336{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001337 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1338 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001339
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001340 size += ttm_round_pot(struct_size);
1341 size += PAGE_ALIGN(npages * sizeof(void *));
1342 size += ttm_round_pot(sizeof(struct ttm_tt));
1343 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001344}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001345EXPORT_SYMBOL(ttm_bo_acc_size);
1346
1347size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1348 unsigned long bo_size,
1349 unsigned struct_size)
1350{
1351 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1352 size_t size = 0;
1353
1354 size += ttm_round_pot(struct_size);
1355 size += PAGE_ALIGN(npages * sizeof(void *));
1356 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1357 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1358 return size;
1359}
1360EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001361
Jerome Glisse09855ac2009-12-10 17:16:27 +01001362int ttm_bo_create(struct ttm_bo_device *bdev,
1363 unsigned long size,
1364 enum ttm_bo_type type,
1365 struct ttm_placement *placement,
1366 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001367 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001368 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001369 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001370{
1371 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001372 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001373 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001374
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001375 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001376 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001377 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001378
Thomas Hellstroma393c732012-06-12 13:28:42 +02001379 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001380 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001381 interruptible, persistent_swap_storage, acc_size,
1382 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001383 if (likely(ret == 0))
1384 *p_bo = bo;
1385
1386 return ret;
1387}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001388EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001389
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001390static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001391 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001392{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001393 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001394 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001395 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001396
1397 /*
1398 * Can't use standard list traversal since we're unlocking.
1399 */
1400
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001401 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001402 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001403 spin_unlock(&glob->lru_lock);
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001404 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001405 if (ret) {
1406 if (allow_errors) {
1407 return ret;
1408 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001409 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001410 }
1411 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001412 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001413 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001414 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001415 return 0;
1416}
1417
1418int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1419{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001420 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001421 int ret = -EINVAL;
1422
1423 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001424 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001425 return ret;
1426 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001427 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001428
1429 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001430 pr_err("Trying to take down uninitialized memory manager type %u\n",
1431 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001432 return ret;
1433 }
1434
1435 man->use_type = false;
1436 man->has_type = false;
1437
1438 ret = 0;
1439 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001440 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001441
Ben Skeggsd961db72010-08-05 10:48:18 +10001442 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001443 }
1444
1445 return ret;
1446}
1447EXPORT_SYMBOL(ttm_bo_clean_mm);
1448
1449int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1450{
1451 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1452
1453 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001454 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001455 return -EINVAL;
1456 }
1457
1458 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001459 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001460 return 0;
1461 }
1462
Jerome Glisseca262a9992009-12-08 15:33:32 +01001463 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001464}
1465EXPORT_SYMBOL(ttm_bo_evict_mm);
1466
1467int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001468 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001469{
1470 int ret = -EINVAL;
1471 struct ttm_mem_type_manager *man;
1472
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001473 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001474 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001475 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001476 man->io_reserve_fastpath = true;
1477 man->use_io_reserve_lru = false;
1478 mutex_init(&man->io_reserve_mutex);
1479 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001480
1481 ret = bdev->driver->init_mem_type(bdev, type, man);
1482 if (ret)
1483 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001484 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001485
1486 ret = 0;
1487 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001488 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001489 if (ret)
1490 return ret;
1491 }
1492 man->has_type = true;
1493 man->use_type = true;
1494 man->size = p_size;
1495
1496 INIT_LIST_HEAD(&man->lru);
1497
1498 return 0;
1499}
1500EXPORT_SYMBOL(ttm_bo_init_mm);
1501
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001502static void ttm_bo_global_kobj_release(struct kobject *kobj)
1503{
1504 struct ttm_bo_global *glob =
1505 container_of(kobj, struct ttm_bo_global, kobj);
1506
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001507 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1508 __free_page(glob->dummy_read_page);
1509 kfree(glob);
1510}
1511
Dave Airlieba4420c2010-03-09 10:56:52 +10001512void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001513{
1514 struct ttm_bo_global *glob = ref->object;
1515
1516 kobject_del(&glob->kobj);
1517 kobject_put(&glob->kobj);
1518}
1519EXPORT_SYMBOL(ttm_bo_global_release);
1520
Dave Airlieba4420c2010-03-09 10:56:52 +10001521int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001522{
1523 struct ttm_bo_global_ref *bo_ref =
1524 container_of(ref, struct ttm_bo_global_ref, ref);
1525 struct ttm_bo_global *glob = ref->object;
1526 int ret;
1527
1528 mutex_init(&glob->device_list_mutex);
1529 spin_lock_init(&glob->lru_lock);
1530 glob->mem_glob = bo_ref->mem_glob;
1531 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1532
1533 if (unlikely(glob->dummy_read_page == NULL)) {
1534 ret = -ENOMEM;
1535 goto out_no_drp;
1536 }
1537
1538 INIT_LIST_HEAD(&glob->swap_lru);
1539 INIT_LIST_HEAD(&glob->device_list);
1540
1541 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1542 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1543 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001544 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001545 goto out_no_shrink;
1546 }
1547
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001548 atomic_set(&glob->bo_count, 0);
1549
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001550 ret = kobject_init_and_add(
1551 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001552 if (unlikely(ret != 0))
1553 kobject_put(&glob->kobj);
1554 return ret;
1555out_no_shrink:
1556 __free_page(glob->dummy_read_page);
1557out_no_drp:
1558 kfree(glob);
1559 return ret;
1560}
1561EXPORT_SYMBOL(ttm_bo_global_init);
1562
1563
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001564int ttm_bo_device_release(struct ttm_bo_device *bdev)
1565{
1566 int ret = 0;
1567 unsigned i = TTM_NUM_MEM_TYPES;
1568 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001569 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001570
1571 while (i--) {
1572 man = &bdev->man[i];
1573 if (man->has_type) {
1574 man->use_type = false;
1575 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1576 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001577 pr_err("DRM memory manager type %d is not clean\n",
1578 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001579 }
1580 man->has_type = false;
1581 }
1582 }
1583
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001584 mutex_lock(&glob->device_list_mutex);
1585 list_del(&bdev->device_list);
1586 mutex_unlock(&glob->device_list_mutex);
1587
Tejun Heof094cfc2010-12-24 15:59:06 +01001588 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001589
1590 while (ttm_bo_delayed_delete(bdev, true))
1591 ;
1592
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001593 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001594 if (list_empty(&bdev->ddestroy))
1595 TTM_DEBUG("Delayed destroy list was clean\n");
1596
1597 if (list_empty(&bdev->man[0].lru))
1598 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001599 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001600
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001601 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1602 write_lock(&bdev->vm_lock);
1603 drm_mm_takedown(&bdev->addr_space_mm);
1604 write_unlock(&bdev->vm_lock);
1605
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001606 return ret;
1607}
1608EXPORT_SYMBOL(ttm_bo_device_release);
1609
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001610int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001611 struct ttm_bo_global *glob,
1612 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001613 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001614 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001615{
1616 int ret = -EINVAL;
1617
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001618 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001619 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001620
1621 memset(bdev->man, 0, sizeof(bdev->man));
1622
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001623 /*
1624 * Initialize the system memory buffer type.
1625 * Other types need to be driver / IOCTL initialized.
1626 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001627 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001628 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001629 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001630
1631 bdev->addr_space_rb = RB_ROOT;
1632 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1633 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001634 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001635
1636 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001637 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001638 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001639 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001640 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001641 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001642 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001643 mutex_lock(&glob->device_list_mutex);
1644 list_add_tail(&bdev->device_list, &glob->device_list);
1645 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001646
1647 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001648out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001649 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001650out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001651 return ret;
1652}
1653EXPORT_SYMBOL(ttm_bo_device_init);
1654
1655/*
1656 * buffer object vm functions.
1657 */
1658
1659bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1660{
1661 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1662
1663 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1664 if (mem->mem_type == TTM_PL_SYSTEM)
1665 return false;
1666
1667 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1668 return false;
1669
1670 if (mem->placement & TTM_PL_FLAG_CACHED)
1671 return false;
1672 }
1673 return true;
1674}
1675
Thomas Hellstromeba67092010-11-11 09:41:57 +01001676void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001677{
1678 struct ttm_bo_device *bdev = bo->bdev;
1679 loff_t offset = (loff_t) bo->addr_space_offset;
1680 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1681
1682 if (!bdev->dev_mapping)
1683 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001684 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001685 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001686}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001687
1688void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1689{
1690 struct ttm_bo_device *bdev = bo->bdev;
1691 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1692
1693 ttm_mem_io_lock(man, false);
1694 ttm_bo_unmap_virtual_locked(bo);
1695 ttm_mem_io_unlock(man);
1696}
1697
1698
Dave Airliee024e112009-06-24 09:48:08 +10001699EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001700
1701static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1702{
1703 struct ttm_bo_device *bdev = bo->bdev;
1704 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1705 struct rb_node *parent = NULL;
1706 struct ttm_buffer_object *cur_bo;
1707 unsigned long offset = bo->vm_node->start;
1708 unsigned long cur_offset;
1709
1710 while (*cur) {
1711 parent = *cur;
1712 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1713 cur_offset = cur_bo->vm_node->start;
1714 if (offset < cur_offset)
1715 cur = &parent->rb_left;
1716 else if (offset > cur_offset)
1717 cur = &parent->rb_right;
1718 else
1719 BUG();
1720 }
1721
1722 rb_link_node(&bo->vm_rb, parent, cur);
1723 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1724}
1725
1726/**
1727 * ttm_bo_setup_vm:
1728 *
1729 * @bo: the buffer to allocate address space for
1730 *
1731 * Allocate address space in the drm device so that applications
1732 * can mmap the buffer and access the contents. This only
1733 * applies to ttm_bo_type_device objects as others are not
1734 * placed in the drm device address space.
1735 */
1736
1737static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1738{
1739 struct ttm_bo_device *bdev = bo->bdev;
1740 int ret;
1741
1742retry_pre_get:
1743 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1744 if (unlikely(ret != 0))
1745 return ret;
1746
1747 write_lock(&bdev->vm_lock);
1748 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1749 bo->mem.num_pages, 0, 0);
1750
1751 if (unlikely(bo->vm_node == NULL)) {
1752 ret = -ENOMEM;
1753 goto out_unlock;
1754 }
1755
1756 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1757 bo->mem.num_pages, 0);
1758
1759 if (unlikely(bo->vm_node == NULL)) {
1760 write_unlock(&bdev->vm_lock);
1761 goto retry_pre_get;
1762 }
1763
1764 ttm_bo_vm_insert_rb(bo);
1765 write_unlock(&bdev->vm_lock);
1766 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1767
1768 return 0;
1769out_unlock:
1770 write_unlock(&bdev->vm_lock);
1771 return ret;
1772}
1773
1774int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001775 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001776{
1777 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001778 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001779 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001780 int ret = 0;
1781
Dave Airlie1717c0e2011-10-27 18:28:37 +02001782 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001783 return 0;
1784
Dave Airlie1717c0e2011-10-27 18:28:37 +02001785 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001786
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001787 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001788 void *tmp_obj = bo->sync_obj;
1789 bo->sync_obj = NULL;
1790 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1791 spin_unlock(&bdev->fence_lock);
1792 driver->sync_obj_unref(&tmp_obj);
1793 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001794 continue;
1795 }
1796
1797 if (no_wait)
1798 return -EBUSY;
1799
Dave Airlie1717c0e2011-10-27 18:28:37 +02001800 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001801 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001802 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001803 lazy, interruptible);
1804 if (unlikely(ret != 0)) {
1805 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001806 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001807 return ret;
1808 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001809 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001810 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001811 void *tmp_obj = bo->sync_obj;
1812 bo->sync_obj = NULL;
1813 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1814 &bo->priv_flags);
1815 spin_unlock(&bdev->fence_lock);
1816 driver->sync_obj_unref(&sync_obj);
1817 driver->sync_obj_unref(&tmp_obj);
1818 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001819 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001820 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001821 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001822 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001823 }
1824 }
1825 return 0;
1826}
1827EXPORT_SYMBOL(ttm_bo_wait);
1828
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001829int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1830{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001831 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001832 int ret = 0;
1833
1834 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001835 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001836 */
1837
1838 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1839 if (unlikely(ret != 0))
1840 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001841 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001842 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001843 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001844 if (likely(ret == 0))
1845 atomic_inc(&bo->cpu_writers);
1846 ttm_bo_unreserve(bo);
1847 return ret;
1848}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001849EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001850
1851void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1852{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001853 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001854}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001855EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001856
1857/**
1858 * A buffer object shrink method that tries to swap out the first
1859 * buffer object on the bo_global::swap_lru list.
1860 */
1861
1862static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1863{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001864 struct ttm_bo_global *glob =
1865 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001866 struct ttm_buffer_object *bo;
1867 int ret = -EBUSY;
1868 int put_count;
1869 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1870
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001871 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001872 list_for_each_entry(bo, &glob->swap_lru, swap) {
Maarten Lankhorst63d0a412013-01-15 14:56:37 +01001873 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001874 if (!ret)
1875 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001876 }
1877
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001878 if (ret) {
1879 spin_unlock(&glob->lru_lock);
1880 return ret;
1881 }
1882
1883 kref_get(&bo->list_kref);
1884
1885 if (!list_empty(&bo->ddestroy)) {
1886 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1887 kref_put(&bo->list_kref, ttm_bo_release_list);
1888 return ret;
1889 }
1890
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001891 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001892 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001893
Dave Airlied6ea8882010-11-22 13:24:40 +10001894 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001895
1896 /**
1897 * Wait for GPU, then move to system cached.
1898 */
1899
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001900 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001901 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001902 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001903
1904 if (unlikely(ret != 0))
1905 goto out;
1906
1907 if ((bo->mem.placement & swap_placement) != swap_placement) {
1908 struct ttm_mem_reg evict_mem;
1909
1910 evict_mem = bo->mem;
1911 evict_mem.mm_node = NULL;
1912 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1913 evict_mem.mem_type = TTM_PL_SYSTEM;
1914
1915 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001916 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001917 if (unlikely(ret != 0))
1918 goto out;
1919 }
1920
1921 ttm_bo_unmap_virtual(bo);
1922
1923 /**
1924 * Swap out. Buffer will be swapped in again as soon as
1925 * anyone tries to access a ttm page.
1926 */
1927
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001928 if (bo->bdev->driver->swap_notify)
1929 bo->bdev->driver->swap_notify(bo);
1930
Jan Engelhardt5df23972011-04-04 01:25:18 +02001931 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001932out:
1933
1934 /**
1935 *
1936 * Unreserve without putting on LRU to avoid swapping out an
1937 * already swapped buffer.
1938 */
1939
1940 atomic_set(&bo->reserved, 0);
1941 wake_up_all(&bo->event_queue);
1942 kref_put(&bo->list_kref, ttm_bo_release_list);
1943 return ret;
1944}
1945
1946void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1947{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001948 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001949 ;
1950}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001951EXPORT_SYMBOL(ttm_bo_swapout_all);