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"
314a488a7aSOded Gabbay 
3219f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768
33e42051d2SShaoyun Liu 
34e42051d2SShaoyun Liu /*
35e42051d2SShaoyun Liu  * kfd_locked is used to lock the kfd driver during suspend or reset
36e42051d2SShaoyun Liu  * once locked, kfd driver will stop any further GPU execution.
37e42051d2SShaoyun Liu  * create process (open) will return -EAGAIN.
38e42051d2SShaoyun Liu  */
39e42051d2SShaoyun Liu static atomic_t kfd_locked = ATOMIC_INIT(0);
4019f6d2a6SOded Gabbay 
4164d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
424a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = {
430da7558cSBen Goz 	.asic_family = CHIP_KAVERI,
440da7558cSBen Goz 	.max_pasid_bits = 16,
45992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
46992839adSYair Shachar 	.max_no_of_hqd	= 24,
47ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
480da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
49f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
50fbeb661bSYair Shachar 	.num_of_watch_points = 4,
51373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
52373d7080SFelix Kuehling 	.supports_cwsr = false,
5364d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
543ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
5598bb9222SYong Zhao 	.num_sdma_engines = 2,
560da7558cSBen Goz };
570da7558cSBen Goz 
580da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
590da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
604a488a7aSOded Gabbay 	.max_pasid_bits = 16,
61eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
62eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
63ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
64b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
65eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
66f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
67373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
68373d7080SFelix Kuehling 	.supports_cwsr = true,
6964d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
703ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
7198bb9222SYong Zhao 	.num_sdma_engines = 2,
724a488a7aSOded Gabbay };
734d663df6SYong Zhao 
744d663df6SYong Zhao static const struct kfd_device_info raven_device_info = {
754d663df6SYong Zhao 	.asic_family = CHIP_RAVEN,
764d663df6SYong Zhao 	.max_pasid_bits = 16,
774d663df6SYong Zhao 	.max_no_of_hqd  = 24,
784d663df6SYong Zhao 	.doorbell_size  = 8,
794d663df6SYong Zhao 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
804d663df6SYong Zhao 	.event_interrupt_class = &event_interrupt_class_v9,
814d663df6SYong Zhao 	.num_of_watch_points = 4,
824d663df6SYong Zhao 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
834d663df6SYong Zhao 	.supports_cwsr = true,
844d663df6SYong Zhao 	.needs_iommu_device = true,
854d663df6SYong Zhao 	.needs_pci_atomics = true,
864d663df6SYong Zhao 	.num_sdma_engines = 1,
874d663df6SYong Zhao };
8864d1c3a4SFelix Kuehling #endif
894a488a7aSOded Gabbay 
90a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
91a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
92a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
93a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
94a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
95ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
96a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
97a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
98a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
99a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
100a3084e6cSFelix Kuehling 	.supports_cwsr = false,
10164d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
102a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
10398bb9222SYong Zhao 	.num_sdma_engines = 2,
104a3084e6cSFelix Kuehling };
105a3084e6cSFelix Kuehling 
106a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
107a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
108a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
109a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
110ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
111a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
112a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
113a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
114a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
115a3084e6cSFelix Kuehling 	.supports_cwsr = false,
11664d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
117a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
11898bb9222SYong Zhao 	.num_sdma_engines = 2,
119a3084e6cSFelix Kuehling };
120a3084e6cSFelix Kuehling 
121a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
122a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
123a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
124a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
125ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
126a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
127a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
128a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
129a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
130a3084e6cSFelix Kuehling 	.supports_cwsr = true,
13164d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
132a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
13398bb9222SYong Zhao 	.num_sdma_engines = 2,
134a3084e6cSFelix Kuehling };
135a3084e6cSFelix Kuehling 
136a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
137a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
138a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
139a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
140ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
141a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
142a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
143a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
144a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
145a3084e6cSFelix Kuehling 	.supports_cwsr = true,
14664d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
147a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
14898bb9222SYong Zhao 	.num_sdma_engines = 2,
149a3084e6cSFelix Kuehling };
150a3084e6cSFelix Kuehling 
151a3084e6cSFelix Kuehling 
152a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
153a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
154a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
155a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
156ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
157a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
158a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
159a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
160a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
161a3084e6cSFelix Kuehling 	.supports_cwsr = true,
16264d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
163a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
16498bb9222SYong Zhao 	.num_sdma_engines = 2,
165a3084e6cSFelix Kuehling };
166a3084e6cSFelix Kuehling 
167a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_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 = false,
17998bb9222SYong Zhao 	.num_sdma_engines = 2,
180a3084e6cSFelix Kuehling };
181a3084e6cSFelix Kuehling 
182a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = {
183a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
184a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
185a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
186ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
187a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
188a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
189a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
190a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
191a3084e6cSFelix Kuehling 	.supports_cwsr = true,
19264d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
193a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
19498bb9222SYong Zhao 	.num_sdma_engines = 2,
195a3084e6cSFelix Kuehling };
196a3084e6cSFelix Kuehling 
197389056e5SFelix Kuehling static const struct kfd_device_info vega10_device_info = {
198389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
199389056e5SFelix Kuehling 	.max_pasid_bits = 16,
200389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
201389056e5SFelix Kuehling 	.doorbell_size  = 8,
202389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
203389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
204389056e5SFelix Kuehling 	.num_of_watch_points = 4,
205389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
206389056e5SFelix Kuehling 	.supports_cwsr = true,
207389056e5SFelix Kuehling 	.needs_iommu_device = false,
208389056e5SFelix Kuehling 	.needs_pci_atomics = false,
20998bb9222SYong Zhao 	.num_sdma_engines = 2,
210389056e5SFelix Kuehling };
211389056e5SFelix Kuehling 
212389056e5SFelix Kuehling static const struct kfd_device_info vega10_vf_device_info = {
213389056e5SFelix Kuehling 	.asic_family = CHIP_VEGA10,
214389056e5SFelix Kuehling 	.max_pasid_bits = 16,
215389056e5SFelix Kuehling 	.max_no_of_hqd  = 24,
216389056e5SFelix Kuehling 	.doorbell_size  = 8,
217389056e5SFelix Kuehling 	.ih_ring_entry_size = 8 * sizeof(uint32_t),
218389056e5SFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_v9,
219389056e5SFelix Kuehling 	.num_of_watch_points = 4,
220389056e5SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
221389056e5SFelix Kuehling 	.supports_cwsr = true,
222389056e5SFelix Kuehling 	.needs_iommu_device = false,
223389056e5SFelix Kuehling 	.needs_pci_atomics = false,
22498bb9222SYong Zhao 	.num_sdma_engines = 2,
225389056e5SFelix Kuehling };
226389056e5SFelix Kuehling 
227a3084e6cSFelix Kuehling 
2284a488a7aSOded Gabbay struct kfd_deviceid {
2294a488a7aSOded Gabbay 	unsigned short did;
2304a488a7aSOded Gabbay 	const struct kfd_device_info *device_info;
2314a488a7aSOded Gabbay };
2324a488a7aSOded Gabbay 
2334a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = {
23464d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
2354a488a7aSOded Gabbay 	{ 0x1304, &kaveri_device_info },	/* Kaveri */
2364a488a7aSOded Gabbay 	{ 0x1305, &kaveri_device_info },	/* Kaveri */
2374a488a7aSOded Gabbay 	{ 0x1306, &kaveri_device_info },	/* Kaveri */
2384a488a7aSOded Gabbay 	{ 0x1307, &kaveri_device_info },	/* Kaveri */
2394a488a7aSOded Gabbay 	{ 0x1309, &kaveri_device_info },	/* Kaveri */
2404a488a7aSOded Gabbay 	{ 0x130A, &kaveri_device_info },	/* Kaveri */
2414a488a7aSOded Gabbay 	{ 0x130B, &kaveri_device_info },	/* Kaveri */
2424a488a7aSOded Gabbay 	{ 0x130C, &kaveri_device_info },	/* Kaveri */
2434a488a7aSOded Gabbay 	{ 0x130D, &kaveri_device_info },	/* Kaveri */
2444a488a7aSOded Gabbay 	{ 0x130E, &kaveri_device_info },	/* Kaveri */
2454a488a7aSOded Gabbay 	{ 0x130F, &kaveri_device_info },	/* Kaveri */
2464a488a7aSOded Gabbay 	{ 0x1310, &kaveri_device_info },	/* Kaveri */
2474a488a7aSOded Gabbay 	{ 0x1311, &kaveri_device_info },	/* Kaveri */
2484a488a7aSOded Gabbay 	{ 0x1312, &kaveri_device_info },	/* Kaveri */
2494a488a7aSOded Gabbay 	{ 0x1313, &kaveri_device_info },	/* Kaveri */
2504a488a7aSOded Gabbay 	{ 0x1315, &kaveri_device_info },	/* Kaveri */
2514a488a7aSOded Gabbay 	{ 0x1316, &kaveri_device_info },	/* Kaveri */
2524a488a7aSOded Gabbay 	{ 0x1317, &kaveri_device_info },	/* Kaveri */
2534a488a7aSOded Gabbay 	{ 0x1318, &kaveri_device_info },	/* Kaveri */
2544a488a7aSOded Gabbay 	{ 0x131B, &kaveri_device_info },	/* Kaveri */
2554a488a7aSOded Gabbay 	{ 0x131C, &kaveri_device_info },	/* Kaveri */
256123576d1SBen Goz 	{ 0x131D, &kaveri_device_info },	/* Kaveri */
257123576d1SBen Goz 	{ 0x9870, &carrizo_device_info },	/* Carrizo */
258123576d1SBen Goz 	{ 0x9874, &carrizo_device_info },	/* Carrizo */
259123576d1SBen Goz 	{ 0x9875, &carrizo_device_info },	/* Carrizo */
260123576d1SBen Goz 	{ 0x9876, &carrizo_device_info },	/* Carrizo */
261a3084e6cSFelix Kuehling 	{ 0x9877, &carrizo_device_info },	/* Carrizo */
2624d663df6SYong Zhao 	{ 0x15DD, &raven_device_info },		/* Raven */
26364d1c3a4SFelix Kuehling #endif
264a3084e6cSFelix Kuehling 	{ 0x67A0, &hawaii_device_info },	/* Hawaii */
265a3084e6cSFelix Kuehling 	{ 0x67A1, &hawaii_device_info },	/* Hawaii */
266a3084e6cSFelix Kuehling 	{ 0x67A2, &hawaii_device_info },	/* Hawaii */
267a3084e6cSFelix Kuehling 	{ 0x67A8, &hawaii_device_info },	/* Hawaii */
268a3084e6cSFelix Kuehling 	{ 0x67A9, &hawaii_device_info },	/* Hawaii */
269a3084e6cSFelix Kuehling 	{ 0x67AA, &hawaii_device_info },	/* Hawaii */
270a3084e6cSFelix Kuehling 	{ 0x67B0, &hawaii_device_info },	/* Hawaii */
271a3084e6cSFelix Kuehling 	{ 0x67B1, &hawaii_device_info },	/* Hawaii */
272a3084e6cSFelix Kuehling 	{ 0x67B8, &hawaii_device_info },	/* Hawaii */
273a3084e6cSFelix Kuehling 	{ 0x67B9, &hawaii_device_info },	/* Hawaii */
274a3084e6cSFelix Kuehling 	{ 0x67BA, &hawaii_device_info },	/* Hawaii */
275a3084e6cSFelix Kuehling 	{ 0x67BE, &hawaii_device_info },	/* Hawaii */
276a3084e6cSFelix Kuehling 	{ 0x6920, &tonga_device_info },		/* Tonga */
277a3084e6cSFelix Kuehling 	{ 0x6921, &tonga_device_info },		/* Tonga */
278a3084e6cSFelix Kuehling 	{ 0x6928, &tonga_device_info },		/* Tonga */
279a3084e6cSFelix Kuehling 	{ 0x6929, &tonga_device_info },		/* Tonga */
280a3084e6cSFelix Kuehling 	{ 0x692B, &tonga_device_info },		/* Tonga */
281a3084e6cSFelix Kuehling 	{ 0x6938, &tonga_device_info },		/* Tonga */
282a3084e6cSFelix Kuehling 	{ 0x6939, &tonga_device_info },		/* Tonga */
283a3084e6cSFelix Kuehling 	{ 0x7300, &fiji_device_info },		/* Fiji */
284a3084e6cSFelix Kuehling 	{ 0x730F, &fiji_vf_device_info },	/* Fiji vf*/
285a3084e6cSFelix Kuehling 	{ 0x67C0, &polaris10_device_info },	/* Polaris10 */
286a3084e6cSFelix Kuehling 	{ 0x67C1, &polaris10_device_info },	/* Polaris10 */
287a3084e6cSFelix Kuehling 	{ 0x67C2, &polaris10_device_info },	/* Polaris10 */
288a3084e6cSFelix Kuehling 	{ 0x67C4, &polaris10_device_info },	/* Polaris10 */
289a3084e6cSFelix Kuehling 	{ 0x67C7, &polaris10_device_info },	/* Polaris10 */
290a3084e6cSFelix Kuehling 	{ 0x67C8, &polaris10_device_info },	/* Polaris10 */
291a3084e6cSFelix Kuehling 	{ 0x67C9, &polaris10_device_info },	/* Polaris10 */
292a3084e6cSFelix Kuehling 	{ 0x67CA, &polaris10_device_info },	/* Polaris10 */
293a3084e6cSFelix Kuehling 	{ 0x67CC, &polaris10_device_info },	/* Polaris10 */
294a3084e6cSFelix Kuehling 	{ 0x67CF, &polaris10_device_info },	/* Polaris10 */
295a3084e6cSFelix Kuehling 	{ 0x67D0, &polaris10_vf_device_info },	/* Polaris10 vf*/
296a3084e6cSFelix Kuehling 	{ 0x67DF, &polaris10_device_info },	/* Polaris10 */
297a3084e6cSFelix Kuehling 	{ 0x67E0, &polaris11_device_info },	/* Polaris11 */
298a3084e6cSFelix Kuehling 	{ 0x67E1, &polaris11_device_info },	/* Polaris11 */
299a3084e6cSFelix Kuehling 	{ 0x67E3, &polaris11_device_info },	/* Polaris11 */
300a3084e6cSFelix Kuehling 	{ 0x67E7, &polaris11_device_info },	/* Polaris11 */
301a3084e6cSFelix Kuehling 	{ 0x67E8, &polaris11_device_info },	/* Polaris11 */
302a3084e6cSFelix Kuehling 	{ 0x67E9, &polaris11_device_info },	/* Polaris11 */
303a3084e6cSFelix Kuehling 	{ 0x67EB, &polaris11_device_info },	/* Polaris11 */
304a3084e6cSFelix Kuehling 	{ 0x67EF, &polaris11_device_info },	/* Polaris11 */
305a3084e6cSFelix Kuehling 	{ 0x67FF, &polaris11_device_info },	/* Polaris11 */
306389056e5SFelix Kuehling 	{ 0x6860, &vega10_device_info },	/* Vega10 */
307389056e5SFelix Kuehling 	{ 0x6861, &vega10_device_info },	/* Vega10 */
308389056e5SFelix Kuehling 	{ 0x6862, &vega10_device_info },	/* Vega10 */
309389056e5SFelix Kuehling 	{ 0x6863, &vega10_device_info },	/* Vega10 */
310389056e5SFelix Kuehling 	{ 0x6864, &vega10_device_info },	/* Vega10 */
311389056e5SFelix Kuehling 	{ 0x6867, &vega10_device_info },	/* Vega10 */
312389056e5SFelix Kuehling 	{ 0x6868, &vega10_device_info },	/* Vega10 */
313389056e5SFelix Kuehling 	{ 0x686C, &vega10_vf_device_info },	/* Vega10  vf*/
314389056e5SFelix Kuehling 	{ 0x687F, &vega10_device_info },	/* Vega10 */
3154a488a7aSOded Gabbay };
3164a488a7aSOded Gabbay 
3176e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
3186e81090bSOded Gabbay 				unsigned int chunk_size);
3196e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
3206e81090bSOded Gabbay 
321b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
322b8935a7cSYong Zhao 
3234a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did)
3244a488a7aSOded Gabbay {
3254a488a7aSOded Gabbay 	size_t i;
3264a488a7aSOded Gabbay 
3274a488a7aSOded Gabbay 	for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
3284a488a7aSOded Gabbay 		if (supported_devices[i].did == did) {
32932fa8219SFelix Kuehling 			WARN_ON(!supported_devices[i].device_info);
3304a488a7aSOded Gabbay 			return supported_devices[i].device_info;
3314a488a7aSOded Gabbay 		}
3324a488a7aSOded Gabbay 	}
3334a488a7aSOded Gabbay 
3344ebc7182SYong Zhao 	dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
3354ebc7182SYong Zhao 		 did);
3364ebc7182SYong Zhao 
3374a488a7aSOded Gabbay 	return NULL;
3384a488a7aSOded Gabbay }
3394a488a7aSOded Gabbay 
340cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
341cea405b1SXihan Zhang 	struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
3424a488a7aSOded Gabbay {
3434a488a7aSOded Gabbay 	struct kfd_dev *kfd;
3446106dce9Swelu 	int ret;
3454a488a7aSOded Gabbay 	const struct kfd_device_info *device_info =
3464a488a7aSOded Gabbay 					lookup_device_info(pdev->device);
3474a488a7aSOded Gabbay 
3484ebc7182SYong Zhao 	if (!device_info) {
3494ebc7182SYong Zhao 		dev_err(kfd_device, "kgd2kfd_probe failed\n");
3504a488a7aSOded Gabbay 		return NULL;
3514ebc7182SYong Zhao 	}
3524a488a7aSOded Gabbay 
353d35f00d8SEric Huang 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
354d35f00d8SEric Huang 	if (!kfd)
355d35f00d8SEric Huang 		return NULL;
356d35f00d8SEric Huang 
3576106dce9Swelu 	/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
3586106dce9Swelu 	 * 32 and 64-bit requests are possible and must be
3596106dce9Swelu 	 * supported.
3603ee2d00cSFelix Kuehling 	 */
3616106dce9Swelu 	ret = pci_enable_atomic_ops_to_root(pdev,
3623ee2d00cSFelix Kuehling 			PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3636106dce9Swelu 			PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3646106dce9Swelu 	if (device_info->needs_pci_atomics && ret < 0) {
3653ee2d00cSFelix Kuehling 		dev_info(kfd_device,
3666106dce9Swelu 			 "skipped device %x:%x, PCI rejects atomics\n",
3673ee2d00cSFelix Kuehling 			 pdev->vendor, pdev->device);
368d35f00d8SEric Huang 		kfree(kfd);
3693ee2d00cSFelix Kuehling 		return NULL;
370d35f00d8SEric Huang 	} else if (!ret)
371d35f00d8SEric Huang 		kfd->pci_atomic_requested = true;
3724a488a7aSOded Gabbay 
3734a488a7aSOded Gabbay 	kfd->kgd = kgd;
3744a488a7aSOded Gabbay 	kfd->device_info = device_info;
3754a488a7aSOded Gabbay 	kfd->pdev = pdev;
37619f6d2a6SOded Gabbay 	kfd->init_complete = false;
377cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
378cea405b1SXihan Zhang 
379cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
380cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
381cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
3824a488a7aSOded Gabbay 
3834a488a7aSOded Gabbay 	return kfd;
3844a488a7aSOded Gabbay }
3854a488a7aSOded Gabbay 
386373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
387373d7080SFelix Kuehling {
388373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
3893e76c239SFelix Kuehling 		if (kfd->device_info->asic_family < CHIP_VEGA10) {
390373d7080SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
391373d7080SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
392373d7080SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
3933e76c239SFelix Kuehling 		} else {
3943e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
3953e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
3963e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
3973e76c239SFelix Kuehling 		}
3983e76c239SFelix Kuehling 
399373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
400373d7080SFelix Kuehling 	}
401373d7080SFelix Kuehling }
402373d7080SFelix Kuehling 
4034a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
4044a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
4054a488a7aSOded Gabbay {
40619f6d2a6SOded Gabbay 	unsigned int size;
40719f6d2a6SOded Gabbay 
408*5ade6c9cSFelix Kuehling 	kfd->mec_fw_version = kfd->kfd2kgd->get_fw_version(kfd->kgd,
409*5ade6c9cSFelix Kuehling 			KGD_ENGINE_MEC1);
410*5ade6c9cSFelix Kuehling 	kfd->sdma_fw_version = kfd->kfd2kgd->get_fw_version(kfd->kgd,
411*5ade6c9cSFelix Kuehling 			KGD_ENGINE_SDMA1);
4124a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
4134a488a7aSOded Gabbay 
41444008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
41544008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
41644008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
41744008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
41844008d7aSYong Zhao 
419a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
420a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
421a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
422a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
423a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
424a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
425a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
426a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
427a99c6d4fSFelix Kuehling 	} else
428a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
429a99c6d4fSFelix Kuehling 
43019f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
431b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
43219f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
43319f6d2a6SOded Gabbay 
434e18e794eSOded Gabbay 	/*
435e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
436e18e794eSOded Gabbay 	 * There can be only 2 packets at once
437e18e794eSOded Gabbay 	 */
438507968ddSFelix Kuehling 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
439507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
440507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
441e18e794eSOded Gabbay 
442e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
443e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
444e18e794eSOded Gabbay 
445e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
44619f6d2a6SOded Gabbay 	size += 512 * 1024;
44719f6d2a6SOded Gabbay 
448cea405b1SXihan Zhang 	if (kfd->kfd2kgd->init_gtt_mem_allocation(
449cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
45015426dbbSYong Zhao 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
45115426dbbSYong Zhao 			false)) {
45279775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
45319f6d2a6SOded Gabbay 		goto out;
45419f6d2a6SOded Gabbay 	}
45519f6d2a6SOded Gabbay 
45679775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
457e18e794eSOded Gabbay 
45873a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
45973a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
46079775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
46173a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
46273a1da0bSOded Gabbay 	}
46373a1da0bSOded Gabbay 
464735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
465735df2baSFelix Kuehling 		dev_err(kfd_device,
466735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
467735df2baSFelix Kuehling 		goto kfd_doorbell_error;
468735df2baSFelix Kuehling 	}
46919f6d2a6SOded Gabbay 
4700c1690e3SShaoyun Liu 	if (kfd->kfd2kgd->get_hive_id)
4710c1690e3SShaoyun Liu 		kfd->hive_id = kfd->kfd2kgd->get_hive_id(kfd->kgd);
4720c1690e3SShaoyun Liu 
4734eacc26bSKent Russell 	if (kfd_topology_add_device(kfd)) {
47479775b62SKent Russell 		dev_err(kfd_device, "Error adding device to topology\n");
47519f6d2a6SOded Gabbay 		goto kfd_topology_add_device_error;
47619f6d2a6SOded Gabbay 	}
47719f6d2a6SOded Gabbay 
4782249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
47979775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
4802249d558SAndrew Lewycky 		goto kfd_interrupt_error;
4812249d558SAndrew Lewycky 	}
4822249d558SAndrew Lewycky 
48364c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
48464c7f8cfSBen Goz 	if (!kfd->dqm) {
48579775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
48664c7f8cfSBen Goz 		goto device_queue_manager_error;
48764c7f8cfSBen Goz 	}
48864c7f8cfSBen Goz 
48964d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
49064d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
49164d1c3a4SFelix Kuehling 		goto device_iommu_error;
49264c7f8cfSBen Goz 	}
49364c7f8cfSBen Goz 
494373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
495373d7080SFelix Kuehling 
496b8935a7cSYong Zhao 	if (kfd_resume(kfd))
497b8935a7cSYong Zhao 		goto kfd_resume_error;
498b8935a7cSYong Zhao 
499fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
500fbeb661bSYair Shachar 
5014a488a7aSOded Gabbay 	kfd->init_complete = true;
50279775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
5034a488a7aSOded Gabbay 		 kfd->pdev->device);
5044a488a7aSOded Gabbay 
50579775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
506d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
50764c7f8cfSBen Goz 
50819f6d2a6SOded Gabbay 	goto out;
50919f6d2a6SOded Gabbay 
510b8935a7cSYong Zhao kfd_resume_error:
51164d1c3a4SFelix Kuehling device_iommu_error:
51264c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
51364c7f8cfSBen Goz device_queue_manager_error:
5142249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
5152249d558SAndrew Lewycky kfd_interrupt_error:
516b17f068aSOded Gabbay 	kfd_topology_remove_device(kfd);
51719f6d2a6SOded Gabbay kfd_topology_add_device_error:
518735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
519735df2baSFelix Kuehling kfd_doorbell_error:
52073a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
52173a1da0bSOded Gabbay kfd_gtt_sa_init_error:
522cea405b1SXihan Zhang 	kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
52319f6d2a6SOded Gabbay 	dev_err(kfd_device,
52479775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
52519f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
52619f6d2a6SOded Gabbay out:
52719f6d2a6SOded Gabbay 	return kfd->init_complete;
5284a488a7aSOded Gabbay }
5294a488a7aSOded Gabbay 
5304a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
5314a488a7aSOded Gabbay {
532b17f068aSOded Gabbay 	if (kfd->init_complete) {
533b8935a7cSYong Zhao 		kgd2kfd_suspend(kfd);
53464c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
5352249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
53619f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
537735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
53873a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
539cea405b1SXihan Zhang 		kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
540b17f068aSOded Gabbay 	}
5415b5c4e40SEvgeny Pinchuk 
5424a488a7aSOded Gabbay 	kfree(kfd);
5434a488a7aSOded Gabbay }
5444a488a7aSOded Gabbay 
545e3b7a967SShaoyun Liu int kgd2kfd_pre_reset(struct kfd_dev *kfd)
546e3b7a967SShaoyun Liu {
547e42051d2SShaoyun Liu 	if (!kfd->init_complete)
548e42051d2SShaoyun Liu 		return 0;
549e42051d2SShaoyun Liu 	kgd2kfd_suspend(kfd);
550e42051d2SShaoyun Liu 
551e42051d2SShaoyun Liu 	/* hold dqm->lock to prevent further execution*/
552e42051d2SShaoyun Liu 	dqm_lock(kfd->dqm);
553e42051d2SShaoyun Liu 
554e42051d2SShaoyun Liu 	kfd_signal_reset_event(kfd);
555e3b7a967SShaoyun Liu 	return 0;
556e3b7a967SShaoyun Liu }
557e3b7a967SShaoyun Liu 
558e42051d2SShaoyun Liu /*
559e42051d2SShaoyun Liu  * Fix me. KFD won't be able to resume existing process for now.
560e42051d2SShaoyun Liu  * We will keep all existing process in a evicted state and
561e42051d2SShaoyun Liu  * wait the process to be terminated.
562e42051d2SShaoyun Liu  */
563e42051d2SShaoyun Liu 
564e3b7a967SShaoyun Liu int kgd2kfd_post_reset(struct kfd_dev *kfd)
565e3b7a967SShaoyun Liu {
566e42051d2SShaoyun Liu 	int ret, count;
567e42051d2SShaoyun Liu 
568e42051d2SShaoyun Liu 	if (!kfd->init_complete)
569e3b7a967SShaoyun Liu 		return 0;
570e42051d2SShaoyun Liu 
571e42051d2SShaoyun Liu 	dqm_unlock(kfd->dqm);
572e42051d2SShaoyun Liu 
573e42051d2SShaoyun Liu 	ret = kfd_resume(kfd);
574e42051d2SShaoyun Liu 	if (ret)
575e42051d2SShaoyun Liu 		return ret;
576e42051d2SShaoyun Liu 	count = atomic_dec_return(&kfd_locked);
577e42051d2SShaoyun Liu 	WARN_ONCE(count != 0, "KFD reset ref. error");
578e42051d2SShaoyun Liu 	return 0;
579e42051d2SShaoyun Liu }
580e42051d2SShaoyun Liu 
581e42051d2SShaoyun Liu bool kfd_is_locked(void)
582e42051d2SShaoyun Liu {
583e42051d2SShaoyun Liu 	return  (atomic_read(&kfd_locked) > 0);
584e3b7a967SShaoyun Liu }
585e3b7a967SShaoyun Liu 
5864a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd)
5874a488a7aSOded Gabbay {
588733fa1f7SYong Zhao 	if (!kfd->init_complete)
589733fa1f7SYong Zhao 		return;
590733fa1f7SYong Zhao 
59126103436SFelix Kuehling 	/* For first KFD device suspend all the KFD processes */
592e42051d2SShaoyun Liu 	if (atomic_inc_return(&kfd_locked) == 1)
59326103436SFelix Kuehling 		kfd_suspend_all_processes();
59426103436SFelix Kuehling 
59545c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
596733fa1f7SYong Zhao 
59764d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
5984a488a7aSOded Gabbay }
5994a488a7aSOded Gabbay 
6004a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd)
6014a488a7aSOded Gabbay {
60226103436SFelix Kuehling 	int ret, count;
60326103436SFelix Kuehling 
604b8935a7cSYong Zhao 	if (!kfd->init_complete)
605b8935a7cSYong Zhao 		return 0;
606b17f068aSOded Gabbay 
60726103436SFelix Kuehling 	ret = kfd_resume(kfd);
60826103436SFelix Kuehling 	if (ret)
60926103436SFelix Kuehling 		return ret;
610b17f068aSOded Gabbay 
611e42051d2SShaoyun Liu 	count = atomic_dec_return(&kfd_locked);
61226103436SFelix Kuehling 	WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
61326103436SFelix Kuehling 	if (count == 0)
61426103436SFelix Kuehling 		ret = kfd_resume_all_processes();
61526103436SFelix Kuehling 
61626103436SFelix Kuehling 	return ret;
6174ebc7182SYong Zhao }
6184ebc7182SYong Zhao 
619b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
620b8935a7cSYong Zhao {
621b8935a7cSYong Zhao 	int err = 0;
622b8935a7cSYong Zhao 
62364d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
62464d1c3a4SFelix Kuehling 	if (err) {
62564d1c3a4SFelix Kuehling 		dev_err(kfd_device,
62664d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
62764d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
62864d1c3a4SFelix Kuehling 		return err;
62964d1c3a4SFelix Kuehling 	}
630733fa1f7SYong Zhao 
631b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
632b8935a7cSYong Zhao 	if (err) {
633b8935a7cSYong Zhao 		dev_err(kfd_device,
634b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
635b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
636b8935a7cSYong Zhao 		goto dqm_start_error;
637b17f068aSOded Gabbay 	}
638b17f068aSOded Gabbay 
639b8935a7cSYong Zhao 	return err;
640b8935a7cSYong Zhao 
641b8935a7cSYong Zhao dqm_start_error:
64264d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
643b8935a7cSYong Zhao 	return err;
6444a488a7aSOded Gabbay }
6454a488a7aSOded Gabbay 
646b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
647b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
6484a488a7aSOded Gabbay {
64958e69886SLan Xiao 	uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
65058e69886SLan Xiao 	bool is_patched = false;
65158e69886SLan Xiao 
6522249d558SAndrew Lewycky 	if (!kfd->init_complete)
6532249d558SAndrew Lewycky 		return;
6542249d558SAndrew Lewycky 
65558e69886SLan Xiao 	if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
65658e69886SLan Xiao 		dev_err_once(kfd_device, "Ring entry too small\n");
65758e69886SLan Xiao 		return;
65858e69886SLan Xiao 	}
65958e69886SLan Xiao 
6602249d558SAndrew Lewycky 	spin_lock(&kfd->interrupt_lock);
6612249d558SAndrew Lewycky 
6622249d558SAndrew Lewycky 	if (kfd->interrupts_active
66358e69886SLan Xiao 	    && interrupt_is_wanted(kfd, ih_ring_entry,
66458e69886SLan Xiao 				   patched_ihre, &is_patched)
66558e69886SLan Xiao 	    && enqueue_ih_ring_entry(kfd,
66658e69886SLan Xiao 				     is_patched ? patched_ihre : ih_ring_entry))
66748e876a2SAndres Rodriguez 		queue_work(kfd->ih_wq, &kfd->interrupt_work);
6682249d558SAndrew Lewycky 
6692249d558SAndrew Lewycky 	spin_unlock(&kfd->interrupt_lock);
6704a488a7aSOded Gabbay }
6716e81090bSOded Gabbay 
6726b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
6736b95e797SFelix Kuehling {
6746b95e797SFelix Kuehling 	struct kfd_process *p;
6756b95e797SFelix Kuehling 	int r;
6766b95e797SFelix Kuehling 
6776b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
6786b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
6796b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
6806b95e797SFelix Kuehling 	 */
6816b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
6826b95e797SFelix Kuehling 	if (!p)
6836b95e797SFelix Kuehling 		return -ESRCH;
6846b95e797SFelix Kuehling 
6856b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
6866b95e797SFelix Kuehling 
6876b95e797SFelix Kuehling 	kfd_unref_process(p);
6886b95e797SFelix Kuehling 	return r;
6896b95e797SFelix Kuehling }
6906b95e797SFelix Kuehling 
6916b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
6926b95e797SFelix Kuehling {
6936b95e797SFelix Kuehling 	struct kfd_process *p;
6946b95e797SFelix Kuehling 	int r;
6956b95e797SFelix Kuehling 
6966b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
6976b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
6986b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
6996b95e797SFelix Kuehling 	 */
7006b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
7016b95e797SFelix Kuehling 	if (!p)
7026b95e797SFelix Kuehling 		return -ESRCH;
7036b95e797SFelix Kuehling 
7046b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
7056b95e797SFelix Kuehling 
7066b95e797SFelix Kuehling 	kfd_unref_process(p);
7076b95e797SFelix Kuehling 	return r;
7086b95e797SFelix Kuehling }
7096b95e797SFelix Kuehling 
71026103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
71126103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
71226103436SFelix Kuehling  *   process.
71326103436SFelix Kuehling  *
71426103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
71526103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
71626103436SFelix Kuehling  *
71726103436SFelix Kuehling  */
71826103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
71926103436SFelix Kuehling 					       struct dma_fence *fence)
72026103436SFelix Kuehling {
72126103436SFelix Kuehling 	struct kfd_process *p;
72226103436SFelix Kuehling 	unsigned long active_time;
72326103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
72426103436SFelix Kuehling 
72526103436SFelix Kuehling 	if (!fence)
72626103436SFelix Kuehling 		return -EINVAL;
72726103436SFelix Kuehling 
72826103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
72926103436SFelix Kuehling 		return 0;
73026103436SFelix Kuehling 
73126103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
73226103436SFelix Kuehling 	if (!p)
73326103436SFelix Kuehling 		return -ENODEV;
73426103436SFelix Kuehling 
73526103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
73626103436SFelix Kuehling 		goto out;
73726103436SFelix Kuehling 
73826103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
73926103436SFelix Kuehling 
74026103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
74126103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
74226103436SFelix Kuehling 	 */
74326103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
74426103436SFelix Kuehling 	if (delay_jiffies > active_time)
74526103436SFelix Kuehling 		delay_jiffies -= active_time;
74626103436SFelix Kuehling 	else
74726103436SFelix Kuehling 		delay_jiffies = 0;
74826103436SFelix Kuehling 
74926103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
75026103436SFelix Kuehling 	 * to kfd_evict_bo_worker
75126103436SFelix Kuehling 	 */
75226103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
75326103436SFelix Kuehling out:
75426103436SFelix Kuehling 	kfd_unref_process(p);
75526103436SFelix Kuehling 	return 0;
75626103436SFelix Kuehling }
75726103436SFelix Kuehling 
7586e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
7596e81090bSOded Gabbay 				unsigned int chunk_size)
7606e81090bSOded Gabbay {
7618625ff9cSFelix Kuehling 	unsigned int num_of_longs;
7626e81090bSOded Gabbay 
76332fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
76432fa8219SFelix Kuehling 		return -EINVAL;
76532fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
76632fa8219SFelix Kuehling 		return -EINVAL;
76732fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
76832fa8219SFelix Kuehling 		return -EINVAL;
7696e81090bSOded Gabbay 
7706e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
7716e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
7726e81090bSOded Gabbay 
7738625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
7748625ff9cSFelix Kuehling 		BITS_PER_LONG;
7756e81090bSOded Gabbay 
7768625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
7776e81090bSOded Gabbay 
7786e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
7796e81090bSOded Gabbay 		return -ENOMEM;
7806e81090bSOded Gabbay 
78179775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
7826e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
7836e81090bSOded Gabbay 
7846e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
7856e81090bSOded Gabbay 
7866e81090bSOded Gabbay 	return 0;
7876e81090bSOded Gabbay 
7886e81090bSOded Gabbay }
7896e81090bSOded Gabbay 
7906e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
7916e81090bSOded Gabbay {
7926e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
7936e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
7946e81090bSOded Gabbay }
7956e81090bSOded Gabbay 
7966e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
7976e81090bSOded Gabbay 						unsigned int bit_num,
7986e81090bSOded Gabbay 						unsigned int chunk_size)
7996e81090bSOded Gabbay {
8006e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
8016e81090bSOded Gabbay }
8026e81090bSOded Gabbay 
8036e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
8046e81090bSOded Gabbay 						unsigned int bit_num,
8056e81090bSOded Gabbay 						unsigned int chunk_size)
8066e81090bSOded Gabbay {
8076e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
8086e81090bSOded Gabbay }
8096e81090bSOded Gabbay 
8106e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
8116e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
8126e81090bSOded Gabbay {
8136e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
8146e81090bSOded Gabbay 
8156e81090bSOded Gabbay 	if (size == 0)
8166e81090bSOded Gabbay 		return -EINVAL;
8176e81090bSOded Gabbay 
8186e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
8196e81090bSOded Gabbay 		return -ENOMEM;
8206e81090bSOded Gabbay 
8211cd106ecSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
8221cd106ecSFelix Kuehling 	if (!(*mem_obj))
8236e81090bSOded Gabbay 		return -ENOMEM;
8246e81090bSOded Gabbay 
82579775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
8266e81090bSOded Gabbay 
8276e81090bSOded Gabbay 	start_search = 0;
8286e81090bSOded Gabbay 
8296e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
8306e81090bSOded Gabbay 
8316e81090bSOded Gabbay kfd_gtt_restart_search:
8326e81090bSOded Gabbay 	/* Find the first chunk that is free */
8336e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
8346e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
8356e81090bSOded Gabbay 					start_search);
8366e81090bSOded Gabbay 
83779775b62SKent Russell 	pr_debug("Found = %d\n", found);
8386e81090bSOded Gabbay 
8396e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
8406e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
8416e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
8426e81090bSOded Gabbay 
8436e81090bSOded Gabbay 	/* Update fields of mem_obj */
8446e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
8456e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
8466e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
8476e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
8486e81090bSOded Gabbay 					found,
8496e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
8506e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
8516e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
8526e81090bSOded Gabbay 					found,
8536e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
8546e81090bSOded Gabbay 
85579775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
8566e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
8576e81090bSOded Gabbay 
8586e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
8596e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
86079775b62SKent Russell 		pr_debug("Single bit\n");
8616e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
8626e81090bSOded Gabbay 		goto kfd_gtt_out;
8636e81090bSOded Gabbay 	}
8646e81090bSOded Gabbay 
8656e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
8666e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
8676e81090bSOded Gabbay 	do {
8686e81090bSOded Gabbay 		(*mem_obj)->range_end =
8696e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
8706e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
8716e81090bSOded Gabbay 		/*
8726e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
8736e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
8746e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
8756e81090bSOded Gabbay 		 */
8766e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
8776e81090bSOded Gabbay 			start_search = found;
8786e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
8796e81090bSOded Gabbay 		}
8806e81090bSOded Gabbay 
8816e81090bSOded Gabbay 		/*
8826e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
8836e81090bSOded Gabbay 		 */
8846e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
8856e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
8866e81090bSOded Gabbay 
8876e81090bSOded Gabbay 		/* Check if we don't need another chunk */
8886e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
8896e81090bSOded Gabbay 			cur_size = 0;
8906e81090bSOded Gabbay 		else
8916e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
8926e81090bSOded Gabbay 
8936e81090bSOded Gabbay 	} while (cur_size > 0);
8946e81090bSOded Gabbay 
89579775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
8966e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
8976e81090bSOded Gabbay 
8986e81090bSOded Gabbay 	/* Mark the chunks as allocated */
8996e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
9006e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
9016e81090bSOded Gabbay 		found++)
9026e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
9036e81090bSOded Gabbay 
9046e81090bSOded Gabbay kfd_gtt_out:
9056e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
9066e81090bSOded Gabbay 	return 0;
9076e81090bSOded Gabbay 
9086e81090bSOded Gabbay kfd_gtt_no_free_chunk:
90979775b62SKent Russell 	pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
9106e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
9116e81090bSOded Gabbay 	kfree(mem_obj);
9126e81090bSOded Gabbay 	return -ENOMEM;
9136e81090bSOded Gabbay }
9146e81090bSOded Gabbay 
9156e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
9166e81090bSOded Gabbay {
9176e81090bSOded Gabbay 	unsigned int bit;
9186e81090bSOded Gabbay 
9199216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
9209216ed29SOded Gabbay 	if (!mem_obj)
9219216ed29SOded Gabbay 		return 0;
9226e81090bSOded Gabbay 
92379775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
9246e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
9256e81090bSOded Gabbay 
9266e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
9276e81090bSOded Gabbay 
9286e81090bSOded Gabbay 	/* Mark the chunks as free */
9296e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
9306e81090bSOded Gabbay 		bit <= mem_obj->range_end;
9316e81090bSOded Gabbay 		bit++)
9326e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
9336e81090bSOded Gabbay 
9346e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
9356e81090bSOded Gabbay 
9366e81090bSOded Gabbay 	kfree(mem_obj);
9376e81090bSOded Gabbay 	return 0;
9386e81090bSOded Gabbay }
939a29ec470SShaoyun Liu 
940a29ec470SShaoyun Liu #if defined(CONFIG_DEBUG_FS)
941a29ec470SShaoyun Liu 
942a29ec470SShaoyun Liu /* This function will send a package to HIQ to hang the HWS
943a29ec470SShaoyun Liu  * which will trigger a GPU reset and bring the HWS back to normal state
944a29ec470SShaoyun Liu  */
945a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev)
946a29ec470SShaoyun Liu {
947a29ec470SShaoyun Liu 	int r = 0;
948a29ec470SShaoyun Liu 
949a29ec470SShaoyun Liu 	if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
950a29ec470SShaoyun Liu 		pr_err("HWS is not enabled");
951a29ec470SShaoyun Liu 		return -EINVAL;
952a29ec470SShaoyun Liu 	}
953a29ec470SShaoyun Liu 
954a29ec470SShaoyun Liu 	r = pm_debugfs_hang_hws(&dev->dqm->packets);
955a29ec470SShaoyun Liu 	if (!r)
956a29ec470SShaoyun Liu 		r = dqm_debugfs_execute_queues(dev->dqm);
957a29ec470SShaoyun Liu 
958a29ec470SShaoyun Liu 	return r;
959a29ec470SShaoyun Liu }
960a29ec470SShaoyun Liu 
961a29ec470SShaoyun Liu #endif
962