blob: 4cb3f493da76e33430e9f50c0171c93fe8adfefc [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
161int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
162{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200163 if (interruptible) {
Jean Delvare965d3802010-10-09 12:36:45 +0000164 return wait_event_interruptible(bo->event_queue,
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200165 !ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200166 } else {
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200167 wait_event(bo->event_queue, !ttm_bo_is_reserved(bo));
Jean Delvare965d3802010-10-09 12:36:45 +0000168 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200169 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200170}
Ben Skeggsd1ede142009-12-11 15:13:00 +1000171EXPORT_SYMBOL(ttm_bo_wait_unreserved);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200172
Dave Airlied6ea8882010-11-22 13:24:40 +1000173void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200174{
175 struct ttm_bo_device *bdev = bo->bdev;
176 struct ttm_mem_type_manager *man;
177
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
216int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
217 bool interruptible,
218 bool no_wait, bool use_sequence, uint32_t sequence)
219{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200220 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200221 int ret;
222
223 while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100224 /**
225 * Deadlock avoidance for multi-bo reserving.
226 */
Thomas Hellstrom96726fe2010-11-17 12:28:28 +0000227 if (use_sequence && bo->seq_valid) {
228 /**
229 * We've already reserved this one.
230 */
231 if (unlikely(sequence == bo->val_seq))
232 return -EDEADLK;
233 /**
234 * Already reserved by a thread that will not back
235 * off for us. We need to back off.
236 */
237 if (unlikely(sequence - bo->val_seq < (1 << 31)))
238 return -EAGAIN;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200239 }
240
241 if (no_wait)
242 return -EBUSY;
243
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200244 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200245 ret = ttm_bo_wait_unreserved(bo, interruptible);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200246 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200247
248 if (unlikely(ret))
249 return ret;
250 }
251
252 if (use_sequence) {
Thomas Hellstrom95ccb0f2010-11-11 10:04:53 +0100253 /**
254 * Wake up waiters that may need to recheck for deadlock,
255 * if we decreased the sequence number.
256 */
257 if (unlikely((bo->val_seq - sequence < (1 << 31))
258 || !bo->seq_valid))
259 wake_up_all(&bo->event_queue);
260
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200261 bo->val_seq = sequence;
262 bo->seq_valid = true;
263 } else {
264 bo->seq_valid = false;
265 }
266
267 return 0;
268}
269EXPORT_SYMBOL(ttm_bo_reserve);
270
271static void ttm_bo_ref_bug(struct kref *list_kref)
272{
273 BUG();
274}
275
Dave Airlied6ea8882010-11-22 13:24:40 +1000276void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
277 bool never_free)
278{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100279 kref_sub(&bo->list_kref, count,
280 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000281}
282
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200283int ttm_bo_reserve(struct ttm_buffer_object *bo,
284 bool interruptible,
285 bool no_wait, bool use_sequence, uint32_t sequence)
286{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200287 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200288 int put_count = 0;
289 int ret;
290
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200291 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200292 ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
293 sequence);
294 if (likely(ret == 0))
295 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200296 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200297
Dave Airlied6ea8882010-11-22 13:24:40 +1000298 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200299
300 return ret;
301}
302
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000303void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
304{
305 ttm_bo_add_to_lru(bo);
306 atomic_set(&bo->reserved, 0);
307 wake_up_all(&bo->event_queue);
308}
309
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200310void ttm_bo_unreserve(struct ttm_buffer_object *bo)
311{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200312 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200313
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200314 spin_lock(&glob->lru_lock);
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000315 ttm_bo_unreserve_locked(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200316 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200317}
318EXPORT_SYMBOL(ttm_bo_unreserve);
319
320/*
321 * Call bo->mutex locked.
322 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200323static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
324{
325 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200326 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200327 int ret = 0;
328 uint32_t page_flags = 0;
329
330 TTM_ASSERT_LOCKED(&bo->mutex);
331 bo->ttm = NULL;
332
Dave Airliead49f502009-07-10 22:36:26 +1000333 if (bdev->need_dma32)
334 page_flags |= TTM_PAGE_FLAG_DMA32;
335
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200336 switch (bo->type) {
337 case ttm_bo_type_device:
338 if (zero_alloc)
339 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
340 case ttm_bo_type_kernel:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400341 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
342 page_flags, glob->dummy_read_page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200343 if (unlikely(bo->ttm == NULL))
344 ret = -ENOMEM;
345 break;
Dave Airlie129b78b2012-04-02 11:46:06 +0100346 case ttm_bo_type_sg:
347 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
348 page_flags | TTM_PAGE_FLAG_SG,
349 glob->dummy_read_page);
350 if (unlikely(bo->ttm == NULL)) {
351 ret = -ENOMEM;
352 break;
353 }
354 bo->ttm->sg = bo->sg;
355 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200356 default:
Joe Perches25d04792012-03-16 21:43:50 -0700357 pr_err("Illegal buffer object type\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200358 ret = -EINVAL;
359 break;
360 }
361
362 return ret;
363}
364
365static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
366 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000367 bool evict, bool interruptible,
368 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200369{
370 struct ttm_bo_device *bdev = bo->bdev;
371 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
372 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
373 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
374 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
375 int ret = 0;
376
377 if (old_is_pci || new_is_pci ||
Thomas Hellstromeba67092010-11-11 09:41:57 +0100378 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
379 ret = ttm_mem_io_lock(old_man, true);
380 if (unlikely(ret != 0))
381 goto out_err;
382 ttm_bo_unmap_virtual_locked(bo);
383 ttm_mem_io_unlock(old_man);
384 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200385
386 /*
387 * Create and bind a ttm if required.
388 */
389
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000390 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
391 if (bo->ttm == NULL) {
Ben Skeggsff02b132011-09-14 06:08:06 +1000392 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
393 ret = ttm_bo_add_ttm(bo, zero);
Ben Skeggs8d3bb232011-08-22 03:15:05 +0000394 if (ret)
395 goto out_err;
396 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200397
398 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
399 if (ret)
Thomas Hellstrom87ef9202009-06-17 12:29:57 +0200400 goto out_err;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200401
402 if (mem->mem_type != TTM_PL_SYSTEM) {
403 ret = ttm_tt_bind(bo->ttm, mem);
404 if (ret)
405 goto out_err;
406 }
407
408 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
Ben Skeggs82ef5942011-02-02 00:27:10 +0000409 if (bdev->driver->move_notify)
410 bdev->driver->move_notify(bo, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100411 bo->mem = *mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200412 mem->mm_node = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200413 goto moved;
414 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200415 }
416
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000417 if (bdev->driver->move_notify)
418 bdev->driver->move_notify(bo, mem);
419
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200420 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
421 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000422 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200423 else if (bdev->driver->move)
424 ret = bdev->driver->move(bo, evict, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000425 no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200426 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000427 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200428
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000429 if (ret) {
430 if (bdev->driver->move_notify) {
431 struct ttm_mem_reg tmp_mem = *mem;
432 *mem = bo->mem;
433 bo->mem = tmp_mem;
434 bdev->driver->move_notify(bo, mem);
435 bo->mem = *mem;
436 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200437
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000438 goto out_err;
439 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500440
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200441moved:
442 if (bo->evicted) {
443 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
444 if (ret)
Joe Perches25d04792012-03-16 21:43:50 -0700445 pr_err("Can not flush read caches\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200446 bo->evicted = false;
447 }
448
449 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000450 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200451 bdev->man[bo->mem.mem_type].gpu_offset;
452 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100453 } else
454 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200455
456 return 0;
457
458out_err:
459 new_man = &bdev->man[bo->mem.mem_type];
460 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
461 ttm_tt_unbind(bo->ttm);
462 ttm_tt_destroy(bo->ttm);
463 bo->ttm = NULL;
464 }
465
466 return ret;
467}
468
469/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200470 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200471 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200472 * This is the place to put in driver specific hooks to release
473 * driver private resources.
474 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200475 */
476
477static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
478{
Jerome Glissedc97b342011-11-18 11:47:03 -0500479 if (bo->bdev->driver->move_notify)
480 bo->bdev->driver->move_notify(bo, NULL);
481
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200482 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200483 ttm_tt_unbind(bo->ttm);
484 ttm_tt_destroy(bo->ttm);
485 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200486 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200487 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200488
489 atomic_set(&bo->reserved, 0);
Thomas Hellstrom06fba6d2010-10-29 10:46:48 +0200490
491 /*
492 * Make processes trying to reserve really pick it up.
493 */
494 smp_mb__after_atomic_dec();
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200495 wake_up_all(&bo->event_queue);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200496}
497
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200498static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200499{
500 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200501 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200502 struct ttm_bo_driver *driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000503 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200504 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200505 int ret;
506
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000507 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200508 (void) ttm_bo_wait(bo, false, false, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200509 if (!bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200510
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200511 spin_lock(&glob->lru_lock);
Thomas Hellstromaaa20732009-12-02 18:33:45 +0100512
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200513 /**
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000514 * Lock inversion between bo:reserve and bdev::fence_lock here,
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200515 * but that's OK, since we're only trylocking.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200516 */
517
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200518 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200519
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200520 if (unlikely(ret == -EBUSY))
521 goto queue;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200522
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000523 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200524 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200525
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200526 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200527 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200528
Dave Airlied6ea8882010-11-22 13:24:40 +1000529 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200530
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200531 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200532 } else {
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200533 spin_lock(&glob->lru_lock);
534 }
535queue:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200536 driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000537 if (bo->sync_obj)
538 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200539
540 kref_get(&bo->list_kref);
541 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
542 spin_unlock(&glob->lru_lock);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000543 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200544
Thomas Hellstromaa123262010-11-02 13:21:47 +0000545 if (sync_obj) {
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +0000546 driver->sync_obj_flush(sync_obj, NULL);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000547 driver->sync_obj_unref(&sync_obj);
548 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200549 schedule_delayed_work(&bdev->wq,
550 ((HZ / 100) < 1) ? 1 : HZ / 100);
551}
552
553/**
554 * function ttm_bo_cleanup_refs
555 * If bo idle, remove from delayed- and lru lists, and unref.
556 * If not idle, do nothing.
557 *
558 * @interruptible Any sleeps should occur interruptibly.
559 * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
560 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
561 */
562
563static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
564 bool interruptible,
565 bool no_wait_reserve,
566 bool no_wait_gpu)
567{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000568 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200569 struct ttm_bo_global *glob = bo->glob;
570 int put_count;
571 int ret = 0;
572
573retry:
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000574 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200575 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000576 spin_unlock(&bdev->fence_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200577
578 if (unlikely(ret != 0))
579 return ret;
580
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000581retry_reserve:
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200582 spin_lock(&glob->lru_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100583
584 if (unlikely(list_empty(&bo->ddestroy))) {
585 spin_unlock(&glob->lru_lock);
586 return 0;
587 }
588
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000589 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200590
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000591 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200592 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000593 if (likely(!no_wait_reserve))
594 ret = ttm_bo_wait_unreserved(bo, interruptible);
595 if (unlikely(ret != 0))
596 return ret;
597
598 goto retry_reserve;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200599 }
600
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000601 BUG_ON(ret != 0);
602
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200603 /**
604 * We can re-check for sync object without taking
605 * the bo::lock since setting the sync object requires
606 * also bo::reserved. A busy object at this point may
607 * be caused by another thread recently starting an accelerated
608 * eviction.
609 */
610
611 if (unlikely(bo->sync_obj)) {
612 atomic_set(&bo->reserved, 0);
613 wake_up_all(&bo->event_queue);
614 spin_unlock(&glob->lru_lock);
615 goto retry;
616 }
617
618 put_count = ttm_bo_del_from_lru(bo);
619 list_del_init(&bo->ddestroy);
620 ++put_count;
621
622 spin_unlock(&glob->lru_lock);
623 ttm_bo_cleanup_memtype_use(bo);
624
Dave Airlied6ea8882010-11-22 13:24:40 +1000625 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200626
627 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200628}
629
630/**
631 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
632 * encountered buffers.
633 */
634
635static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
636{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200637 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100638 struct ttm_buffer_object *entry = NULL;
639 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200640
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200641 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100642 if (list_empty(&bdev->ddestroy))
643 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200644
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100645 entry = list_first_entry(&bdev->ddestroy,
646 struct ttm_buffer_object, ddestroy);
647 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200648
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100649 for (;;) {
650 struct ttm_buffer_object *nentry = NULL;
651
652 if (entry->ddestroy.next != &bdev->ddestroy) {
653 nentry = list_first_entry(&entry->ddestroy,
654 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200655 kref_get(&nentry->list_kref);
656 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200657
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200658 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200659 ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
660 !remove_all);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200661 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100662 entry = nentry;
663
664 if (ret || !entry)
665 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200666
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200667 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100668 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200669 break;
670 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200671
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100672out_unlock:
673 spin_unlock(&glob->lru_lock);
674out:
675 if (entry)
676 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200677 return ret;
678}
679
680static void ttm_bo_delayed_workqueue(struct work_struct *work)
681{
682 struct ttm_bo_device *bdev =
683 container_of(work, struct ttm_bo_device, wq.work);
684
685 if (ttm_bo_delayed_delete(bdev, false)) {
686 schedule_delayed_work(&bdev->wq,
687 ((HZ / 100) < 1) ? 1 : HZ / 100);
688 }
689}
690
691static void ttm_bo_release(struct kref *kref)
692{
693 struct ttm_buffer_object *bo =
694 container_of(kref, struct ttm_buffer_object, kref);
695 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100696 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200697
698 if (likely(bo->vm_node != NULL)) {
699 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
700 drm_mm_put_block(bo->vm_node);
701 bo->vm_node = NULL;
702 }
703 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100704 ttm_mem_io_lock(man, false);
705 ttm_mem_io_free_vm(bo);
706 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200707 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200708 kref_put(&bo->list_kref, ttm_bo_release_list);
709 write_lock(&bdev->vm_lock);
710}
711
712void ttm_bo_unref(struct ttm_buffer_object **p_bo)
713{
714 struct ttm_buffer_object *bo = *p_bo;
715 struct ttm_bo_device *bdev = bo->bdev;
716
717 *p_bo = NULL;
718 write_lock(&bdev->vm_lock);
719 kref_put(&bo->kref, ttm_bo_release);
720 write_unlock(&bdev->vm_lock);
721}
722EXPORT_SYMBOL(ttm_bo_unref);
723
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400724int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
725{
726 return cancel_delayed_work_sync(&bdev->wq);
727}
728EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
729
730void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
731{
732 if (resched)
733 schedule_delayed_work(&bdev->wq,
734 ((HZ / 100) < 1) ? 1 : HZ / 100);
735}
736EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
737
Jerome Glisseca262a9992009-12-08 15:33:32 +0100738static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000739 bool no_wait_reserve, bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200740{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200741 struct ttm_bo_device *bdev = bo->bdev;
742 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100743 struct ttm_placement placement;
744 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200745
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000746 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200747 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000748 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200749
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200750 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100751 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700752 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200753 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200754 goto out;
755 }
756
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200757 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200758
759 evict_mem = bo->mem;
760 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100761 evict_mem.bus.io_reserved_vm = false;
762 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200763
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100764 placement.fpfn = 0;
765 placement.lpfn = 0;
766 placement.num_placement = 0;
767 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100768 bdev->driver->evict_flags(bo, &placement);
769 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000770 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200771 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100772 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700773 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
774 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100775 ttm_bo_mem_space_debug(bo, &placement);
776 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200777 goto out;
778 }
779
780 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000781 no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200782 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100783 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700784 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000785 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200786 goto out;
787 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200788 bo->evicted = true;
789out:
790 return ret;
791}
792
Jerome Glisseca262a9992009-12-08 15:33:32 +0100793static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
794 uint32_t mem_type,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000795 bool interruptible, bool no_wait_reserve,
796 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100797{
798 struct ttm_bo_global *glob = bdev->glob;
799 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
800 struct ttm_buffer_object *bo;
801 int ret, put_count = 0;
802
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100803retry:
Jerome Glisseca262a9992009-12-08 15:33:32 +0100804 spin_lock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100805 if (list_empty(&man->lru)) {
806 spin_unlock(&glob->lru_lock);
807 return -EBUSY;
808 }
809
Jerome Glisseca262a9992009-12-08 15:33:32 +0100810 bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
811 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100812
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200813 if (!list_empty(&bo->ddestroy)) {
814 spin_unlock(&glob->lru_lock);
815 ret = ttm_bo_cleanup_refs(bo, interruptible,
816 no_wait_reserve, no_wait_gpu);
817 kref_put(&bo->list_kref, ttm_bo_release_list);
818
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000819 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200820 }
821
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000822 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100823
824 if (unlikely(ret == -EBUSY)) {
825 spin_unlock(&glob->lru_lock);
Thomas Hellstrom7bc17a72012-10-22 12:51:25 +0000826 if (likely(!no_wait_reserve))
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100827 ret = ttm_bo_wait_unreserved(bo, interruptible);
828
829 kref_put(&bo->list_kref, ttm_bo_release_list);
830
831 /**
832 * We *need* to retry after releasing the lru lock.
833 */
834
835 if (unlikely(ret != 0))
836 return ret;
837 goto retry;
838 }
839
840 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100841 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100842
843 BUG_ON(ret != 0);
844
Dave Airlied6ea8882010-11-22 13:24:40 +1000845 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100846
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000847 ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100848 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100849
Jerome Glisseca262a9992009-12-08 15:33:32 +0100850 kref_put(&bo->list_kref, ttm_bo_release_list);
851 return ret;
852}
853
Ben Skeggs42311ff2010-08-04 12:07:08 +1000854void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
855{
Ben Skeggsd961db72010-08-05 10:48:18 +1000856 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000857
Ben Skeggsd961db72010-08-05 10:48:18 +1000858 if (mem->mm_node)
859 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000860}
861EXPORT_SYMBOL(ttm_bo_mem_put);
862
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200863/**
864 * Repeatedly evict memory from the LRU for @mem_type until we create enough
865 * space, or we've evicted everything and there isn't enough space.
866 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100867static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
868 uint32_t mem_type,
869 struct ttm_placement *placement,
870 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000871 bool interruptible,
872 bool no_wait_reserve,
873 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200874{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100875 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200876 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200877 int ret;
878
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200879 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000880 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200881 if (unlikely(ret != 0))
882 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000883 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100884 break;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100885 ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000886 no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100887 if (unlikely(ret != 0))
888 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200889 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000890 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200891 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200892 mem->mem_type = mem_type;
893 return 0;
894}
895
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200896static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
897 uint32_t cur_placement,
898 uint32_t proposed_placement)
899{
900 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
901 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
902
903 /**
904 * Keep current caching if possible.
905 */
906
907 if ((cur_placement & caching) != 0)
908 result |= (cur_placement & caching);
909 else if ((man->default_caching & caching) != 0)
910 result |= man->default_caching;
911 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
912 result |= TTM_PL_FLAG_CACHED;
913 else if ((TTM_PL_FLAG_WC & caching) != 0)
914 result |= TTM_PL_FLAG_WC;
915 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
916 result |= TTM_PL_FLAG_UNCACHED;
917
918 return result;
919}
920
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200921static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200923 uint32_t proposed_placement,
924 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200925{
926 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
927
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200928 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200929 return false;
930
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200931 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200932 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200933
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200934 cur_flags |= (proposed_placement & man->available_caching);
935
936 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200937 return true;
938}
939
940/**
941 * Creates space for memory region @mem according to its type.
942 *
943 * This function first searches for free space in compatible memory types in
944 * the priority order defined by the driver. If free space isn't found, then
945 * ttm_bo_mem_force_space is attempted in priority order to evict and find
946 * space.
947 */
948int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100949 struct ttm_placement *placement,
950 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000951 bool interruptible, bool no_wait_reserve,
952 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200953{
954 struct ttm_bo_device *bdev = bo->bdev;
955 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200956 uint32_t mem_type = TTM_PL_SYSTEM;
957 uint32_t cur_flags = 0;
958 bool type_found = false;
959 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100960 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100961 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200962
963 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000964 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100965 ret = ttm_mem_type_from_flags(placement->placement[i],
966 &mem_type);
967 if (ret)
968 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200969 man = &bdev->man[mem_type];
970
971 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100972 mem_type,
973 placement->placement[i],
974 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200975
976 if (!type_ok)
977 continue;
978
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200979 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
980 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100981 /*
982 * Use the access and other non-mapping-related flag bits from
983 * the memory placement flags to the current flags
984 */
985 ttm_flag_masked(&cur_flags, placement->placement[i],
986 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200987
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200988 if (mem_type == TTM_PL_SYSTEM)
989 break;
990
991 if (man->has_type && man->use_type) {
992 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000993 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100994 if (unlikely(ret))
995 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200996 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000997 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200998 break;
999 }
1000
Ben Skeggsd961db72010-08-05 10:48:18 +10001001 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001002 mem->mem_type = mem_type;
1003 mem->placement = cur_flags;
1004 return 0;
1005 }
1006
1007 if (!type_found)
1008 return -EINVAL;
1009
Dave Airlieb6637522009-12-14 14:51:35 +10001010 for (i = 0; i < placement->num_busy_placement; ++i) {
1011 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001012 &mem_type);
1013 if (ret)
1014 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001015 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001016 if (!man->has_type)
1017 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001018 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001019 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001020 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001021 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001022 continue;
1023
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001024 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1025 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001026 /*
1027 * Use the access and other non-mapping-related flag bits from
1028 * the memory placement flags to the current flags
1029 */
Dave Airlieb6637522009-12-14 14:51:35 +10001030 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001031 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001032
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001033
1034 if (mem_type == TTM_PL_SYSTEM) {
1035 mem->mem_type = mem_type;
1036 mem->placement = cur_flags;
1037 mem->mm_node = NULL;
1038 return 0;
1039 }
1040
Jerome Glisseca262a9992009-12-08 15:33:32 +01001041 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001042 interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001043 if (ret == 0 && mem->mm_node) {
1044 mem->placement = cur_flags;
1045 return 0;
1046 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001047 if (ret == -ERESTARTSYS)
1048 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001050 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001051 return ret;
1052}
1053EXPORT_SYMBOL(ttm_bo_mem_space);
1054
1055int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
1056{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001057 if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
1058 return -EBUSY;
1059
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001060 return wait_event_interruptible(bo->event_queue,
1061 atomic_read(&bo->cpu_writers) == 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001062}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001063EXPORT_SYMBOL(ttm_bo_wait_cpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001064
1065int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001066 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001067 bool interruptible, bool no_wait_reserve,
1068 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001069{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001070 int ret = 0;
1071 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001072 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001073
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001074 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001075
1076 /*
1077 * FIXME: It's possible to pipeline buffer moves.
1078 * Have the driver move function wait for idle when necessary,
1079 * instead of doing it here.
1080 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001081 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001082 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001083 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001084 if (ret)
1085 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086 mem.num_pages = bo->num_pages;
1087 mem.size = mem.num_pages << PAGE_SHIFT;
1088 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001089 mem.bus.io_reserved_vm = false;
1090 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001091 /*
1092 * Determine where to move the buffer.
1093 */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001094 ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001095 if (ret)
1096 goto out_unlock;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001097 ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001098out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001099 if (ret && mem.mm_node)
1100 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001101 return ret;
1102}
1103
Jerome Glisseca262a9992009-12-08 15:33:32 +01001104static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001105 struct ttm_mem_reg *mem)
1106{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001107 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001108
Ben Skeggsd961db72010-08-05 10:48:18 +10001109 if (mem->mm_node && placement->lpfn != 0 &&
1110 (mem->start < placement->fpfn ||
1111 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001112 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001113
Jerome Glisseca262a9992009-12-08 15:33:32 +01001114 for (i = 0; i < placement->num_placement; i++) {
1115 if ((placement->placement[i] & mem->placement &
1116 TTM_PL_MASK_CACHING) &&
1117 (placement->placement[i] & mem->placement &
1118 TTM_PL_MASK_MEM))
1119 return i;
1120 }
1121 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001122}
1123
Jerome Glisse09855ac2009-12-10 17:16:27 +01001124int ttm_bo_validate(struct ttm_buffer_object *bo,
1125 struct ttm_placement *placement,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001126 bool interruptible, bool no_wait_reserve,
1127 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001128{
1129 int ret;
1130
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001131 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001132 /* Check that range is valid */
1133 if (placement->lpfn || placement->fpfn)
1134 if (placement->fpfn > placement->lpfn ||
1135 (placement->lpfn - placement->fpfn) < bo->num_pages)
1136 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001137 /*
1138 * Check whether we need to move buffer.
1139 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001140 ret = ttm_bo_mem_compat(placement, &bo->mem);
1141 if (ret < 0) {
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001142 ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001143 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001144 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001145 } else {
1146 /*
1147 * Use the access and other non-mapping-related flag bits from
1148 * the compatible memory placement flags to the active flags
1149 */
1150 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1151 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 /*
1154 * We might need to add a TTM.
1155 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001156 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1157 ret = ttm_bo_add_ttm(bo, true);
1158 if (ret)
1159 return ret;
1160 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001161 return 0;
1162}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001163EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001164
Jerome Glisse09855ac2009-12-10 17:16:27 +01001165int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1166 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001167{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001168 BUG_ON((placement->fpfn || placement->lpfn) &&
1169 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001170
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001171 return 0;
1172}
1173
Jerome Glisse09855ac2009-12-10 17:16:27 +01001174int ttm_bo_init(struct ttm_bo_device *bdev,
1175 struct ttm_buffer_object *bo,
1176 unsigned long size,
1177 enum ttm_bo_type type,
1178 struct ttm_placement *placement,
1179 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001180 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001181 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001182 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001183 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001184 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001185{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001186 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001187 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001188 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1189
1190 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1191 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001192 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001193 if (destroy)
1194 (*destroy)(bo);
1195 else
1196 kfree(bo);
1197 return -ENOMEM;
1198 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001199
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001200 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1201 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001202 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001203 if (destroy)
1204 (*destroy)(bo);
1205 else
1206 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001207 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001208 return -EINVAL;
1209 }
1210 bo->destroy = destroy;
1211
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001212 kref_init(&bo->kref);
1213 kref_init(&bo->list_kref);
1214 atomic_set(&bo->cpu_writers, 0);
1215 atomic_set(&bo->reserved, 1);
1216 init_waitqueue_head(&bo->event_queue);
1217 INIT_LIST_HEAD(&bo->lru);
1218 INIT_LIST_HEAD(&bo->ddestroy);
1219 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001220 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001222 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223 bo->type = type;
1224 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001225 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001226 bo->mem.mem_type = TTM_PL_SYSTEM;
1227 bo->mem.num_pages = bo->num_pages;
1228 bo->mem.mm_node = NULL;
1229 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001230 bo->mem.bus.io_reserved_vm = false;
1231 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001232 bo->priv_flags = 0;
1233 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1234 bo->seq_valid = false;
Jan Engelhardt5df23972011-04-04 01:25:18 +02001235 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001236 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001237 bo->sg = sg;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001238 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001239
Jerome Glisse09855ac2009-12-10 17:16:27 +01001240 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001241 if (unlikely(ret != 0))
1242 goto out_err;
1243
1244 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001245 * For ttm_bo_type_device buffers, allocate
1246 * address space from the device.
1247 */
Dave Airlie129b78b2012-04-02 11:46:06 +01001248 if (bo->type == ttm_bo_type_device ||
1249 bo->type == ttm_bo_type_sg) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001250 ret = ttm_bo_setup_vm(bo);
1251 if (ret)
1252 goto out_err;
1253 }
1254
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001255 ret = ttm_bo_validate(bo, placement, interruptible, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001256 if (ret)
1257 goto out_err;
1258
1259 ttm_bo_unreserve(bo);
1260 return 0;
1261
1262out_err:
1263 ttm_bo_unreserve(bo);
1264 ttm_bo_unref(&bo);
1265
1266 return ret;
1267}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001268EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001269
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001270size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1271 unsigned long bo_size,
1272 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001273{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001274 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1275 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001276
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001277 size += ttm_round_pot(struct_size);
1278 size += PAGE_ALIGN(npages * sizeof(void *));
1279 size += ttm_round_pot(sizeof(struct ttm_tt));
1280 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001281}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001282EXPORT_SYMBOL(ttm_bo_acc_size);
1283
1284size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1285 unsigned long bo_size,
1286 unsigned struct_size)
1287{
1288 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1289 size_t size = 0;
1290
1291 size += ttm_round_pot(struct_size);
1292 size += PAGE_ALIGN(npages * sizeof(void *));
1293 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1294 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1295 return size;
1296}
1297EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298
Jerome Glisse09855ac2009-12-10 17:16:27 +01001299int ttm_bo_create(struct ttm_bo_device *bdev,
1300 unsigned long size,
1301 enum ttm_bo_type type,
1302 struct ttm_placement *placement,
1303 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001304 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001305 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001306 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001307{
1308 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001309 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001310 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001311
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001312 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001313 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001314 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001315
Thomas Hellstroma393c732012-06-12 13:28:42 +02001316 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001317 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001318 interruptible, persistent_swap_storage, acc_size,
1319 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001320 if (likely(ret == 0))
1321 *p_bo = bo;
1322
1323 return ret;
1324}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001325EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001326
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001327static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001328 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001329{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001330 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001331 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001332 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001333
1334 /*
1335 * Can't use standard list traversal since we're unlocking.
1336 */
1337
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001338 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001339 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001340 spin_unlock(&glob->lru_lock);
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001341 ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001342 if (ret) {
1343 if (allow_errors) {
1344 return ret;
1345 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001346 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001347 }
1348 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001349 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001350 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001351 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001352 return 0;
1353}
1354
1355int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1356{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001357 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001358 int ret = -EINVAL;
1359
1360 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001361 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001362 return ret;
1363 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001364 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001365
1366 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001367 pr_err("Trying to take down uninitialized memory manager type %u\n",
1368 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001369 return ret;
1370 }
1371
1372 man->use_type = false;
1373 man->has_type = false;
1374
1375 ret = 0;
1376 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001377 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001378
Ben Skeggsd961db72010-08-05 10:48:18 +10001379 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001380 }
1381
1382 return ret;
1383}
1384EXPORT_SYMBOL(ttm_bo_clean_mm);
1385
1386int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1387{
1388 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1389
1390 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001391 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001392 return -EINVAL;
1393 }
1394
1395 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001396 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001397 return 0;
1398 }
1399
Jerome Glisseca262a9992009-12-08 15:33:32 +01001400 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001401}
1402EXPORT_SYMBOL(ttm_bo_evict_mm);
1403
1404int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001405 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001406{
1407 int ret = -EINVAL;
1408 struct ttm_mem_type_manager *man;
1409
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001410 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001411 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001412 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001413 man->io_reserve_fastpath = true;
1414 man->use_io_reserve_lru = false;
1415 mutex_init(&man->io_reserve_mutex);
1416 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001417
1418 ret = bdev->driver->init_mem_type(bdev, type, man);
1419 if (ret)
1420 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001421 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001422
1423 ret = 0;
1424 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001425 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001426 if (ret)
1427 return ret;
1428 }
1429 man->has_type = true;
1430 man->use_type = true;
1431 man->size = p_size;
1432
1433 INIT_LIST_HEAD(&man->lru);
1434
1435 return 0;
1436}
1437EXPORT_SYMBOL(ttm_bo_init_mm);
1438
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001439static void ttm_bo_global_kobj_release(struct kobject *kobj)
1440{
1441 struct ttm_bo_global *glob =
1442 container_of(kobj, struct ttm_bo_global, kobj);
1443
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001444 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1445 __free_page(glob->dummy_read_page);
1446 kfree(glob);
1447}
1448
Dave Airlieba4420c2010-03-09 10:56:52 +10001449void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001450{
1451 struct ttm_bo_global *glob = ref->object;
1452
1453 kobject_del(&glob->kobj);
1454 kobject_put(&glob->kobj);
1455}
1456EXPORT_SYMBOL(ttm_bo_global_release);
1457
Dave Airlieba4420c2010-03-09 10:56:52 +10001458int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001459{
1460 struct ttm_bo_global_ref *bo_ref =
1461 container_of(ref, struct ttm_bo_global_ref, ref);
1462 struct ttm_bo_global *glob = ref->object;
1463 int ret;
1464
1465 mutex_init(&glob->device_list_mutex);
1466 spin_lock_init(&glob->lru_lock);
1467 glob->mem_glob = bo_ref->mem_glob;
1468 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1469
1470 if (unlikely(glob->dummy_read_page == NULL)) {
1471 ret = -ENOMEM;
1472 goto out_no_drp;
1473 }
1474
1475 INIT_LIST_HEAD(&glob->swap_lru);
1476 INIT_LIST_HEAD(&glob->device_list);
1477
1478 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1479 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1480 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001481 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001482 goto out_no_shrink;
1483 }
1484
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001485 atomic_set(&glob->bo_count, 0);
1486
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001487 ret = kobject_init_and_add(
1488 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001489 if (unlikely(ret != 0))
1490 kobject_put(&glob->kobj);
1491 return ret;
1492out_no_shrink:
1493 __free_page(glob->dummy_read_page);
1494out_no_drp:
1495 kfree(glob);
1496 return ret;
1497}
1498EXPORT_SYMBOL(ttm_bo_global_init);
1499
1500
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001501int ttm_bo_device_release(struct ttm_bo_device *bdev)
1502{
1503 int ret = 0;
1504 unsigned i = TTM_NUM_MEM_TYPES;
1505 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001506 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001507
1508 while (i--) {
1509 man = &bdev->man[i];
1510 if (man->has_type) {
1511 man->use_type = false;
1512 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1513 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001514 pr_err("DRM memory manager type %d is not clean\n",
1515 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001516 }
1517 man->has_type = false;
1518 }
1519 }
1520
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001521 mutex_lock(&glob->device_list_mutex);
1522 list_del(&bdev->device_list);
1523 mutex_unlock(&glob->device_list_mutex);
1524
Tejun Heof094cfc2010-12-24 15:59:06 +01001525 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001526
1527 while (ttm_bo_delayed_delete(bdev, true))
1528 ;
1529
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001530 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001531 if (list_empty(&bdev->ddestroy))
1532 TTM_DEBUG("Delayed destroy list was clean\n");
1533
1534 if (list_empty(&bdev->man[0].lru))
1535 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001536 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001537
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1539 write_lock(&bdev->vm_lock);
1540 drm_mm_takedown(&bdev->addr_space_mm);
1541 write_unlock(&bdev->vm_lock);
1542
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001543 return ret;
1544}
1545EXPORT_SYMBOL(ttm_bo_device_release);
1546
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001547int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001548 struct ttm_bo_global *glob,
1549 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001550 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001551 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001552{
1553 int ret = -EINVAL;
1554
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001555 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001556 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001557
1558 memset(bdev->man, 0, sizeof(bdev->man));
1559
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001560 /*
1561 * Initialize the system memory buffer type.
1562 * Other types need to be driver / IOCTL initialized.
1563 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001564 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001565 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001566 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001567
1568 bdev->addr_space_rb = RB_ROOT;
1569 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1570 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001571 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001572
1573 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001574 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001575 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001576 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001577 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001578 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001579 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001580 mutex_lock(&glob->device_list_mutex);
1581 list_add_tail(&bdev->device_list, &glob->device_list);
1582 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001583
1584 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001585out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001586 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001587out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001588 return ret;
1589}
1590EXPORT_SYMBOL(ttm_bo_device_init);
1591
1592/*
1593 * buffer object vm functions.
1594 */
1595
1596bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1597{
1598 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1599
1600 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1601 if (mem->mem_type == TTM_PL_SYSTEM)
1602 return false;
1603
1604 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1605 return false;
1606
1607 if (mem->placement & TTM_PL_FLAG_CACHED)
1608 return false;
1609 }
1610 return true;
1611}
1612
Thomas Hellstromeba67092010-11-11 09:41:57 +01001613void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001614{
1615 struct ttm_bo_device *bdev = bo->bdev;
1616 loff_t offset = (loff_t) bo->addr_space_offset;
1617 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1618
1619 if (!bdev->dev_mapping)
1620 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001621 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001622 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001623}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001624
1625void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1626{
1627 struct ttm_bo_device *bdev = bo->bdev;
1628 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1629
1630 ttm_mem_io_lock(man, false);
1631 ttm_bo_unmap_virtual_locked(bo);
1632 ttm_mem_io_unlock(man);
1633}
1634
1635
Dave Airliee024e112009-06-24 09:48:08 +10001636EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001637
1638static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1639{
1640 struct ttm_bo_device *bdev = bo->bdev;
1641 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1642 struct rb_node *parent = NULL;
1643 struct ttm_buffer_object *cur_bo;
1644 unsigned long offset = bo->vm_node->start;
1645 unsigned long cur_offset;
1646
1647 while (*cur) {
1648 parent = *cur;
1649 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1650 cur_offset = cur_bo->vm_node->start;
1651 if (offset < cur_offset)
1652 cur = &parent->rb_left;
1653 else if (offset > cur_offset)
1654 cur = &parent->rb_right;
1655 else
1656 BUG();
1657 }
1658
1659 rb_link_node(&bo->vm_rb, parent, cur);
1660 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1661}
1662
1663/**
1664 * ttm_bo_setup_vm:
1665 *
1666 * @bo: the buffer to allocate address space for
1667 *
1668 * Allocate address space in the drm device so that applications
1669 * can mmap the buffer and access the contents. This only
1670 * applies to ttm_bo_type_device objects as others are not
1671 * placed in the drm device address space.
1672 */
1673
1674static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1675{
1676 struct ttm_bo_device *bdev = bo->bdev;
1677 int ret;
1678
1679retry_pre_get:
1680 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1681 if (unlikely(ret != 0))
1682 return ret;
1683
1684 write_lock(&bdev->vm_lock);
1685 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1686 bo->mem.num_pages, 0, 0);
1687
1688 if (unlikely(bo->vm_node == NULL)) {
1689 ret = -ENOMEM;
1690 goto out_unlock;
1691 }
1692
1693 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1694 bo->mem.num_pages, 0);
1695
1696 if (unlikely(bo->vm_node == NULL)) {
1697 write_unlock(&bdev->vm_lock);
1698 goto retry_pre_get;
1699 }
1700
1701 ttm_bo_vm_insert_rb(bo);
1702 write_unlock(&bdev->vm_lock);
1703 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1704
1705 return 0;
1706out_unlock:
1707 write_unlock(&bdev->vm_lock);
1708 return ret;
1709}
1710
1711int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001712 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001713{
1714 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001715 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001716 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001717 int ret = 0;
1718
Dave Airlie1717c0e2011-10-27 18:28:37 +02001719 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001720 return 0;
1721
Dave Airlie1717c0e2011-10-27 18:28:37 +02001722 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001723
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001724 if (driver->sync_obj_signaled(bo->sync_obj, NULL)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001725 void *tmp_obj = bo->sync_obj;
1726 bo->sync_obj = NULL;
1727 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1728 spin_unlock(&bdev->fence_lock);
1729 driver->sync_obj_unref(&tmp_obj);
1730 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001731 continue;
1732 }
1733
1734 if (no_wait)
1735 return -EBUSY;
1736
Dave Airlie1717c0e2011-10-27 18:28:37 +02001737 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001738 spin_unlock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001739 ret = driver->sync_obj_wait(sync_obj, NULL,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001740 lazy, interruptible);
1741 if (unlikely(ret != 0)) {
1742 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001743 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001744 return ret;
1745 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001746 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001747 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001748 void *tmp_obj = bo->sync_obj;
1749 bo->sync_obj = NULL;
1750 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1751 &bo->priv_flags);
1752 spin_unlock(&bdev->fence_lock);
1753 driver->sync_obj_unref(&sync_obj);
1754 driver->sync_obj_unref(&tmp_obj);
1755 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001756 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001757 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001758 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001759 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001760 }
1761 }
1762 return 0;
1763}
1764EXPORT_SYMBOL(ttm_bo_wait);
1765
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001766int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1767{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001768 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001769 int ret = 0;
1770
1771 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001772 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001773 */
1774
1775 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1776 if (unlikely(ret != 0))
1777 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001778 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001779 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001780 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001781 if (likely(ret == 0))
1782 atomic_inc(&bo->cpu_writers);
1783 ttm_bo_unreserve(bo);
1784 return ret;
1785}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001786EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001787
1788void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1789{
1790 if (atomic_dec_and_test(&bo->cpu_writers))
1791 wake_up_all(&bo->event_queue);
1792}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001793EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001794
1795/**
1796 * A buffer object shrink method that tries to swap out the first
1797 * buffer object on the bo_global::swap_lru list.
1798 */
1799
1800static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1801{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001802 struct ttm_bo_global *glob =
1803 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001804 struct ttm_buffer_object *bo;
1805 int ret = -EBUSY;
1806 int put_count;
1807 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1808
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001809 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001810 while (ret == -EBUSY) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001811 if (unlikely(list_empty(&glob->swap_lru))) {
1812 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001813 return -EBUSY;
1814 }
1815
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001816 bo = list_first_entry(&glob->swap_lru,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001817 struct ttm_buffer_object, swap);
1818 kref_get(&bo->list_kref);
1819
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001820 if (!list_empty(&bo->ddestroy)) {
1821 spin_unlock(&glob->lru_lock);
1822 (void) ttm_bo_cleanup_refs(bo, false, false, false);
1823 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma8ff3ee2012-06-01 15:39:11 +02001824 spin_lock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +02001825 continue;
1826 }
1827
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001828 /**
1829 * Reserve buffer. Since we unlock while sleeping, we need
1830 * to re-check that nobody removed us from the swap-list while
1831 * we slept.
1832 */
1833
1834 ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
1835 if (unlikely(ret == -EBUSY)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001836 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001837 ttm_bo_wait_unreserved(bo, false);
1838 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001839 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001840 }
1841 }
1842
1843 BUG_ON(ret != 0);
1844 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001845 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001846
Dave Airlied6ea8882010-11-22 13:24:40 +10001847 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001848
1849 /**
1850 * Wait for GPU, then move to system cached.
1851 */
1852
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001853 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001854 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001855 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001856
1857 if (unlikely(ret != 0))
1858 goto out;
1859
1860 if ((bo->mem.placement & swap_placement) != swap_placement) {
1861 struct ttm_mem_reg evict_mem;
1862
1863 evict_mem = bo->mem;
1864 evict_mem.mm_node = NULL;
1865 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1866 evict_mem.mem_type = TTM_PL_SYSTEM;
1867
1868 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001869 false, false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001870 if (unlikely(ret != 0))
1871 goto out;
1872 }
1873
1874 ttm_bo_unmap_virtual(bo);
1875
1876 /**
1877 * Swap out. Buffer will be swapped in again as soon as
1878 * anyone tries to access a ttm page.
1879 */
1880
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001881 if (bo->bdev->driver->swap_notify)
1882 bo->bdev->driver->swap_notify(bo);
1883
Jan Engelhardt5df23972011-04-04 01:25:18 +02001884 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001885out:
1886
1887 /**
1888 *
1889 * Unreserve without putting on LRU to avoid swapping out an
1890 * already swapped buffer.
1891 */
1892
1893 atomic_set(&bo->reserved, 0);
1894 wake_up_all(&bo->event_queue);
1895 kref_put(&bo->list_kref, ttm_bo_release_list);
1896 return ret;
1897}
1898
1899void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1900{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001901 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001902 ;
1903}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001904EXPORT_SYMBOL(ttm_bo_swapout_all);