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