1213d5092SThomas Hellström // SPDX-License-Identifier: MIT 2213d5092SThomas Hellström /* 3213d5092SThomas Hellström * Copyright © 2021 Intel Corporation 4213d5092SThomas Hellström */ 5213d5092SThomas Hellström 6213d5092SThomas Hellström #include <drm/ttm/ttm_bo_driver.h> 7213d5092SThomas Hellström #include <drm/ttm/ttm_placement.h> 8213d5092SThomas Hellström 9213d5092SThomas Hellström #include "i915_drv.h" 10213d5092SThomas Hellström #include "intel_memory_region.h" 11213d5092SThomas Hellström #include "intel_region_ttm.h" 12213d5092SThomas Hellström 13213d5092SThomas Hellström #include "gem/i915_gem_object.h" 14213d5092SThomas Hellström #include "gem/i915_gem_region.h" 15213d5092SThomas Hellström #include "gem/i915_gem_ttm.h" 16cf3e3e86SMaarten Lankhorst #include "gem/i915_gem_mman.h" 17213d5092SThomas Hellström 1850331a7bSRamalingam C #include "gt/intel_migrate.h" 1950331a7bSRamalingam C #include "gt/intel_engine_pm.h" 2050331a7bSRamalingam C 21213d5092SThomas Hellström #define I915_PL_LMEM0 TTM_PL_PRIV 22213d5092SThomas Hellström #define I915_PL_SYSTEM TTM_PL_SYSTEM 23213d5092SThomas Hellström #define I915_PL_STOLEN TTM_PL_VRAM 24213d5092SThomas Hellström #define I915_PL_GGTT TTM_PL_TT 25213d5092SThomas Hellström 26213d5092SThomas Hellström #define I915_TTM_PRIO_PURGE 0 27213d5092SThomas Hellström #define I915_TTM_PRIO_NO_PAGES 1 28213d5092SThomas Hellström #define I915_TTM_PRIO_HAS_PAGES 2 29213d5092SThomas Hellström 3038f28c06SThomas Hellström /* 3138f28c06SThomas Hellström * Size of struct ttm_place vector in on-stack struct ttm_placement allocs 3238f28c06SThomas Hellström */ 3338f28c06SThomas Hellström #define I915_TTM_MAX_PLACEMENTS INTEL_REGION_UNKNOWN 3438f28c06SThomas Hellström 35213d5092SThomas Hellström /** 36213d5092SThomas Hellström * struct i915_ttm_tt - TTM page vector with additional private information 37213d5092SThomas Hellström * @ttm: The base TTM page vector. 38213d5092SThomas Hellström * @dev: The struct device used for dma mapping and unmapping. 39213d5092SThomas Hellström * @cached_st: The cached scatter-gather table. 40213d5092SThomas Hellström * 41213d5092SThomas Hellström * Note that DMA may be going on right up to the point where the page- 42213d5092SThomas Hellström * vector is unpopulated in delayed destroy. Hence keep the 43213d5092SThomas Hellström * scatter-gather table mapped and cached up to that point. This is 44213d5092SThomas Hellström * different from the cached gem object io scatter-gather table which 45213d5092SThomas Hellström * doesn't have an associated dma mapping. 46213d5092SThomas Hellström */ 47213d5092SThomas Hellström struct i915_ttm_tt { 48213d5092SThomas Hellström struct ttm_tt ttm; 49213d5092SThomas Hellström struct device *dev; 50213d5092SThomas Hellström struct sg_table *cached_st; 51213d5092SThomas Hellström }; 52213d5092SThomas Hellström 5338f28c06SThomas Hellström static const struct ttm_place sys_placement_flags = { 54213d5092SThomas Hellström .fpfn = 0, 55213d5092SThomas Hellström .lpfn = 0, 56213d5092SThomas Hellström .mem_type = I915_PL_SYSTEM, 57213d5092SThomas Hellström .flags = 0, 58213d5092SThomas Hellström }; 59213d5092SThomas Hellström 60213d5092SThomas Hellström static struct ttm_placement i915_sys_placement = { 61213d5092SThomas Hellström .num_placement = 1, 6238f28c06SThomas Hellström .placement = &sys_placement_flags, 63213d5092SThomas Hellström .num_busy_placement = 1, 6438f28c06SThomas Hellström .busy_placement = &sys_placement_flags, 65213d5092SThomas Hellström }; 66213d5092SThomas Hellström 67b07a6483SThomas Hellström static int i915_ttm_err_to_gem(int err) 68b07a6483SThomas Hellström { 69b07a6483SThomas Hellström /* Fastpath */ 70b07a6483SThomas Hellström if (likely(!err)) 71b07a6483SThomas Hellström return 0; 72b07a6483SThomas Hellström 73b07a6483SThomas Hellström switch (err) { 74b07a6483SThomas Hellström case -EBUSY: 75b07a6483SThomas Hellström /* 76b07a6483SThomas Hellström * TTM likes to convert -EDEADLK to -EBUSY, and wants us to 77b07a6483SThomas Hellström * restart the operation, since we don't record the contending 78b07a6483SThomas Hellström * lock. We use -EAGAIN to restart. 79b07a6483SThomas Hellström */ 80b07a6483SThomas Hellström return -EAGAIN; 81b07a6483SThomas Hellström case -ENOSPC: 82b07a6483SThomas Hellström /* 83b07a6483SThomas Hellström * Memory type / region is full, and we can't evict. 84b07a6483SThomas Hellström * Except possibly system, that returns -ENOMEM; 85b07a6483SThomas Hellström */ 86b07a6483SThomas Hellström return -ENXIO; 87b07a6483SThomas Hellström default: 88b07a6483SThomas Hellström break; 89b07a6483SThomas Hellström } 90b07a6483SThomas Hellström 91b07a6483SThomas Hellström return err; 92b07a6483SThomas Hellström } 93b07a6483SThomas Hellström 943c2b8f32SThomas Hellström static bool gpu_binds_iomem(struct ttm_resource *mem) 953c2b8f32SThomas Hellström { 963c2b8f32SThomas Hellström return mem->mem_type != TTM_PL_SYSTEM; 973c2b8f32SThomas Hellström } 983c2b8f32SThomas Hellström 993c2b8f32SThomas Hellström static bool cpu_maps_iomem(struct ttm_resource *mem) 1003c2b8f32SThomas Hellström { 1013c2b8f32SThomas Hellström /* Once / if we support GGTT, this is also false for cached ttm_tts */ 1023c2b8f32SThomas Hellström return mem->mem_type != TTM_PL_SYSTEM; 1033c2b8f32SThomas Hellström } 1043c2b8f32SThomas Hellström 1053c2b8f32SThomas Hellström static enum i915_cache_level 1063c2b8f32SThomas Hellström i915_ttm_cache_level(struct drm_i915_private *i915, struct ttm_resource *res, 1073c2b8f32SThomas Hellström struct ttm_tt *ttm) 1083c2b8f32SThomas Hellström { 1093c2b8f32SThomas Hellström return ((HAS_LLC(i915) || HAS_SNOOP(i915)) && !gpu_binds_iomem(res) && 1103c2b8f32SThomas Hellström ttm->caching == ttm_cached) ? I915_CACHE_LLC : 1113c2b8f32SThomas Hellström I915_CACHE_NONE; 1123c2b8f32SThomas Hellström } 1133c2b8f32SThomas Hellström 114213d5092SThomas Hellström static void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj); 115213d5092SThomas Hellström 11638f28c06SThomas Hellström static enum ttm_caching 11738f28c06SThomas Hellström i915_ttm_select_tt_caching(const struct drm_i915_gem_object *obj) 11838f28c06SThomas Hellström { 11938f28c06SThomas Hellström /* 12038f28c06SThomas Hellström * Objects only allowed in system get cached cpu-mappings. 12138f28c06SThomas Hellström * Other objects get WC mapping for now. Even if in system. 12238f28c06SThomas Hellström */ 12338f28c06SThomas Hellström if (obj->mm.region->type == INTEL_MEMORY_SYSTEM && 12438f28c06SThomas Hellström obj->mm.n_placements <= 1) 12538f28c06SThomas Hellström return ttm_cached; 12638f28c06SThomas Hellström 12738f28c06SThomas Hellström return ttm_write_combined; 12838f28c06SThomas Hellström } 12938f28c06SThomas Hellström 13038f28c06SThomas Hellström static void 13138f28c06SThomas Hellström i915_ttm_place_from_region(const struct intel_memory_region *mr, 132beb6a229SMatthew Auld struct ttm_place *place, 133beb6a229SMatthew Auld unsigned int flags) 13438f28c06SThomas Hellström { 13538f28c06SThomas Hellström memset(place, 0, sizeof(*place)); 13638f28c06SThomas Hellström place->mem_type = intel_region_to_ttm_type(mr); 137beb6a229SMatthew Auld 138beb6a229SMatthew Auld if (flags & I915_BO_ALLOC_CONTIGUOUS) 139beb6a229SMatthew Auld place->flags = TTM_PL_FLAG_CONTIGUOUS; 14038f28c06SThomas Hellström } 14138f28c06SThomas Hellström 14238f28c06SThomas Hellström static void 14338f28c06SThomas Hellström i915_ttm_placement_from_obj(const struct drm_i915_gem_object *obj, 14438f28c06SThomas Hellström struct ttm_place *requested, 14538f28c06SThomas Hellström struct ttm_place *busy, 14638f28c06SThomas Hellström struct ttm_placement *placement) 14738f28c06SThomas Hellström { 14838f28c06SThomas Hellström unsigned int num_allowed = obj->mm.n_placements; 149beb6a229SMatthew Auld unsigned int flags = obj->flags; 15038f28c06SThomas Hellström unsigned int i; 15138f28c06SThomas Hellström 15238f28c06SThomas Hellström placement->num_placement = 1; 15338f28c06SThomas Hellström i915_ttm_place_from_region(num_allowed ? obj->mm.placements[0] : 154beb6a229SMatthew Auld obj->mm.region, requested, flags); 15538f28c06SThomas Hellström 15638f28c06SThomas Hellström /* Cache this on object? */ 15738f28c06SThomas Hellström placement->num_busy_placement = num_allowed; 15838f28c06SThomas Hellström for (i = 0; i < placement->num_busy_placement; ++i) 159beb6a229SMatthew Auld i915_ttm_place_from_region(obj->mm.placements[i], busy + i, flags); 16038f28c06SThomas Hellström 16138f28c06SThomas Hellström if (num_allowed == 0) { 16238f28c06SThomas Hellström *busy = *requested; 16338f28c06SThomas Hellström placement->num_busy_placement = 1; 16438f28c06SThomas Hellström } 16538f28c06SThomas Hellström 16638f28c06SThomas Hellström placement->placement = requested; 16738f28c06SThomas Hellström placement->busy_placement = busy; 16838f28c06SThomas Hellström } 16938f28c06SThomas Hellström 170213d5092SThomas Hellström static struct ttm_tt *i915_ttm_tt_create(struct ttm_buffer_object *bo, 171213d5092SThomas Hellström uint32_t page_flags) 172213d5092SThomas Hellström { 173213d5092SThomas Hellström struct ttm_resource_manager *man = 174213d5092SThomas Hellström ttm_manager_type(bo->bdev, bo->resource->mem_type); 175213d5092SThomas Hellström struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 176213d5092SThomas Hellström struct i915_ttm_tt *i915_tt; 177213d5092SThomas Hellström int ret; 178213d5092SThomas Hellström 179213d5092SThomas Hellström i915_tt = kzalloc(sizeof(*i915_tt), GFP_KERNEL); 180213d5092SThomas Hellström if (!i915_tt) 181213d5092SThomas Hellström return NULL; 182213d5092SThomas Hellström 183213d5092SThomas Hellström if (obj->flags & I915_BO_ALLOC_CPU_CLEAR && 184213d5092SThomas Hellström man->use_tt) 185213d5092SThomas Hellström page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC; 186213d5092SThomas Hellström 18738f28c06SThomas Hellström ret = ttm_tt_init(&i915_tt->ttm, bo, page_flags, 18838f28c06SThomas Hellström i915_ttm_select_tt_caching(obj)); 189213d5092SThomas Hellström if (ret) { 190213d5092SThomas Hellström kfree(i915_tt); 191213d5092SThomas Hellström return NULL; 192213d5092SThomas Hellström } 193213d5092SThomas Hellström 194213d5092SThomas Hellström i915_tt->dev = obj->base.dev->dev; 195213d5092SThomas Hellström 196213d5092SThomas Hellström return &i915_tt->ttm; 197213d5092SThomas Hellström } 198213d5092SThomas Hellström 199213d5092SThomas Hellström static void i915_ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm) 200213d5092SThomas Hellström { 201213d5092SThomas Hellström struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm); 202213d5092SThomas Hellström 203213d5092SThomas Hellström if (i915_tt->cached_st) { 204213d5092SThomas Hellström dma_unmap_sgtable(i915_tt->dev, i915_tt->cached_st, 205213d5092SThomas Hellström DMA_BIDIRECTIONAL, 0); 206213d5092SThomas Hellström sg_free_table(i915_tt->cached_st); 207213d5092SThomas Hellström kfree(i915_tt->cached_st); 208213d5092SThomas Hellström i915_tt->cached_st = NULL; 209213d5092SThomas Hellström } 210213d5092SThomas Hellström ttm_pool_free(&bdev->pool, ttm); 211213d5092SThomas Hellström } 212213d5092SThomas Hellström 213213d5092SThomas Hellström static void i915_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm) 214213d5092SThomas Hellström { 215213d5092SThomas Hellström struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm); 216213d5092SThomas Hellström 217213d5092SThomas Hellström ttm_tt_destroy_common(bdev, ttm); 218c865204eSThomas Hellström ttm_tt_fini(ttm); 219213d5092SThomas Hellström kfree(i915_tt); 220213d5092SThomas Hellström } 221213d5092SThomas Hellström 222213d5092SThomas Hellström static bool i915_ttm_eviction_valuable(struct ttm_buffer_object *bo, 223213d5092SThomas Hellström const struct ttm_place *place) 224213d5092SThomas Hellström { 225213d5092SThomas Hellström struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 226213d5092SThomas Hellström 227213d5092SThomas Hellström /* Will do for now. Our pinned objects are still on TTM's LRU lists */ 228d53ec322SMatthew Auld return i915_gem_object_evictable(obj); 229213d5092SThomas Hellström } 230213d5092SThomas Hellström 231213d5092SThomas Hellström static void i915_ttm_evict_flags(struct ttm_buffer_object *bo, 232213d5092SThomas Hellström struct ttm_placement *placement) 233213d5092SThomas Hellström { 234213d5092SThomas Hellström *placement = i915_sys_placement; 235213d5092SThomas Hellström } 236213d5092SThomas Hellström 237213d5092SThomas Hellström static int i915_ttm_move_notify(struct ttm_buffer_object *bo) 238213d5092SThomas Hellström { 239213d5092SThomas Hellström struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 240213d5092SThomas Hellström int ret; 241213d5092SThomas Hellström 242213d5092SThomas Hellström ret = i915_gem_object_unbind(obj, I915_GEM_OBJECT_UNBIND_ACTIVE); 243213d5092SThomas Hellström if (ret) 244213d5092SThomas Hellström return ret; 245213d5092SThomas Hellström 246213d5092SThomas Hellström ret = __i915_gem_object_put_pages(obj); 247213d5092SThomas Hellström if (ret) 248213d5092SThomas Hellström return ret; 249213d5092SThomas Hellström 250213d5092SThomas Hellström return 0; 251213d5092SThomas Hellström } 252213d5092SThomas Hellström 253213d5092SThomas Hellström static void i915_ttm_free_cached_io_st(struct drm_i915_gem_object *obj) 254213d5092SThomas Hellström { 255cf3e3e86SMaarten Lankhorst struct radix_tree_iter iter; 256cf3e3e86SMaarten Lankhorst void __rcu **slot; 257cf3e3e86SMaarten Lankhorst 258cf3e3e86SMaarten Lankhorst if (!obj->ttm.cached_io_st) 259cf3e3e86SMaarten Lankhorst return; 260cf3e3e86SMaarten Lankhorst 261cf3e3e86SMaarten Lankhorst rcu_read_lock(); 262cf3e3e86SMaarten Lankhorst radix_tree_for_each_slot(slot, &obj->ttm.get_io_page.radix, &iter, 0) 263cf3e3e86SMaarten Lankhorst radix_tree_delete(&obj->ttm.get_io_page.radix, iter.index); 264cf3e3e86SMaarten Lankhorst rcu_read_unlock(); 265cf3e3e86SMaarten Lankhorst 266213d5092SThomas Hellström sg_free_table(obj->ttm.cached_io_st); 267213d5092SThomas Hellström kfree(obj->ttm.cached_io_st); 268213d5092SThomas Hellström obj->ttm.cached_io_st = NULL; 269213d5092SThomas Hellström } 270213d5092SThomas Hellström 2713c2b8f32SThomas Hellström static void 2723c2b8f32SThomas Hellström i915_ttm_adjust_domains_after_move(struct drm_i915_gem_object *obj) 2733c2b8f32SThomas Hellström { 2743c2b8f32SThomas Hellström struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 2753c2b8f32SThomas Hellström 2763c2b8f32SThomas Hellström if (cpu_maps_iomem(bo->resource) || bo->ttm->caching != ttm_cached) { 2773c2b8f32SThomas Hellström obj->write_domain = I915_GEM_DOMAIN_WC; 2783c2b8f32SThomas Hellström obj->read_domains = I915_GEM_DOMAIN_WC; 2793c2b8f32SThomas Hellström } else { 2803c2b8f32SThomas Hellström obj->write_domain = I915_GEM_DOMAIN_CPU; 2813c2b8f32SThomas Hellström obj->read_domains = I915_GEM_DOMAIN_CPU; 2823c2b8f32SThomas Hellström } 2833c2b8f32SThomas Hellström } 2843c2b8f32SThomas Hellström 2853c2b8f32SThomas Hellström static void i915_ttm_adjust_gem_after_move(struct drm_i915_gem_object *obj) 2863c2b8f32SThomas Hellström { 2873c2b8f32SThomas Hellström struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 2883c2b8f32SThomas Hellström unsigned int cache_level; 28932b7cf51SThomas Hellström unsigned int i; 29032b7cf51SThomas Hellström 29132b7cf51SThomas Hellström /* 29232b7cf51SThomas Hellström * If object was moved to an allowable region, update the object 29332b7cf51SThomas Hellström * region to consider it migrated. Note that if it's currently not 29432b7cf51SThomas Hellström * in an allowable region, it's evicted and we don't update the 29532b7cf51SThomas Hellström * object region. 29632b7cf51SThomas Hellström */ 29732b7cf51SThomas Hellström if (intel_region_to_ttm_type(obj->mm.region) != bo->resource->mem_type) { 29832b7cf51SThomas Hellström for (i = 0; i < obj->mm.n_placements; ++i) { 29932b7cf51SThomas Hellström struct intel_memory_region *mr = obj->mm.placements[i]; 30032b7cf51SThomas Hellström 30132b7cf51SThomas Hellström if (intel_region_to_ttm_type(mr) == bo->resource->mem_type && 30232b7cf51SThomas Hellström mr != obj->mm.region) { 30332b7cf51SThomas Hellström i915_gem_object_release_memory_region(obj); 30432b7cf51SThomas Hellström i915_gem_object_init_memory_region(obj, mr); 30532b7cf51SThomas Hellström break; 30632b7cf51SThomas Hellström } 30732b7cf51SThomas Hellström } 30832b7cf51SThomas Hellström } 3093c2b8f32SThomas Hellström 3103c2b8f32SThomas Hellström obj->mem_flags &= ~(I915_BO_FLAG_STRUCT_PAGE | I915_BO_FLAG_IOMEM); 3113c2b8f32SThomas Hellström 3123c2b8f32SThomas Hellström obj->mem_flags |= cpu_maps_iomem(bo->resource) ? I915_BO_FLAG_IOMEM : 3133c2b8f32SThomas Hellström I915_BO_FLAG_STRUCT_PAGE; 3143c2b8f32SThomas Hellström 3153c2b8f32SThomas Hellström cache_level = i915_ttm_cache_level(to_i915(bo->base.dev), bo->resource, 3163c2b8f32SThomas Hellström bo->ttm); 3173c2b8f32SThomas Hellström i915_gem_object_set_cache_coherency(obj, cache_level); 3183c2b8f32SThomas Hellström } 3193c2b8f32SThomas Hellström 320213d5092SThomas Hellström static void i915_ttm_purge(struct drm_i915_gem_object *obj) 321213d5092SThomas Hellström { 322213d5092SThomas Hellström struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 323213d5092SThomas Hellström struct ttm_operation_ctx ctx = { 324213d5092SThomas Hellström .interruptible = true, 325213d5092SThomas Hellström .no_wait_gpu = false, 326213d5092SThomas Hellström }; 327213d5092SThomas Hellström struct ttm_placement place = {}; 328213d5092SThomas Hellström int ret; 329213d5092SThomas Hellström 330213d5092SThomas Hellström if (obj->mm.madv == __I915_MADV_PURGED) 331213d5092SThomas Hellström return; 332213d5092SThomas Hellström 333213d5092SThomas Hellström /* TTM's purge interface. Note that we might be reentering. */ 334213d5092SThomas Hellström ret = ttm_bo_validate(bo, &place, &ctx); 335213d5092SThomas Hellström if (!ret) { 3363c2b8f32SThomas Hellström obj->write_domain = 0; 3373c2b8f32SThomas Hellström obj->read_domains = 0; 3383c2b8f32SThomas Hellström i915_ttm_adjust_gem_after_move(obj); 339213d5092SThomas Hellström i915_ttm_free_cached_io_st(obj); 340213d5092SThomas Hellström obj->mm.madv = __I915_MADV_PURGED; 341213d5092SThomas Hellström } 342213d5092SThomas Hellström } 343213d5092SThomas Hellström 344213d5092SThomas Hellström static void i915_ttm_swap_notify(struct ttm_buffer_object *bo) 345213d5092SThomas Hellström { 346213d5092SThomas Hellström struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 347213d5092SThomas Hellström int ret = i915_ttm_move_notify(bo); 348213d5092SThomas Hellström 349213d5092SThomas Hellström GEM_WARN_ON(ret); 350213d5092SThomas Hellström GEM_WARN_ON(obj->ttm.cached_io_st); 351213d5092SThomas Hellström if (!ret && obj->mm.madv != I915_MADV_WILLNEED) 352213d5092SThomas Hellström i915_ttm_purge(obj); 353213d5092SThomas Hellström } 354213d5092SThomas Hellström 355213d5092SThomas Hellström static void i915_ttm_delete_mem_notify(struct ttm_buffer_object *bo) 356213d5092SThomas Hellström { 357213d5092SThomas Hellström struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 358213d5092SThomas Hellström 35948b09612SMaarten Lankhorst if (likely(obj)) 360c865204eSThomas Hellström i915_ttm_free_cached_io_st(obj); 361213d5092SThomas Hellström } 362213d5092SThomas Hellström 363213d5092SThomas Hellström static struct intel_memory_region * 364213d5092SThomas Hellström i915_ttm_region(struct ttm_device *bdev, int ttm_mem_type) 365213d5092SThomas Hellström { 366213d5092SThomas Hellström struct drm_i915_private *i915 = container_of(bdev, typeof(*i915), bdev); 367213d5092SThomas Hellström 368213d5092SThomas Hellström /* There's some room for optimization here... */ 369213d5092SThomas Hellström GEM_BUG_ON(ttm_mem_type != I915_PL_SYSTEM && 370213d5092SThomas Hellström ttm_mem_type < I915_PL_LMEM0); 371213d5092SThomas Hellström if (ttm_mem_type == I915_PL_SYSTEM) 372213d5092SThomas Hellström return intel_memory_region_lookup(i915, INTEL_MEMORY_SYSTEM, 373213d5092SThomas Hellström 0); 374213d5092SThomas Hellström 375213d5092SThomas Hellström return intel_memory_region_lookup(i915, INTEL_MEMORY_LOCAL, 376213d5092SThomas Hellström ttm_mem_type - I915_PL_LMEM0); 377213d5092SThomas Hellström } 378213d5092SThomas Hellström 379213d5092SThomas Hellström static struct sg_table *i915_ttm_tt_get_st(struct ttm_tt *ttm) 380213d5092SThomas Hellström { 381213d5092SThomas Hellström struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm); 382213d5092SThomas Hellström struct sg_table *st; 383213d5092SThomas Hellström int ret; 384213d5092SThomas Hellström 385213d5092SThomas Hellström if (i915_tt->cached_st) 386213d5092SThomas Hellström return i915_tt->cached_st; 387213d5092SThomas Hellström 388213d5092SThomas Hellström st = kzalloc(sizeof(*st), GFP_KERNEL); 389213d5092SThomas Hellström if (!st) 390213d5092SThomas Hellström return ERR_PTR(-ENOMEM); 391213d5092SThomas Hellström 39223852becSLinus Torvalds ret = sg_alloc_table_from_pages_segment(st, 39323852becSLinus Torvalds ttm->pages, ttm->num_pages, 39423852becSLinus Torvalds 0, (unsigned long)ttm->num_pages << PAGE_SHIFT, 39523852becSLinus Torvalds i915_sg_segment_size(), GFP_KERNEL); 39623852becSLinus Torvalds if (ret) { 397213d5092SThomas Hellström kfree(st); 39823852becSLinus Torvalds return ERR_PTR(ret); 399213d5092SThomas Hellström } 400213d5092SThomas Hellström 401213d5092SThomas Hellström ret = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0); 402213d5092SThomas Hellström if (ret) { 403213d5092SThomas Hellström sg_free_table(st); 404213d5092SThomas Hellström kfree(st); 405213d5092SThomas Hellström return ERR_PTR(ret); 406213d5092SThomas Hellström } 407213d5092SThomas Hellström 408213d5092SThomas Hellström i915_tt->cached_st = st; 409213d5092SThomas Hellström return st; 410213d5092SThomas Hellström } 411213d5092SThomas Hellström 412213d5092SThomas Hellström static struct sg_table * 413213d5092SThomas Hellström i915_ttm_resource_get_st(struct drm_i915_gem_object *obj, 414213d5092SThomas Hellström struct ttm_resource *res) 415213d5092SThomas Hellström { 416213d5092SThomas Hellström struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 417213d5092SThomas Hellström 4183c2b8f32SThomas Hellström if (!gpu_binds_iomem(res)) 419213d5092SThomas Hellström return i915_ttm_tt_get_st(bo->ttm); 420213d5092SThomas Hellström 4213c2b8f32SThomas Hellström /* 4223c2b8f32SThomas Hellström * If CPU mapping differs, we need to add the ttm_tt pages to 4233c2b8f32SThomas Hellström * the resulting st. Might make sense for GGTT. 4243c2b8f32SThomas Hellström */ 4253c2b8f32SThomas Hellström GEM_WARN_ON(!cpu_maps_iomem(res)); 426687c7d0fSMatthew Auld return intel_region_ttm_resource_to_st(obj->mm.region, res); 427213d5092SThomas Hellström } 428213d5092SThomas Hellström 42950331a7bSRamalingam C static int i915_ttm_accel_move(struct ttm_buffer_object *bo, 430d8ac30fdSThomas Hellström bool clear, 43150331a7bSRamalingam C struct ttm_resource *dst_mem, 432*0d938863SThomas Hellström struct ttm_tt *dst_ttm, 43350331a7bSRamalingam C struct sg_table *dst_st) 43450331a7bSRamalingam C { 43550331a7bSRamalingam C struct drm_i915_private *i915 = container_of(bo->bdev, typeof(*i915), 43650331a7bSRamalingam C bdev); 43750331a7bSRamalingam C struct ttm_resource_manager *src_man = 43850331a7bSRamalingam C ttm_manager_type(bo->bdev, bo->resource->mem_type); 43950331a7bSRamalingam C struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 44050331a7bSRamalingam C struct sg_table *src_st; 44150331a7bSRamalingam C struct i915_request *rq; 442*0d938863SThomas Hellström struct ttm_tt *src_ttm = bo->ttm; 4433c2b8f32SThomas Hellström enum i915_cache_level src_level, dst_level; 44450331a7bSRamalingam C int ret; 44550331a7bSRamalingam C 44650331a7bSRamalingam C if (!i915->gt.migrate.context) 44750331a7bSRamalingam C return -EINVAL; 44850331a7bSRamalingam C 449*0d938863SThomas Hellström dst_level = i915_ttm_cache_level(i915, dst_mem, dst_ttm); 450d8ac30fdSThomas Hellström if (clear) { 45150331a7bSRamalingam C if (bo->type == ttm_bo_type_kernel) 45250331a7bSRamalingam C return -EINVAL; 45350331a7bSRamalingam C 45450331a7bSRamalingam C intel_engine_pm_get(i915->gt.migrate.context->engine); 45550331a7bSRamalingam C ret = intel_context_migrate_clear(i915->gt.migrate.context, NULL, 4563c2b8f32SThomas Hellström dst_st->sgl, dst_level, 4573c2b8f32SThomas Hellström gpu_binds_iomem(dst_mem), 45850331a7bSRamalingam C 0, &rq); 45950331a7bSRamalingam C 46050331a7bSRamalingam C if (!ret && rq) { 46150331a7bSRamalingam C i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT); 46250331a7bSRamalingam C i915_request_put(rq); 46350331a7bSRamalingam C } 46450331a7bSRamalingam C intel_engine_pm_put(i915->gt.migrate.context->engine); 46550331a7bSRamalingam C } else { 466*0d938863SThomas Hellström src_st = src_man->use_tt ? i915_ttm_tt_get_st(src_ttm) : 46750331a7bSRamalingam C obj->ttm.cached_io_st; 46850331a7bSRamalingam C 469*0d938863SThomas Hellström src_level = i915_ttm_cache_level(i915, bo->resource, src_ttm); 47050331a7bSRamalingam C intel_engine_pm_get(i915->gt.migrate.context->engine); 47150331a7bSRamalingam C ret = intel_context_migrate_copy(i915->gt.migrate.context, 4723c2b8f32SThomas Hellström NULL, src_st->sgl, src_level, 4733c2b8f32SThomas Hellström gpu_binds_iomem(bo->resource), 4743c2b8f32SThomas Hellström dst_st->sgl, dst_level, 4753c2b8f32SThomas Hellström gpu_binds_iomem(dst_mem), 47650331a7bSRamalingam C &rq); 47750331a7bSRamalingam C if (!ret && rq) { 47850331a7bSRamalingam C i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT); 47950331a7bSRamalingam C i915_request_put(rq); 48050331a7bSRamalingam C } 48150331a7bSRamalingam C intel_engine_pm_put(i915->gt.migrate.context->engine); 48250331a7bSRamalingam C } 48350331a7bSRamalingam C 48450331a7bSRamalingam C return ret; 48550331a7bSRamalingam C } 48650331a7bSRamalingam C 487d8ac30fdSThomas Hellström static void __i915_ttm_move(struct ttm_buffer_object *bo, bool clear, 488d8ac30fdSThomas Hellström struct ttm_resource *dst_mem, 489*0d938863SThomas Hellström struct ttm_tt *dst_ttm, 490*0d938863SThomas Hellström struct sg_table *dst_st, 491*0d938863SThomas Hellström bool allow_accel) 492d8ac30fdSThomas Hellström { 493*0d938863SThomas Hellström int ret = -EINVAL; 494d8ac30fdSThomas Hellström 495*0d938863SThomas Hellström if (allow_accel) 496*0d938863SThomas Hellström ret = i915_ttm_accel_move(bo, clear, dst_mem, dst_ttm, dst_st); 497d8ac30fdSThomas Hellström if (ret) { 498d8ac30fdSThomas Hellström struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 499d8ac30fdSThomas Hellström struct intel_memory_region *dst_reg, *src_reg; 500d8ac30fdSThomas Hellström union { 501d8ac30fdSThomas Hellström struct ttm_kmap_iter_tt tt; 502d8ac30fdSThomas Hellström struct ttm_kmap_iter_iomap io; 503d8ac30fdSThomas Hellström } _dst_iter, _src_iter; 504d8ac30fdSThomas Hellström struct ttm_kmap_iter *dst_iter, *src_iter; 505d8ac30fdSThomas Hellström 506d8ac30fdSThomas Hellström dst_reg = i915_ttm_region(bo->bdev, dst_mem->mem_type); 507d8ac30fdSThomas Hellström src_reg = i915_ttm_region(bo->bdev, bo->resource->mem_type); 508d8ac30fdSThomas Hellström GEM_BUG_ON(!dst_reg || !src_reg); 509d8ac30fdSThomas Hellström 510d8ac30fdSThomas Hellström dst_iter = !cpu_maps_iomem(dst_mem) ? 511*0d938863SThomas Hellström ttm_kmap_iter_tt_init(&_dst_iter.tt, dst_ttm) : 512d8ac30fdSThomas Hellström ttm_kmap_iter_iomap_init(&_dst_iter.io, &dst_reg->iomap, 513d8ac30fdSThomas Hellström dst_st, dst_reg->region.start); 514d8ac30fdSThomas Hellström 515d8ac30fdSThomas Hellström src_iter = !cpu_maps_iomem(bo->resource) ? 516d8ac30fdSThomas Hellström ttm_kmap_iter_tt_init(&_src_iter.tt, bo->ttm) : 517d8ac30fdSThomas Hellström ttm_kmap_iter_iomap_init(&_src_iter.io, &src_reg->iomap, 518d8ac30fdSThomas Hellström obj->ttm.cached_io_st, 519d8ac30fdSThomas Hellström src_reg->region.start); 520d8ac30fdSThomas Hellström 52166907633SThomas Hellström ttm_move_memcpy(clear, dst_mem->num_pages, dst_iter, src_iter); 522d8ac30fdSThomas Hellström } 523d8ac30fdSThomas Hellström } 524d8ac30fdSThomas Hellström 525213d5092SThomas Hellström static int i915_ttm_move(struct ttm_buffer_object *bo, bool evict, 526213d5092SThomas Hellström struct ttm_operation_ctx *ctx, 527213d5092SThomas Hellström struct ttm_resource *dst_mem, 528213d5092SThomas Hellström struct ttm_place *hop) 529213d5092SThomas Hellström { 530213d5092SThomas Hellström struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 531213d5092SThomas Hellström struct ttm_resource_manager *dst_man = 532213d5092SThomas Hellström ttm_manager_type(bo->bdev, dst_mem->mem_type); 533d8ac30fdSThomas Hellström struct ttm_tt *ttm = bo->ttm; 534213d5092SThomas Hellström struct sg_table *dst_st; 535d8ac30fdSThomas Hellström bool clear; 536213d5092SThomas Hellström int ret; 537213d5092SThomas Hellström 538213d5092SThomas Hellström /* Sync for now. We could do the actual copy async. */ 539213d5092SThomas Hellström ret = ttm_bo_wait_ctx(bo, ctx); 540213d5092SThomas Hellström if (ret) 541213d5092SThomas Hellström return ret; 542213d5092SThomas Hellström 543213d5092SThomas Hellström ret = i915_ttm_move_notify(bo); 544213d5092SThomas Hellström if (ret) 545213d5092SThomas Hellström return ret; 546213d5092SThomas Hellström 547213d5092SThomas Hellström if (obj->mm.madv != I915_MADV_WILLNEED) { 548213d5092SThomas Hellström i915_ttm_purge(obj); 549213d5092SThomas Hellström ttm_resource_free(bo, &dst_mem); 550213d5092SThomas Hellström return 0; 551213d5092SThomas Hellström } 552213d5092SThomas Hellström 553213d5092SThomas Hellström /* Populate ttm with pages if needed. Typically system memory. */ 554d8ac30fdSThomas Hellström if (ttm && (dst_man->use_tt || (ttm->page_flags & TTM_PAGE_FLAG_SWAPPED))) { 555d8ac30fdSThomas Hellström ret = ttm_tt_populate(bo->bdev, ttm, ctx); 556213d5092SThomas Hellström if (ret) 557213d5092SThomas Hellström return ret; 558213d5092SThomas Hellström } 559213d5092SThomas Hellström 560213d5092SThomas Hellström dst_st = i915_ttm_resource_get_st(obj, dst_mem); 561213d5092SThomas Hellström if (IS_ERR(dst_st)) 562213d5092SThomas Hellström return PTR_ERR(dst_st); 563213d5092SThomas Hellström 564d8ac30fdSThomas Hellström clear = !cpu_maps_iomem(bo->resource) && (!ttm || !ttm_tt_is_populated(ttm)); 565d8ac30fdSThomas Hellström if (!(clear && ttm && !(ttm->page_flags & TTM_PAGE_FLAG_ZERO_ALLOC))) 566*0d938863SThomas Hellström __i915_ttm_move(bo, clear, dst_mem, bo->ttm, dst_st, true); 567213d5092SThomas Hellström 568213d5092SThomas Hellström ttm_bo_move_sync_cleanup(bo, dst_mem); 5693c2b8f32SThomas Hellström i915_ttm_adjust_domains_after_move(obj); 570213d5092SThomas Hellström i915_ttm_free_cached_io_st(obj); 571213d5092SThomas Hellström 5723c2b8f32SThomas Hellström if (gpu_binds_iomem(dst_mem) || cpu_maps_iomem(dst_mem)) { 573213d5092SThomas Hellström obj->ttm.cached_io_st = dst_st; 574cf3e3e86SMaarten Lankhorst obj->ttm.get_io_page.sg_pos = dst_st->sgl; 575cf3e3e86SMaarten Lankhorst obj->ttm.get_io_page.sg_idx = 0; 576cf3e3e86SMaarten Lankhorst } 577213d5092SThomas Hellström 5783c2b8f32SThomas Hellström i915_ttm_adjust_gem_after_move(obj); 579213d5092SThomas Hellström return 0; 580213d5092SThomas Hellström } 581213d5092SThomas Hellström 582cf3e3e86SMaarten Lankhorst static int i915_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem) 583cf3e3e86SMaarten Lankhorst { 5843c2b8f32SThomas Hellström if (!cpu_maps_iomem(mem)) 585cf3e3e86SMaarten Lankhorst return 0; 586cf3e3e86SMaarten Lankhorst 587cf3e3e86SMaarten Lankhorst mem->bus.caching = ttm_write_combined; 588cf3e3e86SMaarten Lankhorst mem->bus.is_iomem = true; 589cf3e3e86SMaarten Lankhorst 590cf3e3e86SMaarten Lankhorst return 0; 591cf3e3e86SMaarten Lankhorst } 592cf3e3e86SMaarten Lankhorst 593cf3e3e86SMaarten Lankhorst static unsigned long i915_ttm_io_mem_pfn(struct ttm_buffer_object *bo, 594cf3e3e86SMaarten Lankhorst unsigned long page_offset) 595cf3e3e86SMaarten Lankhorst { 596cf3e3e86SMaarten Lankhorst struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 597cf3e3e86SMaarten Lankhorst unsigned long base = obj->mm.region->iomap.base - obj->mm.region->region.start; 598cf3e3e86SMaarten Lankhorst struct scatterlist *sg; 599cf3e3e86SMaarten Lankhorst unsigned int ofs; 600cf3e3e86SMaarten Lankhorst 601cf3e3e86SMaarten Lankhorst GEM_WARN_ON(bo->ttm); 602cf3e3e86SMaarten Lankhorst 6037d6a276eSJason Ekstrand sg = __i915_gem_object_get_sg(obj, &obj->ttm.get_io_page, page_offset, &ofs, true); 604cf3e3e86SMaarten Lankhorst 605cf3e3e86SMaarten Lankhorst return ((base + sg_dma_address(sg)) >> PAGE_SHIFT) + ofs; 606cf3e3e86SMaarten Lankhorst } 607cf3e3e86SMaarten Lankhorst 608213d5092SThomas Hellström static struct ttm_device_funcs i915_ttm_bo_driver = { 609213d5092SThomas Hellström .ttm_tt_create = i915_ttm_tt_create, 610213d5092SThomas Hellström .ttm_tt_unpopulate = i915_ttm_tt_unpopulate, 611213d5092SThomas Hellström .ttm_tt_destroy = i915_ttm_tt_destroy, 612213d5092SThomas Hellström .eviction_valuable = i915_ttm_eviction_valuable, 613213d5092SThomas Hellström .evict_flags = i915_ttm_evict_flags, 614213d5092SThomas Hellström .move = i915_ttm_move, 615213d5092SThomas Hellström .swap_notify = i915_ttm_swap_notify, 616213d5092SThomas Hellström .delete_mem_notify = i915_ttm_delete_mem_notify, 617cf3e3e86SMaarten Lankhorst .io_mem_reserve = i915_ttm_io_mem_reserve, 618cf3e3e86SMaarten Lankhorst .io_mem_pfn = i915_ttm_io_mem_pfn, 619213d5092SThomas Hellström }; 620213d5092SThomas Hellström 621213d5092SThomas Hellström /** 622213d5092SThomas Hellström * i915_ttm_driver - Return a pointer to the TTM device funcs 623213d5092SThomas Hellström * 624213d5092SThomas Hellström * Return: Pointer to statically allocated TTM device funcs. 625213d5092SThomas Hellström */ 626213d5092SThomas Hellström struct ttm_device_funcs *i915_ttm_driver(void) 627213d5092SThomas Hellström { 628213d5092SThomas Hellström return &i915_ttm_bo_driver; 629213d5092SThomas Hellström } 630213d5092SThomas Hellström 631b6e913e1SThomas Hellström static int __i915_ttm_get_pages(struct drm_i915_gem_object *obj, 632b6e913e1SThomas Hellström struct ttm_placement *placement) 633213d5092SThomas Hellström { 634213d5092SThomas Hellström struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 635213d5092SThomas Hellström struct ttm_operation_ctx ctx = { 636213d5092SThomas Hellström .interruptible = true, 637213d5092SThomas Hellström .no_wait_gpu = false, 638213d5092SThomas Hellström }; 639213d5092SThomas Hellström struct sg_table *st; 640b07a6483SThomas Hellström int real_num_busy; 641213d5092SThomas Hellström int ret; 642213d5092SThomas Hellström 643b07a6483SThomas Hellström /* First try only the requested placement. No eviction. */ 644b6e913e1SThomas Hellström real_num_busy = fetch_and_zero(&placement->num_busy_placement); 645b6e913e1SThomas Hellström ret = ttm_bo_validate(bo, placement, &ctx); 646b07a6483SThomas Hellström if (ret) { 647b07a6483SThomas Hellström ret = i915_ttm_err_to_gem(ret); 648b07a6483SThomas Hellström /* 649b07a6483SThomas Hellström * Anything that wants to restart the operation gets to 650b07a6483SThomas Hellström * do that. 651b07a6483SThomas Hellström */ 652b07a6483SThomas Hellström if (ret == -EDEADLK || ret == -EINTR || ret == -ERESTARTSYS || 653b07a6483SThomas Hellström ret == -EAGAIN) 654b07a6483SThomas Hellström return ret; 655b07a6483SThomas Hellström 656b07a6483SThomas Hellström /* 657b07a6483SThomas Hellström * If the initial attempt fails, allow all accepted placements, 658b07a6483SThomas Hellström * evicting if necessary. 659b07a6483SThomas Hellström */ 660b6e913e1SThomas Hellström placement->num_busy_placement = real_num_busy; 661b6e913e1SThomas Hellström ret = ttm_bo_validate(bo, placement, &ctx); 662213d5092SThomas Hellström if (ret) 663b07a6483SThomas Hellström return i915_ttm_err_to_gem(ret); 664b07a6483SThomas Hellström } 665213d5092SThomas Hellström 6663c2b8f32SThomas Hellström i915_ttm_adjust_lru(obj); 6673c2b8f32SThomas Hellström if (bo->ttm && !ttm_tt_is_populated(bo->ttm)) { 6683c2b8f32SThomas Hellström ret = ttm_tt_populate(bo->bdev, bo->ttm, &ctx); 6693c2b8f32SThomas Hellström if (ret) 6703c2b8f32SThomas Hellström return ret; 6713c2b8f32SThomas Hellström 6723c2b8f32SThomas Hellström i915_ttm_adjust_domains_after_move(obj); 6733c2b8f32SThomas Hellström i915_ttm_adjust_gem_after_move(obj); 6743c2b8f32SThomas Hellström } 6753c2b8f32SThomas Hellström 67675e38285SJason Ekstrand if (!i915_gem_object_has_pages(obj)) { 677213d5092SThomas Hellström /* Object either has a page vector or is an iomem object */ 678213d5092SThomas Hellström st = bo->ttm ? i915_ttm_tt_get_st(bo->ttm) : obj->ttm.cached_io_st; 679213d5092SThomas Hellström if (IS_ERR(st)) 680213d5092SThomas Hellström return PTR_ERR(st); 681213d5092SThomas Hellström 682213d5092SThomas Hellström __i915_gem_object_set_pages(obj, st, i915_sg_dma_sizes(st->sgl)); 68375e38285SJason Ekstrand } 684213d5092SThomas Hellström 685213d5092SThomas Hellström return ret; 686213d5092SThomas Hellström } 687213d5092SThomas Hellström 688b6e913e1SThomas Hellström static int i915_ttm_get_pages(struct drm_i915_gem_object *obj) 689b6e913e1SThomas Hellström { 690b6e913e1SThomas Hellström struct ttm_place requested, busy[I915_TTM_MAX_PLACEMENTS]; 691b6e913e1SThomas Hellström struct ttm_placement placement; 692b6e913e1SThomas Hellström 693b6e913e1SThomas Hellström GEM_BUG_ON(obj->mm.n_placements > I915_TTM_MAX_PLACEMENTS); 694b6e913e1SThomas Hellström 695b6e913e1SThomas Hellström /* Move to the requested placement. */ 696b6e913e1SThomas Hellström i915_ttm_placement_from_obj(obj, &requested, busy, &placement); 697b6e913e1SThomas Hellström 698b6e913e1SThomas Hellström return __i915_ttm_get_pages(obj, &placement); 699b6e913e1SThomas Hellström } 700b6e913e1SThomas Hellström 701b6e913e1SThomas Hellström /** 702b6e913e1SThomas Hellström * DOC: Migration vs eviction 703b6e913e1SThomas Hellström * 704b6e913e1SThomas Hellström * GEM migration may not be the same as TTM migration / eviction. If 705b6e913e1SThomas Hellström * the TTM core decides to evict an object it may be evicted to a 706b6e913e1SThomas Hellström * TTM memory type that is not in the object's allowable GEM regions, or 707b6e913e1SThomas Hellström * in fact theoretically to a TTM memory type that doesn't correspond to 708b6e913e1SThomas Hellström * a GEM memory region. In that case the object's GEM region is not 709b6e913e1SThomas Hellström * updated, and the data is migrated back to the GEM region at 710b6e913e1SThomas Hellström * get_pages time. TTM may however set up CPU ptes to the object even 711b6e913e1SThomas Hellström * when it is evicted. 712b6e913e1SThomas Hellström * Gem forced migration using the i915_ttm_migrate() op, is allowed even 713b6e913e1SThomas Hellström * to regions that are not in the object's list of allowable placements. 714b6e913e1SThomas Hellström */ 715b6e913e1SThomas Hellström static int i915_ttm_migrate(struct drm_i915_gem_object *obj, 716b6e913e1SThomas Hellström struct intel_memory_region *mr) 717b6e913e1SThomas Hellström { 718b6e913e1SThomas Hellström struct ttm_place requested; 719b6e913e1SThomas Hellström struct ttm_placement placement; 720b6e913e1SThomas Hellström int ret; 721b6e913e1SThomas Hellström 722b6e913e1SThomas Hellström i915_ttm_place_from_region(mr, &requested, obj->flags); 723b6e913e1SThomas Hellström placement.num_placement = 1; 724b6e913e1SThomas Hellström placement.num_busy_placement = 1; 725b6e913e1SThomas Hellström placement.placement = &requested; 726b6e913e1SThomas Hellström placement.busy_placement = &requested; 727b6e913e1SThomas Hellström 728b6e913e1SThomas Hellström ret = __i915_ttm_get_pages(obj, &placement); 729b6e913e1SThomas Hellström if (ret) 730b6e913e1SThomas Hellström return ret; 731b6e913e1SThomas Hellström 732b6e913e1SThomas Hellström /* 733b6e913e1SThomas Hellström * Reinitialize the region bindings. This is primarily 734b6e913e1SThomas Hellström * required for objects where the new region is not in 735b6e913e1SThomas Hellström * its allowable placements. 736b6e913e1SThomas Hellström */ 737b6e913e1SThomas Hellström if (obj->mm.region != mr) { 738b6e913e1SThomas Hellström i915_gem_object_release_memory_region(obj); 739b6e913e1SThomas Hellström i915_gem_object_init_memory_region(obj, mr); 740b6e913e1SThomas Hellström } 741b6e913e1SThomas Hellström 742b6e913e1SThomas Hellström return 0; 743b6e913e1SThomas Hellström } 744b6e913e1SThomas Hellström 745213d5092SThomas Hellström static void i915_ttm_put_pages(struct drm_i915_gem_object *obj, 746213d5092SThomas Hellström struct sg_table *st) 747213d5092SThomas Hellström { 748213d5092SThomas Hellström /* 749213d5092SThomas Hellström * We're currently not called from a shrinker, so put_pages() 750213d5092SThomas Hellström * typically means the object is about to destroyed, or called 751213d5092SThomas Hellström * from move_notify(). So just avoid doing much for now. 752213d5092SThomas Hellström * If the object is not destroyed next, The TTM eviction logic 753213d5092SThomas Hellström * and shrinkers will move it out if needed. 754213d5092SThomas Hellström */ 755213d5092SThomas Hellström 756213d5092SThomas Hellström i915_ttm_adjust_lru(obj); 757213d5092SThomas Hellström } 758213d5092SThomas Hellström 759213d5092SThomas Hellström static void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj) 760213d5092SThomas Hellström { 761213d5092SThomas Hellström struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 762213d5092SThomas Hellström 763213d5092SThomas Hellström /* 764213d5092SThomas Hellström * Don't manipulate the TTM LRUs while in TTM bo destruction. 765213d5092SThomas Hellström * We're called through i915_ttm_delete_mem_notify(). 766213d5092SThomas Hellström */ 767213d5092SThomas Hellström if (!kref_read(&bo->kref)) 768213d5092SThomas Hellström return; 769213d5092SThomas Hellström 770213d5092SThomas Hellström /* 771213d5092SThomas Hellström * Put on the correct LRU list depending on the MADV status 772213d5092SThomas Hellström */ 773213d5092SThomas Hellström spin_lock(&bo->bdev->lru_lock); 774213d5092SThomas Hellström if (obj->mm.madv != I915_MADV_WILLNEED) { 775213d5092SThomas Hellström bo->priority = I915_TTM_PRIO_PURGE; 776213d5092SThomas Hellström } else if (!i915_gem_object_has_pages(obj)) { 777213d5092SThomas Hellström if (bo->priority < I915_TTM_PRIO_HAS_PAGES) 778213d5092SThomas Hellström bo->priority = I915_TTM_PRIO_HAS_PAGES; 779213d5092SThomas Hellström } else { 780213d5092SThomas Hellström if (bo->priority > I915_TTM_PRIO_NO_PAGES) 781213d5092SThomas Hellström bo->priority = I915_TTM_PRIO_NO_PAGES; 782213d5092SThomas Hellström } 783213d5092SThomas Hellström 784213d5092SThomas Hellström ttm_bo_move_to_lru_tail(bo, bo->resource, NULL); 785213d5092SThomas Hellström spin_unlock(&bo->bdev->lru_lock); 786213d5092SThomas Hellström } 787213d5092SThomas Hellström 788213d5092SThomas Hellström /* 789213d5092SThomas Hellström * TTM-backed gem object destruction requires some clarification. 790213d5092SThomas Hellström * Basically we have two possibilities here. We can either rely on the 791213d5092SThomas Hellström * i915 delayed destruction and put the TTM object when the object 792213d5092SThomas Hellström * is idle. This would be detected by TTM which would bypass the 793213d5092SThomas Hellström * TTM delayed destroy handling. The other approach is to put the TTM 794213d5092SThomas Hellström * object early and rely on the TTM destroyed handling, and then free 795213d5092SThomas Hellström * the leftover parts of the GEM object once TTM's destroyed list handling is 796213d5092SThomas Hellström * complete. For now, we rely on the latter for two reasons: 797213d5092SThomas Hellström * a) TTM can evict an object even when it's on the delayed destroy list, 798213d5092SThomas Hellström * which in theory allows for complete eviction. 799213d5092SThomas Hellström * b) There is work going on in TTM to allow freeing an object even when 800213d5092SThomas Hellström * it's not idle, and using the TTM destroyed list handling could help us 801213d5092SThomas Hellström * benefit from that. 802213d5092SThomas Hellström */ 803213d5092SThomas Hellström static void i915_ttm_delayed_free(struct drm_i915_gem_object *obj) 804213d5092SThomas Hellström { 805213d5092SThomas Hellström if (obj->ttm.created) { 806213d5092SThomas Hellström ttm_bo_put(i915_gem_to_ttm(obj)); 807213d5092SThomas Hellström } else { 808213d5092SThomas Hellström __i915_gem_free_object(obj); 809213d5092SThomas Hellström call_rcu(&obj->rcu, __i915_gem_free_object_rcu); 810213d5092SThomas Hellström } 811213d5092SThomas Hellström } 812213d5092SThomas Hellström 813cf3e3e86SMaarten Lankhorst static vm_fault_t vm_fault_ttm(struct vm_fault *vmf) 814cf3e3e86SMaarten Lankhorst { 815cf3e3e86SMaarten Lankhorst struct vm_area_struct *area = vmf->vma; 816cf3e3e86SMaarten Lankhorst struct drm_i915_gem_object *obj = 817cf3e3e86SMaarten Lankhorst i915_ttm_to_gem(area->vm_private_data); 818cf3e3e86SMaarten Lankhorst 819cf3e3e86SMaarten Lankhorst /* Sanity check that we allow writing into this object */ 820cf3e3e86SMaarten Lankhorst if (unlikely(i915_gem_object_is_readonly(obj) && 821cf3e3e86SMaarten Lankhorst area->vm_flags & VM_WRITE)) 822cf3e3e86SMaarten Lankhorst return VM_FAULT_SIGBUS; 823cf3e3e86SMaarten Lankhorst 824cf3e3e86SMaarten Lankhorst return ttm_bo_vm_fault(vmf); 825cf3e3e86SMaarten Lankhorst } 826cf3e3e86SMaarten Lankhorst 827cf3e3e86SMaarten Lankhorst static int 828cf3e3e86SMaarten Lankhorst vm_access_ttm(struct vm_area_struct *area, unsigned long addr, 829cf3e3e86SMaarten Lankhorst void *buf, int len, int write) 830cf3e3e86SMaarten Lankhorst { 831cf3e3e86SMaarten Lankhorst struct drm_i915_gem_object *obj = 832cf3e3e86SMaarten Lankhorst i915_ttm_to_gem(area->vm_private_data); 833cf3e3e86SMaarten Lankhorst 834cf3e3e86SMaarten Lankhorst if (i915_gem_object_is_readonly(obj) && write) 835cf3e3e86SMaarten Lankhorst return -EACCES; 836cf3e3e86SMaarten Lankhorst 837cf3e3e86SMaarten Lankhorst return ttm_bo_vm_access(area, addr, buf, len, write); 838cf3e3e86SMaarten Lankhorst } 839cf3e3e86SMaarten Lankhorst 840cf3e3e86SMaarten Lankhorst static void ttm_vm_open(struct vm_area_struct *vma) 841cf3e3e86SMaarten Lankhorst { 842cf3e3e86SMaarten Lankhorst struct drm_i915_gem_object *obj = 843cf3e3e86SMaarten Lankhorst i915_ttm_to_gem(vma->vm_private_data); 844cf3e3e86SMaarten Lankhorst 845cf3e3e86SMaarten Lankhorst GEM_BUG_ON(!obj); 846cf3e3e86SMaarten Lankhorst i915_gem_object_get(obj); 847cf3e3e86SMaarten Lankhorst } 848cf3e3e86SMaarten Lankhorst 849cf3e3e86SMaarten Lankhorst static void ttm_vm_close(struct vm_area_struct *vma) 850cf3e3e86SMaarten Lankhorst { 851cf3e3e86SMaarten Lankhorst struct drm_i915_gem_object *obj = 852cf3e3e86SMaarten Lankhorst i915_ttm_to_gem(vma->vm_private_data); 853cf3e3e86SMaarten Lankhorst 854cf3e3e86SMaarten Lankhorst GEM_BUG_ON(!obj); 855cf3e3e86SMaarten Lankhorst i915_gem_object_put(obj); 856cf3e3e86SMaarten Lankhorst } 857cf3e3e86SMaarten Lankhorst 858cf3e3e86SMaarten Lankhorst static const struct vm_operations_struct vm_ops_ttm = { 859cf3e3e86SMaarten Lankhorst .fault = vm_fault_ttm, 860cf3e3e86SMaarten Lankhorst .access = vm_access_ttm, 861cf3e3e86SMaarten Lankhorst .open = ttm_vm_open, 862cf3e3e86SMaarten Lankhorst .close = ttm_vm_close, 863cf3e3e86SMaarten Lankhorst }; 864cf3e3e86SMaarten Lankhorst 865cf3e3e86SMaarten Lankhorst static u64 i915_ttm_mmap_offset(struct drm_i915_gem_object *obj) 866cf3e3e86SMaarten Lankhorst { 867cf3e3e86SMaarten Lankhorst /* The ttm_bo must be allocated with I915_BO_ALLOC_USER */ 868cf3e3e86SMaarten Lankhorst GEM_BUG_ON(!drm_mm_node_allocated(&obj->base.vma_node.vm_node)); 869cf3e3e86SMaarten Lankhorst 870cf3e3e86SMaarten Lankhorst return drm_vma_node_offset_addr(&obj->base.vma_node); 871cf3e3e86SMaarten Lankhorst } 872cf3e3e86SMaarten Lankhorst 8734bc2d574SMatthew Auld static const struct drm_i915_gem_object_ops i915_gem_ttm_obj_ops = { 874213d5092SThomas Hellström .name = "i915_gem_object_ttm", 875213d5092SThomas Hellström 876213d5092SThomas Hellström .get_pages = i915_ttm_get_pages, 877213d5092SThomas Hellström .put_pages = i915_ttm_put_pages, 878213d5092SThomas Hellström .truncate = i915_ttm_purge, 879213d5092SThomas Hellström .adjust_lru = i915_ttm_adjust_lru, 880213d5092SThomas Hellström .delayed_free = i915_ttm_delayed_free, 881b6e913e1SThomas Hellström .migrate = i915_ttm_migrate, 882cf3e3e86SMaarten Lankhorst .mmap_offset = i915_ttm_mmap_offset, 883cf3e3e86SMaarten Lankhorst .mmap_ops = &vm_ops_ttm, 884213d5092SThomas Hellström }; 885213d5092SThomas Hellström 886213d5092SThomas Hellström void i915_ttm_bo_destroy(struct ttm_buffer_object *bo) 887213d5092SThomas Hellström { 888213d5092SThomas Hellström struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 889213d5092SThomas Hellström 89048b09612SMaarten Lankhorst /* This releases all gem object bindings to the backend. */ 89148b09612SMaarten Lankhorst __i915_gem_free_object(obj); 89248b09612SMaarten Lankhorst 893213d5092SThomas Hellström i915_gem_object_release_memory_region(obj); 894cf3e3e86SMaarten Lankhorst mutex_destroy(&obj->ttm.get_io_page.lock); 89548b09612SMaarten Lankhorst 896213d5092SThomas Hellström if (obj->ttm.created) 897213d5092SThomas Hellström call_rcu(&obj->rcu, __i915_gem_free_object_rcu); 898213d5092SThomas Hellström } 899213d5092SThomas Hellström 900213d5092SThomas Hellström /** 901213d5092SThomas Hellström * __i915_gem_ttm_object_init - Initialize a ttm-backed i915 gem object 902213d5092SThomas Hellström * @mem: The initial memory region for the object. 903213d5092SThomas Hellström * @obj: The gem object. 904213d5092SThomas Hellström * @size: Object size in bytes. 905213d5092SThomas Hellström * @flags: gem object flags. 906213d5092SThomas Hellström * 907213d5092SThomas Hellström * Return: 0 on success, negative error code on failure. 908213d5092SThomas Hellström */ 909213d5092SThomas Hellström int __i915_gem_ttm_object_init(struct intel_memory_region *mem, 910213d5092SThomas Hellström struct drm_i915_gem_object *obj, 911213d5092SThomas Hellström resource_size_t size, 912d22632c8SMatthew Auld resource_size_t page_size, 913213d5092SThomas Hellström unsigned int flags) 914213d5092SThomas Hellström { 915213d5092SThomas Hellström static struct lock_class_key lock_class; 916213d5092SThomas Hellström struct drm_i915_private *i915 = mem->i915; 9173c2b8f32SThomas Hellström struct ttm_operation_ctx ctx = { 9183c2b8f32SThomas Hellström .interruptible = true, 9193c2b8f32SThomas Hellström .no_wait_gpu = false, 9203c2b8f32SThomas Hellström }; 921213d5092SThomas Hellström enum ttm_bo_type bo_type; 922213d5092SThomas Hellström int ret; 923213d5092SThomas Hellström 924213d5092SThomas Hellström drm_gem_private_object_init(&i915->drm, &obj->base, size); 925213d5092SThomas Hellström i915_gem_object_init(obj, &i915_gem_ttm_obj_ops, &lock_class, flags); 926213d5092SThomas Hellström i915_gem_object_init_memory_region(obj, mem); 927213d5092SThomas Hellström i915_gem_object_make_unshrinkable(obj); 928cf3e3e86SMaarten Lankhorst INIT_RADIX_TREE(&obj->ttm.get_io_page.radix, GFP_KERNEL | __GFP_NOWARN); 929cf3e3e86SMaarten Lankhorst mutex_init(&obj->ttm.get_io_page.lock); 930213d5092SThomas Hellström bo_type = (obj->flags & I915_BO_ALLOC_USER) ? ttm_bo_type_device : 931213d5092SThomas Hellström ttm_bo_type_kernel; 932213d5092SThomas Hellström 9333c2b8f32SThomas Hellström obj->base.vma_node.driver_private = i915_gem_to_ttm(obj); 9343c2b8f32SThomas Hellström 935d22632c8SMatthew Auld /* Forcing the page size is kernel internal only */ 936d22632c8SMatthew Auld GEM_BUG_ON(page_size && obj->mm.n_placements); 937d22632c8SMatthew Auld 938213d5092SThomas Hellström /* 939213d5092SThomas Hellström * If this function fails, it will call the destructor, but 940213d5092SThomas Hellström * our caller still owns the object. So no freeing in the 941213d5092SThomas Hellström * destructor until obj->ttm.created is true. 942213d5092SThomas Hellström * Similarly, in delayed_destroy, we can't call ttm_bo_put() 943213d5092SThomas Hellström * until successful initialization. 944213d5092SThomas Hellström */ 9453c2b8f32SThomas Hellström ret = ttm_bo_init_reserved(&i915->bdev, i915_gem_to_ttm(obj), size, 94613c2ceb6SMatthew Auld bo_type, &i915_sys_placement, 947d22632c8SMatthew Auld page_size >> PAGE_SHIFT, 9483c2b8f32SThomas Hellström &ctx, NULL, NULL, i915_ttm_bo_destroy); 9493c2b8f32SThomas Hellström if (ret) 950b07a6483SThomas Hellström return i915_ttm_err_to_gem(ret); 9513c2b8f32SThomas Hellström 9523c2b8f32SThomas Hellström obj->ttm.created = true; 9533c2b8f32SThomas Hellström i915_ttm_adjust_domains_after_move(obj); 9543c2b8f32SThomas Hellström i915_ttm_adjust_gem_after_move(obj); 9553c2b8f32SThomas Hellström i915_gem_object_unlock(obj); 9563c2b8f32SThomas Hellström 9573c2b8f32SThomas Hellström return 0; 958213d5092SThomas Hellström } 95932b7cf51SThomas Hellström 96032b7cf51SThomas Hellström static const struct intel_memory_region_ops ttm_system_region_ops = { 96132b7cf51SThomas Hellström .init_object = __i915_gem_ttm_object_init, 96232b7cf51SThomas Hellström }; 96332b7cf51SThomas Hellström 96432b7cf51SThomas Hellström struct intel_memory_region * 96532b7cf51SThomas Hellström i915_gem_ttm_system_setup(struct drm_i915_private *i915, 96632b7cf51SThomas Hellström u16 type, u16 instance) 96732b7cf51SThomas Hellström { 96832b7cf51SThomas Hellström struct intel_memory_region *mr; 96932b7cf51SThomas Hellström 97032b7cf51SThomas Hellström mr = intel_memory_region_create(i915, 0, 97132b7cf51SThomas Hellström totalram_pages() << PAGE_SHIFT, 97232b7cf51SThomas Hellström PAGE_SIZE, 0, 97332b7cf51SThomas Hellström type, instance, 97432b7cf51SThomas Hellström &ttm_system_region_ops); 97532b7cf51SThomas Hellström if (IS_ERR(mr)) 97632b7cf51SThomas Hellström return mr; 97732b7cf51SThomas Hellström 97832b7cf51SThomas Hellström intel_memory_region_set_name(mr, "system-ttm"); 97932b7cf51SThomas Hellström return mr; 98032b7cf51SThomas Hellström } 981*0d938863SThomas Hellström 982*0d938863SThomas Hellström /** 983*0d938863SThomas Hellström * i915_gem_obj_copy_ttm - Copy the contents of one ttm-based gem object to 984*0d938863SThomas Hellström * another 985*0d938863SThomas Hellström * @dst: The destination object 986*0d938863SThomas Hellström * @src: The source object 987*0d938863SThomas Hellström * @allow_accel: Allow using the blitter. Otherwise TTM memcpy is used. 988*0d938863SThomas Hellström * @intr: Whether to perform waits interruptible: 989*0d938863SThomas Hellström * 990*0d938863SThomas Hellström * Note: The caller is responsible for assuring that the underlying 991*0d938863SThomas Hellström * TTM objects are populated if needed and locked. 992*0d938863SThomas Hellström * 993*0d938863SThomas Hellström * Return: Zero on success. Negative error code on error. If @intr == true, 994*0d938863SThomas Hellström * then it may return -ERESTARTSYS or -EINTR. 995*0d938863SThomas Hellström */ 996*0d938863SThomas Hellström int i915_gem_obj_copy_ttm(struct drm_i915_gem_object *dst, 997*0d938863SThomas Hellström struct drm_i915_gem_object *src, 998*0d938863SThomas Hellström bool allow_accel, bool intr) 999*0d938863SThomas Hellström { 1000*0d938863SThomas Hellström struct ttm_buffer_object *dst_bo = i915_gem_to_ttm(dst); 1001*0d938863SThomas Hellström struct ttm_buffer_object *src_bo = i915_gem_to_ttm(src); 1002*0d938863SThomas Hellström struct ttm_operation_ctx ctx = { 1003*0d938863SThomas Hellström .interruptible = intr, 1004*0d938863SThomas Hellström }; 1005*0d938863SThomas Hellström struct sg_table *dst_st; 1006*0d938863SThomas Hellström int ret; 1007*0d938863SThomas Hellström 1008*0d938863SThomas Hellström assert_object_held(dst); 1009*0d938863SThomas Hellström assert_object_held(src); 1010*0d938863SThomas Hellström 1011*0d938863SThomas Hellström /* 1012*0d938863SThomas Hellström * Sync for now. This will change with async moves. 1013*0d938863SThomas Hellström */ 1014*0d938863SThomas Hellström ret = ttm_bo_wait_ctx(dst_bo, &ctx); 1015*0d938863SThomas Hellström if (!ret) 1016*0d938863SThomas Hellström ret = ttm_bo_wait_ctx(src_bo, &ctx); 1017*0d938863SThomas Hellström if (ret) 1018*0d938863SThomas Hellström return ret; 1019*0d938863SThomas Hellström 1020*0d938863SThomas Hellström dst_st = gpu_binds_iomem(dst_bo->resource) ? 1021*0d938863SThomas Hellström dst->ttm.cached_io_st : i915_ttm_tt_get_st(dst_bo->ttm); 1022*0d938863SThomas Hellström 1023*0d938863SThomas Hellström __i915_ttm_move(src_bo, false, dst_bo->resource, dst_bo->ttm, 1024*0d938863SThomas Hellström dst_st, allow_accel); 1025*0d938863SThomas Hellström 1026*0d938863SThomas Hellström return 0; 1027*0d938863SThomas Hellström } 1028