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,
450da7558cSBen Goz 	.max_pasid_bits = 16,
46992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
47992839adSYair Shachar 	.max_no_of_hqd	= 24,
48ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
490da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
50f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
51fbeb661bSYair Shachar 	.num_of_watch_points = 4,
52373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
53373d7080SFelix Kuehling 	.supports_cwsr = false,
5464d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
553ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
5698bb9222SYong Zhao 	.num_sdma_engines = 2,
571b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
58d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
590da7558cSBen Goz };
600da7558cSBen Goz 
610da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
620da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
634a488a7aSOded Gabbay 	.max_pasid_bits = 16,
64eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
65eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
66ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
67b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
68eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
69f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
70373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
71373d7080SFelix Kuehling 	.supports_cwsr = true,
7264d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
733ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
7498bb9222SYong Zhao 	.num_sdma_engines = 2,
751b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
76d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
774a488a7aSOded Gabbay };
784d663df6SYong Zhao 
794d663df6SYong Zhao static const struct kfd_device_info raven_device_info = {
804d663df6SYong Zhao 	.asic_family = CHIP_RAVEN,
814d663df6SYong Zhao 	.max_pasid_bits = 16,
824d663df6SYong Zhao 	.max_no_of_hqd  = 24,
834d663df6SYong Zhao 	.doorbell_size  = 8,
844d663df6SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
854d663df6SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
864d663df6SYong Zhao 	.num_of_watch_points = 4,
874d663df6SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
884d663df6SYong Zhao 	.supports_cwsr = true,
894d663df6SYong Zhao 	.needs_iommu_device = true,
904d663df6SYong Zhao 	.needs_pci_atomics = true,
914d663df6SYong Zhao 	.num_sdma_engines = 1,
921b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
93d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
944d663df6SYong Zhao };
9564d1c3a4SFelix Kuehling #endif
964a488a7aSOded Gabbay 
97a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
98a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
99a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
100a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
101a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
102ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
103a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
104a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
105a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
106a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
107a3084e6cSFelix Kuehling 	.supports_cwsr = false,
10864d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
109a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
11098bb9222SYong Zhao 	.num_sdma_engines = 2,
1111b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
112d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
113a3084e6cSFelix Kuehling };
114a3084e6cSFelix Kuehling 
115a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
116a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
117a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
118a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
119ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
120a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
121a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
122a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
123a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
124a3084e6cSFelix Kuehling 	.supports_cwsr = false,
12564d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
126a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
12798bb9222SYong Zhao 	.num_sdma_engines = 2,
1281b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
129d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
130a3084e6cSFelix Kuehling };
131a3084e6cSFelix Kuehling 
132a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
133a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
134a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
135a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
136ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
137a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
138a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
139a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
140a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
141a3084e6cSFelix Kuehling 	.supports_cwsr = true,
14264d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
143a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
14498bb9222SYong Zhao 	.num_sdma_engines = 2,
1451b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
146d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
147a3084e6cSFelix Kuehling };
148a3084e6cSFelix Kuehling 
149a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
150a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
151a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
152a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
153ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
154a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
155a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
156a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
157a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
158a3084e6cSFelix Kuehling 	.supports_cwsr = true,
15964d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
160a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
16198bb9222SYong Zhao 	.num_sdma_engines = 2,
1621b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
163d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
164a3084e6cSFelix Kuehling };
165a3084e6cSFelix Kuehling 
166a3084e6cSFelix Kuehling 
167a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
168a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
169a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
170a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
171ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
172a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
173a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
174a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
175a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
176a3084e6cSFelix Kuehling 	.supports_cwsr = true,
17764d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
178a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
17998bb9222SYong Zhao 	.num_sdma_engines = 2,
1801b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
181d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
182a3084e6cSFelix Kuehling };
183a3084e6cSFelix Kuehling 
184a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = {
185a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
186a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
187a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
188ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
189a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
190a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
191a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
192a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
193a3084e6cSFelix Kuehling 	.supports_cwsr = true,
19464d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
195a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
19698bb9222SYong Zhao 	.num_sdma_engines = 2,
1971b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
198d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
199a3084e6cSFelix Kuehling };
200a3084e6cSFelix Kuehling 
201a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = {
202a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
203a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
204a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
205ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
206a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
207a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
208a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
209a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
210a3084e6cSFelix Kuehling 	.supports_cwsr = true,
21164d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
212a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
21398bb9222SYong Zhao 	.num_sdma_engines = 2,
2141b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
215d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
216a3084e6cSFelix Kuehling };
217a3084e6cSFelix Kuehling 
218846a44d7SGang Ba static const struct kfd_device_info polaris12_device_info = {
219846a44d7SGang Ba 	.asic_family = CHIP_POLARIS12,
220846a44d7SGang Ba 	.max_pasid_bits = 16,
221846a44d7SGang Ba 	.max_no_of_hqd  = 24,
222846a44d7SGang Ba 	.doorbell_size  = 4,
223846a44d7SGang Ba 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
224846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_cik,
225846a44d7SGang Ba 	.num_of_watch_points = 4,
226846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
227846a44d7SGang Ba 	.supports_cwsr = true,
228846a44d7SGang Ba 	.needs_iommu_device = false,
229846a44d7SGang Ba 	.needs_pci_atomics = true,
230846a44d7SGang Ba 	.num_sdma_engines = 2,
2311b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
232846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
233846a44d7SGang Ba };
234846a44d7SGang Ba 
235ed81cd6eSKent Russell static const struct kfd_device_info vegam_device_info = {
236ed81cd6eSKent Russell 	.asic_family = CHIP_VEGAM,
237ed81cd6eSKent Russell 	.max_pasid_bits = 16,
238ed81cd6eSKent Russell 	.max_no_of_hqd  = 24,
239ed81cd6eSKent Russell 	.doorbell_size  = 4,
240ed81cd6eSKent Russell 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
241ed81cd6eSKent Russell 	.event_interrupt_class = &event_interrupt_class_cik,
242ed81cd6eSKent Russell 	.num_of_watch_points = 4,
243ed81cd6eSKent Russell 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
244ed81cd6eSKent Russell 	.supports_cwsr = true,
245ed81cd6eSKent Russell 	.needs_iommu_device = false,
246ed81cd6eSKent Russell 	.needs_pci_atomics = true,
247ed81cd6eSKent Russell 	.num_sdma_engines = 2,
248ed81cd6eSKent Russell 	.num_xgmi_sdma_engines = 0,
249a3084e6cSFelix Kuehling 	.num_sdma_queues_per_engine = 2,
250a3084e6cSFelix Kuehling };
251a3084e6cSFelix Kuehling 
252389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = {
253389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
254389056e5SFelix Kuehling 	.max_pasid_bits = 16,
255389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
256389056e5SFelix Kuehling 	.doorbell_size  = 8,
257389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
258389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
259389056e5SFelix Kuehling 	.num_of_watch_points = 4,
260389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
261389056e5SFelix Kuehling 	.supports_cwsr = true,
262389056e5SFelix Kuehling 	.needs_iommu_device = false,
263389056e5SFelix Kuehling 	.needs_pci_atomics = false,
26498bb9222SYong Zhao 	.num_sdma_engines = 2,
2651b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
266d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
267389056e5SFelix Kuehling };
268389056e5SFelix Kuehling 
269389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_device_info = {
270389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
271389056e5SFelix Kuehling 	.max_pasid_bits = 16,
272389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
273389056e5SFelix Kuehling 	.doorbell_size  = 8,
274389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
275389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
276389056e5SFelix Kuehling 	.num_of_watch_points = 4,
277389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
278389056e5SFelix Kuehling 	.supports_cwsr = true,
279389056e5SFelix Kuehling 	.needs_iommu_device = false,
280389056e5SFelix Kuehling 	.needs_pci_atomics = false,
28198bb9222SYong Zhao 	.num_sdma_engines = 2,
2821b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
283d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
284389056e5SFelix Kuehling };
285389056e5SFelix Kuehling 
286846a44d7SGang Ba static const struct kfd_device_info vega12_device_info = {
287846a44d7SGang Ba 	.asic_family = CHIP_VEGA12,
288846a44d7SGang Ba 	.max_pasid_bits = 16,
289846a44d7SGang Ba 	.max_no_of_hqd  = 24,
290846a44d7SGang Ba 	.doorbell_size  = 8,
291846a44d7SGang Ba 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
292846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_v9,
293846a44d7SGang Ba 	.num_of_watch_points = 4,
294846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
295846a44d7SGang Ba 	.supports_cwsr = true,
296846a44d7SGang Ba 	.needs_iommu_device = false,
297846a44d7SGang Ba 	.needs_pci_atomics = false,
298846a44d7SGang Ba 	.num_sdma_engines = 2,
2991b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
300846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
301846a44d7SGang Ba };
302846a44d7SGang Ba 
30322a3a294SShaoyun Liu static const struct kfd_device_info vega20_device_info = {
30422a3a294SShaoyun Liu 	.asic_family = CHIP_VEGA20,
30522a3a294SShaoyun Liu 	.max_pasid_bits = 16,
30622a3a294SShaoyun Liu 	.max_no_of_hqd	= 24,
30722a3a294SShaoyun Liu 	.doorbell_size	= 8,
30822a3a294SShaoyun Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
30922a3a294SShaoyun Liu 	.event_interrupt_class = &event_interrupt_class_v9,
31022a3a294SShaoyun Liu 	.num_of_watch_points = 4,
31122a3a294SShaoyun Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
31222a3a294SShaoyun Liu 	.supports_cwsr = true,
31322a3a294SShaoyun Liu 	.needs_iommu_device = false,
314006a0b3dSShaoyun Liu 	.needs_pci_atomics = false,
31522a3a294SShaoyun Liu 	.num_sdma_engines = 2,
3161b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
31722a3a294SShaoyun Liu 	.num_sdma_queues_per_engine = 8,
31822a3a294SShaoyun Liu };
31922a3a294SShaoyun Liu 
32049adcf8aSYong Zhao static const struct kfd_device_info arcturus_device_info = {
32149adcf8aSYong Zhao 	.asic_family = CHIP_ARCTURUS,
32249adcf8aSYong Zhao 	.max_pasid_bits = 16,
32349adcf8aSYong Zhao 	.max_no_of_hqd	= 24,
32449adcf8aSYong Zhao 	.doorbell_size	= 8,
32549adcf8aSYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
32649adcf8aSYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
32749adcf8aSYong Zhao 	.num_of_watch_points = 4,
32849adcf8aSYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
32949adcf8aSYong Zhao 	.supports_cwsr = true,
33049adcf8aSYong Zhao 	.needs_iommu_device = false,
33149adcf8aSYong Zhao 	.needs_pci_atomics = false,
332b6689cf7SOak Zeng 	.num_sdma_engines = 2,
333b6689cf7SOak Zeng 	.num_xgmi_sdma_engines = 6,
33449adcf8aSYong Zhao 	.num_sdma_queues_per_engine = 8,
33549adcf8aSYong Zhao };
33649adcf8aSYong Zhao 
33714328aa5SPhilip Cox static const struct kfd_device_info navi10_device_info = {
33814328aa5SPhilip Cox 	.asic_family = CHIP_NAVI10,
33914328aa5SPhilip Cox 	.max_pasid_bits = 16,
34014328aa5SPhilip Cox 	.max_no_of_hqd  = 24,
34114328aa5SPhilip Cox 	.doorbell_size  = 8,
34214328aa5SPhilip Cox 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
34314328aa5SPhilip Cox 	.event_interrupt_class = &event_interrupt_class_v9,
34414328aa5SPhilip Cox 	.num_of_watch_points = 4,
34514328aa5SPhilip Cox 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
34614328aa5SPhilip Cox 	.needs_iommu_device = false,
34714328aa5SPhilip Cox 	.supports_cwsr = true,
34814328aa5SPhilip Cox 	.needs_pci_atomics = false,
34914328aa5SPhilip Cox 	.num_sdma_engines = 2,
35014328aa5SPhilip Cox 	.num_xgmi_sdma_engines = 0,
35114328aa5SPhilip Cox 	.num_sdma_queues_per_engine = 8,
35214328aa5SPhilip Cox };
35314328aa5SPhilip Cox 
3544a488a7aSOded Gabbay struct kfd_deviceid {
3554a488a7aSOded Gabbay 	unsigned short did;
3564a488a7aSOded Gabbay 	const struct kfd_device_info *device_info;
3574a488a7aSOded Gabbay };
3584a488a7aSOded Gabbay 
3594a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = {
36064d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
3614a488a7aSOded Gabbay 	{ 0x1304, &kaveri_device_info },	/* Kaveri */
3624a488a7aSOded Gabbay 	{ 0x1305, &kaveri_device_info },	/* Kaveri */
3634a488a7aSOded Gabbay 	{ 0x1306, &kaveri_device_info },	/* Kaveri */
3644a488a7aSOded Gabbay 	{ 0x1307, &kaveri_device_info },	/* Kaveri */
3654a488a7aSOded Gabbay 	{ 0x1309, &kaveri_device_info },	/* Kaveri */
3664a488a7aSOded Gabbay 	{ 0x130A, &kaveri_device_info },	/* Kaveri */
3674a488a7aSOded Gabbay 	{ 0x130B, &kaveri_device_info },	/* Kaveri */
3684a488a7aSOded Gabbay 	{ 0x130C, &kaveri_device_info },	/* Kaveri */
3694a488a7aSOded Gabbay 	{ 0x130D, &kaveri_device_info },	/* Kaveri */
3704a488a7aSOded Gabbay 	{ 0x130E, &kaveri_device_info },	/* Kaveri */
3714a488a7aSOded Gabbay 	{ 0x130F, &kaveri_device_info },	/* Kaveri */
3724a488a7aSOded Gabbay 	{ 0x1310, &kaveri_device_info },	/* Kaveri */
3734a488a7aSOded Gabbay 	{ 0x1311, &kaveri_device_info },	/* Kaveri */
3744a488a7aSOded Gabbay 	{ 0x1312, &kaveri_device_info },	/* Kaveri */
3754a488a7aSOded Gabbay 	{ 0x1313, &kaveri_device_info },	/* Kaveri */
3764a488a7aSOded Gabbay 	{ 0x1315, &kaveri_device_info },	/* Kaveri */
3774a488a7aSOded Gabbay 	{ 0x1316, &kaveri_device_info },	/* Kaveri */
3784a488a7aSOded Gabbay 	{ 0x1317, &kaveri_device_info },	/* Kaveri */
3794a488a7aSOded Gabbay 	{ 0x1318, &kaveri_device_info },	/* Kaveri */
3804a488a7aSOded Gabbay 	{ 0x131B, &kaveri_device_info },	/* Kaveri */
3814a488a7aSOded Gabbay 	{ 0x131C, &kaveri_device_info },	/* Kaveri */
382123576d1SBen Goz 	{ 0x131D, &kaveri_device_info },	/* Kaveri */
383123576d1SBen Goz 	{ 0x9870, &carrizo_device_info },	/* Carrizo */
384123576d1SBen Goz 	{ 0x9874, &carrizo_device_info },	/* Carrizo */
385123576d1SBen Goz 	{ 0x9875, &carrizo_device_info },	/* Carrizo */
386123576d1SBen Goz 	{ 0x9876, &carrizo_device_info },	/* Carrizo */
387a3084e6cSFelix Kuehling 	{ 0x9877, &carrizo_device_info },	/* Carrizo */
3884d663df6SYong Zhao 	{ 0x15DD, &raven_device_info },		/* Raven */
389e7ad8855SAlex Deucher 	{ 0x15D8, &raven_device_info },		/* Raven */
39064d1c3a4SFelix Kuehling #endif
391a3084e6cSFelix Kuehling 	{ 0x67A0, &hawaii_device_info },	/* Hawaii */
392a3084e6cSFelix Kuehling 	{ 0x67A1, &hawaii_device_info },	/* Hawaii */
393a3084e6cSFelix Kuehling 	{ 0x67A2, &hawaii_device_info },	/* Hawaii */
394a3084e6cSFelix Kuehling 	{ 0x67A8, &hawaii_device_info },	/* Hawaii */
395a3084e6cSFelix Kuehling 	{ 0x67A9, &hawaii_device_info },	/* Hawaii */
396a3084e6cSFelix Kuehling 	{ 0x67AA, &hawaii_device_info },	/* Hawaii */
397a3084e6cSFelix Kuehling 	{ 0x67B0, &hawaii_device_info },	/* Hawaii */
398a3084e6cSFelix Kuehling 	{ 0x67B1, &hawaii_device_info },	/* Hawaii */
399a3084e6cSFelix Kuehling 	{ 0x67B8, &hawaii_device_info },	/* Hawaii */
400a3084e6cSFelix Kuehling 	{ 0x67B9, &hawaii_device_info },	/* Hawaii */
401a3084e6cSFelix Kuehling 	{ 0x67BA, &hawaii_device_info },	/* Hawaii */
402a3084e6cSFelix Kuehling 	{ 0x67BE, &hawaii_device_info },	/* Hawaii */
403a3084e6cSFelix Kuehling 	{ 0x6920, &tonga_device_info },		/* Tonga */
404a3084e6cSFelix Kuehling 	{ 0x6921, &tonga_device_info },		/* Tonga */
405a3084e6cSFelix Kuehling 	{ 0x6928, &tonga_device_info },		/* Tonga */
406a3084e6cSFelix Kuehling 	{ 0x6929, &tonga_device_info },		/* Tonga */
407a3084e6cSFelix Kuehling 	{ 0x692B, &tonga_device_info },		/* Tonga */
408a3084e6cSFelix Kuehling 	{ 0x6938, &tonga_device_info },		/* Tonga */
409a3084e6cSFelix Kuehling 	{ 0x6939, &tonga_device_info },		/* Tonga */
410a3084e6cSFelix Kuehling 	{ 0x7300, &fiji_device_info },		/* Fiji */
411a3084e6cSFelix Kuehling 	{ 0x730F, &fiji_vf_device_info },	/* Fiji vf*/
412a3084e6cSFelix Kuehling 	{ 0x67C0, &polaris10_device_info },	/* Polaris10 */
413a3084e6cSFelix Kuehling 	{ 0x67C1, &polaris10_device_info },	/* Polaris10 */
414a3084e6cSFelix Kuehling 	{ 0x67C2, &polaris10_device_info },	/* Polaris10 */
415a3084e6cSFelix Kuehling 	{ 0x67C4, &polaris10_device_info },	/* Polaris10 */
416a3084e6cSFelix Kuehling 	{ 0x67C7, &polaris10_device_info },	/* Polaris10 */
417a3084e6cSFelix Kuehling 	{ 0x67C8, &polaris10_device_info },	/* Polaris10 */
418a3084e6cSFelix Kuehling 	{ 0x67C9, &polaris10_device_info },	/* Polaris10 */
419a3084e6cSFelix Kuehling 	{ 0x67CA, &polaris10_device_info },	/* Polaris10 */
420a3084e6cSFelix Kuehling 	{ 0x67CC, &polaris10_device_info },	/* Polaris10 */
421a3084e6cSFelix Kuehling 	{ 0x67CF, &polaris10_device_info },	/* Polaris10 */
422a3084e6cSFelix Kuehling 	{ 0x67D0, &polaris10_vf_device_info },	/* Polaris10 vf*/
423a3084e6cSFelix Kuehling 	{ 0x67DF, &polaris10_device_info },	/* Polaris10 */
4240a5a9c27SKent Russell 	{ 0x6FDF, &polaris10_device_info },	/* Polaris10 */
425a3084e6cSFelix Kuehling 	{ 0x67E0, &polaris11_device_info },	/* Polaris11 */
426a3084e6cSFelix Kuehling 	{ 0x67E1, &polaris11_device_info },	/* Polaris11 */
427a3084e6cSFelix Kuehling 	{ 0x67E3, &polaris11_device_info },	/* Polaris11 */
428a3084e6cSFelix Kuehling 	{ 0x67E7, &polaris11_device_info },	/* Polaris11 */
429a3084e6cSFelix Kuehling 	{ 0x67E8, &polaris11_device_info },	/* Polaris11 */
430a3084e6cSFelix Kuehling 	{ 0x67E9, &polaris11_device_info },	/* Polaris11 */
431a3084e6cSFelix Kuehling 	{ 0x67EB, &polaris11_device_info },	/* Polaris11 */
432a3084e6cSFelix Kuehling 	{ 0x67EF, &polaris11_device_info },	/* Polaris11 */
433a3084e6cSFelix Kuehling 	{ 0x67FF, &polaris11_device_info },	/* Polaris11 */
434846a44d7SGang Ba 	{ 0x6980, &polaris12_device_info },	/* Polaris12 */
435846a44d7SGang Ba 	{ 0x6981, &polaris12_device_info },	/* Polaris12 */
436846a44d7SGang Ba 	{ 0x6985, &polaris12_device_info },	/* Polaris12 */
437846a44d7SGang Ba 	{ 0x6986, &polaris12_device_info },	/* Polaris12 */
438846a44d7SGang Ba 	{ 0x6987, &polaris12_device_info },	/* Polaris12 */
439846a44d7SGang Ba 	{ 0x6995, &polaris12_device_info },	/* Polaris12 */
440846a44d7SGang Ba 	{ 0x6997, &polaris12_device_info },	/* Polaris12 */
441846a44d7SGang Ba 	{ 0x699F, &polaris12_device_info },	/* Polaris12 */
442ed81cd6eSKent Russell 	{ 0x694C, &vegam_device_info },		/* VegaM */
443ed81cd6eSKent Russell 	{ 0x694E, &vegam_device_info },		/* VegaM */
444ed81cd6eSKent Russell 	{ 0x694F, &vegam_device_info },		/* VegaM */
445389056e5SFelix Kuehling 	{ 0x6860, &vega10_device_info },	/* Vega10 */
446389056e5SFelix Kuehling 	{ 0x6861, &vega10_device_info },	/* Vega10 */
447389056e5SFelix Kuehling 	{ 0x6862, &vega10_device_info },	/* Vega10 */
448389056e5SFelix Kuehling 	{ 0x6863, &vega10_device_info },	/* Vega10 */
449389056e5SFelix Kuehling 	{ 0x6864, &vega10_device_info },	/* Vega10 */
450389056e5SFelix Kuehling 	{ 0x6867, &vega10_device_info },	/* Vega10 */
451389056e5SFelix Kuehling 	{ 0x6868, &vega10_device_info },	/* Vega10 */
452756e16bfSAlex Deucher 	{ 0x6869, &vega10_device_info },	/* Vega10 */
453756e16bfSAlex Deucher 	{ 0x686A, &vega10_device_info },	/* Vega10 */
454756e16bfSAlex Deucher 	{ 0x686B, &vega10_device_info },	/* Vega10 */
455389056e5SFelix Kuehling 	{ 0x686C, &vega10_vf_device_info },	/* Vega10  vf*/
456756e16bfSAlex Deucher 	{ 0x686D, &vega10_device_info },	/* Vega10 */
457756e16bfSAlex Deucher 	{ 0x686E, &vega10_device_info },	/* Vega10 */
458756e16bfSAlex Deucher 	{ 0x686F, &vega10_device_info },	/* Vega10 */
459389056e5SFelix Kuehling 	{ 0x687F, &vega10_device_info },	/* Vega10 */
460846a44d7SGang Ba 	{ 0x69A0, &vega12_device_info },	/* Vega12 */
461846a44d7SGang Ba 	{ 0x69A1, &vega12_device_info },	/* Vega12 */
462846a44d7SGang Ba 	{ 0x69A2, &vega12_device_info },	/* Vega12 */
463846a44d7SGang Ba 	{ 0x69A3, &vega12_device_info },	/* Vega12 */
464846a44d7SGang Ba 	{ 0x69AF, &vega12_device_info },	/* Vega12 */
46522a3a294SShaoyun Liu 	{ 0x66a0, &vega20_device_info },	/* Vega20 */
46622a3a294SShaoyun Liu 	{ 0x66a1, &vega20_device_info },	/* Vega20 */
46722a3a294SShaoyun Liu 	{ 0x66a2, &vega20_device_info },	/* Vega20 */
46822a3a294SShaoyun Liu 	{ 0x66a3, &vega20_device_info },	/* Vega20 */
4699bd206f8SAlex Deucher 	{ 0x66a4, &vega20_device_info },	/* Vega20 */
47022a3a294SShaoyun Liu 	{ 0x66a7, &vega20_device_info },	/* Vega20 */
47114328aa5SPhilip Cox 	{ 0x66af, &vega20_device_info },	/* Vega20 */
47214328aa5SPhilip Cox 	/* Navi10 */
47314328aa5SPhilip Cox 	{ 0x7310, &navi10_device_info },	/* Navi10 */
47414328aa5SPhilip Cox 	{ 0x7312, &navi10_device_info },	/* Navi10 */
47514328aa5SPhilip Cox 	{ 0x7318, &navi10_device_info },	/* Navi10 */
47614328aa5SPhilip Cox 	{ 0x731a, &navi10_device_info },	/* Navi10 */
47714328aa5SPhilip Cox 	{ 0x731f, &navi10_device_info },	/* Navi10 */
4784a488a7aSOded Gabbay };
4794a488a7aSOded Gabbay 
4806e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
4816e81090bSOded Gabbay 				unsigned int chunk_size);
4826e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
4836e81090bSOded Gabbay 
484b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
485b8935a7cSYong Zhao 
4864a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did)
4874a488a7aSOded Gabbay {
4884a488a7aSOded Gabbay 	size_t i;
4894a488a7aSOded Gabbay 
4904a488a7aSOded Gabbay 	for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
4914a488a7aSOded Gabbay 		if (supported_devices[i].did == did) {
49232fa8219SFelix Kuehling 			WARN_ON(!supported_devices[i].device_info);
4934a488a7aSOded Gabbay 			return supported_devices[i].device_info;
4944a488a7aSOded Gabbay 		}
4954a488a7aSOded Gabbay 	}
4964a488a7aSOded Gabbay 
4974ebc7182SYong Zhao 	dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
4984ebc7182SYong Zhao 		 did);
4994ebc7182SYong Zhao 
5004a488a7aSOded Gabbay 	return NULL;
5014a488a7aSOded Gabbay }
5024a488a7aSOded Gabbay 
503cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
504cea405b1SXihan Zhang 	struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
5054a488a7aSOded Gabbay {
5064a488a7aSOded Gabbay 	struct kfd_dev *kfd;
5074a488a7aSOded Gabbay 	const struct kfd_device_info *device_info =
5084a488a7aSOded Gabbay 					lookup_device_info(pdev->device);
5094a488a7aSOded Gabbay 
5104ebc7182SYong Zhao 	if (!device_info) {
5114ebc7182SYong Zhao 		dev_err(kfd_device, "kgd2kfd_probe failed\n");
5124a488a7aSOded Gabbay 		return NULL;
5134ebc7182SYong Zhao 	}
5144a488a7aSOded Gabbay 
515d35f00d8SEric Huang 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
516d35f00d8SEric Huang 	if (!kfd)
517d35f00d8SEric Huang 		return NULL;
518d35f00d8SEric Huang 
5196106dce9Swelu 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
5206106dce9Swelu 	 * 32 and 64-bit requests are possible and must be
5216106dce9Swelu 	 * supported.
5223ee2d00cSFelix Kuehling 	 */
523aabf3a95SJack Xiao 	kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kgd);
524aabf3a95SJack Xiao 	if (device_info->needs_pci_atomics &&
525aabf3a95SJack Xiao 	    !kfd->pci_atomic_requested) {
5263ee2d00cSFelix Kuehling 		dev_info(kfd_device,
5276106dce9Swelu 			 "skipped device %x:%x, PCI rejects atomics\n",
5283ee2d00cSFelix Kuehling 			 pdev->vendor, pdev->device);
529d35f00d8SEric Huang 		kfree(kfd);
5303ee2d00cSFelix Kuehling 		return NULL;
531aabf3a95SJack Xiao 	}
5324a488a7aSOded Gabbay 
5334a488a7aSOded Gabbay 	kfd->kgd = kgd;
5344a488a7aSOded Gabbay 	kfd->device_info = device_info;
5354a488a7aSOded Gabbay 	kfd->pdev = pdev;
53619f6d2a6SOded Gabbay 	kfd->init_complete = false;
537cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
53843d8107fSHarish Kasiviswanathan 	atomic_set(&kfd->compute_profile, 0);
539cea405b1SXihan Zhang 
540cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
541cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
542cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
5434a488a7aSOded Gabbay 
5449b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
5459b54d201SEric Huang 
5464a488a7aSOded Gabbay 	return kfd;
5474a488a7aSOded Gabbay }
5484a488a7aSOded Gabbay 
549373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
550373d7080SFelix Kuehling {
551373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
5523e76c239SFelix Kuehling 		if (kfd->device_info->asic_family < CHIP_VEGA10) {
553373d7080SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
554373d7080SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
555373d7080SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
556*3baa24f0SOak Zeng 		} else if (kfd->device_info->asic_family == CHIP_ARCTURUS) {
557*3baa24f0SOak Zeng 			BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
558*3baa24f0SOak Zeng 			kfd->cwsr_isa = cwsr_trap_arcturus_hex;
559*3baa24f0SOak Zeng 			kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
56014328aa5SPhilip Cox 		} else if (kfd->device_info->asic_family < CHIP_NAVI10) {
5613e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
5623e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
5633e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
56414328aa5SPhilip Cox 		} else {
56514328aa5SPhilip Cox 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
56614328aa5SPhilip Cox 			kfd->cwsr_isa = cwsr_trap_gfx10_hex;
56714328aa5SPhilip Cox 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
5683e76c239SFelix Kuehling 		}
5693e76c239SFelix Kuehling 
570373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
571373d7080SFelix Kuehling 	}
572373d7080SFelix Kuehling }
573373d7080SFelix Kuehling 
5744a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
5754a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
5764a488a7aSOded Gabbay {
57719f6d2a6SOded Gabbay 	unsigned int size;
57819f6d2a6SOded Gabbay 
5790da8b10eSAmber Lin 	kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
5805ade6c9cSFelix Kuehling 			KGD_ENGINE_MEC1);
5810da8b10eSAmber Lin 	kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
5825ade6c9cSFelix Kuehling 			KGD_ENGINE_SDMA1);
5834a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
5844a488a7aSOded Gabbay 
58544008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
58644008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
58744008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
58844008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
58944008d7aSYong Zhao 
590a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
591a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
592a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
593a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
594a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
595a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
596a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
597a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
598a99c6d4fSFelix Kuehling 	} else
599a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
600a99c6d4fSFelix Kuehling 
601e09d4fc8SOak Zeng 	/* Allocate global GWS that is shared by all KFD processes */
602e09d4fc8SOak Zeng 	if (hws_gws_support && amdgpu_amdkfd_alloc_gws(kfd->kgd,
603e09d4fc8SOak Zeng 			amdgpu_amdkfd_get_num_gws(kfd->kgd), &kfd->gws)) {
604e09d4fc8SOak Zeng 		dev_err(kfd_device, "Could not allocate %d gws\n",
605e09d4fc8SOak Zeng 			amdgpu_amdkfd_get_num_gws(kfd->kgd));
606e09d4fc8SOak Zeng 		goto out;
607e09d4fc8SOak Zeng 	}
60819f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
609b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
61019f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
61119f6d2a6SOded Gabbay 
612e18e794eSOded Gabbay 	/*
613e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
614e18e794eSOded Gabbay 	 * There can be only 2 packets at once
615e18e794eSOded Gabbay 	 */
616507968ddSFelix Kuehling 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
617507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
618507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
619e18e794eSOded Gabbay 
620e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
621e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
622e18e794eSOded Gabbay 
623e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
62419f6d2a6SOded Gabbay 	size += 512 * 1024;
62519f6d2a6SOded Gabbay 
6267cd52c91SAmber Lin 	if (amdgpu_amdkfd_alloc_gtt_mem(
627cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
62815426dbbSYong Zhao 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
62915426dbbSYong Zhao 			false)) {
63079775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
631e09d4fc8SOak Zeng 		goto alloc_gtt_mem_failure;
63219f6d2a6SOded Gabbay 	}
63319f6d2a6SOded Gabbay 
63479775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
635e18e794eSOded Gabbay 
63673a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
63773a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
63879775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
63973a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
64073a1da0bSOded Gabbay 	}
64173a1da0bSOded Gabbay 
642735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
643735df2baSFelix Kuehling 		dev_err(kfd_device,
644735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
645735df2baSFelix Kuehling 		goto kfd_doorbell_error;
646735df2baSFelix Kuehling 	}
64719f6d2a6SOded Gabbay 
6480c1690e3SShaoyun Liu 	if (kfd->kfd2kgd->get_hive_id)
6490c1690e3SShaoyun Liu 		kfd->hive_id = kfd->kfd2kgd->get_hive_id(kfd->kgd);
6500c1690e3SShaoyun Liu 
6512249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
65279775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
6532249d558SAndrew Lewycky 		goto kfd_interrupt_error;
6542249d558SAndrew Lewycky 	}
6552249d558SAndrew Lewycky 
65664c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
65764c7f8cfSBen Goz 	if (!kfd->dqm) {
65879775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
65964c7f8cfSBen Goz 		goto device_queue_manager_error;
66064c7f8cfSBen Goz 	}
66164c7f8cfSBen Goz 
66264d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
66364d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
66464d1c3a4SFelix Kuehling 		goto device_iommu_error;
66564c7f8cfSBen Goz 	}
66664c7f8cfSBen Goz 
667373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
668373d7080SFelix Kuehling 
669b8935a7cSYong Zhao 	if (kfd_resume(kfd))
670b8935a7cSYong Zhao 		goto kfd_resume_error;
671b8935a7cSYong Zhao 
672fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
673fbeb661bSYair Shachar 
674465ab9e0SOak Zeng 	if (kfd_topology_add_device(kfd)) {
675465ab9e0SOak Zeng 		dev_err(kfd_device, "Error adding device to topology\n");
676465ab9e0SOak Zeng 		goto kfd_topology_add_device_error;
677465ab9e0SOak Zeng 	}
678465ab9e0SOak Zeng 
6794a488a7aSOded Gabbay 	kfd->init_complete = true;
68079775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
6814a488a7aSOded Gabbay 		 kfd->pdev->device);
6824a488a7aSOded Gabbay 
68379775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
684d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
68564c7f8cfSBen Goz 
68619f6d2a6SOded Gabbay 	goto out;
68719f6d2a6SOded Gabbay 
688465ab9e0SOak Zeng kfd_topology_add_device_error:
689b8935a7cSYong Zhao kfd_resume_error:
69064d1c3a4SFelix Kuehling device_iommu_error:
69164c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
69264c7f8cfSBen Goz device_queue_manager_error:
6932249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
6942249d558SAndrew Lewycky kfd_interrupt_error:
695735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
696735df2baSFelix Kuehling kfd_doorbell_error:
69773a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
69873a1da0bSOded Gabbay kfd_gtt_sa_init_error:
6997cd52c91SAmber Lin 	amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
700e09d4fc8SOak Zeng alloc_gtt_mem_failure:
701e09d4fc8SOak Zeng 	if (hws_gws_support)
702e09d4fc8SOak Zeng 		amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
70319f6d2a6SOded Gabbay 	dev_err(kfd_device,
70479775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
70519f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
70619f6d2a6SOded Gabbay out:
70719f6d2a6SOded Gabbay 	return kfd->init_complete;
7084a488a7aSOded Gabbay }
7094a488a7aSOded Gabbay 
7104a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
7114a488a7aSOded Gabbay {
712b17f068aSOded Gabbay 	if (kfd->init_complete) {
713b8935a7cSYong Zhao 		kgd2kfd_suspend(kfd);
71464c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
7152249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
71619f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
717735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
71873a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
7197cd52c91SAmber Lin 		amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
720e09d4fc8SOak Zeng 		if (hws_gws_support)
721e09d4fc8SOak Zeng 			amdgpu_amdkfd_free_gws(kfd->kgd, kfd->gws);
722b17f068aSOded Gabbay 	}
7235b5c4e40SEvgeny Pinchuk 
7244a488a7aSOded Gabbay 	kfree(kfd);
7254a488a7aSOded Gabbay }
7264a488a7aSOded Gabbay 
727e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
728e3b7a967SShaoyun Liu {
729e42051d2SShaoyun Liu 	if (!kfd->init_complete)
730e42051d2SShaoyun Liu 		return 0;
731e42051d2SShaoyun Liu 	kgd2kfd_suspend(kfd);
732e42051d2SShaoyun Liu 
733e42051d2SShaoyun Liu 	/* hold dqm->lock to prevent further execution*/
734e42051d2SShaoyun Liu 	dqm_lock(kfd->dqm);
735e42051d2SShaoyun Liu 
736e42051d2SShaoyun Liu 	kfd_signal_reset_event(kfd);
737e3b7a967SShaoyun Liu 	return 0;
738e3b7a967SShaoyun Liu }
739e3b7a967SShaoyun Liu 
740e42051d2SShaoyun Liu /*
741e42051d2SShaoyun Liu  * Fix me. KFD won't be able to resume existing process for now.
742e42051d2SShaoyun Liu  * We will keep all existing process in a evicted state and
743e42051d2SShaoyun Liu  * wait the process to be terminated.
744e42051d2SShaoyun Liu  */
745e42051d2SShaoyun Liu 
746e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
747e3b7a967SShaoyun Liu {
748e42051d2SShaoyun Liu 	int ret, count;
749e42051d2SShaoyun Liu 
750e42051d2SShaoyun Liu 	if (!kfd->init_complete)
751e3b7a967SShaoyun Liu 		return 0;
752e42051d2SShaoyun Liu 
753e42051d2SShaoyun Liu 	dqm_unlock(kfd->dqm);
754e42051d2SShaoyun Liu 
755e42051d2SShaoyun Liu 	ret = kfd_resume(kfd);
756e42051d2SShaoyun Liu 	if (ret)
757e42051d2SShaoyun Liu 		return ret;
758e42051d2SShaoyun Liu 	count = atomic_dec_return(&kfd_locked);
7599b54d201SEric Huang 
7609b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
7619b54d201SEric Huang 
762e42051d2SShaoyun Liu 	return 0;
763e42051d2SShaoyun Liu }
764e42051d2SShaoyun Liu 
765e42051d2SShaoyun Liu bool kfd_is_locked(void)
766e42051d2SShaoyun Liu {
767e42051d2SShaoyun Liu 	return  (atomic_read(&kfd_locked) > 0);
768e3b7a967SShaoyun Liu }
769e3b7a967SShaoyun Liu 
7704a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd)
7714a488a7aSOded Gabbay {
772733fa1f7SYong Zhao 	if (!kfd->init_complete)
773733fa1f7SYong Zhao 		return;
774733fa1f7SYong Zhao 
77526103436SFelix Kuehling 	/* For first KFD device suspend all the KFD processes */
776e42051d2SShaoyun Liu 	if (atomic_inc_return(&kfd_locked) == 1)
77726103436SFelix Kuehling 		kfd_suspend_all_processes();
77826103436SFelix Kuehling 
77945c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
780733fa1f7SYong Zhao 
78164d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
7824a488a7aSOded Gabbay }
7834a488a7aSOded Gabbay 
7844a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd)
7854a488a7aSOded Gabbay {
78626103436SFelix Kuehling 	int ret, count;
78726103436SFelix Kuehling 
788b8935a7cSYong Zhao 	if (!kfd->init_complete)
789b8935a7cSYong Zhao 		return 0;
790b17f068aSOded Gabbay 
79126103436SFelix Kuehling 	ret = kfd_resume(kfd);
79226103436SFelix Kuehling 	if (ret)
79326103436SFelix Kuehling 		return ret;
794b17f068aSOded Gabbay 
795e42051d2SShaoyun Liu 	count = atomic_dec_return(&kfd_locked);
79626103436SFelix Kuehling 	WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
79726103436SFelix Kuehling 	if (count == 0)
79826103436SFelix Kuehling 		ret = kfd_resume_all_processes();
79926103436SFelix Kuehling 
80026103436SFelix Kuehling 	return ret;
8014ebc7182SYong Zhao }
8024ebc7182SYong Zhao 
803b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
804b8935a7cSYong Zhao {
805b8935a7cSYong Zhao 	int err = 0;
806b8935a7cSYong Zhao 
80764d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
80864d1c3a4SFelix Kuehling 	if (err) {
80964d1c3a4SFelix Kuehling 		dev_err(kfd_device,
81064d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
81164d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
81264d1c3a4SFelix Kuehling 		return err;
81364d1c3a4SFelix Kuehling 	}
814733fa1f7SYong Zhao 
815b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
816b8935a7cSYong Zhao 	if (err) {
817b8935a7cSYong Zhao 		dev_err(kfd_device,
818b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
819b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
820b8935a7cSYong Zhao 		goto dqm_start_error;
821b17f068aSOded Gabbay 	}
822b17f068aSOded Gabbay 
823b8935a7cSYong Zhao 	return err;
824b8935a7cSYong Zhao 
825b8935a7cSYong Zhao dqm_start_error:
82664d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
827b8935a7cSYong Zhao 	return err;
8284a488a7aSOded Gabbay }
8294a488a7aSOded Gabbay 
830b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
831b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
8324a488a7aSOded Gabbay {
83358e69886SLan Xiao 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
83458e69886SLan Xiao 	bool is_patched = false;
8352383a767SChristian König 	unsigned long flags;
83658e69886SLan Xiao 
8372249d558SAndrew Lewycky 	if (!kfd->init_complete)
8382249d558SAndrew Lewycky 		return;
8392249d558SAndrew Lewycky 
84058e69886SLan Xiao 	if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
84158e69886SLan Xiao 		dev_err_once(kfd_device, "Ring entry too small\n");
84258e69886SLan Xiao 		return;
84358e69886SLan Xiao 	}
84458e69886SLan Xiao 
8452383a767SChristian König 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
8462249d558SAndrew Lewycky 
8472249d558SAndrew Lewycky 	if (kfd->interrupts_active
84858e69886SLan Xiao 	    && interrupt_is_wanted(kfd, ih_ring_entry,
84958e69886SLan Xiao 				   patched_ihre, &is_patched)
85058e69886SLan Xiao 	    && enqueue_ih_ring_entry(kfd,
85158e69886SLan Xiao 				     is_patched ? patched_ihre : ih_ring_entry))
85248e876a2SAndres Rodriguez 		queue_work(kfd->ih_wq, &kfd->interrupt_work);
8532249d558SAndrew Lewycky 
8542383a767SChristian König 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
8554a488a7aSOded Gabbay }
8566e81090bSOded Gabbay 
8576b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
8586b95e797SFelix Kuehling {
8596b95e797SFelix Kuehling 	struct kfd_process *p;
8606b95e797SFelix Kuehling 	int r;
8616b95e797SFelix Kuehling 
8626b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
8636b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
8646b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
8656b95e797SFelix Kuehling 	 */
8666b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
8676b95e797SFelix Kuehling 	if (!p)
8686b95e797SFelix Kuehling 		return -ESRCH;
8696b95e797SFelix Kuehling 
8706b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
8716b95e797SFelix Kuehling 
8726b95e797SFelix Kuehling 	kfd_unref_process(p);
8736b95e797SFelix Kuehling 	return r;
8746b95e797SFelix Kuehling }
8756b95e797SFelix Kuehling 
8766b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
8776b95e797SFelix Kuehling {
8786b95e797SFelix Kuehling 	struct kfd_process *p;
8796b95e797SFelix Kuehling 	int r;
8806b95e797SFelix Kuehling 
8816b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
8826b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
8836b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
8846b95e797SFelix Kuehling 	 */
8856b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
8866b95e797SFelix Kuehling 	if (!p)
8876b95e797SFelix Kuehling 		return -ESRCH;
8886b95e797SFelix Kuehling 
8896b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
8906b95e797SFelix Kuehling 
8916b95e797SFelix Kuehling 	kfd_unref_process(p);
8926b95e797SFelix Kuehling 	return r;
8936b95e797SFelix Kuehling }
8946b95e797SFelix Kuehling 
89526103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
89626103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
89726103436SFelix Kuehling  *   process.
89826103436SFelix Kuehling  *
89926103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
90026103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
90126103436SFelix Kuehling  *
90226103436SFelix Kuehling  */
90326103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
90426103436SFelix Kuehling 					       struct dma_fence *fence)
90526103436SFelix Kuehling {
90626103436SFelix Kuehling 	struct kfd_process *p;
90726103436SFelix Kuehling 	unsigned long active_time;
90826103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
90926103436SFelix Kuehling 
91026103436SFelix Kuehling 	if (!fence)
91126103436SFelix Kuehling 		return -EINVAL;
91226103436SFelix Kuehling 
91326103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
91426103436SFelix Kuehling 		return 0;
91526103436SFelix Kuehling 
91626103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
91726103436SFelix Kuehling 	if (!p)
91826103436SFelix Kuehling 		return -ENODEV;
91926103436SFelix Kuehling 
92026103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
92126103436SFelix Kuehling 		goto out;
92226103436SFelix Kuehling 
92326103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
92426103436SFelix Kuehling 
92526103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
92626103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
92726103436SFelix Kuehling 	 */
92826103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
92926103436SFelix Kuehling 	if (delay_jiffies > active_time)
93026103436SFelix Kuehling 		delay_jiffies -= active_time;
93126103436SFelix Kuehling 	else
93226103436SFelix Kuehling 		delay_jiffies = 0;
93326103436SFelix Kuehling 
93426103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
93526103436SFelix Kuehling 	 * to kfd_evict_bo_worker
93626103436SFelix Kuehling 	 */
93726103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
93826103436SFelix Kuehling out:
93926103436SFelix Kuehling 	kfd_unref_process(p);
94026103436SFelix Kuehling 	return 0;
94126103436SFelix Kuehling }
94226103436SFelix Kuehling 
9436e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
9446e81090bSOded Gabbay 				unsigned int chunk_size)
9456e81090bSOded Gabbay {
9468625ff9cSFelix Kuehling 	unsigned int num_of_longs;
9476e81090bSOded Gabbay 
94832fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
94932fa8219SFelix Kuehling 		return -EINVAL;
95032fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
95132fa8219SFelix Kuehling 		return -EINVAL;
95232fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
95332fa8219SFelix Kuehling 		return -EINVAL;
9546e81090bSOded Gabbay 
9556e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
9566e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
9576e81090bSOded Gabbay 
9588625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
9598625ff9cSFelix Kuehling 		BITS_PER_LONG;
9606e81090bSOded Gabbay 
9618625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
9626e81090bSOded Gabbay 
9636e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
9646e81090bSOded Gabbay 		return -ENOMEM;
9656e81090bSOded Gabbay 
96679775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
9676e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
9686e81090bSOded Gabbay 
9696e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
9706e81090bSOded Gabbay 
9716e81090bSOded Gabbay 	return 0;
9726e81090bSOded Gabbay 
9736e81090bSOded Gabbay }
9746e81090bSOded Gabbay 
9756e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
9766e81090bSOded Gabbay {
9776e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
9786e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
9796e81090bSOded Gabbay }
9806e81090bSOded Gabbay 
9816e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
9826e81090bSOded Gabbay 						unsigned int bit_num,
9836e81090bSOded Gabbay 						unsigned int chunk_size)
9846e81090bSOded Gabbay {
9856e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
9866e81090bSOded Gabbay }
9876e81090bSOded Gabbay 
9886e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
9896e81090bSOded Gabbay 						unsigned int bit_num,
9906e81090bSOded Gabbay 						unsigned int chunk_size)
9916e81090bSOded Gabbay {
9926e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
9936e81090bSOded Gabbay }
9946e81090bSOded Gabbay 
9956e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
9966e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
9976e81090bSOded Gabbay {
9986e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
9996e81090bSOded Gabbay 
10006e81090bSOded Gabbay 	if (size == 0)
10016e81090bSOded Gabbay 		return -EINVAL;
10026e81090bSOded Gabbay 
10036e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
10046e81090bSOded Gabbay 		return -ENOMEM;
10056e81090bSOded Gabbay 
10061cd106ecSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
10071cd106ecSFelix Kuehling 	if (!(*mem_obj))
10086e81090bSOded Gabbay 		return -ENOMEM;
10096e81090bSOded Gabbay 
101079775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
10116e81090bSOded Gabbay 
10126e81090bSOded Gabbay 	start_search = 0;
10136e81090bSOded Gabbay 
10146e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
10156e81090bSOded Gabbay 
10166e81090bSOded Gabbay kfd_gtt_restart_search:
10176e81090bSOded Gabbay 	/* Find the first chunk that is free */
10186e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
10196e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
10206e81090bSOded Gabbay 					start_search);
10216e81090bSOded Gabbay 
102279775b62SKent Russell 	pr_debug("Found = %d\n", found);
10236e81090bSOded Gabbay 
10246e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
10256e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
10266e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
10276e81090bSOded Gabbay 
10286e81090bSOded Gabbay 	/* Update fields of mem_obj */
10296e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
10306e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
10316e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
10326e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
10336e81090bSOded Gabbay 					found,
10346e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
10356e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
10366e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
10376e81090bSOded Gabbay 					found,
10386e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
10396e81090bSOded Gabbay 
104079775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
10416e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
10426e81090bSOded Gabbay 
10436e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
10446e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
104579775b62SKent Russell 		pr_debug("Single bit\n");
10466e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
10476e81090bSOded Gabbay 		goto kfd_gtt_out;
10486e81090bSOded Gabbay 	}
10496e81090bSOded Gabbay 
10506e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
10516e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
10526e81090bSOded Gabbay 	do {
10536e81090bSOded Gabbay 		(*mem_obj)->range_end =
10546e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
10556e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
10566e81090bSOded Gabbay 		/*
10576e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
10586e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
10596e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
10606e81090bSOded Gabbay 		 */
10616e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
10626e81090bSOded Gabbay 			start_search = found;
10636e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
10646e81090bSOded Gabbay 		}
10656e81090bSOded Gabbay 
10666e81090bSOded Gabbay 		/*
10676e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
10686e81090bSOded Gabbay 		 */
10696e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
10706e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
10716e81090bSOded Gabbay 
10726e81090bSOded Gabbay 		/* Check if we don't need another chunk */
10736e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
10746e81090bSOded Gabbay 			cur_size = 0;
10756e81090bSOded Gabbay 		else
10766e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
10776e81090bSOded Gabbay 
10786e81090bSOded Gabbay 	} while (cur_size > 0);
10796e81090bSOded Gabbay 
108079775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
10816e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
10826e81090bSOded Gabbay 
10836e81090bSOded Gabbay 	/* Mark the chunks as allocated */
10846e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
10856e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
10866e81090bSOded Gabbay 		found++)
10876e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
10886e81090bSOded Gabbay 
10896e81090bSOded Gabbay kfd_gtt_out:
10906e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
10916e81090bSOded Gabbay 	return 0;
10926e81090bSOded Gabbay 
10936e81090bSOded Gabbay kfd_gtt_no_free_chunk:
109479775b62SKent Russell 	pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
10956e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
10966e81090bSOded Gabbay 	kfree(mem_obj);
10976e81090bSOded Gabbay 	return -ENOMEM;
10986e81090bSOded Gabbay }
10996e81090bSOded Gabbay 
11006e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
11016e81090bSOded Gabbay {
11026e81090bSOded Gabbay 	unsigned int bit;
11036e81090bSOded Gabbay 
11049216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
11059216ed29SOded Gabbay 	if (!mem_obj)
11069216ed29SOded Gabbay 		return 0;
11076e81090bSOded Gabbay 
110879775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
11096e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
11106e81090bSOded Gabbay 
11116e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
11126e81090bSOded Gabbay 
11136e81090bSOded Gabbay 	/* Mark the chunks as free */
11146e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
11156e81090bSOded Gabbay 		bit <= mem_obj->range_end;
11166e81090bSOded Gabbay 		bit++)
11176e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
11186e81090bSOded Gabbay 
11196e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
11206e81090bSOded Gabbay 
11216e81090bSOded Gabbay 	kfree(mem_obj);
11226e81090bSOded Gabbay 	return 0;
11236e81090bSOded Gabbay }
1124a29ec470SShaoyun Liu 
11259b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
11269b54d201SEric Huang {
11279b54d201SEric Huang 	if (kfd)
11289b54d201SEric Huang 		atomic_inc(&kfd->sram_ecc_flag);
11299b54d201SEric Huang }
11309b54d201SEric Huang 
113143d8107fSHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd)
113243d8107fSHarish Kasiviswanathan {
113343d8107fSHarish Kasiviswanathan 	if (atomic_inc_return(&kfd->compute_profile) == 1)
113443d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, false);
113543d8107fSHarish Kasiviswanathan }
113643d8107fSHarish Kasiviswanathan 
113743d8107fSHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd)
113843d8107fSHarish Kasiviswanathan {
113943d8107fSHarish Kasiviswanathan 	int count = atomic_dec_return(&kfd->compute_profile);
114043d8107fSHarish Kasiviswanathan 
114143d8107fSHarish Kasiviswanathan 	if (count == 0)
114243d8107fSHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, true);
114343d8107fSHarish Kasiviswanathan 	WARN_ONCE(count < 0, "Compute profile ref. count error");
114443d8107fSHarish Kasiviswanathan }
114543d8107fSHarish Kasiviswanathan 
1146a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
1147a29ec470SShaoyun Liu 
1148a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
1149a29ec470SShaoyun Liu  * which will trigger a GPU reset and bring the HWS back to normal state
1150a29ec470SShaoyun Liu  */
1151a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1152a29ec470SShaoyun Liu {
1153a29ec470SShaoyun Liu 	int r = 0;
1154a29ec470SShaoyun Liu 
1155a29ec470SShaoyun Liu 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1156a29ec470SShaoyun Liu 		pr_err("HWS is not enabled");
1157a29ec470SShaoyun Liu 		return -EINVAL;
1158a29ec470SShaoyun Liu 	}
1159a29ec470SShaoyun Liu 
1160a29ec470SShaoyun Liu 	r = pm_debugfs_hang_hws(&dev->dqm->packets);
1161a29ec470SShaoyun Liu 	if (!r)
1162a29ec470SShaoyun Liu 		r = dqm_debugfs_execute_queues(dev->dqm);
1163a29ec470SShaoyun Liu 
1164a29ec470SShaoyun Liu 	return r;
1165a29ec470SShaoyun Liu }
1166a29ec470SShaoyun Liu 
1167a29ec470SShaoyun Liu #endif
1168