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