at commit 448e76cb
| 1 | // SPDX-License-Identifier: MIT |
| 2 | /* |
| 3 | * Copyright © 2021 Intel Corporation |
| 4 | */ |
| 5 | |
| 6 | #include "xe_bo.h" |
| 7 | |
| 8 | #include <linux/dma-buf.h> |
| 9 | #include <linux/nospec.h> |
| 10 | |
| 11 | #include <drm/drm_drv.h> |
| 12 | #include <drm/drm_dumb_buffers.h> |
| 13 | #include <drm/drm_gem_ttm_helper.h> |
| 14 | #include <drm/drm_managed.h> |
| 15 | #include <drm/ttm/ttm_backup.h> |
| 16 | #include <drm/ttm/ttm_device.h> |
| 17 | #include <drm/ttm/ttm_placement.h> |
| 18 | #include <drm/ttm/ttm_tt.h> |
| 19 | #include <uapi/drm/xe_drm.h> |
| 20 | |
| 21 | #include <kunit/static_stub.h> |
| 22 | |
| 23 | #include <trace/events/gpu_mem.h> |
| 24 | |
| 25 | #include "xe_device.h" |
| 26 | #include "xe_dma_buf.h" |
| 27 | #include "xe_drm_client.h" |
| 28 | #include "xe_ggtt.h" |
| 29 | #include "xe_map.h" |
| 30 | #include "xe_migrate.h" |
| 31 | #include "xe_pat.h" |
| 32 | #include "xe_pm.h" |
| 33 | #include "xe_preempt_fence.h" |
| 34 | #include "xe_pxp.h" |
| 35 | #include "xe_res_cursor.h" |
| 36 | #include "xe_shrinker.h" |
| 37 | #include "xe_sriov_vf_ccs.h" |
| 38 | #include "xe_tile.h" |
| 39 | #include "xe_trace_bo.h" |
| 40 | #include "xe_ttm_stolen_mgr.h" |
| 41 | #include "xe_vm.h" |
| 42 | #include "xe_vram_types.h" |
| 43 | |
| 44 | const char *const xe_mem_type_to_name[TTM_NUM_MEM_TYPES] = { |
| 45 | [XE_PL_SYSTEM] = "system", |
| 46 | [XE_PL_TT] = "gtt", |
| 47 | [XE_PL_VRAM0] = "vram0", |
| 48 | [XE_PL_VRAM1] = "vram1", |
| 49 | [XE_PL_STOLEN] = "stolen" |
| 50 | }; |
| 51 | |
| 52 | static const struct ttm_place sys_placement_flags = { |
| 53 | .fpfn = 0, |
| 54 | .lpfn = 0, |
| 55 | .mem_type = XE_PL_SYSTEM, |
| 56 | .flags = 0, |
| 57 | }; |
| 58 | |
| 59 | static struct ttm_placement sys_placement = { |
| 60 | .num_placement = 1, |
| 61 | .placement = &sys_placement_flags, |
| 62 | }; |
| 63 | |
| 64 | static struct ttm_placement purge_placement; |
| 65 | |
| 66 | static const struct ttm_place tt_placement_flags[] = { |
| 67 | { |
| 68 | .fpfn = 0, |
| 69 | .lpfn = 0, |
| 70 | .mem_type = XE_PL_TT, |
| 71 | .flags = TTM_PL_FLAG_DESIRED, |
| 72 | }, |
| 73 | { |
| 74 | .fpfn = 0, |
| 75 | .lpfn = 0, |
| 76 | .mem_type = XE_PL_SYSTEM, |
| 77 | .flags = TTM_PL_FLAG_FALLBACK, |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | static struct ttm_placement tt_placement = { |
| 82 | .num_placement = 2, |
| 83 | .placement = tt_placement_flags, |
| 84 | }; |
| 85 | |
| 86 | #define for_each_set_bo_vram_flag(bit__, bo_flags__) \ |
| 87 | for (unsigned int __bit_tmp = BIT(0); __bit_tmp <= XE_BO_FLAG_VRAM_MASK; __bit_tmp <<= 1) \ |
| 88 | for_each_if(((bit__) = __bit_tmp) & (bo_flags__) & XE_BO_FLAG_VRAM_MASK) |
| 89 | |
| 90 | bool mem_type_is_vram(u32 mem_type) |
| 91 | { |
| 92 | return mem_type >= XE_PL_VRAM0 && mem_type != XE_PL_STOLEN; |
| 93 | } |
| 94 | |
| 95 | static bool resource_is_stolen_vram(struct xe_device *xe, struct ttm_resource *res) |
| 96 | { |
| 97 | return res->mem_type == XE_PL_STOLEN && IS_DGFX(xe); |
| 98 | } |
| 99 | |
| 100 | static bool resource_is_vram(struct ttm_resource *res) |
| 101 | { |
| 102 | return mem_type_is_vram(res->mem_type); |
| 103 | } |
| 104 | |
| 105 | bool xe_bo_is_vram(struct xe_bo *bo) |
| 106 | { |
| 107 | return resource_is_vram(bo->ttm.resource) || |
| 108 | resource_is_stolen_vram(xe_bo_device(bo), bo->ttm.resource); |
| 109 | } |
| 110 | |
| 111 | bool xe_bo_is_stolen(struct xe_bo *bo) |
| 112 | { |
| 113 | return bo->ttm.resource->mem_type == XE_PL_STOLEN; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * xe_bo_has_single_placement - check if BO is placed only in one memory location |
| 118 | * @bo: The BO |
| 119 | * |
| 120 | * This function checks whether a given BO is placed in only one memory location. |
| 121 | * |
| 122 | * Returns: true if the BO is placed in a single memory location, false otherwise. |
| 123 | * |
| 124 | */ |
| 125 | bool xe_bo_has_single_placement(struct xe_bo *bo) |
| 126 | { |
| 127 | return bo->placement.num_placement == 1; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * xe_bo_is_stolen_devmem - check if BO is of stolen type accessed via PCI BAR |
| 132 | * @bo: The BO |
| 133 | * |
| 134 | * The stolen memory is accessed through the PCI BAR for both DGFX and some |
| 135 | * integrated platforms that have a dedicated bit in the PTE for devmem (DM). |
| 136 | * |
| 137 | * Returns: true if it's stolen memory accessed via PCI BAR, false otherwise. |
| 138 | */ |
| 139 | bool xe_bo_is_stolen_devmem(struct xe_bo *bo) |
| 140 | { |
| 141 | return xe_bo_is_stolen(bo) && |
| 142 | GRAPHICS_VERx100(xe_bo_device(bo)) >= 1270; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * xe_bo_is_vm_bound - check if BO has any mappings through VM_BIND |
| 147 | * @bo: The BO |
| 148 | * |
| 149 | * Check if a given bo is bound through VM_BIND. This requires the |
| 150 | * reservation lock for the BO to be held. |
| 151 | * |
| 152 | * Returns: boolean |
| 153 | */ |
| 154 | bool xe_bo_is_vm_bound(struct xe_bo *bo) |
| 155 | { |
| 156 | xe_bo_assert_held(bo); |
| 157 | |
| 158 | return !list_empty(&bo->ttm.base.gpuva.list); |
| 159 | } |
| 160 | |
| 161 | static bool xe_bo_is_user(struct xe_bo *bo) |
| 162 | { |
| 163 | return bo->flags & XE_BO_FLAG_USER; |
| 164 | } |
| 165 | |
| 166 | static struct xe_migrate * |
| 167 | mem_type_to_migrate(struct xe_device *xe, u32 mem_type) |
| 168 | { |
| 169 | struct xe_tile *tile; |
| 170 | |
| 171 | xe_assert(xe, mem_type == XE_PL_STOLEN || mem_type_is_vram(mem_type)); |
| 172 | tile = &xe->tiles[mem_type == XE_PL_STOLEN ? 0 : (mem_type - XE_PL_VRAM0)]; |
| 173 | return tile->migrate; |
| 174 | } |
| 175 | |
| 176 | static void try_add_system(struct xe_device *xe, struct xe_bo *bo, |
| 177 | u32 bo_flags, u32 *c) |
| 178 | { |
| 179 | if (bo_flags & XE_BO_FLAG_SYSTEM) { |
| 180 | xe_assert(xe, *c < ARRAY_SIZE(bo->placements)); |
| 181 | |
| 182 | bo->placements[*c] = (struct ttm_place) { |
| 183 | .mem_type = XE_PL_TT, |
| 184 | .flags = (bo_flags & XE_BO_FLAG_VRAM_MASK) ? |
| 185 | TTM_PL_FLAG_FALLBACK : 0, |
| 186 | }; |
| 187 | *c += 1; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static bool force_contiguous(u32 bo_flags) |
| 192 | { |
| 193 | if (bo_flags & XE_BO_FLAG_STOLEN) |
| 194 | return true; /* users expect this */ |
| 195 | else if (bo_flags & XE_BO_FLAG_PINNED && |
| 196 | !(bo_flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) |
| 197 | return true; /* needs vmap */ |
| 198 | else if (bo_flags & XE_BO_FLAG_CPU_ADDR_MIRROR) |
| 199 | return true; |
| 200 | |
| 201 | /* |
| 202 | * For eviction / restore on suspend / resume objects pinned in VRAM |
| 203 | * must be contiguous, also only contiguous BOs support xe_bo_vmap. |
| 204 | */ |
| 205 | return bo_flags & XE_BO_FLAG_NEEDS_CPU_ACCESS && |
| 206 | bo_flags & XE_BO_FLAG_PINNED; |
| 207 | } |
| 208 | |
| 209 | static u8 vram_bo_flag_to_tile_id(struct xe_device *xe, u32 vram_bo_flag) |
| 210 | { |
| 211 | xe_assert(xe, vram_bo_flag & XE_BO_FLAG_VRAM_MASK); |
| 212 | xe_assert(xe, (vram_bo_flag & (vram_bo_flag - 1)) == 0); |
| 213 | |
| 214 | return __ffs(vram_bo_flag >> (__ffs(XE_BO_FLAG_VRAM0) - 1)) - 1; |
| 215 | } |
| 216 | |
| 217 | static u32 bo_vram_flags_to_vram_placement(struct xe_device *xe, u32 bo_flags, u32 vram_flag, |
| 218 | enum ttm_bo_type type) |
| 219 | { |
| 220 | u8 tile_id = vram_bo_flag_to_tile_id(xe, vram_flag); |
| 221 | |
| 222 | xe_assert(xe, tile_id < xe->info.tile_count); |
| 223 | |
| 224 | if (type == ttm_bo_type_kernel && !(bo_flags & XE_BO_FLAG_FORCE_USER_VRAM)) |
| 225 | return xe->tiles[tile_id].mem.kernel_vram->placement; |
| 226 | else |
| 227 | return xe->tiles[tile_id].mem.vram->placement; |
| 228 | } |
| 229 | |
| 230 | static void add_vram(struct xe_device *xe, struct xe_bo *bo, |
| 231 | struct ttm_place *places, u32 bo_flags, u32 mem_type, u32 *c) |
| 232 | { |
| 233 | struct ttm_place place = { .mem_type = mem_type }; |
| 234 | struct ttm_resource_manager *mgr = ttm_manager_type(&xe->ttm, mem_type); |
| 235 | struct xe_ttm_vram_mgr *vram_mgr = to_xe_ttm_vram_mgr(mgr); |
| 236 | |
| 237 | struct xe_vram_region *vram; |
| 238 | u64 io_size; |
| 239 | |
| 240 | xe_assert(xe, *c < ARRAY_SIZE(bo->placements)); |
| 241 | |
| 242 | vram = container_of(vram_mgr, struct xe_vram_region, ttm); |
| 243 | xe_assert(xe, vram && vram->usable_size); |
| 244 | io_size = vram->io_size; |
| 245 | |
| 246 | if (force_contiguous(bo_flags)) |
| 247 | place.flags |= TTM_PL_FLAG_CONTIGUOUS; |
| 248 | |
| 249 | if (io_size < vram->usable_size) { |
| 250 | if (bo_flags & XE_BO_FLAG_NEEDS_CPU_ACCESS) { |
| 251 | place.fpfn = 0; |
| 252 | place.lpfn = io_size >> PAGE_SHIFT; |
| 253 | } else { |
| 254 | place.flags |= TTM_PL_FLAG_TOPDOWN; |
| 255 | } |
| 256 | } |
| 257 | places[*c] = place; |
| 258 | *c += 1; |
| 259 | } |
| 260 | |
| 261 | static void try_add_vram(struct xe_device *xe, struct xe_bo *bo, |
| 262 | u32 bo_flags, enum ttm_bo_type type, u32 *c) |
| 263 | { |
| 264 | u32 vram_flag; |
| 265 | |
| 266 | for_each_set_bo_vram_flag(vram_flag, bo_flags) { |
| 267 | u32 pl = bo_vram_flags_to_vram_placement(xe, bo_flags, vram_flag, type); |
| 268 | |
| 269 | add_vram(xe, bo, bo->placements, bo_flags, pl, c); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static void try_add_stolen(struct xe_device *xe, struct xe_bo *bo, |
| 274 | u32 bo_flags, u32 *c) |
| 275 | { |
| 276 | if (bo_flags & XE_BO_FLAG_STOLEN) { |
| 277 | xe_assert(xe, *c < ARRAY_SIZE(bo->placements)); |
| 278 | |
| 279 | bo->placements[*c] = (struct ttm_place) { |
| 280 | .mem_type = XE_PL_STOLEN, |
| 281 | .flags = force_contiguous(bo_flags) ? |
| 282 | TTM_PL_FLAG_CONTIGUOUS : 0, |
| 283 | }; |
| 284 | *c += 1; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static int __xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo, |
| 289 | u32 bo_flags, enum ttm_bo_type type) |
| 290 | { |
| 291 | u32 c = 0; |
| 292 | |
| 293 | try_add_vram(xe, bo, bo_flags, type, &c); |
| 294 | try_add_system(xe, bo, bo_flags, &c); |
| 295 | try_add_stolen(xe, bo, bo_flags, &c); |
| 296 | |
| 297 | if (!c) |
| 298 | return -EINVAL; |
| 299 | |
| 300 | bo->placement = (struct ttm_placement) { |
| 301 | .num_placement = c, |
| 302 | .placement = bo->placements, |
| 303 | }; |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | int xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo, |
| 309 | u32 bo_flags, enum ttm_bo_type type) |
| 310 | { |
| 311 | xe_bo_assert_held(bo); |
| 312 | return __xe_bo_placement_for_flags(xe, bo, bo_flags, type); |
| 313 | } |
| 314 | |
| 315 | static void xe_evict_flags(struct ttm_buffer_object *tbo, |
| 316 | struct ttm_placement *placement) |
| 317 | { |
| 318 | struct xe_device *xe = container_of(tbo->bdev, typeof(*xe), ttm); |
| 319 | bool device_unplugged = drm_dev_is_unplugged(&xe->drm); |
| 320 | struct xe_bo *bo; |
| 321 | |
| 322 | if (!xe_bo_is_xe_bo(tbo)) { |
| 323 | /* Don't handle scatter gather BOs */ |
| 324 | if (tbo->type == ttm_bo_type_sg) { |
| 325 | placement->num_placement = 0; |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | *placement = device_unplugged ? purge_placement : sys_placement; |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | /* No resource: nothing is placed/mapped, so there is nothing to evict. */ |
| 334 | if (!tbo->resource) { |
| 335 | placement->num_placement = 0; |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | bo = ttm_to_xe_bo(tbo); |
| 340 | if (bo->flags & XE_BO_FLAG_CPU_ADDR_MIRROR) { |
| 341 | *placement = sys_placement; |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | if (device_unplugged && !tbo->base.dma_buf) { |
| 346 | *placement = purge_placement; |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | if (xe_bo_madv_is_dontneed(bo)) { |
| 351 | /* |
| 352 | * We can't use purge_placement here, since we need to trigger |
| 353 | * our own purge procedure at the start of xe_bo_move(), which |
| 354 | * would otherwise be skipped. At the same time we don't want |
| 355 | * ttm to then populate the tt with dst pages, before the move |
| 356 | * callback, hence use sys_placement here. |
| 357 | */ |
| 358 | *placement = sys_placement; |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * For xe, sg bos that are evicted to system just triggers a |
| 364 | * rebind of the sg list upon subsequent validation to XE_PL_TT. |
| 365 | */ |
| 366 | switch (tbo->resource->mem_type) { |
| 367 | case XE_PL_VRAM0: |
| 368 | case XE_PL_VRAM1: |
| 369 | case XE_PL_STOLEN: |
| 370 | *placement = tt_placement; |
| 371 | break; |
| 372 | case XE_PL_TT: |
| 373 | default: |
| 374 | *placement = sys_placement; |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /* struct xe_ttm_tt - Subclassed ttm_tt for xe */ |
| 380 | struct xe_ttm_tt { |
| 381 | struct ttm_tt ttm; |
| 382 | struct sg_table sgt; |
| 383 | struct sg_table *sg; |
| 384 | /** @purgeable: Whether the content of the pages of @ttm is purgeable. */ |
| 385 | bool purgeable; |
| 386 | }; |
| 387 | |
| 388 | static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt) |
| 389 | { |
| 390 | struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); |
| 391 | unsigned long num_pages = tt->num_pages; |
| 392 | int ret; |
| 393 | |
| 394 | XE_WARN_ON((tt->page_flags & TTM_TT_FLAG_EXTERNAL) && |
| 395 | !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)); |
| 396 | |
| 397 | if (xe_tt->sg) |
| 398 | return 0; |
| 399 | |
| 400 | ret = sg_alloc_table_from_pages_segment(&xe_tt->sgt, tt->pages, |
| 401 | num_pages, 0, |
| 402 | (u64)num_pages << PAGE_SHIFT, |
| 403 | xe_sg_segment_size(xe->drm.dev), |
| 404 | GFP_KERNEL); |
| 405 | if (ret) |
| 406 | return ret; |
| 407 | |
| 408 | xe_tt->sg = &xe_tt->sgt; |
| 409 | ret = dma_map_sgtable(xe->drm.dev, xe_tt->sg, DMA_BIDIRECTIONAL, |
| 410 | DMA_ATTR_SKIP_CPU_SYNC); |
| 411 | if (ret) { |
| 412 | sg_free_table(xe_tt->sg); |
| 413 | xe_tt->sg = NULL; |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt) |
| 421 | { |
| 422 | struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); |
| 423 | |
| 424 | if (xe_tt->sg) { |
| 425 | dma_unmap_sgtable(xe->drm.dev, xe_tt->sg, |
| 426 | DMA_BIDIRECTIONAL, 0); |
| 427 | sg_free_table(xe_tt->sg); |
| 428 | xe_tt->sg = NULL; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | struct sg_table *xe_bo_sg(struct xe_bo *bo) |
| 433 | { |
| 434 | struct ttm_tt *tt = bo->ttm.ttm; |
| 435 | struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); |
| 436 | |
| 437 | return xe_tt->sg; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * Account ttm pages against the device shrinker's shrinkable and |
| 442 | * purgeable counts. |
| 443 | */ |
| 444 | static void xe_ttm_tt_account_add(struct xe_device *xe, struct ttm_tt *tt) |
| 445 | { |
| 446 | struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); |
| 447 | |
| 448 | if (xe_tt->purgeable) |
| 449 | xe_shrinker_mod_pages(xe->mem.shrinker, 0, tt->num_pages); |
| 450 | else |
| 451 | xe_shrinker_mod_pages(xe->mem.shrinker, tt->num_pages, 0); |
| 452 | } |
| 453 | |
| 454 | static void xe_ttm_tt_account_subtract(struct xe_device *xe, struct ttm_tt *tt) |
| 455 | { |
| 456 | struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); |
| 457 | |
| 458 | if (xe_tt->purgeable) |
| 459 | xe_shrinker_mod_pages(xe->mem.shrinker, 0, -(long)tt->num_pages); |
| 460 | else |
| 461 | xe_shrinker_mod_pages(xe->mem.shrinker, -(long)tt->num_pages, 0); |
| 462 | } |
| 463 | |
| 464 | static bool xe_ttm_bo_is_imported(struct ttm_buffer_object *tbo) |
| 465 | { |
| 466 | dma_resv_assert_held(tbo->base.resv); |
| 467 | |
| 468 | return tbo->ttm && |
| 469 | (tbo->ttm->page_flags & (TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_EXTERNAL_MAPPABLE)) == |
| 470 | TTM_TT_FLAG_EXTERNAL; |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * Account @bo's pages as pinned for the shrinker. Removes @bo's pages |
| 475 | * from the shrinker's shrinkable / purgeable counts on the transition |
| 476 | * from unpinned to pinned. Must be called with @bo's dma-resv held, |
| 477 | * after &ttm_buffer_object.pin_count has been incremented by |
| 478 | * ttm_bo_pin(). Safe to call unconditionally regardless of which pin |
| 479 | * path (kernel, external, framebuffer, backup object, ...) is pinning |
| 480 | * @bo, since it only acts on the true 0->1 pin_count transition. |
| 481 | * |
| 482 | * Imported bos (xe_ttm_bo_is_imported()) are excluded: for those, |
| 483 | * xe_ttm_tt_populate() never actually populates the tt or calls |
| 484 | * xe_ttm_tt_account_add(), but TTM core's ttm_tt_populate() still |
| 485 | * unconditionally marks the tt as populated on driver-hook success. |
| 486 | * Relying on ttm_tt_is_populated() for these would subtract pages that |
| 487 | * were never added, underflowing the shrinker's counts. |
| 488 | */ |
| 489 | static void xe_bo_account_pin(struct xe_bo *bo) |
| 490 | { |
| 491 | struct xe_device *xe = xe_bo_device(bo); |
| 492 | |
| 493 | if (bo->ttm.pin_count == 1 && bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm) && |
| 494 | !xe_ttm_bo_is_imported(&bo->ttm)) |
| 495 | xe_ttm_tt_account_subtract(xe, bo->ttm.ttm); |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Counterpart to xe_bo_account_pin(). Must be called with @bo's dma-resv |
| 500 | * held, before &ttm_buffer_object.pin_count is decremented by |
| 501 | * ttm_bo_unpin(), so that the check against the true 1->0 transition sees |
| 502 | * the pin count that is about to be released. See xe_bo_account_pin() for |
| 503 | * why imported bos are excluded. |
| 504 | * |
| 505 | * On the true last unpin, also removes @bo from whichever pinned-bo list |
| 506 | * (external or kernel_bo_present) it may currently be linked on, since a |
| 507 | * bo's final unpin can happen through a pin path (e.g. framebuffer, |
| 508 | * backup object) that has no notion of, or ownership over, that list. |
| 509 | * This is safe and list-agnostic: list_del_init() only needs the node |
| 510 | * itself, not knowledge of which list it is threaded through, and is a |
| 511 | * no-op if @bo is not linked. |
| 512 | */ |
| 513 | static void xe_bo_account_unpin(struct xe_bo *bo) |
| 514 | { |
| 515 | struct xe_device *xe = xe_bo_device(bo); |
| 516 | bool last_unpin = bo->ttm.pin_count == 1; |
| 517 | |
| 518 | if (last_unpin && !list_empty(&bo->pinned_link)) { |
| 519 | spin_lock(&xe->pinned.lock); |
| 520 | list_del_init(&bo->pinned_link); |
| 521 | spin_unlock(&xe->pinned.lock); |
| 522 | } |
| 523 | |
| 524 | if (last_unpin && bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm) && |
| 525 | !xe_ttm_bo_is_imported(&bo->ttm)) |
| 526 | xe_ttm_tt_account_add(xe, bo->ttm.ttm); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * xe_bo_pin_account() - Pin a bo and account its pages for the shrinker |
| 531 | * @bo: The buffer object to pin. |
| 532 | * |
| 533 | * Pins @bo via ttm_bo_pin() and updates the shrinker's shrinkable / |
| 534 | * purgeable page accounting to match. Must be called with @bo's dma-resv |
| 535 | * held. Safe to call regardless of which pin path (kernel, external, |
| 536 | * framebuffer, backup object, ...) is pinning @bo. |
| 537 | */ |
| 538 | void xe_bo_pin_account(struct xe_bo *bo) |
| 539 | { |
| 540 | ttm_bo_pin(&bo->ttm); |
| 541 | xe_bo_account_pin(bo); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * xe_bo_unpin_account() - Unpin a bo and account its pages for the shrinker |
| 546 | * @bo: The buffer object to unpin. |
| 547 | * |
| 548 | * Counterpart to xe_bo_pin_account(). Updates the shrinker's shrinkable / |
| 549 | * purgeable page accounting to match, then unpins @bo via ttm_bo_unpin(). |
| 550 | * Must be called with @bo's dma-resv held. |
| 551 | */ |
| 552 | void xe_bo_unpin_account(struct xe_bo *bo) |
| 553 | { |
| 554 | xe_bo_account_unpin(bo); |
| 555 | ttm_bo_unpin(&bo->ttm); |
| 556 | } |
| 557 | |
| 558 | static void update_global_total_pages(struct ttm_device *ttm_dev, |
| 559 | long num_pages) |
| 560 | { |
| 561 | #if IS_ENABLED(CONFIG_TRACE_GPU_MEM) |
| 562 | struct xe_device *xe = ttm_to_xe_device(ttm_dev); |
| 563 | u64 global_total_pages = |
| 564 | atomic64_add_return(num_pages, &xe->global_total_pages); |
| 565 | |
| 566 | trace_gpu_mem_total(xe->drm.primary->index, 0, |
| 567 | global_total_pages << PAGE_SHIFT); |
| 568 | #endif |
| 569 | } |
| 570 | |
| 571 | static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo, |
| 572 | u32 page_flags) |
| 573 | { |
| 574 | struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); |
| 575 | struct xe_device *xe = xe_bo_device(bo); |
| 576 | struct xe_ttm_tt *xe_tt; |
| 577 | struct ttm_tt *tt; |
| 578 | unsigned long extra_pages; |
| 579 | enum ttm_caching caching = ttm_cached; |
| 580 | int err; |
| 581 | |
| 582 | xe_tt = kzalloc_obj(*xe_tt); |
| 583 | if (!xe_tt) |
| 584 | return NULL; |
| 585 | |
| 586 | tt = &xe_tt->ttm; |
| 587 | |
| 588 | extra_pages = 0; |
| 589 | if (xe_bo_needs_ccs_pages(bo)) |
| 590 | extra_pages = DIV_ROUND_UP(xe_device_ccs_bytes(xe, xe_bo_size(bo)), |
| 591 | PAGE_SIZE); |
| 592 | |
| 593 | /* |
| 594 | * DGFX system memory is always WB / ttm_cached, since |
| 595 | * other caching modes are only supported on x86. DGFX |
| 596 | * GPU system memory accesses are always coherent with the |
| 597 | * CPU. |
| 598 | */ |
| 599 | if (!IS_DGFX(xe)) { |
| 600 | switch (bo->cpu_caching) { |
| 601 | case DRM_XE_GEM_CPU_CACHING_WC: |
| 602 | caching = ttm_write_combined; |
| 603 | break; |
| 604 | default: |
| 605 | caching = ttm_cached; |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | WARN_ON((bo->flags & XE_BO_FLAG_USER) && !bo->cpu_caching); |
| 610 | |
| 611 | /* |
| 612 | * For Xe_LPG and beyond up to NVL-P (excluding), PPGTT PTE |
| 613 | * lookups are also non-coherent and require a CPU:WC mapping. |
| 614 | */ |
| 615 | if ((!bo->cpu_caching && bo->flags & XE_BO_FLAG_FORCE_WC) || |
| 616 | (!xe->info.has_cached_pt && bo->flags & XE_BO_FLAG_PAGETABLE)) |
| 617 | caching = ttm_write_combined; |
| 618 | } |
| 619 | |
| 620 | if (bo->flags & XE_BO_FLAG_NEEDS_UC) { |
| 621 | /* |
| 622 | * Valid only for internally-created buffers only, for |
| 623 | * which cpu_caching is never initialized. |
| 624 | */ |
| 625 | xe_assert(xe, bo->cpu_caching == 0); |
| 626 | caching = ttm_uncached; |
| 627 | } |
| 628 | |
| 629 | if (ttm_bo->type != ttm_bo_type_sg) |
| 630 | page_flags |= TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_EXTERNAL_MAPPABLE; |
| 631 | |
| 632 | err = ttm_tt_init(tt, &bo->ttm, page_flags, caching, extra_pages); |
| 633 | if (err) { |
| 634 | kfree(xe_tt); |
| 635 | return NULL; |
| 636 | } |
| 637 | |
| 638 | if (ttm_bo->type != ttm_bo_type_sg) { |
| 639 | err = ttm_tt_setup_backup(tt); |
| 640 | if (err) { |
| 641 | ttm_tt_fini(tt); |
| 642 | kfree(xe_tt); |
| 643 | return NULL; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | return tt; |
| 648 | } |
| 649 | |
| 650 | static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt, |
| 651 | struct ttm_operation_ctx *ctx) |
| 652 | { |
| 653 | struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); |
| 654 | int err; |
| 655 | |
| 656 | /* |
| 657 | * dma-bufs are not populated with pages, and the dma- |
| 658 | * addresses are set up when moved to XE_PL_TT. |
| 659 | */ |
| 660 | if ((tt->page_flags & TTM_TT_FLAG_EXTERNAL) && |
| 661 | !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)) |
| 662 | return 0; |
| 663 | |
| 664 | if (ttm_tt_is_backed_up(tt) && !xe_tt->purgeable) { |
| 665 | err = ttm_tt_restore(ttm_dev, tt, ctx); |
| 666 | } else { |
| 667 | ttm_tt_clear_backed_up(tt); |
| 668 | err = ttm_pool_alloc(&ttm_dev->pool, tt, ctx); |
| 669 | } |
| 670 | if (err) |
| 671 | return err; |
| 672 | |
| 673 | xe_tt->purgeable = false; |
| 674 | xe_ttm_tt_account_add(ttm_to_xe_device(ttm_dev), tt); |
| 675 | update_global_total_pages(ttm_dev, tt->num_pages); |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt) |
| 681 | { |
| 682 | struct xe_device *xe = ttm_to_xe_device(ttm_dev); |
| 683 | |
| 684 | if ((tt->page_flags & TTM_TT_FLAG_EXTERNAL) && |
| 685 | !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)) |
| 686 | return; |
| 687 | |
| 688 | xe_tt_unmap_sg(xe, tt); |
| 689 | |
| 690 | ttm_pool_free(&ttm_dev->pool, tt); |
| 691 | xe_ttm_tt_account_subtract(xe, tt); |
| 692 | update_global_total_pages(ttm_dev, -(long)tt->num_pages); |
| 693 | } |
| 694 | |
| 695 | static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt) |
| 696 | { |
| 697 | ttm_tt_fini(tt); |
| 698 | kfree(tt); |
| 699 | } |
| 700 | |
| 701 | static bool xe_ttm_resource_visible(struct xe_device *xe, struct ttm_resource *mem) |
| 702 | { |
| 703 | struct xe_ttm_vram_mgr_resource *vres; |
| 704 | |
| 705 | if (mem->mem_type == XE_PL_STOLEN) { |
| 706 | struct xe_ttm_stolen_mgr *mgr = xe->mem.stolen_mgr; |
| 707 | |
| 708 | return mgr->io_base && !xe_ttm_stolen_cpu_access_needs_ggtt(xe); |
| 709 | } |
| 710 | |
| 711 | vres = to_xe_ttm_vram_mgr_resource(mem); |
| 712 | return vres->used_visible_size == mem->size; |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * xe_bo_is_visible_vram - check if BO is placed entirely in visible VRAM. |
| 717 | * @bo: The BO |
| 718 | * |
| 719 | * This function checks whether a given BO resides entirely in memory visible from the CPU |
| 720 | * |
| 721 | * Returns: true if the BO is entirely visible, false otherwise. |
| 722 | * |
| 723 | */ |
| 724 | bool xe_bo_is_visible_vram(struct xe_bo *bo) |
| 725 | { |
| 726 | if (drm_WARN_ON(bo->ttm.base.dev, !xe_bo_is_vram(bo))) |
| 727 | return false; |
| 728 | |
| 729 | return xe_ttm_resource_visible(xe_bo_device(bo), bo->ttm.resource); |
| 730 | } |
| 731 | |
| 732 | static int xe_ttm_io_mem_reserve(struct ttm_device *bdev, |
| 733 | struct ttm_resource *mem) |
| 734 | { |
| 735 | struct xe_device *xe = ttm_to_xe_device(bdev); |
| 736 | |
| 737 | switch (mem->mem_type) { |
| 738 | case XE_PL_SYSTEM: |
| 739 | case XE_PL_TT: |
| 740 | return 0; |
| 741 | case XE_PL_VRAM0: |
| 742 | case XE_PL_VRAM1: { |
| 743 | struct xe_vram_region *vram = xe_map_resource_to_region(mem); |
| 744 | |
| 745 | if (!xe_ttm_resource_visible(xe, mem)) |
| 746 | return -EINVAL; |
| 747 | |
| 748 | mem->bus.offset = mem->start << PAGE_SHIFT; |
| 749 | |
| 750 | if (vram->mapping && |
| 751 | mem->placement & TTM_PL_FLAG_CONTIGUOUS) |
| 752 | mem->bus.addr = (u8 __force *)vram->mapping + |
| 753 | mem->bus.offset; |
| 754 | |
| 755 | mem->bus.offset += vram->io_start; |
| 756 | mem->bus.is_iomem = true; |
| 757 | |
| 758 | #if !IS_ENABLED(CONFIG_X86) |
| 759 | mem->bus.caching = ttm_write_combined; |
| 760 | #endif |
| 761 | return 0; |
| 762 | } case XE_PL_STOLEN: |
| 763 | return xe_ttm_stolen_io_mem_reserve(xe, mem); |
| 764 | default: |
| 765 | return -EINVAL; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo, |
| 770 | const struct ttm_operation_ctx *ctx) |
| 771 | { |
| 772 | struct dma_resv_iter cursor; |
| 773 | struct dma_fence *fence; |
| 774 | struct drm_gem_object *obj = &bo->ttm.base; |
| 775 | struct drm_gpuvm_bo *vm_bo; |
| 776 | bool idle = false; |
| 777 | int ret = 0; |
| 778 | |
| 779 | dma_resv_assert_held(bo->ttm.base.resv); |
| 780 | |
| 781 | if (!list_empty(&bo->ttm.base.gpuva.list)) { |
| 782 | dma_resv_iter_begin(&cursor, bo->ttm.base.resv, |
| 783 | DMA_RESV_USAGE_BOOKKEEP); |
| 784 | dma_resv_for_each_fence_unlocked(&cursor, fence) |
| 785 | dma_fence_enable_signaling(fence); |
| 786 | dma_resv_iter_end(&cursor); |
| 787 | } |
| 788 | |
| 789 | drm_gem_for_each_gpuvm_bo(vm_bo, obj) { |
| 790 | struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm); |
| 791 | struct drm_gpuva *gpuva; |
| 792 | |
| 793 | if (!xe_vm_in_fault_mode(vm)) { |
| 794 | drm_gpuvm_bo_evict(vm_bo, true); |
| 795 | /* |
| 796 | * L2 cache may not be flushed, so ensure that is done in |
| 797 | * xe_vm_invalidate_vma() below |
| 798 | */ |
| 799 | if (!xe_device_is_l2_flush_optimized(xe)) |
| 800 | continue; |
| 801 | } |
| 802 | |
| 803 | if (!idle) { |
| 804 | long timeout; |
| 805 | |
| 806 | if (ctx->no_wait_gpu && |
| 807 | !dma_resv_test_signaled(bo->ttm.base.resv, |
| 808 | DMA_RESV_USAGE_BOOKKEEP)) |
| 809 | return -EBUSY; |
| 810 | |
| 811 | timeout = dma_resv_wait_timeout(bo->ttm.base.resv, |
| 812 | DMA_RESV_USAGE_BOOKKEEP, |
| 813 | ctx->interruptible, |
| 814 | MAX_SCHEDULE_TIMEOUT); |
| 815 | if (!timeout) |
| 816 | return -ETIME; |
| 817 | if (timeout < 0) |
| 818 | return timeout; |
| 819 | |
| 820 | idle = true; |
| 821 | } |
| 822 | |
| 823 | drm_gpuvm_bo_for_each_va(gpuva, vm_bo) { |
| 824 | struct xe_vma *vma = gpuva_to_vma(gpuva); |
| 825 | |
| 826 | trace_xe_vma_evict(vma); |
| 827 | ret = xe_vm_invalidate_vma(vma); |
| 828 | if (XE_WARN_ON(ret)) |
| 829 | return ret; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * The dma-buf map_attachment() / unmap_attachment() is hooked up here. |
| 838 | * Note that unmapping the attachment is deferred to the next |
| 839 | * map_attachment time, or to bo destroy (after idling) whichever comes first. |
| 840 | * This is to avoid syncing before unmap_attachment(), assuming that the |
| 841 | * caller relies on idling the reservation object before moving the |
| 842 | * backing store out. Should that assumption not hold, then we will be able |
| 843 | * to unconditionally call unmap_attachment() when moving out to system. |
| 844 | */ |
| 845 | static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo, |
| 846 | struct ttm_resource *new_res) |
| 847 | { |
| 848 | struct dma_buf_attachment *attach = ttm_bo->base.import_attach; |
| 849 | struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt, |
| 850 | ttm); |
| 851 | struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); |
| 852 | bool device_unplugged = drm_dev_is_unplugged(&xe->drm); |
| 853 | struct sg_table *sg; |
| 854 | |
| 855 | xe_assert(xe, attach); |
| 856 | xe_assert(xe, ttm_bo->ttm); |
| 857 | |
| 858 | if (device_unplugged && new_res->mem_type == XE_PL_SYSTEM && |
| 859 | ttm_bo->sg) { |
| 860 | dma_resv_wait_timeout(ttm_bo->base.resv, DMA_RESV_USAGE_BOOKKEEP, |
| 861 | false, MAX_SCHEDULE_TIMEOUT); |
| 862 | dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL); |
| 863 | ttm_bo->sg = NULL; |
| 864 | } |
| 865 | |
| 866 | if (new_res->mem_type == XE_PL_SYSTEM) |
| 867 | goto out; |
| 868 | |
| 869 | if (ttm_bo->sg) { |
| 870 | dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL); |
| 871 | ttm_bo->sg = NULL; |
| 872 | } |
| 873 | |
| 874 | sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); |
| 875 | if (IS_ERR(sg)) |
| 876 | return PTR_ERR(sg); |
| 877 | |
| 878 | ttm_bo->sg = sg; |
| 879 | xe_tt->sg = sg; |
| 880 | |
| 881 | out: |
| 882 | ttm_bo_move_null(ttm_bo, new_res); |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * xe_bo_move_notify - Notify subsystems of a pending move |
| 889 | * @bo: The buffer object |
| 890 | * @ctx: The struct ttm_operation_ctx controlling locking and waits. |
| 891 | * |
| 892 | * This function notifies subsystems of an upcoming buffer move. |
| 893 | * Upon receiving such a notification, subsystems should schedule |
| 894 | * halting access to the underlying pages and optionally add a fence |
| 895 | * to the buffer object's dma_resv object, that signals when access is |
| 896 | * stopped. The caller will wait on all dma_resv fences before |
| 897 | * starting the move. |
| 898 | * |
| 899 | * A subsystem may commence access to the object after obtaining |
| 900 | * bindings to the new backing memory under the object lock. |
| 901 | * |
| 902 | * Return: 0 on success, -EINTR or -ERESTARTSYS if interrupted in fault mode, |
| 903 | * negative error code on error. |
| 904 | */ |
| 905 | static int xe_bo_move_notify(struct xe_bo *bo, |
| 906 | const struct ttm_operation_ctx *ctx) |
| 907 | { |
| 908 | struct ttm_buffer_object *ttm_bo = &bo->ttm; |
| 909 | struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); |
| 910 | struct ttm_resource *old_mem = ttm_bo->resource; |
| 911 | u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM; |
| 912 | int ret; |
| 913 | |
| 914 | /* |
| 915 | * If this starts to call into many components, consider |
| 916 | * using a notification chain here. |
| 917 | */ |
| 918 | |
| 919 | if (xe_bo_is_pinned(bo)) |
| 920 | return -EINVAL; |
| 921 | |
| 922 | xe_bo_vunmap(bo); |
| 923 | ret = xe_bo_trigger_rebind(xe, bo, ctx); |
| 924 | if (ret) |
| 925 | return ret; |
| 926 | |
| 927 | /* Don't call move_notify() for imported dma-bufs. */ |
| 928 | if (ttm_bo->base.dma_buf && !ttm_bo->base.import_attach) |
| 929 | dma_buf_invalidate_mappings(ttm_bo->base.dma_buf); |
| 930 | |
| 931 | /* |
| 932 | * TTM has already nuked the mmap for us (see ttm_bo_unmap_virtual), |
| 933 | * so if we moved from VRAM make sure to unlink this from the userfault |
| 934 | * tracking. |
| 935 | */ |
| 936 | if (mem_type_is_vram(old_mem_type)) { |
| 937 | mutex_lock(&xe->mem_access.vram_userfault.lock); |
| 938 | if (!list_empty(&bo->vram_userfault_link)) |
| 939 | list_del_init(&bo->vram_userfault_link); |
| 940 | mutex_unlock(&xe->mem_access.vram_userfault.lock); |
| 941 | } |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * xe_bo_set_purgeable_shrinker() - Update shrinker accounting for purgeable state |
| 948 | * @bo: Buffer object |
| 949 | * @new_state: New purgeable state being set |
| 950 | * |
| 951 | * Transfers pages between shrinkable and purgeable buckets when the BO |
| 952 | * purgeable state changes. Called automatically from xe_bo_set_purgeable_state(). |
| 953 | * For pinned BOs, whose pages are not accounted, only the purgeable flag is |
| 954 | * updated, so that the correct bucket is used when the BO is unpinned. |
| 955 | */ |
| 956 | static void xe_bo_set_purgeable_shrinker(struct xe_bo *bo, |
| 957 | enum xe_madv_purgeable_state new_state) |
| 958 | { |
| 959 | struct ttm_buffer_object *ttm_bo = &bo->ttm; |
| 960 | struct ttm_tt *tt = ttm_bo->ttm; |
| 961 | struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); |
| 962 | struct xe_ttm_tt *xe_tt; |
| 963 | long tt_pages; |
| 964 | |
| 965 | xe_bo_assert_held(bo); |
| 966 | |
| 967 | if (!tt || !ttm_tt_is_populated(tt)) |
| 968 | return; |
| 969 | |
| 970 | xe_tt = container_of(tt, struct xe_ttm_tt, ttm); |
| 971 | /* Pinned bos are not accounted. Only update the purgeable flag. */ |
| 972 | tt_pages = xe_bo_is_pinned(bo) ? 0 : tt->num_pages; |
| 973 | |
| 974 | if (!xe_tt->purgeable && new_state == XE_MADV_PURGEABLE_DONTNEED) { |
| 975 | xe_tt->purgeable = true; |
| 976 | /* Transfer pages from shrinkable to purgeable count */ |
| 977 | xe_shrinker_mod_pages(xe->mem.shrinker, -tt_pages, tt_pages); |
| 978 | } else if (xe_tt->purgeable && new_state == XE_MADV_PURGEABLE_WILLNEED) { |
| 979 | xe_tt->purgeable = false; |
| 980 | /* Transfer pages from purgeable to shrinkable count */ |
| 981 | xe_shrinker_mod_pages(xe->mem.shrinker, tt_pages, -tt_pages); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * xe_bo_set_purgeable_state() - Set BO purgeable state with validation |
| 987 | * @bo: Buffer object |
| 988 | * @new_state: New purgeable state |
| 989 | * |
| 990 | * Sets the purgeable state with lockdep assertions and validates state |
| 991 | * transitions. Once a BO is PURGED, it cannot transition to any other state. |
| 992 | * Invalid transitions are caught with xe_assert(). Shrinker page accounting |
| 993 | * is updated automatically. |
| 994 | */ |
| 995 | void xe_bo_set_purgeable_state(struct xe_bo *bo, |
| 996 | enum xe_madv_purgeable_state new_state) |
| 997 | { |
| 998 | struct xe_device *xe = xe_bo_device(bo); |
| 999 | |
| 1000 | xe_bo_assert_held(bo); |
| 1001 | |
| 1002 | /* Validate state is one of the known values */ |
| 1003 | xe_assert(xe, new_state == XE_MADV_PURGEABLE_WILLNEED || |
| 1004 | new_state == XE_MADV_PURGEABLE_DONTNEED || |
| 1005 | new_state == XE_MADV_PURGEABLE_PURGED); |
| 1006 | |
| 1007 | /* Once purged, always purged - cannot transition out */ |
| 1008 | xe_assert(xe, !(bo->purgeable.state == XE_MADV_PURGEABLE_PURGED && |
| 1009 | new_state != XE_MADV_PURGEABLE_PURGED)); |
| 1010 | |
| 1011 | bo->purgeable.state = new_state; |
| 1012 | xe_bo_set_purgeable_shrinker(bo, new_state); |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * xe_ttm_bo_purge() - Purge buffer object backing store |
| 1017 | * @ttm_bo: The TTM buffer object to purge |
| 1018 | * @ctx: TTM operation context |
| 1019 | * |
| 1020 | * This function purges the backing store of a BO marked as DONTNEED and |
| 1021 | * triggers rebind to invalidate stale GPU mappings. For fault-mode VMs, |
| 1022 | * this zaps the PTEs. The next GPU access will trigger a page fault and |
| 1023 | * perform NULL rebind (scratch pages or clear PTEs based on VM config). |
| 1024 | * |
| 1025 | * Return: 0 on success, negative error code on failure |
| 1026 | */ |
| 1027 | static int xe_ttm_bo_purge(struct ttm_buffer_object *ttm_bo, struct ttm_operation_ctx *ctx) |
| 1028 | { |
| 1029 | struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); |
| 1030 | struct ttm_placement place = {}; |
| 1031 | int ret; |
| 1032 | |
| 1033 | xe_bo_assert_held(bo); |
| 1034 | |
| 1035 | if (!ttm_bo->ttm) |
| 1036 | return 0; |
| 1037 | |
| 1038 | if (!xe_bo_madv_is_dontneed(bo)) |
| 1039 | return 0; |
| 1040 | |
| 1041 | /* |
| 1042 | * Use the standard pre-move hook so we share the same cleanup/invalidate |
| 1043 | * path as migrations: drop any CPU vmap and schedule the necessary GPU |
| 1044 | * unbind/rebind work. |
| 1045 | * |
| 1046 | * This must be called before ttm_bo_validate() frees the pages. |
| 1047 | * May fail in no-wait contexts (fault/shrinker) or if the BO is |
| 1048 | * pinned. Keep state unchanged on failure so we don't end up "PURGED" |
| 1049 | * with stale mappings. |
| 1050 | */ |
| 1051 | ret = xe_bo_move_notify(bo, ctx); |
| 1052 | if (ret) |
| 1053 | return ret; |
| 1054 | |
| 1055 | ret = ttm_bo_validate(ttm_bo, &place, ctx); |
| 1056 | if (ret) |
| 1057 | return ret; |
| 1058 | |
| 1059 | /* Commit the state transition only once invalidation was queued */ |
| 1060 | xe_bo_set_purgeable_state(bo, XE_MADV_PURGEABLE_PURGED); |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict, |
| 1066 | struct ttm_operation_ctx *ctx, |
| 1067 | struct ttm_resource *new_mem, |
| 1068 | struct ttm_place *hop) |
| 1069 | { |
| 1070 | struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); |
| 1071 | struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); |
| 1072 | struct ttm_resource *old_mem = ttm_bo->resource; |
| 1073 | u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM; |
| 1074 | struct ttm_tt *ttm = ttm_bo->ttm; |
| 1075 | struct xe_migrate *migrate = NULL; |
| 1076 | struct dma_fence *fence; |
| 1077 | bool move_lacks_source; |
| 1078 | bool tt_has_data; |
| 1079 | bool needs_clear; |
| 1080 | bool handle_system_ccs = (!IS_DGFX(xe) && xe_bo_needs_ccs_pages(bo) && |
| 1081 | ttm && ttm_tt_is_populated(ttm)) ? true : false; |
| 1082 | int ret = 0; |
| 1083 | |
| 1084 | /* |
| 1085 | * Purge only non-shared BOs explicitly marked DONTNEED by userspace. |
| 1086 | * The move_notify callback will handle invalidation asynchronously. |
| 1087 | */ |
| 1088 | if (evict && xe_bo_madv_is_dontneed(bo)) { |
| 1089 | ret = xe_ttm_bo_purge(ttm_bo, ctx); |
| 1090 | if (ret) |
| 1091 | return ret; |
| 1092 | |
| 1093 | /* Free the unused eviction destination resource */ |
| 1094 | ttm_resource_free(ttm_bo, &new_mem); |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | /* Bo creation path, moving to system or TT. */ |
| 1099 | if ((!old_mem && ttm) && !handle_system_ccs) { |
| 1100 | if (new_mem->mem_type == XE_PL_TT) |
| 1101 | ret = xe_tt_map_sg(xe, ttm); |
| 1102 | if (!ret) |
| 1103 | ttm_bo_move_null(ttm_bo, new_mem); |
| 1104 | goto out; |
| 1105 | } |
| 1106 | |
| 1107 | if (ttm_bo->type == ttm_bo_type_sg) { |
| 1108 | if (new_mem->mem_type == XE_PL_SYSTEM) |
| 1109 | ret = xe_bo_move_notify(bo, ctx); |
| 1110 | if (!ret) |
| 1111 | ret = xe_bo_move_dmabuf(ttm_bo, new_mem); |
| 1112 | return ret; |
| 1113 | } |
| 1114 | |
| 1115 | tt_has_data = ttm && (ttm_tt_is_populated(ttm) || ttm_tt_is_swapped(ttm)); |
| 1116 | |
| 1117 | move_lacks_source = !old_mem || (handle_system_ccs ? (!bo->ccs_cleared) : |
| 1118 | (!mem_type_is_vram(old_mem_type) && !tt_has_data)); |
| 1119 | |
| 1120 | needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) || |
| 1121 | (!ttm && ttm_bo->type == ttm_bo_type_device); |
| 1122 | |
| 1123 | if (new_mem->mem_type == XE_PL_TT) { |
| 1124 | ret = xe_tt_map_sg(xe, ttm); |
| 1125 | if (ret) |
| 1126 | goto out; |
| 1127 | } |
| 1128 | |
| 1129 | if ((move_lacks_source && !needs_clear)) { |
| 1130 | ttm_bo_move_null(ttm_bo, new_mem); |
| 1131 | goto out; |
| 1132 | } |
| 1133 | |
| 1134 | if (!move_lacks_source && (bo->flags & XE_BO_FLAG_CPU_ADDR_MIRROR) && |
| 1135 | new_mem->mem_type == XE_PL_SYSTEM) { |
| 1136 | ret = xe_svm_bo_evict(bo); |
| 1137 | if (!ret) { |
| 1138 | drm_dbg(&xe->drm, "Evict system allocator BO success\n"); |
| 1139 | ttm_bo_move_null(ttm_bo, new_mem); |
| 1140 | } else { |
| 1141 | drm_dbg(&xe->drm, "Evict system allocator BO failed=%pe\n", |
| 1142 | ERR_PTR(ret)); |
| 1143 | /* |
| 1144 | * The semantic we want upon SVM eviction failure |
| 1145 | * because of racing access is keep walking for |
| 1146 | * eviction, which is -ENOSPC. |
| 1147 | */ |
| 1148 | if (ret == -EBUSY) |
| 1149 | ret = -ENOSPC; |
| 1150 | } |
| 1151 | |
| 1152 | goto out; |
| 1153 | } |
| 1154 | |
| 1155 | if (old_mem_type == XE_PL_SYSTEM && new_mem->mem_type == XE_PL_TT && !handle_system_ccs) { |
| 1156 | ttm_bo_move_null(ttm_bo, new_mem); |
| 1157 | goto out; |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * Failed multi-hop where the old_mem is still marked as |
| 1162 | * TTM_PL_FLAG_TEMPORARY, should just be a dummy move. |
| 1163 | */ |
| 1164 | if (old_mem_type == XE_PL_TT && |
| 1165 | new_mem->mem_type == XE_PL_TT) { |
| 1166 | ttm_bo_move_null(ttm_bo, new_mem); |
| 1167 | goto out; |
| 1168 | } |
| 1169 | |
| 1170 | if (!move_lacks_source && !xe_bo_is_pinned(bo)) { |
| 1171 | ret = xe_bo_move_notify(bo, ctx); |
| 1172 | if (ret) |
| 1173 | goto out; |
| 1174 | } |
| 1175 | |
| 1176 | if (old_mem_type == XE_PL_TT && |
| 1177 | new_mem->mem_type == XE_PL_SYSTEM) { |
| 1178 | long timeout = dma_resv_wait_timeout(ttm_bo->base.resv, |
| 1179 | DMA_RESV_USAGE_BOOKKEEP, |
| 1180 | false, |
| 1181 | MAX_SCHEDULE_TIMEOUT); |
| 1182 | if (timeout < 0) { |
| 1183 | ret = timeout; |
| 1184 | goto out; |
| 1185 | } |
| 1186 | |
| 1187 | if (!handle_system_ccs) { |
| 1188 | ttm_bo_move_null(ttm_bo, new_mem); |
| 1189 | goto out; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | if (!move_lacks_source && |
| 1194 | ((old_mem_type == XE_PL_SYSTEM && resource_is_vram(new_mem)) || |
| 1195 | (mem_type_is_vram(old_mem_type) && |
| 1196 | new_mem->mem_type == XE_PL_SYSTEM))) { |
| 1197 | hop->fpfn = 0; |
| 1198 | hop->lpfn = 0; |
| 1199 | hop->mem_type = XE_PL_TT; |
| 1200 | hop->flags = TTM_PL_FLAG_TEMPORARY; |
| 1201 | ret = -EMULTIHOP; |
| 1202 | goto out; |
| 1203 | } |
| 1204 | |
| 1205 | if (bo->tile) |
| 1206 | migrate = bo->tile->migrate; |
| 1207 | else if (resource_is_vram(new_mem)) |
| 1208 | migrate = mem_type_to_migrate(xe, new_mem->mem_type); |
| 1209 | else if (mem_type_is_vram(old_mem_type)) |
| 1210 | migrate = mem_type_to_migrate(xe, old_mem_type); |
| 1211 | else |
| 1212 | migrate = xe->tiles[0].migrate; |
| 1213 | |
| 1214 | xe_assert(xe, migrate); |
| 1215 | trace_xe_bo_move(bo, new_mem->mem_type, old_mem_type, move_lacks_source); |
| 1216 | if (xe_rpm_reclaim_safe(xe)) { |
| 1217 | /* |
| 1218 | * We might be called through swapout in the validation path of |
| 1219 | * another TTM device, so acquire rpm here. |
| 1220 | */ |
| 1221 | xe_pm_runtime_get(xe); |
| 1222 | } else { |
| 1223 | drm_WARN_ON(&xe->drm, handle_system_ccs); |
| 1224 | xe_pm_runtime_get_noresume(xe); |
| 1225 | } |
| 1226 | |
| 1227 | /* |
| 1228 | * Attach CCS BBs before submitting the copy job below so a VF |
| 1229 | * migration racing the copy sees valid, up to date attach state. |
| 1230 | */ |
| 1231 | if (IS_VF_CCS_READY(xe) && |
| 1232 | ((move_lacks_source && new_mem->mem_type == XE_PL_TT) || |
| 1233 | (old_mem_type == XE_PL_SYSTEM && new_mem->mem_type == XE_PL_TT)) && |
| 1234 | handle_system_ccs) { |
| 1235 | ret = xe_sriov_vf_ccs_attach_bo(bo, new_mem); |
| 1236 | if (ret) { |
| 1237 | xe_pm_runtime_put(xe); |
| 1238 | goto out; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | if (move_lacks_source) { |
| 1243 | u32 flags = 0; |
| 1244 | |
| 1245 | if (mem_type_is_vram(new_mem->mem_type)) |
| 1246 | flags |= XE_MIGRATE_CLEAR_FLAG_FULL; |
| 1247 | else if (handle_system_ccs) |
| 1248 | flags |= XE_MIGRATE_CLEAR_FLAG_CCS_DATA; |
| 1249 | |
| 1250 | fence = xe_migrate_clear(migrate, bo, new_mem, flags); |
| 1251 | } else { |
| 1252 | fence = xe_migrate_copy(migrate, bo, bo, old_mem, new_mem, |
| 1253 | handle_system_ccs); |
| 1254 | } |
| 1255 | if (IS_ERR(fence)) { |
| 1256 | ret = PTR_ERR(fence); |
| 1257 | xe_pm_runtime_put(xe); |
| 1258 | goto out; |
| 1259 | } |
| 1260 | if (!move_lacks_source) { |
| 1261 | ret = ttm_bo_move_accel_cleanup(ttm_bo, fence, evict, true, |
| 1262 | new_mem); |
| 1263 | if (ret) { |
| 1264 | dma_fence_wait(fence, false); |
| 1265 | ttm_bo_move_null(ttm_bo, new_mem); |
| 1266 | ret = 0; |
| 1267 | } |
| 1268 | } else { |
| 1269 | /* |
| 1270 | * ttm_bo_move_accel_cleanup() may blow up if |
| 1271 | * bo->resource == NULL, so just attach the |
| 1272 | * fence and set the new resource. |
| 1273 | */ |
| 1274 | dma_resv_add_fence(ttm_bo->base.resv, fence, |
| 1275 | DMA_RESV_USAGE_KERNEL); |
| 1276 | ttm_bo_move_null(ttm_bo, new_mem); |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * Detach must wait for the copy above to complete: a VF migration |
| 1281 | * racing an in-flight copy must still see valid CCS BBs, so don't |
| 1282 | * tear them down until the copy fence has signaled. |
| 1283 | */ |
| 1284 | if (IS_VF_CCS_READY(xe) && old_mem_type == XE_PL_TT && |
| 1285 | new_mem->mem_type == XE_PL_SYSTEM) { |
| 1286 | dma_fence_wait(fence, false); |
| 1287 | xe_sriov_vf_ccs_detach_bo(bo); |
| 1288 | } |
| 1289 | |
| 1290 | dma_fence_put(fence); |
| 1291 | xe_pm_runtime_put(xe); |
| 1292 | |
| 1293 | out: |
| 1294 | if ((!ttm_bo->resource || ttm_bo->resource->mem_type == XE_PL_SYSTEM) && |
| 1295 | ttm_bo->ttm) { |
| 1296 | long timeout = dma_resv_wait_timeout(ttm_bo->base.resv, |
| 1297 | DMA_RESV_USAGE_KERNEL, |
| 1298 | false, |
| 1299 | MAX_SCHEDULE_TIMEOUT); |
| 1300 | if (timeout < 0) |
| 1301 | ret = timeout; |
| 1302 | |
| 1303 | if (IS_VF_CCS_READY(xe)) |
| 1304 | xe_sriov_vf_ccs_detach_bo(bo); |
| 1305 | |
| 1306 | xe_tt_unmap_sg(xe, ttm_bo->ttm); |
| 1307 | } |
| 1308 | |
| 1309 | return ret; |
| 1310 | } |
| 1311 | |
| 1312 | static long xe_bo_shrink_purge(struct ttm_operation_ctx *ctx, |
| 1313 | struct ttm_buffer_object *bo, |
| 1314 | unsigned long *scanned) |
| 1315 | { |
| 1316 | struct xe_device *xe = ttm_to_xe_device(bo->bdev); |
| 1317 | struct ttm_tt *tt = bo->ttm; |
| 1318 | long lret; |
| 1319 | |
| 1320 | /* Fake move to system, without copying data. */ |
| 1321 | if (bo->resource->mem_type != XE_PL_SYSTEM) { |
| 1322 | struct ttm_resource *new_resource; |
| 1323 | |
| 1324 | lret = ttm_bo_wait_ctx(bo, ctx); |
| 1325 | if (lret) |
| 1326 | return lret; |
| 1327 | |
| 1328 | lret = ttm_bo_mem_space(bo, &sys_placement, &new_resource, ctx); |
| 1329 | if (lret) |
| 1330 | return lret; |
| 1331 | |
| 1332 | xe_tt_unmap_sg(xe, bo->ttm); |
| 1333 | ttm_bo_move_null(bo, new_resource); |
| 1334 | } |
| 1335 | |
| 1336 | *scanned += bo->ttm->num_pages; |
| 1337 | lret = ttm_bo_shrink(ctx, bo, (struct ttm_bo_shrink_flags) |
| 1338 | {.purge = true, |
| 1339 | .writeback = false, |
| 1340 | .allow_move = false}); |
| 1341 | |
| 1342 | if (lret > 0) { |
| 1343 | xe_ttm_tt_account_subtract(xe, bo->ttm); |
| 1344 | update_global_total_pages(bo->bdev, -(long)tt->num_pages); |
| 1345 | } |
| 1346 | |
| 1347 | return lret; |
| 1348 | } |
| 1349 | |
| 1350 | static bool |
| 1351 | xe_bo_eviction_valuable(struct ttm_buffer_object *bo, const struct ttm_place *place) |
| 1352 | { |
| 1353 | struct drm_gpuvm_bo *vm_bo; |
| 1354 | |
| 1355 | if (!ttm_bo_eviction_valuable(bo, place)) |
| 1356 | return false; |
| 1357 | |
| 1358 | if (!xe_bo_is_xe_bo(bo)) |
| 1359 | return true; |
| 1360 | |
| 1361 | drm_gem_for_each_gpuvm_bo(vm_bo, &bo->base) { |
| 1362 | if (xe_vm_is_validating(gpuvm_to_vm(vm_bo->vm))) |
| 1363 | return false; |
| 1364 | } |
| 1365 | |
| 1366 | return true; |
| 1367 | } |
| 1368 | |
| 1369 | /** |
| 1370 | * xe_bo_shrink() - Try to shrink an xe bo. |
| 1371 | * @ctx: The struct ttm_operation_ctx used for shrinking. |
| 1372 | * @bo: The TTM buffer object whose pages to shrink. |
| 1373 | * @flags: Flags governing the shrink behaviour. |
| 1374 | * @scanned: Pointer to a counter of the number of pages |
| 1375 | * attempted to shrink. |
| 1376 | * |
| 1377 | * Try to shrink- or purge a bo, and if it succeeds, unmap dma. |
| 1378 | * Note that we need to be able to handle also non xe bos |
| 1379 | * (ghost bos), but only if the struct ttm_tt is embedded in |
| 1380 | * a struct xe_ttm_tt. When the function attempts to shrink |
| 1381 | * the pages of a buffer object, The value pointed to by @scanned |
| 1382 | * is updated. |
| 1383 | * |
| 1384 | * Return: The number of pages shrunken or purged, or negative error |
| 1385 | * code on failure. |
| 1386 | */ |
| 1387 | long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo, |
| 1388 | const struct xe_bo_shrink_flags flags, |
| 1389 | unsigned long *scanned) |
| 1390 | { |
| 1391 | struct ttm_tt *tt = bo->ttm; |
| 1392 | struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); |
| 1393 | struct ttm_place place = {.mem_type = bo->resource->mem_type}; |
| 1394 | struct xe_bo *xe_bo = ttm_to_xe_bo(bo); |
| 1395 | struct xe_device *xe = ttm_to_xe_device(bo->bdev); |
| 1396 | bool needs_rpm; |
| 1397 | long lret = 0L; |
| 1398 | |
| 1399 | if (!(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE) || |
| 1400 | (flags.purge && !xe_tt->purgeable)) |
| 1401 | return -EBUSY; |
| 1402 | |
| 1403 | if (!xe_bo_eviction_valuable(bo, &place)) |
| 1404 | return -EBUSY; |
| 1405 | |
| 1406 | if (!xe_bo_is_xe_bo(bo) || !xe_bo_get_unless_zero(xe_bo)) |
| 1407 | return xe_bo_shrink_purge(ctx, bo, scanned); |
| 1408 | |
| 1409 | if (xe_tt->purgeable) { |
| 1410 | if (bo->resource->mem_type != XE_PL_SYSTEM) |
| 1411 | lret = xe_bo_move_notify(xe_bo, ctx); |
| 1412 | if (!lret) |
| 1413 | lret = xe_bo_shrink_purge(ctx, bo, scanned); |
| 1414 | if (lret > 0 && xe_bo_madv_is_dontneed(xe_bo)) |
| 1415 | xe_bo_set_purgeable_state(xe_bo, |
| 1416 | XE_MADV_PURGEABLE_PURGED); |
| 1417 | goto out_unref; |
| 1418 | } |
| 1419 | |
| 1420 | /* System CCS needs gpu copy when moving PL_TT -> PL_SYSTEM */ |
| 1421 | needs_rpm = (!IS_DGFX(xe) && bo->resource->mem_type != XE_PL_SYSTEM && |
| 1422 | xe_bo_needs_ccs_pages(xe_bo)); |
| 1423 | if (needs_rpm && !xe_pm_runtime_get_if_active(xe)) |
| 1424 | goto out_unref; |
| 1425 | |
| 1426 | *scanned += tt->num_pages; |
| 1427 | lret = ttm_bo_shrink(ctx, bo, (struct ttm_bo_shrink_flags) |
| 1428 | {.purge = false, |
| 1429 | .writeback = flags.writeback, |
| 1430 | .allow_move = true}); |
| 1431 | if (needs_rpm) |
| 1432 | xe_pm_runtime_put(xe); |
| 1433 | |
| 1434 | if (lret > 0) { |
| 1435 | xe_ttm_tt_account_subtract(xe, tt); |
| 1436 | update_global_total_pages(bo->bdev, -(long)tt->num_pages); |
| 1437 | } |
| 1438 | |
| 1439 | out_unref: |
| 1440 | xe_bo_put(xe_bo); |
| 1441 | |
| 1442 | return lret; |
| 1443 | } |
| 1444 | |
| 1445 | /** |
| 1446 | * xe_bo_notifier_prepare_pinned() - Prepare a pinned VRAM object to be backed |
| 1447 | * up in system memory. |
| 1448 | * @bo: The buffer object to prepare. |
| 1449 | * |
| 1450 | * On successful completion, the object backup pages are allocated. Expectation |
| 1451 | * is that this is called from the PM notifier, prior to suspend/hibernation. |
| 1452 | * |
| 1453 | * Return: 0 on success. Negative error code on failure. |
| 1454 | */ |
| 1455 | int xe_bo_notifier_prepare_pinned(struct xe_bo *bo) |
| 1456 | { |
| 1457 | struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); |
| 1458 | struct xe_validation_ctx ctx; |
| 1459 | struct drm_exec exec; |
| 1460 | struct xe_bo *backup; |
| 1461 | int ret = 0; |
| 1462 | |
| 1463 | xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.exclusive = true}, ret) { |
| 1464 | ret = drm_exec_lock_obj(&exec, &bo->ttm.base); |
| 1465 | drm_exec_retry_on_contention(&exec); |
| 1466 | xe_assert(xe, !ret); |
| 1467 | xe_assert(xe, !bo->backup_obj); |
| 1468 | |
| 1469 | /* |
| 1470 | * Since this is called from the PM notifier we might have raced with |
| 1471 | * someone unpinning this after we dropped the pinned list lock and |
| 1472 | * grabbing the above bo lock. |
| 1473 | */ |
| 1474 | if (!xe_bo_is_pinned(bo)) |
| 1475 | break; |
| 1476 | |
| 1477 | if (!xe_bo_is_vram(bo)) |
| 1478 | break; |
| 1479 | |
| 1480 | if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE) |
| 1481 | break; |
| 1482 | |
| 1483 | backup = xe_bo_init_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL, xe_bo_size(bo), |
| 1484 | DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel, |
| 1485 | XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS | |
| 1486 | XE_BO_FLAG_PINNED, NULL, &exec); |
| 1487 | if (IS_ERR(backup)) { |
| 1488 | drm_exec_retry_on_contention(&exec); |
| 1489 | ret = PTR_ERR(backup); |
| 1490 | xe_validation_retry_on_oom(&ctx, &ret); |
| 1491 | break; |
| 1492 | } |
| 1493 | |
| 1494 | backup->parent_obj = xe_bo_get(bo); /* Released by bo_destroy */ |
| 1495 | /* Note: XE_BO_FLAG_SYSTEM resolves to XE_PL_TT so already populated. */ |
| 1496 | xe_bo_pin_account(backup); |
| 1497 | bo->backup_obj = backup; |
| 1498 | } |
| 1499 | |
| 1500 | return ret; |
| 1501 | } |
| 1502 | |
| 1503 | /** |
| 1504 | * xe_bo_notifier_unprepare_pinned() - Undo the previous prepare operation. |
| 1505 | * @bo: The buffer object to undo the prepare for. |
| 1506 | * |
| 1507 | * Always returns 0. The backup object is removed, if still present. Expectation |
| 1508 | * it that this called from the PM notifier when undoing the prepare step. |
| 1509 | * |
| 1510 | * Return: Always returns 0. |
| 1511 | */ |
| 1512 | int xe_bo_notifier_unprepare_pinned(struct xe_bo *bo) |
| 1513 | { |
| 1514 | xe_bo_lock(bo, false); |
| 1515 | if (bo->backup_obj) { |
| 1516 | xe_bo_unpin_account(bo->backup_obj); |
| 1517 | xe_bo_put(bo->backup_obj); |
| 1518 | bo->backup_obj = NULL; |
| 1519 | } |
| 1520 | xe_bo_unlock(bo); |
| 1521 | |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | static int xe_bo_evict_pinned_copy(struct xe_bo *bo, struct xe_bo *backup) |
| 1526 | { |
| 1527 | struct xe_device *xe = xe_bo_device(bo); |
| 1528 | bool unmap = false; |
| 1529 | int ret = 0; |
| 1530 | |
| 1531 | if (xe_bo_is_user(bo) || (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) { |
| 1532 | struct xe_migrate *migrate; |
| 1533 | struct dma_fence *fence; |
| 1534 | |
| 1535 | if (bo->tile) |
| 1536 | migrate = bo->tile->migrate; |
| 1537 | else |
| 1538 | migrate = mem_type_to_migrate(xe, bo->ttm.resource->mem_type); |
| 1539 | |
| 1540 | xe_assert(xe, bo->ttm.base.resv == backup->ttm.base.resv); |
| 1541 | ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1); |
| 1542 | if (ret) |
| 1543 | goto out_backup; |
| 1544 | |
| 1545 | fence = xe_migrate_copy(migrate, bo, backup, bo->ttm.resource, |
| 1546 | backup->ttm.resource, false); |
| 1547 | if (IS_ERR(fence)) { |
| 1548 | ret = PTR_ERR(fence); |
| 1549 | goto out_backup; |
| 1550 | } |
| 1551 | |
| 1552 | dma_resv_add_fence(bo->ttm.base.resv, fence, |
| 1553 | DMA_RESV_USAGE_KERNEL); |
| 1554 | dma_fence_put(fence); |
| 1555 | } else { |
| 1556 | ret = xe_bo_vmap(backup); |
| 1557 | if (ret) |
| 1558 | goto out_backup; |
| 1559 | |
| 1560 | if (iosys_map_is_null(&bo->vmap)) { |
| 1561 | ret = xe_bo_vmap(bo); |
| 1562 | if (ret) |
| 1563 | goto out_vunmap; |
| 1564 | unmap = true; |
| 1565 | } |
| 1566 | |
| 1567 | xe_map_memcpy_from(xe, backup->vmap.vaddr, &bo->vmap, 0, |
| 1568 | xe_bo_size(bo)); |
| 1569 | } |
| 1570 | |
| 1571 | if (!bo->backup_obj) |
| 1572 | bo->backup_obj = backup; |
| 1573 | out_vunmap: |
| 1574 | xe_bo_vunmap(backup); |
| 1575 | out_backup: |
| 1576 | if (unmap) |
| 1577 | xe_bo_vunmap(bo); |
| 1578 | |
| 1579 | return ret; |
| 1580 | } |
| 1581 | |
| 1582 | /** |
| 1583 | * xe_bo_evict_pinned() - Evict a pinned VRAM object to system memory |
| 1584 | * @bo: The buffer object to move. |
| 1585 | * |
| 1586 | * On successful completion, the object memory will be moved to system memory. |
| 1587 | * |
| 1588 | * This is needed to for special handling of pinned VRAM object during |
| 1589 | * suspend-resume. |
| 1590 | * |
| 1591 | * Return: 0 on success. Negative error code on failure. |
| 1592 | */ |
| 1593 | int xe_bo_evict_pinned(struct xe_bo *bo) |
| 1594 | { |
| 1595 | struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); |
| 1596 | struct xe_validation_ctx ctx; |
| 1597 | struct drm_exec exec; |
| 1598 | struct xe_bo *backup = bo->backup_obj; |
| 1599 | bool backup_created = false; |
| 1600 | int ret = 0; |
| 1601 | |
| 1602 | xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.exclusive = true}, ret) { |
| 1603 | ret = drm_exec_lock_obj(&exec, &bo->ttm.base); |
| 1604 | drm_exec_retry_on_contention(&exec); |
| 1605 | xe_assert(xe, !ret); |
| 1606 | |
| 1607 | if (WARN_ON(!bo->ttm.resource)) { |
| 1608 | ret = -EINVAL; |
| 1609 | break; |
| 1610 | } |
| 1611 | |
| 1612 | if (WARN_ON(!xe_bo_is_pinned(bo))) { |
| 1613 | ret = -EINVAL; |
| 1614 | break; |
| 1615 | } |
| 1616 | |
| 1617 | if (!xe_bo_is_vram(bo)) |
| 1618 | break; |
| 1619 | |
| 1620 | if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE) |
| 1621 | break; |
| 1622 | |
| 1623 | if (!backup) { |
| 1624 | backup = xe_bo_init_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL, |
| 1625 | xe_bo_size(bo), |
| 1626 | DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel, |
| 1627 | XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS | |
| 1628 | XE_BO_FLAG_PINNED, NULL, &exec); |
| 1629 | if (IS_ERR(backup)) { |
| 1630 | drm_exec_retry_on_contention(&exec); |
| 1631 | ret = PTR_ERR(backup); |
| 1632 | xe_validation_retry_on_oom(&ctx, &ret); |
| 1633 | break; |
| 1634 | } |
| 1635 | backup->parent_obj = xe_bo_get(bo); /* Released by bo_destroy */ |
| 1636 | backup_created = true; |
| 1637 | } |
| 1638 | |
| 1639 | ret = xe_bo_evict_pinned_copy(bo, backup); |
| 1640 | } |
| 1641 | |
| 1642 | if (ret && backup_created) |
| 1643 | xe_bo_put(backup); |
| 1644 | |
| 1645 | return ret; |
| 1646 | } |
| 1647 | |
| 1648 | /** |
| 1649 | * xe_bo_restore_pinned() - Restore a pinned VRAM object |
| 1650 | * @bo: The buffer object to move. |
| 1651 | * |
| 1652 | * On successful completion, the object memory will be moved back to VRAM. |
| 1653 | * |
| 1654 | * This is needed to for special handling of pinned VRAM object during |
| 1655 | * suspend-resume. |
| 1656 | * |
| 1657 | * Return: 0 on success. Negative error code on failure. |
| 1658 | */ |
| 1659 | int xe_bo_restore_pinned(struct xe_bo *bo) |
| 1660 | { |
| 1661 | struct ttm_operation_ctx ctx = { |
| 1662 | .interruptible = false, |
| 1663 | .gfp_retry_mayfail = false, |
| 1664 | }; |
| 1665 | struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); |
| 1666 | struct xe_bo *backup = bo->backup_obj; |
| 1667 | bool unmap = false; |
| 1668 | int ret; |
| 1669 | |
| 1670 | if (!backup) |
| 1671 | return 0; |
| 1672 | |
| 1673 | xe_bo_lock(bo, false); |
| 1674 | |
| 1675 | if (!xe_bo_is_pinned(backup)) { |
| 1676 | ret = ttm_bo_validate(&backup->ttm, &backup->placement, &ctx); |
| 1677 | if (ret) |
| 1678 | goto out_unlock_bo; |
| 1679 | } |
| 1680 | |
| 1681 | if (xe_bo_is_user(bo) || (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) { |
| 1682 | struct xe_migrate *migrate; |
| 1683 | struct dma_fence *fence; |
| 1684 | |
| 1685 | if (bo->tile) |
| 1686 | migrate = bo->tile->migrate; |
| 1687 | else |
| 1688 | migrate = mem_type_to_migrate(xe, bo->ttm.resource->mem_type); |
| 1689 | |
| 1690 | ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1); |
| 1691 | if (ret) |
| 1692 | goto out_unlock_bo; |
| 1693 | |
| 1694 | fence = xe_migrate_copy(migrate, backup, bo, |
| 1695 | backup->ttm.resource, bo->ttm.resource, |
| 1696 | false); |
| 1697 | if (IS_ERR(fence)) { |
| 1698 | ret = PTR_ERR(fence); |
| 1699 | goto out_unlock_bo; |
| 1700 | } |
| 1701 | |
| 1702 | dma_resv_add_fence(bo->ttm.base.resv, fence, |
| 1703 | DMA_RESV_USAGE_KERNEL); |
| 1704 | dma_fence_put(fence); |
| 1705 | } else { |
| 1706 | ret = xe_bo_vmap(backup); |
| 1707 | if (ret) |
| 1708 | goto out_unlock_bo; |
| 1709 | |
| 1710 | if (iosys_map_is_null(&bo->vmap)) { |
| 1711 | ret = xe_bo_vmap(bo); |
| 1712 | if (ret) |
| 1713 | goto out_backup; |
| 1714 | unmap = true; |
| 1715 | } |
| 1716 | |
| 1717 | xe_map_memcpy_to(xe, &bo->vmap, 0, backup->vmap.vaddr, |
| 1718 | xe_bo_size(bo)); |
| 1719 | } |
| 1720 | |
| 1721 | bo->backup_obj = NULL; |
| 1722 | |
| 1723 | out_backup: |
| 1724 | xe_bo_vunmap(backup); |
| 1725 | if (!bo->backup_obj) { |
| 1726 | if (xe_bo_is_pinned(backup)) |
| 1727 | xe_bo_unpin_account(backup); |
| 1728 | xe_bo_put(backup); |
| 1729 | } |
| 1730 | out_unlock_bo: |
| 1731 | if (unmap) |
| 1732 | xe_bo_vunmap(bo); |
| 1733 | xe_bo_unlock(bo); |
| 1734 | return ret; |
| 1735 | } |
| 1736 | |
| 1737 | int xe_bo_dma_unmap_pinned(struct xe_bo *bo) |
| 1738 | { |
| 1739 | struct ttm_buffer_object *ttm_bo = &bo->ttm; |
| 1740 | struct ttm_tt *tt = ttm_bo->ttm; |
| 1741 | |
| 1742 | if (tt) { |
| 1743 | struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm); |
| 1744 | |
| 1745 | if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) { |
| 1746 | dma_buf_unmap_attachment(ttm_bo->base.import_attach, |
| 1747 | ttm_bo->sg, |
| 1748 | DMA_BIDIRECTIONAL); |
| 1749 | ttm_bo->sg = NULL; |
| 1750 | xe_tt->sg = NULL; |
| 1751 | } else if (xe_tt->sg) { |
| 1752 | dma_unmap_sgtable(ttm_to_xe_device(ttm_bo->bdev)->drm.dev, |
| 1753 | xe_tt->sg, |
| 1754 | DMA_BIDIRECTIONAL, 0); |
| 1755 | sg_free_table(xe_tt->sg); |
| 1756 | xe_tt->sg = NULL; |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | return 0; |
| 1761 | } |
| 1762 | |
| 1763 | static unsigned long xe_ttm_io_mem_pfn(struct ttm_buffer_object *ttm_bo, |
| 1764 | unsigned long page_offset) |
| 1765 | { |
| 1766 | struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); |
| 1767 | struct xe_res_cursor cursor; |
| 1768 | struct xe_vram_region *vram; |
| 1769 | |
| 1770 | if (ttm_bo->resource->mem_type == XE_PL_STOLEN) |
| 1771 | return xe_ttm_stolen_io_offset(bo, page_offset << PAGE_SHIFT) >> PAGE_SHIFT; |
| 1772 | |
| 1773 | vram = xe_map_resource_to_region(ttm_bo->resource); |
| 1774 | xe_res_first(ttm_bo->resource, (u64)page_offset << PAGE_SHIFT, 0, &cursor); |
| 1775 | return (vram->io_start + cursor.start) >> PAGE_SHIFT; |
| 1776 | } |
| 1777 | |
| 1778 | static void __xe_bo_vunmap(struct xe_bo *bo); |
| 1779 | |
| 1780 | /* |
| 1781 | * TODO: Move this function to TTM so we don't rely on how TTM does its |
| 1782 | * locking, thereby abusing TTM internals. |
| 1783 | */ |
| 1784 | static bool xe_ttm_bo_lock_in_destructor(struct ttm_buffer_object *ttm_bo) |
| 1785 | { |
| 1786 | struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); |
| 1787 | bool locked; |
| 1788 | |
| 1789 | xe_assert(xe, !kref_read(&ttm_bo->kref)); |
| 1790 | |
| 1791 | /* |
| 1792 | * We can typically only race with TTM trylocking under the |
| 1793 | * lru_lock, which will immediately be unlocked again since |
| 1794 | * the ttm_bo refcount is zero at this point. So trylocking *should* |
| 1795 | * always succeed here, as long as we hold the lru lock. |
| 1796 | */ |
| 1797 | spin_lock(&ttm_bo->bdev->lru_lock); |
| 1798 | locked = dma_resv_trylock(&ttm_bo->base._resv); |
| 1799 | spin_unlock(&ttm_bo->bdev->lru_lock); |
| 1800 | xe_assert(xe, locked); |
| 1801 | |
| 1802 | return locked; |
| 1803 | } |
| 1804 | |
| 1805 | static void xe_ttm_bo_release_notify(struct ttm_buffer_object *ttm_bo) |
| 1806 | { |
| 1807 | struct dma_resv_iter cursor; |
| 1808 | struct dma_fence *fence; |
| 1809 | struct dma_fence *replacement = NULL; |
| 1810 | struct xe_bo *bo; |
| 1811 | |
| 1812 | if (!xe_bo_is_xe_bo(ttm_bo)) |
| 1813 | return; |
| 1814 | |
| 1815 | bo = ttm_to_xe_bo(ttm_bo); |
| 1816 | xe_assert(xe_bo_device(bo), !(bo->created && kref_read(&ttm_bo->base.refcount))); |
| 1817 | |
| 1818 | if (!xe_ttm_bo_lock_in_destructor(ttm_bo)) |
| 1819 | return; |
| 1820 | |
| 1821 | /* |
| 1822 | * Scrub the preempt fences if any. The unbind fence is already |
| 1823 | * attached to the resv. |
| 1824 | * TODO: Don't do this for external bos once we scrub them after |
| 1825 | * unbind. |
| 1826 | */ |
| 1827 | dma_resv_for_each_fence(&cursor, &ttm_bo->base._resv, |
| 1828 | DMA_RESV_USAGE_BOOKKEEP, fence) { |
| 1829 | if (xe_fence_is_xe_preempt(fence) && |
| 1830 | !dma_fence_is_signaled(fence)) { |
| 1831 | if (!replacement) |
| 1832 | replacement = dma_fence_get_stub(); |
| 1833 | |
| 1834 | dma_resv_replace_fences(&ttm_bo->base._resv, |
| 1835 | fence->context, |
| 1836 | replacement, |
| 1837 | DMA_RESV_USAGE_BOOKKEEP); |
| 1838 | } |
| 1839 | } |
| 1840 | dma_fence_put(replacement); |
| 1841 | |
| 1842 | dma_resv_unlock(&ttm_bo->base._resv); |
| 1843 | } |
| 1844 | |
| 1845 | static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo) |
| 1846 | { |
| 1847 | struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); |
| 1848 | |
| 1849 | if (!xe_bo_is_xe_bo(ttm_bo)) |
| 1850 | return; |
| 1851 | |
| 1852 | if (IS_VF_CCS_READY(ttm_to_xe_device(ttm_bo->bdev))) |
| 1853 | xe_sriov_vf_ccs_detach_bo(bo); |
| 1854 | |
| 1855 | /* |
| 1856 | * Object is idle and about to be destroyed. Release the |
| 1857 | * dma-buf attachment. |
| 1858 | */ |
| 1859 | if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) { |
| 1860 | struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, |
| 1861 | struct xe_ttm_tt, ttm); |
| 1862 | |
| 1863 | dma_buf_unmap_attachment(ttm_bo->base.import_attach, ttm_bo->sg, |
| 1864 | DMA_BIDIRECTIONAL); |
| 1865 | ttm_bo->sg = NULL; |
| 1866 | xe_tt->sg = NULL; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | static void xe_ttm_bo_swap_notify(struct ttm_buffer_object *ttm_bo) |
| 1871 | { |
| 1872 | struct ttm_operation_ctx ctx = { |
| 1873 | .interruptible = false, |
| 1874 | .gfp_retry_mayfail = false, |
| 1875 | }; |
| 1876 | |
| 1877 | if (ttm_bo->ttm) { |
| 1878 | struct xe_ttm_tt *xe_tt = |
| 1879 | container_of(ttm_bo->ttm, struct xe_ttm_tt, ttm); |
| 1880 | |
| 1881 | if (xe_tt->purgeable) |
| 1882 | xe_ttm_bo_purge(ttm_bo, &ctx); |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | static int xe_ttm_access_memory(struct ttm_buffer_object *ttm_bo, |
| 1887 | unsigned long offset, void *buf, int len, |
| 1888 | int write) |
| 1889 | { |
| 1890 | struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); |
| 1891 | struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); |
| 1892 | struct iosys_map vmap; |
| 1893 | struct xe_res_cursor cursor; |
| 1894 | struct xe_vram_region *vram; |
| 1895 | int bytes_left = len; |
| 1896 | int err = 0; |
| 1897 | |
| 1898 | xe_bo_assert_held(bo); |
| 1899 | xe_device_assert_mem_access(xe); |
| 1900 | |
| 1901 | if (!mem_type_is_vram(ttm_bo->resource->mem_type)) |
| 1902 | return -EIO; |
| 1903 | |
| 1904 | if (!xe_bo_is_visible_vram(bo) || len >= SZ_16K) { |
| 1905 | struct xe_migrate *migrate = |
| 1906 | mem_type_to_migrate(xe, ttm_bo->resource->mem_type); |
| 1907 | |
| 1908 | err = xe_migrate_access_memory(migrate, bo, offset, buf, len, |
| 1909 | write); |
| 1910 | goto out; |
| 1911 | } |
| 1912 | |
| 1913 | vram = xe_map_resource_to_region(ttm_bo->resource); |
| 1914 | xe_res_first(ttm_bo->resource, offset & PAGE_MASK, |
| 1915 | xe_bo_size(bo) - (offset & PAGE_MASK), &cursor); |
| 1916 | |
| 1917 | do { |
| 1918 | unsigned long page_offset = (offset & ~PAGE_MASK); |
| 1919 | int byte_count = min((int)(PAGE_SIZE - page_offset), bytes_left); |
| 1920 | |
| 1921 | iosys_map_set_vaddr_iomem(&vmap, (u8 __iomem *)vram->mapping + |
| 1922 | cursor.start); |
| 1923 | if (write) |
| 1924 | xe_map_memcpy_to(xe, &vmap, page_offset, buf, byte_count); |
| 1925 | else |
| 1926 | xe_map_memcpy_from(xe, buf, &vmap, page_offset, byte_count); |
| 1927 | |
| 1928 | buf += byte_count; |
| 1929 | offset += byte_count; |
| 1930 | bytes_left -= byte_count; |
| 1931 | if (bytes_left) |
| 1932 | xe_res_next(&cursor, PAGE_SIZE); |
| 1933 | } while (bytes_left); |
| 1934 | |
| 1935 | out: |
| 1936 | return err ?: len; |
| 1937 | } |
| 1938 | |
| 1939 | const struct ttm_device_funcs xe_ttm_funcs = { |
| 1940 | .ttm_tt_create = xe_ttm_tt_create, |
| 1941 | .ttm_tt_populate = xe_ttm_tt_populate, |
| 1942 | .ttm_tt_unpopulate = xe_ttm_tt_unpopulate, |
| 1943 | .ttm_tt_destroy = xe_ttm_tt_destroy, |
| 1944 | .evict_flags = xe_evict_flags, |
| 1945 | .move = xe_bo_move, |
| 1946 | .io_mem_reserve = xe_ttm_io_mem_reserve, |
| 1947 | .io_mem_pfn = xe_ttm_io_mem_pfn, |
| 1948 | .access_memory = xe_ttm_access_memory, |
| 1949 | .release_notify = xe_ttm_bo_release_notify, |
| 1950 | .eviction_valuable = xe_bo_eviction_valuable, |
| 1951 | .delete_mem_notify = xe_ttm_bo_delete_mem_notify, |
| 1952 | .swap_notify = xe_ttm_bo_swap_notify, |
| 1953 | }; |
| 1954 | |
| 1955 | static void xe_ttm_bo_destroy(struct ttm_buffer_object *ttm_bo) |
| 1956 | { |
| 1957 | struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); |
| 1958 | struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); |
| 1959 | struct xe_tile *tile; |
| 1960 | u8 id; |
| 1961 | |
| 1962 | if (bo->ttm.base.import_attach) |
| 1963 | drm_prime_gem_destroy(&bo->ttm.base, NULL); |
| 1964 | if (bo->dma_buf) |
| 1965 | dma_buf_put(bo->dma_buf); |
| 1966 | drm_gem_object_release(&bo->ttm.base); |
| 1967 | |
| 1968 | xe_assert(xe, list_empty(&ttm_bo->base.gpuva.list)); |
| 1969 | |
| 1970 | for_each_tile(tile, xe, id) |
| 1971 | if (bo->ggtt_node[id]) |
| 1972 | xe_ggtt_remove_bo(tile->mem.ggtt, bo); |
| 1973 | |
| 1974 | #ifdef CONFIG_PROC_FS |
| 1975 | if (bo->client) |
| 1976 | xe_drm_client_remove_bo(bo); |
| 1977 | #endif |
| 1978 | |
| 1979 | if (bo->vm && xe_bo_is_user(bo)) |
| 1980 | xe_vm_put(bo->vm); |
| 1981 | |
| 1982 | if (bo->parent_obj) |
| 1983 | xe_bo_put(bo->parent_obj); |
| 1984 | |
| 1985 | mutex_lock(&xe->mem_access.vram_userfault.lock); |
| 1986 | if (!list_empty(&bo->vram_userfault_link)) |
| 1987 | list_del(&bo->vram_userfault_link); |
| 1988 | mutex_unlock(&xe->mem_access.vram_userfault.lock); |
| 1989 | |
| 1990 | kfree(bo); |
| 1991 | } |
| 1992 | |
| 1993 | static void xe_gem_object_free(struct drm_gem_object *obj) |
| 1994 | { |
| 1995 | /* Our BO reference counting scheme works as follows: |
| 1996 | * |
| 1997 | * The gem object kref is typically used throughout the driver, |
| 1998 | * and the gem object holds a ttm_buffer_object refcount, so |
| 1999 | * that when the last gem object reference is put, which is when |
| 2000 | * we end up in this function, we put also that ttm_buffer_object |
| 2001 | * refcount. Anything using gem interfaces is then no longer |
| 2002 | * allowed to access the object in a way that requires a gem |
| 2003 | * refcount, including locking the object. |
| 2004 | * |
| 2005 | * driver ttm callbacks is allowed to use the ttm_buffer_object |
| 2006 | * refcount directly if needed. |
| 2007 | */ |
| 2008 | __xe_bo_vunmap(gem_to_xe_bo(obj)); |
| 2009 | ttm_bo_fini(container_of(obj, struct ttm_buffer_object, base)); |
| 2010 | } |
| 2011 | |
| 2012 | static void xe_gem_object_close(struct drm_gem_object *obj, |
| 2013 | struct drm_file *file_priv) |
| 2014 | { |
| 2015 | struct xe_bo *bo = gem_to_xe_bo(obj); |
| 2016 | |
| 2017 | if (bo->vm && !xe_vm_in_fault_mode(bo->vm)) { |
| 2018 | xe_assert(xe_bo_device(bo), xe_bo_is_user(bo)); |
| 2019 | |
| 2020 | xe_bo_lock(bo, false); |
| 2021 | ttm_bo_set_bulk_move(&bo->ttm, NULL); |
| 2022 | xe_bo_unlock(bo); |
| 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | static bool should_migrate_to_smem(struct xe_bo *bo) |
| 2027 | { |
| 2028 | /* |
| 2029 | * NOTE: The following atomic checks are platform-specific. For example, |
| 2030 | * if a device supports CXL atomics, these may not be necessary or |
| 2031 | * may behave differently. |
| 2032 | */ |
| 2033 | |
| 2034 | return bo->attr.atomic_access == DRM_XE_ATOMIC_GLOBAL || |
| 2035 | bo->attr.atomic_access == DRM_XE_ATOMIC_CPU; |
| 2036 | } |
| 2037 | |
| 2038 | static int xe_bo_wait_usage_kernel(struct xe_bo *bo, struct ttm_operation_ctx *ctx) |
| 2039 | { |
| 2040 | long lerr; |
| 2041 | |
| 2042 | if (ctx->no_wait_gpu) |
| 2043 | return dma_resv_test_signaled(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL) ? |
| 2044 | 0 : -EBUSY; |
| 2045 | |
| 2046 | lerr = dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL, |
| 2047 | ctx->interruptible, MAX_SCHEDULE_TIMEOUT); |
| 2048 | if (lerr < 0) |
| 2049 | return lerr; |
| 2050 | if (lerr == 0) |
| 2051 | return -EBUSY; |
| 2052 | |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
| 2056 | /* Populate the bo if swapped out, or migrate if the access mode requires that. */ |
| 2057 | static int xe_bo_fault_migrate(struct xe_bo *bo, struct ttm_operation_ctx *ctx, |
| 2058 | struct drm_exec *exec) |
| 2059 | { |
| 2060 | struct ttm_buffer_object *tbo = &bo->ttm; |
| 2061 | int err = 0; |
| 2062 | |
| 2063 | if (ttm_manager_type(tbo->bdev, tbo->resource->mem_type)->use_tt) { |
| 2064 | err = xe_bo_wait_usage_kernel(bo, ctx); |
| 2065 | if (!err) |
| 2066 | err = ttm_bo_populate(&bo->ttm, ctx); |
| 2067 | } else if (should_migrate_to_smem(bo)) { |
| 2068 | xe_assert(xe_bo_device(bo), bo->flags & XE_BO_FLAG_SYSTEM); |
| 2069 | err = xe_bo_migrate(bo, XE_PL_TT, ctx, exec); |
| 2070 | } |
| 2071 | |
| 2072 | return err; |
| 2073 | } |
| 2074 | |
| 2075 | /* Call into TTM to populate PTEs, and register bo for PTE removal on runtime suspend. */ |
| 2076 | static vm_fault_t __xe_bo_cpu_fault(struct vm_fault *vmf, struct xe_device *xe, struct xe_bo *bo) |
| 2077 | { |
| 2078 | vm_fault_t ret; |
| 2079 | |
| 2080 | trace_xe_bo_cpu_fault(bo); |
| 2081 | |
| 2082 | ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot, |
| 2083 | TTM_BO_VM_NUM_PREFAULT); |
| 2084 | /* |
| 2085 | * When TTM is actually called to insert PTEs, ensure no blocking conditions |
| 2086 | * remain, in which case TTM may drop locks and return VM_FAULT_RETRY. |
| 2087 | */ |
| 2088 | xe_assert(xe, ret != VM_FAULT_RETRY); |
| 2089 | |
| 2090 | if (ret == VM_FAULT_NOPAGE && |
| 2091 | mem_type_is_vram(bo->ttm.resource->mem_type)) { |
| 2092 | mutex_lock(&xe->mem_access.vram_userfault.lock); |
| 2093 | if (list_empty(&bo->vram_userfault_link)) |
| 2094 | list_add(&bo->vram_userfault_link, |
| 2095 | &xe->mem_access.vram_userfault.list); |
| 2096 | mutex_unlock(&xe->mem_access.vram_userfault.lock); |
| 2097 | } |
| 2098 | |
| 2099 | return ret; |
| 2100 | } |
| 2101 | |
| 2102 | static vm_fault_t xe_err_to_fault_t(int err) |
| 2103 | { |
| 2104 | switch (err) { |
| 2105 | case 0: |
| 2106 | case -EINTR: |
| 2107 | case -ERESTARTSYS: |
| 2108 | case -EAGAIN: |
| 2109 | return VM_FAULT_NOPAGE; |
| 2110 | case -ENOMEM: |
| 2111 | case -ENOSPC: |
| 2112 | return VM_FAULT_OOM; |
| 2113 | default: |
| 2114 | break; |
| 2115 | } |
| 2116 | return VM_FAULT_SIGBUS; |
| 2117 | } |
| 2118 | |
| 2119 | static vm_fault_t xe_bo_cpu_fault_fastpath(struct vm_fault *vmf, struct xe_device *xe, |
| 2120 | struct xe_bo *bo, bool needs_rpm) |
| 2121 | { |
| 2122 | struct ttm_buffer_object *tbo = &bo->ttm; |
| 2123 | vm_fault_t ret = VM_FAULT_RETRY; |
| 2124 | struct xe_validation_ctx ctx; |
| 2125 | struct ttm_operation_ctx tctx = { |
| 2126 | .interruptible = true, |
| 2127 | .no_wait_gpu = true, |
| 2128 | .gfp_retry_mayfail = true, |
| 2129 | |
| 2130 | }; |
| 2131 | int err; |
| 2132 | |
| 2133 | if (needs_rpm && !xe_pm_runtime_get_if_active(xe)) |
| 2134 | return VM_FAULT_RETRY; |
| 2135 | |
| 2136 | err = xe_validation_ctx_init(&ctx, &xe->val, NULL, |
| 2137 | (struct xe_val_flags) { |
| 2138 | .interruptible = true, |
| 2139 | .no_block = true |
| 2140 | }); |
| 2141 | if (err) |
| 2142 | goto out_pm; |
| 2143 | |
| 2144 | if (!dma_resv_trylock(tbo->base.resv)) |
| 2145 | goto out_validation; |
| 2146 | |
| 2147 | /* |
| 2148 | * Reject CPU faults to purgeable BOs. DONTNEED BOs can be purged |
| 2149 | * at any time, and purged BOs have no backing store. Either case |
| 2150 | * is undefined behavior for CPU access. |
| 2151 | */ |
| 2152 | if (xe_bo_madv_is_dontneed(bo) || xe_bo_is_purged(bo)) { |
| 2153 | ret = VM_FAULT_SIGBUS; |
| 2154 | goto out_unlock; |
| 2155 | } |
| 2156 | |
| 2157 | if (xe_ttm_bo_is_imported(tbo)) { |
| 2158 | ret = VM_FAULT_SIGBUS; |
| 2159 | drm_dbg(&xe->drm, "CPU trying to access an imported buffer object.\n"); |
| 2160 | goto out_unlock; |
| 2161 | } |
| 2162 | |
| 2163 | err = xe_bo_fault_migrate(bo, &tctx, NULL); |
| 2164 | if (err) { |
| 2165 | /* Return VM_FAULT_RETRY on these errors. */ |
| 2166 | if (err != -ENOMEM && err != -ENOSPC && err != -EBUSY) |
| 2167 | ret = xe_err_to_fault_t(err); |
| 2168 | goto out_unlock; |
| 2169 | } |
| 2170 | |
| 2171 | if (dma_resv_test_signaled(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL)) |
| 2172 | ret = __xe_bo_cpu_fault(vmf, xe, bo); |
| 2173 | |
| 2174 | out_unlock: |
| 2175 | dma_resv_unlock(tbo->base.resv); |
| 2176 | out_validation: |
| 2177 | xe_validation_ctx_fini(&ctx); |
| 2178 | out_pm: |
| 2179 | if (needs_rpm) |
| 2180 | xe_pm_runtime_put(xe); |
| 2181 | |
| 2182 | return ret; |
| 2183 | } |
| 2184 | |
| 2185 | static vm_fault_t xe_bo_cpu_fault(struct vm_fault *vmf) |
| 2186 | { |
| 2187 | struct ttm_buffer_object *tbo = vmf->vma->vm_private_data; |
| 2188 | struct drm_device *ddev = tbo->base.dev; |
| 2189 | struct xe_device *xe = to_xe_device(ddev); |
| 2190 | struct xe_bo *bo = ttm_to_xe_bo(tbo); |
| 2191 | bool needs_rpm = bo->flags & XE_BO_FLAG_VRAM_MASK; |
| 2192 | bool retry_after_wait = false; |
| 2193 | struct xe_validation_ctx ctx; |
| 2194 | struct drm_exec exec; |
| 2195 | vm_fault_t ret; |
| 2196 | int err = 0; |
| 2197 | int idx; |
| 2198 | |
| 2199 | if (xe_device_wedged(xe) || !drm_dev_enter(&xe->drm, &idx)) |
| 2200 | return ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot); |
| 2201 | |
| 2202 | ret = xe_bo_cpu_fault_fastpath(vmf, xe, bo, needs_rpm); |
| 2203 | if (ret != VM_FAULT_RETRY) |
| 2204 | goto out; |
| 2205 | |
| 2206 | if (fault_flag_allow_retry_first(vmf->flags)) { |
| 2207 | if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) |
| 2208 | goto out; |
| 2209 | retry_after_wait = true; |
| 2210 | xe_bo_get(bo); |
| 2211 | mmap_read_unlock(vmf->vma->vm_mm); |
| 2212 | } else { |
| 2213 | ret = VM_FAULT_NOPAGE; |
| 2214 | } |
| 2215 | |
| 2216 | /* |
| 2217 | * The fastpath failed and we were not required to return and retry immediately. |
| 2218 | * We're now running in one of two modes: |
| 2219 | * |
| 2220 | * 1) retry_after_wait == true: The mmap_read_lock() is dropped, and we're trying |
| 2221 | * to resolve blocking waits. But we can't resolve the fault since the |
| 2222 | * mmap_read_lock() is dropped. After retrying the fault, the aim is that the fastpath |
| 2223 | * should succeed. But it may fail since we drop the bo lock. |
| 2224 | * |
| 2225 | * 2) retry_after_wait == false: The fastpath failed, typically even after |
| 2226 | * a retry. Do whatever's necessary to resolve the fault. |
| 2227 | * |
| 2228 | * This construct is recommended to avoid excessive waits under the mmap_lock. |
| 2229 | */ |
| 2230 | |
| 2231 | if (needs_rpm) |
| 2232 | xe_pm_runtime_get(xe); |
| 2233 | |
| 2234 | xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = true}, |
| 2235 | err) { |
| 2236 | struct ttm_operation_ctx tctx = { |
| 2237 | .interruptible = true, |
| 2238 | .no_wait_gpu = false, |
| 2239 | .gfp_retry_mayfail = retry_after_wait, |
| 2240 | }; |
| 2241 | |
| 2242 | err = drm_exec_lock_obj(&exec, &tbo->base); |
| 2243 | drm_exec_retry_on_contention(&exec); |
| 2244 | if (err) |
| 2245 | break; |
| 2246 | |
| 2247 | /* |
| 2248 | * Reject CPU faults to purgeable BOs. DONTNEED BOs can be |
| 2249 | * purged at any time, and purged BOs have no backing store. |
| 2250 | */ |
| 2251 | if (xe_bo_madv_is_dontneed(bo) || xe_bo_is_purged(bo)) { |
| 2252 | err = -EFAULT; |
| 2253 | break; |
| 2254 | } |
| 2255 | |
| 2256 | if (xe_ttm_bo_is_imported(tbo)) { |
| 2257 | err = -EFAULT; |
| 2258 | drm_dbg(&xe->drm, "CPU trying to access an imported buffer object.\n"); |
| 2259 | break; |
| 2260 | } |
| 2261 | |
| 2262 | err = xe_bo_fault_migrate(bo, &tctx, &exec); |
| 2263 | if (err) { |
| 2264 | drm_exec_retry_on_contention(&exec); |
| 2265 | xe_validation_retry_on_oom(&ctx, &err); |
| 2266 | break; |
| 2267 | } |
| 2268 | |
| 2269 | err = xe_bo_wait_usage_kernel(bo, &tctx); |
| 2270 | if (err) |
| 2271 | break; |
| 2272 | |
| 2273 | if (!retry_after_wait) |
| 2274 | ret = __xe_bo_cpu_fault(vmf, xe, bo); |
| 2275 | } |
| 2276 | /* if retry_after_wait == true, we *must* return VM_FAULT_RETRY. */ |
| 2277 | if (err && !retry_after_wait) |
| 2278 | ret = xe_err_to_fault_t(err); |
| 2279 | |
| 2280 | if (needs_rpm) |
| 2281 | xe_pm_runtime_put(xe); |
| 2282 | |
| 2283 | if (retry_after_wait) |
| 2284 | xe_bo_put(bo); |
| 2285 | out: |
| 2286 | drm_dev_exit(idx); |
| 2287 | |
| 2288 | return ret; |
| 2289 | } |
| 2290 | |
| 2291 | static int xe_bo_vm_access(struct vm_area_struct *vma, unsigned long addr, |
| 2292 | void *buf, int len, int write) |
| 2293 | { |
| 2294 | struct ttm_buffer_object *ttm_bo = vma->vm_private_data; |
| 2295 | struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); |
| 2296 | struct xe_device *xe = xe_bo_device(bo); |
| 2297 | |
| 2298 | guard(xe_pm_runtime)(xe); |
| 2299 | return ttm_bo_vm_access(vma, addr, buf, len, write); |
| 2300 | } |
| 2301 | |
| 2302 | /** |
| 2303 | * xe_bo_read() - Read from an xe_bo |
| 2304 | * @bo: The buffer object to read from. |
| 2305 | * @offset: The byte offset to start reading from. |
| 2306 | * @dst: Location to store the read. |
| 2307 | * @size: Size in bytes for the read. |
| 2308 | * |
| 2309 | * Read @size bytes from the @bo, starting from @offset, storing into @dst. |
| 2310 | * |
| 2311 | * Return: Zero on success, or negative error. |
| 2312 | */ |
| 2313 | int xe_bo_read(struct xe_bo *bo, u64 offset, void *dst, int size) |
| 2314 | { |
| 2315 | int ret; |
| 2316 | |
| 2317 | ret = ttm_bo_access(&bo->ttm, offset, dst, size, 0); |
| 2318 | if (ret >= 0 && ret != size) |
| 2319 | ret = -EIO; |
| 2320 | else if (ret == size) |
| 2321 | ret = 0; |
| 2322 | |
| 2323 | return ret; |
| 2324 | } |
| 2325 | |
| 2326 | static const struct vm_operations_struct xe_gem_vm_ops = { |
| 2327 | .fault = xe_bo_cpu_fault, |
| 2328 | .open = ttm_bo_vm_open, |
| 2329 | .close = ttm_bo_vm_close, |
| 2330 | .access = xe_bo_vm_access, |
| 2331 | }; |
| 2332 | |
| 2333 | static int xe_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) |
| 2334 | { |
| 2335 | struct xe_bo *bo = gem_to_xe_bo(obj); |
| 2336 | int err = 0; |
| 2337 | |
| 2338 | /* |
| 2339 | * Reject mmap of purgeable BOs. DONTNEED BOs can be purged |
| 2340 | * at any time, making CPU access undefined behavior. Purged BOs have |
| 2341 | * no backing store and are permanently invalid. |
| 2342 | */ |
| 2343 | err = xe_bo_lock(bo, true); |
| 2344 | if (err) |
| 2345 | return err; |
| 2346 | |
| 2347 | if (xe_bo_madv_is_dontneed(bo)) |
| 2348 | err = -EBUSY; |
| 2349 | else if (xe_bo_is_purged(bo)) |
| 2350 | err = -EINVAL; |
| 2351 | xe_bo_unlock(bo); |
| 2352 | if (err) |
| 2353 | return err; |
| 2354 | |
| 2355 | return drm_gem_ttm_mmap(obj, vma); |
| 2356 | } |
| 2357 | |
| 2358 | static const struct drm_gem_object_funcs xe_gem_object_funcs = { |
| 2359 | .free = xe_gem_object_free, |
| 2360 | .close = xe_gem_object_close, |
| 2361 | .mmap = xe_gem_object_mmap, |
| 2362 | .export = xe_gem_prime_export, |
| 2363 | .vm_ops = &xe_gem_vm_ops, |
| 2364 | }; |
| 2365 | |
| 2366 | /** |
| 2367 | * xe_bo_alloc - Allocate storage for a struct xe_bo |
| 2368 | * |
| 2369 | * This function is intended to allocate storage to be used for input |
| 2370 | * to __xe_bo_create_locked(), in the case a pointer to the bo to be |
| 2371 | * created is needed before the call to __xe_bo_create_locked(). |
| 2372 | * If __xe_bo_create_locked ends up never to be called, then the |
| 2373 | * storage allocated with this function needs to be freed using |
| 2374 | * xe_bo_free(). |
| 2375 | * |
| 2376 | * Return: A pointer to an uninitialized struct xe_bo on success, |
| 2377 | * ERR_PTR(-ENOMEM) on error. |
| 2378 | */ |
| 2379 | struct xe_bo *xe_bo_alloc(void) |
| 2380 | { |
| 2381 | struct xe_bo *bo = kzalloc_obj(*bo); |
| 2382 | |
| 2383 | if (!bo) |
| 2384 | return ERR_PTR(-ENOMEM); |
| 2385 | |
| 2386 | return bo; |
| 2387 | } |
| 2388 | |
| 2389 | /** |
| 2390 | * xe_bo_free - Free storage allocated using xe_bo_alloc() |
| 2391 | * @bo: The buffer object storage. |
| 2392 | * |
| 2393 | * Refer to xe_bo_alloc() documentation for valid use-cases. |
| 2394 | */ |
| 2395 | void xe_bo_free(struct xe_bo *bo) |
| 2396 | { |
| 2397 | kfree(bo); |
| 2398 | } |
| 2399 | |
| 2400 | /** |
| 2401 | * xe_bo_init_locked() - Initialize or create an xe_bo. |
| 2402 | * @xe: The xe device. |
| 2403 | * @bo: An already allocated buffer object or NULL |
| 2404 | * if the function should allocate a new one. |
| 2405 | * @tile: The tile to select for migration of this bo, and the tile used for |
| 2406 | * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. |
| 2407 | * @resv: Pointer to a locked shared reservation object to use for this bo, |
| 2408 | * or NULL for the xe_bo to use its own. |
| 2409 | * @bulk: The bulk move to use for LRU bumping, or NULL for external bos. |
| 2410 | * @size: The storage size to use for the bo. |
| 2411 | * @cpu_caching: The cpu caching used for system memory backing store. |
| 2412 | * @type: The TTM buffer object type. |
| 2413 | * @flags: XE_BO_FLAG_ flags. |
| 2414 | * @dma_buf: The dma-buf to reference for the BO lifetime (imported BOs), |
| 2415 | * or NULL. |
| 2416 | * @exec: The drm_exec transaction to use for exhaustive eviction. |
| 2417 | * |
| 2418 | * Initialize or create an xe buffer object. On failure, any allocated buffer |
| 2419 | * object passed in @bo will have been unreferenced. |
| 2420 | * |
| 2421 | * Return: The buffer object on success. Negative error pointer on failure. |
| 2422 | */ |
| 2423 | struct xe_bo *xe_bo_init_locked(struct xe_device *xe, struct xe_bo *bo, |
| 2424 | struct xe_tile *tile, struct dma_resv *resv, |
| 2425 | struct ttm_lru_bulk_move *bulk, size_t size, |
| 2426 | u16 cpu_caching, enum ttm_bo_type type, |
| 2427 | u32 flags, struct dma_buf *dma_buf, |
| 2428 | struct drm_exec *exec) |
| 2429 | { |
| 2430 | struct ttm_operation_ctx ctx = { |
| 2431 | .interruptible = true, |
| 2432 | .no_wait_gpu = false, |
| 2433 | .gfp_retry_mayfail = true, |
| 2434 | }; |
| 2435 | struct ttm_placement *placement; |
| 2436 | uint32_t alignment; |
| 2437 | size_t aligned_size; |
| 2438 | int err; |
| 2439 | |
| 2440 | /* Only kernel objects should set GT */ |
| 2441 | xe_assert(xe, !tile || type == ttm_bo_type_kernel); |
| 2442 | |
| 2443 | if (XE_WARN_ON(!size)) { |
| 2444 | xe_bo_free(bo); |
| 2445 | return ERR_PTR(-EINVAL); |
| 2446 | } |
| 2447 | |
| 2448 | /* XE_BO_FLAG_GGTTx requires XE_BO_FLAG_GGTT also be set */ |
| 2449 | if ((flags & XE_BO_FLAG_GGTT_ALL) && !(flags & XE_BO_FLAG_GGTT)) { |
| 2450 | xe_bo_free(bo); |
| 2451 | return ERR_PTR(-EINVAL); |
| 2452 | } |
| 2453 | |
| 2454 | if (flags & (XE_BO_FLAG_VRAM_MASK | XE_BO_FLAG_STOLEN) && |
| 2455 | !(flags & XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE) && |
| 2456 | ((xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) || |
| 2457 | (flags & (XE_BO_FLAG_NEEDS_64K | XE_BO_FLAG_NEEDS_2M | |
| 2458 | XE_BO_FLAG_NEEDS_1G)))) { |
| 2459 | size_t align; |
| 2460 | |
| 2461 | if (flags & XE_BO_FLAG_NEEDS_1G) |
| 2462 | align = SZ_1G; |
| 2463 | else if (flags & XE_BO_FLAG_NEEDS_2M) |
| 2464 | align = SZ_2M; |
| 2465 | else |
| 2466 | align = SZ_64K; |
| 2467 | |
| 2468 | aligned_size = ALIGN(size, align); |
| 2469 | if (type != ttm_bo_type_device) |
| 2470 | size = ALIGN(size, align); |
| 2471 | flags |= XE_BO_FLAG_INTERNAL_64K; |
| 2472 | alignment = align >> PAGE_SHIFT; |
| 2473 | } else { |
| 2474 | aligned_size = ALIGN(size, SZ_4K); |
| 2475 | flags &= ~XE_BO_FLAG_INTERNAL_64K; |
| 2476 | alignment = SZ_4K >> PAGE_SHIFT; |
| 2477 | } |
| 2478 | |
| 2479 | if (type == ttm_bo_type_device && aligned_size != size) { |
| 2480 | xe_bo_free(bo); |
| 2481 | return ERR_PTR(-EINVAL); |
| 2482 | } |
| 2483 | |
| 2484 | if (!bo) { |
| 2485 | bo = xe_bo_alloc(); |
| 2486 | if (IS_ERR(bo)) |
| 2487 | return bo; |
| 2488 | } |
| 2489 | |
| 2490 | bo->ccs_cleared = false; |
| 2491 | bo->tile = tile; |
| 2492 | bo->flags = flags; |
| 2493 | bo->cpu_caching = cpu_caching; |
| 2494 | bo->ttm.base.funcs = &xe_gem_object_funcs; |
| 2495 | bo->ttm.priority = XE_BO_PRIORITY_NORMAL; |
| 2496 | INIT_LIST_HEAD(&bo->pinned_link); |
| 2497 | #ifdef CONFIG_PROC_FS |
| 2498 | INIT_LIST_HEAD(&bo->client_link); |
| 2499 | #endif |
| 2500 | INIT_LIST_HEAD(&bo->vram_userfault_link); |
| 2501 | |
| 2502 | /* Initialize purge advisory state */ |
| 2503 | bo->purgeable.state = XE_MADV_PURGEABLE_WILLNEED; |
| 2504 | |
| 2505 | drm_gem_private_object_init(&xe->drm, &bo->ttm.base, size); |
| 2506 | |
| 2507 | if (resv) { |
| 2508 | ctx.allow_res_evict = !(flags & XE_BO_FLAG_NO_RESV_EVICT); |
| 2509 | ctx.resv = resv; |
| 2510 | } |
| 2511 | |
| 2512 | xe_validation_assert_exec(xe, exec, &bo->ttm.base); |
| 2513 | if (!(flags & XE_BO_FLAG_FIXED_PLACEMENT)) { |
| 2514 | err = __xe_bo_placement_for_flags(xe, bo, bo->flags, type); |
| 2515 | if (WARN_ON(err)) { |
| 2516 | xe_ttm_bo_destroy(&bo->ttm); |
| 2517 | return ERR_PTR(err); |
| 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | /* Defer populating type_sg bos */ |
| 2522 | placement = (type == ttm_bo_type_sg || |
| 2523 | bo->flags & XE_BO_FLAG_DEFER_BACKING) ? &sys_placement : |
| 2524 | &bo->placement; |
| 2525 | |
| 2526 | /* |
| 2527 | * For imported BOs, keep the exporter dma-buf alive for the BO |
| 2528 | * lifetime. Taken before ttm_bo_init_reserved() to also cover a |
| 2529 | * creation failure there. Released in xe_ttm_bo_destroy(). |
| 2530 | */ |
| 2531 | if (dma_buf) { |
| 2532 | get_dma_buf(dma_buf); |
| 2533 | bo->dma_buf = dma_buf; |
| 2534 | } |
| 2535 | |
| 2536 | err = ttm_bo_init_reserved(&xe->ttm, &bo->ttm, type, |
| 2537 | placement, alignment, |
| 2538 | &ctx, NULL, resv, xe_ttm_bo_destroy); |
| 2539 | if (err) |
| 2540 | return ERR_PTR(err); |
| 2541 | |
| 2542 | /* |
| 2543 | * The VRAM pages underneath are potentially still being accessed by the |
| 2544 | * GPU, as per async GPU clearing and async evictions. However TTM makes |
| 2545 | * sure to add any corresponding move/clear fences into the objects |
| 2546 | * dma-resv using the DMA_RESV_USAGE_KERNEL slot. |
| 2547 | * |
| 2548 | * For KMD internal buffers we don't care about GPU clearing, however we |
| 2549 | * still need to handle async evictions, where the VRAM is still being |
| 2550 | * accessed by the GPU. Most internal callers are not expecting this, |
| 2551 | * since they are missing the required synchronisation before accessing |
| 2552 | * the memory. To keep things simple just sync wait any kernel fences |
| 2553 | * here, if the buffer is designated KMD internal. |
| 2554 | * |
| 2555 | * For normal userspace objects we should already have the required |
| 2556 | * pipelining or sync waiting elsewhere, since we already have to deal |
| 2557 | * with things like async GPU clearing. |
| 2558 | */ |
| 2559 | if (type == ttm_bo_type_kernel) { |
| 2560 | long timeout = dma_resv_wait_timeout(bo->ttm.base.resv, |
| 2561 | DMA_RESV_USAGE_KERNEL, |
| 2562 | ctx.interruptible, |
| 2563 | MAX_SCHEDULE_TIMEOUT); |
| 2564 | |
| 2565 | if (timeout < 0) { |
| 2566 | if (!resv) |
| 2567 | dma_resv_unlock(bo->ttm.base.resv); |
| 2568 | xe_bo_put(bo); |
| 2569 | return ERR_PTR(timeout); |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | bo->created = true; |
| 2574 | if (bulk) |
| 2575 | ttm_bo_set_bulk_move(&bo->ttm, bulk); |
| 2576 | else |
| 2577 | ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); |
| 2578 | |
| 2579 | return bo; |
| 2580 | } |
| 2581 | |
| 2582 | static int __xe_bo_fixed_placement(struct xe_device *xe, |
| 2583 | struct xe_bo *bo, enum ttm_bo_type type, |
| 2584 | u32 flags, |
| 2585 | u64 start, u64 end, u64 size) |
| 2586 | { |
| 2587 | struct ttm_place *place = bo->placements; |
| 2588 | u32 vram_flag, vram_stolen_flags; |
| 2589 | |
| 2590 | /* |
| 2591 | * to allow fixed placement in GGTT of a VF, post-migration fixups would have to |
| 2592 | * include selecting a new fixed offset and shifting the page ranges for it |
| 2593 | */ |
| 2594 | xe_assert(xe, !IS_SRIOV_VF(xe) || !(bo->flags & XE_BO_FLAG_GGTT)); |
| 2595 | |
| 2596 | if (flags & (XE_BO_FLAG_USER | XE_BO_FLAG_SYSTEM)) |
| 2597 | return -EINVAL; |
| 2598 | |
| 2599 | vram_flag = flags & XE_BO_FLAG_VRAM_MASK; |
| 2600 | vram_stolen_flags = (flags & (XE_BO_FLAG_STOLEN)) | vram_flag; |
| 2601 | |
| 2602 | /* check if more than one VRAM/STOLEN flag is set */ |
| 2603 | if (hweight32(vram_stolen_flags) > 1) |
| 2604 | return -EINVAL; |
| 2605 | |
| 2606 | place->flags = TTM_PL_FLAG_CONTIGUOUS; |
| 2607 | place->fpfn = start >> PAGE_SHIFT; |
| 2608 | place->lpfn = end >> PAGE_SHIFT; |
| 2609 | |
| 2610 | if (flags & XE_BO_FLAG_STOLEN) |
| 2611 | place->mem_type = XE_PL_STOLEN; |
| 2612 | else |
| 2613 | place->mem_type = bo_vram_flags_to_vram_placement(xe, flags, vram_flag, type); |
| 2614 | |
| 2615 | bo->placement = (struct ttm_placement) { |
| 2616 | .num_placement = 1, |
| 2617 | .placement = place, |
| 2618 | }; |
| 2619 | |
| 2620 | return 0; |
| 2621 | } |
| 2622 | |
| 2623 | static struct xe_bo * |
| 2624 | __xe_bo_create_locked(struct xe_device *xe, |
| 2625 | struct xe_tile *tile, struct xe_vm *vm, |
| 2626 | size_t size, u64 start, u64 end, |
| 2627 | u16 cpu_caching, enum ttm_bo_type type, u32 flags, |
| 2628 | u64 alignment, struct drm_exec *exec) |
| 2629 | { |
| 2630 | struct xe_bo *bo = NULL; |
| 2631 | int err; |
| 2632 | |
| 2633 | if (vm) |
| 2634 | xe_vm_assert_held(vm); |
| 2635 | |
| 2636 | if (start || end != ~0ULL) { |
| 2637 | bo = xe_bo_alloc(); |
| 2638 | if (IS_ERR(bo)) |
| 2639 | return bo; |
| 2640 | |
| 2641 | flags |= XE_BO_FLAG_FIXED_PLACEMENT; |
| 2642 | err = __xe_bo_fixed_placement(xe, bo, type, flags, start, end, size); |
| 2643 | if (err) { |
| 2644 | xe_bo_free(bo); |
| 2645 | return ERR_PTR(err); |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | bo = xe_bo_init_locked(xe, bo, tile, vm ? xe_vm_resv(vm) : NULL, |
| 2650 | vm && !xe_vm_in_fault_mode(vm) && |
| 2651 | flags & XE_BO_FLAG_USER ? |
| 2652 | &vm->lru_bulk_move : NULL, size, |
| 2653 | cpu_caching, type, flags, NULL, exec); |
| 2654 | if (IS_ERR(bo)) |
| 2655 | return bo; |
| 2656 | |
| 2657 | bo->min_align = alignment; |
| 2658 | |
| 2659 | /* |
| 2660 | * Note that instead of taking a reference no the drm_gpuvm_resv_bo(), |
| 2661 | * to ensure the shared resv doesn't disappear under the bo, the bo |
| 2662 | * will keep a reference to the vm, and avoid circular references |
| 2663 | * by having all the vm's bo refereferences released at vm close |
| 2664 | * time. |
| 2665 | */ |
| 2666 | if (vm && xe_bo_is_user(bo)) |
| 2667 | xe_vm_get(vm); |
| 2668 | bo->vm = vm; |
| 2669 | |
| 2670 | if (bo->flags & XE_BO_FLAG_GGTT) { |
| 2671 | struct xe_tile *t; |
| 2672 | u8 id; |
| 2673 | |
| 2674 | if (!(bo->flags & XE_BO_FLAG_GGTT_ALL)) { |
| 2675 | if (!tile && flags & XE_BO_FLAG_STOLEN) |
| 2676 | tile = xe_device_get_root_tile(xe); |
| 2677 | |
| 2678 | xe_assert(xe, tile); |
| 2679 | } |
| 2680 | |
| 2681 | for_each_tile(t, xe, id) { |
| 2682 | if (t != tile && !(bo->flags & XE_BO_FLAG_GGTTx(t))) |
| 2683 | continue; |
| 2684 | |
| 2685 | if (flags & XE_BO_FLAG_FIXED_PLACEMENT) { |
| 2686 | err = xe_ggtt_insert_bo_at(t->mem.ggtt, bo, |
| 2687 | start + xe_bo_size(bo), U64_MAX, |
| 2688 | exec); |
| 2689 | } else { |
| 2690 | err = xe_ggtt_insert_bo(t->mem.ggtt, bo, exec); |
| 2691 | } |
| 2692 | if (err) |
| 2693 | goto err_unlock_put_bo; |
| 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | trace_xe_bo_create(bo); |
| 2698 | return bo; |
| 2699 | |
| 2700 | err_unlock_put_bo: |
| 2701 | __xe_bo_unset_bulk_move(bo); |
| 2702 | xe_bo_unlock_vm_held(bo); |
| 2703 | xe_bo_put(bo); |
| 2704 | return ERR_PTR(err); |
| 2705 | } |
| 2706 | |
| 2707 | /** |
| 2708 | * xe_bo_create_locked() - Create a BO |
| 2709 | * @xe: The xe device. |
| 2710 | * @tile: The tile to select for migration of this bo, and the tile used for |
| 2711 | * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. |
| 2712 | * @vm: The local vm or NULL for external objects. |
| 2713 | * @size: The storage size to use for the bo. |
| 2714 | * @type: The TTM buffer object type. |
| 2715 | * @flags: XE_BO_FLAG_ flags. |
| 2716 | * @exec: The drm_exec transaction to use for exhaustive eviction. |
| 2717 | * |
| 2718 | * Create a locked xe BO with no range- nor alignment restrictions. |
| 2719 | * |
| 2720 | * Return: The buffer object on success. Negative error pointer on failure. |
| 2721 | */ |
| 2722 | struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile, |
| 2723 | struct xe_vm *vm, size_t size, |
| 2724 | enum ttm_bo_type type, u32 flags, |
| 2725 | struct drm_exec *exec) |
| 2726 | { |
| 2727 | return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, type, |
| 2728 | flags, 0, exec); |
| 2729 | } |
| 2730 | |
| 2731 | static struct xe_bo *xe_bo_create_novm(struct xe_device *xe, struct xe_tile *tile, |
| 2732 | size_t size, u16 cpu_caching, |
| 2733 | enum ttm_bo_type type, u32 flags, |
| 2734 | u64 alignment, bool intr) |
| 2735 | { |
| 2736 | struct xe_validation_ctx ctx; |
| 2737 | struct drm_exec exec; |
| 2738 | struct xe_bo *bo; |
| 2739 | int ret = 0; |
| 2740 | |
| 2741 | xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = intr}, |
| 2742 | ret) { |
| 2743 | bo = __xe_bo_create_locked(xe, tile, NULL, size, 0, ~0ULL, |
| 2744 | cpu_caching, type, flags, alignment, &exec); |
| 2745 | drm_exec_retry_on_contention(&exec); |
| 2746 | if (IS_ERR(bo)) { |
| 2747 | ret = PTR_ERR(bo); |
| 2748 | xe_validation_retry_on_oom(&ctx, &ret); |
| 2749 | } else { |
| 2750 | xe_bo_unlock(bo); |
| 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | return ret ? ERR_PTR(ret) : bo; |
| 2755 | } |
| 2756 | |
| 2757 | #ifdef CONFIG_DRM_XE_DEBUG_PAGE_SIZE |
| 2758 | static void xe_bo_debug_mixed_mode_cur_index_advance(struct xe_device *xe, struct xe_bo *bo) |
| 2759 | { |
| 2760 | if (!xe_debug_page_size_mode_is_mixed(xe)) |
| 2761 | return; |
| 2762 | |
| 2763 | if (!(bo->flags & XE_BO_FLAG_VRAM_MASK) || |
| 2764 | !(bo->flags & XE_BO_FLAG_USER)) |
| 2765 | return; |
| 2766 | |
| 2767 | mutex_lock(&xe->page_size_alloc_ctrl.lock); |
| 2768 | if (xe->page_size_alloc_ctrl.mode == XE_PAGE_SIZE_ALLOC_CTRL_MODE_MIXED) |
| 2769 | xe->page_size_alloc_ctrl.cur_index++; |
| 2770 | mutex_unlock(&xe->page_size_alloc_ctrl.lock); |
| 2771 | } |
| 2772 | |
| 2773 | static bool xe_size_align_overflows(size_t size, size_t align) |
| 2774 | { |
| 2775 | return size > SIZE_MAX - (align - 1); |
| 2776 | } |
| 2777 | |
| 2778 | static u32 get_flag_from_cur_index_in_mixed_mode(struct xe_device *xe, size_t *align_size, |
| 2779 | int *err) |
| 2780 | { |
| 2781 | static const struct { |
| 2782 | u32 flag; |
| 2783 | size_t align; |
| 2784 | } map[] = { |
| 2785 | { 0, SZ_4K }, /* default: 4K, no flag */ |
| 2786 | { XE_BO_FLAG_NEEDS_64K, SZ_64K }, |
| 2787 | { XE_BO_FLAG_NEEDS_2M, SZ_2M }, |
| 2788 | { XE_BO_FLAG_NEEDS_1G, SZ_1G }, |
| 2789 | }; |
| 2790 | u32 idx; |
| 2791 | const typeof(*map) *entry; |
| 2792 | |
| 2793 | lockdep_assert_held(&xe->page_size_alloc_ctrl.lock); |
| 2794 | |
| 2795 | *err = 0; |
| 2796 | idx = xe->page_size_alloc_ctrl.cur_index % ARRAY_SIZE(map); |
| 2797 | |
| 2798 | entry = &map[idx]; |
| 2799 | |
| 2800 | if (!entry->flag) |
| 2801 | return 0; |
| 2802 | |
| 2803 | if (xe_size_align_overflows(*align_size, entry->align)) { |
| 2804 | *err = -EINVAL; |
| 2805 | return 0; |
| 2806 | } |
| 2807 | *align_size = ALIGN(*align_size, entry->align); |
| 2808 | |
| 2809 | return entry->flag; |
| 2810 | } |
| 2811 | |
| 2812 | static int xe_bo_apply_debug_page_size_policy(struct xe_device *xe, |
| 2813 | u32 *bo_flags, |
| 2814 | size_t *size) |
| 2815 | { |
| 2816 | enum xe_page_size_alloc_ctrl_mode mode; |
| 2817 | u32 want = 0; |
| 2818 | size_t align_size = *size; |
| 2819 | int err = 0; |
| 2820 | |
| 2821 | /* |
| 2822 | * The debug page-size policy is only meaningful for BOs placed in |
| 2823 | * VRAM, where the downstream BO init path can |
| 2824 | * actually honor the corresponding minimum page-size requirement. |
| 2825 | */ |
| 2826 | if (!(*bo_flags & XE_BO_FLAG_VRAM_MASK)) |
| 2827 | return 0; |
| 2828 | |
| 2829 | /* |
| 2830 | * Do not override existing page-size requirement flags, since they |
| 2831 | * may reflect functional requirements for specific BO types. |
| 2832 | */ |
| 2833 | if (*bo_flags & (XE_BO_FLAG_NEEDS_64K | |
| 2834 | XE_BO_FLAG_NEEDS_2M | |
| 2835 | XE_BO_FLAG_NEEDS_1G)) |
| 2836 | return 0; |
| 2837 | |
| 2838 | if (!READ_ONCE(xe->page_size_alloc_ctrl.mode)) |
| 2839 | return 0; |
| 2840 | |
| 2841 | mutex_lock(&xe->page_size_alloc_ctrl.lock); |
| 2842 | |
| 2843 | mode = xe->page_size_alloc_ctrl.mode; |
| 2844 | if (mode == XE_PAGE_SIZE_ALLOC_CTRL_MODE_NONE) { |
| 2845 | goto out_unlock; |
| 2846 | } else if (mode == XE_PAGE_SIZE_ALLOC_CTRL_MODE_ONLY_2M) { |
| 2847 | if (xe_size_align_overflows(align_size, SZ_2M)) { |
| 2848 | err = -EINVAL; |
| 2849 | goto out_unlock; |
| 2850 | } |
| 2851 | want = XE_BO_FLAG_NEEDS_2M; |
| 2852 | align_size = ALIGN(align_size, SZ_2M); |
| 2853 | } else if (mode == XE_PAGE_SIZE_ALLOC_CTRL_MODE_ONLY_1G) { |
| 2854 | if (xe_size_align_overflows(align_size, SZ_1G)) { |
| 2855 | err = -EINVAL; |
| 2856 | goto out_unlock; |
| 2857 | } |
| 2858 | want = XE_BO_FLAG_NEEDS_1G; |
| 2859 | align_size = ALIGN(align_size, SZ_1G); |
| 2860 | } else if (mode == XE_PAGE_SIZE_ALLOC_CTRL_MODE_MIXED) { |
| 2861 | want = get_flag_from_cur_index_in_mixed_mode(xe, &align_size, &err); |
| 2862 | if (err) |
| 2863 | goto out_unlock; |
| 2864 | } else { |
| 2865 | goto out_unlock; |
| 2866 | } |
| 2867 | |
| 2868 | mutex_unlock(&xe->page_size_alloc_ctrl.lock); |
| 2869 | |
| 2870 | *bo_flags |= want; |
| 2871 | /* |
| 2872 | * Apply the debug page-size policy by rounding the user BO size up to |
| 2873 | * the selected granularity. |
| 2874 | */ |
| 2875 | *size = align_size; |
| 2876 | return err; |
| 2877 | |
| 2878 | out_unlock: |
| 2879 | mutex_unlock(&xe->page_size_alloc_ctrl.lock); |
| 2880 | return err; |
| 2881 | } |
| 2882 | #else |
| 2883 | static int xe_bo_apply_debug_page_size_policy(struct xe_device *xe, |
| 2884 | u32 *bo_flags, |
| 2885 | size_t *size) |
| 2886 | { |
| 2887 | return 0; |
| 2888 | } |
| 2889 | |
| 2890 | static void xe_bo_debug_mixed_mode_cur_index_advance(struct xe_device *xe, |
| 2891 | struct xe_bo *bo) |
| 2892 | { |
| 2893 | } |
| 2894 | #endif |
| 2895 | |
| 2896 | /** |
| 2897 | * xe_bo_create_user() - Create a user BO |
| 2898 | * @xe: The xe device. |
| 2899 | * @vm: The local vm or NULL for external objects. |
| 2900 | * @size: The storage size to use for the bo. |
| 2901 | * @cpu_caching: The caching mode to be used for system backing store. |
| 2902 | * @flags: XE_BO_FLAG_ flags. |
| 2903 | * @exec: The drm_exec transaction to use for exhaustive eviction, or NULL |
| 2904 | * if such a transaction should be initiated by the call. |
| 2905 | * |
| 2906 | * Create a bo on behalf of user-space. |
| 2907 | * |
| 2908 | * Return: The buffer object on success. Negative error pointer on failure. |
| 2909 | */ |
| 2910 | struct xe_bo *xe_bo_create_user(struct xe_device *xe, |
| 2911 | struct xe_vm *vm, size_t size, |
| 2912 | u16 cpu_caching, |
| 2913 | u32 flags, struct drm_exec *exec) |
| 2914 | { |
| 2915 | struct xe_bo *bo; |
| 2916 | int err = 0; |
| 2917 | |
| 2918 | flags |= XE_BO_FLAG_USER; |
| 2919 | |
| 2920 | if (xe_debug_page_size_mode_not_none(xe)) { |
| 2921 | err = xe_bo_apply_debug_page_size_policy(xe, &flags, &size); |
| 2922 | if (err) |
| 2923 | return ERR_PTR(err); |
| 2924 | } |
| 2925 | |
| 2926 | if (vm || exec) { |
| 2927 | xe_assert(xe, exec); |
| 2928 | bo = __xe_bo_create_locked(xe, NULL, vm, size, 0, ~0ULL, |
| 2929 | cpu_caching, ttm_bo_type_device, |
| 2930 | flags, 0, exec); |
| 2931 | if (!IS_ERR(bo)) |
| 2932 | xe_bo_unlock_vm_held(bo); |
| 2933 | } else { |
| 2934 | bo = xe_bo_create_novm(xe, NULL, size, cpu_caching, |
| 2935 | ttm_bo_type_device, flags, 0, true); |
| 2936 | } |
| 2937 | |
| 2938 | return bo; |
| 2939 | } |
| 2940 | |
| 2941 | /** |
| 2942 | * xe_bo_create_pin_range_novm() - Create and pin a BO with range options. |
| 2943 | * @xe: The xe device. |
| 2944 | * @tile: The tile to select for migration of this bo, and the tile used for |
| 2945 | * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. |
| 2946 | * @size: The storage size to use for the bo. |
| 2947 | * @start: Start of fixed VRAM range or 0. |
| 2948 | * @end: End of fixed VRAM range or ~0ULL. |
| 2949 | * @type: The TTM buffer object type. |
| 2950 | * @flags: XE_BO_FLAG_ flags. |
| 2951 | * |
| 2952 | * Create an Xe BO with range- and options. If @start and @end indicate |
| 2953 | * a fixed VRAM range, this must be a ttm_bo_type_kernel bo with VRAM placement |
| 2954 | * only. |
| 2955 | * |
| 2956 | * Return: The buffer object on success. Negative error pointer on failure. |
| 2957 | */ |
| 2958 | struct xe_bo *xe_bo_create_pin_range_novm(struct xe_device *xe, struct xe_tile *tile, |
| 2959 | size_t size, u64 start, u64 end, |
| 2960 | enum ttm_bo_type type, u32 flags) |
| 2961 | { |
| 2962 | struct xe_validation_ctx ctx; |
| 2963 | struct drm_exec exec; |
| 2964 | struct xe_bo *bo; |
| 2965 | int err = 0; |
| 2966 | |
| 2967 | xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {}, err) { |
| 2968 | bo = __xe_bo_create_locked(xe, tile, NULL, size, start, end, |
| 2969 | 0, type, flags, 0, &exec); |
| 2970 | if (IS_ERR(bo)) { |
| 2971 | drm_exec_retry_on_contention(&exec); |
| 2972 | err = PTR_ERR(bo); |
| 2973 | xe_validation_retry_on_oom(&ctx, &err); |
| 2974 | break; |
| 2975 | } |
| 2976 | |
| 2977 | err = xe_bo_pin(bo, &exec); |
| 2978 | xe_bo_unlock(bo); |
| 2979 | if (err) { |
| 2980 | xe_bo_put(bo); |
| 2981 | drm_exec_retry_on_contention(&exec); |
| 2982 | xe_validation_retry_on_oom(&ctx, &err); |
| 2983 | break; |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | return err ? ERR_PTR(err) : bo; |
| 2988 | } |
| 2989 | |
| 2990 | static struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe, |
| 2991 | struct xe_tile *tile, |
| 2992 | struct xe_vm *vm, |
| 2993 | size_t size, u64 offset, |
| 2994 | enum ttm_bo_type type, u32 flags, |
| 2995 | u64 alignment, struct drm_exec *exec) |
| 2996 | { |
| 2997 | struct xe_bo *bo; |
| 2998 | int err; |
| 2999 | u64 start = offset == ~0ull ? 0 : offset; |
| 3000 | u64 end = offset == ~0ull ? ~0ull : start + size; |
| 3001 | |
| 3002 | if (flags & XE_BO_FLAG_STOLEN && |
| 3003 | xe_ttm_stolen_cpu_access_needs_ggtt(xe)) |
| 3004 | flags |= XE_BO_FLAG_GGTT; |
| 3005 | |
| 3006 | bo = __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, type, |
| 3007 | flags | XE_BO_FLAG_NEEDS_CPU_ACCESS | XE_BO_FLAG_PINNED, |
| 3008 | alignment, exec); |
| 3009 | if (IS_ERR(bo)) |
| 3010 | return bo; |
| 3011 | |
| 3012 | err = xe_bo_pin(bo, exec); |
| 3013 | if (err) |
| 3014 | goto err_put; |
| 3015 | |
| 3016 | err = xe_bo_vmap(bo); |
| 3017 | if (err) |
| 3018 | goto err_unpin; |
| 3019 | |
| 3020 | xe_bo_unlock_vm_held(bo); |
| 3021 | |
| 3022 | return bo; |
| 3023 | |
| 3024 | err_unpin: |
| 3025 | xe_bo_unpin(bo); |
| 3026 | err_put: |
| 3027 | xe_bo_unlock_vm_held(bo); |
| 3028 | xe_bo_put(bo); |
| 3029 | return ERR_PTR(err); |
| 3030 | } |
| 3031 | |
| 3032 | /** |
| 3033 | * xe_bo_create_pin_map_at_novm() - Create pinned and mapped bo at optional VRAM offset |
| 3034 | * @xe: The xe device. |
| 3035 | * @tile: The tile to select for migration of this bo, and the tile used for |
| 3036 | * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. |
| 3037 | * @size: The storage size to use for the bo. |
| 3038 | * @offset: Optional VRAM offset or %~0ull for don't care. |
| 3039 | * @type: The TTM buffer object type. |
| 3040 | * @flags: XE_BO_FLAG_ flags. |
| 3041 | * @alignment: GGTT alignment. |
| 3042 | * @intr: Whether to execute any waits for backing store interruptible. |
| 3043 | * |
| 3044 | * Create a pinned and optionally mapped bo with VRAM offset and GGTT alignment |
| 3045 | * options. The bo will be external and not associated with a VM. |
| 3046 | * |
| 3047 | * Return: The buffer object on success. Negative error pointer on failure. |
| 3048 | * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set |
| 3049 | * to true on entry. |
| 3050 | */ |
| 3051 | struct xe_bo * |
| 3052 | xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile *tile, |
| 3053 | size_t size, u64 offset, enum ttm_bo_type type, u32 flags, |
| 3054 | u64 alignment, bool intr) |
| 3055 | { |
| 3056 | struct xe_validation_ctx ctx; |
| 3057 | struct drm_exec exec; |
| 3058 | struct xe_bo *bo; |
| 3059 | int ret = 0; |
| 3060 | |
| 3061 | xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = intr}, |
| 3062 | ret) { |
| 3063 | bo = xe_bo_create_pin_map_at_aligned(xe, tile, NULL, size, offset, |
| 3064 | type, flags, alignment, &exec); |
| 3065 | if (IS_ERR(bo)) { |
| 3066 | drm_exec_retry_on_contention(&exec); |
| 3067 | ret = PTR_ERR(bo); |
| 3068 | xe_validation_retry_on_oom(&ctx, &ret); |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | return ret ? ERR_PTR(ret) : bo; |
| 3073 | } |
| 3074 | |
| 3075 | /** |
| 3076 | * xe_bo_create_pin_map() - Create pinned and mapped bo |
| 3077 | * @xe: The xe device. |
| 3078 | * @tile: The tile to select for migration of this bo, and the tile used for |
| 3079 | * @vm: The vm to associate the buffer object with. The vm's resv must be locked |
| 3080 | * with the transaction represented by @exec. |
| 3081 | * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. |
| 3082 | * @size: The storage size to use for the bo. |
| 3083 | * @type: The TTM buffer object type. |
| 3084 | * @flags: XE_BO_FLAG_ flags. |
| 3085 | * @exec: The drm_exec transaction to use for exhaustive eviction, and |
| 3086 | * previously used for locking @vm's resv. |
| 3087 | * |
| 3088 | * Create a pinned and mapped bo. The bo will be external and not associated |
| 3089 | * with a VM. |
| 3090 | * |
| 3091 | * Return: The buffer object on success. Negative error pointer on failure. |
| 3092 | * In particular, the function may return ERR_PTR(%-EINTR) if @exec was |
| 3093 | * configured for interruptible locking. |
| 3094 | */ |
| 3095 | struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile, |
| 3096 | struct xe_vm *vm, size_t size, |
| 3097 | enum ttm_bo_type type, u32 flags, |
| 3098 | struct drm_exec *exec) |
| 3099 | { |
| 3100 | return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size, ~0ull, type, flags, |
| 3101 | 0, exec); |
| 3102 | } |
| 3103 | |
| 3104 | /** |
| 3105 | * xe_bo_create_pin_map_novm() - Create pinned and mapped bo |
| 3106 | * @xe: The xe device. |
| 3107 | * @tile: The tile to select for migration of this bo, and the tile used for |
| 3108 | * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. |
| 3109 | * @size: The storage size to use for the bo. |
| 3110 | * @type: The TTM buffer object type. |
| 3111 | * @flags: XE_BO_FLAG_ flags. |
| 3112 | * @intr: Whether to execute any waits for backing store interruptible. |
| 3113 | * |
| 3114 | * Create a pinned and mapped bo. The bo will be external and not associated |
| 3115 | * with a VM. |
| 3116 | * |
| 3117 | * Return: The buffer object on success. Negative error pointer on failure. |
| 3118 | * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set |
| 3119 | * to true on entry. |
| 3120 | */ |
| 3121 | struct xe_bo *xe_bo_create_pin_map_novm(struct xe_device *xe, struct xe_tile *tile, |
| 3122 | size_t size, enum ttm_bo_type type, u32 flags, |
| 3123 | bool intr) |
| 3124 | { |
| 3125 | return xe_bo_create_pin_map_at_novm(xe, tile, size, ~0ull, type, flags, 0, intr); |
| 3126 | } |
| 3127 | |
| 3128 | static void __xe_bo_unpin_map_no_vm(void *arg) |
| 3129 | { |
| 3130 | xe_bo_unpin_map_no_vm(arg); |
| 3131 | } |
| 3132 | |
| 3133 | struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile, |
| 3134 | size_t size, u32 flags) |
| 3135 | { |
| 3136 | struct xe_bo *bo; |
| 3137 | int ret; |
| 3138 | |
| 3139 | KUNIT_STATIC_STUB_REDIRECT(xe_managed_bo_create_pin_map, xe, tile, size, flags); |
| 3140 | bo = xe_bo_create_pin_map_novm(xe, tile, size, ttm_bo_type_kernel, flags, true); |
| 3141 | if (IS_ERR(bo)) |
| 3142 | return bo; |
| 3143 | |
| 3144 | ret = devm_add_action_or_reset(xe->drm.dev, __xe_bo_unpin_map_no_vm, bo); |
| 3145 | if (ret) |
| 3146 | return ERR_PTR(ret); |
| 3147 | |
| 3148 | return bo; |
| 3149 | } |
| 3150 | |
| 3151 | void xe_managed_bo_unpin_map_no_vm(struct xe_bo *bo) |
| 3152 | { |
| 3153 | devm_release_action(xe_bo_device(bo)->drm.dev, __xe_bo_unpin_map_no_vm, bo); |
| 3154 | } |
| 3155 | |
| 3156 | struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile, |
| 3157 | const void *data, size_t size, u32 flags) |
| 3158 | { |
| 3159 | struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, ALIGN(size, PAGE_SIZE), flags); |
| 3160 | |
| 3161 | if (IS_ERR(bo)) |
| 3162 | return bo; |
| 3163 | |
| 3164 | xe_map_memcpy_to(xe, &bo->vmap, 0, data, size); |
| 3165 | |
| 3166 | return bo; |
| 3167 | } |
| 3168 | |
| 3169 | /** |
| 3170 | * xe_managed_bo_reinit_in_vram |
| 3171 | * @xe: xe device |
| 3172 | * @tile: Tile where the new buffer will be created |
| 3173 | * @src: Managed buffer object allocated in system memory |
| 3174 | * |
| 3175 | * Replace a managed src buffer object allocated in system memory with a new |
| 3176 | * one allocated in vram, copying the data between them. |
| 3177 | * Buffer object in VRAM is not going to have the same GGTT address, the caller |
| 3178 | * is responsible for making sure that any old references to it are updated. |
| 3179 | * |
| 3180 | * Returns 0 for success, negative error code otherwise. |
| 3181 | */ |
| 3182 | int xe_managed_bo_reinit_in_vram(struct xe_device *xe, struct xe_tile *tile, struct xe_bo **src) |
| 3183 | { |
| 3184 | struct xe_bo *bo; |
| 3185 | u32 dst_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile) | XE_BO_FLAG_GGTT; |
| 3186 | |
| 3187 | dst_flags |= (*src)->flags & (XE_BO_FLAG_GGTT_INVALIDATE | |
| 3188 | XE_BO_FLAG_PINNED_NORESTORE); |
| 3189 | |
| 3190 | xe_assert(xe, IS_DGFX(xe)); |
| 3191 | xe_assert(xe, !(*src)->vmap.is_iomem); |
| 3192 | |
| 3193 | bo = xe_managed_bo_create_from_data(xe, tile, (*src)->vmap.vaddr, |
| 3194 | xe_bo_size(*src), dst_flags); |
| 3195 | if (IS_ERR(bo)) |
| 3196 | return PTR_ERR(bo); |
| 3197 | |
| 3198 | devm_release_action(xe->drm.dev, __xe_bo_unpin_map_no_vm, *src); |
| 3199 | *src = bo; |
| 3200 | |
| 3201 | return 0; |
| 3202 | } |
| 3203 | |
| 3204 | /* |
| 3205 | * XXX: This is in the VM bind data path, likely should calculate this once and |
| 3206 | * store, with a recalculation if the BO is moved. |
| 3207 | */ |
| 3208 | uint64_t vram_region_gpu_offset(struct ttm_resource *res) |
| 3209 | { |
| 3210 | struct xe_device *xe = ttm_to_xe_device(res->bo->bdev); |
| 3211 | |
| 3212 | switch (res->mem_type) { |
| 3213 | case XE_PL_STOLEN: |
| 3214 | return xe_ttm_stolen_gpu_offset(xe); |
| 3215 | case XE_PL_TT: |
| 3216 | case XE_PL_SYSTEM: |
| 3217 | return 0; |
| 3218 | default: |
| 3219 | return xe_map_resource_to_region(res)->dpa_base; |
| 3220 | } |
| 3221 | return 0; |
| 3222 | } |
| 3223 | |
| 3224 | /** |
| 3225 | * xe_bo_pin_external - pin an external BO |
| 3226 | * @bo: buffer object to be pinned |
| 3227 | * @in_place: Pin in current placement, don't attempt to migrate. |
| 3228 | * @exec: The drm_exec transaction to use for exhaustive eviction. |
| 3229 | * |
| 3230 | * Pin an external (not tied to a VM, can be exported via dma-buf / prime FD) |
| 3231 | * BO. Unique call compared to xe_bo_pin as this function has it own set of |
| 3232 | * asserts and code to ensure evict / restore on suspend / resume. |
| 3233 | * |
| 3234 | * Returns 0 for success, negative error code otherwise. |
| 3235 | */ |
| 3236 | int xe_bo_pin_external(struct xe_bo *bo, bool in_place, struct drm_exec *exec) |
| 3237 | { |
| 3238 | struct xe_device *xe = xe_bo_device(bo); |
| 3239 | bool first_pin = !xe_bo_is_pinned(bo); |
| 3240 | int err; |
| 3241 | |
| 3242 | xe_assert(xe, !bo->vm); |
| 3243 | xe_assert(xe, xe_bo_is_user(bo)); |
| 3244 | |
| 3245 | if (first_pin) { |
| 3246 | if (!in_place) { |
| 3247 | err = xe_bo_validate(bo, NULL, false, exec); |
| 3248 | if (err) |
| 3249 | return err; |
| 3250 | } |
| 3251 | |
| 3252 | spin_lock(&xe->pinned.lock); |
| 3253 | list_add_tail(&bo->pinned_link, &xe->pinned.late.external); |
| 3254 | spin_unlock(&xe->pinned.lock); |
| 3255 | } |
| 3256 | |
| 3257 | xe_bo_pin_account(bo); |
| 3258 | |
| 3259 | /* |
| 3260 | * FIXME: If we always use the reserve / unreserve functions for locking |
| 3261 | * we do not need this. |
| 3262 | */ |
| 3263 | ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); |
| 3264 | |
| 3265 | return 0; |
| 3266 | } |
| 3267 | |
| 3268 | /** |
| 3269 | * xe_bo_pin() - Pin a kernel bo after potentially migrating it |
| 3270 | * @bo: The kernel bo to pin. |
| 3271 | * @exec: The drm_exec transaction to use for exhaustive eviction. |
| 3272 | * |
| 3273 | * Attempts to migrate a bo to @bo->placement. If that succeeds, |
| 3274 | * pins the bo. |
| 3275 | * |
| 3276 | * Return: %0 on success, negative error code on migration failure. |
| 3277 | */ |
| 3278 | int xe_bo_pin(struct xe_bo *bo, struct drm_exec *exec) |
| 3279 | { |
| 3280 | struct ttm_place *place = &bo->placements[0]; |
| 3281 | struct xe_device *xe = xe_bo_device(bo); |
| 3282 | int err; |
| 3283 | |
| 3284 | /* We currently don't expect user BO to be pinned */ |
| 3285 | xe_assert(xe, !xe_bo_is_user(bo)); |
| 3286 | |
| 3287 | /* Pinned object must be in GGTT or have pinned flag */ |
| 3288 | xe_assert(xe, bo->flags & (XE_BO_FLAG_PINNED | |
| 3289 | XE_BO_FLAG_GGTT)); |
| 3290 | |
| 3291 | /* |
| 3292 | * No reason we can't support pinning imported dma-bufs we just don't |
| 3293 | * expect to pin an imported dma-buf. |
| 3294 | */ |
| 3295 | xe_assert(xe, !bo->ttm.base.import_attach); |
| 3296 | |
| 3297 | /* We only expect at most 1 pin */ |
| 3298 | xe_assert(xe, !xe_bo_is_pinned(bo)); |
| 3299 | |
| 3300 | err = xe_bo_validate(bo, NULL, false, exec); |
| 3301 | if (err) |
| 3302 | return err; |
| 3303 | |
| 3304 | if (mem_type_is_vram(place->mem_type) || bo->flags & XE_BO_FLAG_GGTT) { |
| 3305 | spin_lock(&xe->pinned.lock); |
| 3306 | if (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE) |
| 3307 | list_add_tail(&bo->pinned_link, &xe->pinned.late.kernel_bo_present); |
| 3308 | else |
| 3309 | list_add_tail(&bo->pinned_link, &xe->pinned.early.kernel_bo_present); |
| 3310 | spin_unlock(&xe->pinned.lock); |
| 3311 | } |
| 3312 | |
| 3313 | xe_bo_pin_account(bo); |
| 3314 | |
| 3315 | /* |
| 3316 | * FIXME: If we always use the reserve / unreserve functions for locking |
| 3317 | * we do not need this. |
| 3318 | */ |
| 3319 | ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); |
| 3320 | |
| 3321 | return 0; |
| 3322 | } |
| 3323 | |
| 3324 | /** |
| 3325 | * xe_bo_unpin_external - unpin an external BO |
| 3326 | * @bo: buffer object to be unpinned |
| 3327 | * |
| 3328 | * Unpin an external (not tied to a VM, can be exported via dma-buf / prime FD) |
| 3329 | * BO. Unique call compared to xe_bo_unpin as this function has it own set of |
| 3330 | * asserts and code to ensure evict / restore on suspend / resume. |
| 3331 | * |
| 3332 | * Returns 0 for success, negative error code otherwise. |
| 3333 | */ |
| 3334 | void xe_bo_unpin_external(struct xe_bo *bo) |
| 3335 | { |
| 3336 | struct xe_device *xe = xe_bo_device(bo); |
| 3337 | |
| 3338 | xe_assert(xe, !bo->vm); |
| 3339 | xe_assert(xe, xe_bo_is_pinned(bo)); |
| 3340 | xe_assert(xe, xe_bo_is_user(bo)); |
| 3341 | |
| 3342 | xe_bo_unpin_account(bo); |
| 3343 | |
| 3344 | /* |
| 3345 | * FIXME: If we always use the reserve / unreserve functions for locking |
| 3346 | * we do not need this. |
| 3347 | */ |
| 3348 | ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); |
| 3349 | } |
| 3350 | |
| 3351 | void xe_bo_unpin(struct xe_bo *bo) |
| 3352 | { |
| 3353 | struct ttm_place *place = &bo->placements[0]; |
| 3354 | struct xe_device *xe = xe_bo_device(bo); |
| 3355 | |
| 3356 | xe_assert(xe, !bo->ttm.base.import_attach); |
| 3357 | xe_assert(xe, xe_bo_is_pinned(bo)); |
| 3358 | |
| 3359 | if (mem_type_is_vram(place->mem_type) || bo->flags & XE_BO_FLAG_GGTT) { |
| 3360 | xe_assert(xe, !list_empty(&bo->pinned_link)); |
| 3361 | |
| 3362 | if (bo->backup_obj) { |
| 3363 | if (xe_bo_is_pinned(bo->backup_obj)) |
| 3364 | xe_bo_unpin_account(bo->backup_obj); |
| 3365 | xe_bo_put(bo->backup_obj); |
| 3366 | bo->backup_obj = NULL; |
| 3367 | } |
| 3368 | } |
| 3369 | xe_bo_unpin_account(bo); |
| 3370 | } |
| 3371 | |
| 3372 | /** |
| 3373 | * xe_bo_validate() - Make sure the bo is in an allowed placement |
| 3374 | * @bo: The bo, |
| 3375 | * @vm: Pointer to a the vm the bo shares a locked dma_resv object with, or |
| 3376 | * NULL. Used together with @allow_res_evict. |
| 3377 | * @allow_res_evict: Whether it's allowed to evict bos sharing @vm's |
| 3378 | * reservation object. |
| 3379 | * @exec: The drm_exec transaction to use for exhaustive eviction. |
| 3380 | * |
| 3381 | * Make sure the bo is in allowed placement, migrating it if necessary. If |
| 3382 | * needed, other bos will be evicted. If bos selected for eviction shares |
| 3383 | * the @vm's reservation object, they can be evicted iff @allow_res_evict is |
| 3384 | * set to true, otherwise they will be bypassed. |
| 3385 | * |
| 3386 | * Return: 0 on success, negative error code on failure. May return |
| 3387 | * -EINTR or -ERESTARTSYS if internal waits are interrupted by a signal. |
| 3388 | */ |
| 3389 | int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict, |
| 3390 | struct drm_exec *exec) |
| 3391 | { |
| 3392 | struct ttm_operation_ctx ctx = { |
| 3393 | .interruptible = true, |
| 3394 | .no_wait_gpu = false, |
| 3395 | .gfp_retry_mayfail = true, |
| 3396 | }; |
| 3397 | int ret; |
| 3398 | |
| 3399 | if (xe_bo_is_pinned(bo)) |
| 3400 | return 0; |
| 3401 | |
| 3402 | if (vm) { |
| 3403 | lockdep_assert_held(&vm->lock); |
| 3404 | xe_vm_assert_held(vm); |
| 3405 | |
| 3406 | ctx.allow_res_evict = allow_res_evict; |
| 3407 | ctx.resv = xe_vm_resv(vm); |
| 3408 | } |
| 3409 | |
| 3410 | xe_vm_set_validating(vm, allow_res_evict); |
| 3411 | trace_xe_bo_validate(bo); |
| 3412 | xe_validation_assert_exec(xe_bo_device(bo), exec, &bo->ttm.base); |
| 3413 | ret = ttm_bo_validate(&bo->ttm, &bo->placement, &ctx); |
| 3414 | xe_vm_clear_validating(vm, allow_res_evict); |
| 3415 | |
| 3416 | return ret; |
| 3417 | } |
| 3418 | |
| 3419 | bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo) |
| 3420 | { |
| 3421 | if (bo->destroy == &xe_ttm_bo_destroy) |
| 3422 | return true; |
| 3423 | |
| 3424 | return false; |
| 3425 | } |
| 3426 | |
| 3427 | /* |
| 3428 | * Resolve a BO address. There is no assert to check if the proper lock is held |
| 3429 | * so it should only be used in cases where it is not fatal to get the wrong |
| 3430 | * address, such as printing debug information, but not in cases where memory is |
| 3431 | * written based on this result. |
| 3432 | */ |
| 3433 | dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size) |
| 3434 | { |
| 3435 | struct xe_device *xe = xe_bo_device(bo); |
| 3436 | struct xe_res_cursor cur; |
| 3437 | u64 page; |
| 3438 | |
| 3439 | xe_assert(xe, page_size <= PAGE_SIZE); |
| 3440 | page = offset >> PAGE_SHIFT; |
| 3441 | offset &= (PAGE_SIZE - 1); |
| 3442 | |
| 3443 | if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) { |
| 3444 | xe_assert(xe, bo->ttm.ttm); |
| 3445 | |
| 3446 | xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT, |
| 3447 | page_size, &cur); |
| 3448 | return xe_res_dma(&cur) + offset; |
| 3449 | } else { |
| 3450 | struct xe_res_cursor cur; |
| 3451 | |
| 3452 | xe_res_first(bo->ttm.resource, page << PAGE_SHIFT, |
| 3453 | page_size, &cur); |
| 3454 | return cur.start + offset + vram_region_gpu_offset(bo->ttm.resource); |
| 3455 | } |
| 3456 | } |
| 3457 | |
| 3458 | dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size) |
| 3459 | { |
| 3460 | if (!READ_ONCE(bo->ttm.pin_count)) |
| 3461 | xe_bo_assert_held(bo); |
| 3462 | return __xe_bo_addr(bo, offset, page_size); |
| 3463 | } |
| 3464 | |
| 3465 | int xe_bo_vmap(struct xe_bo *bo) |
| 3466 | { |
| 3467 | struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); |
| 3468 | void *virtual; |
| 3469 | bool is_iomem; |
| 3470 | int ret; |
| 3471 | |
| 3472 | xe_bo_assert_held(bo); |
| 3473 | |
| 3474 | if (drm_WARN_ON(&xe->drm, !(bo->flags & XE_BO_FLAG_NEEDS_CPU_ACCESS) || |
| 3475 | !force_contiguous(bo->flags))) |
| 3476 | return -EINVAL; |
| 3477 | |
| 3478 | if (!iosys_map_is_null(&bo->vmap)) |
| 3479 | return 0; |
| 3480 | |
| 3481 | /* |
| 3482 | * We use this more or less deprecated interface for now since |
| 3483 | * ttm_bo_vmap() doesn't offer the optimization of kmapping |
| 3484 | * single page bos, which is done here. |
| 3485 | * TODO: Fix up ttm_bo_vmap to do that, or fix up ttm_bo_kmap |
| 3486 | * to use struct iosys_map. |
| 3487 | */ |
| 3488 | ret = ttm_bo_kmap(&bo->ttm, 0, xe_bo_size(bo) >> PAGE_SHIFT, &bo->kmap); |
| 3489 | if (ret) |
| 3490 | return ret; |
| 3491 | |
| 3492 | virtual = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem); |
| 3493 | if (is_iomem) |
| 3494 | iosys_map_set_vaddr_iomem(&bo->vmap, (void __iomem *)virtual); |
| 3495 | else |
| 3496 | iosys_map_set_vaddr(&bo->vmap, virtual); |
| 3497 | |
| 3498 | return 0; |
| 3499 | } |
| 3500 | |
| 3501 | static void __xe_bo_vunmap(struct xe_bo *bo) |
| 3502 | { |
| 3503 | if (!iosys_map_is_null(&bo->vmap)) { |
| 3504 | iosys_map_clear(&bo->vmap); |
| 3505 | ttm_bo_kunmap(&bo->kmap); |
| 3506 | } |
| 3507 | } |
| 3508 | |
| 3509 | void xe_bo_vunmap(struct xe_bo *bo) |
| 3510 | { |
| 3511 | xe_bo_assert_held(bo); |
| 3512 | __xe_bo_vunmap(bo); |
| 3513 | } |
| 3514 | |
| 3515 | static int gem_create_set_pxp_type(struct xe_device *xe, struct xe_bo *bo, u64 value) |
| 3516 | { |
| 3517 | if (value == DRM_XE_PXP_TYPE_NONE) |
| 3518 | return 0; |
| 3519 | |
| 3520 | /* we only support DRM_XE_PXP_TYPE_HWDRM for now */ |
| 3521 | if (XE_IOCTL_DBG(xe, value != DRM_XE_PXP_TYPE_HWDRM)) |
| 3522 | return -EINVAL; |
| 3523 | |
| 3524 | return xe_pxp_key_assign(xe->pxp, bo); |
| 3525 | } |
| 3526 | |
| 3527 | typedef int (*xe_gem_create_set_property_fn)(struct xe_device *xe, |
| 3528 | struct xe_bo *bo, |
| 3529 | u64 value); |
| 3530 | |
| 3531 | static const xe_gem_create_set_property_fn gem_create_set_property_funcs[] = { |
| 3532 | [DRM_XE_GEM_CREATE_SET_PROPERTY_PXP_TYPE] = gem_create_set_pxp_type, |
| 3533 | }; |
| 3534 | |
| 3535 | static int gem_create_user_ext_set_property(struct xe_device *xe, |
| 3536 | struct xe_bo *bo, |
| 3537 | u64 extension) |
| 3538 | { |
| 3539 | u64 __user *address = u64_to_user_ptr(extension); |
| 3540 | struct drm_xe_ext_set_property ext; |
| 3541 | int err; |
| 3542 | u32 idx; |
| 3543 | |
| 3544 | err = copy_from_user(&ext, address, sizeof(ext)); |
| 3545 | if (XE_IOCTL_DBG(xe, err)) |
| 3546 | return -EFAULT; |
| 3547 | |
| 3548 | if (XE_IOCTL_DBG(xe, ext.property >= |
| 3549 | ARRAY_SIZE(gem_create_set_property_funcs)) || |
| 3550 | XE_IOCTL_DBG(xe, ext.pad) || |
| 3551 | XE_IOCTL_DBG(xe, ext.property != DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY)) |
| 3552 | return -EINVAL; |
| 3553 | |
| 3554 | idx = array_index_nospec(ext.property, ARRAY_SIZE(gem_create_set_property_funcs)); |
| 3555 | if (!gem_create_set_property_funcs[idx]) |
| 3556 | return -EINVAL; |
| 3557 | |
| 3558 | return gem_create_set_property_funcs[idx](xe, bo, ext.value); |
| 3559 | } |
| 3560 | |
| 3561 | typedef int (*xe_gem_create_user_extension_fn)(struct xe_device *xe, |
| 3562 | struct xe_bo *bo, |
| 3563 | u64 extension); |
| 3564 | |
| 3565 | static const xe_gem_create_user_extension_fn gem_create_user_extension_funcs[] = { |
| 3566 | [DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY] = gem_create_user_ext_set_property, |
| 3567 | }; |
| 3568 | |
| 3569 | #define MAX_USER_EXTENSIONS 16 |
| 3570 | static int gem_create_user_extensions(struct xe_device *xe, struct xe_bo *bo, |
| 3571 | u64 extensions, int ext_number) |
| 3572 | { |
| 3573 | u64 __user *address = u64_to_user_ptr(extensions); |
| 3574 | struct drm_xe_user_extension ext; |
| 3575 | int err; |
| 3576 | u32 idx; |
| 3577 | |
| 3578 | if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS)) |
| 3579 | return -E2BIG; |
| 3580 | |
| 3581 | err = copy_from_user(&ext, address, sizeof(ext)); |
| 3582 | if (XE_IOCTL_DBG(xe, err)) |
| 3583 | return -EFAULT; |
| 3584 | |
| 3585 | if (XE_IOCTL_DBG(xe, ext.pad) || |
| 3586 | XE_IOCTL_DBG(xe, ext.name >= ARRAY_SIZE(gem_create_user_extension_funcs))) |
| 3587 | return -EINVAL; |
| 3588 | |
| 3589 | idx = array_index_nospec(ext.name, |
| 3590 | ARRAY_SIZE(gem_create_user_extension_funcs)); |
| 3591 | err = gem_create_user_extension_funcs[idx](xe, bo, extensions); |
| 3592 | if (XE_IOCTL_DBG(xe, err)) |
| 3593 | return err; |
| 3594 | |
| 3595 | if (ext.next_extension) |
| 3596 | return gem_create_user_extensions(xe, bo, ext.next_extension, |
| 3597 | ++ext_number); |
| 3598 | |
| 3599 | return 0; |
| 3600 | } |
| 3601 | |
| 3602 | int xe_gem_create_ioctl(struct drm_device *dev, void *data, |
| 3603 | struct drm_file *file) |
| 3604 | { |
| 3605 | struct xe_device *xe = to_xe_device(dev); |
| 3606 | struct xe_file *xef = to_xe_file(file); |
| 3607 | struct drm_xe_gem_create *args = data; |
| 3608 | struct xe_validation_ctx ctx; |
| 3609 | struct drm_exec exec; |
| 3610 | struct xe_vm *vm = NULL; |
| 3611 | struct xe_bo *bo; |
| 3612 | unsigned int bo_flags; |
| 3613 | u32 handle; |
| 3614 | int err; |
| 3615 | |
| 3616 | if (XE_IOCTL_DBG(xe, args->pad[0] || args->pad[1] || args->pad[2]) || |
| 3617 | XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) |
| 3618 | return -EINVAL; |
| 3619 | |
| 3620 | /* at least one valid memory placement must be specified */ |
| 3621 | if (XE_IOCTL_DBG(xe, (args->placement & ~xe->info.mem_region_mask) || |
| 3622 | !args->placement)) |
| 3623 | return -EINVAL; |
| 3624 | |
| 3625 | if (XE_IOCTL_DBG(xe, args->flags & |
| 3626 | ~(DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING | |
| 3627 | DRM_XE_GEM_CREATE_FLAG_SCANOUT | |
| 3628 | DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM | |
| 3629 | DRM_XE_GEM_CREATE_FLAG_NO_COMPRESSION))) |
| 3630 | return -EINVAL; |
| 3631 | |
| 3632 | if (XE_IOCTL_DBG(xe, args->handle)) |
| 3633 | return -EINVAL; |
| 3634 | |
| 3635 | if (XE_IOCTL_DBG(xe, !args->size)) |
| 3636 | return -EINVAL; |
| 3637 | |
| 3638 | if (XE_IOCTL_DBG(xe, args->size > SIZE_MAX)) |
| 3639 | return -EINVAL; |
| 3640 | |
| 3641 | if (XE_IOCTL_DBG(xe, args->size & ~PAGE_MASK)) |
| 3642 | return -EINVAL; |
| 3643 | |
| 3644 | bo_flags = 0; |
| 3645 | if (args->flags & DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING) |
| 3646 | bo_flags |= XE_BO_FLAG_DEFER_BACKING; |
| 3647 | |
| 3648 | /* |
| 3649 | * Display scanout is always non-coherent with the CPU cache. |
| 3650 | */ |
| 3651 | if (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT) |
| 3652 | bo_flags |= XE_BO_FLAG_FORCE_WC; |
| 3653 | |
| 3654 | if (args->flags & DRM_XE_GEM_CREATE_FLAG_NO_COMPRESSION) { |
| 3655 | if (XE_IOCTL_DBG(xe, GRAPHICS_VER(xe) < 20)) |
| 3656 | return -EOPNOTSUPP; |
| 3657 | bo_flags |= XE_BO_FLAG_NO_COMPRESSION; |
| 3658 | } |
| 3659 | |
| 3660 | bo_flags |= args->placement << (ffs(XE_BO_FLAG_SYSTEM) - 1); |
| 3661 | |
| 3662 | /* CCS formats need physical placement at a 64K alignment in VRAM. */ |
| 3663 | if ((bo_flags & XE_BO_FLAG_VRAM_MASK) && |
| 3664 | (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT) && |
| 3665 | !(xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) && |
| 3666 | IS_ALIGNED(args->size, SZ_64K)) |
| 3667 | bo_flags |= XE_BO_FLAG_NEEDS_64K; |
| 3668 | |
| 3669 | if (args->flags & DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM) { |
| 3670 | if (XE_IOCTL_DBG(xe, !(bo_flags & XE_BO_FLAG_VRAM_MASK))) |
| 3671 | return -EINVAL; |
| 3672 | |
| 3673 | bo_flags |= XE_BO_FLAG_NEEDS_CPU_ACCESS; |
| 3674 | } |
| 3675 | |
| 3676 | if (XE_IOCTL_DBG(xe, !args->cpu_caching || |
| 3677 | args->cpu_caching > DRM_XE_GEM_CPU_CACHING_WC)) |
| 3678 | return -EINVAL; |
| 3679 | |
| 3680 | if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_VRAM_MASK && |
| 3681 | args->cpu_caching != DRM_XE_GEM_CPU_CACHING_WC)) |
| 3682 | return -EINVAL; |
| 3683 | |
| 3684 | if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_FORCE_WC && |
| 3685 | args->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB)) |
| 3686 | return -EINVAL; |
| 3687 | |
| 3688 | if (args->vm_id) { |
| 3689 | vm = xe_vm_lookup(xef, args->vm_id); |
| 3690 | if (XE_IOCTL_DBG(xe, !vm)) |
| 3691 | return -ENOENT; |
| 3692 | } |
| 3693 | |
| 3694 | err = 0; |
| 3695 | xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = true}, |
| 3696 | err) { |
| 3697 | if (vm) { |
| 3698 | err = xe_vm_drm_exec_lock(vm, &exec); |
| 3699 | drm_exec_retry_on_contention(&exec); |
| 3700 | if (err) |
| 3701 | break; |
| 3702 | } |
| 3703 | bo = xe_bo_create_user(xe, vm, args->size, args->cpu_caching, |
| 3704 | bo_flags, &exec); |
| 3705 | drm_exec_retry_on_contention(&exec); |
| 3706 | if (IS_ERR(bo)) { |
| 3707 | err = PTR_ERR(bo); |
| 3708 | xe_validation_retry_on_oom(&ctx, &err); |
| 3709 | break; |
| 3710 | } |
| 3711 | } |
| 3712 | if (err) |
| 3713 | goto out_vm; |
| 3714 | |
| 3715 | if (args->extensions) { |
| 3716 | err = gem_create_user_extensions(xe, bo, args->extensions, 0); |
| 3717 | if (err) |
| 3718 | goto out_bulk; |
| 3719 | } |
| 3720 | |
| 3721 | err = drm_gem_handle_create(file, &bo->ttm.base, &handle); |
| 3722 | if (err) |
| 3723 | goto out_bulk; |
| 3724 | |
| 3725 | xe_bo_debug_mixed_mode_cur_index_advance(xe, bo); |
| 3726 | |
| 3727 | args->handle = handle; |
| 3728 | goto out_put; |
| 3729 | |
| 3730 | out_bulk: |
| 3731 | if (vm && !xe_vm_in_fault_mode(vm)) { |
| 3732 | xe_vm_lock(vm, false); |
| 3733 | __xe_bo_unset_bulk_move(bo); |
| 3734 | xe_vm_unlock(vm); |
| 3735 | } |
| 3736 | out_put: |
| 3737 | xe_bo_put(bo); |
| 3738 | out_vm: |
| 3739 | if (vm) |
| 3740 | xe_vm_put(vm); |
| 3741 | |
| 3742 | return err; |
| 3743 | } |
| 3744 | |
| 3745 | int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data, |
| 3746 | struct drm_file *file) |
| 3747 | { |
| 3748 | struct xe_device *xe = to_xe_device(dev); |
| 3749 | struct drm_xe_gem_mmap_offset *args = data; |
| 3750 | struct drm_gem_object *gem_obj; |
| 3751 | |
| 3752 | if (XE_IOCTL_DBG(xe, args->extensions) || |
| 3753 | XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) |
| 3754 | return -EINVAL; |
| 3755 | |
| 3756 | if (XE_IOCTL_DBG(xe, args->flags & |
| 3757 | ~DRM_XE_MMAP_OFFSET_FLAG_PCI_BARRIER)) |
| 3758 | return -EINVAL; |
| 3759 | |
| 3760 | if (args->flags & DRM_XE_MMAP_OFFSET_FLAG_PCI_BARRIER) { |
| 3761 | if (XE_IOCTL_DBG(xe, !IS_DGFX(xe))) |
| 3762 | return -EINVAL; |
| 3763 | |
| 3764 | if (XE_IOCTL_DBG(xe, args->handle)) |
| 3765 | return -EINVAL; |
| 3766 | |
| 3767 | if (XE_IOCTL_DBG(xe, PAGE_SIZE > SZ_4K)) |
| 3768 | return -EINVAL; |
| 3769 | |
| 3770 | BUILD_BUG_ON(((XE_PCI_BARRIER_MMAP_OFFSET >> XE_PTE_SHIFT) + |
| 3771 | SZ_4K) >= DRM_FILE_PAGE_OFFSET_START); |
| 3772 | args->offset = XE_PCI_BARRIER_MMAP_OFFSET; |
| 3773 | return 0; |
| 3774 | } |
| 3775 | |
| 3776 | gem_obj = drm_gem_object_lookup(file, args->handle); |
| 3777 | if (XE_IOCTL_DBG(xe, !gem_obj)) |
| 3778 | return -ENOENT; |
| 3779 | |
| 3780 | /* The mmap offset was set up at BO allocation time. */ |
| 3781 | args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node); |
| 3782 | |
| 3783 | xe_bo_put(gem_to_xe_bo(gem_obj)); |
| 3784 | return 0; |
| 3785 | } |
| 3786 | |
| 3787 | /** |
| 3788 | * xe_bo_decompress - schedule in-place decompress and install fence |
| 3789 | * @bo: buffer object (caller should hold drm_exec reservations for VM+BO) |
| 3790 | * |
| 3791 | * Schedules an in-place resolve via the migrate layer and installs the |
| 3792 | * returned dma_fence into the BO kernel reservation slot (DMA_RESV_USAGE_KERNEL). |
| 3793 | * In preempt fence mode, this operation interrupts hardware execution |
| 3794 | * which is expensive. Page fault mode is recommended for better performance. |
| 3795 | * |
| 3796 | * The resolve path only runs for VRAM-backed buffers (currently dGPU-only); |
| 3797 | * iGPU/system-memory objects fail the resource check and bypass the resolve. |
| 3798 | * |
| 3799 | * Returns 0 on success, negative errno on error. |
| 3800 | */ |
| 3801 | int xe_bo_decompress(struct xe_bo *bo) |
| 3802 | { |
| 3803 | struct xe_device *xe = xe_bo_device(bo); |
| 3804 | struct xe_tile *tile = xe_device_get_root_tile(xe); |
| 3805 | struct dma_fence *decomp_fence = NULL; |
| 3806 | struct ttm_operation_ctx op_ctx = { |
| 3807 | .interruptible = true, |
| 3808 | .no_wait_gpu = false, |
| 3809 | .gfp_retry_mayfail = false, |
| 3810 | }; |
| 3811 | int err = 0; |
| 3812 | |
| 3813 | /* Silently skip decompression for non-VRAM buffers */ |
| 3814 | if (!bo->ttm.resource || !mem_type_is_vram(bo->ttm.resource->mem_type)) |
| 3815 | return 0; |
| 3816 | |
| 3817 | /* Notify before scheduling resolve */ |
| 3818 | err = xe_bo_move_notify(bo, &op_ctx); |
| 3819 | if (err) |
| 3820 | return err; |
| 3821 | |
| 3822 | /* Reserve fence slot before scheduling */ |
| 3823 | err = dma_resv_reserve_fences(bo->ttm.base.resv, 1); |
| 3824 | if (err) |
| 3825 | return err; |
| 3826 | |
| 3827 | /* Schedule the in-place decompression */ |
| 3828 | decomp_fence = xe_migrate_resolve(tile->migrate, |
| 3829 | bo, |
| 3830 | bo->ttm.resource); |
| 3831 | |
| 3832 | if (IS_ERR(decomp_fence)) |
| 3833 | return PTR_ERR(decomp_fence); |
| 3834 | |
| 3835 | /* Install kernel-usage fence */ |
| 3836 | dma_resv_add_fence(bo->ttm.base.resv, decomp_fence, DMA_RESV_USAGE_KERNEL); |
| 3837 | dma_fence_put(decomp_fence); |
| 3838 | |
| 3839 | return 0; |
| 3840 | } |
| 3841 | |
| 3842 | /** |
| 3843 | * xe_bo_lock() - Lock the buffer object's dma_resv object |
| 3844 | * @bo: The struct xe_bo whose lock is to be taken |
| 3845 | * @intr: Whether to perform any wait interruptible |
| 3846 | * |
| 3847 | * Locks the buffer object's dma_resv object. If the buffer object is |
| 3848 | * pointing to a shared dma_resv object, that shared lock is locked. |
| 3849 | * |
| 3850 | * Return: 0 on success, -EINTR if @intr is true and the wait for a |
| 3851 | * contended lock was interrupted. If @intr is set to false, the |
| 3852 | * function always returns 0. |
| 3853 | */ |
| 3854 | int xe_bo_lock(struct xe_bo *bo, bool intr) |
| 3855 | { |
| 3856 | if (intr) |
| 3857 | return dma_resv_lock_interruptible(bo->ttm.base.resv, NULL); |
| 3858 | |
| 3859 | dma_resv_lock(bo->ttm.base.resv, NULL); |
| 3860 | |
| 3861 | return 0; |
| 3862 | } |
| 3863 | |
| 3864 | /** |
| 3865 | * xe_bo_unlock() - Unlock the buffer object's dma_resv object |
| 3866 | * @bo: The struct xe_bo whose lock is to be released. |
| 3867 | * |
| 3868 | * Unlock a buffer object lock that was locked by xe_bo_lock(). |
| 3869 | */ |
| 3870 | void xe_bo_unlock(struct xe_bo *bo) |
| 3871 | { |
| 3872 | dma_resv_unlock(bo->ttm.base.resv); |
| 3873 | } |
| 3874 | |
| 3875 | /** |
| 3876 | * xe_bo_can_migrate - Whether a buffer object likely can be migrated |
| 3877 | * @bo: The buffer object to migrate |
| 3878 | * @mem_type: The TTM memory type intended to migrate to |
| 3879 | * |
| 3880 | * Check whether the buffer object supports migration to the |
| 3881 | * given memory type. Note that pinning may affect the ability to migrate as |
| 3882 | * returned by this function. |
| 3883 | * |
| 3884 | * This function is primarily intended as a helper for checking the |
| 3885 | * possibility to migrate buffer objects and can be called without |
| 3886 | * the object lock held. |
| 3887 | * |
| 3888 | * Return: true if migration is possible, false otherwise. |
| 3889 | */ |
| 3890 | bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type) |
| 3891 | { |
| 3892 | unsigned int cur_place; |
| 3893 | |
| 3894 | if (bo->ttm.type == ttm_bo_type_kernel) |
| 3895 | return true; |
| 3896 | |
| 3897 | if (bo->ttm.type == ttm_bo_type_sg) |
| 3898 | return false; |
| 3899 | |
| 3900 | for (cur_place = 0; cur_place < bo->placement.num_placement; |
| 3901 | cur_place++) { |
| 3902 | if (bo->placements[cur_place].mem_type == mem_type) |
| 3903 | return true; |
| 3904 | } |
| 3905 | |
| 3906 | return false; |
| 3907 | } |
| 3908 | |
| 3909 | static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place) |
| 3910 | { |
| 3911 | memset(place, 0, sizeof(*place)); |
| 3912 | place->mem_type = mem_type; |
| 3913 | } |
| 3914 | |
| 3915 | /** |
| 3916 | * xe_bo_migrate - Migrate an object to the desired region id |
| 3917 | * @bo: The buffer object to migrate. |
| 3918 | * @mem_type: The TTM region type to migrate to. |
| 3919 | * @tctx: A pointer to a struct ttm_operation_ctx or NULL if |
| 3920 | * a default interruptibe ctx is to be used. |
| 3921 | * @exec: The drm_exec transaction to use for exhaustive eviction. |
| 3922 | * |
| 3923 | * Attempt to migrate the buffer object to the desired memory region. The |
| 3924 | * buffer object may not be pinned, and must be locked. |
| 3925 | * On successful completion, the object memory type will be updated, |
| 3926 | * but an async migration task may not have completed yet, and to |
| 3927 | * accomplish that, the object's kernel fences must be signaled with |
| 3928 | * the object lock held. |
| 3929 | * |
| 3930 | * Return: 0 on success. Negative error code on failure. In particular may |
| 3931 | * return -EINTR or -ERESTARTSYS if signal pending. |
| 3932 | */ |
| 3933 | int xe_bo_migrate(struct xe_bo *bo, u32 mem_type, struct ttm_operation_ctx *tctx, |
| 3934 | struct drm_exec *exec) |
| 3935 | { |
| 3936 | struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); |
| 3937 | struct ttm_operation_ctx ctx = { |
| 3938 | .interruptible = true, |
| 3939 | .no_wait_gpu = false, |
| 3940 | .gfp_retry_mayfail = true, |
| 3941 | }; |
| 3942 | struct ttm_placement placement; |
| 3943 | struct ttm_place requested; |
| 3944 | |
| 3945 | xe_bo_assert_held(bo); |
| 3946 | tctx = tctx ? tctx : &ctx; |
| 3947 | |
| 3948 | if (bo->ttm.resource->mem_type == mem_type) |
| 3949 | return 0; |
| 3950 | |
| 3951 | if (xe_bo_is_pinned(bo)) |
| 3952 | return -EBUSY; |
| 3953 | |
| 3954 | if (!xe_bo_can_migrate(bo, mem_type)) |
| 3955 | return -EINVAL; |
| 3956 | |
| 3957 | xe_place_from_ttm_type(mem_type, &requested); |
| 3958 | placement.num_placement = 1; |
| 3959 | placement.placement = &requested; |
| 3960 | |
| 3961 | /* |
| 3962 | * Stolen needs to be handled like below VRAM handling if we ever need |
| 3963 | * to support it. |
| 3964 | */ |
| 3965 | drm_WARN_ON(&xe->drm, mem_type == XE_PL_STOLEN); |
| 3966 | |
| 3967 | if (mem_type_is_vram(mem_type)) { |
| 3968 | u32 c = 0; |
| 3969 | |
| 3970 | add_vram(xe, bo, &requested, bo->flags, mem_type, &c); |
| 3971 | } |
| 3972 | |
| 3973 | if (!tctx->no_wait_gpu) |
| 3974 | xe_validation_assert_exec(xe_bo_device(bo), exec, &bo->ttm.base); |
| 3975 | return ttm_bo_validate(&bo->ttm, &placement, tctx); |
| 3976 | } |
| 3977 | |
| 3978 | /** |
| 3979 | * xe_bo_evict - Evict an object to evict placement |
| 3980 | * @bo: The buffer object to migrate. |
| 3981 | * @exec: The drm_exec transaction to use for exhaustive eviction. |
| 3982 | * |
| 3983 | * On successful completion, the object memory will be moved to evict |
| 3984 | * placement. This function blocks until the object has been fully moved. |
| 3985 | * |
| 3986 | * Return: 0 on success. Negative error code on failure. |
| 3987 | */ |
| 3988 | int xe_bo_evict(struct xe_bo *bo, struct drm_exec *exec) |
| 3989 | { |
| 3990 | struct ttm_operation_ctx ctx = { |
| 3991 | .interruptible = false, |
| 3992 | .no_wait_gpu = false, |
| 3993 | .gfp_retry_mayfail = true, |
| 3994 | }; |
| 3995 | struct ttm_placement placement; |
| 3996 | int ret; |
| 3997 | |
| 3998 | xe_evict_flags(&bo->ttm, &placement); |
| 3999 | ret = ttm_bo_validate(&bo->ttm, &placement, &ctx); |
| 4000 | if (ret) |
| 4001 | return ret; |
| 4002 | |
| 4003 | dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL, |
| 4004 | false, MAX_SCHEDULE_TIMEOUT); |
| 4005 | |
| 4006 | return 0; |
| 4007 | } |
| 4008 | |
| 4009 | /** |
| 4010 | * xe_bo_needs_ccs_pages - Whether a bo needs to back up CCS pages when |
| 4011 | * placed in system memory. |
| 4012 | * @bo: The xe_bo |
| 4013 | * |
| 4014 | * Return: true if extra pages need to be allocated, false otherwise. |
| 4015 | */ |
| 4016 | bool xe_bo_needs_ccs_pages(struct xe_bo *bo) |
| 4017 | { |
| 4018 | struct xe_device *xe = xe_bo_device(bo); |
| 4019 | |
| 4020 | if (GRAPHICS_VER(xe) >= 20 && IS_DGFX(xe)) |
| 4021 | return false; |
| 4022 | |
| 4023 | if (!xe_device_has_flat_ccs(xe) || bo->ttm.type != ttm_bo_type_device) |
| 4024 | return false; |
| 4025 | |
| 4026 | /* On discrete GPUs, if the GPU can access this buffer from |
| 4027 | * system memory (i.e., it allows XE_PL_TT placement), FlatCCS |
| 4028 | * can't be used since there's no CCS storage associated with |
| 4029 | * non-VRAM addresses. |
| 4030 | */ |
| 4031 | if (IS_DGFX(xe) && (bo->flags & XE_BO_FLAG_SYSTEM)) |
| 4032 | return false; |
| 4033 | |
| 4034 | /* Check if userspace explicitly requested no compression */ |
| 4035 | if (bo->flags & XE_BO_FLAG_NO_COMPRESSION) |
| 4036 | return false; |
| 4037 | |
| 4038 | /* |
| 4039 | * For WB (Write-Back) CPU caching mode, check if the device |
| 4040 | * supports WB compression with coherency. |
| 4041 | */ |
| 4042 | if (bo->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB && |
| 4043 | xe->pat.idx[XE_CACHE_WB_COMPRESSION] == XE_PAT_INVALID_IDX) |
| 4044 | return false; |
| 4045 | |
| 4046 | return true; |
| 4047 | } |
| 4048 | |
| 4049 | /** |
| 4050 | * __xe_bo_release_dummy() - Dummy kref release function |
| 4051 | * @kref: The embedded struct kref. |
| 4052 | * |
| 4053 | * Dummy release function for xe_bo_put_deferred(). Keep off. |
| 4054 | */ |
| 4055 | void __xe_bo_release_dummy(struct kref *kref) |
| 4056 | { |
| 4057 | } |
| 4058 | |
| 4059 | /** |
| 4060 | * xe_bo_put_commit() - Put bos whose put was deferred by xe_bo_put_deferred(). |
| 4061 | * @deferred: The lockless list used for the call to xe_bo_put_deferred(). |
| 4062 | * |
| 4063 | * Puts all bos whose put was deferred by xe_bo_put_deferred(). |
| 4064 | * The @deferred list can be either an onstack local list or a global |
| 4065 | * shared list used by a workqueue. |
| 4066 | */ |
| 4067 | void xe_bo_put_commit(struct llist_head *deferred) |
| 4068 | { |
| 4069 | struct llist_node *freed; |
| 4070 | struct xe_bo *bo, *next; |
| 4071 | |
| 4072 | if (!deferred) |
| 4073 | return; |
| 4074 | |
| 4075 | freed = llist_del_all(deferred); |
| 4076 | if (!freed) |
| 4077 | return; |
| 4078 | |
| 4079 | llist_for_each_entry_safe(bo, next, freed, freed) |
| 4080 | drm_gem_object_free(&bo->ttm.base.refcount); |
| 4081 | } |
| 4082 | |
| 4083 | static void xe_bo_dev_work_func(struct work_struct *work) |
| 4084 | { |
| 4085 | struct xe_bo_dev *bo_dev = container_of(work, typeof(*bo_dev), async_free); |
| 4086 | |
| 4087 | xe_bo_put_commit(&bo_dev->async_list); |
| 4088 | } |
| 4089 | |
| 4090 | /** |
| 4091 | * xe_bo_dev_init() - Initialize BO dev to manage async BO freeing |
| 4092 | * @bo_dev: The BO dev structure |
| 4093 | */ |
| 4094 | void xe_bo_dev_init(struct xe_bo_dev *bo_dev) |
| 4095 | { |
| 4096 | INIT_WORK(&bo_dev->async_free, xe_bo_dev_work_func); |
| 4097 | } |
| 4098 | |
| 4099 | /** |
| 4100 | * xe_bo_dev_fini() - Finalize BO dev managing async BO freeing |
| 4101 | * @bo_dev: The BO dev structure |
| 4102 | */ |
| 4103 | void xe_bo_dev_fini(struct xe_bo_dev *bo_dev) |
| 4104 | { |
| 4105 | flush_work(&bo_dev->async_free); |
| 4106 | } |
| 4107 | |
| 4108 | void xe_bo_put(struct xe_bo *bo) |
| 4109 | { |
| 4110 | struct xe_tile *tile; |
| 4111 | u8 id; |
| 4112 | |
| 4113 | might_sleep(); |
| 4114 | if (bo) { |
| 4115 | #ifdef CONFIG_PROC_FS |
| 4116 | if (bo->client) |
| 4117 | might_lock(&bo->client->bos_lock); |
| 4118 | #endif |
| 4119 | for_each_tile(tile, xe_bo_device(bo), id) |
| 4120 | if (bo->ggtt_node[id]) |
| 4121 | xe_ggtt_might_lock(tile->mem.ggtt); |
| 4122 | drm_gem_object_put(&bo->ttm.base); |
| 4123 | } |
| 4124 | } |
| 4125 | |
| 4126 | /** |
| 4127 | * xe_bo_dumb_create - Create a dumb bo as backing for a fb |
| 4128 | * @file_priv: ... |
| 4129 | * @dev: ... |
| 4130 | * @args: ... |
| 4131 | * |
| 4132 | * See dumb_create() hook in include/drm/drm_drv.h |
| 4133 | * |
| 4134 | * Return: ... |
| 4135 | */ |
| 4136 | int xe_bo_dumb_create(struct drm_file *file_priv, |
| 4137 | struct drm_device *dev, |
| 4138 | struct drm_mode_create_dumb *args) |
| 4139 | { |
| 4140 | struct xe_device *xe = to_xe_device(dev); |
| 4141 | struct xe_bo *bo; |
| 4142 | uint32_t handle; |
| 4143 | int err; |
| 4144 | u32 page_size = max_t(u32, PAGE_SIZE, |
| 4145 | xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K); |
| 4146 | |
| 4147 | err = drm_mode_size_dumb(dev, args, SZ_64, page_size); |
| 4148 | if (err) |
| 4149 | return err; |
| 4150 | |
| 4151 | bo = xe_bo_create_user(xe, NULL, args->size, |
| 4152 | DRM_XE_GEM_CPU_CACHING_WC, |
| 4153 | XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) | |
| 4154 | XE_BO_FLAG_FORCE_WC | |
| 4155 | XE_BO_FLAG_NEEDS_CPU_ACCESS, NULL); |
| 4156 | if (IS_ERR(bo)) |
| 4157 | return PTR_ERR(bo); |
| 4158 | |
| 4159 | err = drm_gem_handle_create(file_priv, &bo->ttm.base, &handle); |
| 4160 | /* drop reference from allocate - handle holds it now */ |
| 4161 | drm_gem_object_put(&bo->ttm.base); |
| 4162 | if (!err) |
| 4163 | args->handle = handle; |
| 4164 | return err; |
| 4165 | } |
| 4166 | |
| 4167 | void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo) |
| 4168 | { |
| 4169 | struct ttm_buffer_object *tbo = &bo->ttm; |
| 4170 | struct ttm_device *bdev = tbo->bdev; |
| 4171 | |
| 4172 | drm_vma_node_unmap(&tbo->base.vma_node, bdev->dev_mapping); |
| 4173 | |
| 4174 | list_del_init(&bo->vram_userfault_link); |
| 4175 | } |
| 4176 | |
| 4177 | #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) |
| 4178 | #include "tests/xe_bo.c" |
| 4179 | #endif |