blob: 5f9fe8044afca69949fc5d9283132560b090ac8b [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);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200153 if (bo->resv == &bo->ttm_resv)
154 reservation_object_fini(&bo->ttm_resv);
155
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200156 if (bo->destroy)
157 bo->destroy(bo);
158 else {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200159 kfree(bo);
160 }
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500161 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200162}
163
Dave Airlied6ea8882010-11-22 13:24:40 +1000164void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200165{
166 struct ttm_bo_device *bdev = bo->bdev;
167 struct ttm_mem_type_manager *man;
168
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200169 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200170
171 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
172
173 BUG_ON(!list_empty(&bo->lru));
174
175 man = &bdev->man[bo->mem.mem_type];
176 list_add_tail(&bo->lru, &man->lru);
177 kref_get(&bo->list_kref);
178
179 if (bo->ttm != NULL) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200180 list_add_tail(&bo->swap, &bo->glob->swap_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200181 kref_get(&bo->list_kref);
182 }
183 }
184}
185
Dave Airlied6ea8882010-11-22 13:24:40 +1000186int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200187{
188 int put_count = 0;
189
190 if (!list_empty(&bo->swap)) {
191 list_del_init(&bo->swap);
192 ++put_count;
193 }
194 if (!list_empty(&bo->lru)) {
195 list_del_init(&bo->lru);
196 ++put_count;
197 }
198
199 /*
200 * TODO: Add a driver hook to delete from
201 * driver-specific LRU's here.
202 */
203
204 return put_count;
205}
206
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100207int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200208 bool interruptible,
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200209 bool no_wait, bool use_ticket,
210 struct ww_acquire_ctx *ticket)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200211{
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200212 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200213
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200214 if (no_wait) {
215 bool success;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200216
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200217 /* not valid any more, fix your locking! */
218 if (WARN_ON(ticket))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200219 return -EBUSY;
220
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200221 success = ww_mutex_trylock(&bo->resv->lock);
222 return success ? 0 : -EBUSY;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200223 }
224
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200225 if (interruptible)
226 ret = ww_mutex_lock_interruptible(&bo->resv->lock,
227 ticket);
228 else
229 ret = ww_mutex_lock(&bo->resv->lock, ticket);
230 if (ret == -EINTR)
231 return -ERESTARTSYS;
232 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200233}
234EXPORT_SYMBOL(ttm_bo_reserve);
235
236static void ttm_bo_ref_bug(struct kref *list_kref)
237{
238 BUG();
239}
240
Dave Airlied6ea8882010-11-22 13:24:40 +1000241void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
242 bool never_free)
243{
Thomas Hellstrom2357cbe2010-11-16 15:21:08 +0100244 kref_sub(&bo->list_kref, count,
245 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
Dave Airlied6ea8882010-11-22 13:24:40 +1000246}
247
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200248int ttm_bo_reserve(struct ttm_buffer_object *bo,
249 bool interruptible,
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200250 bool no_wait, bool use_ticket,
251 struct ww_acquire_ctx *ticket)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200252{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200253 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254 int put_count = 0;
255 int ret;
256
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200257 ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_ticket,
258 ticket);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100259 if (likely(ret == 0)) {
260 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200261 put_count = ttm_bo_del_from_lru(bo);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100262 spin_unlock(&glob->lru_lock);
263 ttm_bo_list_ref_sub(bo, put_count, true);
264 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200265
266 return ret;
267}
268
Maarten Lankhorst5e45d7d2013-01-15 14:57:05 +0100269int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200270 bool interruptible, struct ww_acquire_ctx *ticket)
Maarten Lankhorst5e45d7d2013-01-15 14:57:05 +0100271{
272 struct ttm_bo_global *glob = bo->glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200273 int put_count = 0;
274 int ret = 0;
Maarten Lankhorst5e45d7d2013-01-15 14:57:05 +0100275
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200276 if (interruptible)
277 ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
278 ticket);
279 else
280 ww_mutex_lock_slow(&bo->resv->lock, ticket);
281
282 if (likely(ret == 0)) {
Maarten Lankhorst5e45d7d2013-01-15 14:57:05 +0100283 spin_lock(&glob->lru_lock);
284 put_count = ttm_bo_del_from_lru(bo);
285 spin_unlock(&glob->lru_lock);
286 ttm_bo_list_ref_sub(bo, put_count, true);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200287 } else if (ret == -EINTR)
288 ret = -ERESTARTSYS;
289
Maarten Lankhorst5e45d7d2013-01-15 14:57:05 +0100290 return ret;
291}
292EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
293
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200294void ttm_bo_unreserve_ticket_locked(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket)
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000295{
296 ttm_bo_add_to_lru(bo);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200297 ww_mutex_unlock(&bo->resv->lock);
Thomas Hellstrom95762c22010-11-17 12:28:30 +0000298}
299
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200300void ttm_bo_unreserve(struct ttm_buffer_object *bo)
301{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200302 struct ttm_bo_global *glob = bo->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200303
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200304 spin_lock(&glob->lru_lock);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200305 ttm_bo_unreserve_ticket_locked(bo, NULL);
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200306 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200307}
308EXPORT_SYMBOL(ttm_bo_unreserve);
309
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200310void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket)
311{
312 struct ttm_bo_global *glob = bo->glob;
313
314 spin_lock(&glob->lru_lock);
315 ttm_bo_unreserve_ticket_locked(bo, ticket);
316 spin_unlock(&glob->lru_lock);
317}
318EXPORT_SYMBOL(ttm_bo_unreserve_ticket);
319
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200320/*
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,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000368 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))
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000422 ret = ttm_bo_move_ttm(bo, evict, 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,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000425 no_wait_gpu, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200426 else
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000427 ret = ttm_bo_move_memcpy(bo, evict, 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;
Dave Airlie014b3442013-01-16 15:58:34 +1000436 *mem = tmp_mem;
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000437 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200438
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000439 goto out_err;
440 }
Jerome Glissedc97b342011-11-18 11:47:03 -0500441
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200442moved:
443 if (bo->evicted) {
444 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
445 if (ret)
Joe Perches25d04792012-03-16 21:43:50 -0700446 pr_err("Can not flush read caches\n");
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200447 bo->evicted = false;
448 }
449
450 if (bo->mem.mm_node) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000451 bo->offset = (bo->mem.start << PAGE_SHIFT) +
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200452 bdev->man[bo->mem.mem_type].gpu_offset;
453 bo->cur_placement = bo->mem.placement;
Thomas Hellstrom354fb522010-01-13 22:28:45 +0100454 } else
455 bo->offset = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200456
457 return 0;
458
459out_err:
460 new_man = &bdev->man[bo->mem.mem_type];
461 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
462 ttm_tt_unbind(bo->ttm);
463 ttm_tt_destroy(bo->ttm);
464 bo->ttm = NULL;
465 }
466
467 return ret;
468}
469
470/**
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200471 * Call bo::reserved.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200472 * Will release GPU memory type usage on destruction.
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200473 * This is the place to put in driver specific hooks to release
474 * driver private resources.
475 * Will release the bo::reserved lock.
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200476 */
477
478static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
479{
Jerome Glissedc97b342011-11-18 11:47:03 -0500480 if (bo->bdev->driver->move_notify)
481 bo->bdev->driver->move_notify(bo, NULL);
482
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200483 if (bo->ttm) {
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200484 ttm_tt_unbind(bo->ttm);
485 ttm_tt_destroy(bo->ttm);
486 bo->ttm = NULL;
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200487 }
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200488 ttm_bo_mem_put(bo, &bo->mem);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200489
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200490 ww_mutex_unlock (&bo->resv->lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200491}
492
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200493static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200494{
495 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200496 struct ttm_bo_global *glob = bo->glob;
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100497 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstromaa123262010-11-02 13:21:47 +0000498 void *sync_obj = NULL;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200499 int put_count;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200500 int ret;
501
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100502 spin_lock(&glob->lru_lock);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100503 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100504
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000505 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200506 (void) ttm_bo_wait(bo, false, false, true);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100507 if (!ret && !bo->sync_obj) {
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000508 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200509 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200510
Thomas Hellstrom40d857b2010-10-19 09:01:00 +0200511 spin_unlock(&glob->lru_lock);
Thomas Hellstrom1df6a2e2010-09-30 12:36:45 +0200512 ttm_bo_cleanup_memtype_use(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200513
Dave Airlied6ea8882010-11-22 13:24:40 +1000514 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200515
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200516 return;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200517 }
Thomas Hellstromaa123262010-11-02 13:21:47 +0000518 if (bo->sync_obj)
519 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst4154f052012-11-28 12:25:39 +0100520 spin_unlock(&bdev->fence_lock);
521
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200522 if (!ret)
523 ww_mutex_unlock(&bo->resv->lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200524
525 kref_get(&bo->list_kref);
526 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
527 spin_unlock(&glob->lru_lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200528
Thomas Hellstromaa123262010-11-02 13:21:47 +0000529 if (sync_obj) {
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +0000530 driver->sync_obj_flush(sync_obj);
Thomas Hellstromaa123262010-11-02 13:21:47 +0000531 driver->sync_obj_unref(&sync_obj);
532 }
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200533 schedule_delayed_work(&bdev->wq,
534 ((HZ / 100) < 1) ? 1 : HZ / 100);
535}
536
537/**
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000538 * function ttm_bo_cleanup_refs_and_unlock
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200539 * If bo idle, remove from delayed- and lru lists, and unref.
540 * If not idle, do nothing.
541 *
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000542 * Must be called with lru_lock and reservation held, this function
543 * will drop both before returning.
544 *
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200545 * @interruptible Any sleeps should occur interruptibly.
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200546 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
547 */
548
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000549static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
550 bool interruptible,
551 bool no_wait_gpu)
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200552{
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000553 struct ttm_bo_device *bdev = bo->bdev;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000554 struct ttm_bo_driver *driver = bdev->driver;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200555 struct ttm_bo_global *glob = bo->glob;
556 int put_count;
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000557 int ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200558
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000559 spin_lock(&bdev->fence_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000560 ret = ttm_bo_wait(bo, false, false, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200561
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000562 if (ret && !no_wait_gpu) {
563 void *sync_obj;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200564
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000565 /*
566 * Take a reference to the fence and unreserve,
567 * at this point the buffer should be dead, so
568 * no new sync objects can be attached.
569 */
Maarten Lankhorst0953e762012-12-19 18:21:10 +0100570 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000571 spin_unlock(&bdev->fence_lock);
Thomas Hellstrom26cc40a2011-11-21 13:05:02 +0100572
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200573 ww_mutex_unlock(&bo->resv->lock);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200574 spin_unlock(&glob->lru_lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000575
576 ret = driver->sync_obj_wait(sync_obj, false, interruptible);
577 driver->sync_obj_unref(&sync_obj);
578 if (ret)
579 return ret;
580
581 /*
582 * remove sync_obj with ttm_bo_wait, the wait should be
583 * finished, and no new wait object should have been added.
584 */
585 spin_lock(&bdev->fence_lock);
586 ret = ttm_bo_wait(bo, false, false, true);
587 WARN_ON(ret);
588 spin_unlock(&bdev->fence_lock);
589 if (ret)
590 return ret;
591
592 spin_lock(&glob->lru_lock);
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100593 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000594
595 /*
596 * We raced, and lost, someone else holds the reservation now,
597 * and is probably busy in ttm_bo_cleanup_memtype_use.
598 *
599 * Even if it's not the case, because we finished waiting any
600 * delayed destruction would succeed, so just return success
601 * here.
602 */
603 if (ret) {
604 spin_unlock(&glob->lru_lock);
605 return 0;
606 }
607 } else
608 spin_unlock(&bdev->fence_lock);
609
610 if (ret || unlikely(list_empty(&bo->ddestroy))) {
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200611 ww_mutex_unlock(&bo->resv->lock);
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000612 spin_unlock(&glob->lru_lock);
613 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200614 }
615
616 put_count = ttm_bo_del_from_lru(bo);
617 list_del_init(&bo->ddestroy);
618 ++put_count;
619
620 spin_unlock(&glob->lru_lock);
621 ttm_bo_cleanup_memtype_use(bo);
622
Dave Airlied6ea8882010-11-22 13:24:40 +1000623 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200624
625 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200626}
627
628/**
629 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
630 * encountered buffers.
631 */
632
633static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
634{
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200635 struct ttm_bo_global *glob = bdev->glob;
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100636 struct ttm_buffer_object *entry = NULL;
637 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200638
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200639 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100640 if (list_empty(&bdev->ddestroy))
641 goto out_unlock;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200642
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100643 entry = list_first_entry(&bdev->ddestroy,
644 struct ttm_buffer_object, ddestroy);
645 kref_get(&entry->list_kref);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200646
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100647 for (;;) {
648 struct ttm_buffer_object *nentry = NULL;
649
650 if (entry->ddestroy.next != &bdev->ddestroy) {
651 nentry = list_first_entry(&entry->ddestroy,
652 struct ttm_buffer_object, ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200653 kref_get(&nentry->list_kref);
654 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200655
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100656 ret = ttm_bo_reserve_nolru(entry, false, true, false, 0);
657 if (remove_all && ret) {
658 spin_unlock(&glob->lru_lock);
659 ret = ttm_bo_reserve_nolru(entry, false, false,
660 false, 0);
661 spin_lock(&glob->lru_lock);
662 }
663
Maarten Lankhorst85b144f2012-11-29 11:36:54 +0000664 if (!ret)
665 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
666 !remove_all);
667 else
668 spin_unlock(&glob->lru_lock);
669
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200670 kref_put(&entry->list_kref, ttm_bo_release_list);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100671 entry = nentry;
672
673 if (ret || !entry)
674 goto out;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200675
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200676 spin_lock(&glob->lru_lock);
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100677 if (list_empty(&entry->ddestroy))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200678 break;
679 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200680
Luca Barbieri1a961ce2010-01-20 20:01:30 +0100681out_unlock:
682 spin_unlock(&glob->lru_lock);
683out:
684 if (entry)
685 kref_put(&entry->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200686 return ret;
687}
688
689static void ttm_bo_delayed_workqueue(struct work_struct *work)
690{
691 struct ttm_bo_device *bdev =
692 container_of(work, struct ttm_bo_device, wq.work);
693
694 if (ttm_bo_delayed_delete(bdev, false)) {
695 schedule_delayed_work(&bdev->wq,
696 ((HZ / 100) < 1) ? 1 : HZ / 100);
697 }
698}
699
700static void ttm_bo_release(struct kref *kref)
701{
702 struct ttm_buffer_object *bo =
703 container_of(kref, struct ttm_buffer_object, kref);
704 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100705 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200706
Thomas Hellstrom82fe50b2012-11-21 14:53:21 +0000707 write_lock(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200708 if (likely(bo->vm_node != NULL)) {
709 rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
710 drm_mm_put_block(bo->vm_node);
711 bo->vm_node = NULL;
712 }
713 write_unlock(&bdev->vm_lock);
Thomas Hellstromeba67092010-11-11 09:41:57 +0100714 ttm_mem_io_lock(man, false);
715 ttm_mem_io_free_vm(bo);
716 ttm_mem_io_unlock(man);
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200717 ttm_bo_cleanup_refs_or_queue(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200718 kref_put(&bo->list_kref, ttm_bo_release_list);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200719}
720
721void ttm_bo_unref(struct ttm_buffer_object **p_bo)
722{
723 struct ttm_buffer_object *bo = *p_bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200724
725 *p_bo = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200726 kref_put(&bo->kref, ttm_bo_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200727}
728EXPORT_SYMBOL(ttm_bo_unref);
729
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400730int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
731{
732 return cancel_delayed_work_sync(&bdev->wq);
733}
734EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
735
736void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
737{
738 if (resched)
739 schedule_delayed_work(&bdev->wq,
740 ((HZ / 100) < 1) ? 1 : HZ / 100);
741}
742EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
743
Jerome Glisseca262a9992009-12-08 15:33:32 +0100744static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000745 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200746{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200747 struct ttm_bo_device *bdev = bo->bdev;
748 struct ttm_mem_reg evict_mem;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100749 struct ttm_placement placement;
750 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200751
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000752 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200753 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000754 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200755
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200756 if (unlikely(ret != 0)) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100757 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700758 pr_err("Failed to expire sync object before buffer eviction\n");
Thomas Hellstrom78ecf092009-06-17 12:29:55 +0200759 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200760 goto out;
761 }
762
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +0200763 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200764
765 evict_mem = bo->mem;
766 evict_mem.mm_node = NULL;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100767 evict_mem.bus.io_reserved_vm = false;
768 evict_mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200769
Jerome Glisse7cb7d1d2009-12-09 22:14:27 +0100770 placement.fpfn = 0;
771 placement.lpfn = 0;
772 placement.num_placement = 0;
773 placement.num_busy_placement = 0;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100774 bdev->driver->evict_flags(bo, &placement);
775 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000776 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200777 if (ret) {
Jerome Glissefb53f862009-12-09 21:55:10 +0100778 if (ret != -ERESTARTSYS) {
Joe Perches25d04792012-03-16 21:43:50 -0700779 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
780 bo);
Jerome Glissefb53f862009-12-09 21:55:10 +0100781 ttm_bo_mem_space_debug(bo, &placement);
782 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200783 goto out;
784 }
785
786 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000787 no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200788 if (ret) {
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100789 if (ret != -ERESTARTSYS)
Joe Perches25d04792012-03-16 21:43:50 -0700790 pr_err("Buffer eviction failed\n");
Ben Skeggs42311ff2010-08-04 12:07:08 +1000791 ttm_bo_mem_put(bo, &evict_mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200792 goto out;
793 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200794 bo->evicted = true;
795out:
796 return ret;
797}
798
Jerome Glisseca262a9992009-12-08 15:33:32 +0100799static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
800 uint32_t mem_type,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000801 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000802 bool no_wait_gpu)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100803{
804 struct ttm_bo_global *glob = bdev->glob;
805 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
806 struct ttm_buffer_object *bo;
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000807 int ret = -EBUSY, put_count;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100808
809 spin_lock(&glob->lru_lock);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000810 list_for_each_entry(bo, &man->lru, lru) {
Maarten Lankhorst63d0a412013-01-15 14:56:37 +0100811 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000812 if (!ret)
813 break;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100814 }
815
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000816 if (ret) {
817 spin_unlock(&glob->lru_lock);
Thomas Hellstromb8e902f2012-10-22 12:51:26 +0000818 return ret;
Thomas Hellstrome1efc9b2010-10-19 09:01:01 +0200819 }
820
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000821 kref_get(&bo->list_kref);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100822
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000823 if (!list_empty(&bo->ddestroy)) {
824 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
825 no_wait_gpu);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100826 kref_put(&bo->list_kref, ttm_bo_release_list);
Maarten Lankhorste7ab2012012-11-28 11:25:43 +0000827 return ret;
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100828 }
829
830 put_count = ttm_bo_del_from_lru(bo);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100831 spin_unlock(&glob->lru_lock);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100832
833 BUG_ON(ret != 0);
834
Dave Airlied6ea8882010-11-22 13:24:40 +1000835 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100836
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000837 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100838 ttm_bo_unreserve(bo);
Thomas Hellstrom9c51ba12009-12-02 18:33:46 +0100839
Jerome Glisseca262a9992009-12-08 15:33:32 +0100840 kref_put(&bo->list_kref, ttm_bo_release_list);
841 return ret;
842}
843
Ben Skeggs42311ff2010-08-04 12:07:08 +1000844void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
845{
Ben Skeggsd961db72010-08-05 10:48:18 +1000846 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
Ben Skeggs42311ff2010-08-04 12:07:08 +1000847
Ben Skeggsd961db72010-08-05 10:48:18 +1000848 if (mem->mm_node)
849 (*man->func->put_node)(man, mem);
Ben Skeggs42311ff2010-08-04 12:07:08 +1000850}
851EXPORT_SYMBOL(ttm_bo_mem_put);
852
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200853/**
854 * Repeatedly evict memory from the LRU for @mem_type until we create enough
855 * space, or we've evicted everything and there isn't enough space.
856 */
Jerome Glisseca262a9992009-12-08 15:33:32 +0100857static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
858 uint32_t mem_type,
859 struct ttm_placement *placement,
860 struct ttm_mem_reg *mem,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000861 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000862 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200863{
Jerome Glisseca262a9992009-12-08 15:33:32 +0100864 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200865 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200866 int ret;
867
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200868 do {
Ben Skeggsd961db72010-08-05 10:48:18 +1000869 ret = (*man->func->get_node)(man, bo, placement, mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200870 if (unlikely(ret != 0))
871 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +1000872 if (mem->mm_node)
Jerome Glisseca262a9992009-12-08 15:33:32 +0100873 break;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000874 ret = ttm_mem_evict_first(bdev, mem_type,
875 interruptible, no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100876 if (unlikely(ret != 0))
877 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200878 } while (1);
Ben Skeggsd961db72010-08-05 10:48:18 +1000879 if (mem->mm_node == NULL)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200880 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200881 mem->mem_type = mem_type;
882 return 0;
883}
884
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200885static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
886 uint32_t cur_placement,
887 uint32_t proposed_placement)
888{
889 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
890 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
891
892 /**
893 * Keep current caching if possible.
894 */
895
896 if ((cur_placement & caching) != 0)
897 result |= (cur_placement & caching);
898 else if ((man->default_caching & caching) != 0)
899 result |= man->default_caching;
900 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
901 result |= TTM_PL_FLAG_CACHED;
902 else if ((TTM_PL_FLAG_WC & caching) != 0)
903 result |= TTM_PL_FLAG_WC;
904 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
905 result |= TTM_PL_FLAG_UNCACHED;
906
907 return result;
908}
909
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200910static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200911 uint32_t mem_type,
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200912 uint32_t proposed_placement,
913 uint32_t *masked_placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200914{
915 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
916
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200917 if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200918 return false;
919
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200920 if ((proposed_placement & man->available_caching) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200921 return false;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200922
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200923 cur_flags |= (proposed_placement & man->available_caching);
924
925 *masked_placement = cur_flags;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200926 return true;
927}
928
929/**
930 * Creates space for memory region @mem according to its type.
931 *
932 * This function first searches for free space in compatible memory types in
933 * the priority order defined by the driver. If free space isn't found, then
934 * ttm_bo_mem_force_space is attempted in priority order to evict and find
935 * space.
936 */
937int ttm_bo_mem_space(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100938 struct ttm_placement *placement,
939 struct ttm_mem_reg *mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000940 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000941 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200942{
943 struct ttm_bo_device *bdev = bo->bdev;
944 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200945 uint32_t mem_type = TTM_PL_SYSTEM;
946 uint32_t cur_flags = 0;
947 bool type_found = false;
948 bool type_ok = false;
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100949 bool has_erestartsys = false;
Jerome Glisseca262a9992009-12-08 15:33:32 +0100950 int i, ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200951
952 mem->mm_node = NULL;
Dave Airlieb6637522009-12-14 14:51:35 +1000953 for (i = 0; i < placement->num_placement; ++i) {
Jerome Glisseca262a9992009-12-08 15:33:32 +0100954 ret = ttm_mem_type_from_flags(placement->placement[i],
955 &mem_type);
956 if (ret)
957 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200958 man = &bdev->man[mem_type];
959
960 type_ok = ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100961 mem_type,
962 placement->placement[i],
963 &cur_flags);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200964
965 if (!type_ok)
966 continue;
967
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200968 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
969 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100970 /*
971 * Use the access and other non-mapping-related flag bits from
972 * the memory placement flags to the current flags
973 */
974 ttm_flag_masked(&cur_flags, placement->placement[i],
975 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +0200976
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200977 if (mem_type == TTM_PL_SYSTEM)
978 break;
979
980 if (man->has_type && man->use_type) {
981 type_found = true;
Ben Skeggsd961db72010-08-05 10:48:18 +1000982 ret = (*man->func->get_node)(man, bo, placement, mem);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100983 if (unlikely(ret))
984 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200985 }
Ben Skeggsd961db72010-08-05 10:48:18 +1000986 if (mem->mm_node)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200987 break;
988 }
989
Ben Skeggsd961db72010-08-05 10:48:18 +1000990 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200991 mem->mem_type = mem_type;
992 mem->placement = cur_flags;
993 return 0;
994 }
995
996 if (!type_found)
997 return -EINVAL;
998
Dave Airlieb6637522009-12-14 14:51:35 +1000999 for (i = 0; i < placement->num_busy_placement; ++i) {
1000 ret = ttm_mem_type_from_flags(placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001001 &mem_type);
1002 if (ret)
1003 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001004 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001005 if (!man->has_type)
1006 continue;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001007 if (!ttm_bo_mt_compatible(man,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001008 mem_type,
Dave Airlieb6637522009-12-14 14:51:35 +10001009 placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001010 &cur_flags))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001011 continue;
1012
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001013 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
1014 cur_flags);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001015 /*
1016 * Use the access and other non-mapping-related flag bits from
1017 * the memory placement flags to the current flags
1018 */
Dave Airlieb6637522009-12-14 14:51:35 +10001019 ttm_flag_masked(&cur_flags, placement->busy_placement[i],
Jerome Glisseca262a9992009-12-08 15:33:32 +01001020 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromae3e8122009-06-24 19:57:34 +02001021
Thomas Hellstrom0eaddb22010-01-16 16:05:04 +01001022
1023 if (mem_type == TTM_PL_SYSTEM) {
1024 mem->mem_type = mem_type;
1025 mem->placement = cur_flags;
1026 mem->mm_node = NULL;
1027 return 0;
1028 }
1029
Jerome Glisseca262a9992009-12-08 15:33:32 +01001030 ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001031 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001032 if (ret == 0 && mem->mm_node) {
1033 mem->placement = cur_flags;
1034 return 0;
1035 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001036 if (ret == -ERESTARTSYS)
1037 has_erestartsys = true;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001038 }
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +01001039 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001040 return ret;
1041}
1042EXPORT_SYMBOL(ttm_bo_mem_space);
1043
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001044int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001045 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001046 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001047 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001048{
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001049 int ret = 0;
1050 struct ttm_mem_reg mem;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001051 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001052
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001053 BUG_ON(!ttm_bo_is_reserved(bo));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001054
1055 /*
1056 * FIXME: It's possible to pipeline buffer moves.
1057 * Have the driver move function wait for idle when necessary,
1058 * instead of doing it here.
1059 */
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001060 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001061 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001062 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001063 if (ret)
1064 return ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001065 mem.num_pages = bo->num_pages;
1066 mem.size = mem.num_pages << PAGE_SHIFT;
1067 mem.page_alignment = bo->mem.page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001068 mem.bus.io_reserved_vm = false;
1069 mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001070 /*
1071 * Determine where to move the buffer.
1072 */
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001073 ret = ttm_bo_mem_space(bo, placement, &mem,
1074 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001075 if (ret)
1076 goto out_unlock;
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001077 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1078 interruptible, no_wait_gpu);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001079out_unlock:
Ben Skeggsd961db72010-08-05 10:48:18 +10001080 if (ret && mem.mm_node)
1081 ttm_bo_mem_put(bo, &mem);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001082 return ret;
1083}
1084
Jerome Glisseca262a9992009-12-08 15:33:32 +01001085static int ttm_bo_mem_compat(struct ttm_placement *placement,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001086 struct ttm_mem_reg *mem)
1087{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001088 int i;
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001089
Ben Skeggsd961db72010-08-05 10:48:18 +10001090 if (mem->mm_node && placement->lpfn != 0 &&
1091 (mem->start < placement->fpfn ||
1092 mem->start + mem->num_pages > placement->lpfn))
Thomas Hellstrome22238e2010-02-12 00:18:00 +01001093 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001094
Jerome Glisseca262a9992009-12-08 15:33:32 +01001095 for (i = 0; i < placement->num_placement; i++) {
1096 if ((placement->placement[i] & mem->placement &
1097 TTM_PL_MASK_CACHING) &&
1098 (placement->placement[i] & mem->placement &
1099 TTM_PL_MASK_MEM))
1100 return i;
1101 }
1102 return -1;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001103}
1104
Jerome Glisse09855ac2009-12-10 17:16:27 +01001105int ttm_bo_validate(struct ttm_buffer_object *bo,
1106 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001107 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001108 bool no_wait_gpu)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001109{
1110 int ret;
1111
Maarten Lankhorsta9dbfff2012-10-12 16:58:36 +02001112 BUG_ON(!ttm_bo_is_reserved(bo));
Jerome Glisseca262a9992009-12-08 15:33:32 +01001113 /* Check that range is valid */
1114 if (placement->lpfn || placement->fpfn)
1115 if (placement->fpfn > placement->lpfn ||
1116 (placement->lpfn - placement->fpfn) < bo->num_pages)
1117 return -EINVAL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001118 /*
1119 * Check whether we need to move buffer.
1120 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001121 ret = ttm_bo_mem_compat(placement, &bo->mem);
1122 if (ret < 0) {
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001123 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1124 no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001125 if (ret)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001126 return ret;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001127 } else {
1128 /*
1129 * Use the access and other non-mapping-related flag bits from
1130 * the compatible memory placement flags to the active flags
1131 */
1132 ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
1133 ~TTM_PL_MASK_MEMTYPE);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001134 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001135 /*
1136 * We might need to add a TTM.
1137 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001138 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1139 ret = ttm_bo_add_ttm(bo, true);
1140 if (ret)
1141 return ret;
1142 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001143 return 0;
1144}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001145EXPORT_SYMBOL(ttm_bo_validate);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001146
Jerome Glisse09855ac2009-12-10 17:16:27 +01001147int ttm_bo_check_placement(struct ttm_buffer_object *bo,
1148 struct ttm_placement *placement)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001149{
Thomas Hellstrom29e190e2010-11-02 13:21:48 +00001150 BUG_ON((placement->fpfn || placement->lpfn) &&
1151 (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001152
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001153 return 0;
1154}
1155
Jerome Glisse09855ac2009-12-10 17:16:27 +01001156int ttm_bo_init(struct ttm_bo_device *bdev,
1157 struct ttm_buffer_object *bo,
1158 unsigned long size,
1159 enum ttm_bo_type type,
1160 struct ttm_placement *placement,
1161 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001162 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001163 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001164 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +01001165 struct sg_table *sg,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001166 void (*destroy) (struct ttm_buffer_object *))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001167{
Jerome Glisse09855ac2009-12-10 17:16:27 +01001168 int ret = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001169 unsigned long num_pages;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001170 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001171 bool locked;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001172
1173 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1174 if (ret) {
Joe Perches25d04792012-03-16 21:43:50 -07001175 pr_err("Out of kernel memory\n");
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001176 if (destroy)
1177 (*destroy)(bo);
1178 else
1179 kfree(bo);
1180 return -ENOMEM;
1181 }
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001182
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001183 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1184 if (num_pages == 0) {
Joe Perches25d04792012-03-16 21:43:50 -07001185 pr_err("Illegal buffer object size\n");
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +01001186 if (destroy)
1187 (*destroy)(bo);
1188 else
1189 kfree(bo);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001190 ttm_mem_global_free(mem_glob, acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001191 return -EINVAL;
1192 }
1193 bo->destroy = destroy;
1194
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001195 kref_init(&bo->kref);
1196 kref_init(&bo->list_kref);
1197 atomic_set(&bo->cpu_writers, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001198 INIT_LIST_HEAD(&bo->lru);
1199 INIT_LIST_HEAD(&bo->ddestroy);
1200 INIT_LIST_HEAD(&bo->swap);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001201 INIT_LIST_HEAD(&bo->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001202 bo->bdev = bdev;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001203 bo->glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001204 bo->type = type;
1205 bo->num_pages = num_pages;
Jerome Glisseeb6d2c32009-12-10 16:15:52 +01001206 bo->mem.size = num_pages << PAGE_SHIFT;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001207 bo->mem.mem_type = TTM_PL_SYSTEM;
1208 bo->mem.num_pages = bo->num_pages;
1209 bo->mem.mm_node = NULL;
1210 bo->mem.page_alignment = page_alignment;
Thomas Hellstromeba67092010-11-11 09:41:57 +01001211 bo->mem.bus.io_reserved_vm = false;
1212 bo->mem.bus.io_reserved_count = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001213 bo->priv_flags = 0;
1214 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
Jan Engelhardt5df23972011-04-04 01:25:18 +02001215 bo->persistent_swap_storage = persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001216 bo->acc_size = acc_size;
Dave Airlie129b78b2012-04-02 11:46:06 +01001217 bo->sg = sg;
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001218 bo->resv = &bo->ttm_resv;
1219 reservation_object_init(bo->resv);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001220 atomic_inc(&bo->glob->bo_count);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001221
Jerome Glisse09855ac2009-12-10 17:16:27 +01001222 ret = ttm_bo_check_placement(bo, placement);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001223
1224 /*
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001225 * For ttm_bo_type_device buffers, allocate
1226 * address space from the device.
1227 */
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001228 if (likely(!ret) &&
1229 (bo->type == ttm_bo_type_device ||
1230 bo->type == ttm_bo_type_sg))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001231 ret = ttm_bo_setup_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001232
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001233 locked = ww_mutex_trylock(&bo->resv->lock);
1234 WARN_ON(!locked);
1235
1236 if (likely(!ret))
1237 ret = ttm_bo_validate(bo, placement, interruptible, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001238
1239 ttm_bo_unreserve(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001240
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001241 if (unlikely(ret))
1242 ttm_bo_unref(&bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001243
1244 return ret;
1245}
Jerome Glisse09855ac2009-12-10 17:16:27 +01001246EXPORT_SYMBOL(ttm_bo_init);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001247
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001248size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1249 unsigned long bo_size,
1250 unsigned struct_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001251{
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001252 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1253 size_t size = 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001254
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001255 size += ttm_round_pot(struct_size);
1256 size += PAGE_ALIGN(npages * sizeof(void *));
1257 size += ttm_round_pot(sizeof(struct ttm_tt));
1258 return size;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001259}
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001260EXPORT_SYMBOL(ttm_bo_acc_size);
1261
1262size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1263 unsigned long bo_size,
1264 unsigned struct_size)
1265{
1266 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1267 size_t size = 0;
1268
1269 size += ttm_round_pot(struct_size);
1270 size += PAGE_ALIGN(npages * sizeof(void *));
1271 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1272 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1273 return size;
1274}
1275EXPORT_SYMBOL(ttm_bo_dma_acc_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001276
Jerome Glisse09855ac2009-12-10 17:16:27 +01001277int ttm_bo_create(struct ttm_bo_device *bdev,
1278 unsigned long size,
1279 enum ttm_bo_type type,
1280 struct ttm_placement *placement,
1281 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001282 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +02001283 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +01001284 struct ttm_buffer_object **p_bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001285{
1286 struct ttm_buffer_object *bo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -05001287 size_t acc_size;
Jerome Glisseca262a9992009-12-08 15:33:32 +01001288 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001289
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001290 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
Thomas Hellstroma393c732012-06-12 13:28:42 +02001291 if (unlikely(bo == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001292 return -ENOMEM;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001293
Thomas Hellstroma393c732012-06-12 13:28:42 +02001294 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
Jerome Glisse09855ac2009-12-10 17:16:27 +01001295 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +00001296 interruptible, persistent_swap_storage, acc_size,
1297 NULL, NULL);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001298 if (likely(ret == 0))
1299 *p_bo = bo;
1300
1301 return ret;
1302}
Thomas Hellstrom4d798932011-10-04 20:13:11 +02001303EXPORT_SYMBOL(ttm_bo_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001304
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001305static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001306 unsigned mem_type, bool allow_errors)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001307{
Jerome Glisseca262a9992009-12-08 15:33:32 +01001308 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001309 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001310 int ret;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001311
1312 /*
1313 * Can't use standard list traversal since we're unlocking.
1314 */
1315
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001316 spin_lock(&glob->lru_lock);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001317 while (!list_empty(&man->lru)) {
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001318 spin_unlock(&glob->lru_lock);
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001319 ret = ttm_mem_evict_first(bdev, mem_type, false, false);
Jerome Glisseca262a9992009-12-08 15:33:32 +01001320 if (ret) {
1321 if (allow_errors) {
1322 return ret;
1323 } else {
Joe Perches25d04792012-03-16 21:43:50 -07001324 pr_err("Cleanup eviction failed\n");
Jerome Glisseca262a9992009-12-08 15:33:32 +01001325 }
1326 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001327 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001328 }
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001329 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001330 return 0;
1331}
1332
1333int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1334{
Roel Kluinc96e7c72009-08-03 14:22:53 +02001335 struct ttm_mem_type_manager *man;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001336 int ret = -EINVAL;
1337
1338 if (mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001339 pr_err("Illegal memory type %d\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001340 return ret;
1341 }
Roel Kluinc96e7c72009-08-03 14:22:53 +02001342 man = &bdev->man[mem_type];
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001343
1344 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001345 pr_err("Trying to take down uninitialized memory manager type %u\n",
1346 mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001347 return ret;
1348 }
1349
1350 man->use_type = false;
1351 man->has_type = false;
1352
1353 ret = 0;
1354 if (mem_type > 0) {
Jerome Glisseca262a9992009-12-08 15:33:32 +01001355 ttm_bo_force_list_clean(bdev, mem_type, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001356
Ben Skeggsd961db72010-08-05 10:48:18 +10001357 ret = (*man->func->takedown)(man);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001358 }
1359
1360 return ret;
1361}
1362EXPORT_SYMBOL(ttm_bo_clean_mm);
1363
1364int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1365{
1366 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1367
1368 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
Joe Perches25d04792012-03-16 21:43:50 -07001369 pr_err("Illegal memory manager memory type %u\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001370 return -EINVAL;
1371 }
1372
1373 if (!man->has_type) {
Joe Perches25d04792012-03-16 21:43:50 -07001374 pr_err("Memory type %u has not been initialized\n", mem_type);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001375 return 0;
1376 }
1377
Jerome Glisseca262a9992009-12-08 15:33:32 +01001378 return ttm_bo_force_list_clean(bdev, mem_type, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001379}
1380EXPORT_SYMBOL(ttm_bo_evict_mm);
1381
1382int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +01001383 unsigned long p_size)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001384{
1385 int ret = -EINVAL;
1386 struct ttm_mem_type_manager *man;
1387
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001388 BUG_ON(type >= TTM_NUM_MEM_TYPES);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001389 man = &bdev->man[type];
Thomas Hellstromdbc4a5b2010-10-29 10:46:47 +02001390 BUG_ON(man->has_type);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001391 man->io_reserve_fastpath = true;
1392 man->use_io_reserve_lru = false;
1393 mutex_init(&man->io_reserve_mutex);
1394 INIT_LIST_HEAD(&man->io_reserve_lru);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001395
1396 ret = bdev->driver->init_mem_type(bdev, type, man);
1397 if (ret)
1398 return ret;
Ben Skeggsd961db72010-08-05 10:48:18 +10001399 man->bdev = bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001400
1401 ret = 0;
1402 if (type != TTM_PL_SYSTEM) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001403 ret = (*man->func->init)(man, p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001404 if (ret)
1405 return ret;
1406 }
1407 man->has_type = true;
1408 man->use_type = true;
1409 man->size = p_size;
1410
1411 INIT_LIST_HEAD(&man->lru);
1412
1413 return 0;
1414}
1415EXPORT_SYMBOL(ttm_bo_init_mm);
1416
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001417static void ttm_bo_global_kobj_release(struct kobject *kobj)
1418{
1419 struct ttm_bo_global *glob =
1420 container_of(kobj, struct ttm_bo_global, kobj);
1421
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001422 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1423 __free_page(glob->dummy_read_page);
1424 kfree(glob);
1425}
1426
Dave Airlieba4420c2010-03-09 10:56:52 +10001427void ttm_bo_global_release(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001428{
1429 struct ttm_bo_global *glob = ref->object;
1430
1431 kobject_del(&glob->kobj);
1432 kobject_put(&glob->kobj);
1433}
1434EXPORT_SYMBOL(ttm_bo_global_release);
1435
Dave Airlieba4420c2010-03-09 10:56:52 +10001436int ttm_bo_global_init(struct drm_global_reference *ref)
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001437{
1438 struct ttm_bo_global_ref *bo_ref =
1439 container_of(ref, struct ttm_bo_global_ref, ref);
1440 struct ttm_bo_global *glob = ref->object;
1441 int ret;
1442
1443 mutex_init(&glob->device_list_mutex);
1444 spin_lock_init(&glob->lru_lock);
1445 glob->mem_glob = bo_ref->mem_glob;
1446 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1447
1448 if (unlikely(glob->dummy_read_page == NULL)) {
1449 ret = -ENOMEM;
1450 goto out_no_drp;
1451 }
1452
1453 INIT_LIST_HEAD(&glob->swap_lru);
1454 INIT_LIST_HEAD(&glob->device_list);
1455
1456 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1457 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1458 if (unlikely(ret != 0)) {
Joe Perches25d04792012-03-16 21:43:50 -07001459 pr_err("Could not register buffer object swapout\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001460 goto out_no_shrink;
1461 }
1462
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001463 atomic_set(&glob->bo_count, 0);
1464
Robert P. J. Dayb642ed02010-03-13 10:36:32 +00001465 ret = kobject_init_and_add(
1466 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001467 if (unlikely(ret != 0))
1468 kobject_put(&glob->kobj);
1469 return ret;
1470out_no_shrink:
1471 __free_page(glob->dummy_read_page);
1472out_no_drp:
1473 kfree(glob);
1474 return ret;
1475}
1476EXPORT_SYMBOL(ttm_bo_global_init);
1477
1478
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001479int ttm_bo_device_release(struct ttm_bo_device *bdev)
1480{
1481 int ret = 0;
1482 unsigned i = TTM_NUM_MEM_TYPES;
1483 struct ttm_mem_type_manager *man;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001484 struct ttm_bo_global *glob = bdev->glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001485
1486 while (i--) {
1487 man = &bdev->man[i];
1488 if (man->has_type) {
1489 man->use_type = false;
1490 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1491 ret = -EBUSY;
Joe Perches25d04792012-03-16 21:43:50 -07001492 pr_err("DRM memory manager type %d is not clean\n",
1493 i);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001494 }
1495 man->has_type = false;
1496 }
1497 }
1498
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001499 mutex_lock(&glob->device_list_mutex);
1500 list_del(&bdev->device_list);
1501 mutex_unlock(&glob->device_list_mutex);
1502
Tejun Heof094cfc2010-12-24 15:59:06 +01001503 cancel_delayed_work_sync(&bdev->wq);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001504
1505 while (ttm_bo_delayed_delete(bdev, true))
1506 ;
1507
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001508 spin_lock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001509 if (list_empty(&bdev->ddestroy))
1510 TTM_DEBUG("Delayed destroy list was clean\n");
1511
1512 if (list_empty(&bdev->man[0].lru))
1513 TTM_DEBUG("Swap list was clean\n");
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001514 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001515
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001516 BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
1517 write_lock(&bdev->vm_lock);
1518 drm_mm_takedown(&bdev->addr_space_mm);
1519 write_unlock(&bdev->vm_lock);
1520
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001521 return ret;
1522}
1523EXPORT_SYMBOL(ttm_bo_device_release);
1524
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001525int ttm_bo_device_init(struct ttm_bo_device *bdev,
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001526 struct ttm_bo_global *glob,
1527 struct ttm_bo_driver *driver,
Dave Airlie51c8b402009-08-20 13:38:04 +10001528 uint64_t file_page_offset,
Dave Airliead49f502009-07-10 22:36:26 +10001529 bool need_dma32)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001530{
1531 int ret = -EINVAL;
1532
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001533 rwlock_init(&bdev->vm_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001534 bdev->driver = driver;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001535
1536 memset(bdev->man, 0, sizeof(bdev->man));
1537
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001538 /*
1539 * Initialize the system memory buffer type.
1540 * Other types need to be driver / IOCTL initialized.
1541 */
Jerome Glisseca262a9992009-12-08 15:33:32 +01001542 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001543 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001544 goto out_no_sys;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001545
1546 bdev->addr_space_rb = RB_ROOT;
1547 ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
1548 if (unlikely(ret != 0))
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001549 goto out_no_addr_mm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001550
1551 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001552 INIT_LIST_HEAD(&bdev->ddestroy);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001553 bdev->dev_mapping = NULL;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001554 bdev->glob = glob;
Dave Airliead49f502009-07-10 22:36:26 +10001555 bdev->need_dma32 = need_dma32;
Thomas Hellstrom65705962010-11-17 12:28:31 +00001556 bdev->val_seq = 0;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001557 spin_lock_init(&bdev->fence_lock);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001558 mutex_lock(&glob->device_list_mutex);
1559 list_add_tail(&bdev->device_list, &glob->device_list);
1560 mutex_unlock(&glob->device_list_mutex);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001561
1562 return 0;
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001563out_no_addr_mm:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001564 ttm_bo_clean_mm(bdev, 0);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001565out_no_sys:
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001566 return ret;
1567}
1568EXPORT_SYMBOL(ttm_bo_device_init);
1569
1570/*
1571 * buffer object vm functions.
1572 */
1573
1574bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1575{
1576 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1577
1578 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1579 if (mem->mem_type == TTM_PL_SYSTEM)
1580 return false;
1581
1582 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1583 return false;
1584
1585 if (mem->placement & TTM_PL_FLAG_CACHED)
1586 return false;
1587 }
1588 return true;
1589}
1590
Thomas Hellstromeba67092010-11-11 09:41:57 +01001591void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001592{
1593 struct ttm_bo_device *bdev = bo->bdev;
1594 loff_t offset = (loff_t) bo->addr_space_offset;
1595 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
1596
1597 if (!bdev->dev_mapping)
1598 return;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001599 unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
Thomas Hellstromeba67092010-11-11 09:41:57 +01001600 ttm_mem_io_free_vm(bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001601}
Thomas Hellstromeba67092010-11-11 09:41:57 +01001602
1603void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1604{
1605 struct ttm_bo_device *bdev = bo->bdev;
1606 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1607
1608 ttm_mem_io_lock(man, false);
1609 ttm_bo_unmap_virtual_locked(bo);
1610 ttm_mem_io_unlock(man);
1611}
1612
1613
Dave Airliee024e112009-06-24 09:48:08 +10001614EXPORT_SYMBOL(ttm_bo_unmap_virtual);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001615
1616static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
1617{
1618 struct ttm_bo_device *bdev = bo->bdev;
1619 struct rb_node **cur = &bdev->addr_space_rb.rb_node;
1620 struct rb_node *parent = NULL;
1621 struct ttm_buffer_object *cur_bo;
1622 unsigned long offset = bo->vm_node->start;
1623 unsigned long cur_offset;
1624
1625 while (*cur) {
1626 parent = *cur;
1627 cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
1628 cur_offset = cur_bo->vm_node->start;
1629 if (offset < cur_offset)
1630 cur = &parent->rb_left;
1631 else if (offset > cur_offset)
1632 cur = &parent->rb_right;
1633 else
1634 BUG();
1635 }
1636
1637 rb_link_node(&bo->vm_rb, parent, cur);
1638 rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
1639}
1640
1641/**
1642 * ttm_bo_setup_vm:
1643 *
1644 * @bo: the buffer to allocate address space for
1645 *
1646 * Allocate address space in the drm device so that applications
1647 * can mmap the buffer and access the contents. This only
1648 * applies to ttm_bo_type_device objects as others are not
1649 * placed in the drm device address space.
1650 */
1651
1652static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
1653{
1654 struct ttm_bo_device *bdev = bo->bdev;
1655 int ret;
1656
1657retry_pre_get:
1658 ret = drm_mm_pre_get(&bdev->addr_space_mm);
1659 if (unlikely(ret != 0))
1660 return ret;
1661
1662 write_lock(&bdev->vm_lock);
1663 bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
1664 bo->mem.num_pages, 0, 0);
1665
1666 if (unlikely(bo->vm_node == NULL)) {
1667 ret = -ENOMEM;
1668 goto out_unlock;
1669 }
1670
1671 bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
1672 bo->mem.num_pages, 0);
1673
1674 if (unlikely(bo->vm_node == NULL)) {
1675 write_unlock(&bdev->vm_lock);
1676 goto retry_pre_get;
1677 }
1678
1679 ttm_bo_vm_insert_rb(bo);
1680 write_unlock(&bdev->vm_lock);
1681 bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
1682
1683 return 0;
1684out_unlock:
1685 write_unlock(&bdev->vm_lock);
1686 return ret;
1687}
1688
1689int ttm_bo_wait(struct ttm_buffer_object *bo,
Dave Airlie1717c0e2011-10-27 18:28:37 +02001690 bool lazy, bool interruptible, bool no_wait)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001691{
1692 struct ttm_bo_driver *driver = bo->bdev->driver;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001693 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001694 void *sync_obj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001695 int ret = 0;
1696
Dave Airlie1717c0e2011-10-27 18:28:37 +02001697 if (likely(bo->sync_obj == NULL))
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001698 return 0;
1699
Dave Airlie1717c0e2011-10-27 18:28:37 +02001700 while (bo->sync_obj) {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001701
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001702 if (driver->sync_obj_signaled(bo->sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001703 void *tmp_obj = bo->sync_obj;
1704 bo->sync_obj = NULL;
1705 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1706 spin_unlock(&bdev->fence_lock);
1707 driver->sync_obj_unref(&tmp_obj);
1708 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001709 continue;
1710 }
1711
1712 if (no_wait)
1713 return -EBUSY;
1714
Dave Airlie1717c0e2011-10-27 18:28:37 +02001715 sync_obj = driver->sync_obj_ref(bo->sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001716 spin_unlock(&bdev->fence_lock);
Maarten Lankhorstdedfdff2012-10-12 15:04:00 +00001717 ret = driver->sync_obj_wait(sync_obj,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001718 lazy, interruptible);
1719 if (unlikely(ret != 0)) {
1720 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001721 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001722 return ret;
1723 }
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001724 spin_lock(&bdev->fence_lock);
Maarten Lankhorst5fb4ef02012-10-12 15:02:19 +00001725 if (likely(bo->sync_obj == sync_obj)) {
Dave Airlie1717c0e2011-10-27 18:28:37 +02001726 void *tmp_obj = bo->sync_obj;
1727 bo->sync_obj = NULL;
1728 clear_bit(TTM_BO_PRIV_FLAG_MOVING,
1729 &bo->priv_flags);
1730 spin_unlock(&bdev->fence_lock);
1731 driver->sync_obj_unref(&sync_obj);
1732 driver->sync_obj_unref(&tmp_obj);
1733 spin_lock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001734 } else {
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001735 spin_unlock(&bdev->fence_lock);
Thomas Hellstromfee280d2009-08-03 12:39:06 +02001736 driver->sync_obj_unref(&sync_obj);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001737 spin_lock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001738 }
1739 }
1740 return 0;
1741}
1742EXPORT_SYMBOL(ttm_bo_wait);
1743
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001744int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1745{
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001746 struct ttm_bo_device *bdev = bo->bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001747 int ret = 0;
1748
1749 /*
Thomas Hellstrom8cfe92d2010-04-28 11:33:25 +02001750 * Using ttm_bo_reserve makes sure the lru lists are updated.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001751 */
1752
1753 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1754 if (unlikely(ret != 0))
1755 return ret;
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001756 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001757 ret = ttm_bo_wait(bo, false, true, no_wait);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001758 spin_unlock(&bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001759 if (likely(ret == 0))
1760 atomic_inc(&bo->cpu_writers);
1761 ttm_bo_unreserve(bo);
1762 return ret;
1763}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001764EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001765
1766void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1767{
Maarten Lankhorst654aa7922012-11-06 14:39:43 +01001768 atomic_dec(&bo->cpu_writers);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001769}
Ben Skeggsd1ede142009-12-11 15:13:00 +10001770EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001771
1772/**
1773 * A buffer object shrink method that tries to swap out the first
1774 * buffer object on the bo_global::swap_lru list.
1775 */
1776
1777static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1778{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001779 struct ttm_bo_global *glob =
1780 container_of(shrink, struct ttm_bo_global, shrink);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001781 struct ttm_buffer_object *bo;
1782 int ret = -EBUSY;
1783 int put_count;
1784 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1785
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001786 spin_lock(&glob->lru_lock);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001787 list_for_each_entry(bo, &glob->swap_lru, swap) {
Maarten Lankhorst63d0a412013-01-15 14:56:37 +01001788 ret = ttm_bo_reserve_nolru(bo, false, true, false, 0);
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001789 if (!ret)
1790 break;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001791 }
1792
Maarten Lankhorst2b7b3ad2012-11-28 11:25:42 +00001793 if (ret) {
1794 spin_unlock(&glob->lru_lock);
1795 return ret;
1796 }
1797
1798 kref_get(&bo->list_kref);
1799
1800 if (!list_empty(&bo->ddestroy)) {
1801 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1802 kref_put(&bo->list_kref, ttm_bo_release_list);
1803 return ret;
1804 }
1805
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001806 put_count = ttm_bo_del_from_lru(bo);
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001807 spin_unlock(&glob->lru_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001808
Dave Airlied6ea8882010-11-22 13:24:40 +10001809 ttm_bo_list_ref_sub(bo, put_count, true);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001810
1811 /**
1812 * Wait for GPU, then move to system cached.
1813 */
1814
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001815 spin_lock(&bo->bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001816 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +00001817 spin_unlock(&bo->bdev->fence_lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001818
1819 if (unlikely(ret != 0))
1820 goto out;
1821
1822 if ((bo->mem.placement & swap_placement) != swap_placement) {
1823 struct ttm_mem_reg evict_mem;
1824
1825 evict_mem = bo->mem;
1826 evict_mem.mm_node = NULL;
1827 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1828 evict_mem.mem_type = TTM_PL_SYSTEM;
1829
1830 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +00001831 false, false);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001832 if (unlikely(ret != 0))
1833 goto out;
1834 }
1835
1836 ttm_bo_unmap_virtual(bo);
1837
1838 /**
1839 * Swap out. Buffer will be swapped in again as soon as
1840 * anyone tries to access a ttm page.
1841 */
1842
Thomas Hellstrom3f09ea42010-01-13 22:28:40 +01001843 if (bo->bdev->driver->swap_notify)
1844 bo->bdev->driver->swap_notify(bo);
1845
Jan Engelhardt5df23972011-04-04 01:25:18 +02001846 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001847out:
1848
1849 /**
1850 *
1851 * Unreserve without putting on LRU to avoid swapping out an
1852 * already swapped buffer.
1853 */
1854
Maarten Lankhorst5e338402013-06-27 13:48:19 +02001855 ww_mutex_unlock(&bo->resv->lock);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001856 kref_put(&bo->list_kref, ttm_bo_release_list);
1857 return ret;
1858}
1859
1860void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1861{
Thomas Hellstroma987fca2009-08-18 16:51:56 +02001862 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001863 ;
1864}
Thomas Hellstrome99e1e72010-01-13 22:28:42 +01001865EXPORT_SYMBOL(ttm_bo_swapout_all);