14a488a7aSOded Gabbay /* 24a488a7aSOded Gabbay * Copyright 2014 Advanced Micro Devices, Inc. 34a488a7aSOded Gabbay * 44a488a7aSOded Gabbay * Permission is hereby granted, free of charge, to any person obtaining a 54a488a7aSOded Gabbay * copy of this software and associated documentation files (the "Software"), 64a488a7aSOded Gabbay * to deal in the Software without restriction, including without limitation 74a488a7aSOded Gabbay * the rights to use, copy, modify, merge, publish, distribute, sublicense, 84a488a7aSOded Gabbay * and/or sell copies of the Software, and to permit persons to whom the 94a488a7aSOded Gabbay * Software is furnished to do so, subject to the following conditions: 104a488a7aSOded Gabbay * 114a488a7aSOded Gabbay * The above copyright notice and this permission notice shall be included in 124a488a7aSOded Gabbay * all copies or substantial portions of the Software. 134a488a7aSOded Gabbay * 144a488a7aSOded Gabbay * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 154a488a7aSOded Gabbay * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 164a488a7aSOded Gabbay * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 174a488a7aSOded Gabbay * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 184a488a7aSOded Gabbay * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 194a488a7aSOded Gabbay * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 204a488a7aSOded Gabbay * OTHER DEALINGS IN THE SOFTWARE. 214a488a7aSOded Gabbay */ 224a488a7aSOded Gabbay 234a488a7aSOded Gabbay #include <linux/bsearch.h> 244a488a7aSOded Gabbay #include <linux/pci.h> 254a488a7aSOded Gabbay #include <linux/slab.h> 264a488a7aSOded Gabbay #include "kfd_priv.h" 2764c7f8cfSBen Goz #include "kfd_device_queue_manager.h" 28507968ddSFelix Kuehling #include "kfd_pm4_headers_vi.h" 29fd6a440eSJonathan Kim #include "kfd_pm4_headers_aldebaran.h" 300db54b24SYong Zhao #include "cwsr_trap_handler.h" 3164d1c3a4SFelix Kuehling #include "kfd_iommu.h" 325b87245fSAmber Lin #include "amdgpu_amdkfd.h" 332c2b0d88SMukul Joshi #include "kfd_smi_events.h" 34814ab993SPhilip Yang #include "kfd_migrate.h" 355b983db8SAlex Deucher #include "amdgpu.h" 364a488a7aSOded Gabbay 3719f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768 38e42051d2SShaoyun Liu 39e42051d2SShaoyun Liu /* 40e42051d2SShaoyun Liu * kfd_locked is used to lock the kfd driver during suspend or reset 41e42051d2SShaoyun Liu * once locked, kfd driver will stop any further GPU execution. 42e42051d2SShaoyun Liu * create process (open) will return -EAGAIN. 43e42051d2SShaoyun Liu */ 44e42051d2SShaoyun Liu static atomic_t kfd_locked = ATOMIC_INIT(0); 4519f6d2a6SOded Gabbay 46a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK 47e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v7_kfd2kgd; 48a3e520a2SAlex Deucher #endif 49e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v8_kfd2kgd; 50e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v9_kfd2kgd; 51e392c887SYong Zhao extern const struct kfd2kgd_calls arcturus_kfd2kgd; 525073506cSJonathan Kim extern const struct kfd2kgd_calls aldebaran_kfd2kgd; 53e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v10_kfd2kgd; 543a2f0c81SYong Zhao extern const struct kfd2kgd_calls gfx_v10_3_kfd2kgd; 55e392c887SYong Zhao 566e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 576e81090bSOded Gabbay unsigned int chunk_size); 586e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd); 596e81090bSOded Gabbay 60b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd); 61b8935a7cSYong Zhao 62f89c6bf7SGuchun Chen static void kfd_device_info_set_sdma_queue_num(struct kfd_dev *kfd) 63f89c6bf7SGuchun Chen { 64f89c6bf7SGuchun Chen uint32_t sdma_version = kfd->adev->ip_versions[SDMA0_HWIP][0]; 65f89c6bf7SGuchun Chen 66f89c6bf7SGuchun Chen switch (sdma_version) { 67f89c6bf7SGuchun Chen case IP_VERSION(4, 0, 0):/* VEGA10 */ 68f89c6bf7SGuchun Chen case IP_VERSION(4, 0, 1):/* VEGA12 */ 69f89c6bf7SGuchun Chen case IP_VERSION(4, 1, 0):/* RAVEN */ 70f89c6bf7SGuchun Chen case IP_VERSION(4, 1, 1):/* RAVEN */ 715eb877b2SKent Russell case IP_VERSION(4, 1, 2):/* RENOIR */ 72f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 1):/* VANGOGH */ 73f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 3):/* YELLOW_CARP */ 74f89c6bf7SGuchun Chen kfd->device_info.num_sdma_queues_per_engine = 2; 75f89c6bf7SGuchun Chen break; 76f89c6bf7SGuchun Chen case IP_VERSION(4, 2, 0):/* VEGA20 */ 775eb877b2SKent Russell case IP_VERSION(4, 2, 2):/* ARCTURUS */ 78f89c6bf7SGuchun Chen case IP_VERSION(4, 4, 0):/* ALDEBARAN */ 79f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 0):/* NAVI10 */ 80f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 1):/* CYAN_SKILLFISH */ 81f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 2):/* NAVI14 */ 82f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 5):/* NAVI12 */ 83f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 0):/* SIENNA_CICHLID */ 845eb877b2SKent Russell case IP_VERSION(5, 2, 2):/* NAVY_FLOUNDER */ 85f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 4):/* DIMGREY_CAVEFISH */ 86f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 5):/* BEIGE_GOBY */ 87f89c6bf7SGuchun Chen kfd->device_info.num_sdma_queues_per_engine = 8; 88f89c6bf7SGuchun Chen break; 89f89c6bf7SGuchun Chen default: 90f89c6bf7SGuchun Chen dev_warn(kfd_device, 9120c5e425SGraham Sider "Default sdma queue per engine(8) is set due to mismatch of sdma ip block(SDMA_HWIP:0x%x).\n", 92f89c6bf7SGuchun Chen sdma_version); 93f89c6bf7SGuchun Chen kfd->device_info.num_sdma_queues_per_engine = 8; 94f89c6bf7SGuchun Chen } 95f89c6bf7SGuchun Chen } 96f89c6bf7SGuchun Chen 97f89c6bf7SGuchun Chen static void kfd_device_info_set_event_interrupt_class(struct kfd_dev *kfd) 98f89c6bf7SGuchun Chen { 99f89c6bf7SGuchun Chen uint32_t gc_version = KFD_GC_VERSION(kfd); 100f89c6bf7SGuchun Chen 101f89c6bf7SGuchun Chen switch (gc_version) { 102f89c6bf7SGuchun Chen case IP_VERSION(9, 0, 1): /* VEGA10 */ 103f89c6bf7SGuchun Chen case IP_VERSION(9, 1, 0): /* RAVEN */ 104f89c6bf7SGuchun Chen case IP_VERSION(9, 2, 1): /* VEGA12 */ 105f89c6bf7SGuchun Chen case IP_VERSION(9, 2, 2): /* RAVEN */ 106f89c6bf7SGuchun Chen case IP_VERSION(9, 3, 0): /* RENOIR */ 107f89c6bf7SGuchun Chen case IP_VERSION(9, 4, 0): /* VEGA20 */ 108f89c6bf7SGuchun Chen case IP_VERSION(9, 4, 1): /* ARCTURUS */ 109f89c6bf7SGuchun Chen case IP_VERSION(9, 4, 2): /* ALDEBARAN */ 110f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 1): /* VANGOGH */ 111f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 3): /* YELLOW_CARP */ 112f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 3): /* CYAN_SKILLFISH */ 113*f9ed188dSLang Yu case IP_VERSION(10, 1, 4): 114f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 10): /* NAVI10 */ 115f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 2): /* NAVI12 */ 116f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 1): /* NAVI14 */ 117f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 0): /* SIENNA_CICHLID */ 118f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 2): /* NAVY_FLOUNDER */ 119f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 4): /* DIMGREY_CAVEFISH */ 120f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 5): /* BEIGE_GOBY */ 121f89c6bf7SGuchun Chen kfd->device_info.event_interrupt_class = &event_interrupt_class_v9; 122f89c6bf7SGuchun Chen break; 123f89c6bf7SGuchun Chen default: 124f89c6bf7SGuchun Chen dev_warn(kfd_device, "v9 event interrupt handler is set due to " 125f89c6bf7SGuchun Chen "mismatch of gc ip block(GC_HWIP:0x%x).\n", gc_version); 126f89c6bf7SGuchun Chen kfd->device_info.event_interrupt_class = &event_interrupt_class_v9; 127f89c6bf7SGuchun Chen } 128f89c6bf7SGuchun Chen } 129f89c6bf7SGuchun Chen 130f0dc99a6SGraham Sider static void kfd_device_info_init(struct kfd_dev *kfd, 131f0dc99a6SGraham Sider bool vf, uint32_t gfx_target_version) 132f0dc99a6SGraham Sider { 133f0dc99a6SGraham Sider uint32_t gc_version = KFD_GC_VERSION(kfd); 134f0dc99a6SGraham Sider uint32_t asic_type = kfd->adev->asic_type; 135f0dc99a6SGraham Sider 136f0dc99a6SGraham Sider kfd->device_info.max_pasid_bits = 16; 137f0dc99a6SGraham Sider kfd->device_info.max_no_of_hqd = 24; 138f0dc99a6SGraham Sider kfd->device_info.num_of_watch_points = 4; 139f0dc99a6SGraham Sider kfd->device_info.mqd_size_aligned = MQD_SIZE_ALIGNED; 140f0dc99a6SGraham Sider kfd->device_info.gfx_target_version = gfx_target_version; 141f0dc99a6SGraham Sider 142f0dc99a6SGraham Sider if (KFD_IS_SOC15(kfd)) { 143f0dc99a6SGraham Sider kfd->device_info.doorbell_size = 8; 144f0dc99a6SGraham Sider kfd->device_info.ih_ring_entry_size = 8 * sizeof(uint32_t); 145f0dc99a6SGraham Sider kfd->device_info.supports_cwsr = true; 146f0dc99a6SGraham Sider 147f89c6bf7SGuchun Chen kfd_device_info_set_sdma_queue_num(kfd); 148f89c6bf7SGuchun Chen 149f89c6bf7SGuchun Chen kfd_device_info_set_event_interrupt_class(kfd); 150f0dc99a6SGraham Sider 151f0dc99a6SGraham Sider /* Raven */ 152f0dc99a6SGraham Sider if (gc_version == IP_VERSION(9, 1, 0) || 153f0dc99a6SGraham Sider gc_version == IP_VERSION(9, 2, 2)) 154f0dc99a6SGraham Sider kfd->device_info.needs_iommu_device = true; 155f0dc99a6SGraham Sider 156f0dc99a6SGraham Sider if (gc_version < IP_VERSION(11, 0, 0)) { 157f0dc99a6SGraham Sider /* Navi2x+, Navi1x+ */ 158f0dc99a6SGraham Sider if (gc_version >= IP_VERSION(10, 3, 0)) 159f0dc99a6SGraham Sider kfd->device_info.no_atomic_fw_version = 92; 16027cc310fSchen gong else if (gc_version >= IP_VERSION(10, 1, 1)) 16127cc310fSchen gong kfd->device_info.no_atomic_fw_version = 145; 162f0dc99a6SGraham Sider 163f0dc99a6SGraham Sider /* Navi1x+ */ 164f0dc99a6SGraham Sider if (gc_version >= IP_VERSION(10, 1, 1)) 165f0dc99a6SGraham Sider kfd->device_info.needs_pci_atomics = true; 166f0dc99a6SGraham Sider } 167f0dc99a6SGraham Sider } else { 168f0dc99a6SGraham Sider kfd->device_info.doorbell_size = 4; 169f0dc99a6SGraham Sider kfd->device_info.ih_ring_entry_size = 4 * sizeof(uint32_t); 170f0dc99a6SGraham Sider kfd->device_info.event_interrupt_class = &event_interrupt_class_cik; 171f0dc99a6SGraham Sider kfd->device_info.num_sdma_queues_per_engine = 2; 172f0dc99a6SGraham Sider 173f0dc99a6SGraham Sider if (asic_type != CHIP_KAVERI && 174f0dc99a6SGraham Sider asic_type != CHIP_HAWAII && 175f0dc99a6SGraham Sider asic_type != CHIP_TONGA) 176f0dc99a6SGraham Sider kfd->device_info.supports_cwsr = true; 177f0dc99a6SGraham Sider 178f0dc99a6SGraham Sider if (asic_type == CHIP_KAVERI || 179f0dc99a6SGraham Sider asic_type == CHIP_CARRIZO) 180f0dc99a6SGraham Sider kfd->device_info.needs_iommu_device = true; 181f0dc99a6SGraham Sider 182f0dc99a6SGraham Sider if (asic_type != CHIP_HAWAII && !vf) 183f0dc99a6SGraham Sider kfd->device_info.needs_pci_atomics = true; 184f0dc99a6SGraham Sider } 185f0dc99a6SGraham Sider } 186f0dc99a6SGraham Sider 187b5d1d755SGraham Sider struct kfd_dev *kgd2kfd_probe(struct amdgpu_device *adev, bool vf) 1884a488a7aSOded Gabbay { 189f0dc99a6SGraham Sider struct kfd_dev *kfd = NULL; 190f0dc99a6SGraham Sider const struct kfd2kgd_calls *f2g = NULL; 1915b983db8SAlex Deucher struct pci_dev *pdev = adev->pdev; 192f0dc99a6SGraham Sider uint32_t gfx_target_version = 0; 193050091abSYong Zhao 194c868d584SAlex Deucher switch (adev->asic_type) { 195c868d584SAlex Deucher #ifdef KFD_SUPPORT_IOMMU_V2 196c868d584SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK 197c868d584SAlex Deucher case CHIP_KAVERI: 198f0dc99a6SGraham Sider gfx_target_version = 70000; 199f0dc99a6SGraham Sider if (!vf) 200c868d584SAlex Deucher f2g = &gfx_v7_kfd2kgd; 201c868d584SAlex Deucher break; 202c868d584SAlex Deucher #endif 203c868d584SAlex Deucher case CHIP_CARRIZO: 204f0dc99a6SGraham Sider gfx_target_version = 80001; 205f0dc99a6SGraham Sider if (!vf) 206c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 207c868d584SAlex Deucher break; 208c868d584SAlex Deucher #endif 209c868d584SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK 210c868d584SAlex Deucher case CHIP_HAWAII: 211f0dc99a6SGraham Sider gfx_target_version = 70001; 2120f7ef0b9SFelix Kuehling if (!amdgpu_exp_hw_support) 2130f7ef0b9SFelix Kuehling pr_info( 2140f7ef0b9SFelix Kuehling "KFD support on Hawaii is experimental. See modparam exp_hw_support\n" 2150f7ef0b9SFelix Kuehling ); 2160f7ef0b9SFelix Kuehling else if (!vf) 217c868d584SAlex Deucher f2g = &gfx_v7_kfd2kgd; 218c868d584SAlex Deucher break; 219c868d584SAlex Deucher #endif 220c868d584SAlex Deucher case CHIP_TONGA: 221f0dc99a6SGraham Sider gfx_target_version = 80002; 222f0dc99a6SGraham Sider if (!vf) 223c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 224c868d584SAlex Deucher break; 225c868d584SAlex Deucher case CHIP_FIJI: 226f0dc99a6SGraham Sider gfx_target_version = 80003; 227c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 228c868d584SAlex Deucher break; 229c868d584SAlex Deucher case CHIP_POLARIS10: 230f0dc99a6SGraham Sider gfx_target_version = 80003; 231c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 232c868d584SAlex Deucher break; 233c868d584SAlex Deucher case CHIP_POLARIS11: 234f0dc99a6SGraham Sider gfx_target_version = 80003; 235f0dc99a6SGraham Sider if (!vf) 236c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 237c868d584SAlex Deucher break; 238c868d584SAlex Deucher case CHIP_POLARIS12: 239f0dc99a6SGraham Sider gfx_target_version = 80003; 240f0dc99a6SGraham Sider if (!vf) 241c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 242c868d584SAlex Deucher break; 243c868d584SAlex Deucher case CHIP_VEGAM: 244f0dc99a6SGraham Sider gfx_target_version = 80003; 245f0dc99a6SGraham Sider if (!vf) 246c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 247c868d584SAlex Deucher break; 248c868d584SAlex Deucher default: 249c868d584SAlex Deucher switch (adev->ip_versions[GC_HWIP][0]) { 2502c1f19b3SGraham Sider /* Vega 10 */ 251c868d584SAlex Deucher case IP_VERSION(9, 0, 1): 252f0dc99a6SGraham Sider gfx_target_version = 90000; 253c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd; 254c868d584SAlex Deucher break; 255c868d584SAlex Deucher #ifdef KFD_SUPPORT_IOMMU_V2 2562c1f19b3SGraham Sider /* Raven */ 257c868d584SAlex Deucher case IP_VERSION(9, 1, 0): 258c868d584SAlex Deucher case IP_VERSION(9, 2, 2): 259f0dc99a6SGraham Sider gfx_target_version = 90002; 260f0dc99a6SGraham Sider if (!vf) 261c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd; 262c868d584SAlex Deucher break; 263c868d584SAlex Deucher #endif 2642c1f19b3SGraham Sider /* Vega12 */ 265c868d584SAlex Deucher case IP_VERSION(9, 2, 1): 266f0dc99a6SGraham Sider gfx_target_version = 90004; 267f0dc99a6SGraham Sider if (!vf) 268c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd; 269c868d584SAlex Deucher break; 2702c1f19b3SGraham Sider /* Renoir */ 271c868d584SAlex Deucher case IP_VERSION(9, 3, 0): 272f0dc99a6SGraham Sider gfx_target_version = 90012; 273f0dc99a6SGraham Sider if (!vf) 274c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd; 275c868d584SAlex Deucher break; 2762c1f19b3SGraham Sider /* Vega20 */ 277c868d584SAlex Deucher case IP_VERSION(9, 4, 0): 278f0dc99a6SGraham Sider gfx_target_version = 90006; 279f0dc99a6SGraham Sider if (!vf) 280c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd; 281c868d584SAlex Deucher break; 2822c1f19b3SGraham Sider /* Arcturus */ 283c868d584SAlex Deucher case IP_VERSION(9, 4, 1): 284f0dc99a6SGraham Sider gfx_target_version = 90008; 285c868d584SAlex Deucher f2g = &arcturus_kfd2kgd; 286c868d584SAlex Deucher break; 2872c1f19b3SGraham Sider /* Aldebaran */ 288c868d584SAlex Deucher case IP_VERSION(9, 4, 2): 289f0dc99a6SGraham Sider gfx_target_version = 90010; 290c868d584SAlex Deucher f2g = &aldebaran_kfd2kgd; 291c868d584SAlex Deucher break; 2922c1f19b3SGraham Sider /* Navi10 */ 293c868d584SAlex Deucher case IP_VERSION(10, 1, 10): 294f0dc99a6SGraham Sider gfx_target_version = 100100; 295f0dc99a6SGraham Sider if (!vf) 296c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd; 297c868d584SAlex Deucher break; 2982c1f19b3SGraham Sider /* Navi12 */ 299c868d584SAlex Deucher case IP_VERSION(10, 1, 2): 300f0dc99a6SGraham Sider gfx_target_version = 100101; 301c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd; 302c868d584SAlex Deucher break; 3032c1f19b3SGraham Sider /* Navi14 */ 304c868d584SAlex Deucher case IP_VERSION(10, 1, 1): 305f0dc99a6SGraham Sider gfx_target_version = 100102; 306f0dc99a6SGraham Sider if (!vf) 307c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd; 308c868d584SAlex Deucher break; 3092c1f19b3SGraham Sider /* Cyan Skillfish */ 310c868d584SAlex Deucher case IP_VERSION(10, 1, 3): 311*f9ed188dSLang Yu case IP_VERSION(10, 1, 4): 312f0dc99a6SGraham Sider gfx_target_version = 100103; 313f0dc99a6SGraham Sider if (!vf) 314c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd; 315c868d584SAlex Deucher break; 3162c1f19b3SGraham Sider /* Sienna Cichlid */ 317c868d584SAlex Deucher case IP_VERSION(10, 3, 0): 318f0dc99a6SGraham Sider gfx_target_version = 100300; 319c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 320c868d584SAlex Deucher break; 3212c1f19b3SGraham Sider /* Navy Flounder */ 322c868d584SAlex Deucher case IP_VERSION(10, 3, 2): 323f0dc99a6SGraham Sider gfx_target_version = 100301; 324c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 325c868d584SAlex Deucher break; 3262c1f19b3SGraham Sider /* Van Gogh */ 327c868d584SAlex Deucher case IP_VERSION(10, 3, 1): 328f0dc99a6SGraham Sider gfx_target_version = 100303; 329f0dc99a6SGraham Sider if (!vf) 330c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 331c868d584SAlex Deucher break; 3322c1f19b3SGraham Sider /* Dimgrey Cavefish */ 333c868d584SAlex Deucher case IP_VERSION(10, 3, 4): 334f0dc99a6SGraham Sider gfx_target_version = 100302; 335c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 336c868d584SAlex Deucher break; 3372c1f19b3SGraham Sider /* Beige Goby */ 338c868d584SAlex Deucher case IP_VERSION(10, 3, 5): 339f0dc99a6SGraham Sider gfx_target_version = 100304; 340c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 341c868d584SAlex Deucher break; 3422c1f19b3SGraham Sider /* Yellow Carp */ 343c868d584SAlex Deucher case IP_VERSION(10, 3, 3): 344f0dc99a6SGraham Sider gfx_target_version = 100305; 345f0dc99a6SGraham Sider if (!vf) 346c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 347c868d584SAlex Deucher break; 348c868d584SAlex Deucher default: 349f0dc99a6SGraham Sider break; 350050091abSYong Zhao } 351c868d584SAlex Deucher break; 352c868d584SAlex Deucher } 3534a488a7aSOded Gabbay 354f0dc99a6SGraham Sider if (!f2g) { 355e4804a39SGraham Sider if (adev->ip_versions[GC_HWIP][0]) 356e4804a39SGraham Sider dev_err(kfd_device, "GC IP %06x %s not supported in kfd\n", 357e4804a39SGraham Sider adev->ip_versions[GC_HWIP][0], vf ? "VF" : ""); 358e4804a39SGraham Sider else 359050091abSYong Zhao dev_err(kfd_device, "%s %s not supported in kfd\n", 360c868d584SAlex Deucher amdgpu_asic_name[adev->asic_type], vf ? "VF" : ""); 3614a488a7aSOded Gabbay return NULL; 3624ebc7182SYong Zhao } 3634a488a7aSOded Gabbay 364d35f00d8SEric Huang kfd = kzalloc(sizeof(*kfd), GFP_KERNEL); 365d35f00d8SEric Huang if (!kfd) 366d35f00d8SEric Huang return NULL; 367d35f00d8SEric Huang 368c6c57446SGraham Sider kfd->adev = adev; 369f0dc99a6SGraham Sider kfd_device_info_init(kfd, vf, gfx_target_version); 3704a488a7aSOded Gabbay kfd->pdev = pdev; 37119f6d2a6SOded Gabbay kfd->init_complete = false; 372cea405b1SXihan Zhang kfd->kfd2kgd = f2g; 37343d8107fSHarish Kasiviswanathan atomic_set(&kfd->compute_profile, 0); 374cea405b1SXihan Zhang 375cea405b1SXihan Zhang mutex_init(&kfd->doorbell_mutex); 376cea405b1SXihan Zhang memset(&kfd->doorbell_available_index, 0, 377cea405b1SXihan Zhang sizeof(kfd->doorbell_available_index)); 3784a488a7aSOded Gabbay 3799b54d201SEric Huang atomic_set(&kfd->sram_ecc_flag, 0); 3809b54d201SEric Huang 38159d7115dSMukul Joshi ida_init(&kfd->doorbell_ida); 38259d7115dSMukul Joshi 3834a488a7aSOded Gabbay return kfd; 3844a488a7aSOded Gabbay } 3854a488a7aSOded Gabbay 386373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd) 387373d7080SFelix Kuehling { 388f0dc99a6SGraham Sider if (cwsr_enable && kfd->device_info.supports_cwsr) { 389046e674bSGraham Sider if (KFD_GC_VERSION(kfd) < IP_VERSION(9, 0, 1)) { 390373d7080SFelix Kuehling BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE); 391373d7080SFelix Kuehling kfd->cwsr_isa = cwsr_trap_gfx8_hex; 392373d7080SFelix Kuehling kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex); 393046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)) { 3943baa24f0SOak Zeng BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE); 3953baa24f0SOak Zeng kfd->cwsr_isa = cwsr_trap_arcturus_hex; 3963baa24f0SOak Zeng kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex); 397046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)) { 3980ef6845cSJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex) > PAGE_SIZE); 3990ef6845cSJay Cornwall kfd->cwsr_isa = cwsr_trap_aldebaran_hex; 4000ef6845cSJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex); 401046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 1, 1)) { 4023e76c239SFelix Kuehling BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE); 4033e76c239SFelix Kuehling kfd->cwsr_isa = cwsr_trap_gfx9_hex; 4043e76c239SFelix Kuehling kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex); 405046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 3, 0)) { 40680b6cfedSJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE); 40780b6cfedSJay Cornwall kfd->cwsr_isa = cwsr_trap_nv1x_hex; 40880b6cfedSJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex); 40914328aa5SPhilip Cox } else { 41014328aa5SPhilip Cox BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE); 41114328aa5SPhilip Cox kfd->cwsr_isa = cwsr_trap_gfx10_hex; 41214328aa5SPhilip Cox kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex); 4133e76c239SFelix Kuehling } 4143e76c239SFelix Kuehling 415373d7080SFelix Kuehling kfd->cwsr_enabled = true; 416373d7080SFelix Kuehling } 417373d7080SFelix Kuehling } 418373d7080SFelix Kuehling 41929633d0eSJoseph Greathouse static int kfd_gws_init(struct kfd_dev *kfd) 42029633d0eSJoseph Greathouse { 42129633d0eSJoseph Greathouse int ret = 0; 42229633d0eSJoseph Greathouse 42329633d0eSJoseph Greathouse if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) 42429633d0eSJoseph Greathouse return 0; 42529633d0eSJoseph Greathouse 426046e674bSGraham Sider if (hws_gws_support || (KFD_IS_SOC15(kfd) && 427046e674bSGraham Sider ((KFD_GC_VERSION(kfd) == IP_VERSION(9, 0, 1) 428046e674bSGraham Sider && kfd->mec2_fw_version >= 0x81b3) || 429046e674bSGraham Sider (KFD_GC_VERSION(kfd) <= IP_VERSION(9, 4, 0) 430046e674bSGraham Sider && kfd->mec2_fw_version >= 0x1b3) || 431046e674bSGraham Sider (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1) 432046e674bSGraham Sider && kfd->mec2_fw_version >= 0x30) || 433046e674bSGraham Sider (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2) 434addaac0cSJonathan Kim && kfd->mec2_fw_version >= 0x28)))) 4356bfc7c7eSGraham Sider ret = amdgpu_amdkfd_alloc_gws(kfd->adev, 43602274fc0SGraham Sider kfd->adev->gds.gws_size, &kfd->gws); 43729633d0eSJoseph Greathouse 43829633d0eSJoseph Greathouse return ret; 43929633d0eSJoseph Greathouse } 44029633d0eSJoseph Greathouse 441938a0650SAmber Lin static void kfd_smi_init(struct kfd_dev *dev) { 442938a0650SAmber Lin INIT_LIST_HEAD(&dev->smi_clients); 443938a0650SAmber Lin spin_lock_init(&dev->smi_lock); 444938a0650SAmber Lin } 445938a0650SAmber Lin 4464a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd, 4473a0c3423SHarish Kasiviswanathan struct drm_device *ddev, 4484a488a7aSOded Gabbay const struct kgd2kfd_shared_resources *gpu_resources) 4494a488a7aSOded Gabbay { 450fd6a440eSJonathan Kim unsigned int size, map_process_packet_size; 45119f6d2a6SOded Gabbay 4523a0c3423SHarish Kasiviswanathan kfd->ddev = ddev; 453574c4183SGraham Sider kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev, 4545ade6c9cSFelix Kuehling KGD_ENGINE_MEC1); 455574c4183SGraham Sider kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev, 45629633d0eSJoseph Greathouse KGD_ENGINE_MEC2); 457574c4183SGraham Sider kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev, 4585ade6c9cSFelix Kuehling KGD_ENGINE_SDMA1); 4594a488a7aSOded Gabbay kfd->shared_resources = *gpu_resources; 4604a488a7aSOded Gabbay 46144008d7aSYong Zhao kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1; 46244008d7aSYong Zhao kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1; 46344008d7aSYong Zhao kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd 46444008d7aSYong Zhao - kfd->vm_info.first_vmid_kfd + 1; 46544008d7aSYong Zhao 466e312af6cSFelix Kuehling /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps. 467e312af6cSFelix Kuehling * 32 and 64-bit requests are possible and must be 468e312af6cSFelix Kuehling * supported. 469e312af6cSFelix Kuehling */ 4706bfc7c7eSGraham Sider kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kfd->adev); 471e312af6cSFelix Kuehling if (!kfd->pci_atomic_requested && 472f0dc99a6SGraham Sider kfd->device_info.needs_pci_atomics && 473f0dc99a6SGraham Sider (!kfd->device_info.no_atomic_fw_version || 474f0dc99a6SGraham Sider kfd->mec_fw_version < kfd->device_info.no_atomic_fw_version)) { 475e312af6cSFelix Kuehling dev_info(kfd_device, 476e312af6cSFelix Kuehling "skipped device %x:%x, PCI rejects atomics %d<%d\n", 477e312af6cSFelix Kuehling kfd->pdev->vendor, kfd->pdev->device, 478e312af6cSFelix Kuehling kfd->mec_fw_version, 479f0dc99a6SGraham Sider kfd->device_info.no_atomic_fw_version); 480e312af6cSFelix Kuehling return false; 481e312af6cSFelix Kuehling } 482e312af6cSFelix Kuehling 483a99c6d4fSFelix Kuehling /* Verify module parameters regarding mapped process number*/ 484a99c6d4fSFelix Kuehling if ((hws_max_conc_proc < 0) 485a99c6d4fSFelix Kuehling || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) { 486a99c6d4fSFelix Kuehling dev_err(kfd_device, 487a99c6d4fSFelix Kuehling "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n", 488a99c6d4fSFelix Kuehling hws_max_conc_proc, kfd->vm_info.vmid_num_kfd, 489a99c6d4fSFelix Kuehling kfd->vm_info.vmid_num_kfd); 490a99c6d4fSFelix Kuehling kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd; 491a99c6d4fSFelix Kuehling } else 492a99c6d4fSFelix Kuehling kfd->max_proc_per_quantum = hws_max_conc_proc; 493a99c6d4fSFelix Kuehling 49419f6d2a6SOded Gabbay /* calculate max size of mqds needed for queues */ 495b8cbab04SOded Gabbay size = max_num_of_queues_per_device * 496f0dc99a6SGraham Sider kfd->device_info.mqd_size_aligned; 49719f6d2a6SOded Gabbay 498e18e794eSOded Gabbay /* 499e18e794eSOded Gabbay * calculate max size of runlist packet. 500e18e794eSOded Gabbay * There can be only 2 packets at once 501e18e794eSOded Gabbay */ 502046e674bSGraham Sider map_process_packet_size = KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2) ? 503fd6a440eSJonathan Kim sizeof(struct pm4_mes_map_process_aldebaran) : 504fd6a440eSJonathan Kim sizeof(struct pm4_mes_map_process); 505fd6a440eSJonathan Kim size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size + 506507968ddSFelix Kuehling max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues) 507507968ddSFelix Kuehling + sizeof(struct pm4_mes_runlist)) * 2; 508e18e794eSOded Gabbay 509e18e794eSOded Gabbay /* Add size of HIQ & DIQ */ 510e18e794eSOded Gabbay size += KFD_KERNEL_QUEUE_SIZE * 2; 511e18e794eSOded Gabbay 512e18e794eSOded Gabbay /* add another 512KB for all other allocations on gart (HPD, fences) */ 51319f6d2a6SOded Gabbay size += 512 * 1024; 51419f6d2a6SOded Gabbay 5157cd52c91SAmber Lin if (amdgpu_amdkfd_alloc_gtt_mem( 5166bfc7c7eSGraham Sider kfd->adev, size, &kfd->gtt_mem, 51715426dbbSYong Zhao &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr, 51815426dbbSYong Zhao false)) { 51979775b62SKent Russell dev_err(kfd_device, "Could not allocate %d bytes\n", size); 520e09d4fc8SOak Zeng goto alloc_gtt_mem_failure; 52119f6d2a6SOded Gabbay } 52219f6d2a6SOded Gabbay 52379775b62SKent Russell dev_info(kfd_device, "Allocated %d bytes on gart\n", size); 524e18e794eSOded Gabbay 52573a1da0bSOded Gabbay /* Initialize GTT sa with 512 byte chunk size */ 52673a1da0bSOded Gabbay if (kfd_gtt_sa_init(kfd, size, 512) != 0) { 52779775b62SKent Russell dev_err(kfd_device, "Error initializing gtt sub-allocator\n"); 52873a1da0bSOded Gabbay goto kfd_gtt_sa_init_error; 52973a1da0bSOded Gabbay } 53073a1da0bSOded Gabbay 531735df2baSFelix Kuehling if (kfd_doorbell_init(kfd)) { 532735df2baSFelix Kuehling dev_err(kfd_device, 533735df2baSFelix Kuehling "Error initializing doorbell aperture\n"); 534735df2baSFelix Kuehling goto kfd_doorbell_error; 535735df2baSFelix Kuehling } 53619f6d2a6SOded Gabbay 53702274fc0SGraham Sider kfd->hive_id = kfd->adev->gmc.xgmi.hive_id; 5380c1690e3SShaoyun Liu 53902274fc0SGraham Sider kfd->noretry = kfd->adev->gmc.noretry; 5409b498efaSAlex Deucher 5412249d558SAndrew Lewycky if (kfd_interrupt_init(kfd)) { 54279775b62SKent Russell dev_err(kfd_device, "Error initializing interrupts\n"); 5432249d558SAndrew Lewycky goto kfd_interrupt_error; 5442249d558SAndrew Lewycky } 5452249d558SAndrew Lewycky 54664c7f8cfSBen Goz kfd->dqm = device_queue_manager_init(kfd); 54764c7f8cfSBen Goz if (!kfd->dqm) { 54879775b62SKent Russell dev_err(kfd_device, "Error initializing queue manager\n"); 54964c7f8cfSBen Goz goto device_queue_manager_error; 55064c7f8cfSBen Goz } 55164c7f8cfSBen Goz 55229633d0eSJoseph Greathouse /* If supported on this device, allocate global GWS that is shared 55329633d0eSJoseph Greathouse * by all KFD processes 55429633d0eSJoseph Greathouse */ 55529633d0eSJoseph Greathouse if (kfd_gws_init(kfd)) { 55629633d0eSJoseph Greathouse dev_err(kfd_device, "Could not allocate %d gws\n", 55702274fc0SGraham Sider kfd->adev->gds.gws_size); 55829633d0eSJoseph Greathouse goto gws_error; 55929633d0eSJoseph Greathouse } 56029633d0eSJoseph Greathouse 5616127896fSHuang Rui /* If CRAT is broken, won't set iommu enabled */ 5626127896fSHuang Rui kfd_double_confirm_iommu_support(kfd); 5636127896fSHuang Rui 56464d1c3a4SFelix Kuehling if (kfd_iommu_device_init(kfd)) { 5656f4b590aSYifan Zhang kfd->use_iommu_v2 = false; 56664d1c3a4SFelix Kuehling dev_err(kfd_device, "Error initializing iommuv2\n"); 56764d1c3a4SFelix Kuehling goto device_iommu_error; 56864c7f8cfSBen Goz } 56964c7f8cfSBen Goz 570373d7080SFelix Kuehling kfd_cwsr_init(kfd); 571373d7080SFelix Kuehling 57256c5977eSGraham Sider svm_migrate_init(kfd->adev); 573814ab993SPhilip Yang 574afd18180SYifan Zhang if(kgd2kfd_resume_iommu(kfd)) 575afd18180SYifan Zhang goto device_iommu_error; 576afd18180SYifan Zhang 577b8935a7cSYong Zhao if (kfd_resume(kfd)) 578b8935a7cSYong Zhao goto kfd_resume_error; 579b8935a7cSYong Zhao 580465ab9e0SOak Zeng if (kfd_topology_add_device(kfd)) { 581465ab9e0SOak Zeng dev_err(kfd_device, "Error adding device to topology\n"); 582465ab9e0SOak Zeng goto kfd_topology_add_device_error; 583465ab9e0SOak Zeng } 584465ab9e0SOak Zeng 585938a0650SAmber Lin kfd_smi_init(kfd); 586938a0650SAmber Lin 5874a488a7aSOded Gabbay kfd->init_complete = true; 58879775b62SKent Russell dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor, 5894a488a7aSOded Gabbay kfd->pdev->device); 5904a488a7aSOded Gabbay 59179775b62SKent Russell pr_debug("Starting kfd with the following scheduling policy %d\n", 592d146c5a7SFelix Kuehling kfd->dqm->sched_policy); 59364c7f8cfSBen Goz 59419f6d2a6SOded Gabbay goto out; 59519f6d2a6SOded Gabbay 596465ab9e0SOak Zeng kfd_topology_add_device_error: 597b8935a7cSYong Zhao kfd_resume_error: 59864d1c3a4SFelix Kuehling device_iommu_error: 59929633d0eSJoseph Greathouse gws_error: 60064c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 60164c7f8cfSBen Goz device_queue_manager_error: 6022249d558SAndrew Lewycky kfd_interrupt_exit(kfd); 6032249d558SAndrew Lewycky kfd_interrupt_error: 604735df2baSFelix Kuehling kfd_doorbell_fini(kfd); 605735df2baSFelix Kuehling kfd_doorbell_error: 60673a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd); 60773a1da0bSOded Gabbay kfd_gtt_sa_init_error: 6086bfc7c7eSGraham Sider amdgpu_amdkfd_free_gtt_mem(kfd->adev, kfd->gtt_mem); 609e09d4fc8SOak Zeng alloc_gtt_mem_failure: 61029633d0eSJoseph Greathouse if (kfd->gws) 6116bfc7c7eSGraham Sider amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws); 61219f6d2a6SOded Gabbay dev_err(kfd_device, 61379775b62SKent Russell "device %x:%x NOT added due to errors\n", 61419f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 61519f6d2a6SOded Gabbay out: 61619f6d2a6SOded Gabbay return kfd->init_complete; 6174a488a7aSOded Gabbay } 6184a488a7aSOded Gabbay 6194a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd) 6204a488a7aSOded Gabbay { 621b17f068aSOded Gabbay if (kfd->init_complete) { 62264c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 6232249d558SAndrew Lewycky kfd_interrupt_exit(kfd); 62419f6d2a6SOded Gabbay kfd_topology_remove_device(kfd); 625735df2baSFelix Kuehling kfd_doorbell_fini(kfd); 62659d7115dSMukul Joshi ida_destroy(&kfd->doorbell_ida); 62773a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd); 6286bfc7c7eSGraham Sider amdgpu_amdkfd_free_gtt_mem(kfd->adev, kfd->gtt_mem); 62929633d0eSJoseph Greathouse if (kfd->gws) 6306bfc7c7eSGraham Sider amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws); 631b17f068aSOded Gabbay } 6325b5c4e40SEvgeny Pinchuk 6334a488a7aSOded Gabbay kfree(kfd); 6344a488a7aSOded Gabbay } 6354a488a7aSOded Gabbay 636e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd) 637e3b7a967SShaoyun Liu { 638e42051d2SShaoyun Liu if (!kfd->init_complete) 639e42051d2SShaoyun Liu return 0; 64009c34e8dSFelix Kuehling 64155977744SMukul Joshi kfd_smi_event_update_gpu_reset(kfd, false); 64255977744SMukul Joshi 64309c34e8dSFelix Kuehling kfd->dqm->ops.pre_reset(kfd->dqm); 64409c34e8dSFelix Kuehling 6459593f4d6SRajneesh Bhardwaj kgd2kfd_suspend(kfd, false); 646e42051d2SShaoyun Liu 647e42051d2SShaoyun Liu kfd_signal_reset_event(kfd); 648e3b7a967SShaoyun Liu return 0; 649e3b7a967SShaoyun Liu } 650e3b7a967SShaoyun Liu 651e42051d2SShaoyun Liu /* 652e42051d2SShaoyun Liu * Fix me. KFD won't be able to resume existing process for now. 653e42051d2SShaoyun Liu * We will keep all existing process in a evicted state and 654e42051d2SShaoyun Liu * wait the process to be terminated. 655e42051d2SShaoyun Liu */ 656e42051d2SShaoyun Liu 657e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd) 658e3b7a967SShaoyun Liu { 659a1bd079fSyu kuai int ret; 660e42051d2SShaoyun Liu 661e42051d2SShaoyun Liu if (!kfd->init_complete) 662e3b7a967SShaoyun Liu return 0; 663e42051d2SShaoyun Liu 664e42051d2SShaoyun Liu ret = kfd_resume(kfd); 665e42051d2SShaoyun Liu if (ret) 666e42051d2SShaoyun Liu return ret; 667a1bd079fSyu kuai atomic_dec(&kfd_locked); 6689b54d201SEric Huang 6699b54d201SEric Huang atomic_set(&kfd->sram_ecc_flag, 0); 6709b54d201SEric Huang 67155977744SMukul Joshi kfd_smi_event_update_gpu_reset(kfd, true); 67255977744SMukul Joshi 673e42051d2SShaoyun Liu return 0; 674e42051d2SShaoyun Liu } 675e42051d2SShaoyun Liu 676e42051d2SShaoyun Liu bool kfd_is_locked(void) 677e42051d2SShaoyun Liu { 678e42051d2SShaoyun Liu return (atomic_read(&kfd_locked) > 0); 679e3b7a967SShaoyun Liu } 680e3b7a967SShaoyun Liu 6819593f4d6SRajneesh Bhardwaj void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm) 6824a488a7aSOded Gabbay { 683733fa1f7SYong Zhao if (!kfd->init_complete) 684733fa1f7SYong Zhao return; 685733fa1f7SYong Zhao 6869593f4d6SRajneesh Bhardwaj /* for runtime suspend, skip locking kfd */ 6879593f4d6SRajneesh Bhardwaj if (!run_pm) { 68826103436SFelix Kuehling /* For first KFD device suspend all the KFD processes */ 689e42051d2SShaoyun Liu if (atomic_inc_return(&kfd_locked) == 1) 69026103436SFelix Kuehling kfd_suspend_all_processes(); 6919593f4d6SRajneesh Bhardwaj } 69226103436SFelix Kuehling 69345c9a5e4SOded Gabbay kfd->dqm->ops.stop(kfd->dqm); 69464d1c3a4SFelix Kuehling kfd_iommu_suspend(kfd); 6954a488a7aSOded Gabbay } 6964a488a7aSOded Gabbay 6979593f4d6SRajneesh Bhardwaj int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm) 6984a488a7aSOded Gabbay { 69926103436SFelix Kuehling int ret, count; 70026103436SFelix Kuehling 701b8935a7cSYong Zhao if (!kfd->init_complete) 702b8935a7cSYong Zhao return 0; 703b17f068aSOded Gabbay 70426103436SFelix Kuehling ret = kfd_resume(kfd); 70526103436SFelix Kuehling if (ret) 70626103436SFelix Kuehling return ret; 707b17f068aSOded Gabbay 7089593f4d6SRajneesh Bhardwaj /* for runtime resume, skip unlocking kfd */ 7099593f4d6SRajneesh Bhardwaj if (!run_pm) { 710e42051d2SShaoyun Liu count = atomic_dec_return(&kfd_locked); 71126103436SFelix Kuehling WARN_ONCE(count < 0, "KFD suspend / resume ref. error"); 71226103436SFelix Kuehling if (count == 0) 71326103436SFelix Kuehling ret = kfd_resume_all_processes(); 7149593f4d6SRajneesh Bhardwaj } 71526103436SFelix Kuehling 71626103436SFelix Kuehling return ret; 7174ebc7182SYong Zhao } 7184ebc7182SYong Zhao 719f8846323SJames Zhu int kgd2kfd_resume_iommu(struct kfd_dev *kfd) 720b8935a7cSYong Zhao { 721b8935a7cSYong Zhao int err = 0; 722b8935a7cSYong Zhao 72364d1c3a4SFelix Kuehling err = kfd_iommu_resume(kfd); 724f8846323SJames Zhu if (err) 72564d1c3a4SFelix Kuehling dev_err(kfd_device, 72664d1c3a4SFelix Kuehling "Failed to resume IOMMU for device %x:%x\n", 72764d1c3a4SFelix Kuehling kfd->pdev->vendor, kfd->pdev->device); 72864d1c3a4SFelix Kuehling return err; 72964d1c3a4SFelix Kuehling } 730733fa1f7SYong Zhao 731f8846323SJames Zhu static int kfd_resume(struct kfd_dev *kfd) 732f8846323SJames Zhu { 733f8846323SJames Zhu int err = 0; 734f8846323SJames Zhu 735b8935a7cSYong Zhao err = kfd->dqm->ops.start(kfd->dqm); 736499f4d38SYifan Zhang if (err) 737b8935a7cSYong Zhao dev_err(kfd_device, 738b8935a7cSYong Zhao "Error starting queue manager for device %x:%x\n", 739b8935a7cSYong Zhao kfd->pdev->vendor, kfd->pdev->device); 740b17f068aSOded Gabbay 741b8935a7cSYong Zhao return err; 7424a488a7aSOded Gabbay } 7434a488a7aSOded Gabbay 744b3eca59dSPhilip Yang static inline void kfd_queue_work(struct workqueue_struct *wq, 745b3eca59dSPhilip Yang struct work_struct *work) 746b3eca59dSPhilip Yang { 747b3eca59dSPhilip Yang int cpu, new_cpu; 748b3eca59dSPhilip Yang 749b3eca59dSPhilip Yang cpu = new_cpu = smp_processor_id(); 750b3eca59dSPhilip Yang do { 751b3eca59dSPhilip Yang new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids; 752b3eca59dSPhilip Yang if (cpu_to_node(new_cpu) == numa_node_id()) 753b3eca59dSPhilip Yang break; 754b3eca59dSPhilip Yang } while (cpu != new_cpu); 755b3eca59dSPhilip Yang 756b3eca59dSPhilip Yang queue_work_on(new_cpu, wq, work); 757b3eca59dSPhilip Yang } 758b3eca59dSPhilip Yang 759b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */ 760b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry) 7614a488a7aSOded Gabbay { 76258e69886SLan Xiao uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE]; 76358e69886SLan Xiao bool is_patched = false; 7642383a767SChristian König unsigned long flags; 76558e69886SLan Xiao 7662249d558SAndrew Lewycky if (!kfd->init_complete) 7672249d558SAndrew Lewycky return; 7682249d558SAndrew Lewycky 769f0dc99a6SGraham Sider if (kfd->device_info.ih_ring_entry_size > sizeof(patched_ihre)) { 77058e69886SLan Xiao dev_err_once(kfd_device, "Ring entry too small\n"); 77158e69886SLan Xiao return; 77258e69886SLan Xiao } 77358e69886SLan Xiao 7742383a767SChristian König spin_lock_irqsave(&kfd->interrupt_lock, flags); 7752249d558SAndrew Lewycky 7762249d558SAndrew Lewycky if (kfd->interrupts_active 77758e69886SLan Xiao && interrupt_is_wanted(kfd, ih_ring_entry, 77858e69886SLan Xiao patched_ihre, &is_patched) 77958e69886SLan Xiao && enqueue_ih_ring_entry(kfd, 78058e69886SLan Xiao is_patched ? patched_ihre : ih_ring_entry)) 781b3eca59dSPhilip Yang kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work); 7822249d558SAndrew Lewycky 7832383a767SChristian König spin_unlock_irqrestore(&kfd->interrupt_lock, flags); 7844a488a7aSOded Gabbay } 7856e81090bSOded Gabbay 7866b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm) 7876b95e797SFelix Kuehling { 7886b95e797SFelix Kuehling struct kfd_process *p; 7896b95e797SFelix Kuehling int r; 7906b95e797SFelix Kuehling 7916b95e797SFelix Kuehling /* Because we are called from arbitrary context (workqueue) as opposed 7926b95e797SFelix Kuehling * to process context, kfd_process could attempt to exit while we are 7936b95e797SFelix Kuehling * running so the lookup function increments the process ref count. 7946b95e797SFelix Kuehling */ 7956b95e797SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 7966b95e797SFelix Kuehling if (!p) 7976b95e797SFelix Kuehling return -ESRCH; 7986b95e797SFelix Kuehling 799b2057956SFelix Kuehling WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid); 8006b95e797SFelix Kuehling r = kfd_process_evict_queues(p); 8016b95e797SFelix Kuehling 8026b95e797SFelix Kuehling kfd_unref_process(p); 8036b95e797SFelix Kuehling return r; 8046b95e797SFelix Kuehling } 8056b95e797SFelix Kuehling 8066b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm) 8076b95e797SFelix Kuehling { 8086b95e797SFelix Kuehling struct kfd_process *p; 8096b95e797SFelix Kuehling int r; 8106b95e797SFelix Kuehling 8116b95e797SFelix Kuehling /* Because we are called from arbitrary context (workqueue) as opposed 8126b95e797SFelix Kuehling * to process context, kfd_process could attempt to exit while we are 8136b95e797SFelix Kuehling * running so the lookup function increments the process ref count. 8146b95e797SFelix Kuehling */ 8156b95e797SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 8166b95e797SFelix Kuehling if (!p) 8176b95e797SFelix Kuehling return -ESRCH; 8186b95e797SFelix Kuehling 8196b95e797SFelix Kuehling r = kfd_process_restore_queues(p); 8206b95e797SFelix Kuehling 8216b95e797SFelix Kuehling kfd_unref_process(p); 8226b95e797SFelix Kuehling return r; 8236b95e797SFelix Kuehling } 8246b95e797SFelix Kuehling 82526103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will 82626103436SFelix Kuehling * prepare for safe eviction of KFD BOs that belong to the specified 82726103436SFelix Kuehling * process. 82826103436SFelix Kuehling * 82926103436SFelix Kuehling * @mm: mm_struct that identifies the specified KFD process 83026103436SFelix Kuehling * @fence: eviction fence attached to KFD process BOs 83126103436SFelix Kuehling * 83226103436SFelix Kuehling */ 83326103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm, 83426103436SFelix Kuehling struct dma_fence *fence) 83526103436SFelix Kuehling { 83626103436SFelix Kuehling struct kfd_process *p; 83726103436SFelix Kuehling unsigned long active_time; 83826103436SFelix Kuehling unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS); 83926103436SFelix Kuehling 84026103436SFelix Kuehling if (!fence) 84126103436SFelix Kuehling return -EINVAL; 84226103436SFelix Kuehling 84326103436SFelix Kuehling if (dma_fence_is_signaled(fence)) 84426103436SFelix Kuehling return 0; 84526103436SFelix Kuehling 84626103436SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 84726103436SFelix Kuehling if (!p) 84826103436SFelix Kuehling return -ENODEV; 84926103436SFelix Kuehling 85026103436SFelix Kuehling if (fence->seqno == p->last_eviction_seqno) 85126103436SFelix Kuehling goto out; 85226103436SFelix Kuehling 85326103436SFelix Kuehling p->last_eviction_seqno = fence->seqno; 85426103436SFelix Kuehling 85526103436SFelix Kuehling /* Avoid KFD process starvation. Wait for at least 85626103436SFelix Kuehling * PROCESS_ACTIVE_TIME_MS before evicting the process again 85726103436SFelix Kuehling */ 85826103436SFelix Kuehling active_time = get_jiffies_64() - p->last_restore_timestamp; 85926103436SFelix Kuehling if (delay_jiffies > active_time) 86026103436SFelix Kuehling delay_jiffies -= active_time; 86126103436SFelix Kuehling else 86226103436SFelix Kuehling delay_jiffies = 0; 86326103436SFelix Kuehling 86426103436SFelix Kuehling /* During process initialization eviction_work.dwork is initialized 86526103436SFelix Kuehling * to kfd_evict_bo_worker 86626103436SFelix Kuehling */ 867b2057956SFelix Kuehling WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies", 868b2057956SFelix Kuehling p->lead_thread->pid, delay_jiffies); 86926103436SFelix Kuehling schedule_delayed_work(&p->eviction_work, delay_jiffies); 87026103436SFelix Kuehling out: 87126103436SFelix Kuehling kfd_unref_process(p); 87226103436SFelix Kuehling return 0; 87326103436SFelix Kuehling } 87426103436SFelix Kuehling 8756e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 8766e81090bSOded Gabbay unsigned int chunk_size) 8776e81090bSOded Gabbay { 8788625ff9cSFelix Kuehling unsigned int num_of_longs; 8796e81090bSOded Gabbay 88032fa8219SFelix Kuehling if (WARN_ON(buf_size < chunk_size)) 88132fa8219SFelix Kuehling return -EINVAL; 88232fa8219SFelix Kuehling if (WARN_ON(buf_size == 0)) 88332fa8219SFelix Kuehling return -EINVAL; 88432fa8219SFelix Kuehling if (WARN_ON(chunk_size == 0)) 88532fa8219SFelix Kuehling return -EINVAL; 8866e81090bSOded Gabbay 8876e81090bSOded Gabbay kfd->gtt_sa_chunk_size = chunk_size; 8886e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks = buf_size / chunk_size; 8896e81090bSOded Gabbay 8908625ff9cSFelix Kuehling num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) / 8918625ff9cSFelix Kuehling BITS_PER_LONG; 8926e81090bSOded Gabbay 8938625ff9cSFelix Kuehling kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL); 8946e81090bSOded Gabbay 8956e81090bSOded Gabbay if (!kfd->gtt_sa_bitmap) 8966e81090bSOded Gabbay return -ENOMEM; 8976e81090bSOded Gabbay 89879775b62SKent Russell pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n", 8996e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap); 9006e81090bSOded Gabbay 9016e81090bSOded Gabbay mutex_init(&kfd->gtt_sa_lock); 9026e81090bSOded Gabbay 9036e81090bSOded Gabbay return 0; 9046e81090bSOded Gabbay 9056e81090bSOded Gabbay } 9066e81090bSOded Gabbay 9076e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd) 9086e81090bSOded Gabbay { 9096e81090bSOded Gabbay mutex_destroy(&kfd->gtt_sa_lock); 9106e81090bSOded Gabbay kfree(kfd->gtt_sa_bitmap); 9116e81090bSOded Gabbay } 9126e81090bSOded Gabbay 9136e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr, 9146e81090bSOded Gabbay unsigned int bit_num, 9156e81090bSOded Gabbay unsigned int chunk_size) 9166e81090bSOded Gabbay { 9176e81090bSOded Gabbay return start_addr + bit_num * chunk_size; 9186e81090bSOded Gabbay } 9196e81090bSOded Gabbay 9206e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr, 9216e81090bSOded Gabbay unsigned int bit_num, 9226e81090bSOded Gabbay unsigned int chunk_size) 9236e81090bSOded Gabbay { 9246e81090bSOded Gabbay return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size); 9256e81090bSOded Gabbay } 9266e81090bSOded Gabbay 9276e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size, 9286e81090bSOded Gabbay struct kfd_mem_obj **mem_obj) 9296e81090bSOded Gabbay { 9306e81090bSOded Gabbay unsigned int found, start_search, cur_size; 9316e81090bSOded Gabbay 9326e81090bSOded Gabbay if (size == 0) 9336e81090bSOded Gabbay return -EINVAL; 9346e81090bSOded Gabbay 9356e81090bSOded Gabbay if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size) 9366e81090bSOded Gabbay return -ENOMEM; 9376e81090bSOded Gabbay 9381cd106ecSFelix Kuehling *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL); 9391cd106ecSFelix Kuehling if (!(*mem_obj)) 9406e81090bSOded Gabbay return -ENOMEM; 9416e81090bSOded Gabbay 94279775b62SKent Russell pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size); 9436e81090bSOded Gabbay 9446e81090bSOded Gabbay start_search = 0; 9456e81090bSOded Gabbay 9466e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock); 9476e81090bSOded Gabbay 9486e81090bSOded Gabbay kfd_gtt_restart_search: 9496e81090bSOded Gabbay /* Find the first chunk that is free */ 9506e81090bSOded Gabbay found = find_next_zero_bit(kfd->gtt_sa_bitmap, 9516e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, 9526e81090bSOded Gabbay start_search); 9536e81090bSOded Gabbay 95479775b62SKent Russell pr_debug("Found = %d\n", found); 9556e81090bSOded Gabbay 9566e81090bSOded Gabbay /* If there wasn't any free chunk, bail out */ 9576e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks) 9586e81090bSOded Gabbay goto kfd_gtt_no_free_chunk; 9596e81090bSOded Gabbay 9606e81090bSOded Gabbay /* Update fields of mem_obj */ 9616e81090bSOded Gabbay (*mem_obj)->range_start = found; 9626e81090bSOded Gabbay (*mem_obj)->range_end = found; 9636e81090bSOded Gabbay (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr( 9646e81090bSOded Gabbay kfd->gtt_start_gpu_addr, 9656e81090bSOded Gabbay found, 9666e81090bSOded Gabbay kfd->gtt_sa_chunk_size); 9676e81090bSOded Gabbay (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr( 9686e81090bSOded Gabbay kfd->gtt_start_cpu_ptr, 9696e81090bSOded Gabbay found, 9706e81090bSOded Gabbay kfd->gtt_sa_chunk_size); 9716e81090bSOded Gabbay 97279775b62SKent Russell pr_debug("gpu_addr = %p, cpu_addr = %p\n", 9736e81090bSOded Gabbay (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr); 9746e81090bSOded Gabbay 9756e81090bSOded Gabbay /* If we need only one chunk, mark it as allocated and get out */ 9766e81090bSOded Gabbay if (size <= kfd->gtt_sa_chunk_size) { 97779775b62SKent Russell pr_debug("Single bit\n"); 9786e81090bSOded Gabbay set_bit(found, kfd->gtt_sa_bitmap); 9796e81090bSOded Gabbay goto kfd_gtt_out; 9806e81090bSOded Gabbay } 9816e81090bSOded Gabbay 9826e81090bSOded Gabbay /* Otherwise, try to see if we have enough contiguous chunks */ 9836e81090bSOded Gabbay cur_size = size - kfd->gtt_sa_chunk_size; 9846e81090bSOded Gabbay do { 9856e81090bSOded Gabbay (*mem_obj)->range_end = 9866e81090bSOded Gabbay find_next_zero_bit(kfd->gtt_sa_bitmap, 9876e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, ++found); 9886e81090bSOded Gabbay /* 9896e81090bSOded Gabbay * If next free chunk is not contiguous than we need to 9906e81090bSOded Gabbay * restart our search from the last free chunk we found (which 9916e81090bSOded Gabbay * wasn't contiguous to the previous ones 9926e81090bSOded Gabbay */ 9936e81090bSOded Gabbay if ((*mem_obj)->range_end != found) { 9946e81090bSOded Gabbay start_search = found; 9956e81090bSOded Gabbay goto kfd_gtt_restart_search; 9966e81090bSOded Gabbay } 9976e81090bSOded Gabbay 9986e81090bSOded Gabbay /* 9996e81090bSOded Gabbay * If we reached end of buffer, bail out with error 10006e81090bSOded Gabbay */ 10016e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks) 10026e81090bSOded Gabbay goto kfd_gtt_no_free_chunk; 10036e81090bSOded Gabbay 10046e81090bSOded Gabbay /* Check if we don't need another chunk */ 10056e81090bSOded Gabbay if (cur_size <= kfd->gtt_sa_chunk_size) 10066e81090bSOded Gabbay cur_size = 0; 10076e81090bSOded Gabbay else 10086e81090bSOded Gabbay cur_size -= kfd->gtt_sa_chunk_size; 10096e81090bSOded Gabbay 10106e81090bSOded Gabbay } while (cur_size > 0); 10116e81090bSOded Gabbay 101279775b62SKent Russell pr_debug("range_start = %d, range_end = %d\n", 10136e81090bSOded Gabbay (*mem_obj)->range_start, (*mem_obj)->range_end); 10146e81090bSOded Gabbay 10156e81090bSOded Gabbay /* Mark the chunks as allocated */ 10166e81090bSOded Gabbay for (found = (*mem_obj)->range_start; 10176e81090bSOded Gabbay found <= (*mem_obj)->range_end; 10186e81090bSOded Gabbay found++) 10196e81090bSOded Gabbay set_bit(found, kfd->gtt_sa_bitmap); 10206e81090bSOded Gabbay 10216e81090bSOded Gabbay kfd_gtt_out: 10226e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 10236e81090bSOded Gabbay return 0; 10246e81090bSOded Gabbay 10256e81090bSOded Gabbay kfd_gtt_no_free_chunk: 10263148a6a0SJack Zhang pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj); 10276e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 10283148a6a0SJack Zhang kfree(*mem_obj); 10296e81090bSOded Gabbay return -ENOMEM; 10306e81090bSOded Gabbay } 10316e81090bSOded Gabbay 10326e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj) 10336e81090bSOded Gabbay { 10346e81090bSOded Gabbay unsigned int bit; 10356e81090bSOded Gabbay 10369216ed29SOded Gabbay /* Act like kfree when trying to free a NULL object */ 10379216ed29SOded Gabbay if (!mem_obj) 10389216ed29SOded Gabbay return 0; 10396e81090bSOded Gabbay 104079775b62SKent Russell pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n", 10416e81090bSOded Gabbay mem_obj, mem_obj->range_start, mem_obj->range_end); 10426e81090bSOded Gabbay 10436e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock); 10446e81090bSOded Gabbay 10456e81090bSOded Gabbay /* Mark the chunks as free */ 10466e81090bSOded Gabbay for (bit = mem_obj->range_start; 10476e81090bSOded Gabbay bit <= mem_obj->range_end; 10486e81090bSOded Gabbay bit++) 10496e81090bSOded Gabbay clear_bit(bit, kfd->gtt_sa_bitmap); 10506e81090bSOded Gabbay 10516e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 10526e81090bSOded Gabbay 10536e81090bSOded Gabbay kfree(mem_obj); 10546e81090bSOded Gabbay return 0; 10556e81090bSOded Gabbay } 1056a29ec470SShaoyun Liu 10579b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd) 10589b54d201SEric Huang { 10599b54d201SEric Huang if (kfd) 10609b54d201SEric Huang atomic_inc(&kfd->sram_ecc_flag); 10619b54d201SEric Huang } 10629b54d201SEric Huang 106343d8107fSHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd) 106443d8107fSHarish Kasiviswanathan { 106543d8107fSHarish Kasiviswanathan if (atomic_inc_return(&kfd->compute_profile) == 1) 10666bfc7c7eSGraham Sider amdgpu_amdkfd_set_compute_idle(kfd->adev, false); 106743d8107fSHarish Kasiviswanathan } 106843d8107fSHarish Kasiviswanathan 106943d8107fSHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd) 107043d8107fSHarish Kasiviswanathan { 107143d8107fSHarish Kasiviswanathan int count = atomic_dec_return(&kfd->compute_profile); 107243d8107fSHarish Kasiviswanathan 107343d8107fSHarish Kasiviswanathan if (count == 0) 10746bfc7c7eSGraham Sider amdgpu_amdkfd_set_compute_idle(kfd->adev, true); 107543d8107fSHarish Kasiviswanathan WARN_ONCE(count < 0, "Compute profile ref. count error"); 107643d8107fSHarish Kasiviswanathan } 107743d8107fSHarish Kasiviswanathan 1078410e302eSGraham Sider void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask) 10792c2b0d88SMukul Joshi { 1080158fc08dSAmber Lin if (kfd && kfd->init_complete) 10812c2b0d88SMukul Joshi kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask); 10822c2b0d88SMukul Joshi } 10832c2b0d88SMukul Joshi 1084ee2f17f4SAmber Lin /* kfd_get_num_sdma_engines returns the number of PCIe optimized SDMA and 1085ee2f17f4SAmber Lin * kfd_get_num_xgmi_sdma_engines returns the number of XGMI SDMA. 1086ee2f17f4SAmber Lin * When the device has more than two engines, we reserve two for PCIe to enable 1087ee2f17f4SAmber Lin * full-duplex and the rest are used as XGMI. 1088ee2f17f4SAmber Lin */ 1089ee2f17f4SAmber Lin unsigned int kfd_get_num_sdma_engines(struct kfd_dev *kdev) 1090ee2f17f4SAmber Lin { 1091ee2f17f4SAmber Lin /* If XGMI is not supported, all SDMA engines are PCIe */ 1092ee2f17f4SAmber Lin if (!kdev->adev->gmc.xgmi.supported) 1093ee2f17f4SAmber Lin return kdev->adev->sdma.num_instances; 1094ee2f17f4SAmber Lin 1095ee2f17f4SAmber Lin return min(kdev->adev->sdma.num_instances, 2); 1096ee2f17f4SAmber Lin } 1097ee2f17f4SAmber Lin 1098ee2f17f4SAmber Lin unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_dev *kdev) 1099ee2f17f4SAmber Lin { 1100ee2f17f4SAmber Lin /* After reserved for PCIe, the rest of engines are XGMI */ 1101ee2f17f4SAmber Lin return kdev->adev->sdma.num_instances - kfd_get_num_sdma_engines(kdev); 1102ee2f17f4SAmber Lin } 1103ee2f17f4SAmber Lin 1104a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS) 1105a29ec470SShaoyun Liu 1106a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS 1107a29ec470SShaoyun Liu * which will trigger a GPU reset and bring the HWS back to normal state 1108a29ec470SShaoyun Liu */ 1109a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev) 1110a29ec470SShaoyun Liu { 1111a29ec470SShaoyun Liu if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) { 1112a29ec470SShaoyun Liu pr_err("HWS is not enabled"); 1113a29ec470SShaoyun Liu return -EINVAL; 1114a29ec470SShaoyun Liu } 1115a29ec470SShaoyun Liu 11164f942aaeSOak Zeng return dqm_debugfs_hang_hws(dev->dqm); 1117a29ec470SShaoyun Liu } 1118a29ec470SShaoyun Liu 1119a29ec470SShaoyun Liu #endif 1120