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