1 1.31 riastrad /* $NetBSD: ttm_bo.c,v 1.31 2022/02/14 09:25:39 riastradh Exp $ */ 2 1.11 riastrad 3 1.21 riastrad /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 4 1.1 riastrad /************************************************************************** 5 1.1 riastrad * 6 1.1 riastrad * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 7 1.1 riastrad * All Rights Reserved. 8 1.1 riastrad * 9 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 10 1.1 riastrad * copy of this software and associated documentation files (the 11 1.1 riastrad * "Software"), to deal in the Software without restriction, including 12 1.1 riastrad * without limitation the rights to use, copy, modify, merge, publish, 13 1.1 riastrad * distribute, sub license, and/or sell copies of the Software, and to 14 1.1 riastrad * permit persons to whom the Software is furnished to do so, subject to 15 1.1 riastrad * the following conditions: 16 1.1 riastrad * 17 1.1 riastrad * The above copyright notice and this permission notice (including the 18 1.1 riastrad * next paragraph) shall be included in all copies or substantial portions 19 1.1 riastrad * of the Software. 20 1.1 riastrad * 21 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 24 1.1 riastrad * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 25 1.1 riastrad * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 26 1.1 riastrad * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 27 1.1 riastrad * USE OR OTHER DEALINGS IN THE SOFTWARE. 28 1.1 riastrad * 29 1.1 riastrad **************************************************************************/ 30 1.1 riastrad /* 31 1.1 riastrad * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 32 1.1 riastrad */ 33 1.1 riastrad 34 1.11 riastrad #include <sys/cdefs.h> 35 1.31 riastrad __KERNEL_RCSID(0, "$NetBSD: ttm_bo.c,v 1.31 2022/02/14 09:25:39 riastradh Exp $"); 36 1.11 riastrad 37 1.1 riastrad #define pr_fmt(fmt) "[TTM] " fmt 38 1.1 riastrad 39 1.2 riastrad #ifdef __NetBSD__ 40 1.2 riastrad #include <sys/types.h> 41 1.2 riastrad #include <uvm/uvm_extern.h> 42 1.5 riastrad #include <uvm/uvm_object.h> 43 1.2 riastrad #endif 44 1.2 riastrad 45 1.23 riastrad #include <drm/drm_prime.h> 46 1.1 riastrad #include <drm/ttm/ttm_module.h> 47 1.1 riastrad #include <drm/ttm/ttm_bo_driver.h> 48 1.1 riastrad #include <drm/ttm/ttm_placement.h> 49 1.1 riastrad #include <linux/jiffies.h> 50 1.1 riastrad #include <linux/slab.h> 51 1.1 riastrad #include <linux/sched.h> 52 1.1 riastrad #include <linux/mm.h> 53 1.1 riastrad #include <linux/file.h> 54 1.1 riastrad #include <linux/module.h> 55 1.1 riastrad #include <linux/atomic.h> 56 1.21 riastrad #include <linux/dma-resv.h> 57 1.1 riastrad 58 1.17 riastrad #include <linux/nbsd-namespace.h> 59 1.17 riastrad 60 1.29 riastrad #ifndef __NetBSD__ /* XXX sysfs */ 61 1.1 riastrad static void ttm_bo_global_kobj_release(struct kobject *kobj); 62 1.2 riastrad #endif 63 1.1 riastrad 64 1.21 riastrad /** 65 1.21 riastrad * ttm_global_mutex - protecting the global BO state 66 1.21 riastrad */ 67 1.29 riastrad #ifdef __NetBSD__ 68 1.29 riastrad static struct mutex ttm_global_mutex; 69 1.29 riastrad unsigned ttm_bo_glob_use_count; 70 1.29 riastrad struct ttm_bo_global ttm_bo_glob; 71 1.29 riastrad #else 72 1.21 riastrad DEFINE_MUTEX(ttm_global_mutex); 73 1.21 riastrad unsigned ttm_bo_glob_use_count; 74 1.21 riastrad struct ttm_bo_global ttm_bo_glob; 75 1.21 riastrad EXPORT_SYMBOL(ttm_bo_glob); 76 1.29 riastrad #endif 77 1.21 riastrad 78 1.29 riastrad #ifndef __NetBSD__ /* XXX sysfs */ 79 1.1 riastrad static struct attribute ttm_bo_count = { 80 1.1 riastrad .name = "bo_count", 81 1.1 riastrad .mode = S_IRUGO 82 1.1 riastrad }; 83 1.2 riastrad #endif 84 1.1 riastrad 85 1.21 riastrad /* default destructor */ 86 1.21 riastrad static void ttm_bo_default_destroy(struct ttm_buffer_object *bo) 87 1.21 riastrad { 88 1.21 riastrad kfree(bo); 89 1.21 riastrad } 90 1.21 riastrad 91 1.11 riastrad static inline int ttm_mem_type_from_place(const struct ttm_place *place, 92 1.11 riastrad uint32_t *mem_type) 93 1.1 riastrad { 94 1.21 riastrad int pos; 95 1.21 riastrad 96 1.21 riastrad pos = ffs(place->flags & TTM_PL_MASK_MEM); 97 1.21 riastrad if (unlikely(!pos)) 98 1.21 riastrad return -EINVAL; 99 1.1 riastrad 100 1.21 riastrad *mem_type = pos - 1; 101 1.21 riastrad return 0; 102 1.1 riastrad } 103 1.1 riastrad 104 1.21 riastrad static void ttm_mem_type_debug(struct ttm_bo_device *bdev, struct drm_printer *p, 105 1.21 riastrad int mem_type) 106 1.1 riastrad { 107 1.1 riastrad struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 108 1.1 riastrad 109 1.21 riastrad drm_printf(p, " has_type: %d\n", man->has_type); 110 1.21 riastrad drm_printf(p, " use_type: %d\n", man->use_type); 111 1.21 riastrad drm_printf(p, " flags: 0x%08X\n", man->flags); 112 1.21 riastrad drm_printf(p, " gpu_offset: 0x%08"PRIX64"\n", man->gpu_offset); 113 1.21 riastrad drm_printf(p, " size: %"PRIu64"\n", man->size); 114 1.21 riastrad drm_printf(p, " available_caching: 0x%08X\n", man->available_caching); 115 1.21 riastrad drm_printf(p, " default_caching: 0x%08X\n", man->default_caching); 116 1.1 riastrad if (mem_type != TTM_PL_SYSTEM) 117 1.21 riastrad (*man->func->debug)(man, p); 118 1.1 riastrad } 119 1.1 riastrad 120 1.1 riastrad static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, 121 1.1 riastrad struct ttm_placement *placement) 122 1.1 riastrad { 123 1.21 riastrad struct drm_printer p = drm_debug_printer(TTM_PFX); 124 1.1 riastrad int i, ret, mem_type; 125 1.1 riastrad 126 1.21 riastrad drm_printf(&p, "No space for %p (%lu pages, %luK, %luM)\n", 127 1.21 riastrad bo, bo->mem.num_pages, bo->mem.size >> 10, 128 1.21 riastrad bo->mem.size >> 20); 129 1.1 riastrad for (i = 0; i < placement->num_placement; i++) { 130 1.11 riastrad ret = ttm_mem_type_from_place(&placement->placement[i], 131 1.1 riastrad &mem_type); 132 1.1 riastrad if (ret) 133 1.1 riastrad return; 134 1.21 riastrad drm_printf(&p, " placement[%d]=0x%08X (%d)\n", 135 1.21 riastrad i, placement->placement[i].flags, mem_type); 136 1.21 riastrad ttm_mem_type_debug(bo->bdev, &p, mem_type); 137 1.1 riastrad } 138 1.1 riastrad } 139 1.1 riastrad 140 1.2 riastrad #ifndef __NetBSD__ /* XXX sysfs */ 141 1.1 riastrad static ssize_t ttm_bo_global_show(struct kobject *kobj, 142 1.1 riastrad struct attribute *attr, 143 1.1 riastrad char *buffer) 144 1.1 riastrad { 145 1.1 riastrad struct ttm_bo_global *glob = 146 1.1 riastrad container_of(kobj, struct ttm_bo_global, kobj); 147 1.1 riastrad 148 1.21 riastrad return snprintf(buffer, PAGE_SIZE, "%d\n", 149 1.21 riastrad atomic_read(&glob->bo_count)); 150 1.1 riastrad } 151 1.1 riastrad 152 1.1 riastrad static struct attribute *ttm_bo_global_attrs[] = { 153 1.1 riastrad &ttm_bo_count, 154 1.1 riastrad NULL 155 1.1 riastrad }; 156 1.1 riastrad 157 1.1 riastrad static const struct sysfs_ops ttm_bo_global_ops = { 158 1.1 riastrad .show = &ttm_bo_global_show 159 1.1 riastrad }; 160 1.1 riastrad 161 1.1 riastrad static struct kobj_type ttm_bo_glob_kobj_type = { 162 1.1 riastrad .release = &ttm_bo_global_kobj_release, 163 1.1 riastrad .sysfs_ops = &ttm_bo_global_ops, 164 1.1 riastrad .default_attrs = ttm_bo_global_attrs 165 1.1 riastrad }; 166 1.2 riastrad #endif /* __NetBSD__ */ 167 1.1 riastrad 168 1.1 riastrad 169 1.1 riastrad static inline uint32_t ttm_bo_type_flags(unsigned type) 170 1.1 riastrad { 171 1.1 riastrad return 1 << (type); 172 1.1 riastrad } 173 1.1 riastrad 174 1.1 riastrad static void ttm_bo_release_list(struct kref *list_kref) 175 1.1 riastrad { 176 1.1 riastrad struct ttm_buffer_object *bo = 177 1.1 riastrad container_of(list_kref, struct ttm_buffer_object, list_kref); 178 1.1 riastrad size_t acc_size = bo->acc_size; 179 1.1 riastrad 180 1.21 riastrad BUG_ON(kref_read(&bo->list_kref)); 181 1.21 riastrad BUG_ON(kref_read(&bo->kref)); 182 1.1 riastrad BUG_ON(bo->mem.mm_node != NULL); 183 1.1 riastrad BUG_ON(!list_empty(&bo->lru)); 184 1.1 riastrad BUG_ON(!list_empty(&bo->ddestroy)); 185 1.21 riastrad ttm_tt_destroy(bo->ttm); 186 1.21 riastrad atomic_dec(&ttm_bo_glob.bo_count); 187 1.21 riastrad dma_fence_put(bo->moving); 188 1.21 riastrad if (!ttm_bo_uses_embedded_gem_object(bo)) 189 1.21 riastrad dma_resv_fini(&bo->base._resv); 190 1.21 riastrad bo->destroy(bo); 191 1.21 riastrad ttm_mem_global_free(&ttm_mem_glob, acc_size); 192 1.1 riastrad } 193 1.1 riastrad 194 1.21 riastrad static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo, 195 1.21 riastrad struct ttm_mem_reg *mem) 196 1.1 riastrad { 197 1.1 riastrad struct ttm_bo_device *bdev = bo->bdev; 198 1.1 riastrad struct ttm_mem_type_manager *man; 199 1.1 riastrad 200 1.21 riastrad dma_resv_assert_held(bo->base.resv); 201 1.1 riastrad 202 1.21 riastrad if (!list_empty(&bo->lru)) 203 1.21 riastrad return; 204 1.1 riastrad 205 1.21 riastrad if (mem->placement & TTM_PL_FLAG_NO_EVICT) 206 1.21 riastrad return; 207 1.21 riastrad 208 1.21 riastrad man = &bdev->man[mem->mem_type]; 209 1.21 riastrad list_add_tail(&bo->lru, &man->lru[bo->priority]); 210 1.21 riastrad kref_get(&bo->list_kref); 211 1.1 riastrad 212 1.21 riastrad if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm && 213 1.21 riastrad !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG | 214 1.21 riastrad TTM_PAGE_FLAG_SWAPPED))) { 215 1.21 riastrad list_add_tail(&bo->swap, &ttm_bo_glob.swap_lru[bo->priority]); 216 1.1 riastrad kref_get(&bo->list_kref); 217 1.21 riastrad } 218 1.21 riastrad } 219 1.1 riastrad 220 1.21 riastrad static void ttm_bo_ref_bug(struct kref *list_kref) 221 1.21 riastrad { 222 1.21 riastrad BUG(); 223 1.1 riastrad } 224 1.1 riastrad 225 1.21 riastrad static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo) 226 1.1 riastrad { 227 1.21 riastrad struct ttm_bo_device *bdev = bo->bdev; 228 1.21 riastrad bool notify = false; 229 1.1 riastrad 230 1.1 riastrad if (!list_empty(&bo->swap)) { 231 1.1 riastrad list_del_init(&bo->swap); 232 1.21 riastrad kref_put(&bo->list_kref, ttm_bo_ref_bug); 233 1.21 riastrad notify = true; 234 1.1 riastrad } 235 1.1 riastrad if (!list_empty(&bo->lru)) { 236 1.1 riastrad list_del_init(&bo->lru); 237 1.21 riastrad kref_put(&bo->list_kref, ttm_bo_ref_bug); 238 1.21 riastrad notify = true; 239 1.1 riastrad } 240 1.1 riastrad 241 1.21 riastrad if (notify && bdev->driver->del_from_lru_notify) 242 1.21 riastrad bdev->driver->del_from_lru_notify(bo); 243 1.1 riastrad } 244 1.1 riastrad 245 1.21 riastrad static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos, 246 1.21 riastrad struct ttm_buffer_object *bo) 247 1.1 riastrad { 248 1.21 riastrad if (!pos->first) 249 1.21 riastrad pos->first = bo; 250 1.21 riastrad pos->last = bo; 251 1.1 riastrad } 252 1.1 riastrad 253 1.21 riastrad void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo, 254 1.21 riastrad struct ttm_lru_bulk_move *bulk) 255 1.1 riastrad { 256 1.21 riastrad dma_resv_assert_held(bo->base.resv); 257 1.21 riastrad 258 1.21 riastrad ttm_bo_del_from_lru(bo); 259 1.21 riastrad ttm_bo_add_mem_to_lru(bo, &bo->mem); 260 1.21 riastrad 261 1.21 riastrad if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { 262 1.21 riastrad switch (bo->mem.mem_type) { 263 1.21 riastrad case TTM_PL_TT: 264 1.21 riastrad ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo); 265 1.21 riastrad break; 266 1.21 riastrad 267 1.21 riastrad case TTM_PL_VRAM: 268 1.21 riastrad ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo); 269 1.21 riastrad break; 270 1.21 riastrad } 271 1.21 riastrad if (bo->ttm && !(bo->ttm->page_flags & 272 1.21 riastrad (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED))) 273 1.21 riastrad ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo); 274 1.21 riastrad } 275 1.1 riastrad } 276 1.21 riastrad EXPORT_SYMBOL(ttm_bo_move_to_lru_tail); 277 1.1 riastrad 278 1.21 riastrad void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk) 279 1.1 riastrad { 280 1.21 riastrad unsigned i; 281 1.21 riastrad 282 1.21 riastrad for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 283 1.21 riastrad struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i]; 284 1.21 riastrad struct ttm_mem_type_manager *man; 285 1.21 riastrad 286 1.21 riastrad if (!pos->first) 287 1.21 riastrad continue; 288 1.1 riastrad 289 1.21 riastrad dma_resv_assert_held(pos->first->base.resv); 290 1.21 riastrad dma_resv_assert_held(pos->last->base.resv); 291 1.21 riastrad 292 1.21 riastrad man = &pos->first->bdev->man[TTM_PL_TT]; 293 1.21 riastrad list_bulk_move_tail(&man->lru[i], &pos->first->lru, 294 1.21 riastrad &pos->last->lru); 295 1.21 riastrad } 296 1.1 riastrad 297 1.21 riastrad for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 298 1.21 riastrad struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i]; 299 1.21 riastrad struct ttm_mem_type_manager *man; 300 1.1 riastrad 301 1.21 riastrad if (!pos->first) 302 1.21 riastrad continue; 303 1.1 riastrad 304 1.21 riastrad dma_resv_assert_held(pos->first->base.resv); 305 1.21 riastrad dma_resv_assert_held(pos->last->base.resv); 306 1.1 riastrad 307 1.21 riastrad man = &pos->first->bdev->man[TTM_PL_VRAM]; 308 1.21 riastrad list_bulk_move_tail(&man->lru[i], &pos->first->lru, 309 1.21 riastrad &pos->last->lru); 310 1.1 riastrad } 311 1.1 riastrad 312 1.21 riastrad for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 313 1.21 riastrad struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i]; 314 1.21 riastrad struct list_head *lru; 315 1.21 riastrad 316 1.21 riastrad if (!pos->first) 317 1.21 riastrad continue; 318 1.21 riastrad 319 1.21 riastrad dma_resv_assert_held(pos->first->base.resv); 320 1.21 riastrad dma_resv_assert_held(pos->last->base.resv); 321 1.6 riastrad 322 1.21 riastrad lru = &ttm_bo_glob.swap_lru[i]; 323 1.21 riastrad list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap); 324 1.21 riastrad } 325 1.1 riastrad } 326 1.21 riastrad EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail); 327 1.1 riastrad 328 1.1 riastrad static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, 329 1.21 riastrad struct ttm_mem_reg *mem, bool evict, 330 1.21 riastrad struct ttm_operation_ctx *ctx) 331 1.1 riastrad { 332 1.1 riastrad struct ttm_bo_device *bdev = bo->bdev; 333 1.1 riastrad bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem); 334 1.1 riastrad bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem); 335 1.1 riastrad struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type]; 336 1.1 riastrad struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type]; 337 1.1 riastrad int ret = 0; 338 1.1 riastrad 339 1.1 riastrad if (old_is_pci || new_is_pci || 340 1.1 riastrad ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) { 341 1.1 riastrad ret = ttm_mem_io_lock(old_man, true); 342 1.1 riastrad if (unlikely(ret != 0)) 343 1.1 riastrad goto out_err; 344 1.1 riastrad ttm_bo_unmap_virtual_locked(bo); 345 1.1 riastrad ttm_mem_io_unlock(old_man); 346 1.1 riastrad } 347 1.1 riastrad 348 1.1 riastrad /* 349 1.1 riastrad * Create and bind a ttm if required. 350 1.1 riastrad */ 351 1.1 riastrad 352 1.1 riastrad if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) { 353 1.1 riastrad if (bo->ttm == NULL) { 354 1.1 riastrad bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED); 355 1.21 riastrad ret = ttm_tt_create(bo, zero); 356 1.1 riastrad if (ret) 357 1.1 riastrad goto out_err; 358 1.1 riastrad } 359 1.1 riastrad 360 1.1 riastrad ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement); 361 1.1 riastrad if (ret) 362 1.1 riastrad goto out_err; 363 1.1 riastrad 364 1.1 riastrad if (mem->mem_type != TTM_PL_SYSTEM) { 365 1.21 riastrad ret = ttm_tt_bind(bo->ttm, mem, ctx); 366 1.1 riastrad if (ret) 367 1.1 riastrad goto out_err; 368 1.1 riastrad } 369 1.1 riastrad 370 1.1 riastrad if (bo->mem.mem_type == TTM_PL_SYSTEM) { 371 1.1 riastrad if (bdev->driver->move_notify) 372 1.21 riastrad bdev->driver->move_notify(bo, evict, mem); 373 1.1 riastrad bo->mem = *mem; 374 1.1 riastrad mem->mm_node = NULL; 375 1.1 riastrad goto moved; 376 1.1 riastrad } 377 1.1 riastrad } 378 1.1 riastrad 379 1.1 riastrad if (bdev->driver->move_notify) 380 1.21 riastrad bdev->driver->move_notify(bo, evict, mem); 381 1.1 riastrad 382 1.1 riastrad if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && 383 1.1 riastrad !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) 384 1.21 riastrad ret = ttm_bo_move_ttm(bo, ctx, mem); 385 1.1 riastrad else if (bdev->driver->move) 386 1.21 riastrad ret = bdev->driver->move(bo, evict, ctx, mem); 387 1.1 riastrad else 388 1.21 riastrad ret = ttm_bo_move_memcpy(bo, ctx, mem); 389 1.1 riastrad 390 1.1 riastrad if (ret) { 391 1.1 riastrad if (bdev->driver->move_notify) { 392 1.21 riastrad swap(*mem, bo->mem); 393 1.21 riastrad bdev->driver->move_notify(bo, false, mem); 394 1.21 riastrad swap(*mem, bo->mem); 395 1.1 riastrad } 396 1.1 riastrad 397 1.1 riastrad goto out_err; 398 1.1 riastrad } 399 1.1 riastrad 400 1.1 riastrad moved: 401 1.1 riastrad if (bo->evicted) { 402 1.2 riastrad if (bdev->driver->invalidate_caches) { 403 1.2 riastrad ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); 404 1.2 riastrad if (ret) 405 1.2 riastrad pr_err("Can not flush read caches\n"); 406 1.2 riastrad } 407 1.1 riastrad bo->evicted = false; 408 1.1 riastrad } 409 1.1 riastrad 410 1.21 riastrad if (bo->mem.mm_node) 411 1.1 riastrad bo->offset = (bo->mem.start << PAGE_SHIFT) + 412 1.1 riastrad bdev->man[bo->mem.mem_type].gpu_offset; 413 1.21 riastrad else 414 1.1 riastrad bo->offset = 0; 415 1.1 riastrad 416 1.21 riastrad ctx->bytes_moved += bo->num_pages << PAGE_SHIFT; 417 1.1 riastrad return 0; 418 1.1 riastrad 419 1.1 riastrad out_err: 420 1.1 riastrad new_man = &bdev->man[bo->mem.mem_type]; 421 1.21 riastrad if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) { 422 1.1 riastrad ttm_tt_destroy(bo->ttm); 423 1.1 riastrad bo->ttm = NULL; 424 1.1 riastrad } 425 1.1 riastrad 426 1.1 riastrad return ret; 427 1.1 riastrad } 428 1.1 riastrad 429 1.1 riastrad /** 430 1.1 riastrad * Call bo::reserved. 431 1.1 riastrad * Will release GPU memory type usage on destruction. 432 1.1 riastrad * This is the place to put in driver specific hooks to release 433 1.1 riastrad * driver private resources. 434 1.1 riastrad * Will release the bo::reserved lock. 435 1.1 riastrad */ 436 1.1 riastrad 437 1.1 riastrad static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) 438 1.1 riastrad { 439 1.1 riastrad if (bo->bdev->driver->move_notify) 440 1.21 riastrad bo->bdev->driver->move_notify(bo, false, NULL); 441 1.1 riastrad 442 1.21 riastrad ttm_tt_destroy(bo->ttm); 443 1.21 riastrad bo->ttm = NULL; 444 1.1 riastrad ttm_bo_mem_put(bo, &bo->mem); 445 1.21 riastrad } 446 1.21 riastrad 447 1.21 riastrad static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo) 448 1.21 riastrad { 449 1.21 riastrad int r; 450 1.21 riastrad 451 1.21 riastrad if (bo->base.resv == &bo->base._resv) 452 1.21 riastrad return 0; 453 1.21 riastrad 454 1.21 riastrad BUG_ON(!dma_resv_trylock(&bo->base._resv)); 455 1.1 riastrad 456 1.21 riastrad r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv); 457 1.21 riastrad if (r) 458 1.21 riastrad dma_resv_unlock(&bo->base._resv); 459 1.21 riastrad 460 1.21 riastrad return r; 461 1.1 riastrad } 462 1.1 riastrad 463 1.11 riastrad static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo) 464 1.11 riastrad { 465 1.21 riastrad struct dma_resv_list *fobj; 466 1.21 riastrad struct dma_fence *fence; 467 1.11 riastrad int i; 468 1.11 riastrad 469 1.21 riastrad fobj = dma_resv_get_list(&bo->base._resv); 470 1.21 riastrad fence = dma_resv_get_excl(&bo->base._resv); 471 1.11 riastrad if (fence && !fence->ops->signaled) 472 1.21 riastrad dma_fence_enable_sw_signaling(fence); 473 1.11 riastrad 474 1.11 riastrad for (i = 0; fobj && i < fobj->shared_count; ++i) { 475 1.11 riastrad fence = rcu_dereference_protected(fobj->shared[i], 476 1.21 riastrad dma_resv_held(bo->base.resv)); 477 1.11 riastrad 478 1.11 riastrad if (!fence->ops->signaled) 479 1.21 riastrad dma_fence_enable_sw_signaling(fence); 480 1.11 riastrad } 481 1.11 riastrad } 482 1.11 riastrad 483 1.1 riastrad static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo) 484 1.1 riastrad { 485 1.1 riastrad struct ttm_bo_device *bdev = bo->bdev; 486 1.1 riastrad int ret; 487 1.1 riastrad 488 1.21 riastrad ret = ttm_bo_individualize_resv(bo); 489 1.21 riastrad if (ret) { 490 1.21 riastrad /* Last resort, if we fail to allocate memory for the 491 1.21 riastrad * fences block for the BO to become idle 492 1.21 riastrad */ 493 1.21 riastrad dma_resv_wait_timeout_rcu(bo->base.resv, true, false, 494 1.21 riastrad 30 * HZ); 495 1.21 riastrad spin_lock(&ttm_bo_glob.lru_lock); 496 1.21 riastrad goto error; 497 1.21 riastrad } 498 1.1 riastrad 499 1.21 riastrad spin_lock(&ttm_bo_glob.lru_lock); 500 1.21 riastrad ret = dma_resv_trylock(bo->base.resv) ? 0 : -EBUSY; 501 1.11 riastrad if (!ret) { 502 1.21 riastrad if (dma_resv_test_signaled_rcu(&bo->base._resv, true)) { 503 1.21 riastrad ttm_bo_del_from_lru(bo); 504 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 505 1.21 riastrad if (bo->base.resv != &bo->base._resv) 506 1.21 riastrad dma_resv_unlock(&bo->base._resv); 507 1.1 riastrad 508 1.11 riastrad ttm_bo_cleanup_memtype_use(bo); 509 1.21 riastrad dma_resv_unlock(bo->base.resv); 510 1.21 riastrad return; 511 1.21 riastrad } 512 1.1 riastrad 513 1.21 riastrad ttm_bo_flush_all_fences(bo); 514 1.2 riastrad 515 1.2 riastrad /* 516 1.2 riastrad * Make NO_EVICT bos immediately available to 517 1.2 riastrad * shrinkers, now that they are queued for 518 1.2 riastrad * destruction. 519 1.2 riastrad */ 520 1.2 riastrad if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) { 521 1.2 riastrad bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT; 522 1.21 riastrad ttm_bo_move_to_lru_tail(bo, NULL); 523 1.2 riastrad } 524 1.2 riastrad 525 1.21 riastrad dma_resv_unlock(bo->base.resv); 526 1.1 riastrad } 527 1.21 riastrad if (bo->base.resv != &bo->base._resv) 528 1.21 riastrad dma_resv_unlock(&bo->base._resv); 529 1.1 riastrad 530 1.21 riastrad error: 531 1.1 riastrad kref_get(&bo->list_kref); 532 1.1 riastrad list_add_tail(&bo->ddestroy, &bdev->ddestroy); 533 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 534 1.1 riastrad 535 1.1 riastrad schedule_delayed_work(&bdev->wq, 536 1.12 riastrad ((HZ / 100) < 1) ? 1 : HZ / 100); 537 1.1 riastrad } 538 1.1 riastrad 539 1.1 riastrad /** 540 1.21 riastrad * function ttm_bo_cleanup_refs 541 1.1 riastrad * If bo idle, remove from delayed- and lru lists, and unref. 542 1.1 riastrad * If not idle, do nothing. 543 1.1 riastrad * 544 1.1 riastrad * Must be called with lru_lock and reservation held, this function 545 1.21 riastrad * will drop the lru lock and optionally the reservation lock before returning. 546 1.1 riastrad * 547 1.1 riastrad * @interruptible Any sleeps should occur interruptibly. 548 1.1 riastrad * @no_wait_gpu Never wait for gpu. Return -EBUSY instead. 549 1.21 riastrad * @unlock_resv Unlock the reservation lock as well. 550 1.1 riastrad */ 551 1.1 riastrad 552 1.21 riastrad static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, 553 1.21 riastrad bool interruptible, bool no_wait_gpu, 554 1.21 riastrad bool unlock_resv) 555 1.1 riastrad { 556 1.21 riastrad struct dma_resv *resv; 557 1.1 riastrad int ret; 558 1.1 riastrad 559 1.21 riastrad if (unlikely(list_empty(&bo->ddestroy))) 560 1.21 riastrad resv = bo->base.resv; 561 1.21 riastrad else 562 1.21 riastrad resv = &bo->base._resv; 563 1.21 riastrad 564 1.21 riastrad if (dma_resv_test_signaled_rcu(resv, true)) 565 1.21 riastrad ret = 0; 566 1.21 riastrad else 567 1.21 riastrad ret = -EBUSY; 568 1.1 riastrad 569 1.1 riastrad if (ret && !no_wait_gpu) { 570 1.11 riastrad long lret; 571 1.1 riastrad 572 1.21 riastrad if (unlock_resv) 573 1.21 riastrad dma_resv_unlock(bo->base.resv); 574 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 575 1.21 riastrad 576 1.21 riastrad lret = dma_resv_wait_timeout_rcu(resv, true, 577 1.11 riastrad interruptible, 578 1.11 riastrad 30 * HZ); 579 1.11 riastrad 580 1.11 riastrad if (lret < 0) 581 1.11 riastrad return lret; 582 1.11 riastrad else if (lret == 0) 583 1.11 riastrad return -EBUSY; 584 1.1 riastrad 585 1.21 riastrad spin_lock(&ttm_bo_glob.lru_lock); 586 1.21 riastrad if (unlock_resv && !dma_resv_trylock(bo->base.resv)) { 587 1.21 riastrad /* 588 1.21 riastrad * We raced, and lost, someone else holds the reservation now, 589 1.21 riastrad * and is probably busy in ttm_bo_cleanup_memtype_use. 590 1.21 riastrad * 591 1.21 riastrad * Even if it's not the case, because we finished waiting any 592 1.21 riastrad * delayed destruction would succeed, so just return success 593 1.21 riastrad * here. 594 1.21 riastrad */ 595 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 596 1.1 riastrad return 0; 597 1.1 riastrad } 598 1.21 riastrad ret = 0; 599 1.11 riastrad } 600 1.1 riastrad 601 1.1 riastrad if (ret || unlikely(list_empty(&bo->ddestroy))) { 602 1.21 riastrad if (unlock_resv) 603 1.21 riastrad dma_resv_unlock(bo->base.resv); 604 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 605 1.1 riastrad return ret; 606 1.1 riastrad } 607 1.1 riastrad 608 1.21 riastrad ttm_bo_del_from_lru(bo); 609 1.1 riastrad list_del_init(&bo->ddestroy); 610 1.21 riastrad kref_put(&bo->list_kref, ttm_bo_ref_bug); 611 1.1 riastrad 612 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 613 1.1 riastrad ttm_bo_cleanup_memtype_use(bo); 614 1.1 riastrad 615 1.21 riastrad if (unlock_resv) 616 1.21 riastrad dma_resv_unlock(bo->base.resv); 617 1.1 riastrad 618 1.1 riastrad return 0; 619 1.1 riastrad } 620 1.1 riastrad 621 1.1 riastrad /** 622 1.1 riastrad * Traverse the delayed list, and call ttm_bo_cleanup_refs on all 623 1.1 riastrad * encountered buffers. 624 1.1 riastrad */ 625 1.21 riastrad static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) 626 1.21 riastrad { 627 1.21 riastrad struct ttm_bo_global *glob = &ttm_bo_glob; 628 1.21 riastrad struct list_head removed; 629 1.21 riastrad bool empty; 630 1.1 riastrad 631 1.21 riastrad INIT_LIST_HEAD(&removed); 632 1.1 riastrad 633 1.1 riastrad spin_lock(&glob->lru_lock); 634 1.21 riastrad while (!list_empty(&bdev->ddestroy)) { 635 1.21 riastrad struct ttm_buffer_object *bo; 636 1.1 riastrad 637 1.21 riastrad bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object, 638 1.21 riastrad ddestroy); 639 1.21 riastrad kref_get(&bo->list_kref); 640 1.21 riastrad list_move_tail(&bo->ddestroy, &removed); 641 1.1 riastrad 642 1.21 riastrad if (remove_all || bo->base.resv != &bo->base._resv) { 643 1.2 riastrad spin_unlock(&glob->lru_lock); 644 1.21 riastrad dma_resv_lock(bo->base.resv, NULL); 645 1.21 riastrad 646 1.2 riastrad spin_lock(&glob->lru_lock); 647 1.21 riastrad ttm_bo_cleanup_refs(bo, false, !remove_all, true); 648 1.2 riastrad 649 1.21 riastrad } else if (dma_resv_trylock(bo->base.resv)) { 650 1.21 riastrad ttm_bo_cleanup_refs(bo, false, !remove_all, true); 651 1.21 riastrad } else { 652 1.1 riastrad spin_unlock(&glob->lru_lock); 653 1.21 riastrad } 654 1.1 riastrad 655 1.21 riastrad kref_put(&bo->list_kref, ttm_bo_release_list); 656 1.1 riastrad spin_lock(&glob->lru_lock); 657 1.1 riastrad } 658 1.21 riastrad list_splice_tail(&removed, &bdev->ddestroy); 659 1.21 riastrad empty = list_empty(&bdev->ddestroy); 660 1.21 riastrad spin_unlock(&glob->lru_lock); 661 1.1 riastrad 662 1.21 riastrad return empty; 663 1.1 riastrad } 664 1.1 riastrad 665 1.1 riastrad static void ttm_bo_delayed_workqueue(struct work_struct *work) 666 1.1 riastrad { 667 1.1 riastrad struct ttm_bo_device *bdev = 668 1.1 riastrad container_of(work, struct ttm_bo_device, wq.work); 669 1.1 riastrad 670 1.21 riastrad if (!ttm_bo_delayed_delete(bdev, false)) 671 1.1 riastrad schedule_delayed_work(&bdev->wq, 672 1.12 riastrad ((HZ / 100) < 1) ? 1 : HZ / 100); 673 1.1 riastrad } 674 1.1 riastrad 675 1.1 riastrad static void ttm_bo_release(struct kref *kref) 676 1.1 riastrad { 677 1.1 riastrad struct ttm_buffer_object *bo = 678 1.1 riastrad container_of(kref, struct ttm_buffer_object, kref); 679 1.1 riastrad struct ttm_bo_device *bdev = bo->bdev; 680 1.1 riastrad struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; 681 1.1 riastrad 682 1.21 riastrad if (bo->bdev->driver->release_notify) 683 1.21 riastrad bo->bdev->driver->release_notify(bo); 684 1.21 riastrad 685 1.2 riastrad #ifdef __NetBSD__ 686 1.2 riastrad uvm_obj_destroy(&bo->uvmobj, true); 687 1.2 riastrad #endif 688 1.21 riastrad drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node); 689 1.2 riastrad #ifdef __NetBSD__ 690 1.28 riastrad if (!ttm_bo_uses_embedded_gem_object(bo)) 691 1.28 riastrad drm_vma_node_destroy(&bo->base.vma_node); 692 1.2 riastrad #endif 693 1.1 riastrad ttm_mem_io_lock(man, false); 694 1.1 riastrad ttm_mem_io_free_vm(bo); 695 1.1 riastrad ttm_mem_io_unlock(man); 696 1.1 riastrad ttm_bo_cleanup_refs_or_queue(bo); 697 1.1 riastrad kref_put(&bo->list_kref, ttm_bo_release_list); 698 1.1 riastrad } 699 1.1 riastrad 700 1.21 riastrad void ttm_bo_put(struct ttm_buffer_object *bo) 701 1.1 riastrad { 702 1.1 riastrad kref_put(&bo->kref, ttm_bo_release); 703 1.1 riastrad } 704 1.21 riastrad EXPORT_SYMBOL(ttm_bo_put); 705 1.1 riastrad 706 1.1 riastrad int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev) 707 1.1 riastrad { 708 1.1 riastrad return cancel_delayed_work_sync(&bdev->wq); 709 1.1 riastrad } 710 1.1 riastrad EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue); 711 1.1 riastrad 712 1.1 riastrad void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched) 713 1.1 riastrad { 714 1.1 riastrad if (resched) 715 1.1 riastrad schedule_delayed_work(&bdev->wq, 716 1.12 riastrad ((HZ / 100) < 1) ? 1 : HZ / 100); 717 1.1 riastrad } 718 1.1 riastrad EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue); 719 1.1 riastrad 720 1.21 riastrad static int ttm_bo_evict(struct ttm_buffer_object *bo, 721 1.21 riastrad struct ttm_operation_ctx *ctx) 722 1.1 riastrad { 723 1.1 riastrad struct ttm_bo_device *bdev = bo->bdev; 724 1.1 riastrad struct ttm_mem_reg evict_mem; 725 1.1 riastrad struct ttm_placement placement; 726 1.1 riastrad int ret = 0; 727 1.1 riastrad 728 1.21 riastrad dma_resv_assert_held(bo->base.resv); 729 1.21 riastrad 730 1.21 riastrad placement.num_placement = 0; 731 1.21 riastrad placement.num_busy_placement = 0; 732 1.21 riastrad bdev->driver->evict_flags(bo, &placement); 733 1.21 riastrad 734 1.21 riastrad if (!placement.num_placement && !placement.num_busy_placement) { 735 1.21 riastrad ret = ttm_bo_pipeline_gutting(bo); 736 1.21 riastrad if (ret) 737 1.21 riastrad return ret; 738 1.1 riastrad 739 1.21 riastrad return ttm_tt_create(bo, false); 740 1.1 riastrad } 741 1.1 riastrad 742 1.1 riastrad evict_mem = bo->mem; 743 1.1 riastrad evict_mem.mm_node = NULL; 744 1.1 riastrad evict_mem.bus.io_reserved_vm = false; 745 1.1 riastrad evict_mem.bus.io_reserved_count = 0; 746 1.1 riastrad 747 1.21 riastrad ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx); 748 1.1 riastrad if (ret) { 749 1.1 riastrad if (ret != -ERESTARTSYS) { 750 1.1 riastrad pr_err("Failed to find memory space for buffer 0x%p eviction\n", 751 1.1 riastrad bo); 752 1.1 riastrad ttm_bo_mem_space_debug(bo, &placement); 753 1.1 riastrad } 754 1.1 riastrad goto out; 755 1.1 riastrad } 756 1.1 riastrad 757 1.21 riastrad ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx); 758 1.21 riastrad if (unlikely(ret)) { 759 1.1 riastrad if (ret != -ERESTARTSYS) 760 1.1 riastrad pr_err("Buffer eviction failed\n"); 761 1.1 riastrad ttm_bo_mem_put(bo, &evict_mem); 762 1.1 riastrad goto out; 763 1.1 riastrad } 764 1.1 riastrad bo->evicted = true; 765 1.1 riastrad out: 766 1.1 riastrad return ret; 767 1.1 riastrad } 768 1.1 riastrad 769 1.21 riastrad bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, 770 1.21 riastrad const struct ttm_place *place) 771 1.21 riastrad { 772 1.21 riastrad /* Don't evict this BO if it's outside of the 773 1.21 riastrad * requested placement range 774 1.21 riastrad */ 775 1.21 riastrad if (place->fpfn >= (bo->mem.start + bo->mem.size) || 776 1.21 riastrad (place->lpfn && place->lpfn <= bo->mem.start)) 777 1.21 riastrad return false; 778 1.21 riastrad 779 1.21 riastrad return true; 780 1.21 riastrad } 781 1.21 riastrad EXPORT_SYMBOL(ttm_bo_eviction_valuable); 782 1.21 riastrad 783 1.21 riastrad /** 784 1.21 riastrad * Check the target bo is allowable to be evicted or swapout, including cases: 785 1.21 riastrad * 786 1.21 riastrad * a. if share same reservation object with ctx->resv, have assumption 787 1.21 riastrad * reservation objects should already be locked, so not lock again and 788 1.21 riastrad * return true directly when either the opreation allow_reserved_eviction 789 1.21 riastrad * or the target bo already is in delayed free list; 790 1.21 riastrad * 791 1.21 riastrad * b. Otherwise, trylock it. 792 1.21 riastrad */ 793 1.21 riastrad static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo, 794 1.21 riastrad struct ttm_operation_ctx *ctx, bool *locked, bool *busy) 795 1.21 riastrad { 796 1.21 riastrad bool ret = false; 797 1.21 riastrad 798 1.21 riastrad if (bo->base.resv == ctx->resv) { 799 1.21 riastrad dma_resv_assert_held(bo->base.resv); 800 1.21 riastrad if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT 801 1.21 riastrad || !list_empty(&bo->ddestroy)) 802 1.21 riastrad ret = true; 803 1.21 riastrad *locked = false; 804 1.21 riastrad if (busy) 805 1.21 riastrad *busy = false; 806 1.21 riastrad } else { 807 1.21 riastrad ret = dma_resv_trylock(bo->base.resv); 808 1.21 riastrad *locked = ret; 809 1.21 riastrad if (busy) 810 1.21 riastrad *busy = !ret; 811 1.21 riastrad } 812 1.21 riastrad 813 1.21 riastrad return ret; 814 1.21 riastrad } 815 1.21 riastrad 816 1.21 riastrad /** 817 1.21 riastrad * ttm_mem_evict_wait_busy - wait for a busy BO to become available 818 1.21 riastrad * 819 1.21 riastrad * @busy_bo: BO which couldn't be locked with trylock 820 1.21 riastrad * @ctx: operation context 821 1.21 riastrad * @ticket: acquire ticket 822 1.21 riastrad * 823 1.21 riastrad * Try to lock a busy buffer object to avoid failing eviction. 824 1.21 riastrad */ 825 1.21 riastrad static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo, 826 1.21 riastrad struct ttm_operation_ctx *ctx, 827 1.21 riastrad struct ww_acquire_ctx *ticket) 828 1.21 riastrad { 829 1.21 riastrad int r; 830 1.21 riastrad 831 1.21 riastrad if (!busy_bo || !ticket) 832 1.21 riastrad return -EBUSY; 833 1.21 riastrad 834 1.21 riastrad if (ctx->interruptible) 835 1.21 riastrad r = dma_resv_lock_interruptible(busy_bo->base.resv, 836 1.21 riastrad ticket); 837 1.21 riastrad else 838 1.21 riastrad r = dma_resv_lock(busy_bo->base.resv, ticket); 839 1.21 riastrad 840 1.21 riastrad /* 841 1.21 riastrad * TODO: It would be better to keep the BO locked until allocation is at 842 1.21 riastrad * least tried one more time, but that would mean a much larger rework 843 1.21 riastrad * of TTM. 844 1.21 riastrad */ 845 1.21 riastrad if (!r) 846 1.21 riastrad dma_resv_unlock(busy_bo->base.resv); 847 1.21 riastrad 848 1.21 riastrad return r == -EDEADLK ? -EBUSY : r; 849 1.21 riastrad } 850 1.21 riastrad 851 1.1 riastrad static int ttm_mem_evict_first(struct ttm_bo_device *bdev, 852 1.21 riastrad uint32_t mem_type, 853 1.21 riastrad const struct ttm_place *place, 854 1.21 riastrad struct ttm_operation_ctx *ctx, 855 1.21 riastrad struct ww_acquire_ctx *ticket) 856 1.1 riastrad { 857 1.21 riastrad struct ttm_buffer_object *bo = NULL, *busy_bo = NULL; 858 1.1 riastrad struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 859 1.21 riastrad bool locked = false; 860 1.21 riastrad unsigned i; 861 1.21 riastrad int ret; 862 1.1 riastrad 863 1.21 riastrad spin_lock(&ttm_bo_glob.lru_lock); 864 1.21 riastrad for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 865 1.21 riastrad list_for_each_entry(bo, &man->lru[i], lru) { 866 1.21 riastrad bool busy; 867 1.21 riastrad 868 1.21 riastrad if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, 869 1.21 riastrad &busy)) { 870 1.21 riastrad if (busy && !busy_bo && ticket != 871 1.21 riastrad dma_resv_locking_ctx(bo->base.resv)) 872 1.21 riastrad busy_bo = bo; 873 1.21 riastrad continue; 874 1.11 riastrad } 875 1.11 riastrad 876 1.21 riastrad if (place && !bdev->driver->eviction_valuable(bo, 877 1.21 riastrad place)) { 878 1.21 riastrad if (locked) 879 1.21 riastrad dma_resv_unlock(bo->base.resv); 880 1.21 riastrad continue; 881 1.21 riastrad } 882 1.1 riastrad break; 883 1.11 riastrad } 884 1.21 riastrad 885 1.21 riastrad /* If the inner loop terminated early, we have our candidate */ 886 1.21 riastrad if (&bo->lru != &man->lru[i]) 887 1.21 riastrad break; 888 1.21 riastrad 889 1.21 riastrad bo = NULL; 890 1.1 riastrad } 891 1.1 riastrad 892 1.21 riastrad if (!bo) { 893 1.21 riastrad if (busy_bo) 894 1.21 riastrad kref_get(&busy_bo->list_kref); 895 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 896 1.21 riastrad ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket); 897 1.21 riastrad if (busy_bo) 898 1.21 riastrad kref_put(&busy_bo->list_kref, ttm_bo_release_list); 899 1.1 riastrad return ret; 900 1.1 riastrad } 901 1.1 riastrad 902 1.1 riastrad kref_get(&bo->list_kref); 903 1.1 riastrad 904 1.1 riastrad if (!list_empty(&bo->ddestroy)) { 905 1.21 riastrad ret = ttm_bo_cleanup_refs(bo, ctx->interruptible, 906 1.21 riastrad ctx->no_wait_gpu, locked); 907 1.1 riastrad kref_put(&bo->list_kref, ttm_bo_release_list); 908 1.1 riastrad return ret; 909 1.1 riastrad } 910 1.1 riastrad 911 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 912 1.1 riastrad 913 1.21 riastrad ret = ttm_bo_evict(bo, ctx); 914 1.21 riastrad if (locked) 915 1.21 riastrad ttm_bo_unreserve(bo); 916 1.1 riastrad 917 1.1 riastrad kref_put(&bo->list_kref, ttm_bo_release_list); 918 1.1 riastrad return ret; 919 1.1 riastrad } 920 1.1 riastrad 921 1.1 riastrad void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem) 922 1.1 riastrad { 923 1.1 riastrad struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type]; 924 1.1 riastrad 925 1.1 riastrad if (mem->mm_node) 926 1.1 riastrad (*man->func->put_node)(man, mem); 927 1.1 riastrad } 928 1.1 riastrad EXPORT_SYMBOL(ttm_bo_mem_put); 929 1.1 riastrad 930 1.1 riastrad /** 931 1.21 riastrad * Add the last move fence to the BO and reserve a new shared slot. 932 1.21 riastrad */ 933 1.21 riastrad static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo, 934 1.21 riastrad struct ttm_mem_type_manager *man, 935 1.21 riastrad struct ttm_mem_reg *mem, 936 1.21 riastrad bool no_wait_gpu) 937 1.21 riastrad { 938 1.21 riastrad struct dma_fence *fence; 939 1.21 riastrad int ret; 940 1.21 riastrad 941 1.21 riastrad spin_lock(&man->move_lock); 942 1.21 riastrad fence = dma_fence_get(man->move); 943 1.21 riastrad spin_unlock(&man->move_lock); 944 1.21 riastrad 945 1.21 riastrad if (!fence) 946 1.21 riastrad return 0; 947 1.21 riastrad 948 1.21 riastrad if (no_wait_gpu) 949 1.21 riastrad return -EBUSY; 950 1.21 riastrad 951 1.21 riastrad dma_resv_add_shared_fence(bo->base.resv, fence); 952 1.21 riastrad 953 1.21 riastrad ret = dma_resv_reserve_shared(bo->base.resv, 1); 954 1.21 riastrad if (unlikely(ret)) { 955 1.21 riastrad dma_fence_put(fence); 956 1.21 riastrad return ret; 957 1.21 riastrad } 958 1.21 riastrad 959 1.21 riastrad dma_fence_put(bo->moving); 960 1.21 riastrad bo->moving = fence; 961 1.21 riastrad return 0; 962 1.21 riastrad } 963 1.21 riastrad 964 1.21 riastrad /** 965 1.1 riastrad * Repeatedly evict memory from the LRU for @mem_type until we create enough 966 1.1 riastrad * space, or we've evicted everything and there isn't enough space. 967 1.1 riastrad */ 968 1.1 riastrad static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, 969 1.21 riastrad const struct ttm_place *place, 970 1.21 riastrad struct ttm_mem_reg *mem, 971 1.21 riastrad struct ttm_operation_ctx *ctx) 972 1.1 riastrad { 973 1.1 riastrad struct ttm_bo_device *bdev = bo->bdev; 974 1.21 riastrad struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; 975 1.21 riastrad struct ww_acquire_ctx *ticket; 976 1.1 riastrad int ret; 977 1.1 riastrad 978 1.21 riastrad ticket = dma_resv_locking_ctx(bo->base.resv); 979 1.1 riastrad do { 980 1.11 riastrad ret = (*man->func->get_node)(man, bo, place, mem); 981 1.1 riastrad if (unlikely(ret != 0)) 982 1.1 riastrad return ret; 983 1.1 riastrad if (mem->mm_node) 984 1.1 riastrad break; 985 1.21 riastrad ret = ttm_mem_evict_first(bdev, mem->mem_type, place, ctx, 986 1.21 riastrad ticket); 987 1.1 riastrad if (unlikely(ret != 0)) 988 1.1 riastrad return ret; 989 1.1 riastrad } while (1); 990 1.21 riastrad 991 1.21 riastrad return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu); 992 1.1 riastrad } 993 1.1 riastrad 994 1.1 riastrad static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man, 995 1.1 riastrad uint32_t cur_placement, 996 1.1 riastrad uint32_t proposed_placement) 997 1.1 riastrad { 998 1.1 riastrad uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING; 999 1.1 riastrad uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING; 1000 1.1 riastrad 1001 1.1 riastrad /** 1002 1.1 riastrad * Keep current caching if possible. 1003 1.1 riastrad */ 1004 1.1 riastrad 1005 1.1 riastrad if ((cur_placement & caching) != 0) 1006 1.1 riastrad result |= (cur_placement & caching); 1007 1.1 riastrad else if ((man->default_caching & caching) != 0) 1008 1.1 riastrad result |= man->default_caching; 1009 1.1 riastrad else if ((TTM_PL_FLAG_CACHED & caching) != 0) 1010 1.1 riastrad result |= TTM_PL_FLAG_CACHED; 1011 1.1 riastrad else if ((TTM_PL_FLAG_WC & caching) != 0) 1012 1.1 riastrad result |= TTM_PL_FLAG_WC; 1013 1.1 riastrad else if ((TTM_PL_FLAG_UNCACHED & caching) != 0) 1014 1.1 riastrad result |= TTM_PL_FLAG_UNCACHED; 1015 1.1 riastrad 1016 1.1 riastrad return result; 1017 1.1 riastrad } 1018 1.1 riastrad 1019 1.1 riastrad static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, 1020 1.1 riastrad uint32_t mem_type, 1021 1.11 riastrad const struct ttm_place *place, 1022 1.1 riastrad uint32_t *masked_placement) 1023 1.1 riastrad { 1024 1.1 riastrad uint32_t cur_flags = ttm_bo_type_flags(mem_type); 1025 1.1 riastrad 1026 1.11 riastrad if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0) 1027 1.1 riastrad return false; 1028 1.1 riastrad 1029 1.11 riastrad if ((place->flags & man->available_caching) == 0) 1030 1.1 riastrad return false; 1031 1.1 riastrad 1032 1.11 riastrad cur_flags |= (place->flags & man->available_caching); 1033 1.1 riastrad 1034 1.1 riastrad *masked_placement = cur_flags; 1035 1.1 riastrad return true; 1036 1.1 riastrad } 1037 1.1 riastrad 1038 1.1 riastrad /** 1039 1.21 riastrad * ttm_bo_mem_placement - check if placement is compatible 1040 1.21 riastrad * @bo: BO to find memory for 1041 1.21 riastrad * @place: where to search 1042 1.21 riastrad * @mem: the memory object to fill in 1043 1.21 riastrad * @ctx: operation context 1044 1.21 riastrad * 1045 1.21 riastrad * Check if placement is compatible and fill in mem structure. 1046 1.21 riastrad * Returns -EBUSY if placement won't work or negative error code. 1047 1.21 riastrad * 0 when placement can be used. 1048 1.21 riastrad */ 1049 1.21 riastrad static int ttm_bo_mem_placement(struct ttm_buffer_object *bo, 1050 1.21 riastrad const struct ttm_place *place, 1051 1.21 riastrad struct ttm_mem_reg *mem, 1052 1.21 riastrad struct ttm_operation_ctx *ctx) 1053 1.21 riastrad { 1054 1.21 riastrad struct ttm_bo_device *bdev = bo->bdev; 1055 1.21 riastrad uint32_t mem_type = TTM_PL_SYSTEM; 1056 1.21 riastrad struct ttm_mem_type_manager *man; 1057 1.21 riastrad uint32_t cur_flags = 0; 1058 1.21 riastrad int ret; 1059 1.21 riastrad 1060 1.21 riastrad ret = ttm_mem_type_from_place(place, &mem_type); 1061 1.21 riastrad if (ret) 1062 1.21 riastrad return ret; 1063 1.21 riastrad 1064 1.21 riastrad man = &bdev->man[mem_type]; 1065 1.21 riastrad if (!man->has_type || !man->use_type) 1066 1.21 riastrad return -EBUSY; 1067 1.21 riastrad 1068 1.21 riastrad if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags)) 1069 1.21 riastrad return -EBUSY; 1070 1.21 riastrad 1071 1.21 riastrad cur_flags = ttm_bo_select_caching(man, bo->mem.placement, cur_flags); 1072 1.21 riastrad /* 1073 1.21 riastrad * Use the access and other non-mapping-related flag bits from 1074 1.21 riastrad * the memory placement flags to the current flags 1075 1.21 riastrad */ 1076 1.21 riastrad ttm_flag_masked(&cur_flags, place->flags, ~TTM_PL_MASK_MEMTYPE); 1077 1.21 riastrad 1078 1.21 riastrad mem->mem_type = mem_type; 1079 1.21 riastrad mem->placement = cur_flags; 1080 1.21 riastrad 1081 1.21 riastrad spin_lock(&ttm_bo_glob.lru_lock); 1082 1.21 riastrad ttm_bo_del_from_lru(bo); 1083 1.21 riastrad ttm_bo_add_mem_to_lru(bo, mem); 1084 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 1085 1.21 riastrad 1086 1.21 riastrad return 0; 1087 1.21 riastrad } 1088 1.21 riastrad 1089 1.21 riastrad /** 1090 1.1 riastrad * Creates space for memory region @mem according to its type. 1091 1.1 riastrad * 1092 1.1 riastrad * This function first searches for free space in compatible memory types in 1093 1.1 riastrad * the priority order defined by the driver. If free space isn't found, then 1094 1.1 riastrad * ttm_bo_mem_force_space is attempted in priority order to evict and find 1095 1.1 riastrad * space. 1096 1.1 riastrad */ 1097 1.1 riastrad int ttm_bo_mem_space(struct ttm_buffer_object *bo, 1098 1.1 riastrad struct ttm_placement *placement, 1099 1.1 riastrad struct ttm_mem_reg *mem, 1100 1.21 riastrad struct ttm_operation_ctx *ctx) 1101 1.1 riastrad { 1102 1.1 riastrad struct ttm_bo_device *bdev = bo->bdev; 1103 1.1 riastrad bool type_found = false; 1104 1.1 riastrad int i, ret; 1105 1.1 riastrad 1106 1.21 riastrad ret = dma_resv_reserve_shared(bo->base.resv, 1); 1107 1.21 riastrad if (unlikely(ret)) 1108 1.21 riastrad return ret; 1109 1.21 riastrad 1110 1.1 riastrad mem->mm_node = NULL; 1111 1.1 riastrad for (i = 0; i < placement->num_placement; ++i) { 1112 1.11 riastrad const struct ttm_place *place = &placement->placement[i]; 1113 1.21 riastrad struct ttm_mem_type_manager *man; 1114 1.11 riastrad 1115 1.21 riastrad ret = ttm_bo_mem_placement(bo, place, mem, ctx); 1116 1.21 riastrad if (ret == -EBUSY) 1117 1.21 riastrad continue; 1118 1.1 riastrad if (ret) 1119 1.21 riastrad goto error; 1120 1.1 riastrad 1121 1.11 riastrad type_found = true; 1122 1.21 riastrad mem->mm_node = NULL; 1123 1.21 riastrad if (mem->mem_type == TTM_PL_SYSTEM) 1124 1.21 riastrad return 0; 1125 1.1 riastrad 1126 1.21 riastrad man = &bdev->man[mem->mem_type]; 1127 1.11 riastrad ret = (*man->func->get_node)(man, bo, place, mem); 1128 1.11 riastrad if (unlikely(ret)) 1129 1.21 riastrad goto error; 1130 1.21 riastrad 1131 1.21 riastrad if (!mem->mm_node) 1132 1.21 riastrad continue; 1133 1.21 riastrad 1134 1.21 riastrad ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu); 1135 1.21 riastrad if (unlikely(ret)) { 1136 1.21 riastrad (*man->func->put_node)(man, mem); 1137 1.21 riastrad if (ret == -EBUSY) 1138 1.21 riastrad continue; 1139 1.1 riastrad 1140 1.21 riastrad goto error; 1141 1.21 riastrad } 1142 1.1 riastrad return 0; 1143 1.1 riastrad } 1144 1.1 riastrad 1145 1.11 riastrad for (i = 0; i < placement->num_busy_placement; ++i) { 1146 1.11 riastrad const struct ttm_place *place = &placement->busy_placement[i]; 1147 1.1 riastrad 1148 1.21 riastrad ret = ttm_bo_mem_placement(bo, place, mem, ctx); 1149 1.21 riastrad if (ret == -EBUSY) 1150 1.21 riastrad continue; 1151 1.1 riastrad if (ret) 1152 1.21 riastrad goto error; 1153 1.1 riastrad 1154 1.11 riastrad type_found = true; 1155 1.21 riastrad mem->mm_node = NULL; 1156 1.21 riastrad if (mem->mem_type == TTM_PL_SYSTEM) 1157 1.21 riastrad return 0; 1158 1.1 riastrad 1159 1.21 riastrad ret = ttm_bo_mem_force_space(bo, place, mem, ctx); 1160 1.21 riastrad if (ret == 0 && mem->mm_node) 1161 1.1 riastrad return 0; 1162 1.1 riastrad 1163 1.21 riastrad if (ret && ret != -EBUSY) 1164 1.21 riastrad goto error; 1165 1.1 riastrad } 1166 1.11 riastrad 1167 1.21 riastrad ret = -ENOMEM; 1168 1.11 riastrad if (!type_found) { 1169 1.21 riastrad pr_err(TTM_PFX "No compatible memory type found\n"); 1170 1.21 riastrad ret = -EINVAL; 1171 1.21 riastrad } 1172 1.21 riastrad 1173 1.21 riastrad error: 1174 1.21 riastrad if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) { 1175 1.21 riastrad spin_lock(&ttm_bo_glob.lru_lock); 1176 1.21 riastrad ttm_bo_move_to_lru_tail(bo, NULL); 1177 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 1178 1.11 riastrad } 1179 1.11 riastrad 1180 1.21 riastrad return ret; 1181 1.1 riastrad } 1182 1.1 riastrad EXPORT_SYMBOL(ttm_bo_mem_space); 1183 1.1 riastrad 1184 1.2 riastrad static int ttm_bo_move_buffer(struct ttm_buffer_object *bo, 1185 1.21 riastrad struct ttm_placement *placement, 1186 1.21 riastrad struct ttm_operation_ctx *ctx) 1187 1.1 riastrad { 1188 1.1 riastrad int ret = 0; 1189 1.1 riastrad struct ttm_mem_reg mem; 1190 1.1 riastrad 1191 1.21 riastrad dma_resv_assert_held(bo->base.resv); 1192 1.1 riastrad 1193 1.31 riastrad memset(&mem, 0, sizeof(mem)); 1194 1.1 riastrad mem.num_pages = bo->num_pages; 1195 1.1 riastrad mem.size = mem.num_pages << PAGE_SHIFT; 1196 1.1 riastrad mem.page_alignment = bo->mem.page_alignment; 1197 1.10 riastrad mem.bus.is_iomem = false; 1198 1.1 riastrad mem.bus.io_reserved_vm = false; 1199 1.1 riastrad mem.bus.io_reserved_count = 0; 1200 1.1 riastrad /* 1201 1.1 riastrad * Determine where to move the buffer. 1202 1.1 riastrad */ 1203 1.21 riastrad ret = ttm_bo_mem_space(bo, placement, &mem, ctx); 1204 1.1 riastrad if (ret) 1205 1.1 riastrad goto out_unlock; 1206 1.21 riastrad ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx); 1207 1.1 riastrad out_unlock: 1208 1.1 riastrad if (ret && mem.mm_node) 1209 1.1 riastrad ttm_bo_mem_put(bo, &mem); 1210 1.1 riastrad return ret; 1211 1.1 riastrad } 1212 1.1 riastrad 1213 1.21 riastrad static bool ttm_bo_places_compat(const struct ttm_place *places, 1214 1.21 riastrad unsigned num_placement, 1215 1.21 riastrad struct ttm_mem_reg *mem, 1216 1.21 riastrad uint32_t *new_flags) 1217 1.1 riastrad { 1218 1.21 riastrad unsigned i; 1219 1.21 riastrad 1220 1.21 riastrad for (i = 0; i < num_placement; i++) { 1221 1.21 riastrad const struct ttm_place *heap = &places[i]; 1222 1.1 riastrad 1223 1.21 riastrad if (mem->mm_node && (mem->start < heap->fpfn || 1224 1.11 riastrad (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn))) 1225 1.11 riastrad continue; 1226 1.1 riastrad 1227 1.11 riastrad *new_flags = heap->flags; 1228 1.2 riastrad if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && 1229 1.21 riastrad (*new_flags & mem->placement & TTM_PL_MASK_MEM) && 1230 1.21 riastrad (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) || 1231 1.21 riastrad (mem->placement & TTM_PL_FLAG_CONTIGUOUS))) 1232 1.2 riastrad return true; 1233 1.2 riastrad } 1234 1.21 riastrad return false; 1235 1.21 riastrad } 1236 1.2 riastrad 1237 1.21 riastrad bool ttm_bo_mem_compat(struct ttm_placement *placement, 1238 1.21 riastrad struct ttm_mem_reg *mem, 1239 1.21 riastrad uint32_t *new_flags) 1240 1.21 riastrad { 1241 1.21 riastrad if (ttm_bo_places_compat(placement->placement, placement->num_placement, 1242 1.21 riastrad mem, new_flags)) 1243 1.21 riastrad return true; 1244 1.21 riastrad 1245 1.21 riastrad if ((placement->busy_placement != placement->placement || 1246 1.21 riastrad placement->num_busy_placement > placement->num_placement) && 1247 1.21 riastrad ttm_bo_places_compat(placement->busy_placement, 1248 1.21 riastrad placement->num_busy_placement, 1249 1.21 riastrad mem, new_flags)) 1250 1.21 riastrad return true; 1251 1.2 riastrad 1252 1.2 riastrad return false; 1253 1.1 riastrad } 1254 1.11 riastrad EXPORT_SYMBOL(ttm_bo_mem_compat); 1255 1.1 riastrad 1256 1.1 riastrad int ttm_bo_validate(struct ttm_buffer_object *bo, 1257 1.21 riastrad struct ttm_placement *placement, 1258 1.21 riastrad struct ttm_operation_ctx *ctx) 1259 1.1 riastrad { 1260 1.1 riastrad int ret; 1261 1.2 riastrad uint32_t new_flags; 1262 1.1 riastrad 1263 1.21 riastrad dma_resv_assert_held(bo->base.resv); 1264 1.1 riastrad /* 1265 1.1 riastrad * Check whether we need to move buffer. 1266 1.1 riastrad */ 1267 1.2 riastrad if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) { 1268 1.21 riastrad ret = ttm_bo_move_buffer(bo, placement, ctx); 1269 1.1 riastrad if (ret) 1270 1.1 riastrad return ret; 1271 1.1 riastrad } else { 1272 1.1 riastrad /* 1273 1.1 riastrad * Use the access and other non-mapping-related flag bits from 1274 1.1 riastrad * the compatible memory placement flags to the active flags 1275 1.1 riastrad */ 1276 1.2 riastrad ttm_flag_masked(&bo->mem.placement, new_flags, 1277 1.1 riastrad ~TTM_PL_MASK_MEMTYPE); 1278 1.1 riastrad } 1279 1.1 riastrad /* 1280 1.1 riastrad * We might need to add a TTM. 1281 1.1 riastrad */ 1282 1.1 riastrad if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { 1283 1.21 riastrad ret = ttm_tt_create(bo, true); 1284 1.1 riastrad if (ret) 1285 1.1 riastrad return ret; 1286 1.1 riastrad } 1287 1.1 riastrad return 0; 1288 1.1 riastrad } 1289 1.1 riastrad EXPORT_SYMBOL(ttm_bo_validate); 1290 1.1 riastrad 1291 1.21 riastrad int ttm_bo_init_reserved(struct ttm_bo_device *bdev, 1292 1.21 riastrad struct ttm_buffer_object *bo, 1293 1.21 riastrad unsigned long size, 1294 1.21 riastrad enum ttm_bo_type type, 1295 1.21 riastrad struct ttm_placement *placement, 1296 1.21 riastrad uint32_t page_alignment, 1297 1.21 riastrad struct ttm_operation_ctx *ctx, 1298 1.21 riastrad size_t acc_size, 1299 1.21 riastrad struct sg_table *sg, 1300 1.21 riastrad struct dma_resv *resv, 1301 1.21 riastrad void (*destroy) (struct ttm_buffer_object *)) 1302 1.1 riastrad { 1303 1.21 riastrad struct ttm_mem_global *mem_glob = &ttm_mem_glob; 1304 1.1 riastrad int ret = 0; 1305 1.1 riastrad unsigned long num_pages; 1306 1.2 riastrad bool locked; 1307 1.1 riastrad 1308 1.14 riastrad if (sg && !drm_prime_sg_importable(bdev->dmat, sg)) { 1309 1.14 riastrad pr_err("DRM prime buffer violates DMA constraints\n"); 1310 1.14 riastrad return -EIO; 1311 1.14 riastrad } 1312 1.14 riastrad 1313 1.21 riastrad ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx); 1314 1.1 riastrad if (ret) { 1315 1.1 riastrad pr_err("Out of kernel memory\n"); 1316 1.1 riastrad if (destroy) 1317 1.1 riastrad (*destroy)(bo); 1318 1.1 riastrad else 1319 1.1 riastrad kfree(bo); 1320 1.1 riastrad return -ENOMEM; 1321 1.1 riastrad } 1322 1.1 riastrad 1323 1.1 riastrad num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 1324 1.1 riastrad if (num_pages == 0) { 1325 1.1 riastrad pr_err("Illegal buffer object size\n"); 1326 1.1 riastrad if (destroy) 1327 1.1 riastrad (*destroy)(bo); 1328 1.1 riastrad else 1329 1.1 riastrad kfree(bo); 1330 1.1 riastrad ttm_mem_global_free(mem_glob, acc_size); 1331 1.1 riastrad return -EINVAL; 1332 1.1 riastrad } 1333 1.21 riastrad bo->destroy = destroy ? destroy : ttm_bo_default_destroy; 1334 1.1 riastrad 1335 1.1 riastrad kref_init(&bo->kref); 1336 1.1 riastrad kref_init(&bo->list_kref); 1337 1.1 riastrad INIT_LIST_HEAD(&bo->lru); 1338 1.1 riastrad INIT_LIST_HEAD(&bo->ddestroy); 1339 1.1 riastrad INIT_LIST_HEAD(&bo->swap); 1340 1.1 riastrad INIT_LIST_HEAD(&bo->io_reserve_lru); 1341 1.1 riastrad bo->bdev = bdev; 1342 1.1 riastrad bo->type = type; 1343 1.1 riastrad bo->num_pages = num_pages; 1344 1.1 riastrad bo->mem.size = num_pages << PAGE_SHIFT; 1345 1.1 riastrad bo->mem.mem_type = TTM_PL_SYSTEM; 1346 1.1 riastrad bo->mem.num_pages = bo->num_pages; 1347 1.1 riastrad bo->mem.mm_node = NULL; 1348 1.1 riastrad bo->mem.page_alignment = page_alignment; 1349 1.1 riastrad bo->mem.bus.io_reserved_vm = false; 1350 1.1 riastrad bo->mem.bus.io_reserved_count = 0; 1351 1.21 riastrad bo->moving = NULL; 1352 1.1 riastrad bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); 1353 1.1 riastrad bo->acc_size = acc_size; 1354 1.1 riastrad bo->sg = sg; 1355 1.11 riastrad if (resv) { 1356 1.21 riastrad bo->base.resv = resv; 1357 1.21 riastrad dma_resv_assert_held(bo->base.resv); 1358 1.11 riastrad } else { 1359 1.21 riastrad bo->base.resv = &bo->base._resv; 1360 1.11 riastrad } 1361 1.21 riastrad if (!ttm_bo_uses_embedded_gem_object(bo)) { 1362 1.21 riastrad /* 1363 1.21 riastrad * bo.gem is not initialized, so we have to setup the 1364 1.21 riastrad * struct elements we want use regardless. 1365 1.21 riastrad */ 1366 1.21 riastrad dma_resv_init(&bo->base._resv); 1367 1.2 riastrad #ifdef __NetBSD__ 1368 1.22 riastrad drm_vma_node_init(&bo->base.vma_node); 1369 1.2 riastrad #else 1370 1.21 riastrad drm_vma_node_reset(&bo->base.vma_node); 1371 1.2 riastrad #endif 1372 1.21 riastrad } 1373 1.27 riastrad #ifdef __NetBSD__ 1374 1.27 riastrad uvm_obj_init(&bo->uvmobj, bdev->driver->ttm_uvm_ops, true, 1); 1375 1.27 riastrad #endif 1376 1.21 riastrad atomic_inc(&ttm_bo_glob.bo_count); 1377 1.1 riastrad 1378 1.1 riastrad /* 1379 1.1 riastrad * For ttm_bo_type_device buffers, allocate 1380 1.1 riastrad * address space from the device. 1381 1.1 riastrad */ 1382 1.11 riastrad if (bo->type == ttm_bo_type_device || 1383 1.11 riastrad bo->type == ttm_bo_type_sg) 1384 1.21 riastrad ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node, 1385 1.2 riastrad bo->mem.num_pages); 1386 1.2 riastrad 1387 1.11 riastrad /* passed reservation objects should already be locked, 1388 1.11 riastrad * since otherwise lockdep will be angered in radeon. 1389 1.11 riastrad */ 1390 1.11 riastrad if (!resv) { 1391 1.21 riastrad locked = dma_resv_trylock(bo->base.resv); 1392 1.11 riastrad WARN_ON(!locked); 1393 1.11 riastrad } 1394 1.1 riastrad 1395 1.2 riastrad if (likely(!ret)) 1396 1.21 riastrad ret = ttm_bo_validate(bo, placement, ctx); 1397 1.21 riastrad 1398 1.21 riastrad if (unlikely(ret)) { 1399 1.21 riastrad if (!resv) 1400 1.21 riastrad ttm_bo_unreserve(bo); 1401 1.21 riastrad 1402 1.21 riastrad ttm_bo_put(bo); 1403 1.21 riastrad return ret; 1404 1.21 riastrad } 1405 1.21 riastrad 1406 1.21 riastrad spin_lock(&ttm_bo_glob.lru_lock); 1407 1.21 riastrad ttm_bo_move_to_lru_tail(bo, NULL); 1408 1.21 riastrad spin_unlock(&ttm_bo_glob.lru_lock); 1409 1.21 riastrad 1410 1.21 riastrad return ret; 1411 1.21 riastrad } 1412 1.21 riastrad EXPORT_SYMBOL(ttm_bo_init_reserved); 1413 1.21 riastrad 1414 1.21 riastrad int ttm_bo_init(struct ttm_bo_device *bdev, 1415 1.21 riastrad struct ttm_buffer_object *bo, 1416 1.21 riastrad unsigned long size, 1417 1.21 riastrad enum ttm_bo_type type, 1418 1.21 riastrad struct ttm_placement *placement, 1419 1.21 riastrad uint32_t page_alignment, 1420 1.21 riastrad bool interruptible, 1421 1.21 riastrad size_t acc_size, 1422 1.21 riastrad struct sg_table *sg, 1423 1.21 riastrad struct dma_resv *resv, 1424 1.21 riastrad void (*destroy) (struct ttm_buffer_object *)) 1425 1.21 riastrad { 1426 1.21 riastrad struct ttm_operation_ctx ctx = { interruptible, false }; 1427 1.21 riastrad int ret; 1428 1.21 riastrad 1429 1.21 riastrad ret = ttm_bo_init_reserved(bdev, bo, size, type, placement, 1430 1.21 riastrad page_alignment, &ctx, acc_size, 1431 1.21 riastrad sg, resv, destroy); 1432 1.21 riastrad if (ret) 1433 1.21 riastrad return ret; 1434 1.1 riastrad 1435 1.11 riastrad if (!resv) 1436 1.11 riastrad ttm_bo_unreserve(bo); 1437 1.1 riastrad 1438 1.21 riastrad return 0; 1439 1.1 riastrad } 1440 1.1 riastrad EXPORT_SYMBOL(ttm_bo_init); 1441 1.1 riastrad 1442 1.1 riastrad size_t ttm_bo_acc_size(struct ttm_bo_device *bdev, 1443 1.1 riastrad unsigned long bo_size, 1444 1.1 riastrad unsigned struct_size) 1445 1.1 riastrad { 1446 1.1 riastrad unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; 1447 1.1 riastrad size_t size = 0; 1448 1.1 riastrad 1449 1.1 riastrad size += ttm_round_pot(struct_size); 1450 1.21 riastrad size += ttm_round_pot(npages * sizeof(void *)); 1451 1.1 riastrad size += ttm_round_pot(sizeof(struct ttm_tt)); 1452 1.1 riastrad return size; 1453 1.1 riastrad } 1454 1.1 riastrad EXPORT_SYMBOL(ttm_bo_acc_size); 1455 1.1 riastrad 1456 1.1 riastrad size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, 1457 1.1 riastrad unsigned long bo_size, 1458 1.1 riastrad unsigned struct_size) 1459 1.1 riastrad { 1460 1.1 riastrad unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; 1461 1.1 riastrad size_t size = 0; 1462 1.1 riastrad 1463 1.1 riastrad size += ttm_round_pot(struct_size); 1464 1.21 riastrad size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t))); 1465 1.1 riastrad size += ttm_round_pot(sizeof(struct ttm_dma_tt)); 1466 1.1 riastrad return size; 1467 1.1 riastrad } 1468 1.1 riastrad EXPORT_SYMBOL(ttm_bo_dma_acc_size); 1469 1.1 riastrad 1470 1.1 riastrad int ttm_bo_create(struct ttm_bo_device *bdev, 1471 1.1 riastrad unsigned long size, 1472 1.1 riastrad enum ttm_bo_type type, 1473 1.1 riastrad struct ttm_placement *placement, 1474 1.1 riastrad uint32_t page_alignment, 1475 1.1 riastrad bool interruptible, 1476 1.1 riastrad struct ttm_buffer_object **p_bo) 1477 1.1 riastrad { 1478 1.1 riastrad struct ttm_buffer_object *bo; 1479 1.1 riastrad size_t acc_size; 1480 1.1 riastrad int ret; 1481 1.1 riastrad 1482 1.1 riastrad bo = kzalloc(sizeof(*bo), GFP_KERNEL); 1483 1.1 riastrad if (unlikely(bo == NULL)) 1484 1.1 riastrad return -ENOMEM; 1485 1.1 riastrad 1486 1.1 riastrad acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object)); 1487 1.1 riastrad ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment, 1488 1.21 riastrad interruptible, acc_size, 1489 1.11 riastrad NULL, NULL, NULL); 1490 1.1 riastrad if (likely(ret == 0)) 1491 1.1 riastrad *p_bo = bo; 1492 1.1 riastrad 1493 1.1 riastrad return ret; 1494 1.1 riastrad } 1495 1.1 riastrad EXPORT_SYMBOL(ttm_bo_create); 1496 1.1 riastrad 1497 1.1 riastrad static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, 1498 1.21 riastrad unsigned mem_type) 1499 1.1 riastrad { 1500 1.21 riastrad struct ttm_operation_ctx ctx = { 1501 1.21 riastrad .interruptible = false, 1502 1.21 riastrad .no_wait_gpu = false, 1503 1.21 riastrad .flags = TTM_OPT_FLAG_FORCE_ALLOC 1504 1.21 riastrad }; 1505 1.1 riastrad struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 1506 1.21 riastrad struct ttm_bo_global *glob = &ttm_bo_glob; 1507 1.21 riastrad struct dma_fence *fence; 1508 1.1 riastrad int ret; 1509 1.21 riastrad unsigned i; 1510 1.1 riastrad 1511 1.1 riastrad /* 1512 1.1 riastrad * Can't use standard list traversal since we're unlocking. 1513 1.1 riastrad */ 1514 1.1 riastrad 1515 1.1 riastrad spin_lock(&glob->lru_lock); 1516 1.21 riastrad for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 1517 1.21 riastrad while (!list_empty(&man->lru[i])) { 1518 1.21 riastrad spin_unlock(&glob->lru_lock); 1519 1.21 riastrad ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx, 1520 1.21 riastrad NULL); 1521 1.21 riastrad if (ret) 1522 1.1 riastrad return ret; 1523 1.21 riastrad spin_lock(&glob->lru_lock); 1524 1.1 riastrad } 1525 1.1 riastrad } 1526 1.1 riastrad spin_unlock(&glob->lru_lock); 1527 1.21 riastrad 1528 1.21 riastrad spin_lock(&man->move_lock); 1529 1.21 riastrad fence = dma_fence_get(man->move); 1530 1.21 riastrad spin_unlock(&man->move_lock); 1531 1.21 riastrad 1532 1.21 riastrad if (fence) { 1533 1.21 riastrad ret = dma_fence_wait(fence, false); 1534 1.21 riastrad dma_fence_put(fence); 1535 1.21 riastrad if (ret) 1536 1.21 riastrad return ret; 1537 1.21 riastrad } 1538 1.21 riastrad 1539 1.1 riastrad return 0; 1540 1.1 riastrad } 1541 1.1 riastrad 1542 1.1 riastrad int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type) 1543 1.1 riastrad { 1544 1.1 riastrad struct ttm_mem_type_manager *man; 1545 1.1 riastrad int ret = -EINVAL; 1546 1.1 riastrad 1547 1.1 riastrad if (mem_type >= TTM_NUM_MEM_TYPES) { 1548 1.1 riastrad pr_err("Illegal memory type %d\n", mem_type); 1549 1.1 riastrad return ret; 1550 1.1 riastrad } 1551 1.1 riastrad man = &bdev->man[mem_type]; 1552 1.1 riastrad 1553 1.1 riastrad if (!man->has_type) { 1554 1.1 riastrad pr_err("Trying to take down uninitialized memory manager type %u\n", 1555 1.1 riastrad mem_type); 1556 1.1 riastrad return ret; 1557 1.1 riastrad } 1558 1.1 riastrad 1559 1.1 riastrad man->use_type = false; 1560 1.1 riastrad man->has_type = false; 1561 1.1 riastrad 1562 1.1 riastrad ret = 0; 1563 1.1 riastrad if (mem_type > 0) { 1564 1.21 riastrad ret = ttm_bo_force_list_clean(bdev, mem_type); 1565 1.21 riastrad if (ret) { 1566 1.21 riastrad pr_err("Cleanup eviction failed\n"); 1567 1.21 riastrad return ret; 1568 1.21 riastrad } 1569 1.1 riastrad 1570 1.1 riastrad ret = (*man->func->takedown)(man); 1571 1.1 riastrad } 1572 1.1 riastrad 1573 1.21 riastrad dma_fence_put(man->move); 1574 1.21 riastrad man->move = NULL; 1575 1.2 riastrad 1576 1.1 riastrad return ret; 1577 1.1 riastrad } 1578 1.1 riastrad EXPORT_SYMBOL(ttm_bo_clean_mm); 1579 1.1 riastrad 1580 1.1 riastrad int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type) 1581 1.1 riastrad { 1582 1.1 riastrad struct ttm_mem_type_manager *man = &bdev->man[mem_type]; 1583 1.1 riastrad 1584 1.1 riastrad if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) { 1585 1.1 riastrad pr_err("Illegal memory manager memory type %u\n", mem_type); 1586 1.1 riastrad return -EINVAL; 1587 1.1 riastrad } 1588 1.1 riastrad 1589 1.1 riastrad if (!man->has_type) { 1590 1.1 riastrad pr_err("Memory type %u has not been initialized\n", mem_type); 1591 1.1 riastrad return 0; 1592 1.1 riastrad } 1593 1.1 riastrad 1594 1.21 riastrad return ttm_bo_force_list_clean(bdev, mem_type); 1595 1.1 riastrad } 1596 1.1 riastrad EXPORT_SYMBOL(ttm_bo_evict_mm); 1597 1.1 riastrad 1598 1.1 riastrad int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, 1599 1.1 riastrad unsigned long p_size) 1600 1.1 riastrad { 1601 1.21 riastrad int ret; 1602 1.1 riastrad struct ttm_mem_type_manager *man; 1603 1.21 riastrad unsigned i; 1604 1.1 riastrad 1605 1.1 riastrad BUG_ON(type >= TTM_NUM_MEM_TYPES); 1606 1.1 riastrad man = &bdev->man[type]; 1607 1.1 riastrad BUG_ON(man->has_type); 1608 1.1 riastrad man->io_reserve_fastpath = true; 1609 1.1 riastrad man->use_io_reserve_lru = false; 1610 1.1 riastrad mutex_init(&man->io_reserve_mutex); 1611 1.21 riastrad spin_lock_init(&man->move_lock); 1612 1.1 riastrad INIT_LIST_HEAD(&man->io_reserve_lru); 1613 1.1 riastrad 1614 1.1 riastrad ret = bdev->driver->init_mem_type(bdev, type, man); 1615 1.1 riastrad if (ret) 1616 1.1 riastrad return ret; 1617 1.1 riastrad man->bdev = bdev; 1618 1.1 riastrad 1619 1.1 riastrad if (type != TTM_PL_SYSTEM) { 1620 1.1 riastrad ret = (*man->func->init)(man, p_size); 1621 1.1 riastrad if (ret) 1622 1.1 riastrad return ret; 1623 1.1 riastrad } 1624 1.1 riastrad man->has_type = true; 1625 1.1 riastrad man->use_type = true; 1626 1.1 riastrad man->size = p_size; 1627 1.1 riastrad 1628 1.21 riastrad for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 1629 1.21 riastrad INIT_LIST_HEAD(&man->lru[i]); 1630 1.21 riastrad man->move = NULL; 1631 1.1 riastrad 1632 1.1 riastrad return 0; 1633 1.1 riastrad } 1634 1.1 riastrad EXPORT_SYMBOL(ttm_bo_init_mm); 1635 1.1 riastrad 1636 1.2 riastrad #ifndef __NetBSD__ 1637 1.1 riastrad static void ttm_bo_global_kobj_release(struct kobject *kobj) 1638 1.1 riastrad { 1639 1.1 riastrad struct ttm_bo_global *glob = 1640 1.1 riastrad container_of(kobj, struct ttm_bo_global, kobj); 1641 1.1 riastrad 1642 1.1 riastrad __free_page(glob->dummy_read_page); 1643 1.1 riastrad } 1644 1.2 riastrad #endif 1645 1.1 riastrad 1646 1.21 riastrad static void ttm_bo_global_release(void) 1647 1.1 riastrad { 1648 1.21 riastrad struct ttm_bo_global *glob = &ttm_bo_glob; 1649 1.21 riastrad 1650 1.21 riastrad mutex_lock(&ttm_global_mutex); 1651 1.21 riastrad if (--ttm_bo_glob_use_count > 0) 1652 1.21 riastrad goto out; 1653 1.1 riastrad 1654 1.25 riastrad #ifndef __NetBSD__ 1655 1.25 riastrad kobject_del(&glob->kobj); 1656 1.25 riastrad kobject_put(&glob->kobj); 1657 1.25 riastrad #endif 1658 1.25 riastrad ttm_mem_global_release(&ttm_mem_glob); 1659 1.25 riastrad memset(glob, 0, sizeof(*glob)); 1660 1.2 riastrad #ifdef __NetBSD__ 1661 1.2 riastrad BUG_ON(glob->dummy_read_page != NULL); 1662 1.2 riastrad spin_lock_destroy(&glob->lru_lock); 1663 1.25 riastrad mutex_unlock(&ttm_global_mutex); 1664 1.22 riastrad mutex_destroy(&ttm_global_mutex); 1665 1.25 riastrad return; 1666 1.2 riastrad #endif 1667 1.21 riastrad out: 1668 1.21 riastrad mutex_unlock(&ttm_global_mutex); 1669 1.1 riastrad } 1670 1.1 riastrad 1671 1.21 riastrad static int ttm_bo_global_init(void) 1672 1.1 riastrad { 1673 1.21 riastrad struct ttm_bo_global *glob = &ttm_bo_glob; 1674 1.21 riastrad int ret = 0; 1675 1.21 riastrad unsigned i; 1676 1.21 riastrad 1677 1.22 riastrad mutex_init(&ttm_global_mutex); 1678 1.21 riastrad mutex_lock(&ttm_global_mutex); 1679 1.21 riastrad if (++ttm_bo_glob_use_count > 1) 1680 1.21 riastrad goto out; 1681 1.21 riastrad 1682 1.21 riastrad ret = ttm_mem_global_init(&ttm_mem_glob); 1683 1.21 riastrad if (ret) 1684 1.21 riastrad goto out; 1685 1.1 riastrad 1686 1.1 riastrad spin_lock_init(&glob->lru_lock); 1687 1.2 riastrad #ifdef __NetBSD__ 1688 1.2 riastrad /* Only used by agp back end, will fix there. */ 1689 1.2 riastrad /* XXX Fix agp back end to DTRT. */ 1690 1.2 riastrad glob->dummy_read_page = NULL; 1691 1.2 riastrad #else 1692 1.1 riastrad glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); 1693 1.1 riastrad 1694 1.1 riastrad if (unlikely(glob->dummy_read_page == NULL)) { 1695 1.1 riastrad ret = -ENOMEM; 1696 1.21 riastrad goto out; 1697 1.1 riastrad } 1698 1.2 riastrad #endif 1699 1.1 riastrad 1700 1.21 riastrad for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 1701 1.21 riastrad INIT_LIST_HEAD(&glob->swap_lru[i]); 1702 1.1 riastrad INIT_LIST_HEAD(&glob->device_list); 1703 1.1 riastrad atomic_set(&glob->bo_count, 0); 1704 1.1 riastrad 1705 1.2 riastrad #ifdef __NetBSD__ 1706 1.2 riastrad ret = 0; 1707 1.2 riastrad #else 1708 1.1 riastrad ret = kobject_init_and_add( 1709 1.1 riastrad &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects"); 1710 1.1 riastrad if (unlikely(ret != 0)) 1711 1.1 riastrad kobject_put(&glob->kobj); 1712 1.2 riastrad #endif 1713 1.21 riastrad out: 1714 1.21 riastrad mutex_unlock(&ttm_global_mutex); 1715 1.1 riastrad return ret; 1716 1.1 riastrad } 1717 1.1 riastrad 1718 1.1 riastrad int ttm_bo_device_release(struct ttm_bo_device *bdev) 1719 1.1 riastrad { 1720 1.21 riastrad struct ttm_bo_global *glob = &ttm_bo_glob; 1721 1.1 riastrad int ret = 0; 1722 1.1 riastrad unsigned i = TTM_NUM_MEM_TYPES; 1723 1.1 riastrad struct ttm_mem_type_manager *man; 1724 1.1 riastrad 1725 1.1 riastrad while (i--) { 1726 1.1 riastrad man = &bdev->man[i]; 1727 1.1 riastrad if (man->has_type) { 1728 1.1 riastrad man->use_type = false; 1729 1.1 riastrad if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) { 1730 1.1 riastrad ret = -EBUSY; 1731 1.1 riastrad pr_err("DRM memory manager type %d is not clean\n", 1732 1.1 riastrad i); 1733 1.1 riastrad } 1734 1.1 riastrad man->has_type = false; 1735 1.1 riastrad } 1736 1.1 riastrad } 1737 1.1 riastrad 1738 1.21 riastrad mutex_lock(&ttm_global_mutex); 1739 1.1 riastrad list_del(&bdev->device_list); 1740 1.21 riastrad mutex_unlock(&ttm_global_mutex); 1741 1.1 riastrad 1742 1.1 riastrad cancel_delayed_work_sync(&bdev->wq); 1743 1.1 riastrad 1744 1.21 riastrad if (ttm_bo_delayed_delete(bdev, true)) 1745 1.21 riastrad pr_debug("Delayed destroy list was clean\n"); 1746 1.1 riastrad 1747 1.1 riastrad spin_lock(&glob->lru_lock); 1748 1.21 riastrad for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 1749 1.21 riastrad if (list_empty(&bdev->man[0].lru[0])) 1750 1.21 riastrad pr_debug("Swap list %d was clean\n", i); 1751 1.1 riastrad spin_unlock(&glob->lru_lock); 1752 1.1 riastrad 1753 1.21 riastrad if (!ret) 1754 1.21 riastrad ttm_bo_global_release(); 1755 1.1 riastrad 1756 1.1 riastrad return ret; 1757 1.1 riastrad } 1758 1.1 riastrad EXPORT_SYMBOL(ttm_bo_device_release); 1759 1.1 riastrad 1760 1.1 riastrad int ttm_bo_device_init(struct ttm_bo_device *bdev, 1761 1.1 riastrad struct ttm_bo_driver *driver, 1762 1.2 riastrad #ifdef __NetBSD__ 1763 1.2 riastrad bus_space_tag_t memt, 1764 1.2 riastrad bus_dma_tag_t dmat, 1765 1.2 riastrad #else 1766 1.2 riastrad struct address_space *mapping, 1767 1.2 riastrad #endif 1768 1.21 riastrad struct drm_vma_offset_manager *vma_manager, 1769 1.1 riastrad bool need_dma32) 1770 1.1 riastrad { 1771 1.21 riastrad struct ttm_bo_global *glob = &ttm_bo_glob; 1772 1.21 riastrad int ret; 1773 1.21 riastrad 1774 1.21 riastrad if (WARN_ON(vma_manager == NULL)) 1775 1.21 riastrad return -EINVAL; 1776 1.21 riastrad 1777 1.21 riastrad ret = ttm_bo_global_init(); 1778 1.21 riastrad if (ret) 1779 1.21 riastrad return ret; 1780 1.1 riastrad 1781 1.1 riastrad bdev->driver = driver; 1782 1.1 riastrad 1783 1.1 riastrad memset(bdev->man, 0, sizeof(bdev->man)); 1784 1.1 riastrad 1785 1.1 riastrad /* 1786 1.1 riastrad * Initialize the system memory buffer type. 1787 1.1 riastrad * Other types need to be driver / IOCTL initialized. 1788 1.1 riastrad */ 1789 1.1 riastrad ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0); 1790 1.1 riastrad if (unlikely(ret != 0)) 1791 1.1 riastrad goto out_no_sys; 1792 1.1 riastrad 1793 1.21 riastrad bdev->vma_manager = vma_manager; 1794 1.1 riastrad INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); 1795 1.1 riastrad INIT_LIST_HEAD(&bdev->ddestroy); 1796 1.2 riastrad #ifdef __NetBSD__ 1797 1.2 riastrad bdev->memt = memt; 1798 1.2 riastrad bdev->dmat = dmat; 1799 1.2 riastrad #else 1800 1.2 riastrad bdev->dev_mapping = mapping; 1801 1.2 riastrad #endif 1802 1.1 riastrad bdev->need_dma32 = need_dma32; 1803 1.21 riastrad mutex_lock(&ttm_global_mutex); 1804 1.1 riastrad list_add_tail(&bdev->device_list, &glob->device_list); 1805 1.21 riastrad mutex_unlock(&ttm_global_mutex); 1806 1.1 riastrad 1807 1.1 riastrad return 0; 1808 1.1 riastrad out_no_sys: 1809 1.21 riastrad ttm_bo_global_release(); 1810 1.1 riastrad return ret; 1811 1.1 riastrad } 1812 1.1 riastrad EXPORT_SYMBOL(ttm_bo_device_init); 1813 1.1 riastrad 1814 1.1 riastrad /* 1815 1.1 riastrad * buffer object vm functions. 1816 1.1 riastrad */ 1817 1.1 riastrad 1818 1.1 riastrad bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) 1819 1.1 riastrad { 1820 1.1 riastrad struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; 1821 1.1 riastrad 1822 1.1 riastrad if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) { 1823 1.1 riastrad if (mem->mem_type == TTM_PL_SYSTEM) 1824 1.1 riastrad return false; 1825 1.1 riastrad 1826 1.1 riastrad if (man->flags & TTM_MEMTYPE_FLAG_CMA) 1827 1.1 riastrad return false; 1828 1.1 riastrad 1829 1.1 riastrad if (mem->placement & TTM_PL_FLAG_CACHED) 1830 1.1 riastrad return false; 1831 1.1 riastrad } 1832 1.1 riastrad return true; 1833 1.1 riastrad } 1834 1.1 riastrad 1835 1.1 riastrad void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo) 1836 1.1 riastrad { 1837 1.2 riastrad #ifdef __NetBSD__ 1838 1.2 riastrad if (bo->mem.bus.is_iomem) { 1839 1.7 riastrad paddr_t start, end, pa; 1840 1.7 riastrad 1841 1.9 riastrad KASSERTMSG((bo->mem.bus.base & (PAGE_SIZE - 1)) == 0, 1842 1.30 hannken "bo bus base addr not page-aligned: %" PRIx64 "", 1843 1.30 hannken (uint64_t)bo->mem.bus.base); 1844 1.9 riastrad KASSERTMSG((bo->mem.bus.offset & (PAGE_SIZE - 1)) == 0, 1845 1.9 riastrad "bo bus offset not page-aligned: %lx", 1846 1.9 riastrad bo->mem.bus.offset); 1847 1.7 riastrad start = bo->mem.bus.base + bo->mem.bus.offset; 1848 1.7 riastrad KASSERT((bo->mem.bus.size & (PAGE_SIZE - 1)) == 0); 1849 1.7 riastrad end = start + bo->mem.bus.size; 1850 1.7 riastrad 1851 1.7 riastrad for (pa = start; pa < end; pa += PAGE_SIZE) 1852 1.7 riastrad pmap_pv_protect(pa, VM_PROT_NONE); 1853 1.2 riastrad } else if (bo->ttm != NULL) { 1854 1.2 riastrad unsigned i; 1855 1.2 riastrad 1856 1.20 ad rw_enter(bo->uvmobj.vmobjlock, RW_WRITER); 1857 1.2 riastrad for (i = 0; i < bo->ttm->num_pages; i++) 1858 1.2 riastrad pmap_page_protect(&bo->ttm->pages[i]->p_vmp, 1859 1.2 riastrad VM_PROT_NONE); 1860 1.20 ad rw_exit(bo->uvmobj.vmobjlock); 1861 1.2 riastrad } 1862 1.2 riastrad #else 1863 1.15 mrg struct ttm_bo_device *bdev = bo->bdev; 1864 1.15 mrg 1865 1.21 riastrad drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping); 1866 1.2 riastrad #endif 1867 1.1 riastrad ttm_mem_io_free_vm(bo); 1868 1.1 riastrad } 1869 1.1 riastrad 1870 1.1 riastrad void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) 1871 1.1 riastrad { 1872 1.1 riastrad struct ttm_bo_device *bdev = bo->bdev; 1873 1.1 riastrad struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; 1874 1.1 riastrad 1875 1.1 riastrad ttm_mem_io_lock(man, false); 1876 1.1 riastrad ttm_bo_unmap_virtual_locked(bo); 1877 1.1 riastrad ttm_mem_io_unlock(man); 1878 1.1 riastrad } 1879 1.1 riastrad 1880 1.1 riastrad 1881 1.1 riastrad EXPORT_SYMBOL(ttm_bo_unmap_virtual); 1882 1.1 riastrad 1883 1.1 riastrad int ttm_bo_wait(struct ttm_buffer_object *bo, 1884 1.21 riastrad bool interruptible, bool no_wait) 1885 1.1 riastrad { 1886 1.11 riastrad long timeout = 15 * HZ; 1887 1.1 riastrad 1888 1.21 riastrad if (no_wait) { 1889 1.21 riastrad if (dma_resv_test_signaled_rcu(bo->base.resv, true)) 1890 1.21 riastrad return 0; 1891 1.21 riastrad else 1892 1.21 riastrad return -EBUSY; 1893 1.1 riastrad } 1894 1.11 riastrad 1895 1.21 riastrad timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true, 1896 1.21 riastrad interruptible, timeout); 1897 1.11 riastrad if (timeout < 0) 1898 1.11 riastrad return timeout; 1899 1.11 riastrad 1900 1.11 riastrad if (timeout == 0) 1901 1.11 riastrad return -EBUSY; 1902 1.11 riastrad 1903 1.21 riastrad dma_resv_add_excl_fence(bo->base.resv, NULL); 1904 1.1 riastrad return 0; 1905 1.1 riastrad } 1906 1.1 riastrad EXPORT_SYMBOL(ttm_bo_wait); 1907 1.1 riastrad 1908 1.1 riastrad /** 1909 1.1 riastrad * A buffer object shrink method that tries to swap out the first 1910 1.1 riastrad * buffer object on the bo_global::swap_lru list. 1911 1.1 riastrad */ 1912 1.21 riastrad int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx) 1913 1.1 riastrad { 1914 1.1 riastrad struct ttm_buffer_object *bo; 1915 1.1 riastrad int ret = -EBUSY; 1916 1.21 riastrad bool locked; 1917 1.21 riastrad unsigned i; 1918 1.1 riastrad 1919 1.1 riastrad spin_lock(&glob->lru_lock); 1920 1.21 riastrad for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 1921 1.21 riastrad list_for_each_entry(bo, &glob->swap_lru[i], swap) { 1922 1.21 riastrad if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked, 1923 1.21 riastrad NULL)) { 1924 1.21 riastrad ret = 0; 1925 1.21 riastrad break; 1926 1.21 riastrad } 1927 1.21 riastrad } 1928 1.1 riastrad if (!ret) 1929 1.1 riastrad break; 1930 1.1 riastrad } 1931 1.1 riastrad 1932 1.1 riastrad if (ret) { 1933 1.1 riastrad spin_unlock(&glob->lru_lock); 1934 1.1 riastrad return ret; 1935 1.1 riastrad } 1936 1.1 riastrad 1937 1.1 riastrad kref_get(&bo->list_kref); 1938 1.1 riastrad 1939 1.1 riastrad if (!list_empty(&bo->ddestroy)) { 1940 1.21 riastrad ret = ttm_bo_cleanup_refs(bo, false, false, locked); 1941 1.1 riastrad kref_put(&bo->list_kref, ttm_bo_release_list); 1942 1.1 riastrad return ret; 1943 1.1 riastrad } 1944 1.1 riastrad 1945 1.21 riastrad ttm_bo_del_from_lru(bo); 1946 1.1 riastrad spin_unlock(&glob->lru_lock); 1947 1.1 riastrad 1948 1.1 riastrad /** 1949 1.21 riastrad * Move to system cached 1950 1.1 riastrad */ 1951 1.1 riastrad 1952 1.11 riastrad if (bo->mem.mem_type != TTM_PL_SYSTEM || 1953 1.11 riastrad bo->ttm->caching_state != tt_cached) { 1954 1.21 riastrad struct ttm_operation_ctx ctx = { false, false }; 1955 1.1 riastrad struct ttm_mem_reg evict_mem; 1956 1.1 riastrad 1957 1.1 riastrad evict_mem = bo->mem; 1958 1.1 riastrad evict_mem.mm_node = NULL; 1959 1.1 riastrad evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; 1960 1.1 riastrad evict_mem.mem_type = TTM_PL_SYSTEM; 1961 1.1 riastrad 1962 1.21 riastrad ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx); 1963 1.1 riastrad if (unlikely(ret != 0)) 1964 1.1 riastrad goto out; 1965 1.1 riastrad } 1966 1.1 riastrad 1967 1.21 riastrad /** 1968 1.21 riastrad * Make sure BO is idle. 1969 1.21 riastrad */ 1970 1.21 riastrad 1971 1.21 riastrad ret = ttm_bo_wait(bo, false, false); 1972 1.21 riastrad if (unlikely(ret != 0)) 1973 1.21 riastrad goto out; 1974 1.21 riastrad 1975 1.1 riastrad ttm_bo_unmap_virtual(bo); 1976 1.1 riastrad 1977 1.1 riastrad /** 1978 1.1 riastrad * Swap out. Buffer will be swapped in again as soon as 1979 1.1 riastrad * anyone tries to access a ttm page. 1980 1.1 riastrad */ 1981 1.1 riastrad 1982 1.1 riastrad if (bo->bdev->driver->swap_notify) 1983 1.1 riastrad bo->bdev->driver->swap_notify(bo); 1984 1.1 riastrad 1985 1.1 riastrad ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage); 1986 1.1 riastrad out: 1987 1.1 riastrad 1988 1.1 riastrad /** 1989 1.1 riastrad * 1990 1.1 riastrad * Unreserve without putting on LRU to avoid swapping out an 1991 1.1 riastrad * already swapped buffer. 1992 1.1 riastrad */ 1993 1.21 riastrad if (locked) 1994 1.21 riastrad dma_resv_unlock(bo->base.resv); 1995 1.1 riastrad kref_put(&bo->list_kref, ttm_bo_release_list); 1996 1.1 riastrad return ret; 1997 1.1 riastrad } 1998 1.21 riastrad EXPORT_SYMBOL(ttm_bo_swapout); 1999 1.1 riastrad 2000 1.1 riastrad void ttm_bo_swapout_all(struct ttm_bo_device *bdev) 2001 1.1 riastrad { 2002 1.21 riastrad struct ttm_operation_ctx ctx = { 2003 1.21 riastrad .interruptible = false, 2004 1.21 riastrad .no_wait_gpu = false 2005 1.21 riastrad }; 2006 1.21 riastrad 2007 1.21 riastrad while (ttm_bo_swapout(&ttm_bo_glob, &ctx) == 0); 2008 1.1 riastrad } 2009 1.1 riastrad EXPORT_SYMBOL(ttm_bo_swapout_all); 2010