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