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,
8706e75b88STao Zhou 	[CHIP_CYAN_SKILLFISH] = &gfx_v10_kfd2kgd,
88e392c887SYong Zhao };
89e392c887SYong Zhao 
9064d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
914a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = {
920da7558cSBen Goz 	.asic_family = CHIP_KAVERI,
93c181159aSYong Zhao 	.asic_name = "kaveri",
94*9d6fa9c7SGraham Sider 	.gfx_target_version = 70000,
950da7558cSBen Goz 	.max_pasid_bits = 16,
96992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
97992839adSYair Shachar 	.max_no_of_hqd	= 24,
98ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
990da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
100f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
101fbeb661bSYair Shachar 	.num_of_watch_points = 4,
102373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
103373d7080SFelix Kuehling 	.supports_cwsr = false,
10464d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
1053ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
10698bb9222SYong Zhao 	.num_sdma_engines = 2,
1071b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
108d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1090da7558cSBen Goz };
1100da7558cSBen Goz 
1110da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
1120da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
113c181159aSYong Zhao 	.asic_name = "carrizo",
114*9d6fa9c7SGraham Sider 	.gfx_target_version = 80001,
1154a488a7aSOded Gabbay 	.max_pasid_bits = 16,
116eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
117eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
118ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
119b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
120eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
121f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
122373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
123373d7080SFelix Kuehling 	.supports_cwsr = true,
12464d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
1253ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
12698bb9222SYong Zhao 	.num_sdma_engines = 2,
1271b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
128d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1294a488a7aSOded Gabbay };
1306127896fSHuang Rui #endif
1314d663df6SYong Zhao 
1324d663df6SYong Zhao static const struct kfd_device_info raven_device_info = {
1334d663df6SYong Zhao 	.asic_family = CHIP_RAVEN,
134c181159aSYong Zhao 	.asic_name = "raven",
135*9d6fa9c7SGraham Sider 	.gfx_target_version = 90002,
1364d663df6SYong Zhao 	.max_pasid_bits = 16,
1374d663df6SYong Zhao 	.max_no_of_hqd  = 24,
1384d663df6SYong Zhao 	.doorbell_size  = 8,
1394d663df6SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
1404d663df6SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
1414d663df6SYong Zhao 	.num_of_watch_points = 4,
1424d663df6SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
1434d663df6SYong Zhao 	.supports_cwsr = true,
1444d663df6SYong Zhao 	.needs_iommu_device = true,
1454d663df6SYong Zhao 	.needs_pci_atomics = true,
1464d663df6SYong Zhao 	.num_sdma_engines = 1,
1471b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
148d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1494d663df6SYong Zhao };
1504a488a7aSOded Gabbay 
151a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
152a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
153c181159aSYong Zhao 	.asic_name = "hawaii",
154*9d6fa9c7SGraham Sider 	.gfx_target_version = 70001,
155a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
156a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
157a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
158ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
159a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
160a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
161a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
162a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
163a3084e6cSFelix Kuehling 	.supports_cwsr = false,
16464d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
165a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
16698bb9222SYong Zhao 	.num_sdma_engines = 2,
1671b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
168d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
169a3084e6cSFelix Kuehling };
170a3084e6cSFelix Kuehling 
171a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
172a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
173c181159aSYong Zhao 	.asic_name = "tonga",
174*9d6fa9c7SGraham Sider 	.gfx_target_version = 80002,
175a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
176a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
177ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
178a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
179a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
180a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
181a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
182a3084e6cSFelix Kuehling 	.supports_cwsr = false,
18364d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
184a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
18598bb9222SYong Zhao 	.num_sdma_engines = 2,
1861b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
187d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
188a3084e6cSFelix Kuehling };
189a3084e6cSFelix Kuehling 
190a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
191a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
192c181159aSYong Zhao 	.asic_name = "fiji",
193*9d6fa9c7SGraham Sider 	.gfx_target_version = 80003,
194a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
195a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
196ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
197a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
198a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
199a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
200a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
201a3084e6cSFelix Kuehling 	.supports_cwsr = true,
20264d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
203a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
20498bb9222SYong Zhao 	.num_sdma_engines = 2,
2051b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
206d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
207a3084e6cSFelix Kuehling };
208a3084e6cSFelix Kuehling 
209a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
210a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
211c181159aSYong Zhao 	.asic_name = "fiji",
212*9d6fa9c7SGraham Sider 	.gfx_target_version = 80003,
213a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
214a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
215ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
216a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
217a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
218a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
219a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
220a3084e6cSFelix Kuehling 	.supports_cwsr = true,
22164d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
222a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
22398bb9222SYong Zhao 	.num_sdma_engines = 2,
2241b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
225d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
226a3084e6cSFelix Kuehling };
227a3084e6cSFelix Kuehling 
228a3084e6cSFelix Kuehling 
229a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
230a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
231c181159aSYong Zhao 	.asic_name = "polaris10",
232*9d6fa9c7SGraham Sider 	.gfx_target_version = 80003,
233a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
234a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
235ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
236a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
237a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
238a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
239a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
240a3084e6cSFelix Kuehling 	.supports_cwsr = true,
24164d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
242a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
24398bb9222SYong Zhao 	.num_sdma_engines = 2,
2441b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
245d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
246a3084e6cSFelix Kuehling };
247a3084e6cSFelix Kuehling 
248a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = {
249a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
250c181159aSYong Zhao 	.asic_name = "polaris10",
251*9d6fa9c7SGraham Sider 	.gfx_target_version = 80003,
252a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
253a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
254ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
255a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
256a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
257a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
258a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
259a3084e6cSFelix Kuehling 	.supports_cwsr = true,
26064d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
261a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
26298bb9222SYong Zhao 	.num_sdma_engines = 2,
2631b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
264d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
265a3084e6cSFelix Kuehling };
266a3084e6cSFelix Kuehling 
267a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = {
268a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
269c181159aSYong Zhao 	.asic_name = "polaris11",
270*9d6fa9c7SGraham Sider 	.gfx_target_version = 80003,
271a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
272a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
273ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
274a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
275a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
276a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
277a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
278a3084e6cSFelix Kuehling 	.supports_cwsr = true,
27964d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
280a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
28198bb9222SYong Zhao 	.num_sdma_engines = 2,
2821b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
283d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
284a3084e6cSFelix Kuehling };
285a3084e6cSFelix Kuehling 
286846a44d7SGang Ba static const struct kfd_device_info polaris12_device_info = {
287846a44d7SGang Ba 	.asic_family = CHIP_POLARIS12,
288c181159aSYong Zhao 	.asic_name = "polaris12",
289*9d6fa9c7SGraham Sider 	.gfx_target_version = 80003,
290846a44d7SGang Ba 	.max_pasid_bits = 16,
291846a44d7SGang Ba 	.max_no_of_hqd  = 24,
292846a44d7SGang Ba 	.doorbell_size  = 4,
293846a44d7SGang Ba 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
294846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_cik,
295846a44d7SGang Ba 	.num_of_watch_points = 4,
296846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
297846a44d7SGang Ba 	.supports_cwsr = true,
298846a44d7SGang Ba 	.needs_iommu_device = false,
299846a44d7SGang Ba 	.needs_pci_atomics = true,
300846a44d7SGang Ba 	.num_sdma_engines = 2,
3011b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
302846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
303846a44d7SGang Ba };
304846a44d7SGang Ba 
305ed81cd6eSKent Russell static const struct kfd_device_info vegam_device_info = {
306ed81cd6eSKent Russell 	.asic_family = CHIP_VEGAM,
307c181159aSYong Zhao 	.asic_name = "vegam",
308*9d6fa9c7SGraham Sider 	.gfx_target_version = 80003,
309ed81cd6eSKent Russell 	.max_pasid_bits = 16,
310ed81cd6eSKent Russell 	.max_no_of_hqd  = 24,
311ed81cd6eSKent Russell 	.doorbell_size  = 4,
312ed81cd6eSKent Russell 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
313ed81cd6eSKent Russell 	.event_interrupt_class = &event_interrupt_class_cik,
314ed81cd6eSKent Russell 	.num_of_watch_points = 4,
315ed81cd6eSKent Russell 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
316ed81cd6eSKent Russell 	.supports_cwsr = true,
317ed81cd6eSKent Russell 	.needs_iommu_device = false,
318ed81cd6eSKent Russell 	.needs_pci_atomics = true,
319ed81cd6eSKent Russell 	.num_sdma_engines = 2,
320ed81cd6eSKent Russell 	.num_xgmi_sdma_engines = 0,
321a3084e6cSFelix Kuehling 	.num_sdma_queues_per_engine = 2,
322a3084e6cSFelix Kuehling };
323a3084e6cSFelix Kuehling 
324389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = {
325389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
326c181159aSYong Zhao 	.asic_name = "vega10",
327*9d6fa9c7SGraham Sider 	.gfx_target_version = 90000,
328389056e5SFelix Kuehling 	.max_pasid_bits = 16,
329389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
330389056e5SFelix Kuehling 	.doorbell_size  = 8,
331389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
332389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
333389056e5SFelix Kuehling 	.num_of_watch_points = 4,
334389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
335389056e5SFelix Kuehling 	.supports_cwsr = true,
336389056e5SFelix Kuehling 	.needs_iommu_device = false,
337389056e5SFelix Kuehling 	.needs_pci_atomics = false,
33898bb9222SYong Zhao 	.num_sdma_engines = 2,
3391b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
340d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
341389056e5SFelix Kuehling };
342389056e5SFelix Kuehling 
343389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_device_info = {
344389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
345c181159aSYong Zhao 	.asic_name = "vega10",
346*9d6fa9c7SGraham Sider 	.gfx_target_version = 90000,
347389056e5SFelix Kuehling 	.max_pasid_bits = 16,
348389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
349389056e5SFelix Kuehling 	.doorbell_size  = 8,
350389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
351389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
352389056e5SFelix Kuehling 	.num_of_watch_points = 4,
353389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
354389056e5SFelix Kuehling 	.supports_cwsr = true,
355389056e5SFelix Kuehling 	.needs_iommu_device = false,
356389056e5SFelix Kuehling 	.needs_pci_atomics = false,
35798bb9222SYong Zhao 	.num_sdma_engines = 2,
3581b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
359d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
360389056e5SFelix Kuehling };
361389056e5SFelix Kuehling 
362846a44d7SGang Ba static const struct kfd_device_info vega12_device_info = {
363846a44d7SGang Ba 	.asic_family = CHIP_VEGA12,
364c181159aSYong Zhao 	.asic_name = "vega12",
365*9d6fa9c7SGraham Sider 	.gfx_target_version = 90004,
366846a44d7SGang Ba 	.max_pasid_bits = 16,
367846a44d7SGang Ba 	.max_no_of_hqd  = 24,
368846a44d7SGang Ba 	.doorbell_size  = 8,
369846a44d7SGang Ba 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
370846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_v9,
371846a44d7SGang Ba 	.num_of_watch_points = 4,
372846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
373846a44d7SGang Ba 	.supports_cwsr = true,
374846a44d7SGang Ba 	.needs_iommu_device = false,
375846a44d7SGang Ba 	.needs_pci_atomics = false,
376846a44d7SGang Ba 	.num_sdma_engines = 2,
3771b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
378846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
379846a44d7SGang Ba };
380846a44d7SGang Ba 
38122a3a294SShaoyun Liu static const struct kfd_device_info vega20_device_info = {
38222a3a294SShaoyun Liu 	.asic_family = CHIP_VEGA20,
383c181159aSYong Zhao 	.asic_name = "vega20",
384*9d6fa9c7SGraham Sider 	.gfx_target_version = 90006,
38522a3a294SShaoyun Liu 	.max_pasid_bits = 16,
38622a3a294SShaoyun Liu 	.max_no_of_hqd	= 24,
38722a3a294SShaoyun Liu 	.doorbell_size	= 8,
38822a3a294SShaoyun Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
38922a3a294SShaoyun Liu 	.event_interrupt_class = &event_interrupt_class_v9,
39022a3a294SShaoyun Liu 	.num_of_watch_points = 4,
39122a3a294SShaoyun Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
39222a3a294SShaoyun Liu 	.supports_cwsr = true,
39322a3a294SShaoyun Liu 	.needs_iommu_device = false,
394006a0b3dSShaoyun Liu 	.needs_pci_atomics = false,
39522a3a294SShaoyun Liu 	.num_sdma_engines = 2,
3961b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
39722a3a294SShaoyun Liu 	.num_sdma_queues_per_engine = 8,
39822a3a294SShaoyun Liu };
39922a3a294SShaoyun Liu 
40049adcf8aSYong Zhao static const struct kfd_device_info arcturus_device_info = {
40149adcf8aSYong Zhao 	.asic_family = CHIP_ARCTURUS,
402c181159aSYong Zhao 	.asic_name = "arcturus",
403*9d6fa9c7SGraham Sider 	.gfx_target_version = 90008,
40449adcf8aSYong Zhao 	.max_pasid_bits = 16,
40549adcf8aSYong Zhao 	.max_no_of_hqd	= 24,
40649adcf8aSYong Zhao 	.doorbell_size	= 8,
40749adcf8aSYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
40849adcf8aSYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
40949adcf8aSYong Zhao 	.num_of_watch_points = 4,
41049adcf8aSYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
41149adcf8aSYong Zhao 	.supports_cwsr = true,
41249adcf8aSYong Zhao 	.needs_iommu_device = false,
41349adcf8aSYong Zhao 	.needs_pci_atomics = false,
414b6689cf7SOak Zeng 	.num_sdma_engines = 2,
415b6689cf7SOak Zeng 	.num_xgmi_sdma_engines = 6,
41649adcf8aSYong Zhao 	.num_sdma_queues_per_engine = 8,
41749adcf8aSYong Zhao };
41849adcf8aSYong Zhao 
41936e22d59SYong Zhao static const struct kfd_device_info aldebaran_device_info = {
42036e22d59SYong Zhao 	.asic_family = CHIP_ALDEBARAN,
42136e22d59SYong Zhao 	.asic_name = "aldebaran",
422*9d6fa9c7SGraham Sider 	.gfx_target_version = 90010,
42336e22d59SYong Zhao 	.max_pasid_bits = 16,
42436e22d59SYong Zhao 	.max_no_of_hqd	= 24,
42536e22d59SYong Zhao 	.doorbell_size	= 8,
42636e22d59SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
42736e22d59SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
42836e22d59SYong Zhao 	.num_of_watch_points = 4,
42936e22d59SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
43036e22d59SYong Zhao 	.supports_cwsr = true,
43136e22d59SYong Zhao 	.needs_iommu_device = false,
43236e22d59SYong Zhao 	.needs_pci_atomics = false,
43336e22d59SYong Zhao 	.num_sdma_engines = 2,
43436e22d59SYong Zhao 	.num_xgmi_sdma_engines = 3,
43536e22d59SYong Zhao 	.num_sdma_queues_per_engine = 8,
43636e22d59SYong Zhao };
43736e22d59SYong Zhao 
4382b9c2211SHuang Rui static const struct kfd_device_info renoir_device_info = {
4392b9c2211SHuang Rui 	.asic_family = CHIP_RENOIR,
440acb9acbeSHuang Rui 	.asic_name = "renoir",
441*9d6fa9c7SGraham Sider 	.gfx_target_version = 90002,
4422b9c2211SHuang Rui 	.max_pasid_bits = 16,
4432b9c2211SHuang Rui 	.max_no_of_hqd  = 24,
4442b9c2211SHuang Rui 	.doorbell_size  = 8,
4452b9c2211SHuang Rui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
4462b9c2211SHuang Rui 	.event_interrupt_class = &event_interrupt_class_v9,
4472b9c2211SHuang Rui 	.num_of_watch_points = 4,
4482b9c2211SHuang Rui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
4492b9c2211SHuang Rui 	.supports_cwsr = true,
4502b9c2211SHuang Rui 	.needs_iommu_device = false,
4512b9c2211SHuang Rui 	.needs_pci_atomics = false,
4522b9c2211SHuang Rui 	.num_sdma_engines = 1,
4532b9c2211SHuang Rui 	.num_xgmi_sdma_engines = 0,
4542b9c2211SHuang Rui 	.num_sdma_queues_per_engine = 2,
4552b9c2211SHuang Rui };
4562b9c2211SHuang Rui 
45714328aa5SPhilip Cox static const struct kfd_device_info navi10_device_info = {
45814328aa5SPhilip Cox 	.asic_family = CHIP_NAVI10,
459c181159aSYong Zhao 	.asic_name = "navi10",
460*9d6fa9c7SGraham Sider 	.gfx_target_version = 100100,
46114328aa5SPhilip Cox 	.max_pasid_bits = 16,
46214328aa5SPhilip Cox 	.max_no_of_hqd  = 24,
46314328aa5SPhilip Cox 	.doorbell_size  = 8,
46414328aa5SPhilip Cox 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
46514328aa5SPhilip Cox 	.event_interrupt_class = &event_interrupt_class_v9,
46614328aa5SPhilip Cox 	.num_of_watch_points = 4,
46714328aa5SPhilip Cox 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
46814328aa5SPhilip Cox 	.needs_iommu_device = false,
46914328aa5SPhilip Cox 	.supports_cwsr = true,
4706cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
47114328aa5SPhilip Cox 	.num_sdma_engines = 2,
47214328aa5SPhilip Cox 	.num_xgmi_sdma_engines = 0,
47314328aa5SPhilip Cox 	.num_sdma_queues_per_engine = 8,
47414328aa5SPhilip Cox };
47514328aa5SPhilip Cox 
476b77fb9d8Sshaoyunl static const struct kfd_device_info navi12_device_info = {
4770e94b564Sshaoyunl 	.asic_family = CHIP_NAVI12,
478b77fb9d8Sshaoyunl 	.asic_name = "navi12",
479*9d6fa9c7SGraham Sider 	.gfx_target_version = 100101,
480b77fb9d8Sshaoyunl 	.max_pasid_bits = 16,
481b77fb9d8Sshaoyunl 	.max_no_of_hqd  = 24,
482b77fb9d8Sshaoyunl 	.doorbell_size  = 8,
483b77fb9d8Sshaoyunl 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
484b77fb9d8Sshaoyunl 	.event_interrupt_class = &event_interrupt_class_v9,
485b77fb9d8Sshaoyunl 	.num_of_watch_points = 4,
486b77fb9d8Sshaoyunl 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
487b77fb9d8Sshaoyunl 	.needs_iommu_device = false,
488b77fb9d8Sshaoyunl 	.supports_cwsr = true,
4896cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
490b77fb9d8Sshaoyunl 	.num_sdma_engines = 2,
491b77fb9d8Sshaoyunl 	.num_xgmi_sdma_engines = 0,
492b77fb9d8Sshaoyunl 	.num_sdma_queues_per_engine = 8,
493b77fb9d8Sshaoyunl };
494b77fb9d8Sshaoyunl 
4958099ae40SYong Zhao static const struct kfd_device_info navi14_device_info = {
4968099ae40SYong Zhao 	.asic_family = CHIP_NAVI14,
4978099ae40SYong Zhao 	.asic_name = "navi14",
498*9d6fa9c7SGraham Sider 	.gfx_target_version = 100102,
4998099ae40SYong Zhao 	.max_pasid_bits = 16,
5008099ae40SYong Zhao 	.max_no_of_hqd  = 24,
5018099ae40SYong Zhao 	.doorbell_size  = 8,
5028099ae40SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
5038099ae40SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
5048099ae40SYong Zhao 	.num_of_watch_points = 4,
5058099ae40SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
5068099ae40SYong Zhao 	.needs_iommu_device = false,
5078099ae40SYong Zhao 	.supports_cwsr = true,
5086cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
5098099ae40SYong Zhao 	.num_sdma_engines = 2,
5108099ae40SYong Zhao 	.num_xgmi_sdma_engines = 0,
5118099ae40SYong Zhao 	.num_sdma_queues_per_engine = 8,
5128099ae40SYong Zhao };
5138099ae40SYong Zhao 
5143a2f0c81SYong Zhao static const struct kfd_device_info sienna_cichlid_device_info = {
5153a2f0c81SYong Zhao 	.asic_family = CHIP_SIENNA_CICHLID,
5163a2f0c81SYong Zhao 	.asic_name = "sienna_cichlid",
517*9d6fa9c7SGraham Sider 	.gfx_target_version = 100300,
5183a2f0c81SYong Zhao 	.max_pasid_bits = 16,
5193a2f0c81SYong Zhao 	.max_no_of_hqd  = 24,
5203a2f0c81SYong Zhao 	.doorbell_size  = 8,
5213a2f0c81SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
5223a2f0c81SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
5233a2f0c81SYong Zhao 	.num_of_watch_points = 4,
5243a2f0c81SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
5253a2f0c81SYong Zhao 	.needs_iommu_device = false,
5263a2f0c81SYong Zhao 	.supports_cwsr = true,
5276cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
5283a2f0c81SYong Zhao 	.num_sdma_engines = 4,
5293a2f0c81SYong Zhao 	.num_xgmi_sdma_engines = 0,
5303a2f0c81SYong Zhao 	.num_sdma_queues_per_engine = 8,
5313a2f0c81SYong Zhao };
5323a2f0c81SYong Zhao 
533de89b2e4SChengming Gui static const struct kfd_device_info navy_flounder_device_info = {
534de89b2e4SChengming Gui 	.asic_family = CHIP_NAVY_FLOUNDER,
535de89b2e4SChengming Gui 	.asic_name = "navy_flounder",
536*9d6fa9c7SGraham Sider 	.gfx_target_version = 100301,
537de89b2e4SChengming Gui 	.max_pasid_bits = 16,
538de89b2e4SChengming Gui 	.max_no_of_hqd  = 24,
539de89b2e4SChengming Gui 	.doorbell_size  = 8,
540de89b2e4SChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
541de89b2e4SChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
542de89b2e4SChengming Gui 	.num_of_watch_points = 4,
543de89b2e4SChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
544de89b2e4SChengming Gui 	.needs_iommu_device = false,
545de89b2e4SChengming Gui 	.supports_cwsr = true,
5466cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
547de89b2e4SChengming Gui 	.num_sdma_engines = 2,
548de89b2e4SChengming Gui 	.num_xgmi_sdma_engines = 0,
549de89b2e4SChengming Gui 	.num_sdma_queues_per_engine = 8,
550de89b2e4SChengming Gui };
551de89b2e4SChengming Gui 
5523a5e715dSHuang Rui static const struct kfd_device_info vangogh_device_info = {
5533a5e715dSHuang Rui 	.asic_family = CHIP_VANGOGH,
5543a5e715dSHuang Rui 	.asic_name = "vangogh",
555*9d6fa9c7SGraham Sider 	.gfx_target_version = 100303,
5563a5e715dSHuang Rui 	.max_pasid_bits = 16,
5573a5e715dSHuang Rui 	.max_no_of_hqd  = 24,
5583a5e715dSHuang Rui 	.doorbell_size  = 8,
5593a5e715dSHuang Rui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
5603a5e715dSHuang Rui 	.event_interrupt_class = &event_interrupt_class_v9,
5613a5e715dSHuang Rui 	.num_of_watch_points = 4,
5623a5e715dSHuang Rui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
5633a5e715dSHuang Rui 	.needs_iommu_device = false,
5643a5e715dSHuang Rui 	.supports_cwsr = true,
5653a5e715dSHuang Rui 	.needs_pci_atomics = false,
5663a5e715dSHuang Rui 	.num_sdma_engines = 1,
5673a5e715dSHuang Rui 	.num_xgmi_sdma_engines = 0,
5683a5e715dSHuang Rui 	.num_sdma_queues_per_engine = 2,
5693a5e715dSHuang Rui };
5703a5e715dSHuang Rui 
571eb5a34d4SChengming Gui static const struct kfd_device_info dimgrey_cavefish_device_info = {
572eb5a34d4SChengming Gui 	.asic_family = CHIP_DIMGREY_CAVEFISH,
573eb5a34d4SChengming Gui 	.asic_name = "dimgrey_cavefish",
574*9d6fa9c7SGraham Sider 	.gfx_target_version = 100302,
575eb5a34d4SChengming Gui 	.max_pasid_bits = 16,
576eb5a34d4SChengming Gui 	.max_no_of_hqd  = 24,
577eb5a34d4SChengming Gui 	.doorbell_size  = 8,
578eb5a34d4SChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
579eb5a34d4SChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
580eb5a34d4SChengming Gui 	.num_of_watch_points = 4,
581eb5a34d4SChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
582eb5a34d4SChengming Gui 	.needs_iommu_device = false,
583eb5a34d4SChengming Gui 	.supports_cwsr = true,
5846cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
585eb5a34d4SChengming Gui 	.num_sdma_engines = 2,
586eb5a34d4SChengming Gui 	.num_xgmi_sdma_engines = 0,
587eb5a34d4SChengming Gui 	.num_sdma_queues_per_engine = 8,
588eb5a34d4SChengming Gui };
589eb5a34d4SChengming Gui 
5905cf607ccSChengming Gui static const struct kfd_device_info beige_goby_device_info = {
5915cf607ccSChengming Gui 	.asic_family = CHIP_BEIGE_GOBY,
5925cf607ccSChengming Gui 	.asic_name = "beige_goby",
593*9d6fa9c7SGraham Sider 	.gfx_target_version = 100304,
5945cf607ccSChengming Gui 	.max_pasid_bits = 16,
5955cf607ccSChengming Gui 	.max_no_of_hqd  = 24,
5965cf607ccSChengming Gui 	.doorbell_size  = 8,
5975cf607ccSChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
5985cf607ccSChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
5995cf607ccSChengming Gui 	.num_of_watch_points = 4,
6005cf607ccSChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
6015cf607ccSChengming Gui 	.needs_iommu_device = false,
6025cf607ccSChengming Gui 	.supports_cwsr = true,
6035cf607ccSChengming Gui 	.needs_pci_atomics = true,
6045cf607ccSChengming Gui 	.num_sdma_engines = 1,
6055cf607ccSChengming Gui 	.num_xgmi_sdma_engines = 0,
6065cf607ccSChengming Gui 	.num_sdma_queues_per_engine = 8,
6075cf607ccSChengming Gui };
6085cf607ccSChengming Gui 
609bf9d4e88SAaron Liu static const struct kfd_device_info yellow_carp_device_info = {
610bf9d4e88SAaron Liu 	.asic_family = CHIP_YELLOW_CARP,
611bf9d4e88SAaron Liu 	.asic_name = "yellow_carp",
612*9d6fa9c7SGraham Sider 	.gfx_target_version = 100305,
613bf9d4e88SAaron Liu 	.max_pasid_bits = 16,
614bf9d4e88SAaron Liu 	.max_no_of_hqd  = 24,
615bf9d4e88SAaron Liu 	.doorbell_size  = 8,
616bf9d4e88SAaron Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
617bf9d4e88SAaron Liu 	.event_interrupt_class = &event_interrupt_class_v9,
618bf9d4e88SAaron Liu 	.num_of_watch_points = 4,
619bf9d4e88SAaron Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
620bf9d4e88SAaron Liu 	.needs_iommu_device = false,
621bf9d4e88SAaron Liu 	.supports_cwsr = true,
622bf9d4e88SAaron Liu 	.needs_pci_atomics = false,
623bf9d4e88SAaron Liu 	.num_sdma_engines = 1,
624bf9d4e88SAaron Liu 	.num_xgmi_sdma_engines = 0,
625bf9d4e88SAaron Liu 	.num_sdma_queues_per_engine = 2,
626bf9d4e88SAaron Liu };
627eb5a34d4SChengming Gui 
62806e75b88STao Zhou static const struct kfd_device_info cyan_skillfish_device_info = {
62906e75b88STao Zhou 	.asic_family = CHIP_CYAN_SKILLFISH,
63006e75b88STao Zhou 	.asic_name = "cyan_skillfish",
631*9d6fa9c7SGraham Sider 	.gfx_target_version = 100103,
63206e75b88STao Zhou 	.max_pasid_bits = 16,
63306e75b88STao Zhou 	.max_no_of_hqd  = 24,
63406e75b88STao Zhou 	.doorbell_size  = 8,
63506e75b88STao Zhou 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
63606e75b88STao Zhou 	.event_interrupt_class = &event_interrupt_class_v9,
63706e75b88STao Zhou 	.num_of_watch_points = 4,
63806e75b88STao Zhou 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
63906e75b88STao Zhou 	.needs_iommu_device = false,
64006e75b88STao Zhou 	.supports_cwsr = true,
64106e75b88STao Zhou 	.needs_pci_atomics = true,
64206e75b88STao Zhou 	.num_sdma_engines = 2,
64306e75b88STao Zhou 	.num_xgmi_sdma_engines = 0,
64406e75b88STao Zhou 	.num_sdma_queues_per_engine = 8,
64506e75b88STao Zhou };
64606e75b88STao Zhou 
647050091abSYong Zhao /* For each entry, [0] is regular and [1] is virtualisation device. */
648050091abSYong Zhao static const struct kfd_device_info *kfd_supported_devices[][2] = {
64995a5bd1bSYong Zhao #ifdef KFD_SUPPORT_IOMMU_V2
650050091abSYong Zhao 	[CHIP_KAVERI] = {&kaveri_device_info, NULL},
65195a5bd1bSYong Zhao 	[CHIP_CARRIZO] = {&carrizo_device_info, NULL},
65295a5bd1bSYong Zhao #endif
6532b3bbf23SYueHaibing 	[CHIP_RAVEN] = {&raven_device_info, NULL},
654050091abSYong Zhao 	[CHIP_HAWAII] = {&hawaii_device_info, NULL},
655050091abSYong Zhao 	[CHIP_TONGA] = {&tonga_device_info, NULL},
656050091abSYong Zhao 	[CHIP_FIJI] = {&fiji_device_info, &fiji_vf_device_info},
657050091abSYong Zhao 	[CHIP_POLARIS10] = {&polaris10_device_info, &polaris10_vf_device_info},
658050091abSYong Zhao 	[CHIP_POLARIS11] = {&polaris11_device_info, NULL},
659050091abSYong Zhao 	[CHIP_POLARIS12] = {&polaris12_device_info, NULL},
660050091abSYong Zhao 	[CHIP_VEGAM] = {&vegam_device_info, NULL},
661050091abSYong Zhao 	[CHIP_VEGA10] = {&vega10_device_info, &vega10_vf_device_info},
662050091abSYong Zhao 	[CHIP_VEGA12] = {&vega12_device_info, NULL},
663050091abSYong Zhao 	[CHIP_VEGA20] = {&vega20_device_info, NULL},
6642b9c2211SHuang Rui 	[CHIP_RENOIR] = {&renoir_device_info, NULL},
665050091abSYong Zhao 	[CHIP_ARCTURUS] = {&arcturus_device_info, &arcturus_device_info},
666cecd91b4SZhigang Luo 	[CHIP_ALDEBARAN] = {&aldebaran_device_info, &aldebaran_device_info},
667050091abSYong Zhao 	[CHIP_NAVI10] = {&navi10_device_info, NULL},
668b77fb9d8Sshaoyunl 	[CHIP_NAVI12] = {&navi12_device_info, &navi12_device_info},
6698099ae40SYong Zhao 	[CHIP_NAVI14] = {&navi14_device_info, NULL},
670adab4dadSshaoyunl 	[CHIP_SIENNA_CICHLID] = {&sienna_cichlid_device_info, &sienna_cichlid_device_info},
671de89b2e4SChengming Gui 	[CHIP_NAVY_FLOUNDER] = {&navy_flounder_device_info, &navy_flounder_device_info},
6723a5e715dSHuang Rui 	[CHIP_VANGOGH] = {&vangogh_device_info, NULL},
673eb5a34d4SChengming Gui 	[CHIP_DIMGREY_CAVEFISH] = {&dimgrey_cavefish_device_info, &dimgrey_cavefish_device_info},
6745cf607ccSChengming Gui 	[CHIP_BEIGE_GOBY] = {&beige_goby_device_info, &beige_goby_device_info},
675bf9d4e88SAaron Liu 	[CHIP_YELLOW_CARP] = {&yellow_carp_device_info, NULL},
67606e75b88STao Zhou 	[CHIP_CYAN_SKILLFISH] = {&cyan_skillfish_device_info, NULL},
6774a488a7aSOded Gabbay };
6784a488a7aSOded Gabbay 
6796e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
6806e81090bSOded Gabbay 				unsigned int chunk_size);
6816e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
6826e81090bSOded Gabbay 
683b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
684b8935a7cSYong Zhao 
685cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
686e392c887SYong Zhao 	struct pci_dev *pdev, unsigned int asic_type, bool vf)
6874a488a7aSOded Gabbay {
6884a488a7aSOded Gabbay 	struct kfd_dev *kfd;
689050091abSYong Zhao 	const struct kfd_device_info *device_info;
690e392c887SYong Zhao 	const struct kfd2kgd_calls *f2g;
691050091abSYong Zhao 
692e392c887SYong Zhao 	if (asic_type >= sizeof(kfd_supported_devices) / (sizeof(void *) * 2)
693e392c887SYong Zhao 		|| asic_type >= sizeof(kfd2kgd_funcs) / sizeof(void *)) {
694050091abSYong Zhao 		dev_err(kfd_device, "asic_type %d out of range\n", asic_type);
695050091abSYong Zhao 		return NULL; /* asic_type out of range */
696050091abSYong Zhao 	}
697050091abSYong Zhao 
698050091abSYong Zhao 	device_info = kfd_supported_devices[asic_type][vf];
699e392c887SYong Zhao 	f2g = kfd2kgd_funcs[asic_type];
7004a488a7aSOded Gabbay 
701aa5e899dSDan Carpenter 	if (!device_info || !f2g) {
702050091abSYong Zhao 		dev_err(kfd_device, "%s %s not supported in kfd\n",
703050091abSYong Zhao 			amdgpu_asic_name[asic_type], vf ? "VF" : "");
7044a488a7aSOded Gabbay 		return NULL;
7054ebc7182SYong Zhao 	}
7064a488a7aSOded Gabbay 
707d35f00d8SEric Huang 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
708d35f00d8SEric Huang 	if (!kfd)
709d35f00d8SEric Huang 		return NULL;
710d35f00d8SEric Huang 
7116106dce9Swelu 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
7126106dce9Swelu 	 * 32 and 64-bit requests are possible and must be
7136106dce9Swelu 	 * supported.
7143ee2d00cSFelix Kuehling 	 */
715aabf3a95SJack Xiao 	kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kgd);
716aabf3a95SJack Xiao 	if (device_info->needs_pci_atomics &&
717aabf3a95SJack Xiao 	    !kfd->pci_atomic_requested) {
7183ee2d00cSFelix Kuehling 		dev_info(kfd_device,
7196106dce9Swelu 			 "skipped device %x:%x, PCI rejects atomics\n",
7203ee2d00cSFelix Kuehling 			 pdev->vendor, pdev->device);
721d35f00d8SEric Huang 		kfree(kfd);
7223ee2d00cSFelix Kuehling 		return NULL;
723aabf3a95SJack Xiao 	}
7244a488a7aSOded Gabbay 
7254a488a7aSOded Gabbay 	kfd->kgd = kgd;
7264a488a7aSOded Gabbay 	kfd->device_info = device_info;
7274a488a7aSOded Gabbay 	kfd->pdev = pdev;
72819f6d2a6SOded Gabbay 	kfd->init_complete = false;
729cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
73043d8107fSHarish Kasiviswanathan 	atomic_set(&kfd->compute_profile, 0);
731cea405b1SXihan Zhang 
732cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
733cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
734cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
7354a488a7aSOded Gabbay 
7369b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
7379b54d201SEric Huang 
73859d7115dSMukul Joshi 	ida_init(&kfd->doorbell_ida);
73959d7115dSMukul Joshi 
7404a488a7aSOded Gabbay 	return kfd;
7414a488a7aSOded Gabbay }
7424a488a7aSOded Gabbay 
743373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
744373d7080SFelix Kuehling {
745373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
7463e76c239SFelix Kuehling 		if (kfd->device_info->asic_family < CHIP_VEGA10) {
747373d7080SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
748373d7080SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
749373d7080SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
7500ef6845cSJay Cornwall 		} else if (kfd->device_info->asic_family == CHIP_ARCTURUS) {
7513baa24f0SOak Zeng 			BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
7523baa24f0SOak Zeng 			kfd->cwsr_isa = cwsr_trap_arcturus_hex;
7533baa24f0SOak Zeng 			kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
7540ef6845cSJay Cornwall 		} else if (kfd->device_info->asic_family == CHIP_ALDEBARAN) {
7550ef6845cSJay Cornwall 			BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex) > PAGE_SIZE);
7560ef6845cSJay Cornwall 			kfd->cwsr_isa = cwsr_trap_aldebaran_hex;
7570ef6845cSJay Cornwall 			kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex);
75814328aa5SPhilip Cox 		} else if (kfd->device_info->asic_family < CHIP_NAVI10) {
7593e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
7603e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
7613e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
76280b6cfedSJay Cornwall 		} else if (kfd->device_info->asic_family < CHIP_SIENNA_CICHLID) {
76380b6cfedSJay Cornwall 			BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE);
76480b6cfedSJay Cornwall 			kfd->cwsr_isa = cwsr_trap_nv1x_hex;
76580b6cfedSJay Cornwall 			kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);
76614328aa5SPhilip Cox 		} else {
76714328aa5SPhilip Cox 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
76814328aa5SPhilip Cox 			kfd->cwsr_isa = cwsr_trap_gfx10_hex;
76914328aa5SPhilip Cox 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
7703e76c239SFelix Kuehling 		}
7713e76c239SFelix Kuehling 
772373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
773373d7080SFelix Kuehling 	}
774373d7080SFelix Kuehling }
775373d7080SFelix Kuehling 
77629633d0eSJoseph Greathouse static int kfd_gws_init(struct kfd_dev *kfd)
77729633d0eSJoseph Greathouse {
77829633d0eSJoseph Greathouse 	int ret = 0;
77929633d0eSJoseph Greathouse 
78029633d0eSJoseph Greathouse 	if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
78129633d0eSJoseph Greathouse 		return 0;
78229633d0eSJoseph Greathouse 
78329633d0eSJoseph Greathouse 	if (hws_gws_support
784fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family == CHIP_VEGA10
785fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x81b3)
786fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family >= CHIP_VEGA12
78729633d0eSJoseph Greathouse 			&& kfd->device_info->asic_family <= CHIP_RAVEN
788fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x1b3)
789fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family == CHIP_ARCTURUS
7908baa6018SHarish Kasiviswanathan 			&& kfd->mec2_fw_version >= 0x30)
7918baa6018SHarish Kasiviswanathan 		|| (kfd->device_info->asic_family == CHIP_ALDEBARAN
7928baa6018SHarish Kasiviswanathan 			&& kfd->mec2_fw_version >= 0x28))
79329633d0eSJoseph Greathouse 		ret = amdgpu_amdkfd_alloc_gws(kfd->kgd,
79429633d0eSJoseph Greathouse 				amdgpu_amdkfd_get_num_gws(kfd->kgd), &kfd->gws);
79529633d0eSJoseph Greathouse 
79629633d0eSJoseph Greathouse 	return ret;
79729633d0eSJoseph Greathouse }
79829633d0eSJoseph Greathouse 
799938a0650SAmber Lin static void kfd_smi_init(struct kfd_dev *dev) {
800938a0650SAmber Lin 	INIT_LIST_HEAD(&dev->smi_clients);
801938a0650SAmber Lin 	spin_lock_init(&dev->smi_lock);
802938a0650SAmber Lin }
803938a0650SAmber Lin 
8044a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
8053a0c3423SHarish Kasiviswanathan 			 struct drm_device *ddev,
8064a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
8074a488a7aSOded Gabbay {
808fd6a440eSJonathan Kim 	unsigned int size, map_process_packet_size;
80919f6d2a6SOded Gabbay 
8103a0c3423SHarish Kasiviswanathan 	kfd->ddev = ddev;
8110da8b10eSAmber Lin 	kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
8125ade6c9cSFelix Kuehling 			KGD_ENGINE_MEC1);
81329633d0eSJoseph Greathouse 	kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
81429633d0eSJoseph Greathouse 			KGD_ENGINE_MEC2);
8150da8b10eSAmber Lin 	kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
8165ade6c9cSFelix Kuehling 			KGD_ENGINE_SDMA1);
8174a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
8184a488a7aSOded Gabbay 
81944008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
82044008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
82144008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
82244008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
82344008d7aSYong Zhao 
824a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
825a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
826a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
827a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
828a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
829a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
830a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
831a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
832a99c6d4fSFelix Kuehling 	} else
833a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
834a99c6d4fSFelix Kuehling 
83519f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
836b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
83719f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
83819f6d2a6SOded Gabbay 
839e18e794eSOded Gabbay 	/*
840e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
841e18e794eSOded Gabbay 	 * There can be only 2 packets at once
842e18e794eSOded Gabbay 	 */
843fd6a440eSJonathan Kim 	map_process_packet_size =
844fd6a440eSJonathan Kim 			kfd->device_info->asic_family == CHIP_ALDEBARAN ?
845fd6a440eSJonathan Kim 				sizeof(struct pm4_mes_map_process_aldebaran) :
846fd6a440eSJonathan Kim 					sizeof(struct pm4_mes_map_process);
847fd6a440eSJonathan Kim 	size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size +
848507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
849507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
850e18e794eSOded Gabbay 
851e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
852e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
853e18e794eSOded Gabbay 
854e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
85519f6d2a6SOded Gabbay 	size += 512 * 1024;
85619f6d2a6SOded Gabbay 
8577cd52c91SAmber Lin 	if (amdgpu_amdkfd_alloc_gtt_mem(
858cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
85915426dbbSYong Zhao 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
86015426dbbSYong Zhao 			false)) {
86179775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
862e09d4fc8SOak Zeng 		goto alloc_gtt_mem_failure;
86319f6d2a6SOded Gabbay 	}
86419f6d2a6SOded Gabbay 
86579775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
866e18e794eSOded Gabbay 
86773a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
86873a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
86979775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
87073a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
87173a1da0bSOded Gabbay 	}
87273a1da0bSOded Gabbay 
873735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
874735df2baSFelix Kuehling 		dev_err(kfd_device,
875735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
876735df2baSFelix Kuehling 		goto kfd_doorbell_error;
877735df2baSFelix Kuehling 	}
87819f6d2a6SOded Gabbay 
879332f6e1eSFelix Kuehling 	kfd->hive_id = amdgpu_amdkfd_get_hive_id(kfd->kgd);
8800c1690e3SShaoyun Liu 
8819b498efaSAlex Deucher 	kfd->noretry = amdgpu_amdkfd_get_noretry(kfd->kgd);
8829b498efaSAlex Deucher 
8832249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
88479775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
8852249d558SAndrew Lewycky 		goto kfd_interrupt_error;
8862249d558SAndrew Lewycky 	}
8872249d558SAndrew Lewycky 
88864c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
88964c7f8cfSBen Goz 	if (!kfd->dqm) {
89079775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
89164c7f8cfSBen Goz 		goto device_queue_manager_error;
89264c7f8cfSBen Goz 	}
89364c7f8cfSBen Goz 
89429633d0eSJoseph Greathouse 	/* If supported on this device, allocate global GWS that is shared
89529633d0eSJoseph Greathouse 	 * by all KFD processes
89629633d0eSJoseph Greathouse 	 */
89729633d0eSJoseph Greathouse 	if (kfd_gws_init(kfd)) {
89829633d0eSJoseph Greathouse 		dev_err(kfd_device, "Could not allocate %d gws\n",
89929633d0eSJoseph Greathouse 			amdgpu_amdkfd_get_num_gws(kfd->kgd));
90029633d0eSJoseph Greathouse 		goto gws_error;
90129633d0eSJoseph Greathouse 	}
90229633d0eSJoseph Greathouse 
9036127896fSHuang Rui 	/* If CRAT is broken, won't set iommu enabled */
9046127896fSHuang Rui 	kfd_double_confirm_iommu_support(kfd);
9056127896fSHuang Rui 
90664d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
90764d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
90864d1c3a4SFelix Kuehling 		goto device_iommu_error;
90964c7f8cfSBen Goz 	}
91064c7f8cfSBen Goz 
911373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
912373d7080SFelix Kuehling 
913814ab993SPhilip Yang 	svm_migrate_init((struct amdgpu_device *)kfd->kgd);
914814ab993SPhilip Yang 
915b8935a7cSYong Zhao 	if (kfd_resume(kfd))
916b8935a7cSYong Zhao 		goto kfd_resume_error;
917b8935a7cSYong Zhao 
918fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
919fbeb661bSYair Shachar 
920465ab9e0SOak Zeng 	if (kfd_topology_add_device(kfd)) {
921465ab9e0SOak Zeng 		dev_err(kfd_device, "Error adding device to topology\n");
922465ab9e0SOak Zeng 		goto kfd_topology_add_device_error;
923465ab9e0SOak Zeng 	}
924465ab9e0SOak Zeng 
925938a0650SAmber Lin 	kfd_smi_init(kfd);
926938a0650SAmber Lin 
9274a488a7aSOded Gabbay 	kfd->init_complete = true;
92879775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
9294a488a7aSOded Gabbay 		 kfd->pdev->device);
9304a488a7aSOded Gabbay 
93179775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
932d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
93364c7f8cfSBen Goz 
93419f6d2a6SOded Gabbay 	goto out;
93519f6d2a6SOded Gabbay 
936465ab9e0SOak Zeng kfd_topology_add_device_error:
937b8935a7cSYong Zhao kfd_resume_error:
93864d1c3a4SFelix Kuehling device_iommu_error:
93929633d0eSJoseph Greathouse gws_error:
94064c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
94164c7f8cfSBen Goz device_queue_manager_error:
9422249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
9432249d558SAndrew Lewycky kfd_interrupt_error:
944735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
945735df2baSFelix Kuehling kfd_doorbell_error:
94673a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
94773a1da0bSOded Gabbay kfd_gtt_sa_init_error:
9487cd52c91SAmber Lin 	amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
949e09d4fc8SOak Zeng alloc_gtt_mem_failure:
95029633d0eSJoseph Greathouse 	if (kfd->gws)
951e09d4fc8SOak Zeng 		amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
95219f6d2a6SOded Gabbay 	dev_err(kfd_device,
95379775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
95419f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
95519f6d2a6SOded Gabbay out:
95619f6d2a6SOded Gabbay 	return kfd->init_complete;
9574a488a7aSOded Gabbay }
9584a488a7aSOded Gabbay 
9594a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
9604a488a7aSOded Gabbay {
961b17f068aSOded Gabbay 	if (kfd->init_complete) {
962814ab993SPhilip Yang 		svm_migrate_fini((struct amdgpu_device *)kfd->kgd);
96364c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
9642249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
96519f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
966735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
96759d7115dSMukul Joshi 		ida_destroy(&kfd->doorbell_ida);
96873a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
9697cd52c91SAmber Lin 		amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
97029633d0eSJoseph Greathouse 		if (kfd->gws)
971e09d4fc8SOak Zeng 			amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
972b17f068aSOded Gabbay 	}
9735b5c4e40SEvgeny Pinchuk 
9744a488a7aSOded Gabbay 	kfree(kfd);
9754a488a7aSOded Gabbay }
9764a488a7aSOded Gabbay 
977e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
978e3b7a967SShaoyun Liu {
979e42051d2SShaoyun Liu 	if (!kfd->init_complete)
980e42051d2SShaoyun Liu 		return 0;
98109c34e8dSFelix Kuehling 
98255977744SMukul Joshi 	kfd_smi_event_update_gpu_reset(kfd, false);
98355977744SMukul Joshi 
98409c34e8dSFelix Kuehling 	kfd->dqm->ops.pre_reset(kfd->dqm);
98509c34e8dSFelix Kuehling 
9869593f4d6SRajneesh Bhardwaj 	kgd2kfd_suspend(kfd, false);
987e42051d2SShaoyun Liu 
988e42051d2SShaoyun Liu 	kfd_signal_reset_event(kfd);
989e3b7a967SShaoyun Liu 	return 0;
990e3b7a967SShaoyun Liu }
991e3b7a967SShaoyun Liu 
992e42051d2SShaoyun Liu /*
993e42051d2SShaoyun Liu  * Fix me. KFD won't be able to resume existing process for now.
994e42051d2SShaoyun Liu  * We will keep all existing process in a evicted state and
995e42051d2SShaoyun Liu  * wait the process to be terminated.
996e42051d2SShaoyun Liu  */
997e42051d2SShaoyun Liu 
998e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
999e3b7a967SShaoyun Liu {
1000a1bd079fSyu kuai 	int ret;
1001e42051d2SShaoyun Liu 
1002e42051d2SShaoyun Liu 	if (!kfd->init_complete)
1003e3b7a967SShaoyun Liu 		return 0;
1004e42051d2SShaoyun Liu 
1005e42051d2SShaoyun Liu 	ret = kfd_resume(kfd);
1006e42051d2SShaoyun Liu 	if (ret)
1007e42051d2SShaoyun Liu 		return ret;
1008a1bd079fSyu kuai 	atomic_dec(&kfd_locked);
10099b54d201SEric Huang 
10109b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
10119b54d201SEric Huang 
101255977744SMukul Joshi 	kfd_smi_event_update_gpu_reset(kfd, true);
101355977744SMukul Joshi 
1014e42051d2SShaoyun Liu 	return 0;
1015e42051d2SShaoyun Liu }
1016e42051d2SShaoyun Liu 
1017e42051d2SShaoyun Liu bool kfd_is_locked(void)
1018e42051d2SShaoyun Liu {
1019e42051d2SShaoyun Liu 	return  (atomic_read(&kfd_locked) > 0);
1020e3b7a967SShaoyun Liu }
1021e3b7a967SShaoyun Liu 
10229593f4d6SRajneesh Bhardwaj void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm)
10234a488a7aSOded Gabbay {
1024733fa1f7SYong Zhao 	if (!kfd->init_complete)
1025733fa1f7SYong Zhao 		return;
1026733fa1f7SYong Zhao 
10279593f4d6SRajneesh Bhardwaj 	/* for runtime suspend, skip locking kfd */
10289593f4d6SRajneesh Bhardwaj 	if (!run_pm) {
102926103436SFelix Kuehling 		/* For first KFD device suspend all the KFD processes */
1030e42051d2SShaoyun Liu 		if (atomic_inc_return(&kfd_locked) == 1)
103126103436SFelix Kuehling 			kfd_suspend_all_processes();
10329593f4d6SRajneesh Bhardwaj 	}
103326103436SFelix Kuehling 
103445c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
103564d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
10364a488a7aSOded Gabbay }
10374a488a7aSOded Gabbay 
10389593f4d6SRajneesh Bhardwaj int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm)
10394a488a7aSOded Gabbay {
104026103436SFelix Kuehling 	int ret, count;
104126103436SFelix Kuehling 
1042b8935a7cSYong Zhao 	if (!kfd->init_complete)
1043b8935a7cSYong Zhao 		return 0;
1044b17f068aSOded Gabbay 
104526103436SFelix Kuehling 	ret = kfd_resume(kfd);
104626103436SFelix Kuehling 	if (ret)
104726103436SFelix Kuehling 		return ret;
1048b17f068aSOded Gabbay 
10499593f4d6SRajneesh Bhardwaj 	/* for runtime resume, skip unlocking kfd */
10509593f4d6SRajneesh Bhardwaj 	if (!run_pm) {
1051e42051d2SShaoyun Liu 		count = atomic_dec_return(&kfd_locked);
105226103436SFelix Kuehling 		WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
105326103436SFelix Kuehling 		if (count == 0)
105426103436SFelix Kuehling 			ret = kfd_resume_all_processes();
10559593f4d6SRajneesh Bhardwaj 	}
105626103436SFelix Kuehling 
105726103436SFelix Kuehling 	return ret;
10584ebc7182SYong Zhao }
10594ebc7182SYong Zhao 
1060b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
1061b8935a7cSYong Zhao {
1062b8935a7cSYong Zhao 	int err = 0;
1063b8935a7cSYong Zhao 
106464d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
106564d1c3a4SFelix Kuehling 	if (err) {
106664d1c3a4SFelix Kuehling 		dev_err(kfd_device,
106764d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
106864d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
106964d1c3a4SFelix Kuehling 		return err;
107064d1c3a4SFelix Kuehling 	}
1071733fa1f7SYong Zhao 
1072b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
1073b8935a7cSYong Zhao 	if (err) {
1074b8935a7cSYong Zhao 		dev_err(kfd_device,
1075b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
1076b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
1077b8935a7cSYong Zhao 		goto dqm_start_error;
1078b17f068aSOded Gabbay 	}
1079b17f068aSOded Gabbay 
1080b8935a7cSYong Zhao 	return err;
1081b8935a7cSYong Zhao 
1082b8935a7cSYong Zhao dqm_start_error:
108364d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
1084b8935a7cSYong Zhao 	return err;
10854a488a7aSOded Gabbay }
10864a488a7aSOded Gabbay 
1087b3eca59dSPhilip Yang static inline void kfd_queue_work(struct workqueue_struct *wq,
1088b3eca59dSPhilip Yang 				  struct work_struct *work)
1089b3eca59dSPhilip Yang {
1090b3eca59dSPhilip Yang 	int cpu, new_cpu;
1091b3eca59dSPhilip Yang 
1092b3eca59dSPhilip Yang 	cpu = new_cpu = smp_processor_id();
1093b3eca59dSPhilip Yang 	do {
1094b3eca59dSPhilip Yang 		new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
1095b3eca59dSPhilip Yang 		if (cpu_to_node(new_cpu) == numa_node_id())
1096b3eca59dSPhilip Yang 			break;
1097b3eca59dSPhilip Yang 	} while (cpu != new_cpu);
1098b3eca59dSPhilip Yang 
1099b3eca59dSPhilip Yang 	queue_work_on(new_cpu, wq, work);
1100b3eca59dSPhilip Yang }
1101b3eca59dSPhilip Yang 
1102b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
1103b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
11044a488a7aSOded Gabbay {
110558e69886SLan Xiao 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
110658e69886SLan Xiao 	bool is_patched = false;
11072383a767SChristian König 	unsigned long flags;
110858e69886SLan Xiao 
11092249d558SAndrew Lewycky 	if (!kfd->init_complete)
11102249d558SAndrew Lewycky 		return;
11112249d558SAndrew Lewycky 
111258e69886SLan Xiao 	if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
111358e69886SLan Xiao 		dev_err_once(kfd_device, "Ring entry too small\n");
111458e69886SLan Xiao 		return;
111558e69886SLan Xiao 	}
111658e69886SLan Xiao 
11172383a767SChristian König 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
11182249d558SAndrew Lewycky 
11192249d558SAndrew Lewycky 	if (kfd->interrupts_active
112058e69886SLan Xiao 	    && interrupt_is_wanted(kfd, ih_ring_entry,
112158e69886SLan Xiao 				   patched_ihre, &is_patched)
112258e69886SLan Xiao 	    && enqueue_ih_ring_entry(kfd,
112358e69886SLan Xiao 				     is_patched ? patched_ihre : ih_ring_entry))
1124b3eca59dSPhilip Yang 		kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work);
11252249d558SAndrew Lewycky 
11262383a767SChristian König 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
11274a488a7aSOded Gabbay }
11286e81090bSOded Gabbay 
11296b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
11306b95e797SFelix Kuehling {
11316b95e797SFelix Kuehling 	struct kfd_process *p;
11326b95e797SFelix Kuehling 	int r;
11336b95e797SFelix Kuehling 
11346b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
11356b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
11366b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
11376b95e797SFelix Kuehling 	 */
11386b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
11396b95e797SFelix Kuehling 	if (!p)
11406b95e797SFelix Kuehling 		return -ESRCH;
11416b95e797SFelix Kuehling 
1142b2057956SFelix Kuehling 	WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
11436b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
11446b95e797SFelix Kuehling 
11456b95e797SFelix Kuehling 	kfd_unref_process(p);
11466b95e797SFelix Kuehling 	return r;
11476b95e797SFelix Kuehling }
11486b95e797SFelix Kuehling 
11496b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
11506b95e797SFelix Kuehling {
11516b95e797SFelix Kuehling 	struct kfd_process *p;
11526b95e797SFelix Kuehling 	int r;
11536b95e797SFelix Kuehling 
11546b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
11556b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
11566b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
11576b95e797SFelix Kuehling 	 */
11586b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
11596b95e797SFelix Kuehling 	if (!p)
11606b95e797SFelix Kuehling 		return -ESRCH;
11616b95e797SFelix Kuehling 
11626b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
11636b95e797SFelix Kuehling 
11646b95e797SFelix Kuehling 	kfd_unref_process(p);
11656b95e797SFelix Kuehling 	return r;
11666b95e797SFelix Kuehling }
11676b95e797SFelix Kuehling 
116826103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
116926103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
117026103436SFelix Kuehling  *   process.
117126103436SFelix Kuehling  *
117226103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
117326103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
117426103436SFelix Kuehling  *
117526103436SFelix Kuehling  */
117626103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
117726103436SFelix Kuehling 					       struct dma_fence *fence)
117826103436SFelix Kuehling {
117926103436SFelix Kuehling 	struct kfd_process *p;
118026103436SFelix Kuehling 	unsigned long active_time;
118126103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
118226103436SFelix Kuehling 
118326103436SFelix Kuehling 	if (!fence)
118426103436SFelix Kuehling 		return -EINVAL;
118526103436SFelix Kuehling 
118626103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
118726103436SFelix Kuehling 		return 0;
118826103436SFelix Kuehling 
118926103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
119026103436SFelix Kuehling 	if (!p)
119126103436SFelix Kuehling 		return -ENODEV;
119226103436SFelix Kuehling 
119326103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
119426103436SFelix Kuehling 		goto out;
119526103436SFelix Kuehling 
119626103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
119726103436SFelix Kuehling 
119826103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
119926103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
120026103436SFelix Kuehling 	 */
120126103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
120226103436SFelix Kuehling 	if (delay_jiffies > active_time)
120326103436SFelix Kuehling 		delay_jiffies -= active_time;
120426103436SFelix Kuehling 	else
120526103436SFelix Kuehling 		delay_jiffies = 0;
120626103436SFelix Kuehling 
120726103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
120826103436SFelix Kuehling 	 * to kfd_evict_bo_worker
120926103436SFelix Kuehling 	 */
1210b2057956SFelix Kuehling 	WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",
1211b2057956SFelix Kuehling 	     p->lead_thread->pid, delay_jiffies);
121226103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
121326103436SFelix Kuehling out:
121426103436SFelix Kuehling 	kfd_unref_process(p);
121526103436SFelix Kuehling 	return 0;
121626103436SFelix Kuehling }
121726103436SFelix Kuehling 
12186e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
12196e81090bSOded Gabbay 				unsigned int chunk_size)
12206e81090bSOded Gabbay {
12218625ff9cSFelix Kuehling 	unsigned int num_of_longs;
12226e81090bSOded Gabbay 
122332fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
122432fa8219SFelix Kuehling 		return -EINVAL;
122532fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
122632fa8219SFelix Kuehling 		return -EINVAL;
122732fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
122832fa8219SFelix Kuehling 		return -EINVAL;
12296e81090bSOded Gabbay 
12306e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
12316e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
12326e81090bSOded Gabbay 
12338625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
12348625ff9cSFelix Kuehling 		BITS_PER_LONG;
12356e81090bSOded Gabbay 
12368625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
12376e81090bSOded Gabbay 
12386e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
12396e81090bSOded Gabbay 		return -ENOMEM;
12406e81090bSOded Gabbay 
124179775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
12426e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
12436e81090bSOded Gabbay 
12446e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
12456e81090bSOded Gabbay 
12466e81090bSOded Gabbay 	return 0;
12476e81090bSOded Gabbay 
12486e81090bSOded Gabbay }
12496e81090bSOded Gabbay 
12506e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
12516e81090bSOded Gabbay {
12526e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
12536e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
12546e81090bSOded Gabbay }
12556e81090bSOded Gabbay 
12566e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
12576e81090bSOded Gabbay 						unsigned int bit_num,
12586e81090bSOded Gabbay 						unsigned int chunk_size)
12596e81090bSOded Gabbay {
12606e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
12616e81090bSOded Gabbay }
12626e81090bSOded Gabbay 
12636e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
12646e81090bSOded Gabbay 						unsigned int bit_num,
12656e81090bSOded Gabbay 						unsigned int chunk_size)
12666e81090bSOded Gabbay {
12676e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
12686e81090bSOded Gabbay }
12696e81090bSOded Gabbay 
12706e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
12716e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
12726e81090bSOded Gabbay {
12736e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
12746e81090bSOded Gabbay 
12756e81090bSOded Gabbay 	if (size == 0)
12766e81090bSOded Gabbay 		return -EINVAL;
12776e81090bSOded Gabbay 
12786e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
12796e81090bSOded Gabbay 		return -ENOMEM;
12806e81090bSOded Gabbay 
12811cd106ecSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
12821cd106ecSFelix Kuehling 	if (!(*mem_obj))
12836e81090bSOded Gabbay 		return -ENOMEM;
12846e81090bSOded Gabbay 
128579775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
12866e81090bSOded Gabbay 
12876e81090bSOded Gabbay 	start_search = 0;
12886e81090bSOded Gabbay 
12896e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
12906e81090bSOded Gabbay 
12916e81090bSOded Gabbay kfd_gtt_restart_search:
12926e81090bSOded Gabbay 	/* Find the first chunk that is free */
12936e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
12946e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
12956e81090bSOded Gabbay 					start_search);
12966e81090bSOded Gabbay 
129779775b62SKent Russell 	pr_debug("Found = %d\n", found);
12986e81090bSOded Gabbay 
12996e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
13006e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
13016e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
13026e81090bSOded Gabbay 
13036e81090bSOded Gabbay 	/* Update fields of mem_obj */
13046e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
13056e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
13066e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
13076e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
13086e81090bSOded Gabbay 					found,
13096e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
13106e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
13116e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
13126e81090bSOded Gabbay 					found,
13136e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
13146e81090bSOded Gabbay 
131579775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
13166e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
13176e81090bSOded Gabbay 
13186e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
13196e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
132079775b62SKent Russell 		pr_debug("Single bit\n");
13216e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
13226e81090bSOded Gabbay 		goto kfd_gtt_out;
13236e81090bSOded Gabbay 	}
13246e81090bSOded Gabbay 
13256e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
13266e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
13276e81090bSOded Gabbay 	do {
13286e81090bSOded Gabbay 		(*mem_obj)->range_end =
13296e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
13306e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
13316e81090bSOded Gabbay 		/*
13326e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
13336e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
13346e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
13356e81090bSOded Gabbay 		 */
13366e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
13376e81090bSOded Gabbay 			start_search = found;
13386e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
13396e81090bSOded Gabbay 		}
13406e81090bSOded Gabbay 
13416e81090bSOded Gabbay 		/*
13426e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
13436e81090bSOded Gabbay 		 */
13446e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
13456e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
13466e81090bSOded Gabbay 
13476e81090bSOded Gabbay 		/* Check if we don't need another chunk */
13486e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
13496e81090bSOded Gabbay 			cur_size = 0;
13506e81090bSOded Gabbay 		else
13516e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
13526e81090bSOded Gabbay 
13536e81090bSOded Gabbay 	} while (cur_size > 0);
13546e81090bSOded Gabbay 
135579775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
13566e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
13576e81090bSOded Gabbay 
13586e81090bSOded Gabbay 	/* Mark the chunks as allocated */
13596e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
13606e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
13616e81090bSOded Gabbay 		found++)
13626e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
13636e81090bSOded Gabbay 
13646e81090bSOded Gabbay kfd_gtt_out:
13656e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
13666e81090bSOded Gabbay 	return 0;
13676e81090bSOded Gabbay 
13686e81090bSOded Gabbay kfd_gtt_no_free_chunk:
13693148a6a0SJack Zhang 	pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);
13706e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
13713148a6a0SJack Zhang 	kfree(*mem_obj);
13726e81090bSOded Gabbay 	return -ENOMEM;
13736e81090bSOded Gabbay }
13746e81090bSOded Gabbay 
13756e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
13766e81090bSOded Gabbay {
13776e81090bSOded Gabbay 	unsigned int bit;
13786e81090bSOded Gabbay 
13799216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
13809216ed29SOded Gabbay 	if (!mem_obj)
13819216ed29SOded Gabbay 		return 0;
13826e81090bSOded Gabbay 
138379775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
13846e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
13856e81090bSOded Gabbay 
13866e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
13876e81090bSOded Gabbay 
13886e81090bSOded Gabbay 	/* Mark the chunks as free */
13896e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
13906e81090bSOded Gabbay 		bit <= mem_obj->range_end;
13916e81090bSOded Gabbay 		bit++)
13926e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
13936e81090bSOded Gabbay 
13946e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
13956e81090bSOded Gabbay 
13966e81090bSOded Gabbay 	kfree(mem_obj);
13976e81090bSOded Gabbay 	return 0;
13986e81090bSOded Gabbay }
1399a29ec470SShaoyun Liu 
14009b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
14019b54d201SEric Huang {
14029b54d201SEric Huang 	if (kfd)
14039b54d201SEric Huang 		atomic_inc(&kfd->sram_ecc_flag);
14049b54d201SEric Huang }
14059b54d201SEric Huang 
140643d8107fSHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd)
140743d8107fSHarish Kasiviswanathan {
140843d8107fSHarish Kasiviswanathan 	if (atomic_inc_return(&kfd->compute_profile) == 1)
140943d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, false);
141043d8107fSHarish Kasiviswanathan }
141143d8107fSHarish Kasiviswanathan 
141243d8107fSHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd)
141343d8107fSHarish Kasiviswanathan {
141443d8107fSHarish Kasiviswanathan 	int count = atomic_dec_return(&kfd->compute_profile);
141543d8107fSHarish Kasiviswanathan 
141643d8107fSHarish Kasiviswanathan 	if (count == 0)
141743d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, true);
141843d8107fSHarish Kasiviswanathan 	WARN_ONCE(count < 0, "Compute profile ref. count error");
141943d8107fSHarish Kasiviswanathan }
142043d8107fSHarish Kasiviswanathan 
1421410e302eSGraham Sider void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask)
14222c2b0d88SMukul Joshi {
1423158fc08dSAmber Lin 	if (kfd && kfd->init_complete)
14242c2b0d88SMukul Joshi 		kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask);
14252c2b0d88SMukul Joshi }
14262c2b0d88SMukul Joshi 
1427a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
1428a29ec470SShaoyun Liu 
1429a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
1430a29ec470SShaoyun Liu  * which will trigger a GPU reset and bring the HWS back to normal state
1431a29ec470SShaoyun Liu  */
1432a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1433a29ec470SShaoyun Liu {
1434a29ec470SShaoyun Liu 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1435a29ec470SShaoyun Liu 		pr_err("HWS is not enabled");
1436a29ec470SShaoyun Liu 		return -EINVAL;
1437a29ec470SShaoyun Liu 	}
1438a29ec470SShaoyun Liu 
14394f942aaeSOak Zeng 	return dqm_debugfs_hang_hws(dev->dqm);
1440a29ec470SShaoyun Liu }
1441a29ec470SShaoyun Liu 
1442a29ec470SShaoyun Liu #endif
1443