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",
949d6fa9c7SGraham 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",
1149d6fa9c7SGraham 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",
1359d6fa9c7SGraham 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",
1549d6fa9c7SGraham 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",
1749d6fa9c7SGraham 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",
1939d6fa9c7SGraham 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",
2129d6fa9c7SGraham 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",
2329d6fa9c7SGraham 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",
2519d6fa9c7SGraham 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",
2709d6fa9c7SGraham 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",
2899d6fa9c7SGraham 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",
3089d6fa9c7SGraham 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",
3279d6fa9c7SGraham 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",
3469d6fa9c7SGraham 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",
3659d6fa9c7SGraham 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",
3849d6fa9c7SGraham 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",
4039d6fa9c7SGraham 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",
4229d6fa9c7SGraham 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",
4419d6fa9c7SGraham 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",
4609d6fa9c7SGraham 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,
471e312af6cSFelix Kuehling 	.no_atomic_fw_version = 145,
47214328aa5SPhilip Cox 	.num_sdma_engines = 2,
47314328aa5SPhilip Cox 	.num_xgmi_sdma_engines = 0,
47414328aa5SPhilip Cox 	.num_sdma_queues_per_engine = 8,
47514328aa5SPhilip Cox };
47614328aa5SPhilip Cox 
477b77fb9d8Sshaoyunl static const struct kfd_device_info navi12_device_info = {
4780e94b564Sshaoyunl 	.asic_family = CHIP_NAVI12,
479b77fb9d8Sshaoyunl 	.asic_name = "navi12",
4809d6fa9c7SGraham Sider 	.gfx_target_version = 100101,
481b77fb9d8Sshaoyunl 	.max_pasid_bits = 16,
482b77fb9d8Sshaoyunl 	.max_no_of_hqd  = 24,
483b77fb9d8Sshaoyunl 	.doorbell_size  = 8,
484b77fb9d8Sshaoyunl 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
485b77fb9d8Sshaoyunl 	.event_interrupt_class = &event_interrupt_class_v9,
486b77fb9d8Sshaoyunl 	.num_of_watch_points = 4,
487b77fb9d8Sshaoyunl 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
488b77fb9d8Sshaoyunl 	.needs_iommu_device = false,
489b77fb9d8Sshaoyunl 	.supports_cwsr = true,
4906cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
491e312af6cSFelix Kuehling 	.no_atomic_fw_version = 145,
492b77fb9d8Sshaoyunl 	.num_sdma_engines = 2,
493b77fb9d8Sshaoyunl 	.num_xgmi_sdma_engines = 0,
494b77fb9d8Sshaoyunl 	.num_sdma_queues_per_engine = 8,
495b77fb9d8Sshaoyunl };
496b77fb9d8Sshaoyunl 
4978099ae40SYong Zhao static const struct kfd_device_info navi14_device_info = {
4988099ae40SYong Zhao 	.asic_family = CHIP_NAVI14,
4998099ae40SYong Zhao 	.asic_name = "navi14",
5009d6fa9c7SGraham Sider 	.gfx_target_version = 100102,
5018099ae40SYong Zhao 	.max_pasid_bits = 16,
5028099ae40SYong Zhao 	.max_no_of_hqd  = 24,
5038099ae40SYong Zhao 	.doorbell_size  = 8,
5048099ae40SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
5058099ae40SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
5068099ae40SYong Zhao 	.num_of_watch_points = 4,
5078099ae40SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
5088099ae40SYong Zhao 	.needs_iommu_device = false,
5098099ae40SYong Zhao 	.supports_cwsr = true,
5106cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
511e312af6cSFelix Kuehling 	.no_atomic_fw_version = 145,
5128099ae40SYong Zhao 	.num_sdma_engines = 2,
5138099ae40SYong Zhao 	.num_xgmi_sdma_engines = 0,
5148099ae40SYong Zhao 	.num_sdma_queues_per_engine = 8,
5158099ae40SYong Zhao };
5168099ae40SYong Zhao 
5173a2f0c81SYong Zhao static const struct kfd_device_info sienna_cichlid_device_info = {
5183a2f0c81SYong Zhao 	.asic_family = CHIP_SIENNA_CICHLID,
5193a2f0c81SYong Zhao 	.asic_name = "sienna_cichlid",
5209d6fa9c7SGraham Sider 	.gfx_target_version = 100300,
5213a2f0c81SYong Zhao 	.max_pasid_bits = 16,
5223a2f0c81SYong Zhao 	.max_no_of_hqd  = 24,
5233a2f0c81SYong Zhao 	.doorbell_size  = 8,
5243a2f0c81SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
5253a2f0c81SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
5263a2f0c81SYong Zhao 	.num_of_watch_points = 4,
5273a2f0c81SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
5283a2f0c81SYong Zhao 	.needs_iommu_device = false,
5293a2f0c81SYong Zhao 	.supports_cwsr = true,
5306cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
531e312af6cSFelix Kuehling 	.no_atomic_fw_version = 92,
5323a2f0c81SYong Zhao 	.num_sdma_engines = 4,
5333a2f0c81SYong Zhao 	.num_xgmi_sdma_engines = 0,
5343a2f0c81SYong Zhao 	.num_sdma_queues_per_engine = 8,
5353a2f0c81SYong Zhao };
5363a2f0c81SYong Zhao 
537de89b2e4SChengming Gui static const struct kfd_device_info navy_flounder_device_info = {
538de89b2e4SChengming Gui 	.asic_family = CHIP_NAVY_FLOUNDER,
539de89b2e4SChengming Gui 	.asic_name = "navy_flounder",
5409d6fa9c7SGraham Sider 	.gfx_target_version = 100301,
541de89b2e4SChengming Gui 	.max_pasid_bits = 16,
542de89b2e4SChengming Gui 	.max_no_of_hqd  = 24,
543de89b2e4SChengming Gui 	.doorbell_size  = 8,
544de89b2e4SChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
545de89b2e4SChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
546de89b2e4SChengming Gui 	.num_of_watch_points = 4,
547de89b2e4SChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
548de89b2e4SChengming Gui 	.needs_iommu_device = false,
549de89b2e4SChengming Gui 	.supports_cwsr = true,
5506cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
551e312af6cSFelix Kuehling 	.no_atomic_fw_version = 92,
552de89b2e4SChengming Gui 	.num_sdma_engines = 2,
553de89b2e4SChengming Gui 	.num_xgmi_sdma_engines = 0,
554de89b2e4SChengming Gui 	.num_sdma_queues_per_engine = 8,
555de89b2e4SChengming Gui };
556de89b2e4SChengming Gui 
5573a5e715dSHuang Rui static const struct kfd_device_info vangogh_device_info = {
5583a5e715dSHuang Rui 	.asic_family = CHIP_VANGOGH,
5593a5e715dSHuang Rui 	.asic_name = "vangogh",
5609d6fa9c7SGraham Sider 	.gfx_target_version = 100303,
5613a5e715dSHuang Rui 	.max_pasid_bits = 16,
5623a5e715dSHuang Rui 	.max_no_of_hqd  = 24,
5633a5e715dSHuang Rui 	.doorbell_size  = 8,
5643a5e715dSHuang Rui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
5653a5e715dSHuang Rui 	.event_interrupt_class = &event_interrupt_class_v9,
5663a5e715dSHuang Rui 	.num_of_watch_points = 4,
5673a5e715dSHuang Rui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
5683a5e715dSHuang Rui 	.needs_iommu_device = false,
5693a5e715dSHuang Rui 	.supports_cwsr = true,
570e312af6cSFelix Kuehling 	.needs_pci_atomics = true,
571e312af6cSFelix Kuehling 	.no_atomic_fw_version = 92,
5723a5e715dSHuang Rui 	.num_sdma_engines = 1,
5733a5e715dSHuang Rui 	.num_xgmi_sdma_engines = 0,
5743a5e715dSHuang Rui 	.num_sdma_queues_per_engine = 2,
5753a5e715dSHuang Rui };
5763a5e715dSHuang Rui 
577eb5a34d4SChengming Gui static const struct kfd_device_info dimgrey_cavefish_device_info = {
578eb5a34d4SChengming Gui 	.asic_family = CHIP_DIMGREY_CAVEFISH,
579eb5a34d4SChengming Gui 	.asic_name = "dimgrey_cavefish",
5809d6fa9c7SGraham Sider 	.gfx_target_version = 100302,
581eb5a34d4SChengming Gui 	.max_pasid_bits = 16,
582eb5a34d4SChengming Gui 	.max_no_of_hqd  = 24,
583eb5a34d4SChengming Gui 	.doorbell_size  = 8,
584eb5a34d4SChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
585eb5a34d4SChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
586eb5a34d4SChengming Gui 	.num_of_watch_points = 4,
587eb5a34d4SChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
588eb5a34d4SChengming Gui 	.needs_iommu_device = false,
589eb5a34d4SChengming Gui 	.supports_cwsr = true,
5906cc980e3SHarish Kasiviswanathan 	.needs_pci_atomics = true,
591e312af6cSFelix Kuehling 	.no_atomic_fw_version = 92,
592eb5a34d4SChengming Gui 	.num_sdma_engines = 2,
593eb5a34d4SChengming Gui 	.num_xgmi_sdma_engines = 0,
594eb5a34d4SChengming Gui 	.num_sdma_queues_per_engine = 8,
595eb5a34d4SChengming Gui };
596eb5a34d4SChengming Gui 
5975cf607ccSChengming Gui static const struct kfd_device_info beige_goby_device_info = {
5985cf607ccSChengming Gui 	.asic_family = CHIP_BEIGE_GOBY,
5995cf607ccSChengming Gui 	.asic_name = "beige_goby",
6009d6fa9c7SGraham Sider 	.gfx_target_version = 100304,
6015cf607ccSChengming Gui 	.max_pasid_bits = 16,
6025cf607ccSChengming Gui 	.max_no_of_hqd  = 24,
6035cf607ccSChengming Gui 	.doorbell_size  = 8,
6045cf607ccSChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
6055cf607ccSChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
6065cf607ccSChengming Gui 	.num_of_watch_points = 4,
6075cf607ccSChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
6085cf607ccSChengming Gui 	.needs_iommu_device = false,
6095cf607ccSChengming Gui 	.supports_cwsr = true,
6105cf607ccSChengming Gui 	.needs_pci_atomics = true,
611e312af6cSFelix Kuehling 	.no_atomic_fw_version = 92,
6125cf607ccSChengming Gui 	.num_sdma_engines = 1,
6135cf607ccSChengming Gui 	.num_xgmi_sdma_engines = 0,
6145cf607ccSChengming Gui 	.num_sdma_queues_per_engine = 8,
6155cf607ccSChengming Gui };
6165cf607ccSChengming Gui 
617bf9d4e88SAaron Liu static const struct kfd_device_info yellow_carp_device_info = {
618bf9d4e88SAaron Liu 	.asic_family = CHIP_YELLOW_CARP,
619bf9d4e88SAaron Liu 	.asic_name = "yellow_carp",
6209d6fa9c7SGraham Sider 	.gfx_target_version = 100305,
621bf9d4e88SAaron Liu 	.max_pasid_bits = 16,
622bf9d4e88SAaron Liu 	.max_no_of_hqd  = 24,
623bf9d4e88SAaron Liu 	.doorbell_size  = 8,
624bf9d4e88SAaron Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
625bf9d4e88SAaron Liu 	.event_interrupt_class = &event_interrupt_class_v9,
626bf9d4e88SAaron Liu 	.num_of_watch_points = 4,
627bf9d4e88SAaron Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
628bf9d4e88SAaron Liu 	.needs_iommu_device = false,
629bf9d4e88SAaron Liu 	.supports_cwsr = true,
630e312af6cSFelix Kuehling 	.needs_pci_atomics = true,
631e312af6cSFelix Kuehling 	.no_atomic_fw_version = 92,
632bf9d4e88SAaron Liu 	.num_sdma_engines = 1,
633bf9d4e88SAaron Liu 	.num_xgmi_sdma_engines = 0,
634bf9d4e88SAaron Liu 	.num_sdma_queues_per_engine = 2,
635bf9d4e88SAaron Liu };
636eb5a34d4SChengming Gui 
63706e75b88STao Zhou static const struct kfd_device_info cyan_skillfish_device_info = {
63806e75b88STao Zhou 	.asic_family = CHIP_CYAN_SKILLFISH,
63906e75b88STao Zhou 	.asic_name = "cyan_skillfish",
6409d6fa9c7SGraham Sider 	.gfx_target_version = 100103,
64106e75b88STao Zhou 	.max_pasid_bits = 16,
64206e75b88STao Zhou 	.max_no_of_hqd  = 24,
64306e75b88STao Zhou 	.doorbell_size  = 8,
64406e75b88STao Zhou 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
64506e75b88STao Zhou 	.event_interrupt_class = &event_interrupt_class_v9,
64606e75b88STao Zhou 	.num_of_watch_points = 4,
64706e75b88STao Zhou 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
64806e75b88STao Zhou 	.needs_iommu_device = false,
64906e75b88STao Zhou 	.supports_cwsr = true,
65006e75b88STao Zhou 	.needs_pci_atomics = true,
65106e75b88STao Zhou 	.num_sdma_engines = 2,
65206e75b88STao Zhou 	.num_xgmi_sdma_engines = 0,
65306e75b88STao Zhou 	.num_sdma_queues_per_engine = 8,
65406e75b88STao Zhou };
65506e75b88STao Zhou 
656050091abSYong Zhao /* For each entry, [0] is regular and [1] is virtualisation device. */
657050091abSYong Zhao static const struct kfd_device_info *kfd_supported_devices[][2] = {
65895a5bd1bSYong Zhao #ifdef KFD_SUPPORT_IOMMU_V2
659050091abSYong Zhao 	[CHIP_KAVERI] = {&kaveri_device_info, NULL},
66095a5bd1bSYong Zhao 	[CHIP_CARRIZO] = {&carrizo_device_info, NULL},
66195a5bd1bSYong Zhao #endif
6622b3bbf23SYueHaibing 	[CHIP_RAVEN] = {&raven_device_info, NULL},
663050091abSYong Zhao 	[CHIP_HAWAII] = {&hawaii_device_info, NULL},
664050091abSYong Zhao 	[CHIP_TONGA] = {&tonga_device_info, NULL},
665050091abSYong Zhao 	[CHIP_FIJI] = {&fiji_device_info, &fiji_vf_device_info},
666050091abSYong Zhao 	[CHIP_POLARIS10] = {&polaris10_device_info, &polaris10_vf_device_info},
667050091abSYong Zhao 	[CHIP_POLARIS11] = {&polaris11_device_info, NULL},
668050091abSYong Zhao 	[CHIP_POLARIS12] = {&polaris12_device_info, NULL},
669050091abSYong Zhao 	[CHIP_VEGAM] = {&vegam_device_info, NULL},
670050091abSYong Zhao 	[CHIP_VEGA10] = {&vega10_device_info, &vega10_vf_device_info},
671050091abSYong Zhao 	[CHIP_VEGA12] = {&vega12_device_info, NULL},
672050091abSYong Zhao 	[CHIP_VEGA20] = {&vega20_device_info, NULL},
6732b9c2211SHuang Rui 	[CHIP_RENOIR] = {&renoir_device_info, NULL},
674050091abSYong Zhao 	[CHIP_ARCTURUS] = {&arcturus_device_info, &arcturus_device_info},
675cecd91b4SZhigang Luo 	[CHIP_ALDEBARAN] = {&aldebaran_device_info, &aldebaran_device_info},
676050091abSYong Zhao 	[CHIP_NAVI10] = {&navi10_device_info, NULL},
677b77fb9d8Sshaoyunl 	[CHIP_NAVI12] = {&navi12_device_info, &navi12_device_info},
6788099ae40SYong Zhao 	[CHIP_NAVI14] = {&navi14_device_info, NULL},
679adab4dadSshaoyunl 	[CHIP_SIENNA_CICHLID] = {&sienna_cichlid_device_info, &sienna_cichlid_device_info},
680de89b2e4SChengming Gui 	[CHIP_NAVY_FLOUNDER] = {&navy_flounder_device_info, &navy_flounder_device_info},
6813a5e715dSHuang Rui 	[CHIP_VANGOGH] = {&vangogh_device_info, NULL},
682eb5a34d4SChengming Gui 	[CHIP_DIMGREY_CAVEFISH] = {&dimgrey_cavefish_device_info, &dimgrey_cavefish_device_info},
6835cf607ccSChengming Gui 	[CHIP_BEIGE_GOBY] = {&beige_goby_device_info, &beige_goby_device_info},
684bf9d4e88SAaron Liu 	[CHIP_YELLOW_CARP] = {&yellow_carp_device_info, NULL},
68506e75b88STao Zhou 	[CHIP_CYAN_SKILLFISH] = {&cyan_skillfish_device_info, NULL},
6864a488a7aSOded Gabbay };
6874a488a7aSOded Gabbay 
6886e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
6896e81090bSOded Gabbay 				unsigned int chunk_size);
6906e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
6916e81090bSOded Gabbay 
692b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
693b8935a7cSYong Zhao 
694cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
695e392c887SYong Zhao 	struct pci_dev *pdev, unsigned int asic_type, bool vf)
6964a488a7aSOded Gabbay {
6974a488a7aSOded Gabbay 	struct kfd_dev *kfd;
698050091abSYong Zhao 	const struct kfd_device_info *device_info;
699e392c887SYong Zhao 	const struct kfd2kgd_calls *f2g;
700050091abSYong Zhao 
701e392c887SYong Zhao 	if (asic_type >= sizeof(kfd_supported_devices) / (sizeof(void *) * 2)
702e392c887SYong Zhao 		|| asic_type >= sizeof(kfd2kgd_funcs) / sizeof(void *)) {
703050091abSYong Zhao 		dev_err(kfd_device, "asic_type %d out of range\n", asic_type);
704050091abSYong Zhao 		return NULL; /* asic_type out of range */
705050091abSYong Zhao 	}
706050091abSYong Zhao 
707050091abSYong Zhao 	device_info = kfd_supported_devices[asic_type][vf];
708e392c887SYong Zhao 	f2g = kfd2kgd_funcs[asic_type];
7094a488a7aSOded Gabbay 
710aa5e899dSDan Carpenter 	if (!device_info || !f2g) {
711050091abSYong Zhao 		dev_err(kfd_device, "%s %s not supported in kfd\n",
712050091abSYong Zhao 			amdgpu_asic_name[asic_type], vf ? "VF" : "");
7134a488a7aSOded Gabbay 		return NULL;
7144ebc7182SYong Zhao 	}
7154a488a7aSOded Gabbay 
716d35f00d8SEric Huang 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
717d35f00d8SEric Huang 	if (!kfd)
718d35f00d8SEric Huang 		return NULL;
719d35f00d8SEric Huang 
7204a488a7aSOded Gabbay 	kfd->kgd = kgd;
7214a488a7aSOded Gabbay 	kfd->device_info = device_info;
7224a488a7aSOded Gabbay 	kfd->pdev = pdev;
72319f6d2a6SOded Gabbay 	kfd->init_complete = false;
724cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
72543d8107fSHarish Kasiviswanathan 	atomic_set(&kfd->compute_profile, 0);
726cea405b1SXihan Zhang 
727cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
728cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
729cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
7304a488a7aSOded Gabbay 
7319b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
7329b54d201SEric Huang 
73359d7115dSMukul Joshi 	ida_init(&kfd->doorbell_ida);
73459d7115dSMukul Joshi 
7354a488a7aSOded Gabbay 	return kfd;
7364a488a7aSOded Gabbay }
7374a488a7aSOded Gabbay 
738373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
739373d7080SFelix Kuehling {
740373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
7413e76c239SFelix Kuehling 		if (kfd->device_info->asic_family < CHIP_VEGA10) {
742373d7080SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
743373d7080SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
744373d7080SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
7450ef6845cSJay Cornwall 		} else if (kfd->device_info->asic_family == CHIP_ARCTURUS) {
7463baa24f0SOak Zeng 			BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
7473baa24f0SOak Zeng 			kfd->cwsr_isa = cwsr_trap_arcturus_hex;
7483baa24f0SOak Zeng 			kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
7490ef6845cSJay Cornwall 		} else if (kfd->device_info->asic_family == CHIP_ALDEBARAN) {
7500ef6845cSJay Cornwall 			BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex) > PAGE_SIZE);
7510ef6845cSJay Cornwall 			kfd->cwsr_isa = cwsr_trap_aldebaran_hex;
7520ef6845cSJay Cornwall 			kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex);
75314328aa5SPhilip Cox 		} else if (kfd->device_info->asic_family < CHIP_NAVI10) {
7543e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
7553e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
7563e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
75780b6cfedSJay Cornwall 		} else if (kfd->device_info->asic_family < CHIP_SIENNA_CICHLID) {
75880b6cfedSJay Cornwall 			BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE);
75980b6cfedSJay Cornwall 			kfd->cwsr_isa = cwsr_trap_nv1x_hex;
76080b6cfedSJay Cornwall 			kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);
76114328aa5SPhilip Cox 		} else {
76214328aa5SPhilip Cox 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
76314328aa5SPhilip Cox 			kfd->cwsr_isa = cwsr_trap_gfx10_hex;
76414328aa5SPhilip Cox 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
7653e76c239SFelix Kuehling 		}
7663e76c239SFelix Kuehling 
767373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
768373d7080SFelix Kuehling 	}
769373d7080SFelix Kuehling }
770373d7080SFelix Kuehling 
77129633d0eSJoseph Greathouse static int kfd_gws_init(struct kfd_dev *kfd)
77229633d0eSJoseph Greathouse {
77329633d0eSJoseph Greathouse 	int ret = 0;
77429633d0eSJoseph Greathouse 
77529633d0eSJoseph Greathouse 	if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
77629633d0eSJoseph Greathouse 		return 0;
77729633d0eSJoseph Greathouse 
77829633d0eSJoseph Greathouse 	if (hws_gws_support
779fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family == CHIP_VEGA10
780fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x81b3)
781fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family >= CHIP_VEGA12
78229633d0eSJoseph Greathouse 			&& kfd->device_info->asic_family <= CHIP_RAVEN
783fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x1b3)
784fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family == CHIP_ARCTURUS
7858baa6018SHarish Kasiviswanathan 			&& kfd->mec2_fw_version >= 0x30)
7868baa6018SHarish Kasiviswanathan 		|| (kfd->device_info->asic_family == CHIP_ALDEBARAN
7878baa6018SHarish Kasiviswanathan 			&& kfd->mec2_fw_version >= 0x28))
78829633d0eSJoseph Greathouse 		ret = amdgpu_amdkfd_alloc_gws(kfd->kgd,
78929633d0eSJoseph Greathouse 				amdgpu_amdkfd_get_num_gws(kfd->kgd), &kfd->gws);
79029633d0eSJoseph Greathouse 
79129633d0eSJoseph Greathouse 	return ret;
79229633d0eSJoseph Greathouse }
79329633d0eSJoseph Greathouse 
794938a0650SAmber Lin static void kfd_smi_init(struct kfd_dev *dev) {
795938a0650SAmber Lin 	INIT_LIST_HEAD(&dev->smi_clients);
796938a0650SAmber Lin 	spin_lock_init(&dev->smi_lock);
797938a0650SAmber Lin }
798938a0650SAmber Lin 
7994a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
8003a0c3423SHarish Kasiviswanathan 			 struct drm_device *ddev,
8014a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
8024a488a7aSOded Gabbay {
803fd6a440eSJonathan Kim 	unsigned int size, map_process_packet_size;
80419f6d2a6SOded Gabbay 
8053a0c3423SHarish Kasiviswanathan 	kfd->ddev = ddev;
8060da8b10eSAmber Lin 	kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
8075ade6c9cSFelix Kuehling 			KGD_ENGINE_MEC1);
80829633d0eSJoseph Greathouse 	kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
80929633d0eSJoseph Greathouse 			KGD_ENGINE_MEC2);
8100da8b10eSAmber Lin 	kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
8115ade6c9cSFelix Kuehling 			KGD_ENGINE_SDMA1);
8124a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
8134a488a7aSOded Gabbay 
81444008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
81544008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
81644008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
81744008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
81844008d7aSYong Zhao 
819e312af6cSFelix Kuehling 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
820e312af6cSFelix Kuehling 	 * 32 and 64-bit requests are possible and must be
821e312af6cSFelix Kuehling 	 * supported.
822e312af6cSFelix Kuehling 	 */
823e312af6cSFelix Kuehling 	kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kfd->kgd);
824e312af6cSFelix Kuehling 	if (!kfd->pci_atomic_requested &&
825e312af6cSFelix Kuehling 	    kfd->device_info->needs_pci_atomics &&
826e312af6cSFelix Kuehling 	    (!kfd->device_info->no_atomic_fw_version ||
827e312af6cSFelix Kuehling 	     kfd->mec_fw_version < kfd->device_info->no_atomic_fw_version)) {
828e312af6cSFelix Kuehling 		dev_info(kfd_device,
829e312af6cSFelix Kuehling 			 "skipped device %x:%x, PCI rejects atomics %d<%d\n",
830e312af6cSFelix Kuehling 			 kfd->pdev->vendor, kfd->pdev->device,
831e312af6cSFelix Kuehling 			 kfd->mec_fw_version,
832e312af6cSFelix Kuehling 			 kfd->device_info->no_atomic_fw_version);
833e312af6cSFelix Kuehling 		return false;
834e312af6cSFelix Kuehling 	}
835e312af6cSFelix Kuehling 
836a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
837a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
838a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
839a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
840a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
841a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
842a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
843a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
844a99c6d4fSFelix Kuehling 	} else
845a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
846a99c6d4fSFelix Kuehling 
84719f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
848b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
84919f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
85019f6d2a6SOded Gabbay 
851e18e794eSOded Gabbay 	/*
852e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
853e18e794eSOded Gabbay 	 * There can be only 2 packets at once
854e18e794eSOded Gabbay 	 */
855fd6a440eSJonathan Kim 	map_process_packet_size =
856fd6a440eSJonathan Kim 			kfd->device_info->asic_family == CHIP_ALDEBARAN ?
857fd6a440eSJonathan Kim 				sizeof(struct pm4_mes_map_process_aldebaran) :
858fd6a440eSJonathan Kim 					sizeof(struct pm4_mes_map_process);
859fd6a440eSJonathan Kim 	size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size +
860507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
861507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
862e18e794eSOded Gabbay 
863e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
864e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
865e18e794eSOded Gabbay 
866e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
86719f6d2a6SOded Gabbay 	size += 512 * 1024;
86819f6d2a6SOded Gabbay 
8697cd52c91SAmber Lin 	if (amdgpu_amdkfd_alloc_gtt_mem(
870cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
87115426dbbSYong Zhao 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
87215426dbbSYong Zhao 			false)) {
87379775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
874e09d4fc8SOak Zeng 		goto alloc_gtt_mem_failure;
87519f6d2a6SOded Gabbay 	}
87619f6d2a6SOded Gabbay 
87779775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
878e18e794eSOded Gabbay 
87973a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
88073a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
88179775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
88273a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
88373a1da0bSOded Gabbay 	}
88473a1da0bSOded Gabbay 
885735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
886735df2baSFelix Kuehling 		dev_err(kfd_device,
887735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
888735df2baSFelix Kuehling 		goto kfd_doorbell_error;
889735df2baSFelix Kuehling 	}
89019f6d2a6SOded Gabbay 
891332f6e1eSFelix Kuehling 	kfd->hive_id = amdgpu_amdkfd_get_hive_id(kfd->kgd);
8920c1690e3SShaoyun Liu 
8939b498efaSAlex Deucher 	kfd->noretry = amdgpu_amdkfd_get_noretry(kfd->kgd);
8949b498efaSAlex Deucher 
8952249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
89679775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
8972249d558SAndrew Lewycky 		goto kfd_interrupt_error;
8982249d558SAndrew Lewycky 	}
8992249d558SAndrew Lewycky 
90064c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
90164c7f8cfSBen Goz 	if (!kfd->dqm) {
90279775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
90364c7f8cfSBen Goz 		goto device_queue_manager_error;
90464c7f8cfSBen Goz 	}
90564c7f8cfSBen Goz 
90629633d0eSJoseph Greathouse 	/* If supported on this device, allocate global GWS that is shared
90729633d0eSJoseph Greathouse 	 * by all KFD processes
90829633d0eSJoseph Greathouse 	 */
90929633d0eSJoseph Greathouse 	if (kfd_gws_init(kfd)) {
91029633d0eSJoseph Greathouse 		dev_err(kfd_device, "Could not allocate %d gws\n",
91129633d0eSJoseph Greathouse 			amdgpu_amdkfd_get_num_gws(kfd->kgd));
91229633d0eSJoseph Greathouse 		goto gws_error;
91329633d0eSJoseph Greathouse 	}
91429633d0eSJoseph Greathouse 
9156127896fSHuang Rui 	/* If CRAT is broken, won't set iommu enabled */
9166127896fSHuang Rui 	kfd_double_confirm_iommu_support(kfd);
9176127896fSHuang Rui 
91864d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
91964d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
92064d1c3a4SFelix Kuehling 		goto device_iommu_error;
92164c7f8cfSBen Goz 	}
92264c7f8cfSBen Goz 
923373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
924373d7080SFelix Kuehling 
925814ab993SPhilip Yang 	svm_migrate_init((struct amdgpu_device *)kfd->kgd);
926814ab993SPhilip Yang 
927b8935a7cSYong Zhao 	if (kfd_resume(kfd))
928b8935a7cSYong Zhao 		goto kfd_resume_error;
929b8935a7cSYong Zhao 
930fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
931fbeb661bSYair Shachar 
932465ab9e0SOak Zeng 	if (kfd_topology_add_device(kfd)) {
933465ab9e0SOak Zeng 		dev_err(kfd_device, "Error adding device to topology\n");
934465ab9e0SOak Zeng 		goto kfd_topology_add_device_error;
935465ab9e0SOak Zeng 	}
936465ab9e0SOak Zeng 
937938a0650SAmber Lin 	kfd_smi_init(kfd);
938938a0650SAmber Lin 
9394a488a7aSOded Gabbay 	kfd->init_complete = true;
94079775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
9414a488a7aSOded Gabbay 		 kfd->pdev->device);
9424a488a7aSOded Gabbay 
94379775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
944d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
94564c7f8cfSBen Goz 
94619f6d2a6SOded Gabbay 	goto out;
94719f6d2a6SOded Gabbay 
948465ab9e0SOak Zeng kfd_topology_add_device_error:
949b8935a7cSYong Zhao kfd_resume_error:
95064d1c3a4SFelix Kuehling device_iommu_error:
95129633d0eSJoseph Greathouse gws_error:
95264c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
95364c7f8cfSBen Goz device_queue_manager_error:
9542249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
9552249d558SAndrew Lewycky kfd_interrupt_error:
956735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
957735df2baSFelix Kuehling kfd_doorbell_error:
95873a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
95973a1da0bSOded Gabbay kfd_gtt_sa_init_error:
9607cd52c91SAmber Lin 	amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
961e09d4fc8SOak Zeng alloc_gtt_mem_failure:
96229633d0eSJoseph Greathouse 	if (kfd->gws)
963e09d4fc8SOak Zeng 		amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
96419f6d2a6SOded Gabbay 	dev_err(kfd_device,
96579775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
96619f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
96719f6d2a6SOded Gabbay out:
96819f6d2a6SOded Gabbay 	return kfd->init_complete;
9694a488a7aSOded Gabbay }
9704a488a7aSOded Gabbay 
9714a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
9724a488a7aSOded Gabbay {
973b17f068aSOded Gabbay 	if (kfd->init_complete) {
974814ab993SPhilip Yang 		svm_migrate_fini((struct amdgpu_device *)kfd->kgd);
97564c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
9762249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
97719f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
978735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
97959d7115dSMukul Joshi 		ida_destroy(&kfd->doorbell_ida);
98073a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
9817cd52c91SAmber Lin 		amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
98229633d0eSJoseph Greathouse 		if (kfd->gws)
983e09d4fc8SOak Zeng 			amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
984b17f068aSOded Gabbay 	}
9855b5c4e40SEvgeny Pinchuk 
9864a488a7aSOded Gabbay 	kfree(kfd);
9874a488a7aSOded Gabbay }
9884a488a7aSOded Gabbay 
989e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
990e3b7a967SShaoyun Liu {
991e42051d2SShaoyun Liu 	if (!kfd->init_complete)
992e42051d2SShaoyun Liu 		return 0;
99309c34e8dSFelix Kuehling 
99455977744SMukul Joshi 	kfd_smi_event_update_gpu_reset(kfd, false);
99555977744SMukul Joshi 
99609c34e8dSFelix Kuehling 	kfd->dqm->ops.pre_reset(kfd->dqm);
99709c34e8dSFelix Kuehling 
9989593f4d6SRajneesh Bhardwaj 	kgd2kfd_suspend(kfd, false);
999e42051d2SShaoyun Liu 
1000e42051d2SShaoyun Liu 	kfd_signal_reset_event(kfd);
1001e3b7a967SShaoyun Liu 	return 0;
1002e3b7a967SShaoyun Liu }
1003e3b7a967SShaoyun Liu 
1004e42051d2SShaoyun Liu /*
1005e42051d2SShaoyun Liu  * Fix me. KFD won't be able to resume existing process for now.
1006e42051d2SShaoyun Liu  * We will keep all existing process in a evicted state and
1007e42051d2SShaoyun Liu  * wait the process to be terminated.
1008e42051d2SShaoyun Liu  */
1009e42051d2SShaoyun Liu 
1010e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
1011e3b7a967SShaoyun Liu {
1012a1bd079fSyu kuai 	int ret;
1013e42051d2SShaoyun Liu 
1014e42051d2SShaoyun Liu 	if (!kfd->init_complete)
1015e3b7a967SShaoyun Liu 		return 0;
1016e42051d2SShaoyun Liu 
1017e42051d2SShaoyun Liu 	ret = kfd_resume(kfd);
1018e42051d2SShaoyun Liu 	if (ret)
1019e42051d2SShaoyun Liu 		return ret;
1020a1bd079fSyu kuai 	atomic_dec(&kfd_locked);
10219b54d201SEric Huang 
10229b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
10239b54d201SEric Huang 
102455977744SMukul Joshi 	kfd_smi_event_update_gpu_reset(kfd, true);
102555977744SMukul Joshi 
1026e42051d2SShaoyun Liu 	return 0;
1027e42051d2SShaoyun Liu }
1028e42051d2SShaoyun Liu 
1029e42051d2SShaoyun Liu bool kfd_is_locked(void)
1030e42051d2SShaoyun Liu {
1031e42051d2SShaoyun Liu 	return  (atomic_read(&kfd_locked) > 0);
1032e3b7a967SShaoyun Liu }
1033e3b7a967SShaoyun Liu 
10349593f4d6SRajneesh Bhardwaj void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm)
10354a488a7aSOded Gabbay {
1036733fa1f7SYong Zhao 	if (!kfd->init_complete)
1037733fa1f7SYong Zhao 		return;
1038733fa1f7SYong Zhao 
10399593f4d6SRajneesh Bhardwaj 	/* for runtime suspend, skip locking kfd */
10409593f4d6SRajneesh Bhardwaj 	if (!run_pm) {
104126103436SFelix Kuehling 		/* For first KFD device suspend all the KFD processes */
1042e42051d2SShaoyun Liu 		if (atomic_inc_return(&kfd_locked) == 1)
104326103436SFelix Kuehling 			kfd_suspend_all_processes();
10449593f4d6SRajneesh Bhardwaj 	}
104526103436SFelix Kuehling 
104645c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
104764d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
10484a488a7aSOded Gabbay }
10494a488a7aSOded Gabbay 
10509593f4d6SRajneesh Bhardwaj int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm)
10514a488a7aSOded Gabbay {
105226103436SFelix Kuehling 	int ret, count;
105326103436SFelix Kuehling 
1054b8935a7cSYong Zhao 	if (!kfd->init_complete)
1055b8935a7cSYong Zhao 		return 0;
1056b17f068aSOded Gabbay 
105726103436SFelix Kuehling 	ret = kfd_resume(kfd);
105826103436SFelix Kuehling 	if (ret)
105926103436SFelix Kuehling 		return ret;
1060b17f068aSOded Gabbay 
10619593f4d6SRajneesh Bhardwaj 	/* for runtime resume, skip unlocking kfd */
10629593f4d6SRajneesh Bhardwaj 	if (!run_pm) {
1063e42051d2SShaoyun Liu 		count = atomic_dec_return(&kfd_locked);
106426103436SFelix Kuehling 		WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
106526103436SFelix Kuehling 		if (count == 0)
106626103436SFelix Kuehling 			ret = kfd_resume_all_processes();
10679593f4d6SRajneesh Bhardwaj 	}
106826103436SFelix Kuehling 
106926103436SFelix Kuehling 	return ret;
10704ebc7182SYong Zhao }
10714ebc7182SYong Zhao 
1072*f8846323SJames Zhu int kgd2kfd_resume_iommu(struct kfd_dev *kfd)
1073b8935a7cSYong Zhao {
1074b8935a7cSYong Zhao 	int err = 0;
1075b8935a7cSYong Zhao 
107664d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
1077*f8846323SJames Zhu 	if (err)
107864d1c3a4SFelix Kuehling 		dev_err(kfd_device,
107964d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
108064d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
108164d1c3a4SFelix Kuehling 	return err;
108264d1c3a4SFelix Kuehling }
1083733fa1f7SYong Zhao 
1084*f8846323SJames Zhu static int kfd_resume(struct kfd_dev *kfd)
1085*f8846323SJames Zhu {
1086*f8846323SJames Zhu 	int err = 0;
1087*f8846323SJames Zhu 
1088b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
1089b8935a7cSYong Zhao 	if (err) {
1090b8935a7cSYong Zhao 		dev_err(kfd_device,
1091b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
1092b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
1093b8935a7cSYong Zhao 		goto dqm_start_error;
1094b17f068aSOded Gabbay 	}
1095b17f068aSOded Gabbay 
1096b8935a7cSYong Zhao 	return err;
1097b8935a7cSYong Zhao 
1098b8935a7cSYong Zhao dqm_start_error:
109964d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
1100b8935a7cSYong Zhao 	return err;
11014a488a7aSOded Gabbay }
11024a488a7aSOded Gabbay 
1103b3eca59dSPhilip Yang static inline void kfd_queue_work(struct workqueue_struct *wq,
1104b3eca59dSPhilip Yang 				  struct work_struct *work)
1105b3eca59dSPhilip Yang {
1106b3eca59dSPhilip Yang 	int cpu, new_cpu;
1107b3eca59dSPhilip Yang 
1108b3eca59dSPhilip Yang 	cpu = new_cpu = smp_processor_id();
1109b3eca59dSPhilip Yang 	do {
1110b3eca59dSPhilip Yang 		new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
1111b3eca59dSPhilip Yang 		if (cpu_to_node(new_cpu) == numa_node_id())
1112b3eca59dSPhilip Yang 			break;
1113b3eca59dSPhilip Yang 	} while (cpu != new_cpu);
1114b3eca59dSPhilip Yang 
1115b3eca59dSPhilip Yang 	queue_work_on(new_cpu, wq, work);
1116b3eca59dSPhilip Yang }
1117b3eca59dSPhilip Yang 
1118b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
1119b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
11204a488a7aSOded Gabbay {
112158e69886SLan Xiao 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
112258e69886SLan Xiao 	bool is_patched = false;
11232383a767SChristian König 	unsigned long flags;
112458e69886SLan Xiao 
11252249d558SAndrew Lewycky 	if (!kfd->init_complete)
11262249d558SAndrew Lewycky 		return;
11272249d558SAndrew Lewycky 
112858e69886SLan Xiao 	if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
112958e69886SLan Xiao 		dev_err_once(kfd_device, "Ring entry too small\n");
113058e69886SLan Xiao 		return;
113158e69886SLan Xiao 	}
113258e69886SLan Xiao 
11332383a767SChristian König 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
11342249d558SAndrew Lewycky 
11352249d558SAndrew Lewycky 	if (kfd->interrupts_active
113658e69886SLan Xiao 	    && interrupt_is_wanted(kfd, ih_ring_entry,
113758e69886SLan Xiao 				   patched_ihre, &is_patched)
113858e69886SLan Xiao 	    && enqueue_ih_ring_entry(kfd,
113958e69886SLan Xiao 				     is_patched ? patched_ihre : ih_ring_entry))
1140b3eca59dSPhilip Yang 		kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work);
11412249d558SAndrew Lewycky 
11422383a767SChristian König 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
11434a488a7aSOded Gabbay }
11446e81090bSOded Gabbay 
11456b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
11466b95e797SFelix Kuehling {
11476b95e797SFelix Kuehling 	struct kfd_process *p;
11486b95e797SFelix Kuehling 	int r;
11496b95e797SFelix Kuehling 
11506b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
11516b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
11526b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
11536b95e797SFelix Kuehling 	 */
11546b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
11556b95e797SFelix Kuehling 	if (!p)
11566b95e797SFelix Kuehling 		return -ESRCH;
11576b95e797SFelix Kuehling 
1158b2057956SFelix Kuehling 	WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
11596b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
11606b95e797SFelix Kuehling 
11616b95e797SFelix Kuehling 	kfd_unref_process(p);
11626b95e797SFelix Kuehling 	return r;
11636b95e797SFelix Kuehling }
11646b95e797SFelix Kuehling 
11656b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
11666b95e797SFelix Kuehling {
11676b95e797SFelix Kuehling 	struct kfd_process *p;
11686b95e797SFelix Kuehling 	int r;
11696b95e797SFelix Kuehling 
11706b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
11716b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
11726b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
11736b95e797SFelix Kuehling 	 */
11746b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
11756b95e797SFelix Kuehling 	if (!p)
11766b95e797SFelix Kuehling 		return -ESRCH;
11776b95e797SFelix Kuehling 
11786b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
11796b95e797SFelix Kuehling 
11806b95e797SFelix Kuehling 	kfd_unref_process(p);
11816b95e797SFelix Kuehling 	return r;
11826b95e797SFelix Kuehling }
11836b95e797SFelix Kuehling 
118426103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
118526103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
118626103436SFelix Kuehling  *   process.
118726103436SFelix Kuehling  *
118826103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
118926103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
119026103436SFelix Kuehling  *
119126103436SFelix Kuehling  */
119226103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
119326103436SFelix Kuehling 					       struct dma_fence *fence)
119426103436SFelix Kuehling {
119526103436SFelix Kuehling 	struct kfd_process *p;
119626103436SFelix Kuehling 	unsigned long active_time;
119726103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
119826103436SFelix Kuehling 
119926103436SFelix Kuehling 	if (!fence)
120026103436SFelix Kuehling 		return -EINVAL;
120126103436SFelix Kuehling 
120226103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
120326103436SFelix Kuehling 		return 0;
120426103436SFelix Kuehling 
120526103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
120626103436SFelix Kuehling 	if (!p)
120726103436SFelix Kuehling 		return -ENODEV;
120826103436SFelix Kuehling 
120926103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
121026103436SFelix Kuehling 		goto out;
121126103436SFelix Kuehling 
121226103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
121326103436SFelix Kuehling 
121426103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
121526103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
121626103436SFelix Kuehling 	 */
121726103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
121826103436SFelix Kuehling 	if (delay_jiffies > active_time)
121926103436SFelix Kuehling 		delay_jiffies -= active_time;
122026103436SFelix Kuehling 	else
122126103436SFelix Kuehling 		delay_jiffies = 0;
122226103436SFelix Kuehling 
122326103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
122426103436SFelix Kuehling 	 * to kfd_evict_bo_worker
122526103436SFelix Kuehling 	 */
1226b2057956SFelix Kuehling 	WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",
1227b2057956SFelix Kuehling 	     p->lead_thread->pid, delay_jiffies);
122826103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
122926103436SFelix Kuehling out:
123026103436SFelix Kuehling 	kfd_unref_process(p);
123126103436SFelix Kuehling 	return 0;
123226103436SFelix Kuehling }
123326103436SFelix Kuehling 
12346e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
12356e81090bSOded Gabbay 				unsigned int chunk_size)
12366e81090bSOded Gabbay {
12378625ff9cSFelix Kuehling 	unsigned int num_of_longs;
12386e81090bSOded Gabbay 
123932fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
124032fa8219SFelix Kuehling 		return -EINVAL;
124132fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
124232fa8219SFelix Kuehling 		return -EINVAL;
124332fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
124432fa8219SFelix Kuehling 		return -EINVAL;
12456e81090bSOded Gabbay 
12466e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
12476e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
12486e81090bSOded Gabbay 
12498625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
12508625ff9cSFelix Kuehling 		BITS_PER_LONG;
12516e81090bSOded Gabbay 
12528625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
12536e81090bSOded Gabbay 
12546e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
12556e81090bSOded Gabbay 		return -ENOMEM;
12566e81090bSOded Gabbay 
125779775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
12586e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
12596e81090bSOded Gabbay 
12606e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
12616e81090bSOded Gabbay 
12626e81090bSOded Gabbay 	return 0;
12636e81090bSOded Gabbay 
12646e81090bSOded Gabbay }
12656e81090bSOded Gabbay 
12666e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
12676e81090bSOded Gabbay {
12686e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
12696e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
12706e81090bSOded Gabbay }
12716e81090bSOded Gabbay 
12726e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
12736e81090bSOded Gabbay 						unsigned int bit_num,
12746e81090bSOded Gabbay 						unsigned int chunk_size)
12756e81090bSOded Gabbay {
12766e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
12776e81090bSOded Gabbay }
12786e81090bSOded Gabbay 
12796e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
12806e81090bSOded Gabbay 						unsigned int bit_num,
12816e81090bSOded Gabbay 						unsigned int chunk_size)
12826e81090bSOded Gabbay {
12836e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
12846e81090bSOded Gabbay }
12856e81090bSOded Gabbay 
12866e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
12876e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
12886e81090bSOded Gabbay {
12896e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
12906e81090bSOded Gabbay 
12916e81090bSOded Gabbay 	if (size == 0)
12926e81090bSOded Gabbay 		return -EINVAL;
12936e81090bSOded Gabbay 
12946e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
12956e81090bSOded Gabbay 		return -ENOMEM;
12966e81090bSOded Gabbay 
12971cd106ecSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
12981cd106ecSFelix Kuehling 	if (!(*mem_obj))
12996e81090bSOded Gabbay 		return -ENOMEM;
13006e81090bSOded Gabbay 
130179775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
13026e81090bSOded Gabbay 
13036e81090bSOded Gabbay 	start_search = 0;
13046e81090bSOded Gabbay 
13056e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
13066e81090bSOded Gabbay 
13076e81090bSOded Gabbay kfd_gtt_restart_search:
13086e81090bSOded Gabbay 	/* Find the first chunk that is free */
13096e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
13106e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
13116e81090bSOded Gabbay 					start_search);
13126e81090bSOded Gabbay 
131379775b62SKent Russell 	pr_debug("Found = %d\n", found);
13146e81090bSOded Gabbay 
13156e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
13166e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
13176e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
13186e81090bSOded Gabbay 
13196e81090bSOded Gabbay 	/* Update fields of mem_obj */
13206e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
13216e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
13226e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
13236e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
13246e81090bSOded Gabbay 					found,
13256e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
13266e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
13276e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
13286e81090bSOded Gabbay 					found,
13296e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
13306e81090bSOded Gabbay 
133179775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
13326e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
13336e81090bSOded Gabbay 
13346e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
13356e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
133679775b62SKent Russell 		pr_debug("Single bit\n");
13376e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
13386e81090bSOded Gabbay 		goto kfd_gtt_out;
13396e81090bSOded Gabbay 	}
13406e81090bSOded Gabbay 
13416e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
13426e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
13436e81090bSOded Gabbay 	do {
13446e81090bSOded Gabbay 		(*mem_obj)->range_end =
13456e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
13466e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
13476e81090bSOded Gabbay 		/*
13486e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
13496e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
13506e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
13516e81090bSOded Gabbay 		 */
13526e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
13536e81090bSOded Gabbay 			start_search = found;
13546e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
13556e81090bSOded Gabbay 		}
13566e81090bSOded Gabbay 
13576e81090bSOded Gabbay 		/*
13586e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
13596e81090bSOded Gabbay 		 */
13606e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
13616e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
13626e81090bSOded Gabbay 
13636e81090bSOded Gabbay 		/* Check if we don't need another chunk */
13646e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
13656e81090bSOded Gabbay 			cur_size = 0;
13666e81090bSOded Gabbay 		else
13676e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
13686e81090bSOded Gabbay 
13696e81090bSOded Gabbay 	} while (cur_size > 0);
13706e81090bSOded Gabbay 
137179775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
13726e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
13736e81090bSOded Gabbay 
13746e81090bSOded Gabbay 	/* Mark the chunks as allocated */
13756e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
13766e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
13776e81090bSOded Gabbay 		found++)
13786e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
13796e81090bSOded Gabbay 
13806e81090bSOded Gabbay kfd_gtt_out:
13816e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
13826e81090bSOded Gabbay 	return 0;
13836e81090bSOded Gabbay 
13846e81090bSOded Gabbay kfd_gtt_no_free_chunk:
13853148a6a0SJack Zhang 	pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);
13866e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
13873148a6a0SJack Zhang 	kfree(*mem_obj);
13886e81090bSOded Gabbay 	return -ENOMEM;
13896e81090bSOded Gabbay }
13906e81090bSOded Gabbay 
13916e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
13926e81090bSOded Gabbay {
13936e81090bSOded Gabbay 	unsigned int bit;
13946e81090bSOded Gabbay 
13959216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
13969216ed29SOded Gabbay 	if (!mem_obj)
13979216ed29SOded Gabbay 		return 0;
13986e81090bSOded Gabbay 
139979775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
14006e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
14016e81090bSOded Gabbay 
14026e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
14036e81090bSOded Gabbay 
14046e81090bSOded Gabbay 	/* Mark the chunks as free */
14056e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
14066e81090bSOded Gabbay 		bit <= mem_obj->range_end;
14076e81090bSOded Gabbay 		bit++)
14086e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
14096e81090bSOded Gabbay 
14106e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
14116e81090bSOded Gabbay 
14126e81090bSOded Gabbay 	kfree(mem_obj);
14136e81090bSOded Gabbay 	return 0;
14146e81090bSOded Gabbay }
1415a29ec470SShaoyun Liu 
14169b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
14179b54d201SEric Huang {
14189b54d201SEric Huang 	if (kfd)
14199b54d201SEric Huang 		atomic_inc(&kfd->sram_ecc_flag);
14209b54d201SEric Huang }
14219b54d201SEric Huang 
142243d8107fSHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd)
142343d8107fSHarish Kasiviswanathan {
142443d8107fSHarish Kasiviswanathan 	if (atomic_inc_return(&kfd->compute_profile) == 1)
142543d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, false);
142643d8107fSHarish Kasiviswanathan }
142743d8107fSHarish Kasiviswanathan 
142843d8107fSHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd)
142943d8107fSHarish Kasiviswanathan {
143043d8107fSHarish Kasiviswanathan 	int count = atomic_dec_return(&kfd->compute_profile);
143143d8107fSHarish Kasiviswanathan 
143243d8107fSHarish Kasiviswanathan 	if (count == 0)
143343d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, true);
143443d8107fSHarish Kasiviswanathan 	WARN_ONCE(count < 0, "Compute profile ref. count error");
143543d8107fSHarish Kasiviswanathan }
143643d8107fSHarish Kasiviswanathan 
1437410e302eSGraham Sider void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask)
14382c2b0d88SMukul Joshi {
1439158fc08dSAmber Lin 	if (kfd && kfd->init_complete)
14402c2b0d88SMukul Joshi 		kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask);
14412c2b0d88SMukul Joshi }
14422c2b0d88SMukul Joshi 
1443a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
1444a29ec470SShaoyun Liu 
1445a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
1446a29ec470SShaoyun Liu  * which will trigger a GPU reset and bring the HWS back to normal state
1447a29ec470SShaoyun Liu  */
1448a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1449a29ec470SShaoyun Liu {
1450a29ec470SShaoyun Liu 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1451a29ec470SShaoyun Liu 		pr_err("HWS is not enabled");
1452a29ec470SShaoyun Liu 		return -EINVAL;
1453a29ec470SShaoyun Liu 	}
1454a29ec470SShaoyun Liu 
14554f942aaeSOak Zeng 	return dqm_debugfs_hang_hws(dev->dqm);
1456a29ec470SShaoyun Liu }
1457a29ec470SShaoyun Liu 
1458a29ec470SShaoyun Liu #endif
1459