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