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