blob: 4df47f72214a381a0d537ff170ccdf3a6fb4c5ce [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;
493 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200494
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000495 goto out_err;
496 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500497
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200498moved:
499 if (bo->evicted) {
500 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
501 if (ret)
Joe Perches25d04792012-03-16 21:43:50 -0700502 pr_err("Can not flush read caches\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200503 bo->evicted = false;
504 }
505
506 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000507 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200508 bdev->man[bo->mem.mem_type].gpu_offset;
509 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100510 } else
511 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200512
513 return 0;
514
515out_err:
516 new_man = &bdev->man[bo->mem.mem_type];
517 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
518 ttm_tt_unbind(bo->ttm);
519 ttm_tt_destroy(bo->ttm);
520 bo->ttm = NULL;
521 }
522
523 return ret;
524}
525
526/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200527 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200528 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200529 * This is the place to put in driver specific hooks to release
530 * driver private resources.
531 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200532 */
533
534static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
535{
Jerome Glissedc97b342011-11-18 11:47:03 -0500536 if (bo->bdev->driver->move_notify)
537 bo->bdev->driver->move_notify(bo, NULL);
538
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200539 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200540 ttm_tt_unbind(bo->ttm);
541 ttm_tt_destroy(bo->ttm);
542 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200543 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200544 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200545
546 atomic_set(&bo->reserved, 0);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000547 wake_up_all(&bo->event_queue);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200548
549 /*
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000550 * Since the final reference to this bo may not be dropped by
551 * the current task we have to put a memory barrier here to make
552 * sure the changes done in this function are always visible.
553 *
554 * This function only needs protection against the final kref_put.
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200555 */
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000556 smp_mb__before_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200557}
558
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200559static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200560{
561 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200562 struct ttm_bo_global *glob = bo->glob;
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100563 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000564 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200565 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200566 int ret;
567
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100568 spin_lock(&glob->lru_lock);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100569 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100570
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000571 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200572 (void) ttm_bo_wait(bo, false, false, true);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100573 if (!ret && !bo->sync_obj) {
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000574 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200575 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200576
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200577 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200578 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200579
Dave Airlied6ea8882010-11-22 13:24:40 +1000580 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200581
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200582 return;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200583 }
Thomas Hellstromaa123262010-11-02 13:21:47 +0000584 if (bo->sync_obj)
585 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100586 spin_unlock(&bdev->fence_lock);
587
588 if (!ret) {
589 atomic_set(&bo->reserved, 0);
590 wake_up_all(&bo->event_queue);
591 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200592
593 kref_get(&bo->list_kref);
594 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
595 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200596
Thomas Hellstromaa123262010-11-02 13:21:47 +0000597 if (sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +0000598 driver->sync_obj_flush(sync_obj);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000599 driver->sync_obj_unref(&sync_obj);
600 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200601 schedule_delayed_work(&bdev->wq,
602 ((HZ / 100) < 1) ? 1 : HZ / 100);
603}
604
605/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000606 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200607 * If bo idle, remove from delayed- and lru lists, and unref.
608 * If not idle, do nothing.
609 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000610 * Must be called with lru_lock and reservation held, this function
611 * will drop both before returning.
612 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200613 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200614 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
615 */
616
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000617static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
618 bool interruptible,
619 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200620{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000621 struct ttm_bo_device *bdev = bo->bdev;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000622 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200623 struct ttm_bo_global *glob = bo->glob;
624 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000625 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200626
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000627 spin_lock(&bdev->fence_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000628 ret = ttm_bo_wait(bo, false, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200629
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000630 if (ret && !no_wait_gpu) {
631 void *sync_obj;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200632
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000633 /*
634 * Take a reference to the fence and unreserve,
635 * at this point the buffer should be dead, so
636 * no new sync objects can be attached.
637 */
Maarten Lankhorst0953e762012-12-19 18:21:10 +0100638 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000639 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100640
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200641 atomic_set(&bo->reserved, 0);
642 wake_up_all(&bo->event_queue);
643 spin_unlock(&glob->lru_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000644
645 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
646 driver->sync_obj_unref(&sync_obj);
647 if (ret)
648 return ret;
649
650 /*
651 * remove sync_obj with ttm_bo_wait, the wait should be
652 * finished, and no new wait object should have been added.
653 */
654 spin_lock(&bdev->fence_lock);
655 ret = ttm_bo_wait(bo, false, false, true);
656 WARN_ON(ret);
657 spin_unlock(&bdev->fence_lock);
658 if (ret)
659 return ret;
660
661 spin_lock(&glob->lru_lock);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100662 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000663
664 /*
665 * We raced, and lost, someone else holds the reservation now,
666 * and is probably busy in ttm_bo_cleanup_memtype_use.
667 *
668 * Even if it's not the case, because we finished waiting any
669 * delayed destruction would succeed, so just return success
670 * here.
671 */
672 if (ret) {
673 spin_unlock(&glob->lru_lock);
674 return 0;
675 }
676 } else
677 spin_unlock(&bdev->fence_lock);
678
679 if (ret || unlikely(list_empty(&bo->ddestroy))) {
680 atomic_set(&bo->reserved, 0);
681 wake_up_all(&bo->event_queue);
682 spin_unlock(&glob->lru_lock);
683 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200684 }
685
686 put_count = ttm_bo_del_from_lru(bo);
687 list_del_init(&bo->ddestroy);
688 ++put_count;
689
690 spin_unlock(&glob->lru_lock);
691 ttm_bo_cleanup_memtype_use(bo);
692
Dave Airlied6ea8882010-11-22 13:24:40 +1000693 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200694
695 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200696}
697
698/**
699 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
700 * encountered buffers.
701 */
702
703static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
704{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200705 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100706 struct ttm_buffer_object *entry = NULL;
707 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200708
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200709 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100710 if (list_empty(&bdev->ddestroy))
711 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200712
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100713 entry = list_first_entry(&bdev->ddestroy,
714 struct ttm_buffer_object, ddestroy);
715 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200716
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100717 for (;;) {
718 struct ttm_buffer_object *nentry = NULL;
719
720 if (entry->ddestroy.next != &bdev->ddestroy) {
721 nentry = list_first_entry(&entry->ddestroy,
722 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200723 kref_get(&nentry->list_kref);
724 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200725
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100726 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
727 if (remove_all && ret) {
728 spin_unlock(&glob->lru_lock);
729 ret = ttm_bo_reserve_nolru(entry, false, false,
730 false, 0);
731 spin_lock(&glob->lru_lock);
732 }
733
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000734 if (!ret)
735 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
736 !remove_all);
737 else
738 spin_unlock(&glob->lru_lock);
739
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200740 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100741 entry = nentry;
742
743 if (ret || !entry)
744 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200745
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200746 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100747 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200748 break;
749 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200750
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100751out_unlock:
752 spin_unlock(&glob->lru_lock);
753out:
754 if (entry)
755 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200756 return ret;
757}
758
759static void ttm_bo_delayed_workqueue(struct work_struct *work)
760{
761 struct ttm_bo_device *bdev =
762 container_of(work, struct ttm_bo_device, wq.work);
763
764 if (ttm_bo_delayed_delete(bdev, false)) {
765 schedule_delayed_work(&bdev->wq,
766 ((HZ / 100) < 1) ? 1 : HZ / 100);
767 }
768}
769
770static void ttm_bo_release(struct kref *kref)
771{
772 struct ttm_buffer_object *bo =
773 container_of(kref, struct ttm_buffer_object, kref);
774 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100775 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200776
Thomas Hellstrom82fe50b2012-11-21 14:53:21 +0000777 write_lock(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200778 if (likely(bo->vm_node != NULL)) {
779 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
780 drm_mm_put_block(bo->vm_node);
781 bo->vm_node = NULL;
782 }
783 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100784 ttm_mem_io_lock(man, false);
785 ttm_mem_io_free_vm(bo);
786 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200787 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200788 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200789}
790
791void ttm_bo_unref(struct ttm_buffer_object **p_bo)
792{
793 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200794
795 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200796 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200797}
798EXPORT_SYMBOL(ttm_bo_unref);
799
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400800int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
801{
802 return cancel_delayed_work_sync(&bdev->wq);
803}
804EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
805
806void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
807{
808 if (resched)
809 schedule_delayed_work(&bdev->wq,
810 ((HZ / 100) < 1) ? 1 : HZ / 100);
811}
812EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
813
Jerome Glisseca262a9992009-12-08 15:33:32 +0100814static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000815 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200816{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200817 struct ttm_bo_device *bdev = bo->bdev;
818 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100819 struct ttm_placement placement;
820 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200821
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000822 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200823 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000824 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200825
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200826 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100827 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700828 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200829 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200830 goto out;
831 }
832
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200833 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200834
835 evict_mem = bo->mem;
836 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100837 evict_mem.bus.io_reserved_vm = false;
838 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200839
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100840 placement.fpfn = 0;
841 placement.lpfn = 0;
842 placement.num_placement = 0;
843 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100844 bdev->driver->evict_flags(bo, &placement);
845 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000846 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200847 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100848 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700849 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
850 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100851 ttm_bo_mem_space_debug(bo, &placement);
852 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200853 goto out;
854 }
855
856 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000857 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200858 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100859 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700860 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000861 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200862 goto out;
863 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200864 bo->evicted = true;
865out:
866 return ret;
867}
868
Jerome Glisseca262a9992009-12-08 15:33:32 +0100869static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
870 uint32_t mem_type,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000871 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000872 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100873{
874 struct ttm_bo_global *glob = bdev->glob;
875 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
876 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000877 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100878
879 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000880 list_for_each_entry(bo, &man->lru, lru) {
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100881 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000882 if (!ret)
883 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100884 }
885
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000886 if (ret) {
887 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000888 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200889 }
890
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000891 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100892
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000893 if (!list_empty(&bo->ddestroy)) {
894 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
895 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100896 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000897 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100898 }
899
900 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100901 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100902
903 BUG_ON(ret != 0);
904
Dave Airlied6ea8882010-11-22 13:24:40 +1000905 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100906
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000907 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100908 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100909
Jerome Glisseca262a9992009-12-08 15:33:32 +0100910 kref_put(&bo->list_kref, ttm_bo_release_list);
911 return ret;
912}
913
Ben Skeggs42311ff2010-08-04 12:07:08 +1000914void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
915{
Ben Skeggsd961db72010-08-05 10:48:18 +1000916 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000917
Ben Skeggsd961db72010-08-05 10:48:18 +1000918 if (mem->mm_node)
919 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000920}
921EXPORT_SYMBOL(ttm_bo_mem_put);
922
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200923/**
924 * Repeatedly evict memory from the LRU for @mem_type until we create enough
925 * space, or we've evicted everything and there isn't enough space.
926 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100927static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
928 uint32_t mem_type,
929 struct ttm_placement *placement,
930 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000931 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000932 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200933{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100934 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200935 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200936 int ret;
937
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200938 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000939 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200940 if (unlikely(ret != 0))
941 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000942 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100943 break;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000944 ret = ttm_mem_evict_first(bdev, mem_type,
945 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100946 if (unlikely(ret != 0))
947 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200948 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000949 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200950 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951 mem->mem_type = mem_type;
952 return 0;
953}
954
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200955static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
956 uint32_t cur_placement,
957 uint32_t proposed_placement)
958{
959 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
960 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
961
962 /**
963 * Keep current caching if possible.
964 */
965
966 if ((cur_placement & caching) != 0)
967 result |= (cur_placement & caching);
968 else if ((man->default_caching & caching) != 0)
969 result |= man->default_caching;
970 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
971 result |= TTM_PL_FLAG_CACHED;
972 else if ((TTM_PL_FLAG_WC & caching) != 0)
973 result |= TTM_PL_FLAG_WC;
974 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
975 result |= TTM_PL_FLAG_UNCACHED;
976
977 return result;
978}
979
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200980static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200981 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200982 uint32_t proposed_placement,
983 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200984{
985 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
986
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200987 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988 return false;
989
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200990 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200991 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200992
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200993 cur_flags |= (proposed_placement & man->available_caching);
994
995 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200996 return true;
997}
998
999/**
1000 * Creates space for memory region @mem according to its type.
1001 *
1002 * This function first searches for free space in compatible memory types in
1003 * the priority order defined by the driver. If free space isn't found, then
1004 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1005 * space.
1006 */
1007int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001008 struct ttm_placement *placement,
1009 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001010 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001011 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001012{
1013 struct ttm_bo_device *bdev = bo->bdev;
1014 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 uint32_t mem_type = TTM_PL_SYSTEM;
1016 uint32_t cur_flags = 0;
1017 bool type_found = false;
1018 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001019 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001020 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001021
1022 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +10001023 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001024 ret = ttm_mem_type_from_flags(placement->placement[i],
1025 &mem_type);
1026 if (ret)
1027 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001028 man = &bdev->man[mem_type];
1029
1030 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001031 mem_type,
1032 placement->placement[i],
1033 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001034
1035 if (!type_ok)
1036 continue;
1037
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001038 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1039 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001040 /*
1041 * Use the access and other non-mapping-related flag bits from
1042 * the memory placement flags to the current flags
1043 */
1044 ttm_flag_masked(&cur_flags, placement->placement[i],
1045 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001046
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001047 if (mem_type == TTM_PL_SYSTEM)
1048 break;
1049
1050 if (man->has_type && man->use_type) {
1051 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +10001052 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001053 if (unlikely(ret))
1054 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001055 }
Ben Skeggsd961db72010-08-05 10:48:18 +10001056 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001057 break;
1058 }
1059
Ben Skeggsd961db72010-08-05 10:48:18 +10001060 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001061 mem->mem_type = mem_type;
1062 mem->placement = cur_flags;
1063 return 0;
1064 }
1065
1066 if (!type_found)
1067 return -EINVAL;
1068
Dave Airlieb6637522009-12-14 14:51:35 +10001069 for (i = 0; i < placement->num_busy_placement; ++i) {
1070 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001071 &mem_type);
1072 if (ret)
1073 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001074 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001075 if (!man->has_type)
1076 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001077 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001078 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001079 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001080 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001081 continue;
1082
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001083 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1084 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001085 /*
1086 * Use the access and other non-mapping-related flag bits from
1087 * the memory placement flags to the current flags
1088 */
Dave Airlieb6637522009-12-14 14:51:35 +10001089 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001090 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001091
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001092
1093 if (mem_type == TTM_PL_SYSTEM) {
1094 mem->mem_type = mem_type;
1095 mem->placement = cur_flags;
1096 mem->mm_node = NULL;
1097 return 0;
1098 }
1099
Jerome Glisseca262a9992009-12-08 15:33:32 +01001100 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001101 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001102 if (ret == 0 && mem->mm_node) {
1103 mem->placement = cur_flags;
1104 return 0;
1105 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001106 if (ret == -ERESTARTSYS)
1107 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001108 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001109 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001110 return ret;
1111}
1112EXPORT_SYMBOL(ttm_bo_mem_space);
1113
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001114int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001115 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001116 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001117 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001118{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001119 int ret = 0;
1120 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001121 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001122
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001123 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001124
1125 /*
1126 * FIXME: It's possible to pipeline buffer moves.
1127 * Have the driver move function wait for idle when necessary,
1128 * instead of doing it here.
1129 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001130 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001131 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001132 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001133 if (ret)
1134 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 mem.num_pages = bo->num_pages;
1136 mem.size = mem.num_pages << PAGE_SHIFT;
1137 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001138 mem.bus.io_reserved_vm = false;
1139 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001140 /*
1141 * Determine where to move the buffer.
1142 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001143 ret = ttm_bo_mem_space(bo, placement, &mem,
1144 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001145 if (ret)
1146 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001147 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1148 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001150 if (ret && mem.mm_node)
1151 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152 return ret;
1153}
1154
Jerome Glisseca262a9992009-12-08 15:33:32 +01001155static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001156 struct ttm_mem_reg *mem)
1157{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001158 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001159
Ben Skeggsd961db72010-08-05 10:48:18 +10001160 if (mem->mm_node && placement->lpfn != 0 &&
1161 (mem->start < placement->fpfn ||
1162 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001163 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001164
Jerome Glisseca262a9992009-12-08 15:33:32 +01001165 for (i = 0; i < placement->num_placement; i++) {
1166 if ((placement->placement[i] & mem->placement &
1167 TTM_PL_MASK_CACHING) &&
1168 (placement->placement[i] & mem->placement &
1169 TTM_PL_MASK_MEM))
1170 return i;
1171 }
1172 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001173}
1174
Jerome Glisse09855ac2009-12-10 17:16:27 +01001175int ttm_bo_validate(struct ttm_buffer_object *bo,
1176 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001177 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001178 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001179{
1180 int ret;
1181
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001182 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001183 /* Check that range is valid */
1184 if (placement->lpfn || placement->fpfn)
1185 if (placement->fpfn > placement->lpfn ||
1186 (placement->lpfn - placement->fpfn) < bo->num_pages)
1187 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001188 /*
1189 * Check whether we need to move buffer.
1190 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001191 ret = ttm_bo_mem_compat(placement, &bo->mem);
1192 if (ret < 0) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001193 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1194 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001195 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001196 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001197 } else {
1198 /*
1199 * Use the access and other non-mapping-related flag bits from
1200 * the compatible memory placement flags to the active flags
1201 */
1202 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1203 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001204 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001205 /*
1206 * We might need to add a TTM.
1207 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001208 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1209 ret = ttm_bo_add_ttm(bo, true);
1210 if (ret)
1211 return ret;
1212 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001213 return 0;
1214}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001215EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001216
Jerome Glisse09855ac2009-12-10 17:16:27 +01001217int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1218 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001219{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001220 BUG_ON((placement->fpfn || placement->lpfn) &&
1221 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001222
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223 return 0;
1224}
1225
Jerome Glisse09855ac2009-12-10 17:16:27 +01001226int ttm_bo_init(struct ttm_bo_device *bdev,
1227 struct ttm_buffer_object *bo,
1228 unsigned long size,
1229 enum ttm_bo_type type,
1230 struct ttm_placement *placement,
1231 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001232 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001233 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001234 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001235 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001236 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001237{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001238 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001239 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001240 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1241
1242 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1243 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001244 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001245 if (destroy)
1246 (*destroy)(bo);
1247 else
1248 kfree(bo);
1249 return -ENOMEM;
1250 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001252 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1253 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001254 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001255 if (destroy)
1256 (*destroy)(bo);
1257 else
1258 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001259 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001260 return -EINVAL;
1261 }
1262 bo->destroy = destroy;
1263
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001264 kref_init(&bo->kref);
1265 kref_init(&bo->list_kref);
1266 atomic_set(&bo->cpu_writers, 0);
1267 atomic_set(&bo->reserved, 1);
1268 init_waitqueue_head(&bo->event_queue);
1269 INIT_LIST_HEAD(&bo->lru);
1270 INIT_LIST_HEAD(&bo->ddestroy);
1271 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001272 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001273 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001274 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001275 bo->type = type;
1276 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001277 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001278 bo->mem.mem_type = TTM_PL_SYSTEM;
1279 bo->mem.num_pages = bo->num_pages;
1280 bo->mem.mm_node = NULL;
1281 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001282 bo->mem.bus.io_reserved_vm = false;
1283 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001284 bo->priv_flags = 0;
1285 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1286 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001287 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001288 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001289 bo->sg = sg;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001290 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001291
Jerome Glisse09855ac2009-12-10 17:16:27 +01001292 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001293 if (unlikely(ret != 0))
1294 goto out_err;
1295
1296 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001297 * For ttm_bo_type_device buffers, allocate
1298 * address space from the device.
1299 */
Dave Airlie129b78b2012-04-02 11:46:06 +01001300 if (bo->type == ttm_bo_type_device ||
1301 bo->type == ttm_bo_type_sg) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001302 ret = ttm_bo_setup_vm(bo);
1303 if (ret)
1304 goto out_err;
1305 }
1306
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001307 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001308 if (ret)
1309 goto out_err;
1310
1311 ttm_bo_unreserve(bo);
1312 return 0;
1313
1314out_err:
1315 ttm_bo_unreserve(bo);
1316 ttm_bo_unref(&bo);
1317
1318 return ret;
1319}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001320EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001321
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001322size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1323 unsigned long bo_size,
1324 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001325{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001326 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1327 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001328
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001329 size += ttm_round_pot(struct_size);
1330 size += PAGE_ALIGN(npages * sizeof(void *));
1331 size += ttm_round_pot(sizeof(struct ttm_tt));
1332 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001333}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001334EXPORT_SYMBOL(ttm_bo_acc_size);
1335
1336size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1337 unsigned long bo_size,
1338 unsigned struct_size)
1339{
1340 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1341 size_t size = 0;
1342
1343 size += ttm_round_pot(struct_size);
1344 size += PAGE_ALIGN(npages * sizeof(void *));
1345 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1346 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1347 return size;
1348}
1349EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001350
Jerome Glisse09855ac2009-12-10 17:16:27 +01001351int ttm_bo_create(struct ttm_bo_device *bdev,
1352 unsigned long size,
1353 enum ttm_bo_type type,
1354 struct ttm_placement *placement,
1355 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001356 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001357 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001358 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001359{
1360 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001361 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001362 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001363
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001364 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001365 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001366 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001367
Thomas Hellstroma393c732012-06-12 13:28:42 +02001368 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001369 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001370 interruptible, persistent_swap_storage, acc_size,
1371 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001372 if (likely(ret == 0))
1373 *p_bo = bo;
1374
1375 return ret;
1376}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001377EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001378
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001379static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001380 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001381{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001382 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001383 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001384 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001385
1386 /*
1387 * Can't use standard list traversal since we're unlocking.
1388 */
1389
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001390 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001391 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001392 spin_unlock(&glob->lru_lock);
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001393 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001394 if (ret) {
1395 if (allow_errors) {
1396 return ret;
1397 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001398 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001399 }
1400 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001401 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001402 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001403 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001404 return 0;
1405}
1406
1407int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1408{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001409 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001410 int ret = -EINVAL;
1411
1412 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001413 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001414 return ret;
1415 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001416 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001417
1418 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001419 pr_err("Trying to take down uninitialized memory manager type %u\n",
1420 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001421 return ret;
1422 }
1423
1424 man->use_type = false;
1425 man->has_type = false;
1426
1427 ret = 0;
1428 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001429 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001430
Ben Skeggsd961db72010-08-05 10:48:18 +10001431 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001432 }
1433
1434 return ret;
1435}
1436EXPORT_SYMBOL(ttm_bo_clean_mm);
1437
1438int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1439{
1440 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1441
1442 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001443 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001444 return -EINVAL;
1445 }
1446
1447 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001448 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001449 return 0;
1450 }
1451
Jerome Glisseca262a9992009-12-08 15:33:32 +01001452 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001453}
1454EXPORT_SYMBOL(ttm_bo_evict_mm);
1455
1456int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001457 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001458{
1459 int ret = -EINVAL;
1460 struct ttm_mem_type_manager *man;
1461
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001462 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001463 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001464 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001465 man->io_reserve_fastpath = true;
1466 man->use_io_reserve_lru = false;
1467 mutex_init(&man->io_reserve_mutex);
1468 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001469
1470 ret = bdev->driver->init_mem_type(bdev, type, man);
1471 if (ret)
1472 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001473 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001474
1475 ret = 0;
1476 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001477 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001478 if (ret)
1479 return ret;
1480 }
1481 man->has_type = true;
1482 man->use_type = true;
1483 man->size = p_size;
1484
1485 INIT_LIST_HEAD(&man->lru);
1486
1487 return 0;
1488}
1489EXPORT_SYMBOL(ttm_bo_init_mm);
1490
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001491static void ttm_bo_global_kobj_release(struct kobject *kobj)
1492{
1493 struct ttm_bo_global *glob =
1494 container_of(kobj, struct ttm_bo_global, kobj);
1495
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001496 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1497 __free_page(glob->dummy_read_page);
1498 kfree(glob);
1499}
1500
Dave Airlieba4420c2010-03-09 10:56:52 +10001501void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001502{
1503 struct ttm_bo_global *glob = ref->object;
1504
1505 kobject_del(&glob->kobj);
1506 kobject_put(&glob->kobj);
1507}
1508EXPORT_SYMBOL(ttm_bo_global_release);
1509
Dave Airlieba4420c2010-03-09 10:56:52 +10001510int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001511{
1512 struct ttm_bo_global_ref *bo_ref =
1513 container_of(ref, struct ttm_bo_global_ref, ref);
1514 struct ttm_bo_global *glob = ref->object;
1515 int ret;
1516
1517 mutex_init(&glob->device_list_mutex);
1518 spin_lock_init(&glob->lru_lock);
1519 glob->mem_glob = bo_ref->mem_glob;
1520 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1521
1522 if (unlikely(glob->dummy_read_page == NULL)) {
1523 ret = -ENOMEM;
1524 goto out_no_drp;
1525 }
1526
1527 INIT_LIST_HEAD(&glob->swap_lru);
1528 INIT_LIST_HEAD(&glob->device_list);
1529
1530 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1531 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1532 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001533 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001534 goto out_no_shrink;
1535 }
1536
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001537 atomic_set(&glob->bo_count, 0);
1538
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001539 ret = kobject_init_and_add(
1540 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001541 if (unlikely(ret != 0))
1542 kobject_put(&glob->kobj);
1543 return ret;
1544out_no_shrink:
1545 __free_page(glob->dummy_read_page);
1546out_no_drp:
1547 kfree(glob);
1548 return ret;
1549}
1550EXPORT_SYMBOL(ttm_bo_global_init);
1551
1552
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553int ttm_bo_device_release(struct ttm_bo_device *bdev)
1554{
1555 int ret = 0;
1556 unsigned i = TTM_NUM_MEM_TYPES;
1557 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001558 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001559
1560 while (i--) {
1561 man = &bdev->man[i];
1562 if (man->has_type) {
1563 man->use_type = false;
1564 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1565 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001566 pr_err("DRM memory manager type %d is not clean\n",
1567 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001568 }
1569 man->has_type = false;
1570 }
1571 }
1572
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001573 mutex_lock(&glob->device_list_mutex);
1574 list_del(&bdev->device_list);
1575 mutex_unlock(&glob->device_list_mutex);
1576
Tejun Heof094cfc2010-12-24 15:59:06 +01001577 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001578
1579 while (ttm_bo_delayed_delete(bdev, true))
1580 ;
1581
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001582 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001583 if (list_empty(&bdev->ddestroy))
1584 TTM_DEBUG("Delayed destroy list was clean\n");
1585
1586 if (list_empty(&bdev->man[0].lru))
1587 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001588 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001589
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001590 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1591 write_lock(&bdev->vm_lock);
1592 drm_mm_takedown(&bdev->addr_space_mm);
1593 write_unlock(&bdev->vm_lock);
1594
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001595 return ret;
1596}
1597EXPORT_SYMBOL(ttm_bo_device_release);
1598
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001599int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001600 struct ttm_bo_global *glob,
1601 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001602 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001603 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001604{
1605 int ret = -EINVAL;
1606
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001607 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001608 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001609
1610 memset(bdev->man, 0, sizeof(bdev->man));
1611
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001612 /*
1613 * Initialize the system memory buffer type.
1614 * Other types need to be driver / IOCTL initialized.
1615 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001616 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001617 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001618 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001619
1620 bdev->addr_space_rb = RB_ROOT;
1621 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1622 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001623 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001624
1625 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001626 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001627 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001628 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001629 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001630 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001631 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001632 mutex_lock(&glob->device_list_mutex);
1633 list_add_tail(&bdev->device_list, &glob->device_list);
1634 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001635
1636 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001637out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001638 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001639out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001640 return ret;
1641}
1642EXPORT_SYMBOL(ttm_bo_device_init);
1643
1644/*
1645 * buffer object vm functions.
1646 */
1647
1648bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1649{
1650 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1651
1652 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1653 if (mem->mem_type == TTM_PL_SYSTEM)
1654 return false;
1655
1656 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1657 return false;
1658
1659 if (mem->placement & TTM_PL_FLAG_CACHED)
1660 return false;
1661 }
1662 return true;
1663}
1664
Thomas Hellstromeba67092010-11-11 09:41:57 +01001665void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001666{
1667 struct ttm_bo_device *bdev = bo->bdev;
1668 loff_t offset = (loff_t) bo->addr_space_offset;
1669 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1670
1671 if (!bdev->dev_mapping)
1672 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001673 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001674 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001675}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001676
1677void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1678{
1679 struct ttm_bo_device *bdev = bo->bdev;
1680 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1681
1682 ttm_mem_io_lock(man, false);
1683 ttm_bo_unmap_virtual_locked(bo);
1684 ttm_mem_io_unlock(man);
1685}
1686
1687
Dave Airliee024e112009-06-24 09:48:08 +10001688EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001689
1690static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1691{
1692 struct ttm_bo_device *bdev = bo->bdev;
1693 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1694 struct rb_node *parent = NULL;
1695 struct ttm_buffer_object *cur_bo;
1696 unsigned long offset = bo->vm_node->start;
1697 unsigned long cur_offset;
1698
1699 while (*cur) {
1700 parent = *cur;
1701 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1702 cur_offset = cur_bo->vm_node->start;
1703 if (offset < cur_offset)
1704 cur = &parent->rb_left;
1705 else if (offset > cur_offset)
1706 cur = &parent->rb_right;
1707 else
1708 BUG();
1709 }
1710
1711 rb_link_node(&bo->vm_rb, parent, cur);
1712 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1713}
1714
1715/**
1716 * ttm_bo_setup_vm:
1717 *
1718 * @bo: the buffer to allocate address space for
1719 *
1720 * Allocate address space in the drm device so that applications
1721 * can mmap the buffer and access the contents. This only
1722 * applies to ttm_bo_type_device objects as others are not
1723 * placed in the drm device address space.
1724 */
1725
1726static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1727{
1728 struct ttm_bo_device *bdev = bo->bdev;
1729 int ret;
1730
1731retry_pre_get:
1732 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1733 if (unlikely(ret != 0))
1734 return ret;
1735
1736 write_lock(&bdev->vm_lock);
1737 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1738 bo->mem.num_pages, 0, 0);
1739
1740 if (unlikely(bo->vm_node == NULL)) {
1741 ret = -ENOMEM;
1742 goto out_unlock;
1743 }
1744
1745 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1746 bo->mem.num_pages, 0);
1747
1748 if (unlikely(bo->vm_node == NULL)) {
1749 write_unlock(&bdev->vm_lock);
1750 goto retry_pre_get;
1751 }
1752
1753 ttm_bo_vm_insert_rb(bo);
1754 write_unlock(&bdev->vm_lock);
1755 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1756
1757 return 0;
1758out_unlock:
1759 write_unlock(&bdev->vm_lock);
1760 return ret;
1761}
1762
1763int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001764 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001765{
1766 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001767 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001768 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001769 int ret = 0;
1770
Dave Airlie1717c0e2011-10-27 18:28:37 +02001771 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001772 return 0;
1773
Dave Airlie1717c0e2011-10-27 18:28:37 +02001774 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001775
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001776 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001777 void *tmp_obj = bo->sync_obj;
1778 bo->sync_obj = NULL;
1779 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1780 spin_unlock(&bdev->fence_lock);
1781 driver->sync_obj_unref(&tmp_obj);
1782 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001783 continue;
1784 }
1785
1786 if (no_wait)
1787 return -EBUSY;
1788
Dave Airlie1717c0e2011-10-27 18:28:37 +02001789 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001790 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001791 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001792 lazy, interruptible);
1793 if (unlikely(ret != 0)) {
1794 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001795 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001796 return ret;
1797 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001798 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001799 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001800 void *tmp_obj = bo->sync_obj;
1801 bo->sync_obj = NULL;
1802 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1803 &bo->priv_flags);
1804 spin_unlock(&bdev->fence_lock);
1805 driver->sync_obj_unref(&sync_obj);
1806 driver->sync_obj_unref(&tmp_obj);
1807 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001808 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001809 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001810 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001811 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001812 }
1813 }
1814 return 0;
1815}
1816EXPORT_SYMBOL(ttm_bo_wait);
1817
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001818int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1819{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001820 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001821 int ret = 0;
1822
1823 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001824 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001825 */
1826
1827 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1828 if (unlikely(ret != 0))
1829 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001830 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001831 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001832 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001833 if (likely(ret == 0))
1834 atomic_inc(&bo->cpu_writers);
1835 ttm_bo_unreserve(bo);
1836 return ret;
1837}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001838EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001839
1840void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1841{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001842 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001843}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001844EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001845
1846/**
1847 * A buffer object shrink method that tries to swap out the first
1848 * buffer object on the bo_global::swap_lru list.
1849 */
1850
1851static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1852{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001853 struct ttm_bo_global *glob =
1854 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001855 struct ttm_buffer_object *bo;
1856 int ret = -EBUSY;
1857 int put_count;
1858 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1859
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001860 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001861 list_for_each_entry(bo, &glob->swap_lru, swap) {
Maarten Lankhorst63d0a412013-01-15 14:56:37 +01001862 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001863 if (!ret)
1864 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001865 }
1866
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001867 if (ret) {
1868 spin_unlock(&glob->lru_lock);
1869 return ret;
1870 }
1871
1872 kref_get(&bo->list_kref);
1873
1874 if (!list_empty(&bo->ddestroy)) {
1875 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1876 kref_put(&bo->list_kref, ttm_bo_release_list);
1877 return ret;
1878 }
1879
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001880 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001881 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001882
Dave Airlied6ea8882010-11-22 13:24:40 +10001883 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001884
1885 /**
1886 * Wait for GPU, then move to system cached.
1887 */
1888
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001889 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001890 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001891 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001892
1893 if (unlikely(ret != 0))
1894 goto out;
1895
1896 if ((bo->mem.placement & swap_placement) != swap_placement) {
1897 struct ttm_mem_reg evict_mem;
1898
1899 evict_mem = bo->mem;
1900 evict_mem.mm_node = NULL;
1901 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1902 evict_mem.mem_type = TTM_PL_SYSTEM;
1903
1904 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001905 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001906 if (unlikely(ret != 0))
1907 goto out;
1908 }
1909
1910 ttm_bo_unmap_virtual(bo);
1911
1912 /**
1913 * Swap out. Buffer will be swapped in again as soon as
1914 * anyone tries to access a ttm page.
1915 */
1916
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001917 if (bo->bdev->driver->swap_notify)
1918 bo->bdev->driver->swap_notify(bo);
1919
Jan Engelhardt5df23972011-04-04 01:25:18 +02001920 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001921out:
1922
1923 /**
1924 *
1925 * Unreserve without putting on LRU to avoid swapping out an
1926 * already swapped buffer.
1927 */
1928
1929 atomic_set(&bo->reserved, 0);
1930 wake_up_all(&bo->event_queue);
1931 kref_put(&bo->list_kref, ttm_bo_release_list);
1932 return ret;
1933}
1934
1935void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1936{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001937 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001938 ;
1939}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001940EXPORT_SYMBOL(ttm_bo_swapout_all);