Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Advanced Micro Devices, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 23 | #if defined(CONFIG_AMD_IOMMU_V2_MODULE) || defined(CONFIG_AMD_IOMMU_V2) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 24 | #include <linux/amd-iommu.h> |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 25 | #endif |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 26 | #include <linux/bsearch.h> |
| 27 | #include <linux/pci.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include "kfd_priv.h" |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 30 | #include "kfd_device_queue_manager.h" |
Felix Kuehling | 507968d | 2017-08-15 23:00:15 -0400 | [diff] [blame] | 31 | #include "kfd_pm4_headers_vi.h" |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 32 | #include "cwsr_trap_handler_gfx8.asm" |
Felix Kuehling | 3e76c23 | 2018-04-10 17:33:16 -0400 | [diff] [blame] | 33 | #include "cwsr_trap_handler_gfx9.asm" |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 34 | #include "kfd_iommu.h" |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 35 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 36 | #define MQD_SIZE_ALIGNED 768 |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 37 | static atomic_t kfd_device_suspended = ATOMIC_INIT(0); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 38 | |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 39 | #ifdef KFD_SUPPORT_IOMMU_V2 |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 40 | static const struct kfd_device_info kaveri_device_info = { |
Ben Goz | 0da7558 | 2015-01-01 17:10:01 +0200 | [diff] [blame] | 41 | .asic_family = CHIP_KAVERI, |
| 42 | .max_pasid_bits = 16, |
Yair Shachar | 992839a | 2015-05-20 13:43:04 +0300 | [diff] [blame] | 43 | /* max num of queues for KV.TODO should be a dynamic value */ |
| 44 | .max_no_of_hqd = 24, |
Felix Kuehling | ada2b29 | 2018-04-10 17:33:03 -0400 | [diff] [blame] | 45 | .doorbell_size = 4, |
Ben Goz | 0da7558 | 2015-01-01 17:10:01 +0200 | [diff] [blame] | 46 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 47 | .event_interrupt_class = &event_interrupt_class_cik, |
Yair Shachar | fbeb661 | 2015-05-20 13:48:26 +0300 | [diff] [blame] | 48 | .num_of_watch_points = 4, |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 49 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 50 | .supports_cwsr = false, |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 51 | .needs_iommu_device = true, |
Felix Kuehling | 3ee2d00 | 2018-01-04 17:17:41 -0500 | [diff] [blame] | 52 | .needs_pci_atomics = false, |
Ben Goz | 0da7558 | 2015-01-01 17:10:01 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static const struct kfd_device_info carrizo_device_info = { |
| 56 | .asic_family = CHIP_CARRIZO, |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 57 | .max_pasid_bits = 16, |
Oded Gabbay | eaccd6e | 2015-06-06 21:45:43 +0300 | [diff] [blame] | 58 | /* max num of queues for CZ.TODO should be a dynamic value */ |
| 59 | .max_no_of_hqd = 24, |
Felix Kuehling | ada2b29 | 2018-04-10 17:33:03 -0400 | [diff] [blame] | 60 | .doorbell_size = 4, |
Andrew Lewycky | b3f5e6b | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 61 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
Oded Gabbay | eaccd6e | 2015-06-06 21:45:43 +0300 | [diff] [blame] | 62 | .event_interrupt_class = &event_interrupt_class_cik, |
Alexey Skidanov | f7c826a | 2014-10-13 16:35:12 +0300 | [diff] [blame] | 63 | .num_of_watch_points = 4, |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 64 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 65 | .supports_cwsr = true, |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 66 | .needs_iommu_device = true, |
Felix Kuehling | 3ee2d00 | 2018-01-04 17:17:41 -0500 | [diff] [blame] | 67 | .needs_pci_atomics = false, |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 68 | }; |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 69 | #endif |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 70 | |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 71 | static const struct kfd_device_info hawaii_device_info = { |
| 72 | .asic_family = CHIP_HAWAII, |
| 73 | .max_pasid_bits = 16, |
| 74 | /* max num of queues for KV.TODO should be a dynamic value */ |
| 75 | .max_no_of_hqd = 24, |
Felix Kuehling | ada2b29 | 2018-04-10 17:33:03 -0400 | [diff] [blame] | 76 | .doorbell_size = 4, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 77 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 78 | .event_interrupt_class = &event_interrupt_class_cik, |
| 79 | .num_of_watch_points = 4, |
| 80 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 81 | .supports_cwsr = false, |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 82 | .needs_iommu_device = false, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 83 | .needs_pci_atomics = false, |
| 84 | }; |
| 85 | |
| 86 | static const struct kfd_device_info tonga_device_info = { |
| 87 | .asic_family = CHIP_TONGA, |
| 88 | .max_pasid_bits = 16, |
| 89 | .max_no_of_hqd = 24, |
Felix Kuehling | ada2b29 | 2018-04-10 17:33:03 -0400 | [diff] [blame] | 90 | .doorbell_size = 4, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 91 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 92 | .event_interrupt_class = &event_interrupt_class_cik, |
| 93 | .num_of_watch_points = 4, |
| 94 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 95 | .supports_cwsr = false, |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 96 | .needs_iommu_device = false, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 97 | .needs_pci_atomics = true, |
| 98 | }; |
| 99 | |
| 100 | static const struct kfd_device_info tonga_vf_device_info = { |
| 101 | .asic_family = CHIP_TONGA, |
| 102 | .max_pasid_bits = 16, |
| 103 | .max_no_of_hqd = 24, |
Felix Kuehling | ada2b29 | 2018-04-10 17:33:03 -0400 | [diff] [blame] | 104 | .doorbell_size = 4, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 105 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 106 | .event_interrupt_class = &event_interrupt_class_cik, |
| 107 | .num_of_watch_points = 4, |
| 108 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 109 | .supports_cwsr = false, |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 110 | .needs_iommu_device = false, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 111 | .needs_pci_atomics = false, |
| 112 | }; |
| 113 | |
| 114 | static const struct kfd_device_info fiji_device_info = { |
| 115 | .asic_family = CHIP_FIJI, |
| 116 | .max_pasid_bits = 16, |
| 117 | .max_no_of_hqd = 24, |
Felix Kuehling | ada2b29 | 2018-04-10 17:33:03 -0400 | [diff] [blame] | 118 | .doorbell_size = 4, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 119 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 120 | .event_interrupt_class = &event_interrupt_class_cik, |
| 121 | .num_of_watch_points = 4, |
| 122 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 123 | .supports_cwsr = true, |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 124 | .needs_iommu_device = false, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 125 | .needs_pci_atomics = true, |
| 126 | }; |
| 127 | |
| 128 | static const struct kfd_device_info fiji_vf_device_info = { |
| 129 | .asic_family = CHIP_FIJI, |
| 130 | .max_pasid_bits = 16, |
| 131 | .max_no_of_hqd = 24, |
Felix Kuehling | ada2b29 | 2018-04-10 17:33:03 -0400 | [diff] [blame] | 132 | .doorbell_size = 4, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 133 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 134 | .event_interrupt_class = &event_interrupt_class_cik, |
| 135 | .num_of_watch_points = 4, |
| 136 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 137 | .supports_cwsr = true, |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 138 | .needs_iommu_device = false, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 139 | .needs_pci_atomics = false, |
| 140 | }; |
| 141 | |
| 142 | |
| 143 | static const struct kfd_device_info polaris10_device_info = { |
| 144 | .asic_family = CHIP_POLARIS10, |
| 145 | .max_pasid_bits = 16, |
| 146 | .max_no_of_hqd = 24, |
Felix Kuehling | ada2b29 | 2018-04-10 17:33:03 -0400 | [diff] [blame] | 147 | .doorbell_size = 4, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 148 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 149 | .event_interrupt_class = &event_interrupt_class_cik, |
| 150 | .num_of_watch_points = 4, |
| 151 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 152 | .supports_cwsr = true, |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 153 | .needs_iommu_device = false, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 154 | .needs_pci_atomics = true, |
| 155 | }; |
| 156 | |
| 157 | static const struct kfd_device_info polaris10_vf_device_info = { |
| 158 | .asic_family = CHIP_POLARIS10, |
| 159 | .max_pasid_bits = 16, |
| 160 | .max_no_of_hqd = 24, |
Felix Kuehling | ada2b29 | 2018-04-10 17:33:03 -0400 | [diff] [blame] | 161 | .doorbell_size = 4, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 162 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 163 | .event_interrupt_class = &event_interrupt_class_cik, |
| 164 | .num_of_watch_points = 4, |
| 165 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 166 | .supports_cwsr = true, |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 167 | .needs_iommu_device = false, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 168 | .needs_pci_atomics = false, |
| 169 | }; |
| 170 | |
| 171 | static const struct kfd_device_info polaris11_device_info = { |
| 172 | .asic_family = CHIP_POLARIS11, |
| 173 | .max_pasid_bits = 16, |
| 174 | .max_no_of_hqd = 24, |
Felix Kuehling | ada2b29 | 2018-04-10 17:33:03 -0400 | [diff] [blame] | 175 | .doorbell_size = 4, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 176 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 177 | .event_interrupt_class = &event_interrupt_class_cik, |
| 178 | .num_of_watch_points = 4, |
| 179 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 180 | .supports_cwsr = true, |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 181 | .needs_iommu_device = false, |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 182 | .needs_pci_atomics = true, |
| 183 | }; |
| 184 | |
Felix Kuehling | 389056e | 2018-04-10 17:33:18 -0400 | [diff] [blame^] | 185 | static const struct kfd_device_info vega10_device_info = { |
| 186 | .asic_family = CHIP_VEGA10, |
| 187 | .max_pasid_bits = 16, |
| 188 | .max_no_of_hqd = 24, |
| 189 | .doorbell_size = 8, |
| 190 | .ih_ring_entry_size = 8 * sizeof(uint32_t), |
| 191 | .event_interrupt_class = &event_interrupt_class_v9, |
| 192 | .num_of_watch_points = 4, |
| 193 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 194 | .supports_cwsr = true, |
| 195 | .needs_iommu_device = false, |
| 196 | .needs_pci_atomics = false, |
| 197 | }; |
| 198 | |
| 199 | static const struct kfd_device_info vega10_vf_device_info = { |
| 200 | .asic_family = CHIP_VEGA10, |
| 201 | .max_pasid_bits = 16, |
| 202 | .max_no_of_hqd = 24, |
| 203 | .doorbell_size = 8, |
| 204 | .ih_ring_entry_size = 8 * sizeof(uint32_t), |
| 205 | .event_interrupt_class = &event_interrupt_class_v9, |
| 206 | .num_of_watch_points = 4, |
| 207 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 208 | .supports_cwsr = true, |
| 209 | .needs_iommu_device = false, |
| 210 | .needs_pci_atomics = false, |
| 211 | }; |
| 212 | |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 213 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 214 | struct kfd_deviceid { |
| 215 | unsigned short did; |
| 216 | const struct kfd_device_info *device_info; |
| 217 | }; |
| 218 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 219 | static const struct kfd_deviceid supported_devices[] = { |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 220 | #ifdef KFD_SUPPORT_IOMMU_V2 |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 221 | { 0x1304, &kaveri_device_info }, /* Kaveri */ |
| 222 | { 0x1305, &kaveri_device_info }, /* Kaveri */ |
| 223 | { 0x1306, &kaveri_device_info }, /* Kaveri */ |
| 224 | { 0x1307, &kaveri_device_info }, /* Kaveri */ |
| 225 | { 0x1309, &kaveri_device_info }, /* Kaveri */ |
| 226 | { 0x130A, &kaveri_device_info }, /* Kaveri */ |
| 227 | { 0x130B, &kaveri_device_info }, /* Kaveri */ |
| 228 | { 0x130C, &kaveri_device_info }, /* Kaveri */ |
| 229 | { 0x130D, &kaveri_device_info }, /* Kaveri */ |
| 230 | { 0x130E, &kaveri_device_info }, /* Kaveri */ |
| 231 | { 0x130F, &kaveri_device_info }, /* Kaveri */ |
| 232 | { 0x1310, &kaveri_device_info }, /* Kaveri */ |
| 233 | { 0x1311, &kaveri_device_info }, /* Kaveri */ |
| 234 | { 0x1312, &kaveri_device_info }, /* Kaveri */ |
| 235 | { 0x1313, &kaveri_device_info }, /* Kaveri */ |
| 236 | { 0x1315, &kaveri_device_info }, /* Kaveri */ |
| 237 | { 0x1316, &kaveri_device_info }, /* Kaveri */ |
| 238 | { 0x1317, &kaveri_device_info }, /* Kaveri */ |
| 239 | { 0x1318, &kaveri_device_info }, /* Kaveri */ |
| 240 | { 0x131B, &kaveri_device_info }, /* Kaveri */ |
| 241 | { 0x131C, &kaveri_device_info }, /* Kaveri */ |
Ben Goz | 123576d | 2015-01-12 14:37:24 +0200 | [diff] [blame] | 242 | { 0x131D, &kaveri_device_info }, /* Kaveri */ |
| 243 | { 0x9870, &carrizo_device_info }, /* Carrizo */ |
| 244 | { 0x9874, &carrizo_device_info }, /* Carrizo */ |
| 245 | { 0x9875, &carrizo_device_info }, /* Carrizo */ |
| 246 | { 0x9876, &carrizo_device_info }, /* Carrizo */ |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 247 | { 0x9877, &carrizo_device_info }, /* Carrizo */ |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 248 | #endif |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame] | 249 | { 0x67A0, &hawaii_device_info }, /* Hawaii */ |
| 250 | { 0x67A1, &hawaii_device_info }, /* Hawaii */ |
| 251 | { 0x67A2, &hawaii_device_info }, /* Hawaii */ |
| 252 | { 0x67A8, &hawaii_device_info }, /* Hawaii */ |
| 253 | { 0x67A9, &hawaii_device_info }, /* Hawaii */ |
| 254 | { 0x67AA, &hawaii_device_info }, /* Hawaii */ |
| 255 | { 0x67B0, &hawaii_device_info }, /* Hawaii */ |
| 256 | { 0x67B1, &hawaii_device_info }, /* Hawaii */ |
| 257 | { 0x67B8, &hawaii_device_info }, /* Hawaii */ |
| 258 | { 0x67B9, &hawaii_device_info }, /* Hawaii */ |
| 259 | { 0x67BA, &hawaii_device_info }, /* Hawaii */ |
| 260 | { 0x67BE, &hawaii_device_info }, /* Hawaii */ |
| 261 | { 0x6920, &tonga_device_info }, /* Tonga */ |
| 262 | { 0x6921, &tonga_device_info }, /* Tonga */ |
| 263 | { 0x6928, &tonga_device_info }, /* Tonga */ |
| 264 | { 0x6929, &tonga_device_info }, /* Tonga */ |
| 265 | { 0x692B, &tonga_device_info }, /* Tonga */ |
| 266 | { 0x692F, &tonga_vf_device_info }, /* Tonga vf */ |
| 267 | { 0x6938, &tonga_device_info }, /* Tonga */ |
| 268 | { 0x6939, &tonga_device_info }, /* Tonga */ |
| 269 | { 0x7300, &fiji_device_info }, /* Fiji */ |
| 270 | { 0x730F, &fiji_vf_device_info }, /* Fiji vf*/ |
| 271 | { 0x67C0, &polaris10_device_info }, /* Polaris10 */ |
| 272 | { 0x67C1, &polaris10_device_info }, /* Polaris10 */ |
| 273 | { 0x67C2, &polaris10_device_info }, /* Polaris10 */ |
| 274 | { 0x67C4, &polaris10_device_info }, /* Polaris10 */ |
| 275 | { 0x67C7, &polaris10_device_info }, /* Polaris10 */ |
| 276 | { 0x67C8, &polaris10_device_info }, /* Polaris10 */ |
| 277 | { 0x67C9, &polaris10_device_info }, /* Polaris10 */ |
| 278 | { 0x67CA, &polaris10_device_info }, /* Polaris10 */ |
| 279 | { 0x67CC, &polaris10_device_info }, /* Polaris10 */ |
| 280 | { 0x67CF, &polaris10_device_info }, /* Polaris10 */ |
| 281 | { 0x67D0, &polaris10_vf_device_info }, /* Polaris10 vf*/ |
| 282 | { 0x67DF, &polaris10_device_info }, /* Polaris10 */ |
| 283 | { 0x67E0, &polaris11_device_info }, /* Polaris11 */ |
| 284 | { 0x67E1, &polaris11_device_info }, /* Polaris11 */ |
| 285 | { 0x67E3, &polaris11_device_info }, /* Polaris11 */ |
| 286 | { 0x67E7, &polaris11_device_info }, /* Polaris11 */ |
| 287 | { 0x67E8, &polaris11_device_info }, /* Polaris11 */ |
| 288 | { 0x67E9, &polaris11_device_info }, /* Polaris11 */ |
| 289 | { 0x67EB, &polaris11_device_info }, /* Polaris11 */ |
| 290 | { 0x67EF, &polaris11_device_info }, /* Polaris11 */ |
| 291 | { 0x67FF, &polaris11_device_info }, /* Polaris11 */ |
Felix Kuehling | 389056e | 2018-04-10 17:33:18 -0400 | [diff] [blame^] | 292 | { 0x6860, &vega10_device_info }, /* Vega10 */ |
| 293 | { 0x6861, &vega10_device_info }, /* Vega10 */ |
| 294 | { 0x6862, &vega10_device_info }, /* Vega10 */ |
| 295 | { 0x6863, &vega10_device_info }, /* Vega10 */ |
| 296 | { 0x6864, &vega10_device_info }, /* Vega10 */ |
| 297 | { 0x6867, &vega10_device_info }, /* Vega10 */ |
| 298 | { 0x6868, &vega10_device_info }, /* Vega10 */ |
| 299 | { 0x686C, &vega10_vf_device_info }, /* Vega10 vf*/ |
| 300 | { 0x687F, &vega10_device_info }, /* Vega10 */ |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 301 | }; |
| 302 | |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 303 | static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, |
| 304 | unsigned int chunk_size); |
| 305 | static void kfd_gtt_sa_fini(struct kfd_dev *kfd); |
| 306 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 307 | static int kfd_resume(struct kfd_dev *kfd); |
| 308 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 309 | static const struct kfd_device_info *lookup_device_info(unsigned short did) |
| 310 | { |
| 311 | size_t i; |
| 312 | |
| 313 | for (i = 0; i < ARRAY_SIZE(supported_devices); i++) { |
| 314 | if (supported_devices[i].did == did) { |
Felix Kuehling | 32fa821 | 2017-08-15 23:00:12 -0400 | [diff] [blame] | 315 | WARN_ON(!supported_devices[i].device_info); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 316 | return supported_devices[i].device_info; |
| 317 | } |
| 318 | } |
| 319 | |
Yong Zhao | 4ebc718 | 2017-08-15 23:00:13 -0400 | [diff] [blame] | 320 | dev_warn(kfd_device, "DID %04x is missing in supported_devices\n", |
| 321 | did); |
| 322 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 323 | return NULL; |
| 324 | } |
| 325 | |
Xihan Zhang | cea405b | 2015-03-17 19:32:53 +0800 | [diff] [blame] | 326 | struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, |
| 327 | struct pci_dev *pdev, const struct kfd2kgd_calls *f2g) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 328 | { |
| 329 | struct kfd_dev *kfd; |
welu | 6106dce | 2018-04-10 17:33:17 -0400 | [diff] [blame] | 330 | int ret; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 331 | const struct kfd_device_info *device_info = |
| 332 | lookup_device_info(pdev->device); |
| 333 | |
Yong Zhao | 4ebc718 | 2017-08-15 23:00:13 -0400 | [diff] [blame] | 334 | if (!device_info) { |
| 335 | dev_err(kfd_device, "kgd2kfd_probe failed\n"); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 336 | return NULL; |
Yong Zhao | 4ebc718 | 2017-08-15 23:00:13 -0400 | [diff] [blame] | 337 | } |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 338 | |
welu | 6106dce | 2018-04-10 17:33:17 -0400 | [diff] [blame] | 339 | /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps. |
| 340 | * 32 and 64-bit requests are possible and must be |
| 341 | * supported. |
| 342 | */ |
| 343 | ret = pci_enable_atomic_ops_to_root(pdev, |
| 344 | PCI_EXP_DEVCAP2_ATOMIC_COMP32 | |
| 345 | PCI_EXP_DEVCAP2_ATOMIC_COMP64); |
| 346 | if (device_info->needs_pci_atomics && ret < 0) { |
| 347 | dev_info(kfd_device, |
| 348 | "skipped device %x:%x, PCI rejects atomics\n", |
| 349 | pdev->vendor, pdev->device); |
| 350 | return NULL; |
Felix Kuehling | 3ee2d00 | 2018-01-04 17:17:41 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 353 | kfd = kzalloc(sizeof(*kfd), GFP_KERNEL); |
| 354 | if (!kfd) |
| 355 | return NULL; |
| 356 | |
| 357 | kfd->kgd = kgd; |
| 358 | kfd->device_info = device_info; |
| 359 | kfd->pdev = pdev; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 360 | kfd->init_complete = false; |
Xihan Zhang | cea405b | 2015-03-17 19:32:53 +0800 | [diff] [blame] | 361 | kfd->kfd2kgd = f2g; |
| 362 | |
| 363 | mutex_init(&kfd->doorbell_mutex); |
| 364 | memset(&kfd->doorbell_available_index, 0, |
| 365 | sizeof(kfd->doorbell_available_index)); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 366 | |
| 367 | return kfd; |
| 368 | } |
| 369 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 370 | static void kfd_cwsr_init(struct kfd_dev *kfd) |
| 371 | { |
| 372 | if (cwsr_enable && kfd->device_info->supports_cwsr) { |
Felix Kuehling | 3e76c23 | 2018-04-10 17:33:16 -0400 | [diff] [blame] | 373 | if (kfd->device_info->asic_family < CHIP_VEGA10) { |
| 374 | BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE); |
| 375 | kfd->cwsr_isa = cwsr_trap_gfx8_hex; |
| 376 | kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex); |
| 377 | } else { |
| 378 | BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE); |
| 379 | kfd->cwsr_isa = cwsr_trap_gfx9_hex; |
| 380 | kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex); |
| 381 | } |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 382 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 383 | kfd->cwsr_enabled = true; |
| 384 | } |
| 385 | } |
| 386 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 387 | bool kgd2kfd_device_init(struct kfd_dev *kfd, |
| 388 | const struct kgd2kfd_shared_resources *gpu_resources) |
| 389 | { |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 390 | unsigned int size; |
| 391 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 392 | kfd->shared_resources = *gpu_resources; |
| 393 | |
Yong Zhao | 44008d7 | 2017-09-20 18:10:18 -0400 | [diff] [blame] | 394 | kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1; |
| 395 | kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1; |
| 396 | kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd |
| 397 | - kfd->vm_info.first_vmid_kfd + 1; |
| 398 | |
Felix Kuehling | a99c6d4 | 2017-11-27 18:29:45 -0500 | [diff] [blame] | 399 | /* Verify module parameters regarding mapped process number*/ |
| 400 | if ((hws_max_conc_proc < 0) |
| 401 | || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) { |
| 402 | dev_err(kfd_device, |
| 403 | "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n", |
| 404 | hws_max_conc_proc, kfd->vm_info.vmid_num_kfd, |
| 405 | kfd->vm_info.vmid_num_kfd); |
| 406 | kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd; |
| 407 | } else |
| 408 | kfd->max_proc_per_quantum = hws_max_conc_proc; |
| 409 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 410 | /* calculate max size of mqds needed for queues */ |
Oded Gabbay | b8cbab0 | 2015-01-18 13:18:01 +0200 | [diff] [blame] | 411 | size = max_num_of_queues_per_device * |
| 412 | kfd->device_info->mqd_size_aligned; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 413 | |
Oded Gabbay | e18e794 | 2014-10-26 10:12:22 +0200 | [diff] [blame] | 414 | /* |
| 415 | * calculate max size of runlist packet. |
| 416 | * There can be only 2 packets at once |
| 417 | */ |
Felix Kuehling | 507968d | 2017-08-15 23:00:15 -0400 | [diff] [blame] | 418 | size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) + |
| 419 | max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues) |
| 420 | + sizeof(struct pm4_mes_runlist)) * 2; |
Oded Gabbay | e18e794 | 2014-10-26 10:12:22 +0200 | [diff] [blame] | 421 | |
| 422 | /* Add size of HIQ & DIQ */ |
| 423 | size += KFD_KERNEL_QUEUE_SIZE * 2; |
| 424 | |
| 425 | /* add another 512KB for all other allocations on gart (HPD, fences) */ |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 426 | size += 512 * 1024; |
| 427 | |
Xihan Zhang | cea405b | 2015-03-17 19:32:53 +0800 | [diff] [blame] | 428 | if (kfd->kfd2kgd->init_gtt_mem_allocation( |
| 429 | kfd->kgd, size, &kfd->gtt_mem, |
| 430 | &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){ |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 431 | dev_err(kfd_device, "Could not allocate %d bytes\n", size); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 432 | goto out; |
| 433 | } |
| 434 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 435 | dev_info(kfd_device, "Allocated %d bytes on gart\n", size); |
Oded Gabbay | e18e794 | 2014-10-26 10:12:22 +0200 | [diff] [blame] | 436 | |
Oded Gabbay | 73a1da0 | 2014-10-26 09:53:37 +0200 | [diff] [blame] | 437 | /* Initialize GTT sa with 512 byte chunk size */ |
| 438 | if (kfd_gtt_sa_init(kfd, size, 512) != 0) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 439 | dev_err(kfd_device, "Error initializing gtt sub-allocator\n"); |
Oded Gabbay | 73a1da0 | 2014-10-26 09:53:37 +0200 | [diff] [blame] | 440 | goto kfd_gtt_sa_init_error; |
| 441 | } |
| 442 | |
Felix Kuehling | 735df2b | 2017-08-15 23:00:10 -0400 | [diff] [blame] | 443 | if (kfd_doorbell_init(kfd)) { |
| 444 | dev_err(kfd_device, |
| 445 | "Error initializing doorbell aperture\n"); |
| 446 | goto kfd_doorbell_error; |
| 447 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 448 | |
Kent Russell | 4eacc26b | 2017-08-15 23:00:06 -0400 | [diff] [blame] | 449 | if (kfd_topology_add_device(kfd)) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 450 | dev_err(kfd_device, "Error adding device to topology\n"); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 451 | goto kfd_topology_add_device_error; |
| 452 | } |
| 453 | |
Andrew Lewycky | 2249d55 | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 454 | if (kfd_interrupt_init(kfd)) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 455 | dev_err(kfd_device, "Error initializing interrupts\n"); |
Andrew Lewycky | 2249d55 | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 456 | goto kfd_interrupt_error; |
| 457 | } |
| 458 | |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 459 | kfd->dqm = device_queue_manager_init(kfd); |
| 460 | if (!kfd->dqm) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 461 | dev_err(kfd_device, "Error initializing queue manager\n"); |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 462 | goto device_queue_manager_error; |
| 463 | } |
| 464 | |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 465 | if (kfd_iommu_device_init(kfd)) { |
| 466 | dev_err(kfd_device, "Error initializing iommuv2\n"); |
| 467 | goto device_iommu_error; |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 468 | } |
| 469 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 470 | kfd_cwsr_init(kfd); |
| 471 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 472 | if (kfd_resume(kfd)) |
| 473 | goto kfd_resume_error; |
| 474 | |
Yair Shachar | fbeb661 | 2015-05-20 13:48:26 +0300 | [diff] [blame] | 475 | kfd->dbgmgr = NULL; |
| 476 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 477 | kfd->init_complete = true; |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 478 | dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor, |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 479 | kfd->pdev->device); |
| 480 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 481 | pr_debug("Starting kfd with the following scheduling policy %d\n", |
Felix Kuehling | d146c5a | 2018-01-04 17:17:43 -0500 | [diff] [blame] | 482 | kfd->dqm->sched_policy); |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 483 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 484 | goto out; |
| 485 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 486 | kfd_resume_error: |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 487 | device_iommu_error: |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 488 | device_queue_manager_uninit(kfd->dqm); |
| 489 | device_queue_manager_error: |
Andrew Lewycky | 2249d55 | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 490 | kfd_interrupt_exit(kfd); |
| 491 | kfd_interrupt_error: |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 492 | kfd_topology_remove_device(kfd); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 493 | kfd_topology_add_device_error: |
Felix Kuehling | 735df2b | 2017-08-15 23:00:10 -0400 | [diff] [blame] | 494 | kfd_doorbell_fini(kfd); |
| 495 | kfd_doorbell_error: |
Oded Gabbay | 73a1da0 | 2014-10-26 09:53:37 +0200 | [diff] [blame] | 496 | kfd_gtt_sa_fini(kfd); |
| 497 | kfd_gtt_sa_init_error: |
Xihan Zhang | cea405b | 2015-03-17 19:32:53 +0800 | [diff] [blame] | 498 | kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 499 | dev_err(kfd_device, |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 500 | "device %x:%x NOT added due to errors\n", |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 501 | kfd->pdev->vendor, kfd->pdev->device); |
| 502 | out: |
| 503 | return kfd->init_complete; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void kgd2kfd_device_exit(struct kfd_dev *kfd) |
| 507 | { |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 508 | if (kfd->init_complete) { |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 509 | kgd2kfd_suspend(kfd); |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 510 | device_queue_manager_uninit(kfd->dqm); |
Andrew Lewycky | 2249d55 | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 511 | kfd_interrupt_exit(kfd); |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 512 | kfd_topology_remove_device(kfd); |
Felix Kuehling | 735df2b | 2017-08-15 23:00:10 -0400 | [diff] [blame] | 513 | kfd_doorbell_fini(kfd); |
Oded Gabbay | 73a1da0 | 2014-10-26 09:53:37 +0200 | [diff] [blame] | 514 | kfd_gtt_sa_fini(kfd); |
Xihan Zhang | cea405b | 2015-03-17 19:32:53 +0800 | [diff] [blame] | 515 | kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem); |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 516 | } |
Evgeny Pinchuk | 5b5c4e4 | 2014-07-16 21:22:32 +0300 | [diff] [blame] | 517 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 518 | kfree(kfd); |
| 519 | } |
| 520 | |
| 521 | void kgd2kfd_suspend(struct kfd_dev *kfd) |
| 522 | { |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 523 | if (!kfd->init_complete) |
| 524 | return; |
| 525 | |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 526 | /* For first KFD device suspend all the KFD processes */ |
| 527 | if (atomic_inc_return(&kfd_device_suspended) == 1) |
| 528 | kfd_suspend_all_processes(); |
| 529 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 530 | kfd->dqm->ops.stop(kfd->dqm); |
| 531 | |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 532 | kfd_iommu_suspend(kfd); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | int kgd2kfd_resume(struct kfd_dev *kfd) |
| 536 | { |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 537 | int ret, count; |
| 538 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 539 | if (!kfd->init_complete) |
| 540 | return 0; |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 541 | |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 542 | ret = kfd_resume(kfd); |
| 543 | if (ret) |
| 544 | return ret; |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 545 | |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 546 | count = atomic_dec_return(&kfd_device_suspended); |
| 547 | WARN_ONCE(count < 0, "KFD suspend / resume ref. error"); |
| 548 | if (count == 0) |
| 549 | ret = kfd_resume_all_processes(); |
| 550 | |
| 551 | return ret; |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 552 | } |
Yong Zhao | 4ebc718 | 2017-08-15 23:00:13 -0400 | [diff] [blame] | 553 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 554 | static int kfd_resume(struct kfd_dev *kfd) |
| 555 | { |
| 556 | int err = 0; |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 557 | |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 558 | err = kfd_iommu_resume(kfd); |
| 559 | if (err) { |
| 560 | dev_err(kfd_device, |
| 561 | "Failed to resume IOMMU for device %x:%x\n", |
| 562 | kfd->pdev->vendor, kfd->pdev->device); |
| 563 | return err; |
| 564 | } |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 565 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 566 | err = kfd->dqm->ops.start(kfd->dqm); |
| 567 | if (err) { |
| 568 | dev_err(kfd_device, |
| 569 | "Error starting queue manager for device %x:%x\n", |
| 570 | kfd->pdev->vendor, kfd->pdev->device); |
| 571 | goto dqm_start_error; |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 572 | } |
| 573 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 574 | return err; |
| 575 | |
| 576 | dqm_start_error: |
Felix Kuehling | 64d1c3a | 2017-12-08 19:22:12 -0500 | [diff] [blame] | 577 | kfd_iommu_suspend(kfd); |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 578 | return err; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 579 | } |
| 580 | |
Andrew Lewycky | b3f5e6b | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 581 | /* This is called directly from KGD at ISR. */ |
| 582 | void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 583 | { |
Andrew Lewycky | 2249d55 | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 584 | if (!kfd->init_complete) |
| 585 | return; |
| 586 | |
| 587 | spin_lock(&kfd->interrupt_lock); |
| 588 | |
| 589 | if (kfd->interrupts_active |
| 590 | && interrupt_is_wanted(kfd, ih_ring_entry) |
| 591 | && enqueue_ih_ring_entry(kfd, ih_ring_entry)) |
Andres Rodriguez | 48e876a | 2017-10-27 19:35:34 -0400 | [diff] [blame] | 592 | queue_work(kfd->ih_wq, &kfd->interrupt_work); |
Andrew Lewycky | 2249d55 | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 593 | |
| 594 | spin_unlock(&kfd->interrupt_lock); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 595 | } |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 596 | |
Felix Kuehling | 6b95e79 | 2018-03-23 15:32:32 -0400 | [diff] [blame] | 597 | int kgd2kfd_quiesce_mm(struct mm_struct *mm) |
| 598 | { |
| 599 | struct kfd_process *p; |
| 600 | int r; |
| 601 | |
| 602 | /* Because we are called from arbitrary context (workqueue) as opposed |
| 603 | * to process context, kfd_process could attempt to exit while we are |
| 604 | * running so the lookup function increments the process ref count. |
| 605 | */ |
| 606 | p = kfd_lookup_process_by_mm(mm); |
| 607 | if (!p) |
| 608 | return -ESRCH; |
| 609 | |
| 610 | r = kfd_process_evict_queues(p); |
| 611 | |
| 612 | kfd_unref_process(p); |
| 613 | return r; |
| 614 | } |
| 615 | |
| 616 | int kgd2kfd_resume_mm(struct mm_struct *mm) |
| 617 | { |
| 618 | struct kfd_process *p; |
| 619 | int r; |
| 620 | |
| 621 | /* Because we are called from arbitrary context (workqueue) as opposed |
| 622 | * to process context, kfd_process could attempt to exit while we are |
| 623 | * running so the lookup function increments the process ref count. |
| 624 | */ |
| 625 | p = kfd_lookup_process_by_mm(mm); |
| 626 | if (!p) |
| 627 | return -ESRCH; |
| 628 | |
| 629 | r = kfd_process_restore_queues(p); |
| 630 | |
| 631 | kfd_unref_process(p); |
| 632 | return r; |
| 633 | } |
| 634 | |
Felix Kuehling | 2610343 | 2018-02-06 20:32:45 -0500 | [diff] [blame] | 635 | /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will |
| 636 | * prepare for safe eviction of KFD BOs that belong to the specified |
| 637 | * process. |
| 638 | * |
| 639 | * @mm: mm_struct that identifies the specified KFD process |
| 640 | * @fence: eviction fence attached to KFD process BOs |
| 641 | * |
| 642 | */ |
| 643 | int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm, |
| 644 | struct dma_fence *fence) |
| 645 | { |
| 646 | struct kfd_process *p; |
| 647 | unsigned long active_time; |
| 648 | unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS); |
| 649 | |
| 650 | if (!fence) |
| 651 | return -EINVAL; |
| 652 | |
| 653 | if (dma_fence_is_signaled(fence)) |
| 654 | return 0; |
| 655 | |
| 656 | p = kfd_lookup_process_by_mm(mm); |
| 657 | if (!p) |
| 658 | return -ENODEV; |
| 659 | |
| 660 | if (fence->seqno == p->last_eviction_seqno) |
| 661 | goto out; |
| 662 | |
| 663 | p->last_eviction_seqno = fence->seqno; |
| 664 | |
| 665 | /* Avoid KFD process starvation. Wait for at least |
| 666 | * PROCESS_ACTIVE_TIME_MS before evicting the process again |
| 667 | */ |
| 668 | active_time = get_jiffies_64() - p->last_restore_timestamp; |
| 669 | if (delay_jiffies > active_time) |
| 670 | delay_jiffies -= active_time; |
| 671 | else |
| 672 | delay_jiffies = 0; |
| 673 | |
| 674 | /* During process initialization eviction_work.dwork is initialized |
| 675 | * to kfd_evict_bo_worker |
| 676 | */ |
| 677 | schedule_delayed_work(&p->eviction_work, delay_jiffies); |
| 678 | out: |
| 679 | kfd_unref_process(p); |
| 680 | return 0; |
| 681 | } |
| 682 | |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 683 | static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, |
| 684 | unsigned int chunk_size) |
| 685 | { |
Felix Kuehling | 8625ff9 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 686 | unsigned int num_of_longs; |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 687 | |
Felix Kuehling | 32fa821 | 2017-08-15 23:00:12 -0400 | [diff] [blame] | 688 | if (WARN_ON(buf_size < chunk_size)) |
| 689 | return -EINVAL; |
| 690 | if (WARN_ON(buf_size == 0)) |
| 691 | return -EINVAL; |
| 692 | if (WARN_ON(chunk_size == 0)) |
| 693 | return -EINVAL; |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 694 | |
| 695 | kfd->gtt_sa_chunk_size = chunk_size; |
| 696 | kfd->gtt_sa_num_of_chunks = buf_size / chunk_size; |
| 697 | |
Felix Kuehling | 8625ff9 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 698 | num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) / |
| 699 | BITS_PER_LONG; |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 700 | |
Felix Kuehling | 8625ff9 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 701 | kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL); |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 702 | |
| 703 | if (!kfd->gtt_sa_bitmap) |
| 704 | return -ENOMEM; |
| 705 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 706 | pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n", |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 707 | kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap); |
| 708 | |
| 709 | mutex_init(&kfd->gtt_sa_lock); |
| 710 | |
| 711 | return 0; |
| 712 | |
| 713 | } |
| 714 | |
| 715 | static void kfd_gtt_sa_fini(struct kfd_dev *kfd) |
| 716 | { |
| 717 | mutex_destroy(&kfd->gtt_sa_lock); |
| 718 | kfree(kfd->gtt_sa_bitmap); |
| 719 | } |
| 720 | |
| 721 | static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr, |
| 722 | unsigned int bit_num, |
| 723 | unsigned int chunk_size) |
| 724 | { |
| 725 | return start_addr + bit_num * chunk_size; |
| 726 | } |
| 727 | |
| 728 | static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr, |
| 729 | unsigned int bit_num, |
| 730 | unsigned int chunk_size) |
| 731 | { |
| 732 | return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size); |
| 733 | } |
| 734 | |
| 735 | int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size, |
| 736 | struct kfd_mem_obj **mem_obj) |
| 737 | { |
| 738 | unsigned int found, start_search, cur_size; |
| 739 | |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 740 | if (size == 0) |
| 741 | return -EINVAL; |
| 742 | |
| 743 | if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size) |
| 744 | return -ENOMEM; |
| 745 | |
Felix Kuehling | b91d43d | 2018-04-10 17:33:08 -0400 | [diff] [blame] | 746 | *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_NOIO); |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 747 | if ((*mem_obj) == NULL) |
| 748 | return -ENOMEM; |
| 749 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 750 | pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size); |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 751 | |
| 752 | start_search = 0; |
| 753 | |
| 754 | mutex_lock(&kfd->gtt_sa_lock); |
| 755 | |
| 756 | kfd_gtt_restart_search: |
| 757 | /* Find the first chunk that is free */ |
| 758 | found = find_next_zero_bit(kfd->gtt_sa_bitmap, |
| 759 | kfd->gtt_sa_num_of_chunks, |
| 760 | start_search); |
| 761 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 762 | pr_debug("Found = %d\n", found); |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 763 | |
| 764 | /* If there wasn't any free chunk, bail out */ |
| 765 | if (found == kfd->gtt_sa_num_of_chunks) |
| 766 | goto kfd_gtt_no_free_chunk; |
| 767 | |
| 768 | /* Update fields of mem_obj */ |
| 769 | (*mem_obj)->range_start = found; |
| 770 | (*mem_obj)->range_end = found; |
| 771 | (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr( |
| 772 | kfd->gtt_start_gpu_addr, |
| 773 | found, |
| 774 | kfd->gtt_sa_chunk_size); |
| 775 | (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr( |
| 776 | kfd->gtt_start_cpu_ptr, |
| 777 | found, |
| 778 | kfd->gtt_sa_chunk_size); |
| 779 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 780 | pr_debug("gpu_addr = %p, cpu_addr = %p\n", |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 781 | (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr); |
| 782 | |
| 783 | /* If we need only one chunk, mark it as allocated and get out */ |
| 784 | if (size <= kfd->gtt_sa_chunk_size) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 785 | pr_debug("Single bit\n"); |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 786 | set_bit(found, kfd->gtt_sa_bitmap); |
| 787 | goto kfd_gtt_out; |
| 788 | } |
| 789 | |
| 790 | /* Otherwise, try to see if we have enough contiguous chunks */ |
| 791 | cur_size = size - kfd->gtt_sa_chunk_size; |
| 792 | do { |
| 793 | (*mem_obj)->range_end = |
| 794 | find_next_zero_bit(kfd->gtt_sa_bitmap, |
| 795 | kfd->gtt_sa_num_of_chunks, ++found); |
| 796 | /* |
| 797 | * If next free chunk is not contiguous than we need to |
| 798 | * restart our search from the last free chunk we found (which |
| 799 | * wasn't contiguous to the previous ones |
| 800 | */ |
| 801 | if ((*mem_obj)->range_end != found) { |
| 802 | start_search = found; |
| 803 | goto kfd_gtt_restart_search; |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * If we reached end of buffer, bail out with error |
| 808 | */ |
| 809 | if (found == kfd->gtt_sa_num_of_chunks) |
| 810 | goto kfd_gtt_no_free_chunk; |
| 811 | |
| 812 | /* Check if we don't need another chunk */ |
| 813 | if (cur_size <= kfd->gtt_sa_chunk_size) |
| 814 | cur_size = 0; |
| 815 | else |
| 816 | cur_size -= kfd->gtt_sa_chunk_size; |
| 817 | |
| 818 | } while (cur_size > 0); |
| 819 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 820 | pr_debug("range_start = %d, range_end = %d\n", |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 821 | (*mem_obj)->range_start, (*mem_obj)->range_end); |
| 822 | |
| 823 | /* Mark the chunks as allocated */ |
| 824 | for (found = (*mem_obj)->range_start; |
| 825 | found <= (*mem_obj)->range_end; |
| 826 | found++) |
| 827 | set_bit(found, kfd->gtt_sa_bitmap); |
| 828 | |
| 829 | kfd_gtt_out: |
| 830 | mutex_unlock(&kfd->gtt_sa_lock); |
| 831 | return 0; |
| 832 | |
| 833 | kfd_gtt_no_free_chunk: |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 834 | pr_debug("Allocation failed with mem_obj = %p\n", mem_obj); |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 835 | mutex_unlock(&kfd->gtt_sa_lock); |
| 836 | kfree(mem_obj); |
| 837 | return -ENOMEM; |
| 838 | } |
| 839 | |
| 840 | int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj) |
| 841 | { |
| 842 | unsigned int bit; |
| 843 | |
Oded Gabbay | 9216ed2 | 2015-01-12 22:34:21 +0200 | [diff] [blame] | 844 | /* Act like kfree when trying to free a NULL object */ |
| 845 | if (!mem_obj) |
| 846 | return 0; |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 847 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 848 | pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n", |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 849 | mem_obj, mem_obj->range_start, mem_obj->range_end); |
| 850 | |
| 851 | mutex_lock(&kfd->gtt_sa_lock); |
| 852 | |
| 853 | /* Mark the chunks as free */ |
| 854 | for (bit = mem_obj->range_start; |
| 855 | bit <= mem_obj->range_end; |
| 856 | bit++) |
| 857 | clear_bit(bit, kfd->gtt_sa_bitmap); |
| 858 | |
| 859 | mutex_unlock(&kfd->gtt_sa_lock); |
| 860 | |
| 861 | kfree(mem_obj); |
| 862 | return 0; |
| 863 | } |