blob: 56f2bd0a02384e40754d196b58d571188c660c17 [file] [log] [blame]
Oded Gabbay4a488a72014-07-16 21:08:55 +03001/*
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 Gabbay4a488a72014-07-16 21:08:55 +030023#include <linux/bsearch.h>
24#include <linux/pci.h>
25#include <linux/slab.h>
26#include "kfd_priv.h"
Ben Goz64c7f8c2014-07-17 01:27:00 +030027#include "kfd_device_queue_manager.h"
Felix Kuehling507968d2017-08-15 23:00:15 -040028#include "kfd_pm4_headers_vi.h"
Yong Zhao0db54b22018-05-01 17:56:06 -040029#include "cwsr_trap_handler.h"
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050030#include "kfd_iommu.h"
Oded Gabbay4a488a72014-07-16 21:08:55 +030031
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030032#define MQD_SIZE_ALIGNED 768
Shaoyun Liue42051d2018-07-11 22:32:56 -040033
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 */
39static atomic_t kfd_locked = ATOMIC_INIT(0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030040
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050041#ifdef KFD_SUPPORT_IOMMU_V2
Oded Gabbay4a488a72014-07-16 21:08:55 +030042static const struct kfd_device_info kaveri_device_info = {
Ben Goz0da75582015-01-01 17:10:01 +020043 .asic_family = CHIP_KAVERI,
44 .max_pasid_bits = 16,
Yair Shachar992839a2015-05-20 13:43:04 +030045 /* max num of queues for KV.TODO should be a dynamic value */
46 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040047 .doorbell_size = 4,
Ben Goz0da75582015-01-01 17:10:01 +020048 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Andrew Lewyckyf3a39812015-05-10 12:15:46 +030049 .event_interrupt_class = &event_interrupt_class_cik,
Yair Shacharfbeb6612015-05-20 13:48:26 +030050 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050051 .mqd_size_aligned = MQD_SIZE_ALIGNED,
52 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050053 .needs_iommu_device = true,
Felix Kuehling3ee2d002018-01-04 17:17:41 -050054 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -040055 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -050056 .num_sdma_queues_per_engine = 2,
Ben Goz0da75582015-01-01 17:10:01 +020057};
58
59static const struct kfd_device_info carrizo_device_info = {
60 .asic_family = CHIP_CARRIZO,
Oded Gabbay4a488a72014-07-16 21:08:55 +030061 .max_pasid_bits = 16,
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030062 /* max num of queues for CZ.TODO should be a dynamic value */
63 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040064 .doorbell_size = 4,
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +030065 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030066 .event_interrupt_class = &event_interrupt_class_cik,
Alexey Skidanovf7c826a2014-10-13 16:35:12 +030067 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050068 .mqd_size_aligned = MQD_SIZE_ALIGNED,
69 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050070 .needs_iommu_device = true,
Felix Kuehling3ee2d002018-01-04 17:17:41 -050071 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -040072 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -050073 .num_sdma_queues_per_engine = 2,
Oded Gabbay4a488a72014-07-16 21:08:55 +030074};
Yong Zhao4d663df2018-07-13 16:17:48 -040075
76static 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 Liud5094182018-02-09 16:29:14 -050089 .num_sdma_queues_per_engine = 2,
Yong Zhao4d663df2018-07-13 16:17:48 -040090};
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050091#endif
Oded Gabbay4a488a72014-07-16 21:08:55 +030092
Felix Kuehlinga3084e62018-01-04 17:17:47 -050093static 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 Kuehlingada2b292018-04-10 17:33:03 -040098 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -050099 .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 Kuehling64d1c3a2017-12-08 19:22:12 -0500104 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500105 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -0400106 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -0500107 .num_sdma_queues_per_engine = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500108};
109
110static 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 Kuehlingada2b292018-04-10 17:33:03 -0400114 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500115 .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 Kuehling64d1c3a2017-12-08 19:22:12 -0500120 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500121 .needs_pci_atomics = true,
Yong Zhao98bb9222018-07-13 16:17:44 -0400122 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -0500123 .num_sdma_queues_per_engine = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500124};
125
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500126static 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 Kuehlingada2b292018-04-10 17:33:03 -0400130 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500131 .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 Kuehling64d1c3a2017-12-08 19:22:12 -0500136 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500137 .needs_pci_atomics = true,
Yong Zhao98bb9222018-07-13 16:17:44 -0400138 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -0500139 .num_sdma_queues_per_engine = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500140};
141
142static 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 Kuehlingada2b292018-04-10 17:33:03 -0400146 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500147 .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 Kuehling64d1c3a2017-12-08 19:22:12 -0500152 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500153 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -0400154 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -0500155 .num_sdma_queues_per_engine = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500156};
157
158
159static 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 Kuehlingada2b292018-04-10 17:33:03 -0400163 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500164 .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 Kuehling64d1c3a2017-12-08 19:22:12 -0500169 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500170 .needs_pci_atomics = true,
Yong Zhao98bb9222018-07-13 16:17:44 -0400171 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -0500172 .num_sdma_queues_per_engine = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500173};
174
175static 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 Kuehlingada2b292018-04-10 17:33:03 -0400179 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500180 .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 Kuehling64d1c3a2017-12-08 19:22:12 -0500185 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500186 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -0400187 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -0500188 .num_sdma_queues_per_engine = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500189};
190
191static 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 Kuehlingada2b292018-04-10 17:33:03 -0400195 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500196 .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 Kuehling64d1c3a2017-12-08 19:22:12 -0500201 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500202 .needs_pci_atomics = true,
Yong Zhao98bb9222018-07-13 16:17:44 -0400203 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -0500204 .num_sdma_queues_per_engine = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500205};
206
Felix Kuehling389056e2018-04-10 17:33:18 -0400207static 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 Zhao98bb9222018-07-13 16:17:44 -0400219 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -0500220 .num_sdma_queues_per_engine = 2,
Felix Kuehling389056e2018-04-10 17:33:18 -0400221};
222
223static 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 Zhao98bb9222018-07-13 16:17:44 -0400235 .num_sdma_engines = 2,
Shaoyun Liud5094182018-02-09 16:29:14 -0500236 .num_sdma_queues_per_engine = 2,
Felix Kuehling389056e2018-04-10 17:33:18 -0400237};
238
Oded Gabbay4a488a72014-07-16 21:08:55 +0300239struct kfd_deviceid {
240 unsigned short did;
241 const struct kfd_device_info *device_info;
242};
243
Oded Gabbay4a488a72014-07-16 21:08:55 +0300244static const struct kfd_deviceid supported_devices[] = {
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500245#ifdef KFD_SUPPORT_IOMMU_V2
Oded Gabbay4a488a72014-07-16 21:08:55 +0300246 { 0x1304, &kaveri_device_info }, /* Kaveri */
247 { 0x1305, &kaveri_device_info }, /* Kaveri */
248 { 0x1306, &kaveri_device_info }, /* Kaveri */
249 { 0x1307, &kaveri_device_info }, /* Kaveri */
250 { 0x1309, &kaveri_device_info }, /* Kaveri */
251 { 0x130A, &kaveri_device_info }, /* Kaveri */
252 { 0x130B, &kaveri_device_info }, /* Kaveri */
253 { 0x130C, &kaveri_device_info }, /* Kaveri */
254 { 0x130D, &kaveri_device_info }, /* Kaveri */
255 { 0x130E, &kaveri_device_info }, /* Kaveri */
256 { 0x130F, &kaveri_device_info }, /* Kaveri */
257 { 0x1310, &kaveri_device_info }, /* Kaveri */
258 { 0x1311, &kaveri_device_info }, /* Kaveri */
259 { 0x1312, &kaveri_device_info }, /* Kaveri */
260 { 0x1313, &kaveri_device_info }, /* Kaveri */
261 { 0x1315, &kaveri_device_info }, /* Kaveri */
262 { 0x1316, &kaveri_device_info }, /* Kaveri */
263 { 0x1317, &kaveri_device_info }, /* Kaveri */
264 { 0x1318, &kaveri_device_info }, /* Kaveri */
265 { 0x131B, &kaveri_device_info }, /* Kaveri */
266 { 0x131C, &kaveri_device_info }, /* Kaveri */
Ben Goz123576d2015-01-12 14:37:24 +0200267 { 0x131D, &kaveri_device_info }, /* Kaveri */
268 { 0x9870, &carrizo_device_info }, /* Carrizo */
269 { 0x9874, &carrizo_device_info }, /* Carrizo */
270 { 0x9875, &carrizo_device_info }, /* Carrizo */
271 { 0x9876, &carrizo_device_info }, /* Carrizo */
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500272 { 0x9877, &carrizo_device_info }, /* Carrizo */
Yong Zhao4d663df2018-07-13 16:17:48 -0400273 { 0x15DD, &raven_device_info }, /* Raven */
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500274#endif
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500275 { 0x67A0, &hawaii_device_info }, /* Hawaii */
276 { 0x67A1, &hawaii_device_info }, /* Hawaii */
277 { 0x67A2, &hawaii_device_info }, /* Hawaii */
278 { 0x67A8, &hawaii_device_info }, /* Hawaii */
279 { 0x67A9, &hawaii_device_info }, /* Hawaii */
280 { 0x67AA, &hawaii_device_info }, /* Hawaii */
281 { 0x67B0, &hawaii_device_info }, /* Hawaii */
282 { 0x67B1, &hawaii_device_info }, /* Hawaii */
283 { 0x67B8, &hawaii_device_info }, /* Hawaii */
284 { 0x67B9, &hawaii_device_info }, /* Hawaii */
285 { 0x67BA, &hawaii_device_info }, /* Hawaii */
286 { 0x67BE, &hawaii_device_info }, /* Hawaii */
287 { 0x6920, &tonga_device_info }, /* Tonga */
288 { 0x6921, &tonga_device_info }, /* Tonga */
289 { 0x6928, &tonga_device_info }, /* Tonga */
290 { 0x6929, &tonga_device_info }, /* Tonga */
291 { 0x692B, &tonga_device_info }, /* Tonga */
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500292 { 0x6938, &tonga_device_info }, /* Tonga */
293 { 0x6939, &tonga_device_info }, /* Tonga */
294 { 0x7300, &fiji_device_info }, /* Fiji */
295 { 0x730F, &fiji_vf_device_info }, /* Fiji vf*/
296 { 0x67C0, &polaris10_device_info }, /* Polaris10 */
297 { 0x67C1, &polaris10_device_info }, /* Polaris10 */
298 { 0x67C2, &polaris10_device_info }, /* Polaris10 */
299 { 0x67C4, &polaris10_device_info }, /* Polaris10 */
300 { 0x67C7, &polaris10_device_info }, /* Polaris10 */
301 { 0x67C8, &polaris10_device_info }, /* Polaris10 */
302 { 0x67C9, &polaris10_device_info }, /* Polaris10 */
303 { 0x67CA, &polaris10_device_info }, /* Polaris10 */
304 { 0x67CC, &polaris10_device_info }, /* Polaris10 */
305 { 0x67CF, &polaris10_device_info }, /* Polaris10 */
306 { 0x67D0, &polaris10_vf_device_info }, /* Polaris10 vf*/
307 { 0x67DF, &polaris10_device_info }, /* Polaris10 */
308 { 0x67E0, &polaris11_device_info }, /* Polaris11 */
309 { 0x67E1, &polaris11_device_info }, /* Polaris11 */
310 { 0x67E3, &polaris11_device_info }, /* Polaris11 */
311 { 0x67E7, &polaris11_device_info }, /* Polaris11 */
312 { 0x67E8, &polaris11_device_info }, /* Polaris11 */
313 { 0x67E9, &polaris11_device_info }, /* Polaris11 */
314 { 0x67EB, &polaris11_device_info }, /* Polaris11 */
315 { 0x67EF, &polaris11_device_info }, /* Polaris11 */
316 { 0x67FF, &polaris11_device_info }, /* Polaris11 */
Felix Kuehling389056e2018-04-10 17:33:18 -0400317 { 0x6860, &vega10_device_info }, /* Vega10 */
318 { 0x6861, &vega10_device_info }, /* Vega10 */
319 { 0x6862, &vega10_device_info }, /* Vega10 */
320 { 0x6863, &vega10_device_info }, /* Vega10 */
321 { 0x6864, &vega10_device_info }, /* Vega10 */
322 { 0x6867, &vega10_device_info }, /* Vega10 */
323 { 0x6868, &vega10_device_info }, /* Vega10 */
324 { 0x686C, &vega10_vf_device_info }, /* Vega10 vf*/
325 { 0x687F, &vega10_device_info }, /* Vega10 */
Oded Gabbay4a488a72014-07-16 21:08:55 +0300326};
327
Oded Gabbay6e810902014-10-27 14:36:07 +0200328static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
329 unsigned int chunk_size);
330static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
331
Yong Zhaob8935a72017-09-20 18:10:13 -0400332static int kfd_resume(struct kfd_dev *kfd);
333
Oded Gabbay4a488a72014-07-16 21:08:55 +0300334static const struct kfd_device_info *lookup_device_info(unsigned short did)
335{
336 size_t i;
337
338 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
339 if (supported_devices[i].did == did) {
Felix Kuehling32fa8212017-08-15 23:00:12 -0400340 WARN_ON(!supported_devices[i].device_info);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300341 return supported_devices[i].device_info;
342 }
343 }
344
Yong Zhao4ebc7182017-08-15 23:00:13 -0400345 dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
346 did);
347
Oded Gabbay4a488a72014-07-16 21:08:55 +0300348 return NULL;
349}
350
Xihan Zhangcea405b2015-03-17 19:32:53 +0800351struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
352 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300353{
354 struct kfd_dev *kfd;
welu6106dce2018-04-10 17:33:17 -0400355 int ret;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300356 const struct kfd_device_info *device_info =
357 lookup_device_info(pdev->device);
358
Yong Zhao4ebc7182017-08-15 23:00:13 -0400359 if (!device_info) {
360 dev_err(kfd_device, "kgd2kfd_probe failed\n");
Oded Gabbay4a488a72014-07-16 21:08:55 +0300361 return NULL;
Yong Zhao4ebc7182017-08-15 23:00:13 -0400362 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300363
Eric Huangd35f00d2018-06-04 15:22:24 -0400364 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
365 if (!kfd)
366 return NULL;
367
welu6106dce2018-04-10 17:33:17 -0400368 /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
369 * 32 and 64-bit requests are possible and must be
370 * supported.
371 */
372 ret = pci_enable_atomic_ops_to_root(pdev,
373 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
374 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
375 if (device_info->needs_pci_atomics && ret < 0) {
376 dev_info(kfd_device,
377 "skipped device %x:%x, PCI rejects atomics\n",
378 pdev->vendor, pdev->device);
Eric Huangd35f00d2018-06-04 15:22:24 -0400379 kfree(kfd);
welu6106dce2018-04-10 17:33:17 -0400380 return NULL;
Eric Huangd35f00d2018-06-04 15:22:24 -0400381 } else if (!ret)
382 kfd->pci_atomic_requested = true;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300383
384 kfd->kgd = kgd;
385 kfd->device_info = device_info;
386 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300387 kfd->init_complete = false;
Xihan Zhangcea405b2015-03-17 19:32:53 +0800388 kfd->kfd2kgd = f2g;
389
390 mutex_init(&kfd->doorbell_mutex);
391 memset(&kfd->doorbell_available_index, 0,
392 sizeof(kfd->doorbell_available_index));
Oded Gabbay4a488a72014-07-16 21:08:55 +0300393
394 return kfd;
395}
396
Felix Kuehling373d7082017-11-14 16:41:19 -0500397static void kfd_cwsr_init(struct kfd_dev *kfd)
398{
399 if (cwsr_enable && kfd->device_info->supports_cwsr) {
Felix Kuehling3e76c232018-04-10 17:33:16 -0400400 if (kfd->device_info->asic_family < CHIP_VEGA10) {
401 BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
402 kfd->cwsr_isa = cwsr_trap_gfx8_hex;
403 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
404 } else {
405 BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
406 kfd->cwsr_isa = cwsr_trap_gfx9_hex;
407 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
408 }
Felix Kuehling373d7082017-11-14 16:41:19 -0500409
Felix Kuehling373d7082017-11-14 16:41:19 -0500410 kfd->cwsr_enabled = true;
411 }
412}
413
Oded Gabbay4a488a72014-07-16 21:08:55 +0300414bool kgd2kfd_device_init(struct kfd_dev *kfd,
415 const struct kgd2kfd_shared_resources *gpu_resources)
416{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300417 unsigned int size;
418
Felix Kuehling5ade6c92018-08-20 20:15:00 -0400419 kfd->mec_fw_version = kfd->kfd2kgd->get_fw_version(kfd->kgd,
420 KGD_ENGINE_MEC1);
421 kfd->sdma_fw_version = kfd->kfd2kgd->get_fw_version(kfd->kgd,
422 KGD_ENGINE_SDMA1);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300423 kfd->shared_resources = *gpu_resources;
424
Yong Zhao44008d72017-09-20 18:10:18 -0400425 kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
426 kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
427 kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
428 - kfd->vm_info.first_vmid_kfd + 1;
429
Felix Kuehlinga99c6d42017-11-27 18:29:45 -0500430 /* Verify module parameters regarding mapped process number*/
431 if ((hws_max_conc_proc < 0)
432 || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
433 dev_err(kfd_device,
434 "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
435 hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
436 kfd->vm_info.vmid_num_kfd);
437 kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
438 } else
439 kfd->max_proc_per_quantum = hws_max_conc_proc;
440
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300441 /* calculate max size of mqds needed for queues */
Oded Gabbayb8cbab02015-01-18 13:18:01 +0200442 size = max_num_of_queues_per_device *
443 kfd->device_info->mqd_size_aligned;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300444
Oded Gabbaye18e7942014-10-26 10:12:22 +0200445 /*
446 * calculate max size of runlist packet.
447 * There can be only 2 packets at once
448 */
Felix Kuehling507968d2017-08-15 23:00:15 -0400449 size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
450 max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
451 + sizeof(struct pm4_mes_runlist)) * 2;
Oded Gabbaye18e7942014-10-26 10:12:22 +0200452
453 /* Add size of HIQ & DIQ */
454 size += KFD_KERNEL_QUEUE_SIZE * 2;
455
456 /* add another 512KB for all other allocations on gart (HPD, fences) */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300457 size += 512 * 1024;
458
Xihan Zhangcea405b2015-03-17 19:32:53 +0800459 if (kfd->kfd2kgd->init_gtt_mem_allocation(
460 kfd->kgd, size, &kfd->gtt_mem,
Yong Zhao15426db2018-09-12 21:42:19 -0400461 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
462 false)) {
Kent Russell79775b62017-08-15 23:00:05 -0400463 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300464 goto out;
465 }
466
Kent Russell79775b62017-08-15 23:00:05 -0400467 dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
Oded Gabbaye18e7942014-10-26 10:12:22 +0200468
Oded Gabbay73a1da02014-10-26 09:53:37 +0200469 /* Initialize GTT sa with 512 byte chunk size */
470 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
Kent Russell79775b62017-08-15 23:00:05 -0400471 dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
Oded Gabbay73a1da02014-10-26 09:53:37 +0200472 goto kfd_gtt_sa_init_error;
473 }
474
Felix Kuehling735df2b2017-08-15 23:00:10 -0400475 if (kfd_doorbell_init(kfd)) {
476 dev_err(kfd_device,
477 "Error initializing doorbell aperture\n");
478 goto kfd_doorbell_error;
479 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300480
Shaoyun Liu0c1690e2018-07-06 11:32:42 -0400481 if (kfd->kfd2kgd->get_hive_id)
482 kfd->hive_id = kfd->kfd2kgd->get_hive_id(kfd->kgd);
483
Kent Russell4eacc26b2017-08-15 23:00:06 -0400484 if (kfd_topology_add_device(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400485 dev_err(kfd_device, "Error adding device to topology\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300486 goto kfd_topology_add_device_error;
487 }
488
Andrew Lewycky2249d552014-07-17 01:37:30 +0300489 if (kfd_interrupt_init(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400490 dev_err(kfd_device, "Error initializing interrupts\n");
Andrew Lewycky2249d552014-07-17 01:37:30 +0300491 goto kfd_interrupt_error;
492 }
493
Ben Goz64c7f8c2014-07-17 01:27:00 +0300494 kfd->dqm = device_queue_manager_init(kfd);
495 if (!kfd->dqm) {
Kent Russell79775b62017-08-15 23:00:05 -0400496 dev_err(kfd_device, "Error initializing queue manager\n");
Ben Goz64c7f8c2014-07-17 01:27:00 +0300497 goto device_queue_manager_error;
498 }
499
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500500 if (kfd_iommu_device_init(kfd)) {
501 dev_err(kfd_device, "Error initializing iommuv2\n");
502 goto device_iommu_error;
Ben Goz64c7f8c2014-07-17 01:27:00 +0300503 }
504
Felix Kuehling373d7082017-11-14 16:41:19 -0500505 kfd_cwsr_init(kfd);
506
Yong Zhaob8935a72017-09-20 18:10:13 -0400507 if (kfd_resume(kfd))
508 goto kfd_resume_error;
509
Yair Shacharfbeb6612015-05-20 13:48:26 +0300510 kfd->dbgmgr = NULL;
511
Oded Gabbay4a488a72014-07-16 21:08:55 +0300512 kfd->init_complete = true;
Kent Russell79775b62017-08-15 23:00:05 -0400513 dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
Oded Gabbay4a488a72014-07-16 21:08:55 +0300514 kfd->pdev->device);
515
Kent Russell79775b62017-08-15 23:00:05 -0400516 pr_debug("Starting kfd with the following scheduling policy %d\n",
Felix Kuehlingd146c5a2018-01-04 17:17:43 -0500517 kfd->dqm->sched_policy);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300518
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300519 goto out;
520
Yong Zhaob8935a72017-09-20 18:10:13 -0400521kfd_resume_error:
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500522device_iommu_error:
Ben Goz64c7f8c2014-07-17 01:27:00 +0300523 device_queue_manager_uninit(kfd->dqm);
524device_queue_manager_error:
Andrew Lewycky2249d552014-07-17 01:37:30 +0300525 kfd_interrupt_exit(kfd);
526kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300527 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300528kfd_topology_add_device_error:
Felix Kuehling735df2b2017-08-15 23:00:10 -0400529 kfd_doorbell_fini(kfd);
530kfd_doorbell_error:
Oded Gabbay73a1da02014-10-26 09:53:37 +0200531 kfd_gtt_sa_fini(kfd);
532kfd_gtt_sa_init_error:
Xihan Zhangcea405b2015-03-17 19:32:53 +0800533 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300534 dev_err(kfd_device,
Kent Russell79775b62017-08-15 23:00:05 -0400535 "device %x:%x NOT added due to errors\n",
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300536 kfd->pdev->vendor, kfd->pdev->device);
537out:
538 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300539}
540
541void kgd2kfd_device_exit(struct kfd_dev *kfd)
542{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300543 if (kfd->init_complete) {
Yong Zhaob8935a72017-09-20 18:10:13 -0400544 kgd2kfd_suspend(kfd);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300545 device_queue_manager_uninit(kfd->dqm);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300546 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300547 kfd_topology_remove_device(kfd);
Felix Kuehling735df2b2017-08-15 23:00:10 -0400548 kfd_doorbell_fini(kfd);
Oded Gabbay73a1da02014-10-26 09:53:37 +0200549 kfd_gtt_sa_fini(kfd);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800550 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300551 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300552
Oded Gabbay4a488a72014-07-16 21:08:55 +0300553 kfree(kfd);
554}
555
Shaoyun Liue3b7a962018-07-11 22:32:54 -0400556int kgd2kfd_pre_reset(struct kfd_dev *kfd)
557{
Shaoyun Liue42051d2018-07-11 22:32:56 -0400558 if (!kfd->init_complete)
559 return 0;
560 kgd2kfd_suspend(kfd);
561
562 /* hold dqm->lock to prevent further execution*/
563 dqm_lock(kfd->dqm);
564
565 kfd_signal_reset_event(kfd);
Shaoyun Liue3b7a962018-07-11 22:32:54 -0400566 return 0;
567}
568
Shaoyun Liue42051d2018-07-11 22:32:56 -0400569/*
570 * Fix me. KFD won't be able to resume existing process for now.
571 * We will keep all existing process in a evicted state and
572 * wait the process to be terminated.
573 */
574
Shaoyun Liue3b7a962018-07-11 22:32:54 -0400575int kgd2kfd_post_reset(struct kfd_dev *kfd)
576{
Shaoyun Liue42051d2018-07-11 22:32:56 -0400577 int ret, count;
578
579 if (!kfd->init_complete)
580 return 0;
581
582 dqm_unlock(kfd->dqm);
583
584 ret = kfd_resume(kfd);
585 if (ret)
586 return ret;
587 count = atomic_dec_return(&kfd_locked);
588 WARN_ONCE(count != 0, "KFD reset ref. error");
Shaoyun Liue3b7a962018-07-11 22:32:54 -0400589 return 0;
590}
591
Shaoyun Liue42051d2018-07-11 22:32:56 -0400592bool kfd_is_locked(void)
593{
594 return (atomic_read(&kfd_locked) > 0);
595}
596
Oded Gabbay4a488a72014-07-16 21:08:55 +0300597void kgd2kfd_suspend(struct kfd_dev *kfd)
598{
Yong Zhao733fa1f2017-09-20 18:10:14 -0400599 if (!kfd->init_complete)
600 return;
601
Felix Kuehling26103432018-02-06 20:32:45 -0500602 /* For first KFD device suspend all the KFD processes */
Shaoyun Liue42051d2018-07-11 22:32:56 -0400603 if (atomic_inc_return(&kfd_locked) == 1)
Felix Kuehling26103432018-02-06 20:32:45 -0500604 kfd_suspend_all_processes();
605
Yong Zhao733fa1f2017-09-20 18:10:14 -0400606 kfd->dqm->ops.stop(kfd->dqm);
607
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500608 kfd_iommu_suspend(kfd);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300609}
610
611int kgd2kfd_resume(struct kfd_dev *kfd)
612{
Felix Kuehling26103432018-02-06 20:32:45 -0500613 int ret, count;
614
Yong Zhaob8935a72017-09-20 18:10:13 -0400615 if (!kfd->init_complete)
616 return 0;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300617
Felix Kuehling26103432018-02-06 20:32:45 -0500618 ret = kfd_resume(kfd);
619 if (ret)
620 return ret;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300621
Shaoyun Liue42051d2018-07-11 22:32:56 -0400622 count = atomic_dec_return(&kfd_locked);
Felix Kuehling26103432018-02-06 20:32:45 -0500623 WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
624 if (count == 0)
625 ret = kfd_resume_all_processes();
626
627 return ret;
Yong Zhaob8935a72017-09-20 18:10:13 -0400628}
Yong Zhao4ebc7182017-08-15 23:00:13 -0400629
Yong Zhaob8935a72017-09-20 18:10:13 -0400630static int kfd_resume(struct kfd_dev *kfd)
631{
632 int err = 0;
Yong Zhaob8935a72017-09-20 18:10:13 -0400633
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500634 err = kfd_iommu_resume(kfd);
635 if (err) {
636 dev_err(kfd_device,
637 "Failed to resume IOMMU for device %x:%x\n",
638 kfd->pdev->vendor, kfd->pdev->device);
639 return err;
640 }
Yong Zhao733fa1f2017-09-20 18:10:14 -0400641
Yong Zhaob8935a72017-09-20 18:10:13 -0400642 err = kfd->dqm->ops.start(kfd->dqm);
643 if (err) {
644 dev_err(kfd_device,
645 "Error starting queue manager for device %x:%x\n",
646 kfd->pdev->vendor, kfd->pdev->device);
647 goto dqm_start_error;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300648 }
649
Yong Zhaob8935a72017-09-20 18:10:13 -0400650 return err;
651
652dqm_start_error:
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500653 kfd_iommu_suspend(kfd);
Yong Zhaob8935a72017-09-20 18:10:13 -0400654 return err;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300655}
656
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300657/* This is called directly from KGD at ISR. */
658void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300659{
Lan Xiao58e69882018-07-11 22:32:51 -0400660 uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
661 bool is_patched = false;
662
Andrew Lewycky2249d552014-07-17 01:37:30 +0300663 if (!kfd->init_complete)
664 return;
665
Lan Xiao58e69882018-07-11 22:32:51 -0400666 if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
667 dev_err_once(kfd_device, "Ring entry too small\n");
668 return;
669 }
670
Andrew Lewycky2249d552014-07-17 01:37:30 +0300671 spin_lock(&kfd->interrupt_lock);
672
673 if (kfd->interrupts_active
Lan Xiao58e69882018-07-11 22:32:51 -0400674 && interrupt_is_wanted(kfd, ih_ring_entry,
675 patched_ihre, &is_patched)
676 && enqueue_ih_ring_entry(kfd,
677 is_patched ? patched_ihre : ih_ring_entry))
Andres Rodriguez48e876a2017-10-27 19:35:34 -0400678 queue_work(kfd->ih_wq, &kfd->interrupt_work);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300679
680 spin_unlock(&kfd->interrupt_lock);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300681}
Oded Gabbay6e810902014-10-27 14:36:07 +0200682
Felix Kuehling6b95e792018-03-23 15:32:32 -0400683int kgd2kfd_quiesce_mm(struct mm_struct *mm)
684{
685 struct kfd_process *p;
686 int r;
687
688 /* Because we are called from arbitrary context (workqueue) as opposed
689 * to process context, kfd_process could attempt to exit while we are
690 * running so the lookup function increments the process ref count.
691 */
692 p = kfd_lookup_process_by_mm(mm);
693 if (!p)
694 return -ESRCH;
695
696 r = kfd_process_evict_queues(p);
697
698 kfd_unref_process(p);
699 return r;
700}
701
702int kgd2kfd_resume_mm(struct mm_struct *mm)
703{
704 struct kfd_process *p;
705 int r;
706
707 /* Because we are called from arbitrary context (workqueue) as opposed
708 * to process context, kfd_process could attempt to exit while we are
709 * running so the lookup function increments the process ref count.
710 */
711 p = kfd_lookup_process_by_mm(mm);
712 if (!p)
713 return -ESRCH;
714
715 r = kfd_process_restore_queues(p);
716
717 kfd_unref_process(p);
718 return r;
719}
720
Felix Kuehling26103432018-02-06 20:32:45 -0500721/** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
722 * prepare for safe eviction of KFD BOs that belong to the specified
723 * process.
724 *
725 * @mm: mm_struct that identifies the specified KFD process
726 * @fence: eviction fence attached to KFD process BOs
727 *
728 */
729int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
730 struct dma_fence *fence)
731{
732 struct kfd_process *p;
733 unsigned long active_time;
734 unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
735
736 if (!fence)
737 return -EINVAL;
738
739 if (dma_fence_is_signaled(fence))
740 return 0;
741
742 p = kfd_lookup_process_by_mm(mm);
743 if (!p)
744 return -ENODEV;
745
746 if (fence->seqno == p->last_eviction_seqno)
747 goto out;
748
749 p->last_eviction_seqno = fence->seqno;
750
751 /* Avoid KFD process starvation. Wait for at least
752 * PROCESS_ACTIVE_TIME_MS before evicting the process again
753 */
754 active_time = get_jiffies_64() - p->last_restore_timestamp;
755 if (delay_jiffies > active_time)
756 delay_jiffies -= active_time;
757 else
758 delay_jiffies = 0;
759
760 /* During process initialization eviction_work.dwork is initialized
761 * to kfd_evict_bo_worker
762 */
763 schedule_delayed_work(&p->eviction_work, delay_jiffies);
764out:
765 kfd_unref_process(p);
766 return 0;
767}
768
Oded Gabbay6e810902014-10-27 14:36:07 +0200769static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
770 unsigned int chunk_size)
771{
Felix Kuehling8625ff92017-08-15 23:00:11 -0400772 unsigned int num_of_longs;
Oded Gabbay6e810902014-10-27 14:36:07 +0200773
Felix Kuehling32fa8212017-08-15 23:00:12 -0400774 if (WARN_ON(buf_size < chunk_size))
775 return -EINVAL;
776 if (WARN_ON(buf_size == 0))
777 return -EINVAL;
778 if (WARN_ON(chunk_size == 0))
779 return -EINVAL;
Oded Gabbay6e810902014-10-27 14:36:07 +0200780
781 kfd->gtt_sa_chunk_size = chunk_size;
782 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
783
Felix Kuehling8625ff92017-08-15 23:00:11 -0400784 num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
785 BITS_PER_LONG;
Oded Gabbay6e810902014-10-27 14:36:07 +0200786
Felix Kuehling8625ff92017-08-15 23:00:11 -0400787 kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
Oded Gabbay6e810902014-10-27 14:36:07 +0200788
789 if (!kfd->gtt_sa_bitmap)
790 return -ENOMEM;
791
Kent Russell79775b62017-08-15 23:00:05 -0400792 pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200793 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
794
795 mutex_init(&kfd->gtt_sa_lock);
796
797 return 0;
798
799}
800
801static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
802{
803 mutex_destroy(&kfd->gtt_sa_lock);
804 kfree(kfd->gtt_sa_bitmap);
805}
806
807static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
808 unsigned int bit_num,
809 unsigned int chunk_size)
810{
811 return start_addr + bit_num * chunk_size;
812}
813
814static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
815 unsigned int bit_num,
816 unsigned int chunk_size)
817{
818 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
819}
820
821int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
822 struct kfd_mem_obj **mem_obj)
823{
824 unsigned int found, start_search, cur_size;
825
Oded Gabbay6e810902014-10-27 14:36:07 +0200826 if (size == 0)
827 return -EINVAL;
828
829 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
830 return -ENOMEM;
831
Felix Kuehling1cd106e2018-07-11 22:32:45 -0400832 *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
833 if (!(*mem_obj))
Oded Gabbay6e810902014-10-27 14:36:07 +0200834 return -ENOMEM;
835
Kent Russell79775b62017-08-15 23:00:05 -0400836 pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
Oded Gabbay6e810902014-10-27 14:36:07 +0200837
838 start_search = 0;
839
840 mutex_lock(&kfd->gtt_sa_lock);
841
842kfd_gtt_restart_search:
843 /* Find the first chunk that is free */
844 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
845 kfd->gtt_sa_num_of_chunks,
846 start_search);
847
Kent Russell79775b62017-08-15 23:00:05 -0400848 pr_debug("Found = %d\n", found);
Oded Gabbay6e810902014-10-27 14:36:07 +0200849
850 /* If there wasn't any free chunk, bail out */
851 if (found == kfd->gtt_sa_num_of_chunks)
852 goto kfd_gtt_no_free_chunk;
853
854 /* Update fields of mem_obj */
855 (*mem_obj)->range_start = found;
856 (*mem_obj)->range_end = found;
857 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
858 kfd->gtt_start_gpu_addr,
859 found,
860 kfd->gtt_sa_chunk_size);
861 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
862 kfd->gtt_start_cpu_ptr,
863 found,
864 kfd->gtt_sa_chunk_size);
865
Kent Russell79775b62017-08-15 23:00:05 -0400866 pr_debug("gpu_addr = %p, cpu_addr = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200867 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
868
869 /* If we need only one chunk, mark it as allocated and get out */
870 if (size <= kfd->gtt_sa_chunk_size) {
Kent Russell79775b62017-08-15 23:00:05 -0400871 pr_debug("Single bit\n");
Oded Gabbay6e810902014-10-27 14:36:07 +0200872 set_bit(found, kfd->gtt_sa_bitmap);
873 goto kfd_gtt_out;
874 }
875
876 /* Otherwise, try to see if we have enough contiguous chunks */
877 cur_size = size - kfd->gtt_sa_chunk_size;
878 do {
879 (*mem_obj)->range_end =
880 find_next_zero_bit(kfd->gtt_sa_bitmap,
881 kfd->gtt_sa_num_of_chunks, ++found);
882 /*
883 * If next free chunk is not contiguous than we need to
884 * restart our search from the last free chunk we found (which
885 * wasn't contiguous to the previous ones
886 */
887 if ((*mem_obj)->range_end != found) {
888 start_search = found;
889 goto kfd_gtt_restart_search;
890 }
891
892 /*
893 * If we reached end of buffer, bail out with error
894 */
895 if (found == kfd->gtt_sa_num_of_chunks)
896 goto kfd_gtt_no_free_chunk;
897
898 /* Check if we don't need another chunk */
899 if (cur_size <= kfd->gtt_sa_chunk_size)
900 cur_size = 0;
901 else
902 cur_size -= kfd->gtt_sa_chunk_size;
903
904 } while (cur_size > 0);
905
Kent Russell79775b62017-08-15 23:00:05 -0400906 pr_debug("range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200907 (*mem_obj)->range_start, (*mem_obj)->range_end);
908
909 /* Mark the chunks as allocated */
910 for (found = (*mem_obj)->range_start;
911 found <= (*mem_obj)->range_end;
912 found++)
913 set_bit(found, kfd->gtt_sa_bitmap);
914
915kfd_gtt_out:
916 mutex_unlock(&kfd->gtt_sa_lock);
917 return 0;
918
919kfd_gtt_no_free_chunk:
Kent Russell79775b62017-08-15 23:00:05 -0400920 pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
Oded Gabbay6e810902014-10-27 14:36:07 +0200921 mutex_unlock(&kfd->gtt_sa_lock);
922 kfree(mem_obj);
923 return -ENOMEM;
924}
925
926int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
927{
928 unsigned int bit;
929
Oded Gabbay9216ed22015-01-12 22:34:21 +0200930 /* Act like kfree when trying to free a NULL object */
931 if (!mem_obj)
932 return 0;
Oded Gabbay6e810902014-10-27 14:36:07 +0200933
Kent Russell79775b62017-08-15 23:00:05 -0400934 pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200935 mem_obj, mem_obj->range_start, mem_obj->range_end);
936
937 mutex_lock(&kfd->gtt_sa_lock);
938
939 /* Mark the chunks as free */
940 for (bit = mem_obj->range_start;
941 bit <= mem_obj->range_end;
942 bit++)
943 clear_bit(bit, kfd->gtt_sa_bitmap);
944
945 mutex_unlock(&kfd->gtt_sa_lock);
946
947 kfree(mem_obj);
948 return 0;
949}
Shaoyun Liua29ec472018-07-11 22:33:04 -0400950
951#if defined(CONFIG_DEBUG_FS)
952
953/* This function will send a package to HIQ to hang the HWS
954 * which will trigger a GPU reset and bring the HWS back to normal state
955 */
956int kfd_debugfs_hang_hws(struct kfd_dev *dev)
957{
958 int r = 0;
959
960 if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
961 pr_err("HWS is not enabled");
962 return -EINVAL;
963 }
964
965 r = pm_debugfs_hang_hws(&dev->dqm->packets);
966 if (!r)
967 r = dqm_debugfs_execute_queues(dev->dqm);
968
969 return r;
970}
971
972#endif