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,
57*1b4670f6SOak 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,
75*1b4670f6SOak 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,
92*1b4670f6SOak 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,
111*1b4670f6SOak 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,
128*1b4670f6SOak 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,
145*1b4670f6SOak 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,
162*1b4670f6SOak 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,
180*1b4670f6SOak 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,
197*1b4670f6SOak 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,
214*1b4670f6SOak 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,
231*1b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
232846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
233846a44d7SGang Ba };
234846a44d7SGang Ba 
235389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = {
236389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
237389056e5SFelix Kuehling 	.max_pasid_bits = 16,
238389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
239389056e5SFelix Kuehling 	.doorbell_size  = 8,
240389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
241389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
242389056e5SFelix Kuehling 	.num_of_watch_points = 4,
243389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
244389056e5SFelix Kuehling 	.supports_cwsr = true,
245389056e5SFelix Kuehling 	.needs_iommu_device = false,
246389056e5SFelix Kuehling 	.needs_pci_atomics = false,
24798bb9222SYong Zhao 	.num_sdma_engines = 2,
248*1b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
249d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
250389056e5SFelix Kuehling };
251389056e5SFelix Kuehling 
252389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_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,
265*1b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
266d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
267389056e5SFelix Kuehling };
268389056e5SFelix Kuehling 
269846a44d7SGang Ba static const struct kfd_device_info vega12_device_info = {
270846a44d7SGang Ba 	.asic_family = CHIP_VEGA12,
271846a44d7SGang Ba 	.max_pasid_bits = 16,
272846a44d7SGang Ba 	.max_no_of_hqd  = 24,
273846a44d7SGang Ba 	.doorbell_size  = 8,
274846a44d7SGang Ba 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
275846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_v9,
276846a44d7SGang Ba 	.num_of_watch_points = 4,
277846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
278846a44d7SGang Ba 	.supports_cwsr = true,
279846a44d7SGang Ba 	.needs_iommu_device = false,
280846a44d7SGang Ba 	.needs_pci_atomics = false,
281846a44d7SGang Ba 	.num_sdma_engines = 2,
282*1b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
283846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
284846a44d7SGang Ba };
285846a44d7SGang Ba 
28622a3a294SShaoyun Liu static const struct kfd_device_info vega20_device_info = {
28722a3a294SShaoyun Liu 	.asic_family = CHIP_VEGA20,
28822a3a294SShaoyun Liu 	.max_pasid_bits = 16,
28922a3a294SShaoyun Liu 	.max_no_of_hqd	= 24,
29022a3a294SShaoyun Liu 	.doorbell_size	= 8,
29122a3a294SShaoyun Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
29222a3a294SShaoyun Liu 	.event_interrupt_class = &event_interrupt_class_v9,
29322a3a294SShaoyun Liu 	.num_of_watch_points = 4,
29422a3a294SShaoyun Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
29522a3a294SShaoyun Liu 	.supports_cwsr = true,
29622a3a294SShaoyun Liu 	.needs_iommu_device = false,
297006a0b3dSShaoyun Liu 	.needs_pci_atomics = false,
29822a3a294SShaoyun Liu 	.num_sdma_engines = 2,
299*1b4670f6SOak Zeng 	.num_xgmi_sdma_engines = 0,
30022a3a294SShaoyun Liu 	.num_sdma_queues_per_engine = 8,
30122a3a294SShaoyun Liu };
30222a3a294SShaoyun Liu 
3034a488a7aSOded Gabbay struct kfd_deviceid {
3044a488a7aSOded Gabbay 	unsigned short did;
3054a488a7aSOded Gabbay 	const struct kfd_device_info *device_info;
3064a488a7aSOded Gabbay };
3074a488a7aSOded Gabbay 
3084a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = {
30964d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
3104a488a7aSOded Gabbay 	{ 0x1304, &kaveri_device_info },	/* Kaveri */
3114a488a7aSOded Gabbay 	{ 0x1305, &kaveri_device_info },	/* Kaveri */
3124a488a7aSOded Gabbay 	{ 0x1306, &kaveri_device_info },	/* Kaveri */
3134a488a7aSOded Gabbay 	{ 0x1307, &kaveri_device_info },	/* Kaveri */
3144a488a7aSOded Gabbay 	{ 0x1309, &kaveri_device_info },	/* Kaveri */
3154a488a7aSOded Gabbay 	{ 0x130A, &kaveri_device_info },	/* Kaveri */
3164a488a7aSOded Gabbay 	{ 0x130B, &kaveri_device_info },	/* Kaveri */
3174a488a7aSOded Gabbay 	{ 0x130C, &kaveri_device_info },	/* Kaveri */
3184a488a7aSOded Gabbay 	{ 0x130D, &kaveri_device_info },	/* Kaveri */
3194a488a7aSOded Gabbay 	{ 0x130E, &kaveri_device_info },	/* Kaveri */
3204a488a7aSOded Gabbay 	{ 0x130F, &kaveri_device_info },	/* Kaveri */
3214a488a7aSOded Gabbay 	{ 0x1310, &kaveri_device_info },	/* Kaveri */
3224a488a7aSOded Gabbay 	{ 0x1311, &kaveri_device_info },	/* Kaveri */
3234a488a7aSOded Gabbay 	{ 0x1312, &kaveri_device_info },	/* Kaveri */
3244a488a7aSOded Gabbay 	{ 0x1313, &kaveri_device_info },	/* Kaveri */
3254a488a7aSOded Gabbay 	{ 0x1315, &kaveri_device_info },	/* Kaveri */
3264a488a7aSOded Gabbay 	{ 0x1316, &kaveri_device_info },	/* Kaveri */
3274a488a7aSOded Gabbay 	{ 0x1317, &kaveri_device_info },	/* Kaveri */
3284a488a7aSOded Gabbay 	{ 0x1318, &kaveri_device_info },	/* Kaveri */
3294a488a7aSOded Gabbay 	{ 0x131B, &kaveri_device_info },	/* Kaveri */
3304a488a7aSOded Gabbay 	{ 0x131C, &kaveri_device_info },	/* Kaveri */
331123576d1SBen Goz 	{ 0x131D, &kaveri_device_info },	/* Kaveri */
332123576d1SBen Goz 	{ 0x9870, &carrizo_device_info },	/* Carrizo */
333123576d1SBen Goz 	{ 0x9874, &carrizo_device_info },	/* Carrizo */
334123576d1SBen Goz 	{ 0x9875, &carrizo_device_info },	/* Carrizo */
335123576d1SBen Goz 	{ 0x9876, &carrizo_device_info },	/* Carrizo */
336a3084e6cSFelix Kuehling 	{ 0x9877, &carrizo_device_info },	/* Carrizo */
3374d663df6SYong Zhao 	{ 0x15DD, &raven_device_info },		/* Raven */
338e7ad8855SAlex Deucher 	{ 0x15D8, &raven_device_info },		/* Raven */
33964d1c3a4SFelix Kuehling #endif
340a3084e6cSFelix Kuehling 	{ 0x67A0, &hawaii_device_info },	/* Hawaii */
341a3084e6cSFelix Kuehling 	{ 0x67A1, &hawaii_device_info },	/* Hawaii */
342a3084e6cSFelix Kuehling 	{ 0x67A2, &hawaii_device_info },	/* Hawaii */
343a3084e6cSFelix Kuehling 	{ 0x67A8, &hawaii_device_info },	/* Hawaii */
344a3084e6cSFelix Kuehling 	{ 0x67A9, &hawaii_device_info },	/* Hawaii */
345a3084e6cSFelix Kuehling 	{ 0x67AA, &hawaii_device_info },	/* Hawaii */
346a3084e6cSFelix Kuehling 	{ 0x67B0, &hawaii_device_info },	/* Hawaii */
347a3084e6cSFelix Kuehling 	{ 0x67B1, &hawaii_device_info },	/* Hawaii */
348a3084e6cSFelix Kuehling 	{ 0x67B8, &hawaii_device_info },	/* Hawaii */
349a3084e6cSFelix Kuehling 	{ 0x67B9, &hawaii_device_info },	/* Hawaii */
350a3084e6cSFelix Kuehling 	{ 0x67BA, &hawaii_device_info },	/* Hawaii */
351a3084e6cSFelix Kuehling 	{ 0x67BE, &hawaii_device_info },	/* Hawaii */
352a3084e6cSFelix Kuehling 	{ 0x6920, &tonga_device_info },		/* Tonga */
353a3084e6cSFelix Kuehling 	{ 0x6921, &tonga_device_info },		/* Tonga */
354a3084e6cSFelix Kuehling 	{ 0x6928, &tonga_device_info },		/* Tonga */
355a3084e6cSFelix Kuehling 	{ 0x6929, &tonga_device_info },		/* Tonga */
356a3084e6cSFelix Kuehling 	{ 0x692B, &tonga_device_info },		/* Tonga */
357a3084e6cSFelix Kuehling 	{ 0x6938, &tonga_device_info },		/* Tonga */
358a3084e6cSFelix Kuehling 	{ 0x6939, &tonga_device_info },		/* Tonga */
359a3084e6cSFelix Kuehling 	{ 0x7300, &fiji_device_info },		/* Fiji */
360a3084e6cSFelix Kuehling 	{ 0x730F, &fiji_vf_device_info },	/* Fiji vf*/
361a3084e6cSFelix Kuehling 	{ 0x67C0, &polaris10_device_info },	/* Polaris10 */
362a3084e6cSFelix Kuehling 	{ 0x67C1, &polaris10_device_info },	/* Polaris10 */
363a3084e6cSFelix Kuehling 	{ 0x67C2, &polaris10_device_info },	/* Polaris10 */
364a3084e6cSFelix Kuehling 	{ 0x67C4, &polaris10_device_info },	/* Polaris10 */
365a3084e6cSFelix Kuehling 	{ 0x67C7, &polaris10_device_info },	/* Polaris10 */
366a3084e6cSFelix Kuehling 	{ 0x67C8, &polaris10_device_info },	/* Polaris10 */
367a3084e6cSFelix Kuehling 	{ 0x67C9, &polaris10_device_info },	/* Polaris10 */
368a3084e6cSFelix Kuehling 	{ 0x67CA, &polaris10_device_info },	/* Polaris10 */
369a3084e6cSFelix Kuehling 	{ 0x67CC, &polaris10_device_info },	/* Polaris10 */
370a3084e6cSFelix Kuehling 	{ 0x67CF, &polaris10_device_info },	/* Polaris10 */
371a3084e6cSFelix Kuehling 	{ 0x67D0, &polaris10_vf_device_info },	/* Polaris10 vf*/
372a3084e6cSFelix Kuehling 	{ 0x67DF, &polaris10_device_info },	/* Polaris10 */
373a3084e6cSFelix Kuehling 	{ 0x67E0, &polaris11_device_info },	/* Polaris11 */
374a3084e6cSFelix Kuehling 	{ 0x67E1, &polaris11_device_info },	/* Polaris11 */
375a3084e6cSFelix Kuehling 	{ 0x67E3, &polaris11_device_info },	/* Polaris11 */
376a3084e6cSFelix Kuehling 	{ 0x67E7, &polaris11_device_info },	/* Polaris11 */
377a3084e6cSFelix Kuehling 	{ 0x67E8, &polaris11_device_info },	/* Polaris11 */
378a3084e6cSFelix Kuehling 	{ 0x67E9, &polaris11_device_info },	/* Polaris11 */
379a3084e6cSFelix Kuehling 	{ 0x67EB, &polaris11_device_info },	/* Polaris11 */
380a3084e6cSFelix Kuehling 	{ 0x67EF, &polaris11_device_info },	/* Polaris11 */
381a3084e6cSFelix Kuehling 	{ 0x67FF, &polaris11_device_info },	/* Polaris11 */
382846a44d7SGang Ba 	{ 0x6980, &polaris12_device_info },	/* Polaris12 */
383846a44d7SGang Ba 	{ 0x6981, &polaris12_device_info },	/* Polaris12 */
384846a44d7SGang Ba 	{ 0x6985, &polaris12_device_info },	/* Polaris12 */
385846a44d7SGang Ba 	{ 0x6986, &polaris12_device_info },	/* Polaris12 */
386846a44d7SGang Ba 	{ 0x6987, &polaris12_device_info },	/* Polaris12 */
387846a44d7SGang Ba 	{ 0x6995, &polaris12_device_info },	/* Polaris12 */
388846a44d7SGang Ba 	{ 0x6997, &polaris12_device_info },	/* Polaris12 */
389846a44d7SGang Ba 	{ 0x699F, &polaris12_device_info },	/* Polaris12 */
390389056e5SFelix Kuehling 	{ 0x6860, &vega10_device_info },	/* Vega10 */
391389056e5SFelix Kuehling 	{ 0x6861, &vega10_device_info },	/* Vega10 */
392389056e5SFelix Kuehling 	{ 0x6862, &vega10_device_info },	/* Vega10 */
393389056e5SFelix Kuehling 	{ 0x6863, &vega10_device_info },	/* Vega10 */
394389056e5SFelix Kuehling 	{ 0x6864, &vega10_device_info },	/* Vega10 */
395389056e5SFelix Kuehling 	{ 0x6867, &vega10_device_info },	/* Vega10 */
396389056e5SFelix Kuehling 	{ 0x6868, &vega10_device_info },	/* Vega10 */
397756e16bfSAlex Deucher 	{ 0x6869, &vega10_device_info },	/* Vega10 */
398756e16bfSAlex Deucher 	{ 0x686A, &vega10_device_info },	/* Vega10 */
399756e16bfSAlex Deucher 	{ 0x686B, &vega10_device_info },	/* Vega10 */
400389056e5SFelix Kuehling 	{ 0x686C, &vega10_vf_device_info },	/* Vega10  vf*/
401756e16bfSAlex Deucher 	{ 0x686D, &vega10_device_info },	/* Vega10 */
402756e16bfSAlex Deucher 	{ 0x686E, &vega10_device_info },	/* Vega10 */
403756e16bfSAlex Deucher 	{ 0x686F, &vega10_device_info },	/* Vega10 */
404389056e5SFelix Kuehling 	{ 0x687F, &vega10_device_info },	/* Vega10 */
405846a44d7SGang Ba 	{ 0x69A0, &vega12_device_info },	/* Vega12 */
406846a44d7SGang Ba 	{ 0x69A1, &vega12_device_info },	/* Vega12 */
407846a44d7SGang Ba 	{ 0x69A2, &vega12_device_info },	/* Vega12 */
408846a44d7SGang Ba 	{ 0x69A3, &vega12_device_info },	/* Vega12 */
409846a44d7SGang Ba 	{ 0x69AF, &vega12_device_info },	/* Vega12 */
41022a3a294SShaoyun Liu 	{ 0x66a0, &vega20_device_info },	/* Vega20 */
41122a3a294SShaoyun Liu 	{ 0x66a1, &vega20_device_info },	/* Vega20 */
41222a3a294SShaoyun Liu 	{ 0x66a2, &vega20_device_info },	/* Vega20 */
41322a3a294SShaoyun Liu 	{ 0x66a3, &vega20_device_info },	/* Vega20 */
4149bd206f8SAlex Deucher 	{ 0x66a4, &vega20_device_info },	/* Vega20 */
41522a3a294SShaoyun Liu 	{ 0x66a7, &vega20_device_info },	/* Vega20 */
41622a3a294SShaoyun Liu 	{ 0x66af, &vega20_device_info }		/* Vega20 */
4174a488a7aSOded Gabbay };
4184a488a7aSOded Gabbay 
4196e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
4206e81090bSOded Gabbay 				unsigned int chunk_size);
4216e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
4226e81090bSOded Gabbay 
423b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
424b8935a7cSYong Zhao 
4254a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did)
4264a488a7aSOded Gabbay {
4274a488a7aSOded Gabbay 	size_t i;
4284a488a7aSOded Gabbay 
4294a488a7aSOded Gabbay 	for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
4304a488a7aSOded Gabbay 		if (supported_devices[i].did == did) {
43132fa8219SFelix Kuehling 			WARN_ON(!supported_devices[i].device_info);
4324a488a7aSOded Gabbay 			return supported_devices[i].device_info;
4334a488a7aSOded Gabbay 		}
4344a488a7aSOded Gabbay 	}
4354a488a7aSOded Gabbay 
4364ebc7182SYong Zhao 	dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
4374ebc7182SYong Zhao 		 did);
4384ebc7182SYong Zhao 
4394a488a7aSOded Gabbay 	return NULL;
4404a488a7aSOded Gabbay }
4414a488a7aSOded Gabbay 
442cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
443cea405b1SXihan Zhang 	struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
4444a488a7aSOded Gabbay {
4454a488a7aSOded Gabbay 	struct kfd_dev *kfd;
4466106dce9Swelu 	int ret;
4474a488a7aSOded Gabbay 	const struct kfd_device_info *device_info =
4484a488a7aSOded Gabbay 					lookup_device_info(pdev->device);
4494a488a7aSOded Gabbay 
4504ebc7182SYong Zhao 	if (!device_info) {
4514ebc7182SYong Zhao 		dev_err(kfd_device, "kgd2kfd_probe failed\n");
4524a488a7aSOded Gabbay 		return NULL;
4534ebc7182SYong Zhao 	}
4544a488a7aSOded Gabbay 
455d35f00d8SEric Huang 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
456d35f00d8SEric Huang 	if (!kfd)
457d35f00d8SEric Huang 		return NULL;
458d35f00d8SEric Huang 
4596106dce9Swelu 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
4606106dce9Swelu 	 * 32 and 64-bit requests are possible and must be
4616106dce9Swelu 	 * supported.
4623ee2d00cSFelix Kuehling 	 */
4636106dce9Swelu 	ret = pci_enable_atomic_ops_to_root(pdev,
4643ee2d00cSFelix Kuehling 			PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
4656106dce9Swelu 			PCI_EXP_DEVCAP2_ATOMIC_COMP64);
4666106dce9Swelu 	if (device_info->needs_pci_atomics && ret < 0) {
4673ee2d00cSFelix Kuehling 		dev_info(kfd_device,
4686106dce9Swelu 			 "skipped device %x:%x, PCI rejects atomics\n",
4693ee2d00cSFelix Kuehling 			 pdev->vendor, pdev->device);
470d35f00d8SEric Huang 		kfree(kfd);
4713ee2d00cSFelix Kuehling 		return NULL;
472d35f00d8SEric Huang 	} else if (!ret)
473d35f00d8SEric Huang 		kfd->pci_atomic_requested = true;
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;
480f756e631SHarish 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);
4983e76c239SFelix Kuehling 		} else {
4993e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
5003e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
5013e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
5023e76c239SFelix Kuehling 		}
5033e76c239SFelix Kuehling 
504373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
505373d7080SFelix Kuehling 	}
506373d7080SFelix Kuehling }
507373d7080SFelix Kuehling 
5084a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
5094a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
5104a488a7aSOded Gabbay {
51119f6d2a6SOded Gabbay 	unsigned int size;
51219f6d2a6SOded Gabbay 
5130da8b10eSAmber Lin 	kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
5145ade6c9cSFelix Kuehling 			KGD_ENGINE_MEC1);
5150da8b10eSAmber Lin 	kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
5165ade6c9cSFelix Kuehling 			KGD_ENGINE_SDMA1);
5174a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
5184a488a7aSOded Gabbay 
51944008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
52044008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
52144008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
52244008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
52344008d7aSYong Zhao 
524a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
525a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
526a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
527a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
528a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
529a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
530a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
531a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
532a99c6d4fSFelix Kuehling 	} else
533a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
534a99c6d4fSFelix Kuehling 
53519f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
536b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
53719f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
53819f6d2a6SOded Gabbay 
539e18e794eSOded Gabbay 	/*
540e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
541e18e794eSOded Gabbay 	 * There can be only 2 packets at once
542e18e794eSOded Gabbay 	 */
543507968ddSFelix Kuehling 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
544507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
545507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
546e18e794eSOded Gabbay 
547e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
548e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
549e18e794eSOded Gabbay 
550e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
55119f6d2a6SOded Gabbay 	size += 512 * 1024;
55219f6d2a6SOded Gabbay 
5537cd52c91SAmber Lin 	if (amdgpu_amdkfd_alloc_gtt_mem(
554cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
55515426dbbSYong Zhao 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
55615426dbbSYong Zhao 			false)) {
55779775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
55819f6d2a6SOded Gabbay 		goto out;
55919f6d2a6SOded Gabbay 	}
56019f6d2a6SOded Gabbay 
56179775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
562e18e794eSOded Gabbay 
56373a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
56473a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
56579775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
56673a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
56773a1da0bSOded Gabbay 	}
56873a1da0bSOded Gabbay 
569735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
570735df2baSFelix Kuehling 		dev_err(kfd_device,
571735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
572735df2baSFelix Kuehling 		goto kfd_doorbell_error;
573735df2baSFelix Kuehling 	}
57419f6d2a6SOded Gabbay 
5750c1690e3SShaoyun Liu 	if (kfd->kfd2kgd->get_hive_id)
5760c1690e3SShaoyun Liu 		kfd->hive_id = kfd->kfd2kgd->get_hive_id(kfd->kgd);
5770c1690e3SShaoyun Liu 
5784eacc26bSKent Russell 	if (kfd_topology_add_device(kfd)) {
57979775b62SKent Russell 		dev_err(kfd_device, "Error adding device to topology\n");
58019f6d2a6SOded Gabbay 		goto kfd_topology_add_device_error;
58119f6d2a6SOded Gabbay 	}
58219f6d2a6SOded Gabbay 
5832249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
58479775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
5852249d558SAndrew Lewycky 		goto kfd_interrupt_error;
5862249d558SAndrew Lewycky 	}
5872249d558SAndrew Lewycky 
58864c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
58964c7f8cfSBen Goz 	if (!kfd->dqm) {
59079775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
59164c7f8cfSBen Goz 		goto device_queue_manager_error;
59264c7f8cfSBen Goz 	}
59364c7f8cfSBen Goz 
59464d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
59564d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
59664d1c3a4SFelix Kuehling 		goto device_iommu_error;
59764c7f8cfSBen Goz 	}
59864c7f8cfSBen Goz 
599373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
600373d7080SFelix Kuehling 
601b8935a7cSYong Zhao 	if (kfd_resume(kfd))
602b8935a7cSYong Zhao 		goto kfd_resume_error;
603b8935a7cSYong Zhao 
604fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
605fbeb661bSYair Shachar 
6064a488a7aSOded Gabbay 	kfd->init_complete = true;
60779775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
6084a488a7aSOded Gabbay 		 kfd->pdev->device);
6094a488a7aSOded Gabbay 
61079775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
611d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
61264c7f8cfSBen Goz 
61319f6d2a6SOded Gabbay 	goto out;
61419f6d2a6SOded Gabbay 
615b8935a7cSYong Zhao kfd_resume_error:
61664d1c3a4SFelix Kuehling device_iommu_error:
61764c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
61864c7f8cfSBen Goz device_queue_manager_error:
6192249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
6202249d558SAndrew Lewycky kfd_interrupt_error:
621b17f068aSOded Gabbay 	kfd_topology_remove_device(kfd);
62219f6d2a6SOded Gabbay kfd_topology_add_device_error:
623735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
624735df2baSFelix Kuehling kfd_doorbell_error:
62573a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
62673a1da0bSOded Gabbay kfd_gtt_sa_init_error:
6277cd52c91SAmber Lin 	amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
62819f6d2a6SOded Gabbay 	dev_err(kfd_device,
62979775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
63019f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
63119f6d2a6SOded Gabbay out:
63219f6d2a6SOded Gabbay 	return kfd->init_complete;
6334a488a7aSOded Gabbay }
6344a488a7aSOded Gabbay 
6354a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
6364a488a7aSOded Gabbay {
637b17f068aSOded Gabbay 	if (kfd->init_complete) {
638b8935a7cSYong Zhao 		kgd2kfd_suspend(kfd);
63964c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
6402249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
64119f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
642735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
64373a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
6447cd52c91SAmber Lin 		amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
645b17f068aSOded Gabbay 	}
6465b5c4e40SEvgeny Pinchuk 
6474a488a7aSOded Gabbay 	kfree(kfd);
6484a488a7aSOded Gabbay }
6494a488a7aSOded Gabbay 
650e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
651e3b7a967SShaoyun Liu {
652e42051d2SShaoyun Liu 	if (!kfd->init_complete)
653e42051d2SShaoyun Liu 		return 0;
654e42051d2SShaoyun Liu 	kgd2kfd_suspend(kfd);
655e42051d2SShaoyun Liu 
656e42051d2SShaoyun Liu 	/* hold dqm->lock to prevent further execution*/
657e42051d2SShaoyun Liu 	dqm_lock(kfd->dqm);
658e42051d2SShaoyun Liu 
659e42051d2SShaoyun Liu 	kfd_signal_reset_event(kfd);
660e3b7a967SShaoyun Liu 	return 0;
661e3b7a967SShaoyun Liu }
662e3b7a967SShaoyun Liu 
663e42051d2SShaoyun Liu /*
664e42051d2SShaoyun Liu  * Fix me. KFD won't be able to resume existing process for now.
665e42051d2SShaoyun Liu  * We will keep all existing process in a evicted state and
666e42051d2SShaoyun Liu  * wait the process to be terminated.
667e42051d2SShaoyun Liu  */
668e42051d2SShaoyun Liu 
669e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
670e3b7a967SShaoyun Liu {
671e42051d2SShaoyun Liu 	int ret, count;
672e42051d2SShaoyun Liu 
673e42051d2SShaoyun Liu 	if (!kfd->init_complete)
674e3b7a967SShaoyun Liu 		return 0;
675e42051d2SShaoyun Liu 
676e42051d2SShaoyun Liu 	dqm_unlock(kfd->dqm);
677e42051d2SShaoyun Liu 
678e42051d2SShaoyun Liu 	ret = kfd_resume(kfd);
679e42051d2SShaoyun Liu 	if (ret)
680e42051d2SShaoyun Liu 		return ret;
681e42051d2SShaoyun Liu 	count = atomic_dec_return(&kfd_locked);
682e42051d2SShaoyun Liu 	WARN_ONCE(count != 0, "KFD reset ref. error");
6839b54d201SEric Huang 
6849b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
6859b54d201SEric Huang 
686e42051d2SShaoyun Liu 	return 0;
687e42051d2SShaoyun Liu }
688e42051d2SShaoyun Liu 
689e42051d2SShaoyun Liu bool kfd_is_locked(void)
690e42051d2SShaoyun Liu {
691e42051d2SShaoyun Liu 	return  (atomic_read(&kfd_locked) > 0);
692e3b7a967SShaoyun Liu }
693e3b7a967SShaoyun Liu 
6944a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd)
6954a488a7aSOded Gabbay {
696733fa1f7SYong Zhao 	if (!kfd->init_complete)
697733fa1f7SYong Zhao 		return;
698733fa1f7SYong Zhao 
69926103436SFelix Kuehling 	/* For first KFD device suspend all the KFD processes */
700e42051d2SShaoyun Liu 	if (atomic_inc_return(&kfd_locked) == 1)
70126103436SFelix Kuehling 		kfd_suspend_all_processes();
70226103436SFelix Kuehling 
70345c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
704733fa1f7SYong Zhao 
70564d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
7064a488a7aSOded Gabbay }
7074a488a7aSOded Gabbay 
7084a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd)
7094a488a7aSOded Gabbay {
71026103436SFelix Kuehling 	int ret, count;
71126103436SFelix Kuehling 
712b8935a7cSYong Zhao 	if (!kfd->init_complete)
713b8935a7cSYong Zhao 		return 0;
714b17f068aSOded Gabbay 
71526103436SFelix Kuehling 	ret = kfd_resume(kfd);
71626103436SFelix Kuehling 	if (ret)
71726103436SFelix Kuehling 		return ret;
718b17f068aSOded Gabbay 
719e42051d2SShaoyun Liu 	count = atomic_dec_return(&kfd_locked);
72026103436SFelix Kuehling 	WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
72126103436SFelix Kuehling 	if (count == 0)
72226103436SFelix Kuehling 		ret = kfd_resume_all_processes();
72326103436SFelix Kuehling 
72426103436SFelix Kuehling 	return ret;
7254ebc7182SYong Zhao }
7264ebc7182SYong Zhao 
727b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
728b8935a7cSYong Zhao {
729b8935a7cSYong Zhao 	int err = 0;
730b8935a7cSYong Zhao 
73164d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
73264d1c3a4SFelix Kuehling 	if (err) {
73364d1c3a4SFelix Kuehling 		dev_err(kfd_device,
73464d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
73564d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
73664d1c3a4SFelix Kuehling 		return err;
73764d1c3a4SFelix Kuehling 	}
738733fa1f7SYong Zhao 
739b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
740b8935a7cSYong Zhao 	if (err) {
741b8935a7cSYong Zhao 		dev_err(kfd_device,
742b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
743b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
744b8935a7cSYong Zhao 		goto dqm_start_error;
745b17f068aSOded Gabbay 	}
746b17f068aSOded Gabbay 
747b8935a7cSYong Zhao 	return err;
748b8935a7cSYong Zhao 
749b8935a7cSYong Zhao dqm_start_error:
75064d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
751b8935a7cSYong Zhao 	return err;
7524a488a7aSOded Gabbay }
7534a488a7aSOded Gabbay 
754b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
755b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
7564a488a7aSOded Gabbay {
75758e69886SLan Xiao 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
75858e69886SLan Xiao 	bool is_patched = false;
7592383a767SChristian König 	unsigned long flags;
76058e69886SLan Xiao 
7612249d558SAndrew Lewycky 	if (!kfd->init_complete)
7622249d558SAndrew Lewycky 		return;
7632249d558SAndrew Lewycky 
76458e69886SLan Xiao 	if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
76558e69886SLan Xiao 		dev_err_once(kfd_device, "Ring entry too small\n");
76658e69886SLan Xiao 		return;
76758e69886SLan Xiao 	}
76858e69886SLan Xiao 
7692383a767SChristian König 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
7702249d558SAndrew Lewycky 
7712249d558SAndrew Lewycky 	if (kfd->interrupts_active
77258e69886SLan Xiao 	    && interrupt_is_wanted(kfd, ih_ring_entry,
77358e69886SLan Xiao 				   patched_ihre, &is_patched)
77458e69886SLan Xiao 	    && enqueue_ih_ring_entry(kfd,
77558e69886SLan Xiao 				     is_patched ? patched_ihre : ih_ring_entry))
77648e876a2SAndres Rodriguez 		queue_work(kfd->ih_wq, &kfd->interrupt_work);
7772249d558SAndrew Lewycky 
7782383a767SChristian König 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
7794a488a7aSOded Gabbay }
7806e81090bSOded Gabbay 
7816b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
7826b95e797SFelix Kuehling {
7836b95e797SFelix Kuehling 	struct kfd_process *p;
7846b95e797SFelix Kuehling 	int r;
7856b95e797SFelix Kuehling 
7866b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
7876b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
7886b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
7896b95e797SFelix Kuehling 	 */
7906b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
7916b95e797SFelix Kuehling 	if (!p)
7926b95e797SFelix Kuehling 		return -ESRCH;
7936b95e797SFelix Kuehling 
7946b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
7956b95e797SFelix Kuehling 
7966b95e797SFelix Kuehling 	kfd_unref_process(p);
7976b95e797SFelix Kuehling 	return r;
7986b95e797SFelix Kuehling }
7996b95e797SFelix Kuehling 
8006b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
8016b95e797SFelix Kuehling {
8026b95e797SFelix Kuehling 	struct kfd_process *p;
8036b95e797SFelix Kuehling 	int r;
8046b95e797SFelix Kuehling 
8056b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
8066b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
8076b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
8086b95e797SFelix Kuehling 	 */
8096b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
8106b95e797SFelix Kuehling 	if (!p)
8116b95e797SFelix Kuehling 		return -ESRCH;
8126b95e797SFelix Kuehling 
8136b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
8146b95e797SFelix Kuehling 
8156b95e797SFelix Kuehling 	kfd_unref_process(p);
8166b95e797SFelix Kuehling 	return r;
8176b95e797SFelix Kuehling }
8186b95e797SFelix Kuehling 
81926103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
82026103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
82126103436SFelix Kuehling  *   process.
82226103436SFelix Kuehling  *
82326103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
82426103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
82526103436SFelix Kuehling  *
82626103436SFelix Kuehling  */
82726103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
82826103436SFelix Kuehling 					       struct dma_fence *fence)
82926103436SFelix Kuehling {
83026103436SFelix Kuehling 	struct kfd_process *p;
83126103436SFelix Kuehling 	unsigned long active_time;
83226103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
83326103436SFelix Kuehling 
83426103436SFelix Kuehling 	if (!fence)
83526103436SFelix Kuehling 		return -EINVAL;
83626103436SFelix Kuehling 
83726103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
83826103436SFelix Kuehling 		return 0;
83926103436SFelix Kuehling 
84026103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
84126103436SFelix Kuehling 	if (!p)
84226103436SFelix Kuehling 		return -ENODEV;
84326103436SFelix Kuehling 
84426103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
84526103436SFelix Kuehling 		goto out;
84626103436SFelix Kuehling 
84726103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
84826103436SFelix Kuehling 
84926103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
85026103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
85126103436SFelix Kuehling 	 */
85226103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
85326103436SFelix Kuehling 	if (delay_jiffies > active_time)
85426103436SFelix Kuehling 		delay_jiffies -= active_time;
85526103436SFelix Kuehling 	else
85626103436SFelix Kuehling 		delay_jiffies = 0;
85726103436SFelix Kuehling 
85826103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
85926103436SFelix Kuehling 	 * to kfd_evict_bo_worker
86026103436SFelix Kuehling 	 */
86126103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
86226103436SFelix Kuehling out:
86326103436SFelix Kuehling 	kfd_unref_process(p);
86426103436SFelix Kuehling 	return 0;
86526103436SFelix Kuehling }
86626103436SFelix Kuehling 
8676e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
8686e81090bSOded Gabbay 				unsigned int chunk_size)
8696e81090bSOded Gabbay {
8708625ff9cSFelix Kuehling 	unsigned int num_of_longs;
8716e81090bSOded Gabbay 
87232fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
87332fa8219SFelix Kuehling 		return -EINVAL;
87432fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
87532fa8219SFelix Kuehling 		return -EINVAL;
87632fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
87732fa8219SFelix Kuehling 		return -EINVAL;
8786e81090bSOded Gabbay 
8796e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
8806e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
8816e81090bSOded Gabbay 
8828625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
8838625ff9cSFelix Kuehling 		BITS_PER_LONG;
8846e81090bSOded Gabbay 
8858625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
8866e81090bSOded Gabbay 
8876e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
8886e81090bSOded Gabbay 		return -ENOMEM;
8896e81090bSOded Gabbay 
89079775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
8916e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
8926e81090bSOded Gabbay 
8936e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
8946e81090bSOded Gabbay 
8956e81090bSOded Gabbay 	return 0;
8966e81090bSOded Gabbay 
8976e81090bSOded Gabbay }
8986e81090bSOded Gabbay 
8996e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
9006e81090bSOded Gabbay {
9016e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
9026e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
9036e81090bSOded Gabbay }
9046e81090bSOded Gabbay 
9056e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
9066e81090bSOded Gabbay 						unsigned int bit_num,
9076e81090bSOded Gabbay 						unsigned int chunk_size)
9086e81090bSOded Gabbay {
9096e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
9106e81090bSOded Gabbay }
9116e81090bSOded Gabbay 
9126e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
9136e81090bSOded Gabbay 						unsigned int bit_num,
9146e81090bSOded Gabbay 						unsigned int chunk_size)
9156e81090bSOded Gabbay {
9166e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
9176e81090bSOded Gabbay }
9186e81090bSOded Gabbay 
9196e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
9206e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
9216e81090bSOded Gabbay {
9226e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
9236e81090bSOded Gabbay 
9246e81090bSOded Gabbay 	if (size == 0)
9256e81090bSOded Gabbay 		return -EINVAL;
9266e81090bSOded Gabbay 
9276e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
9286e81090bSOded Gabbay 		return -ENOMEM;
9296e81090bSOded Gabbay 
9301cd106ecSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
9311cd106ecSFelix Kuehling 	if (!(*mem_obj))
9326e81090bSOded Gabbay 		return -ENOMEM;
9336e81090bSOded Gabbay 
93479775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
9356e81090bSOded Gabbay 
9366e81090bSOded Gabbay 	start_search = 0;
9376e81090bSOded Gabbay 
9386e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
9396e81090bSOded Gabbay 
9406e81090bSOded Gabbay kfd_gtt_restart_search:
9416e81090bSOded Gabbay 	/* Find the first chunk that is free */
9426e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
9436e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
9446e81090bSOded Gabbay 					start_search);
9456e81090bSOded Gabbay 
94679775b62SKent Russell 	pr_debug("Found = %d\n", found);
9476e81090bSOded Gabbay 
9486e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
9496e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
9506e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
9516e81090bSOded Gabbay 
9526e81090bSOded Gabbay 	/* Update fields of mem_obj */
9536e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
9546e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
9556e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
9566e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
9576e81090bSOded Gabbay 					found,
9586e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
9596e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
9606e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
9616e81090bSOded Gabbay 					found,
9626e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
9636e81090bSOded Gabbay 
96479775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
9656e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
9666e81090bSOded Gabbay 
9676e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
9686e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
96979775b62SKent Russell 		pr_debug("Single bit\n");
9706e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
9716e81090bSOded Gabbay 		goto kfd_gtt_out;
9726e81090bSOded Gabbay 	}
9736e81090bSOded Gabbay 
9746e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
9756e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
9766e81090bSOded Gabbay 	do {
9776e81090bSOded Gabbay 		(*mem_obj)->range_end =
9786e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
9796e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
9806e81090bSOded Gabbay 		/*
9816e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
9826e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
9836e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
9846e81090bSOded Gabbay 		 */
9856e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
9866e81090bSOded Gabbay 			start_search = found;
9876e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
9886e81090bSOded Gabbay 		}
9896e81090bSOded Gabbay 
9906e81090bSOded Gabbay 		/*
9916e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
9926e81090bSOded Gabbay 		 */
9936e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
9946e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
9956e81090bSOded Gabbay 
9966e81090bSOded Gabbay 		/* Check if we don't need another chunk */
9976e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
9986e81090bSOded Gabbay 			cur_size = 0;
9996e81090bSOded Gabbay 		else
10006e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
10016e81090bSOded Gabbay 
10026e81090bSOded Gabbay 	} while (cur_size > 0);
10036e81090bSOded Gabbay 
100479775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
10056e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
10066e81090bSOded Gabbay 
10076e81090bSOded Gabbay 	/* Mark the chunks as allocated */
10086e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
10096e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
10106e81090bSOded Gabbay 		found++)
10116e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
10126e81090bSOded Gabbay 
10136e81090bSOded Gabbay kfd_gtt_out:
10146e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
10156e81090bSOded Gabbay 	return 0;
10166e81090bSOded Gabbay 
10176e81090bSOded Gabbay kfd_gtt_no_free_chunk:
101879775b62SKent Russell 	pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
10196e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
10206e81090bSOded Gabbay 	kfree(mem_obj);
10216e81090bSOded Gabbay 	return -ENOMEM;
10226e81090bSOded Gabbay }
10236e81090bSOded Gabbay 
10246e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
10256e81090bSOded Gabbay {
10266e81090bSOded Gabbay 	unsigned int bit;
10276e81090bSOded Gabbay 
10289216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
10299216ed29SOded Gabbay 	if (!mem_obj)
10309216ed29SOded Gabbay 		return 0;
10316e81090bSOded Gabbay 
103279775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
10336e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
10346e81090bSOded Gabbay 
10356e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
10366e81090bSOded Gabbay 
10376e81090bSOded Gabbay 	/* Mark the chunks as free */
10386e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
10396e81090bSOded Gabbay 		bit <= mem_obj->range_end;
10406e81090bSOded Gabbay 		bit++)
10416e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
10426e81090bSOded Gabbay 
10436e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
10446e81090bSOded Gabbay 
10456e81090bSOded Gabbay 	kfree(mem_obj);
10466e81090bSOded Gabbay 	return 0;
10476e81090bSOded Gabbay }
1048a29ec470SShaoyun Liu 
10499b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
10509b54d201SEric Huang {
10519b54d201SEric Huang 	if (kfd)
10529b54d201SEric Huang 		atomic_inc(&kfd->sram_ecc_flag);
10539b54d201SEric Huang }
10549b54d201SEric Huang 
1055f756e631SHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *kfd)
1056f756e631SHarish Kasiviswanathan {
1057f756e631SHarish Kasiviswanathan 	if (atomic_inc_return(&kfd->compute_profile) == 1)
1058f756e631SHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, false);
1059f756e631SHarish Kasiviswanathan }
1060f756e631SHarish Kasiviswanathan 
1061f756e631SHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *kfd)
1062f756e631SHarish Kasiviswanathan {
1063f756e631SHarish Kasiviswanathan 	int count = atomic_dec_return(&kfd->compute_profile);
1064f756e631SHarish Kasiviswanathan 
1065f756e631SHarish Kasiviswanathan 	if (count == 0)
1066f756e631SHarish Kasiviswanathan 		amdgpu_amdkfd_set_compute_idle(kfd->kgd, true);
1067f756e631SHarish Kasiviswanathan 	WARN_ONCE(count < 0, "Compute profile ref. count error");
1068f756e631SHarish Kasiviswanathan }
1069f756e631SHarish Kasiviswanathan 
1070a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
1071a29ec470SShaoyun Liu 
1072a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
1073a29ec470SShaoyun Liu  * which will trigger a GPU reset and bring the HWS back to normal state
1074a29ec470SShaoyun Liu  */
1075a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1076a29ec470SShaoyun Liu {
1077a29ec470SShaoyun Liu 	int r = 0;
1078a29ec470SShaoyun Liu 
1079a29ec470SShaoyun Liu 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1080a29ec470SShaoyun Liu 		pr_err("HWS is not enabled");
1081a29ec470SShaoyun Liu 		return -EINVAL;
1082a29ec470SShaoyun Liu 	}
1083a29ec470SShaoyun Liu 
1084a29ec470SShaoyun Liu 	r = pm_debugfs_hang_hws(&dev->dqm->packets);
1085a29ec470SShaoyun Liu 	if (!r)
1086a29ec470SShaoyun Liu 		r = dqm_debugfs_execute_queues(dev->dqm);
1087a29ec470SShaoyun Liu 
1088a29ec470SShaoyun Liu 	return r;
1089a29ec470SShaoyun Liu }
1090a29ec470SShaoyun Liu 
1091a29ec470SShaoyun Liu #endif
1092