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