1d87f36a0SRajneesh Bhardwaj // SPDX-License-Identifier: GPL-2.0 OR MIT 24a488a7aSOded Gabbay /* 3d87f36a0SRajneesh Bhardwaj * Copyright 2014-2022 Advanced Micro Devices, Inc. 44a488a7aSOded Gabbay * 54a488a7aSOded Gabbay * Permission is hereby granted, free of charge, to any person obtaining a 64a488a7aSOded Gabbay * copy of this software and associated documentation files (the "Software"), 74a488a7aSOded Gabbay * to deal in the Software without restriction, including without limitation 84a488a7aSOded Gabbay * the rights to use, copy, modify, merge, publish, distribute, sublicense, 94a488a7aSOded Gabbay * and/or sell copies of the Software, and to permit persons to whom the 104a488a7aSOded Gabbay * Software is furnished to do so, subject to the following conditions: 114a488a7aSOded Gabbay * 124a488a7aSOded Gabbay * The above copyright notice and this permission notice shall be included in 134a488a7aSOded Gabbay * all copies or substantial portions of the Software. 144a488a7aSOded Gabbay * 154a488a7aSOded Gabbay * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 164a488a7aSOded Gabbay * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 174a488a7aSOded Gabbay * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 184a488a7aSOded Gabbay * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 194a488a7aSOded Gabbay * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 204a488a7aSOded Gabbay * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 214a488a7aSOded Gabbay * OTHER DEALINGS IN THE SOFTWARE. 224a488a7aSOded Gabbay */ 234a488a7aSOded Gabbay 244a488a7aSOded Gabbay #include <linux/bsearch.h> 254a488a7aSOded Gabbay #include <linux/pci.h> 264a488a7aSOded Gabbay #include <linux/slab.h> 274a488a7aSOded Gabbay #include "kfd_priv.h" 2864c7f8cfSBen Goz #include "kfd_device_queue_manager.h" 29507968ddSFelix Kuehling #include "kfd_pm4_headers_vi.h" 30fd6a440eSJonathan Kim #include "kfd_pm4_headers_aldebaran.h" 310db54b24SYong Zhao #include "cwsr_trap_handler.h" 3264d1c3a4SFelix Kuehling #include "kfd_iommu.h" 335b87245fSAmber Lin #include "amdgpu_amdkfd.h" 342c2b0d88SMukul Joshi #include "kfd_smi_events.h" 35814ab993SPhilip Yang #include "kfd_migrate.h" 365b983db8SAlex Deucher #include "amdgpu.h" 374a488a7aSOded Gabbay 3819f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768 39e42051d2SShaoyun Liu 40e42051d2SShaoyun Liu /* 41e42051d2SShaoyun Liu * kfd_locked is used to lock the kfd driver during suspend or reset 42e42051d2SShaoyun Liu * once locked, kfd driver will stop any further GPU execution. 43e42051d2SShaoyun Liu * create process (open) will return -EAGAIN. 44e42051d2SShaoyun Liu */ 45e42051d2SShaoyun Liu static atomic_t kfd_locked = ATOMIC_INIT(0); 4619f6d2a6SOded Gabbay 47a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK 48e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v7_kfd2kgd; 49a3e520a2SAlex Deucher #endif 50e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v8_kfd2kgd; 51e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v9_kfd2kgd; 52e392c887SYong Zhao extern const struct kfd2kgd_calls arcturus_kfd2kgd; 535073506cSJonathan Kim extern const struct kfd2kgd_calls aldebaran_kfd2kgd; 54e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v10_kfd2kgd; 553a2f0c81SYong Zhao extern const struct kfd2kgd_calls gfx_v10_3_kfd2kgd; 56cc009e61SMukul Joshi extern const struct kfd2kgd_calls gfx_v11_kfd2kgd; 57e392c887SYong Zhao 586e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 596e81090bSOded Gabbay unsigned int chunk_size); 606e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd); 616e81090bSOded Gabbay 627ee938acSFelix Kuehling static int kfd_resume_iommu(struct kfd_dev *kfd); 63b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd); 64b8935a7cSYong Zhao 65cc009e61SMukul Joshi static void kfd_device_info_set_sdma_info(struct kfd_dev *kfd) 66f89c6bf7SGuchun Chen { 67f89c6bf7SGuchun Chen uint32_t sdma_version = kfd->adev->ip_versions[SDMA0_HWIP][0]; 68f89c6bf7SGuchun Chen 69f89c6bf7SGuchun Chen switch (sdma_version) { 70f89c6bf7SGuchun Chen case IP_VERSION(4, 0, 0):/* VEGA10 */ 71f89c6bf7SGuchun Chen case IP_VERSION(4, 0, 1):/* VEGA12 */ 72f89c6bf7SGuchun Chen case IP_VERSION(4, 1, 0):/* RAVEN */ 73f89c6bf7SGuchun Chen case IP_VERSION(4, 1, 1):/* RAVEN */ 745eb877b2SKent Russell case IP_VERSION(4, 1, 2):/* RENOIR */ 75f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 1):/* VANGOGH */ 76f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 3):/* YELLOW_CARP */ 777c4f4f19SMario Limonciello case IP_VERSION(5, 2, 6):/* GC 10.3.6 */ 787c4f4f19SMario Limonciello case IP_VERSION(5, 2, 7):/* GC 10.3.7 */ 79f89c6bf7SGuchun Chen kfd->device_info.num_sdma_queues_per_engine = 2; 80f89c6bf7SGuchun Chen break; 81f89c6bf7SGuchun Chen case IP_VERSION(4, 2, 0):/* VEGA20 */ 825eb877b2SKent Russell case IP_VERSION(4, 2, 2):/* ARCTURUS */ 83f89c6bf7SGuchun Chen case IP_VERSION(4, 4, 0):/* ALDEBARAN */ 84f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 0):/* NAVI10 */ 85f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 1):/* CYAN_SKILLFISH */ 86f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 2):/* NAVI14 */ 87f89c6bf7SGuchun Chen case IP_VERSION(5, 0, 5):/* NAVI12 */ 88f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 0):/* SIENNA_CICHLID */ 895eb877b2SKent Russell case IP_VERSION(5, 2, 2):/* NAVY_FLOUNDER */ 90f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 4):/* DIMGREY_CAVEFISH */ 91f89c6bf7SGuchun Chen case IP_VERSION(5, 2, 5):/* BEIGE_GOBY */ 92cc009e61SMukul Joshi case IP_VERSION(6, 0, 0): 93efb4fd10SYifan Zhang case IP_VERSION(6, 0, 1): 9422dd871eSEric Huang case IP_VERSION(6, 0, 2): 955ddb5fe9SDavid Belanger case IP_VERSION(6, 0, 3): 96f89c6bf7SGuchun Chen kfd->device_info.num_sdma_queues_per_engine = 8; 97f89c6bf7SGuchun Chen break; 98f89c6bf7SGuchun Chen default: 99f89c6bf7SGuchun Chen dev_warn(kfd_device, 10020c5e425SGraham Sider "Default sdma queue per engine(8) is set due to mismatch of sdma ip block(SDMA_HWIP:0x%x).\n", 101f89c6bf7SGuchun Chen sdma_version); 102f89c6bf7SGuchun Chen kfd->device_info.num_sdma_queues_per_engine = 8; 103f89c6bf7SGuchun Chen } 104cc009e61SMukul Joshi 105cc009e61SMukul Joshi switch (sdma_version) { 106cc009e61SMukul Joshi case IP_VERSION(6, 0, 0): 10722dd871eSEric Huang case IP_VERSION(6, 0, 2): 1085ddb5fe9SDavid Belanger case IP_VERSION(6, 0, 3): 109cc009e61SMukul Joshi /* Reserve 1 for paging and 1 for gfx */ 110cc009e61SMukul Joshi kfd->device_info.num_reserved_sdma_queues_per_engine = 2; 111cc009e61SMukul Joshi /* BIT(0)=engine-0 queue-0; BIT(1)=engine-1 queue-0; BIT(2)=engine-0 queue-1; ... */ 112cc009e61SMukul Joshi kfd->device_info.reserved_sdma_queues_bitmap = 0xFULL; 113cc009e61SMukul Joshi break; 114e48e6a13SYifan Zhang case IP_VERSION(6, 0, 1): 115e48e6a13SYifan Zhang /* Reserve 1 for paging and 1 for gfx */ 116e48e6a13SYifan Zhang kfd->device_info.num_reserved_sdma_queues_per_engine = 2; 117e48e6a13SYifan Zhang /* BIT(0)=engine-0 queue-0; BIT(1)=engine-0 queue-1; ... */ 118e48e6a13SYifan Zhang kfd->device_info.reserved_sdma_queues_bitmap = 0x3ULL; 119e48e6a13SYifan Zhang break; 120cc009e61SMukul Joshi default: 121cc009e61SMukul Joshi break; 122cc009e61SMukul Joshi } 123f89c6bf7SGuchun Chen } 124f89c6bf7SGuchun Chen 125f89c6bf7SGuchun Chen static void kfd_device_info_set_event_interrupt_class(struct kfd_dev *kfd) 126f89c6bf7SGuchun Chen { 127f89c6bf7SGuchun Chen uint32_t gc_version = KFD_GC_VERSION(kfd); 128f89c6bf7SGuchun Chen 129f89c6bf7SGuchun Chen switch (gc_version) { 130f89c6bf7SGuchun Chen case IP_VERSION(9, 0, 1): /* VEGA10 */ 131f89c6bf7SGuchun Chen case IP_VERSION(9, 1, 0): /* RAVEN */ 132f89c6bf7SGuchun Chen case IP_VERSION(9, 2, 1): /* VEGA12 */ 133f89c6bf7SGuchun Chen case IP_VERSION(9, 2, 2): /* RAVEN */ 134f89c6bf7SGuchun Chen case IP_VERSION(9, 3, 0): /* RENOIR */ 135f89c6bf7SGuchun Chen case IP_VERSION(9, 4, 0): /* VEGA20 */ 136f89c6bf7SGuchun Chen case IP_VERSION(9, 4, 1): /* ARCTURUS */ 137f89c6bf7SGuchun Chen case IP_VERSION(9, 4, 2): /* ALDEBARAN */ 138f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 1): /* VANGOGH */ 139f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 3): /* YELLOW_CARP */ 1407c4f4f19SMario Limonciello case IP_VERSION(10, 3, 6): /* GC 10.3.6 */ 1417c4f4f19SMario Limonciello case IP_VERSION(10, 3, 7): /* GC 10.3.7 */ 142f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 3): /* CYAN_SKILLFISH */ 143f9ed188dSLang Yu case IP_VERSION(10, 1, 4): 144f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 10): /* NAVI10 */ 145f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 2): /* NAVI12 */ 146f89c6bf7SGuchun Chen case IP_VERSION(10, 1, 1): /* NAVI14 */ 147f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 0): /* SIENNA_CICHLID */ 148f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 2): /* NAVY_FLOUNDER */ 149f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 4): /* DIMGREY_CAVEFISH */ 150f89c6bf7SGuchun Chen case IP_VERSION(10, 3, 5): /* BEIGE_GOBY */ 151f89c6bf7SGuchun Chen kfd->device_info.event_interrupt_class = &event_interrupt_class_v9; 152f89c6bf7SGuchun Chen break; 153cc009e61SMukul Joshi case IP_VERSION(11, 0, 0): 15426776a70SHuang Rui case IP_VERSION(11, 0, 1): 155ec661f1cSEric Huang case IP_VERSION(11, 0, 2): 1565ddb5fe9SDavid Belanger case IP_VERSION(11, 0, 3): 15788c21c2bSYifan Zhang case IP_VERSION(11, 0, 4): 158cc009e61SMukul Joshi kfd->device_info.event_interrupt_class = &event_interrupt_class_v11; 159cc009e61SMukul Joshi break; 160f89c6bf7SGuchun Chen default: 161f89c6bf7SGuchun Chen dev_warn(kfd_device, "v9 event interrupt handler is set due to " 162f89c6bf7SGuchun Chen "mismatch of gc ip block(GC_HWIP:0x%x).\n", gc_version); 163f89c6bf7SGuchun Chen kfd->device_info.event_interrupt_class = &event_interrupt_class_v9; 164f89c6bf7SGuchun Chen } 165f89c6bf7SGuchun Chen } 166f89c6bf7SGuchun Chen 167f0dc99a6SGraham Sider static void kfd_device_info_init(struct kfd_dev *kfd, 168f0dc99a6SGraham Sider bool vf, uint32_t gfx_target_version) 169f0dc99a6SGraham Sider { 170f0dc99a6SGraham Sider uint32_t gc_version = KFD_GC_VERSION(kfd); 171f0dc99a6SGraham Sider uint32_t asic_type = kfd->adev->asic_type; 172f0dc99a6SGraham Sider 173f0dc99a6SGraham Sider kfd->device_info.max_pasid_bits = 16; 174f0dc99a6SGraham Sider kfd->device_info.max_no_of_hqd = 24; 175f0dc99a6SGraham Sider kfd->device_info.num_of_watch_points = 4; 176f0dc99a6SGraham Sider kfd->device_info.mqd_size_aligned = MQD_SIZE_ALIGNED; 177f0dc99a6SGraham Sider kfd->device_info.gfx_target_version = gfx_target_version; 178f0dc99a6SGraham Sider 179f0dc99a6SGraham Sider if (KFD_IS_SOC15(kfd)) { 180f0dc99a6SGraham Sider kfd->device_info.doorbell_size = 8; 181f0dc99a6SGraham Sider kfd->device_info.ih_ring_entry_size = 8 * sizeof(uint32_t); 182f0dc99a6SGraham Sider kfd->device_info.supports_cwsr = true; 183f0dc99a6SGraham Sider 184cc009e61SMukul Joshi kfd_device_info_set_sdma_info(kfd); 185f89c6bf7SGuchun Chen 186f89c6bf7SGuchun Chen kfd_device_info_set_event_interrupt_class(kfd); 187f0dc99a6SGraham Sider 188f0dc99a6SGraham Sider /* Raven */ 189f0dc99a6SGraham Sider if (gc_version == IP_VERSION(9, 1, 0) || 190f0dc99a6SGraham Sider gc_version == IP_VERSION(9, 2, 2)) 191f0dc99a6SGraham Sider kfd->device_info.needs_iommu_device = true; 192f0dc99a6SGraham Sider 193f0dc99a6SGraham Sider if (gc_version < IP_VERSION(11, 0, 0)) { 194f0dc99a6SGraham Sider /* Navi2x+, Navi1x+ */ 19568e355c0SJesse Zhang if (gc_version == IP_VERSION(10, 3, 6)) 19668e355c0SJesse Zhang kfd->device_info.no_atomic_fw_version = 14; 197c4e85551SPrike Liang else if (gc_version == IP_VERSION(10, 3, 7)) 198c4e85551SPrike Liang kfd->device_info.no_atomic_fw_version = 3; 19968e355c0SJesse Zhang else if (gc_version >= IP_VERSION(10, 3, 0)) 200f0dc99a6SGraham Sider kfd->device_info.no_atomic_fw_version = 92; 20127cc310fSchen gong else if (gc_version >= IP_VERSION(10, 1, 1)) 20227cc310fSchen gong kfd->device_info.no_atomic_fw_version = 145; 203f0dc99a6SGraham Sider 204f0dc99a6SGraham Sider /* Navi1x+ */ 205f0dc99a6SGraham Sider if (gc_version >= IP_VERSION(10, 1, 1)) 206f0dc99a6SGraham Sider kfd->device_info.needs_pci_atomics = true; 207*00fa4035SSreekant Somasekharan } else if (gc_version < IP_VERSION(12, 0, 0)) { 208*00fa4035SSreekant Somasekharan /* 209*00fa4035SSreekant Somasekharan * PCIe atomics support acknowledgment in GFX11 RS64 CPFW requires 210*00fa4035SSreekant Somasekharan * MEC version >= 509. Prior RS64 CPFW versions (and all F32) require 211*00fa4035SSreekant Somasekharan * PCIe atomics support. 212*00fa4035SSreekant Somasekharan */ 213*00fa4035SSreekant Somasekharan kfd->device_info.needs_pci_atomics = true; 214*00fa4035SSreekant Somasekharan kfd->device_info.no_atomic_fw_version = kfd->adev->gfx.rs64_enable ? 509 : 0; 215f0dc99a6SGraham Sider } 216f0dc99a6SGraham Sider } else { 217f0dc99a6SGraham Sider kfd->device_info.doorbell_size = 4; 218f0dc99a6SGraham Sider kfd->device_info.ih_ring_entry_size = 4 * sizeof(uint32_t); 219f0dc99a6SGraham Sider kfd->device_info.event_interrupt_class = &event_interrupt_class_cik; 220f0dc99a6SGraham Sider kfd->device_info.num_sdma_queues_per_engine = 2; 221f0dc99a6SGraham Sider 222f0dc99a6SGraham Sider if (asic_type != CHIP_KAVERI && 223f0dc99a6SGraham Sider asic_type != CHIP_HAWAII && 224f0dc99a6SGraham Sider asic_type != CHIP_TONGA) 225f0dc99a6SGraham Sider kfd->device_info.supports_cwsr = true; 226f0dc99a6SGraham Sider 227f0dc99a6SGraham Sider if (asic_type == CHIP_KAVERI || 228f0dc99a6SGraham Sider asic_type == CHIP_CARRIZO) 229f0dc99a6SGraham Sider kfd->device_info.needs_iommu_device = true; 230f0dc99a6SGraham Sider 231f0dc99a6SGraham Sider if (asic_type != CHIP_HAWAII && !vf) 232f0dc99a6SGraham Sider kfd->device_info.needs_pci_atomics = true; 233f0dc99a6SGraham Sider } 234f0dc99a6SGraham Sider } 235f0dc99a6SGraham Sider 236b5d1d755SGraham Sider struct kfd_dev *kgd2kfd_probe(struct amdgpu_device *adev, bool vf) 2374a488a7aSOded Gabbay { 238f0dc99a6SGraham Sider struct kfd_dev *kfd = NULL; 239f0dc99a6SGraham Sider const struct kfd2kgd_calls *f2g = NULL; 240f0dc99a6SGraham Sider uint32_t gfx_target_version = 0; 241050091abSYong Zhao 242c868d584SAlex Deucher switch (adev->asic_type) { 243c868d584SAlex Deucher #ifdef KFD_SUPPORT_IOMMU_V2 244c868d584SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK 245c868d584SAlex Deucher case CHIP_KAVERI: 246f0dc99a6SGraham Sider gfx_target_version = 70000; 247f0dc99a6SGraham Sider if (!vf) 248c868d584SAlex Deucher f2g = &gfx_v7_kfd2kgd; 249c868d584SAlex Deucher break; 250c868d584SAlex Deucher #endif 251c868d584SAlex Deucher case CHIP_CARRIZO: 252f0dc99a6SGraham Sider gfx_target_version = 80001; 253f0dc99a6SGraham Sider if (!vf) 254c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 255c868d584SAlex Deucher break; 256c868d584SAlex Deucher #endif 257c868d584SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK 258c868d584SAlex Deucher case CHIP_HAWAII: 259f0dc99a6SGraham Sider gfx_target_version = 70001; 2600f7ef0b9SFelix Kuehling if (!amdgpu_exp_hw_support) 2610f7ef0b9SFelix Kuehling pr_info( 2620f7ef0b9SFelix Kuehling "KFD support on Hawaii is experimental. See modparam exp_hw_support\n" 2630f7ef0b9SFelix Kuehling ); 2640f7ef0b9SFelix Kuehling else if (!vf) 265c868d584SAlex Deucher f2g = &gfx_v7_kfd2kgd; 266c868d584SAlex Deucher break; 267c868d584SAlex Deucher #endif 268c868d584SAlex Deucher case CHIP_TONGA: 269f0dc99a6SGraham Sider gfx_target_version = 80002; 270f0dc99a6SGraham Sider if (!vf) 271c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 272c868d584SAlex Deucher break; 273c868d584SAlex Deucher case CHIP_FIJI: 274c868d584SAlex Deucher case CHIP_POLARIS10: 275f0dc99a6SGraham Sider gfx_target_version = 80003; 276c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 277c868d584SAlex Deucher break; 278c868d584SAlex Deucher case CHIP_POLARIS11: 279c868d584SAlex Deucher case CHIP_POLARIS12: 280c868d584SAlex Deucher case CHIP_VEGAM: 281f0dc99a6SGraham Sider gfx_target_version = 80003; 282f0dc99a6SGraham Sider if (!vf) 283c868d584SAlex Deucher f2g = &gfx_v8_kfd2kgd; 284c868d584SAlex Deucher break; 285c868d584SAlex Deucher default: 286c868d584SAlex Deucher switch (adev->ip_versions[GC_HWIP][0]) { 2872c1f19b3SGraham Sider /* Vega 10 */ 288c868d584SAlex Deucher case IP_VERSION(9, 0, 1): 289f0dc99a6SGraham Sider gfx_target_version = 90000; 290c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd; 291c868d584SAlex Deucher break; 292c868d584SAlex Deucher #ifdef KFD_SUPPORT_IOMMU_V2 2932c1f19b3SGraham Sider /* Raven */ 294c868d584SAlex Deucher case IP_VERSION(9, 1, 0): 295c868d584SAlex Deucher case IP_VERSION(9, 2, 2): 296f0dc99a6SGraham Sider gfx_target_version = 90002; 297f0dc99a6SGraham Sider if (!vf) 298c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd; 299c868d584SAlex Deucher break; 300c868d584SAlex Deucher #endif 3012c1f19b3SGraham Sider /* Vega12 */ 302c868d584SAlex Deucher case IP_VERSION(9, 2, 1): 303f0dc99a6SGraham Sider gfx_target_version = 90004; 304f0dc99a6SGraham Sider if (!vf) 305c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd; 306c868d584SAlex Deucher break; 3072c1f19b3SGraham Sider /* Renoir */ 308c868d584SAlex Deucher case IP_VERSION(9, 3, 0): 309f0dc99a6SGraham Sider gfx_target_version = 90012; 310f0dc99a6SGraham Sider if (!vf) 311c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd; 312c868d584SAlex Deucher break; 3132c1f19b3SGraham Sider /* Vega20 */ 314c868d584SAlex Deucher case IP_VERSION(9, 4, 0): 315f0dc99a6SGraham Sider gfx_target_version = 90006; 316f0dc99a6SGraham Sider if (!vf) 317c868d584SAlex Deucher f2g = &gfx_v9_kfd2kgd; 318c868d584SAlex Deucher break; 3192c1f19b3SGraham Sider /* Arcturus */ 320c868d584SAlex Deucher case IP_VERSION(9, 4, 1): 321f0dc99a6SGraham Sider gfx_target_version = 90008; 322c868d584SAlex Deucher f2g = &arcturus_kfd2kgd; 323c868d584SAlex Deucher break; 3242c1f19b3SGraham Sider /* Aldebaran */ 325c868d584SAlex Deucher case IP_VERSION(9, 4, 2): 326828d9a87SHawking Zhang case IP_VERSION(9, 4, 3): 327f0dc99a6SGraham Sider gfx_target_version = 90010; 328c868d584SAlex Deucher f2g = &aldebaran_kfd2kgd; 329c868d584SAlex Deucher break; 3302c1f19b3SGraham Sider /* Navi10 */ 331c868d584SAlex Deucher case IP_VERSION(10, 1, 10): 332f0dc99a6SGraham Sider gfx_target_version = 100100; 333f0dc99a6SGraham Sider if (!vf) 334c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd; 335c868d584SAlex Deucher break; 3362c1f19b3SGraham Sider /* Navi12 */ 337c868d584SAlex Deucher case IP_VERSION(10, 1, 2): 338f0dc99a6SGraham Sider gfx_target_version = 100101; 339c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd; 340c868d584SAlex Deucher break; 3412c1f19b3SGraham Sider /* Navi14 */ 342c868d584SAlex Deucher case IP_VERSION(10, 1, 1): 343f0dc99a6SGraham Sider gfx_target_version = 100102; 344f0dc99a6SGraham Sider if (!vf) 345c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd; 346c868d584SAlex Deucher break; 3472c1f19b3SGraham Sider /* Cyan Skillfish */ 348c868d584SAlex Deucher case IP_VERSION(10, 1, 3): 349f9ed188dSLang Yu case IP_VERSION(10, 1, 4): 350f0dc99a6SGraham Sider gfx_target_version = 100103; 351f0dc99a6SGraham Sider if (!vf) 352c868d584SAlex Deucher f2g = &gfx_v10_kfd2kgd; 353c868d584SAlex Deucher break; 3542c1f19b3SGraham Sider /* Sienna Cichlid */ 355c868d584SAlex Deucher case IP_VERSION(10, 3, 0): 356f0dc99a6SGraham Sider gfx_target_version = 100300; 357c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 358c868d584SAlex Deucher break; 3592c1f19b3SGraham Sider /* Navy Flounder */ 360c868d584SAlex Deucher case IP_VERSION(10, 3, 2): 361f0dc99a6SGraham Sider gfx_target_version = 100301; 362c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 363c868d584SAlex Deucher break; 3642c1f19b3SGraham Sider /* Van Gogh */ 365c868d584SAlex Deucher case IP_VERSION(10, 3, 1): 366f0dc99a6SGraham Sider gfx_target_version = 100303; 367f0dc99a6SGraham Sider if (!vf) 368c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 369c868d584SAlex Deucher break; 3702c1f19b3SGraham Sider /* Dimgrey Cavefish */ 371c868d584SAlex Deucher case IP_VERSION(10, 3, 4): 372f0dc99a6SGraham Sider gfx_target_version = 100302; 373c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 374c868d584SAlex Deucher break; 3752c1f19b3SGraham Sider /* Beige Goby */ 376c868d584SAlex Deucher case IP_VERSION(10, 3, 5): 377f0dc99a6SGraham Sider gfx_target_version = 100304; 378c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 379c868d584SAlex Deucher break; 3802c1f19b3SGraham Sider /* Yellow Carp */ 381c868d584SAlex Deucher case IP_VERSION(10, 3, 3): 382f0dc99a6SGraham Sider gfx_target_version = 100305; 383f0dc99a6SGraham Sider if (!vf) 384c868d584SAlex Deucher f2g = &gfx_v10_3_kfd2kgd; 385c868d584SAlex Deucher break; 3867c4f4f19SMario Limonciello case IP_VERSION(10, 3, 6): 3877c4f4f19SMario Limonciello case IP_VERSION(10, 3, 7): 3882724efa3SPrike Liang gfx_target_version = 100306; 3897c4f4f19SMario Limonciello if (!vf) 3907c4f4f19SMario Limonciello f2g = &gfx_v10_3_kfd2kgd; 3917c4f4f19SMario Limonciello break; 392cc009e61SMukul Joshi case IP_VERSION(11, 0, 0): 393cc009e61SMukul Joshi gfx_target_version = 110000; 394cc009e61SMukul Joshi f2g = &gfx_v11_kfd2kgd; 395cc009e61SMukul Joshi break; 39626776a70SHuang Rui case IP_VERSION(11, 0, 1): 39788c21c2bSYifan Zhang case IP_VERSION(11, 0, 4): 39826776a70SHuang Rui gfx_target_version = 110003; 39926776a70SHuang Rui f2g = &gfx_v11_kfd2kgd; 40026776a70SHuang Rui break; 401ec661f1cSEric Huang case IP_VERSION(11, 0, 2): 402ec661f1cSEric Huang gfx_target_version = 110002; 403ec661f1cSEric Huang f2g = &gfx_v11_kfd2kgd; 404ec661f1cSEric Huang break; 4055ddb5fe9SDavid Belanger case IP_VERSION(11, 0, 3): 4065ddb5fe9SDavid Belanger /* Note: Compiler version is 11.0.1 while HW version is 11.0.3 */ 4075ddb5fe9SDavid Belanger gfx_target_version = 110001; 4085ddb5fe9SDavid Belanger f2g = &gfx_v11_kfd2kgd; 4095ddb5fe9SDavid Belanger break; 410c868d584SAlex Deucher default: 411f0dc99a6SGraham Sider break; 412050091abSYong Zhao } 413c868d584SAlex Deucher break; 414c868d584SAlex Deucher } 4154a488a7aSOded Gabbay 416f0dc99a6SGraham Sider if (!f2g) { 417e4804a39SGraham Sider if (adev->ip_versions[GC_HWIP][0]) 418e4804a39SGraham Sider dev_err(kfd_device, "GC IP %06x %s not supported in kfd\n", 419e4804a39SGraham Sider adev->ip_versions[GC_HWIP][0], vf ? "VF" : ""); 420e4804a39SGraham Sider else 421050091abSYong Zhao dev_err(kfd_device, "%s %s not supported in kfd\n", 422c868d584SAlex Deucher amdgpu_asic_name[adev->asic_type], vf ? "VF" : ""); 4234a488a7aSOded Gabbay return NULL; 4244ebc7182SYong Zhao } 4254a488a7aSOded Gabbay 426d35f00d8SEric Huang kfd = kzalloc(sizeof(*kfd), GFP_KERNEL); 427d35f00d8SEric Huang if (!kfd) 428d35f00d8SEric Huang return NULL; 429d35f00d8SEric Huang 430c6c57446SGraham Sider kfd->adev = adev; 431f0dc99a6SGraham Sider kfd_device_info_init(kfd, vf, gfx_target_version); 43219f6d2a6SOded Gabbay kfd->init_complete = false; 433cea405b1SXihan Zhang kfd->kfd2kgd = f2g; 43443d8107fSHarish Kasiviswanathan atomic_set(&kfd->compute_profile, 0); 435cea405b1SXihan Zhang 436cea405b1SXihan Zhang mutex_init(&kfd->doorbell_mutex); 437cea405b1SXihan Zhang memset(&kfd->doorbell_available_index, 0, 438cea405b1SXihan Zhang sizeof(kfd->doorbell_available_index)); 4394a488a7aSOded Gabbay 4409b54d201SEric Huang atomic_set(&kfd->sram_ecc_flag, 0); 4419b54d201SEric Huang 44259d7115dSMukul Joshi ida_init(&kfd->doorbell_ida); 44359d7115dSMukul Joshi 4444a488a7aSOded Gabbay return kfd; 4454a488a7aSOded Gabbay } 4464a488a7aSOded Gabbay 447373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd) 448373d7080SFelix Kuehling { 449f0dc99a6SGraham Sider if (cwsr_enable && kfd->device_info.supports_cwsr) { 450046e674bSGraham Sider if (KFD_GC_VERSION(kfd) < IP_VERSION(9, 0, 1)) { 451373d7080SFelix Kuehling BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE); 452373d7080SFelix Kuehling kfd->cwsr_isa = cwsr_trap_gfx8_hex; 453373d7080SFelix Kuehling kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex); 454046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)) { 4553baa24f0SOak Zeng BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE); 4563baa24f0SOak Zeng kfd->cwsr_isa = cwsr_trap_arcturus_hex; 4573baa24f0SOak Zeng kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex); 458046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)) { 4590ef6845cSJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex) > PAGE_SIZE); 4600ef6845cSJay Cornwall kfd->cwsr_isa = cwsr_trap_aldebaran_hex; 4610ef6845cSJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex); 462828d9a87SHawking Zhang } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 3)) { 4631d44ff3dSJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_4_3_hex) > PAGE_SIZE); 4641d44ff3dSJay Cornwall kfd->cwsr_isa = cwsr_trap_gfx9_4_3_hex; 4651d44ff3dSJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_4_3_hex); 466046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 1, 1)) { 4673e76c239SFelix Kuehling BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE); 4683e76c239SFelix Kuehling kfd->cwsr_isa = cwsr_trap_gfx9_hex; 4693e76c239SFelix Kuehling kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex); 470046e674bSGraham Sider } else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 3, 0)) { 47180b6cfedSJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE); 47280b6cfedSJay Cornwall kfd->cwsr_isa = cwsr_trap_nv1x_hex; 47380b6cfedSJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex); 4746a817038SJay Cornwall } else if (KFD_GC_VERSION(kfd) < IP_VERSION(11, 0, 0)) { 47514328aa5SPhilip Cox BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE); 47614328aa5SPhilip Cox kfd->cwsr_isa = cwsr_trap_gfx10_hex; 47714328aa5SPhilip Cox kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex); 4786a817038SJay Cornwall } else { 4796a817038SJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_gfx11_hex) > PAGE_SIZE); 4806a817038SJay Cornwall kfd->cwsr_isa = cwsr_trap_gfx11_hex; 4816a817038SJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx11_hex); 4823e76c239SFelix Kuehling } 4833e76c239SFelix Kuehling 484373d7080SFelix Kuehling kfd->cwsr_enabled = true; 485373d7080SFelix Kuehling } 486373d7080SFelix Kuehling } 487373d7080SFelix Kuehling 48829633d0eSJoseph Greathouse static int kfd_gws_init(struct kfd_dev *kfd) 48929633d0eSJoseph Greathouse { 49029633d0eSJoseph Greathouse int ret = 0; 49129633d0eSJoseph Greathouse 49229633d0eSJoseph Greathouse if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) 49329633d0eSJoseph Greathouse return 0; 49429633d0eSJoseph Greathouse 495046e674bSGraham Sider if (hws_gws_support || (KFD_IS_SOC15(kfd) && 496046e674bSGraham Sider ((KFD_GC_VERSION(kfd) == IP_VERSION(9, 0, 1) 497046e674bSGraham Sider && kfd->mec2_fw_version >= 0x81b3) || 498046e674bSGraham Sider (KFD_GC_VERSION(kfd) <= IP_VERSION(9, 4, 0) 499046e674bSGraham Sider && kfd->mec2_fw_version >= 0x1b3) || 500046e674bSGraham Sider (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1) 501046e674bSGraham Sider && kfd->mec2_fw_version >= 0x30) || 502046e674bSGraham Sider (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2) 503beb15bc1SJonathan Kim && kfd->mec2_fw_version >= 0x28) || 504beb15bc1SJonathan Kim (KFD_GC_VERSION(kfd) >= IP_VERSION(10, 3, 0) 505beb15bc1SJonathan Kim && KFD_GC_VERSION(kfd) < IP_VERSION(11, 0, 0) 506beb15bc1SJonathan Kim && kfd->mec2_fw_version >= 0x6b)))) 5076bfc7c7eSGraham Sider ret = amdgpu_amdkfd_alloc_gws(kfd->adev, 50802274fc0SGraham Sider kfd->adev->gds.gws_size, &kfd->gws); 50929633d0eSJoseph Greathouse 51029633d0eSJoseph Greathouse return ret; 51129633d0eSJoseph Greathouse } 51229633d0eSJoseph Greathouse 5132243f493SRajneesh Bhardwaj static void kfd_smi_init(struct kfd_dev *dev) 5142243f493SRajneesh Bhardwaj { 515938a0650SAmber Lin INIT_LIST_HEAD(&dev->smi_clients); 516938a0650SAmber Lin spin_lock_init(&dev->smi_lock); 517938a0650SAmber Lin } 518938a0650SAmber Lin 5194a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd, 5204a488a7aSOded Gabbay const struct kgd2kfd_shared_resources *gpu_resources) 5214a488a7aSOded Gabbay { 522fd6a440eSJonathan Kim unsigned int size, map_process_packet_size; 52319f6d2a6SOded Gabbay 524574c4183SGraham Sider kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev, 5255ade6c9cSFelix Kuehling KGD_ENGINE_MEC1); 526574c4183SGraham Sider kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev, 52729633d0eSJoseph Greathouse KGD_ENGINE_MEC2); 528574c4183SGraham Sider kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev, 5295ade6c9cSFelix Kuehling KGD_ENGINE_SDMA1); 5304a488a7aSOded Gabbay kfd->shared_resources = *gpu_resources; 5314a488a7aSOded Gabbay 53244008d7aSYong Zhao kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1; 53344008d7aSYong Zhao kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1; 53444008d7aSYong Zhao kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd 53544008d7aSYong Zhao - kfd->vm_info.first_vmid_kfd + 1; 53644008d7aSYong Zhao 537e312af6cSFelix Kuehling /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps. 538e312af6cSFelix Kuehling * 32 and 64-bit requests are possible and must be 539e312af6cSFelix Kuehling * supported. 540e312af6cSFelix Kuehling */ 5416bfc7c7eSGraham Sider kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kfd->adev); 542e312af6cSFelix Kuehling if (!kfd->pci_atomic_requested && 543f0dc99a6SGraham Sider kfd->device_info.needs_pci_atomics && 544f0dc99a6SGraham Sider (!kfd->device_info.no_atomic_fw_version || 545f0dc99a6SGraham Sider kfd->mec_fw_version < kfd->device_info.no_atomic_fw_version)) { 546e312af6cSFelix Kuehling dev_info(kfd_device, 547e312af6cSFelix Kuehling "skipped device %x:%x, PCI rejects atomics %d<%d\n", 548d69a3b76SMukul Joshi kfd->adev->pdev->vendor, kfd->adev->pdev->device, 549e312af6cSFelix Kuehling kfd->mec_fw_version, 550f0dc99a6SGraham Sider kfd->device_info.no_atomic_fw_version); 551e312af6cSFelix Kuehling return false; 552e312af6cSFelix Kuehling } 553e312af6cSFelix Kuehling 554a99c6d4fSFelix Kuehling /* Verify module parameters regarding mapped process number*/ 555b7dfbd2eSTushar Patel if (hws_max_conc_proc >= 0) 556b7dfbd2eSTushar Patel kfd->max_proc_per_quantum = min((u32)hws_max_conc_proc, kfd->vm_info.vmid_num_kfd); 557b7dfbd2eSTushar Patel else 558a99c6d4fSFelix Kuehling kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd; 559a99c6d4fSFelix Kuehling 56019f6d2a6SOded Gabbay /* calculate max size of mqds needed for queues */ 561b8cbab04SOded Gabbay size = max_num_of_queues_per_device * 562f0dc99a6SGraham Sider kfd->device_info.mqd_size_aligned; 56319f6d2a6SOded Gabbay 564e18e794eSOded Gabbay /* 565e18e794eSOded Gabbay * calculate max size of runlist packet. 566e18e794eSOded Gabbay * There can be only 2 packets at once 567e18e794eSOded Gabbay */ 568046e674bSGraham Sider map_process_packet_size = KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2) ? 569fd6a440eSJonathan Kim sizeof(struct pm4_mes_map_process_aldebaran) : 570fd6a440eSJonathan Kim sizeof(struct pm4_mes_map_process); 571fd6a440eSJonathan Kim size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size + 572507968ddSFelix Kuehling max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues) 573507968ddSFelix Kuehling + sizeof(struct pm4_mes_runlist)) * 2; 574e18e794eSOded Gabbay 575e18e794eSOded Gabbay /* Add size of HIQ & DIQ */ 576e18e794eSOded Gabbay size += KFD_KERNEL_QUEUE_SIZE * 2; 577e18e794eSOded Gabbay 578e18e794eSOded Gabbay /* add another 512KB for all other allocations on gart (HPD, fences) */ 57919f6d2a6SOded Gabbay size += 512 * 1024; 58019f6d2a6SOded Gabbay 5817cd52c91SAmber Lin if (amdgpu_amdkfd_alloc_gtt_mem( 5826bfc7c7eSGraham Sider kfd->adev, size, &kfd->gtt_mem, 58315426dbbSYong Zhao &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr, 58415426dbbSYong Zhao false)) { 58579775b62SKent Russell dev_err(kfd_device, "Could not allocate %d bytes\n", size); 586e09d4fc8SOak Zeng goto alloc_gtt_mem_failure; 58719f6d2a6SOded Gabbay } 58819f6d2a6SOded Gabbay 58979775b62SKent Russell dev_info(kfd_device, "Allocated %d bytes on gart\n", size); 590e18e794eSOded Gabbay 59173a1da0bSOded Gabbay /* Initialize GTT sa with 512 byte chunk size */ 59273a1da0bSOded Gabbay if (kfd_gtt_sa_init(kfd, size, 512) != 0) { 59379775b62SKent Russell dev_err(kfd_device, "Error initializing gtt sub-allocator\n"); 59473a1da0bSOded Gabbay goto kfd_gtt_sa_init_error; 59573a1da0bSOded Gabbay } 59673a1da0bSOded Gabbay 597735df2baSFelix Kuehling if (kfd_doorbell_init(kfd)) { 598735df2baSFelix Kuehling dev_err(kfd_device, 599735df2baSFelix Kuehling "Error initializing doorbell aperture\n"); 600735df2baSFelix Kuehling goto kfd_doorbell_error; 601735df2baSFelix Kuehling } 60219f6d2a6SOded Gabbay 603c5650327SDivya Shikre if (amdgpu_use_xgmi_p2p) 60402274fc0SGraham Sider kfd->hive_id = kfd->adev->gmc.xgmi.hive_id; 6050c1690e3SShaoyun Liu 60602274fc0SGraham Sider kfd->noretry = kfd->adev->gmc.noretry; 6079b498efaSAlex Deucher 6082249d558SAndrew Lewycky if (kfd_interrupt_init(kfd)) { 60979775b62SKent Russell dev_err(kfd_device, "Error initializing interrupts\n"); 6102249d558SAndrew Lewycky goto kfd_interrupt_error; 6112249d558SAndrew Lewycky } 6122249d558SAndrew Lewycky 61364c7f8cfSBen Goz kfd->dqm = device_queue_manager_init(kfd); 61464c7f8cfSBen Goz if (!kfd->dqm) { 61579775b62SKent Russell dev_err(kfd_device, "Error initializing queue manager\n"); 61664c7f8cfSBen Goz goto device_queue_manager_error; 61764c7f8cfSBen Goz } 61864c7f8cfSBen Goz 61929633d0eSJoseph Greathouse /* If supported on this device, allocate global GWS that is shared 62029633d0eSJoseph Greathouse * by all KFD processes 62129633d0eSJoseph Greathouse */ 62229633d0eSJoseph Greathouse if (kfd_gws_init(kfd)) { 62329633d0eSJoseph Greathouse dev_err(kfd_device, "Could not allocate %d gws\n", 62402274fc0SGraham Sider kfd->adev->gds.gws_size); 62529633d0eSJoseph Greathouse goto gws_error; 62629633d0eSJoseph Greathouse } 62729633d0eSJoseph Greathouse 6286127896fSHuang Rui /* If CRAT is broken, won't set iommu enabled */ 6296127896fSHuang Rui kfd_double_confirm_iommu_support(kfd); 6306127896fSHuang Rui 63164d1c3a4SFelix Kuehling if (kfd_iommu_device_init(kfd)) { 6326f4b590aSYifan Zhang kfd->use_iommu_v2 = false; 63364d1c3a4SFelix Kuehling dev_err(kfd_device, "Error initializing iommuv2\n"); 63464d1c3a4SFelix Kuehling goto device_iommu_error; 63564c7f8cfSBen Goz } 63664c7f8cfSBen Goz 637373d7080SFelix Kuehling kfd_cwsr_init(kfd); 638373d7080SFelix Kuehling 63956c5977eSGraham Sider svm_migrate_init(kfd->adev); 640814ab993SPhilip Yang 6417ee938acSFelix Kuehling if (kfd_resume_iommu(kfd)) 642afd18180SYifan Zhang goto device_iommu_error; 643afd18180SYifan Zhang 644b8935a7cSYong Zhao if (kfd_resume(kfd)) 645b8935a7cSYong Zhao goto kfd_resume_error; 646b8935a7cSYong Zhao 647b179fc28SMukul Joshi amdgpu_amdkfd_get_local_mem_info(kfd->adev, &kfd->local_mem_info); 648b179fc28SMukul Joshi 649465ab9e0SOak Zeng if (kfd_topology_add_device(kfd)) { 650465ab9e0SOak Zeng dev_err(kfd_device, "Error adding device to topology\n"); 651465ab9e0SOak Zeng goto kfd_topology_add_device_error; 652465ab9e0SOak Zeng } 653465ab9e0SOak Zeng 654938a0650SAmber Lin kfd_smi_init(kfd); 655938a0650SAmber Lin 6564a488a7aSOded Gabbay kfd->init_complete = true; 657d69a3b76SMukul Joshi dev_info(kfd_device, "added device %x:%x\n", kfd->adev->pdev->vendor, 658d69a3b76SMukul Joshi kfd->adev->pdev->device); 6594a488a7aSOded Gabbay 66079775b62SKent Russell pr_debug("Starting kfd with the following scheduling policy %d\n", 661d146c5a7SFelix Kuehling kfd->dqm->sched_policy); 66264c7f8cfSBen Goz 66319f6d2a6SOded Gabbay goto out; 66419f6d2a6SOded Gabbay 665465ab9e0SOak Zeng kfd_topology_add_device_error: 666b8935a7cSYong Zhao kfd_resume_error: 66764d1c3a4SFelix Kuehling device_iommu_error: 66829633d0eSJoseph Greathouse gws_error: 66964c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 67064c7f8cfSBen Goz device_queue_manager_error: 6712249d558SAndrew Lewycky kfd_interrupt_exit(kfd); 6722249d558SAndrew Lewycky kfd_interrupt_error: 673735df2baSFelix Kuehling kfd_doorbell_fini(kfd); 674735df2baSFelix Kuehling kfd_doorbell_error: 67573a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd); 67673a1da0bSOded Gabbay kfd_gtt_sa_init_error: 6776bfc7c7eSGraham Sider amdgpu_amdkfd_free_gtt_mem(kfd->adev, kfd->gtt_mem); 678e09d4fc8SOak Zeng alloc_gtt_mem_failure: 67929633d0eSJoseph Greathouse if (kfd->gws) 6806bfc7c7eSGraham Sider amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws); 68119f6d2a6SOded Gabbay dev_err(kfd_device, 68279775b62SKent Russell "device %x:%x NOT added due to errors\n", 683d69a3b76SMukul Joshi kfd->adev->pdev->vendor, kfd->adev->pdev->device); 68419f6d2a6SOded Gabbay out: 68519f6d2a6SOded Gabbay return kfd->init_complete; 6864a488a7aSOded Gabbay } 6874a488a7aSOded Gabbay 6884a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd) 6894a488a7aSOded Gabbay { 690b17f068aSOded Gabbay if (kfd->init_complete) { 69164c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 6922249d558SAndrew Lewycky kfd_interrupt_exit(kfd); 69319f6d2a6SOded Gabbay kfd_topology_remove_device(kfd); 694735df2baSFelix Kuehling kfd_doorbell_fini(kfd); 69559d7115dSMukul Joshi ida_destroy(&kfd->doorbell_ida); 69673a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd); 6976bfc7c7eSGraham Sider amdgpu_amdkfd_free_gtt_mem(kfd->adev, kfd->gtt_mem); 69829633d0eSJoseph Greathouse if (kfd->gws) 6996bfc7c7eSGraham Sider amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws); 700b17f068aSOded Gabbay } 7015b5c4e40SEvgeny Pinchuk 7024a488a7aSOded Gabbay kfree(kfd); 7034a488a7aSOded Gabbay } 7044a488a7aSOded Gabbay 705e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd) 706e3b7a967SShaoyun Liu { 707e42051d2SShaoyun Liu if (!kfd->init_complete) 708e42051d2SShaoyun Liu return 0; 70909c34e8dSFelix Kuehling 71055977744SMukul Joshi kfd_smi_event_update_gpu_reset(kfd, false); 71155977744SMukul Joshi 71209c34e8dSFelix Kuehling kfd->dqm->ops.pre_reset(kfd->dqm); 71309c34e8dSFelix Kuehling 7149593f4d6SRajneesh Bhardwaj kgd2kfd_suspend(kfd, false); 715e42051d2SShaoyun Liu 716e42051d2SShaoyun Liu kfd_signal_reset_event(kfd); 717e3b7a967SShaoyun Liu return 0; 718e3b7a967SShaoyun Liu } 719e3b7a967SShaoyun Liu 720e42051d2SShaoyun Liu /* 721e42051d2SShaoyun Liu * Fix me. KFD won't be able to resume existing process for now. 722e42051d2SShaoyun Liu * We will keep all existing process in a evicted state and 723e42051d2SShaoyun Liu * wait the process to be terminated. 724e42051d2SShaoyun Liu */ 725e42051d2SShaoyun Liu 726e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd) 727e3b7a967SShaoyun Liu { 728a1bd079fSyu kuai int ret; 729e42051d2SShaoyun Liu 730e42051d2SShaoyun Liu if (!kfd->init_complete) 731e3b7a967SShaoyun Liu return 0; 732e42051d2SShaoyun Liu 733e42051d2SShaoyun Liu ret = kfd_resume(kfd); 734e42051d2SShaoyun Liu if (ret) 735e42051d2SShaoyun Liu return ret; 736a1bd079fSyu kuai atomic_dec(&kfd_locked); 7379b54d201SEric Huang 7389b54d201SEric Huang atomic_set(&kfd->sram_ecc_flag, 0); 7399b54d201SEric Huang 74055977744SMukul Joshi kfd_smi_event_update_gpu_reset(kfd, true); 74155977744SMukul Joshi 742e42051d2SShaoyun Liu return 0; 743e42051d2SShaoyun Liu } 744e42051d2SShaoyun Liu 745e42051d2SShaoyun Liu bool kfd_is_locked(void) 746e42051d2SShaoyun Liu { 747e42051d2SShaoyun Liu return (atomic_read(&kfd_locked) > 0); 748e3b7a967SShaoyun Liu } 749e3b7a967SShaoyun Liu 7509593f4d6SRajneesh Bhardwaj void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm) 7514a488a7aSOded Gabbay { 752733fa1f7SYong Zhao if (!kfd->init_complete) 753733fa1f7SYong Zhao return; 754733fa1f7SYong Zhao 7559593f4d6SRajneesh Bhardwaj /* for runtime suspend, skip locking kfd */ 7569593f4d6SRajneesh Bhardwaj if (!run_pm) { 75726103436SFelix Kuehling /* For first KFD device suspend all the KFD processes */ 758e42051d2SShaoyun Liu if (atomic_inc_return(&kfd_locked) == 1) 75926103436SFelix Kuehling kfd_suspend_all_processes(); 7609593f4d6SRajneesh Bhardwaj } 76126103436SFelix Kuehling 76245c9a5e4SOded Gabbay kfd->dqm->ops.stop(kfd->dqm); 76364d1c3a4SFelix Kuehling kfd_iommu_suspend(kfd); 7644a488a7aSOded Gabbay } 7654a488a7aSOded Gabbay 7669593f4d6SRajneesh Bhardwaj int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm) 7674a488a7aSOded Gabbay { 76826103436SFelix Kuehling int ret, count; 76926103436SFelix Kuehling 770b8935a7cSYong Zhao if (!kfd->init_complete) 771b8935a7cSYong Zhao return 0; 772b17f068aSOded Gabbay 77326103436SFelix Kuehling ret = kfd_resume(kfd); 77426103436SFelix Kuehling if (ret) 77526103436SFelix Kuehling return ret; 776b17f068aSOded Gabbay 7779593f4d6SRajneesh Bhardwaj /* for runtime resume, skip unlocking kfd */ 7789593f4d6SRajneesh Bhardwaj if (!run_pm) { 779e42051d2SShaoyun Liu count = atomic_dec_return(&kfd_locked); 78026103436SFelix Kuehling WARN_ONCE(count < 0, "KFD suspend / resume ref. error"); 78126103436SFelix Kuehling if (count == 0) 78226103436SFelix Kuehling ret = kfd_resume_all_processes(); 7839593f4d6SRajneesh Bhardwaj } 78426103436SFelix Kuehling 78526103436SFelix Kuehling return ret; 7864ebc7182SYong Zhao } 7874ebc7182SYong Zhao 788f8846323SJames Zhu int kgd2kfd_resume_iommu(struct kfd_dev *kfd) 789b8935a7cSYong Zhao { 7907ee938acSFelix Kuehling if (!kfd->init_complete) 7917ee938acSFelix Kuehling return 0; 7927ee938acSFelix Kuehling 7937ee938acSFelix Kuehling return kfd_resume_iommu(kfd); 7947ee938acSFelix Kuehling } 7957ee938acSFelix Kuehling 7967ee938acSFelix Kuehling static int kfd_resume_iommu(struct kfd_dev *kfd) 7977ee938acSFelix Kuehling { 798b8935a7cSYong Zhao int err = 0; 799b8935a7cSYong Zhao 80064d1c3a4SFelix Kuehling err = kfd_iommu_resume(kfd); 801f8846323SJames Zhu if (err) 80264d1c3a4SFelix Kuehling dev_err(kfd_device, 80364d1c3a4SFelix Kuehling "Failed to resume IOMMU for device %x:%x\n", 804d69a3b76SMukul Joshi kfd->adev->pdev->vendor, kfd->adev->pdev->device); 80564d1c3a4SFelix Kuehling return err; 80664d1c3a4SFelix Kuehling } 807733fa1f7SYong Zhao 808f8846323SJames Zhu static int kfd_resume(struct kfd_dev *kfd) 809f8846323SJames Zhu { 810f8846323SJames Zhu int err = 0; 811f8846323SJames Zhu 812b8935a7cSYong Zhao err = kfd->dqm->ops.start(kfd->dqm); 813499f4d38SYifan Zhang if (err) 814b8935a7cSYong Zhao dev_err(kfd_device, 815b8935a7cSYong Zhao "Error starting queue manager for device %x:%x\n", 816d69a3b76SMukul Joshi kfd->adev->pdev->vendor, kfd->adev->pdev->device); 817b17f068aSOded Gabbay 818b8935a7cSYong Zhao return err; 8194a488a7aSOded Gabbay } 8204a488a7aSOded Gabbay 821b3eca59dSPhilip Yang static inline void kfd_queue_work(struct workqueue_struct *wq, 822b3eca59dSPhilip Yang struct work_struct *work) 823b3eca59dSPhilip Yang { 824b3eca59dSPhilip Yang int cpu, new_cpu; 825b3eca59dSPhilip Yang 826b3eca59dSPhilip Yang cpu = new_cpu = smp_processor_id(); 827b3eca59dSPhilip Yang do { 828b3eca59dSPhilip Yang new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids; 829b3eca59dSPhilip Yang if (cpu_to_node(new_cpu) == numa_node_id()) 830b3eca59dSPhilip Yang break; 831b3eca59dSPhilip Yang } while (cpu != new_cpu); 832b3eca59dSPhilip Yang 833b3eca59dSPhilip Yang queue_work_on(new_cpu, wq, work); 834b3eca59dSPhilip Yang } 835b3eca59dSPhilip Yang 836b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */ 837b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry) 8384a488a7aSOded Gabbay { 83958e69886SLan Xiao uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE]; 84058e69886SLan Xiao bool is_patched = false; 8412383a767SChristian König unsigned long flags; 84258e69886SLan Xiao 8432249d558SAndrew Lewycky if (!kfd->init_complete) 8442249d558SAndrew Lewycky return; 8452249d558SAndrew Lewycky 846f0dc99a6SGraham Sider if (kfd->device_info.ih_ring_entry_size > sizeof(patched_ihre)) { 84758e69886SLan Xiao dev_err_once(kfd_device, "Ring entry too small\n"); 84858e69886SLan Xiao return; 84958e69886SLan Xiao } 85058e69886SLan Xiao 8512383a767SChristian König spin_lock_irqsave(&kfd->interrupt_lock, flags); 8522249d558SAndrew Lewycky 8532249d558SAndrew Lewycky if (kfd->interrupts_active 85458e69886SLan Xiao && interrupt_is_wanted(kfd, ih_ring_entry, 85558e69886SLan Xiao patched_ihre, &is_patched) 85658e69886SLan Xiao && enqueue_ih_ring_entry(kfd, 85758e69886SLan Xiao is_patched ? patched_ihre : ih_ring_entry)) 858b3eca59dSPhilip Yang kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work); 8592249d558SAndrew Lewycky 8602383a767SChristian König spin_unlock_irqrestore(&kfd->interrupt_lock, flags); 8614a488a7aSOded Gabbay } 8626e81090bSOded Gabbay 863c7f21978SPhilip Yang int kgd2kfd_quiesce_mm(struct mm_struct *mm, uint32_t trigger) 8646b95e797SFelix Kuehling { 8656b95e797SFelix Kuehling struct kfd_process *p; 8666b95e797SFelix Kuehling int r; 8676b95e797SFelix Kuehling 8686b95e797SFelix Kuehling /* Because we are called from arbitrary context (workqueue) as opposed 8696b95e797SFelix Kuehling * to process context, kfd_process could attempt to exit while we are 8706b95e797SFelix Kuehling * running so the lookup function increments the process ref count. 8716b95e797SFelix Kuehling */ 8726b95e797SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 8736b95e797SFelix Kuehling if (!p) 8746b95e797SFelix Kuehling return -ESRCH; 8756b95e797SFelix Kuehling 876b2057956SFelix Kuehling WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid); 877c7f21978SPhilip Yang r = kfd_process_evict_queues(p, trigger); 8786b95e797SFelix Kuehling 8796b95e797SFelix Kuehling kfd_unref_process(p); 8806b95e797SFelix Kuehling return r; 8816b95e797SFelix Kuehling } 8826b95e797SFelix Kuehling 8836b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm) 8846b95e797SFelix Kuehling { 8856b95e797SFelix Kuehling struct kfd_process *p; 8866b95e797SFelix Kuehling int r; 8876b95e797SFelix Kuehling 8886b95e797SFelix Kuehling /* Because we are called from arbitrary context (workqueue) as opposed 8896b95e797SFelix Kuehling * to process context, kfd_process could attempt to exit while we are 8906b95e797SFelix Kuehling * running so the lookup function increments the process ref count. 8916b95e797SFelix Kuehling */ 8926b95e797SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 8936b95e797SFelix Kuehling if (!p) 8946b95e797SFelix Kuehling return -ESRCH; 8956b95e797SFelix Kuehling 8966b95e797SFelix Kuehling r = kfd_process_restore_queues(p); 8976b95e797SFelix Kuehling 8986b95e797SFelix Kuehling kfd_unref_process(p); 8996b95e797SFelix Kuehling return r; 9006b95e797SFelix Kuehling } 9016b95e797SFelix Kuehling 90226103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will 90326103436SFelix Kuehling * prepare for safe eviction of KFD BOs that belong to the specified 90426103436SFelix Kuehling * process. 90526103436SFelix Kuehling * 90626103436SFelix Kuehling * @mm: mm_struct that identifies the specified KFD process 90726103436SFelix Kuehling * @fence: eviction fence attached to KFD process BOs 90826103436SFelix Kuehling * 90926103436SFelix Kuehling */ 91026103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm, 91126103436SFelix Kuehling struct dma_fence *fence) 91226103436SFelix Kuehling { 91326103436SFelix Kuehling struct kfd_process *p; 91426103436SFelix Kuehling unsigned long active_time; 91526103436SFelix Kuehling unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS); 91626103436SFelix Kuehling 91726103436SFelix Kuehling if (!fence) 91826103436SFelix Kuehling return -EINVAL; 91926103436SFelix Kuehling 92026103436SFelix Kuehling if (dma_fence_is_signaled(fence)) 92126103436SFelix Kuehling return 0; 92226103436SFelix Kuehling 92326103436SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 92426103436SFelix Kuehling if (!p) 92526103436SFelix Kuehling return -ENODEV; 92626103436SFelix Kuehling 92726103436SFelix Kuehling if (fence->seqno == p->last_eviction_seqno) 92826103436SFelix Kuehling goto out; 92926103436SFelix Kuehling 93026103436SFelix Kuehling p->last_eviction_seqno = fence->seqno; 93126103436SFelix Kuehling 93226103436SFelix Kuehling /* Avoid KFD process starvation. Wait for at least 93326103436SFelix Kuehling * PROCESS_ACTIVE_TIME_MS before evicting the process again 93426103436SFelix Kuehling */ 93526103436SFelix Kuehling active_time = get_jiffies_64() - p->last_restore_timestamp; 93626103436SFelix Kuehling if (delay_jiffies > active_time) 93726103436SFelix Kuehling delay_jiffies -= active_time; 93826103436SFelix Kuehling else 93926103436SFelix Kuehling delay_jiffies = 0; 94026103436SFelix Kuehling 94126103436SFelix Kuehling /* During process initialization eviction_work.dwork is initialized 94226103436SFelix Kuehling * to kfd_evict_bo_worker 94326103436SFelix Kuehling */ 944b2057956SFelix Kuehling WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies", 945b2057956SFelix Kuehling p->lead_thread->pid, delay_jiffies); 94626103436SFelix Kuehling schedule_delayed_work(&p->eviction_work, delay_jiffies); 94726103436SFelix Kuehling out: 94826103436SFelix Kuehling kfd_unref_process(p); 94926103436SFelix Kuehling return 0; 95026103436SFelix Kuehling } 95126103436SFelix Kuehling 9526e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 9536e81090bSOded Gabbay unsigned int chunk_size) 9546e81090bSOded Gabbay { 95532fa8219SFelix Kuehling if (WARN_ON(buf_size < chunk_size)) 95632fa8219SFelix Kuehling return -EINVAL; 95732fa8219SFelix Kuehling if (WARN_ON(buf_size == 0)) 95832fa8219SFelix Kuehling return -EINVAL; 95932fa8219SFelix Kuehling if (WARN_ON(chunk_size == 0)) 96032fa8219SFelix Kuehling return -EINVAL; 9616e81090bSOded Gabbay 9626e81090bSOded Gabbay kfd->gtt_sa_chunk_size = chunk_size; 9636e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks = buf_size / chunk_size; 9646e81090bSOded Gabbay 965f43a9f18SChristophe JAILLET kfd->gtt_sa_bitmap = bitmap_zalloc(kfd->gtt_sa_num_of_chunks, 966f43a9f18SChristophe JAILLET GFP_KERNEL); 9676e81090bSOded Gabbay if (!kfd->gtt_sa_bitmap) 9686e81090bSOded Gabbay return -ENOMEM; 9696e81090bSOded Gabbay 97079775b62SKent Russell pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n", 9716e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap); 9726e81090bSOded Gabbay 9736e81090bSOded Gabbay mutex_init(&kfd->gtt_sa_lock); 9746e81090bSOded Gabbay 9756e81090bSOded Gabbay return 0; 9766e81090bSOded Gabbay } 9776e81090bSOded Gabbay 9786e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd) 9796e81090bSOded Gabbay { 9806e81090bSOded Gabbay mutex_destroy(&kfd->gtt_sa_lock); 981f43a9f18SChristophe JAILLET bitmap_free(kfd->gtt_sa_bitmap); 9826e81090bSOded Gabbay } 9836e81090bSOded Gabbay 9846e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr, 9856e81090bSOded Gabbay unsigned int bit_num, 9866e81090bSOded Gabbay unsigned int chunk_size) 9876e81090bSOded Gabbay { 9886e81090bSOded Gabbay return start_addr + bit_num * chunk_size; 9896e81090bSOded Gabbay } 9906e81090bSOded Gabbay 9916e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr, 9926e81090bSOded Gabbay unsigned int bit_num, 9936e81090bSOded Gabbay unsigned int chunk_size) 9946e81090bSOded Gabbay { 9956e81090bSOded Gabbay return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size); 9966e81090bSOded Gabbay } 9976e81090bSOded Gabbay 9986e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size, 9996e81090bSOded Gabbay struct kfd_mem_obj **mem_obj) 10006e81090bSOded Gabbay { 10016e81090bSOded Gabbay unsigned int found, start_search, cur_size; 10026e81090bSOded Gabbay 10036e81090bSOded Gabbay if (size == 0) 10046e81090bSOded Gabbay return -EINVAL; 10056e81090bSOded Gabbay 10066e81090bSOded Gabbay if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size) 10076e81090bSOded Gabbay return -ENOMEM; 10086e81090bSOded Gabbay 10091cd106ecSFelix Kuehling *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL); 10101cd106ecSFelix Kuehling if (!(*mem_obj)) 10116e81090bSOded Gabbay return -ENOMEM; 10126e81090bSOded Gabbay 101379775b62SKent Russell pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size); 10146e81090bSOded Gabbay 10156e81090bSOded Gabbay start_search = 0; 10166e81090bSOded Gabbay 10176e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock); 10186e81090bSOded Gabbay 10196e81090bSOded Gabbay kfd_gtt_restart_search: 10206e81090bSOded Gabbay /* Find the first chunk that is free */ 10216e81090bSOded Gabbay found = find_next_zero_bit(kfd->gtt_sa_bitmap, 10226e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, 10236e81090bSOded Gabbay start_search); 10246e81090bSOded Gabbay 102579775b62SKent Russell pr_debug("Found = %d\n", found); 10266e81090bSOded Gabbay 10276e81090bSOded Gabbay /* If there wasn't any free chunk, bail out */ 10286e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks) 10296e81090bSOded Gabbay goto kfd_gtt_no_free_chunk; 10306e81090bSOded Gabbay 10316e81090bSOded Gabbay /* Update fields of mem_obj */ 10326e81090bSOded Gabbay (*mem_obj)->range_start = found; 10336e81090bSOded Gabbay (*mem_obj)->range_end = found; 10346e81090bSOded Gabbay (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr( 10356e81090bSOded Gabbay kfd->gtt_start_gpu_addr, 10366e81090bSOded Gabbay found, 10376e81090bSOded Gabbay kfd->gtt_sa_chunk_size); 10386e81090bSOded Gabbay (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr( 10396e81090bSOded Gabbay kfd->gtt_start_cpu_ptr, 10406e81090bSOded Gabbay found, 10416e81090bSOded Gabbay kfd->gtt_sa_chunk_size); 10426e81090bSOded Gabbay 104379775b62SKent Russell pr_debug("gpu_addr = %p, cpu_addr = %p\n", 10446e81090bSOded Gabbay (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr); 10456e81090bSOded Gabbay 10466e81090bSOded Gabbay /* If we need only one chunk, mark it as allocated and get out */ 10476e81090bSOded Gabbay if (size <= kfd->gtt_sa_chunk_size) { 104879775b62SKent Russell pr_debug("Single bit\n"); 1049b8b9ba58SChristophe JAILLET __set_bit(found, kfd->gtt_sa_bitmap); 10506e81090bSOded Gabbay goto kfd_gtt_out; 10516e81090bSOded Gabbay } 10526e81090bSOded Gabbay 10536e81090bSOded Gabbay /* Otherwise, try to see if we have enough contiguous chunks */ 10546e81090bSOded Gabbay cur_size = size - kfd->gtt_sa_chunk_size; 10556e81090bSOded Gabbay do { 10566e81090bSOded Gabbay (*mem_obj)->range_end = 10576e81090bSOded Gabbay find_next_zero_bit(kfd->gtt_sa_bitmap, 10586e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, ++found); 10596e81090bSOded Gabbay /* 10606e81090bSOded Gabbay * If next free chunk is not contiguous than we need to 10616e81090bSOded Gabbay * restart our search from the last free chunk we found (which 10626e81090bSOded Gabbay * wasn't contiguous to the previous ones 10636e81090bSOded Gabbay */ 10646e81090bSOded Gabbay if ((*mem_obj)->range_end != found) { 10656e81090bSOded Gabbay start_search = found; 10666e81090bSOded Gabbay goto kfd_gtt_restart_search; 10676e81090bSOded Gabbay } 10686e81090bSOded Gabbay 10696e81090bSOded Gabbay /* 10706e81090bSOded Gabbay * If we reached end of buffer, bail out with error 10716e81090bSOded Gabbay */ 10726e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks) 10736e81090bSOded Gabbay goto kfd_gtt_no_free_chunk; 10746e81090bSOded Gabbay 10756e81090bSOded Gabbay /* Check if we don't need another chunk */ 10766e81090bSOded Gabbay if (cur_size <= kfd->gtt_sa_chunk_size) 10776e81090bSOded Gabbay cur_size = 0; 10786e81090bSOded Gabbay else 10796e81090bSOded Gabbay cur_size -= kfd->gtt_sa_chunk_size; 10806e81090bSOded Gabbay 10816e81090bSOded Gabbay } while (cur_size > 0); 10826e81090bSOded Gabbay 108379775b62SKent Russell pr_debug("range_start = %d, range_end = %d\n", 10846e81090bSOded Gabbay (*mem_obj)->range_start, (*mem_obj)->range_end); 10856e81090bSOded Gabbay 10866e81090bSOded Gabbay /* Mark the chunks as allocated */ 1087b8b9ba58SChristophe JAILLET bitmap_set(kfd->gtt_sa_bitmap, (*mem_obj)->range_start, 1088b8b9ba58SChristophe JAILLET (*mem_obj)->range_end - (*mem_obj)->range_start + 1); 10896e81090bSOded Gabbay 10906e81090bSOded Gabbay kfd_gtt_out: 10916e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 10926e81090bSOded Gabbay return 0; 10936e81090bSOded Gabbay 10946e81090bSOded Gabbay kfd_gtt_no_free_chunk: 10953148a6a0SJack Zhang pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj); 10966e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 10973148a6a0SJack Zhang kfree(*mem_obj); 10986e81090bSOded Gabbay return -ENOMEM; 10996e81090bSOded Gabbay } 11006e81090bSOded Gabbay 11016e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj) 11026e81090bSOded Gabbay { 11039216ed29SOded Gabbay /* Act like kfree when trying to free a NULL object */ 11049216ed29SOded Gabbay if (!mem_obj) 11059216ed29SOded Gabbay return 0; 11066e81090bSOded Gabbay 110779775b62SKent Russell pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n", 11086e81090bSOded Gabbay mem_obj, mem_obj->range_start, mem_obj->range_end); 11096e81090bSOded Gabbay 11106e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock); 11116e81090bSOded Gabbay 11126e81090bSOded Gabbay /* Mark the chunks as free */ 1113b8b9ba58SChristophe JAILLET bitmap_clear(kfd->gtt_sa_bitmap, mem_obj->range_start, 1114b8b9ba58SChristophe JAILLET mem_obj->range_end - mem_obj->range_start + 1); 11156e81090bSOded Gabbay 11166e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 11176e81090bSOded Gabbay 11186e81090bSOded Gabbay kfree(mem_obj); 11196e81090bSOded Gabbay return 0; 11206e81090bSOded Gabbay } 1121a29ec470SShaoyun Liu 11229b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd) 11239b54d201SEric Huang { 11249b54d201SEric Huang if (kfd) 11259b54d201SEric Huang atomic_inc(&kfd->sram_ecc_flag); 11269b54d201SEric Huang } 11279b54d201SEric Huang 112843d8107fSHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd) 112943d8107fSHarish Kasiviswanathan { 113043d8107fSHarish Kasiviswanathan if (atomic_inc_return(&kfd->compute_profile) == 1) 11316bfc7c7eSGraham Sider amdgpu_amdkfd_set_compute_idle(kfd->adev, false); 113243d8107fSHarish Kasiviswanathan } 113343d8107fSHarish Kasiviswanathan 113443d8107fSHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd) 113543d8107fSHarish Kasiviswanathan { 113643d8107fSHarish Kasiviswanathan int count = atomic_dec_return(&kfd->compute_profile); 113743d8107fSHarish Kasiviswanathan 113843d8107fSHarish Kasiviswanathan if (count == 0) 11396bfc7c7eSGraham Sider amdgpu_amdkfd_set_compute_idle(kfd->adev, true); 114043d8107fSHarish Kasiviswanathan WARN_ONCE(count < 0, "Compute profile ref. count error"); 114143d8107fSHarish Kasiviswanathan } 114243d8107fSHarish Kasiviswanathan 1143410e302eSGraham Sider void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask) 11442c2b0d88SMukul Joshi { 1145158fc08dSAmber Lin if (kfd && kfd->init_complete) 11462c2b0d88SMukul Joshi kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask); 11472c2b0d88SMukul Joshi } 11482c2b0d88SMukul Joshi 1149ee2f17f4SAmber Lin /* kfd_get_num_sdma_engines returns the number of PCIe optimized SDMA and 1150ee2f17f4SAmber Lin * kfd_get_num_xgmi_sdma_engines returns the number of XGMI SDMA. 1151ee2f17f4SAmber Lin * When the device has more than two engines, we reserve two for PCIe to enable 1152ee2f17f4SAmber Lin * full-duplex and the rest are used as XGMI. 1153ee2f17f4SAmber Lin */ 1154ee2f17f4SAmber Lin unsigned int kfd_get_num_sdma_engines(struct kfd_dev *kdev) 1155ee2f17f4SAmber Lin { 1156ee2f17f4SAmber Lin /* If XGMI is not supported, all SDMA engines are PCIe */ 1157ee2f17f4SAmber Lin if (!kdev->adev->gmc.xgmi.supported) 1158ee2f17f4SAmber Lin return kdev->adev->sdma.num_instances; 1159ee2f17f4SAmber Lin 1160ee2f17f4SAmber Lin return min(kdev->adev->sdma.num_instances, 2); 1161ee2f17f4SAmber Lin } 1162ee2f17f4SAmber Lin 1163ee2f17f4SAmber Lin unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_dev *kdev) 1164ee2f17f4SAmber Lin { 1165ee2f17f4SAmber Lin /* After reserved for PCIe, the rest of engines are XGMI */ 1166ee2f17f4SAmber Lin return kdev->adev->sdma.num_instances - kfd_get_num_sdma_engines(kdev); 1167ee2f17f4SAmber Lin } 1168ee2f17f4SAmber Lin 1169a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS) 1170a29ec470SShaoyun Liu 1171a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS 1172a29ec470SShaoyun Liu * which will trigger a GPU reset and bring the HWS back to normal state 1173a29ec470SShaoyun Liu */ 1174a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev) 1175a29ec470SShaoyun Liu { 1176a29ec470SShaoyun Liu if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) { 1177a29ec470SShaoyun Liu pr_err("HWS is not enabled"); 1178a29ec470SShaoyun Liu return -EINVAL; 1179a29ec470SShaoyun Liu } 1180a29ec470SShaoyun Liu 11814f942aaeSOak Zeng return dqm_debugfs_hang_hws(dev->dqm); 1182a29ec470SShaoyun Liu } 1183a29ec470SShaoyun Liu 1184a29ec470SShaoyun Liu #endif 1185