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,
85c86eb517SChengming Gui 	[CHIP_BEIGE_GOBY] = &gfx_v10_3_kfd2kgd,
86bf9d4e88SAaron Liu 	[CHIP_YELLOW_CARP] = &gfx_v10_3_kfd2kgd,
87e392c887SYong Zhao };
88e392c887SYong Zhao 
8964d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
904a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = {
910da7558cSBen Goz 	.asic_family = CHIP_KAVERI,
92c181159aSYong Zhao 	.asic_name = "kaveri",
930da7558cSBen Goz 	.max_pasid_bits = 16,
94992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
95992839adSYair Shachar 	.max_no_of_hqd	= 24,
96ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
970da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
98f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
99fbeb661bSYair Shachar 	.num_of_watch_points = 4,
100373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
101373d7080SFelix Kuehling 	.supports_cwsr = false,
10264d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
1033ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
10498bb9222SYong Zhao 	.num_sdma_engines = 2,
1051b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
106d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1070da7558cSBen Goz };
1080da7558cSBen Goz 
1090da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
1100da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
111c181159aSYong Zhao 	.asic_name = "carrizo",
1124a488a7aSOded Gabbay 	.max_pasid_bits = 16,
113eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
114eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
115ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
116b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
117eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
118f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
119373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
120373d7080SFelix Kuehling 	.supports_cwsr = true,
12164d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
1223ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
12398bb9222SYong Zhao 	.num_sdma_engines = 2,
1241b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
125d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1264a488a7aSOded Gabbay };
1276127896fSHuang Rui #endif
1284d663df6SYong Zhao 
1294d663df6SYong Zhao static const struct kfd_device_info raven_device_info = {
1304d663df6SYong Zhao 	.asic_family = CHIP_RAVEN,
131c181159aSYong Zhao 	.asic_name = "raven",
1324d663df6SYong Zhao 	.max_pasid_bits = 16,
1334d663df6SYong Zhao 	.max_no_of_hqd  = 24,
1344d663df6SYong Zhao 	.doorbell_size  = 8,
1354d663df6SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
1364d663df6SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
1374d663df6SYong Zhao 	.num_of_watch_points = 4,
1384d663df6SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
1394d663df6SYong Zhao 	.supports_cwsr = true,
1404d663df6SYong Zhao 	.needs_iommu_device = true,
1414d663df6SYong Zhao 	.needs_pci_atomics = true,
1424d663df6SYong Zhao 	.num_sdma_engines = 1,
1431b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
144d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1454d663df6SYong Zhao };
1464a488a7aSOded Gabbay 
147a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
148a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
149c181159aSYong Zhao 	.asic_name = "hawaii",
150a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
151a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
152a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
153ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
154a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
155a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
156a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
157a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
158a3084e6cSFelix Kuehling 	.supports_cwsr = false,
15964d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
160a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
16198bb9222SYong Zhao 	.num_sdma_engines = 2,
1621b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
163d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
164a3084e6cSFelix Kuehling };
165a3084e6cSFelix Kuehling 
166a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
167a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
168c181159aSYong Zhao 	.asic_name = "tonga",
169a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
170a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
171ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
172a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
173a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
174a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
175a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
176a3084e6cSFelix Kuehling 	.supports_cwsr = false,
17764d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
178a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
17998bb9222SYong Zhao 	.num_sdma_engines = 2,
1801b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
181d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
182a3084e6cSFelix Kuehling };
183a3084e6cSFelix Kuehling 
184a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
185a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
186c181159aSYong Zhao 	.asic_name = "fiji",
187a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
188a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
189ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
190a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
191a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
192a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
193a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
194a3084e6cSFelix Kuehling 	.supports_cwsr = true,
19564d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
196a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
19798bb9222SYong Zhao 	.num_sdma_engines = 2,
1981b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
199d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
200a3084e6cSFelix Kuehling };
201a3084e6cSFelix Kuehling 
202a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
203a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
204c181159aSYong Zhao 	.asic_name = "fiji",
205a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
206a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
207ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
208a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
209a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
210a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
211a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
212a3084e6cSFelix Kuehling 	.supports_cwsr = true,
21364d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
214a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
21598bb9222SYong Zhao 	.num_sdma_engines = 2,
2161b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
217d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
218a3084e6cSFelix Kuehling };
219a3084e6cSFelix Kuehling 
220a3084e6cSFelix Kuehling 
221a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
222a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
223c181159aSYong Zhao 	.asic_name = "polaris10",
224a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
225a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
226ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
227a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
228a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
229a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
230a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
231a3084e6cSFelix Kuehling 	.supports_cwsr = true,
23264d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
233a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
23498bb9222SYong Zhao 	.num_sdma_engines = 2,
2351b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
236d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
237a3084e6cSFelix Kuehling };
238a3084e6cSFelix Kuehling 
239a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = {
240a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
241c181159aSYong Zhao 	.asic_name = "polaris10",
242a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
243a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
244ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
245a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
246a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
247a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
248a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
249a3084e6cSFelix Kuehling 	.supports_cwsr = true,
25064d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
251a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
25298bb9222SYong Zhao 	.num_sdma_engines = 2,
2531b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
254d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
255a3084e6cSFelix Kuehling };
256a3084e6cSFelix Kuehling 
257a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = {
258a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
259c181159aSYong Zhao 	.asic_name = "polaris11",
260a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
261a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
262ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
263a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
264a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
265a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
266a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
267a3084e6cSFelix Kuehling 	.supports_cwsr = true,
26864d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
269a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
27098bb9222SYong Zhao 	.num_sdma_engines = 2,
2711b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
272d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
273a3084e6cSFelix Kuehling };
274a3084e6cSFelix Kuehling 
275846a44d7SGang Ba static const struct kfd_device_info polaris12_device_info = {
276846a44d7SGang Ba 	.asic_family = CHIP_POLARIS12,
277c181159aSYong Zhao 	.asic_name = "polaris12",
278846a44d7SGang Ba 	.max_pasid_bits = 16,
279846a44d7SGang Ba 	.max_no_of_hqd  = 24,
280846a44d7SGang Ba 	.doorbell_size  = 4,
281846a44d7SGang Ba 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
282846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_cik,
283846a44d7SGang Ba 	.num_of_watch_points = 4,
284846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
285846a44d7SGang Ba 	.supports_cwsr = true,
286846a44d7SGang Ba 	.needs_iommu_device = false,
287846a44d7SGang Ba 	.needs_pci_atomics = true,
288846a44d7SGang Ba 	.num_sdma_engines = 2,
2891b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
290846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
291846a44d7SGang Ba };
292846a44d7SGang Ba 
293ed81cd6eSKent Russell static const struct kfd_device_info vegam_device_info = {
294ed81cd6eSKent Russell 	.asic_family = CHIP_VEGAM,
295c181159aSYong Zhao 	.asic_name = "vegam",
296ed81cd6eSKent Russell 	.max_pasid_bits = 16,
297ed81cd6eSKent Russell 	.max_no_of_hqd  = 24,
298ed81cd6eSKent Russell 	.doorbell_size  = 4,
299ed81cd6eSKent Russell 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
300ed81cd6eSKent Russell 	.event_interrupt_class = &event_interrupt_class_cik,
301ed81cd6eSKent Russell 	.num_of_watch_points = 4,
302ed81cd6eSKent Russell 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
303ed81cd6eSKent Russell 	.supports_cwsr = true,
304ed81cd6eSKent Russell 	.needs_iommu_device = false,
305ed81cd6eSKent Russell 	.needs_pci_atomics = true,
306ed81cd6eSKent Russell 	.num_sdma_engines = 2,
307ed81cd6eSKent Russell 	.num_xgmi_sdma_engines = 0,
308a3084e6cSFelix Kuehling 	.num_sdma_queues_per_engine = 2,
309a3084e6cSFelix Kuehling };
310a3084e6cSFelix Kuehling 
311389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = {
312389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
313c181159aSYong Zhao 	.asic_name = "vega10",
314389056e5SFelix Kuehling 	.max_pasid_bits = 16,
315389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
316389056e5SFelix Kuehling 	.doorbell_size  = 8,
317389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
318389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
319389056e5SFelix Kuehling 	.num_of_watch_points = 4,
320389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
321389056e5SFelix Kuehling 	.supports_cwsr = true,
322389056e5SFelix Kuehling 	.needs_iommu_device = false,
323389056e5SFelix Kuehling 	.needs_pci_atomics = false,
32498bb9222SYong Zhao 	.num_sdma_engines = 2,
3251b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
326d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
327389056e5SFelix Kuehling };
328389056e5SFelix Kuehling 
329389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_device_info = {
330389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
331c181159aSYong Zhao 	.asic_name = "vega10",
332389056e5SFelix Kuehling 	.max_pasid_bits = 16,
333389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
334389056e5SFelix Kuehling 	.doorbell_size  = 8,
335389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
336389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
337389056e5SFelix Kuehling 	.num_of_watch_points = 4,
338389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
339389056e5SFelix Kuehling 	.supports_cwsr = true,
340389056e5SFelix Kuehling 	.needs_iommu_device = false,
341389056e5SFelix Kuehling 	.needs_pci_atomics = false,
34298bb9222SYong Zhao 	.num_sdma_engines = 2,
3431b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
344d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
345389056e5SFelix Kuehling };
346389056e5SFelix Kuehling 
347846a44d7SGang Ba static const struct kfd_device_info vega12_device_info = {
348846a44d7SGang Ba 	.asic_family = CHIP_VEGA12,
349c181159aSYong Zhao 	.asic_name = "vega12",
350846a44d7SGang Ba 	.max_pasid_bits = 16,
351846a44d7SGang Ba 	.max_no_of_hqd  = 24,
352846a44d7SGang Ba 	.doorbell_size  = 8,
353846a44d7SGang Ba 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
354846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_v9,
355846a44d7SGang Ba 	.num_of_watch_points = 4,
356846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
357846a44d7SGang Ba 	.supports_cwsr = true,
358846a44d7SGang Ba 	.needs_iommu_device = false,
359846a44d7SGang Ba 	.needs_pci_atomics = false,
360846a44d7SGang Ba 	.num_sdma_engines = 2,
3611b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
362846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
363846a44d7SGang Ba };
364846a44d7SGang Ba 
36522a3a294SShaoyun Liu static const struct kfd_device_info vega20_device_info = {
36622a3a294SShaoyun Liu 	.asic_family = CHIP_VEGA20,
367c181159aSYong Zhao 	.asic_name = "vega20",
36822a3a294SShaoyun Liu 	.max_pasid_bits = 16,
36922a3a294SShaoyun Liu 	.max_no_of_hqd	= 24,
37022a3a294SShaoyun Liu 	.doorbell_size	= 8,
37122a3a294SShaoyun Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
37222a3a294SShaoyun Liu 	.event_interrupt_class = &event_interrupt_class_v9,
37322a3a294SShaoyun Liu 	.num_of_watch_points = 4,
37422a3a294SShaoyun Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
37522a3a294SShaoyun Liu 	.supports_cwsr = true,
37622a3a294SShaoyun Liu 	.needs_iommu_device = false,
377006a0b3dSShaoyun Liu 	.needs_pci_atomics = false,
37822a3a294SShaoyun Liu 	.num_sdma_engines = 2,
3791b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
38022a3a294SShaoyun Liu 	.num_sdma_queues_per_engine = 8,
38122a3a294SShaoyun Liu };
38222a3a294SShaoyun Liu 
38349adcf8aSYong Zhao static const struct kfd_device_info arcturus_device_info = {
38449adcf8aSYong Zhao 	.asic_family = CHIP_ARCTURUS,
385c181159aSYong Zhao 	.asic_name = "arcturus",
38649adcf8aSYong Zhao 	.max_pasid_bits = 16,
38749adcf8aSYong Zhao 	.max_no_of_hqd	= 24,
38849adcf8aSYong Zhao 	.doorbell_size	= 8,
38949adcf8aSYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
39049adcf8aSYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
39149adcf8aSYong Zhao 	.num_of_watch_points = 4,
39249adcf8aSYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
39349adcf8aSYong Zhao 	.supports_cwsr = true,
39449adcf8aSYong Zhao 	.needs_iommu_device = false,
39549adcf8aSYong Zhao 	.needs_pci_atomics = false,
396b6689cf7SOak Zeng 	.num_sdma_engines = 2,
397b6689cf7SOak Zeng 	.num_xgmi_sdma_engines = 6,
39849adcf8aSYong Zhao 	.num_sdma_queues_per_engine = 8,
39949adcf8aSYong Zhao };
40049adcf8aSYong Zhao 
40136e22d59SYong Zhao static const struct kfd_device_info aldebaran_device_info = {
40236e22d59SYong Zhao 	.asic_family = CHIP_ALDEBARAN,
40336e22d59SYong Zhao 	.asic_name = "aldebaran",
40436e22d59SYong Zhao 	.max_pasid_bits = 16,
40536e22d59SYong Zhao 	.max_no_of_hqd	= 24,
40636e22d59SYong Zhao 	.doorbell_size	= 8,
40736e22d59SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
40836e22d59SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
40936e22d59SYong Zhao 	.num_of_watch_points = 4,
41036e22d59SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
41136e22d59SYong Zhao 	.supports_cwsr = true,
41236e22d59SYong Zhao 	.needs_iommu_device = false,
41336e22d59SYong Zhao 	.needs_pci_atomics = false,
41436e22d59SYong Zhao 	.num_sdma_engines = 2,
41536e22d59SYong Zhao 	.num_xgmi_sdma_engines = 3,
41636e22d59SYong Zhao 	.num_sdma_queues_per_engine = 8,
41736e22d59SYong Zhao };
41836e22d59SYong Zhao 
4192b9c2211SHuang Rui static const struct kfd_device_info renoir_device_info = {
4202b9c2211SHuang Rui 	.asic_family = CHIP_RENOIR,
421acb9acbeSHuang Rui 	.asic_name = "renoir",
4222b9c2211SHuang Rui 	.max_pasid_bits = 16,
4232b9c2211SHuang Rui 	.max_no_of_hqd  = 24,
4242b9c2211SHuang Rui 	.doorbell_size  = 8,
4252b9c2211SHuang Rui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
4262b9c2211SHuang Rui 	.event_interrupt_class = &event_interrupt_class_v9,
4272b9c2211SHuang Rui 	.num_of_watch_points = 4,
4282b9c2211SHuang Rui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
4292b9c2211SHuang Rui 	.supports_cwsr = true,
4302b9c2211SHuang Rui 	.needs_iommu_device = false,
4312b9c2211SHuang Rui 	.needs_pci_atomics = false,
4322b9c2211SHuang Rui 	.num_sdma_engines = 1,
4332b9c2211SHuang Rui 	.num_xgmi_sdma_engines = 0,
4342b9c2211SHuang Rui 	.num_sdma_queues_per_engine = 2,
4352b9c2211SHuang Rui };
4362b9c2211SHuang Rui 
43714328aa5SPhilip Cox static const struct kfd_device_info navi10_device_info = {
43814328aa5SPhilip Cox 	.asic_family = CHIP_NAVI10,
439c181159aSYong Zhao 	.asic_name = "navi10",
44014328aa5SPhilip Cox 	.max_pasid_bits = 16,
44114328aa5SPhilip Cox 	.max_no_of_hqd  = 24,
44214328aa5SPhilip Cox 	.doorbell_size  = 8,
44314328aa5SPhilip Cox 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
44414328aa5SPhilip Cox 	.event_interrupt_class = &event_interrupt_class_v9,
44514328aa5SPhilip Cox 	.num_of_watch_points = 4,
44614328aa5SPhilip Cox 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
44714328aa5SPhilip Cox 	.needs_iommu_device = false,
44814328aa5SPhilip Cox 	.supports_cwsr = true,
4496cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
45014328aa5SPhilip Cox 	.num_sdma_engines = 2,
45114328aa5SPhilip Cox 	.num_xgmi_sdma_engines = 0,
45214328aa5SPhilip Cox 	.num_sdma_queues_per_engine = 8,
45314328aa5SPhilip Cox };
45414328aa5SPhilip Cox 
455b77fb9d8Sshaoyunl static const struct kfd_device_info navi12_device_info = {
4560e94b564Sshaoyunl 	.asic_family = CHIP_NAVI12,
457b77fb9d8Sshaoyunl 	.asic_name = "navi12",
458b77fb9d8Sshaoyunl 	.max_pasid_bits = 16,
459b77fb9d8Sshaoyunl 	.max_no_of_hqd  = 24,
460b77fb9d8Sshaoyunl 	.doorbell_size  = 8,
461b77fb9d8Sshaoyunl 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
462b77fb9d8Sshaoyunl 	.event_interrupt_class = &event_interrupt_class_v9,
463b77fb9d8Sshaoyunl 	.num_of_watch_points = 4,
464b77fb9d8Sshaoyunl 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
465b77fb9d8Sshaoyunl 	.needs_iommu_device = false,
466b77fb9d8Sshaoyunl 	.supports_cwsr = true,
4676cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
468b77fb9d8Sshaoyunl 	.num_sdma_engines = 2,
469b77fb9d8Sshaoyunl 	.num_xgmi_sdma_engines = 0,
470b77fb9d8Sshaoyunl 	.num_sdma_queues_per_engine = 8,
471b77fb9d8Sshaoyunl };
472b77fb9d8Sshaoyunl 
4738099ae40SYong Zhao static const struct kfd_device_info navi14_device_info = {
4748099ae40SYong Zhao 	.asic_family = CHIP_NAVI14,
4758099ae40SYong Zhao 	.asic_name = "navi14",
4768099ae40SYong Zhao 	.max_pasid_bits = 16,
4778099ae40SYong Zhao 	.max_no_of_hqd  = 24,
4788099ae40SYong Zhao 	.doorbell_size  = 8,
4798099ae40SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
4808099ae40SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
4818099ae40SYong Zhao 	.num_of_watch_points = 4,
4828099ae40SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
4838099ae40SYong Zhao 	.needs_iommu_device = false,
4848099ae40SYong Zhao 	.supports_cwsr = true,
4856cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
4868099ae40SYong Zhao 	.num_sdma_engines = 2,
4878099ae40SYong Zhao 	.num_xgmi_sdma_engines = 0,
4888099ae40SYong Zhao 	.num_sdma_queues_per_engine = 8,
4898099ae40SYong Zhao };
4908099ae40SYong Zhao 
4913a2f0c81SYong Zhao static const struct kfd_device_info sienna_cichlid_device_info = {
4923a2f0c81SYong Zhao 	.asic_family = CHIP_SIENNA_CICHLID,
4933a2f0c81SYong Zhao 	.asic_name = "sienna_cichlid",
4943a2f0c81SYong Zhao 	.max_pasid_bits = 16,
4953a2f0c81SYong Zhao 	.max_no_of_hqd  = 24,
4963a2f0c81SYong Zhao 	.doorbell_size  = 8,
4973a2f0c81SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
4983a2f0c81SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
4993a2f0c81SYong Zhao 	.num_of_watch_points = 4,
5003a2f0c81SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
5013a2f0c81SYong Zhao 	.needs_iommu_device = false,
5023a2f0c81SYong Zhao 	.supports_cwsr = true,
5036cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
5043a2f0c81SYong Zhao 	.num_sdma_engines = 4,
5053a2f0c81SYong Zhao 	.num_xgmi_sdma_engines = 0,
5063a2f0c81SYong Zhao 	.num_sdma_queues_per_engine = 8,
5073a2f0c81SYong Zhao };
5083a2f0c81SYong Zhao 
509de89b2e4SChengming Gui static const struct kfd_device_info navy_flounder_device_info = {
510de89b2e4SChengming Gui 	.asic_family = CHIP_NAVY_FLOUNDER,
511de89b2e4SChengming Gui 	.asic_name = "navy_flounder",
512de89b2e4SChengming Gui 	.max_pasid_bits = 16,
513de89b2e4SChengming Gui 	.max_no_of_hqd  = 24,
514de89b2e4SChengming Gui 	.doorbell_size  = 8,
515de89b2e4SChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
516de89b2e4SChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
517de89b2e4SChengming Gui 	.num_of_watch_points = 4,
518de89b2e4SChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
519de89b2e4SChengming Gui 	.needs_iommu_device = false,
520de89b2e4SChengming Gui 	.supports_cwsr = true,
5216cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
522de89b2e4SChengming Gui 	.num_sdma_engines = 2,
523de89b2e4SChengming Gui 	.num_xgmi_sdma_engines = 0,
524de89b2e4SChengming Gui 	.num_sdma_queues_per_engine = 8,
525de89b2e4SChengming Gui };
526de89b2e4SChengming Gui 
5273a5e715dSHuang Rui static const struct kfd_device_info vangogh_device_info = {
5283a5e715dSHuang Rui 	.asic_family = CHIP_VANGOGH,
5293a5e715dSHuang Rui 	.asic_name = "vangogh",
5303a5e715dSHuang Rui 	.max_pasid_bits = 16,
5313a5e715dSHuang Rui 	.max_no_of_hqd  = 24,
5323a5e715dSHuang Rui 	.doorbell_size  = 8,
5333a5e715dSHuang Rui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
5343a5e715dSHuang Rui 	.event_interrupt_class = &event_interrupt_class_v9,
5353a5e715dSHuang Rui 	.num_of_watch_points = 4,
5363a5e715dSHuang Rui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
5373a5e715dSHuang Rui 	.needs_iommu_device = false,
5383a5e715dSHuang Rui 	.supports_cwsr = true,
5393a5e715dSHuang Rui 	.needs_pci_atomics = false,
5403a5e715dSHuang Rui 	.num_sdma_engines = 1,
5413a5e715dSHuang Rui 	.num_xgmi_sdma_engines = 0,
5423a5e715dSHuang Rui 	.num_sdma_queues_per_engine = 2,
5433a5e715dSHuang Rui };
5443a5e715dSHuang Rui 
545eb5a34d4SChengming Gui static const struct kfd_device_info dimgrey_cavefish_device_info = {
546eb5a34d4SChengming Gui 	.asic_family = CHIP_DIMGREY_CAVEFISH,
547eb5a34d4SChengming Gui 	.asic_name = "dimgrey_cavefish",
548eb5a34d4SChengming Gui 	.max_pasid_bits = 16,
549eb5a34d4SChengming Gui 	.max_no_of_hqd  = 24,
550eb5a34d4SChengming Gui 	.doorbell_size  = 8,
551eb5a34d4SChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
552eb5a34d4SChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
553eb5a34d4SChengming Gui 	.num_of_watch_points = 4,
554eb5a34d4SChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
555eb5a34d4SChengming Gui 	.needs_iommu_device = false,
556eb5a34d4SChengming Gui 	.supports_cwsr = true,
5576cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
558eb5a34d4SChengming Gui 	.num_sdma_engines = 2,
559eb5a34d4SChengming Gui 	.num_xgmi_sdma_engines = 0,
560eb5a34d4SChengming Gui 	.num_sdma_queues_per_engine = 8,
561eb5a34d4SChengming Gui };
562eb5a34d4SChengming Gui 
5635cf607ccSChengming Gui static const struct kfd_device_info beige_goby_device_info = {
5645cf607ccSChengming Gui 	.asic_family = CHIP_BEIGE_GOBY,
5655cf607ccSChengming Gui 	.asic_name = "beige_goby",
5665cf607ccSChengming Gui 	.max_pasid_bits = 16,
5675cf607ccSChengming Gui 	.max_no_of_hqd  = 24,
5685cf607ccSChengming Gui 	.doorbell_size  = 8,
5695cf607ccSChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
5705cf607ccSChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
5715cf607ccSChengming Gui 	.num_of_watch_points = 4,
5725cf607ccSChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
5735cf607ccSChengming Gui 	.needs_iommu_device = false,
5745cf607ccSChengming Gui 	.supports_cwsr = true,
5755cf607ccSChengming Gui 	.needs_pci_atomics = true,
5765cf607ccSChengming Gui 	.num_sdma_engines = 1,
5775cf607ccSChengming Gui 	.num_xgmi_sdma_engines = 0,
5785cf607ccSChengming Gui 	.num_sdma_queues_per_engine = 8,
5795cf607ccSChengming Gui };
5805cf607ccSChengming Gui 
581bf9d4e88SAaron Liu static const struct kfd_device_info yellow_carp_device_info = {
582bf9d4e88SAaron Liu 	.asic_family = CHIP_YELLOW_CARP,
583bf9d4e88SAaron Liu 	.asic_name = "yellow_carp",
584bf9d4e88SAaron Liu 	.max_pasid_bits = 16,
585bf9d4e88SAaron Liu 	.max_no_of_hqd  = 24,
586bf9d4e88SAaron Liu 	.doorbell_size  = 8,
587bf9d4e88SAaron Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
588bf9d4e88SAaron Liu 	.event_interrupt_class = &event_interrupt_class_v9,
589bf9d4e88SAaron Liu 	.num_of_watch_points = 4,
590bf9d4e88SAaron Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
591bf9d4e88SAaron Liu 	.needs_iommu_device = false,
592bf9d4e88SAaron Liu 	.supports_cwsr = true,
593bf9d4e88SAaron Liu 	.needs_pci_atomics = false,
594bf9d4e88SAaron Liu 	.num_sdma_engines = 1,
595bf9d4e88SAaron Liu 	.num_xgmi_sdma_engines = 0,
596bf9d4e88SAaron Liu 	.num_sdma_queues_per_engine = 2,
597bf9d4e88SAaron Liu };
598eb5a34d4SChengming Gui 
599050091abSYong Zhao /* For each entry, [0] is regular and [1] is virtualisation device. */
600050091abSYong Zhao static const struct kfd_device_info *kfd_supported_devices[][2] = {
60195a5bd1bSYong Zhao #ifdef KFD_SUPPORT_IOMMU_V2
602050091abSYong Zhao 	[CHIP_KAVERI] = {&kaveri_device_info, NULL},
60395a5bd1bSYong Zhao 	[CHIP_CARRIZO] = {&carrizo_device_info, NULL},
60495a5bd1bSYong Zhao #endif
6052b3bbf23SYueHaibing 	[CHIP_RAVEN] = {&raven_device_info, NULL},
606050091abSYong Zhao 	[CHIP_HAWAII] = {&hawaii_device_info, NULL},
607050091abSYong Zhao 	[CHIP_TONGA] = {&tonga_device_info, NULL},
608050091abSYong Zhao 	[CHIP_FIJI] = {&fiji_device_info, &fiji_vf_device_info},
609050091abSYong Zhao 	[CHIP_POLARIS10] = {&polaris10_device_info, &polaris10_vf_device_info},
610050091abSYong Zhao 	[CHIP_POLARIS11] = {&polaris11_device_info, NULL},
611050091abSYong Zhao 	[CHIP_POLARIS12] = {&polaris12_device_info, NULL},
612050091abSYong Zhao 	[CHIP_VEGAM] = {&vegam_device_info, NULL},
613050091abSYong Zhao 	[CHIP_VEGA10] = {&vega10_device_info, &vega10_vf_device_info},
614050091abSYong Zhao 	[CHIP_VEGA12] = {&vega12_device_info, NULL},
615050091abSYong Zhao 	[CHIP_VEGA20] = {&vega20_device_info, NULL},
6162b9c2211SHuang Rui 	[CHIP_RENOIR] = {&renoir_device_info, NULL},
617050091abSYong Zhao 	[CHIP_ARCTURUS] = {&arcturus_device_info, &arcturus_device_info},
618cecd91b4SZhigang Luo 	[CHIP_ALDEBARAN] = {&aldebaran_device_info, &aldebaran_device_info},
619050091abSYong Zhao 	[CHIP_NAVI10] = {&navi10_device_info, NULL},
620b77fb9d8Sshaoyunl 	[CHIP_NAVI12] = {&navi12_device_info, &navi12_device_info},
6218099ae40SYong Zhao 	[CHIP_NAVI14] = {&navi14_device_info, NULL},
622adab4dadSshaoyunl 	[CHIP_SIENNA_CICHLID] = {&sienna_cichlid_device_info, &sienna_cichlid_device_info},
623de89b2e4SChengming Gui 	[CHIP_NAVY_FLOUNDER] = {&navy_flounder_device_info, &navy_flounder_device_info},
6243a5e715dSHuang Rui 	[CHIP_VANGOGH] = {&vangogh_device_info, NULL},
625eb5a34d4SChengming Gui 	[CHIP_DIMGREY_CAVEFISH] = {&dimgrey_cavefish_device_info, &dimgrey_cavefish_device_info},
6265cf607ccSChengming Gui 	[CHIP_BEIGE_GOBY] = {&beige_goby_device_info, &beige_goby_device_info},
627bf9d4e88SAaron Liu 	[CHIP_YELLOW_CARP] = {&yellow_carp_device_info, NULL},
6284a488a7aSOded Gabbay };
6294a488a7aSOded Gabbay 
6306e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
6316e81090bSOded Gabbay 				unsigned int chunk_size);
6326e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
6336e81090bSOded Gabbay 
634b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
635b8935a7cSYong Zhao 
636cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
637e392c887SYong Zhao 	struct pci_dev *pdev, unsigned int asic_type, bool vf)
6384a488a7aSOded Gabbay {
6394a488a7aSOded Gabbay 	struct kfd_dev *kfd;
640050091abSYong Zhao 	const struct kfd_device_info *device_info;
641e392c887SYong Zhao 	const struct kfd2kgd_calls *f2g;
642050091abSYong Zhao 
643e392c887SYong Zhao 	if (asic_type >= sizeof(kfd_supported_devices) / (sizeof(void *) * 2)
644e392c887SYong Zhao 		|| asic_type >= sizeof(kfd2kgd_funcs) / sizeof(void *)) {
645050091abSYong Zhao 		dev_err(kfd_device, "asic_type %d out of range\n", asic_type);
646050091abSYong Zhao 		return NULL; /* asic_type out of range */
647050091abSYong Zhao 	}
648050091abSYong Zhao 
649050091abSYong Zhao 	device_info = kfd_supported_devices[asic_type][vf];
650e392c887SYong Zhao 	f2g = kfd2kgd_funcs[asic_type];
6514a488a7aSOded Gabbay 
652aa5e899dSDan Carpenter 	if (!device_info || !f2g) {
653050091abSYong Zhao 		dev_err(kfd_device, "%s %s not supported in kfd\n",
654050091abSYong Zhao 			amdgpu_asic_name[asic_type], vf ? "VF" : "");
6554a488a7aSOded Gabbay 		return NULL;
6564ebc7182SYong Zhao 	}
6574a488a7aSOded Gabbay 
658d35f00d8SEric Huang 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
659d35f00d8SEric Huang 	if (!kfd)
660d35f00d8SEric Huang 		return NULL;
661d35f00d8SEric Huang 
6626106dce9Swelu 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
6636106dce9Swelu 	 * 32 and 64-bit requests are possible and must be
6646106dce9Swelu 	 * supported.
6653ee2d00cSFelix Kuehling 	 */
666aabf3a95SJack Xiao 	kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kgd);
667aabf3a95SJack Xiao 	if (device_info->needs_pci_atomics &&
668aabf3a95SJack Xiao 	    !kfd->pci_atomic_requested) {
6693ee2d00cSFelix Kuehling 		dev_info(kfd_device,
6706106dce9Swelu 			 "skipped device %x:%x, PCI rejects atomics\n",
6713ee2d00cSFelix Kuehling 			 pdev->vendor, pdev->device);
672d35f00d8SEric Huang 		kfree(kfd);
6733ee2d00cSFelix Kuehling 		return NULL;
674aabf3a95SJack Xiao 	}
6754a488a7aSOded Gabbay 
6764a488a7aSOded Gabbay 	kfd->kgd = kgd;
6774a488a7aSOded Gabbay 	kfd->device_info = device_info;
6784a488a7aSOded Gabbay 	kfd->pdev = pdev;
67919f6d2a6SOded Gabbay 	kfd->init_complete = false;
680cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
68143d8107fSHarish Kasiviswanathan 	atomic_set(&kfd->compute_profile, 0);
682cea405b1SXihan Zhang 
683cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
684cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
685cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
6864a488a7aSOded Gabbay 
6879b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
6889b54d201SEric Huang 
68959d7115dSMukul Joshi 	ida_init(&kfd->doorbell_ida);
69059d7115dSMukul Joshi 
6914a488a7aSOded Gabbay 	return kfd;
6924a488a7aSOded Gabbay }
6934a488a7aSOded Gabbay 
694373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
695373d7080SFelix Kuehling {
696373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
6973e76c239SFelix Kuehling 		if (kfd->device_info->asic_family < CHIP_VEGA10) {
698373d7080SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
699373d7080SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
700373d7080SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
7010ef6845cSJay Cornwall 		} else if (kfd->device_info->asic_family == CHIP_ARCTURUS) {
7023baa24f0SOak Zeng 			BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
7033baa24f0SOak Zeng 			kfd->cwsr_isa = cwsr_trap_arcturus_hex;
7043baa24f0SOak Zeng 			kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
7050ef6845cSJay Cornwall 		} else if (kfd->device_info->asic_family == CHIP_ALDEBARAN) {
7060ef6845cSJay Cornwall 			BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex) > PAGE_SIZE);
7070ef6845cSJay Cornwall 			kfd->cwsr_isa = cwsr_trap_aldebaran_hex;
7080ef6845cSJay Cornwall 			kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex);
70914328aa5SPhilip Cox 		} else if (kfd->device_info->asic_family < CHIP_NAVI10) {
7103e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
7113e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
7123e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
71380b6cfedSJay Cornwall 		} else if (kfd->device_info->asic_family < CHIP_SIENNA_CICHLID) {
71480b6cfedSJay Cornwall 			BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE);
71580b6cfedSJay Cornwall 			kfd->cwsr_isa = cwsr_trap_nv1x_hex;
71680b6cfedSJay Cornwall 			kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);
71714328aa5SPhilip Cox 		} else {
71814328aa5SPhilip Cox 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
71914328aa5SPhilip Cox 			kfd->cwsr_isa = cwsr_trap_gfx10_hex;
72014328aa5SPhilip Cox 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
7213e76c239SFelix Kuehling 		}
7223e76c239SFelix Kuehling 
723373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
724373d7080SFelix Kuehling 	}
725373d7080SFelix Kuehling }
726373d7080SFelix Kuehling 
72729633d0eSJoseph Greathouse static int kfd_gws_init(struct kfd_dev *kfd)
72829633d0eSJoseph Greathouse {
72929633d0eSJoseph Greathouse 	int ret = 0;
73029633d0eSJoseph Greathouse 
73129633d0eSJoseph Greathouse 	if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
73229633d0eSJoseph Greathouse 		return 0;
73329633d0eSJoseph Greathouse 
73429633d0eSJoseph Greathouse 	if (hws_gws_support
735fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family == CHIP_VEGA10
736fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x81b3)
737fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family >= CHIP_VEGA12
73829633d0eSJoseph Greathouse 			&& kfd->device_info->asic_family <= CHIP_RAVEN
739fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x1b3)
740fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family == CHIP_ARCTURUS
7418baa6018SHarish Kasiviswanathan 			&& kfd->mec2_fw_version >= 0x30)
7428baa6018SHarish Kasiviswanathan 		|| (kfd->device_info->asic_family == CHIP_ALDEBARAN
7438baa6018SHarish Kasiviswanathan 			&& kfd->mec2_fw_version >= 0x28))
74429633d0eSJoseph Greathouse 		ret = amdgpu_amdkfd_alloc_gws(kfd->kgd,
74529633d0eSJoseph Greathouse 				amdgpu_amdkfd_get_num_gws(kfd->kgd), &kfd->gws);
74629633d0eSJoseph Greathouse 
74729633d0eSJoseph Greathouse 	return ret;
74829633d0eSJoseph Greathouse }
74929633d0eSJoseph Greathouse 
750938a0650SAmber Lin static void kfd_smi_init(struct kfd_dev *dev) {
751938a0650SAmber Lin 	INIT_LIST_HEAD(&dev->smi_clients);
752938a0650SAmber Lin 	spin_lock_init(&dev->smi_lock);
753938a0650SAmber Lin }
754938a0650SAmber Lin 
7554a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
7563a0c3423SHarish Kasiviswanathan 			 struct drm_device *ddev,
7574a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
7584a488a7aSOded Gabbay {
759fd6a440eSJonathan Kim 	unsigned int size, map_process_packet_size;
76019f6d2a6SOded Gabbay 
7613a0c3423SHarish Kasiviswanathan 	kfd->ddev = ddev;
7620da8b10eSAmber Lin 	kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
7635ade6c9cSFelix Kuehling 			KGD_ENGINE_MEC1);
76429633d0eSJoseph Greathouse 	kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
76529633d0eSJoseph Greathouse 			KGD_ENGINE_MEC2);
7660da8b10eSAmber Lin 	kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
7675ade6c9cSFelix Kuehling 			KGD_ENGINE_SDMA1);
7684a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
7694a488a7aSOded Gabbay 
77044008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
77144008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
77244008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
77344008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
77444008d7aSYong Zhao 
775a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
776a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
777a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
778a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
779a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
780a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
781a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
782a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
783a99c6d4fSFelix Kuehling 	} else
784a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
785a99c6d4fSFelix Kuehling 
78619f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
787b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
78819f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
78919f6d2a6SOded Gabbay 
790e18e794eSOded Gabbay 	/*
791e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
792e18e794eSOded Gabbay 	 * There can be only 2 packets at once
793e18e794eSOded Gabbay 	 */
794fd6a440eSJonathan Kim 	map_process_packet_size =
795fd6a440eSJonathan Kim 			kfd->device_info->asic_family == CHIP_ALDEBARAN ?
796fd6a440eSJonathan Kim 				sizeof(struct pm4_mes_map_process_aldebaran) :
797fd6a440eSJonathan Kim 					sizeof(struct pm4_mes_map_process);
798fd6a440eSJonathan Kim 	size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size +
799507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
800507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
801e18e794eSOded Gabbay 
802e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
803e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
804e18e794eSOded Gabbay 
805e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
80619f6d2a6SOded Gabbay 	size += 512 * 1024;
80719f6d2a6SOded Gabbay 
8087cd52c91SAmber Lin 	if (amdgpu_amdkfd_alloc_gtt_mem(
809cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
81015426dbbSYong Zhao 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
81115426dbbSYong Zhao 			false)) {
81279775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
813e09d4fc8SOak Zeng 		goto alloc_gtt_mem_failure;
81419f6d2a6SOded Gabbay 	}
81519f6d2a6SOded Gabbay 
81679775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
817e18e794eSOded Gabbay 
81873a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
81973a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
82079775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
82173a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
82273a1da0bSOded Gabbay 	}
82373a1da0bSOded Gabbay 
824735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
825735df2baSFelix Kuehling 		dev_err(kfd_device,
826735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
827735df2baSFelix Kuehling 		goto kfd_doorbell_error;
828735df2baSFelix Kuehling 	}
82919f6d2a6SOded Gabbay 
830332f6e1eSFelix Kuehling 	kfd->hive_id = amdgpu_amdkfd_get_hive_id(kfd->kgd);
8310c1690e3SShaoyun Liu 
8329b498efaSAlex Deucher 	kfd->noretry = amdgpu_amdkfd_get_noretry(kfd->kgd);
8339b498efaSAlex Deucher 
8342249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
83579775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
8362249d558SAndrew Lewycky 		goto kfd_interrupt_error;
8372249d558SAndrew Lewycky 	}
8382249d558SAndrew Lewycky 
83964c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
84064c7f8cfSBen Goz 	if (!kfd->dqm) {
84179775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
84264c7f8cfSBen Goz 		goto device_queue_manager_error;
84364c7f8cfSBen Goz 	}
84464c7f8cfSBen Goz 
84529633d0eSJoseph Greathouse 	/* If supported on this device, allocate global GWS that is shared
84629633d0eSJoseph Greathouse 	 * by all KFD processes
84729633d0eSJoseph Greathouse 	 */
84829633d0eSJoseph Greathouse 	if (kfd_gws_init(kfd)) {
84929633d0eSJoseph Greathouse 		dev_err(kfd_device, "Could not allocate %d gws\n",
85029633d0eSJoseph Greathouse 			amdgpu_amdkfd_get_num_gws(kfd->kgd));
85129633d0eSJoseph Greathouse 		goto gws_error;
85229633d0eSJoseph Greathouse 	}
85329633d0eSJoseph Greathouse 
8546127896fSHuang Rui 	/* If CRAT is broken, won't set iommu enabled */
8556127896fSHuang Rui 	kfd_double_confirm_iommu_support(kfd);
8566127896fSHuang Rui 
85764d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
85864d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
85964d1c3a4SFelix Kuehling 		goto device_iommu_error;
86064c7f8cfSBen Goz 	}
86164c7f8cfSBen Goz 
862373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
863373d7080SFelix Kuehling 
864814ab993SPhilip Yang 	svm_migrate_init((struct amdgpu_device *)kfd->kgd);
865814ab993SPhilip Yang 
866b8935a7cSYong Zhao 	if (kfd_resume(kfd))
867b8935a7cSYong Zhao 		goto kfd_resume_error;
868b8935a7cSYong Zhao 
869fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
870fbeb661bSYair Shachar 
871465ab9e0SOak Zeng 	if (kfd_topology_add_device(kfd)) {
872465ab9e0SOak Zeng 		dev_err(kfd_device, "Error adding device to topology\n");
873465ab9e0SOak Zeng 		goto kfd_topology_add_device_error;
874465ab9e0SOak Zeng 	}
875465ab9e0SOak Zeng 
876938a0650SAmber Lin 	kfd_smi_init(kfd);
877938a0650SAmber Lin 
8784a488a7aSOded Gabbay 	kfd->init_complete = true;
87979775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
8804a488a7aSOded Gabbay 		 kfd->pdev->device);
8814a488a7aSOded Gabbay 
88279775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
883d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
88464c7f8cfSBen Goz 
88519f6d2a6SOded Gabbay 	goto out;
88619f6d2a6SOded Gabbay 
887465ab9e0SOak Zeng kfd_topology_add_device_error:
888b8935a7cSYong Zhao kfd_resume_error:
88964d1c3a4SFelix Kuehling device_iommu_error:
89029633d0eSJoseph Greathouse gws_error:
89164c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
89264c7f8cfSBen Goz device_queue_manager_error:
8932249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
8942249d558SAndrew Lewycky kfd_interrupt_error:
895735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
896735df2baSFelix Kuehling kfd_doorbell_error:
89773a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
89873a1da0bSOded Gabbay kfd_gtt_sa_init_error:
8997cd52c91SAmber Lin 	amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
900e09d4fc8SOak Zeng alloc_gtt_mem_failure:
90129633d0eSJoseph Greathouse 	if (kfd->gws)
902e09d4fc8SOak Zeng 		amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
90319f6d2a6SOded Gabbay 	dev_err(kfd_device,
90479775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
90519f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
90619f6d2a6SOded Gabbay out:
90719f6d2a6SOded Gabbay 	return kfd->init_complete;
9084a488a7aSOded Gabbay }
9094a488a7aSOded Gabbay 
9104a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
9114a488a7aSOded Gabbay {
912b17f068aSOded Gabbay 	if (kfd->init_complete) {
913814ab993SPhilip Yang 		svm_migrate_fini((struct amdgpu_device *)kfd->kgd);
91464c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
9152249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
91619f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
917735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
91859d7115dSMukul Joshi 		ida_destroy(&kfd->doorbell_ida);
91973a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
9207cd52c91SAmber Lin 		amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
92129633d0eSJoseph Greathouse 		if (kfd->gws)
922e09d4fc8SOak Zeng 			amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
923b17f068aSOded Gabbay 	}
9245b5c4e40SEvgeny Pinchuk 
9254a488a7aSOded Gabbay 	kfree(kfd);
9264a488a7aSOded Gabbay }
9274a488a7aSOded Gabbay 
928e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
929e3b7a967SShaoyun Liu {
930e42051d2SShaoyun Liu 	if (!kfd->init_complete)
931e42051d2SShaoyun Liu 		return 0;
93209c34e8dSFelix Kuehling 
93355977744SMukul Joshi 	kfd_smi_event_update_gpu_reset(kfd, false);
93455977744SMukul Joshi 
93509c34e8dSFelix Kuehling 	kfd->dqm->ops.pre_reset(kfd->dqm);
93609c34e8dSFelix Kuehling 
9379593f4d6SRajneesh Bhardwaj 	kgd2kfd_suspend(kfd, false);
938e42051d2SShaoyun Liu 
939e42051d2SShaoyun Liu 	kfd_signal_reset_event(kfd);
940e3b7a967SShaoyun Liu 	return 0;
941e3b7a967SShaoyun Liu }
942e3b7a967SShaoyun Liu 
943e42051d2SShaoyun Liu /*
944e42051d2SShaoyun Liu  * Fix me. KFD won't be able to resume existing process for now.
945e42051d2SShaoyun Liu  * We will keep all existing process in a evicted state and
946e42051d2SShaoyun Liu  * wait the process to be terminated.
947e42051d2SShaoyun Liu  */
948e42051d2SShaoyun Liu 
949e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
950e3b7a967SShaoyun Liu {
951a1bd079fSyu kuai 	int ret;
952e42051d2SShaoyun Liu 
953e42051d2SShaoyun Liu 	if (!kfd->init_complete)
954e3b7a967SShaoyun Liu 		return 0;
955e42051d2SShaoyun Liu 
956e42051d2SShaoyun Liu 	ret = kfd_resume(kfd);
957e42051d2SShaoyun Liu 	if (ret)
958e42051d2SShaoyun Liu 		return ret;
959a1bd079fSyu kuai 	atomic_dec(&kfd_locked);
9609b54d201SEric Huang 
9619b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
9629b54d201SEric Huang 
96355977744SMukul Joshi 	kfd_smi_event_update_gpu_reset(kfd, true);
96455977744SMukul Joshi 
965e42051d2SShaoyun Liu 	return 0;
966e42051d2SShaoyun Liu }
967e42051d2SShaoyun Liu 
968e42051d2SShaoyun Liu bool kfd_is_locked(void)
969e42051d2SShaoyun Liu {
970e42051d2SShaoyun Liu 	return  (atomic_read(&kfd_locked) > 0);
971e3b7a967SShaoyun Liu }
972e3b7a967SShaoyun Liu 
9739593f4d6SRajneesh Bhardwaj void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm)
9744a488a7aSOded Gabbay {
975733fa1f7SYong Zhao 	if (!kfd->init_complete)
976733fa1f7SYong Zhao 		return;
977733fa1f7SYong Zhao 
9789593f4d6SRajneesh Bhardwaj 	/* for runtime suspend, skip locking kfd */
9799593f4d6SRajneesh Bhardwaj 	if (!run_pm) {
98026103436SFelix Kuehling 		/* For first KFD device suspend all the KFD processes */
981e42051d2SShaoyun Liu 		if (atomic_inc_return(&kfd_locked) == 1)
98226103436SFelix Kuehling 			kfd_suspend_all_processes();
9839593f4d6SRajneesh Bhardwaj 	}
98426103436SFelix Kuehling 
98545c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
98664d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
9874a488a7aSOded Gabbay }
9884a488a7aSOded Gabbay 
9899593f4d6SRajneesh Bhardwaj int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm)
9904a488a7aSOded Gabbay {
99126103436SFelix Kuehling 	int ret, count;
99226103436SFelix Kuehling 
993b8935a7cSYong Zhao 	if (!kfd->init_complete)
994b8935a7cSYong Zhao 		return 0;
995b17f068aSOded Gabbay 
99626103436SFelix Kuehling 	ret = kfd_resume(kfd);
99726103436SFelix Kuehling 	if (ret)
99826103436SFelix Kuehling 		return ret;
999b17f068aSOded Gabbay 
10009593f4d6SRajneesh Bhardwaj 	/* for runtime resume, skip unlocking kfd */
10019593f4d6SRajneesh Bhardwaj 	if (!run_pm) {
1002e42051d2SShaoyun Liu 		count = atomic_dec_return(&kfd_locked);
100326103436SFelix Kuehling 		WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
100426103436SFelix Kuehling 		if (count == 0)
100526103436SFelix Kuehling 			ret = kfd_resume_all_processes();
10069593f4d6SRajneesh Bhardwaj 	}
100726103436SFelix Kuehling 
100826103436SFelix Kuehling 	return ret;
10094ebc7182SYong Zhao }
10104ebc7182SYong Zhao 
1011b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
1012b8935a7cSYong Zhao {
1013b8935a7cSYong Zhao 	int err = 0;
1014b8935a7cSYong Zhao 
101564d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
101664d1c3a4SFelix Kuehling 	if (err) {
101764d1c3a4SFelix Kuehling 		dev_err(kfd_device,
101864d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
101964d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
102064d1c3a4SFelix Kuehling 		return err;
102164d1c3a4SFelix Kuehling 	}
1022733fa1f7SYong Zhao 
1023b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
1024b8935a7cSYong Zhao 	if (err) {
1025b8935a7cSYong Zhao 		dev_err(kfd_device,
1026b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
1027b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
1028b8935a7cSYong Zhao 		goto dqm_start_error;
1029b17f068aSOded Gabbay 	}
1030b17f068aSOded Gabbay 
1031b8935a7cSYong Zhao 	return err;
1032b8935a7cSYong Zhao 
1033b8935a7cSYong Zhao dqm_start_error:
103464d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
1035b8935a7cSYong Zhao 	return err;
10364a488a7aSOded Gabbay }
10374a488a7aSOded Gabbay 
1038b3eca59dSPhilip Yang static inline void kfd_queue_work(struct workqueue_struct *wq,
1039b3eca59dSPhilip Yang 				  struct work_struct *work)
1040b3eca59dSPhilip Yang {
1041b3eca59dSPhilip Yang 	int cpu, new_cpu;
1042b3eca59dSPhilip Yang 
1043b3eca59dSPhilip Yang 	cpu = new_cpu = smp_processor_id();
1044b3eca59dSPhilip Yang 	do {
1045b3eca59dSPhilip Yang 		new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
1046b3eca59dSPhilip Yang 		if (cpu_to_node(new_cpu) == numa_node_id())
1047b3eca59dSPhilip Yang 			break;
1048b3eca59dSPhilip Yang 	} while (cpu != new_cpu);
1049b3eca59dSPhilip Yang 
1050b3eca59dSPhilip Yang 	queue_work_on(new_cpu, wq, work);
1051b3eca59dSPhilip Yang }
1052b3eca59dSPhilip Yang 
1053b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
1054b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
10554a488a7aSOded Gabbay {
105658e69886SLan Xiao 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
105758e69886SLan Xiao 	bool is_patched = false;
10582383a767SChristian König 	unsigned long flags;
105958e69886SLan Xiao 
10602249d558SAndrew Lewycky 	if (!kfd->init_complete)
10612249d558SAndrew Lewycky 		return;
10622249d558SAndrew Lewycky 
106358e69886SLan Xiao 	if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
106458e69886SLan Xiao 		dev_err_once(kfd_device, "Ring entry too small\n");
106558e69886SLan Xiao 		return;
106658e69886SLan Xiao 	}
106758e69886SLan Xiao 
10682383a767SChristian König 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
10692249d558SAndrew Lewycky 
10702249d558SAndrew Lewycky 	if (kfd->interrupts_active
107158e69886SLan Xiao 	    && interrupt_is_wanted(kfd, ih_ring_entry,
107258e69886SLan Xiao 				   patched_ihre, &is_patched)
107358e69886SLan Xiao 	    && enqueue_ih_ring_entry(kfd,
107458e69886SLan Xiao 				     is_patched ? patched_ihre : ih_ring_entry))
1075b3eca59dSPhilip Yang 		kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work);
10762249d558SAndrew Lewycky 
10772383a767SChristian König 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
10784a488a7aSOded Gabbay }
10796e81090bSOded Gabbay 
10806b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
10816b95e797SFelix Kuehling {
10826b95e797SFelix Kuehling 	struct kfd_process *p;
10836b95e797SFelix Kuehling 	int r;
10846b95e797SFelix Kuehling 
10856b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
10866b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
10876b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
10886b95e797SFelix Kuehling 	 */
10896b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
10906b95e797SFelix Kuehling 	if (!p)
10916b95e797SFelix Kuehling 		return -ESRCH;
10926b95e797SFelix Kuehling 
1093b2057956SFelix Kuehling 	WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
10946b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
10956b95e797SFelix Kuehling 
10966b95e797SFelix Kuehling 	kfd_unref_process(p);
10976b95e797SFelix Kuehling 	return r;
10986b95e797SFelix Kuehling }
10996b95e797SFelix Kuehling 
11006b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
11016b95e797SFelix Kuehling {
11026b95e797SFelix Kuehling 	struct kfd_process *p;
11036b95e797SFelix Kuehling 	int r;
11046b95e797SFelix Kuehling 
11056b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
11066b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
11076b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
11086b95e797SFelix Kuehling 	 */
11096b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
11106b95e797SFelix Kuehling 	if (!p)
11116b95e797SFelix Kuehling 		return -ESRCH;
11126b95e797SFelix Kuehling 
11136b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
11146b95e797SFelix Kuehling 
11156b95e797SFelix Kuehling 	kfd_unref_process(p);
11166b95e797SFelix Kuehling 	return r;
11176b95e797SFelix Kuehling }
11186b95e797SFelix Kuehling 
111926103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
112026103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
112126103436SFelix Kuehling  *   process.
112226103436SFelix Kuehling  *
112326103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
112426103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
112526103436SFelix Kuehling  *
112626103436SFelix Kuehling  */
112726103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
112826103436SFelix Kuehling 					       struct dma_fence *fence)
112926103436SFelix Kuehling {
113026103436SFelix Kuehling 	struct kfd_process *p;
113126103436SFelix Kuehling 	unsigned long active_time;
113226103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
113326103436SFelix Kuehling 
113426103436SFelix Kuehling 	if (!fence)
113526103436SFelix Kuehling 		return -EINVAL;
113626103436SFelix Kuehling 
113726103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
113826103436SFelix Kuehling 		return 0;
113926103436SFelix Kuehling 
114026103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
114126103436SFelix Kuehling 	if (!p)
114226103436SFelix Kuehling 		return -ENODEV;
114326103436SFelix Kuehling 
114426103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
114526103436SFelix Kuehling 		goto out;
114626103436SFelix Kuehling 
114726103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
114826103436SFelix Kuehling 
114926103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
115026103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
115126103436SFelix Kuehling 	 */
115226103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
115326103436SFelix Kuehling 	if (delay_jiffies > active_time)
115426103436SFelix Kuehling 		delay_jiffies -= active_time;
115526103436SFelix Kuehling 	else
115626103436SFelix Kuehling 		delay_jiffies = 0;
115726103436SFelix Kuehling 
115826103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
115926103436SFelix Kuehling 	 * to kfd_evict_bo_worker
116026103436SFelix Kuehling 	 */
1161b2057956SFelix Kuehling 	WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",
1162b2057956SFelix Kuehling 	     p->lead_thread->pid, delay_jiffies);
116326103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
116426103436SFelix Kuehling out:
116526103436SFelix Kuehling 	kfd_unref_process(p);
116626103436SFelix Kuehling 	return 0;
116726103436SFelix Kuehling }
116826103436SFelix Kuehling 
11696e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
11706e81090bSOded Gabbay 				unsigned int chunk_size)
11716e81090bSOded Gabbay {
11728625ff9cSFelix Kuehling 	unsigned int num_of_longs;
11736e81090bSOded Gabbay 
117432fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
117532fa8219SFelix Kuehling 		return -EINVAL;
117632fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
117732fa8219SFelix Kuehling 		return -EINVAL;
117832fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
117932fa8219SFelix Kuehling 		return -EINVAL;
11806e81090bSOded Gabbay 
11816e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
11826e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
11836e81090bSOded Gabbay 
11848625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
11858625ff9cSFelix Kuehling 		BITS_PER_LONG;
11866e81090bSOded Gabbay 
11878625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
11886e81090bSOded Gabbay 
11896e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
11906e81090bSOded Gabbay 		return -ENOMEM;
11916e81090bSOded Gabbay 
119279775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
11936e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
11946e81090bSOded Gabbay 
11956e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
11966e81090bSOded Gabbay 
11976e81090bSOded Gabbay 	return 0;
11986e81090bSOded Gabbay 
11996e81090bSOded Gabbay }
12006e81090bSOded Gabbay 
12016e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
12026e81090bSOded Gabbay {
12036e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
12046e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
12056e81090bSOded Gabbay }
12066e81090bSOded Gabbay 
12076e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
12086e81090bSOded Gabbay 						unsigned int bit_num,
12096e81090bSOded Gabbay 						unsigned int chunk_size)
12106e81090bSOded Gabbay {
12116e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
12126e81090bSOded Gabbay }
12136e81090bSOded Gabbay 
12146e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
12156e81090bSOded Gabbay 						unsigned int bit_num,
12166e81090bSOded Gabbay 						unsigned int chunk_size)
12176e81090bSOded Gabbay {
12186e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
12196e81090bSOded Gabbay }
12206e81090bSOded Gabbay 
12216e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
12226e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
12236e81090bSOded Gabbay {
12246e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
12256e81090bSOded Gabbay 
12266e81090bSOded Gabbay 	if (size == 0)
12276e81090bSOded Gabbay 		return -EINVAL;
12286e81090bSOded Gabbay 
12296e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
12306e81090bSOded Gabbay 		return -ENOMEM;
12316e81090bSOded Gabbay 
12321cd106ecSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
12331cd106ecSFelix Kuehling 	if (!(*mem_obj))
12346e81090bSOded Gabbay 		return -ENOMEM;
12356e81090bSOded Gabbay 
123679775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
12376e81090bSOded Gabbay 
12386e81090bSOded Gabbay 	start_search = 0;
12396e81090bSOded Gabbay 
12406e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
12416e81090bSOded Gabbay 
12426e81090bSOded Gabbay kfd_gtt_restart_search:
12436e81090bSOded Gabbay 	/* Find the first chunk that is free */
12446e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
12456e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
12466e81090bSOded Gabbay 					start_search);
12476e81090bSOded Gabbay 
124879775b62SKent Russell 	pr_debug("Found = %d\n", found);
12496e81090bSOded Gabbay 
12506e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
12516e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
12526e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
12536e81090bSOded Gabbay 
12546e81090bSOded Gabbay 	/* Update fields of mem_obj */
12556e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
12566e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
12576e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
12586e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
12596e81090bSOded Gabbay 					found,
12606e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
12616e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
12626e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
12636e81090bSOded Gabbay 					found,
12646e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
12656e81090bSOded Gabbay 
126679775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
12676e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
12686e81090bSOded Gabbay 
12696e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
12706e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
127179775b62SKent Russell 		pr_debug("Single bit\n");
12726e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
12736e81090bSOded Gabbay 		goto kfd_gtt_out;
12746e81090bSOded Gabbay 	}
12756e81090bSOded Gabbay 
12766e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
12776e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
12786e81090bSOded Gabbay 	do {
12796e81090bSOded Gabbay 		(*mem_obj)->range_end =
12806e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
12816e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
12826e81090bSOded Gabbay 		/*
12836e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
12846e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
12856e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
12866e81090bSOded Gabbay 		 */
12876e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
12886e81090bSOded Gabbay 			start_search = found;
12896e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
12906e81090bSOded Gabbay 		}
12916e81090bSOded Gabbay 
12926e81090bSOded Gabbay 		/*
12936e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
12946e81090bSOded Gabbay 		 */
12956e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
12966e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
12976e81090bSOded Gabbay 
12986e81090bSOded Gabbay 		/* Check if we don't need another chunk */
12996e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
13006e81090bSOded Gabbay 			cur_size = 0;
13016e81090bSOded Gabbay 		else
13026e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
13036e81090bSOded Gabbay 
13046e81090bSOded Gabbay 	} while (cur_size > 0);
13056e81090bSOded Gabbay 
130679775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
13076e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
13086e81090bSOded Gabbay 
13096e81090bSOded Gabbay 	/* Mark the chunks as allocated */
13106e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
13116e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
13126e81090bSOded Gabbay 		found++)
13136e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
13146e81090bSOded Gabbay 
13156e81090bSOded Gabbay kfd_gtt_out:
13166e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
13176e81090bSOded Gabbay 	return 0;
13186e81090bSOded Gabbay 
13196e81090bSOded Gabbay kfd_gtt_no_free_chunk:
13203148a6a0SJack Zhang 	pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);
13216e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
13223148a6a0SJack Zhang 	kfree(*mem_obj);
13236e81090bSOded Gabbay 	return -ENOMEM;
13246e81090bSOded Gabbay }
13256e81090bSOded Gabbay 
13266e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
13276e81090bSOded Gabbay {
13286e81090bSOded Gabbay 	unsigned int bit;
13296e81090bSOded Gabbay 
13309216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
13319216ed29SOded Gabbay 	if (!mem_obj)
13329216ed29SOded Gabbay 		return 0;
13336e81090bSOded Gabbay 
133479775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
13356e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
13366e81090bSOded Gabbay 
13376e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
13386e81090bSOded Gabbay 
13396e81090bSOded Gabbay 	/* Mark the chunks as free */
13406e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
13416e81090bSOded Gabbay 		bit <= mem_obj->range_end;
13426e81090bSOded Gabbay 		bit++)
13436e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
13446e81090bSOded Gabbay 
13456e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
13466e81090bSOded Gabbay 
13476e81090bSOded Gabbay 	kfree(mem_obj);
13486e81090bSOded Gabbay 	return 0;
13496e81090bSOded Gabbay }
1350a29ec470SShaoyun Liu 
13519b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
13529b54d201SEric Huang {
13539b54d201SEric Huang 	if (kfd)
13549b54d201SEric Huang 		atomic_inc(&kfd->sram_ecc_flag);
13559b54d201SEric Huang }
13569b54d201SEric Huang 
135743d8107fSHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd)
135843d8107fSHarish Kasiviswanathan {
135943d8107fSHarish Kasiviswanathan 	if (atomic_inc_return(&kfd->compute_profile) == 1)
136043d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, false);
136143d8107fSHarish Kasiviswanathan }
136243d8107fSHarish Kasiviswanathan 
136343d8107fSHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd)
136443d8107fSHarish Kasiviswanathan {
136543d8107fSHarish Kasiviswanathan 	int count = atomic_dec_return(&kfd->compute_profile);
136643d8107fSHarish Kasiviswanathan 
136743d8107fSHarish Kasiviswanathan 	if (count == 0)
136843d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, true);
136943d8107fSHarish Kasiviswanathan 	WARN_ONCE(count < 0, "Compute profile ref. count error");
137043d8107fSHarish Kasiviswanathan }
137143d8107fSHarish Kasiviswanathan 
13722c2b0d88SMukul Joshi void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint32_t throttle_bitmask)
13732c2b0d88SMukul Joshi {
1374158fc08dSAmber Lin 	if (kfd && kfd->init_complete)
13752c2b0d88SMukul Joshi 		kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask);
13762c2b0d88SMukul Joshi }
13772c2b0d88SMukul Joshi 
1378a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
1379a29ec470SShaoyun Liu 
1380a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
1381a29ec470SShaoyun Liu  * which will trigger a GPU reset and bring the HWS back to normal state
1382a29ec470SShaoyun Liu  */
1383a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1384a29ec470SShaoyun Liu {
1385a29ec470SShaoyun Liu 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1386a29ec470SShaoyun Liu 		pr_err("HWS is not enabled");
1387a29ec470SShaoyun Liu 		return -EINVAL;
1388a29ec470SShaoyun Liu 	}
1389a29ec470SShaoyun Liu 
1390*4f942aaeSOak Zeng 	return dqm_debugfs_hang_hws(dev->dqm);
1391a29ec470SShaoyun Liu }
1392a29ec470SShaoyun Liu 
1393a29ec470SShaoyun Liu #endif
1394