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 141dc102c43SAndres Rodriguez void amdgpu_amdkfd_device_init(struct amdgpu_device *adev) 142130e0371SOded Gabbay { 143234441ddSYong Zhao int i; 144d0b63bb3SAndres Rodriguez int last_valid_bit; 145*18192001SFelix Kuehling int ret; 146611736d8SFelix Kuehling 14727fb73a0SMukul Joshi amdgpu_amdkfd_gpuvm_init_mem_limits(); 14827fb73a0SMukul Joshi 149611736d8SFelix Kuehling if (adev->kfd.dev) { 150130e0371SOded Gabbay struct kgd2kfd_shared_resources gpu_resources = { 15140111ec2SFelix Kuehling .compute_vmid_bitmap = 15240111ec2SFelix Kuehling ((1 << AMDGPU_NUM_VMID) - 1) - 15340111ec2SFelix Kuehling ((1 << adev->vm_manager.first_kfd_vmid) - 1), 154d0b63bb3SAndres Rodriguez .num_pipe_per_mec = adev->gfx.mec.num_pipe_per_mec, 155155494dbSFelix Kuehling .num_queue_per_pipe = adev->gfx.mec.num_queue_per_pipe, 156155494dbSFelix Kuehling .gpuvm_size = min(adev->vm_manager.max_pfn 157155494dbSFelix Kuehling << AMDGPU_GPU_PAGE_SHIFT, 158ad9a5b78SChristian König AMDGPU_GMC_HOLE_START), 1594a580877SLuben Tuikov .drm_render_minor = adev_to_drm(adev)->render->index, 160234441ddSYong Zhao .sdma_doorbell_idx = adev->doorbell_index.sdma_engine, 161cc009e61SMukul Joshi .enable_mes = adev->enable_mes, 162130e0371SOded Gabbay }; 163130e0371SOded Gabbay 164*18192001SFelix Kuehling ret = drm_client_init(&adev->ddev, &adev->kfd.client, "kfd", NULL); 165*18192001SFelix Kuehling if (ret) { 166*18192001SFelix Kuehling dev_err(adev->dev, "Failed to init DRM client: %d\n", ret); 167*18192001SFelix Kuehling return; 168*18192001SFelix Kuehling } 169*18192001SFelix Kuehling 170d0b63bb3SAndres Rodriguez /* this is going to have a few of the MSBs set that we need to 1710d87c9cfSKent Russell * clear 1720d87c9cfSKent Russell */ 173e6945304SYong Zhao bitmap_complement(gpu_resources.cp_queue_bitmap, 174be697aa3SLe Ma adev->gfx.mec_bitmap[0].queue_bitmap, 17568fa72a4SMukul Joshi AMDGPU_MAX_QUEUES); 176d0b63bb3SAndres Rodriguez 177d0b63bb3SAndres Rodriguez /* According to linux/bitmap.h we shouldn't use bitmap_clear if 1780d87c9cfSKent Russell * nbits is not compile time constant 1790d87c9cfSKent Russell */ 1803447d220SJay Cornwall last_valid_bit = 1 /* only first MEC can have compute queues */ 181d0b63bb3SAndres Rodriguez * adev->gfx.mec.num_pipe_per_mec 182d0b63bb3SAndres Rodriguez * adev->gfx.mec.num_queue_per_pipe; 18368fa72a4SMukul Joshi for (i = last_valid_bit; i < AMDGPU_MAX_QUEUES; ++i) 184e6945304SYong Zhao clear_bit(i, gpu_resources.cp_queue_bitmap); 185d0b63bb3SAndres Rodriguez 186dc102c43SAndres Rodriguez amdgpu_doorbell_get_kfd_info(adev, 187130e0371SOded Gabbay &gpu_resources.doorbell_physical_address, 188130e0371SOded Gabbay &gpu_resources.doorbell_aperture_size, 189130e0371SOded Gabbay &gpu_resources.doorbell_start_offset); 190c5892230SShaoyun Liu 1911f86805aSYong Zhao /* Since SOC15, BIF starts to statically use the 1921f86805aSYong Zhao * lower 12 bits of doorbell addresses for routing 1931f86805aSYong Zhao * based on settings in registers like 1941f86805aSYong Zhao * SDMA0_DOORBELL_RANGE etc.. 1951f86805aSYong Zhao * In order to route a doorbell to CP engine, the lower 1961f86805aSYong Zhao * 12 bits of its address has to be outside the range 1971f86805aSYong Zhao * set for SDMA, VCN, and IH blocks. 198642a0e80SFelix Kuehling */ 199234441ddSYong Zhao if (adev->asic_type >= CHIP_VEGA10) { 2001f86805aSYong Zhao gpu_resources.non_cp_doorbells_start = 2011f86805aSYong Zhao adev->doorbell_index.first_non_cp; 2021f86805aSYong Zhao gpu_resources.non_cp_doorbells_end = 2031f86805aSYong Zhao adev->doorbell_index.last_non_cp; 204234441ddSYong Zhao } 205130e0371SOded Gabbay 2068e2712e7Sshaoyunl adev->kfd.init_complete = kgd2kfd_device_init(adev->kfd.dev, 207d69a3b76SMukul Joshi &gpu_resources); 208*18192001SFelix Kuehling if (adev->kfd.init_complete) 209*18192001SFelix Kuehling drm_client_register(&adev->kfd.client); 210*18192001SFelix Kuehling else 211*18192001SFelix Kuehling drm_client_release(&adev->kfd.client); 212b5fd0cf3SAndrey Grodzovsky 2132302d507SPhilip Yang amdgpu_amdkfd_total_mem_size += adev->gmc.real_vram_size; 2142302d507SPhilip Yang 215b5fd0cf3SAndrey Grodzovsky INIT_WORK(&adev->kfd.reset_work, amdgpu_amdkfd_reset_work); 216130e0371SOded Gabbay } 217130e0371SOded Gabbay } 218130e0371SOded Gabbay 219e9669fb7SAndrey Grodzovsky void amdgpu_amdkfd_device_fini_sw(struct amdgpu_device *adev) 220130e0371SOded Gabbay { 221611736d8SFelix Kuehling if (adev->kfd.dev) { 2228e07e267SAmber Lin kgd2kfd_device_exit(adev->kfd.dev); 223611736d8SFelix Kuehling adev->kfd.dev = NULL; 2242302d507SPhilip Yang amdgpu_amdkfd_total_mem_size -= adev->gmc.real_vram_size; 225130e0371SOded Gabbay } 226130e0371SOded Gabbay } 227130e0371SOded Gabbay 228dc102c43SAndres Rodriguez void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev, 229130e0371SOded Gabbay const void *ih_ring_entry) 230130e0371SOded Gabbay { 231611736d8SFelix Kuehling if (adev->kfd.dev) 2328e07e267SAmber Lin kgd2kfd_interrupt(adev->kfd.dev, ih_ring_entry); 233130e0371SOded Gabbay } 234130e0371SOded Gabbay 2359593f4d6SRajneesh Bhardwaj void amdgpu_amdkfd_suspend(struct amdgpu_device *adev, bool run_pm) 236130e0371SOded Gabbay { 237611736d8SFelix Kuehling if (adev->kfd.dev) 2389593f4d6SRajneesh Bhardwaj kgd2kfd_suspend(adev->kfd.dev, run_pm); 239130e0371SOded Gabbay } 240130e0371SOded Gabbay 2419593f4d6SRajneesh Bhardwaj int amdgpu_amdkfd_resume(struct amdgpu_device *adev, bool run_pm) 242130e0371SOded Gabbay { 243130e0371SOded Gabbay int r = 0; 244130e0371SOded Gabbay 245611736d8SFelix Kuehling if (adev->kfd.dev) 2469593f4d6SRajneesh Bhardwaj r = kgd2kfd_resume(adev->kfd.dev, run_pm); 247130e0371SOded Gabbay 248130e0371SOded Gabbay return r; 249130e0371SOded Gabbay } 250130e0371SOded Gabbay 2515c6dd71eSShaoyun Liu int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev) 2525c6dd71eSShaoyun Liu { 2535c6dd71eSShaoyun Liu int r = 0; 2545c6dd71eSShaoyun Liu 255611736d8SFelix Kuehling if (adev->kfd.dev) 2568e07e267SAmber Lin r = kgd2kfd_pre_reset(adev->kfd.dev); 2575c6dd71eSShaoyun Liu 2585c6dd71eSShaoyun Liu return r; 2595c6dd71eSShaoyun Liu } 2605c6dd71eSShaoyun Liu 2615c6dd71eSShaoyun Liu int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev) 2625c6dd71eSShaoyun Liu { 2635c6dd71eSShaoyun Liu int r = 0; 2645c6dd71eSShaoyun Liu 265611736d8SFelix Kuehling if (adev->kfd.dev) 2668e07e267SAmber Lin r = kgd2kfd_post_reset(adev->kfd.dev); 2675c6dd71eSShaoyun Liu 2685c6dd71eSShaoyun Liu return r; 2695c6dd71eSShaoyun Liu } 2705c6dd71eSShaoyun Liu 2716bfc7c7eSGraham Sider void amdgpu_amdkfd_gpu_reset(struct amdgpu_device *adev) 27224da5a9cSShaoyun Liu { 27312938fadSChristian König if (amdgpu_device_should_recover_gpu(adev)) 274b5fd0cf3SAndrey Grodzovsky amdgpu_reset_domain_schedule(adev->reset_domain, 275b5fd0cf3SAndrey Grodzovsky &adev->kfd.reset_work); 27624da5a9cSShaoyun Liu } 27724da5a9cSShaoyun Liu 2786bfc7c7eSGraham Sider int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size, 279130e0371SOded Gabbay void **mem_obj, uint64_t *gpu_addr, 280fa5bde80SYong Zhao void **cpu_ptr, bool cp_mqd_gfx9) 281130e0371SOded Gabbay { 282473fee47SYong Zhao struct amdgpu_bo *bo = NULL; 2833216c6b7SChunming Zhou struct amdgpu_bo_param bp; 284130e0371SOded Gabbay int r; 285473fee47SYong Zhao void *cpu_ptr_tmp = NULL; 286130e0371SOded Gabbay 2873216c6b7SChunming Zhou memset(&bp, 0, sizeof(bp)); 2883216c6b7SChunming Zhou bp.size = size; 2893216c6b7SChunming Zhou bp.byte_align = PAGE_SIZE; 2903216c6b7SChunming Zhou bp.domain = AMDGPU_GEM_DOMAIN_GTT; 2913216c6b7SChunming Zhou bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC; 2923216c6b7SChunming Zhou bp.type = ttm_bo_type_kernel; 2933216c6b7SChunming Zhou bp.resv = NULL; 2949fd5543eSNirmoy Das bp.bo_ptr_size = sizeof(struct amdgpu_bo); 29515426dbbSYong Zhao 296fa5bde80SYong Zhao if (cp_mqd_gfx9) 297fa5bde80SYong Zhao bp.flags |= AMDGPU_GEM_CREATE_CP_MQD_GFX9; 29815426dbbSYong Zhao 2993216c6b7SChunming Zhou r = amdgpu_bo_create(adev, &bp, &bo); 300130e0371SOded Gabbay if (r) { 301dc102c43SAndres Rodriguez dev_err(adev->dev, 302130e0371SOded Gabbay "failed to allocate BO for amdkfd (%d)\n", r); 303130e0371SOded Gabbay return r; 304130e0371SOded Gabbay } 305130e0371SOded Gabbay 306130e0371SOded Gabbay /* map the buffer */ 307473fee47SYong Zhao r = amdgpu_bo_reserve(bo, true); 308130e0371SOded Gabbay if (r) { 309dc102c43SAndres Rodriguez dev_err(adev->dev, "(%d) failed to reserve bo for amdkfd\n", r); 310130e0371SOded Gabbay goto allocate_mem_reserve_bo_failed; 311130e0371SOded Gabbay } 312130e0371SOded Gabbay 3137b7c6c81SJunwei Zhang r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT); 314130e0371SOded Gabbay if (r) { 315dc102c43SAndres Rodriguez dev_err(adev->dev, "(%d) failed to pin bo for amdkfd\n", r); 316130e0371SOded Gabbay goto allocate_mem_pin_bo_failed; 317130e0371SOded Gabbay } 318130e0371SOded Gabbay 319bb812f1eSJunwei Zhang r = amdgpu_ttm_alloc_gart(&bo->tbo); 320bb812f1eSJunwei Zhang if (r) { 321bb812f1eSJunwei Zhang dev_err(adev->dev, "%p bind failed\n", bo); 322bb812f1eSJunwei Zhang goto allocate_mem_kmap_bo_failed; 323bb812f1eSJunwei Zhang } 324bb812f1eSJunwei Zhang 325473fee47SYong Zhao r = amdgpu_bo_kmap(bo, &cpu_ptr_tmp); 326130e0371SOded Gabbay if (r) { 327dc102c43SAndres Rodriguez dev_err(adev->dev, 328130e0371SOded Gabbay "(%d) failed to map bo to kernel for amdkfd\n", r); 329130e0371SOded Gabbay goto allocate_mem_kmap_bo_failed; 330130e0371SOded Gabbay } 331130e0371SOded Gabbay 332473fee47SYong Zhao *mem_obj = bo; 3337b7c6c81SJunwei Zhang *gpu_addr = amdgpu_bo_gpu_offset(bo); 334473fee47SYong Zhao *cpu_ptr = cpu_ptr_tmp; 335473fee47SYong Zhao 336473fee47SYong Zhao amdgpu_bo_unreserve(bo); 337130e0371SOded Gabbay 338130e0371SOded Gabbay return 0; 339130e0371SOded Gabbay 340130e0371SOded Gabbay allocate_mem_kmap_bo_failed: 341473fee47SYong Zhao amdgpu_bo_unpin(bo); 342130e0371SOded Gabbay allocate_mem_pin_bo_failed: 343473fee47SYong Zhao amdgpu_bo_unreserve(bo); 344130e0371SOded Gabbay allocate_mem_reserve_bo_failed: 345473fee47SYong Zhao amdgpu_bo_unref(&bo); 346130e0371SOded Gabbay 347130e0371SOded Gabbay return r; 348130e0371SOded Gabbay } 349130e0371SOded Gabbay 3506bfc7c7eSGraham Sider void amdgpu_amdkfd_free_gtt_mem(struct amdgpu_device *adev, void *mem_obj) 351130e0371SOded Gabbay { 352473fee47SYong Zhao struct amdgpu_bo *bo = (struct amdgpu_bo *) mem_obj; 353130e0371SOded Gabbay 354473fee47SYong Zhao amdgpu_bo_reserve(bo, true); 355473fee47SYong Zhao amdgpu_bo_kunmap(bo); 356473fee47SYong Zhao amdgpu_bo_unpin(bo); 357473fee47SYong Zhao amdgpu_bo_unreserve(bo); 358473fee47SYong Zhao amdgpu_bo_unref(&(bo)); 359130e0371SOded Gabbay } 360130e0371SOded Gabbay 3616bfc7c7eSGraham Sider int amdgpu_amdkfd_alloc_gws(struct amdgpu_device *adev, size_t size, 362ca66fb8fSOak Zeng void **mem_obj) 363ca66fb8fSOak Zeng { 364ca66fb8fSOak Zeng struct amdgpu_bo *bo = NULL; 36522b40f7aSNirmoy Das struct amdgpu_bo_user *ubo; 366ca66fb8fSOak Zeng struct amdgpu_bo_param bp; 367ca66fb8fSOak Zeng int r; 368ca66fb8fSOak Zeng 369ca66fb8fSOak Zeng memset(&bp, 0, sizeof(bp)); 370ca66fb8fSOak Zeng bp.size = size; 371ca66fb8fSOak Zeng bp.byte_align = 1; 372ca66fb8fSOak Zeng bp.domain = AMDGPU_GEM_DOMAIN_GWS; 373ca66fb8fSOak Zeng bp.flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS; 374ca66fb8fSOak Zeng bp.type = ttm_bo_type_device; 375ca66fb8fSOak Zeng bp.resv = NULL; 3769fd5543eSNirmoy Das bp.bo_ptr_size = sizeof(struct amdgpu_bo); 377ca66fb8fSOak Zeng 37822b40f7aSNirmoy Das r = amdgpu_bo_create_user(adev, &bp, &ubo); 379ca66fb8fSOak Zeng if (r) { 380ca66fb8fSOak Zeng dev_err(adev->dev, 381ca66fb8fSOak Zeng "failed to allocate gws BO for amdkfd (%d)\n", r); 382ca66fb8fSOak Zeng return r; 383ca66fb8fSOak Zeng } 384ca66fb8fSOak Zeng 38522b40f7aSNirmoy Das bo = &ubo->bo; 386ca66fb8fSOak Zeng *mem_obj = bo; 387ca66fb8fSOak Zeng return 0; 388ca66fb8fSOak Zeng } 389ca66fb8fSOak Zeng 3906bfc7c7eSGraham Sider void amdgpu_amdkfd_free_gws(struct amdgpu_device *adev, void *mem_obj) 391ca66fb8fSOak Zeng { 392ca66fb8fSOak Zeng struct amdgpu_bo *bo = (struct amdgpu_bo *)mem_obj; 393ca66fb8fSOak Zeng 394ca66fb8fSOak Zeng amdgpu_bo_unref(&bo); 395ca66fb8fSOak Zeng } 396ca66fb8fSOak Zeng 397574c4183SGraham Sider uint32_t amdgpu_amdkfd_get_fw_version(struct amdgpu_device *adev, 3980da8b10eSAmber Lin enum kgd_engine_type type) 3990da8b10eSAmber Lin { 4000da8b10eSAmber Lin switch (type) { 4010da8b10eSAmber Lin case KGD_ENGINE_PFP: 4020da8b10eSAmber Lin return adev->gfx.pfp_fw_version; 4030da8b10eSAmber Lin 4040da8b10eSAmber Lin case KGD_ENGINE_ME: 4050da8b10eSAmber Lin return adev->gfx.me_fw_version; 4060da8b10eSAmber Lin 4070da8b10eSAmber Lin case KGD_ENGINE_CE: 4080da8b10eSAmber Lin return adev->gfx.ce_fw_version; 4090da8b10eSAmber Lin 4100da8b10eSAmber Lin case KGD_ENGINE_MEC1: 4110da8b10eSAmber Lin return adev->gfx.mec_fw_version; 4120da8b10eSAmber Lin 4130da8b10eSAmber Lin case KGD_ENGINE_MEC2: 4140da8b10eSAmber Lin return adev->gfx.mec2_fw_version; 4150da8b10eSAmber Lin 4160da8b10eSAmber Lin case KGD_ENGINE_RLC: 4170da8b10eSAmber Lin return adev->gfx.rlc_fw_version; 4180da8b10eSAmber Lin 4190da8b10eSAmber Lin case KGD_ENGINE_SDMA1: 4200da8b10eSAmber Lin return adev->sdma.instance[0].fw_version; 4210da8b10eSAmber Lin 4220da8b10eSAmber Lin case KGD_ENGINE_SDMA2: 4230da8b10eSAmber Lin return adev->sdma.instance[1].fw_version; 4240da8b10eSAmber Lin 4250da8b10eSAmber Lin default: 4260da8b10eSAmber Lin return 0; 4270da8b10eSAmber Lin } 4280da8b10eSAmber Lin 4290da8b10eSAmber Lin return 0; 4300da8b10eSAmber Lin } 4310da8b10eSAmber Lin 432574c4183SGraham Sider void amdgpu_amdkfd_get_local_mem_info(struct amdgpu_device *adev, 433315e29ecSMukul Joshi struct kfd_local_mem_info *mem_info, 4349a3ce1a7SHawking Zhang struct amdgpu_xcp *xcp) 43530f1c042SHarish Kasiviswanathan { 43630f1c042SHarish Kasiviswanathan memset(mem_info, 0, sizeof(*mem_info)); 4374c7e8a9eSGang Ba 4389a3ce1a7SHawking Zhang if (xcp) { 439315e29ecSMukul Joshi if (adev->gmc.real_vram_size == adev->gmc.visible_vram_size) 440315e29ecSMukul Joshi mem_info->local_mem_size_public = 4419a3ce1a7SHawking Zhang KFD_XCP_MEMORY_SIZE(adev, xcp->id); 442315e29ecSMukul Joshi else 443315e29ecSMukul Joshi mem_info->local_mem_size_private = 4449a3ce1a7SHawking Zhang KFD_XCP_MEMORY_SIZE(adev, xcp->id); 445315e29ecSMukul Joshi } else { 446770d13b1SChristian König mem_info->local_mem_size_public = adev->gmc.visible_vram_size; 447770d13b1SChristian König mem_info->local_mem_size_private = adev->gmc.real_vram_size - 448770d13b1SChristian König adev->gmc.visible_vram_size; 449315e29ecSMukul Joshi } 450770d13b1SChristian König mem_info->vram_width = adev->gmc.vram_width; 45130f1c042SHarish Kasiviswanathan 4524c7e8a9eSGang Ba pr_debug("Address base: %pap public 0x%llx private 0x%llx\n", 4534c7e8a9eSGang Ba &adev->gmc.aper_base, 45430f1c042SHarish Kasiviswanathan mem_info->local_mem_size_public, 45530f1c042SHarish Kasiviswanathan mem_info->local_mem_size_private); 45630f1c042SHarish Kasiviswanathan 4570bc119faSHorace Chen if (adev->pm.dpm_enabled) { 4586bdadb20SHawking Zhang if (amdgpu_emu_mode == 1) 4596bdadb20SHawking Zhang mem_info->mem_clk_max = 0; 4607ba01f9eSShaoyun Liu else 4616bdadb20SHawking Zhang mem_info->mem_clk_max = amdgpu_dpm_get_mclk(adev, false) / 100; 4626bdadb20SHawking Zhang } else 4637ba01f9eSShaoyun Liu mem_info->mem_clk_max = 100; 46430f1c042SHarish Kasiviswanathan } 46530f1c042SHarish Kasiviswanathan 466574c4183SGraham Sider uint64_t amdgpu_amdkfd_get_gpu_clock_counter(struct amdgpu_device *adev) 467130e0371SOded Gabbay { 468dc102c43SAndres Rodriguez if (adev->gfx.funcs->get_gpu_clock_counter) 469dc102c43SAndres Rodriguez return adev->gfx.funcs->get_gpu_clock_counter(adev); 470130e0371SOded Gabbay return 0; 471130e0371SOded Gabbay } 472130e0371SOded Gabbay 473574c4183SGraham Sider uint32_t amdgpu_amdkfd_get_max_engine_clock_in_mhz(struct amdgpu_device *adev) 474130e0371SOded Gabbay { 475a9efcc19SFelix Kuehling /* the sclk is in quantas of 10kHz */ 4760bc119faSHorace Chen if (adev->pm.dpm_enabled) 477a9efcc19SFelix Kuehling return amdgpu_dpm_get_sclk(adev, false) / 100; 4787ba01f9eSShaoyun Liu else 4797ba01f9eSShaoyun Liu return 100; 480130e0371SOded Gabbay } 481ebdebf42SFlora Cui 482574c4183SGraham Sider int amdgpu_amdkfd_get_dmabuf_info(struct amdgpu_device *adev, int dma_buf_fd, 483574c4183SGraham Sider struct amdgpu_device **dmabuf_adev, 4841dde0ea9SFelix Kuehling uint64_t *bo_size, void *metadata_buffer, 4851dde0ea9SFelix Kuehling size_t buffer_size, uint32_t *metadata_size, 4862fa9ff25SPhilip Yang uint32_t *flags, int8_t *xcp_id) 4871dde0ea9SFelix Kuehling { 4881dde0ea9SFelix Kuehling struct dma_buf *dma_buf; 4891dde0ea9SFelix Kuehling struct drm_gem_object *obj; 4901dde0ea9SFelix Kuehling struct amdgpu_bo *bo; 4911dde0ea9SFelix Kuehling uint64_t metadata_flags; 4921dde0ea9SFelix Kuehling int r = -EINVAL; 4931dde0ea9SFelix Kuehling 4941dde0ea9SFelix Kuehling dma_buf = dma_buf_get(dma_buf_fd); 4951dde0ea9SFelix Kuehling if (IS_ERR(dma_buf)) 4961dde0ea9SFelix Kuehling return PTR_ERR(dma_buf); 4971dde0ea9SFelix Kuehling 4981dde0ea9SFelix Kuehling if (dma_buf->ops != &amdgpu_dmabuf_ops) 4991dde0ea9SFelix Kuehling /* Can't handle non-graphics buffers */ 5001dde0ea9SFelix Kuehling goto out_put; 5011dde0ea9SFelix Kuehling 5021dde0ea9SFelix Kuehling obj = dma_buf->priv; 5034a580877SLuben Tuikov if (obj->dev->driver != adev_to_drm(adev)->driver) 5041dde0ea9SFelix Kuehling /* Can't handle buffers from different drivers */ 5051dde0ea9SFelix Kuehling goto out_put; 5061dde0ea9SFelix Kuehling 5071348969aSLuben Tuikov adev = drm_to_adev(obj->dev); 5081dde0ea9SFelix Kuehling bo = gem_to_amdgpu_bo(obj); 5091dde0ea9SFelix Kuehling if (!(bo->preferred_domains & (AMDGPU_GEM_DOMAIN_VRAM | 5101dde0ea9SFelix Kuehling AMDGPU_GEM_DOMAIN_GTT))) 5111dde0ea9SFelix Kuehling /* Only VRAM and GTT BOs are supported */ 5121dde0ea9SFelix Kuehling goto out_put; 5131dde0ea9SFelix Kuehling 5141dde0ea9SFelix Kuehling r = 0; 515574c4183SGraham Sider if (dmabuf_adev) 516574c4183SGraham Sider *dmabuf_adev = adev; 5171dde0ea9SFelix Kuehling if (bo_size) 5181dde0ea9SFelix Kuehling *bo_size = amdgpu_bo_size(bo); 5191dde0ea9SFelix Kuehling if (metadata_buffer) 5201dde0ea9SFelix Kuehling r = amdgpu_bo_get_metadata(bo, metadata_buffer, buffer_size, 5211dde0ea9SFelix Kuehling metadata_size, &metadata_flags); 5221dde0ea9SFelix Kuehling if (flags) { 5231dde0ea9SFelix Kuehling *flags = (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) ? 5241d251d90SYong Zhao KFD_IOC_ALLOC_MEM_FLAGS_VRAM 5251d251d90SYong Zhao : KFD_IOC_ALLOC_MEM_FLAGS_GTT; 5261dde0ea9SFelix Kuehling 5271dde0ea9SFelix Kuehling if (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) 5281d251d90SYong Zhao *flags |= KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC; 5291dde0ea9SFelix Kuehling } 5302fa9ff25SPhilip Yang if (xcp_id) 5312fa9ff25SPhilip Yang *xcp_id = bo->xcp_id; 5321dde0ea9SFelix Kuehling 5331dde0ea9SFelix Kuehling out_put: 5341dde0ea9SFelix Kuehling dma_buf_put(dma_buf); 5351dde0ea9SFelix Kuehling return r; 5361dde0ea9SFelix Kuehling } 5371dde0ea9SFelix Kuehling 538574c4183SGraham Sider uint8_t amdgpu_amdkfd_get_xgmi_hops_count(struct amdgpu_device *dst, 539574c4183SGraham Sider struct amdgpu_device *src) 540da361dd1Sshaoyunl { 541574c4183SGraham Sider struct amdgpu_device *peer_adev = src; 542574c4183SGraham Sider struct amdgpu_device *adev = dst; 543da361dd1Sshaoyunl int ret = amdgpu_xgmi_get_hops_count(adev, peer_adev); 544da361dd1Sshaoyunl 545da361dd1Sshaoyunl if (ret < 0) { 546da361dd1Sshaoyunl DRM_ERROR("amdgpu: failed to get xgmi hops count between node %d and %d. ret = %d\n", 547da361dd1Sshaoyunl adev->gmc.xgmi.physical_node_id, 548da361dd1Sshaoyunl peer_adev->gmc.xgmi.physical_node_id, ret); 549da361dd1Sshaoyunl ret = 0; 550da361dd1Sshaoyunl } 551da361dd1Sshaoyunl return (uint8_t)ret; 552da361dd1Sshaoyunl } 553db8b62c0SShaoyun Liu 554574c4183SGraham Sider int amdgpu_amdkfd_get_xgmi_bandwidth_mbytes(struct amdgpu_device *dst, 555574c4183SGraham Sider struct amdgpu_device *src, 556574c4183SGraham Sider bool is_min) 5573f46c4e9SJonathan Kim { 558574c4183SGraham Sider struct amdgpu_device *adev = dst, *peer_adev; 5593f46c4e9SJonathan Kim int num_links; 5603f46c4e9SJonathan Kim 56135c425f5SJonathan Kim if (amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(9, 4, 2)) 5623f46c4e9SJonathan Kim return 0; 5633f46c4e9SJonathan Kim 5643f46c4e9SJonathan Kim if (src) 565574c4183SGraham Sider peer_adev = src; 5663f46c4e9SJonathan Kim 5673f46c4e9SJonathan Kim /* num links returns 0 for indirect peers since indirect route is unknown. */ 5683f46c4e9SJonathan Kim num_links = is_min ? 1 : amdgpu_xgmi_get_num_links(adev, peer_adev); 5693f46c4e9SJonathan Kim if (num_links < 0) { 5703f46c4e9SJonathan Kim DRM_ERROR("amdgpu: failed to get xgmi num links between node %d and %d. ret = %d\n", 5713f46c4e9SJonathan Kim adev->gmc.xgmi.physical_node_id, 5723f46c4e9SJonathan Kim peer_adev->gmc.xgmi.physical_node_id, num_links); 5733f46c4e9SJonathan Kim num_links = 0; 5743f46c4e9SJonathan Kim } 5753f46c4e9SJonathan Kim 5763f46c4e9SJonathan Kim /* Aldebaran xGMI DPM is defeatured so assume x16 x 25Gbps for bandwidth. */ 5773f46c4e9SJonathan Kim return (num_links * 16 * 25000)/BITS_PER_BYTE; 5783f46c4e9SJonathan Kim } 5793f46c4e9SJonathan Kim 580574c4183SGraham Sider int amdgpu_amdkfd_get_pcie_bandwidth_mbytes(struct amdgpu_device *adev, bool is_min) 58193304810SJonathan Kim { 58293304810SJonathan Kim int num_lanes_shift = (is_min ? ffs(adev->pm.pcie_mlw_mask) : 58393304810SJonathan Kim fls(adev->pm.pcie_mlw_mask)) - 1; 58493304810SJonathan Kim int gen_speed_shift = (is_min ? ffs(adev->pm.pcie_gen_mask & 58593304810SJonathan Kim CAIL_PCIE_LINK_SPEED_SUPPORT_MASK) : 58693304810SJonathan Kim fls(adev->pm.pcie_gen_mask & 58793304810SJonathan Kim CAIL_PCIE_LINK_SPEED_SUPPORT_MASK)) - 1; 58893304810SJonathan Kim uint32_t num_lanes_mask = 1 << num_lanes_shift; 58993304810SJonathan Kim uint32_t gen_speed_mask = 1 << gen_speed_shift; 59093304810SJonathan Kim int num_lanes_factor = 0, gen_speed_mbits_factor = 0; 59193304810SJonathan Kim 59293304810SJonathan Kim switch (num_lanes_mask) { 59393304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X1: 59493304810SJonathan Kim num_lanes_factor = 1; 59593304810SJonathan Kim break; 59693304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X2: 59793304810SJonathan Kim num_lanes_factor = 2; 59893304810SJonathan Kim break; 59993304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X4: 60093304810SJonathan Kim num_lanes_factor = 4; 60193304810SJonathan Kim break; 60293304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X8: 60393304810SJonathan Kim num_lanes_factor = 8; 60493304810SJonathan Kim break; 60593304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X12: 60693304810SJonathan Kim num_lanes_factor = 12; 60793304810SJonathan Kim break; 60893304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X16: 60993304810SJonathan Kim num_lanes_factor = 16; 61093304810SJonathan Kim break; 61193304810SJonathan Kim case CAIL_PCIE_LINK_WIDTH_SUPPORT_X32: 61293304810SJonathan Kim num_lanes_factor = 32; 61393304810SJonathan Kim break; 61493304810SJonathan Kim } 61593304810SJonathan Kim 61693304810SJonathan Kim switch (gen_speed_mask) { 61793304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1: 61893304810SJonathan Kim gen_speed_mbits_factor = 2500; 61993304810SJonathan Kim break; 62093304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2: 62193304810SJonathan Kim gen_speed_mbits_factor = 5000; 62293304810SJonathan Kim break; 62393304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3: 62493304810SJonathan Kim gen_speed_mbits_factor = 8000; 62593304810SJonathan Kim break; 62693304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4: 62793304810SJonathan Kim gen_speed_mbits_factor = 16000; 62893304810SJonathan Kim break; 62993304810SJonathan Kim case CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5: 63093304810SJonathan Kim gen_speed_mbits_factor = 32000; 63193304810SJonathan Kim break; 63293304810SJonathan Kim } 63393304810SJonathan Kim 63493304810SJonathan Kim return (num_lanes_factor * gen_speed_mbits_factor)/BITS_PER_BYTE; 63593304810SJonathan Kim } 63693304810SJonathan Kim 6376bfc7c7eSGraham Sider int amdgpu_amdkfd_submit_ib(struct amdgpu_device *adev, 6386bfc7c7eSGraham Sider enum kgd_engine_type engine, 6394c660c8fSFelix Kuehling uint32_t vmid, uint64_t gpu_addr, 6404c660c8fSFelix Kuehling uint32_t *ib_cmd, uint32_t ib_len) 6414c660c8fSFelix Kuehling { 6424c660c8fSFelix Kuehling struct amdgpu_job *job; 6434c660c8fSFelix Kuehling struct amdgpu_ib *ib; 6444c660c8fSFelix Kuehling struct amdgpu_ring *ring; 6454c660c8fSFelix Kuehling struct dma_fence *f = NULL; 6464c660c8fSFelix Kuehling int ret; 6474c660c8fSFelix Kuehling 6484c660c8fSFelix Kuehling switch (engine) { 6494c660c8fSFelix Kuehling case KGD_ENGINE_MEC1: 6504c660c8fSFelix Kuehling ring = &adev->gfx.compute_ring[0]; 6514c660c8fSFelix Kuehling break; 6524c660c8fSFelix Kuehling case KGD_ENGINE_SDMA1: 6534c660c8fSFelix Kuehling ring = &adev->sdma.instance[0].ring; 6544c660c8fSFelix Kuehling break; 6554c660c8fSFelix Kuehling case KGD_ENGINE_SDMA2: 6564c660c8fSFelix Kuehling ring = &adev->sdma.instance[1].ring; 6574c660c8fSFelix Kuehling break; 6584c660c8fSFelix Kuehling default: 6594c660c8fSFelix Kuehling pr_err("Invalid engine in IB submission: %d\n", engine); 6604c660c8fSFelix Kuehling ret = -EINVAL; 6614c660c8fSFelix Kuehling goto err; 6624c660c8fSFelix Kuehling } 6634c660c8fSFelix Kuehling 664f7d66fb2SChristian König ret = amdgpu_job_alloc(adev, NULL, NULL, NULL, 1, &job); 6654c660c8fSFelix Kuehling if (ret) 6664c660c8fSFelix Kuehling goto err; 6674c660c8fSFelix Kuehling 6684c660c8fSFelix Kuehling ib = &job->ibs[0]; 6694c660c8fSFelix Kuehling memset(ib, 0, sizeof(struct amdgpu_ib)); 6704c660c8fSFelix Kuehling 6714c660c8fSFelix Kuehling ib->gpu_addr = gpu_addr; 6724c660c8fSFelix Kuehling ib->ptr = ib_cmd; 6734c660c8fSFelix Kuehling ib->length_dw = ib_len; 6744c660c8fSFelix Kuehling /* This works for NO_HWS. TODO: need to handle without knowing VMID */ 6754c660c8fSFelix Kuehling job->vmid = vmid; 6764624459cSChristian König job->num_ibs = 1; 6774c660c8fSFelix Kuehling 6784c660c8fSFelix Kuehling ret = amdgpu_ib_schedule(ring, 1, ib, job, &f); 67994561899SDennis Li 6804c660c8fSFelix Kuehling if (ret) { 6814c660c8fSFelix Kuehling DRM_ERROR("amdgpu: failed to schedule IB.\n"); 6824c660c8fSFelix Kuehling goto err_ib_sched; 6834c660c8fSFelix Kuehling } 6844c660c8fSFelix Kuehling 6859ae55f03SAndrey Grodzovsky /* Drop the initial kref_init count (see drm_sched_main as example) */ 6869ae55f03SAndrey Grodzovsky dma_fence_put(f); 6874c660c8fSFelix Kuehling ret = dma_fence_wait(f, false); 6884c660c8fSFelix Kuehling 6894c660c8fSFelix Kuehling err_ib_sched: 6904c660c8fSFelix Kuehling amdgpu_job_free(job); 6914c660c8fSFelix Kuehling err: 6924c660c8fSFelix Kuehling return ret; 6934c660c8fSFelix Kuehling } 6944c660c8fSFelix Kuehling 6956bfc7c7eSGraham Sider void amdgpu_amdkfd_set_compute_idle(struct amdgpu_device *adev, bool idle) 69601c097dbSFelix Kuehling { 697e341631fSJesse Zhang enum amd_powergating_state state = idle ? AMD_PG_STATE_GATE : AMD_PG_STATE_UNGATE; 698087b8542SGraham Sider /* Temporary workaround to fix issues observed in some 699087b8542SGraham Sider * compute applications when GFXOFF is enabled on GFX11. 700087b8542SGraham Sider */ 7014e8303cfSLijo Lazar if (IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0)) == 11) { 702087b8542SGraham Sider pr_debug("GFXOFF is %s\n", idle ? "enabled" : "disabled"); 703087b8542SGraham Sider amdgpu_gfx_off_ctrl(adev, idle); 704e341631fSJesse Zhang } else if ((IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0)) == 9) && 705e341631fSJesse Zhang (adev->flags & AMD_IS_APU)) { 706e341631fSJesse Zhang /* Disable GFXOFF and PG. Temporary workaround 707e341631fSJesse Zhang * to fix some compute applications issue on GFX9. 708e341631fSJesse Zhang */ 709e341631fSJesse Zhang adev->ip_blocks[AMD_IP_BLOCK_TYPE_GFX].version->funcs->set_powergating_state((void *)adev, state); 710087b8542SGraham Sider } 71101c097dbSFelix Kuehling amdgpu_dpm_switch_power_profile(adev, 712919a52fcSFelix Kuehling PP_SMC_POWER_PROFILE_COMPUTE, 713919a52fcSFelix Kuehling !idle); 71401c097dbSFelix Kuehling } 71501c097dbSFelix Kuehling 716155494dbSFelix Kuehling bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid) 717155494dbSFelix Kuehling { 71840111ec2SFelix Kuehling if (adev->kfd.dev) 71940111ec2SFelix Kuehling return vmid >= adev->vm_manager.first_kfd_vmid; 720155494dbSFelix Kuehling 721155494dbSFelix Kuehling return false; 722155494dbSFelix Kuehling } 723fcdfa432SOded Gabbay 7246bfc7c7eSGraham Sider bool amdgpu_amdkfd_have_atomics_support(struct amdgpu_device *adev) 725aabf3a95SJack Xiao { 726aabf3a95SJack Xiao return adev->have_atomics_support; 727aabf3a95SJack Xiao } 728c7490949STao Zhou 729a70a93faSJonathan Kim void amdgpu_amdkfd_debug_mem_fence(struct amdgpu_device *adev) 730a70a93faSJonathan Kim { 731a70a93faSJonathan Kim amdgpu_device_flush_hdp(adev, NULL); 732a70a93faSJonathan Kim } 733a70a93faSJonathan Kim 734b6485bedSTao Zhou void amdgpu_amdkfd_ras_poison_consumption_handler(struct amdgpu_device *adev, bool reset) 735c7490949STao Zhou { 7361ed0e176STao Zhou amdgpu_umc_poison_handler(adev, reset); 737c7490949STao Zhou } 7386475ae2bSTao Zhou 73912fb1ad7SJonathan Kim int amdgpu_amdkfd_send_close_event_drain_irq(struct amdgpu_device *adev, 74012fb1ad7SJonathan Kim uint32_t *payload) 74112fb1ad7SJonathan Kim { 74212fb1ad7SJonathan Kim int ret; 74312fb1ad7SJonathan Kim 74412fb1ad7SJonathan Kim /* Device or IH ring is not ready so bail. */ 74512fb1ad7SJonathan Kim ret = amdgpu_ih_wait_on_checkpoint_process_ts(adev, &adev->irq.ih); 74612fb1ad7SJonathan Kim if (ret) 74712fb1ad7SJonathan Kim return ret; 74812fb1ad7SJonathan Kim 74912fb1ad7SJonathan Kim /* Send payload to fence KFD interrupts */ 75012fb1ad7SJonathan Kim amdgpu_amdkfd_interrupt(adev, payload); 75112fb1ad7SJonathan Kim 75212fb1ad7SJonathan Kim return 0; 75312fb1ad7SJonathan Kim } 75412fb1ad7SJonathan Kim 7556475ae2bSTao Zhou bool amdgpu_amdkfd_ras_query_utcl2_poison_status(struct amdgpu_device *adev) 7566475ae2bSTao Zhou { 7573cd3e731SFelix Kuehling if (adev->gfx.ras && adev->gfx.ras->query_utcl2_poison_status) 7586475ae2bSTao Zhou return adev->gfx.ras->query_utcl2_poison_status(adev); 7596475ae2bSTao Zhou else 7606475ae2bSTao Zhou return false; 7616475ae2bSTao Zhou } 7620c7315e7SMukul Joshi 7630c7315e7SMukul Joshi int amdgpu_amdkfd_check_and_lock_kfd(struct amdgpu_device *adev) 7640c7315e7SMukul Joshi { 7650c7315e7SMukul Joshi return kgd2kfd_check_and_lock_kfd(); 7660c7315e7SMukul Joshi } 7670c7315e7SMukul Joshi 7680c7315e7SMukul Joshi void amdgpu_amdkfd_unlock_kfd(struct amdgpu_device *adev) 7690c7315e7SMukul Joshi { 7700c7315e7SMukul Joshi kgd2kfd_unlock_kfd(); 7710c7315e7SMukul Joshi } 77245b3a914SAlex Deucher 77345b3a914SAlex Deucher 77445b3a914SAlex Deucher u64 amdgpu_amdkfd_xcp_memory_size(struct amdgpu_device *adev, int xcp_id) 77545b3a914SAlex Deucher { 77645b3a914SAlex Deucher s8 mem_id = KFD_XCP_MEM_ID(adev, xcp_id); 777f4bff6e0SRajneesh Bhardwaj u64 tmp; 77845b3a914SAlex Deucher 77945b3a914SAlex Deucher if (adev->gmc.num_mem_partitions && xcp_id >= 0 && mem_id >= 0) { 780f4bff6e0SRajneesh Bhardwaj if (adev->gmc.is_app_apu && adev->gmc.num_mem_partitions == 1) { 781f4bff6e0SRajneesh Bhardwaj /* In NPS1 mode, we should restrict the vram reporting 782f4bff6e0SRajneesh Bhardwaj * tied to the ttm_pages_limit which is 1/2 of the system 783f4bff6e0SRajneesh Bhardwaj * memory. For other partition modes, the HBM is uniformly 784f4bff6e0SRajneesh Bhardwaj * divided already per numa node reported. If user wants to 785f4bff6e0SRajneesh Bhardwaj * go beyond the default ttm limit and maximize the ROCm 786f4bff6e0SRajneesh Bhardwaj * allocations, they can go up to max ttm and sysmem limits. 787f4bff6e0SRajneesh Bhardwaj */ 788f4bff6e0SRajneesh Bhardwaj 789f4bff6e0SRajneesh Bhardwaj tmp = (ttm_tt_pages_limit() << PAGE_SHIFT) / num_online_nodes(); 790f4bff6e0SRajneesh Bhardwaj } else { 79145b3a914SAlex Deucher tmp = adev->gmc.mem_partitions[mem_id].size; 792f4bff6e0SRajneesh Bhardwaj } 79345b3a914SAlex Deucher do_div(tmp, adev->xcp_mgr->num_xcp_per_mem_partition); 794acf429dcSPhilip Yang return ALIGN_DOWN(tmp, PAGE_SIZE); 79545b3a914SAlex Deucher } else { 79645b3a914SAlex Deucher return adev->gmc.real_vram_size; 79745b3a914SAlex Deucher } 79845b3a914SAlex Deucher } 7999041b53aSMukul Joshi 8009041b53aSMukul Joshi int amdgpu_amdkfd_unmap_hiq(struct amdgpu_device *adev, u32 doorbell_off, 8019041b53aSMukul Joshi u32 inst) 8029041b53aSMukul Joshi { 8039041b53aSMukul Joshi struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst]; 8049041b53aSMukul Joshi struct amdgpu_ring *kiq_ring = &kiq->ring; 805bd3c4142SSrinivasan Shanmugam struct amdgpu_ring_funcs *ring_funcs; 806bd3c4142SSrinivasan Shanmugam struct amdgpu_ring *ring; 8079041b53aSMukul Joshi int r = 0; 8089041b53aSMukul Joshi 8099041b53aSMukul Joshi if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues) 8109041b53aSMukul Joshi return -EINVAL; 8119041b53aSMukul Joshi 812bd3c4142SSrinivasan Shanmugam ring_funcs = kzalloc(sizeof(*ring_funcs), GFP_KERNEL); 813bd3c4142SSrinivasan Shanmugam if (!ring_funcs) 814bd3c4142SSrinivasan Shanmugam return -ENOMEM; 8159041b53aSMukul Joshi 816bd3c4142SSrinivasan Shanmugam ring = kzalloc(sizeof(*ring), GFP_KERNEL); 817bd3c4142SSrinivasan Shanmugam if (!ring) { 818bd3c4142SSrinivasan Shanmugam r = -ENOMEM; 819bd3c4142SSrinivasan Shanmugam goto free_ring_funcs; 820bd3c4142SSrinivasan Shanmugam } 821bd3c4142SSrinivasan Shanmugam 822bd3c4142SSrinivasan Shanmugam ring_funcs->type = AMDGPU_RING_TYPE_COMPUTE; 823bd3c4142SSrinivasan Shanmugam ring->doorbell_index = doorbell_off; 824bd3c4142SSrinivasan Shanmugam ring->funcs = ring_funcs; 8259041b53aSMukul Joshi 8269041b53aSMukul Joshi spin_lock(&kiq->ring_lock); 8279041b53aSMukul Joshi 8289041b53aSMukul Joshi if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size)) { 8299041b53aSMukul Joshi spin_unlock(&kiq->ring_lock); 830bd3c4142SSrinivasan Shanmugam r = -ENOMEM; 831bd3c4142SSrinivasan Shanmugam goto free_ring; 8329041b53aSMukul Joshi } 8339041b53aSMukul Joshi 834bd3c4142SSrinivasan Shanmugam kiq->pmf->kiq_unmap_queues(kiq_ring, ring, RESET_QUEUES, 0, 0); 8359041b53aSMukul Joshi 8369041b53aSMukul Joshi if (kiq_ring->sched.ready && !adev->job_hang) 8379041b53aSMukul Joshi r = amdgpu_ring_test_helper(kiq_ring); 8389041b53aSMukul Joshi 8399041b53aSMukul Joshi spin_unlock(&kiq->ring_lock); 8409041b53aSMukul Joshi 841bd3c4142SSrinivasan Shanmugam free_ring: 842bd3c4142SSrinivasan Shanmugam kfree(ring); 843bd3c4142SSrinivasan Shanmugam 844bd3c4142SSrinivasan Shanmugam free_ring_funcs: 845bd3c4142SSrinivasan Shanmugam kfree(ring_funcs); 846bd3c4142SSrinivasan Shanmugam 8479041b53aSMukul Joshi return r; 8489041b53aSMukul Joshi } 849