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 
4264d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
434a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = {
440da7558cSBen Goz 	.asic_family = CHIP_KAVERI,
45c181159aSYong Zhao 	.asic_name = "kaveri",
460da7558cSBen Goz 	.max_pasid_bits = 16,
47992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
48992839adSYair Shachar 	.max_no_of_hqd	= 24,
49ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
500da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
51f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
52fbeb661bSYair Shachar 	.num_of_watch_points = 4,
53373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
54373d7080SFelix Kuehling 	.supports_cwsr = false,
5564d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
563ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
5798bb9222SYong Zhao 	.num_sdma_engines = 2,
581b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
59d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
600da7558cSBen Goz };
610da7558cSBen Goz 
620da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
630da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
64c181159aSYong Zhao 	.asic_name = "carrizo",
654a488a7aSOded Gabbay 	.max_pasid_bits = 16,
66eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
67eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
68ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
69b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
70eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
71f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
72373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
73373d7080SFelix Kuehling 	.supports_cwsr = true,
7464d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
753ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
7698bb9222SYong Zhao 	.num_sdma_engines = 2,
771b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
78d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
794a488a7aSOded Gabbay };
804d663df6SYong Zhao 
814d663df6SYong Zhao static const struct kfd_device_info raven_device_info = {
824d663df6SYong Zhao 	.asic_family = CHIP_RAVEN,
83c181159aSYong Zhao 	.asic_name = "raven",
844d663df6SYong Zhao 	.max_pasid_bits = 16,
854d663df6SYong Zhao 	.max_no_of_hqd  = 24,
864d663df6SYong Zhao 	.doorbell_size  = 8,
874d663df6SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
884d663df6SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
894d663df6SYong Zhao 	.num_of_watch_points = 4,
904d663df6SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
914d663df6SYong Zhao 	.supports_cwsr = true,
924d663df6SYong Zhao 	.needs_iommu_device = true,
934d663df6SYong Zhao 	.needs_pci_atomics = true,
944d663df6SYong Zhao 	.num_sdma_engines = 1,
951b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
96d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
974d663df6SYong Zhao };
9864d1c3a4SFelix Kuehling #endif
994a488a7aSOded Gabbay 
100a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
101a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
102c181159aSYong Zhao 	.asic_name = "hawaii",
103a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
104a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
105a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
106ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
107a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
108a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
109a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
110a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
111a3084e6cSFelix Kuehling 	.supports_cwsr = false,
11264d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
113a3084e6cSFelix 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,
117a3084e6cSFelix Kuehling };
118a3084e6cSFelix Kuehling 
119a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
120a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
121c181159aSYong Zhao 	.asic_name = "tonga",
122a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
123a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
124ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
125a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
126a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
127a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
128a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
129a3084e6cSFelix Kuehling 	.supports_cwsr = false,
13064d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
131a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
13298bb9222SYong Zhao 	.num_sdma_engines = 2,
1331b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
134d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
135a3084e6cSFelix Kuehling };
136a3084e6cSFelix Kuehling 
137a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
138a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
139c181159aSYong Zhao 	.asic_name = "fiji",
140a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
141a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
142ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
143a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
144a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
145a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
146a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
147a3084e6cSFelix Kuehling 	.supports_cwsr = true,
14864d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
149a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
15098bb9222SYong Zhao 	.num_sdma_engines = 2,
1511b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
152d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
153a3084e6cSFelix Kuehling };
154a3084e6cSFelix Kuehling 
155a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
156a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
157c181159aSYong Zhao 	.asic_name = "fiji",
158a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
159a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
160ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
161a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
162a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
163a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
164a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
165a3084e6cSFelix Kuehling 	.supports_cwsr = true,
16664d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
167a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
16898bb9222SYong Zhao 	.num_sdma_engines = 2,
1691b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
170d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
171a3084e6cSFelix Kuehling };
172a3084e6cSFelix Kuehling 
173a3084e6cSFelix Kuehling 
174a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
175a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
176c181159aSYong Zhao 	.asic_name = "polaris10",
177a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
178a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
179ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
180a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
181a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
182a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
183a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
184a3084e6cSFelix Kuehling 	.supports_cwsr = true,
18564d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
186a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
18798bb9222SYong Zhao 	.num_sdma_engines = 2,
1881b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
189d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
190a3084e6cSFelix Kuehling };
191a3084e6cSFelix Kuehling 
192a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = {
193a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
194c181159aSYong Zhao 	.asic_name = "polaris10",
195a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
196a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
197ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
198a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
199a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
200a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
201a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
202a3084e6cSFelix Kuehling 	.supports_cwsr = true,
20364d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
204a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
20598bb9222SYong Zhao 	.num_sdma_engines = 2,
2061b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
207d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
208a3084e6cSFelix Kuehling };
209a3084e6cSFelix Kuehling 
210a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = {
211a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
212c181159aSYong Zhao 	.asic_name = "polaris11",
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 = true,
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 
228846a44d7SGang Ba static const struct kfd_device_info polaris12_device_info = {
229846a44d7SGang Ba 	.asic_family = CHIP_POLARIS12,
230c181159aSYong Zhao 	.asic_name = "polaris12",
231846a44d7SGang Ba 	.max_pasid_bits = 16,
232846a44d7SGang Ba 	.max_no_of_hqd  = 24,
233846a44d7SGang Ba 	.doorbell_size  = 4,
234846a44d7SGang Ba 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
235846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_cik,
236846a44d7SGang Ba 	.num_of_watch_points = 4,
237846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
238846a44d7SGang Ba 	.supports_cwsr = true,
239846a44d7SGang Ba 	.needs_iommu_device = false,
240846a44d7SGang Ba 	.needs_pci_atomics = true,
241846a44d7SGang Ba 	.num_sdma_engines = 2,
2421b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
243846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
244846a44d7SGang Ba };
245846a44d7SGang Ba 
246ed81cd6eSKent Russell static const struct kfd_device_info vegam_device_info = {
247ed81cd6eSKent Russell 	.asic_family = CHIP_VEGAM,
248c181159aSYong Zhao 	.asic_name = "vegam",
249ed81cd6eSKent Russell 	.max_pasid_bits = 16,
250ed81cd6eSKent Russell 	.max_no_of_hqd  = 24,
251ed81cd6eSKent Russell 	.doorbell_size  = 4,
252ed81cd6eSKent Russell 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
253ed81cd6eSKent Russell 	.event_interrupt_class = &event_interrupt_class_cik,
254ed81cd6eSKent Russell 	.num_of_watch_points = 4,
255ed81cd6eSKent Russell 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
256ed81cd6eSKent Russell 	.supports_cwsr = true,
257ed81cd6eSKent Russell 	.needs_iommu_device = false,
258ed81cd6eSKent Russell 	.needs_pci_atomics = true,
259ed81cd6eSKent Russell 	.num_sdma_engines = 2,
260ed81cd6eSKent Russell 	.num_xgmi_sdma_engines = 0,
261a3084e6cSFelix Kuehling 	.num_sdma_queues_per_engine = 2,
262a3084e6cSFelix Kuehling };
263a3084e6cSFelix Kuehling 
264389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = {
265389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
266c181159aSYong Zhao 	.asic_name = "vega10",
267389056e5SFelix Kuehling 	.max_pasid_bits = 16,
268389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
269389056e5SFelix Kuehling 	.doorbell_size  = 8,
270389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
271389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
272389056e5SFelix Kuehling 	.num_of_watch_points = 4,
273389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
274389056e5SFelix Kuehling 	.supports_cwsr = true,
275389056e5SFelix Kuehling 	.needs_iommu_device = false,
276389056e5SFelix Kuehling 	.needs_pci_atomics = false,
27798bb9222SYong Zhao 	.num_sdma_engines = 2,
2781b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
279d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
280389056e5SFelix Kuehling };
281389056e5SFelix Kuehling 
282389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_device_info = {
283389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
284c181159aSYong Zhao 	.asic_name = "vega10",
285389056e5SFelix Kuehling 	.max_pasid_bits = 16,
286389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
287389056e5SFelix Kuehling 	.doorbell_size  = 8,
288389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
289389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
290389056e5SFelix Kuehling 	.num_of_watch_points = 4,
291389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
292389056e5SFelix Kuehling 	.supports_cwsr = true,
293389056e5SFelix Kuehling 	.needs_iommu_device = false,
294389056e5SFelix Kuehling 	.needs_pci_atomics = false,
29598bb9222SYong Zhao 	.num_sdma_engines = 2,
2961b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
297d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
298389056e5SFelix Kuehling };
299389056e5SFelix Kuehling 
300846a44d7SGang Ba static const struct kfd_device_info vega12_device_info = {
301846a44d7SGang Ba 	.asic_family = CHIP_VEGA12,
302c181159aSYong Zhao 	.asic_name = "vega12",
303846a44d7SGang Ba 	.max_pasid_bits = 16,
304846a44d7SGang Ba 	.max_no_of_hqd  = 24,
305846a44d7SGang Ba 	.doorbell_size  = 8,
306846a44d7SGang Ba 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
307846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_v9,
308846a44d7SGang Ba 	.num_of_watch_points = 4,
309846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
310846a44d7SGang Ba 	.supports_cwsr = true,
311846a44d7SGang Ba 	.needs_iommu_device = false,
312846a44d7SGang Ba 	.needs_pci_atomics = false,
313846a44d7SGang Ba 	.num_sdma_engines = 2,
3141b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
315846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
316846a44d7SGang Ba };
317846a44d7SGang Ba 
31822a3a294SShaoyun Liu static const struct kfd_device_info vega20_device_info = {
31922a3a294SShaoyun Liu 	.asic_family = CHIP_VEGA20,
320c181159aSYong Zhao 	.asic_name = "vega20",
32122a3a294SShaoyun Liu 	.max_pasid_bits = 16,
32222a3a294SShaoyun Liu 	.max_no_of_hqd	= 24,
32322a3a294SShaoyun Liu 	.doorbell_size	= 8,
32422a3a294SShaoyun Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
32522a3a294SShaoyun Liu 	.event_interrupt_class = &event_interrupt_class_v9,
32622a3a294SShaoyun Liu 	.num_of_watch_points = 4,
32722a3a294SShaoyun Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
32822a3a294SShaoyun Liu 	.supports_cwsr = true,
32922a3a294SShaoyun Liu 	.needs_iommu_device = false,
330006a0b3dSShaoyun Liu 	.needs_pci_atomics = false,
33122a3a294SShaoyun Liu 	.num_sdma_engines = 2,
3321b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
33322a3a294SShaoyun Liu 	.num_sdma_queues_per_engine = 8,
33422a3a294SShaoyun Liu };
33522a3a294SShaoyun Liu 
33649adcf8aSYong Zhao static const struct kfd_device_info arcturus_device_info = {
33749adcf8aSYong Zhao 	.asic_family = CHIP_ARCTURUS,
338c181159aSYong Zhao 	.asic_name = "arcturus",
33949adcf8aSYong Zhao 	.max_pasid_bits = 16,
34049adcf8aSYong Zhao 	.max_no_of_hqd	= 24,
34149adcf8aSYong Zhao 	.doorbell_size	= 8,
34249adcf8aSYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
34349adcf8aSYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
34449adcf8aSYong Zhao 	.num_of_watch_points = 4,
34549adcf8aSYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
34649adcf8aSYong Zhao 	.supports_cwsr = true,
34749adcf8aSYong Zhao 	.needs_iommu_device = false,
34849adcf8aSYong Zhao 	.needs_pci_atomics = false,
349b6689cf7SOak Zeng 	.num_sdma_engines = 2,
350b6689cf7SOak Zeng 	.num_xgmi_sdma_engines = 6,
35149adcf8aSYong Zhao 	.num_sdma_queues_per_engine = 8,
35249adcf8aSYong Zhao };
35349adcf8aSYong Zhao 
3542b9c2211SHuang Rui static const struct kfd_device_info renoir_device_info = {
3552b9c2211SHuang Rui 	.asic_family = CHIP_RENOIR,
356*acb9acbeSHuang Rui 	.asic_name = "renoir",
3572b9c2211SHuang Rui 	.max_pasid_bits = 16,
3582b9c2211SHuang Rui 	.max_no_of_hqd  = 24,
3592b9c2211SHuang Rui 	.doorbell_size  = 8,
3602b9c2211SHuang Rui 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
3612b9c2211SHuang Rui 	.event_interrupt_class = &event_interrupt_class_v9,
3622b9c2211SHuang Rui 	.num_of_watch_points = 4,
3632b9c2211SHuang Rui 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
3642b9c2211SHuang Rui 	.supports_cwsr = true,
3652b9c2211SHuang Rui 	.needs_iommu_device = false,
3662b9c2211SHuang Rui 	.needs_pci_atomics = false,
3672b9c2211SHuang Rui 	.num_sdma_engines = 1,
3682b9c2211SHuang Rui 	.num_xgmi_sdma_engines = 0,
3692b9c2211SHuang Rui 	.num_sdma_queues_per_engine = 2,
3702b9c2211SHuang Rui };
3712b9c2211SHuang Rui 
37214328aa5SPhilip Cox static const struct kfd_device_info navi10_device_info = {
37314328aa5SPhilip Cox 	.asic_family = CHIP_NAVI10,
374c181159aSYong Zhao 	.asic_name = "navi10",
37514328aa5SPhilip Cox 	.max_pasid_bits = 16,
37614328aa5SPhilip Cox 	.max_no_of_hqd  = 24,
37714328aa5SPhilip Cox 	.doorbell_size  = 8,
37814328aa5SPhilip Cox 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
37914328aa5SPhilip Cox 	.event_interrupt_class = &event_interrupt_class_v9,
38014328aa5SPhilip Cox 	.num_of_watch_points = 4,
38114328aa5SPhilip Cox 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
38214328aa5SPhilip Cox 	.needs_iommu_device = false,
38314328aa5SPhilip Cox 	.supports_cwsr = true,
38414328aa5SPhilip Cox 	.needs_pci_atomics = false,
38514328aa5SPhilip Cox 	.num_sdma_engines = 2,
38614328aa5SPhilip Cox 	.num_xgmi_sdma_engines = 0,
38714328aa5SPhilip Cox 	.num_sdma_queues_per_engine = 8,
38814328aa5SPhilip Cox };
38914328aa5SPhilip Cox 
3908099ae40SYong Zhao static const struct kfd_device_info navi14_device_info = {
3918099ae40SYong Zhao 	.asic_family = CHIP_NAVI14,
3928099ae40SYong Zhao 	.asic_name = "navi14",
3938099ae40SYong Zhao 	.max_pasid_bits = 16,
3948099ae40SYong Zhao 	.max_no_of_hqd  = 24,
3958099ae40SYong Zhao 	.doorbell_size  = 8,
3968099ae40SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
3978099ae40SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
3988099ae40SYong Zhao 	.num_of_watch_points = 4,
3998099ae40SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
4008099ae40SYong Zhao 	.needs_iommu_device = false,
4018099ae40SYong Zhao 	.supports_cwsr = true,
4028099ae40SYong Zhao 	.needs_pci_atomics = false,
4038099ae40SYong Zhao 	.num_sdma_engines = 2,
4048099ae40SYong Zhao 	.num_xgmi_sdma_engines = 0,
4058099ae40SYong Zhao 	.num_sdma_queues_per_engine = 8,
4068099ae40SYong Zhao };
4078099ae40SYong Zhao 
408050091abSYong Zhao /* For each entry, [0] is regular and [1] is virtualisation device. */
409050091abSYong Zhao static const struct kfd_device_info *kfd_supported_devices[][2] = {
41095a5bd1bSYong Zhao #ifdef KFD_SUPPORT_IOMMU_V2
411050091abSYong Zhao 	[CHIP_KAVERI] = {&kaveri_device_info, NULL},
41295a5bd1bSYong Zhao 	[CHIP_CARRIZO] = {&carrizo_device_info, NULL},
41395a5bd1bSYong Zhao 	[CHIP_RAVEN] = {&raven_device_info, NULL},
41495a5bd1bSYong Zhao #endif
415050091abSYong Zhao 	[CHIP_HAWAII] = {&hawaii_device_info, NULL},
416050091abSYong Zhao 	[CHIP_TONGA] = {&tonga_device_info, NULL},
417050091abSYong Zhao 	[CHIP_FIJI] = {&fiji_device_info, &fiji_vf_device_info},
418050091abSYong Zhao 	[CHIP_POLARIS10] = {&polaris10_device_info, &polaris10_vf_device_info},
419050091abSYong Zhao 	[CHIP_POLARIS11] = {&polaris11_device_info, NULL},
420050091abSYong Zhao 	[CHIP_POLARIS12] = {&polaris12_device_info, NULL},
421050091abSYong Zhao 	[CHIP_VEGAM] = {&vegam_device_info, NULL},
422050091abSYong Zhao 	[CHIP_VEGA10] = {&vega10_device_info, &vega10_vf_device_info},
423050091abSYong Zhao 	[CHIP_VEGA12] = {&vega12_device_info, NULL},
424050091abSYong Zhao 	[CHIP_VEGA20] = {&vega20_device_info, NULL},
4252b9c2211SHuang Rui 	[CHIP_RENOIR] = {&renoir_device_info, NULL},
426050091abSYong Zhao 	[CHIP_ARCTURUS] = {&arcturus_device_info, &arcturus_device_info},
427050091abSYong Zhao 	[CHIP_NAVI10] = {&navi10_device_info, NULL},
4288099ae40SYong Zhao 	[CHIP_NAVI14] = {&navi14_device_info, NULL},
4294a488a7aSOded Gabbay };
4304a488a7aSOded Gabbay 
4316e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
4326e81090bSOded Gabbay 				unsigned int chunk_size);
4336e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
4346e81090bSOded Gabbay 
435b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
436b8935a7cSYong Zhao 
437cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
438050091abSYong Zhao 	struct pci_dev *pdev, const struct kfd2kgd_calls *f2g,
439050091abSYong Zhao 	unsigned int asic_type, bool vf)
4404a488a7aSOded Gabbay {
4414a488a7aSOded Gabbay 	struct kfd_dev *kfd;
442050091abSYong Zhao 	const struct kfd_device_info *device_info;
443050091abSYong Zhao 
444050091abSYong Zhao 	if (asic_type >= sizeof(kfd_supported_devices) / (sizeof(void *) * 2)) {
445050091abSYong Zhao 		dev_err(kfd_device, "asic_type %d out of range\n", asic_type);
446050091abSYong Zhao 		return NULL; /* asic_type out of range */
447050091abSYong Zhao 	}
448050091abSYong Zhao 
449050091abSYong Zhao 	device_info = kfd_supported_devices[asic_type][vf];
4504a488a7aSOded Gabbay 
4514ebc7182SYong Zhao 	if (!device_info) {
452050091abSYong Zhao 		dev_err(kfd_device, "%s %s not supported in kfd\n",
453050091abSYong Zhao 			amdgpu_asic_name[asic_type], vf ? "VF" : "");
4544a488a7aSOded Gabbay 		return NULL;
4554ebc7182SYong Zhao 	}
4564a488a7aSOded Gabbay 
457d35f00d8SEric Huang 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
458d35f00d8SEric Huang 	if (!kfd)
459d35f00d8SEric Huang 		return NULL;
460d35f00d8SEric Huang 
4616106dce9Swelu 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
4626106dce9Swelu 	 * 32 and 64-bit requests are possible and must be
4636106dce9Swelu 	 * supported.
4643ee2d00cSFelix Kuehling 	 */
465aabf3a95SJack Xiao 	kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kgd);
466aabf3a95SJack Xiao 	if (device_info->needs_pci_atomics &&
467aabf3a95SJack Xiao 	    !kfd->pci_atomic_requested) {
4683ee2d00cSFelix Kuehling 		dev_info(kfd_device,
4696106dce9Swelu 			 "skipped device %x:%x, PCI rejects atomics\n",
4703ee2d00cSFelix Kuehling 			 pdev->vendor, pdev->device);
471d35f00d8SEric Huang 		kfree(kfd);
4723ee2d00cSFelix Kuehling 		return NULL;
473aabf3a95SJack Xiao 	}
4744a488a7aSOded Gabbay 
4754a488a7aSOded Gabbay 	kfd->kgd = kgd;
4764a488a7aSOded Gabbay 	kfd->device_info = device_info;
4774a488a7aSOded Gabbay 	kfd->pdev = pdev;
47819f6d2a6SOded Gabbay 	kfd->init_complete = false;
479cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
48043d8107fSHarish Kasiviswanathan 	atomic_set(&kfd->compute_profile, 0);
481cea405b1SXihan Zhang 
482cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
483cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
484cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
4854a488a7aSOded Gabbay 
4869b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
4879b54d201SEric Huang 
4884a488a7aSOded Gabbay 	return kfd;
4894a488a7aSOded Gabbay }
4904a488a7aSOded Gabbay 
491373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
492373d7080SFelix Kuehling {
493373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
4943e76c239SFelix Kuehling 		if (kfd->device_info->asic_family < CHIP_VEGA10) {
495373d7080SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
496373d7080SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
497373d7080SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
4983baa24f0SOak Zeng 		} else if (kfd->device_info->asic_family == CHIP_ARCTURUS) {
4993baa24f0SOak Zeng 			BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
5003baa24f0SOak Zeng 			kfd->cwsr_isa = cwsr_trap_arcturus_hex;
5013baa24f0SOak Zeng 			kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
50214328aa5SPhilip Cox 		} else if (kfd->device_info->asic_family < CHIP_NAVI10) {
5033e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
5043e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
5053e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
50614328aa5SPhilip Cox 		} else {
50714328aa5SPhilip Cox 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
50814328aa5SPhilip Cox 			kfd->cwsr_isa = cwsr_trap_gfx10_hex;
50914328aa5SPhilip Cox 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
5103e76c239SFelix Kuehling 		}
5113e76c239SFelix Kuehling 
512373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
513373d7080SFelix Kuehling 	}
514373d7080SFelix Kuehling }
515373d7080SFelix Kuehling 
5164a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
5174a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
5184a488a7aSOded Gabbay {
51919f6d2a6SOded Gabbay 	unsigned int size;
52019f6d2a6SOded Gabbay 
5210da8b10eSAmber Lin 	kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
5225ade6c9cSFelix Kuehling 			KGD_ENGINE_MEC1);
5230da8b10eSAmber Lin 	kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
5245ade6c9cSFelix Kuehling 			KGD_ENGINE_SDMA1);
5254a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
5264a488a7aSOded Gabbay 
52744008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
52844008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
52944008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
53044008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
53144008d7aSYong Zhao 
532a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
533a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
534a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
535a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
536a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
537a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
538a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
539a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
540a99c6d4fSFelix Kuehling 	} else
541a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
542a99c6d4fSFelix Kuehling 
543e09d4fc8SOak Zeng 	/* Allocate global GWS that is shared by all KFD processes */
544e09d4fc8SOak Zeng 	if (hws_gws_support && amdgpu_amdkfd_alloc_gws(kfd->kgd,
545e09d4fc8SOak Zeng 			amdgpu_amdkfd_get_num_gws(kfd->kgd), &kfd->gws)) {
546e09d4fc8SOak Zeng 		dev_err(kfd_device, "Could not allocate %d gws\n",
547e09d4fc8SOak Zeng 			amdgpu_amdkfd_get_num_gws(kfd->kgd));
548e09d4fc8SOak Zeng 		goto out;
549e09d4fc8SOak Zeng 	}
55019f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
551b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
55219f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
55319f6d2a6SOded Gabbay 
554e18e794eSOded Gabbay 	/*
555e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
556e18e794eSOded Gabbay 	 * There can be only 2 packets at once
557e18e794eSOded Gabbay 	 */
558507968ddSFelix Kuehling 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
559507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
560507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
561e18e794eSOded Gabbay 
562e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
563e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
564e18e794eSOded Gabbay 
565e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
56619f6d2a6SOded Gabbay 	size += 512 * 1024;
56719f6d2a6SOded Gabbay 
5687cd52c91SAmber Lin 	if (amdgpu_amdkfd_alloc_gtt_mem(
569cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
57015426dbbSYong Zhao 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
57115426dbbSYong Zhao 			false)) {
57279775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
573e09d4fc8SOak Zeng 		goto alloc_gtt_mem_failure;
57419f6d2a6SOded Gabbay 	}
57519f6d2a6SOded Gabbay 
57679775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
577e18e794eSOded Gabbay 
57873a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
57973a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
58079775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
58173a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
58273a1da0bSOded Gabbay 	}
58373a1da0bSOded Gabbay 
584735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
585735df2baSFelix Kuehling 		dev_err(kfd_device,
586735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
587735df2baSFelix Kuehling 		goto kfd_doorbell_error;
588735df2baSFelix Kuehling 	}
58919f6d2a6SOded Gabbay 
5900c1690e3SShaoyun Liu 	if (kfd->kfd2kgd->get_hive_id)
5910c1690e3SShaoyun Liu 		kfd->hive_id = kfd->kfd2kgd->get_hive_id(kfd->kgd);
5920c1690e3SShaoyun Liu 
5932249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
59479775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
5952249d558SAndrew Lewycky 		goto kfd_interrupt_error;
5962249d558SAndrew Lewycky 	}
5972249d558SAndrew Lewycky 
59864c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
59964c7f8cfSBen Goz 	if (!kfd->dqm) {
60079775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
60164c7f8cfSBen Goz 		goto device_queue_manager_error;
60264c7f8cfSBen Goz 	}
60364c7f8cfSBen Goz 
60464d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
60564d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
60664d1c3a4SFelix Kuehling 		goto device_iommu_error;
60764c7f8cfSBen Goz 	}
60864c7f8cfSBen Goz 
609373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
610373d7080SFelix Kuehling 
611b8935a7cSYong Zhao 	if (kfd_resume(kfd))
612b8935a7cSYong Zhao 		goto kfd_resume_error;
613b8935a7cSYong Zhao 
614fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
615fbeb661bSYair Shachar 
616465ab9e0SOak Zeng 	if (kfd_topology_add_device(kfd)) {
617465ab9e0SOak Zeng 		dev_err(kfd_device, "Error adding device to topology\n");
618465ab9e0SOak Zeng 		goto kfd_topology_add_device_error;
619465ab9e0SOak Zeng 	}
620465ab9e0SOak Zeng 
6214a488a7aSOded Gabbay 	kfd->init_complete = true;
62279775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
6234a488a7aSOded Gabbay 		 kfd->pdev->device);
6244a488a7aSOded Gabbay 
62579775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
626d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
62764c7f8cfSBen Goz 
62819f6d2a6SOded Gabbay 	goto out;
62919f6d2a6SOded Gabbay 
630465ab9e0SOak Zeng kfd_topology_add_device_error:
631b8935a7cSYong Zhao kfd_resume_error:
63264d1c3a4SFelix Kuehling device_iommu_error:
63364c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
63464c7f8cfSBen Goz device_queue_manager_error:
6352249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
6362249d558SAndrew Lewycky kfd_interrupt_error:
637735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
638735df2baSFelix Kuehling kfd_doorbell_error:
63973a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
64073a1da0bSOded Gabbay kfd_gtt_sa_init_error:
6417cd52c91SAmber Lin 	amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
642e09d4fc8SOak Zeng alloc_gtt_mem_failure:
643e09d4fc8SOak Zeng 	if (hws_gws_support)
644e09d4fc8SOak Zeng 		amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
64519f6d2a6SOded Gabbay 	dev_err(kfd_device,
64679775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
64719f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
64819f6d2a6SOded Gabbay out:
64919f6d2a6SOded Gabbay 	return kfd->init_complete;
6504a488a7aSOded Gabbay }
6514a488a7aSOded Gabbay 
6524a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
6534a488a7aSOded Gabbay {
654b17f068aSOded Gabbay 	if (kfd->init_complete) {
655b8935a7cSYong Zhao 		kgd2kfd_suspend(kfd);
65664c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
6572249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
65819f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
659735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
66073a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
6617cd52c91SAmber Lin 		amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
662e09d4fc8SOak Zeng 		if (hws_gws_support)
663e09d4fc8SOak Zeng 			amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
664b17f068aSOded Gabbay 	}
6655b5c4e40SEvgeny Pinchuk 
6664a488a7aSOded Gabbay 	kfree(kfd);
6674a488a7aSOded Gabbay }
6684a488a7aSOded Gabbay 
669e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
670e3b7a967SShaoyun Liu {
671e42051d2SShaoyun Liu 	if (!kfd->init_complete)
672e42051d2SShaoyun Liu 		return 0;
673e42051d2SShaoyun Liu 	kgd2kfd_suspend(kfd);
674e42051d2SShaoyun Liu 
675e42051d2SShaoyun Liu 	/* hold dqm->lock to prevent further execution*/
676e42051d2SShaoyun Liu 	dqm_lock(kfd->dqm);
677e42051d2SShaoyun Liu 
678e42051d2SShaoyun Liu 	kfd_signal_reset_event(kfd);
679e3b7a967SShaoyun Liu 	return 0;
680e3b7a967SShaoyun Liu }
681e3b7a967SShaoyun Liu 
682e42051d2SShaoyun Liu /*
683e42051d2SShaoyun Liu  * Fix me. KFD won't be able to resume existing process for now.
684e42051d2SShaoyun Liu  * We will keep all existing process in a evicted state and
685e42051d2SShaoyun Liu  * wait the process to be terminated.
686e42051d2SShaoyun Liu  */
687e42051d2SShaoyun Liu 
688e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
689e3b7a967SShaoyun Liu {
690e42051d2SShaoyun Liu 	int ret, count;
691e42051d2SShaoyun Liu 
692e42051d2SShaoyun Liu 	if (!kfd->init_complete)
693e3b7a967SShaoyun Liu 		return 0;
694e42051d2SShaoyun Liu 
695e42051d2SShaoyun Liu 	dqm_unlock(kfd->dqm);
696e42051d2SShaoyun Liu 
697e42051d2SShaoyun Liu 	ret = kfd_resume(kfd);
698e42051d2SShaoyun Liu 	if (ret)
699e42051d2SShaoyun Liu 		return ret;
700e42051d2SShaoyun Liu 	count = atomic_dec_return(&kfd_locked);
7019b54d201SEric Huang 
7029b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
7039b54d201SEric Huang 
704e42051d2SShaoyun Liu 	return 0;
705e42051d2SShaoyun Liu }
706e42051d2SShaoyun Liu 
707e42051d2SShaoyun Liu bool kfd_is_locked(void)
708e42051d2SShaoyun Liu {
709e42051d2SShaoyun Liu 	return  (atomic_read(&kfd_locked) > 0);
710e3b7a967SShaoyun Liu }
711e3b7a967SShaoyun Liu 
7124a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd)
7134a488a7aSOded Gabbay {
714733fa1f7SYong Zhao 	if (!kfd->init_complete)
715733fa1f7SYong Zhao 		return;
716733fa1f7SYong Zhao 
71726103436SFelix Kuehling 	/* For first KFD device suspend all the KFD processes */
718e42051d2SShaoyun Liu 	if (atomic_inc_return(&kfd_locked) == 1)
71926103436SFelix Kuehling 		kfd_suspend_all_processes();
72026103436SFelix Kuehling 
72145c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
722733fa1f7SYong Zhao 
72364d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
7244a488a7aSOded Gabbay }
7254a488a7aSOded Gabbay 
7264a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd)
7274a488a7aSOded Gabbay {
72826103436SFelix Kuehling 	int ret, count;
72926103436SFelix Kuehling 
730b8935a7cSYong Zhao 	if (!kfd->init_complete)
731b8935a7cSYong Zhao 		return 0;
732b17f068aSOded Gabbay 
73326103436SFelix Kuehling 	ret = kfd_resume(kfd);
73426103436SFelix Kuehling 	if (ret)
73526103436SFelix Kuehling 		return ret;
736b17f068aSOded Gabbay 
737e42051d2SShaoyun Liu 	count = atomic_dec_return(&kfd_locked);
73826103436SFelix Kuehling 	WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
73926103436SFelix Kuehling 	if (count == 0)
74026103436SFelix Kuehling 		ret = kfd_resume_all_processes();
74126103436SFelix Kuehling 
74226103436SFelix Kuehling 	return ret;
7434ebc7182SYong Zhao }
7444ebc7182SYong Zhao 
745b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
746b8935a7cSYong Zhao {
747b8935a7cSYong Zhao 	int err = 0;
748b8935a7cSYong Zhao 
74964d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
75064d1c3a4SFelix Kuehling 	if (err) {
75164d1c3a4SFelix Kuehling 		dev_err(kfd_device,
75264d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
75364d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
75464d1c3a4SFelix Kuehling 		return err;
75564d1c3a4SFelix Kuehling 	}
756733fa1f7SYong Zhao 
757b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
758b8935a7cSYong Zhao 	if (err) {
759b8935a7cSYong Zhao 		dev_err(kfd_device,
760b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
761b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
762b8935a7cSYong Zhao 		goto dqm_start_error;
763b17f068aSOded Gabbay 	}
764b17f068aSOded Gabbay 
765b8935a7cSYong Zhao 	return err;
766b8935a7cSYong Zhao 
767b8935a7cSYong Zhao dqm_start_error:
76864d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
769b8935a7cSYong Zhao 	return err;
7704a488a7aSOded Gabbay }
7714a488a7aSOded Gabbay 
772b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
773b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
7744a488a7aSOded Gabbay {
77558e69886SLan Xiao 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
77658e69886SLan Xiao 	bool is_patched = false;
7772383a767SChristian König 	unsigned long flags;
77858e69886SLan Xiao 
7792249d558SAndrew Lewycky 	if (!kfd->init_complete)
7802249d558SAndrew Lewycky 		return;
7812249d558SAndrew Lewycky 
78258e69886SLan Xiao 	if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
78358e69886SLan Xiao 		dev_err_once(kfd_device, "Ring entry too small\n");
78458e69886SLan Xiao 		return;
78558e69886SLan Xiao 	}
78658e69886SLan Xiao 
7872383a767SChristian König 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
7882249d558SAndrew Lewycky 
7892249d558SAndrew Lewycky 	if (kfd->interrupts_active
79058e69886SLan Xiao 	    && interrupt_is_wanted(kfd, ih_ring_entry,
79158e69886SLan Xiao 				   patched_ihre, &is_patched)
79258e69886SLan Xiao 	    && enqueue_ih_ring_entry(kfd,
79358e69886SLan Xiao 				     is_patched ? patched_ihre : ih_ring_entry))
79448e876a2SAndres Rodriguez 		queue_work(kfd->ih_wq, &kfd->interrupt_work);
7952249d558SAndrew Lewycky 
7962383a767SChristian König 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
7974a488a7aSOded Gabbay }
7986e81090bSOded Gabbay 
7996b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
8006b95e797SFelix Kuehling {
8016b95e797SFelix Kuehling 	struct kfd_process *p;
8026b95e797SFelix Kuehling 	int r;
8036b95e797SFelix Kuehling 
8046b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
8056b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
8066b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
8076b95e797SFelix Kuehling 	 */
8086b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
8096b95e797SFelix Kuehling 	if (!p)
8106b95e797SFelix Kuehling 		return -ESRCH;
8116b95e797SFelix Kuehling 
8126b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
8136b95e797SFelix Kuehling 
8146b95e797SFelix Kuehling 	kfd_unref_process(p);
8156b95e797SFelix Kuehling 	return r;
8166b95e797SFelix Kuehling }
8176b95e797SFelix Kuehling 
8186b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
8196b95e797SFelix Kuehling {
8206b95e797SFelix Kuehling 	struct kfd_process *p;
8216b95e797SFelix Kuehling 	int r;
8226b95e797SFelix Kuehling 
8236b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
8246b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
8256b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
8266b95e797SFelix Kuehling 	 */
8276b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
8286b95e797SFelix Kuehling 	if (!p)
8296b95e797SFelix Kuehling 		return -ESRCH;
8306b95e797SFelix Kuehling 
8316b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
8326b95e797SFelix Kuehling 
8336b95e797SFelix Kuehling 	kfd_unref_process(p);
8346b95e797SFelix Kuehling 	return r;
8356b95e797SFelix Kuehling }
8366b95e797SFelix Kuehling 
83726103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
83826103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
83926103436SFelix Kuehling  *   process.
84026103436SFelix Kuehling  *
84126103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
84226103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
84326103436SFelix Kuehling  *
84426103436SFelix Kuehling  */
84526103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
84626103436SFelix Kuehling 					       struct dma_fence *fence)
84726103436SFelix Kuehling {
84826103436SFelix Kuehling 	struct kfd_process *p;
84926103436SFelix Kuehling 	unsigned long active_time;
85026103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
85126103436SFelix Kuehling 
85226103436SFelix Kuehling 	if (!fence)
85326103436SFelix Kuehling 		return -EINVAL;
85426103436SFelix Kuehling 
85526103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
85626103436SFelix Kuehling 		return 0;
85726103436SFelix Kuehling 
85826103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
85926103436SFelix Kuehling 	if (!p)
86026103436SFelix Kuehling 		return -ENODEV;
86126103436SFelix Kuehling 
86226103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
86326103436SFelix Kuehling 		goto out;
86426103436SFelix Kuehling 
86526103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
86626103436SFelix Kuehling 
86726103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
86826103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
86926103436SFelix Kuehling 	 */
87026103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
87126103436SFelix Kuehling 	if (delay_jiffies > active_time)
87226103436SFelix Kuehling 		delay_jiffies -= active_time;
87326103436SFelix Kuehling 	else
87426103436SFelix Kuehling 		delay_jiffies = 0;
87526103436SFelix Kuehling 
87626103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
87726103436SFelix Kuehling 	 * to kfd_evict_bo_worker
87826103436SFelix Kuehling 	 */
87926103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
88026103436SFelix Kuehling out:
88126103436SFelix Kuehling 	kfd_unref_process(p);
88226103436SFelix Kuehling 	return 0;
88326103436SFelix Kuehling }
88426103436SFelix Kuehling 
8856e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
8866e81090bSOded Gabbay 				unsigned int chunk_size)
8876e81090bSOded Gabbay {
8888625ff9cSFelix Kuehling 	unsigned int num_of_longs;
8896e81090bSOded Gabbay 
89032fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
89132fa8219SFelix Kuehling 		return -EINVAL;
89232fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
89332fa8219SFelix Kuehling 		return -EINVAL;
89432fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
89532fa8219SFelix Kuehling 		return -EINVAL;
8966e81090bSOded Gabbay 
8976e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
8986e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
8996e81090bSOded Gabbay 
9008625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
9018625ff9cSFelix Kuehling 		BITS_PER_LONG;
9026e81090bSOded Gabbay 
9038625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
9046e81090bSOded Gabbay 
9056e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
9066e81090bSOded Gabbay 		return -ENOMEM;
9076e81090bSOded Gabbay 
90879775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
9096e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
9106e81090bSOded Gabbay 
9116e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
9126e81090bSOded Gabbay 
9136e81090bSOded Gabbay 	return 0;
9146e81090bSOded Gabbay 
9156e81090bSOded Gabbay }
9166e81090bSOded Gabbay 
9176e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
9186e81090bSOded Gabbay {
9196e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
9206e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
9216e81090bSOded Gabbay }
9226e81090bSOded Gabbay 
9236e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
9246e81090bSOded Gabbay 						unsigned int bit_num,
9256e81090bSOded Gabbay 						unsigned int chunk_size)
9266e81090bSOded Gabbay {
9276e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
9286e81090bSOded Gabbay }
9296e81090bSOded Gabbay 
9306e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
9316e81090bSOded Gabbay 						unsigned int bit_num,
9326e81090bSOded Gabbay 						unsigned int chunk_size)
9336e81090bSOded Gabbay {
9346e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
9356e81090bSOded Gabbay }
9366e81090bSOded Gabbay 
9376e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
9386e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
9396e81090bSOded Gabbay {
9406e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
9416e81090bSOded Gabbay 
9426e81090bSOded Gabbay 	if (size == 0)
9436e81090bSOded Gabbay 		return -EINVAL;
9446e81090bSOded Gabbay 
9456e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
9466e81090bSOded Gabbay 		return -ENOMEM;
9476e81090bSOded Gabbay 
9481cd106ecSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
9491cd106ecSFelix Kuehling 	if (!(*mem_obj))
9506e81090bSOded Gabbay 		return -ENOMEM;
9516e81090bSOded Gabbay 
95279775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
9536e81090bSOded Gabbay 
9546e81090bSOded Gabbay 	start_search = 0;
9556e81090bSOded Gabbay 
9566e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
9576e81090bSOded Gabbay 
9586e81090bSOded Gabbay kfd_gtt_restart_search:
9596e81090bSOded Gabbay 	/* Find the first chunk that is free */
9606e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
9616e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
9626e81090bSOded Gabbay 					start_search);
9636e81090bSOded Gabbay 
96479775b62SKent Russell 	pr_debug("Found = %d\n", found);
9656e81090bSOded Gabbay 
9666e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
9676e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
9686e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
9696e81090bSOded Gabbay 
9706e81090bSOded Gabbay 	/* Update fields of mem_obj */
9716e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
9726e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
9736e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
9746e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
9756e81090bSOded Gabbay 					found,
9766e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
9776e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
9786e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
9796e81090bSOded Gabbay 					found,
9806e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
9816e81090bSOded Gabbay 
98279775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
9836e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
9846e81090bSOded Gabbay 
9856e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
9866e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
98779775b62SKent Russell 		pr_debug("Single bit\n");
9886e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
9896e81090bSOded Gabbay 		goto kfd_gtt_out;
9906e81090bSOded Gabbay 	}
9916e81090bSOded Gabbay 
9926e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
9936e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
9946e81090bSOded Gabbay 	do {
9956e81090bSOded Gabbay 		(*mem_obj)->range_end =
9966e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
9976e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
9986e81090bSOded Gabbay 		/*
9996e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
10006e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
10016e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
10026e81090bSOded Gabbay 		 */
10036e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
10046e81090bSOded Gabbay 			start_search = found;
10056e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
10066e81090bSOded Gabbay 		}
10076e81090bSOded Gabbay 
10086e81090bSOded Gabbay 		/*
10096e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
10106e81090bSOded Gabbay 		 */
10116e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
10126e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
10136e81090bSOded Gabbay 
10146e81090bSOded Gabbay 		/* Check if we don't need another chunk */
10156e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
10166e81090bSOded Gabbay 			cur_size = 0;
10176e81090bSOded Gabbay 		else
10186e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
10196e81090bSOded Gabbay 
10206e81090bSOded Gabbay 	} while (cur_size > 0);
10216e81090bSOded Gabbay 
102279775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
10236e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
10246e81090bSOded Gabbay 
10256e81090bSOded Gabbay 	/* Mark the chunks as allocated */
10266e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
10276e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
10286e81090bSOded Gabbay 		found++)
10296e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
10306e81090bSOded Gabbay 
10316e81090bSOded Gabbay kfd_gtt_out:
10326e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
10336e81090bSOded Gabbay 	return 0;
10346e81090bSOded Gabbay 
10356e81090bSOded Gabbay kfd_gtt_no_free_chunk:
103679775b62SKent Russell 	pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
10376e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
10386e81090bSOded Gabbay 	kfree(mem_obj);
10396e81090bSOded Gabbay 	return -ENOMEM;
10406e81090bSOded Gabbay }
10416e81090bSOded Gabbay 
10426e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
10436e81090bSOded Gabbay {
10446e81090bSOded Gabbay 	unsigned int bit;
10456e81090bSOded Gabbay 
10469216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
10479216ed29SOded Gabbay 	if (!mem_obj)
10489216ed29SOded Gabbay 		return 0;
10496e81090bSOded Gabbay 
105079775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
10516e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
10526e81090bSOded Gabbay 
10536e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
10546e81090bSOded Gabbay 
10556e81090bSOded Gabbay 	/* Mark the chunks as free */
10566e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
10576e81090bSOded Gabbay 		bit <= mem_obj->range_end;
10586e81090bSOded Gabbay 		bit++)
10596e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
10606e81090bSOded Gabbay 
10616e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
10626e81090bSOded Gabbay 
10636e81090bSOded Gabbay 	kfree(mem_obj);
10646e81090bSOded Gabbay 	return 0;
10656e81090bSOded Gabbay }
1066a29ec470SShaoyun Liu 
10679b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
10689b54d201SEric Huang {
10699b54d201SEric Huang 	if (kfd)
10709b54d201SEric Huang 		atomic_inc(&kfd->sram_ecc_flag);
10719b54d201SEric Huang }
10729b54d201SEric Huang 
107343d8107fSHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd)
107443d8107fSHarish Kasiviswanathan {
107543d8107fSHarish Kasiviswanathan 	if (atomic_inc_return(&kfd->compute_profile) == 1)
107643d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, false);
107743d8107fSHarish Kasiviswanathan }
107843d8107fSHarish Kasiviswanathan 
107943d8107fSHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd)
108043d8107fSHarish Kasiviswanathan {
108143d8107fSHarish Kasiviswanathan 	int count = atomic_dec_return(&kfd->compute_profile);
108243d8107fSHarish Kasiviswanathan 
108343d8107fSHarish Kasiviswanathan 	if (count == 0)
108443d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, true);
108543d8107fSHarish Kasiviswanathan 	WARN_ONCE(count < 0, "Compute profile ref. count error");
108643d8107fSHarish Kasiviswanathan }
108743d8107fSHarish Kasiviswanathan 
1088a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
1089a29ec470SShaoyun Liu 
1090a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
1091a29ec470SShaoyun Liu  * which will trigger a GPU reset and bring the HWS back to normal state
1092a29ec470SShaoyun Liu  */
1093a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1094a29ec470SShaoyun Liu {
1095a29ec470SShaoyun Liu 	int r = 0;
1096a29ec470SShaoyun Liu 
1097a29ec470SShaoyun Liu 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1098a29ec470SShaoyun Liu 		pr_err("HWS is not enabled");
1099a29ec470SShaoyun Liu 		return -EINVAL;
1100a29ec470SShaoyun Liu 	}
1101a29ec470SShaoyun Liu 
1102a29ec470SShaoyun Liu 	r = pm_debugfs_hang_hws(&dev->dqm->packets);
1103a29ec470SShaoyun Liu 	if (!r)
1104a29ec470SShaoyun Liu 		r = dqm_debugfs_execute_queues(dev->dqm);
1105a29ec470SShaoyun Liu 
1106a29ec470SShaoyun Liu 	return r;
1107a29ec470SShaoyun Liu }
1108a29ec470SShaoyun Liu 
1109a29ec470SShaoyun Liu #endif
1110