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 359213d5092SThomas Hellström if (likely(obj)) { 360213d5092SThomas Hellström /* This releases all gem object bindings to the backend. */ 361c865204eSThomas Hellström i915_ttm_free_cached_io_st(obj); 362213d5092SThomas Hellström __i915_gem_free_object(obj); 363213d5092SThomas Hellström } 364213d5092SThomas Hellström } 365213d5092SThomas Hellström 366213d5092SThomas Hellström static struct intel_memory_region * 367213d5092SThomas Hellström i915_ttm_region(struct ttm_device *bdev, int ttm_mem_type) 368213d5092SThomas Hellström { 369213d5092SThomas Hellström struct drm_i915_private *i915 = container_of(bdev, typeof(*i915), bdev); 370213d5092SThomas Hellström 371213d5092SThomas Hellström /* There's some room for optimization here... */ 372213d5092SThomas Hellström GEM_BUG_ON(ttm_mem_type != I915_PL_SYSTEM && 373213d5092SThomas Hellström ttm_mem_type < I915_PL_LMEM0); 374213d5092SThomas Hellström if (ttm_mem_type == I915_PL_SYSTEM) 375213d5092SThomas Hellström return intel_memory_region_lookup(i915, INTEL_MEMORY_SYSTEM, 376213d5092SThomas Hellström 0); 377213d5092SThomas Hellström 378213d5092SThomas Hellström return intel_memory_region_lookup(i915, INTEL_MEMORY_LOCAL, 379213d5092SThomas Hellström ttm_mem_type - I915_PL_LMEM0); 380213d5092SThomas Hellström } 381213d5092SThomas Hellström 382213d5092SThomas Hellström static struct sg_table *i915_ttm_tt_get_st(struct ttm_tt *ttm) 383213d5092SThomas Hellström { 384213d5092SThomas Hellström struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm); 385213d5092SThomas Hellström struct scatterlist *sg; 386213d5092SThomas Hellström struct sg_table *st; 387213d5092SThomas Hellström int ret; 388213d5092SThomas Hellström 389213d5092SThomas Hellström if (i915_tt->cached_st) 390213d5092SThomas Hellström return i915_tt->cached_st; 391213d5092SThomas Hellström 392213d5092SThomas Hellström st = kzalloc(sizeof(*st), GFP_KERNEL); 393213d5092SThomas Hellström if (!st) 394213d5092SThomas Hellström return ERR_PTR(-ENOMEM); 395213d5092SThomas Hellström 396213d5092SThomas Hellström sg = __sg_alloc_table_from_pages 397213d5092SThomas Hellström (st, ttm->pages, ttm->num_pages, 0, 398213d5092SThomas Hellström (unsigned long)ttm->num_pages << PAGE_SHIFT, 399213d5092SThomas Hellström i915_sg_segment_size(), NULL, 0, GFP_KERNEL); 400213d5092SThomas Hellström if (IS_ERR(sg)) { 401213d5092SThomas Hellström kfree(st); 402213d5092SThomas Hellström return ERR_CAST(sg); 403213d5092SThomas Hellström } 404213d5092SThomas Hellström 405213d5092SThomas Hellström ret = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0); 406213d5092SThomas Hellström if (ret) { 407213d5092SThomas Hellström sg_free_table(st); 408213d5092SThomas Hellström kfree(st); 409213d5092SThomas Hellström return ERR_PTR(ret); 410213d5092SThomas Hellström } 411213d5092SThomas Hellström 412213d5092SThomas Hellström i915_tt->cached_st = st; 413213d5092SThomas Hellström return st; 414213d5092SThomas Hellström } 415213d5092SThomas Hellström 416213d5092SThomas Hellström static struct sg_table * 417213d5092SThomas Hellström i915_ttm_resource_get_st(struct drm_i915_gem_object *obj, 418213d5092SThomas Hellström struct ttm_resource *res) 419213d5092SThomas Hellström { 420213d5092SThomas Hellström struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 421213d5092SThomas Hellström 4223c2b8f32SThomas Hellström if (!gpu_binds_iomem(res)) 423213d5092SThomas Hellström return i915_ttm_tt_get_st(bo->ttm); 424213d5092SThomas Hellström 4253c2b8f32SThomas Hellström /* 4263c2b8f32SThomas Hellström * If CPU mapping differs, we need to add the ttm_tt pages to 4273c2b8f32SThomas Hellström * the resulting st. Might make sense for GGTT. 4283c2b8f32SThomas Hellström */ 4293c2b8f32SThomas Hellström GEM_WARN_ON(!cpu_maps_iomem(res)); 430687c7d0fSMatthew Auld return intel_region_ttm_resource_to_st(obj->mm.region, res); 431213d5092SThomas Hellström } 432213d5092SThomas Hellström 43350331a7bSRamalingam C static int i915_ttm_accel_move(struct ttm_buffer_object *bo, 43450331a7bSRamalingam C struct ttm_resource *dst_mem, 43550331a7bSRamalingam C struct sg_table *dst_st) 43650331a7bSRamalingam C { 43750331a7bSRamalingam C struct drm_i915_private *i915 = container_of(bo->bdev, typeof(*i915), 43850331a7bSRamalingam C bdev); 43950331a7bSRamalingam C struct ttm_resource_manager *src_man = 44050331a7bSRamalingam C ttm_manager_type(bo->bdev, bo->resource->mem_type); 44150331a7bSRamalingam C struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 44250331a7bSRamalingam C struct sg_table *src_st; 44350331a7bSRamalingam C struct i915_request *rq; 4443c2b8f32SThomas Hellström struct ttm_tt *ttm = bo->ttm; 4453c2b8f32SThomas Hellström enum i915_cache_level src_level, dst_level; 44650331a7bSRamalingam C int ret; 44750331a7bSRamalingam C 44850331a7bSRamalingam C if (!i915->gt.migrate.context) 44950331a7bSRamalingam C return -EINVAL; 45050331a7bSRamalingam C 4513c2b8f32SThomas Hellström dst_level = i915_ttm_cache_level(i915, dst_mem, ttm); 4523c2b8f32SThomas Hellström if (!ttm || !ttm_tt_is_populated(ttm)) { 45350331a7bSRamalingam C if (bo->type == ttm_bo_type_kernel) 45450331a7bSRamalingam C return -EINVAL; 45550331a7bSRamalingam C 4563c2b8f32SThomas Hellström if (ttm && !(ttm->page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)) 45750331a7bSRamalingam C return 0; 45850331a7bSRamalingam C 45950331a7bSRamalingam C intel_engine_pm_get(i915->gt.migrate.context->engine); 46050331a7bSRamalingam C ret = intel_context_migrate_clear(i915->gt.migrate.context, NULL, 4613c2b8f32SThomas Hellström dst_st->sgl, dst_level, 4623c2b8f32SThomas Hellström gpu_binds_iomem(dst_mem), 46350331a7bSRamalingam C 0, &rq); 46450331a7bSRamalingam C 46550331a7bSRamalingam C if (!ret && rq) { 46650331a7bSRamalingam C i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT); 46750331a7bSRamalingam C i915_request_put(rq); 46850331a7bSRamalingam C } 46950331a7bSRamalingam C intel_engine_pm_put(i915->gt.migrate.context->engine); 47050331a7bSRamalingam C } else { 4713c2b8f32SThomas Hellström src_st = src_man->use_tt ? i915_ttm_tt_get_st(ttm) : 47250331a7bSRamalingam C obj->ttm.cached_io_st; 47350331a7bSRamalingam C 4743c2b8f32SThomas Hellström src_level = i915_ttm_cache_level(i915, bo->resource, ttm); 47550331a7bSRamalingam C intel_engine_pm_get(i915->gt.migrate.context->engine); 47650331a7bSRamalingam C ret = intel_context_migrate_copy(i915->gt.migrate.context, 4773c2b8f32SThomas Hellström NULL, src_st->sgl, src_level, 4783c2b8f32SThomas Hellström gpu_binds_iomem(bo->resource), 4793c2b8f32SThomas Hellström dst_st->sgl, dst_level, 4803c2b8f32SThomas Hellström gpu_binds_iomem(dst_mem), 48150331a7bSRamalingam C &rq); 48250331a7bSRamalingam C if (!ret && rq) { 48350331a7bSRamalingam C i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT); 48450331a7bSRamalingam C i915_request_put(rq); 48550331a7bSRamalingam C } 48650331a7bSRamalingam C intel_engine_pm_put(i915->gt.migrate.context->engine); 48750331a7bSRamalingam C } 48850331a7bSRamalingam C 48950331a7bSRamalingam C return ret; 49050331a7bSRamalingam C } 49150331a7bSRamalingam C 492213d5092SThomas Hellström static int i915_ttm_move(struct ttm_buffer_object *bo, bool evict, 493213d5092SThomas Hellström struct ttm_operation_ctx *ctx, 494213d5092SThomas Hellström struct ttm_resource *dst_mem, 495213d5092SThomas Hellström struct ttm_place *hop) 496213d5092SThomas Hellström { 497213d5092SThomas Hellström struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 498213d5092SThomas Hellström struct ttm_resource_manager *dst_man = 499213d5092SThomas Hellström ttm_manager_type(bo->bdev, dst_mem->mem_type); 500213d5092SThomas Hellström struct intel_memory_region *dst_reg, *src_reg; 501213d5092SThomas Hellström union { 502213d5092SThomas Hellström struct ttm_kmap_iter_tt tt; 503213d5092SThomas Hellström struct ttm_kmap_iter_iomap io; 504213d5092SThomas Hellström } _dst_iter, _src_iter; 505213d5092SThomas Hellström struct ttm_kmap_iter *dst_iter, *src_iter; 506213d5092SThomas Hellström struct sg_table *dst_st; 507213d5092SThomas Hellström int ret; 508213d5092SThomas Hellström 509213d5092SThomas Hellström dst_reg = i915_ttm_region(bo->bdev, dst_mem->mem_type); 510213d5092SThomas Hellström src_reg = i915_ttm_region(bo->bdev, bo->resource->mem_type); 511213d5092SThomas Hellström GEM_BUG_ON(!dst_reg || !src_reg); 512213d5092SThomas Hellström 513213d5092SThomas Hellström /* Sync for now. We could do the actual copy async. */ 514213d5092SThomas Hellström ret = ttm_bo_wait_ctx(bo, ctx); 515213d5092SThomas Hellström if (ret) 516213d5092SThomas Hellström return ret; 517213d5092SThomas Hellström 518213d5092SThomas Hellström ret = i915_ttm_move_notify(bo); 519213d5092SThomas Hellström if (ret) 520213d5092SThomas Hellström return ret; 521213d5092SThomas Hellström 522213d5092SThomas Hellström if (obj->mm.madv != I915_MADV_WILLNEED) { 523213d5092SThomas Hellström i915_ttm_purge(obj); 524213d5092SThomas Hellström ttm_resource_free(bo, &dst_mem); 525213d5092SThomas Hellström return 0; 526213d5092SThomas Hellström } 527213d5092SThomas Hellström 528213d5092SThomas Hellström /* Populate ttm with pages if needed. Typically system memory. */ 529213d5092SThomas Hellström if (bo->ttm && (dst_man->use_tt || 530213d5092SThomas Hellström (bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED))) { 531213d5092SThomas Hellström ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx); 532213d5092SThomas Hellström if (ret) 533213d5092SThomas Hellström return ret; 534213d5092SThomas Hellström } 535213d5092SThomas Hellström 536213d5092SThomas Hellström dst_st = i915_ttm_resource_get_st(obj, dst_mem); 537213d5092SThomas Hellström if (IS_ERR(dst_st)) 538213d5092SThomas Hellström return PTR_ERR(dst_st); 539213d5092SThomas Hellström 54050331a7bSRamalingam C ret = i915_ttm_accel_move(bo, dst_mem, dst_st); 54150331a7bSRamalingam C if (ret) { 542213d5092SThomas Hellström /* If we start mapping GGTT, we can no longer use man::use_tt here. */ 5433c2b8f32SThomas Hellström dst_iter = !cpu_maps_iomem(dst_mem) ? 544213d5092SThomas Hellström ttm_kmap_iter_tt_init(&_dst_iter.tt, bo->ttm) : 545213d5092SThomas Hellström ttm_kmap_iter_iomap_init(&_dst_iter.io, &dst_reg->iomap, 546213d5092SThomas Hellström dst_st, dst_reg->region.start); 547213d5092SThomas Hellström 5483c2b8f32SThomas Hellström src_iter = !cpu_maps_iomem(bo->resource) ? 549213d5092SThomas Hellström ttm_kmap_iter_tt_init(&_src_iter.tt, bo->ttm) : 550213d5092SThomas Hellström ttm_kmap_iter_iomap_init(&_src_iter.io, &src_reg->iomap, 551213d5092SThomas Hellström obj->ttm.cached_io_st, 552213d5092SThomas Hellström src_reg->region.start); 553213d5092SThomas Hellström 554213d5092SThomas Hellström ttm_move_memcpy(bo, dst_mem->num_pages, dst_iter, src_iter); 55550331a7bSRamalingam C } 5563c2b8f32SThomas Hellström /* Below dst_mem becomes bo->resource. */ 557213d5092SThomas Hellström ttm_bo_move_sync_cleanup(bo, dst_mem); 5583c2b8f32SThomas Hellström i915_ttm_adjust_domains_after_move(obj); 559213d5092SThomas Hellström i915_ttm_free_cached_io_st(obj); 560213d5092SThomas Hellström 5613c2b8f32SThomas Hellström if (gpu_binds_iomem(dst_mem) || cpu_maps_iomem(dst_mem)) { 562213d5092SThomas Hellström obj->ttm.cached_io_st = dst_st; 563cf3e3e86SMaarten Lankhorst obj->ttm.get_io_page.sg_pos = dst_st->sgl; 564cf3e3e86SMaarten Lankhorst obj->ttm.get_io_page.sg_idx = 0; 565cf3e3e86SMaarten Lankhorst } 566213d5092SThomas Hellström 5673c2b8f32SThomas Hellström i915_ttm_adjust_gem_after_move(obj); 568213d5092SThomas Hellström return 0; 569213d5092SThomas Hellström } 570213d5092SThomas Hellström 571cf3e3e86SMaarten Lankhorst static int i915_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem) 572cf3e3e86SMaarten Lankhorst { 5733c2b8f32SThomas Hellström if (!cpu_maps_iomem(mem)) 574cf3e3e86SMaarten Lankhorst return 0; 575cf3e3e86SMaarten Lankhorst 576cf3e3e86SMaarten Lankhorst mem->bus.caching = ttm_write_combined; 577cf3e3e86SMaarten Lankhorst mem->bus.is_iomem = true; 578cf3e3e86SMaarten Lankhorst 579cf3e3e86SMaarten Lankhorst return 0; 580cf3e3e86SMaarten Lankhorst } 581cf3e3e86SMaarten Lankhorst 582cf3e3e86SMaarten Lankhorst static unsigned long i915_ttm_io_mem_pfn(struct ttm_buffer_object *bo, 583cf3e3e86SMaarten Lankhorst unsigned long page_offset) 584cf3e3e86SMaarten Lankhorst { 585cf3e3e86SMaarten Lankhorst struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 586cf3e3e86SMaarten Lankhorst unsigned long base = obj->mm.region->iomap.base - obj->mm.region->region.start; 587cf3e3e86SMaarten Lankhorst struct scatterlist *sg; 588cf3e3e86SMaarten Lankhorst unsigned int ofs; 589cf3e3e86SMaarten Lankhorst 590cf3e3e86SMaarten Lankhorst GEM_WARN_ON(bo->ttm); 591cf3e3e86SMaarten Lankhorst 592*7d6a276eSJason Ekstrand sg = __i915_gem_object_get_sg(obj, &obj->ttm.get_io_page, page_offset, &ofs, true); 593cf3e3e86SMaarten Lankhorst 594cf3e3e86SMaarten Lankhorst return ((base + sg_dma_address(sg)) >> PAGE_SHIFT) + ofs; 595cf3e3e86SMaarten Lankhorst } 596cf3e3e86SMaarten Lankhorst 597213d5092SThomas Hellström static struct ttm_device_funcs i915_ttm_bo_driver = { 598213d5092SThomas Hellström .ttm_tt_create = i915_ttm_tt_create, 599213d5092SThomas Hellström .ttm_tt_unpopulate = i915_ttm_tt_unpopulate, 600213d5092SThomas Hellström .ttm_tt_destroy = i915_ttm_tt_destroy, 601213d5092SThomas Hellström .eviction_valuable = i915_ttm_eviction_valuable, 602213d5092SThomas Hellström .evict_flags = i915_ttm_evict_flags, 603213d5092SThomas Hellström .move = i915_ttm_move, 604213d5092SThomas Hellström .swap_notify = i915_ttm_swap_notify, 605213d5092SThomas Hellström .delete_mem_notify = i915_ttm_delete_mem_notify, 606cf3e3e86SMaarten Lankhorst .io_mem_reserve = i915_ttm_io_mem_reserve, 607cf3e3e86SMaarten Lankhorst .io_mem_pfn = i915_ttm_io_mem_pfn, 608213d5092SThomas Hellström }; 609213d5092SThomas Hellström 610213d5092SThomas Hellström /** 611213d5092SThomas Hellström * i915_ttm_driver - Return a pointer to the TTM device funcs 612213d5092SThomas Hellström * 613213d5092SThomas Hellström * Return: Pointer to statically allocated TTM device funcs. 614213d5092SThomas Hellström */ 615213d5092SThomas Hellström struct ttm_device_funcs *i915_ttm_driver(void) 616213d5092SThomas Hellström { 617213d5092SThomas Hellström return &i915_ttm_bo_driver; 618213d5092SThomas Hellström } 619213d5092SThomas Hellström 620b6e913e1SThomas Hellström static int __i915_ttm_get_pages(struct drm_i915_gem_object *obj, 621b6e913e1SThomas Hellström struct ttm_placement *placement) 622213d5092SThomas Hellström { 623213d5092SThomas Hellström struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 624213d5092SThomas Hellström struct ttm_operation_ctx ctx = { 625213d5092SThomas Hellström .interruptible = true, 626213d5092SThomas Hellström .no_wait_gpu = false, 627213d5092SThomas Hellström }; 628213d5092SThomas Hellström struct sg_table *st; 629b07a6483SThomas Hellström int real_num_busy; 630213d5092SThomas Hellström int ret; 631213d5092SThomas Hellström 632b07a6483SThomas Hellström /* First try only the requested placement. No eviction. */ 633b6e913e1SThomas Hellström real_num_busy = fetch_and_zero(&placement->num_busy_placement); 634b6e913e1SThomas Hellström ret = ttm_bo_validate(bo, placement, &ctx); 635b07a6483SThomas Hellström if (ret) { 636b07a6483SThomas Hellström ret = i915_ttm_err_to_gem(ret); 637b07a6483SThomas Hellström /* 638b07a6483SThomas Hellström * Anything that wants to restart the operation gets to 639b07a6483SThomas Hellström * do that. 640b07a6483SThomas Hellström */ 641b07a6483SThomas Hellström if (ret == -EDEADLK || ret == -EINTR || ret == -ERESTARTSYS || 642b07a6483SThomas Hellström ret == -EAGAIN) 643b07a6483SThomas Hellström return ret; 644b07a6483SThomas Hellström 645b07a6483SThomas Hellström /* 646b07a6483SThomas Hellström * If the initial attempt fails, allow all accepted placements, 647b07a6483SThomas Hellström * evicting if necessary. 648b07a6483SThomas Hellström */ 649b6e913e1SThomas Hellström placement->num_busy_placement = real_num_busy; 650b6e913e1SThomas Hellström ret = ttm_bo_validate(bo, placement, &ctx); 651213d5092SThomas Hellström if (ret) 652b07a6483SThomas Hellström return i915_ttm_err_to_gem(ret); 653b07a6483SThomas Hellström } 654213d5092SThomas Hellström 6553c2b8f32SThomas Hellström i915_ttm_adjust_lru(obj); 6563c2b8f32SThomas Hellström if (bo->ttm && !ttm_tt_is_populated(bo->ttm)) { 6573c2b8f32SThomas Hellström ret = ttm_tt_populate(bo->bdev, bo->ttm, &ctx); 6583c2b8f32SThomas Hellström if (ret) 6593c2b8f32SThomas Hellström return ret; 6603c2b8f32SThomas Hellström 6613c2b8f32SThomas Hellström i915_ttm_adjust_domains_after_move(obj); 6623c2b8f32SThomas Hellström i915_ttm_adjust_gem_after_move(obj); 6633c2b8f32SThomas Hellström } 6643c2b8f32SThomas Hellström 665b6e913e1SThomas Hellström GEM_WARN_ON(obj->mm.pages); 666213d5092SThomas Hellström /* Object either has a page vector or is an iomem object */ 667213d5092SThomas Hellström st = bo->ttm ? i915_ttm_tt_get_st(bo->ttm) : obj->ttm.cached_io_st; 668213d5092SThomas Hellström if (IS_ERR(st)) 669213d5092SThomas Hellström return PTR_ERR(st); 670213d5092SThomas Hellström 671213d5092SThomas Hellström __i915_gem_object_set_pages(obj, st, i915_sg_dma_sizes(st->sgl)); 672213d5092SThomas Hellström 673213d5092SThomas Hellström return ret; 674213d5092SThomas Hellström } 675213d5092SThomas Hellström 676b6e913e1SThomas Hellström static int i915_ttm_get_pages(struct drm_i915_gem_object *obj) 677b6e913e1SThomas Hellström { 678b6e913e1SThomas Hellström struct ttm_place requested, busy[I915_TTM_MAX_PLACEMENTS]; 679b6e913e1SThomas Hellström struct ttm_placement placement; 680b6e913e1SThomas Hellström 681b6e913e1SThomas Hellström GEM_BUG_ON(obj->mm.n_placements > I915_TTM_MAX_PLACEMENTS); 682b6e913e1SThomas Hellström 683b6e913e1SThomas Hellström /* Move to the requested placement. */ 684b6e913e1SThomas Hellström i915_ttm_placement_from_obj(obj, &requested, busy, &placement); 685b6e913e1SThomas Hellström 686b6e913e1SThomas Hellström return __i915_ttm_get_pages(obj, &placement); 687b6e913e1SThomas Hellström } 688b6e913e1SThomas Hellström 689b6e913e1SThomas Hellström /** 690b6e913e1SThomas Hellström * DOC: Migration vs eviction 691b6e913e1SThomas Hellström * 692b6e913e1SThomas Hellström * GEM migration may not be the same as TTM migration / eviction. If 693b6e913e1SThomas Hellström * the TTM core decides to evict an object it may be evicted to a 694b6e913e1SThomas Hellström * TTM memory type that is not in the object's allowable GEM regions, or 695b6e913e1SThomas Hellström * in fact theoretically to a TTM memory type that doesn't correspond to 696b6e913e1SThomas Hellström * a GEM memory region. In that case the object's GEM region is not 697b6e913e1SThomas Hellström * updated, and the data is migrated back to the GEM region at 698b6e913e1SThomas Hellström * get_pages time. TTM may however set up CPU ptes to the object even 699b6e913e1SThomas Hellström * when it is evicted. 700b6e913e1SThomas Hellström * Gem forced migration using the i915_ttm_migrate() op, is allowed even 701b6e913e1SThomas Hellström * to regions that are not in the object's list of allowable placements. 702b6e913e1SThomas Hellström */ 703b6e913e1SThomas Hellström static int i915_ttm_migrate(struct drm_i915_gem_object *obj, 704b6e913e1SThomas Hellström struct intel_memory_region *mr) 705b6e913e1SThomas Hellström { 706b6e913e1SThomas Hellström struct ttm_place requested; 707b6e913e1SThomas Hellström struct ttm_placement placement; 708b6e913e1SThomas Hellström int ret; 709b6e913e1SThomas Hellström 710b6e913e1SThomas Hellström i915_ttm_place_from_region(mr, &requested, obj->flags); 711b6e913e1SThomas Hellström placement.num_placement = 1; 712b6e913e1SThomas Hellström placement.num_busy_placement = 1; 713b6e913e1SThomas Hellström placement.placement = &requested; 714b6e913e1SThomas Hellström placement.busy_placement = &requested; 715b6e913e1SThomas Hellström 716b6e913e1SThomas Hellström ret = __i915_ttm_get_pages(obj, &placement); 717b6e913e1SThomas Hellström if (ret) 718b6e913e1SThomas Hellström return ret; 719b6e913e1SThomas Hellström 720b6e913e1SThomas Hellström /* 721b6e913e1SThomas Hellström * Reinitialize the region bindings. This is primarily 722b6e913e1SThomas Hellström * required for objects where the new region is not in 723b6e913e1SThomas Hellström * its allowable placements. 724b6e913e1SThomas Hellström */ 725b6e913e1SThomas Hellström if (obj->mm.region != mr) { 726b6e913e1SThomas Hellström i915_gem_object_release_memory_region(obj); 727b6e913e1SThomas Hellström i915_gem_object_init_memory_region(obj, mr); 728b6e913e1SThomas Hellström } 729b6e913e1SThomas Hellström 730b6e913e1SThomas Hellström return 0; 731b6e913e1SThomas Hellström } 732b6e913e1SThomas Hellström 733213d5092SThomas Hellström static void i915_ttm_put_pages(struct drm_i915_gem_object *obj, 734213d5092SThomas Hellström struct sg_table *st) 735213d5092SThomas Hellström { 736213d5092SThomas Hellström /* 737213d5092SThomas Hellström * We're currently not called from a shrinker, so put_pages() 738213d5092SThomas Hellström * typically means the object is about to destroyed, or called 739213d5092SThomas Hellström * from move_notify(). So just avoid doing much for now. 740213d5092SThomas Hellström * If the object is not destroyed next, The TTM eviction logic 741213d5092SThomas Hellström * and shrinkers will move it out if needed. 742213d5092SThomas Hellström */ 743213d5092SThomas Hellström 744213d5092SThomas Hellström i915_ttm_adjust_lru(obj); 745213d5092SThomas Hellström } 746213d5092SThomas Hellström 747213d5092SThomas Hellström static void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj) 748213d5092SThomas Hellström { 749213d5092SThomas Hellström struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 750213d5092SThomas Hellström 751213d5092SThomas Hellström /* 752213d5092SThomas Hellström * Don't manipulate the TTM LRUs while in TTM bo destruction. 753213d5092SThomas Hellström * We're called through i915_ttm_delete_mem_notify(). 754213d5092SThomas Hellström */ 755213d5092SThomas Hellström if (!kref_read(&bo->kref)) 756213d5092SThomas Hellström return; 757213d5092SThomas Hellström 758213d5092SThomas Hellström /* 759213d5092SThomas Hellström * Put on the correct LRU list depending on the MADV status 760213d5092SThomas Hellström */ 761213d5092SThomas Hellström spin_lock(&bo->bdev->lru_lock); 762213d5092SThomas Hellström if (obj->mm.madv != I915_MADV_WILLNEED) { 763213d5092SThomas Hellström bo->priority = I915_TTM_PRIO_PURGE; 764213d5092SThomas Hellström } else if (!i915_gem_object_has_pages(obj)) { 765213d5092SThomas Hellström if (bo->priority < I915_TTM_PRIO_HAS_PAGES) 766213d5092SThomas Hellström bo->priority = I915_TTM_PRIO_HAS_PAGES; 767213d5092SThomas Hellström } else { 768213d5092SThomas Hellström if (bo->priority > I915_TTM_PRIO_NO_PAGES) 769213d5092SThomas Hellström bo->priority = I915_TTM_PRIO_NO_PAGES; 770213d5092SThomas Hellström } 771213d5092SThomas Hellström 772213d5092SThomas Hellström ttm_bo_move_to_lru_tail(bo, bo->resource, NULL); 773213d5092SThomas Hellström spin_unlock(&bo->bdev->lru_lock); 774213d5092SThomas Hellström } 775213d5092SThomas Hellström 776213d5092SThomas Hellström /* 777213d5092SThomas Hellström * TTM-backed gem object destruction requires some clarification. 778213d5092SThomas Hellström * Basically we have two possibilities here. We can either rely on the 779213d5092SThomas Hellström * i915 delayed destruction and put the TTM object when the object 780213d5092SThomas Hellström * is idle. This would be detected by TTM which would bypass the 781213d5092SThomas Hellström * TTM delayed destroy handling. The other approach is to put the TTM 782213d5092SThomas Hellström * object early and rely on the TTM destroyed handling, and then free 783213d5092SThomas Hellström * the leftover parts of the GEM object once TTM's destroyed list handling is 784213d5092SThomas Hellström * complete. For now, we rely on the latter for two reasons: 785213d5092SThomas Hellström * a) TTM can evict an object even when it's on the delayed destroy list, 786213d5092SThomas Hellström * which in theory allows for complete eviction. 787213d5092SThomas Hellström * b) There is work going on in TTM to allow freeing an object even when 788213d5092SThomas Hellström * it's not idle, and using the TTM destroyed list handling could help us 789213d5092SThomas Hellström * benefit from that. 790213d5092SThomas Hellström */ 791213d5092SThomas Hellström static void i915_ttm_delayed_free(struct drm_i915_gem_object *obj) 792213d5092SThomas Hellström { 793213d5092SThomas Hellström if (obj->ttm.created) { 794213d5092SThomas Hellström ttm_bo_put(i915_gem_to_ttm(obj)); 795213d5092SThomas Hellström } else { 796213d5092SThomas Hellström __i915_gem_free_object(obj); 797213d5092SThomas Hellström call_rcu(&obj->rcu, __i915_gem_free_object_rcu); 798213d5092SThomas Hellström } 799213d5092SThomas Hellström } 800213d5092SThomas Hellström 801cf3e3e86SMaarten Lankhorst static vm_fault_t vm_fault_ttm(struct vm_fault *vmf) 802cf3e3e86SMaarten Lankhorst { 803cf3e3e86SMaarten Lankhorst struct vm_area_struct *area = vmf->vma; 804cf3e3e86SMaarten Lankhorst struct drm_i915_gem_object *obj = 805cf3e3e86SMaarten Lankhorst i915_ttm_to_gem(area->vm_private_data); 806cf3e3e86SMaarten Lankhorst 807cf3e3e86SMaarten Lankhorst /* Sanity check that we allow writing into this object */ 808cf3e3e86SMaarten Lankhorst if (unlikely(i915_gem_object_is_readonly(obj) && 809cf3e3e86SMaarten Lankhorst area->vm_flags & VM_WRITE)) 810cf3e3e86SMaarten Lankhorst return VM_FAULT_SIGBUS; 811cf3e3e86SMaarten Lankhorst 812cf3e3e86SMaarten Lankhorst return ttm_bo_vm_fault(vmf); 813cf3e3e86SMaarten Lankhorst } 814cf3e3e86SMaarten Lankhorst 815cf3e3e86SMaarten Lankhorst static int 816cf3e3e86SMaarten Lankhorst vm_access_ttm(struct vm_area_struct *area, unsigned long addr, 817cf3e3e86SMaarten Lankhorst void *buf, int len, int write) 818cf3e3e86SMaarten Lankhorst { 819cf3e3e86SMaarten Lankhorst struct drm_i915_gem_object *obj = 820cf3e3e86SMaarten Lankhorst i915_ttm_to_gem(area->vm_private_data); 821cf3e3e86SMaarten Lankhorst 822cf3e3e86SMaarten Lankhorst if (i915_gem_object_is_readonly(obj) && write) 823cf3e3e86SMaarten Lankhorst return -EACCES; 824cf3e3e86SMaarten Lankhorst 825cf3e3e86SMaarten Lankhorst return ttm_bo_vm_access(area, addr, buf, len, write); 826cf3e3e86SMaarten Lankhorst } 827cf3e3e86SMaarten Lankhorst 828cf3e3e86SMaarten Lankhorst static void ttm_vm_open(struct vm_area_struct *vma) 829cf3e3e86SMaarten Lankhorst { 830cf3e3e86SMaarten Lankhorst struct drm_i915_gem_object *obj = 831cf3e3e86SMaarten Lankhorst i915_ttm_to_gem(vma->vm_private_data); 832cf3e3e86SMaarten Lankhorst 833cf3e3e86SMaarten Lankhorst GEM_BUG_ON(!obj); 834cf3e3e86SMaarten Lankhorst i915_gem_object_get(obj); 835cf3e3e86SMaarten Lankhorst } 836cf3e3e86SMaarten Lankhorst 837cf3e3e86SMaarten Lankhorst static void ttm_vm_close(struct vm_area_struct *vma) 838cf3e3e86SMaarten Lankhorst { 839cf3e3e86SMaarten Lankhorst struct drm_i915_gem_object *obj = 840cf3e3e86SMaarten Lankhorst i915_ttm_to_gem(vma->vm_private_data); 841cf3e3e86SMaarten Lankhorst 842cf3e3e86SMaarten Lankhorst GEM_BUG_ON(!obj); 843cf3e3e86SMaarten Lankhorst i915_gem_object_put(obj); 844cf3e3e86SMaarten Lankhorst } 845cf3e3e86SMaarten Lankhorst 846cf3e3e86SMaarten Lankhorst static const struct vm_operations_struct vm_ops_ttm = { 847cf3e3e86SMaarten Lankhorst .fault = vm_fault_ttm, 848cf3e3e86SMaarten Lankhorst .access = vm_access_ttm, 849cf3e3e86SMaarten Lankhorst .open = ttm_vm_open, 850cf3e3e86SMaarten Lankhorst .close = ttm_vm_close, 851cf3e3e86SMaarten Lankhorst }; 852cf3e3e86SMaarten Lankhorst 853cf3e3e86SMaarten Lankhorst static u64 i915_ttm_mmap_offset(struct drm_i915_gem_object *obj) 854cf3e3e86SMaarten Lankhorst { 855cf3e3e86SMaarten Lankhorst /* The ttm_bo must be allocated with I915_BO_ALLOC_USER */ 856cf3e3e86SMaarten Lankhorst GEM_BUG_ON(!drm_mm_node_allocated(&obj->base.vma_node.vm_node)); 857cf3e3e86SMaarten Lankhorst 858cf3e3e86SMaarten Lankhorst return drm_vma_node_offset_addr(&obj->base.vma_node); 859cf3e3e86SMaarten Lankhorst } 860cf3e3e86SMaarten Lankhorst 8614bc2d574SMatthew Auld static const struct drm_i915_gem_object_ops i915_gem_ttm_obj_ops = { 862213d5092SThomas Hellström .name = "i915_gem_object_ttm", 863213d5092SThomas Hellström 864213d5092SThomas Hellström .get_pages = i915_ttm_get_pages, 865213d5092SThomas Hellström .put_pages = i915_ttm_put_pages, 866213d5092SThomas Hellström .truncate = i915_ttm_purge, 867213d5092SThomas Hellström .adjust_lru = i915_ttm_adjust_lru, 868213d5092SThomas Hellström .delayed_free = i915_ttm_delayed_free, 869b6e913e1SThomas Hellström .migrate = i915_ttm_migrate, 870cf3e3e86SMaarten Lankhorst .mmap_offset = i915_ttm_mmap_offset, 871cf3e3e86SMaarten Lankhorst .mmap_ops = &vm_ops_ttm, 872213d5092SThomas Hellström }; 873213d5092SThomas Hellström 874213d5092SThomas Hellström void i915_ttm_bo_destroy(struct ttm_buffer_object *bo) 875213d5092SThomas Hellström { 876213d5092SThomas Hellström struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 877213d5092SThomas Hellström 878213d5092SThomas Hellström i915_gem_object_release_memory_region(obj); 879cf3e3e86SMaarten Lankhorst mutex_destroy(&obj->ttm.get_io_page.lock); 880213d5092SThomas Hellström if (obj->ttm.created) 881213d5092SThomas Hellström call_rcu(&obj->rcu, __i915_gem_free_object_rcu); 882213d5092SThomas Hellström } 883213d5092SThomas Hellström 884213d5092SThomas Hellström /** 885213d5092SThomas Hellström * __i915_gem_ttm_object_init - Initialize a ttm-backed i915 gem object 886213d5092SThomas Hellström * @mem: The initial memory region for the object. 887213d5092SThomas Hellström * @obj: The gem object. 888213d5092SThomas Hellström * @size: Object size in bytes. 889213d5092SThomas Hellström * @flags: gem object flags. 890213d5092SThomas Hellström * 891213d5092SThomas Hellström * Return: 0 on success, negative error code on failure. 892213d5092SThomas Hellström */ 893213d5092SThomas Hellström int __i915_gem_ttm_object_init(struct intel_memory_region *mem, 894213d5092SThomas Hellström struct drm_i915_gem_object *obj, 895213d5092SThomas Hellström resource_size_t size, 896d22632c8SMatthew Auld resource_size_t page_size, 897213d5092SThomas Hellström unsigned int flags) 898213d5092SThomas Hellström { 899213d5092SThomas Hellström static struct lock_class_key lock_class; 900213d5092SThomas Hellström struct drm_i915_private *i915 = mem->i915; 9013c2b8f32SThomas Hellström struct ttm_operation_ctx ctx = { 9023c2b8f32SThomas Hellström .interruptible = true, 9033c2b8f32SThomas Hellström .no_wait_gpu = false, 9043c2b8f32SThomas Hellström }; 905213d5092SThomas Hellström enum ttm_bo_type bo_type; 906213d5092SThomas Hellström int ret; 907213d5092SThomas Hellström 908213d5092SThomas Hellström drm_gem_private_object_init(&i915->drm, &obj->base, size); 909213d5092SThomas Hellström i915_gem_object_init(obj, &i915_gem_ttm_obj_ops, &lock_class, flags); 910213d5092SThomas Hellström i915_gem_object_init_memory_region(obj, mem); 911213d5092SThomas Hellström i915_gem_object_make_unshrinkable(obj); 912cf3e3e86SMaarten Lankhorst INIT_RADIX_TREE(&obj->ttm.get_io_page.radix, GFP_KERNEL | __GFP_NOWARN); 913cf3e3e86SMaarten Lankhorst mutex_init(&obj->ttm.get_io_page.lock); 914213d5092SThomas Hellström bo_type = (obj->flags & I915_BO_ALLOC_USER) ? ttm_bo_type_device : 915213d5092SThomas Hellström ttm_bo_type_kernel; 916213d5092SThomas Hellström 9173c2b8f32SThomas Hellström obj->base.vma_node.driver_private = i915_gem_to_ttm(obj); 9183c2b8f32SThomas Hellström 919d22632c8SMatthew Auld /* Forcing the page size is kernel internal only */ 920d22632c8SMatthew Auld GEM_BUG_ON(page_size && obj->mm.n_placements); 921d22632c8SMatthew Auld 922213d5092SThomas Hellström /* 923213d5092SThomas Hellström * If this function fails, it will call the destructor, but 924213d5092SThomas Hellström * our caller still owns the object. So no freeing in the 925213d5092SThomas Hellström * destructor until obj->ttm.created is true. 926213d5092SThomas Hellström * Similarly, in delayed_destroy, we can't call ttm_bo_put() 927213d5092SThomas Hellström * until successful initialization. 928213d5092SThomas Hellström */ 9293c2b8f32SThomas Hellström ret = ttm_bo_init_reserved(&i915->bdev, i915_gem_to_ttm(obj), size, 93013c2ceb6SMatthew Auld bo_type, &i915_sys_placement, 931d22632c8SMatthew Auld page_size >> PAGE_SHIFT, 9323c2b8f32SThomas Hellström &ctx, NULL, NULL, i915_ttm_bo_destroy); 9333c2b8f32SThomas Hellström if (ret) 934b07a6483SThomas Hellström return i915_ttm_err_to_gem(ret); 9353c2b8f32SThomas Hellström 9363c2b8f32SThomas Hellström obj->ttm.created = true; 9373c2b8f32SThomas Hellström i915_ttm_adjust_domains_after_move(obj); 9383c2b8f32SThomas Hellström i915_ttm_adjust_gem_after_move(obj); 9393c2b8f32SThomas Hellström i915_gem_object_unlock(obj); 9403c2b8f32SThomas Hellström 9413c2b8f32SThomas Hellström return 0; 942213d5092SThomas Hellström } 94332b7cf51SThomas Hellström 94432b7cf51SThomas Hellström static const struct intel_memory_region_ops ttm_system_region_ops = { 94532b7cf51SThomas Hellström .init_object = __i915_gem_ttm_object_init, 94632b7cf51SThomas Hellström }; 94732b7cf51SThomas Hellström 94832b7cf51SThomas Hellström struct intel_memory_region * 94932b7cf51SThomas Hellström i915_gem_ttm_system_setup(struct drm_i915_private *i915, 95032b7cf51SThomas Hellström u16 type, u16 instance) 95132b7cf51SThomas Hellström { 95232b7cf51SThomas Hellström struct intel_memory_region *mr; 95332b7cf51SThomas Hellström 95432b7cf51SThomas Hellström mr = intel_memory_region_create(i915, 0, 95532b7cf51SThomas Hellström totalram_pages() << PAGE_SHIFT, 95632b7cf51SThomas Hellström PAGE_SIZE, 0, 95732b7cf51SThomas Hellström type, instance, 95832b7cf51SThomas Hellström &ttm_system_region_ops); 95932b7cf51SThomas Hellström if (IS_ERR(mr)) 96032b7cf51SThomas Hellström return mr; 96132b7cf51SThomas Hellström 96232b7cf51SThomas Hellström intel_memory_region_set_name(mr, "system-ttm"); 96332b7cf51SThomas Hellström return mr; 96432b7cf51SThomas Hellström } 965