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" 290db54b24SYong Zhao #include "cwsr_trap_handler.h" 3064d1c3a4SFelix Kuehling #include "kfd_iommu.h" 315b87245fSAmber Lin #include "amdgpu_amdkfd.h" 322c2b0d88SMukul Joshi #include "kfd_smi_events.h" 334a488a7aSOded Gabbay 3419f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768 35e42051d2SShaoyun Liu 36e42051d2SShaoyun Liu /* 37e42051d2SShaoyun Liu * kfd_locked is used to lock the kfd driver during suspend or reset 38e42051d2SShaoyun Liu * once locked, kfd driver will stop any further GPU execution. 39e42051d2SShaoyun Liu * create process (open) will return -EAGAIN. 40e42051d2SShaoyun Liu */ 41e42051d2SShaoyun Liu static atomic_t kfd_locked = ATOMIC_INIT(0); 4219f6d2a6SOded Gabbay 43a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK 44e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v7_kfd2kgd; 45a3e520a2SAlex Deucher #endif 46e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v8_kfd2kgd; 47e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v9_kfd2kgd; 48e392c887SYong Zhao extern const struct kfd2kgd_calls arcturus_kfd2kgd; 49e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v10_kfd2kgd; 503a2f0c81SYong Zhao extern const struct kfd2kgd_calls gfx_v10_3_kfd2kgd; 51e392c887SYong Zhao 52e392c887SYong Zhao static const struct kfd2kgd_calls *kfd2kgd_funcs[] = { 53e392c887SYong Zhao #ifdef KFD_SUPPORT_IOMMU_V2 54a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK 55e392c887SYong Zhao [CHIP_KAVERI] = &gfx_v7_kfd2kgd, 56a3e520a2SAlex Deucher #endif 57e392c887SYong Zhao [CHIP_CARRIZO] = &gfx_v8_kfd2kgd, 58e392c887SYong Zhao [CHIP_RAVEN] = &gfx_v9_kfd2kgd, 59e392c887SYong Zhao #endif 60a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK 61e392c887SYong Zhao [CHIP_HAWAII] = &gfx_v7_kfd2kgd, 62a3e520a2SAlex Deucher #endif 63e392c887SYong Zhao [CHIP_TONGA] = &gfx_v8_kfd2kgd, 64e392c887SYong Zhao [CHIP_FIJI] = &gfx_v8_kfd2kgd, 65e392c887SYong Zhao [CHIP_POLARIS10] = &gfx_v8_kfd2kgd, 66e392c887SYong Zhao [CHIP_POLARIS11] = &gfx_v8_kfd2kgd, 67e392c887SYong Zhao [CHIP_POLARIS12] = &gfx_v8_kfd2kgd, 68e392c887SYong Zhao [CHIP_VEGAM] = &gfx_v8_kfd2kgd, 69e392c887SYong Zhao [CHIP_VEGA10] = &gfx_v9_kfd2kgd, 70e392c887SYong Zhao [CHIP_VEGA12] = &gfx_v9_kfd2kgd, 71e392c887SYong Zhao [CHIP_VEGA20] = &gfx_v9_kfd2kgd, 72e392c887SYong Zhao [CHIP_RENOIR] = &gfx_v9_kfd2kgd, 73e392c887SYong Zhao [CHIP_ARCTURUS] = &arcturus_kfd2kgd, 74e392c887SYong Zhao [CHIP_NAVI10] = &gfx_v10_kfd2kgd, 75e392c887SYong Zhao [CHIP_NAVI12] = &gfx_v10_kfd2kgd, 76e392c887SYong Zhao [CHIP_NAVI14] = &gfx_v10_kfd2kgd, 773a2f0c81SYong Zhao [CHIP_SIENNA_CICHLID] = &gfx_v10_3_kfd2kgd, 7809759e13SChengming Gui [CHIP_NAVY_FLOUNDER] = &gfx_v10_3_kfd2kgd, 793a5e715dSHuang Rui [CHIP_VANGOGH] = &gfx_v10_3_kfd2kgd, 808f72ce64SChengming Gui [CHIP_DIMGREY_CAVEFISH] = &gfx_v10_3_kfd2kgd, 81e392c887SYong Zhao }; 82e392c887SYong Zhao 8364d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2 844a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = { 850da7558cSBen Goz .asic_family = CHIP_KAVERI, 86c181159aSYong Zhao .asic_name = "kaveri", 870da7558cSBen Goz .max_pasid_bits = 16, 88992839adSYair Shachar /* max num of queues for KV.TODO should be a dynamic value */ 89992839adSYair Shachar .max_no_of_hqd = 24, 90ada2b29cSFelix Kuehling .doorbell_size = 4, 910da7558cSBen Goz .ih_ring_entry_size = 4 * sizeof(uint32_t), 92f3a39818SAndrew Lewycky .event_interrupt_class = &event_interrupt_class_cik, 93fbeb661bSYair Shachar .num_of_watch_points = 4, 94373d7080SFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 95373d7080SFelix Kuehling .supports_cwsr = false, 9664d1c3a4SFelix Kuehling .needs_iommu_device = true, 973ee2d00cSFelix Kuehling .needs_pci_atomics = false, 9898bb9222SYong Zhao .num_sdma_engines = 2, 991b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 100d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 1010da7558cSBen Goz }; 1020da7558cSBen Goz 1030da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = { 1040da7558cSBen Goz .asic_family = CHIP_CARRIZO, 105c181159aSYong Zhao .asic_name = "carrizo", 1064a488a7aSOded Gabbay .max_pasid_bits = 16, 107eaccd6e7SOded Gabbay /* max num of queues for CZ.TODO should be a dynamic value */ 108eaccd6e7SOded Gabbay .max_no_of_hqd = 24, 109ada2b29cSFelix Kuehling .doorbell_size = 4, 110b3f5e6b4SAndrew Lewycky .ih_ring_entry_size = 4 * sizeof(uint32_t), 111eaccd6e7SOded Gabbay .event_interrupt_class = &event_interrupt_class_cik, 112f7c826adSAlexey Skidanov .num_of_watch_points = 4, 113373d7080SFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 114373d7080SFelix Kuehling .supports_cwsr = true, 11564d1c3a4SFelix Kuehling .needs_iommu_device = true, 1163ee2d00cSFelix Kuehling .needs_pci_atomics = false, 11798bb9222SYong Zhao .num_sdma_engines = 2, 1181b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 119d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 1204a488a7aSOded Gabbay }; 1216127896fSHuang Rui #endif 1224d663df6SYong Zhao 1234d663df6SYong Zhao static const struct kfd_device_info raven_device_info = { 1244d663df6SYong Zhao .asic_family = CHIP_RAVEN, 125c181159aSYong Zhao .asic_name = "raven", 1264d663df6SYong Zhao .max_pasid_bits = 16, 1274d663df6SYong Zhao .max_no_of_hqd = 24, 1284d663df6SYong Zhao .doorbell_size = 8, 1294d663df6SYong Zhao .ih_ring_entry_size = 8 * sizeof(uint32_t), 1304d663df6SYong Zhao .event_interrupt_class = &event_interrupt_class_v9, 1314d663df6SYong Zhao .num_of_watch_points = 4, 1324d663df6SYong Zhao .mqd_size_aligned = MQD_SIZE_ALIGNED, 1334d663df6SYong Zhao .supports_cwsr = true, 1344d663df6SYong Zhao .needs_iommu_device = true, 1354d663df6SYong Zhao .needs_pci_atomics = true, 1364d663df6SYong Zhao .num_sdma_engines = 1, 1371b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 138d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 1394d663df6SYong Zhao }; 1404a488a7aSOded Gabbay 141a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = { 142a3084e6cSFelix Kuehling .asic_family = CHIP_HAWAII, 143c181159aSYong Zhao .asic_name = "hawaii", 144a3084e6cSFelix Kuehling .max_pasid_bits = 16, 145a3084e6cSFelix Kuehling /* max num of queues for KV.TODO should be a dynamic value */ 146a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 147ada2b29cSFelix Kuehling .doorbell_size = 4, 148a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 149a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 150a3084e6cSFelix Kuehling .num_of_watch_points = 4, 151a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 152a3084e6cSFelix Kuehling .supports_cwsr = false, 15364d1c3a4SFelix Kuehling .needs_iommu_device = false, 154a3084e6cSFelix Kuehling .needs_pci_atomics = false, 15598bb9222SYong Zhao .num_sdma_engines = 2, 1561b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 157d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 158a3084e6cSFelix Kuehling }; 159a3084e6cSFelix Kuehling 160a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = { 161a3084e6cSFelix Kuehling .asic_family = CHIP_TONGA, 162c181159aSYong Zhao .asic_name = "tonga", 163a3084e6cSFelix Kuehling .max_pasid_bits = 16, 164a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 165ada2b29cSFelix Kuehling .doorbell_size = 4, 166a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 167a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 168a3084e6cSFelix Kuehling .num_of_watch_points = 4, 169a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 170a3084e6cSFelix Kuehling .supports_cwsr = false, 17164d1c3a4SFelix Kuehling .needs_iommu_device = false, 172a3084e6cSFelix Kuehling .needs_pci_atomics = true, 17398bb9222SYong Zhao .num_sdma_engines = 2, 1741b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 175d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 176a3084e6cSFelix Kuehling }; 177a3084e6cSFelix Kuehling 178a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = { 179a3084e6cSFelix Kuehling .asic_family = CHIP_FIJI, 180c181159aSYong Zhao .asic_name = "fiji", 181a3084e6cSFelix Kuehling .max_pasid_bits = 16, 182a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 183ada2b29cSFelix Kuehling .doorbell_size = 4, 184a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 185a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 186a3084e6cSFelix Kuehling .num_of_watch_points = 4, 187a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 188a3084e6cSFelix Kuehling .supports_cwsr = true, 18964d1c3a4SFelix Kuehling .needs_iommu_device = false, 190a3084e6cSFelix Kuehling .needs_pci_atomics = true, 19198bb9222SYong Zhao .num_sdma_engines = 2, 1921b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 193d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 194a3084e6cSFelix Kuehling }; 195a3084e6cSFelix Kuehling 196a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = { 197a3084e6cSFelix Kuehling .asic_family = CHIP_FIJI, 198c181159aSYong Zhao .asic_name = "fiji", 199a3084e6cSFelix Kuehling .max_pasid_bits = 16, 200a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 201ada2b29cSFelix Kuehling .doorbell_size = 4, 202a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 203a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 204a3084e6cSFelix Kuehling .num_of_watch_points = 4, 205a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 206a3084e6cSFelix Kuehling .supports_cwsr = true, 20764d1c3a4SFelix Kuehling .needs_iommu_device = false, 208a3084e6cSFelix Kuehling .needs_pci_atomics = false, 20998bb9222SYong Zhao .num_sdma_engines = 2, 2101b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 211d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 212a3084e6cSFelix Kuehling }; 213a3084e6cSFelix Kuehling 214a3084e6cSFelix Kuehling 215a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = { 216a3084e6cSFelix Kuehling .asic_family = CHIP_POLARIS10, 217c181159aSYong Zhao .asic_name = "polaris10", 218a3084e6cSFelix Kuehling .max_pasid_bits = 16, 219a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 220ada2b29cSFelix Kuehling .doorbell_size = 4, 221a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 222a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 223a3084e6cSFelix Kuehling .num_of_watch_points = 4, 224a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 225a3084e6cSFelix Kuehling .supports_cwsr = true, 22664d1c3a4SFelix Kuehling .needs_iommu_device = false, 227a3084e6cSFelix Kuehling .needs_pci_atomics = true, 22898bb9222SYong Zhao .num_sdma_engines = 2, 2291b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 230d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 231a3084e6cSFelix Kuehling }; 232a3084e6cSFelix Kuehling 233a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = { 234a3084e6cSFelix Kuehling .asic_family = CHIP_POLARIS10, 235c181159aSYong Zhao .asic_name = "polaris10", 236a3084e6cSFelix Kuehling .max_pasid_bits = 16, 237a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 238ada2b29cSFelix Kuehling .doorbell_size = 4, 239a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 240a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 241a3084e6cSFelix Kuehling .num_of_watch_points = 4, 242a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 243a3084e6cSFelix Kuehling .supports_cwsr = true, 24464d1c3a4SFelix Kuehling .needs_iommu_device = false, 245a3084e6cSFelix Kuehling .needs_pci_atomics = false, 24698bb9222SYong Zhao .num_sdma_engines = 2, 2471b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 248d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 249a3084e6cSFelix Kuehling }; 250a3084e6cSFelix Kuehling 251a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = { 252a3084e6cSFelix Kuehling .asic_family = CHIP_POLARIS11, 253c181159aSYong Zhao .asic_name = "polaris11", 254a3084e6cSFelix Kuehling .max_pasid_bits = 16, 255a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 256ada2b29cSFelix Kuehling .doorbell_size = 4, 257a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 258a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 259a3084e6cSFelix Kuehling .num_of_watch_points = 4, 260a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 261a3084e6cSFelix Kuehling .supports_cwsr = true, 26264d1c3a4SFelix Kuehling .needs_iommu_device = false, 263a3084e6cSFelix Kuehling .needs_pci_atomics = true, 26498bb9222SYong Zhao .num_sdma_engines = 2, 2651b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 266d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 267a3084e6cSFelix Kuehling }; 268a3084e6cSFelix Kuehling 269846a44d7SGang Ba static const struct kfd_device_info polaris12_device_info = { 270846a44d7SGang Ba .asic_family = CHIP_POLARIS12, 271c181159aSYong Zhao .asic_name = "polaris12", 272846a44d7SGang Ba .max_pasid_bits = 16, 273846a44d7SGang Ba .max_no_of_hqd = 24, 274846a44d7SGang Ba .doorbell_size = 4, 275846a44d7SGang Ba .ih_ring_entry_size = 4 * sizeof(uint32_t), 276846a44d7SGang Ba .event_interrupt_class = &event_interrupt_class_cik, 277846a44d7SGang Ba .num_of_watch_points = 4, 278846a44d7SGang Ba .mqd_size_aligned = MQD_SIZE_ALIGNED, 279846a44d7SGang Ba .supports_cwsr = true, 280846a44d7SGang Ba .needs_iommu_device = false, 281846a44d7SGang Ba .needs_pci_atomics = true, 282846a44d7SGang Ba .num_sdma_engines = 2, 2831b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 284846a44d7SGang Ba .num_sdma_queues_per_engine = 2, 285846a44d7SGang Ba }; 286846a44d7SGang Ba 287ed81cd6eSKent Russell static const struct kfd_device_info vegam_device_info = { 288ed81cd6eSKent Russell .asic_family = CHIP_VEGAM, 289c181159aSYong Zhao .asic_name = "vegam", 290ed81cd6eSKent Russell .max_pasid_bits = 16, 291ed81cd6eSKent Russell .max_no_of_hqd = 24, 292ed81cd6eSKent Russell .doorbell_size = 4, 293ed81cd6eSKent Russell .ih_ring_entry_size = 4 * sizeof(uint32_t), 294ed81cd6eSKent Russell .event_interrupt_class = &event_interrupt_class_cik, 295ed81cd6eSKent Russell .num_of_watch_points = 4, 296ed81cd6eSKent Russell .mqd_size_aligned = MQD_SIZE_ALIGNED, 297ed81cd6eSKent Russell .supports_cwsr = true, 298ed81cd6eSKent Russell .needs_iommu_device = false, 299ed81cd6eSKent Russell .needs_pci_atomics = true, 300ed81cd6eSKent Russell .num_sdma_engines = 2, 301ed81cd6eSKent Russell .num_xgmi_sdma_engines = 0, 302a3084e6cSFelix Kuehling .num_sdma_queues_per_engine = 2, 303a3084e6cSFelix Kuehling }; 304a3084e6cSFelix Kuehling 305389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = { 306389056e5SFelix Kuehling .asic_family = CHIP_VEGA10, 307c181159aSYong Zhao .asic_name = "vega10", 308389056e5SFelix Kuehling .max_pasid_bits = 16, 309389056e5SFelix Kuehling .max_no_of_hqd = 24, 310389056e5SFelix Kuehling .doorbell_size = 8, 311389056e5SFelix Kuehling .ih_ring_entry_size = 8 * sizeof(uint32_t), 312389056e5SFelix Kuehling .event_interrupt_class = &event_interrupt_class_v9, 313389056e5SFelix Kuehling .num_of_watch_points = 4, 314389056e5SFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 315389056e5SFelix Kuehling .supports_cwsr = true, 316389056e5SFelix Kuehling .needs_iommu_device = false, 317389056e5SFelix Kuehling .needs_pci_atomics = false, 31898bb9222SYong Zhao .num_sdma_engines = 2, 3191b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 320d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 321389056e5SFelix Kuehling }; 322389056e5SFelix Kuehling 323389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_device_info = { 324389056e5SFelix Kuehling .asic_family = CHIP_VEGA10, 325c181159aSYong Zhao .asic_name = "vega10", 326389056e5SFelix Kuehling .max_pasid_bits = 16, 327389056e5SFelix Kuehling .max_no_of_hqd = 24, 328389056e5SFelix Kuehling .doorbell_size = 8, 329389056e5SFelix Kuehling .ih_ring_entry_size = 8 * sizeof(uint32_t), 330389056e5SFelix Kuehling .event_interrupt_class = &event_interrupt_class_v9, 331389056e5SFelix Kuehling .num_of_watch_points = 4, 332389056e5SFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 333389056e5SFelix Kuehling .supports_cwsr = true, 334389056e5SFelix Kuehling .needs_iommu_device = false, 335389056e5SFelix Kuehling .needs_pci_atomics = false, 33698bb9222SYong Zhao .num_sdma_engines = 2, 3371b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 338d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 339389056e5SFelix Kuehling }; 340389056e5SFelix Kuehling 341846a44d7SGang Ba static const struct kfd_device_info vega12_device_info = { 342846a44d7SGang Ba .asic_family = CHIP_VEGA12, 343c181159aSYong Zhao .asic_name = "vega12", 344846a44d7SGang Ba .max_pasid_bits = 16, 345846a44d7SGang Ba .max_no_of_hqd = 24, 346846a44d7SGang Ba .doorbell_size = 8, 347846a44d7SGang Ba .ih_ring_entry_size = 8 * sizeof(uint32_t), 348846a44d7SGang Ba .event_interrupt_class = &event_interrupt_class_v9, 349846a44d7SGang Ba .num_of_watch_points = 4, 350846a44d7SGang Ba .mqd_size_aligned = MQD_SIZE_ALIGNED, 351846a44d7SGang Ba .supports_cwsr = true, 352846a44d7SGang Ba .needs_iommu_device = false, 353846a44d7SGang Ba .needs_pci_atomics = false, 354846a44d7SGang Ba .num_sdma_engines = 2, 3551b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 356846a44d7SGang Ba .num_sdma_queues_per_engine = 2, 357846a44d7SGang Ba }; 358846a44d7SGang Ba 35922a3a294SShaoyun Liu static const struct kfd_device_info vega20_device_info = { 36022a3a294SShaoyun Liu .asic_family = CHIP_VEGA20, 361c181159aSYong Zhao .asic_name = "vega20", 36222a3a294SShaoyun Liu .max_pasid_bits = 16, 36322a3a294SShaoyun Liu .max_no_of_hqd = 24, 36422a3a294SShaoyun Liu .doorbell_size = 8, 36522a3a294SShaoyun Liu .ih_ring_entry_size = 8 * sizeof(uint32_t), 36622a3a294SShaoyun Liu .event_interrupt_class = &event_interrupt_class_v9, 36722a3a294SShaoyun Liu .num_of_watch_points = 4, 36822a3a294SShaoyun Liu .mqd_size_aligned = MQD_SIZE_ALIGNED, 36922a3a294SShaoyun Liu .supports_cwsr = true, 37022a3a294SShaoyun Liu .needs_iommu_device = false, 371006a0b3dSShaoyun Liu .needs_pci_atomics = false, 37222a3a294SShaoyun Liu .num_sdma_engines = 2, 3731b4670f6SOak Zeng .num_xgmi_sdma_engines = 0, 37422a3a294SShaoyun Liu .num_sdma_queues_per_engine = 8, 37522a3a294SShaoyun Liu }; 37622a3a294SShaoyun Liu 37749adcf8aSYong Zhao static const struct kfd_device_info arcturus_device_info = { 37849adcf8aSYong Zhao .asic_family = CHIP_ARCTURUS, 379c181159aSYong Zhao .asic_name = "arcturus", 38049adcf8aSYong Zhao .max_pasid_bits = 16, 38149adcf8aSYong Zhao .max_no_of_hqd = 24, 38249adcf8aSYong Zhao .doorbell_size = 8, 38349adcf8aSYong Zhao .ih_ring_entry_size = 8 * sizeof(uint32_t), 38449adcf8aSYong Zhao .event_interrupt_class = &event_interrupt_class_v9, 38549adcf8aSYong Zhao .num_of_watch_points = 4, 38649adcf8aSYong Zhao .mqd_size_aligned = MQD_SIZE_ALIGNED, 38749adcf8aSYong Zhao .supports_cwsr = true, 38849adcf8aSYong Zhao .needs_iommu_device = false, 38949adcf8aSYong Zhao .needs_pci_atomics = false, 390b6689cf7SOak Zeng .num_sdma_engines = 2, 391b6689cf7SOak Zeng .num_xgmi_sdma_engines = 6, 39249adcf8aSYong Zhao .num_sdma_queues_per_engine = 8, 39349adcf8aSYong Zhao }; 39449adcf8aSYong Zhao 3952b9c2211SHuang Rui static const struct kfd_device_info renoir_device_info = { 3962b9c2211SHuang Rui .asic_family = CHIP_RENOIR, 397acb9acbeSHuang Rui .asic_name = "renoir", 3982b9c2211SHuang Rui .max_pasid_bits = 16, 3992b9c2211SHuang Rui .max_no_of_hqd = 24, 4002b9c2211SHuang Rui .doorbell_size = 8, 4012b9c2211SHuang Rui .ih_ring_entry_size = 8 * sizeof(uint32_t), 4022b9c2211SHuang Rui .event_interrupt_class = &event_interrupt_class_v9, 4032b9c2211SHuang Rui .num_of_watch_points = 4, 4042b9c2211SHuang Rui .mqd_size_aligned = MQD_SIZE_ALIGNED, 4052b9c2211SHuang Rui .supports_cwsr = true, 4062b9c2211SHuang Rui .needs_iommu_device = false, 4072b9c2211SHuang Rui .needs_pci_atomics = false, 4082b9c2211SHuang Rui .num_sdma_engines = 1, 4092b9c2211SHuang Rui .num_xgmi_sdma_engines = 0, 4102b9c2211SHuang Rui .num_sdma_queues_per_engine = 2, 4112b9c2211SHuang Rui }; 4122b9c2211SHuang Rui 41314328aa5SPhilip Cox static const struct kfd_device_info navi10_device_info = { 41414328aa5SPhilip Cox .asic_family = CHIP_NAVI10, 415c181159aSYong Zhao .asic_name = "navi10", 41614328aa5SPhilip Cox .max_pasid_bits = 16, 41714328aa5SPhilip Cox .max_no_of_hqd = 24, 41814328aa5SPhilip Cox .doorbell_size = 8, 41914328aa5SPhilip Cox .ih_ring_entry_size = 8 * sizeof(uint32_t), 42014328aa5SPhilip Cox .event_interrupt_class = &event_interrupt_class_v9, 42114328aa5SPhilip Cox .num_of_watch_points = 4, 42214328aa5SPhilip Cox .mqd_size_aligned = MQD_SIZE_ALIGNED, 42314328aa5SPhilip Cox .needs_iommu_device = false, 42414328aa5SPhilip Cox .supports_cwsr = true, 425*6cc980e3SHarish Kasiviswanathan .needs_pci_atomics = true, 42614328aa5SPhilip Cox .num_sdma_engines = 2, 42714328aa5SPhilip Cox .num_xgmi_sdma_engines = 0, 42814328aa5SPhilip Cox .num_sdma_queues_per_engine = 8, 42914328aa5SPhilip Cox }; 43014328aa5SPhilip Cox 431b77fb9d8Sshaoyunl static const struct kfd_device_info navi12_device_info = { 4320e94b564Sshaoyunl .asic_family = CHIP_NAVI12, 433b77fb9d8Sshaoyunl .asic_name = "navi12", 434b77fb9d8Sshaoyunl .max_pasid_bits = 16, 435b77fb9d8Sshaoyunl .max_no_of_hqd = 24, 436b77fb9d8Sshaoyunl .doorbell_size = 8, 437b77fb9d8Sshaoyunl .ih_ring_entry_size = 8 * sizeof(uint32_t), 438b77fb9d8Sshaoyunl .event_interrupt_class = &event_interrupt_class_v9, 439b77fb9d8Sshaoyunl .num_of_watch_points = 4, 440b77fb9d8Sshaoyunl .mqd_size_aligned = MQD_SIZE_ALIGNED, 441b77fb9d8Sshaoyunl .needs_iommu_device = false, 442b77fb9d8Sshaoyunl .supports_cwsr = true, 443*6cc980e3SHarish Kasiviswanathan .needs_pci_atomics = true, 444b77fb9d8Sshaoyunl .num_sdma_engines = 2, 445b77fb9d8Sshaoyunl .num_xgmi_sdma_engines = 0, 446b77fb9d8Sshaoyunl .num_sdma_queues_per_engine = 8, 447b77fb9d8Sshaoyunl }; 448b77fb9d8Sshaoyunl 4498099ae40SYong Zhao static const struct kfd_device_info navi14_device_info = { 4508099ae40SYong Zhao .asic_family = CHIP_NAVI14, 4518099ae40SYong Zhao .asic_name = "navi14", 4528099ae40SYong Zhao .max_pasid_bits = 16, 4538099ae40SYong Zhao .max_no_of_hqd = 24, 4548099ae40SYong Zhao .doorbell_size = 8, 4558099ae40SYong Zhao .ih_ring_entry_size = 8 * sizeof(uint32_t), 4568099ae40SYong Zhao .event_interrupt_class = &event_interrupt_class_v9, 4578099ae40SYong Zhao .num_of_watch_points = 4, 4588099ae40SYong Zhao .mqd_size_aligned = MQD_SIZE_ALIGNED, 4598099ae40SYong Zhao .needs_iommu_device = false, 4608099ae40SYong Zhao .supports_cwsr = true, 461*6cc980e3SHarish Kasiviswanathan .needs_pci_atomics = true, 4628099ae40SYong Zhao .num_sdma_engines = 2, 4638099ae40SYong Zhao .num_xgmi_sdma_engines = 0, 4648099ae40SYong Zhao .num_sdma_queues_per_engine = 8, 4658099ae40SYong Zhao }; 4668099ae40SYong Zhao 4673a2f0c81SYong Zhao static const struct kfd_device_info sienna_cichlid_device_info = { 4683a2f0c81SYong Zhao .asic_family = CHIP_SIENNA_CICHLID, 4693a2f0c81SYong Zhao .asic_name = "sienna_cichlid", 4703a2f0c81SYong Zhao .max_pasid_bits = 16, 4713a2f0c81SYong Zhao .max_no_of_hqd = 24, 4723a2f0c81SYong Zhao .doorbell_size = 8, 4733a2f0c81SYong Zhao .ih_ring_entry_size = 8 * sizeof(uint32_t), 4743a2f0c81SYong Zhao .event_interrupt_class = &event_interrupt_class_v9, 4753a2f0c81SYong Zhao .num_of_watch_points = 4, 4763a2f0c81SYong Zhao .mqd_size_aligned = MQD_SIZE_ALIGNED, 4773a2f0c81SYong Zhao .needs_iommu_device = false, 4783a2f0c81SYong Zhao .supports_cwsr = true, 479*6cc980e3SHarish Kasiviswanathan .needs_pci_atomics = true, 4803a2f0c81SYong Zhao .num_sdma_engines = 4, 4813a2f0c81SYong Zhao .num_xgmi_sdma_engines = 0, 4823a2f0c81SYong Zhao .num_sdma_queues_per_engine = 8, 4833a2f0c81SYong Zhao }; 4843a2f0c81SYong Zhao 485de89b2e4SChengming Gui static const struct kfd_device_info navy_flounder_device_info = { 486de89b2e4SChengming Gui .asic_family = CHIP_NAVY_FLOUNDER, 487de89b2e4SChengming Gui .asic_name = "navy_flounder", 488de89b2e4SChengming Gui .max_pasid_bits = 16, 489de89b2e4SChengming Gui .max_no_of_hqd = 24, 490de89b2e4SChengming Gui .doorbell_size = 8, 491de89b2e4SChengming Gui .ih_ring_entry_size = 8 * sizeof(uint32_t), 492de89b2e4SChengming Gui .event_interrupt_class = &event_interrupt_class_v9, 493de89b2e4SChengming Gui .num_of_watch_points = 4, 494de89b2e4SChengming Gui .mqd_size_aligned = MQD_SIZE_ALIGNED, 495de89b2e4SChengming Gui .needs_iommu_device = false, 496de89b2e4SChengming Gui .supports_cwsr = true, 497*6cc980e3SHarish Kasiviswanathan .needs_pci_atomics = true, 498de89b2e4SChengming Gui .num_sdma_engines = 2, 499de89b2e4SChengming Gui .num_xgmi_sdma_engines = 0, 500de89b2e4SChengming Gui .num_sdma_queues_per_engine = 8, 501de89b2e4SChengming Gui }; 502de89b2e4SChengming Gui 5033a5e715dSHuang Rui static const struct kfd_device_info vangogh_device_info = { 5043a5e715dSHuang Rui .asic_family = CHIP_VANGOGH, 5053a5e715dSHuang Rui .asic_name = "vangogh", 5063a5e715dSHuang Rui .max_pasid_bits = 16, 5073a5e715dSHuang Rui .max_no_of_hqd = 24, 5083a5e715dSHuang Rui .doorbell_size = 8, 5093a5e715dSHuang Rui .ih_ring_entry_size = 8 * sizeof(uint32_t), 5103a5e715dSHuang Rui .event_interrupt_class = &event_interrupt_class_v9, 5113a5e715dSHuang Rui .num_of_watch_points = 4, 5123a5e715dSHuang Rui .mqd_size_aligned = MQD_SIZE_ALIGNED, 5133a5e715dSHuang Rui .needs_iommu_device = false, 5143a5e715dSHuang Rui .supports_cwsr = true, 5153a5e715dSHuang Rui .needs_pci_atomics = false, 5163a5e715dSHuang Rui .num_sdma_engines = 1, 5173a5e715dSHuang Rui .num_xgmi_sdma_engines = 0, 5183a5e715dSHuang Rui .num_sdma_queues_per_engine = 2, 5193a5e715dSHuang Rui }; 5203a5e715dSHuang Rui 521eb5a34d4SChengming Gui static const struct kfd_device_info dimgrey_cavefish_device_info = { 522eb5a34d4SChengming Gui .asic_family = CHIP_DIMGREY_CAVEFISH, 523eb5a34d4SChengming Gui .asic_name = "dimgrey_cavefish", 524eb5a34d4SChengming Gui .max_pasid_bits = 16, 525eb5a34d4SChengming Gui .max_no_of_hqd = 24, 526eb5a34d4SChengming Gui .doorbell_size = 8, 527eb5a34d4SChengming Gui .ih_ring_entry_size = 8 * sizeof(uint32_t), 528eb5a34d4SChengming Gui .event_interrupt_class = &event_interrupt_class_v9, 529eb5a34d4SChengming Gui .num_of_watch_points = 4, 530eb5a34d4SChengming Gui .mqd_size_aligned = MQD_SIZE_ALIGNED, 531eb5a34d4SChengming Gui .needs_iommu_device = false, 532eb5a34d4SChengming Gui .supports_cwsr = true, 533*6cc980e3SHarish Kasiviswanathan .needs_pci_atomics = true, 534eb5a34d4SChengming Gui .num_sdma_engines = 2, 535eb5a34d4SChengming Gui .num_xgmi_sdma_engines = 0, 536eb5a34d4SChengming Gui .num_sdma_queues_per_engine = 8, 537eb5a34d4SChengming Gui }; 538eb5a34d4SChengming Gui 539eb5a34d4SChengming Gui 540050091abSYong Zhao /* For each entry, [0] is regular and [1] is virtualisation device. */ 541050091abSYong Zhao static const struct kfd_device_info *kfd_supported_devices[][2] = { 54295a5bd1bSYong Zhao #ifdef KFD_SUPPORT_IOMMU_V2 543050091abSYong Zhao [CHIP_KAVERI] = {&kaveri_device_info, NULL}, 54495a5bd1bSYong Zhao [CHIP_CARRIZO] = {&carrizo_device_info, NULL}, 54595a5bd1bSYong Zhao #endif 5462b3bbf23SYueHaibing [CHIP_RAVEN] = {&raven_device_info, NULL}, 547050091abSYong Zhao [CHIP_HAWAII] = {&hawaii_device_info, NULL}, 548050091abSYong Zhao [CHIP_TONGA] = {&tonga_device_info, NULL}, 549050091abSYong Zhao [CHIP_FIJI] = {&fiji_device_info, &fiji_vf_device_info}, 550050091abSYong Zhao [CHIP_POLARIS10] = {&polaris10_device_info, &polaris10_vf_device_info}, 551050091abSYong Zhao [CHIP_POLARIS11] = {&polaris11_device_info, NULL}, 552050091abSYong Zhao [CHIP_POLARIS12] = {&polaris12_device_info, NULL}, 553050091abSYong Zhao [CHIP_VEGAM] = {&vegam_device_info, NULL}, 554050091abSYong Zhao [CHIP_VEGA10] = {&vega10_device_info, &vega10_vf_device_info}, 555050091abSYong Zhao [CHIP_VEGA12] = {&vega12_device_info, NULL}, 556050091abSYong Zhao [CHIP_VEGA20] = {&vega20_device_info, NULL}, 5572b9c2211SHuang Rui [CHIP_RENOIR] = {&renoir_device_info, NULL}, 558050091abSYong Zhao [CHIP_ARCTURUS] = {&arcturus_device_info, &arcturus_device_info}, 559050091abSYong Zhao [CHIP_NAVI10] = {&navi10_device_info, NULL}, 560b77fb9d8Sshaoyunl [CHIP_NAVI12] = {&navi12_device_info, &navi12_device_info}, 5618099ae40SYong Zhao [CHIP_NAVI14] = {&navi14_device_info, NULL}, 562adab4dadSshaoyunl [CHIP_SIENNA_CICHLID] = {&sienna_cichlid_device_info, &sienna_cichlid_device_info}, 563de89b2e4SChengming Gui [CHIP_NAVY_FLOUNDER] = {&navy_flounder_device_info, &navy_flounder_device_info}, 5643a5e715dSHuang Rui [CHIP_VANGOGH] = {&vangogh_device_info, NULL}, 565eb5a34d4SChengming Gui [CHIP_DIMGREY_CAVEFISH] = {&dimgrey_cavefish_device_info, &dimgrey_cavefish_device_info}, 5664a488a7aSOded Gabbay }; 5674a488a7aSOded Gabbay 5686e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 5696e81090bSOded Gabbay unsigned int chunk_size); 5706e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd); 5716e81090bSOded Gabbay 572b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd); 573b8935a7cSYong Zhao 574cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, 575e392c887SYong Zhao struct pci_dev *pdev, unsigned int asic_type, bool vf) 5764a488a7aSOded Gabbay { 5774a488a7aSOded Gabbay struct kfd_dev *kfd; 578050091abSYong Zhao const struct kfd_device_info *device_info; 579e392c887SYong Zhao const struct kfd2kgd_calls *f2g; 580050091abSYong Zhao 581e392c887SYong Zhao if (asic_type >= sizeof(kfd_supported_devices) / (sizeof(void *) * 2) 582e392c887SYong Zhao || asic_type >= sizeof(kfd2kgd_funcs) / sizeof(void *)) { 583050091abSYong Zhao dev_err(kfd_device, "asic_type %d out of range\n", asic_type); 584050091abSYong Zhao return NULL; /* asic_type out of range */ 585050091abSYong Zhao } 586050091abSYong Zhao 587050091abSYong Zhao device_info = kfd_supported_devices[asic_type][vf]; 588e392c887SYong Zhao f2g = kfd2kgd_funcs[asic_type]; 5894a488a7aSOded Gabbay 590aa5e899dSDan Carpenter if (!device_info || !f2g) { 591050091abSYong Zhao dev_err(kfd_device, "%s %s not supported in kfd\n", 592050091abSYong Zhao amdgpu_asic_name[asic_type], vf ? "VF" : ""); 5934a488a7aSOded Gabbay return NULL; 5944ebc7182SYong Zhao } 5954a488a7aSOded Gabbay 596d35f00d8SEric Huang kfd = kzalloc(sizeof(*kfd), GFP_KERNEL); 597d35f00d8SEric Huang if (!kfd) 598d35f00d8SEric Huang return NULL; 599d35f00d8SEric Huang 6006106dce9Swelu /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps. 6016106dce9Swelu * 32 and 64-bit requests are possible and must be 6026106dce9Swelu * supported. 6033ee2d00cSFelix Kuehling */ 604aabf3a95SJack Xiao kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kgd); 605aabf3a95SJack Xiao if (device_info->needs_pci_atomics && 606aabf3a95SJack Xiao !kfd->pci_atomic_requested) { 6073ee2d00cSFelix Kuehling dev_info(kfd_device, 6086106dce9Swelu "skipped device %x:%x, PCI rejects atomics\n", 6093ee2d00cSFelix Kuehling pdev->vendor, pdev->device); 610d35f00d8SEric Huang kfree(kfd); 6113ee2d00cSFelix Kuehling return NULL; 612aabf3a95SJack Xiao } 6134a488a7aSOded Gabbay 6144a488a7aSOded Gabbay kfd->kgd = kgd; 6154a488a7aSOded Gabbay kfd->device_info = device_info; 6164a488a7aSOded Gabbay kfd->pdev = pdev; 61719f6d2a6SOded Gabbay kfd->init_complete = false; 618cea405b1SXihan Zhang kfd->kfd2kgd = f2g; 61943d8107fSHarish Kasiviswanathan atomic_set(&kfd->compute_profile, 0); 620cea405b1SXihan Zhang 621cea405b1SXihan Zhang mutex_init(&kfd->doorbell_mutex); 622cea405b1SXihan Zhang memset(&kfd->doorbell_available_index, 0, 623cea405b1SXihan Zhang sizeof(kfd->doorbell_available_index)); 6244a488a7aSOded Gabbay 6259b54d201SEric Huang atomic_set(&kfd->sram_ecc_flag, 0); 6269b54d201SEric Huang 62759d7115dSMukul Joshi ida_init(&kfd->doorbell_ida); 62859d7115dSMukul Joshi 6294a488a7aSOded Gabbay return kfd; 6304a488a7aSOded Gabbay } 6314a488a7aSOded Gabbay 632373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd) 633373d7080SFelix Kuehling { 634373d7080SFelix Kuehling if (cwsr_enable && kfd->device_info->supports_cwsr) { 6353e76c239SFelix Kuehling if (kfd->device_info->asic_family < CHIP_VEGA10) { 636373d7080SFelix Kuehling BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE); 637373d7080SFelix Kuehling kfd->cwsr_isa = cwsr_trap_gfx8_hex; 638373d7080SFelix Kuehling kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex); 6393baa24f0SOak Zeng } else if (kfd->device_info->asic_family == CHIP_ARCTURUS) { 6403baa24f0SOak Zeng BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE); 6413baa24f0SOak Zeng kfd->cwsr_isa = cwsr_trap_arcturus_hex; 6423baa24f0SOak Zeng kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex); 64314328aa5SPhilip Cox } else if (kfd->device_info->asic_family < CHIP_NAVI10) { 6443e76c239SFelix Kuehling BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE); 6453e76c239SFelix Kuehling kfd->cwsr_isa = cwsr_trap_gfx9_hex; 6463e76c239SFelix Kuehling kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex); 64780b6cfedSJay Cornwall } else if (kfd->device_info->asic_family < CHIP_SIENNA_CICHLID) { 64880b6cfedSJay Cornwall BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE); 64980b6cfedSJay Cornwall kfd->cwsr_isa = cwsr_trap_nv1x_hex; 65080b6cfedSJay Cornwall kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex); 65114328aa5SPhilip Cox } else { 65214328aa5SPhilip Cox BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE); 65314328aa5SPhilip Cox kfd->cwsr_isa = cwsr_trap_gfx10_hex; 65414328aa5SPhilip Cox kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex); 6553e76c239SFelix Kuehling } 6563e76c239SFelix Kuehling 657373d7080SFelix Kuehling kfd->cwsr_enabled = true; 658373d7080SFelix Kuehling } 659373d7080SFelix Kuehling } 660373d7080SFelix Kuehling 66129633d0eSJoseph Greathouse static int kfd_gws_init(struct kfd_dev *kfd) 66229633d0eSJoseph Greathouse { 66329633d0eSJoseph Greathouse int ret = 0; 66429633d0eSJoseph Greathouse 66529633d0eSJoseph Greathouse if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) 66629633d0eSJoseph Greathouse return 0; 66729633d0eSJoseph Greathouse 66829633d0eSJoseph Greathouse if (hws_gws_support 669fea7d919SJoseph Greathouse || (kfd->device_info->asic_family == CHIP_VEGA10 670fea7d919SJoseph Greathouse && kfd->mec2_fw_version >= 0x81b3) 671fea7d919SJoseph Greathouse || (kfd->device_info->asic_family >= CHIP_VEGA12 67229633d0eSJoseph Greathouse && kfd->device_info->asic_family <= CHIP_RAVEN 673fea7d919SJoseph Greathouse && kfd->mec2_fw_version >= 0x1b3) 674fea7d919SJoseph Greathouse || (kfd->device_info->asic_family == CHIP_ARCTURUS 675fea7d919SJoseph Greathouse && kfd->mec2_fw_version >= 0x30)) 67629633d0eSJoseph Greathouse ret = amdgpu_amdkfd_alloc_gws(kfd->kgd, 67729633d0eSJoseph Greathouse amdgpu_amdkfd_get_num_gws(kfd->kgd), &kfd->gws); 67829633d0eSJoseph Greathouse 67929633d0eSJoseph Greathouse return ret; 68029633d0eSJoseph Greathouse } 68129633d0eSJoseph Greathouse 682938a0650SAmber Lin static void kfd_smi_init(struct kfd_dev *dev) { 683938a0650SAmber Lin INIT_LIST_HEAD(&dev->smi_clients); 684938a0650SAmber Lin spin_lock_init(&dev->smi_lock); 685938a0650SAmber Lin } 686938a0650SAmber Lin 6874a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd, 6883a0c3423SHarish Kasiviswanathan struct drm_device *ddev, 6894a488a7aSOded Gabbay const struct kgd2kfd_shared_resources *gpu_resources) 6904a488a7aSOded Gabbay { 69119f6d2a6SOded Gabbay unsigned int size; 69219f6d2a6SOded Gabbay 6933a0c3423SHarish Kasiviswanathan kfd->ddev = ddev; 6940da8b10eSAmber Lin kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd, 6955ade6c9cSFelix Kuehling KGD_ENGINE_MEC1); 69629633d0eSJoseph Greathouse kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd, 69729633d0eSJoseph Greathouse KGD_ENGINE_MEC2); 6980da8b10eSAmber Lin kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd, 6995ade6c9cSFelix Kuehling KGD_ENGINE_SDMA1); 7004a488a7aSOded Gabbay kfd->shared_resources = *gpu_resources; 7014a488a7aSOded Gabbay 70244008d7aSYong Zhao kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1; 70344008d7aSYong Zhao kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1; 70444008d7aSYong Zhao kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd 70544008d7aSYong Zhao - kfd->vm_info.first_vmid_kfd + 1; 70644008d7aSYong Zhao 707a99c6d4fSFelix Kuehling /* Verify module parameters regarding mapped process number*/ 708a99c6d4fSFelix Kuehling if ((hws_max_conc_proc < 0) 709a99c6d4fSFelix Kuehling || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) { 710a99c6d4fSFelix Kuehling dev_err(kfd_device, 711a99c6d4fSFelix Kuehling "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n", 712a99c6d4fSFelix Kuehling hws_max_conc_proc, kfd->vm_info.vmid_num_kfd, 713a99c6d4fSFelix Kuehling kfd->vm_info.vmid_num_kfd); 714a99c6d4fSFelix Kuehling kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd; 715a99c6d4fSFelix Kuehling } else 716a99c6d4fSFelix Kuehling kfd->max_proc_per_quantum = hws_max_conc_proc; 717a99c6d4fSFelix Kuehling 71819f6d2a6SOded Gabbay /* calculate max size of mqds needed for queues */ 719b8cbab04SOded Gabbay size = max_num_of_queues_per_device * 72019f6d2a6SOded Gabbay kfd->device_info->mqd_size_aligned; 72119f6d2a6SOded Gabbay 722e18e794eSOded Gabbay /* 723e18e794eSOded Gabbay * calculate max size of runlist packet. 724e18e794eSOded Gabbay * There can be only 2 packets at once 725e18e794eSOded Gabbay */ 726507968ddSFelix Kuehling size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) + 727507968ddSFelix Kuehling max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues) 728507968ddSFelix Kuehling + sizeof(struct pm4_mes_runlist)) * 2; 729e18e794eSOded Gabbay 730e18e794eSOded Gabbay /* Add size of HIQ & DIQ */ 731e18e794eSOded Gabbay size += KFD_KERNEL_QUEUE_SIZE * 2; 732e18e794eSOded Gabbay 733e18e794eSOded Gabbay /* add another 512KB for all other allocations on gart (HPD, fences) */ 73419f6d2a6SOded Gabbay size += 512 * 1024; 73519f6d2a6SOded Gabbay 7367cd52c91SAmber Lin if (amdgpu_amdkfd_alloc_gtt_mem( 737cea405b1SXihan Zhang kfd->kgd, size, &kfd->gtt_mem, 73815426dbbSYong Zhao &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr, 73915426dbbSYong Zhao false)) { 74079775b62SKent Russell dev_err(kfd_device, "Could not allocate %d bytes\n", size); 741e09d4fc8SOak Zeng goto alloc_gtt_mem_failure; 74219f6d2a6SOded Gabbay } 74319f6d2a6SOded Gabbay 74479775b62SKent Russell dev_info(kfd_device, "Allocated %d bytes on gart\n", size); 745e18e794eSOded Gabbay 74673a1da0bSOded Gabbay /* Initialize GTT sa with 512 byte chunk size */ 74773a1da0bSOded Gabbay if (kfd_gtt_sa_init(kfd, size, 512) != 0) { 74879775b62SKent Russell dev_err(kfd_device, "Error initializing gtt sub-allocator\n"); 74973a1da0bSOded Gabbay goto kfd_gtt_sa_init_error; 75073a1da0bSOded Gabbay } 75173a1da0bSOded Gabbay 752735df2baSFelix Kuehling if (kfd_doorbell_init(kfd)) { 753735df2baSFelix Kuehling dev_err(kfd_device, 754735df2baSFelix Kuehling "Error initializing doorbell aperture\n"); 755735df2baSFelix Kuehling goto kfd_doorbell_error; 756735df2baSFelix Kuehling } 75719f6d2a6SOded Gabbay 758332f6e1eSFelix Kuehling kfd->hive_id = amdgpu_amdkfd_get_hive_id(kfd->kgd); 7590c1690e3SShaoyun Liu 7609b498efaSAlex Deucher kfd->noretry = amdgpu_amdkfd_get_noretry(kfd->kgd); 7619b498efaSAlex Deucher 7622249d558SAndrew Lewycky if (kfd_interrupt_init(kfd)) { 76379775b62SKent Russell dev_err(kfd_device, "Error initializing interrupts\n"); 7642249d558SAndrew Lewycky goto kfd_interrupt_error; 7652249d558SAndrew Lewycky } 7662249d558SAndrew Lewycky 76764c7f8cfSBen Goz kfd->dqm = device_queue_manager_init(kfd); 76864c7f8cfSBen Goz if (!kfd->dqm) { 76979775b62SKent Russell dev_err(kfd_device, "Error initializing queue manager\n"); 77064c7f8cfSBen Goz goto device_queue_manager_error; 77164c7f8cfSBen Goz } 77264c7f8cfSBen Goz 77329633d0eSJoseph Greathouse /* If supported on this device, allocate global GWS that is shared 77429633d0eSJoseph Greathouse * by all KFD processes 77529633d0eSJoseph Greathouse */ 77629633d0eSJoseph Greathouse if (kfd_gws_init(kfd)) { 77729633d0eSJoseph Greathouse dev_err(kfd_device, "Could not allocate %d gws\n", 77829633d0eSJoseph Greathouse amdgpu_amdkfd_get_num_gws(kfd->kgd)); 77929633d0eSJoseph Greathouse goto gws_error; 78029633d0eSJoseph Greathouse } 78129633d0eSJoseph Greathouse 7826127896fSHuang Rui /* If CRAT is broken, won't set iommu enabled */ 7836127896fSHuang Rui kfd_double_confirm_iommu_support(kfd); 7846127896fSHuang Rui 78564d1c3a4SFelix Kuehling if (kfd_iommu_device_init(kfd)) { 78664d1c3a4SFelix Kuehling dev_err(kfd_device, "Error initializing iommuv2\n"); 78764d1c3a4SFelix Kuehling goto device_iommu_error; 78864c7f8cfSBen Goz } 78964c7f8cfSBen Goz 790373d7080SFelix Kuehling kfd_cwsr_init(kfd); 791373d7080SFelix Kuehling 792b8935a7cSYong Zhao if (kfd_resume(kfd)) 793b8935a7cSYong Zhao goto kfd_resume_error; 794b8935a7cSYong Zhao 795fbeb661bSYair Shachar kfd->dbgmgr = NULL; 796fbeb661bSYair Shachar 797465ab9e0SOak Zeng if (kfd_topology_add_device(kfd)) { 798465ab9e0SOak Zeng dev_err(kfd_device, "Error adding device to topology\n"); 799465ab9e0SOak Zeng goto kfd_topology_add_device_error; 800465ab9e0SOak Zeng } 801465ab9e0SOak Zeng 802938a0650SAmber Lin kfd_smi_init(kfd); 803938a0650SAmber Lin 8044a488a7aSOded Gabbay kfd->init_complete = true; 80579775b62SKent Russell dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor, 8064a488a7aSOded Gabbay kfd->pdev->device); 8074a488a7aSOded Gabbay 80879775b62SKent Russell pr_debug("Starting kfd with the following scheduling policy %d\n", 809d146c5a7SFelix Kuehling kfd->dqm->sched_policy); 81064c7f8cfSBen Goz 81119f6d2a6SOded Gabbay goto out; 81219f6d2a6SOded Gabbay 813465ab9e0SOak Zeng kfd_topology_add_device_error: 814b8935a7cSYong Zhao kfd_resume_error: 81564d1c3a4SFelix Kuehling device_iommu_error: 81629633d0eSJoseph Greathouse gws_error: 81764c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 81864c7f8cfSBen Goz device_queue_manager_error: 8192249d558SAndrew Lewycky kfd_interrupt_exit(kfd); 8202249d558SAndrew Lewycky kfd_interrupt_error: 821735df2baSFelix Kuehling kfd_doorbell_fini(kfd); 822735df2baSFelix Kuehling kfd_doorbell_error: 82373a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd); 82473a1da0bSOded Gabbay kfd_gtt_sa_init_error: 8257cd52c91SAmber Lin amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem); 826e09d4fc8SOak Zeng alloc_gtt_mem_failure: 82729633d0eSJoseph Greathouse if (kfd->gws) 828e09d4fc8SOak Zeng amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws); 82919f6d2a6SOded Gabbay dev_err(kfd_device, 83079775b62SKent Russell "device %x:%x NOT added due to errors\n", 83119f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 83219f6d2a6SOded Gabbay out: 83319f6d2a6SOded Gabbay return kfd->init_complete; 8344a488a7aSOded Gabbay } 8354a488a7aSOded Gabbay 8364a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd) 8374a488a7aSOded Gabbay { 838b17f068aSOded Gabbay if (kfd->init_complete) { 8399593f4d6SRajneesh Bhardwaj kgd2kfd_suspend(kfd, false); 84064c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 8412249d558SAndrew Lewycky kfd_interrupt_exit(kfd); 84219f6d2a6SOded Gabbay kfd_topology_remove_device(kfd); 843735df2baSFelix Kuehling kfd_doorbell_fini(kfd); 84459d7115dSMukul Joshi ida_destroy(&kfd->doorbell_ida); 84573a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd); 8467cd52c91SAmber Lin amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem); 84729633d0eSJoseph Greathouse if (kfd->gws) 848e09d4fc8SOak Zeng amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws); 849b17f068aSOded Gabbay } 8505b5c4e40SEvgeny Pinchuk 8514a488a7aSOded Gabbay kfree(kfd); 8524a488a7aSOded Gabbay } 8534a488a7aSOded Gabbay 854e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd) 855e3b7a967SShaoyun Liu { 856e42051d2SShaoyun Liu if (!kfd->init_complete) 857e42051d2SShaoyun Liu return 0; 85809c34e8dSFelix Kuehling 85955977744SMukul Joshi kfd_smi_event_update_gpu_reset(kfd, false); 86055977744SMukul Joshi 86109c34e8dSFelix Kuehling kfd->dqm->ops.pre_reset(kfd->dqm); 86209c34e8dSFelix Kuehling 8639593f4d6SRajneesh Bhardwaj kgd2kfd_suspend(kfd, false); 864e42051d2SShaoyun Liu 865e42051d2SShaoyun Liu kfd_signal_reset_event(kfd); 866e3b7a967SShaoyun Liu return 0; 867e3b7a967SShaoyun Liu } 868e3b7a967SShaoyun Liu 869e42051d2SShaoyun Liu /* 870e42051d2SShaoyun Liu * Fix me. KFD won't be able to resume existing process for now. 871e42051d2SShaoyun Liu * We will keep all existing process in a evicted state and 872e42051d2SShaoyun Liu * wait the process to be terminated. 873e42051d2SShaoyun Liu */ 874e42051d2SShaoyun Liu 875e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd) 876e3b7a967SShaoyun Liu { 877a1bd079fSyu kuai int ret; 878e42051d2SShaoyun Liu 879e42051d2SShaoyun Liu if (!kfd->init_complete) 880e3b7a967SShaoyun Liu return 0; 881e42051d2SShaoyun Liu 882e42051d2SShaoyun Liu ret = kfd_resume(kfd); 883e42051d2SShaoyun Liu if (ret) 884e42051d2SShaoyun Liu return ret; 885a1bd079fSyu kuai atomic_dec(&kfd_locked); 8869b54d201SEric Huang 8879b54d201SEric Huang atomic_set(&kfd->sram_ecc_flag, 0); 8889b54d201SEric Huang 88955977744SMukul Joshi kfd_smi_event_update_gpu_reset(kfd, true); 89055977744SMukul Joshi 891e42051d2SShaoyun Liu return 0; 892e42051d2SShaoyun Liu } 893e42051d2SShaoyun Liu 894e42051d2SShaoyun Liu bool kfd_is_locked(void) 895e42051d2SShaoyun Liu { 896e42051d2SShaoyun Liu return (atomic_read(&kfd_locked) > 0); 897e3b7a967SShaoyun Liu } 898e3b7a967SShaoyun Liu 8999593f4d6SRajneesh Bhardwaj void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm) 9004a488a7aSOded Gabbay { 901733fa1f7SYong Zhao if (!kfd->init_complete) 902733fa1f7SYong Zhao return; 903733fa1f7SYong Zhao 9049593f4d6SRajneesh Bhardwaj /* for runtime suspend, skip locking kfd */ 9059593f4d6SRajneesh Bhardwaj if (!run_pm) { 90626103436SFelix Kuehling /* For first KFD device suspend all the KFD processes */ 907e42051d2SShaoyun Liu if (atomic_inc_return(&kfd_locked) == 1) 90826103436SFelix Kuehling kfd_suspend_all_processes(); 9099593f4d6SRajneesh Bhardwaj } 91026103436SFelix Kuehling 91145c9a5e4SOded Gabbay kfd->dqm->ops.stop(kfd->dqm); 91264d1c3a4SFelix Kuehling kfd_iommu_suspend(kfd); 9134a488a7aSOded Gabbay } 9144a488a7aSOded Gabbay 9159593f4d6SRajneesh Bhardwaj int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm) 9164a488a7aSOded Gabbay { 91726103436SFelix Kuehling int ret, count; 91826103436SFelix Kuehling 919b8935a7cSYong Zhao if (!kfd->init_complete) 920b8935a7cSYong Zhao return 0; 921b17f068aSOded Gabbay 92226103436SFelix Kuehling ret = kfd_resume(kfd); 92326103436SFelix Kuehling if (ret) 92426103436SFelix Kuehling return ret; 925b17f068aSOded Gabbay 9269593f4d6SRajneesh Bhardwaj /* for runtime resume, skip unlocking kfd */ 9279593f4d6SRajneesh Bhardwaj if (!run_pm) { 928e42051d2SShaoyun Liu count = atomic_dec_return(&kfd_locked); 92926103436SFelix Kuehling WARN_ONCE(count < 0, "KFD suspend / resume ref. error"); 93026103436SFelix Kuehling if (count == 0) 93126103436SFelix Kuehling ret = kfd_resume_all_processes(); 9329593f4d6SRajneesh Bhardwaj } 93326103436SFelix Kuehling 93426103436SFelix Kuehling return ret; 9354ebc7182SYong Zhao } 9364ebc7182SYong Zhao 937b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd) 938b8935a7cSYong Zhao { 939b8935a7cSYong Zhao int err = 0; 940b8935a7cSYong Zhao 94164d1c3a4SFelix Kuehling err = kfd_iommu_resume(kfd); 94264d1c3a4SFelix Kuehling if (err) { 94364d1c3a4SFelix Kuehling dev_err(kfd_device, 94464d1c3a4SFelix Kuehling "Failed to resume IOMMU for device %x:%x\n", 94564d1c3a4SFelix Kuehling kfd->pdev->vendor, kfd->pdev->device); 94664d1c3a4SFelix Kuehling return err; 94764d1c3a4SFelix Kuehling } 948733fa1f7SYong Zhao 949b8935a7cSYong Zhao err = kfd->dqm->ops.start(kfd->dqm); 950b8935a7cSYong Zhao if (err) { 951b8935a7cSYong Zhao dev_err(kfd_device, 952b8935a7cSYong Zhao "Error starting queue manager for device %x:%x\n", 953b8935a7cSYong Zhao kfd->pdev->vendor, kfd->pdev->device); 954b8935a7cSYong Zhao goto dqm_start_error; 955b17f068aSOded Gabbay } 956b17f068aSOded Gabbay 957b8935a7cSYong Zhao return err; 958b8935a7cSYong Zhao 959b8935a7cSYong Zhao dqm_start_error: 96064d1c3a4SFelix Kuehling kfd_iommu_suspend(kfd); 961b8935a7cSYong Zhao return err; 9624a488a7aSOded Gabbay } 9634a488a7aSOded Gabbay 964b3eca59dSPhilip Yang static inline void kfd_queue_work(struct workqueue_struct *wq, 965b3eca59dSPhilip Yang struct work_struct *work) 966b3eca59dSPhilip Yang { 967b3eca59dSPhilip Yang int cpu, new_cpu; 968b3eca59dSPhilip Yang 969b3eca59dSPhilip Yang cpu = new_cpu = smp_processor_id(); 970b3eca59dSPhilip Yang do { 971b3eca59dSPhilip Yang new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids; 972b3eca59dSPhilip Yang if (cpu_to_node(new_cpu) == numa_node_id()) 973b3eca59dSPhilip Yang break; 974b3eca59dSPhilip Yang } while (cpu != new_cpu); 975b3eca59dSPhilip Yang 976b3eca59dSPhilip Yang queue_work_on(new_cpu, wq, work); 977b3eca59dSPhilip Yang } 978b3eca59dSPhilip Yang 979b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */ 980b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry) 9814a488a7aSOded Gabbay { 98258e69886SLan Xiao uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE]; 98358e69886SLan Xiao bool is_patched = false; 9842383a767SChristian König unsigned long flags; 98558e69886SLan Xiao 9862249d558SAndrew Lewycky if (!kfd->init_complete) 9872249d558SAndrew Lewycky return; 9882249d558SAndrew Lewycky 98958e69886SLan Xiao if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) { 99058e69886SLan Xiao dev_err_once(kfd_device, "Ring entry too small\n"); 99158e69886SLan Xiao return; 99258e69886SLan Xiao } 99358e69886SLan Xiao 9942383a767SChristian König spin_lock_irqsave(&kfd->interrupt_lock, flags); 9952249d558SAndrew Lewycky 9962249d558SAndrew Lewycky if (kfd->interrupts_active 99758e69886SLan Xiao && interrupt_is_wanted(kfd, ih_ring_entry, 99858e69886SLan Xiao patched_ihre, &is_patched) 99958e69886SLan Xiao && enqueue_ih_ring_entry(kfd, 100058e69886SLan Xiao is_patched ? patched_ihre : ih_ring_entry)) 1001b3eca59dSPhilip Yang kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work); 10022249d558SAndrew Lewycky 10032383a767SChristian König spin_unlock_irqrestore(&kfd->interrupt_lock, flags); 10044a488a7aSOded Gabbay } 10056e81090bSOded Gabbay 10066b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm) 10076b95e797SFelix Kuehling { 10086b95e797SFelix Kuehling struct kfd_process *p; 10096b95e797SFelix Kuehling int r; 10106b95e797SFelix Kuehling 10116b95e797SFelix Kuehling /* Because we are called from arbitrary context (workqueue) as opposed 10126b95e797SFelix Kuehling * to process context, kfd_process could attempt to exit while we are 10136b95e797SFelix Kuehling * running so the lookup function increments the process ref count. 10146b95e797SFelix Kuehling */ 10156b95e797SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 10166b95e797SFelix Kuehling if (!p) 10176b95e797SFelix Kuehling return -ESRCH; 10186b95e797SFelix Kuehling 1019b2057956SFelix Kuehling WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid); 10206b95e797SFelix Kuehling r = kfd_process_evict_queues(p); 10216b95e797SFelix Kuehling 10226b95e797SFelix Kuehling kfd_unref_process(p); 10236b95e797SFelix Kuehling return r; 10246b95e797SFelix Kuehling } 10256b95e797SFelix Kuehling 10266b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm) 10276b95e797SFelix Kuehling { 10286b95e797SFelix Kuehling struct kfd_process *p; 10296b95e797SFelix Kuehling int r; 10306b95e797SFelix Kuehling 10316b95e797SFelix Kuehling /* Because we are called from arbitrary context (workqueue) as opposed 10326b95e797SFelix Kuehling * to process context, kfd_process could attempt to exit while we are 10336b95e797SFelix Kuehling * running so the lookup function increments the process ref count. 10346b95e797SFelix Kuehling */ 10356b95e797SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 10366b95e797SFelix Kuehling if (!p) 10376b95e797SFelix Kuehling return -ESRCH; 10386b95e797SFelix Kuehling 10396b95e797SFelix Kuehling r = kfd_process_restore_queues(p); 10406b95e797SFelix Kuehling 10416b95e797SFelix Kuehling kfd_unref_process(p); 10426b95e797SFelix Kuehling return r; 10436b95e797SFelix Kuehling } 10446b95e797SFelix Kuehling 104526103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will 104626103436SFelix Kuehling * prepare for safe eviction of KFD BOs that belong to the specified 104726103436SFelix Kuehling * process. 104826103436SFelix Kuehling * 104926103436SFelix Kuehling * @mm: mm_struct that identifies the specified KFD process 105026103436SFelix Kuehling * @fence: eviction fence attached to KFD process BOs 105126103436SFelix Kuehling * 105226103436SFelix Kuehling */ 105326103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm, 105426103436SFelix Kuehling struct dma_fence *fence) 105526103436SFelix Kuehling { 105626103436SFelix Kuehling struct kfd_process *p; 105726103436SFelix Kuehling unsigned long active_time; 105826103436SFelix Kuehling unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS); 105926103436SFelix Kuehling 106026103436SFelix Kuehling if (!fence) 106126103436SFelix Kuehling return -EINVAL; 106226103436SFelix Kuehling 106326103436SFelix Kuehling if (dma_fence_is_signaled(fence)) 106426103436SFelix Kuehling return 0; 106526103436SFelix Kuehling 106626103436SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 106726103436SFelix Kuehling if (!p) 106826103436SFelix Kuehling return -ENODEV; 106926103436SFelix Kuehling 107026103436SFelix Kuehling if (fence->seqno == p->last_eviction_seqno) 107126103436SFelix Kuehling goto out; 107226103436SFelix Kuehling 107326103436SFelix Kuehling p->last_eviction_seqno = fence->seqno; 107426103436SFelix Kuehling 107526103436SFelix Kuehling /* Avoid KFD process starvation. Wait for at least 107626103436SFelix Kuehling * PROCESS_ACTIVE_TIME_MS before evicting the process again 107726103436SFelix Kuehling */ 107826103436SFelix Kuehling active_time = get_jiffies_64() - p->last_restore_timestamp; 107926103436SFelix Kuehling if (delay_jiffies > active_time) 108026103436SFelix Kuehling delay_jiffies -= active_time; 108126103436SFelix Kuehling else 108226103436SFelix Kuehling delay_jiffies = 0; 108326103436SFelix Kuehling 108426103436SFelix Kuehling /* During process initialization eviction_work.dwork is initialized 108526103436SFelix Kuehling * to kfd_evict_bo_worker 108626103436SFelix Kuehling */ 1087b2057956SFelix Kuehling WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies", 1088b2057956SFelix Kuehling p->lead_thread->pid, delay_jiffies); 108926103436SFelix Kuehling schedule_delayed_work(&p->eviction_work, delay_jiffies); 109026103436SFelix Kuehling out: 109126103436SFelix Kuehling kfd_unref_process(p); 109226103436SFelix Kuehling return 0; 109326103436SFelix Kuehling } 109426103436SFelix Kuehling 10956e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 10966e81090bSOded Gabbay unsigned int chunk_size) 10976e81090bSOded Gabbay { 10988625ff9cSFelix Kuehling unsigned int num_of_longs; 10996e81090bSOded Gabbay 110032fa8219SFelix Kuehling if (WARN_ON(buf_size < chunk_size)) 110132fa8219SFelix Kuehling return -EINVAL; 110232fa8219SFelix Kuehling if (WARN_ON(buf_size == 0)) 110332fa8219SFelix Kuehling return -EINVAL; 110432fa8219SFelix Kuehling if (WARN_ON(chunk_size == 0)) 110532fa8219SFelix Kuehling return -EINVAL; 11066e81090bSOded Gabbay 11076e81090bSOded Gabbay kfd->gtt_sa_chunk_size = chunk_size; 11086e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks = buf_size / chunk_size; 11096e81090bSOded Gabbay 11108625ff9cSFelix Kuehling num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) / 11118625ff9cSFelix Kuehling BITS_PER_LONG; 11126e81090bSOded Gabbay 11138625ff9cSFelix Kuehling kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL); 11146e81090bSOded Gabbay 11156e81090bSOded Gabbay if (!kfd->gtt_sa_bitmap) 11166e81090bSOded Gabbay return -ENOMEM; 11176e81090bSOded Gabbay 111879775b62SKent Russell pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n", 11196e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap); 11206e81090bSOded Gabbay 11216e81090bSOded Gabbay mutex_init(&kfd->gtt_sa_lock); 11226e81090bSOded Gabbay 11236e81090bSOded Gabbay return 0; 11246e81090bSOded Gabbay 11256e81090bSOded Gabbay } 11266e81090bSOded Gabbay 11276e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd) 11286e81090bSOded Gabbay { 11296e81090bSOded Gabbay mutex_destroy(&kfd->gtt_sa_lock); 11306e81090bSOded Gabbay kfree(kfd->gtt_sa_bitmap); 11316e81090bSOded Gabbay } 11326e81090bSOded Gabbay 11336e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr, 11346e81090bSOded Gabbay unsigned int bit_num, 11356e81090bSOded Gabbay unsigned int chunk_size) 11366e81090bSOded Gabbay { 11376e81090bSOded Gabbay return start_addr + bit_num * chunk_size; 11386e81090bSOded Gabbay } 11396e81090bSOded Gabbay 11406e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr, 11416e81090bSOded Gabbay unsigned int bit_num, 11426e81090bSOded Gabbay unsigned int chunk_size) 11436e81090bSOded Gabbay { 11446e81090bSOded Gabbay return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size); 11456e81090bSOded Gabbay } 11466e81090bSOded Gabbay 11476e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size, 11486e81090bSOded Gabbay struct kfd_mem_obj **mem_obj) 11496e81090bSOded Gabbay { 11506e81090bSOded Gabbay unsigned int found, start_search, cur_size; 11516e81090bSOded Gabbay 11526e81090bSOded Gabbay if (size == 0) 11536e81090bSOded Gabbay return -EINVAL; 11546e81090bSOded Gabbay 11556e81090bSOded Gabbay if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size) 11566e81090bSOded Gabbay return -ENOMEM; 11576e81090bSOded Gabbay 11581cd106ecSFelix Kuehling *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL); 11591cd106ecSFelix Kuehling if (!(*mem_obj)) 11606e81090bSOded Gabbay return -ENOMEM; 11616e81090bSOded Gabbay 116279775b62SKent Russell pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size); 11636e81090bSOded Gabbay 11646e81090bSOded Gabbay start_search = 0; 11656e81090bSOded Gabbay 11666e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock); 11676e81090bSOded Gabbay 11686e81090bSOded Gabbay kfd_gtt_restart_search: 11696e81090bSOded Gabbay /* Find the first chunk that is free */ 11706e81090bSOded Gabbay found = find_next_zero_bit(kfd->gtt_sa_bitmap, 11716e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, 11726e81090bSOded Gabbay start_search); 11736e81090bSOded Gabbay 117479775b62SKent Russell pr_debug("Found = %d\n", found); 11756e81090bSOded Gabbay 11766e81090bSOded Gabbay /* If there wasn't any free chunk, bail out */ 11776e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks) 11786e81090bSOded Gabbay goto kfd_gtt_no_free_chunk; 11796e81090bSOded Gabbay 11806e81090bSOded Gabbay /* Update fields of mem_obj */ 11816e81090bSOded Gabbay (*mem_obj)->range_start = found; 11826e81090bSOded Gabbay (*mem_obj)->range_end = found; 11836e81090bSOded Gabbay (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr( 11846e81090bSOded Gabbay kfd->gtt_start_gpu_addr, 11856e81090bSOded Gabbay found, 11866e81090bSOded Gabbay kfd->gtt_sa_chunk_size); 11876e81090bSOded Gabbay (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr( 11886e81090bSOded Gabbay kfd->gtt_start_cpu_ptr, 11896e81090bSOded Gabbay found, 11906e81090bSOded Gabbay kfd->gtt_sa_chunk_size); 11916e81090bSOded Gabbay 119279775b62SKent Russell pr_debug("gpu_addr = %p, cpu_addr = %p\n", 11936e81090bSOded Gabbay (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr); 11946e81090bSOded Gabbay 11956e81090bSOded Gabbay /* If we need only one chunk, mark it as allocated and get out */ 11966e81090bSOded Gabbay if (size <= kfd->gtt_sa_chunk_size) { 119779775b62SKent Russell pr_debug("Single bit\n"); 11986e81090bSOded Gabbay set_bit(found, kfd->gtt_sa_bitmap); 11996e81090bSOded Gabbay goto kfd_gtt_out; 12006e81090bSOded Gabbay } 12016e81090bSOded Gabbay 12026e81090bSOded Gabbay /* Otherwise, try to see if we have enough contiguous chunks */ 12036e81090bSOded Gabbay cur_size = size - kfd->gtt_sa_chunk_size; 12046e81090bSOded Gabbay do { 12056e81090bSOded Gabbay (*mem_obj)->range_end = 12066e81090bSOded Gabbay find_next_zero_bit(kfd->gtt_sa_bitmap, 12076e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, ++found); 12086e81090bSOded Gabbay /* 12096e81090bSOded Gabbay * If next free chunk is not contiguous than we need to 12106e81090bSOded Gabbay * restart our search from the last free chunk we found (which 12116e81090bSOded Gabbay * wasn't contiguous to the previous ones 12126e81090bSOded Gabbay */ 12136e81090bSOded Gabbay if ((*mem_obj)->range_end != found) { 12146e81090bSOded Gabbay start_search = found; 12156e81090bSOded Gabbay goto kfd_gtt_restart_search; 12166e81090bSOded Gabbay } 12176e81090bSOded Gabbay 12186e81090bSOded Gabbay /* 12196e81090bSOded Gabbay * If we reached end of buffer, bail out with error 12206e81090bSOded Gabbay */ 12216e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks) 12226e81090bSOded Gabbay goto kfd_gtt_no_free_chunk; 12236e81090bSOded Gabbay 12246e81090bSOded Gabbay /* Check if we don't need another chunk */ 12256e81090bSOded Gabbay if (cur_size <= kfd->gtt_sa_chunk_size) 12266e81090bSOded Gabbay cur_size = 0; 12276e81090bSOded Gabbay else 12286e81090bSOded Gabbay cur_size -= kfd->gtt_sa_chunk_size; 12296e81090bSOded Gabbay 12306e81090bSOded Gabbay } while (cur_size > 0); 12316e81090bSOded Gabbay 123279775b62SKent Russell pr_debug("range_start = %d, range_end = %d\n", 12336e81090bSOded Gabbay (*mem_obj)->range_start, (*mem_obj)->range_end); 12346e81090bSOded Gabbay 12356e81090bSOded Gabbay /* Mark the chunks as allocated */ 12366e81090bSOded Gabbay for (found = (*mem_obj)->range_start; 12376e81090bSOded Gabbay found <= (*mem_obj)->range_end; 12386e81090bSOded Gabbay found++) 12396e81090bSOded Gabbay set_bit(found, kfd->gtt_sa_bitmap); 12406e81090bSOded Gabbay 12416e81090bSOded Gabbay kfd_gtt_out: 12426e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 12436e81090bSOded Gabbay return 0; 12446e81090bSOded Gabbay 12456e81090bSOded Gabbay kfd_gtt_no_free_chunk: 12463148a6a0SJack Zhang pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj); 12476e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 12483148a6a0SJack Zhang kfree(*mem_obj); 12496e81090bSOded Gabbay return -ENOMEM; 12506e81090bSOded Gabbay } 12516e81090bSOded Gabbay 12526e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj) 12536e81090bSOded Gabbay { 12546e81090bSOded Gabbay unsigned int bit; 12556e81090bSOded Gabbay 12569216ed29SOded Gabbay /* Act like kfree when trying to free a NULL object */ 12579216ed29SOded Gabbay if (!mem_obj) 12589216ed29SOded Gabbay return 0; 12596e81090bSOded Gabbay 126079775b62SKent Russell pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n", 12616e81090bSOded Gabbay mem_obj, mem_obj->range_start, mem_obj->range_end); 12626e81090bSOded Gabbay 12636e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock); 12646e81090bSOded Gabbay 12656e81090bSOded Gabbay /* Mark the chunks as free */ 12666e81090bSOded Gabbay for (bit = mem_obj->range_start; 12676e81090bSOded Gabbay bit <= mem_obj->range_end; 12686e81090bSOded Gabbay bit++) 12696e81090bSOded Gabbay clear_bit(bit, kfd->gtt_sa_bitmap); 12706e81090bSOded Gabbay 12716e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 12726e81090bSOded Gabbay 12736e81090bSOded Gabbay kfree(mem_obj); 12746e81090bSOded Gabbay return 0; 12756e81090bSOded Gabbay } 1276a29ec470SShaoyun Liu 12779b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd) 12789b54d201SEric Huang { 12799b54d201SEric Huang if (kfd) 12809b54d201SEric Huang atomic_inc(&kfd->sram_ecc_flag); 12819b54d201SEric Huang } 12829b54d201SEric Huang 128343d8107fSHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd) 128443d8107fSHarish Kasiviswanathan { 128543d8107fSHarish Kasiviswanathan if (atomic_inc_return(&kfd->compute_profile) == 1) 128643d8107fSHarish Kasiviswanathan amdgpu_amdkfd_set_compute_idle(kfd->kgd, false); 128743d8107fSHarish Kasiviswanathan } 128843d8107fSHarish Kasiviswanathan 128943d8107fSHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd) 129043d8107fSHarish Kasiviswanathan { 129143d8107fSHarish Kasiviswanathan int count = atomic_dec_return(&kfd->compute_profile); 129243d8107fSHarish Kasiviswanathan 129343d8107fSHarish Kasiviswanathan if (count == 0) 129443d8107fSHarish Kasiviswanathan amdgpu_amdkfd_set_compute_idle(kfd->kgd, true); 129543d8107fSHarish Kasiviswanathan WARN_ONCE(count < 0, "Compute profile ref. count error"); 129643d8107fSHarish Kasiviswanathan } 129743d8107fSHarish Kasiviswanathan 12982c2b0d88SMukul Joshi void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint32_t throttle_bitmask) 12992c2b0d88SMukul Joshi { 13002c2b0d88SMukul Joshi if (kfd) 13012c2b0d88SMukul Joshi kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask); 13022c2b0d88SMukul Joshi } 13032c2b0d88SMukul Joshi 1304a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS) 1305a29ec470SShaoyun Liu 1306a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS 1307a29ec470SShaoyun Liu * which will trigger a GPU reset and bring the HWS back to normal state 1308a29ec470SShaoyun Liu */ 1309a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev) 1310a29ec470SShaoyun Liu { 1311a29ec470SShaoyun Liu int r = 0; 1312a29ec470SShaoyun Liu 1313a29ec470SShaoyun Liu if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) { 1314a29ec470SShaoyun Liu pr_err("HWS is not enabled"); 1315a29ec470SShaoyun Liu return -EINVAL; 1316a29ec470SShaoyun Liu } 1317a29ec470SShaoyun Liu 1318a29ec470SShaoyun Liu r = pm_debugfs_hang_hws(&dev->dqm->packets); 1319a29ec470SShaoyun Liu if (!r) 1320a29ec470SShaoyun Liu r = dqm_debugfs_execute_queues(dev->dqm); 1321a29ec470SShaoyun Liu 1322a29ec470SShaoyun Liu return r; 1323a29ec470SShaoyun Liu } 1324a29ec470SShaoyun Liu 1325a29ec470SShaoyun Liu #endif 1326