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"
290db54b24SYong Zhao #include "cwsr_trap_handler.h"
3064d1c3a4SFelix Kuehling #include "kfd_iommu.h"
315b87245fSAmber Lin #include "amdgpu_amdkfd.h"
324a488a7aSOded Gabbay 
3319f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768
34e42051d2SShaoyun Liu 
35e42051d2SShaoyun Liu /*
36e42051d2SShaoyun Liu  * kfd_locked is used to lock the kfd driver during suspend or reset
37e42051d2SShaoyun Liu  * once locked, kfd driver will stop any further GPU execution.
38e42051d2SShaoyun Liu  * create process (open) will return -EAGAIN.
39e42051d2SShaoyun Liu  */
40e42051d2SShaoyun Liu static atomic_t kfd_locked = ATOMIC_INIT(0);
4119f6d2a6SOded Gabbay 
42a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK
43e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v7_kfd2kgd;
44a3e520a2SAlex Deucher #endif
45e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v8_kfd2kgd;
46e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v9_kfd2kgd;
47e392c887SYong Zhao extern const struct kfd2kgd_calls arcturus_kfd2kgd;
48e392c887SYong Zhao extern const struct kfd2kgd_calls gfx_v10_kfd2kgd;
493a2f0c81SYong Zhao extern const struct kfd2kgd_calls gfx_v10_3_kfd2kgd;
50e392c887SYong Zhao 
51e392c887SYong Zhao static const struct kfd2kgd_calls *kfd2kgd_funcs[] = {
52e392c887SYong Zhao #ifdef KFD_SUPPORT_IOMMU_V2
53a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK
54e392c887SYong Zhao 	[CHIP_KAVERI] = &gfx_v7_kfd2kgd,
55a3e520a2SAlex Deucher #endif
56e392c887SYong Zhao 	[CHIP_CARRIZO] = &gfx_v8_kfd2kgd,
57e392c887SYong Zhao 	[CHIP_RAVEN] = &gfx_v9_kfd2kgd,
58e392c887SYong Zhao #endif
59a3e520a2SAlex Deucher #ifdef CONFIG_DRM_AMDGPU_CIK
60e392c887SYong Zhao 	[CHIP_HAWAII] = &gfx_v7_kfd2kgd,
61a3e520a2SAlex Deucher #endif
62e392c887SYong Zhao 	[CHIP_TONGA] = &gfx_v8_kfd2kgd,
63e392c887SYong Zhao 	[CHIP_FIJI] = &gfx_v8_kfd2kgd,
64e392c887SYong Zhao 	[CHIP_POLARIS10] = &gfx_v8_kfd2kgd,
65e392c887SYong Zhao 	[CHIP_POLARIS11] = &gfx_v8_kfd2kgd,
66e392c887SYong Zhao 	[CHIP_POLARIS12] = &gfx_v8_kfd2kgd,
67e392c887SYong Zhao 	[CHIP_VEGAM] = &gfx_v8_kfd2kgd,
68e392c887SYong Zhao 	[CHIP_VEGA10] = &gfx_v9_kfd2kgd,
69e392c887SYong Zhao 	[CHIP_VEGA12] = &gfx_v9_kfd2kgd,
70e392c887SYong Zhao 	[CHIP_VEGA20] = &gfx_v9_kfd2kgd,
71e392c887SYong Zhao 	[CHIP_RENOIR] = &gfx_v9_kfd2kgd,
72e392c887SYong Zhao 	[CHIP_ARCTURUS] = &arcturus_kfd2kgd,
73e392c887SYong Zhao 	[CHIP_NAVI10] = &gfx_v10_kfd2kgd,
74e392c887SYong Zhao 	[CHIP_NAVI12] = &gfx_v10_kfd2kgd,
75e392c887SYong Zhao 	[CHIP_NAVI14] = &gfx_v10_kfd2kgd,
763a2f0c81SYong Zhao 	[CHIP_SIENNA_CICHLID] = &gfx_v10_3_kfd2kgd,
7709759e13SChengming Gui 	[CHIP_NAVY_FLOUNDER] = &gfx_v10_3_kfd2kgd,
78e392c887SYong Zhao };
79e392c887SYong Zhao 
8064d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
814a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = {
820da7558cSBen Goz 	.asic_family = CHIP_KAVERI,
83c181159aSYong Zhao 	.asic_name = "kaveri",
840da7558cSBen Goz 	.max_pasid_bits = 16,
85992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
86992839adSYair Shachar 	.max_no_of_hqd	= 24,
87ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
880da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
89f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
90fbeb661bSYair Shachar 	.num_of_watch_points = 4,
91373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
92373d7080SFelix Kuehling 	.supports_cwsr = false,
9364d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
943ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
9598bb9222SYong Zhao 	.num_sdma_engines = 2,
961b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
97d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
980da7558cSBen Goz };
990da7558cSBen Goz 
1000da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
1010da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
102c181159aSYong Zhao 	.asic_name = "carrizo",
1034a488a7aSOded Gabbay 	.max_pasid_bits = 16,
104eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
105eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
106ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
107b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
108eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
109f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
110373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
111373d7080SFelix Kuehling 	.supports_cwsr = true,
11264d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
1133ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
11498bb9222SYong Zhao 	.num_sdma_engines = 2,
1151b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
116d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1174a488a7aSOded Gabbay };
1184d663df6SYong Zhao 
1194d663df6SYong Zhao static const struct kfd_device_info raven_device_info = {
1204d663df6SYong Zhao 	.asic_family = CHIP_RAVEN,
121c181159aSYong Zhao 	.asic_name = "raven",
1224d663df6SYong Zhao 	.max_pasid_bits = 16,
1234d663df6SYong Zhao 	.max_no_of_hqd  = 24,
1244d663df6SYong Zhao 	.doorbell_size  = 8,
1254d663df6SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
1264d663df6SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
1274d663df6SYong Zhao 	.num_of_watch_points = 4,
1284d663df6SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
1294d663df6SYong Zhao 	.supports_cwsr = true,
1304d663df6SYong Zhao 	.needs_iommu_device = true,
1314d663df6SYong Zhao 	.needs_pci_atomics = true,
1324d663df6SYong Zhao 	.num_sdma_engines = 1,
1331b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
134d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
1354d663df6SYong Zhao };
13664d1c3a4SFelix Kuehling #endif
1374a488a7aSOded Gabbay 
138a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
139a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
140c181159aSYong Zhao 	.asic_name = "hawaii",
141a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
142a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
143a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
144ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
145a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
146a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
147a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
148a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
149a3084e6cSFelix Kuehling 	.supports_cwsr = false,
15064d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
151a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
15298bb9222SYong Zhao 	.num_sdma_engines = 2,
1531b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
154d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
155a3084e6cSFelix Kuehling };
156a3084e6cSFelix Kuehling 
157a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
158a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
159c181159aSYong Zhao 	.asic_name = "tonga",
160a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
161a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
162ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
163a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
164a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
165a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
166a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
167a3084e6cSFelix Kuehling 	.supports_cwsr = false,
16864d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
169a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
17098bb9222SYong Zhao 	.num_sdma_engines = 2,
1711b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
172d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
173a3084e6cSFelix Kuehling };
174a3084e6cSFelix Kuehling 
175a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
176a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
177c181159aSYong Zhao 	.asic_name = "fiji",
178a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
179a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
180ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
181a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
182a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
183a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
184a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
185a3084e6cSFelix Kuehling 	.supports_cwsr = true,
18664d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
187a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
18898bb9222SYong Zhao 	.num_sdma_engines = 2,
1891b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
190d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
191a3084e6cSFelix Kuehling };
192a3084e6cSFelix Kuehling 
193a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
194a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
195c181159aSYong Zhao 	.asic_name = "fiji",
196a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
197a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
198ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
199a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
200a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
201a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
202a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
203a3084e6cSFelix Kuehling 	.supports_cwsr = true,
20464d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
205a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
20698bb9222SYong Zhao 	.num_sdma_engines = 2,
2071b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
208d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
209a3084e6cSFelix Kuehling };
210a3084e6cSFelix Kuehling 
211a3084e6cSFelix Kuehling 
212a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
213a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
214c181159aSYong Zhao 	.asic_name = "polaris10",
215a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
216a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
217ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
218a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
219a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
220a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
221a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
222a3084e6cSFelix Kuehling 	.supports_cwsr = true,
22364d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
224a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
22598bb9222SYong Zhao 	.num_sdma_engines = 2,
2261b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
227d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
228a3084e6cSFelix Kuehling };
229a3084e6cSFelix Kuehling 
230a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = {
231a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
232c181159aSYong Zhao 	.asic_name = "polaris10",
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 = false,
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 polaris11_device_info = {
249a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
250c181159aSYong Zhao 	.asic_name = "polaris11",
251a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
252a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
253ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
254a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
255a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
256a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
257a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
258a3084e6cSFelix Kuehling 	.supports_cwsr = true,
25964d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
260a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
26198bb9222SYong Zhao 	.num_sdma_engines = 2,
2621b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
263d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
264a3084e6cSFelix Kuehling };
265a3084e6cSFelix Kuehling 
266846a44d7SGang Ba static const struct kfd_device_info polaris12_device_info = {
267846a44d7SGang Ba 	.asic_family = CHIP_POLARIS12,
268c181159aSYong Zhao 	.asic_name = "polaris12",
269846a44d7SGang Ba 	.max_pasid_bits = 16,
270846a44d7SGang Ba 	.max_no_of_hqd  = 24,
271846a44d7SGang Ba 	.doorbell_size  = 4,
272846a44d7SGang Ba 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
273846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_cik,
274846a44d7SGang Ba 	.num_of_watch_points = 4,
275846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
276846a44d7SGang Ba 	.supports_cwsr = true,
277846a44d7SGang Ba 	.needs_iommu_device = false,
278846a44d7SGang Ba 	.needs_pci_atomics = true,
279846a44d7SGang Ba 	.num_sdma_engines = 2,
2801b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
281846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
282846a44d7SGang Ba };
283846a44d7SGang Ba 
284ed81cd6eSKent Russell static const struct kfd_device_info vegam_device_info = {
285ed81cd6eSKent Russell 	.asic_family = CHIP_VEGAM,
286c181159aSYong Zhao 	.asic_name = "vegam",
287ed81cd6eSKent Russell 	.max_pasid_bits = 16,
288ed81cd6eSKent Russell 	.max_no_of_hqd  = 24,
289ed81cd6eSKent Russell 	.doorbell_size  = 4,
290ed81cd6eSKent Russell 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
291ed81cd6eSKent Russell 	.event_interrupt_class = &event_interrupt_class_cik,
292ed81cd6eSKent Russell 	.num_of_watch_points = 4,
293ed81cd6eSKent Russell 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
294ed81cd6eSKent Russell 	.supports_cwsr = true,
295ed81cd6eSKent Russell 	.needs_iommu_device = false,
296ed81cd6eSKent Russell 	.needs_pci_atomics = true,
297ed81cd6eSKent Russell 	.num_sdma_engines = 2,
298ed81cd6eSKent Russell 	.num_xgmi_sdma_engines = 0,
299a3084e6cSFelix Kuehling 	.num_sdma_queues_per_engine = 2,
300a3084e6cSFelix Kuehling };
301a3084e6cSFelix Kuehling 
302389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = {
303389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
304c181159aSYong Zhao 	.asic_name = "vega10",
305389056e5SFelix Kuehling 	.max_pasid_bits = 16,
306389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
307389056e5SFelix Kuehling 	.doorbell_size  = 8,
308389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
309389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
310389056e5SFelix Kuehling 	.num_of_watch_points = 4,
311389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
312389056e5SFelix Kuehling 	.supports_cwsr = true,
313389056e5SFelix Kuehling 	.needs_iommu_device = false,
314389056e5SFelix Kuehling 	.needs_pci_atomics = false,
31598bb9222SYong Zhao 	.num_sdma_engines = 2,
3161b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
317d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
318389056e5SFelix Kuehling };
319389056e5SFelix Kuehling 
320389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_device_info = {
321389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
322c181159aSYong Zhao 	.asic_name = "vega10",
323389056e5SFelix Kuehling 	.max_pasid_bits = 16,
324389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
325389056e5SFelix Kuehling 	.doorbell_size  = 8,
326389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
327389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
328389056e5SFelix Kuehling 	.num_of_watch_points = 4,
329389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
330389056e5SFelix Kuehling 	.supports_cwsr = true,
331389056e5SFelix Kuehling 	.needs_iommu_device = false,
332389056e5SFelix Kuehling 	.needs_pci_atomics = false,
33398bb9222SYong Zhao 	.num_sdma_engines = 2,
3341b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
335d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
336389056e5SFelix Kuehling };
337389056e5SFelix Kuehling 
338846a44d7SGang Ba static const struct kfd_device_info vega12_device_info = {
339846a44d7SGang Ba 	.asic_family = CHIP_VEGA12,
340c181159aSYong Zhao 	.asic_name = "vega12",
341846a44d7SGang Ba 	.max_pasid_bits = 16,
342846a44d7SGang Ba 	.max_no_of_hqd  = 24,
343846a44d7SGang Ba 	.doorbell_size  = 8,
344846a44d7SGang Ba 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
345846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_v9,
346846a44d7SGang Ba 	.num_of_watch_points = 4,
347846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
348846a44d7SGang Ba 	.supports_cwsr = true,
349846a44d7SGang Ba 	.needs_iommu_device = false,
350846a44d7SGang Ba 	.needs_pci_atomics = false,
351846a44d7SGang Ba 	.num_sdma_engines = 2,
3521b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
353846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
354846a44d7SGang Ba };
355846a44d7SGang Ba 
35622a3a294SShaoyun Liu static const struct kfd_device_info vega20_device_info = {
35722a3a294SShaoyun Liu 	.asic_family = CHIP_VEGA20,
358c181159aSYong Zhao 	.asic_name = "vega20",
35922a3a294SShaoyun Liu 	.max_pasid_bits = 16,
36022a3a294SShaoyun Liu 	.max_no_of_hqd	= 24,
36122a3a294SShaoyun Liu 	.doorbell_size	= 8,
36222a3a294SShaoyun Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
36322a3a294SShaoyun Liu 	.event_interrupt_class = &event_interrupt_class_v9,
36422a3a294SShaoyun Liu 	.num_of_watch_points = 4,
36522a3a294SShaoyun Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
36622a3a294SShaoyun Liu 	.supports_cwsr = true,
36722a3a294SShaoyun Liu 	.needs_iommu_device = false,
368006a0b3dSShaoyun Liu 	.needs_pci_atomics = false,
36922a3a294SShaoyun Liu 	.num_sdma_engines = 2,
3701b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
37122a3a294SShaoyun Liu 	.num_sdma_queues_per_engine = 8,
37222a3a294SShaoyun Liu };
37322a3a294SShaoyun Liu 
37449adcf8aSYong Zhao static const struct kfd_device_info arcturus_device_info = {
37549adcf8aSYong Zhao 	.asic_family = CHIP_ARCTURUS,
376c181159aSYong Zhao 	.asic_name = "arcturus",
37749adcf8aSYong Zhao 	.max_pasid_bits = 16,
37849adcf8aSYong Zhao 	.max_no_of_hqd	= 24,
37949adcf8aSYong Zhao 	.doorbell_size	= 8,
38049adcf8aSYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
38149adcf8aSYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
38249adcf8aSYong Zhao 	.num_of_watch_points = 4,
38349adcf8aSYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
38449adcf8aSYong Zhao 	.supports_cwsr = true,
38549adcf8aSYong Zhao 	.needs_iommu_device = false,
38649adcf8aSYong Zhao 	.needs_pci_atomics = false,
387b6689cf7SOak Zeng 	.num_sdma_engines = 2,
388b6689cf7SOak Zeng 	.num_xgmi_sdma_engines = 6,
38949adcf8aSYong Zhao 	.num_sdma_queues_per_engine = 8,
39049adcf8aSYong Zhao };
39149adcf8aSYong Zhao 
3922b9c2211SHuang Rui static const struct kfd_device_info renoir_device_info = {
3932b9c2211SHuang Rui 	.asic_family = CHIP_RENOIR,
394acb9acbeSHuang Rui 	.asic_name = "renoir",
3952b9c2211SHuang Rui 	.max_pasid_bits = 16,
3962b9c2211SHuang Rui 	.max_no_of_hqd  = 24,
3972b9c2211SHuang Rui 	.doorbell_size  = 8,
3982b9c2211SHuang Rui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
3992b9c2211SHuang Rui 	.event_interrupt_class = &event_interrupt_class_v9,
4002b9c2211SHuang Rui 	.num_of_watch_points = 4,
4012b9c2211SHuang Rui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
4022b9c2211SHuang Rui 	.supports_cwsr = true,
4032b9c2211SHuang Rui 	.needs_iommu_device = false,
4042b9c2211SHuang Rui 	.needs_pci_atomics = false,
4052b9c2211SHuang Rui 	.num_sdma_engines = 1,
4062b9c2211SHuang Rui 	.num_xgmi_sdma_engines = 0,
4072b9c2211SHuang Rui 	.num_sdma_queues_per_engine = 2,
4082b9c2211SHuang Rui };
4092b9c2211SHuang Rui 
41014328aa5SPhilip Cox static const struct kfd_device_info navi10_device_info = {
41114328aa5SPhilip Cox 	.asic_family = CHIP_NAVI10,
412c181159aSYong Zhao 	.asic_name = "navi10",
41314328aa5SPhilip Cox 	.max_pasid_bits = 16,
41414328aa5SPhilip Cox 	.max_no_of_hqd  = 24,
41514328aa5SPhilip Cox 	.doorbell_size  = 8,
41614328aa5SPhilip Cox 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
41714328aa5SPhilip Cox 	.event_interrupt_class = &event_interrupt_class_v9,
41814328aa5SPhilip Cox 	.num_of_watch_points = 4,
41914328aa5SPhilip Cox 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
42014328aa5SPhilip Cox 	.needs_iommu_device = false,
42114328aa5SPhilip Cox 	.supports_cwsr = true,
42214328aa5SPhilip Cox 	.needs_pci_atomics = false,
42314328aa5SPhilip Cox 	.num_sdma_engines = 2,
42414328aa5SPhilip Cox 	.num_xgmi_sdma_engines = 0,
42514328aa5SPhilip Cox 	.num_sdma_queues_per_engine = 8,
42614328aa5SPhilip Cox };
42714328aa5SPhilip Cox 
428b77fb9d8Sshaoyunl static const struct kfd_device_info navi12_device_info = {
4290e94b564Sshaoyunl 	.asic_family = CHIP_NAVI12,
430b77fb9d8Sshaoyunl 	.asic_name = "navi12",
431b77fb9d8Sshaoyunl 	.max_pasid_bits = 16,
432b77fb9d8Sshaoyunl 	.max_no_of_hqd  = 24,
433b77fb9d8Sshaoyunl 	.doorbell_size  = 8,
434b77fb9d8Sshaoyunl 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
435b77fb9d8Sshaoyunl 	.event_interrupt_class = &event_interrupt_class_v9,
436b77fb9d8Sshaoyunl 	.num_of_watch_points = 4,
437b77fb9d8Sshaoyunl 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
438b77fb9d8Sshaoyunl 	.needs_iommu_device = false,
439b77fb9d8Sshaoyunl 	.supports_cwsr = true,
440b77fb9d8Sshaoyunl 	.needs_pci_atomics = false,
441b77fb9d8Sshaoyunl 	.num_sdma_engines = 2,
442b77fb9d8Sshaoyunl 	.num_xgmi_sdma_engines = 0,
443b77fb9d8Sshaoyunl 	.num_sdma_queues_per_engine = 8,
444b77fb9d8Sshaoyunl };
445b77fb9d8Sshaoyunl 
4468099ae40SYong Zhao static const struct kfd_device_info navi14_device_info = {
4478099ae40SYong Zhao 	.asic_family = CHIP_NAVI14,
4488099ae40SYong Zhao 	.asic_name = "navi14",
4498099ae40SYong Zhao 	.max_pasid_bits = 16,
4508099ae40SYong Zhao 	.max_no_of_hqd  = 24,
4518099ae40SYong Zhao 	.doorbell_size  = 8,
4528099ae40SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
4538099ae40SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
4548099ae40SYong Zhao 	.num_of_watch_points = 4,
4558099ae40SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
4568099ae40SYong Zhao 	.needs_iommu_device = false,
4578099ae40SYong Zhao 	.supports_cwsr = true,
4588099ae40SYong Zhao 	.needs_pci_atomics = false,
4598099ae40SYong Zhao 	.num_sdma_engines = 2,
4608099ae40SYong Zhao 	.num_xgmi_sdma_engines = 0,
4618099ae40SYong Zhao 	.num_sdma_queues_per_engine = 8,
4628099ae40SYong Zhao };
4638099ae40SYong Zhao 
4643a2f0c81SYong Zhao static const struct kfd_device_info sienna_cichlid_device_info = {
4653a2f0c81SYong Zhao 	.asic_family = CHIP_SIENNA_CICHLID,
4663a2f0c81SYong Zhao 	.asic_name = "sienna_cichlid",
4673a2f0c81SYong Zhao 	.max_pasid_bits = 16,
4683a2f0c81SYong Zhao 	.max_no_of_hqd  = 24,
4693a2f0c81SYong Zhao 	.doorbell_size  = 8,
4703a2f0c81SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
4713a2f0c81SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
4723a2f0c81SYong Zhao 	.num_of_watch_points = 4,
4733a2f0c81SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
4743a2f0c81SYong Zhao 	.needs_iommu_device = false,
4753a2f0c81SYong Zhao 	.supports_cwsr = true,
4763a2f0c81SYong Zhao 	.needs_pci_atomics = false,
4773a2f0c81SYong Zhao 	.num_sdma_engines = 4,
4783a2f0c81SYong Zhao 	.num_xgmi_sdma_engines = 0,
4793a2f0c81SYong Zhao 	.num_sdma_queues_per_engine = 8,
4803a2f0c81SYong Zhao };
4813a2f0c81SYong Zhao 
482de89b2e4SChengming Gui static const struct kfd_device_info navy_flounder_device_info = {
483de89b2e4SChengming Gui 	.asic_family = CHIP_NAVY_FLOUNDER,
484de89b2e4SChengming Gui 	.asic_name = "navy_flounder",
485de89b2e4SChengming Gui 	.max_pasid_bits = 16,
486de89b2e4SChengming Gui 	.max_no_of_hqd  = 24,
487de89b2e4SChengming Gui 	.doorbell_size  = 8,
488de89b2e4SChengming Gui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
489de89b2e4SChengming Gui 	.event_interrupt_class = &event_interrupt_class_v9,
490de89b2e4SChengming Gui 	.num_of_watch_points = 4,
491de89b2e4SChengming Gui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
492de89b2e4SChengming Gui 	.needs_iommu_device = false,
493de89b2e4SChengming Gui 	.supports_cwsr = true,
494de89b2e4SChengming Gui 	.needs_pci_atomics = false,
495de89b2e4SChengming Gui 	.num_sdma_engines = 2,
496de89b2e4SChengming Gui 	.num_xgmi_sdma_engines = 0,
497de89b2e4SChengming Gui 	.num_sdma_queues_per_engine = 8,
498de89b2e4SChengming Gui };
499de89b2e4SChengming Gui 
500050091abSYong Zhao /* For each entry, [0] is regular and [1] is virtualisation device. */
501050091abSYong Zhao static const struct kfd_device_info *kfd_supported_devices[][2] = {
50295a5bd1bSYong Zhao #ifdef KFD_SUPPORT_IOMMU_V2
503050091abSYong Zhao 	[CHIP_KAVERI] = {&kaveri_device_info, NULL},
50495a5bd1bSYong Zhao 	[CHIP_CARRIZO] = {&carrizo_device_info, NULL},
50595a5bd1bSYong Zhao 	[CHIP_RAVEN] = {&raven_device_info, NULL},
50695a5bd1bSYong Zhao #endif
507050091abSYong Zhao 	[CHIP_HAWAII] = {&hawaii_device_info, NULL},
508050091abSYong Zhao 	[CHIP_TONGA] = {&tonga_device_info, NULL},
509050091abSYong Zhao 	[CHIP_FIJI] = {&fiji_device_info, &fiji_vf_device_info},
510050091abSYong Zhao 	[CHIP_POLARIS10] = {&polaris10_device_info, &polaris10_vf_device_info},
511050091abSYong Zhao 	[CHIP_POLARIS11] = {&polaris11_device_info, NULL},
512050091abSYong Zhao 	[CHIP_POLARIS12] = {&polaris12_device_info, NULL},
513050091abSYong Zhao 	[CHIP_VEGAM] = {&vegam_device_info, NULL},
514050091abSYong Zhao 	[CHIP_VEGA10] = {&vega10_device_info, &vega10_vf_device_info},
515050091abSYong Zhao 	[CHIP_VEGA12] = {&vega12_device_info, NULL},
516050091abSYong Zhao 	[CHIP_VEGA20] = {&vega20_device_info, NULL},
5172b9c2211SHuang Rui 	[CHIP_RENOIR] = {&renoir_device_info, NULL},
518050091abSYong Zhao 	[CHIP_ARCTURUS] = {&arcturus_device_info, &arcturus_device_info},
519050091abSYong Zhao 	[CHIP_NAVI10] = {&navi10_device_info, NULL},
520b77fb9d8Sshaoyunl 	[CHIP_NAVI12] = {&navi12_device_info, &navi12_device_info},
5218099ae40SYong Zhao 	[CHIP_NAVI14] = {&navi14_device_info, NULL},
522adab4dadSshaoyunl 	[CHIP_SIENNA_CICHLID] = {&sienna_cichlid_device_info, &sienna_cichlid_device_info},
523de89b2e4SChengming Gui 	[CHIP_NAVY_FLOUNDER] = {&navy_flounder_device_info, &navy_flounder_device_info},
5244a488a7aSOded Gabbay };
5254a488a7aSOded Gabbay 
5266e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
5276e81090bSOded Gabbay 				unsigned int chunk_size);
5286e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
5296e81090bSOded Gabbay 
530b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
531b8935a7cSYong Zhao 
532cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
533e392c887SYong Zhao 	struct pci_dev *pdev, unsigned int asic_type, bool vf)
5344a488a7aSOded Gabbay {
5354a488a7aSOded Gabbay 	struct kfd_dev *kfd;
536050091abSYong Zhao 	const struct kfd_device_info *device_info;
537e392c887SYong Zhao 	const struct kfd2kgd_calls *f2g;
538050091abSYong Zhao 
539e392c887SYong Zhao 	if (asic_type >= sizeof(kfd_supported_devices) / (sizeof(void *) * 2)
540e392c887SYong Zhao 		|| asic_type >= sizeof(kfd2kgd_funcs) / sizeof(void *)) {
541050091abSYong Zhao 		dev_err(kfd_device, "asic_type %d out of range\n", asic_type);
542050091abSYong Zhao 		return NULL; /* asic_type out of range */
543050091abSYong Zhao 	}
544050091abSYong Zhao 
545050091abSYong Zhao 	device_info = kfd_supported_devices[asic_type][vf];
546e392c887SYong Zhao 	f2g = kfd2kgd_funcs[asic_type];
5474a488a7aSOded Gabbay 
548aa5e899dSDan Carpenter 	if (!device_info || !f2g) {
549050091abSYong Zhao 		dev_err(kfd_device, "%s %s not supported in kfd\n",
550050091abSYong Zhao 			amdgpu_asic_name[asic_type], vf ? "VF" : "");
5514a488a7aSOded Gabbay 		return NULL;
5524ebc7182SYong Zhao 	}
5534a488a7aSOded Gabbay 
554d35f00d8SEric Huang 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
555d35f00d8SEric Huang 	if (!kfd)
556d35f00d8SEric Huang 		return NULL;
557d35f00d8SEric Huang 
5586106dce9Swelu 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
5596106dce9Swelu 	 * 32 and 64-bit requests are possible and must be
5606106dce9Swelu 	 * supported.
5613ee2d00cSFelix Kuehling 	 */
562aabf3a95SJack Xiao 	kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kgd);
563aabf3a95SJack Xiao 	if (device_info->needs_pci_atomics &&
564aabf3a95SJack Xiao 	    !kfd->pci_atomic_requested) {
5653ee2d00cSFelix Kuehling 		dev_info(kfd_device,
5666106dce9Swelu 			 "skipped device %x:%x, PCI rejects atomics\n",
5673ee2d00cSFelix Kuehling 			 pdev->vendor, pdev->device);
568d35f00d8SEric Huang 		kfree(kfd);
5693ee2d00cSFelix Kuehling 		return NULL;
570aabf3a95SJack Xiao 	}
5714a488a7aSOded Gabbay 
5724a488a7aSOded Gabbay 	kfd->kgd = kgd;
5734a488a7aSOded Gabbay 	kfd->device_info = device_info;
5744a488a7aSOded Gabbay 	kfd->pdev = pdev;
57519f6d2a6SOded Gabbay 	kfd->init_complete = false;
576cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
57743d8107fSHarish Kasiviswanathan 	atomic_set(&kfd->compute_profile, 0);
578cea405b1SXihan Zhang 
579cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
580cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
581cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
5824a488a7aSOded Gabbay 
5839b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
5849b54d201SEric Huang 
5854a488a7aSOded Gabbay 	return kfd;
5864a488a7aSOded Gabbay }
5874a488a7aSOded Gabbay 
588373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
589373d7080SFelix Kuehling {
590373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
5913e76c239SFelix Kuehling 		if (kfd->device_info->asic_family < CHIP_VEGA10) {
592373d7080SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
593373d7080SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
594373d7080SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
5953baa24f0SOak Zeng 		} else if (kfd->device_info->asic_family == CHIP_ARCTURUS) {
5963baa24f0SOak Zeng 			BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
5973baa24f0SOak Zeng 			kfd->cwsr_isa = cwsr_trap_arcturus_hex;
5983baa24f0SOak Zeng 			kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
59914328aa5SPhilip Cox 		} else if (kfd->device_info->asic_family < CHIP_NAVI10) {
6003e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
6013e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
6023e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
60380b6cfedSJay Cornwall 		} else if (kfd->device_info->asic_family < CHIP_SIENNA_CICHLID) {
60480b6cfedSJay Cornwall 			BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE);
60580b6cfedSJay Cornwall 			kfd->cwsr_isa = cwsr_trap_nv1x_hex;
60680b6cfedSJay Cornwall 			kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);
60714328aa5SPhilip Cox 		} else {
60814328aa5SPhilip Cox 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
60914328aa5SPhilip Cox 			kfd->cwsr_isa = cwsr_trap_gfx10_hex;
61014328aa5SPhilip Cox 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
6113e76c239SFelix Kuehling 		}
6123e76c239SFelix Kuehling 
613373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
614373d7080SFelix Kuehling 	}
615373d7080SFelix Kuehling }
616373d7080SFelix Kuehling 
61729633d0eSJoseph Greathouse static int kfd_gws_init(struct kfd_dev *kfd)
61829633d0eSJoseph Greathouse {
61929633d0eSJoseph Greathouse 	int ret = 0;
62029633d0eSJoseph Greathouse 
62129633d0eSJoseph Greathouse 	if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
62229633d0eSJoseph Greathouse 		return 0;
62329633d0eSJoseph Greathouse 
62429633d0eSJoseph Greathouse 	if (hws_gws_support
625fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family == CHIP_VEGA10
626fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x81b3)
627fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family >= CHIP_VEGA12
62829633d0eSJoseph Greathouse 			&& kfd->device_info->asic_family <= CHIP_RAVEN
629fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x1b3)
630fea7d919SJoseph Greathouse 		|| (kfd->device_info->asic_family == CHIP_ARCTURUS
631fea7d919SJoseph Greathouse 			&& kfd->mec2_fw_version >= 0x30))
63229633d0eSJoseph Greathouse 		ret = amdgpu_amdkfd_alloc_gws(kfd->kgd,
63329633d0eSJoseph Greathouse 				amdgpu_amdkfd_get_num_gws(kfd->kgd), &kfd->gws);
63429633d0eSJoseph Greathouse 
63529633d0eSJoseph Greathouse 	return ret;
63629633d0eSJoseph Greathouse }
63729633d0eSJoseph Greathouse 
638*938a0650SAmber Lin static void kfd_smi_init(struct kfd_dev *dev) {
639*938a0650SAmber Lin 	INIT_LIST_HEAD(&dev->smi_clients);
640*938a0650SAmber Lin 	spin_lock_init(&dev->smi_lock);
641*938a0650SAmber Lin }
642*938a0650SAmber Lin 
6434a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
6443a0c3423SHarish Kasiviswanathan 			 struct drm_device *ddev,
6454a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
6464a488a7aSOded Gabbay {
64719f6d2a6SOded Gabbay 	unsigned int size;
64819f6d2a6SOded Gabbay 
6493a0c3423SHarish Kasiviswanathan 	kfd->ddev = ddev;
6500da8b10eSAmber Lin 	kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
6515ade6c9cSFelix Kuehling 			KGD_ENGINE_MEC1);
65229633d0eSJoseph Greathouse 	kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
65329633d0eSJoseph Greathouse 			KGD_ENGINE_MEC2);
6540da8b10eSAmber Lin 	kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
6555ade6c9cSFelix Kuehling 			KGD_ENGINE_SDMA1);
6564a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
6574a488a7aSOded Gabbay 
65844008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
65944008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
66044008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
66144008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
66244008d7aSYong Zhao 
663a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
664a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
665a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
666a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
667a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
668a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
669a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
670a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
671a99c6d4fSFelix Kuehling 	} else
672a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
673a99c6d4fSFelix Kuehling 
67419f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
675b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
67619f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
67719f6d2a6SOded Gabbay 
678e18e794eSOded Gabbay 	/*
679e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
680e18e794eSOded Gabbay 	 * There can be only 2 packets at once
681e18e794eSOded Gabbay 	 */
682507968ddSFelix Kuehling 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
683507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
684507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
685e18e794eSOded Gabbay 
686e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
687e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
688e18e794eSOded Gabbay 
689e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
69019f6d2a6SOded Gabbay 	size += 512 * 1024;
69119f6d2a6SOded Gabbay 
6927cd52c91SAmber Lin 	if (amdgpu_amdkfd_alloc_gtt_mem(
693cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
69415426dbbSYong Zhao 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
69515426dbbSYong Zhao 			false)) {
69679775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
697e09d4fc8SOak Zeng 		goto alloc_gtt_mem_failure;
69819f6d2a6SOded Gabbay 	}
69919f6d2a6SOded Gabbay 
70079775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
701e18e794eSOded Gabbay 
70273a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
70373a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
70479775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
70573a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
70673a1da0bSOded Gabbay 	}
70773a1da0bSOded Gabbay 
708735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
709735df2baSFelix Kuehling 		dev_err(kfd_device,
710735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
711735df2baSFelix Kuehling 		goto kfd_doorbell_error;
712735df2baSFelix Kuehling 	}
71319f6d2a6SOded Gabbay 
7140c1690e3SShaoyun Liu 	if (kfd->kfd2kgd->get_hive_id)
7150c1690e3SShaoyun Liu 		kfd->hive_id = kfd->kfd2kgd->get_hive_id(kfd->kgd);
7160c1690e3SShaoyun Liu 
7170c663695SDivya Shikre 	if (kfd->kfd2kgd->get_unique_id)
7180c663695SDivya Shikre 		kfd->unique_id = kfd->kfd2kgd->get_unique_id(kfd->kgd);
7190c663695SDivya Shikre 
7202249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
72179775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
7222249d558SAndrew Lewycky 		goto kfd_interrupt_error;
7232249d558SAndrew Lewycky 	}
7242249d558SAndrew Lewycky 
72564c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
72664c7f8cfSBen Goz 	if (!kfd->dqm) {
72779775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
72864c7f8cfSBen Goz 		goto device_queue_manager_error;
72964c7f8cfSBen Goz 	}
73064c7f8cfSBen Goz 
73129633d0eSJoseph Greathouse 	/* If supported on this device, allocate global GWS that is shared
73229633d0eSJoseph Greathouse 	 * by all KFD processes
73329633d0eSJoseph Greathouse 	 */
73429633d0eSJoseph Greathouse 	if (kfd_gws_init(kfd)) {
73529633d0eSJoseph Greathouse 		dev_err(kfd_device, "Could not allocate %d gws\n",
73629633d0eSJoseph Greathouse 			amdgpu_amdkfd_get_num_gws(kfd->kgd));
73729633d0eSJoseph Greathouse 		goto gws_error;
73829633d0eSJoseph Greathouse 	}
73929633d0eSJoseph Greathouse 
74064d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
74164d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
74264d1c3a4SFelix Kuehling 		goto device_iommu_error;
74364c7f8cfSBen Goz 	}
74464c7f8cfSBen Goz 
745373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
746373d7080SFelix Kuehling 
747b8935a7cSYong Zhao 	if (kfd_resume(kfd))
748b8935a7cSYong Zhao 		goto kfd_resume_error;
749b8935a7cSYong Zhao 
750fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
751fbeb661bSYair Shachar 
752465ab9e0SOak Zeng 	if (kfd_topology_add_device(kfd)) {
753465ab9e0SOak Zeng 		dev_err(kfd_device, "Error adding device to topology\n");
754465ab9e0SOak Zeng 		goto kfd_topology_add_device_error;
755465ab9e0SOak Zeng 	}
756465ab9e0SOak Zeng 
757*938a0650SAmber Lin 	kfd_smi_init(kfd);
758*938a0650SAmber Lin 
7594a488a7aSOded Gabbay 	kfd->init_complete = true;
76079775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
7614a488a7aSOded Gabbay 		 kfd->pdev->device);
7624a488a7aSOded Gabbay 
76379775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
764d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
76564c7f8cfSBen Goz 
76619f6d2a6SOded Gabbay 	goto out;
76719f6d2a6SOded Gabbay 
768465ab9e0SOak Zeng kfd_topology_add_device_error:
769b8935a7cSYong Zhao kfd_resume_error:
77064d1c3a4SFelix Kuehling device_iommu_error:
77129633d0eSJoseph Greathouse gws_error:
77264c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
77364c7f8cfSBen Goz device_queue_manager_error:
7742249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
7752249d558SAndrew Lewycky kfd_interrupt_error:
776735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
777735df2baSFelix Kuehling kfd_doorbell_error:
77873a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
77973a1da0bSOded Gabbay kfd_gtt_sa_init_error:
7807cd52c91SAmber Lin 	amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
781e09d4fc8SOak Zeng alloc_gtt_mem_failure:
78229633d0eSJoseph Greathouse 	if (kfd->gws)
783e09d4fc8SOak Zeng 		amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
78419f6d2a6SOded Gabbay 	dev_err(kfd_device,
78579775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
78619f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
78719f6d2a6SOded Gabbay out:
78819f6d2a6SOded Gabbay 	return kfd->init_complete;
7894a488a7aSOded Gabbay }
7904a488a7aSOded Gabbay 
7914a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
7924a488a7aSOded Gabbay {
793b17f068aSOded Gabbay 	if (kfd->init_complete) {
7949593f4d6SRajneesh Bhardwaj 		kgd2kfd_suspend(kfd, false);
79564c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
7962249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
79719f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
798735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
79973a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
8007cd52c91SAmber Lin 		amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
80129633d0eSJoseph Greathouse 		if (kfd->gws)
802e09d4fc8SOak Zeng 			amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
803b17f068aSOded Gabbay 	}
8045b5c4e40SEvgeny Pinchuk 
8054a488a7aSOded Gabbay 	kfree(kfd);
8064a488a7aSOded Gabbay }
8074a488a7aSOded Gabbay 
808e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
809e3b7a967SShaoyun Liu {
810e42051d2SShaoyun Liu 	if (!kfd->init_complete)
811e42051d2SShaoyun Liu 		return 0;
81209c34e8dSFelix Kuehling 
81309c34e8dSFelix Kuehling 	kfd->dqm->ops.pre_reset(kfd->dqm);
81409c34e8dSFelix Kuehling 
8159593f4d6SRajneesh Bhardwaj 	kgd2kfd_suspend(kfd, false);
816e42051d2SShaoyun Liu 
817e42051d2SShaoyun Liu 	kfd_signal_reset_event(kfd);
818e3b7a967SShaoyun Liu 	return 0;
819e3b7a967SShaoyun Liu }
820e3b7a967SShaoyun Liu 
821e42051d2SShaoyun Liu /*
822e42051d2SShaoyun Liu  * Fix me. KFD won't be able to resume existing process for now.
823e42051d2SShaoyun Liu  * We will keep all existing process in a evicted state and
824e42051d2SShaoyun Liu  * wait the process to be terminated.
825e42051d2SShaoyun Liu  */
826e42051d2SShaoyun Liu 
827e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
828e3b7a967SShaoyun Liu {
829a1bd079fSyu kuai 	int ret;
830e42051d2SShaoyun Liu 
831e42051d2SShaoyun Liu 	if (!kfd->init_complete)
832e3b7a967SShaoyun Liu 		return 0;
833e42051d2SShaoyun Liu 
834e42051d2SShaoyun Liu 	ret = kfd_resume(kfd);
835e42051d2SShaoyun Liu 	if (ret)
836e42051d2SShaoyun Liu 		return ret;
837a1bd079fSyu kuai 	atomic_dec(&kfd_locked);
8389b54d201SEric Huang 
8399b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
8409b54d201SEric Huang 
841e42051d2SShaoyun Liu 	return 0;
842e42051d2SShaoyun Liu }
843e42051d2SShaoyun Liu 
844e42051d2SShaoyun Liu bool kfd_is_locked(void)
845e42051d2SShaoyun Liu {
846e42051d2SShaoyun Liu 	return  (atomic_read(&kfd_locked) > 0);
847e3b7a967SShaoyun Liu }
848e3b7a967SShaoyun Liu 
8499593f4d6SRajneesh Bhardwaj void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm)
8504a488a7aSOded Gabbay {
851733fa1f7SYong Zhao 	if (!kfd->init_complete)
852733fa1f7SYong Zhao 		return;
853733fa1f7SYong Zhao 
8549593f4d6SRajneesh Bhardwaj 	/* for runtime suspend, skip locking kfd */
8559593f4d6SRajneesh Bhardwaj 	if (!run_pm) {
85626103436SFelix Kuehling 		/* For first KFD device suspend all the KFD processes */
857e42051d2SShaoyun Liu 		if (atomic_inc_return(&kfd_locked) == 1)
85826103436SFelix Kuehling 			kfd_suspend_all_processes();
8599593f4d6SRajneesh Bhardwaj 	}
86026103436SFelix Kuehling 
86145c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
86264d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
8634a488a7aSOded Gabbay }
8644a488a7aSOded Gabbay 
8659593f4d6SRajneesh Bhardwaj int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm)
8664a488a7aSOded Gabbay {
86726103436SFelix Kuehling 	int ret, count;
86826103436SFelix Kuehling 
869b8935a7cSYong Zhao 	if (!kfd->init_complete)
870b8935a7cSYong Zhao 		return 0;
871b17f068aSOded Gabbay 
87226103436SFelix Kuehling 	ret = kfd_resume(kfd);
87326103436SFelix Kuehling 	if (ret)
87426103436SFelix Kuehling 		return ret;
875b17f068aSOded Gabbay 
8769593f4d6SRajneesh Bhardwaj 	/* for runtime resume, skip unlocking kfd */
8779593f4d6SRajneesh Bhardwaj 	if (!run_pm) {
878e42051d2SShaoyun Liu 		count = atomic_dec_return(&kfd_locked);
87926103436SFelix Kuehling 		WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
88026103436SFelix Kuehling 		if (count == 0)
88126103436SFelix Kuehling 			ret = kfd_resume_all_processes();
8829593f4d6SRajneesh Bhardwaj 	}
88326103436SFelix Kuehling 
88426103436SFelix Kuehling 	return ret;
8854ebc7182SYong Zhao }
8864ebc7182SYong Zhao 
887b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
888b8935a7cSYong Zhao {
889b8935a7cSYong Zhao 	int err = 0;
890b8935a7cSYong Zhao 
89164d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
89264d1c3a4SFelix Kuehling 	if (err) {
89364d1c3a4SFelix Kuehling 		dev_err(kfd_device,
89464d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
89564d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
89664d1c3a4SFelix Kuehling 		return err;
89764d1c3a4SFelix Kuehling 	}
898733fa1f7SYong Zhao 
899b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
900b8935a7cSYong Zhao 	if (err) {
901b8935a7cSYong Zhao 		dev_err(kfd_device,
902b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
903b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
904b8935a7cSYong Zhao 		goto dqm_start_error;
905b17f068aSOded Gabbay 	}
906b17f068aSOded Gabbay 
907b8935a7cSYong Zhao 	return err;
908b8935a7cSYong Zhao 
909b8935a7cSYong Zhao dqm_start_error:
91064d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
911b8935a7cSYong Zhao 	return err;
9124a488a7aSOded Gabbay }
9134a488a7aSOded Gabbay 
914b3eca59dSPhilip Yang static inline void kfd_queue_work(struct workqueue_struct *wq,
915b3eca59dSPhilip Yang 				  struct work_struct *work)
916b3eca59dSPhilip Yang {
917b3eca59dSPhilip Yang 	int cpu, new_cpu;
918b3eca59dSPhilip Yang 
919b3eca59dSPhilip Yang 	cpu = new_cpu = smp_processor_id();
920b3eca59dSPhilip Yang 	do {
921b3eca59dSPhilip Yang 		new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
922b3eca59dSPhilip Yang 		if (cpu_to_node(new_cpu) == numa_node_id())
923b3eca59dSPhilip Yang 			break;
924b3eca59dSPhilip Yang 	} while (cpu != new_cpu);
925b3eca59dSPhilip Yang 
926b3eca59dSPhilip Yang 	queue_work_on(new_cpu, wq, work);
927b3eca59dSPhilip Yang }
928b3eca59dSPhilip Yang 
929b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
930b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
9314a488a7aSOded Gabbay {
93258e69886SLan Xiao 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
93358e69886SLan Xiao 	bool is_patched = false;
9342383a767SChristian König 	unsigned long flags;
93558e69886SLan Xiao 
9362249d558SAndrew Lewycky 	if (!kfd->init_complete)
9372249d558SAndrew Lewycky 		return;
9382249d558SAndrew Lewycky 
93958e69886SLan Xiao 	if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
94058e69886SLan Xiao 		dev_err_once(kfd_device, "Ring entry too small\n");
94158e69886SLan Xiao 		return;
94258e69886SLan Xiao 	}
94358e69886SLan Xiao 
9442383a767SChristian König 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
9452249d558SAndrew Lewycky 
9462249d558SAndrew Lewycky 	if (kfd->interrupts_active
94758e69886SLan Xiao 	    && interrupt_is_wanted(kfd, ih_ring_entry,
94858e69886SLan Xiao 				   patched_ihre, &is_patched)
94958e69886SLan Xiao 	    && enqueue_ih_ring_entry(kfd,
95058e69886SLan Xiao 				     is_patched ? patched_ihre : ih_ring_entry))
951b3eca59dSPhilip Yang 		kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work);
9522249d558SAndrew Lewycky 
9532383a767SChristian König 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
9544a488a7aSOded Gabbay }
9556e81090bSOded Gabbay 
9566b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
9576b95e797SFelix Kuehling {
9586b95e797SFelix Kuehling 	struct kfd_process *p;
9596b95e797SFelix Kuehling 	int r;
9606b95e797SFelix Kuehling 
9616b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
9626b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
9636b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
9646b95e797SFelix Kuehling 	 */
9656b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
9666b95e797SFelix Kuehling 	if (!p)
9676b95e797SFelix Kuehling 		return -ESRCH;
9686b95e797SFelix Kuehling 
969b2057956SFelix Kuehling 	WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
9706b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
9716b95e797SFelix Kuehling 
9726b95e797SFelix Kuehling 	kfd_unref_process(p);
9736b95e797SFelix Kuehling 	return r;
9746b95e797SFelix Kuehling }
9756b95e797SFelix Kuehling 
9766b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
9776b95e797SFelix Kuehling {
9786b95e797SFelix Kuehling 	struct kfd_process *p;
9796b95e797SFelix Kuehling 	int r;
9806b95e797SFelix Kuehling 
9816b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
9826b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
9836b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
9846b95e797SFelix Kuehling 	 */
9856b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
9866b95e797SFelix Kuehling 	if (!p)
9876b95e797SFelix Kuehling 		return -ESRCH;
9886b95e797SFelix Kuehling 
9896b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
9906b95e797SFelix Kuehling 
9916b95e797SFelix Kuehling 	kfd_unref_process(p);
9926b95e797SFelix Kuehling 	return r;
9936b95e797SFelix Kuehling }
9946b95e797SFelix Kuehling 
99526103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
99626103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
99726103436SFelix Kuehling  *   process.
99826103436SFelix Kuehling  *
99926103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
100026103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
100126103436SFelix Kuehling  *
100226103436SFelix Kuehling  */
100326103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
100426103436SFelix Kuehling 					       struct dma_fence *fence)
100526103436SFelix Kuehling {
100626103436SFelix Kuehling 	struct kfd_process *p;
100726103436SFelix Kuehling 	unsigned long active_time;
100826103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
100926103436SFelix Kuehling 
101026103436SFelix Kuehling 	if (!fence)
101126103436SFelix Kuehling 		return -EINVAL;
101226103436SFelix Kuehling 
101326103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
101426103436SFelix Kuehling 		return 0;
101526103436SFelix Kuehling 
101626103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
101726103436SFelix Kuehling 	if (!p)
101826103436SFelix Kuehling 		return -ENODEV;
101926103436SFelix Kuehling 
102026103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
102126103436SFelix Kuehling 		goto out;
102226103436SFelix Kuehling 
102326103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
102426103436SFelix Kuehling 
102526103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
102626103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
102726103436SFelix Kuehling 	 */
102826103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
102926103436SFelix Kuehling 	if (delay_jiffies > active_time)
103026103436SFelix Kuehling 		delay_jiffies -= active_time;
103126103436SFelix Kuehling 	else
103226103436SFelix Kuehling 		delay_jiffies = 0;
103326103436SFelix Kuehling 
103426103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
103526103436SFelix Kuehling 	 * to kfd_evict_bo_worker
103626103436SFelix Kuehling 	 */
1037b2057956SFelix Kuehling 	WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",
1038b2057956SFelix Kuehling 	     p->lead_thread->pid, delay_jiffies);
103926103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
104026103436SFelix Kuehling out:
104126103436SFelix Kuehling 	kfd_unref_process(p);
104226103436SFelix Kuehling 	return 0;
104326103436SFelix Kuehling }
104426103436SFelix Kuehling 
10456e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
10466e81090bSOded Gabbay 				unsigned int chunk_size)
10476e81090bSOded Gabbay {
10488625ff9cSFelix Kuehling 	unsigned int num_of_longs;
10496e81090bSOded Gabbay 
105032fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
105132fa8219SFelix Kuehling 		return -EINVAL;
105232fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
105332fa8219SFelix Kuehling 		return -EINVAL;
105432fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
105532fa8219SFelix Kuehling 		return -EINVAL;
10566e81090bSOded Gabbay 
10576e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
10586e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
10596e81090bSOded Gabbay 
10608625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
10618625ff9cSFelix Kuehling 		BITS_PER_LONG;
10626e81090bSOded Gabbay 
10638625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
10646e81090bSOded Gabbay 
10656e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
10666e81090bSOded Gabbay 		return -ENOMEM;
10676e81090bSOded Gabbay 
106879775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
10696e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
10706e81090bSOded Gabbay 
10716e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
10726e81090bSOded Gabbay 
10736e81090bSOded Gabbay 	return 0;
10746e81090bSOded Gabbay 
10756e81090bSOded Gabbay }
10766e81090bSOded Gabbay 
10776e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
10786e81090bSOded Gabbay {
10796e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
10806e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
10816e81090bSOded Gabbay }
10826e81090bSOded Gabbay 
10836e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
10846e81090bSOded Gabbay 						unsigned int bit_num,
10856e81090bSOded Gabbay 						unsigned int chunk_size)
10866e81090bSOded Gabbay {
10876e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
10886e81090bSOded Gabbay }
10896e81090bSOded Gabbay 
10906e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
10916e81090bSOded Gabbay 						unsigned int bit_num,
10926e81090bSOded Gabbay 						unsigned int chunk_size)
10936e81090bSOded Gabbay {
10946e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
10956e81090bSOded Gabbay }
10966e81090bSOded Gabbay 
10976e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
10986e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
10996e81090bSOded Gabbay {
11006e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
11016e81090bSOded Gabbay 
11026e81090bSOded Gabbay 	if (size == 0)
11036e81090bSOded Gabbay 		return -EINVAL;
11046e81090bSOded Gabbay 
11056e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
11066e81090bSOded Gabbay 		return -ENOMEM;
11076e81090bSOded Gabbay 
11081cd106ecSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
11091cd106ecSFelix Kuehling 	if (!(*mem_obj))
11106e81090bSOded Gabbay 		return -ENOMEM;
11116e81090bSOded Gabbay 
111279775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
11136e81090bSOded Gabbay 
11146e81090bSOded Gabbay 	start_search = 0;
11156e81090bSOded Gabbay 
11166e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
11176e81090bSOded Gabbay 
11186e81090bSOded Gabbay kfd_gtt_restart_search:
11196e81090bSOded Gabbay 	/* Find the first chunk that is free */
11206e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
11216e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
11226e81090bSOded Gabbay 					start_search);
11236e81090bSOded Gabbay 
112479775b62SKent Russell 	pr_debug("Found = %d\n", found);
11256e81090bSOded Gabbay 
11266e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
11276e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
11286e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
11296e81090bSOded Gabbay 
11306e81090bSOded Gabbay 	/* Update fields of mem_obj */
11316e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
11326e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
11336e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
11346e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
11356e81090bSOded Gabbay 					found,
11366e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
11376e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
11386e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
11396e81090bSOded Gabbay 					found,
11406e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
11416e81090bSOded Gabbay 
114279775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
11436e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
11446e81090bSOded Gabbay 
11456e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
11466e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
114779775b62SKent Russell 		pr_debug("Single bit\n");
11486e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
11496e81090bSOded Gabbay 		goto kfd_gtt_out;
11506e81090bSOded Gabbay 	}
11516e81090bSOded Gabbay 
11526e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
11536e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
11546e81090bSOded Gabbay 	do {
11556e81090bSOded Gabbay 		(*mem_obj)->range_end =
11566e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
11576e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
11586e81090bSOded Gabbay 		/*
11596e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
11606e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
11616e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
11626e81090bSOded Gabbay 		 */
11636e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
11646e81090bSOded Gabbay 			start_search = found;
11656e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
11666e81090bSOded Gabbay 		}
11676e81090bSOded Gabbay 
11686e81090bSOded Gabbay 		/*
11696e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
11706e81090bSOded Gabbay 		 */
11716e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
11726e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
11736e81090bSOded Gabbay 
11746e81090bSOded Gabbay 		/* Check if we don't need another chunk */
11756e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
11766e81090bSOded Gabbay 			cur_size = 0;
11776e81090bSOded Gabbay 		else
11786e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
11796e81090bSOded Gabbay 
11806e81090bSOded Gabbay 	} while (cur_size > 0);
11816e81090bSOded Gabbay 
118279775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
11836e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
11846e81090bSOded Gabbay 
11856e81090bSOded Gabbay 	/* Mark the chunks as allocated */
11866e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
11876e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
11886e81090bSOded Gabbay 		found++)
11896e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
11906e81090bSOded Gabbay 
11916e81090bSOded Gabbay kfd_gtt_out:
11926e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
11936e81090bSOded Gabbay 	return 0;
11946e81090bSOded Gabbay 
11956e81090bSOded Gabbay kfd_gtt_no_free_chunk:
11963148a6a0SJack Zhang 	pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);
11976e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
11983148a6a0SJack Zhang 	kfree(*mem_obj);
11996e81090bSOded Gabbay 	return -ENOMEM;
12006e81090bSOded Gabbay }
12016e81090bSOded Gabbay 
12026e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
12036e81090bSOded Gabbay {
12046e81090bSOded Gabbay 	unsigned int bit;
12056e81090bSOded Gabbay 
12069216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
12079216ed29SOded Gabbay 	if (!mem_obj)
12089216ed29SOded Gabbay 		return 0;
12096e81090bSOded Gabbay 
121079775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
12116e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
12126e81090bSOded Gabbay 
12136e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
12146e81090bSOded Gabbay 
12156e81090bSOded Gabbay 	/* Mark the chunks as free */
12166e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
12176e81090bSOded Gabbay 		bit <= mem_obj->range_end;
12186e81090bSOded Gabbay 		bit++)
12196e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
12206e81090bSOded Gabbay 
12216e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
12226e81090bSOded Gabbay 
12236e81090bSOded Gabbay 	kfree(mem_obj);
12246e81090bSOded Gabbay 	return 0;
12256e81090bSOded Gabbay }
1226a29ec470SShaoyun Liu 
12279b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
12289b54d201SEric Huang {
12299b54d201SEric Huang 	if (kfd)
12309b54d201SEric Huang 		atomic_inc(&kfd->sram_ecc_flag);
12319b54d201SEric Huang }
12329b54d201SEric Huang 
123343d8107fSHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd)
123443d8107fSHarish Kasiviswanathan {
123543d8107fSHarish Kasiviswanathan 	if (atomic_inc_return(&kfd->compute_profile) == 1)
123643d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, false);
123743d8107fSHarish Kasiviswanathan }
123843d8107fSHarish Kasiviswanathan 
123943d8107fSHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd)
124043d8107fSHarish Kasiviswanathan {
124143d8107fSHarish Kasiviswanathan 	int count = atomic_dec_return(&kfd->compute_profile);
124243d8107fSHarish Kasiviswanathan 
124343d8107fSHarish Kasiviswanathan 	if (count == 0)
124443d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, true);
124543d8107fSHarish Kasiviswanathan 	WARN_ONCE(count < 0, "Compute profile ref. count error");
124643d8107fSHarish Kasiviswanathan }
124743d8107fSHarish Kasiviswanathan 
1248a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
1249a29ec470SShaoyun Liu 
1250a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
1251a29ec470SShaoyun Liu  * which will trigger a GPU reset and bring the HWS back to normal state
1252a29ec470SShaoyun Liu  */
1253a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1254a29ec470SShaoyun Liu {
1255a29ec470SShaoyun Liu 	int r = 0;
1256a29ec470SShaoyun Liu 
1257a29ec470SShaoyun Liu 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1258a29ec470SShaoyun Liu 		pr_err("HWS is not enabled");
1259a29ec470SShaoyun Liu 		return -EINVAL;
1260a29ec470SShaoyun Liu 	}
1261a29ec470SShaoyun Liu 
1262a29ec470SShaoyun Liu 	r = pm_debugfs_hang_hws(&dev->dqm->packets);
1263a29ec470SShaoyun Liu 	if (!r)
1264a29ec470SShaoyun Liu 		r = dqm_debugfs_execute_queues(dev->dqm);
1265a29ec470SShaoyun Liu 
1266a29ec470SShaoyun Liu 	return r;
1267a29ec470SShaoyun Liu }
1268a29ec470SShaoyun Liu 
1269a29ec470SShaoyun Liu #endif
1270