Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1 | /************************************************************************** |
| 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 Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 31 | #define pr_fmt(fmt) "[TTM] " fmt |
| 32 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 33 | #include <drm/ttm/ttm_module.h> |
| 34 | #include <drm/ttm/ttm_bo_driver.h> |
| 35 | #include <drm/ttm/ttm_placement.h> |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 36 | #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 Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 42 | #include <linux/atomic.h> |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 43 | |
| 44 | #define TTM_ASSERT_LOCKED(param) |
| 45 | #define TTM_DEBUG(fmt, arg...) |
| 46 | #define TTM_BO_HASH_ORDER 13 |
| 47 | |
| 48 | static int ttm_bo_setup_vm(struct ttm_buffer_object *bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 49 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 50 | static void ttm_bo_global_kobj_release(struct kobject *kobj); |
| 51 | |
| 52 | static struct attribute ttm_bo_count = { |
| 53 | .name = "bo_count", |
| 54 | .mode = S_IRUGO |
| 55 | }; |
| 56 | |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 57 | static 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 Glisse | 5012f50 | 2009-12-10 18:07:26 +0100 | [diff] [blame] | 69 | static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type) |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 70 | { |
Jerome Glisse | 5012f50 | 2009-12-10 18:07:26 +0100 | [diff] [blame] | 71 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 72 | |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 73 | 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 Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 80 | if (mem_type != TTM_PL_SYSTEM) |
| 81 | (*man->func->debug)(man, TTM_PFX); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, |
| 85 | struct ttm_placement *placement) |
| 86 | { |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 87 | int i, ret, mem_type; |
| 88 | |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 89 | 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 Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 92 | for (i = 0; i < placement->num_placement; i++) { |
| 93 | ret = ttm_mem_type_from_flags(placement->placement[i], |
| 94 | &mem_type); |
| 95 | if (ret) |
| 96 | return; |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 97 | pr_err(" placement[%d]=0x%08X (%d)\n", |
| 98 | i, placement->placement[i], mem_type); |
Jerome Glisse | 5012f50 | 2009-12-10 18:07:26 +0100 | [diff] [blame] | 99 | ttm_mem_type_debug(bo->bdev, mem_type); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 103 | static 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 | |
| 114 | static struct attribute *ttm_bo_global_attrs[] = { |
| 115 | &ttm_bo_count, |
| 116 | NULL |
| 117 | }; |
| 118 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 119 | static const struct sysfs_ops ttm_bo_global_ops = { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 120 | .show = &ttm_bo_global_show |
| 121 | }; |
| 122 | |
| 123 | static 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 129 | |
| 130 | static inline uint32_t ttm_bo_type_flags(unsigned type) |
| 131 | { |
| 132 | return 1 << (type); |
| 133 | } |
| 134 | |
| 135 | static 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 Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 140 | size_t acc_size = bo->acc_size; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 141 | |
| 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 Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 152 | atomic_dec(&bo->glob->bo_count); |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 153 | if (bo->resv == &bo->ttm_resv) |
| 154 | reservation_object_fini(&bo->ttm_resv); |
| 155 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 156 | if (bo->destroy) |
| 157 | bo->destroy(bo); |
| 158 | else { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 159 | kfree(bo); |
| 160 | } |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 161 | ttm_mem_global_free(bdev->glob->mem_glob, acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 164 | void ttm_bo_add_to_lru(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 165 | { |
| 166 | struct ttm_bo_device *bdev = bo->bdev; |
| 167 | struct ttm_mem_type_manager *man; |
| 168 | |
Maarten Lankhorst | a9dbfff | 2012-10-12 16:58:36 +0200 | [diff] [blame] | 169 | BUG_ON(!ttm_bo_is_reserved(bo)); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 170 | |
| 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 Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 180 | list_add_tail(&bo->swap, &bo->glob->swap_lru); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 181 | kref_get(&bo->list_kref); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 186 | int ttm_bo_del_from_lru(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 187 | { |
| 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 Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 207 | int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 208 | bool interruptible, |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 209 | bool no_wait, bool use_ticket, |
| 210 | struct ww_acquire_ctx *ticket) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 211 | { |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 212 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 213 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 214 | if (no_wait) { |
| 215 | bool success; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 216 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 217 | /* not valid any more, fix your locking! */ |
| 218 | if (WARN_ON(ticket)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 219 | return -EBUSY; |
| 220 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 221 | success = ww_mutex_trylock(&bo->resv->lock); |
| 222 | return success ? 0 : -EBUSY; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 225 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 233 | } |
| 234 | EXPORT_SYMBOL(ttm_bo_reserve); |
| 235 | |
| 236 | static void ttm_bo_ref_bug(struct kref *list_kref) |
| 237 | { |
| 238 | BUG(); |
| 239 | } |
| 240 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 241 | void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count, |
| 242 | bool never_free) |
| 243 | { |
Thomas Hellstrom | 2357cbe | 2010-11-16 15:21:08 +0100 | [diff] [blame] | 244 | kref_sub(&bo->list_kref, count, |
| 245 | (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list); |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 246 | } |
| 247 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 248 | int ttm_bo_reserve(struct ttm_buffer_object *bo, |
| 249 | bool interruptible, |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 250 | bool no_wait, bool use_ticket, |
| 251 | struct ww_acquire_ctx *ticket) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 252 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 253 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 254 | int put_count = 0; |
| 255 | int ret; |
| 256 | |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 257 | ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_ticket, |
| 258 | ticket); |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 259 | if (likely(ret == 0)) { |
| 260 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 261 | put_count = ttm_bo_del_from_lru(bo); |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 262 | spin_unlock(&glob->lru_lock); |
| 263 | ttm_bo_list_ref_sub(bo, put_count, true); |
| 264 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 265 | |
| 266 | return ret; |
| 267 | } |
| 268 | |
Maarten Lankhorst | 5e45d7d | 2013-01-15 14:57:05 +0100 | [diff] [blame] | 269 | int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo, |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 270 | bool interruptible, struct ww_acquire_ctx *ticket) |
Maarten Lankhorst | 5e45d7d | 2013-01-15 14:57:05 +0100 | [diff] [blame] | 271 | { |
| 272 | struct ttm_bo_global *glob = bo->glob; |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 273 | int put_count = 0; |
| 274 | int ret = 0; |
Maarten Lankhorst | 5e45d7d | 2013-01-15 14:57:05 +0100 | [diff] [blame] | 275 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 276 | 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 Lankhorst | 5e45d7d | 2013-01-15 14:57:05 +0100 | [diff] [blame] | 283 | 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 Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 287 | } else if (ret == -EINTR) |
| 288 | ret = -ERESTARTSYS; |
| 289 | |
Maarten Lankhorst | 5e45d7d | 2013-01-15 14:57:05 +0100 | [diff] [blame] | 290 | return ret; |
| 291 | } |
| 292 | EXPORT_SYMBOL(ttm_bo_reserve_slowpath); |
| 293 | |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 294 | void ttm_bo_unreserve_ticket_locked(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket) |
Thomas Hellstrom | 95762c2 | 2010-11-17 12:28:30 +0000 | [diff] [blame] | 295 | { |
| 296 | ttm_bo_add_to_lru(bo); |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 297 | ww_mutex_unlock(&bo->resv->lock); |
Thomas Hellstrom | 95762c2 | 2010-11-17 12:28:30 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 300 | void ttm_bo_unreserve(struct ttm_buffer_object *bo) |
| 301 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 302 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 303 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 304 | spin_lock(&glob->lru_lock); |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 305 | ttm_bo_unreserve_ticket_locked(bo, NULL); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 306 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 307 | } |
| 308 | EXPORT_SYMBOL(ttm_bo_unreserve); |
| 309 | |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 310 | void 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 | } |
| 318 | EXPORT_SYMBOL(ttm_bo_unreserve_ticket); |
| 319 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 320 | /* |
| 321 | * Call bo->mutex locked. |
| 322 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 323 | static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc) |
| 324 | { |
| 325 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 326 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 327 | int ret = 0; |
| 328 | uint32_t page_flags = 0; |
| 329 | |
| 330 | TTM_ASSERT_LOCKED(&bo->mutex); |
| 331 | bo->ttm = NULL; |
| 332 | |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 333 | if (bdev->need_dma32) |
| 334 | page_flags |= TTM_PAGE_FLAG_DMA32; |
| 335 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 336 | 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 Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 341 | bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
| 342 | page_flags, glob->dummy_read_page); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 343 | if (unlikely(bo->ttm == NULL)) |
| 344 | ret = -ENOMEM; |
| 345 | break; |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 346 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 356 | default: |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 357 | pr_err("Illegal buffer object type\n"); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 358 | ret = -EINVAL; |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, |
| 366 | struct ttm_mem_reg *mem, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 367 | bool evict, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 368 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 369 | { |
| 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 Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 378 | ((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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 385 | |
| 386 | /* |
| 387 | * Create and bind a ttm if required. |
| 388 | */ |
| 389 | |
Ben Skeggs | 8d3bb23 | 2011-08-22 03:15:05 +0000 | [diff] [blame] | 390 | if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
| 391 | if (bo->ttm == NULL) { |
Ben Skeggs | ff02b13 | 2011-09-14 06:08:06 +1000 | [diff] [blame] | 392 | bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED); |
| 393 | ret = ttm_bo_add_ttm(bo, zero); |
Ben Skeggs | 8d3bb23 | 2011-08-22 03:15:05 +0000 | [diff] [blame] | 394 | if (ret) |
| 395 | goto out_err; |
| 396 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 397 | |
| 398 | ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement); |
| 399 | if (ret) |
Thomas Hellstrom | 87ef920 | 2009-06-17 12:29:57 +0200 | [diff] [blame] | 400 | goto out_err; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 401 | |
| 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 Skeggs | 82ef594 | 2011-02-02 00:27:10 +0000 | [diff] [blame] | 409 | if (bdev->driver->move_notify) |
| 410 | bdev->driver->move_notify(bo, mem); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 411 | bo->mem = *mem; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 412 | mem->mm_node = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 413 | goto moved; |
| 414 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 417 | if (bdev->driver->move_notify) |
| 418 | bdev->driver->move_notify(bo, mem); |
| 419 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 420 | if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && |
| 421 | !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 422 | ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 423 | else if (bdev->driver->move) |
| 424 | ret = bdev->driver->move(bo, evict, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 425 | no_wait_gpu, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 426 | else |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 427 | ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 428 | |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 429 | 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 Airlie | 014b344 | 2013-01-16 15:58:34 +1000 | [diff] [blame] | 436 | *mem = tmp_mem; |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 437 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 438 | |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 439 | goto out_err; |
| 440 | } |
Jerome Glisse | dc97b34 | 2011-11-18 11:47:03 -0500 | [diff] [blame] | 441 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 442 | moved: |
| 443 | if (bo->evicted) { |
| 444 | ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); |
| 445 | if (ret) |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 446 | pr_err("Can not flush read caches\n"); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 447 | bo->evicted = false; |
| 448 | } |
| 449 | |
| 450 | if (bo->mem.mm_node) { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 451 | bo->offset = (bo->mem.start << PAGE_SHIFT) + |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 452 | bdev->man[bo->mem.mem_type].gpu_offset; |
| 453 | bo->cur_placement = bo->mem.placement; |
Thomas Hellstrom | 354fb52 | 2010-01-13 22:28:45 +0100 | [diff] [blame] | 454 | } else |
| 455 | bo->offset = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 456 | |
| 457 | return 0; |
| 458 | |
| 459 | out_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 Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 471 | * Call bo::reserved. |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 472 | * Will release GPU memory type usage on destruction. |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 473 | * This is the place to put in driver specific hooks to release |
| 474 | * driver private resources. |
| 475 | * Will release the bo::reserved lock. |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 476 | */ |
| 477 | |
| 478 | static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) |
| 479 | { |
Jerome Glisse | dc97b34 | 2011-11-18 11:47:03 -0500 | [diff] [blame] | 480 | if (bo->bdev->driver->move_notify) |
| 481 | bo->bdev->driver->move_notify(bo, NULL); |
| 482 | |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 483 | if (bo->ttm) { |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 484 | ttm_tt_unbind(bo->ttm); |
| 485 | ttm_tt_destroy(bo->ttm); |
| 486 | bo->ttm = NULL; |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 487 | } |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 488 | ttm_bo_mem_put(bo, &bo->mem); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 489 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 490 | ww_mutex_unlock (&bo->resv->lock); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 491 | } |
| 492 | |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 493 | static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 494 | { |
| 495 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 496 | struct ttm_bo_global *glob = bo->glob; |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 497 | struct ttm_bo_driver *driver = bdev->driver; |
Thomas Hellstrom | aa12326 | 2010-11-02 13:21:47 +0000 | [diff] [blame] | 498 | void *sync_obj = NULL; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 499 | int put_count; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 500 | int ret; |
| 501 | |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 502 | spin_lock(&glob->lru_lock); |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 503 | ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 504 | |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 505 | spin_lock(&bdev->fence_lock); |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 506 | (void) ttm_bo_wait(bo, false, false, true); |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 507 | if (!ret && !bo->sync_obj) { |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 508 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 509 | put_count = ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 510 | |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 511 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 512 | ttm_bo_cleanup_memtype_use(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 513 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 514 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 515 | |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 516 | return; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 517 | } |
Thomas Hellstrom | aa12326 | 2010-11-02 13:21:47 +0000 | [diff] [blame] | 518 | if (bo->sync_obj) |
| 519 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 520 | spin_unlock(&bdev->fence_lock); |
| 521 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 522 | if (!ret) |
| 523 | ww_mutex_unlock(&bo->resv->lock); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 524 | |
| 525 | kref_get(&bo->list_kref); |
| 526 | list_add_tail(&bo->ddestroy, &bdev->ddestroy); |
| 527 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 528 | |
Thomas Hellstrom | aa12326 | 2010-11-02 13:21:47 +0000 | [diff] [blame] | 529 | if (sync_obj) { |
Maarten Lankhorst | dedfdff | 2012-10-12 15:04:00 +0000 | [diff] [blame] | 530 | driver->sync_obj_flush(sync_obj); |
Thomas Hellstrom | aa12326 | 2010-11-02 13:21:47 +0000 | [diff] [blame] | 531 | driver->sync_obj_unref(&sync_obj); |
| 532 | } |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 533 | schedule_delayed_work(&bdev->wq, |
| 534 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 535 | } |
| 536 | |
| 537 | /** |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 538 | * function ttm_bo_cleanup_refs_and_unlock |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 539 | * If bo idle, remove from delayed- and lru lists, and unref. |
| 540 | * If not idle, do nothing. |
| 541 | * |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 542 | * Must be called with lru_lock and reservation held, this function |
| 543 | * will drop both before returning. |
| 544 | * |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 545 | * @interruptible Any sleeps should occur interruptibly. |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 546 | * @no_wait_gpu Never wait for gpu. Return -EBUSY instead. |
| 547 | */ |
| 548 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 549 | static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo, |
| 550 | bool interruptible, |
| 551 | bool no_wait_gpu) |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 552 | { |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 553 | struct ttm_bo_device *bdev = bo->bdev; |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 554 | struct ttm_bo_driver *driver = bdev->driver; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 555 | struct ttm_bo_global *glob = bo->glob; |
| 556 | int put_count; |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 557 | int ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 558 | |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 559 | spin_lock(&bdev->fence_lock); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 560 | ret = ttm_bo_wait(bo, false, false, true); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 561 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 562 | if (ret && !no_wait_gpu) { |
| 563 | void *sync_obj; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 564 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 565 | /* |
| 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 Lankhorst | 0953e76 | 2012-12-19 18:21:10 +0100 | [diff] [blame] | 570 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 571 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | 26cc40a | 2011-11-21 13:05:02 +0100 | [diff] [blame] | 572 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 573 | ww_mutex_unlock(&bo->resv->lock); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 574 | spin_unlock(&glob->lru_lock); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 575 | |
| 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 Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 593 | ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 594 | |
| 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 Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 611 | ww_mutex_unlock(&bo->resv->lock); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 612 | spin_unlock(&glob->lru_lock); |
| 613 | return ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 614 | } |
| 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 Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 623 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 624 | |
| 625 | return 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Traverse the delayed list, and call ttm_bo_cleanup_refs on all |
| 630 | * encountered buffers. |
| 631 | */ |
| 632 | |
| 633 | static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) |
| 634 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 635 | struct ttm_bo_global *glob = bdev->glob; |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 636 | struct ttm_buffer_object *entry = NULL; |
| 637 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 638 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 639 | spin_lock(&glob->lru_lock); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 640 | if (list_empty(&bdev->ddestroy)) |
| 641 | goto out_unlock; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 642 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 643 | entry = list_first_entry(&bdev->ddestroy, |
| 644 | struct ttm_buffer_object, ddestroy); |
| 645 | kref_get(&entry->list_kref); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 646 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 647 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 653 | kref_get(&nentry->list_kref); |
| 654 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 655 | |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 656 | 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 Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 664 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 670 | kref_put(&entry->list_kref, ttm_bo_release_list); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 671 | entry = nentry; |
| 672 | |
| 673 | if (ret || !entry) |
| 674 | goto out; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 675 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 676 | spin_lock(&glob->lru_lock); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 677 | if (list_empty(&entry->ddestroy)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 678 | break; |
| 679 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 680 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 681 | out_unlock: |
| 682 | spin_unlock(&glob->lru_lock); |
| 683 | out: |
| 684 | if (entry) |
| 685 | kref_put(&entry->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 686 | return ret; |
| 687 | } |
| 688 | |
| 689 | static 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 | |
| 700 | static 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 Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 705 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 706 | |
Thomas Hellstrom | 82fe50b | 2012-11-21 14:53:21 +0000 | [diff] [blame] | 707 | write_lock(&bdev->vm_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 708 | 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 Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 714 | ttm_mem_io_lock(man, false); |
| 715 | ttm_mem_io_free_vm(bo); |
| 716 | ttm_mem_io_unlock(man); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 717 | ttm_bo_cleanup_refs_or_queue(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 718 | kref_put(&bo->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void ttm_bo_unref(struct ttm_buffer_object **p_bo) |
| 722 | { |
| 723 | struct ttm_buffer_object *bo = *p_bo; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 724 | |
| 725 | *p_bo = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 726 | kref_put(&bo->kref, ttm_bo_release); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 727 | } |
| 728 | EXPORT_SYMBOL(ttm_bo_unref); |
| 729 | |
Matthew Garrett | 7c5ee53 | 2010-04-26 16:00:09 -0400 | [diff] [blame] | 730 | int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev) |
| 731 | { |
| 732 | return cancel_delayed_work_sync(&bdev->wq); |
| 733 | } |
| 734 | EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue); |
| 735 | |
| 736 | void 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 | } |
| 742 | EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue); |
| 743 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 744 | static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 745 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 746 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 747 | struct ttm_bo_device *bdev = bo->bdev; |
| 748 | struct ttm_mem_reg evict_mem; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 749 | struct ttm_placement placement; |
| 750 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 751 | |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 752 | spin_lock(&bdev->fence_lock); |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 753 | ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 754 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 755 | |
Thomas Hellstrom | 78ecf09 | 2009-06-17 12:29:55 +0200 | [diff] [blame] | 756 | if (unlikely(ret != 0)) { |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 757 | if (ret != -ERESTARTSYS) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 758 | pr_err("Failed to expire sync object before buffer eviction\n"); |
Thomas Hellstrom | 78ecf09 | 2009-06-17 12:29:55 +0200 | [diff] [blame] | 759 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 760 | goto out; |
| 761 | } |
| 762 | |
Maarten Lankhorst | a9dbfff | 2012-10-12 16:58:36 +0200 | [diff] [blame] | 763 | BUG_ON(!ttm_bo_is_reserved(bo)); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 764 | |
| 765 | evict_mem = bo->mem; |
| 766 | evict_mem.mm_node = NULL; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 767 | evict_mem.bus.io_reserved_vm = false; |
| 768 | evict_mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 769 | |
Jerome Glisse | 7cb7d1d | 2009-12-09 22:14:27 +0100 | [diff] [blame] | 770 | placement.fpfn = 0; |
| 771 | placement.lpfn = 0; |
| 772 | placement.num_placement = 0; |
| 773 | placement.num_busy_placement = 0; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 774 | bdev->driver->evict_flags(bo, &placement); |
| 775 | ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 776 | no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 777 | if (ret) { |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 778 | if (ret != -ERESTARTSYS) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 779 | pr_err("Failed to find memory space for buffer 0x%p eviction\n", |
| 780 | bo); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 781 | ttm_bo_mem_space_debug(bo, &placement); |
| 782 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 783 | goto out; |
| 784 | } |
| 785 | |
| 786 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 787 | no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 788 | if (ret) { |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 789 | if (ret != -ERESTARTSYS) |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 790 | pr_err("Buffer eviction failed\n"); |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 791 | ttm_bo_mem_put(bo, &evict_mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 792 | goto out; |
| 793 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 794 | bo->evicted = true; |
| 795 | out: |
| 796 | return ret; |
| 797 | } |
| 798 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 799 | static int ttm_mem_evict_first(struct ttm_bo_device *bdev, |
| 800 | uint32_t mem_type, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 801 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 802 | bool no_wait_gpu) |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 803 | { |
| 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 Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 807 | int ret = -EBUSY, put_count; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 808 | |
| 809 | spin_lock(&glob->lru_lock); |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 810 | list_for_each_entry(bo, &man->lru, lru) { |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 811 | ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 812 | if (!ret) |
| 813 | break; |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 816 | if (ret) { |
| 817 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | b8e902f | 2012-10-22 12:51:26 +0000 | [diff] [blame] | 818 | return ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 819 | } |
| 820 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 821 | kref_get(&bo->list_kref); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 822 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 823 | if (!list_empty(&bo->ddestroy)) { |
| 824 | ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible, |
| 825 | no_wait_gpu); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 826 | kref_put(&bo->list_kref, ttm_bo_release_list); |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 827 | return ret; |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | put_count = ttm_bo_del_from_lru(bo); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 831 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 832 | |
| 833 | BUG_ON(ret != 0); |
| 834 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 835 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 836 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 837 | ret = ttm_bo_evict(bo, interruptible, no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 838 | ttm_bo_unreserve(bo); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 839 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 840 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 841 | return ret; |
| 842 | } |
| 843 | |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 844 | void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem) |
| 845 | { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 846 | struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type]; |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 847 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 848 | if (mem->mm_node) |
| 849 | (*man->func->put_node)(man, mem); |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 850 | } |
| 851 | EXPORT_SYMBOL(ttm_bo_mem_put); |
| 852 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 853 | /** |
| 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 Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 857 | static 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 Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 861 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 862 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 863 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 864 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 865 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 866 | int ret; |
| 867 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 868 | do { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 869 | ret = (*man->func->get_node)(man, bo, placement, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 870 | if (unlikely(ret != 0)) |
| 871 | return ret; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 872 | if (mem->mm_node) |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 873 | break; |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 874 | ret = ttm_mem_evict_first(bdev, mem_type, |
| 875 | interruptible, no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 876 | if (unlikely(ret != 0)) |
| 877 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 878 | } while (1); |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 879 | if (mem->mm_node == NULL) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 880 | return -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 881 | mem->mem_type = mem_type; |
| 882 | return 0; |
| 883 | } |
| 884 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 885 | static 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 910 | static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 911 | uint32_t mem_type, |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 912 | uint32_t proposed_placement, |
| 913 | uint32_t *masked_placement) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 914 | { |
| 915 | uint32_t cur_flags = ttm_bo_type_flags(mem_type); |
| 916 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 917 | if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 918 | return false; |
| 919 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 920 | if ((proposed_placement & man->available_caching) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 921 | return false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 922 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 923 | cur_flags |= (proposed_placement & man->available_caching); |
| 924 | |
| 925 | *masked_placement = cur_flags; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 926 | 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 | */ |
| 937 | int ttm_bo_mem_space(struct ttm_buffer_object *bo, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 938 | struct ttm_placement *placement, |
| 939 | struct ttm_mem_reg *mem, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 940 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 941 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 942 | { |
| 943 | struct ttm_bo_device *bdev = bo->bdev; |
| 944 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 945 | uint32_t mem_type = TTM_PL_SYSTEM; |
| 946 | uint32_t cur_flags = 0; |
| 947 | bool type_found = false; |
| 948 | bool type_ok = false; |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 949 | bool has_erestartsys = false; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 950 | int i, ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 951 | |
| 952 | mem->mm_node = NULL; |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 953 | for (i = 0; i < placement->num_placement; ++i) { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 954 | ret = ttm_mem_type_from_flags(placement->placement[i], |
| 955 | &mem_type); |
| 956 | if (ret) |
| 957 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 958 | man = &bdev->man[mem_type]; |
| 959 | |
| 960 | type_ok = ttm_bo_mt_compatible(man, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 961 | mem_type, |
| 962 | placement->placement[i], |
| 963 | &cur_flags); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 964 | |
| 965 | if (!type_ok) |
| 966 | continue; |
| 967 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 968 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 969 | cur_flags); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 970 | /* |
| 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 Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 976 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 977 | if (mem_type == TTM_PL_SYSTEM) |
| 978 | break; |
| 979 | |
| 980 | if (man->has_type && man->use_type) { |
| 981 | type_found = true; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 982 | ret = (*man->func->get_node)(man, bo, placement, mem); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 983 | if (unlikely(ret)) |
| 984 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 985 | } |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 986 | if (mem->mm_node) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 987 | break; |
| 988 | } |
| 989 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 990 | if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 991 | mem->mem_type = mem_type; |
| 992 | mem->placement = cur_flags; |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | if (!type_found) |
| 997 | return -EINVAL; |
| 998 | |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 999 | for (i = 0; i < placement->num_busy_placement; ++i) { |
| 1000 | ret = ttm_mem_type_from_flags(placement->busy_placement[i], |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1001 | &mem_type); |
| 1002 | if (ret) |
| 1003 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1004 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1005 | if (!man->has_type) |
| 1006 | continue; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1007 | if (!ttm_bo_mt_compatible(man, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1008 | mem_type, |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 1009 | placement->busy_placement[i], |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1010 | &cur_flags)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1011 | continue; |
| 1012 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 1013 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 1014 | cur_flags); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1015 | /* |
| 1016 | * Use the access and other non-mapping-related flag bits from |
| 1017 | * the memory placement flags to the current flags |
| 1018 | */ |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 1019 | ttm_flag_masked(&cur_flags, placement->busy_placement[i], |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1020 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 1021 | |
Thomas Hellstrom | 0eaddb2 | 2010-01-16 16:05:04 +0100 | [diff] [blame] | 1022 | |
| 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 Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1030 | ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1031 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1032 | if (ret == 0 && mem->mm_node) { |
| 1033 | mem->placement = cur_flags; |
| 1034 | return 0; |
| 1035 | } |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 1036 | if (ret == -ERESTARTSYS) |
| 1037 | has_erestartsys = true; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1038 | } |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 1039 | ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1040 | return ret; |
| 1041 | } |
| 1042 | EXPORT_SYMBOL(ttm_bo_mem_space); |
| 1043 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1044 | int ttm_bo_move_buffer(struct ttm_buffer_object *bo, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1045 | struct ttm_placement *placement, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1046 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 1047 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1048 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1049 | int ret = 0; |
| 1050 | struct ttm_mem_reg mem; |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1051 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1052 | |
Maarten Lankhorst | a9dbfff | 2012-10-12 16:58:36 +0200 | [diff] [blame] | 1053 | BUG_ON(!ttm_bo_is_reserved(bo)); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1054 | |
| 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 Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1060 | spin_lock(&bdev->fence_lock); |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1061 | ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1062 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1063 | if (ret) |
| 1064 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1065 | mem.num_pages = bo->num_pages; |
| 1066 | mem.size = mem.num_pages << PAGE_SHIFT; |
| 1067 | mem.page_alignment = bo->mem.page_alignment; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1068 | mem.bus.io_reserved_vm = false; |
| 1069 | mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1070 | /* |
| 1071 | * Determine where to move the buffer. |
| 1072 | */ |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1073 | ret = ttm_bo_mem_space(bo, placement, &mem, |
| 1074 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1075 | if (ret) |
| 1076 | goto out_unlock; |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1077 | ret = ttm_bo_handle_move_mem(bo, &mem, false, |
| 1078 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1079 | out_unlock: |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1080 | if (ret && mem.mm_node) |
| 1081 | ttm_bo_mem_put(bo, &mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1082 | return ret; |
| 1083 | } |
| 1084 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1085 | static int ttm_bo_mem_compat(struct ttm_placement *placement, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1086 | struct ttm_mem_reg *mem) |
| 1087 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1088 | int i; |
Thomas Hellstrom | e22238e | 2010-02-12 00:18:00 +0100 | [diff] [blame] | 1089 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1090 | if (mem->mm_node && placement->lpfn != 0 && |
| 1091 | (mem->start < placement->fpfn || |
| 1092 | mem->start + mem->num_pages > placement->lpfn)) |
Thomas Hellstrom | e22238e | 2010-02-12 00:18:00 +0100 | [diff] [blame] | 1093 | return -1; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1094 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1095 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1105 | int ttm_bo_validate(struct ttm_buffer_object *bo, |
| 1106 | struct ttm_placement *placement, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1107 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 1108 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1109 | { |
| 1110 | int ret; |
| 1111 | |
Maarten Lankhorst | a9dbfff | 2012-10-12 16:58:36 +0200 | [diff] [blame] | 1112 | BUG_ON(!ttm_bo_is_reserved(bo)); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1113 | /* 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1118 | /* |
| 1119 | * Check whether we need to move buffer. |
| 1120 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1121 | ret = ttm_bo_mem_compat(placement, &bo->mem); |
| 1122 | if (ret < 0) { |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1123 | ret = ttm_bo_move_buffer(bo, placement, interruptible, |
| 1124 | no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1125 | if (ret) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1126 | return ret; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1127 | } 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1134 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1135 | /* |
| 1136 | * We might need to add a TTM. |
| 1137 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1138 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1143 | return 0; |
| 1144 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1145 | EXPORT_SYMBOL(ttm_bo_validate); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1146 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1147 | int ttm_bo_check_placement(struct ttm_buffer_object *bo, |
| 1148 | struct ttm_placement *placement) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1149 | { |
Thomas Hellstrom | 29e190e | 2010-11-02 13:21:48 +0000 | [diff] [blame] | 1150 | BUG_ON((placement->fpfn || placement->lpfn) && |
| 1151 | (bo->mem.num_pages > (placement->lpfn - placement->fpfn))); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1152 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1153 | return 0; |
| 1154 | } |
| 1155 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1156 | int 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 Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1162 | bool interruptible, |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1163 | struct file *persistent_swap_storage, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1164 | size_t acc_size, |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 1165 | struct sg_table *sg, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1166 | void (*destroy) (struct ttm_buffer_object *)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1167 | { |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1168 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1169 | unsigned long num_pages; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1170 | struct ttm_mem_global *mem_glob = bdev->glob->mem_glob; |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 1171 | bool locked; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1172 | |
| 1173 | ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); |
| 1174 | if (ret) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1175 | pr_err("Out of kernel memory\n"); |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1176 | if (destroy) |
| 1177 | (*destroy)(bo); |
| 1178 | else |
| 1179 | kfree(bo); |
| 1180 | return -ENOMEM; |
| 1181 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1182 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1183 | num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1184 | if (num_pages == 0) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1185 | pr_err("Illegal buffer object size\n"); |
Thomas Hellstrom | 7dfbbdc | 2010-11-09 21:31:44 +0100 | [diff] [blame] | 1186 | if (destroy) |
| 1187 | (*destroy)(bo); |
| 1188 | else |
| 1189 | kfree(bo); |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1190 | ttm_mem_global_free(mem_glob, acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1191 | return -EINVAL; |
| 1192 | } |
| 1193 | bo->destroy = destroy; |
| 1194 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1195 | kref_init(&bo->kref); |
| 1196 | kref_init(&bo->list_kref); |
| 1197 | atomic_set(&bo->cpu_writers, 0); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1198 | INIT_LIST_HEAD(&bo->lru); |
| 1199 | INIT_LIST_HEAD(&bo->ddestroy); |
| 1200 | INIT_LIST_HEAD(&bo->swap); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1201 | INIT_LIST_HEAD(&bo->io_reserve_lru); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1202 | bo->bdev = bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1203 | bo->glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1204 | bo->type = type; |
| 1205 | bo->num_pages = num_pages; |
Jerome Glisse | eb6d2c3 | 2009-12-10 16:15:52 +0100 | [diff] [blame] | 1206 | bo->mem.size = num_pages << PAGE_SHIFT; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1207 | 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 Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1211 | bo->mem.bus.io_reserved_vm = false; |
| 1212 | bo->mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1213 | bo->priv_flags = 0; |
| 1214 | bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1215 | bo->persistent_swap_storage = persistent_swap_storage; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1216 | bo->acc_size = acc_size; |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 1217 | bo->sg = sg; |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 1218 | bo->resv = &bo->ttm_resv; |
| 1219 | reservation_object_init(bo->resv); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1220 | atomic_inc(&bo->glob->bo_count); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1221 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1222 | ret = ttm_bo_check_placement(bo, placement); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1223 | |
| 1224 | /* |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1225 | * For ttm_bo_type_device buffers, allocate |
| 1226 | * address space from the device. |
| 1227 | */ |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 1228 | if (likely(!ret) && |
| 1229 | (bo->type == ttm_bo_type_device || |
| 1230 | bo->type == ttm_bo_type_sg)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1231 | ret = ttm_bo_setup_vm(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1232 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 1233 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1238 | |
| 1239 | ttm_bo_unreserve(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1240 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 1241 | if (unlikely(ret)) |
| 1242 | ttm_bo_unref(&bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1243 | |
| 1244 | return ret; |
| 1245 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1246 | EXPORT_SYMBOL(ttm_bo_init); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1247 | |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1248 | size_t ttm_bo_acc_size(struct ttm_bo_device *bdev, |
| 1249 | unsigned long bo_size, |
| 1250 | unsigned struct_size) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1251 | { |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1252 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; |
| 1253 | size_t size = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1254 | |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1255 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1259 | } |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1260 | EXPORT_SYMBOL(ttm_bo_acc_size); |
| 1261 | |
| 1262 | size_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 | } |
| 1275 | EXPORT_SYMBOL(ttm_bo_dma_acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1276 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1277 | int 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 Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1282 | bool interruptible, |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1283 | struct file *persistent_swap_storage, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1284 | struct ttm_buffer_object **p_bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1285 | { |
| 1286 | struct ttm_buffer_object *bo; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1287 | size_t acc_size; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1288 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1289 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1290 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1291 | if (unlikely(bo == NULL)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1292 | return -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1293 | |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1294 | acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object)); |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1295 | ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment, |
Marcin Slusarz | 0b91c4a | 2012-11-06 21:49:51 +0000 | [diff] [blame] | 1296 | interruptible, persistent_swap_storage, acc_size, |
| 1297 | NULL, NULL); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1298 | if (likely(ret == 0)) |
| 1299 | *p_bo = bo; |
| 1300 | |
| 1301 | return ret; |
| 1302 | } |
Thomas Hellstrom | 4d79893 | 2011-10-04 20:13:11 +0200 | [diff] [blame] | 1303 | EXPORT_SYMBOL(ttm_bo_create); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1304 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1305 | static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1306 | unsigned mem_type, bool allow_errors) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1307 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1308 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1309 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1310 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1311 | |
| 1312 | /* |
| 1313 | * Can't use standard list traversal since we're unlocking. |
| 1314 | */ |
| 1315 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1316 | spin_lock(&glob->lru_lock); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1317 | while (!list_empty(&man->lru)) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1318 | spin_unlock(&glob->lru_lock); |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1319 | ret = ttm_mem_evict_first(bdev, mem_type, false, false); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1320 | if (ret) { |
| 1321 | if (allow_errors) { |
| 1322 | return ret; |
| 1323 | } else { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1324 | pr_err("Cleanup eviction failed\n"); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1325 | } |
| 1326 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1327 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1328 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1329 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1334 | { |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1335 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1336 | int ret = -EINVAL; |
| 1337 | |
| 1338 | if (mem_type >= TTM_NUM_MEM_TYPES) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1339 | pr_err("Illegal memory type %d\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1340 | return ret; |
| 1341 | } |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1342 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1343 | |
| 1344 | if (!man->has_type) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1345 | pr_err("Trying to take down uninitialized memory manager type %u\n", |
| 1346 | mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1347 | return ret; |
| 1348 | } |
| 1349 | |
| 1350 | man->use_type = false; |
| 1351 | man->has_type = false; |
| 1352 | |
| 1353 | ret = 0; |
| 1354 | if (mem_type > 0) { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1355 | ttm_bo_force_list_clean(bdev, mem_type, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1356 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1357 | ret = (*man->func->takedown)(man); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | return ret; |
| 1361 | } |
| 1362 | EXPORT_SYMBOL(ttm_bo_clean_mm); |
| 1363 | |
| 1364 | int 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 Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1369 | pr_err("Illegal memory manager memory type %u\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1370 | return -EINVAL; |
| 1371 | } |
| 1372 | |
| 1373 | if (!man->has_type) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1374 | pr_err("Memory type %u has not been initialized\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1375 | return 0; |
| 1376 | } |
| 1377 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1378 | return ttm_bo_force_list_clean(bdev, mem_type, true); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1379 | } |
| 1380 | EXPORT_SYMBOL(ttm_bo_evict_mm); |
| 1381 | |
| 1382 | int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1383 | unsigned long p_size) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1384 | { |
| 1385 | int ret = -EINVAL; |
| 1386 | struct ttm_mem_type_manager *man; |
| 1387 | |
Thomas Hellstrom | dbc4a5b | 2010-10-29 10:46:47 +0200 | [diff] [blame] | 1388 | BUG_ON(type >= TTM_NUM_MEM_TYPES); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1389 | man = &bdev->man[type]; |
Thomas Hellstrom | dbc4a5b | 2010-10-29 10:46:47 +0200 | [diff] [blame] | 1390 | BUG_ON(man->has_type); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1391 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1395 | |
| 1396 | ret = bdev->driver->init_mem_type(bdev, type, man); |
| 1397 | if (ret) |
| 1398 | return ret; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1399 | man->bdev = bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1400 | |
| 1401 | ret = 0; |
| 1402 | if (type != TTM_PL_SYSTEM) { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1403 | ret = (*man->func->init)(man, p_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1404 | 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 | } |
| 1415 | EXPORT_SYMBOL(ttm_bo_init_mm); |
| 1416 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1417 | static 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 Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1422 | ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink); |
| 1423 | __free_page(glob->dummy_read_page); |
| 1424 | kfree(glob); |
| 1425 | } |
| 1426 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 1427 | void ttm_bo_global_release(struct drm_global_reference *ref) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1428 | { |
| 1429 | struct ttm_bo_global *glob = ref->object; |
| 1430 | |
| 1431 | kobject_del(&glob->kobj); |
| 1432 | kobject_put(&glob->kobj); |
| 1433 | } |
| 1434 | EXPORT_SYMBOL(ttm_bo_global_release); |
| 1435 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 1436 | int ttm_bo_global_init(struct drm_global_reference *ref) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1437 | { |
| 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 Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1459 | pr_err("Could not register buffer object swapout\n"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1460 | goto out_no_shrink; |
| 1461 | } |
| 1462 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1463 | atomic_set(&glob->bo_count, 0); |
| 1464 | |
Robert P. J. Day | b642ed0 | 2010-03-13 10:36:32 +0000 | [diff] [blame] | 1465 | ret = kobject_init_and_add( |
| 1466 | &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1467 | if (unlikely(ret != 0)) |
| 1468 | kobject_put(&glob->kobj); |
| 1469 | return ret; |
| 1470 | out_no_shrink: |
| 1471 | __free_page(glob->dummy_read_page); |
| 1472 | out_no_drp: |
| 1473 | kfree(glob); |
| 1474 | return ret; |
| 1475 | } |
| 1476 | EXPORT_SYMBOL(ttm_bo_global_init); |
| 1477 | |
| 1478 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1479 | int 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 Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1484 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1485 | |
| 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 Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1492 | pr_err("DRM memory manager type %d is not clean\n", |
| 1493 | i); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1494 | } |
| 1495 | man->has_type = false; |
| 1496 | } |
| 1497 | } |
| 1498 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1499 | mutex_lock(&glob->device_list_mutex); |
| 1500 | list_del(&bdev->device_list); |
| 1501 | mutex_unlock(&glob->device_list_mutex); |
| 1502 | |
Tejun Heo | f094cfc | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 1503 | cancel_delayed_work_sync(&bdev->wq); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1504 | |
| 1505 | while (ttm_bo_delayed_delete(bdev, true)) |
| 1506 | ; |
| 1507 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1508 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1509 | 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 Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1514 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1515 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1516 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1521 | return ret; |
| 1522 | } |
| 1523 | EXPORT_SYMBOL(ttm_bo_device_release); |
| 1524 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1525 | int ttm_bo_device_init(struct ttm_bo_device *bdev, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1526 | struct ttm_bo_global *glob, |
| 1527 | struct ttm_bo_driver *driver, |
Dave Airlie | 51c8b40 | 2009-08-20 13:38:04 +1000 | [diff] [blame] | 1528 | uint64_t file_page_offset, |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1529 | bool need_dma32) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1530 | { |
| 1531 | int ret = -EINVAL; |
| 1532 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1533 | rwlock_init(&bdev->vm_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1534 | bdev->driver = driver; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1535 | |
| 1536 | memset(bdev->man, 0, sizeof(bdev->man)); |
| 1537 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1538 | /* |
| 1539 | * Initialize the system memory buffer type. |
| 1540 | * Other types need to be driver / IOCTL initialized. |
| 1541 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1542 | ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1543 | if (unlikely(ret != 0)) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1544 | goto out_no_sys; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1545 | |
| 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 Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1549 | goto out_no_addr_mm; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1550 | |
| 1551 | INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1552 | INIT_LIST_HEAD(&bdev->ddestroy); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1553 | bdev->dev_mapping = NULL; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1554 | bdev->glob = glob; |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1555 | bdev->need_dma32 = need_dma32; |
Thomas Hellstrom | 6570596 | 2010-11-17 12:28:31 +0000 | [diff] [blame] | 1556 | bdev->val_seq = 0; |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1557 | spin_lock_init(&bdev->fence_lock); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1558 | mutex_lock(&glob->device_list_mutex); |
| 1559 | list_add_tail(&bdev->device_list, &glob->device_list); |
| 1560 | mutex_unlock(&glob->device_list_mutex); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1561 | |
| 1562 | return 0; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1563 | out_no_addr_mm: |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1564 | ttm_bo_clean_mm(bdev, 0); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1565 | out_no_sys: |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1566 | return ret; |
| 1567 | } |
| 1568 | EXPORT_SYMBOL(ttm_bo_device_init); |
| 1569 | |
| 1570 | /* |
| 1571 | * buffer object vm functions. |
| 1572 | */ |
| 1573 | |
| 1574 | bool 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 Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1591 | void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1592 | { |
| 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1599 | unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1600 | ttm_mem_io_free_vm(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1601 | } |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1602 | |
| 1603 | void 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 Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 1614 | EXPORT_SYMBOL(ttm_bo_unmap_virtual); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1615 | |
| 1616 | static 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 | |
| 1652 | static int ttm_bo_setup_vm(struct ttm_buffer_object *bo) |
| 1653 | { |
| 1654 | struct ttm_bo_device *bdev = bo->bdev; |
| 1655 | int ret; |
| 1656 | |
| 1657 | retry_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; |
| 1684 | out_unlock: |
| 1685 | write_unlock(&bdev->vm_lock); |
| 1686 | return ret; |
| 1687 | } |
| 1688 | |
| 1689 | int ttm_bo_wait(struct ttm_buffer_object *bo, |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1690 | bool lazy, bool interruptible, bool no_wait) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1691 | { |
| 1692 | struct ttm_bo_driver *driver = bo->bdev->driver; |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1693 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1694 | void *sync_obj; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1695 | int ret = 0; |
| 1696 | |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1697 | if (likely(bo->sync_obj == NULL)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1698 | return 0; |
| 1699 | |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1700 | while (bo->sync_obj) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1701 | |
Maarten Lankhorst | dedfdff | 2012-10-12 15:04:00 +0000 | [diff] [blame] | 1702 | if (driver->sync_obj_signaled(bo->sync_obj)) { |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1703 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1709 | continue; |
| 1710 | } |
| 1711 | |
| 1712 | if (no_wait) |
| 1713 | return -EBUSY; |
| 1714 | |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1715 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1716 | spin_unlock(&bdev->fence_lock); |
Maarten Lankhorst | dedfdff | 2012-10-12 15:04:00 +0000 | [diff] [blame] | 1717 | ret = driver->sync_obj_wait(sync_obj, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1718 | lazy, interruptible); |
| 1719 | if (unlikely(ret != 0)) { |
| 1720 | driver->sync_obj_unref(&sync_obj); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1721 | spin_lock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1722 | return ret; |
| 1723 | } |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1724 | spin_lock(&bdev->fence_lock); |
Maarten Lankhorst | 5fb4ef0 | 2012-10-12 15:02:19 +0000 | [diff] [blame] | 1725 | if (likely(bo->sync_obj == sync_obj)) { |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1726 | 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 Hellstrom | fee280d | 2009-08-03 12:39:06 +0200 | [diff] [blame] | 1734 | } else { |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1735 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | fee280d | 2009-08-03 12:39:06 +0200 | [diff] [blame] | 1736 | driver->sync_obj_unref(&sync_obj); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1737 | spin_lock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | return 0; |
| 1741 | } |
| 1742 | EXPORT_SYMBOL(ttm_bo_wait); |
| 1743 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1744 | int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait) |
| 1745 | { |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1746 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1747 | int ret = 0; |
| 1748 | |
| 1749 | /* |
Thomas Hellstrom | 8cfe92d | 2010-04-28 11:33:25 +0200 | [diff] [blame] | 1750 | * Using ttm_bo_reserve makes sure the lru lists are updated. |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1751 | */ |
| 1752 | |
| 1753 | ret = ttm_bo_reserve(bo, true, no_wait, false, 0); |
| 1754 | if (unlikely(ret != 0)) |
| 1755 | return ret; |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1756 | spin_lock(&bdev->fence_lock); |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1757 | ret = ttm_bo_wait(bo, false, true, no_wait); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1758 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1759 | if (likely(ret == 0)) |
| 1760 | atomic_inc(&bo->cpu_writers); |
| 1761 | ttm_bo_unreserve(bo); |
| 1762 | return ret; |
| 1763 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 1764 | EXPORT_SYMBOL(ttm_bo_synccpu_write_grab); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1765 | |
| 1766 | void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo) |
| 1767 | { |
Maarten Lankhorst | 654aa792 | 2012-11-06 14:39:43 +0100 | [diff] [blame] | 1768 | atomic_dec(&bo->cpu_writers); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1769 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 1770 | EXPORT_SYMBOL(ttm_bo_synccpu_write_release); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1771 | |
| 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 | |
| 1777 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink) |
| 1778 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1779 | struct ttm_bo_global *glob = |
| 1780 | container_of(shrink, struct ttm_bo_global, shrink); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1781 | 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 Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1786 | spin_lock(&glob->lru_lock); |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1787 | list_for_each_entry(bo, &glob->swap_lru, swap) { |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 1788 | ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1789 | if (!ret) |
| 1790 | break; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1791 | } |
| 1792 | |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1793 | 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 Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1806 | put_count = ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1807 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1808 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 1809 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1810 | |
| 1811 | /** |
| 1812 | * Wait for GPU, then move to system cached. |
| 1813 | */ |
| 1814 | |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1815 | spin_lock(&bo->bdev->fence_lock); |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1816 | ret = ttm_bo_wait(bo, false, false, false); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1817 | spin_unlock(&bo->bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1818 | |
| 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 Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1831 | false, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1832 | 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 Hellstrom | 3f09ea4 | 2010-01-13 22:28:40 +0100 | [diff] [blame] | 1843 | if (bo->bdev->driver->swap_notify) |
| 1844 | bo->bdev->driver->swap_notify(bo); |
| 1845 | |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1846 | ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1847 | out: |
| 1848 | |
| 1849 | /** |
| 1850 | * |
| 1851 | * Unreserve without putting on LRU to avoid swapping out an |
| 1852 | * already swapped buffer. |
| 1853 | */ |
| 1854 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame^] | 1855 | ww_mutex_unlock(&bo->resv->lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1856 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 1857 | return ret; |
| 1858 | } |
| 1859 | |
| 1860 | void ttm_bo_swapout_all(struct ttm_bo_device *bdev) |
| 1861 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1862 | while (ttm_bo_swapout(&bdev->glob->shrink) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1863 | ; |
| 1864 | } |
Thomas Hellstrom | e99e1e7 | 2010-01-13 22:28:42 +0100 | [diff] [blame] | 1865 | EXPORT_SYMBOL(ttm_bo_swapout_all); |