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