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