14a488a7aSOded Gabbay /*
24a488a7aSOded Gabbay  * Copyright 2014 Advanced Micro Devices, Inc.
34a488a7aSOded Gabbay  *
44a488a7aSOded Gabbay  * Permission is hereby granted, free of charge, to any person obtaining a
54a488a7aSOded Gabbay  * copy of this software and associated documentation files (the "Software"),
64a488a7aSOded Gabbay  * to deal in the Software without restriction, including without limitation
74a488a7aSOded Gabbay  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84a488a7aSOded Gabbay  * and/or sell copies of the Software, and to permit persons to whom the
94a488a7aSOded Gabbay  * Software is furnished to do so, subject to the following conditions:
104a488a7aSOded Gabbay  *
114a488a7aSOded Gabbay  * The above copyright notice and this permission notice shall be included in
124a488a7aSOded Gabbay  * all copies or substantial portions of the Software.
134a488a7aSOded Gabbay  *
144a488a7aSOded Gabbay  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154a488a7aSOded Gabbay  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164a488a7aSOded Gabbay  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
174a488a7aSOded Gabbay  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
184a488a7aSOded Gabbay  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194a488a7aSOded Gabbay  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
204a488a7aSOded Gabbay  * OTHER DEALINGS IN THE SOFTWARE.
214a488a7aSOded Gabbay  */
224a488a7aSOded Gabbay 
234a488a7aSOded Gabbay #include <linux/bsearch.h>
244a488a7aSOded Gabbay #include <linux/pci.h>
254a488a7aSOded Gabbay #include <linux/slab.h>
264a488a7aSOded Gabbay #include "kfd_priv.h"
2764c7f8cfSBen Goz #include "kfd_device_queue_manager.h"
28507968ddSFelix Kuehling #include "kfd_pm4_headers_vi.h"
29fd6a440eSJonathan Kim #include "kfd_pm4_headers_aldebaran.h"
300db54b24SYong Zhao #include "cwsr_trap_handler.h"
3164d1c3a4SFelix Kuehling #include "kfd_iommu.h"
325b87245fSAmber Lin #include "amdgpu_amdkfd.h"
332c2b0d88SMukul Joshi #include "kfd_smi_events.h"
34814ab993SPhilip Yang #include "kfd_migrate.h"
354a488a7aSOded Gabbay 
3619f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768
37e42051d2SShaoyun Liu 
38e42051d2SShaoyun Liu /*
39e42051d2SShaoyun Liu  * kfd_locked is used to lock the kfd driver during suspend or reset
40e42051d2SShaoyun Liu  * once locked, kfd driver will stop any further GPU execution.
41e42051d2SShaoyun Liu  * create process (open) will return -EAGAIN.
42e42051d2SShaoyun Liu  */
43e42051d2SShaoyun Liu static atomic_t kfd_locked = ATOMIC_INIT(0);
4419f6d2a6SOded Gabbay 
45a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK
46e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v7_kfd2kgd;
47a3e520a2SAlex Deucher #endif
48e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v8_kfd2kgd;
49e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v9_kfd2kgd;
50e392c887SYong Zhao extern const struct kfd2kgd_calls arcturus_kfd2kgd;
515073506cSJonathan Kim extern const struct kfd2kgd_calls aldebaran_kfd2kgd;
52e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v10_kfd2kgd;
533a2f0c81SYong Zhao extern const struct kfd2kgd_calls gfx_v10_3_kfd2kgd;
54e392c887SYong Zhao 
55e392c887SYong Zhao static const struct kfd2kgd_calls *kfd2kgd_funcs[] = {
56e392c887SYong Zhao #ifdef KFD_SUPPORT_IOMMU_V2
57a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK
58e392c887SYong Zhao 	[CHIP_KAVERI] = &gfx_v7_kfd2kgd,
59a3e520a2SAlex Deucher #endif
60e392c887SYong Zhao 	[CHIP_CARRIZO] = &gfx_v8_kfd2kgd,
61e392c887SYong Zhao 	[CHIP_RAVEN] = &gfx_v9_kfd2kgd,
62e392c887SYong Zhao #endif
63a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK
64e392c887SYong Zhao 	[CHIP_HAWAII] = &gfx_v7_kfd2kgd,
65a3e520a2SAlex Deucher #endif
66e392c887SYong Zhao 	[CHIP_TONGA] = &gfx_v8_kfd2kgd,
67e392c887SYong Zhao 	[CHIP_FIJI] = &gfx_v8_kfd2kgd,
68e392c887SYong Zhao 	[CHIP_POLARIS10] = &gfx_v8_kfd2kgd,
69e392c887SYong Zhao 	[CHIP_POLARIS11] = &gfx_v8_kfd2kgd,
70e392c887SYong Zhao 	[CHIP_POLARIS12] = &gfx_v8_kfd2kgd,
71e392c887SYong Zhao 	[CHIP_VEGAM] = &gfx_v8_kfd2kgd,
72e392c887SYong Zhao 	[CHIP_VEGA10] = &gfx_v9_kfd2kgd,
73e392c887SYong Zhao 	[CHIP_VEGA12] = &gfx_v9_kfd2kgd,
74e392c887SYong Zhao 	[CHIP_VEGA20] = &gfx_v9_kfd2kgd,
75e392c887SYong Zhao 	[CHIP_RENOIR] = &gfx_v9_kfd2kgd,
76e392c887SYong Zhao 	[CHIP_ARCTURUS] = &arcturus_kfd2kgd,
775073506cSJonathan Kim 	[CHIP_ALDEBARAN] = &aldebaran_kfd2kgd,
78e392c887SYong Zhao 	[CHIP_NAVI10] = &gfx_v10_kfd2kgd,
79e392c887SYong Zhao 	[CHIP_NAVI12] = &gfx_v10_kfd2kgd,
80e392c887SYong Zhao 	[CHIP_NAVI14] = &gfx_v10_kfd2kgd,
813a2f0c81SYong Zhao 	[CHIP_SIENNA_CICHLID] = &gfx_v10_3_kfd2kgd,
8209759e13SChengming Gui 	[CHIP_NAVY_FLOUNDER] = &gfx_v10_3_kfd2kgd,
833a5e715dSHuang Rui 	[CHIP_VANGOGH] = &gfx_v10_3_kfd2kgd,
848f72ce64SChengming Gui 	[CHIP_DIMGREY_CAVEFISH] = &gfx_v10_3_kfd2kgd,
85e392c887SYong Zhao };
86e392c887SYong Zhao 
8764d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
884a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = {
890da7558cSBen Goz 	.asic_family = CHIP_KAVERI,
90c181159aSYong Zhao 	.asic_name = "kaveri",
910da7558cSBen Goz 	.max_pasid_bits = 16,
92992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
93992839adSYair Shachar 	.max_no_of_hqd	= 24,
94ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
950da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
96f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
97fbeb661bSYair Shachar 	.num_of_watch_points = 4,
98373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
99373d7080SFelix Kuehling 	.supports_cwsr = false,
10064d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
1013ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
10298bb9222SYong Zhao 	.num_sdma_engines = 2,
1031b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
104d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1050da7558cSBen Goz };
1060da7558cSBen Goz 
1070da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
1080da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
109c181159aSYong Zhao 	.asic_name = "carrizo",
1104a488a7aSOded Gabbay 	.max_pasid_bits = 16,
111eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
112eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
113ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
114b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
115eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
116f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
117373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
118373d7080SFelix Kuehling 	.supports_cwsr = true,
11964d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
1203ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
12198bb9222SYong Zhao 	.num_sdma_engines = 2,
1221b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
123d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1244a488a7aSOded Gabbay };
1256127896fSHuang Rui #endif
1264d663df6SYong Zhao 
1274d663df6SYong Zhao static const struct kfd_device_info raven_device_info = {
1284d663df6SYong Zhao 	.asic_family = CHIP_RAVEN,
129c181159aSYong Zhao 	.asic_name = "raven",
1304d663df6SYong Zhao 	.max_pasid_bits = 16,
1314d663df6SYong Zhao 	.max_no_of_hqd  = 24,
1324d663df6SYong Zhao 	.doorbell_size  = 8,
1334d663df6SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
1344d663df6SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
1354d663df6SYong Zhao 	.num_of_watch_points = 4,
1364d663df6SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
1374d663df6SYong Zhao 	.supports_cwsr = true,
1384d663df6SYong Zhao 	.needs_iommu_device = true,
1394d663df6SYong Zhao 	.needs_pci_atomics = true,
1404d663df6SYong Zhao 	.num_sdma_engines = 1,
1411b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
142d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1434d663df6SYong Zhao };
1444a488a7aSOded Gabbay 
145a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
146a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
147c181159aSYong Zhao 	.asic_name = "hawaii",
148a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
149a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
150a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
151ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
152a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
153a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
154a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
155a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
156a3084e6cSFelix Kuehling 	.supports_cwsr = false,
15764d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
158a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
15998bb9222SYong Zhao 	.num_sdma_engines = 2,
1601b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
161d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
162a3084e6cSFelix Kuehling };
163a3084e6cSFelix Kuehling 
164a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
165a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
166c181159aSYong Zhao 	.asic_name = "tonga",
167a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
168a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
169ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
170a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
171a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
172a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
173a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
174a3084e6cSFelix Kuehling 	.supports_cwsr = false,
17564d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
176a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
17798bb9222SYong Zhao 	.num_sdma_engines = 2,
1781b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
179d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
180a3084e6cSFelix Kuehling };
181a3084e6cSFelix Kuehling 
182a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
183a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
184c181159aSYong Zhao 	.asic_name = "fiji",
185a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
186a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
187ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
188a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
189a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
190a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
191a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
192a3084e6cSFelix Kuehling 	.supports_cwsr = true,
19364d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
194a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
19598bb9222SYong Zhao 	.num_sdma_engines = 2,
1961b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
197d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
198a3084e6cSFelix Kuehling };
199a3084e6cSFelix Kuehling 
200a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
201a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
202c181159aSYong Zhao 	.asic_name = "fiji",
203a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
204a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
205ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
206a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
207a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
208a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
209a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
210a3084e6cSFelix Kuehling 	.supports_cwsr = true,
21164d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
212a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
21398bb9222SYong Zhao 	.num_sdma_engines = 2,
2141b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
215d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
216a3084e6cSFelix Kuehling };
217a3084e6cSFelix Kuehling 
218a3084e6cSFelix Kuehling 
219a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
220a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
221c181159aSYong Zhao 	.asic_name = "polaris10",
222a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
223a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
224ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
225a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
226a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
227a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
228a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
229a3084e6cSFelix Kuehling 	.supports_cwsr = true,
23064d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
231a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
23298bb9222SYong Zhao 	.num_sdma_engines = 2,
2331b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
234d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
235a3084e6cSFelix Kuehling };
236a3084e6cSFelix Kuehling 
237a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = {
238a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
239c181159aSYong Zhao 	.asic_name = "polaris10",
240a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
241a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
242ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
243a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
244a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
245a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
246a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
247a3084e6cSFelix Kuehling 	.supports_cwsr = true,
24864d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
249a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
25098bb9222SYong Zhao 	.num_sdma_engines = 2,
2511b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
252d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
253a3084e6cSFelix Kuehling };
254a3084e6cSFelix Kuehling 
255a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = {
256a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
257c181159aSYong Zhao 	.asic_name = "polaris11",
258a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
259a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
260ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
261a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
262a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
263a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
264a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
265a3084e6cSFelix Kuehling 	.supports_cwsr = true,
26664d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
267a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
26898bb9222SYong Zhao 	.num_sdma_engines = 2,
2691b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
270d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
271a3084e6cSFelix Kuehling };
272a3084e6cSFelix Kuehling 
273846a44d7SGang Ba static const struct kfd_device_info polaris12_device_info = {
274846a44d7SGang Ba 	.asic_family = CHIP_POLARIS12,
275c181159aSYong Zhao 	.asic_name = "polaris12",
276846a44d7SGang Ba 	.max_pasid_bits = 16,
277846a44d7SGang Ba 	.max_no_of_hqd  = 24,
278846a44d7SGang Ba 	.doorbell_size  = 4,
279846a44d7SGang Ba 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
280846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_cik,
281846a44d7SGang Ba 	.num_of_watch_points = 4,
282846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
283846a44d7SGang Ba 	.supports_cwsr = true,
284846a44d7SGang Ba 	.needs_iommu_device = false,
285846a44d7SGang Ba 	.needs_pci_atomics = true,
286846a44d7SGang Ba 	.num_sdma_engines = 2,
2871b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
288846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
289846a44d7SGang Ba };
290846a44d7SGang Ba 
291ed81cd6eSKent Russell static const struct kfd_device_info vegam_device_info = {
292ed81cd6eSKent Russell 	.asic_family = CHIP_VEGAM,
293c181159aSYong Zhao 	.asic_name = "vegam",
294ed81cd6eSKent Russell 	.max_pasid_bits = 16,
295ed81cd6eSKent Russell 	.max_no_of_hqd  = 24,
296ed81cd6eSKent Russell 	.doorbell_size  = 4,
297ed81cd6eSKent Russell 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
298ed81cd6eSKent Russell 	.event_interrupt_class = &event_interrupt_class_cik,
299ed81cd6eSKent Russell 	.num_of_watch_points = 4,
300ed81cd6eSKent Russell 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
301ed81cd6eSKent Russell 	.supports_cwsr = true,
302ed81cd6eSKent Russell 	.needs_iommu_device = false,
303ed81cd6eSKent Russell 	.needs_pci_atomics = true,
304ed81cd6eSKent Russell 	.num_sdma_engines = 2,
305ed81cd6eSKent Russell 	.num_xgmi_sdma_engines = 0,
306a3084e6cSFelix Kuehling 	.num_sdma_queues_per_engine = 2,
307a3084e6cSFelix Kuehling };
308a3084e6cSFelix Kuehling 
309389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = {
310389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
311c181159aSYong Zhao 	.asic_name = "vega10",
312389056e5SFelix Kuehling 	.max_pasid_bits = 16,
313389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
314389056e5SFelix Kuehling 	.doorbell_size  = 8,
315389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
316389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
317389056e5SFelix Kuehling 	.num_of_watch_points = 4,
318389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
319389056e5SFelix Kuehling 	.supports_cwsr = true,
320389056e5SFelix Kuehling 	.needs_iommu_device = false,
321389056e5SFelix Kuehling 	.needs_pci_atomics = false,
32298bb9222SYong Zhao 	.num_sdma_engines = 2,
3231b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
324d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
325389056e5SFelix Kuehling };
326389056e5SFelix Kuehling 
327389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_device_info = {
328389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
329c181159aSYong Zhao 	.asic_name = "vega10",
330389056e5SFelix Kuehling 	.max_pasid_bits = 16,
331389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
332389056e5SFelix Kuehling 	.doorbell_size  = 8,
333389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
334389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
335389056e5SFelix Kuehling 	.num_of_watch_points = 4,
336389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
337389056e5SFelix Kuehling 	.supports_cwsr = true,
338389056e5SFelix Kuehling 	.needs_iommu_device = false,
339389056e5SFelix Kuehling 	.needs_pci_atomics = false,
34098bb9222SYong Zhao 	.num_sdma_engines = 2,
3411b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
342d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
343389056e5SFelix Kuehling };
344389056e5SFelix Kuehling 
345846a44d7SGang Ba static const struct kfd_device_info vega12_device_info = {
346846a44d7SGang Ba 	.asic_family = CHIP_VEGA12,
347c181159aSYong Zhao 	.asic_name = "vega12",
348846a44d7SGang Ba 	.max_pasid_bits = 16,
349846a44d7SGang Ba 	.max_no_of_hqd  = 24,
350846a44d7SGang Ba 	.doorbell_size  = 8,
351846a44d7SGang Ba 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
352846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_v9,
353846a44d7SGang Ba 	.num_of_watch_points = 4,
354846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
355846a44d7SGang Ba 	.supports_cwsr = true,
356846a44d7SGang Ba 	.needs_iommu_device = false,
357846a44d7SGang Ba 	.needs_pci_atomics = false,
358846a44d7SGang Ba 	.num_sdma_engines = 2,
3591b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
360846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
361846a44d7SGang Ba };
362846a44d7SGang Ba 
36322a3a294SShaoyun Liu static const struct kfd_device_info vega20_device_info = {
36422a3a294SShaoyun Liu 	.asic_family = CHIP_VEGA20,
365c181159aSYong Zhao 	.asic_name = "vega20",
36622a3a294SShaoyun Liu 	.max_pasid_bits = 16,
36722a3a294SShaoyun Liu 	.max_no_of_hqd	= 24,
36822a3a294SShaoyun Liu 	.doorbell_size	= 8,
36922a3a294SShaoyun Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
37022a3a294SShaoyun Liu 	.event_interrupt_class = &event_interrupt_class_v9,
37122a3a294SShaoyun Liu 	.num_of_watch_points = 4,
37222a3a294SShaoyun Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
37322a3a294SShaoyun Liu 	.supports_cwsr = true,
37422a3a294SShaoyun Liu 	.needs_iommu_device = false,
375006a0b3dSShaoyun Liu 	.needs_pci_atomics = false,
37622a3a294SShaoyun Liu 	.num_sdma_engines = 2,
3771b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
37822a3a294SShaoyun Liu 	.num_sdma_queues_per_engine = 8,
37922a3a294SShaoyun Liu };
38022a3a294SShaoyun Liu 
38149adcf8aSYong Zhao static const struct kfd_device_info arcturus_device_info = {
38249adcf8aSYong Zhao 	.asic_family = CHIP_ARCTURUS,
383c181159aSYong Zhao 	.asic_name = "arcturus",
38449adcf8aSYong Zhao 	.max_pasid_bits = 16,
38549adcf8aSYong Zhao 	.max_no_of_hqd	= 24,
38649adcf8aSYong Zhao 	.doorbell_size	= 8,
38749adcf8aSYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
38849adcf8aSYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
38949adcf8aSYong Zhao 	.num_of_watch_points = 4,
39049adcf8aSYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
39149adcf8aSYong Zhao 	.supports_cwsr = true,
39249adcf8aSYong Zhao 	.needs_iommu_device = false,
39349adcf8aSYong Zhao 	.needs_pci_atomics = false,
394b6689cf7SOak Zeng 	.num_sdma_engines = 2,
395b6689cf7SOak Zeng 	.num_xgmi_sdma_engines = 6,
39649adcf8aSYong Zhao 	.num_sdma_queues_per_engine = 8,
39749adcf8aSYong Zhao };
39849adcf8aSYong Zhao 
39936e22d59SYong Zhao static const struct kfd_device_info aldebaran_device_info = {
40036e22d59SYong Zhao 	.asic_family = CHIP_ALDEBARAN,
40136e22d59SYong Zhao 	.asic_name = "aldebaran",
40236e22d59SYong Zhao 	.max_pasid_bits = 16,
40336e22d59SYong Zhao 	.max_no_of_hqd	= 24,
40436e22d59SYong Zhao 	.doorbell_size	= 8,
40536e22d59SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
40636e22d59SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
40736e22d59SYong Zhao 	.num_of_watch_points = 4,
40836e22d59SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
40936e22d59SYong Zhao 	.supports_cwsr = true,
41036e22d59SYong Zhao 	.needs_iommu_device = false,
41136e22d59SYong Zhao 	.needs_pci_atomics = false,
41236e22d59SYong Zhao 	.num_sdma_engines = 2,
41336e22d59SYong Zhao 	.num_xgmi_sdma_engines = 3,
41436e22d59SYong Zhao 	.num_sdma_queues_per_engine = 8,
41536e22d59SYong Zhao };
41636e22d59SYong Zhao 
4172b9c2211SHuang Rui static const struct kfd_device_info renoir_device_info = {
4182b9c2211SHuang Rui 	.asic_family = CHIP_RENOIR,
419acb9acbeSHuang Rui 	.asic_name = "renoir",
4202b9c2211SHuang Rui 	.max_pasid_bits = 16,
4212b9c2211SHuang Rui 	.max_no_of_hqd  = 24,
4222b9c2211SHuang Rui 	.doorbell_size  = 8,
4232b9c2211SHuang Rui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
4242b9c2211SHuang Rui 	.event_interrupt_class = &event_interrupt_class_v9,
4252b9c2211SHuang Rui 	.num_of_watch_points = 4,
4262b9c2211SHuang Rui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
4272b9c2211SHuang Rui 	.supports_cwsr = true,
4282b9c2211SHuang Rui 	.needs_iommu_device = false,
4292b9c2211SHuang Rui 	.needs_pci_atomics = false,
4302b9c2211SHuang Rui 	.num_sdma_engines = 1,
4312b9c2211SHuang Rui 	.num_xgmi_sdma_engines = 0,
4322b9c2211SHuang Rui 	.num_sdma_queues_per_engine = 2,
4332b9c2211SHuang Rui };
4342b9c2211SHuang Rui 
43514328aa5SPhilip Cox static const struct kfd_device_info navi10_device_info = {
43614328aa5SPhilip Cox 	.asic_family = CHIP_NAVI10,
437c181159aSYong Zhao 	.asic_name = "navi10",
43814328aa5SPhilip Cox 	.max_pasid_bits = 16,
43914328aa5SPhilip Cox 	.max_no_of_hqd  = 24,
44014328aa5SPhilip Cox 	.doorbell_size  = 8,
44114328aa5SPhilip Cox 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
44214328aa5SPhilip Cox 	.event_interrupt_class = &event_interrupt_class_v9,
44314328aa5SPhilip Cox 	.num_of_watch_points = 4,
44414328aa5SPhilip Cox 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
44514328aa5SPhilip Cox 	.needs_iommu_device = false,
44614328aa5SPhilip Cox 	.supports_cwsr = true,
4476cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
44814328aa5SPhilip Cox 	.num_sdma_engines = 2,
44914328aa5SPhilip Cox 	.num_xgmi_sdma_engines = 0,
45014328aa5SPhilip Cox 	.num_sdma_queues_per_engine = 8,
45114328aa5SPhilip Cox };
45214328aa5SPhilip Cox 
453b77fb9d8Sshaoyunl static const struct kfd_device_info navi12_device_info = {
4540e94b564Sshaoyunl 	.asic_family = CHIP_NAVI12,
455b77fb9d8Sshaoyunl 	.asic_name = "navi12",
456b77fb9d8Sshaoyunl 	.max_pasid_bits = 16,
457b77fb9d8Sshaoyunl 	.max_no_of_hqd  = 24,
458b77fb9d8Sshaoyunl 	.doorbell_size  = 8,
459b77fb9d8Sshaoyunl 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
460b77fb9d8Sshaoyunl 	.event_interrupt_class = &event_interrupt_class_v9,
461b77fb9d8Sshaoyunl 	.num_of_watch_points = 4,
462b77fb9d8Sshaoyunl 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
463b77fb9d8Sshaoyunl 	.needs_iommu_device = false,
464b77fb9d8Sshaoyunl 	.supports_cwsr = true,
4656cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
466b77fb9d8Sshaoyunl 	.num_sdma_engines = 2,
467b77fb9d8Sshaoyunl 	.num_xgmi_sdma_engines = 0,
468b77fb9d8Sshaoyunl 	.num_sdma_queues_per_engine = 8,
469b77fb9d8Sshaoyunl };
470b77fb9d8Sshaoyunl 
4718099ae40SYong Zhao static const struct kfd_device_info navi14_device_info = {
4728099ae40SYong Zhao 	.asic_family = CHIP_NAVI14,
4738099ae40SYong Zhao 	.asic_name = "navi14",
4748099ae40SYong Zhao 	.max_pasid_bits = 16,
4758099ae40SYong Zhao 	.max_no_of_hqd  = 24,
4768099ae40SYong Zhao 	.doorbell_size  = 8,
4778099ae40SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
4788099ae40SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
4798099ae40SYong Zhao 	.num_of_watch_points = 4,
4808099ae40SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
4818099ae40SYong Zhao 	.needs_iommu_device = false,
4828099ae40SYong Zhao 	.supports_cwsr = true,
4836cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
4848099ae40SYong Zhao 	.num_sdma_engines = 2,
4858099ae40SYong Zhao 	.num_xgmi_sdma_engines = 0,
4868099ae40SYong Zhao 	.num_sdma_queues_per_engine = 8,
4878099ae40SYong Zhao };
4888099ae40SYong Zhao 
4893a2f0c81SYong Zhao static const struct kfd_device_info sienna_cichlid_device_info = {
4903a2f0c81SYong Zhao 	.asic_family = CHIP_SIENNA_CICHLID,
4913a2f0c81SYong Zhao 	.asic_name = "sienna_cichlid",
4923a2f0c81SYong Zhao 	.max_pasid_bits = 16,
4933a2f0c81SYong Zhao 	.max_no_of_hqd  = 24,
4943a2f0c81SYong Zhao 	.doorbell_size  = 8,
4953a2f0c81SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
4963a2f0c81SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
4973a2f0c81SYong Zhao 	.num_of_watch_points = 4,
4983a2f0c81SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
4993a2f0c81SYong Zhao 	.needs_iommu_device = false,
5003a2f0c81SYong Zhao 	.supports_cwsr = true,
5016cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
5023a2f0c81SYong Zhao 	.num_sdma_engines = 4,
5033a2f0c81SYong Zhao 	.num_xgmi_sdma_engines = 0,
5043a2f0c81SYong Zhao 	.num_sdma_queues_per_engine = 8,
5053a2f0c81SYong Zhao };
5063a2f0c81SYong Zhao 
507de89b2e4SChengming Gui static const struct kfd_device_info navy_flounder_device_info = {
508de89b2e4SChengming Gui 	.asic_family = CHIP_NAVY_FLOUNDER,
509de89b2e4SChengming Gui 	.asic_name = "navy_flounder",
510de89b2e4SChengming Gui 	.max_pasid_bits = 16,
511de89b2e4SChengming Gui 	.max_no_of_hqd  = 24,
512de89b2e4SChengming Gui 	.doorbell_size  = 8,
513de89b2e4SChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
514de89b2e4SChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
515de89b2e4SChengming Gui 	.num_of_watch_points = 4,
516de89b2e4SChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
517de89b2e4SChengming Gui 	.needs_iommu_device = false,
518de89b2e4SChengming Gui 	.supports_cwsr = true,
5196cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
520de89b2e4SChengming Gui 	.num_sdma_engines = 2,
521de89b2e4SChengming Gui 	.num_xgmi_sdma_engines = 0,
522de89b2e4SChengming Gui 	.num_sdma_queues_per_engine = 8,
523de89b2e4SChengming Gui };
524de89b2e4SChengming Gui 
5253a5e715dSHuang Rui static const struct kfd_device_info vangogh_device_info = {
5263a5e715dSHuang Rui 	.asic_family = CHIP_VANGOGH,
5273a5e715dSHuang Rui 	.asic_name = "vangogh",
5283a5e715dSHuang Rui 	.max_pasid_bits = 16,
5293a5e715dSHuang Rui 	.max_no_of_hqd  = 24,
5303a5e715dSHuang Rui 	.doorbell_size  = 8,
5313a5e715dSHuang Rui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
5323a5e715dSHuang Rui 	.event_interrupt_class = &event_interrupt_class_v9,
5333a5e715dSHuang Rui 	.num_of_watch_points = 4,
5343a5e715dSHuang Rui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
5353a5e715dSHuang Rui 	.needs_iommu_device = false,
5363a5e715dSHuang Rui 	.supports_cwsr = true,
5373a5e715dSHuang Rui 	.needs_pci_atomics = false,
5383a5e715dSHuang Rui 	.num_sdma_engines = 1,
5393a5e715dSHuang Rui 	.num_xgmi_sdma_engines = 0,
5403a5e715dSHuang Rui 	.num_sdma_queues_per_engine = 2,
5413a5e715dSHuang Rui };
5423a5e715dSHuang Rui 
543eb5a34d4SChengming Gui static const struct kfd_device_info dimgrey_cavefish_device_info = {
544eb5a34d4SChengming Gui 	.asic_family = CHIP_DIMGREY_CAVEFISH,
545eb5a34d4SChengming Gui 	.asic_name = "dimgrey_cavefish",
546eb5a34d4SChengming Gui 	.max_pasid_bits = 16,
547eb5a34d4SChengming Gui 	.max_no_of_hqd  = 24,
548eb5a34d4SChengming Gui 	.doorbell_size  = 8,
549eb5a34d4SChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
550eb5a34d4SChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
551eb5a34d4SChengming Gui 	.num_of_watch_points = 4,
552eb5a34d4SChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
553eb5a34d4SChengming Gui 	.needs_iommu_device = false,
554eb5a34d4SChengming Gui 	.supports_cwsr = true,
5556cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
556eb5a34d4SChengming Gui 	.num_sdma_engines = 2,
557eb5a34d4SChengming Gui 	.num_xgmi_sdma_engines = 0,
558eb5a34d4SChengming Gui 	.num_sdma_queues_per_engine = 8,
559eb5a34d4SChengming Gui };
560eb5a34d4SChengming Gui 
561*5cf607ccSChengming Gui static const struct kfd_device_info beige_goby_device_info = {
562*5cf607ccSChengming Gui 	.asic_family = CHIP_BEIGE_GOBY,
563*5cf607ccSChengming Gui 	.asic_name = "beige_goby",
564*5cf607ccSChengming Gui 	.max_pasid_bits = 16,
565*5cf607ccSChengming Gui 	.max_no_of_hqd  = 24,
566*5cf607ccSChengming Gui 	.doorbell_size  = 8,
567*5cf607ccSChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
568*5cf607ccSChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
569*5cf607ccSChengming Gui 	.num_of_watch_points = 4,
570*5cf607ccSChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
571*5cf607ccSChengming Gui 	.needs_iommu_device = false,
572*5cf607ccSChengming Gui 	.supports_cwsr = true,
573*5cf607ccSChengming Gui 	.needs_pci_atomics = true,
574*5cf607ccSChengming Gui 	.num_sdma_engines = 1,
575*5cf607ccSChengming Gui 	.num_xgmi_sdma_engines = 0,
576*5cf607ccSChengming Gui 	.num_sdma_queues_per_engine = 8,
577*5cf607ccSChengming Gui };
578*5cf607ccSChengming Gui 
579eb5a34d4SChengming Gui 
580050091abSYong Zhao /* For each entry, [0] is regular and [1] is virtualisation device. */
581050091abSYong Zhao static const struct kfd_device_info *kfd_supported_devices[][2] = {
58295a5bd1bSYong Zhao #ifdef KFD_SUPPORT_IOMMU_V2
583050091abSYong Zhao 	[CHIP_KAVERI] = {&kaveri_device_info, NULL},
58495a5bd1bSYong Zhao 	[CHIP_CARRIZO] = {&carrizo_device_info, NULL},
58595a5bd1bSYong Zhao #endif
5862b3bbf23SYueHaibing 	[CHIP_RAVEN] = {&raven_device_info, NULL},
587050091abSYong Zhao 	[CHIP_HAWAII] = {&hawaii_device_info, NULL},
588050091abSYong Zhao 	[CHIP_TONGA] = {&tonga_device_info, NULL},
589050091abSYong Zhao 	[CHIP_FIJI] = {&fiji_device_info, &fiji_vf_device_info},
590050091abSYong Zhao 	[CHIP_POLARIS10] = {&polaris10_device_info, &polaris10_vf_device_info},
591050091abSYong Zhao 	[CHIP_POLARIS11] = {&polaris11_device_info, NULL},
592050091abSYong Zhao 	[CHIP_POLARIS12] = {&polaris12_device_info, NULL},
593050091abSYong Zhao 	[CHIP_VEGAM] = {&vegam_device_info, NULL},
594050091abSYong Zhao 	[CHIP_VEGA10] = {&vega10_device_info, &vega10_vf_device_info},
595050091abSYong Zhao 	[CHIP_VEGA12] = {&vega12_device_info, NULL},
596050091abSYong Zhao 	[CHIP_VEGA20] = {&vega20_device_info, NULL},
5972b9c2211SHuang Rui 	[CHIP_RENOIR] = {&renoir_device_info, NULL},
598050091abSYong Zhao 	[CHIP_ARCTURUS] = {&arcturus_device_info, &arcturus_device_info},
599cecd91b4SZhigang Luo 	[CHIP_ALDEBARAN] = {&aldebaran_device_info, &aldebaran_device_info},
600050091abSYong Zhao 	[CHIP_NAVI10] = {&navi10_device_info, NULL},
601b77fb9d8Sshaoyunl 	[CHIP_NAVI12] = {&navi12_device_info, &navi12_device_info},
6028099ae40SYong Zhao 	[CHIP_NAVI14] = {&navi14_device_info, NULL},
603adab4dadSshaoyunl 	[CHIP_SIENNA_CICHLID] = {&sienna_cichlid_device_info, &sienna_cichlid_device_info},
604de89b2e4SChengming Gui 	[CHIP_NAVY_FLOUNDER] = {&navy_flounder_device_info, &navy_flounder_device_info},
6053a5e715dSHuang Rui 	[CHIP_VANGOGH] = {&vangogh_device_info, NULL},
606eb5a34d4SChengming Gui 	[CHIP_DIMGREY_CAVEFISH] = {&dimgrey_cavefish_device_info, &dimgrey_cavefish_device_info},
607*5cf607ccSChengming Gui 	[CHIP_BEIGE_GOBY] = {&beige_goby_device_info, &beige_goby_device_info},
6084a488a7aSOded Gabbay };
6094a488a7aSOded Gabbay 
6106e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
6116e81090bSOded Gabbay 				unsigned int chunk_size);
6126e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
6136e81090bSOded Gabbay 
614b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
615b8935a7cSYong Zhao 
616cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
617e392c887SYong Zhao 	struct pci_dev *pdev, unsigned int asic_type, bool vf)
6184a488a7aSOded Gabbay {
6194a488a7aSOded Gabbay 	struct kfd_dev *kfd;
620050091abSYong Zhao 	const struct kfd_device_info *device_info;
621e392c887SYong Zhao 	const struct kfd2kgd_calls *f2g;
622050091abSYong Zhao 
623e392c887SYong Zhao 	if (asic_type >= sizeof(kfd_supported_devices) / (sizeof(void *) * 2)
624e392c887SYong Zhao 		|| asic_type >= sizeof(kfd2kgd_funcs) / sizeof(void *)) {
625050091abSYong Zhao 		dev_err(kfd_device, "asic_type %d out of range\n", asic_type);
626050091abSYong Zhao 		return NULL; /* asic_type out of range */
627050091abSYong Zhao 	}
628050091abSYong Zhao 
629050091abSYong Zhao 	device_info = kfd_supported_devices[asic_type][vf];
630e392c887SYong Zhao 	f2g = kfd2kgd_funcs[asic_type];
6314a488a7aSOded Gabbay 
632aa5e899dSDan Carpenter 	if (!device_info || !f2g) {
633050091abSYong Zhao 		dev_err(kfd_device, "%s %s not supported in kfd\n",
634050091abSYong Zhao 			amdgpu_asic_name[asic_type], vf ? "VF" : "");
6354a488a7aSOded Gabbay 		return NULL;
6364ebc7182SYong Zhao 	}
6374a488a7aSOded Gabbay 
638d35f00d8SEric Huang 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
639d35f00d8SEric Huang 	if (!kfd)
640d35f00d8SEric Huang 		return NULL;
641d35f00d8SEric Huang 
6426106dce9Swelu 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
6436106dce9Swelu 	 * 32 and 64-bit requests are possible and must be
6446106dce9Swelu 	 * supported.
6453ee2d00cSFelix Kuehling 	 */
646aabf3a95SJack Xiao 	kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kgd);
647aabf3a95SJack Xiao 	if (device_info->needs_pci_atomics &&
648aabf3a95SJack Xiao 	    !kfd->pci_atomic_requested) {
6493ee2d00cSFelix Kuehling 		dev_info(kfd_device,
6506106dce9Swelu 			 "skipped device %x:%x, PCI rejects atomics\n",
6513ee2d00cSFelix Kuehling 			 pdev->vendor, pdev->device);
652d35f00d8SEric Huang 		kfree(kfd);
6533ee2d00cSFelix Kuehling 		return NULL;
654aabf3a95SJack Xiao 	}
6554a488a7aSOded Gabbay 
6564a488a7aSOded Gabbay 	kfd->kgd = kgd;
6574a488a7aSOded Gabbay 	kfd->device_info = device_info;
6584a488a7aSOded Gabbay 	kfd->pdev = pdev;
65919f6d2a6SOded Gabbay 	kfd->init_complete = false;
660cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
66143d8107fSHarish Kasiviswanathan 	atomic_set(&kfd->compute_profile, 0);
662cea405b1SXihan Zhang 
663cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
664cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
665cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
6664a488a7aSOded Gabbay 
6679b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
6689b54d201SEric Huang 
66959d7115dSMukul Joshi 	ida_init(&kfd->doorbell_ida);
67059d7115dSMukul Joshi 
6714a488a7aSOded Gabbay 	return kfd;
6724a488a7aSOded Gabbay }
6734a488a7aSOded Gabbay 
674373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
675373d7080SFelix Kuehling {
676373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
6773e76c239SFelix Kuehling 		if (kfd->device_info->asic_family < CHIP_VEGA10) {
678373d7080SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
679373d7080SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
680373d7080SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
6810ef6845cSJay Cornwall 		} else if (kfd->device_info->asic_family == CHIP_ARCTURUS) {
6823baa24f0SOak Zeng 			BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
6833baa24f0SOak Zeng 			kfd->cwsr_isa = cwsr_trap_arcturus_hex;
6843baa24f0SOak Zeng 			kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
6850ef6845cSJay Cornwall 		} else if (kfd->device_info->asic_family == CHIP_ALDEBARAN) {
6860ef6845cSJay Cornwall 			BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex) > PAGE_SIZE);
6870ef6845cSJay Cornwall 			kfd->cwsr_isa = cwsr_trap_aldebaran_hex;
6880ef6845cSJay Cornwall 			kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex);
68914328aa5SPhilip Cox 		} else if (kfd->device_info->asic_family < CHIP_NAVI10) {
6903e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
6913e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
6923e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
69380b6cfedSJay Cornwall 		} else if (kfd->device_info->asic_family < CHIP_SIENNA_CICHLID) {
69480b6cfedSJay Cornwall 			BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE);
69580b6cfedSJay Cornwall 			kfd->cwsr_isa = cwsr_trap_nv1x_hex;
69680b6cfedSJay Cornwall 			kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);
69714328aa5SPhilip Cox 		} else {
69814328aa5SPhilip Cox 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
69914328aa5SPhilip Cox 			kfd->cwsr_isa = cwsr_trap_gfx10_hex;
70014328aa5SPhilip Cox 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
7013e76c239SFelix Kuehling 		}
7023e76c239SFelix Kuehling 
703373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
704373d7080SFelix Kuehling 	}
705373d7080SFelix Kuehling }
706373d7080SFelix Kuehling 
70729633d0eSJoseph Greathouse static int kfd_gws_init(struct kfd_dev *kfd)
70829633d0eSJoseph Greathouse {
70929633d0eSJoseph Greathouse 	int ret = 0;
71029633d0eSJoseph Greathouse 
71129633d0eSJoseph Greathouse 	if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
71229633d0eSJoseph Greathouse 		return 0;
71329633d0eSJoseph Greathouse 
71429633d0eSJoseph Greathouse 	if (hws_gws_support
715fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family == CHIP_VEGA10
716fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x81b3)
717fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family >= CHIP_VEGA12
71829633d0eSJoseph Greathouse 			&& kfd->device_info->asic_family <= CHIP_RAVEN
719fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x1b3)
720fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family == CHIP_ARCTURUS
7218baa6018SHarish Kasiviswanathan 			&& kfd->mec2_fw_version >= 0x30)
7228baa6018SHarish Kasiviswanathan 		|| (kfd->device_info->asic_family == CHIP_ALDEBARAN
7238baa6018SHarish Kasiviswanathan 			&& kfd->mec2_fw_version >= 0x28))
72429633d0eSJoseph Greathouse 		ret = amdgpu_amdkfd_alloc_gws(kfd->kgd,
72529633d0eSJoseph Greathouse 				amdgpu_amdkfd_get_num_gws(kfd->kgd), &kfd->gws);
72629633d0eSJoseph Greathouse 
72729633d0eSJoseph Greathouse 	return ret;
72829633d0eSJoseph Greathouse }
72929633d0eSJoseph Greathouse 
730938a0650SAmber Lin static void kfd_smi_init(struct kfd_dev *dev) {
731938a0650SAmber Lin 	INIT_LIST_HEAD(&dev->smi_clients);
732938a0650SAmber Lin 	spin_lock_init(&dev->smi_lock);
733938a0650SAmber Lin }
734938a0650SAmber Lin 
7354a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
7363a0c3423SHarish Kasiviswanathan 			 struct drm_device *ddev,
7374a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
7384a488a7aSOded Gabbay {
739fd6a440eSJonathan Kim 	unsigned int size, map_process_packet_size;
74019f6d2a6SOded Gabbay 
7413a0c3423SHarish Kasiviswanathan 	kfd->ddev = ddev;
7420da8b10eSAmber Lin 	kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
7435ade6c9cSFelix Kuehling 			KGD_ENGINE_MEC1);
74429633d0eSJoseph Greathouse 	kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
74529633d0eSJoseph Greathouse 			KGD_ENGINE_MEC2);
7460da8b10eSAmber Lin 	kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
7475ade6c9cSFelix Kuehling 			KGD_ENGINE_SDMA1);
7484a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
7494a488a7aSOded Gabbay 
75044008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
75144008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
75244008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
75344008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
75444008d7aSYong Zhao 
755a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
756a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
757a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
758a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
759a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
760a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
761a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
762a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
763a99c6d4fSFelix Kuehling 	} else
764a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
765a99c6d4fSFelix Kuehling 
76619f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
767b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
76819f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
76919f6d2a6SOded Gabbay 
770e18e794eSOded Gabbay 	/*
771e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
772e18e794eSOded Gabbay 	 * There can be only 2 packets at once
773e18e794eSOded Gabbay 	 */
774fd6a440eSJonathan Kim 	map_process_packet_size =
775fd6a440eSJonathan Kim 			kfd->device_info->asic_family == CHIP_ALDEBARAN ?
776fd6a440eSJonathan Kim 				sizeof(struct pm4_mes_map_process_aldebaran) :
777fd6a440eSJonathan Kim 					sizeof(struct pm4_mes_map_process);
778fd6a440eSJonathan Kim 	size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size +
779507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
780507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
781e18e794eSOded Gabbay 
782e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
783e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
784e18e794eSOded Gabbay 
785e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
78619f6d2a6SOded Gabbay 	size += 512 * 1024;
78719f6d2a6SOded Gabbay 
7887cd52c91SAmber Lin 	if (amdgpu_amdkfd_alloc_gtt_mem(
789cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
79015426dbbSYong Zhao 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
79115426dbbSYong Zhao 			false)) {
79279775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
793e09d4fc8SOak Zeng 		goto alloc_gtt_mem_failure;
79419f6d2a6SOded Gabbay 	}
79519f6d2a6SOded Gabbay 
79679775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
797e18e794eSOded Gabbay 
79873a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
79973a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
80079775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
80173a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
80273a1da0bSOded Gabbay 	}
80373a1da0bSOded Gabbay 
804735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
805735df2baSFelix Kuehling 		dev_err(kfd_device,
806735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
807735df2baSFelix Kuehling 		goto kfd_doorbell_error;
808735df2baSFelix Kuehling 	}
80919f6d2a6SOded Gabbay 
810332f6e1eSFelix Kuehling 	kfd->hive_id = amdgpu_amdkfd_get_hive_id(kfd->kgd);
8110c1690e3SShaoyun Liu 
8129b498efaSAlex Deucher 	kfd->noretry = amdgpu_amdkfd_get_noretry(kfd->kgd);
8139b498efaSAlex Deucher 
8142249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
81579775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
8162249d558SAndrew Lewycky 		goto kfd_interrupt_error;
8172249d558SAndrew Lewycky 	}
8182249d558SAndrew Lewycky 
81964c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
82064c7f8cfSBen Goz 	if (!kfd->dqm) {
82179775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
82264c7f8cfSBen Goz 		goto device_queue_manager_error;
82364c7f8cfSBen Goz 	}
82464c7f8cfSBen Goz 
82529633d0eSJoseph Greathouse 	/* If supported on this device, allocate global GWS that is shared
82629633d0eSJoseph Greathouse 	 * by all KFD processes
82729633d0eSJoseph Greathouse 	 */
82829633d0eSJoseph Greathouse 	if (kfd_gws_init(kfd)) {
82929633d0eSJoseph Greathouse 		dev_err(kfd_device, "Could not allocate %d gws\n",
83029633d0eSJoseph Greathouse 			amdgpu_amdkfd_get_num_gws(kfd->kgd));
83129633d0eSJoseph Greathouse 		goto gws_error;
83229633d0eSJoseph Greathouse 	}
83329633d0eSJoseph Greathouse 
8346127896fSHuang Rui 	/* If CRAT is broken, won't set iommu enabled */
8356127896fSHuang Rui 	kfd_double_confirm_iommu_support(kfd);
8366127896fSHuang Rui 
83764d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
83864d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
83964d1c3a4SFelix Kuehling 		goto device_iommu_error;
84064c7f8cfSBen Goz 	}
84164c7f8cfSBen Goz 
842373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
843373d7080SFelix Kuehling 
844814ab993SPhilip Yang 	svm_migrate_init((struct amdgpu_device *)kfd->kgd);
845814ab993SPhilip Yang 
846b8935a7cSYong Zhao 	if (kfd_resume(kfd))
847b8935a7cSYong Zhao 		goto kfd_resume_error;
848b8935a7cSYong Zhao 
849fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
850fbeb661bSYair Shachar 
851465ab9e0SOak Zeng 	if (kfd_topology_add_device(kfd)) {
852465ab9e0SOak Zeng 		dev_err(kfd_device, "Error adding device to topology\n");
853465ab9e0SOak Zeng 		goto kfd_topology_add_device_error;
854465ab9e0SOak Zeng 	}
855465ab9e0SOak Zeng 
856938a0650SAmber Lin 	kfd_smi_init(kfd);
857938a0650SAmber Lin 
8584a488a7aSOded Gabbay 	kfd->init_complete = true;
85979775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
8604a488a7aSOded Gabbay 		 kfd->pdev->device);
8614a488a7aSOded Gabbay 
86279775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
863d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
86464c7f8cfSBen Goz 
86519f6d2a6SOded Gabbay 	goto out;
86619f6d2a6SOded Gabbay 
867465ab9e0SOak Zeng kfd_topology_add_device_error:
868b8935a7cSYong Zhao kfd_resume_error:
86964d1c3a4SFelix Kuehling device_iommu_error:
87029633d0eSJoseph Greathouse gws_error:
87164c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
87264c7f8cfSBen Goz device_queue_manager_error:
8732249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
8742249d558SAndrew Lewycky kfd_interrupt_error:
875735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
876735df2baSFelix Kuehling kfd_doorbell_error:
87773a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
87873a1da0bSOded Gabbay kfd_gtt_sa_init_error:
8797cd52c91SAmber Lin 	amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
880e09d4fc8SOak Zeng alloc_gtt_mem_failure:
88129633d0eSJoseph Greathouse 	if (kfd->gws)
882e09d4fc8SOak Zeng 		amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
88319f6d2a6SOded Gabbay 	dev_err(kfd_device,
88479775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
88519f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
88619f6d2a6SOded Gabbay out:
88719f6d2a6SOded Gabbay 	return kfd->init_complete;
8884a488a7aSOded Gabbay }
8894a488a7aSOded Gabbay 
8904a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
8914a488a7aSOded Gabbay {
892b17f068aSOded Gabbay 	if (kfd->init_complete) {
8939593f4d6SRajneesh Bhardwaj 		kgd2kfd_suspend(kfd, false);
894814ab993SPhilip Yang 		svm_migrate_fini((struct amdgpu_device *)kfd->kgd);
89564c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
8962249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
89719f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
898735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
89959d7115dSMukul Joshi 		ida_destroy(&kfd->doorbell_ida);
90073a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
9017cd52c91SAmber Lin 		amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
90229633d0eSJoseph Greathouse 		if (kfd->gws)
903e09d4fc8SOak Zeng 			amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
904b17f068aSOded Gabbay 	}
9055b5c4e40SEvgeny Pinchuk 
9064a488a7aSOded Gabbay 	kfree(kfd);
9074a488a7aSOded Gabbay }
9084a488a7aSOded Gabbay 
909e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
910e3b7a967SShaoyun Liu {
911e42051d2SShaoyun Liu 	if (!kfd->init_complete)
912e42051d2SShaoyun Liu 		return 0;
91309c34e8dSFelix Kuehling 
91455977744SMukul Joshi 	kfd_smi_event_update_gpu_reset(kfd, false);
91555977744SMukul Joshi 
91609c34e8dSFelix Kuehling 	kfd->dqm->ops.pre_reset(kfd->dqm);
91709c34e8dSFelix Kuehling 
9189593f4d6SRajneesh Bhardwaj 	kgd2kfd_suspend(kfd, false);
919e42051d2SShaoyun Liu 
920e42051d2SShaoyun Liu 	kfd_signal_reset_event(kfd);
921e3b7a967SShaoyun Liu 	return 0;
922e3b7a967SShaoyun Liu }
923e3b7a967SShaoyun Liu 
924e42051d2SShaoyun Liu /*
925e42051d2SShaoyun Liu  * Fix me. KFD won't be able to resume existing process for now.
926e42051d2SShaoyun Liu  * We will keep all existing process in a evicted state and
927e42051d2SShaoyun Liu  * wait the process to be terminated.
928e42051d2SShaoyun Liu  */
929e42051d2SShaoyun Liu 
930e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
931e3b7a967SShaoyun Liu {
932a1bd079fSyu kuai 	int ret;
933e42051d2SShaoyun Liu 
934e42051d2SShaoyun Liu 	if (!kfd->init_complete)
935e3b7a967SShaoyun Liu 		return 0;
936e42051d2SShaoyun Liu 
937e42051d2SShaoyun Liu 	ret = kfd_resume(kfd);
938e42051d2SShaoyun Liu 	if (ret)
939e42051d2SShaoyun Liu 		return ret;
940a1bd079fSyu kuai 	atomic_dec(&kfd_locked);
9419b54d201SEric Huang 
9429b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
9439b54d201SEric Huang 
94455977744SMukul Joshi 	kfd_smi_event_update_gpu_reset(kfd, true);
94555977744SMukul Joshi 
946e42051d2SShaoyun Liu 	return 0;
947e42051d2SShaoyun Liu }
948e42051d2SShaoyun Liu 
949e42051d2SShaoyun Liu bool kfd_is_locked(void)
950e42051d2SShaoyun Liu {
951e42051d2SShaoyun Liu 	return  (atomic_read(&kfd_locked) > 0);
952e3b7a967SShaoyun Liu }
953e3b7a967SShaoyun Liu 
9549593f4d6SRajneesh Bhardwaj void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm)
9554a488a7aSOded Gabbay {
956733fa1f7SYong Zhao 	if (!kfd->init_complete)
957733fa1f7SYong Zhao 		return;
958733fa1f7SYong Zhao 
9599593f4d6SRajneesh Bhardwaj 	/* for runtime suspend, skip locking kfd */
9609593f4d6SRajneesh Bhardwaj 	if (!run_pm) {
96126103436SFelix Kuehling 		/* For first KFD device suspend all the KFD processes */
962e42051d2SShaoyun Liu 		if (atomic_inc_return(&kfd_locked) == 1)
96326103436SFelix Kuehling 			kfd_suspend_all_processes();
9649593f4d6SRajneesh Bhardwaj 	}
96526103436SFelix Kuehling 
96645c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
96764d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
9684a488a7aSOded Gabbay }
9694a488a7aSOded Gabbay 
9709593f4d6SRajneesh Bhardwaj int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm)
9714a488a7aSOded Gabbay {
97226103436SFelix Kuehling 	int ret, count;
97326103436SFelix Kuehling 
974b8935a7cSYong Zhao 	if (!kfd->init_complete)
975b8935a7cSYong Zhao 		return 0;
976b17f068aSOded Gabbay 
97726103436SFelix Kuehling 	ret = kfd_resume(kfd);
97826103436SFelix Kuehling 	if (ret)
97926103436SFelix Kuehling 		return ret;
980b17f068aSOded Gabbay 
9819593f4d6SRajneesh Bhardwaj 	/* for runtime resume, skip unlocking kfd */
9829593f4d6SRajneesh Bhardwaj 	if (!run_pm) {
983e42051d2SShaoyun Liu 		count = atomic_dec_return(&kfd_locked);
98426103436SFelix Kuehling 		WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
98526103436SFelix Kuehling 		if (count == 0)
98626103436SFelix Kuehling 			ret = kfd_resume_all_processes();
9879593f4d6SRajneesh Bhardwaj 	}
98826103436SFelix Kuehling 
98926103436SFelix Kuehling 	return ret;
9904ebc7182SYong Zhao }
9914ebc7182SYong Zhao 
992b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
993b8935a7cSYong Zhao {
994b8935a7cSYong Zhao 	int err = 0;
995b8935a7cSYong Zhao 
99664d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
99764d1c3a4SFelix Kuehling 	if (err) {
99864d1c3a4SFelix Kuehling 		dev_err(kfd_device,
99964d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
100064d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
100164d1c3a4SFelix Kuehling 		return err;
100264d1c3a4SFelix Kuehling 	}
1003733fa1f7SYong Zhao 
1004b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
1005b8935a7cSYong Zhao 	if (err) {
1006b8935a7cSYong Zhao 		dev_err(kfd_device,
1007b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
1008b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
1009b8935a7cSYong Zhao 		goto dqm_start_error;
1010b17f068aSOded Gabbay 	}
1011b17f068aSOded Gabbay 
1012b8935a7cSYong Zhao 	return err;
1013b8935a7cSYong Zhao 
1014b8935a7cSYong Zhao dqm_start_error:
101564d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
1016b8935a7cSYong Zhao 	return err;
10174a488a7aSOded Gabbay }
10184a488a7aSOded Gabbay 
1019b3eca59dSPhilip Yang static inline void kfd_queue_work(struct workqueue_struct *wq,
1020b3eca59dSPhilip Yang 				  struct work_struct *work)
1021b3eca59dSPhilip Yang {
1022b3eca59dSPhilip Yang 	int cpu, new_cpu;
1023b3eca59dSPhilip Yang 
1024b3eca59dSPhilip Yang 	cpu = new_cpu = smp_processor_id();
1025b3eca59dSPhilip Yang 	do {
1026b3eca59dSPhilip Yang 		new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
1027b3eca59dSPhilip Yang 		if (cpu_to_node(new_cpu) == numa_node_id())
1028b3eca59dSPhilip Yang 			break;
1029b3eca59dSPhilip Yang 	} while (cpu != new_cpu);
1030b3eca59dSPhilip Yang 
1031b3eca59dSPhilip Yang 	queue_work_on(new_cpu, wq, work);
1032b3eca59dSPhilip Yang }
1033b3eca59dSPhilip Yang 
1034b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
1035b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
10364a488a7aSOded Gabbay {
103758e69886SLan Xiao 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
103858e69886SLan Xiao 	bool is_patched = false;
10392383a767SChristian König 	unsigned long flags;
104058e69886SLan Xiao 
10412249d558SAndrew Lewycky 	if (!kfd->init_complete)
10422249d558SAndrew Lewycky 		return;
10432249d558SAndrew Lewycky 
104458e69886SLan Xiao 	if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
104558e69886SLan Xiao 		dev_err_once(kfd_device, "Ring entry too small\n");
104658e69886SLan Xiao 		return;
104758e69886SLan Xiao 	}
104858e69886SLan Xiao 
10492383a767SChristian König 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
10502249d558SAndrew Lewycky 
10512249d558SAndrew Lewycky 	if (kfd->interrupts_active
105258e69886SLan Xiao 	    && interrupt_is_wanted(kfd, ih_ring_entry,
105358e69886SLan Xiao 				   patched_ihre, &is_patched)
105458e69886SLan Xiao 	    && enqueue_ih_ring_entry(kfd,
105558e69886SLan Xiao 				     is_patched ? patched_ihre : ih_ring_entry))
1056b3eca59dSPhilip Yang 		kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work);
10572249d558SAndrew Lewycky 
10582383a767SChristian König 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
10594a488a7aSOded Gabbay }
10606e81090bSOded Gabbay 
10616b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
10626b95e797SFelix Kuehling {
10636b95e797SFelix Kuehling 	struct kfd_process *p;
10646b95e797SFelix Kuehling 	int r;
10656b95e797SFelix Kuehling 
10666b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
10676b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
10686b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
10696b95e797SFelix Kuehling 	 */
10706b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
10716b95e797SFelix Kuehling 	if (!p)
10726b95e797SFelix Kuehling 		return -ESRCH;
10736b95e797SFelix Kuehling 
1074b2057956SFelix Kuehling 	WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
10756b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
10766b95e797SFelix Kuehling 
10776b95e797SFelix Kuehling 	kfd_unref_process(p);
10786b95e797SFelix Kuehling 	return r;
10796b95e797SFelix Kuehling }
10806b95e797SFelix Kuehling 
10816b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
10826b95e797SFelix Kuehling {
10836b95e797SFelix Kuehling 	struct kfd_process *p;
10846b95e797SFelix Kuehling 	int r;
10856b95e797SFelix Kuehling 
10866b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
10876b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
10886b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
10896b95e797SFelix Kuehling 	 */
10906b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
10916b95e797SFelix Kuehling 	if (!p)
10926b95e797SFelix Kuehling 		return -ESRCH;
10936b95e797SFelix Kuehling 
10946b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
10956b95e797SFelix Kuehling 
10966b95e797SFelix Kuehling 	kfd_unref_process(p);
10976b95e797SFelix Kuehling 	return r;
10986b95e797SFelix Kuehling }
10996b95e797SFelix Kuehling 
110026103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
110126103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
110226103436SFelix Kuehling  *   process.
110326103436SFelix Kuehling  *
110426103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
110526103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
110626103436SFelix Kuehling  *
110726103436SFelix Kuehling  */
110826103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
110926103436SFelix Kuehling 					       struct dma_fence *fence)
111026103436SFelix Kuehling {
111126103436SFelix Kuehling 	struct kfd_process *p;
111226103436SFelix Kuehling 	unsigned long active_time;
111326103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
111426103436SFelix Kuehling 
111526103436SFelix Kuehling 	if (!fence)
111626103436SFelix Kuehling 		return -EINVAL;
111726103436SFelix Kuehling 
111826103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
111926103436SFelix Kuehling 		return 0;
112026103436SFelix Kuehling 
112126103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
112226103436SFelix Kuehling 	if (!p)
112326103436SFelix Kuehling 		return -ENODEV;
112426103436SFelix Kuehling 
112526103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
112626103436SFelix Kuehling 		goto out;
112726103436SFelix Kuehling 
112826103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
112926103436SFelix Kuehling 
113026103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
113126103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
113226103436SFelix Kuehling 	 */
113326103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
113426103436SFelix Kuehling 	if (delay_jiffies > active_time)
113526103436SFelix Kuehling 		delay_jiffies -= active_time;
113626103436SFelix Kuehling 	else
113726103436SFelix Kuehling 		delay_jiffies = 0;
113826103436SFelix Kuehling 
113926103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
114026103436SFelix Kuehling 	 * to kfd_evict_bo_worker
114126103436SFelix Kuehling 	 */
1142b2057956SFelix Kuehling 	WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",
1143b2057956SFelix Kuehling 	     p->lead_thread->pid, delay_jiffies);
114426103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
114526103436SFelix Kuehling out:
114626103436SFelix Kuehling 	kfd_unref_process(p);
114726103436SFelix Kuehling 	return 0;
114826103436SFelix Kuehling }
114926103436SFelix Kuehling 
11506e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
11516e81090bSOded Gabbay 				unsigned int chunk_size)
11526e81090bSOded Gabbay {
11538625ff9cSFelix Kuehling 	unsigned int num_of_longs;
11546e81090bSOded Gabbay 
115532fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
115632fa8219SFelix Kuehling 		return -EINVAL;
115732fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
115832fa8219SFelix Kuehling 		return -EINVAL;
115932fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
116032fa8219SFelix Kuehling 		return -EINVAL;
11616e81090bSOded Gabbay 
11626e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
11636e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
11646e81090bSOded Gabbay 
11658625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
11668625ff9cSFelix Kuehling 		BITS_PER_LONG;
11676e81090bSOded Gabbay 
11688625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
11696e81090bSOded Gabbay 
11706e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
11716e81090bSOded Gabbay 		return -ENOMEM;
11726e81090bSOded Gabbay 
117379775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
11746e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
11756e81090bSOded Gabbay 
11766e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
11776e81090bSOded Gabbay 
11786e81090bSOded Gabbay 	return 0;
11796e81090bSOded Gabbay 
11806e81090bSOded Gabbay }
11816e81090bSOded Gabbay 
11826e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
11836e81090bSOded Gabbay {
11846e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
11856e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
11866e81090bSOded Gabbay }
11876e81090bSOded Gabbay 
11886e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
11896e81090bSOded Gabbay 						unsigned int bit_num,
11906e81090bSOded Gabbay 						unsigned int chunk_size)
11916e81090bSOded Gabbay {
11926e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
11936e81090bSOded Gabbay }
11946e81090bSOded Gabbay 
11956e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
11966e81090bSOded Gabbay 						unsigned int bit_num,
11976e81090bSOded Gabbay 						unsigned int chunk_size)
11986e81090bSOded Gabbay {
11996e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
12006e81090bSOded Gabbay }
12016e81090bSOded Gabbay 
12026e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
12036e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
12046e81090bSOded Gabbay {
12056e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
12066e81090bSOded Gabbay 
12076e81090bSOded Gabbay 	if (size == 0)
12086e81090bSOded Gabbay 		return -EINVAL;
12096e81090bSOded Gabbay 
12106e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
12116e81090bSOded Gabbay 		return -ENOMEM;
12126e81090bSOded Gabbay 
12131cd106ecSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
12141cd106ecSFelix Kuehling 	if (!(*mem_obj))
12156e81090bSOded Gabbay 		return -ENOMEM;
12166e81090bSOded Gabbay 
121779775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
12186e81090bSOded Gabbay 
12196e81090bSOded Gabbay 	start_search = 0;
12206e81090bSOded Gabbay 
12216e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
12226e81090bSOded Gabbay 
12236e81090bSOded Gabbay kfd_gtt_restart_search:
12246e81090bSOded Gabbay 	/* Find the first chunk that is free */
12256e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
12266e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
12276e81090bSOded Gabbay 					start_search);
12286e81090bSOded Gabbay 
122979775b62SKent Russell 	pr_debug("Found = %d\n", found);
12306e81090bSOded Gabbay 
12316e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
12326e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
12336e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
12346e81090bSOded Gabbay 
12356e81090bSOded Gabbay 	/* Update fields of mem_obj */
12366e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
12376e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
12386e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
12396e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
12406e81090bSOded Gabbay 					found,
12416e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
12426e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
12436e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
12446e81090bSOded Gabbay 					found,
12456e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
12466e81090bSOded Gabbay 
124779775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
12486e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
12496e81090bSOded Gabbay 
12506e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
12516e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
125279775b62SKent Russell 		pr_debug("Single bit\n");
12536e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
12546e81090bSOded Gabbay 		goto kfd_gtt_out;
12556e81090bSOded Gabbay 	}
12566e81090bSOded Gabbay 
12576e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
12586e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
12596e81090bSOded Gabbay 	do {
12606e81090bSOded Gabbay 		(*mem_obj)->range_end =
12616e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
12626e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
12636e81090bSOded Gabbay 		/*
12646e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
12656e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
12666e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
12676e81090bSOded Gabbay 		 */
12686e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
12696e81090bSOded Gabbay 			start_search = found;
12706e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
12716e81090bSOded Gabbay 		}
12726e81090bSOded Gabbay 
12736e81090bSOded Gabbay 		/*
12746e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
12756e81090bSOded Gabbay 		 */
12766e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
12776e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
12786e81090bSOded Gabbay 
12796e81090bSOded Gabbay 		/* Check if we don't need another chunk */
12806e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
12816e81090bSOded Gabbay 			cur_size = 0;
12826e81090bSOded Gabbay 		else
12836e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
12846e81090bSOded Gabbay 
12856e81090bSOded Gabbay 	} while (cur_size > 0);
12866e81090bSOded Gabbay 
128779775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
12886e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
12896e81090bSOded Gabbay 
12906e81090bSOded Gabbay 	/* Mark the chunks as allocated */
12916e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
12926e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
12936e81090bSOded Gabbay 		found++)
12946e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
12956e81090bSOded Gabbay 
12966e81090bSOded Gabbay kfd_gtt_out:
12976e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
12986e81090bSOded Gabbay 	return 0;
12996e81090bSOded Gabbay 
13006e81090bSOded Gabbay kfd_gtt_no_free_chunk:
13013148a6a0SJack Zhang 	pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);
13026e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
13033148a6a0SJack Zhang 	kfree(*mem_obj);
13046e81090bSOded Gabbay 	return -ENOMEM;
13056e81090bSOded Gabbay }
13066e81090bSOded Gabbay 
13076e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
13086e81090bSOded Gabbay {
13096e81090bSOded Gabbay 	unsigned int bit;
13106e81090bSOded Gabbay 
13119216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
13129216ed29SOded Gabbay 	if (!mem_obj)
13139216ed29SOded Gabbay 		return 0;
13146e81090bSOded Gabbay 
131579775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
13166e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
13176e81090bSOded Gabbay 
13186e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
13196e81090bSOded Gabbay 
13206e81090bSOded Gabbay 	/* Mark the chunks as free */
13216e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
13226e81090bSOded Gabbay 		bit <= mem_obj->range_end;
13236e81090bSOded Gabbay 		bit++)
13246e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
13256e81090bSOded Gabbay 
13266e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
13276e81090bSOded Gabbay 
13286e81090bSOded Gabbay 	kfree(mem_obj);
13296e81090bSOded Gabbay 	return 0;
13306e81090bSOded Gabbay }
1331a29ec470SShaoyun Liu 
13329b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
13339b54d201SEric Huang {
13349b54d201SEric Huang 	if (kfd)
13359b54d201SEric Huang 		atomic_inc(&kfd->sram_ecc_flag);
13369b54d201SEric Huang }
13379b54d201SEric Huang 
133843d8107fSHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd)
133943d8107fSHarish Kasiviswanathan {
134043d8107fSHarish Kasiviswanathan 	if (atomic_inc_return(&kfd->compute_profile) == 1)
134143d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, false);
134243d8107fSHarish Kasiviswanathan }
134343d8107fSHarish Kasiviswanathan 
134443d8107fSHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd)
134543d8107fSHarish Kasiviswanathan {
134643d8107fSHarish Kasiviswanathan 	int count = atomic_dec_return(&kfd->compute_profile);
134743d8107fSHarish Kasiviswanathan 
134843d8107fSHarish Kasiviswanathan 	if (count == 0)
134943d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, true);
135043d8107fSHarish Kasiviswanathan 	WARN_ONCE(count < 0, "Compute profile ref. count error");
135143d8107fSHarish Kasiviswanathan }
135243d8107fSHarish Kasiviswanathan 
13532c2b0d88SMukul Joshi void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint32_t throttle_bitmask)
13542c2b0d88SMukul Joshi {
1355158fc08dSAmber Lin 	if (kfd && kfd->init_complete)
13562c2b0d88SMukul Joshi 		kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask);
13572c2b0d88SMukul Joshi }
13582c2b0d88SMukul Joshi 
1359a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
1360a29ec470SShaoyun Liu 
1361a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
1362a29ec470SShaoyun Liu  * which will trigger a GPU reset and bring the HWS back to normal state
1363a29ec470SShaoyun Liu  */
1364a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1365a29ec470SShaoyun Liu {
1366a29ec470SShaoyun Liu 	int r = 0;
1367a29ec470SShaoyun Liu 
1368a29ec470SShaoyun Liu 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1369a29ec470SShaoyun Liu 		pr_err("HWS is not enabled");
1370a29ec470SShaoyun Liu 		return -EINVAL;
1371a29ec470SShaoyun Liu 	}
1372a29ec470SShaoyun Liu 
1373a29ec470SShaoyun Liu 	r = pm_debugfs_hang_hws(&dev->dqm->packets);
1374a29ec470SShaoyun Liu 	if (!r)
1375a29ec470SShaoyun Liu 		r = dqm_debugfs_execute_queues(dev->dqm);
1376a29ec470SShaoyun Liu 
1377a29ec470SShaoyun Liu 	return r;
1378a29ec470SShaoyun Liu }
1379a29ec470SShaoyun Liu 
1380a29ec470SShaoyun Liu #endif
1381