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