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); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 153 | if (bo->destroy) |
| 154 | bo->destroy(bo); |
| 155 | else { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 156 | kfree(bo); |
| 157 | } |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 158 | ttm_mem_global_free(bdev->glob->mem_glob, acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Maarten Lankhorst | cc4c0c4 | 2013-01-15 14:57:28 +0100 | [diff] [blame] | 161 | static int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, |
| 162 | bool interruptible) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 163 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 164 | if (interruptible) { |
Jean Delvare | 965d380 | 2010-10-09 12:36:45 +0000 | [diff] [blame] | 165 | return wait_event_interruptible(bo->event_queue, |
Maarten Lankhorst | a9dbfff | 2012-10-12 16:58:36 +0200 | [diff] [blame] | 166 | !ttm_bo_is_reserved(bo)); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 167 | } else { |
Maarten Lankhorst | a9dbfff | 2012-10-12 16:58:36 +0200 | [diff] [blame] | 168 | wait_event(bo->event_queue, !ttm_bo_is_reserved(bo)); |
Jean Delvare | 965d380 | 2010-10-09 12:36:45 +0000 | [diff] [blame] | 169 | return 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 170 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 173 | void ttm_bo_add_to_lru(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 174 | { |
| 175 | struct ttm_bo_device *bdev = bo->bdev; |
| 176 | struct ttm_mem_type_manager *man; |
| 177 | |
Maarten Lankhorst | a9dbfff | 2012-10-12 16:58:36 +0200 | [diff] [blame] | 178 | BUG_ON(!ttm_bo_is_reserved(bo)); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 179 | |
| 180 | if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { |
| 181 | |
| 182 | BUG_ON(!list_empty(&bo->lru)); |
| 183 | |
| 184 | man = &bdev->man[bo->mem.mem_type]; |
| 185 | list_add_tail(&bo->lru, &man->lru); |
| 186 | kref_get(&bo->list_kref); |
| 187 | |
| 188 | if (bo->ttm != NULL) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 189 | list_add_tail(&bo->swap, &bo->glob->swap_lru); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 190 | kref_get(&bo->list_kref); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 195 | int ttm_bo_del_from_lru(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 196 | { |
| 197 | int put_count = 0; |
| 198 | |
| 199 | if (!list_empty(&bo->swap)) { |
| 200 | list_del_init(&bo->swap); |
| 201 | ++put_count; |
| 202 | } |
| 203 | if (!list_empty(&bo->lru)) { |
| 204 | list_del_init(&bo->lru); |
| 205 | ++put_count; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * TODO: Add a driver hook to delete from |
| 210 | * driver-specific LRU's here. |
| 211 | */ |
| 212 | |
| 213 | return put_count; |
| 214 | } |
| 215 | |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 216 | int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 217 | bool interruptible, |
| 218 | bool no_wait, bool use_sequence, uint32_t sequence) |
| 219 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 220 | int ret; |
| 221 | |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 222 | while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) { |
Thomas Hellstrom | 95ccb0f | 2010-11-11 10:04:53 +0100 | [diff] [blame] | 223 | /** |
| 224 | * Deadlock avoidance for multi-bo reserving. |
| 225 | */ |
Thomas Hellstrom | 96726fe | 2010-11-17 12:28:28 +0000 | [diff] [blame] | 226 | if (use_sequence && bo->seq_valid) { |
| 227 | /** |
| 228 | * We've already reserved this one. |
| 229 | */ |
| 230 | if (unlikely(sequence == bo->val_seq)) |
| 231 | return -EDEADLK; |
| 232 | /** |
| 233 | * Already reserved by a thread that will not back |
| 234 | * off for us. We need to back off. |
| 235 | */ |
| 236 | if (unlikely(sequence - bo->val_seq < (1 << 31))) |
| 237 | return -EAGAIN; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | if (no_wait) |
| 241 | return -EBUSY; |
| 242 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 243 | ret = ttm_bo_wait_unreserved(bo, interruptible); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 244 | |
| 245 | if (unlikely(ret)) |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | if (use_sequence) { |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 250 | bool wake_up = false; |
Thomas Hellstrom | 95ccb0f | 2010-11-11 10:04:53 +0100 | [diff] [blame] | 251 | /** |
| 252 | * Wake up waiters that may need to recheck for deadlock, |
| 253 | * if we decreased the sequence number. |
| 254 | */ |
| 255 | if (unlikely((bo->val_seq - sequence < (1 << 31)) |
| 256 | || !bo->seq_valid)) |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 257 | wake_up = true; |
Thomas Hellstrom | 95ccb0f | 2010-11-11 10:04:53 +0100 | [diff] [blame] | 258 | |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 259 | /* |
| 260 | * In the worst case with memory ordering these values can be |
| 261 | * seen in the wrong order. However since we call wake_up_all |
| 262 | * in that case, this will hopefully not pose a problem, |
| 263 | * and the worst case would only cause someone to accidentally |
| 264 | * hit -EAGAIN in ttm_bo_reserve when they see old value of |
| 265 | * val_seq. However this would only happen if seq_valid was |
| 266 | * written before val_seq was, and just means some slightly |
| 267 | * increased cpu usage |
| 268 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 269 | bo->val_seq = sequence; |
| 270 | bo->seq_valid = true; |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 271 | if (wake_up) |
| 272 | wake_up_all(&bo->event_queue); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 273 | } else { |
| 274 | bo->seq_valid = false; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | EXPORT_SYMBOL(ttm_bo_reserve); |
| 280 | |
| 281 | static void ttm_bo_ref_bug(struct kref *list_kref) |
| 282 | { |
| 283 | BUG(); |
| 284 | } |
| 285 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 286 | void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count, |
| 287 | bool never_free) |
| 288 | { |
Thomas Hellstrom | 2357cbe | 2010-11-16 15:21:08 +0100 | [diff] [blame] | 289 | kref_sub(&bo->list_kref, count, |
| 290 | (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list); |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 291 | } |
| 292 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 293 | int ttm_bo_reserve(struct ttm_buffer_object *bo, |
| 294 | bool interruptible, |
| 295 | bool no_wait, bool use_sequence, uint32_t sequence) |
| 296 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 297 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 298 | int put_count = 0; |
| 299 | int ret; |
| 300 | |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 301 | ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_sequence, |
| 302 | sequence); |
| 303 | if (likely(ret == 0)) { |
| 304 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 305 | put_count = ttm_bo_del_from_lru(bo); |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 306 | spin_unlock(&glob->lru_lock); |
| 307 | ttm_bo_list_ref_sub(bo, put_count, true); |
| 308 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 309 | |
| 310 | return ret; |
| 311 | } |
| 312 | |
Maarten Lankhorst | 5e45d7d | 2013-01-15 14:57:05 +0100 | [diff] [blame] | 313 | int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo, |
| 314 | bool interruptible, uint32_t sequence) |
| 315 | { |
| 316 | bool wake_up = false; |
| 317 | int ret; |
| 318 | |
| 319 | while (unlikely(atomic_xchg(&bo->reserved, 1) != 0)) { |
| 320 | WARN_ON(bo->seq_valid && sequence == bo->val_seq); |
| 321 | |
| 322 | ret = ttm_bo_wait_unreserved(bo, interruptible); |
| 323 | |
| 324 | if (unlikely(ret)) |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | if ((bo->val_seq - sequence < (1 << 31)) || !bo->seq_valid) |
| 329 | wake_up = true; |
| 330 | |
| 331 | /** |
| 332 | * Wake up waiters that may need to recheck for deadlock, |
| 333 | * if we decreased the sequence number. |
| 334 | */ |
| 335 | bo->val_seq = sequence; |
| 336 | bo->seq_valid = true; |
| 337 | if (wake_up) |
| 338 | wake_up_all(&bo->event_queue); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo, |
| 344 | bool interruptible, uint32_t sequence) |
| 345 | { |
| 346 | struct ttm_bo_global *glob = bo->glob; |
| 347 | int put_count, ret; |
| 348 | |
| 349 | ret = ttm_bo_reserve_slowpath_nolru(bo, interruptible, sequence); |
| 350 | if (likely(!ret)) { |
| 351 | spin_lock(&glob->lru_lock); |
| 352 | put_count = ttm_bo_del_from_lru(bo); |
| 353 | spin_unlock(&glob->lru_lock); |
| 354 | ttm_bo_list_ref_sub(bo, put_count, true); |
| 355 | } |
| 356 | return ret; |
| 357 | } |
| 358 | EXPORT_SYMBOL(ttm_bo_reserve_slowpath); |
| 359 | |
Thomas Hellstrom | 95762c2 | 2010-11-17 12:28:30 +0000 | [diff] [blame] | 360 | void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo) |
| 361 | { |
| 362 | ttm_bo_add_to_lru(bo); |
| 363 | atomic_set(&bo->reserved, 0); |
| 364 | wake_up_all(&bo->event_queue); |
| 365 | } |
| 366 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 367 | void ttm_bo_unreserve(struct ttm_buffer_object *bo) |
| 368 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 369 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 370 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 371 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | 95762c2 | 2010-11-17 12:28:30 +0000 | [diff] [blame] | 372 | ttm_bo_unreserve_locked(bo); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 373 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 374 | } |
| 375 | EXPORT_SYMBOL(ttm_bo_unreserve); |
| 376 | |
| 377 | /* |
| 378 | * Call bo->mutex locked. |
| 379 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 380 | static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc) |
| 381 | { |
| 382 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 383 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 384 | int ret = 0; |
| 385 | uint32_t page_flags = 0; |
| 386 | |
| 387 | TTM_ASSERT_LOCKED(&bo->mutex); |
| 388 | bo->ttm = NULL; |
| 389 | |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 390 | if (bdev->need_dma32) |
| 391 | page_flags |= TTM_PAGE_FLAG_DMA32; |
| 392 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 393 | switch (bo->type) { |
| 394 | case ttm_bo_type_device: |
| 395 | if (zero_alloc) |
| 396 | page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC; |
| 397 | case ttm_bo_type_kernel: |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 398 | bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
| 399 | page_flags, glob->dummy_read_page); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 400 | if (unlikely(bo->ttm == NULL)) |
| 401 | ret = -ENOMEM; |
| 402 | break; |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 403 | case ttm_bo_type_sg: |
| 404 | bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
| 405 | page_flags | TTM_PAGE_FLAG_SG, |
| 406 | glob->dummy_read_page); |
| 407 | if (unlikely(bo->ttm == NULL)) { |
| 408 | ret = -ENOMEM; |
| 409 | break; |
| 410 | } |
| 411 | bo->ttm->sg = bo->sg; |
| 412 | break; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 413 | default: |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 414 | pr_err("Illegal buffer object type\n"); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 415 | ret = -EINVAL; |
| 416 | break; |
| 417 | } |
| 418 | |
| 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, |
| 423 | struct ttm_mem_reg *mem, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 424 | bool evict, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 425 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 426 | { |
| 427 | struct ttm_bo_device *bdev = bo->bdev; |
| 428 | bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem); |
| 429 | bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem); |
| 430 | struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type]; |
| 431 | struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type]; |
| 432 | int ret = 0; |
| 433 | |
| 434 | if (old_is_pci || new_is_pci || |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 435 | ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) { |
| 436 | ret = ttm_mem_io_lock(old_man, true); |
| 437 | if (unlikely(ret != 0)) |
| 438 | goto out_err; |
| 439 | ttm_bo_unmap_virtual_locked(bo); |
| 440 | ttm_mem_io_unlock(old_man); |
| 441 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * Create and bind a ttm if required. |
| 445 | */ |
| 446 | |
Ben Skeggs | 8d3bb23 | 2011-08-22 03:15:05 +0000 | [diff] [blame] | 447 | if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
| 448 | if (bo->ttm == NULL) { |
Ben Skeggs | ff02b13 | 2011-09-14 06:08:06 +1000 | [diff] [blame] | 449 | bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED); |
| 450 | ret = ttm_bo_add_ttm(bo, zero); |
Ben Skeggs | 8d3bb23 | 2011-08-22 03:15:05 +0000 | [diff] [blame] | 451 | if (ret) |
| 452 | goto out_err; |
| 453 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 454 | |
| 455 | ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement); |
| 456 | if (ret) |
Thomas Hellstrom | 87ef920 | 2009-06-17 12:29:57 +0200 | [diff] [blame] | 457 | goto out_err; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 458 | |
| 459 | if (mem->mem_type != TTM_PL_SYSTEM) { |
| 460 | ret = ttm_tt_bind(bo->ttm, mem); |
| 461 | if (ret) |
| 462 | goto out_err; |
| 463 | } |
| 464 | |
| 465 | if (bo->mem.mem_type == TTM_PL_SYSTEM) { |
Ben Skeggs | 82ef594 | 2011-02-02 00:27:10 +0000 | [diff] [blame] | 466 | if (bdev->driver->move_notify) |
| 467 | bdev->driver->move_notify(bo, mem); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 468 | bo->mem = *mem; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 469 | mem->mm_node = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 470 | goto moved; |
| 471 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 474 | if (bdev->driver->move_notify) |
| 475 | bdev->driver->move_notify(bo, mem); |
| 476 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 477 | if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && |
| 478 | !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 479 | ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 480 | else if (bdev->driver->move) |
| 481 | ret = bdev->driver->move(bo, evict, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 482 | no_wait_gpu, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 483 | else |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 484 | ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 485 | |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 486 | if (ret) { |
| 487 | if (bdev->driver->move_notify) { |
| 488 | struct ttm_mem_reg tmp_mem = *mem; |
| 489 | *mem = bo->mem; |
| 490 | bo->mem = tmp_mem; |
| 491 | bdev->driver->move_notify(bo, mem); |
| 492 | bo->mem = *mem; |
Dave Airlie | 014b344 | 2013-01-16 15:58:34 +1000 | [diff] [blame] | 493 | *mem = tmp_mem; |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 494 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 495 | |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 496 | goto out_err; |
| 497 | } |
Jerome Glisse | dc97b34 | 2011-11-18 11:47:03 -0500 | [diff] [blame] | 498 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 499 | moved: |
| 500 | if (bo->evicted) { |
| 501 | ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); |
| 502 | if (ret) |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 503 | pr_err("Can not flush read caches\n"); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 504 | bo->evicted = false; |
| 505 | } |
| 506 | |
| 507 | if (bo->mem.mm_node) { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 508 | bo->offset = (bo->mem.start << PAGE_SHIFT) + |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 509 | bdev->man[bo->mem.mem_type].gpu_offset; |
| 510 | bo->cur_placement = bo->mem.placement; |
Thomas Hellstrom | 354fb52 | 2010-01-13 22:28:45 +0100 | [diff] [blame] | 511 | } else |
| 512 | bo->offset = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 513 | |
| 514 | return 0; |
| 515 | |
| 516 | out_err: |
| 517 | new_man = &bdev->man[bo->mem.mem_type]; |
| 518 | if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) { |
| 519 | ttm_tt_unbind(bo->ttm); |
| 520 | ttm_tt_destroy(bo->ttm); |
| 521 | bo->ttm = NULL; |
| 522 | } |
| 523 | |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | /** |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 528 | * Call bo::reserved. |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 529 | * Will release GPU memory type usage on destruction. |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 530 | * This is the place to put in driver specific hooks to release |
| 531 | * driver private resources. |
| 532 | * Will release the bo::reserved lock. |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 533 | */ |
| 534 | |
| 535 | static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) |
| 536 | { |
Jerome Glisse | dc97b34 | 2011-11-18 11:47:03 -0500 | [diff] [blame] | 537 | if (bo->bdev->driver->move_notify) |
| 538 | bo->bdev->driver->move_notify(bo, NULL); |
| 539 | |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 540 | if (bo->ttm) { |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 541 | ttm_tt_unbind(bo->ttm); |
| 542 | ttm_tt_destroy(bo->ttm); |
| 543 | bo->ttm = NULL; |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 544 | } |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 545 | ttm_bo_mem_put(bo, &bo->mem); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 546 | |
| 547 | atomic_set(&bo->reserved, 0); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 548 | wake_up_all(&bo->event_queue); |
Thomas Hellstrom | 06fba6d | 2010-10-29 10:46:48 +0200 | [diff] [blame] | 549 | |
| 550 | /* |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 551 | * Since the final reference to this bo may not be dropped by |
| 552 | * the current task we have to put a memory barrier here to make |
| 553 | * sure the changes done in this function are always visible. |
| 554 | * |
| 555 | * This function only needs protection against the final kref_put. |
Thomas Hellstrom | 06fba6d | 2010-10-29 10:46:48 +0200 | [diff] [blame] | 556 | */ |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 557 | smp_mb__before_atomic_dec(); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 558 | } |
| 559 | |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 560 | 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] | 561 | { |
| 562 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 563 | struct ttm_bo_global *glob = bo->glob; |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 564 | struct ttm_bo_driver *driver = bdev->driver; |
Thomas Hellstrom | aa12326 | 2010-11-02 13:21:47 +0000 | [diff] [blame] | 565 | void *sync_obj = NULL; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 566 | int put_count; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 567 | int ret; |
| 568 | |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 569 | spin_lock(&glob->lru_lock); |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 570 | ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 571 | |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 572 | spin_lock(&bdev->fence_lock); |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 573 | (void) ttm_bo_wait(bo, false, false, true); |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 574 | if (!ret && !bo->sync_obj) { |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 575 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 576 | put_count = ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 577 | |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 578 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 579 | ttm_bo_cleanup_memtype_use(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 580 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 581 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 582 | |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 583 | return; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 584 | } |
Thomas Hellstrom | aa12326 | 2010-11-02 13:21:47 +0000 | [diff] [blame] | 585 | if (bo->sync_obj) |
| 586 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 587 | spin_unlock(&bdev->fence_lock); |
| 588 | |
| 589 | if (!ret) { |
| 590 | atomic_set(&bo->reserved, 0); |
| 591 | wake_up_all(&bo->event_queue); |
| 592 | } |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 593 | |
| 594 | kref_get(&bo->list_kref); |
| 595 | list_add_tail(&bo->ddestroy, &bdev->ddestroy); |
| 596 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 597 | |
Thomas Hellstrom | aa12326 | 2010-11-02 13:21:47 +0000 | [diff] [blame] | 598 | if (sync_obj) { |
Maarten Lankhorst | dedfdff | 2012-10-12 15:04:00 +0000 | [diff] [blame] | 599 | driver->sync_obj_flush(sync_obj); |
Thomas Hellstrom | aa12326 | 2010-11-02 13:21:47 +0000 | [diff] [blame] | 600 | driver->sync_obj_unref(&sync_obj); |
| 601 | } |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 602 | schedule_delayed_work(&bdev->wq, |
| 603 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 604 | } |
| 605 | |
| 606 | /** |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 607 | * function ttm_bo_cleanup_refs_and_unlock |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 608 | * If bo idle, remove from delayed- and lru lists, and unref. |
| 609 | * If not idle, do nothing. |
| 610 | * |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 611 | * Must be called with lru_lock and reservation held, this function |
| 612 | * will drop both before returning. |
| 613 | * |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 614 | * @interruptible Any sleeps should occur interruptibly. |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 615 | * @no_wait_gpu Never wait for gpu. Return -EBUSY instead. |
| 616 | */ |
| 617 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 618 | static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo, |
| 619 | bool interruptible, |
| 620 | bool no_wait_gpu) |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 621 | { |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 622 | struct ttm_bo_device *bdev = bo->bdev; |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 623 | struct ttm_bo_driver *driver = bdev->driver; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 624 | struct ttm_bo_global *glob = bo->glob; |
| 625 | int put_count; |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 626 | int ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 627 | |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 628 | spin_lock(&bdev->fence_lock); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 629 | ret = ttm_bo_wait(bo, false, false, true); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 630 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 631 | if (ret && !no_wait_gpu) { |
| 632 | void *sync_obj; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 633 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 634 | /* |
| 635 | * Take a reference to the fence and unreserve, |
| 636 | * at this point the buffer should be dead, so |
| 637 | * no new sync objects can be attached. |
| 638 | */ |
Maarten Lankhorst | 0953e76 | 2012-12-19 18:21:10 +0100 | [diff] [blame] | 639 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 640 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | 26cc40a | 2011-11-21 13:05:02 +0100 | [diff] [blame] | 641 | |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 642 | atomic_set(&bo->reserved, 0); |
| 643 | wake_up_all(&bo->event_queue); |
| 644 | spin_unlock(&glob->lru_lock); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 645 | |
| 646 | ret = driver->sync_obj_wait(sync_obj, false, interruptible); |
| 647 | driver->sync_obj_unref(&sync_obj); |
| 648 | if (ret) |
| 649 | return ret; |
| 650 | |
| 651 | /* |
| 652 | * remove sync_obj with ttm_bo_wait, the wait should be |
| 653 | * finished, and no new wait object should have been added. |
| 654 | */ |
| 655 | spin_lock(&bdev->fence_lock); |
| 656 | ret = ttm_bo_wait(bo, false, false, true); |
| 657 | WARN_ON(ret); |
| 658 | spin_unlock(&bdev->fence_lock); |
| 659 | if (ret) |
| 660 | return ret; |
| 661 | |
| 662 | spin_lock(&glob->lru_lock); |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 663 | ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 664 | |
| 665 | /* |
| 666 | * We raced, and lost, someone else holds the reservation now, |
| 667 | * and is probably busy in ttm_bo_cleanup_memtype_use. |
| 668 | * |
| 669 | * Even if it's not the case, because we finished waiting any |
| 670 | * delayed destruction would succeed, so just return success |
| 671 | * here. |
| 672 | */ |
| 673 | if (ret) { |
| 674 | spin_unlock(&glob->lru_lock); |
| 675 | return 0; |
| 676 | } |
| 677 | } else |
| 678 | spin_unlock(&bdev->fence_lock); |
| 679 | |
| 680 | if (ret || unlikely(list_empty(&bo->ddestroy))) { |
| 681 | atomic_set(&bo->reserved, 0); |
| 682 | wake_up_all(&bo->event_queue); |
| 683 | spin_unlock(&glob->lru_lock); |
| 684 | return ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | put_count = ttm_bo_del_from_lru(bo); |
| 688 | list_del_init(&bo->ddestroy); |
| 689 | ++put_count; |
| 690 | |
| 691 | spin_unlock(&glob->lru_lock); |
| 692 | ttm_bo_cleanup_memtype_use(bo); |
| 693 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 694 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 695 | |
| 696 | return 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Traverse the delayed list, and call ttm_bo_cleanup_refs on all |
| 701 | * encountered buffers. |
| 702 | */ |
| 703 | |
| 704 | static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) |
| 705 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 706 | struct ttm_bo_global *glob = bdev->glob; |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 707 | struct ttm_buffer_object *entry = NULL; |
| 708 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 709 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 710 | spin_lock(&glob->lru_lock); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 711 | if (list_empty(&bdev->ddestroy)) |
| 712 | goto out_unlock; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 713 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 714 | entry = list_first_entry(&bdev->ddestroy, |
| 715 | struct ttm_buffer_object, ddestroy); |
| 716 | kref_get(&entry->list_kref); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 717 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 718 | for (;;) { |
| 719 | struct ttm_buffer_object *nentry = NULL; |
| 720 | |
| 721 | if (entry->ddestroy.next != &bdev->ddestroy) { |
| 722 | nentry = list_first_entry(&entry->ddestroy, |
| 723 | struct ttm_buffer_object, ddestroy); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 724 | kref_get(&nentry->list_kref); |
| 725 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 726 | |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 727 | ret = ttm_bo_reserve_nolru(entry, false, true, false, 0); |
| 728 | if (remove_all && ret) { |
| 729 | spin_unlock(&glob->lru_lock); |
| 730 | ret = ttm_bo_reserve_nolru(entry, false, false, |
| 731 | false, 0); |
| 732 | spin_lock(&glob->lru_lock); |
| 733 | } |
| 734 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 735 | if (!ret) |
| 736 | ret = ttm_bo_cleanup_refs_and_unlock(entry, false, |
| 737 | !remove_all); |
| 738 | else |
| 739 | spin_unlock(&glob->lru_lock); |
| 740 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 741 | kref_put(&entry->list_kref, ttm_bo_release_list); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 742 | entry = nentry; |
| 743 | |
| 744 | if (ret || !entry) |
| 745 | goto out; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 746 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 747 | spin_lock(&glob->lru_lock); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 748 | if (list_empty(&entry->ddestroy)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 749 | break; |
| 750 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 751 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 752 | out_unlock: |
| 753 | spin_unlock(&glob->lru_lock); |
| 754 | out: |
| 755 | if (entry) |
| 756 | kref_put(&entry->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 757 | return ret; |
| 758 | } |
| 759 | |
| 760 | static void ttm_bo_delayed_workqueue(struct work_struct *work) |
| 761 | { |
| 762 | struct ttm_bo_device *bdev = |
| 763 | container_of(work, struct ttm_bo_device, wq.work); |
| 764 | |
| 765 | if (ttm_bo_delayed_delete(bdev, false)) { |
| 766 | schedule_delayed_work(&bdev->wq, |
| 767 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | static void ttm_bo_release(struct kref *kref) |
| 772 | { |
| 773 | struct ttm_buffer_object *bo = |
| 774 | container_of(kref, struct ttm_buffer_object, kref); |
| 775 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 776 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 777 | |
Thomas Hellstrom | 82fe50b | 2012-11-21 14:53:21 +0000 | [diff] [blame] | 778 | write_lock(&bdev->vm_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 779 | if (likely(bo->vm_node != NULL)) { |
| 780 | rb_erase(&bo->vm_rb, &bdev->addr_space_rb); |
| 781 | drm_mm_put_block(bo->vm_node); |
| 782 | bo->vm_node = NULL; |
| 783 | } |
| 784 | write_unlock(&bdev->vm_lock); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 785 | ttm_mem_io_lock(man, false); |
| 786 | ttm_mem_io_free_vm(bo); |
| 787 | ttm_mem_io_unlock(man); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 788 | ttm_bo_cleanup_refs_or_queue(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 789 | kref_put(&bo->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | void ttm_bo_unref(struct ttm_buffer_object **p_bo) |
| 793 | { |
| 794 | struct ttm_buffer_object *bo = *p_bo; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 795 | |
| 796 | *p_bo = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 797 | kref_put(&bo->kref, ttm_bo_release); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 798 | } |
| 799 | EXPORT_SYMBOL(ttm_bo_unref); |
| 800 | |
Matthew Garrett | 7c5ee53 | 2010-04-26 16:00:09 -0400 | [diff] [blame] | 801 | int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev) |
| 802 | { |
| 803 | return cancel_delayed_work_sync(&bdev->wq); |
| 804 | } |
| 805 | EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue); |
| 806 | |
| 807 | void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched) |
| 808 | { |
| 809 | if (resched) |
| 810 | schedule_delayed_work(&bdev->wq, |
| 811 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 812 | } |
| 813 | EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue); |
| 814 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 815 | static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 816 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 817 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 818 | struct ttm_bo_device *bdev = bo->bdev; |
| 819 | struct ttm_mem_reg evict_mem; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 820 | struct ttm_placement placement; |
| 821 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 822 | |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 823 | spin_lock(&bdev->fence_lock); |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 824 | ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 825 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 826 | |
Thomas Hellstrom | 78ecf09 | 2009-06-17 12:29:55 +0200 | [diff] [blame] | 827 | if (unlikely(ret != 0)) { |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 828 | if (ret != -ERESTARTSYS) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 829 | pr_err("Failed to expire sync object before buffer eviction\n"); |
Thomas Hellstrom | 78ecf09 | 2009-06-17 12:29:55 +0200 | [diff] [blame] | 830 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 831 | goto out; |
| 832 | } |
| 833 | |
Maarten Lankhorst | a9dbfff | 2012-10-12 16:58:36 +0200 | [diff] [blame] | 834 | BUG_ON(!ttm_bo_is_reserved(bo)); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 835 | |
| 836 | evict_mem = bo->mem; |
| 837 | evict_mem.mm_node = NULL; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 838 | evict_mem.bus.io_reserved_vm = false; |
| 839 | evict_mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 840 | |
Jerome Glisse | 7cb7d1d | 2009-12-09 22:14:27 +0100 | [diff] [blame] | 841 | placement.fpfn = 0; |
| 842 | placement.lpfn = 0; |
| 843 | placement.num_placement = 0; |
| 844 | placement.num_busy_placement = 0; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 845 | bdev->driver->evict_flags(bo, &placement); |
| 846 | ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 847 | no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 848 | if (ret) { |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 849 | if (ret != -ERESTARTSYS) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 850 | pr_err("Failed to find memory space for buffer 0x%p eviction\n", |
| 851 | bo); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 852 | ttm_bo_mem_space_debug(bo, &placement); |
| 853 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 854 | goto out; |
| 855 | } |
| 856 | |
| 857 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 858 | no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 859 | if (ret) { |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 860 | if (ret != -ERESTARTSYS) |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 861 | pr_err("Buffer eviction failed\n"); |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 862 | ttm_bo_mem_put(bo, &evict_mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 863 | goto out; |
| 864 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 865 | bo->evicted = true; |
| 866 | out: |
| 867 | return ret; |
| 868 | } |
| 869 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 870 | static int ttm_mem_evict_first(struct ttm_bo_device *bdev, |
| 871 | uint32_t mem_type, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 872 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 873 | bool no_wait_gpu) |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 874 | { |
| 875 | struct ttm_bo_global *glob = bdev->glob; |
| 876 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 877 | struct ttm_buffer_object *bo; |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 878 | int ret = -EBUSY, put_count; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 879 | |
| 880 | spin_lock(&glob->lru_lock); |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 881 | list_for_each_entry(bo, &man->lru, lru) { |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 882 | ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 883 | if (!ret) |
| 884 | break; |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 885 | } |
| 886 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 887 | if (ret) { |
| 888 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | b8e902f | 2012-10-22 12:51:26 +0000 | [diff] [blame] | 889 | return ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 892 | kref_get(&bo->list_kref); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 893 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 894 | if (!list_empty(&bo->ddestroy)) { |
| 895 | ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible, |
| 896 | no_wait_gpu); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 897 | kref_put(&bo->list_kref, ttm_bo_release_list); |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 898 | return ret; |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | put_count = ttm_bo_del_from_lru(bo); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 902 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 903 | |
| 904 | BUG_ON(ret != 0); |
| 905 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 906 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 907 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 908 | ret = ttm_bo_evict(bo, interruptible, no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 909 | ttm_bo_unreserve(bo); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 910 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 911 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 912 | return ret; |
| 913 | } |
| 914 | |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 915 | void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem) |
| 916 | { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 917 | struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type]; |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 918 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 919 | if (mem->mm_node) |
| 920 | (*man->func->put_node)(man, mem); |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 921 | } |
| 922 | EXPORT_SYMBOL(ttm_bo_mem_put); |
| 923 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 924 | /** |
| 925 | * Repeatedly evict memory from the LRU for @mem_type until we create enough |
| 926 | * space, or we've evicted everything and there isn't enough space. |
| 927 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 928 | static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, |
| 929 | uint32_t mem_type, |
| 930 | struct ttm_placement *placement, |
| 931 | struct ttm_mem_reg *mem, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 932 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 933 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 934 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 935 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 936 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 937 | int ret; |
| 938 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 939 | do { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 940 | ret = (*man->func->get_node)(man, bo, placement, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 941 | if (unlikely(ret != 0)) |
| 942 | return ret; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 943 | if (mem->mm_node) |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 944 | break; |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 945 | ret = ttm_mem_evict_first(bdev, mem_type, |
| 946 | interruptible, no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 947 | if (unlikely(ret != 0)) |
| 948 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 949 | } while (1); |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 950 | if (mem->mm_node == NULL) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 951 | return -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 952 | mem->mem_type = mem_type; |
| 953 | return 0; |
| 954 | } |
| 955 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 956 | static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man, |
| 957 | uint32_t cur_placement, |
| 958 | uint32_t proposed_placement) |
| 959 | { |
| 960 | uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING; |
| 961 | uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING; |
| 962 | |
| 963 | /** |
| 964 | * Keep current caching if possible. |
| 965 | */ |
| 966 | |
| 967 | if ((cur_placement & caching) != 0) |
| 968 | result |= (cur_placement & caching); |
| 969 | else if ((man->default_caching & caching) != 0) |
| 970 | result |= man->default_caching; |
| 971 | else if ((TTM_PL_FLAG_CACHED & caching) != 0) |
| 972 | result |= TTM_PL_FLAG_CACHED; |
| 973 | else if ((TTM_PL_FLAG_WC & caching) != 0) |
| 974 | result |= TTM_PL_FLAG_WC; |
| 975 | else if ((TTM_PL_FLAG_UNCACHED & caching) != 0) |
| 976 | result |= TTM_PL_FLAG_UNCACHED; |
| 977 | |
| 978 | return result; |
| 979 | } |
| 980 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 981 | static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 982 | uint32_t mem_type, |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 983 | uint32_t proposed_placement, |
| 984 | uint32_t *masked_placement) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 985 | { |
| 986 | uint32_t cur_flags = ttm_bo_type_flags(mem_type); |
| 987 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 988 | if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 989 | return false; |
| 990 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 991 | if ((proposed_placement & man->available_caching) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 992 | return false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 993 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 994 | cur_flags |= (proposed_placement & man->available_caching); |
| 995 | |
| 996 | *masked_placement = cur_flags; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 997 | return true; |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * Creates space for memory region @mem according to its type. |
| 1002 | * |
| 1003 | * This function first searches for free space in compatible memory types in |
| 1004 | * the priority order defined by the driver. If free space isn't found, then |
| 1005 | * ttm_bo_mem_force_space is attempted in priority order to evict and find |
| 1006 | * space. |
| 1007 | */ |
| 1008 | int ttm_bo_mem_space(struct ttm_buffer_object *bo, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1009 | struct ttm_placement *placement, |
| 1010 | struct ttm_mem_reg *mem, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1011 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 1012 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1013 | { |
| 1014 | struct ttm_bo_device *bdev = bo->bdev; |
| 1015 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1016 | uint32_t mem_type = TTM_PL_SYSTEM; |
| 1017 | uint32_t cur_flags = 0; |
| 1018 | bool type_found = false; |
| 1019 | bool type_ok = false; |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 1020 | bool has_erestartsys = false; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1021 | int i, ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1022 | |
| 1023 | mem->mm_node = NULL; |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 1024 | for (i = 0; i < placement->num_placement; ++i) { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1025 | ret = ttm_mem_type_from_flags(placement->placement[i], |
| 1026 | &mem_type); |
| 1027 | if (ret) |
| 1028 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1029 | man = &bdev->man[mem_type]; |
| 1030 | |
| 1031 | type_ok = ttm_bo_mt_compatible(man, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1032 | mem_type, |
| 1033 | placement->placement[i], |
| 1034 | &cur_flags); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1035 | |
| 1036 | if (!type_ok) |
| 1037 | continue; |
| 1038 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 1039 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 1040 | cur_flags); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1041 | /* |
| 1042 | * Use the access and other non-mapping-related flag bits from |
| 1043 | * the memory placement flags to the current flags |
| 1044 | */ |
| 1045 | ttm_flag_masked(&cur_flags, placement->placement[i], |
| 1046 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 1047 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1048 | if (mem_type == TTM_PL_SYSTEM) |
| 1049 | break; |
| 1050 | |
| 1051 | if (man->has_type && man->use_type) { |
| 1052 | type_found = true; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1053 | ret = (*man->func->get_node)(man, bo, placement, mem); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1054 | if (unlikely(ret)) |
| 1055 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1056 | } |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1057 | if (mem->mm_node) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1058 | break; |
| 1059 | } |
| 1060 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1061 | if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1062 | mem->mem_type = mem_type; |
| 1063 | mem->placement = cur_flags; |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | if (!type_found) |
| 1068 | return -EINVAL; |
| 1069 | |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 1070 | for (i = 0; i < placement->num_busy_placement; ++i) { |
| 1071 | ret = ttm_mem_type_from_flags(placement->busy_placement[i], |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1072 | &mem_type); |
| 1073 | if (ret) |
| 1074 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1075 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1076 | if (!man->has_type) |
| 1077 | continue; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1078 | if (!ttm_bo_mt_compatible(man, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1079 | mem_type, |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 1080 | placement->busy_placement[i], |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1081 | &cur_flags)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1082 | continue; |
| 1083 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 1084 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 1085 | cur_flags); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1086 | /* |
| 1087 | * Use the access and other non-mapping-related flag bits from |
| 1088 | * the memory placement flags to the current flags |
| 1089 | */ |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 1090 | ttm_flag_masked(&cur_flags, placement->busy_placement[i], |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1091 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 1092 | |
Thomas Hellstrom | 0eaddb2 | 2010-01-16 16:05:04 +0100 | [diff] [blame] | 1093 | |
| 1094 | if (mem_type == TTM_PL_SYSTEM) { |
| 1095 | mem->mem_type = mem_type; |
| 1096 | mem->placement = cur_flags; |
| 1097 | mem->mm_node = NULL; |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1101 | ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1102 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1103 | if (ret == 0 && mem->mm_node) { |
| 1104 | mem->placement = cur_flags; |
| 1105 | return 0; |
| 1106 | } |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 1107 | if (ret == -ERESTARTSYS) |
| 1108 | has_erestartsys = true; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1109 | } |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 1110 | ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1111 | return ret; |
| 1112 | } |
| 1113 | EXPORT_SYMBOL(ttm_bo_mem_space); |
| 1114 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1115 | int ttm_bo_move_buffer(struct ttm_buffer_object *bo, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1116 | struct ttm_placement *placement, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1117 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 1118 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1119 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1120 | int ret = 0; |
| 1121 | struct ttm_mem_reg mem; |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1122 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1123 | |
Maarten Lankhorst | a9dbfff | 2012-10-12 16:58:36 +0200 | [diff] [blame] | 1124 | BUG_ON(!ttm_bo_is_reserved(bo)); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1125 | |
| 1126 | /* |
| 1127 | * FIXME: It's possible to pipeline buffer moves. |
| 1128 | * Have the driver move function wait for idle when necessary, |
| 1129 | * instead of doing it here. |
| 1130 | */ |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1131 | spin_lock(&bdev->fence_lock); |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1132 | ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1133 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1134 | if (ret) |
| 1135 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1136 | mem.num_pages = bo->num_pages; |
| 1137 | mem.size = mem.num_pages << PAGE_SHIFT; |
| 1138 | mem.page_alignment = bo->mem.page_alignment; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1139 | mem.bus.io_reserved_vm = false; |
| 1140 | mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1141 | /* |
| 1142 | * Determine where to move the buffer. |
| 1143 | */ |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1144 | ret = ttm_bo_mem_space(bo, placement, &mem, |
| 1145 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1146 | if (ret) |
| 1147 | goto out_unlock; |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1148 | ret = ttm_bo_handle_move_mem(bo, &mem, false, |
| 1149 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1150 | out_unlock: |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1151 | if (ret && mem.mm_node) |
| 1152 | ttm_bo_mem_put(bo, &mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1153 | return ret; |
| 1154 | } |
| 1155 | |
Thomas Hellstrom | 4f94b6e | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1156 | static bool ttm_bo_mem_compat(struct ttm_placement *placement, |
| 1157 | struct ttm_mem_reg *mem, |
| 1158 | uint32_t *new_flags) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1159 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1160 | int i; |
Thomas Hellstrom | e22238e | 2010-02-12 00:18:00 +0100 | [diff] [blame] | 1161 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1162 | if (mem->mm_node && placement->lpfn != 0 && |
| 1163 | (mem->start < placement->fpfn || |
| 1164 | mem->start + mem->num_pages > placement->lpfn)) |
Thomas Hellstrom | 4f94b6e | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1165 | return false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1166 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1167 | for (i = 0; i < placement->num_placement; i++) { |
Thomas Hellstrom | 4f94b6e | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1168 | *new_flags = placement->placement[i]; |
| 1169 | if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && |
| 1170 | (*new_flags & mem->placement & TTM_PL_MASK_MEM)) |
| 1171 | return true; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1172 | } |
Thomas Hellstrom | 4f94b6e | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1173 | |
| 1174 | for (i = 0; i < placement->num_busy_placement; i++) { |
| 1175 | *new_flags = placement->busy_placement[i]; |
| 1176 | if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && |
| 1177 | (*new_flags & mem->placement & TTM_PL_MASK_MEM)) |
| 1178 | return true; |
| 1179 | } |
| 1180 | |
| 1181 | return false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1184 | int ttm_bo_validate(struct ttm_buffer_object *bo, |
| 1185 | struct ttm_placement *placement, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1186 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 1187 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1188 | { |
| 1189 | int ret; |
Thomas Hellstrom | 4f94b6e | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1190 | uint32_t new_flags; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1191 | |
Maarten Lankhorst | a9dbfff | 2012-10-12 16:58:36 +0200 | [diff] [blame] | 1192 | BUG_ON(!ttm_bo_is_reserved(bo)); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1193 | /* Check that range is valid */ |
| 1194 | if (placement->lpfn || placement->fpfn) |
| 1195 | if (placement->fpfn > placement->lpfn || |
| 1196 | (placement->lpfn - placement->fpfn) < bo->num_pages) |
| 1197 | return -EINVAL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1198 | /* |
| 1199 | * Check whether we need to move buffer. |
| 1200 | */ |
Thomas Hellstrom | 4f94b6e | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1201 | if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) { |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1202 | ret = ttm_bo_move_buffer(bo, placement, interruptible, |
| 1203 | no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1204 | if (ret) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1205 | return ret; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1206 | } else { |
| 1207 | /* |
| 1208 | * Use the access and other non-mapping-related flag bits from |
| 1209 | * the compatible memory placement flags to the active flags |
| 1210 | */ |
Thomas Hellstrom | 4f94b6e | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1211 | ttm_flag_masked(&bo->mem.placement, new_flags, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1212 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1213 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1214 | /* |
| 1215 | * We might need to add a TTM. |
| 1216 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1217 | if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { |
| 1218 | ret = ttm_bo_add_ttm(bo, true); |
| 1219 | if (ret) |
| 1220 | return ret; |
| 1221 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1222 | return 0; |
| 1223 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1224 | EXPORT_SYMBOL(ttm_bo_validate); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1225 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1226 | int ttm_bo_check_placement(struct ttm_buffer_object *bo, |
| 1227 | struct ttm_placement *placement) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1228 | { |
Thomas Hellstrom | 29e190e | 2010-11-02 13:21:48 +0000 | [diff] [blame] | 1229 | BUG_ON((placement->fpfn || placement->lpfn) && |
| 1230 | (bo->mem.num_pages > (placement->lpfn - placement->fpfn))); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1231 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1232 | return 0; |
| 1233 | } |
| 1234 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1235 | int ttm_bo_init(struct ttm_bo_device *bdev, |
| 1236 | struct ttm_buffer_object *bo, |
| 1237 | unsigned long size, |
| 1238 | enum ttm_bo_type type, |
| 1239 | struct ttm_placement *placement, |
| 1240 | uint32_t page_alignment, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1241 | bool interruptible, |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1242 | struct file *persistent_swap_storage, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1243 | size_t acc_size, |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 1244 | struct sg_table *sg, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1245 | void (*destroy) (struct ttm_buffer_object *)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1246 | { |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1247 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1248 | unsigned long num_pages; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1249 | struct ttm_mem_global *mem_glob = bdev->glob->mem_glob; |
| 1250 | |
| 1251 | ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); |
| 1252 | if (ret) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1253 | pr_err("Out of kernel memory\n"); |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1254 | if (destroy) |
| 1255 | (*destroy)(bo); |
| 1256 | else |
| 1257 | kfree(bo); |
| 1258 | return -ENOMEM; |
| 1259 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1260 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1261 | num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1262 | if (num_pages == 0) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1263 | pr_err("Illegal buffer object size\n"); |
Thomas Hellstrom | 7dfbbdc | 2010-11-09 21:31:44 +0100 | [diff] [blame] | 1264 | if (destroy) |
| 1265 | (*destroy)(bo); |
| 1266 | else |
| 1267 | kfree(bo); |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1268 | ttm_mem_global_free(mem_glob, acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1269 | return -EINVAL; |
| 1270 | } |
| 1271 | bo->destroy = destroy; |
| 1272 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1273 | kref_init(&bo->kref); |
| 1274 | kref_init(&bo->list_kref); |
| 1275 | atomic_set(&bo->cpu_writers, 0); |
| 1276 | atomic_set(&bo->reserved, 1); |
| 1277 | init_waitqueue_head(&bo->event_queue); |
| 1278 | INIT_LIST_HEAD(&bo->lru); |
| 1279 | INIT_LIST_HEAD(&bo->ddestroy); |
| 1280 | INIT_LIST_HEAD(&bo->swap); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1281 | INIT_LIST_HEAD(&bo->io_reserve_lru); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1282 | bo->bdev = bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1283 | bo->glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1284 | bo->type = type; |
| 1285 | bo->num_pages = num_pages; |
Jerome Glisse | eb6d2c3 | 2009-12-10 16:15:52 +0100 | [diff] [blame] | 1286 | bo->mem.size = num_pages << PAGE_SHIFT; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1287 | bo->mem.mem_type = TTM_PL_SYSTEM; |
| 1288 | bo->mem.num_pages = bo->num_pages; |
| 1289 | bo->mem.mm_node = NULL; |
| 1290 | bo->mem.page_alignment = page_alignment; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1291 | bo->mem.bus.io_reserved_vm = false; |
| 1292 | bo->mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1293 | bo->priv_flags = 0; |
| 1294 | bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); |
| 1295 | bo->seq_valid = false; |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1296 | bo->persistent_swap_storage = persistent_swap_storage; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1297 | bo->acc_size = acc_size; |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 1298 | bo->sg = sg; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1299 | atomic_inc(&bo->glob->bo_count); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1300 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1301 | ret = ttm_bo_check_placement(bo, placement); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1302 | if (unlikely(ret != 0)) |
| 1303 | goto out_err; |
| 1304 | |
| 1305 | /* |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1306 | * For ttm_bo_type_device buffers, allocate |
| 1307 | * address space from the device. |
| 1308 | */ |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 1309 | if (bo->type == ttm_bo_type_device || |
| 1310 | bo->type == ttm_bo_type_sg) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1311 | ret = ttm_bo_setup_vm(bo); |
| 1312 | if (ret) |
| 1313 | goto out_err; |
| 1314 | } |
| 1315 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1316 | ret = ttm_bo_validate(bo, placement, interruptible, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1317 | if (ret) |
| 1318 | goto out_err; |
| 1319 | |
| 1320 | ttm_bo_unreserve(bo); |
| 1321 | return 0; |
| 1322 | |
| 1323 | out_err: |
| 1324 | ttm_bo_unreserve(bo); |
| 1325 | ttm_bo_unref(&bo); |
| 1326 | |
| 1327 | return ret; |
| 1328 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1329 | EXPORT_SYMBOL(ttm_bo_init); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1330 | |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1331 | size_t ttm_bo_acc_size(struct ttm_bo_device *bdev, |
| 1332 | unsigned long bo_size, |
| 1333 | unsigned struct_size) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1334 | { |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1335 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; |
| 1336 | size_t size = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1337 | |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1338 | size += ttm_round_pot(struct_size); |
| 1339 | size += PAGE_ALIGN(npages * sizeof(void *)); |
| 1340 | size += ttm_round_pot(sizeof(struct ttm_tt)); |
| 1341 | return size; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1342 | } |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1343 | EXPORT_SYMBOL(ttm_bo_acc_size); |
| 1344 | |
| 1345 | size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, |
| 1346 | unsigned long bo_size, |
| 1347 | unsigned struct_size) |
| 1348 | { |
| 1349 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; |
| 1350 | size_t size = 0; |
| 1351 | |
| 1352 | size += ttm_round_pot(struct_size); |
| 1353 | size += PAGE_ALIGN(npages * sizeof(void *)); |
| 1354 | size += PAGE_ALIGN(npages * sizeof(dma_addr_t)); |
| 1355 | size += ttm_round_pot(sizeof(struct ttm_dma_tt)); |
| 1356 | return size; |
| 1357 | } |
| 1358 | EXPORT_SYMBOL(ttm_bo_dma_acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1359 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1360 | int ttm_bo_create(struct ttm_bo_device *bdev, |
| 1361 | unsigned long size, |
| 1362 | enum ttm_bo_type type, |
| 1363 | struct ttm_placement *placement, |
| 1364 | uint32_t page_alignment, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1365 | bool interruptible, |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1366 | struct file *persistent_swap_storage, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1367 | struct ttm_buffer_object **p_bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1368 | { |
| 1369 | struct ttm_buffer_object *bo; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1370 | size_t acc_size; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1371 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1372 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1373 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1374 | if (unlikely(bo == NULL)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1375 | return -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1376 | |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1377 | 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] | 1378 | ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment, |
Marcin Slusarz | 0b91c4a | 2012-11-06 21:49:51 +0000 | [diff] [blame] | 1379 | interruptible, persistent_swap_storage, acc_size, |
| 1380 | NULL, NULL); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1381 | if (likely(ret == 0)) |
| 1382 | *p_bo = bo; |
| 1383 | |
| 1384 | return ret; |
| 1385 | } |
Thomas Hellstrom | 4d79893 | 2011-10-04 20:13:11 +0200 | [diff] [blame] | 1386 | EXPORT_SYMBOL(ttm_bo_create); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1387 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1388 | static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1389 | unsigned mem_type, bool allow_errors) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1390 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1391 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1392 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1393 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1394 | |
| 1395 | /* |
| 1396 | * Can't use standard list traversal since we're unlocking. |
| 1397 | */ |
| 1398 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1399 | spin_lock(&glob->lru_lock); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1400 | while (!list_empty(&man->lru)) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1401 | spin_unlock(&glob->lru_lock); |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1402 | ret = ttm_mem_evict_first(bdev, mem_type, false, false); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1403 | if (ret) { |
| 1404 | if (allow_errors) { |
| 1405 | return ret; |
| 1406 | } else { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1407 | pr_err("Cleanup eviction failed\n"); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1408 | } |
| 1409 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1410 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1411 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1412 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1417 | { |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1418 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1419 | int ret = -EINVAL; |
| 1420 | |
| 1421 | if (mem_type >= TTM_NUM_MEM_TYPES) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1422 | pr_err("Illegal memory type %d\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1423 | return ret; |
| 1424 | } |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1425 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1426 | |
| 1427 | if (!man->has_type) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1428 | pr_err("Trying to take down uninitialized memory manager type %u\n", |
| 1429 | mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1430 | return ret; |
| 1431 | } |
| 1432 | |
| 1433 | man->use_type = false; |
| 1434 | man->has_type = false; |
| 1435 | |
| 1436 | ret = 0; |
| 1437 | if (mem_type > 0) { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1438 | ttm_bo_force_list_clean(bdev, mem_type, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1439 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1440 | ret = (*man->func->takedown)(man); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | return ret; |
| 1444 | } |
| 1445 | EXPORT_SYMBOL(ttm_bo_clean_mm); |
| 1446 | |
| 1447 | int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1448 | { |
| 1449 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 1450 | |
| 1451 | if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1452 | pr_err("Illegal memory manager memory type %u\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1453 | return -EINVAL; |
| 1454 | } |
| 1455 | |
| 1456 | if (!man->has_type) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1457 | pr_err("Memory type %u has not been initialized\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1458 | return 0; |
| 1459 | } |
| 1460 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1461 | return ttm_bo_force_list_clean(bdev, mem_type, true); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1462 | } |
| 1463 | EXPORT_SYMBOL(ttm_bo_evict_mm); |
| 1464 | |
| 1465 | int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1466 | unsigned long p_size) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1467 | { |
| 1468 | int ret = -EINVAL; |
| 1469 | struct ttm_mem_type_manager *man; |
| 1470 | |
Thomas Hellstrom | dbc4a5b | 2010-10-29 10:46:47 +0200 | [diff] [blame] | 1471 | BUG_ON(type >= TTM_NUM_MEM_TYPES); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1472 | man = &bdev->man[type]; |
Thomas Hellstrom | dbc4a5b | 2010-10-29 10:46:47 +0200 | [diff] [blame] | 1473 | BUG_ON(man->has_type); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1474 | man->io_reserve_fastpath = true; |
| 1475 | man->use_io_reserve_lru = false; |
| 1476 | mutex_init(&man->io_reserve_mutex); |
| 1477 | INIT_LIST_HEAD(&man->io_reserve_lru); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1478 | |
| 1479 | ret = bdev->driver->init_mem_type(bdev, type, man); |
| 1480 | if (ret) |
| 1481 | return ret; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1482 | man->bdev = bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1483 | |
| 1484 | ret = 0; |
| 1485 | if (type != TTM_PL_SYSTEM) { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1486 | ret = (*man->func->init)(man, p_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1487 | if (ret) |
| 1488 | return ret; |
| 1489 | } |
| 1490 | man->has_type = true; |
| 1491 | man->use_type = true; |
| 1492 | man->size = p_size; |
| 1493 | |
| 1494 | INIT_LIST_HEAD(&man->lru); |
| 1495 | |
| 1496 | return 0; |
| 1497 | } |
| 1498 | EXPORT_SYMBOL(ttm_bo_init_mm); |
| 1499 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1500 | static void ttm_bo_global_kobj_release(struct kobject *kobj) |
| 1501 | { |
| 1502 | struct ttm_bo_global *glob = |
| 1503 | container_of(kobj, struct ttm_bo_global, kobj); |
| 1504 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1505 | ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink); |
| 1506 | __free_page(glob->dummy_read_page); |
| 1507 | kfree(glob); |
| 1508 | } |
| 1509 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 1510 | void ttm_bo_global_release(struct drm_global_reference *ref) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1511 | { |
| 1512 | struct ttm_bo_global *glob = ref->object; |
| 1513 | |
| 1514 | kobject_del(&glob->kobj); |
| 1515 | kobject_put(&glob->kobj); |
| 1516 | } |
| 1517 | EXPORT_SYMBOL(ttm_bo_global_release); |
| 1518 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 1519 | int ttm_bo_global_init(struct drm_global_reference *ref) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1520 | { |
| 1521 | struct ttm_bo_global_ref *bo_ref = |
| 1522 | container_of(ref, struct ttm_bo_global_ref, ref); |
| 1523 | struct ttm_bo_global *glob = ref->object; |
| 1524 | int ret; |
| 1525 | |
| 1526 | mutex_init(&glob->device_list_mutex); |
| 1527 | spin_lock_init(&glob->lru_lock); |
| 1528 | glob->mem_glob = bo_ref->mem_glob; |
| 1529 | glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); |
| 1530 | |
| 1531 | if (unlikely(glob->dummy_read_page == NULL)) { |
| 1532 | ret = -ENOMEM; |
| 1533 | goto out_no_drp; |
| 1534 | } |
| 1535 | |
| 1536 | INIT_LIST_HEAD(&glob->swap_lru); |
| 1537 | INIT_LIST_HEAD(&glob->device_list); |
| 1538 | |
| 1539 | ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout); |
| 1540 | ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink); |
| 1541 | if (unlikely(ret != 0)) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1542 | pr_err("Could not register buffer object swapout\n"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1543 | goto out_no_shrink; |
| 1544 | } |
| 1545 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1546 | atomic_set(&glob->bo_count, 0); |
| 1547 | |
Robert P. J. Day | b642ed0 | 2010-03-13 10:36:32 +0000 | [diff] [blame] | 1548 | ret = kobject_init_and_add( |
| 1549 | &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1550 | if (unlikely(ret != 0)) |
| 1551 | kobject_put(&glob->kobj); |
| 1552 | return ret; |
| 1553 | out_no_shrink: |
| 1554 | __free_page(glob->dummy_read_page); |
| 1555 | out_no_drp: |
| 1556 | kfree(glob); |
| 1557 | return ret; |
| 1558 | } |
| 1559 | EXPORT_SYMBOL(ttm_bo_global_init); |
| 1560 | |
| 1561 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1562 | int ttm_bo_device_release(struct ttm_bo_device *bdev) |
| 1563 | { |
| 1564 | int ret = 0; |
| 1565 | unsigned i = TTM_NUM_MEM_TYPES; |
| 1566 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1567 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1568 | |
| 1569 | while (i--) { |
| 1570 | man = &bdev->man[i]; |
| 1571 | if (man->has_type) { |
| 1572 | man->use_type = false; |
| 1573 | if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) { |
| 1574 | ret = -EBUSY; |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1575 | pr_err("DRM memory manager type %d is not clean\n", |
| 1576 | i); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1577 | } |
| 1578 | man->has_type = false; |
| 1579 | } |
| 1580 | } |
| 1581 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1582 | mutex_lock(&glob->device_list_mutex); |
| 1583 | list_del(&bdev->device_list); |
| 1584 | mutex_unlock(&glob->device_list_mutex); |
| 1585 | |
Tejun Heo | f094cfc | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 1586 | cancel_delayed_work_sync(&bdev->wq); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1587 | |
| 1588 | while (ttm_bo_delayed_delete(bdev, true)) |
| 1589 | ; |
| 1590 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1591 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1592 | if (list_empty(&bdev->ddestroy)) |
| 1593 | TTM_DEBUG("Delayed destroy list was clean\n"); |
| 1594 | |
| 1595 | if (list_empty(&bdev->man[0].lru)) |
| 1596 | TTM_DEBUG("Swap list was clean\n"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1597 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1598 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1599 | BUG_ON(!drm_mm_clean(&bdev->addr_space_mm)); |
| 1600 | write_lock(&bdev->vm_lock); |
| 1601 | drm_mm_takedown(&bdev->addr_space_mm); |
| 1602 | write_unlock(&bdev->vm_lock); |
| 1603 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1604 | return ret; |
| 1605 | } |
| 1606 | EXPORT_SYMBOL(ttm_bo_device_release); |
| 1607 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1608 | int ttm_bo_device_init(struct ttm_bo_device *bdev, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1609 | struct ttm_bo_global *glob, |
| 1610 | struct ttm_bo_driver *driver, |
Dave Airlie | 51c8b40 | 2009-08-20 13:38:04 +1000 | [diff] [blame] | 1611 | uint64_t file_page_offset, |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1612 | bool need_dma32) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1613 | { |
| 1614 | int ret = -EINVAL; |
| 1615 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1616 | rwlock_init(&bdev->vm_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1617 | bdev->driver = driver; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1618 | |
| 1619 | memset(bdev->man, 0, sizeof(bdev->man)); |
| 1620 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1621 | /* |
| 1622 | * Initialize the system memory buffer type. |
| 1623 | * Other types need to be driver / IOCTL initialized. |
| 1624 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1625 | ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1626 | if (unlikely(ret != 0)) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1627 | goto out_no_sys; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1628 | |
| 1629 | bdev->addr_space_rb = RB_ROOT; |
| 1630 | ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000); |
| 1631 | if (unlikely(ret != 0)) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1632 | goto out_no_addr_mm; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1633 | |
| 1634 | INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1635 | INIT_LIST_HEAD(&bdev->ddestroy); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1636 | bdev->dev_mapping = NULL; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1637 | bdev->glob = glob; |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1638 | bdev->need_dma32 = need_dma32; |
Thomas Hellstrom | 6570596 | 2010-11-17 12:28:31 +0000 | [diff] [blame] | 1639 | bdev->val_seq = 0; |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1640 | spin_lock_init(&bdev->fence_lock); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1641 | mutex_lock(&glob->device_list_mutex); |
| 1642 | list_add_tail(&bdev->device_list, &glob->device_list); |
| 1643 | mutex_unlock(&glob->device_list_mutex); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1644 | |
| 1645 | return 0; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1646 | out_no_addr_mm: |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1647 | ttm_bo_clean_mm(bdev, 0); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1648 | out_no_sys: |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1649 | return ret; |
| 1650 | } |
| 1651 | EXPORT_SYMBOL(ttm_bo_device_init); |
| 1652 | |
| 1653 | /* |
| 1654 | * buffer object vm functions. |
| 1655 | */ |
| 1656 | |
| 1657 | bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 1658 | { |
| 1659 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
| 1660 | |
| 1661 | if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
| 1662 | if (mem->mem_type == TTM_PL_SYSTEM) |
| 1663 | return false; |
| 1664 | |
| 1665 | if (man->flags & TTM_MEMTYPE_FLAG_CMA) |
| 1666 | return false; |
| 1667 | |
| 1668 | if (mem->placement & TTM_PL_FLAG_CACHED) |
| 1669 | return false; |
| 1670 | } |
| 1671 | return true; |
| 1672 | } |
| 1673 | |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1674 | void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1675 | { |
| 1676 | struct ttm_bo_device *bdev = bo->bdev; |
| 1677 | loff_t offset = (loff_t) bo->addr_space_offset; |
| 1678 | loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT; |
| 1679 | |
| 1680 | if (!bdev->dev_mapping) |
| 1681 | return; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1682 | unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1683 | ttm_mem_io_free_vm(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1684 | } |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1685 | |
| 1686 | void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) |
| 1687 | { |
| 1688 | struct ttm_bo_device *bdev = bo->bdev; |
| 1689 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; |
| 1690 | |
| 1691 | ttm_mem_io_lock(man, false); |
| 1692 | ttm_bo_unmap_virtual_locked(bo); |
| 1693 | ttm_mem_io_unlock(man); |
| 1694 | } |
| 1695 | |
| 1696 | |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 1697 | EXPORT_SYMBOL(ttm_bo_unmap_virtual); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1698 | |
| 1699 | static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo) |
| 1700 | { |
| 1701 | struct ttm_bo_device *bdev = bo->bdev; |
| 1702 | struct rb_node **cur = &bdev->addr_space_rb.rb_node; |
| 1703 | struct rb_node *parent = NULL; |
| 1704 | struct ttm_buffer_object *cur_bo; |
| 1705 | unsigned long offset = bo->vm_node->start; |
| 1706 | unsigned long cur_offset; |
| 1707 | |
| 1708 | while (*cur) { |
| 1709 | parent = *cur; |
| 1710 | cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb); |
| 1711 | cur_offset = cur_bo->vm_node->start; |
| 1712 | if (offset < cur_offset) |
| 1713 | cur = &parent->rb_left; |
| 1714 | else if (offset > cur_offset) |
| 1715 | cur = &parent->rb_right; |
| 1716 | else |
| 1717 | BUG(); |
| 1718 | } |
| 1719 | |
| 1720 | rb_link_node(&bo->vm_rb, parent, cur); |
| 1721 | rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb); |
| 1722 | } |
| 1723 | |
| 1724 | /** |
| 1725 | * ttm_bo_setup_vm: |
| 1726 | * |
| 1727 | * @bo: the buffer to allocate address space for |
| 1728 | * |
| 1729 | * Allocate address space in the drm device so that applications |
| 1730 | * can mmap the buffer and access the contents. This only |
| 1731 | * applies to ttm_bo_type_device objects as others are not |
| 1732 | * placed in the drm device address space. |
| 1733 | */ |
| 1734 | |
| 1735 | static int ttm_bo_setup_vm(struct ttm_buffer_object *bo) |
| 1736 | { |
| 1737 | struct ttm_bo_device *bdev = bo->bdev; |
| 1738 | int ret; |
| 1739 | |
| 1740 | retry_pre_get: |
| 1741 | ret = drm_mm_pre_get(&bdev->addr_space_mm); |
| 1742 | if (unlikely(ret != 0)) |
| 1743 | return ret; |
| 1744 | |
| 1745 | write_lock(&bdev->vm_lock); |
| 1746 | bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm, |
| 1747 | bo->mem.num_pages, 0, 0); |
| 1748 | |
| 1749 | if (unlikely(bo->vm_node == NULL)) { |
| 1750 | ret = -ENOMEM; |
| 1751 | goto out_unlock; |
| 1752 | } |
| 1753 | |
| 1754 | bo->vm_node = drm_mm_get_block_atomic(bo->vm_node, |
| 1755 | bo->mem.num_pages, 0); |
| 1756 | |
| 1757 | if (unlikely(bo->vm_node == NULL)) { |
| 1758 | write_unlock(&bdev->vm_lock); |
| 1759 | goto retry_pre_get; |
| 1760 | } |
| 1761 | |
| 1762 | ttm_bo_vm_insert_rb(bo); |
| 1763 | write_unlock(&bdev->vm_lock); |
| 1764 | bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT; |
| 1765 | |
| 1766 | return 0; |
| 1767 | out_unlock: |
| 1768 | write_unlock(&bdev->vm_lock); |
| 1769 | return ret; |
| 1770 | } |
| 1771 | |
| 1772 | int ttm_bo_wait(struct ttm_buffer_object *bo, |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1773 | bool lazy, bool interruptible, bool no_wait) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1774 | { |
| 1775 | struct ttm_bo_driver *driver = bo->bdev->driver; |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1776 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1777 | void *sync_obj; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1778 | int ret = 0; |
| 1779 | |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1780 | if (likely(bo->sync_obj == NULL)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1781 | return 0; |
| 1782 | |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1783 | while (bo->sync_obj) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1784 | |
Maarten Lankhorst | dedfdff | 2012-10-12 15:04:00 +0000 | [diff] [blame] | 1785 | if (driver->sync_obj_signaled(bo->sync_obj)) { |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1786 | void *tmp_obj = bo->sync_obj; |
| 1787 | bo->sync_obj = NULL; |
| 1788 | clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags); |
| 1789 | spin_unlock(&bdev->fence_lock); |
| 1790 | driver->sync_obj_unref(&tmp_obj); |
| 1791 | spin_lock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1792 | continue; |
| 1793 | } |
| 1794 | |
| 1795 | if (no_wait) |
| 1796 | return -EBUSY; |
| 1797 | |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1798 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1799 | spin_unlock(&bdev->fence_lock); |
Maarten Lankhorst | dedfdff | 2012-10-12 15:04:00 +0000 | [diff] [blame] | 1800 | ret = driver->sync_obj_wait(sync_obj, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1801 | lazy, interruptible); |
| 1802 | if (unlikely(ret != 0)) { |
| 1803 | driver->sync_obj_unref(&sync_obj); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1804 | spin_lock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1805 | return ret; |
| 1806 | } |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1807 | spin_lock(&bdev->fence_lock); |
Maarten Lankhorst | 5fb4ef0 | 2012-10-12 15:02:19 +0000 | [diff] [blame] | 1808 | if (likely(bo->sync_obj == sync_obj)) { |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1809 | void *tmp_obj = bo->sync_obj; |
| 1810 | bo->sync_obj = NULL; |
| 1811 | clear_bit(TTM_BO_PRIV_FLAG_MOVING, |
| 1812 | &bo->priv_flags); |
| 1813 | spin_unlock(&bdev->fence_lock); |
| 1814 | driver->sync_obj_unref(&sync_obj); |
| 1815 | driver->sync_obj_unref(&tmp_obj); |
| 1816 | spin_lock(&bdev->fence_lock); |
Thomas Hellstrom | fee280d | 2009-08-03 12:39:06 +0200 | [diff] [blame] | 1817 | } else { |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1818 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | fee280d | 2009-08-03 12:39:06 +0200 | [diff] [blame] | 1819 | driver->sync_obj_unref(&sync_obj); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1820 | spin_lock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1821 | } |
| 1822 | } |
| 1823 | return 0; |
| 1824 | } |
| 1825 | EXPORT_SYMBOL(ttm_bo_wait); |
| 1826 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1827 | int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait) |
| 1828 | { |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1829 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1830 | int ret = 0; |
| 1831 | |
| 1832 | /* |
Thomas Hellstrom | 8cfe92d | 2010-04-28 11:33:25 +0200 | [diff] [blame] | 1833 | * Using ttm_bo_reserve makes sure the lru lists are updated. |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1834 | */ |
| 1835 | |
| 1836 | ret = ttm_bo_reserve(bo, true, no_wait, false, 0); |
| 1837 | if (unlikely(ret != 0)) |
| 1838 | return ret; |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1839 | spin_lock(&bdev->fence_lock); |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1840 | ret = ttm_bo_wait(bo, false, true, no_wait); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1841 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1842 | if (likely(ret == 0)) |
| 1843 | atomic_inc(&bo->cpu_writers); |
| 1844 | ttm_bo_unreserve(bo); |
| 1845 | return ret; |
| 1846 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 1847 | EXPORT_SYMBOL(ttm_bo_synccpu_write_grab); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1848 | |
| 1849 | void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo) |
| 1850 | { |
Maarten Lankhorst | 654aa792 | 2012-11-06 14:39:43 +0100 | [diff] [blame] | 1851 | atomic_dec(&bo->cpu_writers); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1852 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 1853 | EXPORT_SYMBOL(ttm_bo_synccpu_write_release); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1854 | |
| 1855 | /** |
| 1856 | * A buffer object shrink method that tries to swap out the first |
| 1857 | * buffer object on the bo_global::swap_lru list. |
| 1858 | */ |
| 1859 | |
| 1860 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink) |
| 1861 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1862 | struct ttm_bo_global *glob = |
| 1863 | container_of(shrink, struct ttm_bo_global, shrink); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1864 | struct ttm_buffer_object *bo; |
| 1865 | int ret = -EBUSY; |
| 1866 | int put_count; |
| 1867 | uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM); |
| 1868 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1869 | spin_lock(&glob->lru_lock); |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1870 | list_for_each_entry(bo, &glob->swap_lru, swap) { |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 1871 | ret = ttm_bo_reserve_nolru(bo, false, true, false, 0); |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1872 | if (!ret) |
| 1873 | break; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1874 | } |
| 1875 | |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1876 | if (ret) { |
| 1877 | spin_unlock(&glob->lru_lock); |
| 1878 | return ret; |
| 1879 | } |
| 1880 | |
| 1881 | kref_get(&bo->list_kref); |
| 1882 | |
| 1883 | if (!list_empty(&bo->ddestroy)) { |
| 1884 | ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false); |
| 1885 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 1886 | return ret; |
| 1887 | } |
| 1888 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1889 | put_count = ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1890 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1891 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 1892 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1893 | |
| 1894 | /** |
| 1895 | * Wait for GPU, then move to system cached. |
| 1896 | */ |
| 1897 | |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1898 | spin_lock(&bo->bdev->fence_lock); |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1899 | ret = ttm_bo_wait(bo, false, false, false); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 1900 | spin_unlock(&bo->bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1901 | |
| 1902 | if (unlikely(ret != 0)) |
| 1903 | goto out; |
| 1904 | |
| 1905 | if ((bo->mem.placement & swap_placement) != swap_placement) { |
| 1906 | struct ttm_mem_reg evict_mem; |
| 1907 | |
| 1908 | evict_mem = bo->mem; |
| 1909 | evict_mem.mm_node = NULL; |
| 1910 | evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; |
| 1911 | evict_mem.mem_type = TTM_PL_SYSTEM; |
| 1912 | |
| 1913 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1914 | false, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1915 | if (unlikely(ret != 0)) |
| 1916 | goto out; |
| 1917 | } |
| 1918 | |
| 1919 | ttm_bo_unmap_virtual(bo); |
| 1920 | |
| 1921 | /** |
| 1922 | * Swap out. Buffer will be swapped in again as soon as |
| 1923 | * anyone tries to access a ttm page. |
| 1924 | */ |
| 1925 | |
Thomas Hellstrom | 3f09ea4 | 2010-01-13 22:28:40 +0100 | [diff] [blame] | 1926 | if (bo->bdev->driver->swap_notify) |
| 1927 | bo->bdev->driver->swap_notify(bo); |
| 1928 | |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1929 | ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1930 | out: |
| 1931 | |
| 1932 | /** |
| 1933 | * |
| 1934 | * Unreserve without putting on LRU to avoid swapping out an |
| 1935 | * already swapped buffer. |
| 1936 | */ |
| 1937 | |
| 1938 | atomic_set(&bo->reserved, 0); |
| 1939 | wake_up_all(&bo->event_queue); |
| 1940 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 1941 | return ret; |
| 1942 | } |
| 1943 | |
| 1944 | void ttm_bo_swapout_all(struct ttm_bo_device *bdev) |
| 1945 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1946 | while (ttm_bo_swapout(&bdev->glob->shrink) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1947 | ; |
| 1948 | } |
Thomas Hellstrom | e99e1e7 | 2010-01-13 22:28:42 +0100 | [diff] [blame] | 1949 | EXPORT_SYMBOL(ttm_bo_swapout_all); |