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,
57d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
580da7558cSBen Goz };
590da7558cSBen Goz 
600da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
610da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
624a488a7aSOded Gabbay 	.max_pasid_bits = 16,
63eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
64eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
65ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
66b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
67eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
68f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
69373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
70373d7080SFelix Kuehling 	.supports_cwsr = true,
7164d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
723ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
7398bb9222SYong Zhao 	.num_sdma_engines = 2,
74d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
754a488a7aSOded Gabbay };
764d663df6SYong Zhao 
774d663df6SYong Zhao static const struct kfd_device_info raven_device_info = {
784d663df6SYong Zhao 	.asic_family = CHIP_RAVEN,
794d663df6SYong Zhao 	.max_pasid_bits = 16,
804d663df6SYong Zhao 	.max_no_of_hqd  = 24,
814d663df6SYong Zhao 	.doorbell_size  = 8,
824d663df6SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
834d663df6SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
844d663df6SYong Zhao 	.num_of_watch_points = 4,
854d663df6SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
864d663df6SYong Zhao 	.supports_cwsr = true,
874d663df6SYong Zhao 	.needs_iommu_device = true,
884d663df6SYong Zhao 	.needs_pci_atomics = true,
894d663df6SYong Zhao 	.num_sdma_engines = 1,
90d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
914d663df6SYong Zhao };
9264d1c3a4SFelix Kuehling #endif
934a488a7aSOded Gabbay 
94a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
95a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
96a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
97a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
98a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
99ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
100a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
101a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
102a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
103a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
104a3084e6cSFelix Kuehling 	.supports_cwsr = false,
10564d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
106a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
10798bb9222SYong Zhao 	.num_sdma_engines = 2,
108d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
109a3084e6cSFelix Kuehling };
110a3084e6cSFelix Kuehling 
111a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
112a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
113a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
114a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
115ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
116a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
117a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
118a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
119a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
120a3084e6cSFelix Kuehling 	.supports_cwsr = false,
12164d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
122a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
12398bb9222SYong Zhao 	.num_sdma_engines = 2,
124d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
125a3084e6cSFelix Kuehling };
126a3084e6cSFelix Kuehling 
127a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
128a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
129a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
130a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
131ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
132a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
133a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
134a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
135a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
136a3084e6cSFelix Kuehling 	.supports_cwsr = true,
13764d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
138a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
13998bb9222SYong Zhao 	.num_sdma_engines = 2,
140d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
141a3084e6cSFelix Kuehling };
142a3084e6cSFelix Kuehling 
143a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
144a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
145a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
146a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
147ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
148a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
149a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
150a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
151a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
152a3084e6cSFelix Kuehling 	.supports_cwsr = true,
15364d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
154a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
15598bb9222SYong Zhao 	.num_sdma_engines = 2,
156d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
157a3084e6cSFelix Kuehling };
158a3084e6cSFelix Kuehling 
159a3084e6cSFelix Kuehling 
160a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
161a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
162a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
163a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
164ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
165a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
166a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
167a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
168a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
169a3084e6cSFelix Kuehling 	.supports_cwsr = true,
17064d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
171a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
17298bb9222SYong Zhao 	.num_sdma_engines = 2,
173d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
174a3084e6cSFelix Kuehling };
175a3084e6cSFelix Kuehling 
176a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = {
177a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
178a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
179a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
180ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
181a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
182a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
183a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
184a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
185a3084e6cSFelix Kuehling 	.supports_cwsr = true,
18664d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
187a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
18898bb9222SYong Zhao 	.num_sdma_engines = 2,
189d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
190a3084e6cSFelix Kuehling };
191a3084e6cSFelix Kuehling 
192a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = {
193a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
194a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
195a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
196ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
197a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
198a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
199a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
200a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
201a3084e6cSFelix Kuehling 	.supports_cwsr = true,
20264d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
203a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
20498bb9222SYong Zhao 	.num_sdma_engines = 2,
205d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
206a3084e6cSFelix Kuehling };
207a3084e6cSFelix Kuehling 
208846a44d7SGang Ba static const struct kfd_device_info polaris12_device_info = {
209846a44d7SGang Ba 	.asic_family = CHIP_POLARIS12,
210846a44d7SGang Ba 	.max_pasid_bits = 16,
211846a44d7SGang Ba 	.max_no_of_hqd  = 24,
212846a44d7SGang Ba 	.doorbell_size  = 4,
213846a44d7SGang Ba 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
214846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_cik,
215846a44d7SGang Ba 	.num_of_watch_points = 4,
216846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
217846a44d7SGang Ba 	.supports_cwsr = true,
218846a44d7SGang Ba 	.needs_iommu_device = false,
219846a44d7SGang Ba 	.needs_pci_atomics = true,
220846a44d7SGang Ba 	.num_sdma_engines = 2,
221846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
222846a44d7SGang Ba };
223846a44d7SGang Ba 
224389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = {
225389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
226389056e5SFelix Kuehling 	.max_pasid_bits = 16,
227389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
228389056e5SFelix Kuehling 	.doorbell_size  = 8,
229389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
230389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
231389056e5SFelix Kuehling 	.num_of_watch_points = 4,
232389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
233389056e5SFelix Kuehling 	.supports_cwsr = true,
234389056e5SFelix Kuehling 	.needs_iommu_device = false,
235389056e5SFelix Kuehling 	.needs_pci_atomics = false,
23698bb9222SYong Zhao 	.num_sdma_engines = 2,
237d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
238389056e5SFelix Kuehling };
239389056e5SFelix Kuehling 
240389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_device_info = {
241389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
242389056e5SFelix Kuehling 	.max_pasid_bits = 16,
243389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
244389056e5SFelix Kuehling 	.doorbell_size  = 8,
245389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
246389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
247389056e5SFelix Kuehling 	.num_of_watch_points = 4,
248389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
249389056e5SFelix Kuehling 	.supports_cwsr = true,
250389056e5SFelix Kuehling 	.needs_iommu_device = false,
251389056e5SFelix Kuehling 	.needs_pci_atomics = false,
25298bb9222SYong Zhao 	.num_sdma_engines = 2,
253d5094189SShaoyun Liu 	.num_sdma_queues_per_engine = 2,
254389056e5SFelix Kuehling };
255389056e5SFelix Kuehling 
256846a44d7SGang Ba static const struct kfd_device_info vega12_device_info = {
257846a44d7SGang Ba 	.asic_family = CHIP_VEGA12,
258846a44d7SGang Ba 	.max_pasid_bits = 16,
259846a44d7SGang Ba 	.max_no_of_hqd  = 24,
260846a44d7SGang Ba 	.doorbell_size  = 8,
261846a44d7SGang Ba 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
262846a44d7SGang Ba 	.event_interrupt_class = &event_interrupt_class_v9,
263846a44d7SGang Ba 	.num_of_watch_points = 4,
264846a44d7SGang Ba 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
265846a44d7SGang Ba 	.supports_cwsr = true,
266846a44d7SGang Ba 	.needs_iommu_device = false,
267846a44d7SGang Ba 	.needs_pci_atomics = false,
268846a44d7SGang Ba 	.num_sdma_engines = 2,
269846a44d7SGang Ba 	.num_sdma_queues_per_engine = 2,
270846a44d7SGang Ba };
271846a44d7SGang Ba 
27222a3a294SShaoyun Liu static const struct kfd_device_info vega20_device_info = {
27322a3a294SShaoyun Liu 	.asic_family = CHIP_VEGA20,
27422a3a294SShaoyun Liu 	.max_pasid_bits = 16,
27522a3a294SShaoyun Liu 	.max_no_of_hqd	= 24,
27622a3a294SShaoyun Liu 	.doorbell_size	= 8,
27722a3a294SShaoyun Liu 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
27822a3a294SShaoyun Liu 	.event_interrupt_class = &event_interrupt_class_v9,
27922a3a294SShaoyun Liu 	.num_of_watch_points = 4,
28022a3a294SShaoyun Liu 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
28122a3a294SShaoyun Liu 	.supports_cwsr = true,
28222a3a294SShaoyun Liu 	.needs_iommu_device = false,
283006a0b3dSShaoyun Liu 	.needs_pci_atomics = false,
28422a3a294SShaoyun Liu 	.num_sdma_engines = 2,
28522a3a294SShaoyun Liu 	.num_sdma_queues_per_engine = 8,
28622a3a294SShaoyun Liu };
28722a3a294SShaoyun Liu 
2884a488a7aSOded Gabbay struct kfd_deviceid {
2894a488a7aSOded Gabbay 	unsigned short did;
2904a488a7aSOded Gabbay 	const struct kfd_device_info *device_info;
2914a488a7aSOded Gabbay };
2924a488a7aSOded Gabbay 
2934a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = {
29464d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
2954a488a7aSOded Gabbay 	{ 0x1304, &kaveri_device_info },	/* Kaveri */
2964a488a7aSOded Gabbay 	{ 0x1305, &kaveri_device_info },	/* Kaveri */
2974a488a7aSOded Gabbay 	{ 0x1306, &kaveri_device_info },	/* Kaveri */
2984a488a7aSOded Gabbay 	{ 0x1307, &kaveri_device_info },	/* Kaveri */
2994a488a7aSOded Gabbay 	{ 0x1309, &kaveri_device_info },	/* Kaveri */
3004a488a7aSOded Gabbay 	{ 0x130A, &kaveri_device_info },	/* Kaveri */
3014a488a7aSOded Gabbay 	{ 0x130B, &kaveri_device_info },	/* Kaveri */
3024a488a7aSOded Gabbay 	{ 0x130C, &kaveri_device_info },	/* Kaveri */
3034a488a7aSOded Gabbay 	{ 0x130D, &kaveri_device_info },	/* Kaveri */
3044a488a7aSOded Gabbay 	{ 0x130E, &kaveri_device_info },	/* Kaveri */
3054a488a7aSOded Gabbay 	{ 0x130F, &kaveri_device_info },	/* Kaveri */
3064a488a7aSOded Gabbay 	{ 0x1310, &kaveri_device_info },	/* Kaveri */
3074a488a7aSOded Gabbay 	{ 0x1311, &kaveri_device_info },	/* Kaveri */
3084a488a7aSOded Gabbay 	{ 0x1312, &kaveri_device_info },	/* Kaveri */
3094a488a7aSOded Gabbay 	{ 0x1313, &kaveri_device_info },	/* Kaveri */
3104a488a7aSOded Gabbay 	{ 0x1315, &kaveri_device_info },	/* Kaveri */
3114a488a7aSOded Gabbay 	{ 0x1316, &kaveri_device_info },	/* Kaveri */
3124a488a7aSOded Gabbay 	{ 0x1317, &kaveri_device_info },	/* Kaveri */
3134a488a7aSOded Gabbay 	{ 0x1318, &kaveri_device_info },	/* Kaveri */
3144a488a7aSOded Gabbay 	{ 0x131B, &kaveri_device_info },	/* Kaveri */
3154a488a7aSOded Gabbay 	{ 0x131C, &kaveri_device_info },	/* Kaveri */
316123576d1SBen Goz 	{ 0x131D, &kaveri_device_info },	/* Kaveri */
317123576d1SBen Goz 	{ 0x9870, &carrizo_device_info },	/* Carrizo */
318123576d1SBen Goz 	{ 0x9874, &carrizo_device_info },	/* Carrizo */
319123576d1SBen Goz 	{ 0x9875, &carrizo_device_info },	/* Carrizo */
320123576d1SBen Goz 	{ 0x9876, &carrizo_device_info },	/* Carrizo */
321a3084e6cSFelix Kuehling 	{ 0x9877, &carrizo_device_info },	/* Carrizo */
3224d663df6SYong Zhao 	{ 0x15DD, &raven_device_info },		/* Raven */
32364d1c3a4SFelix Kuehling #endif
324a3084e6cSFelix Kuehling 	{ 0x67A0, &hawaii_device_info },	/* Hawaii */
325a3084e6cSFelix Kuehling 	{ 0x67A1, &hawaii_device_info },	/* Hawaii */
326a3084e6cSFelix Kuehling 	{ 0x67A2, &hawaii_device_info },	/* Hawaii */
327a3084e6cSFelix Kuehling 	{ 0x67A8, &hawaii_device_info },	/* Hawaii */
328a3084e6cSFelix Kuehling 	{ 0x67A9, &hawaii_device_info },	/* Hawaii */
329a3084e6cSFelix Kuehling 	{ 0x67AA, &hawaii_device_info },	/* Hawaii */
330a3084e6cSFelix Kuehling 	{ 0x67B0, &hawaii_device_info },	/* Hawaii */
331a3084e6cSFelix Kuehling 	{ 0x67B1, &hawaii_device_info },	/* Hawaii */
332a3084e6cSFelix Kuehling 	{ 0x67B8, &hawaii_device_info },	/* Hawaii */
333a3084e6cSFelix Kuehling 	{ 0x67B9, &hawaii_device_info },	/* Hawaii */
334a3084e6cSFelix Kuehling 	{ 0x67BA, &hawaii_device_info },	/* Hawaii */
335a3084e6cSFelix Kuehling 	{ 0x67BE, &hawaii_device_info },	/* Hawaii */
336a3084e6cSFelix Kuehling 	{ 0x6920, &tonga_device_info },		/* Tonga */
337a3084e6cSFelix Kuehling 	{ 0x6921, &tonga_device_info },		/* Tonga */
338a3084e6cSFelix Kuehling 	{ 0x6928, &tonga_device_info },		/* Tonga */
339a3084e6cSFelix Kuehling 	{ 0x6929, &tonga_device_info },		/* Tonga */
340a3084e6cSFelix Kuehling 	{ 0x692B, &tonga_device_info },		/* Tonga */
341a3084e6cSFelix Kuehling 	{ 0x6938, &tonga_device_info },		/* Tonga */
342a3084e6cSFelix Kuehling 	{ 0x6939, &tonga_device_info },		/* Tonga */
343a3084e6cSFelix Kuehling 	{ 0x7300, &fiji_device_info },		/* Fiji */
344a3084e6cSFelix Kuehling 	{ 0x730F, &fiji_vf_device_info },	/* Fiji vf*/
345a3084e6cSFelix Kuehling 	{ 0x67C0, &polaris10_device_info },	/* Polaris10 */
346a3084e6cSFelix Kuehling 	{ 0x67C1, &polaris10_device_info },	/* Polaris10 */
347a3084e6cSFelix Kuehling 	{ 0x67C2, &polaris10_device_info },	/* Polaris10 */
348a3084e6cSFelix Kuehling 	{ 0x67C4, &polaris10_device_info },	/* Polaris10 */
349a3084e6cSFelix Kuehling 	{ 0x67C7, &polaris10_device_info },	/* Polaris10 */
350a3084e6cSFelix Kuehling 	{ 0x67C8, &polaris10_device_info },	/* Polaris10 */
351a3084e6cSFelix Kuehling 	{ 0x67C9, &polaris10_device_info },	/* Polaris10 */
352a3084e6cSFelix Kuehling 	{ 0x67CA, &polaris10_device_info },	/* Polaris10 */
353a3084e6cSFelix Kuehling 	{ 0x67CC, &polaris10_device_info },	/* Polaris10 */
354a3084e6cSFelix Kuehling 	{ 0x67CF, &polaris10_device_info },	/* Polaris10 */
355a3084e6cSFelix Kuehling 	{ 0x67D0, &polaris10_vf_device_info },	/* Polaris10 vf*/
356a3084e6cSFelix Kuehling 	{ 0x67DF, &polaris10_device_info },	/* Polaris10 */
357a3084e6cSFelix Kuehling 	{ 0x67E0, &polaris11_device_info },	/* Polaris11 */
358a3084e6cSFelix Kuehling 	{ 0x67E1, &polaris11_device_info },	/* Polaris11 */
359a3084e6cSFelix Kuehling 	{ 0x67E3, &polaris11_device_info },	/* Polaris11 */
360a3084e6cSFelix Kuehling 	{ 0x67E7, &polaris11_device_info },	/* Polaris11 */
361a3084e6cSFelix Kuehling 	{ 0x67E8, &polaris11_device_info },	/* Polaris11 */
362a3084e6cSFelix Kuehling 	{ 0x67E9, &polaris11_device_info },	/* Polaris11 */
363a3084e6cSFelix Kuehling 	{ 0x67EB, &polaris11_device_info },	/* Polaris11 */
364a3084e6cSFelix Kuehling 	{ 0x67EF, &polaris11_device_info },	/* Polaris11 */
365a3084e6cSFelix Kuehling 	{ 0x67FF, &polaris11_device_info },	/* Polaris11 */
366846a44d7SGang Ba 	{ 0x6980, &polaris12_device_info },	/* Polaris12 */
367846a44d7SGang Ba 	{ 0x6981, &polaris12_device_info },	/* Polaris12 */
368846a44d7SGang Ba 	{ 0x6985, &polaris12_device_info },	/* Polaris12 */
369846a44d7SGang Ba 	{ 0x6986, &polaris12_device_info },	/* Polaris12 */
370846a44d7SGang Ba 	{ 0x6987, &polaris12_device_info },	/* Polaris12 */
371846a44d7SGang Ba 	{ 0x6995, &polaris12_device_info },	/* Polaris12 */
372846a44d7SGang Ba 	{ 0x6997, &polaris12_device_info },	/* Polaris12 */
373846a44d7SGang Ba 	{ 0x699F, &polaris12_device_info },	/* Polaris12 */
374389056e5SFelix Kuehling 	{ 0x6860, &vega10_device_info },	/* Vega10 */
375389056e5SFelix Kuehling 	{ 0x6861, &vega10_device_info },	/* Vega10 */
376389056e5SFelix Kuehling 	{ 0x6862, &vega10_device_info },	/* Vega10 */
377389056e5SFelix Kuehling 	{ 0x6863, &vega10_device_info },	/* Vega10 */
378389056e5SFelix Kuehling 	{ 0x6864, &vega10_device_info },	/* Vega10 */
379389056e5SFelix Kuehling 	{ 0x6867, &vega10_device_info },	/* Vega10 */
380389056e5SFelix Kuehling 	{ 0x6868, &vega10_device_info },	/* Vega10 */
381756e16bfSAlex Deucher 	{ 0x6869, &vega10_device_info },	/* Vega10 */
382756e16bfSAlex Deucher 	{ 0x686A, &vega10_device_info },	/* Vega10 */
383756e16bfSAlex Deucher 	{ 0x686B, &vega10_device_info },	/* Vega10 */
384389056e5SFelix Kuehling 	{ 0x686C, &vega10_vf_device_info },	/* Vega10  vf*/
385756e16bfSAlex Deucher 	{ 0x686D, &vega10_device_info },	/* Vega10 */
386756e16bfSAlex Deucher 	{ 0x686E, &vega10_device_info },	/* Vega10 */
387756e16bfSAlex Deucher 	{ 0x686F, &vega10_device_info },	/* Vega10 */
388389056e5SFelix Kuehling 	{ 0x687F, &vega10_device_info },	/* Vega10 */
389846a44d7SGang Ba 	{ 0x69A0, &vega12_device_info },	/* Vega12 */
390846a44d7SGang Ba 	{ 0x69A1, &vega12_device_info },	/* Vega12 */
391846a44d7SGang Ba 	{ 0x69A2, &vega12_device_info },	/* Vega12 */
392846a44d7SGang Ba 	{ 0x69A3, &vega12_device_info },	/* Vega12 */
393846a44d7SGang Ba 	{ 0x69AF, &vega12_device_info },	/* Vega12 */
39422a3a294SShaoyun Liu 	{ 0x66a0, &vega20_device_info },	/* Vega20 */
39522a3a294SShaoyun Liu 	{ 0x66a1, &vega20_device_info },	/* Vega20 */
39622a3a294SShaoyun Liu 	{ 0x66a2, &vega20_device_info },	/* Vega20 */
39722a3a294SShaoyun Liu 	{ 0x66a3, &vega20_device_info },	/* Vega20 */
3989bd206f8SAlex Deucher 	{ 0x66a4, &vega20_device_info },	/* Vega20 */
39922a3a294SShaoyun Liu 	{ 0x66a7, &vega20_device_info },	/* Vega20 */
40022a3a294SShaoyun Liu 	{ 0x66af, &vega20_device_info }		/* Vega20 */
4014a488a7aSOded Gabbay };
4024a488a7aSOded Gabbay 
4036e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
4046e81090bSOded Gabbay 				unsigned int chunk_size);
4056e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
4066e81090bSOded Gabbay 
407b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
408b8935a7cSYong Zhao 
4094a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did)
4104a488a7aSOded Gabbay {
4114a488a7aSOded Gabbay 	size_t i;
4124a488a7aSOded Gabbay 
4134a488a7aSOded Gabbay 	for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
4144a488a7aSOded Gabbay 		if (supported_devices[i].did == did) {
41532fa8219SFelix Kuehling 			WARN_ON(!supported_devices[i].device_info);
4164a488a7aSOded Gabbay 			return supported_devices[i].device_info;
4174a488a7aSOded Gabbay 		}
4184a488a7aSOded Gabbay 	}
4194a488a7aSOded Gabbay 
4204ebc7182SYong Zhao 	dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
4214ebc7182SYong Zhao 		 did);
4224ebc7182SYong Zhao 
4234a488a7aSOded Gabbay 	return NULL;
4244a488a7aSOded Gabbay }
4254a488a7aSOded Gabbay 
426cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
427cea405b1SXihan Zhang 	struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
4284a488a7aSOded Gabbay {
4294a488a7aSOded Gabbay 	struct kfd_dev *kfd;
4306106dce9Swelu 	int ret;
4314a488a7aSOded Gabbay 	const struct kfd_device_info *device_info =
4324a488a7aSOded Gabbay 					lookup_device_info(pdev->device);
4334a488a7aSOded Gabbay 
4344ebc7182SYong Zhao 	if (!device_info) {
4354ebc7182SYong Zhao 		dev_err(kfd_device, "kgd2kfd_probe failed\n");
4364a488a7aSOded Gabbay 		return NULL;
4374ebc7182SYong Zhao 	}
4384a488a7aSOded Gabbay 
439d35f00d8SEric Huang 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
440d35f00d8SEric Huang 	if (!kfd)
441d35f00d8SEric Huang 		return NULL;
442d35f00d8SEric Huang 
4436106dce9Swelu 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
4446106dce9Swelu 	 * 32 and 64-bit requests are possible and must be
4456106dce9Swelu 	 * supported.
4463ee2d00cSFelix Kuehling 	 */
4476106dce9Swelu 	ret = pci_enable_atomic_ops_to_root(pdev,
4483ee2d00cSFelix Kuehling 			PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
4496106dce9Swelu 			PCI_EXP_DEVCAP2_ATOMIC_COMP64);
4506106dce9Swelu 	if (device_info->needs_pci_atomics && ret < 0) {
4513ee2d00cSFelix Kuehling 		dev_info(kfd_device,
4526106dce9Swelu 			 "skipped device %x:%x, PCI rejects atomics\n",
4533ee2d00cSFelix Kuehling 			 pdev->vendor, pdev->device);
454d35f00d8SEric Huang 		kfree(kfd);
4553ee2d00cSFelix Kuehling 		return NULL;
456d35f00d8SEric Huang 	} else if (!ret)
457d35f00d8SEric Huang 		kfd->pci_atomic_requested = true;
4584a488a7aSOded Gabbay 
4594a488a7aSOded Gabbay 	kfd->kgd = kgd;
4604a488a7aSOded Gabbay 	kfd->device_info = device_info;
4614a488a7aSOded Gabbay 	kfd->pdev = pdev;
46219f6d2a6SOded Gabbay 	kfd->init_complete = false;
463cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
464cea405b1SXihan Zhang 
465cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
466cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
467cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
4684a488a7aSOded Gabbay 
4699b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
4709b54d201SEric Huang 
4714a488a7aSOded Gabbay 	return kfd;
4724a488a7aSOded Gabbay }
4734a488a7aSOded Gabbay 
474373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
475373d7080SFelix Kuehling {
476373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
4773e76c239SFelix Kuehling 		if (kfd->device_info->asic_family < CHIP_VEGA10) {
478373d7080SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
479373d7080SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
480373d7080SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
4813e76c239SFelix Kuehling 		} else {
4823e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
4833e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
4843e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
4853e76c239SFelix Kuehling 		}
4863e76c239SFelix Kuehling 
487373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
488373d7080SFelix Kuehling 	}
489373d7080SFelix Kuehling }
490373d7080SFelix Kuehling 
4914a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
4924a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
4934a488a7aSOded Gabbay {
49419f6d2a6SOded Gabbay 	unsigned int size;
49519f6d2a6SOded Gabbay 
496*0da8b10eSAmber Lin 	kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
4975ade6c9cSFelix Kuehling 			KGD_ENGINE_MEC1);
498*0da8b10eSAmber Lin 	kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->kgd,
4995ade6c9cSFelix Kuehling 			KGD_ENGINE_SDMA1);
5004a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
5014a488a7aSOded Gabbay 
50244008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
50344008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
50444008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
50544008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
50644008d7aSYong Zhao 
507a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
508a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
509a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
510a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
511a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
512a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
513a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
514a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
515a99c6d4fSFelix Kuehling 	} else
516a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
517a99c6d4fSFelix Kuehling 
51819f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
519b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
52019f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
52119f6d2a6SOded Gabbay 
522e18e794eSOded Gabbay 	/*
523e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
524e18e794eSOded Gabbay 	 * There can be only 2 packets at once
525e18e794eSOded Gabbay 	 */
526507968ddSFelix Kuehling 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
527507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
528507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
529e18e794eSOded Gabbay 
530e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
531e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
532e18e794eSOded Gabbay 
533e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
53419f6d2a6SOded Gabbay 	size += 512 * 1024;
53519f6d2a6SOded Gabbay 
5367cd52c91SAmber Lin 	if (amdgpu_amdkfd_alloc_gtt_mem(
537cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
53815426dbbSYong Zhao 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
53915426dbbSYong Zhao 			false)) {
54079775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
54119f6d2a6SOded Gabbay 		goto out;
54219f6d2a6SOded Gabbay 	}
54319f6d2a6SOded Gabbay 
54479775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
545e18e794eSOded Gabbay 
54673a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
54773a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
54879775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
54973a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
55073a1da0bSOded Gabbay 	}
55173a1da0bSOded Gabbay 
552735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
553735df2baSFelix Kuehling 		dev_err(kfd_device,
554735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
555735df2baSFelix Kuehling 		goto kfd_doorbell_error;
556735df2baSFelix Kuehling 	}
55719f6d2a6SOded Gabbay 
5580c1690e3SShaoyun Liu 	if (kfd->kfd2kgd->get_hive_id)
5590c1690e3SShaoyun Liu 		kfd->hive_id = kfd->kfd2kgd->get_hive_id(kfd->kgd);
5600c1690e3SShaoyun Liu 
5614eacc26bSKent Russell 	if (kfd_topology_add_device(kfd)) {
56279775b62SKent Russell 		dev_err(kfd_device, "Error adding device to topology\n");
56319f6d2a6SOded Gabbay 		goto kfd_topology_add_device_error;
56419f6d2a6SOded Gabbay 	}
56519f6d2a6SOded Gabbay 
5662249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
56779775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
5682249d558SAndrew Lewycky 		goto kfd_interrupt_error;
5692249d558SAndrew Lewycky 	}
5702249d558SAndrew Lewycky 
57164c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
57264c7f8cfSBen Goz 	if (!kfd->dqm) {
57379775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
57464c7f8cfSBen Goz 		goto device_queue_manager_error;
57564c7f8cfSBen Goz 	}
57664c7f8cfSBen Goz 
57764d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
57864d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
57964d1c3a4SFelix Kuehling 		goto device_iommu_error;
58064c7f8cfSBen Goz 	}
58164c7f8cfSBen Goz 
582373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
583373d7080SFelix Kuehling 
584b8935a7cSYong Zhao 	if (kfd_resume(kfd))
585b8935a7cSYong Zhao 		goto kfd_resume_error;
586b8935a7cSYong Zhao 
587fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
588fbeb661bSYair Shachar 
5894a488a7aSOded Gabbay 	kfd->init_complete = true;
59079775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
5914a488a7aSOded Gabbay 		 kfd->pdev->device);
5924a488a7aSOded Gabbay 
59379775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
594d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
59564c7f8cfSBen Goz 
59619f6d2a6SOded Gabbay 	goto out;
59719f6d2a6SOded Gabbay 
598b8935a7cSYong Zhao kfd_resume_error:
59964d1c3a4SFelix Kuehling device_iommu_error:
60064c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
60164c7f8cfSBen Goz device_queue_manager_error:
6022249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
6032249d558SAndrew Lewycky kfd_interrupt_error:
604b17f068aSOded Gabbay 	kfd_topology_remove_device(kfd);
60519f6d2a6SOded Gabbay kfd_topology_add_device_error:
606735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
607735df2baSFelix Kuehling kfd_doorbell_error:
60873a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
60973a1da0bSOded Gabbay kfd_gtt_sa_init_error:
6107cd52c91SAmber Lin 	amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
61119f6d2a6SOded Gabbay 	dev_err(kfd_device,
61279775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
61319f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
61419f6d2a6SOded Gabbay out:
61519f6d2a6SOded Gabbay 	return kfd->init_complete;
6164a488a7aSOded Gabbay }
6174a488a7aSOded Gabbay 
6184a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
6194a488a7aSOded Gabbay {
620b17f068aSOded Gabbay 	if (kfd->init_complete) {
621b8935a7cSYong Zhao 		kgd2kfd_suspend(kfd);
62264c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
6232249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
62419f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
625735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
62673a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
6277cd52c91SAmber Lin 		amdgpu_amdkfd_free_gtt_mem(kfd->kgd, kfd->gtt_mem);
628b17f068aSOded Gabbay 	}
6295b5c4e40SEvgeny Pinchuk 
6304a488a7aSOded Gabbay 	kfree(kfd);
6314a488a7aSOded Gabbay }
6324a488a7aSOded Gabbay 
633e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
634e3b7a967SShaoyun Liu {
635e42051d2SShaoyun Liu 	if (!kfd->init_complete)
636e42051d2SShaoyun Liu 		return 0;
637e42051d2SShaoyun Liu 	kgd2kfd_suspend(kfd);
638e42051d2SShaoyun Liu 
639e42051d2SShaoyun Liu 	/* hold dqm->lock to prevent further execution*/
640e42051d2SShaoyun Liu 	dqm_lock(kfd->dqm);
641e42051d2SShaoyun Liu 
642e42051d2SShaoyun Liu 	kfd_signal_reset_event(kfd);
643e3b7a967SShaoyun Liu 	return 0;
644e3b7a967SShaoyun Liu }
645e3b7a967SShaoyun Liu 
646e42051d2SShaoyun Liu /*
647e42051d2SShaoyun Liu  * Fix me. KFD won't be able to resume existing process for now.
648e42051d2SShaoyun Liu  * We will keep all existing process in a evicted state and
649e42051d2SShaoyun Liu  * wait the process to be terminated.
650e42051d2SShaoyun Liu  */
651e42051d2SShaoyun Liu 
652e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
653e3b7a967SShaoyun Liu {
654e42051d2SShaoyun Liu 	int ret, count;
655e42051d2SShaoyun Liu 
656e42051d2SShaoyun Liu 	if (!kfd->init_complete)
657e3b7a967SShaoyun Liu 		return 0;
658e42051d2SShaoyun Liu 
659e42051d2SShaoyun Liu 	dqm_unlock(kfd->dqm);
660e42051d2SShaoyun Liu 
661e42051d2SShaoyun Liu 	ret = kfd_resume(kfd);
662e42051d2SShaoyun Liu 	if (ret)
663e42051d2SShaoyun Liu 		return ret;
664e42051d2SShaoyun Liu 	count = atomic_dec_return(&kfd_locked);
665e42051d2SShaoyun Liu 	WARN_ONCE(count != 0, "KFD reset ref. error");
6669b54d201SEric Huang 
6679b54d201SEric Huang 	atomic_set(&kfd->sram_ecc_flag, 0);
6689b54d201SEric Huang 
669e42051d2SShaoyun Liu 	return 0;
670e42051d2SShaoyun Liu }
671e42051d2SShaoyun Liu 
672e42051d2SShaoyun Liu bool kfd_is_locked(void)
673e42051d2SShaoyun Liu {
674e42051d2SShaoyun Liu 	return  (atomic_read(&kfd_locked) > 0);
675e3b7a967SShaoyun Liu }
676e3b7a967SShaoyun Liu 
6774a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd)
6784a488a7aSOded Gabbay {
679733fa1f7SYong Zhao 	if (!kfd->init_complete)
680733fa1f7SYong Zhao 		return;
681733fa1f7SYong Zhao 
68226103436SFelix Kuehling 	/* For first KFD device suspend all the KFD processes */
683e42051d2SShaoyun Liu 	if (atomic_inc_return(&kfd_locked) == 1)
68426103436SFelix Kuehling 		kfd_suspend_all_processes();
68526103436SFelix Kuehling 
68645c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
687733fa1f7SYong Zhao 
68864d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
6894a488a7aSOded Gabbay }
6904a488a7aSOded Gabbay 
6914a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd)
6924a488a7aSOded Gabbay {
69326103436SFelix Kuehling 	int ret, count;
69426103436SFelix Kuehling 
695b8935a7cSYong Zhao 	if (!kfd->init_complete)
696b8935a7cSYong Zhao 		return 0;
697b17f068aSOded Gabbay 
69826103436SFelix Kuehling 	ret = kfd_resume(kfd);
69926103436SFelix Kuehling 	if (ret)
70026103436SFelix Kuehling 		return ret;
701b17f068aSOded Gabbay 
702e42051d2SShaoyun Liu 	count = atomic_dec_return(&kfd_locked);
70326103436SFelix Kuehling 	WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
70426103436SFelix Kuehling 	if (count == 0)
70526103436SFelix Kuehling 		ret = kfd_resume_all_processes();
70626103436SFelix Kuehling 
70726103436SFelix Kuehling 	return ret;
7084ebc7182SYong Zhao }
7094ebc7182SYong Zhao 
710b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
711b8935a7cSYong Zhao {
712b8935a7cSYong Zhao 	int err = 0;
713b8935a7cSYong Zhao 
71464d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
71564d1c3a4SFelix Kuehling 	if (err) {
71664d1c3a4SFelix Kuehling 		dev_err(kfd_device,
71764d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
71864d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
71964d1c3a4SFelix Kuehling 		return err;
72064d1c3a4SFelix Kuehling 	}
721733fa1f7SYong Zhao 
722b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
723b8935a7cSYong Zhao 	if (err) {
724b8935a7cSYong Zhao 		dev_err(kfd_device,
725b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
726b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
727b8935a7cSYong Zhao 		goto dqm_start_error;
728b17f068aSOded Gabbay 	}
729b17f068aSOded Gabbay 
730b8935a7cSYong Zhao 	return err;
731b8935a7cSYong Zhao 
732b8935a7cSYong Zhao dqm_start_error:
73364d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
734b8935a7cSYong Zhao 	return err;
7354a488a7aSOded Gabbay }
7364a488a7aSOded Gabbay 
737b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
738b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
7394a488a7aSOded Gabbay {
74058e69886SLan Xiao 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
74158e69886SLan Xiao 	bool is_patched = false;
7422383a767SChristian König 	unsigned long flags;
74358e69886SLan Xiao 
7442249d558SAndrew Lewycky 	if (!kfd->init_complete)
7452249d558SAndrew Lewycky 		return;
7462249d558SAndrew Lewycky 
74758e69886SLan Xiao 	if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
74858e69886SLan Xiao 		dev_err_once(kfd_device, "Ring entry too small\n");
74958e69886SLan Xiao 		return;
75058e69886SLan Xiao 	}
75158e69886SLan Xiao 
7522383a767SChristian König 	spin_lock_irqsave(&kfd->interrupt_lock, flags);
7532249d558SAndrew Lewycky 
7542249d558SAndrew Lewycky 	if (kfd->interrupts_active
75558e69886SLan Xiao 	    && interrupt_is_wanted(kfd, ih_ring_entry,
75658e69886SLan Xiao 				   patched_ihre, &is_patched)
75758e69886SLan Xiao 	    && enqueue_ih_ring_entry(kfd,
75858e69886SLan Xiao 				     is_patched ? patched_ihre : ih_ring_entry))
75948e876a2SAndres Rodriguez 		queue_work(kfd->ih_wq, &kfd->interrupt_work);
7602249d558SAndrew Lewycky 
7612383a767SChristian König 	spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
7624a488a7aSOded Gabbay }
7636e81090bSOded Gabbay 
7646b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
7656b95e797SFelix Kuehling {
7666b95e797SFelix Kuehling 	struct kfd_process *p;
7676b95e797SFelix Kuehling 	int r;
7686b95e797SFelix Kuehling 
7696b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
7706b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
7716b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
7726b95e797SFelix Kuehling 	 */
7736b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
7746b95e797SFelix Kuehling 	if (!p)
7756b95e797SFelix Kuehling 		return -ESRCH;
7766b95e797SFelix Kuehling 
7776b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
7786b95e797SFelix Kuehling 
7796b95e797SFelix Kuehling 	kfd_unref_process(p);
7806b95e797SFelix Kuehling 	return r;
7816b95e797SFelix Kuehling }
7826b95e797SFelix Kuehling 
7836b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
7846b95e797SFelix Kuehling {
7856b95e797SFelix Kuehling 	struct kfd_process *p;
7866b95e797SFelix Kuehling 	int r;
7876b95e797SFelix Kuehling 
7886b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
7896b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
7906b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
7916b95e797SFelix Kuehling 	 */
7926b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
7936b95e797SFelix Kuehling 	if (!p)
7946b95e797SFelix Kuehling 		return -ESRCH;
7956b95e797SFelix Kuehling 
7966b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
7976b95e797SFelix Kuehling 
7986b95e797SFelix Kuehling 	kfd_unref_process(p);
7996b95e797SFelix Kuehling 	return r;
8006b95e797SFelix Kuehling }
8016b95e797SFelix Kuehling 
80226103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
80326103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
80426103436SFelix Kuehling  *   process.
80526103436SFelix Kuehling  *
80626103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
80726103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
80826103436SFelix Kuehling  *
80926103436SFelix Kuehling  */
81026103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
81126103436SFelix Kuehling 					       struct dma_fence *fence)
81226103436SFelix Kuehling {
81326103436SFelix Kuehling 	struct kfd_process *p;
81426103436SFelix Kuehling 	unsigned long active_time;
81526103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
81626103436SFelix Kuehling 
81726103436SFelix Kuehling 	if (!fence)
81826103436SFelix Kuehling 		return -EINVAL;
81926103436SFelix Kuehling 
82026103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
82126103436SFelix Kuehling 		return 0;
82226103436SFelix Kuehling 
82326103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
82426103436SFelix Kuehling 	if (!p)
82526103436SFelix Kuehling 		return -ENODEV;
82626103436SFelix Kuehling 
82726103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
82826103436SFelix Kuehling 		goto out;
82926103436SFelix Kuehling 
83026103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
83126103436SFelix Kuehling 
83226103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
83326103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
83426103436SFelix Kuehling 	 */
83526103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
83626103436SFelix Kuehling 	if (delay_jiffies > active_time)
83726103436SFelix Kuehling 		delay_jiffies -= active_time;
83826103436SFelix Kuehling 	else
83926103436SFelix Kuehling 		delay_jiffies = 0;
84026103436SFelix Kuehling 
84126103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
84226103436SFelix Kuehling 	 * to kfd_evict_bo_worker
84326103436SFelix Kuehling 	 */
84426103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
84526103436SFelix Kuehling out:
84626103436SFelix Kuehling 	kfd_unref_process(p);
84726103436SFelix Kuehling 	return 0;
84826103436SFelix Kuehling }
84926103436SFelix Kuehling 
8506e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
8516e81090bSOded Gabbay 				unsigned int chunk_size)
8526e81090bSOded Gabbay {
8538625ff9cSFelix Kuehling 	unsigned int num_of_longs;
8546e81090bSOded Gabbay 
85532fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
85632fa8219SFelix Kuehling 		return -EINVAL;
85732fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
85832fa8219SFelix Kuehling 		return -EINVAL;
85932fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
86032fa8219SFelix Kuehling 		return -EINVAL;
8616e81090bSOded Gabbay 
8626e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
8636e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
8646e81090bSOded Gabbay 
8658625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
8668625ff9cSFelix Kuehling 		BITS_PER_LONG;
8676e81090bSOded Gabbay 
8688625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
8696e81090bSOded Gabbay 
8706e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
8716e81090bSOded Gabbay 		return -ENOMEM;
8726e81090bSOded Gabbay 
87379775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
8746e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
8756e81090bSOded Gabbay 
8766e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
8776e81090bSOded Gabbay 
8786e81090bSOded Gabbay 	return 0;
8796e81090bSOded Gabbay 
8806e81090bSOded Gabbay }
8816e81090bSOded Gabbay 
8826e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
8836e81090bSOded Gabbay {
8846e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
8856e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
8866e81090bSOded Gabbay }
8876e81090bSOded Gabbay 
8886e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
8896e81090bSOded Gabbay 						unsigned int bit_num,
8906e81090bSOded Gabbay 						unsigned int chunk_size)
8916e81090bSOded Gabbay {
8926e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
8936e81090bSOded Gabbay }
8946e81090bSOded Gabbay 
8956e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
8966e81090bSOded Gabbay 						unsigned int bit_num,
8976e81090bSOded Gabbay 						unsigned int chunk_size)
8986e81090bSOded Gabbay {
8996e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
9006e81090bSOded Gabbay }
9016e81090bSOded Gabbay 
9026e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
9036e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
9046e81090bSOded Gabbay {
9056e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
9066e81090bSOded Gabbay 
9076e81090bSOded Gabbay 	if (size == 0)
9086e81090bSOded Gabbay 		return -EINVAL;
9096e81090bSOded Gabbay 
9106e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
9116e81090bSOded Gabbay 		return -ENOMEM;
9126e81090bSOded Gabbay 
9131cd106ecSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
9141cd106ecSFelix Kuehling 	if (!(*mem_obj))
9156e81090bSOded Gabbay 		return -ENOMEM;
9166e81090bSOded Gabbay 
91779775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
9186e81090bSOded Gabbay 
9196e81090bSOded Gabbay 	start_search = 0;
9206e81090bSOded Gabbay 
9216e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
9226e81090bSOded Gabbay 
9236e81090bSOded Gabbay kfd_gtt_restart_search:
9246e81090bSOded Gabbay 	/* Find the first chunk that is free */
9256e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
9266e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
9276e81090bSOded Gabbay 					start_search);
9286e81090bSOded Gabbay 
92979775b62SKent Russell 	pr_debug("Found = %d\n", found);
9306e81090bSOded Gabbay 
9316e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
9326e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
9336e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
9346e81090bSOded Gabbay 
9356e81090bSOded Gabbay 	/* Update fields of mem_obj */
9366e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
9376e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
9386e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
9396e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
9406e81090bSOded Gabbay 					found,
9416e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
9426e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
9436e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
9446e81090bSOded Gabbay 					found,
9456e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
9466e81090bSOded Gabbay 
94779775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
9486e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
9496e81090bSOded Gabbay 
9506e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
9516e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
95279775b62SKent Russell 		pr_debug("Single bit\n");
9536e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
9546e81090bSOded Gabbay 		goto kfd_gtt_out;
9556e81090bSOded Gabbay 	}
9566e81090bSOded Gabbay 
9576e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
9586e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
9596e81090bSOded Gabbay 	do {
9606e81090bSOded Gabbay 		(*mem_obj)->range_end =
9616e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
9626e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
9636e81090bSOded Gabbay 		/*
9646e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
9656e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
9666e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
9676e81090bSOded Gabbay 		 */
9686e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
9696e81090bSOded Gabbay 			start_search = found;
9706e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
9716e81090bSOded Gabbay 		}
9726e81090bSOded Gabbay 
9736e81090bSOded Gabbay 		/*
9746e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
9756e81090bSOded Gabbay 		 */
9766e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
9776e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
9786e81090bSOded Gabbay 
9796e81090bSOded Gabbay 		/* Check if we don't need another chunk */
9806e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
9816e81090bSOded Gabbay 			cur_size = 0;
9826e81090bSOded Gabbay 		else
9836e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
9846e81090bSOded Gabbay 
9856e81090bSOded Gabbay 	} while (cur_size > 0);
9866e81090bSOded Gabbay 
98779775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
9886e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
9896e81090bSOded Gabbay 
9906e81090bSOded Gabbay 	/* Mark the chunks as allocated */
9916e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
9926e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
9936e81090bSOded Gabbay 		found++)
9946e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
9956e81090bSOded Gabbay 
9966e81090bSOded Gabbay kfd_gtt_out:
9976e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
9986e81090bSOded Gabbay 	return 0;
9996e81090bSOded Gabbay 
10006e81090bSOded Gabbay kfd_gtt_no_free_chunk:
100179775b62SKent Russell 	pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
10026e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
10036e81090bSOded Gabbay 	kfree(mem_obj);
10046e81090bSOded Gabbay 	return -ENOMEM;
10056e81090bSOded Gabbay }
10066e81090bSOded Gabbay 
10076e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
10086e81090bSOded Gabbay {
10096e81090bSOded Gabbay 	unsigned int bit;
10106e81090bSOded Gabbay 
10119216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
10129216ed29SOded Gabbay 	if (!mem_obj)
10139216ed29SOded Gabbay 		return 0;
10146e81090bSOded Gabbay 
101579775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
10166e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
10176e81090bSOded Gabbay 
10186e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
10196e81090bSOded Gabbay 
10206e81090bSOded Gabbay 	/* Mark the chunks as free */
10216e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
10226e81090bSOded Gabbay 		bit <= mem_obj->range_end;
10236e81090bSOded Gabbay 		bit++)
10246e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
10256e81090bSOded Gabbay 
10266e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
10276e81090bSOded Gabbay 
10286e81090bSOded Gabbay 	kfree(mem_obj);
10296e81090bSOded Gabbay 	return 0;
10306e81090bSOded Gabbay }
1031a29ec470SShaoyun Liu 
10329b54d201SEric Huang void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
10339b54d201SEric Huang {
10349b54d201SEric Huang 	if (kfd)
10359b54d201SEric Huang 		atomic_inc(&kfd->sram_ecc_flag);
10369b54d201SEric Huang }
10379b54d201SEric Huang 
1038a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
1039a29ec470SShaoyun Liu 
1040a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
1041a29ec470SShaoyun Liu  * which will trigger a GPU reset and bring the HWS back to normal state
1042a29ec470SShaoyun Liu  */
1043a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1044a29ec470SShaoyun Liu {
1045a29ec470SShaoyun Liu 	int r = 0;
1046a29ec470SShaoyun Liu 
1047a29ec470SShaoyun Liu 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1048a29ec470SShaoyun Liu 		pr_err("HWS is not enabled");
1049a29ec470SShaoyun Liu 		return -EINVAL;
1050a29ec470SShaoyun Liu 	}
1051a29ec470SShaoyun Liu 
1052a29ec470SShaoyun Liu 	r = pm_debugfs_hang_hws(&dev->dqm->packets);
1053a29ec470SShaoyun Liu 	if (!r)
1054a29ec470SShaoyun Liu 		r = dqm_debugfs_execute_queues(dev->dqm);
1055a29ec470SShaoyun Liu 
1056a29ec470SShaoyun Liu 	return r;
1057a29ec470SShaoyun Liu }
1058a29ec470SShaoyun Liu 
1059a29ec470SShaoyun Liu #endif
1060