• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2014-2022 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <linux/bsearch.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include "kfd_priv.h"
28 #include "kfd_device_queue_manager.h"
29 #include "kfd_pm4_headers_vi.h"
30 #include "kfd_pm4_headers_aldebaran.h"
31 #include "cwsr_trap_handler.h"
32 #include "kfd_iommu.h"
33 #include "amdgpu_amdkfd.h"
34 #include "kfd_smi_events.h"
35 #include "kfd_migrate.h"
36 #include "amdgpu.h"
37 
38 #define MQD_SIZE_ALIGNED 768
39 
40 /*
41  * kfd_locked is used to lock the kfd driver during suspend or reset
42  * once locked, kfd driver will stop any further GPU execution.
43  * create process (open) will return -EAGAIN.
44  */
45 static atomic_t kfd_locked = ATOMIC_INIT(0);
46 
47 #ifdef CONFIG_DRM_AMDGPU_CIK
48 extern const struct kfd2kgd_calls gfx_v7_kfd2kgd;
49 #endif
50 extern const struct kfd2kgd_calls gfx_v8_kfd2kgd;
51 extern const struct kfd2kgd_calls gfx_v9_kfd2kgd;
52 extern const struct kfd2kgd_calls arcturus_kfd2kgd;
53 extern const struct kfd2kgd_calls aldebaran_kfd2kgd;
54 extern const struct kfd2kgd_calls gfx_v10_kfd2kgd;
55 extern const struct kfd2kgd_calls gfx_v10_3_kfd2kgd;
56 extern const struct kfd2kgd_calls gfx_v11_kfd2kgd;
57 
58 static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
59 				unsigned int chunk_size);
60 static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
61 
62 static int kfd_resume_iommu(struct kfd_dev *kfd);
63 static int kfd_resume(struct kfd_dev *kfd);
64 
kfd_device_info_set_sdma_info(struct kfd_dev * kfd)65 static void kfd_device_info_set_sdma_info(struct kfd_dev *kfd)
66 {
67 	uint32_t sdma_version = kfd->adev->ip_versions[SDMA0_HWIP][0];
68 
69 	switch (sdma_version) {
70 	case IP_VERSION(4, 0, 0):/* VEGA10 */
71 	case IP_VERSION(4, 0, 1):/* VEGA12 */
72 	case IP_VERSION(4, 1, 0):/* RAVEN */
73 	case IP_VERSION(4, 1, 1):/* RAVEN */
74 	case IP_VERSION(4, 1, 2):/* RENOIR */
75 	case IP_VERSION(5, 2, 1):/* VANGOGH */
76 	case IP_VERSION(5, 2, 3):/* YELLOW_CARP */
77 	case IP_VERSION(5, 2, 6):/* GC 10.3.6 */
78 	case IP_VERSION(5, 2, 7):/* GC 10.3.7 */
79 		kfd->device_info.num_sdma_queues_per_engine = 2;
80 		break;
81 	case IP_VERSION(4, 2, 0):/* VEGA20 */
82 	case IP_VERSION(4, 2, 2):/* ARCTURUS */
83 	case IP_VERSION(4, 4, 0):/* ALDEBARAN */
84 	case IP_VERSION(5, 0, 0):/* NAVI10 */
85 	case IP_VERSION(5, 0, 1):/* CYAN_SKILLFISH */
86 	case IP_VERSION(5, 0, 2):/* NAVI14 */
87 	case IP_VERSION(5, 0, 5):/* NAVI12 */
88 	case IP_VERSION(5, 2, 0):/* SIENNA_CICHLID */
89 	case IP_VERSION(5, 2, 2):/* NAVY_FLOUNDER */
90 	case IP_VERSION(5, 2, 4):/* DIMGREY_CAVEFISH */
91 	case IP_VERSION(5, 2, 5):/* BEIGE_GOBY */
92 	case IP_VERSION(6, 0, 0):
93 	case IP_VERSION(6, 0, 1):
94 	case IP_VERSION(6, 0, 2):
95 	case IP_VERSION(6, 0, 3):
96 		kfd->device_info.num_sdma_queues_per_engine = 8;
97 		break;
98 	default:
99 		dev_warn(kfd_device,
100 			"Default sdma queue per engine(8) is set due to mismatch of sdma ip block(SDMA_HWIP:0x%x).\n",
101 			sdma_version);
102 		kfd->device_info.num_sdma_queues_per_engine = 8;
103 	}
104 
105 	switch (sdma_version) {
106 	case IP_VERSION(6, 0, 0):
107 	case IP_VERSION(6, 0, 2):
108 	case IP_VERSION(6, 0, 3):
109 		/* Reserve 1 for paging and 1 for gfx */
110 		kfd->device_info.num_reserved_sdma_queues_per_engine = 2;
111 		/* BIT(0)=engine-0 queue-0; BIT(1)=engine-1 queue-0; BIT(2)=engine-0 queue-1; ... */
112 		kfd->device_info.reserved_sdma_queues_bitmap = 0xFULL;
113 		break;
114 	case IP_VERSION(6, 0, 1):
115 		/* Reserve 1 for paging and 1 for gfx */
116 		kfd->device_info.num_reserved_sdma_queues_per_engine = 2;
117 		/* BIT(0)=engine-0 queue-0; BIT(1)=engine-0 queue-1; ... */
118 		kfd->device_info.reserved_sdma_queues_bitmap = 0x3ULL;
119 		break;
120 	default:
121 		break;
122 	}
123 }
124 
kfd_device_info_set_event_interrupt_class(struct kfd_dev * kfd)125 static void kfd_device_info_set_event_interrupt_class(struct kfd_dev *kfd)
126 {
127 	uint32_t gc_version = KFD_GC_VERSION(kfd);
128 
129 	switch (gc_version) {
130 	case IP_VERSION(9, 0, 1): /* VEGA10 */
131 	case IP_VERSION(9, 1, 0): /* RAVEN */
132 	case IP_VERSION(9, 2, 1): /* VEGA12 */
133 	case IP_VERSION(9, 2, 2): /* RAVEN */
134 	case IP_VERSION(9, 3, 0): /* RENOIR */
135 	case IP_VERSION(9, 4, 0): /* VEGA20 */
136 	case IP_VERSION(9, 4, 1): /* ARCTURUS */
137 	case IP_VERSION(9, 4, 2): /* ALDEBARAN */
138 	case IP_VERSION(10, 3, 1): /* VANGOGH */
139 	case IP_VERSION(10, 3, 3): /* YELLOW_CARP */
140 	case IP_VERSION(10, 3, 6): /* GC 10.3.6 */
141 	case IP_VERSION(10, 3, 7): /* GC 10.3.7 */
142 	case IP_VERSION(10, 1, 3): /* CYAN_SKILLFISH */
143 	case IP_VERSION(10, 1, 4):
144 	case IP_VERSION(10, 1, 10): /* NAVI10 */
145 	case IP_VERSION(10, 1, 2): /* NAVI12 */
146 	case IP_VERSION(10, 1, 1): /* NAVI14 */
147 	case IP_VERSION(10, 3, 0): /* SIENNA_CICHLID */
148 	case IP_VERSION(10, 3, 2): /* NAVY_FLOUNDER */
149 	case IP_VERSION(10, 3, 4): /* DIMGREY_CAVEFISH */
150 	case IP_VERSION(10, 3, 5): /* BEIGE_GOBY */
151 		kfd->device_info.event_interrupt_class = &event_interrupt_class_v9;
152 		break;
153 	case IP_VERSION(11, 0, 0):
154 	case IP_VERSION(11, 0, 1):
155 	case IP_VERSION(11, 0, 2):
156 	case IP_VERSION(11, 0, 3):
157 	case IP_VERSION(11, 0, 4):
158 		kfd->device_info.event_interrupt_class = &event_interrupt_class_v11;
159 		break;
160 	default:
161 		dev_warn(kfd_device, "v9 event interrupt handler is set due to "
162 			"mismatch of gc ip block(GC_HWIP:0x%x).\n", gc_version);
163 		kfd->device_info.event_interrupt_class = &event_interrupt_class_v9;
164 	}
165 }
166 
kfd_device_info_init(struct kfd_dev * kfd,bool vf,uint32_t gfx_target_version)167 static void kfd_device_info_init(struct kfd_dev *kfd,
168 				 bool vf, uint32_t gfx_target_version)
169 {
170 	uint32_t gc_version = KFD_GC_VERSION(kfd);
171 	uint32_t asic_type = kfd->adev->asic_type;
172 
173 	kfd->device_info.max_pasid_bits = 16;
174 	kfd->device_info.max_no_of_hqd = 24;
175 	kfd->device_info.num_of_watch_points = 4;
176 	kfd->device_info.mqd_size_aligned = MQD_SIZE_ALIGNED;
177 	kfd->device_info.gfx_target_version = gfx_target_version;
178 
179 	if (KFD_IS_SOC15(kfd)) {
180 		kfd->device_info.doorbell_size = 8;
181 		kfd->device_info.ih_ring_entry_size = 8 * sizeof(uint32_t);
182 		kfd->device_info.supports_cwsr = true;
183 
184 		kfd_device_info_set_sdma_info(kfd);
185 
186 		kfd_device_info_set_event_interrupt_class(kfd);
187 
188 		/* Raven */
189 		if (gc_version == IP_VERSION(9, 1, 0) ||
190 		    gc_version == IP_VERSION(9, 2, 2))
191 			kfd->device_info.needs_iommu_device = true;
192 
193 		if (gc_version < IP_VERSION(11, 0, 0)) {
194 			/* Navi2x+, Navi1x+ */
195 			if (gc_version == IP_VERSION(10, 3, 6))
196 				kfd->device_info.no_atomic_fw_version = 14;
197 			else if (gc_version == IP_VERSION(10, 3, 7))
198 				kfd->device_info.no_atomic_fw_version = 3;
199 			else if (gc_version >= IP_VERSION(10, 3, 0))
200 				kfd->device_info.no_atomic_fw_version = 92;
201 			else if (gc_version >= IP_VERSION(10, 1, 1))
202 				kfd->device_info.no_atomic_fw_version = 145;
203 
204 			/* Navi1x+ */
205 			if (gc_version >= IP_VERSION(10, 1, 1))
206 				kfd->device_info.needs_pci_atomics = true;
207 		}
208 	} else {
209 		kfd->device_info.doorbell_size = 4;
210 		kfd->device_info.ih_ring_entry_size = 4 * sizeof(uint32_t);
211 		kfd->device_info.event_interrupt_class = &event_interrupt_class_cik;
212 		kfd->device_info.num_sdma_queues_per_engine = 2;
213 
214 		if (asic_type != CHIP_KAVERI &&
215 		    asic_type != CHIP_HAWAII &&
216 		    asic_type != CHIP_TONGA)
217 			kfd->device_info.supports_cwsr = true;
218 
219 		if (asic_type == CHIP_KAVERI ||
220 		    asic_type == CHIP_CARRIZO)
221 			kfd->device_info.needs_iommu_device = true;
222 
223 		if (asic_type != CHIP_HAWAII && !vf)
224 			kfd->device_info.needs_pci_atomics = true;
225 	}
226 }
227 
kgd2kfd_probe(struct amdgpu_device * adev,bool vf)228 struct kfd_dev *kgd2kfd_probe(struct amdgpu_device *adev, bool vf)
229 {
230 	struct kfd_dev *kfd = NULL;
231 	const struct kfd2kgd_calls *f2g = NULL;
232 	struct pci_dev *pdev = adev->pdev;
233 	uint32_t gfx_target_version = 0;
234 
235 	switch (adev->asic_type) {
236 #ifdef KFD_SUPPORT_IOMMU_V2
237 #ifdef CONFIG_DRM_AMDGPU_CIK
238 	case CHIP_KAVERI:
239 		gfx_target_version = 70000;
240 		if (!vf)
241 			f2g = &gfx_v7_kfd2kgd;
242 		break;
243 #endif
244 	case CHIP_CARRIZO:
245 		gfx_target_version = 80001;
246 		if (!vf)
247 			f2g = &gfx_v8_kfd2kgd;
248 		break;
249 #endif
250 #ifdef CONFIG_DRM_AMDGPU_CIK
251 	case CHIP_HAWAII:
252 		gfx_target_version = 70001;
253 		if (!amdgpu_exp_hw_support)
254 			pr_info(
255 	"KFD support on Hawaii is experimental. See modparam exp_hw_support\n"
256 				);
257 		else if (!vf)
258 			f2g = &gfx_v7_kfd2kgd;
259 		break;
260 #endif
261 	case CHIP_TONGA:
262 		gfx_target_version = 80002;
263 		if (!vf)
264 			f2g = &gfx_v8_kfd2kgd;
265 		break;
266 	case CHIP_FIJI:
267 		gfx_target_version = 80003;
268 		f2g = &gfx_v8_kfd2kgd;
269 		break;
270 	case CHIP_POLARIS10:
271 		gfx_target_version = 80003;
272 		f2g = &gfx_v8_kfd2kgd;
273 		break;
274 	case CHIP_POLARIS11:
275 		gfx_target_version = 80003;
276 		if (!vf)
277 			f2g = &gfx_v8_kfd2kgd;
278 		break;
279 	case CHIP_POLARIS12:
280 		gfx_target_version = 80003;
281 		if (!vf)
282 			f2g = &gfx_v8_kfd2kgd;
283 		break;
284 	case CHIP_VEGAM:
285 		gfx_target_version = 80003;
286 		if (!vf)
287 			f2g = &gfx_v8_kfd2kgd;
288 		break;
289 	default:
290 		switch (adev->ip_versions[GC_HWIP][0]) {
291 		/* Vega 10 */
292 		case IP_VERSION(9, 0, 1):
293 			gfx_target_version = 90000;
294 			f2g = &gfx_v9_kfd2kgd;
295 			break;
296 #ifdef KFD_SUPPORT_IOMMU_V2
297 		/* Raven */
298 		case IP_VERSION(9, 1, 0):
299 		case IP_VERSION(9, 2, 2):
300 			gfx_target_version = 90002;
301 			if (!vf)
302 				f2g = &gfx_v9_kfd2kgd;
303 			break;
304 #endif
305 		/* Vega12 */
306 		case IP_VERSION(9, 2, 1):
307 			gfx_target_version = 90004;
308 			if (!vf)
309 				f2g = &gfx_v9_kfd2kgd;
310 			break;
311 		/* Renoir */
312 		case IP_VERSION(9, 3, 0):
313 			gfx_target_version = 90012;
314 			if (!vf)
315 				f2g = &gfx_v9_kfd2kgd;
316 			break;
317 		/* Vega20 */
318 		case IP_VERSION(9, 4, 0):
319 			gfx_target_version = 90006;
320 			if (!vf)
321 				f2g = &gfx_v9_kfd2kgd;
322 			break;
323 		/* Arcturus */
324 		case IP_VERSION(9, 4, 1):
325 			gfx_target_version = 90008;
326 			f2g = &arcturus_kfd2kgd;
327 			break;
328 		/* Aldebaran */
329 		case IP_VERSION(9, 4, 2):
330 			gfx_target_version = 90010;
331 			f2g = &aldebaran_kfd2kgd;
332 			break;
333 		/* Navi10 */
334 		case IP_VERSION(10, 1, 10):
335 			gfx_target_version = 100100;
336 			if (!vf)
337 				f2g = &gfx_v10_kfd2kgd;
338 			break;
339 		/* Navi12 */
340 		case IP_VERSION(10, 1, 2):
341 			gfx_target_version = 100101;
342 			f2g = &gfx_v10_kfd2kgd;
343 			break;
344 		/* Navi14 */
345 		case IP_VERSION(10, 1, 1):
346 			gfx_target_version = 100102;
347 			if (!vf)
348 				f2g = &gfx_v10_kfd2kgd;
349 			break;
350 		/* Cyan Skillfish */
351 		case IP_VERSION(10, 1, 3):
352 		case IP_VERSION(10, 1, 4):
353 			gfx_target_version = 100103;
354 			if (!vf)
355 				f2g = &gfx_v10_kfd2kgd;
356 			break;
357 		/* Sienna Cichlid */
358 		case IP_VERSION(10, 3, 0):
359 			gfx_target_version = 100300;
360 			f2g = &gfx_v10_3_kfd2kgd;
361 			break;
362 		/* Navy Flounder */
363 		case IP_VERSION(10, 3, 2):
364 			gfx_target_version = 100301;
365 			f2g = &gfx_v10_3_kfd2kgd;
366 			break;
367 		/* Van Gogh */
368 		case IP_VERSION(10, 3, 1):
369 			gfx_target_version = 100303;
370 			if (!vf)
371 				f2g = &gfx_v10_3_kfd2kgd;
372 			break;
373 		/* Dimgrey Cavefish */
374 		case IP_VERSION(10, 3, 4):
375 			gfx_target_version = 100302;
376 			f2g = &gfx_v10_3_kfd2kgd;
377 			break;
378 		/* Beige Goby */
379 		case IP_VERSION(10, 3, 5):
380 			gfx_target_version = 100304;
381 			f2g = &gfx_v10_3_kfd2kgd;
382 			break;
383 		/* Yellow Carp */
384 		case IP_VERSION(10, 3, 3):
385 			gfx_target_version = 100305;
386 			if (!vf)
387 				f2g = &gfx_v10_3_kfd2kgd;
388 			break;
389 		case IP_VERSION(10, 3, 6):
390 		case IP_VERSION(10, 3, 7):
391 			gfx_target_version = 100306;
392 			if (!vf)
393 				f2g = &gfx_v10_3_kfd2kgd;
394 			break;
395 		case IP_VERSION(11, 0, 0):
396 			gfx_target_version = 110000;
397 			f2g = &gfx_v11_kfd2kgd;
398 			break;
399 		case IP_VERSION(11, 0, 1):
400 		case IP_VERSION(11, 0, 4):
401 			gfx_target_version = 110003;
402 			f2g = &gfx_v11_kfd2kgd;
403 			break;
404 		case IP_VERSION(11, 0, 2):
405 			gfx_target_version = 110002;
406 			f2g = &gfx_v11_kfd2kgd;
407 			break;
408 		case IP_VERSION(11, 0, 3):
409 			/* Note: Compiler version is 11.0.1 while HW version is 11.0.3 */
410 			gfx_target_version = 110001;
411 			f2g = &gfx_v11_kfd2kgd;
412 			break;
413 		default:
414 			break;
415 		}
416 		break;
417 	}
418 
419 	if (!f2g) {
420 		if (adev->ip_versions[GC_HWIP][0])
421 			dev_err(kfd_device, "GC IP %06x %s not supported in kfd\n",
422 				adev->ip_versions[GC_HWIP][0], vf ? "VF" : "");
423 		else
424 			dev_err(kfd_device, "%s %s not supported in kfd\n",
425 				amdgpu_asic_name[adev->asic_type], vf ? "VF" : "");
426 		return NULL;
427 	}
428 
429 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
430 	if (!kfd)
431 		return NULL;
432 
433 	kfd->adev = adev;
434 	kfd_device_info_init(kfd, vf, gfx_target_version);
435 	kfd->pdev = pdev;
436 	kfd->init_complete = false;
437 	kfd->kfd2kgd = f2g;
438 	atomic_set(&kfd->compute_profile, 0);
439 
440 	mutex_init(&kfd->doorbell_mutex);
441 	memset(&kfd->doorbell_available_index, 0,
442 		sizeof(kfd->doorbell_available_index));
443 
444 	atomic_set(&kfd->sram_ecc_flag, 0);
445 
446 	ida_init(&kfd->doorbell_ida);
447 
448 	return kfd;
449 }
450 
kfd_cwsr_init(struct kfd_dev * kfd)451 static void kfd_cwsr_init(struct kfd_dev *kfd)
452 {
453 	if (cwsr_enable && kfd->device_info.supports_cwsr) {
454 		if (KFD_GC_VERSION(kfd) < IP_VERSION(9, 0, 1)) {
455 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
456 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
457 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
458 		} else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)) {
459 			BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
460 			kfd->cwsr_isa = cwsr_trap_arcturus_hex;
461 			kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
462 		} else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)) {
463 			BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex) > PAGE_SIZE);
464 			kfd->cwsr_isa = cwsr_trap_aldebaran_hex;
465 			kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex);
466 		} else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 1, 1)) {
467 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
468 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
469 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
470 		} else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 3, 0)) {
471 			BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE);
472 			kfd->cwsr_isa = cwsr_trap_nv1x_hex;
473 			kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);
474 		} else if (KFD_GC_VERSION(kfd) < IP_VERSION(11, 0, 0)) {
475 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
476 			kfd->cwsr_isa = cwsr_trap_gfx10_hex;
477 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
478 		} else {
479 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx11_hex) > PAGE_SIZE);
480 			kfd->cwsr_isa = cwsr_trap_gfx11_hex;
481 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx11_hex);
482 		}
483 
484 		kfd->cwsr_enabled = true;
485 	}
486 }
487 
kfd_gws_init(struct kfd_dev * kfd)488 static int kfd_gws_init(struct kfd_dev *kfd)
489 {
490 	int ret = 0;
491 
492 	if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
493 		return 0;
494 
495 	if (hws_gws_support || (KFD_IS_SOC15(kfd) &&
496 		((KFD_GC_VERSION(kfd) == IP_VERSION(9, 0, 1)
497 			&& kfd->mec2_fw_version >= 0x81b3) ||
498 		(KFD_GC_VERSION(kfd) <= IP_VERSION(9, 4, 0)
499 			&& kfd->mec2_fw_version >= 0x1b3)  ||
500 		(KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)
501 			&& kfd->mec2_fw_version >= 0x30)   ||
502 		(KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)
503 			&& kfd->mec2_fw_version >= 0x28))))
504 		ret = amdgpu_amdkfd_alloc_gws(kfd->adev,
505 				kfd->adev->gds.gws_size, &kfd->gws);
506 
507 	return ret;
508 }
509 
kfd_smi_init(struct kfd_dev * dev)510 static void kfd_smi_init(struct kfd_dev *dev)
511 {
512 	INIT_LIST_HEAD(&dev->smi_clients);
513 	spin_lock_init(&dev->smi_lock);
514 }
515 
kgd2kfd_device_init(struct kfd_dev * kfd,struct drm_device * ddev,const struct kgd2kfd_shared_resources * gpu_resources)516 bool kgd2kfd_device_init(struct kfd_dev *kfd,
517 			 struct drm_device *ddev,
518 			 const struct kgd2kfd_shared_resources *gpu_resources)
519 {
520 	unsigned int size, map_process_packet_size;
521 
522 	kfd->ddev = ddev;
523 	kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
524 			KGD_ENGINE_MEC1);
525 	kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
526 			KGD_ENGINE_MEC2);
527 	kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
528 			KGD_ENGINE_SDMA1);
529 	kfd->shared_resources = *gpu_resources;
530 
531 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
532 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
533 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
534 			- kfd->vm_info.first_vmid_kfd + 1;
535 
536 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
537 	 * 32 and 64-bit requests are possible and must be
538 	 * supported.
539 	 */
540 	kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kfd->adev);
541 	if (!kfd->pci_atomic_requested &&
542 	    kfd->device_info.needs_pci_atomics &&
543 	    (!kfd->device_info.no_atomic_fw_version ||
544 	     kfd->mec_fw_version < kfd->device_info.no_atomic_fw_version)) {
545 		dev_info(kfd_device,
546 			 "skipped device %x:%x, PCI rejects atomics %d<%d\n",
547 			 kfd->pdev->vendor, kfd->pdev->device,
548 			 kfd->mec_fw_version,
549 			 kfd->device_info.no_atomic_fw_version);
550 		return false;
551 	}
552 
553 	/* Verify module parameters regarding mapped process number*/
554 	if (hws_max_conc_proc >= 0)
555 		kfd->max_proc_per_quantum = min((u32)hws_max_conc_proc, kfd->vm_info.vmid_num_kfd);
556 	else
557 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
558 
559 	/* calculate max size of mqds needed for queues */
560 	size = max_num_of_queues_per_device *
561 			kfd->device_info.mqd_size_aligned;
562 
563 	/*
564 	 * calculate max size of runlist packet.
565 	 * There can be only 2 packets at once
566 	 */
567 	map_process_packet_size = KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2) ?
568 				sizeof(struct pm4_mes_map_process_aldebaran) :
569 				sizeof(struct pm4_mes_map_process);
570 	size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size +
571 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
572 		+ sizeof(struct pm4_mes_runlist)) * 2;
573 
574 	/* Add size of HIQ & DIQ */
575 	size += KFD_KERNEL_QUEUE_SIZE * 2;
576 
577 	/* add another 512KB for all other allocations on gart (HPD, fences) */
578 	size += 512 * 1024;
579 
580 	if (amdgpu_amdkfd_alloc_gtt_mem(
581 			kfd->adev, size, &kfd->gtt_mem,
582 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
583 			false)) {
584 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
585 		goto alloc_gtt_mem_failure;
586 	}
587 
588 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
589 
590 	/* Initialize GTT sa with 512 byte chunk size */
591 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
592 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
593 		goto kfd_gtt_sa_init_error;
594 	}
595 
596 	if (kfd_doorbell_init(kfd)) {
597 		dev_err(kfd_device,
598 			"Error initializing doorbell aperture\n");
599 		goto kfd_doorbell_error;
600 	}
601 
602 	if (amdgpu_use_xgmi_p2p)
603 		kfd->hive_id = kfd->adev->gmc.xgmi.hive_id;
604 
605 	kfd->noretry = kfd->adev->gmc.noretry;
606 
607 	if (kfd_interrupt_init(kfd)) {
608 		dev_err(kfd_device, "Error initializing interrupts\n");
609 		goto kfd_interrupt_error;
610 	}
611 
612 	kfd->dqm = device_queue_manager_init(kfd);
613 	if (!kfd->dqm) {
614 		dev_err(kfd_device, "Error initializing queue manager\n");
615 		goto device_queue_manager_error;
616 	}
617 
618 	/* If supported on this device, allocate global GWS that is shared
619 	 * by all KFD processes
620 	 */
621 	if (kfd_gws_init(kfd)) {
622 		dev_err(kfd_device, "Could not allocate %d gws\n",
623 			kfd->adev->gds.gws_size);
624 		goto gws_error;
625 	}
626 
627 	/* If CRAT is broken, won't set iommu enabled */
628 	kfd_double_confirm_iommu_support(kfd);
629 
630 	if (kfd_iommu_device_init(kfd)) {
631 		kfd->use_iommu_v2 = false;
632 		dev_err(kfd_device, "Error initializing iommuv2\n");
633 		goto device_iommu_error;
634 	}
635 
636 	kfd_cwsr_init(kfd);
637 
638 	svm_migrate_init(kfd->adev);
639 
640 	if (kfd_resume_iommu(kfd))
641 		goto device_iommu_error;
642 
643 	if (kfd_resume(kfd))
644 		goto kfd_resume_error;
645 
646 	amdgpu_amdkfd_get_local_mem_info(kfd->adev, &kfd->local_mem_info);
647 
648 	if (kfd_topology_add_device(kfd)) {
649 		dev_err(kfd_device, "Error adding device to topology\n");
650 		goto kfd_topology_add_device_error;
651 	}
652 
653 	kfd_smi_init(kfd);
654 
655 	kfd->init_complete = true;
656 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
657 		 kfd->pdev->device);
658 
659 	pr_debug("Starting kfd with the following scheduling policy %d\n",
660 		kfd->dqm->sched_policy);
661 
662 	goto out;
663 
664 kfd_topology_add_device_error:
665 kfd_resume_error:
666 device_iommu_error:
667 gws_error:
668 	device_queue_manager_uninit(kfd->dqm);
669 device_queue_manager_error:
670 	kfd_interrupt_exit(kfd);
671 kfd_interrupt_error:
672 	kfd_doorbell_fini(kfd);
673 kfd_doorbell_error:
674 	kfd_gtt_sa_fini(kfd);
675 kfd_gtt_sa_init_error:
676 	amdgpu_amdkfd_free_gtt_mem(kfd->adev, kfd->gtt_mem);
677 alloc_gtt_mem_failure:
678 	if (kfd->gws)
679 		amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws);
680 	dev_err(kfd_device,
681 		"device %x:%x NOT added due to errors\n",
682 		kfd->pdev->vendor, kfd->pdev->device);
683 out:
684 	return kfd->init_complete;
685 }
686 
kgd2kfd_device_exit(struct kfd_dev * kfd)687 void kgd2kfd_device_exit(struct kfd_dev *kfd)
688 {
689 	if (kfd->init_complete) {
690 		device_queue_manager_uninit(kfd->dqm);
691 		kfd_interrupt_exit(kfd);
692 		kfd_topology_remove_device(kfd);
693 		kfd_doorbell_fini(kfd);
694 		ida_destroy(&kfd->doorbell_ida);
695 		kfd_gtt_sa_fini(kfd);
696 		amdgpu_amdkfd_free_gtt_mem(kfd->adev, kfd->gtt_mem);
697 		if (kfd->gws)
698 			amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws);
699 	}
700 
701 	kfree(kfd);
702 }
703 
kgd2kfd_pre_reset(struct kfd_dev * kfd)704 int kgd2kfd_pre_reset(struct kfd_dev *kfd)
705 {
706 	if (!kfd->init_complete)
707 		return 0;
708 
709 	kfd_smi_event_update_gpu_reset(kfd, false);
710 
711 	kfd->dqm->ops.pre_reset(kfd->dqm);
712 
713 	kgd2kfd_suspend(kfd, false);
714 
715 	kfd_signal_reset_event(kfd);
716 	return 0;
717 }
718 
719 /*
720  * Fix me. KFD won't be able to resume existing process for now.
721  * We will keep all existing process in a evicted state and
722  * wait the process to be terminated.
723  */
724 
kgd2kfd_post_reset(struct kfd_dev * kfd)725 int kgd2kfd_post_reset(struct kfd_dev *kfd)
726 {
727 	int ret;
728 
729 	if (!kfd->init_complete)
730 		return 0;
731 
732 	ret = kfd_resume(kfd);
733 	if (ret)
734 		return ret;
735 	atomic_dec(&kfd_locked);
736 
737 	atomic_set(&kfd->sram_ecc_flag, 0);
738 
739 	kfd_smi_event_update_gpu_reset(kfd, true);
740 
741 	return 0;
742 }
743 
kfd_is_locked(void)744 bool kfd_is_locked(void)
745 {
746 	return  (atomic_read(&kfd_locked) > 0);
747 }
748 
kgd2kfd_suspend(struct kfd_dev * kfd,bool run_pm)749 void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm)
750 {
751 	if (!kfd->init_complete)
752 		return;
753 
754 	/* for runtime suspend, skip locking kfd */
755 	if (!run_pm) {
756 		/* For first KFD device suspend all the KFD processes */
757 		if (atomic_inc_return(&kfd_locked) == 1)
758 			kfd_suspend_all_processes();
759 	}
760 
761 	kfd->dqm->ops.stop(kfd->dqm);
762 	kfd_iommu_suspend(kfd);
763 }
764 
kgd2kfd_resume(struct kfd_dev * kfd,bool run_pm)765 int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm)
766 {
767 	int ret, count;
768 
769 	if (!kfd->init_complete)
770 		return 0;
771 
772 	ret = kfd_resume(kfd);
773 	if (ret)
774 		return ret;
775 
776 	/* for runtime resume, skip unlocking kfd */
777 	if (!run_pm) {
778 		count = atomic_dec_return(&kfd_locked);
779 		WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
780 		if (count == 0)
781 			ret = kfd_resume_all_processes();
782 	}
783 
784 	return ret;
785 }
786 
kgd2kfd_resume_iommu(struct kfd_dev * kfd)787 int kgd2kfd_resume_iommu(struct kfd_dev *kfd)
788 {
789 	if (!kfd->init_complete)
790 		return 0;
791 
792 	return kfd_resume_iommu(kfd);
793 }
794 
kfd_resume_iommu(struct kfd_dev * kfd)795 static int kfd_resume_iommu(struct kfd_dev *kfd)
796 {
797 	int err = 0;
798 
799 	err = kfd_iommu_resume(kfd);
800 	if (err)
801 		dev_err(kfd_device,
802 			"Failed to resume IOMMU for device %x:%x\n",
803 			kfd->pdev->vendor, kfd->pdev->device);
804 	return err;
805 }
806 
kfd_resume(struct kfd_dev * kfd)807 static int kfd_resume(struct kfd_dev *kfd)
808 {
809 	int err = 0;
810 
811 	err = kfd->dqm->ops.start(kfd->dqm);
812 	if (err)
813 		dev_err(kfd_device,
814 			"Error starting queue manager for device %x:%x\n",
815 			kfd->pdev->vendor, kfd->pdev->device);
816 
817 	return err;
818 }
819 
kfd_queue_work(struct workqueue_struct * wq,struct work_struct * work)820 static inline void kfd_queue_work(struct workqueue_struct *wq,
821 				  struct work_struct *work)
822 {
823 	int cpu, new_cpu;
824 
825 	cpu = new_cpu = smp_processor_id();
826 	do {
827 		new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
828 		if (cpu_to_node(new_cpu) == numa_node_id())
829 			break;
830 	} while (cpu != new_cpu);
831 
832 	queue_work_on(new_cpu, wq, work);
833 }
834 
835 /* This is called directly from KGD at ISR. */
kgd2kfd_interrupt(struct kfd_dev * kfd,const void * ih_ring_entry)836 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
837 {
838 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
839 	bool is_patched = false;
840 	unsigned long flags;
841 
842 	if (!kfd->init_complete)
843 		return;
844 
845 	if (kfd->device_info.ih_ring_entry_size > sizeof(patched_ihre)) {
846 		dev_err_once(kfd_device, "Ring entry too small\n");
847 		return;
848 	}
849 
850 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
851 
852 	if (kfd->interrupts_active
853 	    && interrupt_is_wanted(kfd, ih_ring_entry,
854 				   patched_ihre, &is_patched)
855 	    && enqueue_ih_ring_entry(kfd,
856 				     is_patched ? patched_ihre : ih_ring_entry))
857 		kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work);
858 
859 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
860 }
861 
kgd2kfd_quiesce_mm(struct mm_struct * mm,uint32_t trigger)862 int kgd2kfd_quiesce_mm(struct mm_struct *mm, uint32_t trigger)
863 {
864 	struct kfd_process *p;
865 	int r;
866 
867 	/* Because we are called from arbitrary context (workqueue) as opposed
868 	 * to process context, kfd_process could attempt to exit while we are
869 	 * running so the lookup function increments the process ref count.
870 	 */
871 	p = kfd_lookup_process_by_mm(mm);
872 	if (!p)
873 		return -ESRCH;
874 
875 	WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
876 	r = kfd_process_evict_queues(p, trigger);
877 
878 	kfd_unref_process(p);
879 	return r;
880 }
881 
kgd2kfd_resume_mm(struct mm_struct * mm)882 int kgd2kfd_resume_mm(struct mm_struct *mm)
883 {
884 	struct kfd_process *p;
885 	int r;
886 
887 	/* Because we are called from arbitrary context (workqueue) as opposed
888 	 * to process context, kfd_process could attempt to exit while we are
889 	 * running so the lookup function increments the process ref count.
890 	 */
891 	p = kfd_lookup_process_by_mm(mm);
892 	if (!p)
893 		return -ESRCH;
894 
895 	r = kfd_process_restore_queues(p);
896 
897 	kfd_unref_process(p);
898 	return r;
899 }
900 
901 /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
902  *   prepare for safe eviction of KFD BOs that belong to the specified
903  *   process.
904  *
905  * @mm: mm_struct that identifies the specified KFD process
906  * @fence: eviction fence attached to KFD process BOs
907  *
908  */
kgd2kfd_schedule_evict_and_restore_process(struct mm_struct * mm,struct dma_fence * fence)909 int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
910 					       struct dma_fence *fence)
911 {
912 	struct kfd_process *p;
913 	unsigned long active_time;
914 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
915 
916 	if (!fence)
917 		return -EINVAL;
918 
919 	if (dma_fence_is_signaled(fence))
920 		return 0;
921 
922 	p = kfd_lookup_process_by_mm(mm);
923 	if (!p)
924 		return -ENODEV;
925 
926 	if (fence->seqno == p->last_eviction_seqno)
927 		goto out;
928 
929 	p->last_eviction_seqno = fence->seqno;
930 
931 	/* Avoid KFD process starvation. Wait for at least
932 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
933 	 */
934 	active_time = get_jiffies_64() - p->last_restore_timestamp;
935 	if (delay_jiffies > active_time)
936 		delay_jiffies -= active_time;
937 	else
938 		delay_jiffies = 0;
939 
940 	/* During process initialization eviction_work.dwork is initialized
941 	 * to kfd_evict_bo_worker
942 	 */
943 	WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",
944 	     p->lead_thread->pid, delay_jiffies);
945 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
946 out:
947 	kfd_unref_process(p);
948 	return 0;
949 }
950 
kfd_gtt_sa_init(struct kfd_dev * kfd,unsigned int buf_size,unsigned int chunk_size)951 static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
952 				unsigned int chunk_size)
953 {
954 	if (WARN_ON(buf_size < chunk_size))
955 		return -EINVAL;
956 	if (WARN_ON(buf_size == 0))
957 		return -EINVAL;
958 	if (WARN_ON(chunk_size == 0))
959 		return -EINVAL;
960 
961 	kfd->gtt_sa_chunk_size = chunk_size;
962 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
963 
964 	kfd->gtt_sa_bitmap = bitmap_zalloc(kfd->gtt_sa_num_of_chunks,
965 					   GFP_KERNEL);
966 	if (!kfd->gtt_sa_bitmap)
967 		return -ENOMEM;
968 
969 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
970 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
971 
972 	mutex_init(&kfd->gtt_sa_lock);
973 
974 	return 0;
975 }
976 
kfd_gtt_sa_fini(struct kfd_dev * kfd)977 static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
978 {
979 	mutex_destroy(&kfd->gtt_sa_lock);
980 	bitmap_free(kfd->gtt_sa_bitmap);
981 }
982 
kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,unsigned int bit_num,unsigned int chunk_size)983 static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
984 						unsigned int bit_num,
985 						unsigned int chunk_size)
986 {
987 	return start_addr + bit_num * chunk_size;
988 }
989 
kfd_gtt_sa_calc_cpu_addr(void * start_addr,unsigned int bit_num,unsigned int chunk_size)990 static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
991 						unsigned int bit_num,
992 						unsigned int chunk_size)
993 {
994 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
995 }
996 
kfd_gtt_sa_allocate(struct kfd_dev * kfd,unsigned int size,struct kfd_mem_obj ** mem_obj)997 int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
998 			struct kfd_mem_obj **mem_obj)
999 {
1000 	unsigned int found, start_search, cur_size;
1001 
1002 	if (size == 0)
1003 		return -EINVAL;
1004 
1005 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
1006 		return -ENOMEM;
1007 
1008 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
1009 	if (!(*mem_obj))
1010 		return -ENOMEM;
1011 
1012 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
1013 
1014 	start_search = 0;
1015 
1016 	mutex_lock(&kfd->gtt_sa_lock);
1017 
1018 kfd_gtt_restart_search:
1019 	/* Find the first chunk that is free */
1020 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
1021 					kfd->gtt_sa_num_of_chunks,
1022 					start_search);
1023 
1024 	pr_debug("Found = %d\n", found);
1025 
1026 	/* If there wasn't any free chunk, bail out */
1027 	if (found == kfd->gtt_sa_num_of_chunks)
1028 		goto kfd_gtt_no_free_chunk;
1029 
1030 	/* Update fields of mem_obj */
1031 	(*mem_obj)->range_start = found;
1032 	(*mem_obj)->range_end = found;
1033 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
1034 					kfd->gtt_start_gpu_addr,
1035 					found,
1036 					kfd->gtt_sa_chunk_size);
1037 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
1038 					kfd->gtt_start_cpu_ptr,
1039 					found,
1040 					kfd->gtt_sa_chunk_size);
1041 
1042 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
1043 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
1044 
1045 	/* If we need only one chunk, mark it as allocated and get out */
1046 	if (size <= kfd->gtt_sa_chunk_size) {
1047 		pr_debug("Single bit\n");
1048 		__set_bit(found, kfd->gtt_sa_bitmap);
1049 		goto kfd_gtt_out;
1050 	}
1051 
1052 	/* Otherwise, try to see if we have enough contiguous chunks */
1053 	cur_size = size - kfd->gtt_sa_chunk_size;
1054 	do {
1055 		(*mem_obj)->range_end =
1056 			find_next_zero_bit(kfd->gtt_sa_bitmap,
1057 					kfd->gtt_sa_num_of_chunks, ++found);
1058 		/*
1059 		 * If next free chunk is not contiguous than we need to
1060 		 * restart our search from the last free chunk we found (which
1061 		 * wasn't contiguous to the previous ones
1062 		 */
1063 		if ((*mem_obj)->range_end != found) {
1064 			start_search = found;
1065 			goto kfd_gtt_restart_search;
1066 		}
1067 
1068 		/*
1069 		 * If we reached end of buffer, bail out with error
1070 		 */
1071 		if (found == kfd->gtt_sa_num_of_chunks)
1072 			goto kfd_gtt_no_free_chunk;
1073 
1074 		/* Check if we don't need another chunk */
1075 		if (cur_size <= kfd->gtt_sa_chunk_size)
1076 			cur_size = 0;
1077 		else
1078 			cur_size -= kfd->gtt_sa_chunk_size;
1079 
1080 	} while (cur_size > 0);
1081 
1082 	pr_debug("range_start = %d, range_end = %d\n",
1083 		(*mem_obj)->range_start, (*mem_obj)->range_end);
1084 
1085 	/* Mark the chunks as allocated */
1086 	bitmap_set(kfd->gtt_sa_bitmap, (*mem_obj)->range_start,
1087 		   (*mem_obj)->range_end - (*mem_obj)->range_start + 1);
1088 
1089 kfd_gtt_out:
1090 	mutex_unlock(&kfd->gtt_sa_lock);
1091 	return 0;
1092 
1093 kfd_gtt_no_free_chunk:
1094 	pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);
1095 	mutex_unlock(&kfd->gtt_sa_lock);
1096 	kfree(*mem_obj);
1097 	return -ENOMEM;
1098 }
1099 
kfd_gtt_sa_free(struct kfd_dev * kfd,struct kfd_mem_obj * mem_obj)1100 int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
1101 {
1102 	/* Act like kfree when trying to free a NULL object */
1103 	if (!mem_obj)
1104 		return 0;
1105 
1106 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
1107 			mem_obj, mem_obj->range_start, mem_obj->range_end);
1108 
1109 	mutex_lock(&kfd->gtt_sa_lock);
1110 
1111 	/* Mark the chunks as free */
1112 	bitmap_clear(kfd->gtt_sa_bitmap, mem_obj->range_start,
1113 		     mem_obj->range_end - mem_obj->range_start + 1);
1114 
1115 	mutex_unlock(&kfd->gtt_sa_lock);
1116 
1117 	kfree(mem_obj);
1118 	return 0;
1119 }
1120 
kgd2kfd_set_sram_ecc_flag(struct kfd_dev * kfd)1121 void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
1122 {
1123 	if (kfd)
1124 		atomic_inc(&kfd->sram_ecc_flag);
1125 }
1126 
kfd_inc_compute_active(struct kfd_dev * kfd)1127 void kfd_inc_compute_active(struct kfd_dev *kfd)
1128 {
1129 	if (atomic_inc_return(&kfd->compute_profile) == 1)
1130 		amdgpu_amdkfd_set_compute_idle(kfd->adev, false);
1131 }
1132 
kfd_dec_compute_active(struct kfd_dev * kfd)1133 void kfd_dec_compute_active(struct kfd_dev *kfd)
1134 {
1135 	int count = atomic_dec_return(&kfd->compute_profile);
1136 
1137 	if (count == 0)
1138 		amdgpu_amdkfd_set_compute_idle(kfd->adev, true);
1139 	WARN_ONCE(count < 0, "Compute profile ref. count error");
1140 }
1141 
kgd2kfd_smi_event_throttle(struct kfd_dev * kfd,uint64_t throttle_bitmask)1142 void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask)
1143 {
1144 	if (kfd && kfd->init_complete)
1145 		kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask);
1146 }
1147 
1148 /* kfd_get_num_sdma_engines returns the number of PCIe optimized SDMA and
1149  * kfd_get_num_xgmi_sdma_engines returns the number of XGMI SDMA.
1150  * When the device has more than two engines, we reserve two for PCIe to enable
1151  * full-duplex and the rest are used as XGMI.
1152  */
kfd_get_num_sdma_engines(struct kfd_dev * kdev)1153 unsigned int kfd_get_num_sdma_engines(struct kfd_dev *kdev)
1154 {
1155 	/* If XGMI is not supported, all SDMA engines are PCIe */
1156 	if (!kdev->adev->gmc.xgmi.supported)
1157 		return kdev->adev->sdma.num_instances;
1158 
1159 	return min(kdev->adev->sdma.num_instances, 2);
1160 }
1161 
kfd_get_num_xgmi_sdma_engines(struct kfd_dev * kdev)1162 unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_dev *kdev)
1163 {
1164 	/* After reserved for PCIe, the rest of engines are XGMI */
1165 	return kdev->adev->sdma.num_instances - kfd_get_num_sdma_engines(kdev);
1166 }
1167 
1168 #if defined(CONFIG_DEBUG_FS)
1169 
1170 /* This function will send a package to HIQ to hang the HWS
1171  * which will trigger a GPU reset and bring the HWS back to normal state
1172  */
kfd_debugfs_hang_hws(struct kfd_dev * dev)1173 int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1174 {
1175 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1176 		pr_err("HWS is not enabled");
1177 		return -EINVAL;
1178 	}
1179 
1180 	return dqm_debugfs_hang_hws(dev->dqm);
1181 }
1182 
1183 #endif
1184