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 | |
| 23 | #include <linux/amd-iommu.h> |
| 24 | #include <linux/bsearch.h> |
| 25 | #include <linux/pci.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include "kfd_priv.h" |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 28 | #include "kfd_device_queue_manager.h" |
Felix Kuehling | 507968d | 2017-08-15 23:00:15 -0400 | [diff] [blame] | 29 | #include "kfd_pm4_headers_vi.h" |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 30 | #include "cwsr_trap_handler_gfx8.asm" |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 31 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 32 | #define MQD_SIZE_ALIGNED 768 |
| 33 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 34 | static const struct kfd_device_info kaveri_device_info = { |
Ben Goz | 0da7558 | 2015-01-01 17:10:01 +0200 | [diff] [blame] | 35 | .asic_family = CHIP_KAVERI, |
| 36 | .max_pasid_bits = 16, |
Yair Shachar | 992839a | 2015-05-20 13:43:04 +0300 | [diff] [blame] | 37 | /* max num of queues for KV.TODO should be a dynamic value */ |
| 38 | .max_no_of_hqd = 24, |
Ben Goz | 0da7558 | 2015-01-01 17:10:01 +0200 | [diff] [blame] | 39 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
Andrew Lewycky | f3a3981 | 2015-05-10 12:15:46 +0300 | [diff] [blame] | 40 | .event_interrupt_class = &event_interrupt_class_cik, |
Yair Shachar | fbeb661 | 2015-05-20 13:48:26 +0300 | [diff] [blame] | 41 | .num_of_watch_points = 4, |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 42 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 43 | .supports_cwsr = false, |
Felix Kuehling | 3ee2d00 | 2018-01-04 17:17:41 -0500 | [diff] [blame] | 44 | .needs_pci_atomics = false, |
Ben Goz | 0da7558 | 2015-01-01 17:10:01 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static const struct kfd_device_info carrizo_device_info = { |
| 48 | .asic_family = CHIP_CARRIZO, |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 49 | .max_pasid_bits = 16, |
Oded Gabbay | eaccd6e | 2015-06-06 21:45:43 +0300 | [diff] [blame] | 50 | /* max num of queues for CZ.TODO should be a dynamic value */ |
| 51 | .max_no_of_hqd = 24, |
Andrew Lewycky | b3f5e6b | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 52 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
Oded Gabbay | eaccd6e | 2015-06-06 21:45:43 +0300 | [diff] [blame] | 53 | .event_interrupt_class = &event_interrupt_class_cik, |
Alexey Skidanov | f7c826a | 2014-10-13 16:35:12 +0300 | [diff] [blame] | 54 | .num_of_watch_points = 4, |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 55 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 56 | .supports_cwsr = true, |
Felix Kuehling | 3ee2d00 | 2018-01-04 17:17:41 -0500 | [diff] [blame] | 57 | .needs_pci_atomics = false, |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 58 | }; |
| 59 | |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame^] | 60 | static const struct kfd_device_info hawaii_device_info = { |
| 61 | .asic_family = CHIP_HAWAII, |
| 62 | .max_pasid_bits = 16, |
| 63 | /* max num of queues for KV.TODO should be a dynamic value */ |
| 64 | .max_no_of_hqd = 24, |
| 65 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 66 | .event_interrupt_class = &event_interrupt_class_cik, |
| 67 | .num_of_watch_points = 4, |
| 68 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 69 | .supports_cwsr = false, |
| 70 | .needs_pci_atomics = false, |
| 71 | }; |
| 72 | |
| 73 | static const struct kfd_device_info tonga_device_info = { |
| 74 | .asic_family = CHIP_TONGA, |
| 75 | .max_pasid_bits = 16, |
| 76 | .max_no_of_hqd = 24, |
| 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, |
| 82 | .needs_pci_atomics = true, |
| 83 | }; |
| 84 | |
| 85 | static const struct kfd_device_info tonga_vf_device_info = { |
| 86 | .asic_family = CHIP_TONGA, |
| 87 | .max_pasid_bits = 16, |
| 88 | .max_no_of_hqd = 24, |
| 89 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 90 | .event_interrupt_class = &event_interrupt_class_cik, |
| 91 | .num_of_watch_points = 4, |
| 92 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 93 | .supports_cwsr = false, |
| 94 | .needs_pci_atomics = false, |
| 95 | }; |
| 96 | |
| 97 | static const struct kfd_device_info fiji_device_info = { |
| 98 | .asic_family = CHIP_FIJI, |
| 99 | .max_pasid_bits = 16, |
| 100 | .max_no_of_hqd = 24, |
| 101 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 102 | .event_interrupt_class = &event_interrupt_class_cik, |
| 103 | .num_of_watch_points = 4, |
| 104 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 105 | .supports_cwsr = true, |
| 106 | .needs_pci_atomics = true, |
| 107 | }; |
| 108 | |
| 109 | static const struct kfd_device_info fiji_vf_device_info = { |
| 110 | .asic_family = CHIP_FIJI, |
| 111 | .max_pasid_bits = 16, |
| 112 | .max_no_of_hqd = 24, |
| 113 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 114 | .event_interrupt_class = &event_interrupt_class_cik, |
| 115 | .num_of_watch_points = 4, |
| 116 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 117 | .supports_cwsr = true, |
| 118 | .needs_pci_atomics = false, |
| 119 | }; |
| 120 | |
| 121 | |
| 122 | static const struct kfd_device_info polaris10_device_info = { |
| 123 | .asic_family = CHIP_POLARIS10, |
| 124 | .max_pasid_bits = 16, |
| 125 | .max_no_of_hqd = 24, |
| 126 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 127 | .event_interrupt_class = &event_interrupt_class_cik, |
| 128 | .num_of_watch_points = 4, |
| 129 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 130 | .supports_cwsr = true, |
| 131 | .needs_pci_atomics = true, |
| 132 | }; |
| 133 | |
| 134 | static const struct kfd_device_info polaris10_vf_device_info = { |
| 135 | .asic_family = CHIP_POLARIS10, |
| 136 | .max_pasid_bits = 16, |
| 137 | .max_no_of_hqd = 24, |
| 138 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 139 | .event_interrupt_class = &event_interrupt_class_cik, |
| 140 | .num_of_watch_points = 4, |
| 141 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 142 | .supports_cwsr = true, |
| 143 | .needs_pci_atomics = false, |
| 144 | }; |
| 145 | |
| 146 | static const struct kfd_device_info polaris11_device_info = { |
| 147 | .asic_family = CHIP_POLARIS11, |
| 148 | .max_pasid_bits = 16, |
| 149 | .max_no_of_hqd = 24, |
| 150 | .ih_ring_entry_size = 4 * sizeof(uint32_t), |
| 151 | .event_interrupt_class = &event_interrupt_class_cik, |
| 152 | .num_of_watch_points = 4, |
| 153 | .mqd_size_aligned = MQD_SIZE_ALIGNED, |
| 154 | .supports_cwsr = true, |
| 155 | .needs_pci_atomics = true, |
| 156 | }; |
| 157 | |
| 158 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 159 | struct kfd_deviceid { |
| 160 | unsigned short did; |
| 161 | const struct kfd_device_info *device_info; |
| 162 | }; |
| 163 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 164 | static const struct kfd_deviceid supported_devices[] = { |
| 165 | { 0x1304, &kaveri_device_info }, /* Kaveri */ |
| 166 | { 0x1305, &kaveri_device_info }, /* Kaveri */ |
| 167 | { 0x1306, &kaveri_device_info }, /* Kaveri */ |
| 168 | { 0x1307, &kaveri_device_info }, /* Kaveri */ |
| 169 | { 0x1309, &kaveri_device_info }, /* Kaveri */ |
| 170 | { 0x130A, &kaveri_device_info }, /* Kaveri */ |
| 171 | { 0x130B, &kaveri_device_info }, /* Kaveri */ |
| 172 | { 0x130C, &kaveri_device_info }, /* Kaveri */ |
| 173 | { 0x130D, &kaveri_device_info }, /* Kaveri */ |
| 174 | { 0x130E, &kaveri_device_info }, /* Kaveri */ |
| 175 | { 0x130F, &kaveri_device_info }, /* Kaveri */ |
| 176 | { 0x1310, &kaveri_device_info }, /* Kaveri */ |
| 177 | { 0x1311, &kaveri_device_info }, /* Kaveri */ |
| 178 | { 0x1312, &kaveri_device_info }, /* Kaveri */ |
| 179 | { 0x1313, &kaveri_device_info }, /* Kaveri */ |
| 180 | { 0x1315, &kaveri_device_info }, /* Kaveri */ |
| 181 | { 0x1316, &kaveri_device_info }, /* Kaveri */ |
| 182 | { 0x1317, &kaveri_device_info }, /* Kaveri */ |
| 183 | { 0x1318, &kaveri_device_info }, /* Kaveri */ |
| 184 | { 0x131B, &kaveri_device_info }, /* Kaveri */ |
| 185 | { 0x131C, &kaveri_device_info }, /* Kaveri */ |
Ben Goz | 123576d | 2015-01-12 14:37:24 +0200 | [diff] [blame] | 186 | { 0x131D, &kaveri_device_info }, /* Kaveri */ |
| 187 | { 0x9870, &carrizo_device_info }, /* Carrizo */ |
| 188 | { 0x9874, &carrizo_device_info }, /* Carrizo */ |
| 189 | { 0x9875, &carrizo_device_info }, /* Carrizo */ |
| 190 | { 0x9876, &carrizo_device_info }, /* Carrizo */ |
Felix Kuehling | a3084e6 | 2018-01-04 17:17:47 -0500 | [diff] [blame^] | 191 | { 0x9877, &carrizo_device_info }, /* Carrizo */ |
| 192 | { 0x67A0, &hawaii_device_info }, /* Hawaii */ |
| 193 | { 0x67A1, &hawaii_device_info }, /* Hawaii */ |
| 194 | { 0x67A2, &hawaii_device_info }, /* Hawaii */ |
| 195 | { 0x67A8, &hawaii_device_info }, /* Hawaii */ |
| 196 | { 0x67A9, &hawaii_device_info }, /* Hawaii */ |
| 197 | { 0x67AA, &hawaii_device_info }, /* Hawaii */ |
| 198 | { 0x67B0, &hawaii_device_info }, /* Hawaii */ |
| 199 | { 0x67B1, &hawaii_device_info }, /* Hawaii */ |
| 200 | { 0x67B8, &hawaii_device_info }, /* Hawaii */ |
| 201 | { 0x67B9, &hawaii_device_info }, /* Hawaii */ |
| 202 | { 0x67BA, &hawaii_device_info }, /* Hawaii */ |
| 203 | { 0x67BE, &hawaii_device_info }, /* Hawaii */ |
| 204 | { 0x6920, &tonga_device_info }, /* Tonga */ |
| 205 | { 0x6921, &tonga_device_info }, /* Tonga */ |
| 206 | { 0x6928, &tonga_device_info }, /* Tonga */ |
| 207 | { 0x6929, &tonga_device_info }, /* Tonga */ |
| 208 | { 0x692B, &tonga_device_info }, /* Tonga */ |
| 209 | { 0x692F, &tonga_vf_device_info }, /* Tonga vf */ |
| 210 | { 0x6938, &tonga_device_info }, /* Tonga */ |
| 211 | { 0x6939, &tonga_device_info }, /* Tonga */ |
| 212 | { 0x7300, &fiji_device_info }, /* Fiji */ |
| 213 | { 0x730F, &fiji_vf_device_info }, /* Fiji vf*/ |
| 214 | { 0x67C0, &polaris10_device_info }, /* Polaris10 */ |
| 215 | { 0x67C1, &polaris10_device_info }, /* Polaris10 */ |
| 216 | { 0x67C2, &polaris10_device_info }, /* Polaris10 */ |
| 217 | { 0x67C4, &polaris10_device_info }, /* Polaris10 */ |
| 218 | { 0x67C7, &polaris10_device_info }, /* Polaris10 */ |
| 219 | { 0x67C8, &polaris10_device_info }, /* Polaris10 */ |
| 220 | { 0x67C9, &polaris10_device_info }, /* Polaris10 */ |
| 221 | { 0x67CA, &polaris10_device_info }, /* Polaris10 */ |
| 222 | { 0x67CC, &polaris10_device_info }, /* Polaris10 */ |
| 223 | { 0x67CF, &polaris10_device_info }, /* Polaris10 */ |
| 224 | { 0x67D0, &polaris10_vf_device_info }, /* Polaris10 vf*/ |
| 225 | { 0x67DF, &polaris10_device_info }, /* Polaris10 */ |
| 226 | { 0x67E0, &polaris11_device_info }, /* Polaris11 */ |
| 227 | { 0x67E1, &polaris11_device_info }, /* Polaris11 */ |
| 228 | { 0x67E3, &polaris11_device_info }, /* Polaris11 */ |
| 229 | { 0x67E7, &polaris11_device_info }, /* Polaris11 */ |
| 230 | { 0x67E8, &polaris11_device_info }, /* Polaris11 */ |
| 231 | { 0x67E9, &polaris11_device_info }, /* Polaris11 */ |
| 232 | { 0x67EB, &polaris11_device_info }, /* Polaris11 */ |
| 233 | { 0x67EF, &polaris11_device_info }, /* Polaris11 */ |
| 234 | { 0x67FF, &polaris11_device_info }, /* Polaris11 */ |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 235 | }; |
| 236 | |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 237 | static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, |
| 238 | unsigned int chunk_size); |
| 239 | static void kfd_gtt_sa_fini(struct kfd_dev *kfd); |
| 240 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 241 | static int kfd_resume(struct kfd_dev *kfd); |
| 242 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 243 | static const struct kfd_device_info *lookup_device_info(unsigned short did) |
| 244 | { |
| 245 | size_t i; |
| 246 | |
| 247 | for (i = 0; i < ARRAY_SIZE(supported_devices); i++) { |
| 248 | if (supported_devices[i].did == did) { |
Felix Kuehling | 32fa821 | 2017-08-15 23:00:12 -0400 | [diff] [blame] | 249 | WARN_ON(!supported_devices[i].device_info); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 250 | return supported_devices[i].device_info; |
| 251 | } |
| 252 | } |
| 253 | |
Yong Zhao | 4ebc718 | 2017-08-15 23:00:13 -0400 | [diff] [blame] | 254 | dev_warn(kfd_device, "DID %04x is missing in supported_devices\n", |
| 255 | did); |
| 256 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 257 | return NULL; |
| 258 | } |
| 259 | |
Xihan Zhang | cea405b | 2015-03-17 19:32:53 +0800 | [diff] [blame] | 260 | struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, |
| 261 | struct pci_dev *pdev, const struct kfd2kgd_calls *f2g) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 262 | { |
| 263 | struct kfd_dev *kfd; |
| 264 | |
| 265 | const struct kfd_device_info *device_info = |
| 266 | lookup_device_info(pdev->device); |
| 267 | |
Yong Zhao | 4ebc718 | 2017-08-15 23:00:13 -0400 | [diff] [blame] | 268 | if (!device_info) { |
| 269 | dev_err(kfd_device, "kgd2kfd_probe failed\n"); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 270 | return NULL; |
Yong Zhao | 4ebc718 | 2017-08-15 23:00:13 -0400 | [diff] [blame] | 271 | } |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 272 | |
Felix Kuehling | 3ee2d00 | 2018-01-04 17:17:41 -0500 | [diff] [blame] | 273 | if (device_info->needs_pci_atomics) { |
| 274 | /* Allow BIF to recode atomics to PCIe 3.0 |
| 275 | * AtomicOps. 32 and 64-bit requests are possible and |
| 276 | * must be supported. |
| 277 | */ |
| 278 | if (pci_enable_atomic_ops_to_root(pdev, |
| 279 | PCI_EXP_DEVCAP2_ATOMIC_COMP32 | |
| 280 | PCI_EXP_DEVCAP2_ATOMIC_COMP64) < 0) { |
| 281 | dev_info(kfd_device, |
| 282 | "skipped device %x:%x, PCI rejects atomics", |
| 283 | pdev->vendor, pdev->device); |
| 284 | return NULL; |
| 285 | } |
| 286 | } |
| 287 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 288 | kfd = kzalloc(sizeof(*kfd), GFP_KERNEL); |
| 289 | if (!kfd) |
| 290 | return NULL; |
| 291 | |
| 292 | kfd->kgd = kgd; |
| 293 | kfd->device_info = device_info; |
| 294 | kfd->pdev = pdev; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 295 | kfd->init_complete = false; |
Xihan Zhang | cea405b | 2015-03-17 19:32:53 +0800 | [diff] [blame] | 296 | kfd->kfd2kgd = f2g; |
| 297 | |
| 298 | mutex_init(&kfd->doorbell_mutex); |
| 299 | memset(&kfd->doorbell_available_index, 0, |
| 300 | sizeof(kfd->doorbell_available_index)); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 301 | |
| 302 | return kfd; |
| 303 | } |
| 304 | |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 305 | static bool device_iommu_pasid_init(struct kfd_dev *kfd) |
| 306 | { |
| 307 | const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP | |
| 308 | AMD_IOMMU_DEVICE_FLAG_PRI_SUP | |
| 309 | AMD_IOMMU_DEVICE_FLAG_PASID_SUP; |
| 310 | |
| 311 | struct amd_iommu_device_info iommu_info; |
| 312 | unsigned int pasid_limit; |
| 313 | int err; |
| 314 | |
| 315 | err = amd_iommu_device_info(kfd->pdev, &iommu_info); |
| 316 | if (err < 0) { |
| 317 | dev_err(kfd_device, |
| 318 | "error getting iommu info. is the iommu enabled?\n"); |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 323 | dev_err(kfd_device, "error required iommu flags ats %i, pri %i, pasid %i\n", |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 324 | (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0, |
| 325 | (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0, |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 326 | (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) |
| 327 | != 0); |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 328 | return false; |
| 329 | } |
| 330 | |
| 331 | pasid_limit = min_t(unsigned int, |
Kent Russell | 8eabaf5 | 2017-08-15 23:00:04 -0400 | [diff] [blame] | 332 | (unsigned int)(1 << kfd->device_info->max_pasid_bits), |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 333 | iommu_info.max_pasids); |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 334 | |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 335 | if (!kfd_set_pasid_limit(pasid_limit)) { |
| 336 | dev_err(kfd_device, "error setting pasid limit\n"); |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 337 | return false; |
| 338 | } |
| 339 | |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid) |
| 344 | { |
| 345 | struct kfd_dev *dev = kfd_device_by_pci_dev(pdev); |
| 346 | |
| 347 | if (dev) |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 348 | kfd_process_iommu_unbind_callback(dev, pasid); |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 349 | } |
| 350 | |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 351 | /* |
| 352 | * This function called by IOMMU driver on PPR failure |
| 353 | */ |
| 354 | static int iommu_invalid_ppr_cb(struct pci_dev *pdev, int pasid, |
| 355 | unsigned long address, u16 flags) |
| 356 | { |
| 357 | struct kfd_dev *dev; |
| 358 | |
| 359 | dev_warn(kfd_device, |
| 360 | "Invalid PPR device %x:%x.%x pasid %d address 0x%lX flags 0x%X", |
| 361 | PCI_BUS_NUM(pdev->devfn), |
| 362 | PCI_SLOT(pdev->devfn), |
| 363 | PCI_FUNC(pdev->devfn), |
| 364 | pasid, |
| 365 | address, |
| 366 | flags); |
| 367 | |
| 368 | dev = kfd_device_by_pci_dev(pdev); |
Felix Kuehling | 32fa821 | 2017-08-15 23:00:12 -0400 | [diff] [blame] | 369 | if (!WARN_ON(!dev)) |
| 370 | kfd_signal_iommu_event(dev, pasid, address, |
Alexey Skidanov | 59d3e8b | 2015-04-14 18:05:49 +0300 | [diff] [blame] | 371 | flags & PPR_FAULT_WRITE, flags & PPR_FAULT_EXEC); |
| 372 | |
| 373 | return AMD_IOMMU_INV_PRI_RSP_INVALID; |
| 374 | } |
| 375 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 376 | static void kfd_cwsr_init(struct kfd_dev *kfd) |
| 377 | { |
| 378 | if (cwsr_enable && kfd->device_info->supports_cwsr) { |
| 379 | BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE); |
| 380 | |
| 381 | kfd->cwsr_isa = cwsr_trap_gfx8_hex; |
| 382 | kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex); |
| 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 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 465 | if (!device_iommu_pasid_init(kfd)) { |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 466 | dev_err(kfd_device, |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 467 | "Error initializing iommuv2 for device %x:%x\n", |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 468 | kfd->pdev->vendor, kfd->pdev->device); |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 469 | goto device_iommu_pasid_error; |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 470 | } |
| 471 | |
Felix Kuehling | 373d708 | 2017-11-14 16:41:19 -0500 | [diff] [blame] | 472 | kfd_cwsr_init(kfd); |
| 473 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 474 | if (kfd_resume(kfd)) |
| 475 | goto kfd_resume_error; |
| 476 | |
Yair Shachar | fbeb661 | 2015-05-20 13:48:26 +0300 | [diff] [blame] | 477 | kfd->dbgmgr = NULL; |
| 478 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 479 | kfd->init_complete = true; |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 480 | dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor, |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 481 | kfd->pdev->device); |
| 482 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 483 | pr_debug("Starting kfd with the following scheduling policy %d\n", |
Felix Kuehling | d146c5a | 2018-01-04 17:17:43 -0500 | [diff] [blame] | 484 | kfd->dqm->sched_policy); |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 485 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 486 | goto out; |
| 487 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 488 | kfd_resume_error: |
| 489 | device_iommu_pasid_error: |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 490 | device_queue_manager_uninit(kfd->dqm); |
| 491 | device_queue_manager_error: |
Andrew Lewycky | 2249d55 | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 492 | kfd_interrupt_exit(kfd); |
| 493 | kfd_interrupt_error: |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 494 | kfd_topology_remove_device(kfd); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 495 | kfd_topology_add_device_error: |
Felix Kuehling | 735df2b | 2017-08-15 23:00:10 -0400 | [diff] [blame] | 496 | kfd_doorbell_fini(kfd); |
| 497 | kfd_doorbell_error: |
Oded Gabbay | 73a1da0 | 2014-10-26 09:53:37 +0200 | [diff] [blame] | 498 | kfd_gtt_sa_fini(kfd); |
| 499 | kfd_gtt_sa_init_error: |
Xihan Zhang | cea405b | 2015-03-17 19:32:53 +0800 | [diff] [blame] | 500 | kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 501 | dev_err(kfd_device, |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 502 | "device %x:%x NOT added due to errors\n", |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 503 | kfd->pdev->vendor, kfd->pdev->device); |
| 504 | out: |
| 505 | return kfd->init_complete; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void kgd2kfd_device_exit(struct kfd_dev *kfd) |
| 509 | { |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 510 | if (kfd->init_complete) { |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 511 | kgd2kfd_suspend(kfd); |
Ben Goz | 64c7f8c | 2014-07-17 01:27:00 +0300 | [diff] [blame] | 512 | device_queue_manager_uninit(kfd->dqm); |
Andrew Lewycky | 2249d55 | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 513 | kfd_interrupt_exit(kfd); |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 514 | kfd_topology_remove_device(kfd); |
Felix Kuehling | 735df2b | 2017-08-15 23:00:10 -0400 | [diff] [blame] | 515 | kfd_doorbell_fini(kfd); |
Oded Gabbay | 73a1da0 | 2014-10-26 09:53:37 +0200 | [diff] [blame] | 516 | kfd_gtt_sa_fini(kfd); |
Xihan Zhang | cea405b | 2015-03-17 19:32:53 +0800 | [diff] [blame] | 517 | kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem); |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 518 | } |
Evgeny Pinchuk | 5b5c4e4 | 2014-07-16 21:22:32 +0300 | [diff] [blame] | 519 | |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 520 | kfree(kfd); |
| 521 | } |
| 522 | |
| 523 | void kgd2kfd_suspend(struct kfd_dev *kfd) |
| 524 | { |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 525 | if (!kfd->init_complete) |
| 526 | return; |
| 527 | |
| 528 | kfd->dqm->ops.stop(kfd->dqm); |
| 529 | |
| 530 | kfd_unbind_processes_from_device(kfd); |
| 531 | |
| 532 | amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL); |
| 533 | amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL); |
| 534 | amd_iommu_free_device(kfd->pdev); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | int kgd2kfd_resume(struct kfd_dev *kfd) |
| 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 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 542 | return kfd_resume(kfd); |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 543 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 544 | } |
Yong Zhao | 4ebc718 | 2017-08-15 23:00:13 -0400 | [diff] [blame] | 545 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 546 | static int kfd_resume(struct kfd_dev *kfd) |
| 547 | { |
| 548 | int err = 0; |
| 549 | unsigned int pasid_limit = kfd_get_pasid_limit(); |
| 550 | |
| 551 | err = amd_iommu_init_device(kfd->pdev, pasid_limit); |
| 552 | if (err) |
| 553 | return -ENXIO; |
| 554 | amd_iommu_set_invalidate_ctx_cb(kfd->pdev, |
| 555 | iommu_pasid_shutdown_callback); |
| 556 | amd_iommu_set_invalid_ppr_cb(kfd->pdev, |
| 557 | iommu_invalid_ppr_cb); |
| 558 | |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 559 | err = kfd_bind_processes_to_device(kfd); |
| 560 | if (err) |
| 561 | goto processes_bind_error; |
| 562 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 563 | err = kfd->dqm->ops.start(kfd->dqm); |
| 564 | if (err) { |
| 565 | dev_err(kfd_device, |
| 566 | "Error starting queue manager for device %x:%x\n", |
| 567 | kfd->pdev->vendor, kfd->pdev->device); |
| 568 | goto dqm_start_error; |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 569 | } |
| 570 | |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 571 | return err; |
| 572 | |
| 573 | dqm_start_error: |
Yong Zhao | 733fa1f | 2017-09-20 18:10:14 -0400 | [diff] [blame] | 574 | processes_bind_error: |
Yong Zhao | b8935a7 | 2017-09-20 18:10:13 -0400 | [diff] [blame] | 575 | amd_iommu_free_device(kfd->pdev); |
| 576 | |
| 577 | return err; |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 578 | } |
| 579 | |
Andrew Lewycky | b3f5e6b | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 580 | /* This is called directly from KGD at ISR. */ |
| 581 | void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry) |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 582 | { |
Andrew Lewycky | 2249d55 | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 583 | if (!kfd->init_complete) |
| 584 | return; |
| 585 | |
| 586 | spin_lock(&kfd->interrupt_lock); |
| 587 | |
| 588 | if (kfd->interrupts_active |
| 589 | && interrupt_is_wanted(kfd, ih_ring_entry) |
| 590 | && enqueue_ih_ring_entry(kfd, ih_ring_entry)) |
Andres Rodriguez | 48e876a | 2017-10-27 19:35:34 -0400 | [diff] [blame] | 591 | queue_work(kfd->ih_wq, &kfd->interrupt_work); |
Andrew Lewycky | 2249d55 | 2014-07-17 01:37:30 +0300 | [diff] [blame] | 592 | |
| 593 | spin_unlock(&kfd->interrupt_lock); |
Oded Gabbay | 4a488a7 | 2014-07-16 21:08:55 +0300 | [diff] [blame] | 594 | } |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 595 | |
| 596 | static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, |
| 597 | unsigned int chunk_size) |
| 598 | { |
Felix Kuehling | 8625ff9 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 599 | unsigned int num_of_longs; |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 600 | |
Felix Kuehling | 32fa821 | 2017-08-15 23:00:12 -0400 | [diff] [blame] | 601 | if (WARN_ON(buf_size < chunk_size)) |
| 602 | return -EINVAL; |
| 603 | if (WARN_ON(buf_size == 0)) |
| 604 | return -EINVAL; |
| 605 | if (WARN_ON(chunk_size == 0)) |
| 606 | return -EINVAL; |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 607 | |
| 608 | kfd->gtt_sa_chunk_size = chunk_size; |
| 609 | kfd->gtt_sa_num_of_chunks = buf_size / chunk_size; |
| 610 | |
Felix Kuehling | 8625ff9 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 611 | num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) / |
| 612 | BITS_PER_LONG; |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 613 | |
Felix Kuehling | 8625ff9 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 614 | kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL); |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 615 | |
| 616 | if (!kfd->gtt_sa_bitmap) |
| 617 | return -ENOMEM; |
| 618 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 619 | 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] | 620 | kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap); |
| 621 | |
| 622 | mutex_init(&kfd->gtt_sa_lock); |
| 623 | |
| 624 | return 0; |
| 625 | |
| 626 | } |
| 627 | |
| 628 | static void kfd_gtt_sa_fini(struct kfd_dev *kfd) |
| 629 | { |
| 630 | mutex_destroy(&kfd->gtt_sa_lock); |
| 631 | kfree(kfd->gtt_sa_bitmap); |
| 632 | } |
| 633 | |
| 634 | static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr, |
| 635 | unsigned int bit_num, |
| 636 | unsigned int chunk_size) |
| 637 | { |
| 638 | return start_addr + bit_num * chunk_size; |
| 639 | } |
| 640 | |
| 641 | static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr, |
| 642 | unsigned int bit_num, |
| 643 | unsigned int chunk_size) |
| 644 | { |
| 645 | return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size); |
| 646 | } |
| 647 | |
| 648 | int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size, |
| 649 | struct kfd_mem_obj **mem_obj) |
| 650 | { |
| 651 | unsigned int found, start_search, cur_size; |
| 652 | |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 653 | if (size == 0) |
| 654 | return -EINVAL; |
| 655 | |
| 656 | if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size) |
| 657 | return -ENOMEM; |
| 658 | |
| 659 | *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL); |
| 660 | if ((*mem_obj) == NULL) |
| 661 | return -ENOMEM; |
| 662 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 663 | 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] | 664 | |
| 665 | start_search = 0; |
| 666 | |
| 667 | mutex_lock(&kfd->gtt_sa_lock); |
| 668 | |
| 669 | kfd_gtt_restart_search: |
| 670 | /* Find the first chunk that is free */ |
| 671 | found = find_next_zero_bit(kfd->gtt_sa_bitmap, |
| 672 | kfd->gtt_sa_num_of_chunks, |
| 673 | start_search); |
| 674 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 675 | pr_debug("Found = %d\n", found); |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 676 | |
| 677 | /* If there wasn't any free chunk, bail out */ |
| 678 | if (found == kfd->gtt_sa_num_of_chunks) |
| 679 | goto kfd_gtt_no_free_chunk; |
| 680 | |
| 681 | /* Update fields of mem_obj */ |
| 682 | (*mem_obj)->range_start = found; |
| 683 | (*mem_obj)->range_end = found; |
| 684 | (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr( |
| 685 | kfd->gtt_start_gpu_addr, |
| 686 | found, |
| 687 | kfd->gtt_sa_chunk_size); |
| 688 | (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr( |
| 689 | kfd->gtt_start_cpu_ptr, |
| 690 | found, |
| 691 | kfd->gtt_sa_chunk_size); |
| 692 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 693 | pr_debug("gpu_addr = %p, cpu_addr = %p\n", |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 694 | (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr); |
| 695 | |
| 696 | /* If we need only one chunk, mark it as allocated and get out */ |
| 697 | if (size <= kfd->gtt_sa_chunk_size) { |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 698 | pr_debug("Single bit\n"); |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 699 | set_bit(found, kfd->gtt_sa_bitmap); |
| 700 | goto kfd_gtt_out; |
| 701 | } |
| 702 | |
| 703 | /* Otherwise, try to see if we have enough contiguous chunks */ |
| 704 | cur_size = size - kfd->gtt_sa_chunk_size; |
| 705 | do { |
| 706 | (*mem_obj)->range_end = |
| 707 | find_next_zero_bit(kfd->gtt_sa_bitmap, |
| 708 | kfd->gtt_sa_num_of_chunks, ++found); |
| 709 | /* |
| 710 | * If next free chunk is not contiguous than we need to |
| 711 | * restart our search from the last free chunk we found (which |
| 712 | * wasn't contiguous to the previous ones |
| 713 | */ |
| 714 | if ((*mem_obj)->range_end != found) { |
| 715 | start_search = found; |
| 716 | goto kfd_gtt_restart_search; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * If we reached end of buffer, bail out with error |
| 721 | */ |
| 722 | if (found == kfd->gtt_sa_num_of_chunks) |
| 723 | goto kfd_gtt_no_free_chunk; |
| 724 | |
| 725 | /* Check if we don't need another chunk */ |
| 726 | if (cur_size <= kfd->gtt_sa_chunk_size) |
| 727 | cur_size = 0; |
| 728 | else |
| 729 | cur_size -= kfd->gtt_sa_chunk_size; |
| 730 | |
| 731 | } while (cur_size > 0); |
| 732 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 733 | pr_debug("range_start = %d, range_end = %d\n", |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 734 | (*mem_obj)->range_start, (*mem_obj)->range_end); |
| 735 | |
| 736 | /* Mark the chunks as allocated */ |
| 737 | for (found = (*mem_obj)->range_start; |
| 738 | found <= (*mem_obj)->range_end; |
| 739 | found++) |
| 740 | set_bit(found, kfd->gtt_sa_bitmap); |
| 741 | |
| 742 | kfd_gtt_out: |
| 743 | mutex_unlock(&kfd->gtt_sa_lock); |
| 744 | return 0; |
| 745 | |
| 746 | kfd_gtt_no_free_chunk: |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 747 | pr_debug("Allocation failed with mem_obj = %p\n", mem_obj); |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 748 | mutex_unlock(&kfd->gtt_sa_lock); |
| 749 | kfree(mem_obj); |
| 750 | return -ENOMEM; |
| 751 | } |
| 752 | |
| 753 | int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj) |
| 754 | { |
| 755 | unsigned int bit; |
| 756 | |
Oded Gabbay | 9216ed2 | 2015-01-12 22:34:21 +0200 | [diff] [blame] | 757 | /* Act like kfree when trying to free a NULL object */ |
| 758 | if (!mem_obj) |
| 759 | return 0; |
Oded Gabbay | 6e81090 | 2014-10-27 14:36:07 +0200 | [diff] [blame] | 760 | |
Kent Russell | 79775b6 | 2017-08-15 23:00:05 -0400 | [diff] [blame] | 761 | 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] | 762 | mem_obj, mem_obj->range_start, mem_obj->range_end); |
| 763 | |
| 764 | mutex_lock(&kfd->gtt_sa_lock); |
| 765 | |
| 766 | /* Mark the chunks as free */ |
| 767 | for (bit = mem_obj->range_start; |
| 768 | bit <= mem_obj->range_end; |
| 769 | bit++) |
| 770 | clear_bit(bit, kfd->gtt_sa_bitmap); |
| 771 | |
| 772 | mutex_unlock(&kfd->gtt_sa_lock); |
| 773 | |
| 774 | kfree(mem_obj); |
| 775 | return 0; |
| 776 | } |