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" 314a488a7aSOded Gabbay 3219f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768 33e42051d2SShaoyun Liu 34e42051d2SShaoyun Liu /* 35e42051d2SShaoyun Liu * kfd_locked is used to lock the kfd driver during suspend or reset 36e42051d2SShaoyun Liu * once locked, kfd driver will stop any further GPU execution. 37e42051d2SShaoyun Liu * create process (open) will return -EAGAIN. 38e42051d2SShaoyun Liu */ 39e42051d2SShaoyun Liu static atomic_t kfd_locked = ATOMIC_INIT(0); 4019f6d2a6SOded Gabbay 4164d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2 424a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = { 430da7558cSBen Goz .asic_family = CHIP_KAVERI, 440da7558cSBen Goz .max_pasid_bits = 16, 45992839adSYair Shachar /* max num of queues for KV.TODO should be a dynamic value */ 46992839adSYair Shachar .max_no_of_hqd = 24, 47ada2b29cSFelix Kuehling .doorbell_size = 4, 480da7558cSBen Goz .ih_ring_entry_size = 4 * sizeof(uint32_t), 49f3a39818SAndrew Lewycky .event_interrupt_class = &event_interrupt_class_cik, 50fbeb661bSYair Shachar .num_of_watch_points = 4, 51373d7080SFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 52373d7080SFelix Kuehling .supports_cwsr = false, 5364d1c3a4SFelix Kuehling .needs_iommu_device = true, 543ee2d00cSFelix Kuehling .needs_pci_atomics = false, 5598bb9222SYong Zhao .num_sdma_engines = 2, 56d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 570da7558cSBen Goz }; 580da7558cSBen Goz 590da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = { 600da7558cSBen Goz .asic_family = CHIP_CARRIZO, 614a488a7aSOded Gabbay .max_pasid_bits = 16, 62eaccd6e7SOded Gabbay /* max num of queues for CZ.TODO should be a dynamic value */ 63eaccd6e7SOded Gabbay .max_no_of_hqd = 24, 64ada2b29cSFelix Kuehling .doorbell_size = 4, 65b3f5e6b4SAndrew Lewycky .ih_ring_entry_size = 4 * sizeof(uint32_t), 66eaccd6e7SOded Gabbay .event_interrupt_class = &event_interrupt_class_cik, 67f7c826adSAlexey Skidanov .num_of_watch_points = 4, 68373d7080SFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 69373d7080SFelix Kuehling .supports_cwsr = true, 7064d1c3a4SFelix Kuehling .needs_iommu_device = true, 713ee2d00cSFelix Kuehling .needs_pci_atomics = false, 7298bb9222SYong Zhao .num_sdma_engines = 2, 73d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 744a488a7aSOded Gabbay }; 754d663df6SYong Zhao 764d663df6SYong Zhao static const struct kfd_device_info raven_device_info = { 774d663df6SYong Zhao .asic_family = CHIP_RAVEN, 784d663df6SYong Zhao .max_pasid_bits = 16, 794d663df6SYong Zhao .max_no_of_hqd = 24, 804d663df6SYong Zhao .doorbell_size = 8, 814d663df6SYong Zhao .ih_ring_entry_size = 8 * sizeof(uint32_t), 824d663df6SYong Zhao .event_interrupt_class = &event_interrupt_class_v9, 834d663df6SYong Zhao .num_of_watch_points = 4, 844d663df6SYong Zhao .mqd_size_aligned = MQD_SIZE_ALIGNED, 854d663df6SYong Zhao .supports_cwsr = true, 864d663df6SYong Zhao .needs_iommu_device = true, 874d663df6SYong Zhao .needs_pci_atomics = true, 884d663df6SYong Zhao .num_sdma_engines = 1, 89d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 904d663df6SYong Zhao }; 9164d1c3a4SFelix Kuehling #endif 924a488a7aSOded Gabbay 93a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = { 94a3084e6cSFelix Kuehling .asic_family = CHIP_HAWAII, 95a3084e6cSFelix Kuehling .max_pasid_bits = 16, 96a3084e6cSFelix Kuehling /* max num of queues for KV.TODO should be a dynamic value */ 97a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 98ada2b29cSFelix Kuehling .doorbell_size = 4, 99a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 100a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 101a3084e6cSFelix Kuehling .num_of_watch_points = 4, 102a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 103a3084e6cSFelix Kuehling .supports_cwsr = false, 10464d1c3a4SFelix Kuehling .needs_iommu_device = false, 105a3084e6cSFelix Kuehling .needs_pci_atomics = false, 10698bb9222SYong Zhao .num_sdma_engines = 2, 107d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 108a3084e6cSFelix Kuehling }; 109a3084e6cSFelix Kuehling 110a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = { 111a3084e6cSFelix Kuehling .asic_family = CHIP_TONGA, 112a3084e6cSFelix Kuehling .max_pasid_bits = 16, 113a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 114ada2b29cSFelix Kuehling .doorbell_size = 4, 115a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 116a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 117a3084e6cSFelix Kuehling .num_of_watch_points = 4, 118a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 119a3084e6cSFelix Kuehling .supports_cwsr = false, 12064d1c3a4SFelix Kuehling .needs_iommu_device = false, 121a3084e6cSFelix Kuehling .needs_pci_atomics = true, 12298bb9222SYong Zhao .num_sdma_engines = 2, 123d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 124a3084e6cSFelix Kuehling }; 125a3084e6cSFelix Kuehling 126a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = { 127a3084e6cSFelix Kuehling .asic_family = CHIP_FIJI, 128a3084e6cSFelix Kuehling .max_pasid_bits = 16, 129a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 130ada2b29cSFelix Kuehling .doorbell_size = 4, 131a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 132a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 133a3084e6cSFelix Kuehling .num_of_watch_points = 4, 134a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 135a3084e6cSFelix Kuehling .supports_cwsr = true, 13664d1c3a4SFelix Kuehling .needs_iommu_device = false, 137a3084e6cSFelix Kuehling .needs_pci_atomics = true, 13898bb9222SYong Zhao .num_sdma_engines = 2, 139d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 140a3084e6cSFelix Kuehling }; 141a3084e6cSFelix Kuehling 142a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = { 143a3084e6cSFelix Kuehling .asic_family = CHIP_FIJI, 144a3084e6cSFelix Kuehling .max_pasid_bits = 16, 145a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 146ada2b29cSFelix Kuehling .doorbell_size = 4, 147a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 148a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 149a3084e6cSFelix Kuehling .num_of_watch_points = 4, 150a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 151a3084e6cSFelix Kuehling .supports_cwsr = true, 15264d1c3a4SFelix Kuehling .needs_iommu_device = false, 153a3084e6cSFelix Kuehling .needs_pci_atomics = false, 15498bb9222SYong Zhao .num_sdma_engines = 2, 155d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 156a3084e6cSFelix Kuehling }; 157a3084e6cSFelix Kuehling 158a3084e6cSFelix Kuehling 159a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = { 160a3084e6cSFelix Kuehling .asic_family = CHIP_POLARIS10, 161a3084e6cSFelix Kuehling .max_pasid_bits = 16, 162a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 163ada2b29cSFelix Kuehling .doorbell_size = 4, 164a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 165a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 166a3084e6cSFelix Kuehling .num_of_watch_points = 4, 167a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 168a3084e6cSFelix Kuehling .supports_cwsr = true, 16964d1c3a4SFelix Kuehling .needs_iommu_device = false, 170a3084e6cSFelix Kuehling .needs_pci_atomics = true, 17198bb9222SYong Zhao .num_sdma_engines = 2, 172d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 173a3084e6cSFelix Kuehling }; 174a3084e6cSFelix Kuehling 175a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = { 176a3084e6cSFelix Kuehling .asic_family = CHIP_POLARIS10, 177a3084e6cSFelix Kuehling .max_pasid_bits = 16, 178a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 179ada2b29cSFelix Kuehling .doorbell_size = 4, 180a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 181a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 182a3084e6cSFelix Kuehling .num_of_watch_points = 4, 183a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 184a3084e6cSFelix Kuehling .supports_cwsr = true, 18564d1c3a4SFelix Kuehling .needs_iommu_device = false, 186a3084e6cSFelix Kuehling .needs_pci_atomics = false, 18798bb9222SYong Zhao .num_sdma_engines = 2, 188d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 189a3084e6cSFelix Kuehling }; 190a3084e6cSFelix Kuehling 191a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = { 192a3084e6cSFelix Kuehling .asic_family = CHIP_POLARIS11, 193a3084e6cSFelix Kuehling .max_pasid_bits = 16, 194a3084e6cSFelix Kuehling .max_no_of_hqd = 24, 195ada2b29cSFelix Kuehling .doorbell_size = 4, 196a3084e6cSFelix Kuehling .ih_ring_entry_size = 4 * sizeof(uint32_t), 197a3084e6cSFelix Kuehling .event_interrupt_class = &event_interrupt_class_cik, 198a3084e6cSFelix Kuehling .num_of_watch_points = 4, 199a3084e6cSFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 200a3084e6cSFelix Kuehling .supports_cwsr = true, 20164d1c3a4SFelix Kuehling .needs_iommu_device = false, 202a3084e6cSFelix Kuehling .needs_pci_atomics = true, 20398bb9222SYong Zhao .num_sdma_engines = 2, 204d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 205a3084e6cSFelix Kuehling }; 206a3084e6cSFelix Kuehling 207389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = { 208389056e5SFelix Kuehling .asic_family = CHIP_VEGA10, 209389056e5SFelix Kuehling .max_pasid_bits = 16, 210389056e5SFelix Kuehling .max_no_of_hqd = 24, 211389056e5SFelix Kuehling .doorbell_size = 8, 212389056e5SFelix Kuehling .ih_ring_entry_size = 8 * sizeof(uint32_t), 213389056e5SFelix Kuehling .event_interrupt_class = &event_interrupt_class_v9, 214389056e5SFelix Kuehling .num_of_watch_points = 4, 215389056e5SFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 216389056e5SFelix Kuehling .supports_cwsr = true, 217389056e5SFelix Kuehling .needs_iommu_device = false, 218389056e5SFelix Kuehling .needs_pci_atomics = false, 21998bb9222SYong Zhao .num_sdma_engines = 2, 220d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 221389056e5SFelix Kuehling }; 222389056e5SFelix Kuehling 223389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_device_info = { 224389056e5SFelix Kuehling .asic_family = CHIP_VEGA10, 225389056e5SFelix Kuehling .max_pasid_bits = 16, 226389056e5SFelix Kuehling .max_no_of_hqd = 24, 227389056e5SFelix Kuehling .doorbell_size = 8, 228389056e5SFelix Kuehling .ih_ring_entry_size = 8 * sizeof(uint32_t), 229389056e5SFelix Kuehling .event_interrupt_class = &event_interrupt_class_v9, 230389056e5SFelix Kuehling .num_of_watch_points = 4, 231389056e5SFelix Kuehling .mqd_size_aligned = MQD_SIZE_ALIGNED, 232389056e5SFelix Kuehling .supports_cwsr = true, 233389056e5SFelix Kuehling .needs_iommu_device = false, 234389056e5SFelix Kuehling .needs_pci_atomics = false, 23598bb9222SYong Zhao .num_sdma_engines = 2, 236d5094189SShaoyun Liu .num_sdma_queues_per_engine = 2, 237389056e5SFelix Kuehling }; 238389056e5SFelix Kuehling 23922a3a294SShaoyun Liu static const struct kfd_device_info vega20_device_info = { 24022a3a294SShaoyun Liu .asic_family = CHIP_VEGA20, 24122a3a294SShaoyun Liu .max_pasid_bits = 16, 24222a3a294SShaoyun Liu .max_no_of_hqd = 24, 24322a3a294SShaoyun Liu .doorbell_size = 8, 24422a3a294SShaoyun Liu .ih_ring_entry_size = 8 * sizeof(uint32_t), 24522a3a294SShaoyun Liu .event_interrupt_class = &event_interrupt_class_v9, 24622a3a294SShaoyun Liu .num_of_watch_points = 4, 24722a3a294SShaoyun Liu .mqd_size_aligned = MQD_SIZE_ALIGNED, 24822a3a294SShaoyun Liu .supports_cwsr = true, 24922a3a294SShaoyun Liu .needs_iommu_device = false, 250006a0b3dSShaoyun Liu .needs_pci_atomics = false, 25122a3a294SShaoyun Liu .num_sdma_engines = 2, 25222a3a294SShaoyun Liu .num_sdma_queues_per_engine = 8, 25322a3a294SShaoyun Liu }; 25422a3a294SShaoyun Liu 2554a488a7aSOded Gabbay struct kfd_deviceid { 2564a488a7aSOded Gabbay unsigned short did; 2574a488a7aSOded Gabbay const struct kfd_device_info *device_info; 2584a488a7aSOded Gabbay }; 2594a488a7aSOded Gabbay 2604a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = { 26164d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2 2624a488a7aSOded Gabbay { 0x1304, &kaveri_device_info }, /* Kaveri */ 2634a488a7aSOded Gabbay { 0x1305, &kaveri_device_info }, /* Kaveri */ 2644a488a7aSOded Gabbay { 0x1306, &kaveri_device_info }, /* Kaveri */ 2654a488a7aSOded Gabbay { 0x1307, &kaveri_device_info }, /* Kaveri */ 2664a488a7aSOded Gabbay { 0x1309, &kaveri_device_info }, /* Kaveri */ 2674a488a7aSOded Gabbay { 0x130A, &kaveri_device_info }, /* Kaveri */ 2684a488a7aSOded Gabbay { 0x130B, &kaveri_device_info }, /* Kaveri */ 2694a488a7aSOded Gabbay { 0x130C, &kaveri_device_info }, /* Kaveri */ 2704a488a7aSOded Gabbay { 0x130D, &kaveri_device_info }, /* Kaveri */ 2714a488a7aSOded Gabbay { 0x130E, &kaveri_device_info }, /* Kaveri */ 2724a488a7aSOded Gabbay { 0x130F, &kaveri_device_info }, /* Kaveri */ 2734a488a7aSOded Gabbay { 0x1310, &kaveri_device_info }, /* Kaveri */ 2744a488a7aSOded Gabbay { 0x1311, &kaveri_device_info }, /* Kaveri */ 2754a488a7aSOded Gabbay { 0x1312, &kaveri_device_info }, /* Kaveri */ 2764a488a7aSOded Gabbay { 0x1313, &kaveri_device_info }, /* Kaveri */ 2774a488a7aSOded Gabbay { 0x1315, &kaveri_device_info }, /* Kaveri */ 2784a488a7aSOded Gabbay { 0x1316, &kaveri_device_info }, /* Kaveri */ 2794a488a7aSOded Gabbay { 0x1317, &kaveri_device_info }, /* Kaveri */ 2804a488a7aSOded Gabbay { 0x1318, &kaveri_device_info }, /* Kaveri */ 2814a488a7aSOded Gabbay { 0x131B, &kaveri_device_info }, /* Kaveri */ 2824a488a7aSOded Gabbay { 0x131C, &kaveri_device_info }, /* Kaveri */ 283123576d1SBen Goz { 0x131D, &kaveri_device_info }, /* Kaveri */ 284123576d1SBen Goz { 0x9870, &carrizo_device_info }, /* Carrizo */ 285123576d1SBen Goz { 0x9874, &carrizo_device_info }, /* Carrizo */ 286123576d1SBen Goz { 0x9875, &carrizo_device_info }, /* Carrizo */ 287123576d1SBen Goz { 0x9876, &carrizo_device_info }, /* Carrizo */ 288a3084e6cSFelix Kuehling { 0x9877, &carrizo_device_info }, /* Carrizo */ 2894d663df6SYong Zhao { 0x15DD, &raven_device_info }, /* Raven */ 29064d1c3a4SFelix Kuehling #endif 291a3084e6cSFelix Kuehling { 0x67A0, &hawaii_device_info }, /* Hawaii */ 292a3084e6cSFelix Kuehling { 0x67A1, &hawaii_device_info }, /* Hawaii */ 293a3084e6cSFelix Kuehling { 0x67A2, &hawaii_device_info }, /* Hawaii */ 294a3084e6cSFelix Kuehling { 0x67A8, &hawaii_device_info }, /* Hawaii */ 295a3084e6cSFelix Kuehling { 0x67A9, &hawaii_device_info }, /* Hawaii */ 296a3084e6cSFelix Kuehling { 0x67AA, &hawaii_device_info }, /* Hawaii */ 297a3084e6cSFelix Kuehling { 0x67B0, &hawaii_device_info }, /* Hawaii */ 298a3084e6cSFelix Kuehling { 0x67B1, &hawaii_device_info }, /* Hawaii */ 299a3084e6cSFelix Kuehling { 0x67B8, &hawaii_device_info }, /* Hawaii */ 300a3084e6cSFelix Kuehling { 0x67B9, &hawaii_device_info }, /* Hawaii */ 301a3084e6cSFelix Kuehling { 0x67BA, &hawaii_device_info }, /* Hawaii */ 302a3084e6cSFelix Kuehling { 0x67BE, &hawaii_device_info }, /* Hawaii */ 303a3084e6cSFelix Kuehling { 0x6920, &tonga_device_info }, /* Tonga */ 304a3084e6cSFelix Kuehling { 0x6921, &tonga_device_info }, /* Tonga */ 305a3084e6cSFelix Kuehling { 0x6928, &tonga_device_info }, /* Tonga */ 306a3084e6cSFelix Kuehling { 0x6929, &tonga_device_info }, /* Tonga */ 307a3084e6cSFelix Kuehling { 0x692B, &tonga_device_info }, /* Tonga */ 308a3084e6cSFelix Kuehling { 0x6938, &tonga_device_info }, /* Tonga */ 309a3084e6cSFelix Kuehling { 0x6939, &tonga_device_info }, /* Tonga */ 310a3084e6cSFelix Kuehling { 0x7300, &fiji_device_info }, /* Fiji */ 311a3084e6cSFelix Kuehling { 0x730F, &fiji_vf_device_info }, /* Fiji vf*/ 312a3084e6cSFelix Kuehling { 0x67C0, &polaris10_device_info }, /* Polaris10 */ 313a3084e6cSFelix Kuehling { 0x67C1, &polaris10_device_info }, /* Polaris10 */ 314a3084e6cSFelix Kuehling { 0x67C2, &polaris10_device_info }, /* Polaris10 */ 315a3084e6cSFelix Kuehling { 0x67C4, &polaris10_device_info }, /* Polaris10 */ 316a3084e6cSFelix Kuehling { 0x67C7, &polaris10_device_info }, /* Polaris10 */ 317a3084e6cSFelix Kuehling { 0x67C8, &polaris10_device_info }, /* Polaris10 */ 318a3084e6cSFelix Kuehling { 0x67C9, &polaris10_device_info }, /* Polaris10 */ 319a3084e6cSFelix Kuehling { 0x67CA, &polaris10_device_info }, /* Polaris10 */ 320a3084e6cSFelix Kuehling { 0x67CC, &polaris10_device_info }, /* Polaris10 */ 321a3084e6cSFelix Kuehling { 0x67CF, &polaris10_device_info }, /* Polaris10 */ 322a3084e6cSFelix Kuehling { 0x67D0, &polaris10_vf_device_info }, /* Polaris10 vf*/ 323a3084e6cSFelix Kuehling { 0x67DF, &polaris10_device_info }, /* Polaris10 */ 324a3084e6cSFelix Kuehling { 0x67E0, &polaris11_device_info }, /* Polaris11 */ 325a3084e6cSFelix Kuehling { 0x67E1, &polaris11_device_info }, /* Polaris11 */ 326a3084e6cSFelix Kuehling { 0x67E3, &polaris11_device_info }, /* Polaris11 */ 327a3084e6cSFelix Kuehling { 0x67E7, &polaris11_device_info }, /* Polaris11 */ 328a3084e6cSFelix Kuehling { 0x67E8, &polaris11_device_info }, /* Polaris11 */ 329a3084e6cSFelix Kuehling { 0x67E9, &polaris11_device_info }, /* Polaris11 */ 330a3084e6cSFelix Kuehling { 0x67EB, &polaris11_device_info }, /* Polaris11 */ 331a3084e6cSFelix Kuehling { 0x67EF, &polaris11_device_info }, /* Polaris11 */ 332a3084e6cSFelix Kuehling { 0x67FF, &polaris11_device_info }, /* Polaris11 */ 333389056e5SFelix Kuehling { 0x6860, &vega10_device_info }, /* Vega10 */ 334389056e5SFelix Kuehling { 0x6861, &vega10_device_info }, /* Vega10 */ 335389056e5SFelix Kuehling { 0x6862, &vega10_device_info }, /* Vega10 */ 336389056e5SFelix Kuehling { 0x6863, &vega10_device_info }, /* Vega10 */ 337389056e5SFelix Kuehling { 0x6864, &vega10_device_info }, /* Vega10 */ 338389056e5SFelix Kuehling { 0x6867, &vega10_device_info }, /* Vega10 */ 339389056e5SFelix Kuehling { 0x6868, &vega10_device_info }, /* Vega10 */ 340756e16bfSAlex Deucher { 0x6869, &vega10_device_info }, /* Vega10 */ 341756e16bfSAlex Deucher { 0x686A, &vega10_device_info }, /* Vega10 */ 342756e16bfSAlex Deucher { 0x686B, &vega10_device_info }, /* Vega10 */ 343389056e5SFelix Kuehling { 0x686C, &vega10_vf_device_info }, /* Vega10 vf*/ 344756e16bfSAlex Deucher { 0x686D, &vega10_device_info }, /* Vega10 */ 345756e16bfSAlex Deucher { 0x686E, &vega10_device_info }, /* Vega10 */ 346756e16bfSAlex Deucher { 0x686F, &vega10_device_info }, /* Vega10 */ 347389056e5SFelix Kuehling { 0x687F, &vega10_device_info }, /* Vega10 */ 34822a3a294SShaoyun Liu { 0x66a0, &vega20_device_info }, /* Vega20 */ 34922a3a294SShaoyun Liu { 0x66a1, &vega20_device_info }, /* Vega20 */ 35022a3a294SShaoyun Liu { 0x66a2, &vega20_device_info }, /* Vega20 */ 35122a3a294SShaoyun Liu { 0x66a3, &vega20_device_info }, /* Vega20 */ 352*9bd206f8SAlex Deucher { 0x66a4, &vega20_device_info }, /* Vega20 */ 35322a3a294SShaoyun Liu { 0x66a7, &vega20_device_info }, /* Vega20 */ 35422a3a294SShaoyun Liu { 0x66af, &vega20_device_info } /* Vega20 */ 3554a488a7aSOded Gabbay }; 3564a488a7aSOded Gabbay 3576e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 3586e81090bSOded Gabbay unsigned int chunk_size); 3596e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd); 3606e81090bSOded Gabbay 361b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd); 362b8935a7cSYong Zhao 3634a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did) 3644a488a7aSOded Gabbay { 3654a488a7aSOded Gabbay size_t i; 3664a488a7aSOded Gabbay 3674a488a7aSOded Gabbay for (i = 0; i < ARRAY_SIZE(supported_devices); i++) { 3684a488a7aSOded Gabbay if (supported_devices[i].did == did) { 36932fa8219SFelix Kuehling WARN_ON(!supported_devices[i].device_info); 3704a488a7aSOded Gabbay return supported_devices[i].device_info; 3714a488a7aSOded Gabbay } 3724a488a7aSOded Gabbay } 3734a488a7aSOded Gabbay 3744ebc7182SYong Zhao dev_warn(kfd_device, "DID %04x is missing in supported_devices\n", 3754ebc7182SYong Zhao did); 3764ebc7182SYong Zhao 3774a488a7aSOded Gabbay return NULL; 3784a488a7aSOded Gabbay } 3794a488a7aSOded Gabbay 380cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, 381cea405b1SXihan Zhang struct pci_dev *pdev, const struct kfd2kgd_calls *f2g) 3824a488a7aSOded Gabbay { 3834a488a7aSOded Gabbay struct kfd_dev *kfd; 3846106dce9Swelu int ret; 3854a488a7aSOded Gabbay const struct kfd_device_info *device_info = 3864a488a7aSOded Gabbay lookup_device_info(pdev->device); 3874a488a7aSOded Gabbay 3884ebc7182SYong Zhao if (!device_info) { 3894ebc7182SYong Zhao dev_err(kfd_device, "kgd2kfd_probe failed\n"); 3904a488a7aSOded Gabbay return NULL; 3914ebc7182SYong Zhao } 3924a488a7aSOded Gabbay 393d35f00d8SEric Huang kfd = kzalloc(sizeof(*kfd), GFP_KERNEL); 394d35f00d8SEric Huang if (!kfd) 395d35f00d8SEric Huang return NULL; 396d35f00d8SEric Huang 3976106dce9Swelu /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps. 3986106dce9Swelu * 32 and 64-bit requests are possible and must be 3996106dce9Swelu * supported. 4003ee2d00cSFelix Kuehling */ 4016106dce9Swelu ret = pci_enable_atomic_ops_to_root(pdev, 4023ee2d00cSFelix Kuehling PCI_EXP_DEVCAP2_ATOMIC_COMP32 | 4036106dce9Swelu PCI_EXP_DEVCAP2_ATOMIC_COMP64); 4046106dce9Swelu if (device_info->needs_pci_atomics && ret < 0) { 4053ee2d00cSFelix Kuehling dev_info(kfd_device, 4066106dce9Swelu "skipped device %x:%x, PCI rejects atomics\n", 4073ee2d00cSFelix Kuehling pdev->vendor, pdev->device); 408d35f00d8SEric Huang kfree(kfd); 4093ee2d00cSFelix Kuehling return NULL; 410d35f00d8SEric Huang } else if (!ret) 411d35f00d8SEric Huang kfd->pci_atomic_requested = true; 4124a488a7aSOded Gabbay 4134a488a7aSOded Gabbay kfd->kgd = kgd; 4144a488a7aSOded Gabbay kfd->device_info = device_info; 4154a488a7aSOded Gabbay kfd->pdev = pdev; 41619f6d2a6SOded Gabbay kfd->init_complete = false; 417cea405b1SXihan Zhang kfd->kfd2kgd = f2g; 418cea405b1SXihan Zhang 419cea405b1SXihan Zhang mutex_init(&kfd->doorbell_mutex); 420cea405b1SXihan Zhang memset(&kfd->doorbell_available_index, 0, 421cea405b1SXihan Zhang sizeof(kfd->doorbell_available_index)); 4224a488a7aSOded Gabbay 4234a488a7aSOded Gabbay return kfd; 4244a488a7aSOded Gabbay } 4254a488a7aSOded Gabbay 426373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd) 427373d7080SFelix Kuehling { 428373d7080SFelix Kuehling if (cwsr_enable && kfd->device_info->supports_cwsr) { 4293e76c239SFelix Kuehling if (kfd->device_info->asic_family < CHIP_VEGA10) { 430373d7080SFelix Kuehling BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE); 431373d7080SFelix Kuehling kfd->cwsr_isa = cwsr_trap_gfx8_hex; 432373d7080SFelix Kuehling kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex); 4333e76c239SFelix Kuehling } else { 4343e76c239SFelix Kuehling BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE); 4353e76c239SFelix Kuehling kfd->cwsr_isa = cwsr_trap_gfx9_hex; 4363e76c239SFelix Kuehling kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex); 4373e76c239SFelix Kuehling } 4383e76c239SFelix Kuehling 439373d7080SFelix Kuehling kfd->cwsr_enabled = true; 440373d7080SFelix Kuehling } 441373d7080SFelix Kuehling } 442373d7080SFelix Kuehling 4434a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd, 4444a488a7aSOded Gabbay const struct kgd2kfd_shared_resources *gpu_resources) 4454a488a7aSOded Gabbay { 44619f6d2a6SOded Gabbay unsigned int size; 44719f6d2a6SOded Gabbay 4485ade6c9cSFelix Kuehling kfd->mec_fw_version = kfd->kfd2kgd->get_fw_version(kfd->kgd, 4495ade6c9cSFelix Kuehling KGD_ENGINE_MEC1); 4505ade6c9cSFelix Kuehling kfd->sdma_fw_version = kfd->kfd2kgd->get_fw_version(kfd->kgd, 4515ade6c9cSFelix Kuehling KGD_ENGINE_SDMA1); 4524a488a7aSOded Gabbay kfd->shared_resources = *gpu_resources; 4534a488a7aSOded Gabbay 45444008d7aSYong Zhao kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1; 45544008d7aSYong Zhao kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1; 45644008d7aSYong Zhao kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd 45744008d7aSYong Zhao - kfd->vm_info.first_vmid_kfd + 1; 45844008d7aSYong Zhao 459a99c6d4fSFelix Kuehling /* Verify module parameters regarding mapped process number*/ 460a99c6d4fSFelix Kuehling if ((hws_max_conc_proc < 0) 461a99c6d4fSFelix Kuehling || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) { 462a99c6d4fSFelix Kuehling dev_err(kfd_device, 463a99c6d4fSFelix Kuehling "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n", 464a99c6d4fSFelix Kuehling hws_max_conc_proc, kfd->vm_info.vmid_num_kfd, 465a99c6d4fSFelix Kuehling kfd->vm_info.vmid_num_kfd); 466a99c6d4fSFelix Kuehling kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd; 467a99c6d4fSFelix Kuehling } else 468a99c6d4fSFelix Kuehling kfd->max_proc_per_quantum = hws_max_conc_proc; 469a99c6d4fSFelix Kuehling 47019f6d2a6SOded Gabbay /* calculate max size of mqds needed for queues */ 471b8cbab04SOded Gabbay size = max_num_of_queues_per_device * 47219f6d2a6SOded Gabbay kfd->device_info->mqd_size_aligned; 47319f6d2a6SOded Gabbay 474e18e794eSOded Gabbay /* 475e18e794eSOded Gabbay * calculate max size of runlist packet. 476e18e794eSOded Gabbay * There can be only 2 packets at once 477e18e794eSOded Gabbay */ 478507968ddSFelix Kuehling size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) + 479507968ddSFelix Kuehling max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues) 480507968ddSFelix Kuehling + sizeof(struct pm4_mes_runlist)) * 2; 481e18e794eSOded Gabbay 482e18e794eSOded Gabbay /* Add size of HIQ & DIQ */ 483e18e794eSOded Gabbay size += KFD_KERNEL_QUEUE_SIZE * 2; 484e18e794eSOded Gabbay 485e18e794eSOded Gabbay /* add another 512KB for all other allocations on gart (HPD, fences) */ 48619f6d2a6SOded Gabbay size += 512 * 1024; 48719f6d2a6SOded Gabbay 488cea405b1SXihan Zhang if (kfd->kfd2kgd->init_gtt_mem_allocation( 489cea405b1SXihan Zhang kfd->kgd, size, &kfd->gtt_mem, 49015426dbbSYong Zhao &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr, 49115426dbbSYong Zhao false)) { 49279775b62SKent Russell dev_err(kfd_device, "Could not allocate %d bytes\n", size); 49319f6d2a6SOded Gabbay goto out; 49419f6d2a6SOded Gabbay } 49519f6d2a6SOded Gabbay 49679775b62SKent Russell dev_info(kfd_device, "Allocated %d bytes on gart\n", size); 497e18e794eSOded Gabbay 49873a1da0bSOded Gabbay /* Initialize GTT sa with 512 byte chunk size */ 49973a1da0bSOded Gabbay if (kfd_gtt_sa_init(kfd, size, 512) != 0) { 50079775b62SKent Russell dev_err(kfd_device, "Error initializing gtt sub-allocator\n"); 50173a1da0bSOded Gabbay goto kfd_gtt_sa_init_error; 50273a1da0bSOded Gabbay } 50373a1da0bSOded Gabbay 504735df2baSFelix Kuehling if (kfd_doorbell_init(kfd)) { 505735df2baSFelix Kuehling dev_err(kfd_device, 506735df2baSFelix Kuehling "Error initializing doorbell aperture\n"); 507735df2baSFelix Kuehling goto kfd_doorbell_error; 508735df2baSFelix Kuehling } 50919f6d2a6SOded Gabbay 5100c1690e3SShaoyun Liu if (kfd->kfd2kgd->get_hive_id) 5110c1690e3SShaoyun Liu kfd->hive_id = kfd->kfd2kgd->get_hive_id(kfd->kgd); 5120c1690e3SShaoyun Liu 5134eacc26bSKent Russell if (kfd_topology_add_device(kfd)) { 51479775b62SKent Russell dev_err(kfd_device, "Error adding device to topology\n"); 51519f6d2a6SOded Gabbay goto kfd_topology_add_device_error; 51619f6d2a6SOded Gabbay } 51719f6d2a6SOded Gabbay 5182249d558SAndrew Lewycky if (kfd_interrupt_init(kfd)) { 51979775b62SKent Russell dev_err(kfd_device, "Error initializing interrupts\n"); 5202249d558SAndrew Lewycky goto kfd_interrupt_error; 5212249d558SAndrew Lewycky } 5222249d558SAndrew Lewycky 52364c7f8cfSBen Goz kfd->dqm = device_queue_manager_init(kfd); 52464c7f8cfSBen Goz if (!kfd->dqm) { 52579775b62SKent Russell dev_err(kfd_device, "Error initializing queue manager\n"); 52664c7f8cfSBen Goz goto device_queue_manager_error; 52764c7f8cfSBen Goz } 52864c7f8cfSBen Goz 52964d1c3a4SFelix Kuehling if (kfd_iommu_device_init(kfd)) { 53064d1c3a4SFelix Kuehling dev_err(kfd_device, "Error initializing iommuv2\n"); 53164d1c3a4SFelix Kuehling goto device_iommu_error; 53264c7f8cfSBen Goz } 53364c7f8cfSBen Goz 534373d7080SFelix Kuehling kfd_cwsr_init(kfd); 535373d7080SFelix Kuehling 536b8935a7cSYong Zhao if (kfd_resume(kfd)) 537b8935a7cSYong Zhao goto kfd_resume_error; 538b8935a7cSYong Zhao 539fbeb661bSYair Shachar kfd->dbgmgr = NULL; 540fbeb661bSYair Shachar 5414a488a7aSOded Gabbay kfd->init_complete = true; 54279775b62SKent Russell dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor, 5434a488a7aSOded Gabbay kfd->pdev->device); 5444a488a7aSOded Gabbay 54579775b62SKent Russell pr_debug("Starting kfd with the following scheduling policy %d\n", 546d146c5a7SFelix Kuehling kfd->dqm->sched_policy); 54764c7f8cfSBen Goz 54819f6d2a6SOded Gabbay goto out; 54919f6d2a6SOded Gabbay 550b8935a7cSYong Zhao kfd_resume_error: 55164d1c3a4SFelix Kuehling device_iommu_error: 55264c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 55364c7f8cfSBen Goz device_queue_manager_error: 5542249d558SAndrew Lewycky kfd_interrupt_exit(kfd); 5552249d558SAndrew Lewycky kfd_interrupt_error: 556b17f068aSOded Gabbay kfd_topology_remove_device(kfd); 55719f6d2a6SOded Gabbay kfd_topology_add_device_error: 558735df2baSFelix Kuehling kfd_doorbell_fini(kfd); 559735df2baSFelix Kuehling kfd_doorbell_error: 56073a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd); 56173a1da0bSOded Gabbay kfd_gtt_sa_init_error: 562cea405b1SXihan Zhang kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem); 56319f6d2a6SOded Gabbay dev_err(kfd_device, 56479775b62SKent Russell "device %x:%x NOT added due to errors\n", 56519f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 56619f6d2a6SOded Gabbay out: 56719f6d2a6SOded Gabbay return kfd->init_complete; 5684a488a7aSOded Gabbay } 5694a488a7aSOded Gabbay 5704a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd) 5714a488a7aSOded Gabbay { 572b17f068aSOded Gabbay if (kfd->init_complete) { 573b8935a7cSYong Zhao kgd2kfd_suspend(kfd); 57464c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 5752249d558SAndrew Lewycky kfd_interrupt_exit(kfd); 57619f6d2a6SOded Gabbay kfd_topology_remove_device(kfd); 577735df2baSFelix Kuehling kfd_doorbell_fini(kfd); 57873a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd); 579cea405b1SXihan Zhang kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem); 580b17f068aSOded Gabbay } 5815b5c4e40SEvgeny Pinchuk 5824a488a7aSOded Gabbay kfree(kfd); 5834a488a7aSOded Gabbay } 5844a488a7aSOded Gabbay 585e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd) 586e3b7a967SShaoyun Liu { 587e42051d2SShaoyun Liu if (!kfd->init_complete) 588e42051d2SShaoyun Liu return 0; 589e42051d2SShaoyun Liu kgd2kfd_suspend(kfd); 590e42051d2SShaoyun Liu 591e42051d2SShaoyun Liu /* hold dqm->lock to prevent further execution*/ 592e42051d2SShaoyun Liu dqm_lock(kfd->dqm); 593e42051d2SShaoyun Liu 594e42051d2SShaoyun Liu kfd_signal_reset_event(kfd); 595e3b7a967SShaoyun Liu return 0; 596e3b7a967SShaoyun Liu } 597e3b7a967SShaoyun Liu 598e42051d2SShaoyun Liu /* 599e42051d2SShaoyun Liu * Fix me. KFD won't be able to resume existing process for now. 600e42051d2SShaoyun Liu * We will keep all existing process in a evicted state and 601e42051d2SShaoyun Liu * wait the process to be terminated. 602e42051d2SShaoyun Liu */ 603e42051d2SShaoyun Liu 604e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd) 605e3b7a967SShaoyun Liu { 606e42051d2SShaoyun Liu int ret, count; 607e42051d2SShaoyun Liu 608e42051d2SShaoyun Liu if (!kfd->init_complete) 609e3b7a967SShaoyun Liu return 0; 610e42051d2SShaoyun Liu 611e42051d2SShaoyun Liu dqm_unlock(kfd->dqm); 612e42051d2SShaoyun Liu 613e42051d2SShaoyun Liu ret = kfd_resume(kfd); 614e42051d2SShaoyun Liu if (ret) 615e42051d2SShaoyun Liu return ret; 616e42051d2SShaoyun Liu count = atomic_dec_return(&kfd_locked); 617e42051d2SShaoyun Liu WARN_ONCE(count != 0, "KFD reset ref. error"); 618e42051d2SShaoyun Liu return 0; 619e42051d2SShaoyun Liu } 620e42051d2SShaoyun Liu 621e42051d2SShaoyun Liu bool kfd_is_locked(void) 622e42051d2SShaoyun Liu { 623e42051d2SShaoyun Liu return (atomic_read(&kfd_locked) > 0); 624e3b7a967SShaoyun Liu } 625e3b7a967SShaoyun Liu 6264a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd) 6274a488a7aSOded Gabbay { 628733fa1f7SYong Zhao if (!kfd->init_complete) 629733fa1f7SYong Zhao return; 630733fa1f7SYong Zhao 63126103436SFelix Kuehling /* For first KFD device suspend all the KFD processes */ 632e42051d2SShaoyun Liu if (atomic_inc_return(&kfd_locked) == 1) 63326103436SFelix Kuehling kfd_suspend_all_processes(); 63426103436SFelix Kuehling 63545c9a5e4SOded Gabbay kfd->dqm->ops.stop(kfd->dqm); 636733fa1f7SYong Zhao 63764d1c3a4SFelix Kuehling kfd_iommu_suspend(kfd); 6384a488a7aSOded Gabbay } 6394a488a7aSOded Gabbay 6404a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd) 6414a488a7aSOded Gabbay { 64226103436SFelix Kuehling int ret, count; 64326103436SFelix Kuehling 644b8935a7cSYong Zhao if (!kfd->init_complete) 645b8935a7cSYong Zhao return 0; 646b17f068aSOded Gabbay 64726103436SFelix Kuehling ret = kfd_resume(kfd); 64826103436SFelix Kuehling if (ret) 64926103436SFelix Kuehling return ret; 650b17f068aSOded Gabbay 651e42051d2SShaoyun Liu count = atomic_dec_return(&kfd_locked); 65226103436SFelix Kuehling WARN_ONCE(count < 0, "KFD suspend / resume ref. error"); 65326103436SFelix Kuehling if (count == 0) 65426103436SFelix Kuehling ret = kfd_resume_all_processes(); 65526103436SFelix Kuehling 65626103436SFelix Kuehling return ret; 6574ebc7182SYong Zhao } 6584ebc7182SYong Zhao 659b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd) 660b8935a7cSYong Zhao { 661b8935a7cSYong Zhao int err = 0; 662b8935a7cSYong Zhao 66364d1c3a4SFelix Kuehling err = kfd_iommu_resume(kfd); 66464d1c3a4SFelix Kuehling if (err) { 66564d1c3a4SFelix Kuehling dev_err(kfd_device, 66664d1c3a4SFelix Kuehling "Failed to resume IOMMU for device %x:%x\n", 66764d1c3a4SFelix Kuehling kfd->pdev->vendor, kfd->pdev->device); 66864d1c3a4SFelix Kuehling return err; 66964d1c3a4SFelix Kuehling } 670733fa1f7SYong Zhao 671b8935a7cSYong Zhao err = kfd->dqm->ops.start(kfd->dqm); 672b8935a7cSYong Zhao if (err) { 673b8935a7cSYong Zhao dev_err(kfd_device, 674b8935a7cSYong Zhao "Error starting queue manager for device %x:%x\n", 675b8935a7cSYong Zhao kfd->pdev->vendor, kfd->pdev->device); 676b8935a7cSYong Zhao goto dqm_start_error; 677b17f068aSOded Gabbay } 678b17f068aSOded Gabbay 679b8935a7cSYong Zhao return err; 680b8935a7cSYong Zhao 681b8935a7cSYong Zhao dqm_start_error: 68264d1c3a4SFelix Kuehling kfd_iommu_suspend(kfd); 683b8935a7cSYong Zhao return err; 6844a488a7aSOded Gabbay } 6854a488a7aSOded Gabbay 686b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */ 687b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry) 6884a488a7aSOded Gabbay { 68958e69886SLan Xiao uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE]; 69058e69886SLan Xiao bool is_patched = false; 69158e69886SLan Xiao 6922249d558SAndrew Lewycky if (!kfd->init_complete) 6932249d558SAndrew Lewycky return; 6942249d558SAndrew Lewycky 69558e69886SLan Xiao if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) { 69658e69886SLan Xiao dev_err_once(kfd_device, "Ring entry too small\n"); 69758e69886SLan Xiao return; 69858e69886SLan Xiao } 69958e69886SLan Xiao 7002249d558SAndrew Lewycky spin_lock(&kfd->interrupt_lock); 7012249d558SAndrew Lewycky 7022249d558SAndrew Lewycky if (kfd->interrupts_active 70358e69886SLan Xiao && interrupt_is_wanted(kfd, ih_ring_entry, 70458e69886SLan Xiao patched_ihre, &is_patched) 70558e69886SLan Xiao && enqueue_ih_ring_entry(kfd, 70658e69886SLan Xiao is_patched ? patched_ihre : ih_ring_entry)) 70748e876a2SAndres Rodriguez queue_work(kfd->ih_wq, &kfd->interrupt_work); 7082249d558SAndrew Lewycky 7092249d558SAndrew Lewycky spin_unlock(&kfd->interrupt_lock); 7104a488a7aSOded Gabbay } 7116e81090bSOded Gabbay 7126b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm) 7136b95e797SFelix Kuehling { 7146b95e797SFelix Kuehling struct kfd_process *p; 7156b95e797SFelix Kuehling int r; 7166b95e797SFelix Kuehling 7176b95e797SFelix Kuehling /* Because we are called from arbitrary context (workqueue) as opposed 7186b95e797SFelix Kuehling * to process context, kfd_process could attempt to exit while we are 7196b95e797SFelix Kuehling * running so the lookup function increments the process ref count. 7206b95e797SFelix Kuehling */ 7216b95e797SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 7226b95e797SFelix Kuehling if (!p) 7236b95e797SFelix Kuehling return -ESRCH; 7246b95e797SFelix Kuehling 7256b95e797SFelix Kuehling r = kfd_process_evict_queues(p); 7266b95e797SFelix Kuehling 7276b95e797SFelix Kuehling kfd_unref_process(p); 7286b95e797SFelix Kuehling return r; 7296b95e797SFelix Kuehling } 7306b95e797SFelix Kuehling 7316b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm) 7326b95e797SFelix Kuehling { 7336b95e797SFelix Kuehling struct kfd_process *p; 7346b95e797SFelix Kuehling int r; 7356b95e797SFelix Kuehling 7366b95e797SFelix Kuehling /* Because we are called from arbitrary context (workqueue) as opposed 7376b95e797SFelix Kuehling * to process context, kfd_process could attempt to exit while we are 7386b95e797SFelix Kuehling * running so the lookup function increments the process ref count. 7396b95e797SFelix Kuehling */ 7406b95e797SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 7416b95e797SFelix Kuehling if (!p) 7426b95e797SFelix Kuehling return -ESRCH; 7436b95e797SFelix Kuehling 7446b95e797SFelix Kuehling r = kfd_process_restore_queues(p); 7456b95e797SFelix Kuehling 7466b95e797SFelix Kuehling kfd_unref_process(p); 7476b95e797SFelix Kuehling return r; 7486b95e797SFelix Kuehling } 7496b95e797SFelix Kuehling 75026103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will 75126103436SFelix Kuehling * prepare for safe eviction of KFD BOs that belong to the specified 75226103436SFelix Kuehling * process. 75326103436SFelix Kuehling * 75426103436SFelix Kuehling * @mm: mm_struct that identifies the specified KFD process 75526103436SFelix Kuehling * @fence: eviction fence attached to KFD process BOs 75626103436SFelix Kuehling * 75726103436SFelix Kuehling */ 75826103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm, 75926103436SFelix Kuehling struct dma_fence *fence) 76026103436SFelix Kuehling { 76126103436SFelix Kuehling struct kfd_process *p; 76226103436SFelix Kuehling unsigned long active_time; 76326103436SFelix Kuehling unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS); 76426103436SFelix Kuehling 76526103436SFelix Kuehling if (!fence) 76626103436SFelix Kuehling return -EINVAL; 76726103436SFelix Kuehling 76826103436SFelix Kuehling if (dma_fence_is_signaled(fence)) 76926103436SFelix Kuehling return 0; 77026103436SFelix Kuehling 77126103436SFelix Kuehling p = kfd_lookup_process_by_mm(mm); 77226103436SFelix Kuehling if (!p) 77326103436SFelix Kuehling return -ENODEV; 77426103436SFelix Kuehling 77526103436SFelix Kuehling if (fence->seqno == p->last_eviction_seqno) 77626103436SFelix Kuehling goto out; 77726103436SFelix Kuehling 77826103436SFelix Kuehling p->last_eviction_seqno = fence->seqno; 77926103436SFelix Kuehling 78026103436SFelix Kuehling /* Avoid KFD process starvation. Wait for at least 78126103436SFelix Kuehling * PROCESS_ACTIVE_TIME_MS before evicting the process again 78226103436SFelix Kuehling */ 78326103436SFelix Kuehling active_time = get_jiffies_64() - p->last_restore_timestamp; 78426103436SFelix Kuehling if (delay_jiffies > active_time) 78526103436SFelix Kuehling delay_jiffies -= active_time; 78626103436SFelix Kuehling else 78726103436SFelix Kuehling delay_jiffies = 0; 78826103436SFelix Kuehling 78926103436SFelix Kuehling /* During process initialization eviction_work.dwork is initialized 79026103436SFelix Kuehling * to kfd_evict_bo_worker 79126103436SFelix Kuehling */ 79226103436SFelix Kuehling schedule_delayed_work(&p->eviction_work, delay_jiffies); 79326103436SFelix Kuehling out: 79426103436SFelix Kuehling kfd_unref_process(p); 79526103436SFelix Kuehling return 0; 79626103436SFelix Kuehling } 79726103436SFelix Kuehling 7986e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 7996e81090bSOded Gabbay unsigned int chunk_size) 8006e81090bSOded Gabbay { 8018625ff9cSFelix Kuehling unsigned int num_of_longs; 8026e81090bSOded Gabbay 80332fa8219SFelix Kuehling if (WARN_ON(buf_size < chunk_size)) 80432fa8219SFelix Kuehling return -EINVAL; 80532fa8219SFelix Kuehling if (WARN_ON(buf_size == 0)) 80632fa8219SFelix Kuehling return -EINVAL; 80732fa8219SFelix Kuehling if (WARN_ON(chunk_size == 0)) 80832fa8219SFelix Kuehling return -EINVAL; 8096e81090bSOded Gabbay 8106e81090bSOded Gabbay kfd->gtt_sa_chunk_size = chunk_size; 8116e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks = buf_size / chunk_size; 8126e81090bSOded Gabbay 8138625ff9cSFelix Kuehling num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) / 8148625ff9cSFelix Kuehling BITS_PER_LONG; 8156e81090bSOded Gabbay 8168625ff9cSFelix Kuehling kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL); 8176e81090bSOded Gabbay 8186e81090bSOded Gabbay if (!kfd->gtt_sa_bitmap) 8196e81090bSOded Gabbay return -ENOMEM; 8206e81090bSOded Gabbay 82179775b62SKent Russell pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n", 8226e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap); 8236e81090bSOded Gabbay 8246e81090bSOded Gabbay mutex_init(&kfd->gtt_sa_lock); 8256e81090bSOded Gabbay 8266e81090bSOded Gabbay return 0; 8276e81090bSOded Gabbay 8286e81090bSOded Gabbay } 8296e81090bSOded Gabbay 8306e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd) 8316e81090bSOded Gabbay { 8326e81090bSOded Gabbay mutex_destroy(&kfd->gtt_sa_lock); 8336e81090bSOded Gabbay kfree(kfd->gtt_sa_bitmap); 8346e81090bSOded Gabbay } 8356e81090bSOded Gabbay 8366e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr, 8376e81090bSOded Gabbay unsigned int bit_num, 8386e81090bSOded Gabbay unsigned int chunk_size) 8396e81090bSOded Gabbay { 8406e81090bSOded Gabbay return start_addr + bit_num * chunk_size; 8416e81090bSOded Gabbay } 8426e81090bSOded Gabbay 8436e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr, 8446e81090bSOded Gabbay unsigned int bit_num, 8456e81090bSOded Gabbay unsigned int chunk_size) 8466e81090bSOded Gabbay { 8476e81090bSOded Gabbay return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size); 8486e81090bSOded Gabbay } 8496e81090bSOded Gabbay 8506e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size, 8516e81090bSOded Gabbay struct kfd_mem_obj **mem_obj) 8526e81090bSOded Gabbay { 8536e81090bSOded Gabbay unsigned int found, start_search, cur_size; 8546e81090bSOded Gabbay 8556e81090bSOded Gabbay if (size == 0) 8566e81090bSOded Gabbay return -EINVAL; 8576e81090bSOded Gabbay 8586e81090bSOded Gabbay if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size) 8596e81090bSOded Gabbay return -ENOMEM; 8606e81090bSOded Gabbay 8611cd106ecSFelix Kuehling *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL); 8621cd106ecSFelix Kuehling if (!(*mem_obj)) 8636e81090bSOded Gabbay return -ENOMEM; 8646e81090bSOded Gabbay 86579775b62SKent Russell pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size); 8666e81090bSOded Gabbay 8676e81090bSOded Gabbay start_search = 0; 8686e81090bSOded Gabbay 8696e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock); 8706e81090bSOded Gabbay 8716e81090bSOded Gabbay kfd_gtt_restart_search: 8726e81090bSOded Gabbay /* Find the first chunk that is free */ 8736e81090bSOded Gabbay found = find_next_zero_bit(kfd->gtt_sa_bitmap, 8746e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, 8756e81090bSOded Gabbay start_search); 8766e81090bSOded Gabbay 87779775b62SKent Russell pr_debug("Found = %d\n", found); 8786e81090bSOded Gabbay 8796e81090bSOded Gabbay /* If there wasn't any free chunk, bail out */ 8806e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks) 8816e81090bSOded Gabbay goto kfd_gtt_no_free_chunk; 8826e81090bSOded Gabbay 8836e81090bSOded Gabbay /* Update fields of mem_obj */ 8846e81090bSOded Gabbay (*mem_obj)->range_start = found; 8856e81090bSOded Gabbay (*mem_obj)->range_end = found; 8866e81090bSOded Gabbay (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr( 8876e81090bSOded Gabbay kfd->gtt_start_gpu_addr, 8886e81090bSOded Gabbay found, 8896e81090bSOded Gabbay kfd->gtt_sa_chunk_size); 8906e81090bSOded Gabbay (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr( 8916e81090bSOded Gabbay kfd->gtt_start_cpu_ptr, 8926e81090bSOded Gabbay found, 8936e81090bSOded Gabbay kfd->gtt_sa_chunk_size); 8946e81090bSOded Gabbay 89579775b62SKent Russell pr_debug("gpu_addr = %p, cpu_addr = %p\n", 8966e81090bSOded Gabbay (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr); 8976e81090bSOded Gabbay 8986e81090bSOded Gabbay /* If we need only one chunk, mark it as allocated and get out */ 8996e81090bSOded Gabbay if (size <= kfd->gtt_sa_chunk_size) { 90079775b62SKent Russell pr_debug("Single bit\n"); 9016e81090bSOded Gabbay set_bit(found, kfd->gtt_sa_bitmap); 9026e81090bSOded Gabbay goto kfd_gtt_out; 9036e81090bSOded Gabbay } 9046e81090bSOded Gabbay 9056e81090bSOded Gabbay /* Otherwise, try to see if we have enough contiguous chunks */ 9066e81090bSOded Gabbay cur_size = size - kfd->gtt_sa_chunk_size; 9076e81090bSOded Gabbay do { 9086e81090bSOded Gabbay (*mem_obj)->range_end = 9096e81090bSOded Gabbay find_next_zero_bit(kfd->gtt_sa_bitmap, 9106e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, ++found); 9116e81090bSOded Gabbay /* 9126e81090bSOded Gabbay * If next free chunk is not contiguous than we need to 9136e81090bSOded Gabbay * restart our search from the last free chunk we found (which 9146e81090bSOded Gabbay * wasn't contiguous to the previous ones 9156e81090bSOded Gabbay */ 9166e81090bSOded Gabbay if ((*mem_obj)->range_end != found) { 9176e81090bSOded Gabbay start_search = found; 9186e81090bSOded Gabbay goto kfd_gtt_restart_search; 9196e81090bSOded Gabbay } 9206e81090bSOded Gabbay 9216e81090bSOded Gabbay /* 9226e81090bSOded Gabbay * If we reached end of buffer, bail out with error 9236e81090bSOded Gabbay */ 9246e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks) 9256e81090bSOded Gabbay goto kfd_gtt_no_free_chunk; 9266e81090bSOded Gabbay 9276e81090bSOded Gabbay /* Check if we don't need another chunk */ 9286e81090bSOded Gabbay if (cur_size <= kfd->gtt_sa_chunk_size) 9296e81090bSOded Gabbay cur_size = 0; 9306e81090bSOded Gabbay else 9316e81090bSOded Gabbay cur_size -= kfd->gtt_sa_chunk_size; 9326e81090bSOded Gabbay 9336e81090bSOded Gabbay } while (cur_size > 0); 9346e81090bSOded Gabbay 93579775b62SKent Russell pr_debug("range_start = %d, range_end = %d\n", 9366e81090bSOded Gabbay (*mem_obj)->range_start, (*mem_obj)->range_end); 9376e81090bSOded Gabbay 9386e81090bSOded Gabbay /* Mark the chunks as allocated */ 9396e81090bSOded Gabbay for (found = (*mem_obj)->range_start; 9406e81090bSOded Gabbay found <= (*mem_obj)->range_end; 9416e81090bSOded Gabbay found++) 9426e81090bSOded Gabbay set_bit(found, kfd->gtt_sa_bitmap); 9436e81090bSOded Gabbay 9446e81090bSOded Gabbay kfd_gtt_out: 9456e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 9466e81090bSOded Gabbay return 0; 9476e81090bSOded Gabbay 9486e81090bSOded Gabbay kfd_gtt_no_free_chunk: 94979775b62SKent Russell pr_debug("Allocation failed with mem_obj = %p\n", mem_obj); 9506e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 9516e81090bSOded Gabbay kfree(mem_obj); 9526e81090bSOded Gabbay return -ENOMEM; 9536e81090bSOded Gabbay } 9546e81090bSOded Gabbay 9556e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj) 9566e81090bSOded Gabbay { 9576e81090bSOded Gabbay unsigned int bit; 9586e81090bSOded Gabbay 9599216ed29SOded Gabbay /* Act like kfree when trying to free a NULL object */ 9609216ed29SOded Gabbay if (!mem_obj) 9619216ed29SOded Gabbay return 0; 9626e81090bSOded Gabbay 96379775b62SKent Russell pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n", 9646e81090bSOded Gabbay mem_obj, mem_obj->range_start, mem_obj->range_end); 9656e81090bSOded Gabbay 9666e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock); 9676e81090bSOded Gabbay 9686e81090bSOded Gabbay /* Mark the chunks as free */ 9696e81090bSOded Gabbay for (bit = mem_obj->range_start; 9706e81090bSOded Gabbay bit <= mem_obj->range_end; 9716e81090bSOded Gabbay bit++) 9726e81090bSOded Gabbay clear_bit(bit, kfd->gtt_sa_bitmap); 9736e81090bSOded Gabbay 9746e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 9756e81090bSOded Gabbay 9766e81090bSOded Gabbay kfree(mem_obj); 9776e81090bSOded Gabbay return 0; 9786e81090bSOded Gabbay } 979a29ec470SShaoyun Liu 980a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS) 981a29ec470SShaoyun Liu 982a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS 983a29ec470SShaoyun Liu * which will trigger a GPU reset and bring the HWS back to normal state 984a29ec470SShaoyun Liu */ 985a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev) 986a29ec470SShaoyun Liu { 987a29ec470SShaoyun Liu int r = 0; 988a29ec470SShaoyun Liu 989a29ec470SShaoyun Liu if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) { 990a29ec470SShaoyun Liu pr_err("HWS is not enabled"); 991a29ec470SShaoyun Liu return -EINVAL; 992a29ec470SShaoyun Liu } 993a29ec470SShaoyun Liu 994a29ec470SShaoyun Liu r = pm_debugfs_hang_hws(&dev->dqm->packets); 995a29ec470SShaoyun Liu if (!r) 996a29ec470SShaoyun Liu r = dqm_debugfs_execute_queues(dev->dqm); 997a29ec470SShaoyun Liu 998a29ec470SShaoyun Liu return r; 999a29ec470SShaoyun Liu } 1000a29ec470SShaoyun Liu 1001a29ec470SShaoyun Liu #endif 1002