12f4ca1baSJingyu Wang // SPDX-License-Identifier: MIT 2130e0371SOded Gabbay /* 3130e0371SOded Gabbay * Copyright 2014 Advanced Micro Devices, Inc. 4130e0371SOded Gabbay * 5130e0371SOded Gabbay * Permission is hereby granted, free of charge, to any person obtaining a 6130e0371SOded Gabbay * copy of this software and associated documentation files (the "Software"), 7130e0371SOded Gabbay * to deal in the Software without restriction, including without limitation 8130e0371SOded Gabbay * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9130e0371SOded Gabbay * and/or sell copies of the Software, and to permit persons to whom the 10130e0371SOded Gabbay * Software is furnished to do so, subject to the following conditions: 11130e0371SOded Gabbay * 12130e0371SOded Gabbay * The above copyright notice and this permission notice shall be included in 13130e0371SOded Gabbay * all copies or substantial portions of the Software. 14130e0371SOded Gabbay * 15130e0371SOded Gabbay * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16130e0371SOded Gabbay * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17130e0371SOded Gabbay * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18130e0371SOded Gabbay * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19130e0371SOded Gabbay * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20130e0371SOded Gabbay * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21130e0371SOded Gabbay * OTHER DEALINGS IN THE SOFTWARE. 22130e0371SOded Gabbay */ 23130e0371SOded Gabbay 24130e0371SOded Gabbay #include "amdgpu_amdkfd.h" 2593304810SJonathan Kim #include "amd_pcie.h" 262f7d10b3SJammy Zhou #include "amd_shared.h" 27fdf2f6c5SSam Ravnborg 28130e0371SOded Gabbay #include "amdgpu.h" 292db0cdbeSAlex Deucher #include "amdgpu_gfx.h" 302fbd6f94SChristian König #include "amdgpu_dma_buf.h" 31f4bff6e0SRajneesh Bhardwaj #include <drm/ttm/ttm_tt.h> 32130e0371SOded Gabbay #include <linux/module.h> 331dde0ea9SFelix Kuehling #include <linux/dma-buf.h> 34da361dd1Sshaoyunl #include "amdgpu_xgmi.h" 351d251d90SYong Zhao #include <uapi/linux/kfd_ioctl.h> 36c7490949STao Zhou #include "amdgpu_ras.h" 37c7490949STao Zhou #include "amdgpu_umc.h" 38b5fd0cf3SAndrey Grodzovsky #include "amdgpu_reset.h" 39130e0371SOded Gabbay 40611736d8SFelix Kuehling /* Total memory size in system memory and all GPU VRAM. Used to 41611736d8SFelix Kuehling * estimate worst case amount of memory to reserve for page tables 42611736d8SFelix Kuehling */ 43611736d8SFelix Kuehling uint64_t amdgpu_amdkfd_total_mem_size; 44611736d8SFelix Kuehling 45402bde58Skernel test robot static bool kfd_initialized; 46c7651b73SFelix Kuehling 47efb1c658SOded Gabbay int amdgpu_amdkfd_init(void) 48130e0371SOded Gabbay { 49611736d8SFelix Kuehling struct sysinfo si; 50efb1c658SOded Gabbay int ret; 51efb1c658SOded Gabbay 52611736d8SFelix Kuehling si_meminfo(&si); 53df23d1bbSOak Zeng amdgpu_amdkfd_total_mem_size = si.freeram - si.freehigh; 54611736d8SFelix Kuehling amdgpu_amdkfd_total_mem_size *= si.mem_unit; 55611736d8SFelix Kuehling 56308176d6SAmber Lin ret = kgd2kfd_init(); 57c7651b73SFelix Kuehling kfd_initialized = !ret; 58fcdfa432SOded Gabbay 59efb1c658SOded Gabbay return ret; 60130e0371SOded Gabbay } 61130e0371SOded Gabbay 62130e0371SOded Gabbay void amdgpu_amdkfd_fini(void) 63130e0371SOded Gabbay { 64c7651b73SFelix Kuehling if (kfd_initialized) { 658e07e267SAmber Lin kgd2kfd_exit(); 66c7651b73SFelix Kuehling kfd_initialized = false; 67c7651b73SFelix Kuehling } 68130e0371SOded Gabbay } 69130e0371SOded Gabbay 70dc102c43SAndres Rodriguez void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev) 71130e0371SOded Gabbay { 72050091abSYong Zhao bool vf = amdgpu_sriov_vf(adev); 735c33f214SFelix Kuehling 74c7651b73SFelix Kuehling if (!kfd_initialized) 75c7651b73SFelix Kuehling return; 76c7651b73SFelix Kuehling 77b5d1d755SGraham Sider adev->kfd.dev = kgd2kfd_probe(adev, vf); 78130e0371SOded Gabbay } 79130e0371SOded Gabbay 8022cb0164SAlex Deucher /** 8122cb0164SAlex Deucher * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to 8222cb0164SAlex Deucher * setup amdkfd 8322cb0164SAlex Deucher * 8422cb0164SAlex Deucher * @adev: amdgpu_device pointer 8522cb0164SAlex Deucher * @aperture_base: output returning doorbell aperture base physical address 8622cb0164SAlex Deucher * @aperture_size: output returning doorbell aperture size in bytes 8722cb0164SAlex Deucher * @start_offset: output returning # of doorbell bytes reserved for amdgpu. 8822cb0164SAlex Deucher * 8922cb0164SAlex Deucher * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up, 9022cb0164SAlex Deucher * takes doorbells required for its own rings and reports the setup to amdkfd. 9122cb0164SAlex Deucher * amdgpu reserved doorbells are at the start of the doorbell aperture. 9222cb0164SAlex Deucher */ 9322cb0164SAlex Deucher static void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev, 9422cb0164SAlex Deucher phys_addr_t *aperture_base, 9522cb0164SAlex Deucher size_t *aperture_size, 9622cb0164SAlex Deucher size_t *start_offset) 9722cb0164SAlex Deucher { 9822cb0164SAlex Deucher /* 990512e9ffSShashank Sharma * The first num_kernel_doorbells are used by amdgpu. 10022cb0164SAlex Deucher * amdkfd takes whatever's left in the aperture. 10122cb0164SAlex Deucher */ 102cc009e61SMukul Joshi if (adev->enable_mes) { 103cc009e61SMukul Joshi /* 104cc009e61SMukul Joshi * With MES enabled, we only need to initialize 105cc009e61SMukul Joshi * the base address. The size and offset are 106cc009e61SMukul Joshi * not initialized as AMDGPU manages the whole 107cc009e61SMukul Joshi * doorbell space. 108cc009e61SMukul Joshi */ 109cc009e61SMukul Joshi *aperture_base = adev->doorbell.base; 110cc009e61SMukul Joshi *aperture_size = 0; 111cc009e61SMukul Joshi *start_offset = 0; 1120512e9ffSShashank Sharma } else if (adev->doorbell.size > adev->doorbell.num_kernel_doorbells * 113cc009e61SMukul Joshi sizeof(u32)) { 11422cb0164SAlex Deucher *aperture_base = adev->doorbell.base; 11522cb0164SAlex Deucher *aperture_size = adev->doorbell.size; 1160512e9ffSShashank Sharma *start_offset = adev->doorbell.num_kernel_doorbells * sizeof(u32); 11722cb0164SAlex Deucher } else { 11822cb0164SAlex Deucher *aperture_base = 0; 11922cb0164SAlex Deucher *aperture_size = 0; 12022cb0164SAlex Deucher *start_offset = 0; 12122cb0164SAlex Deucher } 12222cb0164SAlex Deucher } 12322cb0164SAlex Deucher 124b5fd0cf3SAndrey Grodzovsky 125b5fd0cf3SAndrey Grodzovsky static void amdgpu_amdkfd_reset_work(struct work_struct *work) 126b5fd0cf3SAndrey Grodzovsky { 127b5fd0cf3SAndrey Grodzovsky struct amdgpu_device *adev = container_of(work, struct amdgpu_device, 128b5fd0cf3SAndrey Grodzovsky kfd.reset_work); 129b5fd0cf3SAndrey Grodzovsky 130f1549c09SLikun Gao struct amdgpu_reset_context reset_context; 1312f4ca1baSJingyu Wang 132f1549c09SLikun Gao memset(&reset_context, 0, sizeof(reset_context)); 133f1549c09SLikun Gao 134f1549c09SLikun Gao reset_context.method = AMD_RESET_METHOD_NONE; 135f1549c09SLikun Gao reset_context.reset_req_dev = adev; 136f1549c09SLikun Gao clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 137f1549c09SLikun Gao 138f1549c09SLikun Gao amdgpu_device_gpu_recover(adev, NULL, &reset_context); 139b5fd0cf3SAndrey Grodzovsky } 140b5fd0cf3SAndrey Grodzovsky 141733965a9SFlora Cui static const struct drm_client_funcs kfd_client_funcs = { 142733965a9SFlora Cui .unregister = drm_client_release, 143733965a9SFlora Cui }; 144dc102c43SAndres Rodriguez void amdgpu_amdkfd_device_init(struct amdgpu_device *adev) 145130e0371SOded Gabbay { 146234441ddSYong Zhao int i; 147d0b63bb3SAndres Rodriguez int last_valid_bit; 14818192001SFelix Kuehling int ret; 149611736d8SFelix Kuehling 15027fb73a0SMukul Joshi amdgpu_amdkfd_gpuvm_init_mem_limits(); 15127fb73a0SMukul Joshi 152611736d8SFelix Kuehling if (adev->kfd.dev) { 153130e0371SOded Gabbay struct kgd2kfd_shared_resources gpu_resources = { 15440111ec2SFelix Kuehling .compute_vmid_bitmap = 15540111ec2SFelix Kuehling ((1 << AMDGPU_NUM_VMID) - 1) - 15640111ec2SFelix Kuehling ((1 << adev->vm_manager.first_kfd_vmid) - 1), 157d0b63bb3SAndres Rodriguez .num_pipe_per_mec = adev->gfx.mec.num_pipe_per_mec, 158155494dbSFelix Kuehling .num_queue_per_pipe = adev->gfx.mec.num_queue_per_pipe, 159155494dbSFelix Kuehling .gpuvm_size = min(adev->vm_manager.max_pfn 160155494dbSFelix Kuehling << AMDGPU_GPU_PAGE_SHIFT, 161ad9a5b78SChristian König AMDGPU_GMC_HOLE_START), 1624a580877SLuben Tuikov .drm_render_minor = adev_to_drm(adev)->render->index, 163234441ddSYong Zhao .sdma_doorbell_idx = adev->doorbell_index.sdma_engine, 164cc009e61SMukul Joshi .enable_mes = adev->enable_mes, 165130e0371SOded Gabbay }; 166130e0371SOded Gabbay 167733965a9SFlora Cui ret = drm_client_init(&adev->ddev, &adev->kfd.client, "kfd", &kfd_client_funcs); 16818192001SFelix Kuehling if (ret) { 16918192001SFelix Kuehling dev_err(adev->dev, "Failed to init DRM client: %d\n", ret); 17018192001SFelix Kuehling return; 17118192001SFelix Kuehling } 17218192001SFelix Kuehling 173d0b63bb3SAndres Rodriguez /* this is going to have a few of the MSBs set that we need to 1740d87c9cfSKent Russell * clear 1750d87c9cfSKent Russell */ 176e6945304SYong Zhao bitmap_complement(gpu_resources.cp_queue_bitmap, 177be697aa3SLe Ma adev->gfx.mec_bitmap[0].queue_bitmap, 17868fa72a4SMukul Joshi AMDGPU_MAX_QUEUES); 179d0b63bb3SAndres Rodriguez 180d0b63bb3SAndres Rodriguez /* According to linux/bitmap.h we shouldn't use bitmap_clear if 1810d87c9cfSKent Russell * nbits is not compile time constant 1820d87c9cfSKent Russell */ 1833447d220SJay Cornwall last_valid_bit = 1 /* only first MEC can have compute queues */ 184d0b63bb3SAndres Rodriguez * adev->gfx.mec.num_pipe_per_mec 185d0b63bb3SAndres Rodriguez * adev->gfx.mec.num_queue_per_pipe; 18668fa72a4SMukul Joshi for (i = last_valid_bit; i < AMDGPU_MAX_QUEUES; ++i) 187e6945304SYong Zhao clear_bit(i, gpu_resources.cp_queue_bitmap); 188d0b63bb3SAndres Rodriguez 189dc102c43SAndres Rodriguez amdgpu_doorbell_get_kfd_info(adev, 190130e0371SOded Gabbay &gpu_resources.doorbell_physical_address, 191130e0371SOded Gabbay &gpu_resources.doorbell_aperture_size, 192130e0371SOded Gabbay &gpu_resources.doorbell_start_offset); 193c5892230SShaoyun Liu 1941f86805aSYong Zhao /* Since SOC15, BIF starts to statically use the 1951f86805aSYong Zhao * lower 12 bits of doorbell addresses for routing 1961f86805aSYong Zhao * based on settings in registers like 1971f86805aSYong Zhao * SDMA0_DOORBELL_RANGE etc.. 1981f86805aSYong Zhao * In order to route a doorbell to CP engine, the lower 1991f86805aSYong Zhao * 12 bits of its address has to be outside the range 2001f86805aSYong Zhao * set for SDMA, VCN, and IH blocks. 201642a0e80SFelix Kuehling */ 202234441ddSYong Zhao if (adev->asic_type >= CHIP_VEGA10) { 2031f86805aSYong Zhao gpu_resources.non_cp_doorbells_start = 2041f86805aSYong Zhao adev->doorbell_index.first_non_cp; 2051f86805aSYong Zhao gpu_resources.non_cp_doorbells_end = 2061f86805aSYong Zhao adev->doorbell_index.last_non_cp; 207234441ddSYong Zhao } 208130e0371SOded Gabbay 2098e2712e7Sshaoyunl adev->kfd.init_complete = kgd2kfd_device_init(adev->kfd.dev, 210d69a3b76SMukul Joshi &gpu_resources); 21118192001SFelix Kuehling if (adev->kfd.init_complete) 21218192001SFelix Kuehling drm_client_register(&adev->kfd.client); 21318192001SFelix Kuehling else 21418192001SFelix Kuehling drm_client_release(&adev->kfd.client); 215b5fd0cf3SAndrey Grodzovsky 2162302d507SPhilip Yang amdgpu_amdkfd_total_mem_size += adev->gmc.real_vram_size; 2172302d507SPhilip Yang 218b5fd0cf3SAndrey Grodzovsky INIT_WORK(&adev->kfd.reset_work, amdgpu_amdkfd_reset_work); 219130e0371SOded Gabbay } 220130e0371SOded Gabbay } 221130e0371SOded Gabbay 222e9669fb7SAndrey Grodzovsky void amdgpu_amdkfd_device_fini_sw(struct amdgpu_device *adev) 223130e0371SOded Gabbay { 224611736d8SFelix Kuehling if (adev->kfd.dev) { 2258e07e267SAmber Lin kgd2kfd_device_exit(adev->kfd.dev); 226611736d8SFelix Kuehling adev->kfd.dev = NULL; 2272302d507SPhilip Yang amdgpu_amdkfd_total_mem_size -= adev->gmc.real_vram_size; 228130e0371SOded Gabbay } 229130e0371SOded Gabbay } 230130e0371SOded Gabbay 231dc102c43SAndres Rodriguez void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev, 232130e0371SOded Gabbay const void *ih_ring_entry) 233130e0371SOded Gabbay { 234611736d8SFelix Kuehling if (adev->kfd.dev) 2358e07e267SAmber Lin kgd2kfd_interrupt(adev->kfd.dev, ih_ring_entry); 236130e0371SOded Gabbay } 237130e0371SOded Gabbay 2389593f4d6SRajneesh Bhardwaj void amdgpu_amdkfd_suspend(struct amdgpu_device *adev, bool run_pm) 239130e0371SOded Gabbay { 240611736d8SFelix Kuehling if (adev->kfd.dev) 2419593f4d6SRajneesh Bhardwaj kgd2kfd_suspend(adev->kfd.dev, run_pm); 242130e0371SOded Gabbay } 243130e0371SOded Gabbay 2449593f4d6SRajneesh Bhardwaj int amdgpu_amdkfd_resume(struct amdgpu_device *adev, bool run_pm) 245130e0371SOded Gabbay { 246130e0371SOded Gabbay int r = 0; 247130e0371SOded Gabbay 248611736d8SFelix Kuehling if (adev->kfd.dev) 2499593f4d6SRajneesh Bhardwaj r = kgd2kfd_resume(adev->kfd.dev, run_pm); 250130e0371SOded Gabbay 251130e0371SOded Gabbay return r; 252130e0371SOded Gabbay } 253130e0371SOded Gabbay 2545c6dd71eSShaoyun Liu int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev) 2555c6dd71eSShaoyun Liu { 2565c6dd71eSShaoyun Liu int r = 0; 2575c6dd71eSShaoyun Liu 258611736d8SFelix Kuehling if (adev->kfd.dev) 2598e07e267SAmber Lin r = kgd2kfd_pre_reset(adev->kfd.dev); 2605c6dd71eSShaoyun Liu 2615c6dd71eSShaoyun Liu return r; 2625c6dd71eSShaoyun Liu } 2635c6dd71eSShaoyun Liu 2645c6dd71eSShaoyun Liu int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev) 2655c6dd71eSShaoyun Liu { 2665c6dd71eSShaoyun Liu int r = 0; 2675c6dd71eSShaoyun Liu 268611736d8SFelix Kuehling if (adev->kfd.dev) 2698e07e267SAmber Lin r = kgd2kfd_post_reset(adev->kfd.dev); 2705c6dd71eSShaoyun Liu 2715c6dd71eSShaoyun Liu return r; 2725c6dd71eSShaoyun Liu } 2735c6dd71eSShaoyun Liu 2746bfc7c7eSGraham Sider void amdgpu_amdkfd_gpu_reset(struct amdgpu_device *adev) 27524da5a9cSShaoyun Liu { 27612938fadSChristian König if (amdgpu_device_should_recover_gpu(adev)) 277b5fd0cf3SAndrey Grodzovsky amdgpu_reset_domain_schedule(adev->reset_domain, 278b5fd0cf3SAndrey Grodzovsky &adev->kfd.reset_work); 27924da5a9cSShaoyun Liu } 28024da5a9cSShaoyun Liu 2816bfc7c7eSGraham Sider int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size, 282130e0371SOded Gabbay void **mem_obj, uint64_t *gpu_addr, 283fa5bde80SYong Zhao void **cpu_ptr, bool cp_mqd_gfx9) 284130e0371SOded Gabbay { 285473fee47SYong Zhao struct amdgpu_bo *bo = NULL; 2863216c6b7SChunming Zhou struct amdgpu_bo_param bp; 287130e0371SOded Gabbay int r; 288473fee47SYong Zhao void *cpu_ptr_tmp = NULL; 289130e0371SOded Gabbay 2903216c6b7SChunming Zhou memset(&bp, 0, sizeof(bp)); 2913216c6b7SChunming Zhou bp.size = size; 2923216c6b7SChunming Zhou bp.byte_align = PAGE_SIZE; 2933216c6b7SChunming Zhou bp.domain = AMDGPU_GEM_DOMAIN_GTT; 2943216c6b7SChunming Zhou bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC; 2953216c6b7SChunming Zhou bp.type = ttm_bo_type_kernel; 2963216c6b7SChunming Zhou bp.resv = NULL; 2979fd5543eSNirmoy Das bp.bo_ptr_size = sizeof(struct amdgpu_bo); 29815426dbbSYong Zhao 299fa5bde80SYong Zhao if (cp_mqd_gfx9) 300fa5bde80SYong Zhao bp.flags |= AMDGPU_GEM_CREATE_CP_MQD_GFX9; 30115426dbbSYong Zhao 3023216c6b7SChunming Zhou r = amdgpu_bo_create(adev, &bp, &bo); 303130e0371SOded Gabbay if (r) { 304dc102c43SAndres Rodriguez dev_err(adev->dev, 305130e0371SOded Gabbay "failed to allocate BO for amdkfd (%d)\n", r); 306130e0371SOded Gabbay return r; 307130e0371SOded Gabbay } 308130e0371SOded Gabbay 309130e0371SOded Gabbay /* map the buffer */ 310473fee47SYong Zhao r = amdgpu_bo_reserve(bo, true); 311130e0371SOded Gabbay if (r) { 312dc102c43SAndres Rodriguez dev_err(adev->dev, "(%d) failed to reserve bo for amdkfd\n", r); 313130e0371SOded Gabbay goto allocate_mem_reserve_bo_failed; 314130e0371SOded Gabbay } 315130e0371SOded Gabbay 3167b7c6c81SJunwei Zhang r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT); 317130e0371SOded Gabbay if (r) { 318dc102c43SAndres Rodriguez dev_err(adev->dev, "(%d) failed to pin bo for amdkfd\n", r); 319130e0371SOded Gabbay goto allocate_mem_pin_bo_failed; 320130e0371SOded Gabbay } 321130e0371SOded Gabbay 322bb812f1eSJunwei Zhang r = amdgpu_ttm_alloc_gart(&bo->tbo); 323bb812f1eSJunwei Zhang if (r) { 324bb812f1eSJunwei Zhang dev_err(adev->dev, "%p bind failed\n", bo); 325bb812f1eSJunwei Zhang goto allocate_mem_kmap_bo_failed; 326bb812f1eSJunwei Zhang } 327bb812f1eSJunwei Zhang 328473fee47SYong Zhao r = amdgpu_bo_kmap(bo, &cpu_ptr_tmp); 329130e0371SOded Gabbay if (r) { 330dc102c43SAndres Rodriguez dev_err(adev->dev, 331130e0371SOded Gabbay "(%d) failed to map bo to kernel for amdkfd\n", r); 332130e0371SOded Gabbay goto allocate_mem_kmap_bo_failed; 333130e0371SOded Gabbay } 334130e0371SOded Gabbay 335473fee47SYong Zhao *mem_obj = bo; 3367b7c6c81SJunwei Zhang *gpu_addr = amdgpu_bo_gpu_offset(bo); 337473fee47SYong Zhao *cpu_ptr = cpu_ptr_tmp; 338473fee47SYong Zhao 339473fee47SYong Zhao amdgpu_bo_unreserve(bo); 340130e0371SOded Gabbay 341130e0371SOded Gabbay return 0; 342130e0371SOded Gabbay 343130e0371SOded Gabbay allocate_mem_kmap_bo_failed: 344473fee47SYong Zhao amdgpu_bo_unpin(bo); 345130e0371SOded Gabbay allocate_mem_pin_bo_failed: 346473fee47SYong Zhao amdgpu_bo_unreserve(bo); 347130e0371SOded Gabbay allocate_mem_reserve_bo_failed: 348473fee47SYong Zhao amdgpu_bo_unref(&bo); 349130e0371SOded Gabbay 350130e0371SOded Gabbay return r; 351130e0371SOded Gabbay } 352130e0371SOded Gabbay 3536bfc7c7eSGraham Sider void amdgpu_amdkfd_free_gtt_mem(struct amdgpu_device *adev, void *mem_obj) 354130e0371SOded Gabbay { 355473fee47SYong Zhao struct amdgpu_bo *bo = (struct amdgpu_bo *) mem_obj; 356130e0371SOded Gabbay 357473fee47SYong Zhao amdgpu_bo_reserve(bo, true); 358473fee47SYong Zhao amdgpu_bo_kunmap(bo); 359473fee47SYong Zhao amdgpu_bo_unpin(bo); 360473fee47SYong Zhao amdgpu_bo_unreserve(bo); 361473fee47SYong Zhao amdgpu_bo_unref(&(bo)); 362130e0371SOded Gabbay } 363130e0371SOded Gabbay 3646bfc7c7eSGraham Sider int amdgpu_amdkfd_alloc_gws(struct amdgpu_device *adev, size_t size, 365ca66fb8fSOak Zeng void **mem_obj) 366ca66fb8fSOak Zeng { 367ca66fb8fSOak Zeng struct amdgpu_bo *bo = NULL; 36822b40f7aSNirmoy Das struct amdgpu_bo_user *ubo; 369ca66fb8fSOak Zeng struct amdgpu_bo_param bp; 370ca66fb8fSOak Zeng int r; 371ca66fb8fSOak Zeng 372ca66fb8fSOak Zeng memset(&bp, 0, sizeof(bp)); 373ca66fb8fSOak Zeng bp.size = size; 374ca66fb8fSOak Zeng bp.byte_align = 1; 375ca66fb8fSOak Zeng bp.domain = AMDGPU_GEM_DOMAIN_GWS; 376ca66fb8fSOak Zeng bp.flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS; 377ca66fb8fSOak Zeng bp.type = ttm_bo_type_device; 378ca66fb8fSOak Zeng bp.resv = NULL; 3799fd5543eSNirmoy Das bp.bo_ptr_size = sizeof(struct amdgpu_bo); 380ca66fb8fSOak Zeng 38122b40f7aSNirmoy Das r = amdgpu_bo_create_user(adev, &bp, &ubo); 382ca66fb8fSOak Zeng if (r) { 383ca66fb8fSOak Zeng dev_err(adev->dev, 384ca66fb8fSOak Zeng "failed to allocate gws BO for amdkfd (%d)\n", r); 385ca66fb8fSOak Zeng return r; 386ca66fb8fSOak Zeng } 387ca66fb8fSOak Zeng 38822b40f7aSNirmoy Das bo = &ubo->bo; 389ca66fb8fSOak Zeng *mem_obj = bo; 390ca66fb8fSOak Zeng return 0; 391ca66fb8fSOak Zeng } 392ca66fb8fSOak Zeng 3936bfc7c7eSGraham Sider void amdgpu_amdkfd_free_gws(struct amdgpu_device *adev, void *mem_obj) 394ca66fb8fSOak Zeng { 395ca66fb8fSOak Zeng struct amdgpu_bo *bo = (struct amdgpu_bo *)mem_obj; 396ca66fb8fSOak Zeng 397ca66fb8fSOak Zeng amdgpu_bo_unref(&bo); 398ca66fb8fSOak Zeng } 399ca66fb8fSOak Zeng 400574c4183SGraham Sider uint32_t amdgpu_amdkfd_get_fw_version(struct amdgpu_device *adev, 4010da8b10eSAmber Lin enum kgd_engine_type type) 4020da8b10eSAmber Lin { 4030da8b10eSAmber Lin switch (type) { 4040da8b10eSAmber Lin case KGD_ENGINE_PFP: 4050da8b10eSAmber Lin return adev->gfx.pfp_fw_version; 4060da8b10eSAmber Lin 4070da8b10eSAmber Lin case KGD_ENGINE_ME: 4080da8b10eSAmber Lin return adev->gfx.me_fw_version; 4090da8b10eSAmber Lin 4100da8b10eSAmber Lin case KGD_ENGINE_CE: 4110da8b10eSAmber Lin return adev->gfx.ce_fw_version; 4120da8b10eSAmber Lin 4130da8b10eSAmber Lin case KGD_ENGINE_MEC1: 4140da8b10eSAmber Lin return adev->gfx.mec_fw_version; 4150da8b10eSAmber Lin 4160da8b10eSAmber Lin case KGD_ENGINE_MEC2: 4170da8b10eSAmber Lin return adev->gfx.mec2_fw_version; 4180da8b10eSAmber Lin 4190da8b10eSAmber Lin case KGD_ENGINE_RLC: 4200da8b10eSAmber Lin return adev->gfx.rlc_fw_version; 4210da8b10eSAmber Lin 4220da8b10eSAmber Lin case KGD_ENGINE_SDMA1: 4230da8b10eSAmber Lin return adev->sdma.instance[0].fw_version; 4240da8b10eSAmber Lin 4250da8b10eSAmber Lin case KGD_ENGINE_SDMA2: 4260da8b10eSAmber Lin return adev->sdma.instance[1].fw_version; 4270da8b10eSAmber Lin 4280da8b10eSAmber Lin default: 4290da8b10eSAmber Lin return 0; 4300da8b10eSAmber Lin } 4310da8b10eSAmber Lin 4320da8b10eSAmber Lin return 0; 4330da8b10eSAmber Lin } 4340da8b10eSAmber Lin 435574c4183SGraham Sider void amdgpu_amdkfd_get_local_mem_info(struct amdgpu_device *adev, 436315e29ecSMukul Joshi struct kfd_local_mem_info *mem_info, 4379a3ce1a7SHawking Zhang struct amdgpu_xcp *xcp) 43830f1c042SHarish Kasiviswanathan { 43930f1c042SHarish Kasiviswanathan memset(mem_info, 0, sizeof(*mem_info)); 4404c7e8a9eSGang Ba 4419a3ce1a7SHawking Zhang if (xcp) { 442315e29ecSMukul Joshi if (adev->gmc.real_vram_size == adev->gmc.visible_vram_size) 443315e29ecSMukul Joshi mem_info->local_mem_size_public = 4449a3ce1a7SHawking Zhang KFD_XCP_MEMORY_SIZE(adev, xcp->id); 445315e29ecSMukul Joshi else 446315e29ecSMukul Joshi mem_info->local_mem_size_private = 4479a3ce1a7SHawking Zhang KFD_XCP_MEMORY_SIZE(adev, xcp->id); 448315e29ecSMukul Joshi } else { 449770d13b1SChristian König mem_info->local_mem_size_public = adev->gmc.visible_vram_size; 450770d13b1SChristian König mem_info->local_mem_size_private = adev->gmc.real_vram_size - 451770d13b1SChristian König adev->gmc.visible_vram_size; 452315e29ecSMukul Joshi } 453770d13b1SChristian König mem_info->vram_width = adev->gmc.vram_width; 45430f1c042SHarish Kasiviswanathan 4554c7e8a9eSGang Ba pr_debug("Address base: %pap public 0x%llx private 0x%llx\n", 4564c7e8a9eSGang Ba &adev->gmc.aper_base, 45730f1c042SHarish Kasiviswanathan mem_info->local_mem_size_public, 45830f1c042SHarish Kasiviswanathan mem_info->local_mem_size_private); 45930f1c042SHarish Kasiviswanathan 4600bc119faSHorace Chen if (adev->pm.dpm_enabled) { 4616bdadb20SHawking Zhang if (amdgpu_emu_mode == 1) 4626bdadb20SHawking Zhang mem_info->mem_clk_max = 0; 4637ba01f9eSShaoyun Liu else 4646bdadb20SHawking Zhang mem_info->mem_clk_max = amdgpu_dpm_get_mclk(adev, false) / 100; 4656bdadb20SHawking Zhang } else 4667ba01f9eSShaoyun Liu mem_info->mem_clk_max = 100; 46730f1c042SHarish Kasiviswanathan } 46830f1c042SHarish Kasiviswanathan 469574c4183SGraham Sider uint64_t amdgpu_amdkfd_get_gpu_clock_counter(struct amdgpu_device *adev) 470130e0371SOded Gabbay { 471dc102c43SAndres Rodriguez if (adev->gfx.funcs->get_gpu_clock_counter) 472dc102c43SAndres Rodriguez return adev->gfx.funcs->get_gpu_clock_counter(adev); 473130e0371SOded Gabbay return 0; 474130e0371SOded Gabbay } 475130e0371SOded Gabbay 476574c4183SGraham Sider uint32_t amdgpu_amdkfd_get_max_engine_clock_in_mhz(struct amdgpu_device *adev) 477130e0371SOded Gabbay { 478a9efcc19SFelix Kuehling /* the sclk is in quantas of 10kHz */ 4790bc119faSHorace Chen if (adev->pm.dpm_enabled) 480a9efcc19SFelix Kuehling return amdgpu_dpm_get_sclk(adev, false) / 100; 4817ba01f9eSShaoyun Liu else 4827ba01f9eSShaoyun Liu return 100; 483130e0371SOded Gabbay } 484ebdebf42SFlora Cui 485574c4183SGraham Sider int amdgpu_amdkfd_get_dmabuf_info(struct amdgpu_device *adev, int dma_buf_fd, 486574c4183SGraham Sider struct amdgpu_device **dmabuf_adev, 4871dde0ea9SFelix Kuehling uint64_t *bo_size, void *metadata_buffer, 4881dde0ea9SFelix Kuehling size_t buffer_size, uint32_t *metadata_size, 4892fa9ff25SPhilip Yang uint32_t *flags, int8_t *xcp_id) 4901dde0ea9SFelix Kuehling { 4911dde0ea9SFelix Kuehling struct dma_buf *dma_buf; 4921dde0ea9SFelix Kuehling struct drm_gem_object *obj; 4931dde0ea9SFelix Kuehling struct amdgpu_bo *bo; 4941dde0ea9SFelix Kuehling uint64_t metadata_flags; 4951dde0ea9SFelix Kuehling int r = -EINVAL; 4961dde0ea9SFelix Kuehling 4971dde0ea9SFelix Kuehling dma_buf = dma_buf_get(dma_buf_fd); 4981dde0ea9SFelix Kuehling if (IS_ERR(dma_buf)) 4991dde0ea9SFelix Kuehling return PTR_ERR(dma_buf); 5001dde0ea9SFelix Kuehling 5011dde0ea9SFelix Kuehling if (dma_buf->ops != &amdgpu_dmabuf_ops) 5021dde0ea9SFelix Kuehling /* Can't handle non-graphics buffers */ 5031dde0ea9SFelix Kuehling goto out_put; 5041dde0ea9SFelix Kuehling 5051dde0ea9SFelix Kuehling obj = dma_buf->priv; 5064a580877SLuben Tuikov if (obj->dev->driver != adev_to_drm(adev)->driver) 5071dde0ea9SFelix Kuehling /* Can't handle buffers from different drivers */ 5081dde0ea9SFelix Kuehling goto out_put; 5091dde0ea9SFelix Kuehling 5101348969aSLuben Tuikov adev = drm_to_adev(obj->dev); 5111dde0ea9SFelix Kuehling bo = gem_to_amdgpu_bo(obj); 5121dde0ea9SFelix Kuehling if (!(bo->preferred_domains & (AMDGPU_GEM_DOMAIN_VRAM | 5131dde0ea9SFelix Kuehling AMDGPU_GEM_DOMAIN_GTT))) 5141dde0ea9SFelix Kuehling /* Only VRAM and GTT BOs are supported */ 5151dde0ea9SFelix Kuehling goto out_put; 5161dde0ea9SFelix Kuehling 5171dde0ea9SFelix Kuehling r = 0; 518574c4183SGraham Sider if (dmabuf_adev) 519574c4183SGraham Sider *dmabuf_adev = adev; 5201dde0ea9SFelix Kuehling if (bo_size) 5211dde0ea9SFelix Kuehling *bo_size = amdgpu_bo_size(bo); 5221dde0ea9SFelix Kuehling if (metadata_buffer) 5231dde0ea9SFelix Kuehling r = amdgpu_bo_get_metadata(bo, metadata_buffer, buffer_size, 5241dde0ea9SFelix Kuehling metadata_size, &metadata_flags); 5251dde0ea9SFelix Kuehling if (flags) { 5261dde0ea9SFelix Kuehling *flags = (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) ? 5271d251d90SYong Zhao KFD_IOC_ALLOC_MEM_FLAGS_VRAM 5281d251d90SYong Zhao : KFD_IOC_ALLOC_MEM_FLAGS_GTT; 5291dde0ea9SFelix Kuehling 5301dde0ea9SFelix Kuehling if (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) 5311d251d90SYong Zhao *flags |= KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC; 5321dde0ea9SFelix Kuehling } 5332fa9ff25SPhilip Yang if (xcp_id) 5342fa9ff25SPhilip Yang *xcp_id = bo->xcp_id; 5351dde0ea9SFelix Kuehling 5361dde0ea9SFelix Kuehling out_put: 5371dde0ea9SFelix Kuehling dma_buf_put(dma_buf); 5381dde0ea9SFelix Kuehling return r; 5391dde0ea9SFelix Kuehling } 5401dde0ea9SFelix Kuehling 541574c4183SGraham Sider uint8_t amdgpu_amdkfd_get_xgmi_hops_count(struct amdgpu_device *dst, 542574c4183SGraham Sider struct amdgpu_device *src) 543da361dd1Sshaoyunl { 544574c4183SGraham Sider struct amdgpu_device *peer_adev = src; 545574c4183SGraham Sider struct amdgpu_device *adev = dst; 546da361dd1Sshaoyunl int ret = amdgpu_xgmi_get_hops_count(adev, peer_adev); 547da361dd1Sshaoyunl 548da361dd1Sshaoyunl if (ret < 0) { 549da361dd1Sshaoyunl DRM_ERROR("amdgpu: failed to get xgmi hops count between node %d and %d. ret = %d\n", 550da361dd1Sshaoyunl adev->gmc.xgmi.physical_node_id, 551da361dd1Sshaoyunl peer_adev->gmc.xgmi.physical_node_id, ret); 552da361dd1Sshaoyunl ret = 0; 553da361dd1Sshaoyunl } 554da361dd1Sshaoyunl return (uint8_t)ret; 555da361dd1Sshaoyunl } 556db8b62c0SShaoyun Liu 557574c4183SGraham Sider int amdgpu_amdkfd_get_xgmi_bandwidth_mbytes(struct amdgpu_device *dst, 558574c4183SGraham Sider struct amdgpu_device *src, 559574c4183SGraham Sider bool is_min) 5603f46c4e9SJonathan Kim { 561574c4183SGraham Sider struct amdgpu_device *adev = dst, *peer_adev; 5623f46c4e9SJonathan Kim int num_links; 5633f46c4e9SJonathan Kim 56435c425f5SJonathan Kim if (amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(9, 4, 2)) 5653f46c4e9SJonathan Kim return 0; 5663f46c4e9SJonathan Kim 5673f46c4e9SJonathan Kim if (src) 568574c4183SGraham Sider peer_adev = src; 5693f46c4e9SJonathan Kim 5703f46c4e9SJonathan Kim /* num links returns 0 for indirect peers since indirect route is unknown. */ 5713f46c4e9SJonathan Kim num_links = is_min ? 1 : amdgpu_xgmi_get_num_links(adev, peer_adev); 5723f46c4e9SJonathan Kim if (num_links < 0) { 5733f46c4e9SJonathan Kim DRM_ERROR("amdgpu: failed to get xgmi num links between node %d and %d. ret = %d\n", 5743f46c4e9SJonathan Kim adev->gmc.xgmi.physical_node_id, 5753f46c4e9SJonathan Kim peer_adev->gmc.xgmi.physical_node_id, num_links); 5763f46c4e9SJonathan Kim num_links = 0; 5773f46c4e9SJonathan Kim } 5783f46c4e9SJonathan Kim 5793f46c4e9SJonathan Kim /* Aldebaran xGMI DPM is defeatured so assume x16 x 25Gbps for bandwidth. */ 5803f46c4e9SJonathan Kim return (num_links * 16 * 25000)/BITS_PER_BYTE; 5813f46c4e9SJonathan Kim } 5823f46c4e9SJonathan Kim 583574c4183SGraham Sider int amdgpu_amdkfd_get_pcie_bandwidth_mbytes(struct amdgpu_device *adev, bool is_min) 58493304810SJonathan Kim { 58593304810SJonathan Kim int num_lanes_shift = (is_min ? ffs(adev->pm.pcie_mlw_mask) : 58693304810SJonathan Kim fls(adev->pm.pcie_mlw_mask)) - 1; 58793304810SJonathan Kim int gen_speed_shift = (is_min ? ffs(adev->pm.pcie_gen_mask & 58893304810SJonathan Kim CAIL_PCIE_LINK_SPEED_SUPPORT_MASK) : 58993304810SJonathan Kim fls(adev->pm.pcie_gen_mask & 59093304810SJonathan Kim CAIL_PCIE_LINK_SPEED_SUPPORT_MASK)) - 1; 59193304810SJonathan Kim uint32_t num_lanes_mask = 1 << num_lanes_shift; 59293304810SJonathan Kim uint32_t gen_speed_mask = 1 << gen_speed_shift; 59393304810SJonathan Kim int num_lanes_factor = 0, gen_speed_mbits_factor = 0; 59493304810SJonathan Kim 59593304810SJonathan Kim switch (num_lanes_mask) { 59693304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X1: 59793304810SJonathan Kim num_lanes_factor = 1; 59893304810SJonathan Kim break; 59993304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X2: 60093304810SJonathan Kim num_lanes_factor = 2; 60193304810SJonathan Kim break; 60293304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X4: 60393304810SJonathan Kim num_lanes_factor = 4; 60493304810SJonathan Kim break; 60593304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X8: 60693304810SJonathan Kim num_lanes_factor = 8; 60793304810SJonathan Kim break; 60893304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X12: 60993304810SJonathan Kim num_lanes_factor = 12; 61093304810SJonathan Kim break; 61193304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X16: 61293304810SJonathan Kim num_lanes_factor = 16; 61393304810SJonathan Kim break; 61493304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X32: 61593304810SJonathan Kim num_lanes_factor = 32; 61693304810SJonathan Kim break; 61793304810SJonathan Kim } 61893304810SJonathan Kim 61993304810SJonathan Kim switch (gen_speed_mask) { 62093304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1: 62193304810SJonathan Kim gen_speed_mbits_factor = 2500; 62293304810SJonathan Kim break; 62393304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2: 62493304810SJonathan Kim gen_speed_mbits_factor = 5000; 62593304810SJonathan Kim break; 62693304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3: 62793304810SJonathan Kim gen_speed_mbits_factor = 8000; 62893304810SJonathan Kim break; 62993304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4: 63093304810SJonathan Kim gen_speed_mbits_factor = 16000; 63193304810SJonathan Kim break; 63293304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5: 63393304810SJonathan Kim gen_speed_mbits_factor = 32000; 63493304810SJonathan Kim break; 63593304810SJonathan Kim } 63693304810SJonathan Kim 63793304810SJonathan Kim return (num_lanes_factor * gen_speed_mbits_factor)/BITS_PER_BYTE; 63893304810SJonathan Kim } 63993304810SJonathan Kim 6406bfc7c7eSGraham Sider int amdgpu_amdkfd_submit_ib(struct amdgpu_device *adev, 6416bfc7c7eSGraham Sider enum kgd_engine_type engine, 6424c660c8fSFelix Kuehling uint32_t vmid, uint64_t gpu_addr, 6434c660c8fSFelix Kuehling uint32_t *ib_cmd, uint32_t ib_len) 6444c660c8fSFelix Kuehling { 6454c660c8fSFelix Kuehling struct amdgpu_job *job; 6464c660c8fSFelix Kuehling struct amdgpu_ib *ib; 6474c660c8fSFelix Kuehling struct amdgpu_ring *ring; 6484c660c8fSFelix Kuehling struct dma_fence *f = NULL; 6494c660c8fSFelix Kuehling int ret; 6504c660c8fSFelix Kuehling 6514c660c8fSFelix Kuehling switch (engine) { 6524c660c8fSFelix Kuehling case KGD_ENGINE_MEC1: 6534c660c8fSFelix Kuehling ring = &adev->gfx.compute_ring[0]; 6544c660c8fSFelix Kuehling break; 6554c660c8fSFelix Kuehling case KGD_ENGINE_SDMA1: 6564c660c8fSFelix Kuehling ring = &adev->sdma.instance[0].ring; 6574c660c8fSFelix Kuehling break; 6584c660c8fSFelix Kuehling case KGD_ENGINE_SDMA2: 6594c660c8fSFelix Kuehling ring = &adev->sdma.instance[1].ring; 6604c660c8fSFelix Kuehling break; 6614c660c8fSFelix Kuehling default: 6624c660c8fSFelix Kuehling pr_err("Invalid engine in IB submission: %d\n", engine); 6634c660c8fSFelix Kuehling ret = -EINVAL; 6644c660c8fSFelix Kuehling goto err; 6654c660c8fSFelix Kuehling } 6664c660c8fSFelix Kuehling 667f7d66fb2SChristian König ret = amdgpu_job_alloc(adev, NULL, NULL, NULL, 1, &job); 6684c660c8fSFelix Kuehling if (ret) 6694c660c8fSFelix Kuehling goto err; 6704c660c8fSFelix Kuehling 6714c660c8fSFelix Kuehling ib = &job->ibs[0]; 6724c660c8fSFelix Kuehling memset(ib, 0, sizeof(struct amdgpu_ib)); 6734c660c8fSFelix Kuehling 6744c660c8fSFelix Kuehling ib->gpu_addr = gpu_addr; 6754c660c8fSFelix Kuehling ib->ptr = ib_cmd; 6764c660c8fSFelix Kuehling ib->length_dw = ib_len; 6774c660c8fSFelix Kuehling /* This works for NO_HWS. TODO: need to handle without knowing VMID */ 6784c660c8fSFelix Kuehling job->vmid = vmid; 6794624459cSChristian König job->num_ibs = 1; 6804c660c8fSFelix Kuehling 6814c660c8fSFelix Kuehling ret = amdgpu_ib_schedule(ring, 1, ib, job, &f); 68294561899SDennis Li 6834c660c8fSFelix Kuehling if (ret) { 6844c660c8fSFelix Kuehling DRM_ERROR("amdgpu: failed to schedule IB.\n"); 6854c660c8fSFelix Kuehling goto err_ib_sched; 6864c660c8fSFelix Kuehling } 6874c660c8fSFelix Kuehling 6889ae55f03SAndrey Grodzovsky /* Drop the initial kref_init count (see drm_sched_main as example) */ 6899ae55f03SAndrey Grodzovsky dma_fence_put(f); 6904c660c8fSFelix Kuehling ret = dma_fence_wait(f, false); 6914c660c8fSFelix Kuehling 6924c660c8fSFelix Kuehling err_ib_sched: 6934c660c8fSFelix Kuehling amdgpu_job_free(job); 6944c660c8fSFelix Kuehling err: 6954c660c8fSFelix Kuehling return ret; 6964c660c8fSFelix Kuehling } 6974c660c8fSFelix Kuehling 6986bfc7c7eSGraham Sider void amdgpu_amdkfd_set_compute_idle(struct amdgpu_device *adev, bool idle) 69901c097dbSFelix Kuehling { 700e341631fSJesse Zhang enum amd_powergating_state state = idle ? AMD_PG_STATE_GATE : AMD_PG_STATE_UNGATE; 701*776b0953SOri Messinger if (IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0)) == 11 && 702*776b0953SOri Messinger ((adev->mes.kiq_version & AMDGPU_MES_VERSION_MASK) <= 64)) { 703087b8542SGraham Sider pr_debug("GFXOFF is %s\n", idle ? "enabled" : "disabled"); 704087b8542SGraham Sider amdgpu_gfx_off_ctrl(adev, idle); 705e341631fSJesse Zhang } else if ((IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0)) == 9) && 706e341631fSJesse Zhang (adev->flags & AMD_IS_APU)) { 707e341631fSJesse Zhang /* Disable GFXOFF and PG. Temporary workaround 708e341631fSJesse Zhang * to fix some compute applications issue on GFX9. 709e341631fSJesse Zhang */ 710e341631fSJesse Zhang adev->ip_blocks[AMD_IP_BLOCK_TYPE_GFX].version->funcs->set_powergating_state((void *)adev, state); 711087b8542SGraham Sider } 71201c097dbSFelix Kuehling amdgpu_dpm_switch_power_profile(adev, 713919a52fcSFelix Kuehling PP_SMC_POWER_PROFILE_COMPUTE, 714919a52fcSFelix Kuehling !idle); 71501c097dbSFelix Kuehling } 71601c097dbSFelix Kuehling 717155494dbSFelix Kuehling bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid) 718155494dbSFelix Kuehling { 71940111ec2SFelix Kuehling if (adev->kfd.dev) 72040111ec2SFelix Kuehling return vmid >= adev->vm_manager.first_kfd_vmid; 721155494dbSFelix Kuehling 722155494dbSFelix Kuehling return false; 723155494dbSFelix Kuehling } 724fcdfa432SOded Gabbay 7256bfc7c7eSGraham Sider bool amdgpu_amdkfd_have_atomics_support(struct amdgpu_device *adev) 726aabf3a95SJack Xiao { 727aabf3a95SJack Xiao return adev->have_atomics_support; 728aabf3a95SJack Xiao } 729c7490949STao Zhou 730a70a93faSJonathan Kim void amdgpu_amdkfd_debug_mem_fence(struct amdgpu_device *adev) 731a70a93faSJonathan Kim { 732a70a93faSJonathan Kim amdgpu_device_flush_hdp(adev, NULL); 733a70a93faSJonathan Kim } 734a70a93faSJonathan Kim 735b6485bedSTao Zhou void amdgpu_amdkfd_ras_poison_consumption_handler(struct amdgpu_device *adev, bool reset) 736c7490949STao Zhou { 7371ed0e176STao Zhou amdgpu_umc_poison_handler(adev, reset); 738c7490949STao Zhou } 7396475ae2bSTao Zhou 74012fb1ad7SJonathan Kim int amdgpu_amdkfd_send_close_event_drain_irq(struct amdgpu_device *adev, 74112fb1ad7SJonathan Kim uint32_t *payload) 74212fb1ad7SJonathan Kim { 74312fb1ad7SJonathan Kim int ret; 74412fb1ad7SJonathan Kim 74512fb1ad7SJonathan Kim /* Device or IH ring is not ready so bail. */ 74612fb1ad7SJonathan Kim ret = amdgpu_ih_wait_on_checkpoint_process_ts(adev, &adev->irq.ih); 74712fb1ad7SJonathan Kim if (ret) 74812fb1ad7SJonathan Kim return ret; 74912fb1ad7SJonathan Kim 75012fb1ad7SJonathan Kim /* Send payload to fence KFD interrupts */ 75112fb1ad7SJonathan Kim amdgpu_amdkfd_interrupt(adev, payload); 75212fb1ad7SJonathan Kim 75312fb1ad7SJonathan Kim return 0; 75412fb1ad7SJonathan Kim } 75512fb1ad7SJonathan Kim 7566475ae2bSTao Zhou bool amdgpu_amdkfd_ras_query_utcl2_poison_status(struct amdgpu_device *adev) 7576475ae2bSTao Zhou { 7583cd3e731SFelix Kuehling if (adev->gfx.ras && adev->gfx.ras->query_utcl2_poison_status) 7596475ae2bSTao Zhou return adev->gfx.ras->query_utcl2_poison_status(adev); 7606475ae2bSTao Zhou else 7616475ae2bSTao Zhou return false; 7626475ae2bSTao Zhou } 7630c7315e7SMukul Joshi 7640c7315e7SMukul Joshi int amdgpu_amdkfd_check_and_lock_kfd(struct amdgpu_device *adev) 7650c7315e7SMukul Joshi { 7660c7315e7SMukul Joshi return kgd2kfd_check_and_lock_kfd(); 7670c7315e7SMukul Joshi } 7680c7315e7SMukul Joshi 7690c7315e7SMukul Joshi void amdgpu_amdkfd_unlock_kfd(struct amdgpu_device *adev) 7700c7315e7SMukul Joshi { 7710c7315e7SMukul Joshi kgd2kfd_unlock_kfd(); 7720c7315e7SMukul Joshi } 77345b3a914SAlex Deucher 77445b3a914SAlex Deucher 77545b3a914SAlex Deucher u64 amdgpu_amdkfd_xcp_memory_size(struct amdgpu_device *adev, int xcp_id) 77645b3a914SAlex Deucher { 77745b3a914SAlex Deucher s8 mem_id = KFD_XCP_MEM_ID(adev, xcp_id); 778f4bff6e0SRajneesh Bhardwaj u64 tmp; 77945b3a914SAlex Deucher 78045b3a914SAlex Deucher if (adev->gmc.num_mem_partitions && xcp_id >= 0 && mem_id >= 0) { 781f4bff6e0SRajneesh Bhardwaj if (adev->gmc.is_app_apu && adev->gmc.num_mem_partitions == 1) { 782f4bff6e0SRajneesh Bhardwaj /* In NPS1 mode, we should restrict the vram reporting 783f4bff6e0SRajneesh Bhardwaj * tied to the ttm_pages_limit which is 1/2 of the system 784f4bff6e0SRajneesh Bhardwaj * memory. For other partition modes, the HBM is uniformly 785f4bff6e0SRajneesh Bhardwaj * divided already per numa node reported. If user wants to 786f4bff6e0SRajneesh Bhardwaj * go beyond the default ttm limit and maximize the ROCm 787f4bff6e0SRajneesh Bhardwaj * allocations, they can go up to max ttm and sysmem limits. 788f4bff6e0SRajneesh Bhardwaj */ 789f4bff6e0SRajneesh Bhardwaj 790f4bff6e0SRajneesh Bhardwaj tmp = (ttm_tt_pages_limit() << PAGE_SHIFT) / num_online_nodes(); 791f4bff6e0SRajneesh Bhardwaj } else { 79245b3a914SAlex Deucher tmp = adev->gmc.mem_partitions[mem_id].size; 793f4bff6e0SRajneesh Bhardwaj } 79445b3a914SAlex Deucher do_div(tmp, adev->xcp_mgr->num_xcp_per_mem_partition); 795acf429dcSPhilip Yang return ALIGN_DOWN(tmp, PAGE_SIZE); 79645b3a914SAlex Deucher } else { 79745b3a914SAlex Deucher return adev->gmc.real_vram_size; 79845b3a914SAlex Deucher } 79945b3a914SAlex Deucher } 8009041b53aSMukul Joshi 8019041b53aSMukul Joshi int amdgpu_amdkfd_unmap_hiq(struct amdgpu_device *adev, u32 doorbell_off, 8029041b53aSMukul Joshi u32 inst) 8039041b53aSMukul Joshi { 8049041b53aSMukul Joshi struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst]; 8059041b53aSMukul Joshi struct amdgpu_ring *kiq_ring = &kiq->ring; 806bd3c4142SSrinivasan Shanmugam struct amdgpu_ring_funcs *ring_funcs; 807bd3c4142SSrinivasan Shanmugam struct amdgpu_ring *ring; 8089041b53aSMukul Joshi int r = 0; 8099041b53aSMukul Joshi 8109041b53aSMukul Joshi if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues) 8119041b53aSMukul Joshi return -EINVAL; 8129041b53aSMukul Joshi 813bd3c4142SSrinivasan Shanmugam ring_funcs = kzalloc(sizeof(*ring_funcs), GFP_KERNEL); 814bd3c4142SSrinivasan Shanmugam if (!ring_funcs) 815bd3c4142SSrinivasan Shanmugam return -ENOMEM; 8169041b53aSMukul Joshi 817bd3c4142SSrinivasan Shanmugam ring = kzalloc(sizeof(*ring), GFP_KERNEL); 818bd3c4142SSrinivasan Shanmugam if (!ring) { 819bd3c4142SSrinivasan Shanmugam r = -ENOMEM; 820bd3c4142SSrinivasan Shanmugam goto free_ring_funcs; 821bd3c4142SSrinivasan Shanmugam } 822bd3c4142SSrinivasan Shanmugam 823bd3c4142SSrinivasan Shanmugam ring_funcs->type = AMDGPU_RING_TYPE_COMPUTE; 824bd3c4142SSrinivasan Shanmugam ring->doorbell_index = doorbell_off; 825bd3c4142SSrinivasan Shanmugam ring->funcs = ring_funcs; 8269041b53aSMukul Joshi 8279041b53aSMukul Joshi spin_lock(&kiq->ring_lock); 8289041b53aSMukul Joshi 8299041b53aSMukul Joshi if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size)) { 8309041b53aSMukul Joshi spin_unlock(&kiq->ring_lock); 831bd3c4142SSrinivasan Shanmugam r = -ENOMEM; 832bd3c4142SSrinivasan Shanmugam goto free_ring; 8339041b53aSMukul Joshi } 8349041b53aSMukul Joshi 835bd3c4142SSrinivasan Shanmugam kiq->pmf->kiq_unmap_queues(kiq_ring, ring, RESET_QUEUES, 0, 0); 8369041b53aSMukul Joshi 8379041b53aSMukul Joshi if (kiq_ring->sched.ready && !adev->job_hang) 8389041b53aSMukul Joshi r = amdgpu_ring_test_helper(kiq_ring); 8399041b53aSMukul Joshi 8409041b53aSMukul Joshi spin_unlock(&kiq->ring_lock); 8419041b53aSMukul Joshi 842bd3c4142SSrinivasan Shanmugam free_ring: 843bd3c4142SSrinivasan Shanmugam kfree(ring); 844bd3c4142SSrinivasan Shanmugam 845bd3c4142SSrinivasan Shanmugam free_ring_funcs: 846bd3c4142SSrinivasan Shanmugam kfree(ring_funcs); 847bd3c4142SSrinivasan Shanmugam 8489041b53aSMukul Joshi return r; 8499041b53aSMukul Joshi } 850