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