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