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