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 
2364d1c3a4SFelix Kuehling #if defined(CONFIG_AMD_IOMMU_V2_MODULE) || defined(CONFIG_AMD_IOMMU_V2)
244a488a7aSOded Gabbay #include <linux/amd-iommu.h>
2564d1c3a4SFelix Kuehling #endif
264a488a7aSOded Gabbay #include <linux/bsearch.h>
274a488a7aSOded Gabbay #include <linux/pci.h>
284a488a7aSOded Gabbay #include <linux/slab.h>
294a488a7aSOded Gabbay #include "kfd_priv.h"
3064c7f8cfSBen Goz #include "kfd_device_queue_manager.h"
31507968ddSFelix Kuehling #include "kfd_pm4_headers_vi.h"
32373d7080SFelix Kuehling #include "cwsr_trap_handler_gfx8.asm"
33*3e76c239SFelix Kuehling #include "cwsr_trap_handler_gfx9.asm"
3464d1c3a4SFelix Kuehling #include "kfd_iommu.h"
354a488a7aSOded Gabbay 
3619f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768
3726103436SFelix Kuehling static atomic_t kfd_device_suspended = ATOMIC_INIT(0);
3819f6d2a6SOded Gabbay 
3964d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
404a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = {
410da7558cSBen Goz 	.asic_family = CHIP_KAVERI,
420da7558cSBen Goz 	.max_pasid_bits = 16,
43992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
44992839adSYair Shachar 	.max_no_of_hqd	= 24,
45ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
460da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
47f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
48fbeb661bSYair Shachar 	.num_of_watch_points = 4,
49373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
50373d7080SFelix Kuehling 	.supports_cwsr = false,
5164d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
523ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
530da7558cSBen Goz };
540da7558cSBen Goz 
550da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
560da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
574a488a7aSOded Gabbay 	.max_pasid_bits = 16,
58eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
59eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
60ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
61b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
62eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
63f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
64373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
65373d7080SFelix Kuehling 	.supports_cwsr = true,
6664d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
673ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
684a488a7aSOded Gabbay };
6964d1c3a4SFelix Kuehling #endif
704a488a7aSOded Gabbay 
71a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
72a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
73a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
74a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
75a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
76ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
77a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
78a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
79a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
80a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
81a3084e6cSFelix Kuehling 	.supports_cwsr = false,
8264d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
83a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
84a3084e6cSFelix Kuehling };
85a3084e6cSFelix Kuehling 
86a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
87a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
88a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
89a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
90ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
91a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
92a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
93a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
94a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
95a3084e6cSFelix Kuehling 	.supports_cwsr = false,
9664d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
97a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
98a3084e6cSFelix Kuehling };
99a3084e6cSFelix Kuehling 
100a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_vf_device_info = {
101a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
102a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
103a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
104ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
105a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
106a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
107a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
108a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
109a3084e6cSFelix Kuehling 	.supports_cwsr = false,
11064d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
111a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
112a3084e6cSFelix Kuehling };
113a3084e6cSFelix Kuehling 
114a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
115a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
116a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
117a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
118ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
119a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
120a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
121a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
122a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
123a3084e6cSFelix Kuehling 	.supports_cwsr = true,
12464d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
125a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
126a3084e6cSFelix Kuehling };
127a3084e6cSFelix Kuehling 
128a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
129a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
130a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
131a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
132ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
133a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
134a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
135a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
136a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
137a3084e6cSFelix Kuehling 	.supports_cwsr = true,
13864d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
139a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
140a3084e6cSFelix Kuehling };
141a3084e6cSFelix Kuehling 
142a3084e6cSFelix Kuehling 
143a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
144a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
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 = true,
155a3084e6cSFelix Kuehling };
156a3084e6cSFelix Kuehling 
157a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = {
158a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
159a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
160a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
161ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
162a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
163a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
164a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
165a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
166a3084e6cSFelix Kuehling 	.supports_cwsr = true,
16764d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
168a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
169a3084e6cSFelix Kuehling };
170a3084e6cSFelix Kuehling 
171a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = {
172a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
173a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
174a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
175ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
176a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
177a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
178a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
179a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
180a3084e6cSFelix Kuehling 	.supports_cwsr = true,
18164d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
182a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
183a3084e6cSFelix Kuehling };
184a3084e6cSFelix Kuehling 
185a3084e6cSFelix Kuehling 
1864a488a7aSOded Gabbay struct kfd_deviceid {
1874a488a7aSOded Gabbay 	unsigned short did;
1884a488a7aSOded Gabbay 	const struct kfd_device_info *device_info;
1894a488a7aSOded Gabbay };
1904a488a7aSOded Gabbay 
1914a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = {
19264d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
1934a488a7aSOded Gabbay 	{ 0x1304, &kaveri_device_info },	/* Kaveri */
1944a488a7aSOded Gabbay 	{ 0x1305, &kaveri_device_info },	/* Kaveri */
1954a488a7aSOded Gabbay 	{ 0x1306, &kaveri_device_info },	/* Kaveri */
1964a488a7aSOded Gabbay 	{ 0x1307, &kaveri_device_info },	/* Kaveri */
1974a488a7aSOded Gabbay 	{ 0x1309, &kaveri_device_info },	/* Kaveri */
1984a488a7aSOded Gabbay 	{ 0x130A, &kaveri_device_info },	/* Kaveri */
1994a488a7aSOded Gabbay 	{ 0x130B, &kaveri_device_info },	/* Kaveri */
2004a488a7aSOded Gabbay 	{ 0x130C, &kaveri_device_info },	/* Kaveri */
2014a488a7aSOded Gabbay 	{ 0x130D, &kaveri_device_info },	/* Kaveri */
2024a488a7aSOded Gabbay 	{ 0x130E, &kaveri_device_info },	/* Kaveri */
2034a488a7aSOded Gabbay 	{ 0x130F, &kaveri_device_info },	/* Kaveri */
2044a488a7aSOded Gabbay 	{ 0x1310, &kaveri_device_info },	/* Kaveri */
2054a488a7aSOded Gabbay 	{ 0x1311, &kaveri_device_info },	/* Kaveri */
2064a488a7aSOded Gabbay 	{ 0x1312, &kaveri_device_info },	/* Kaveri */
2074a488a7aSOded Gabbay 	{ 0x1313, &kaveri_device_info },	/* Kaveri */
2084a488a7aSOded Gabbay 	{ 0x1315, &kaveri_device_info },	/* Kaveri */
2094a488a7aSOded Gabbay 	{ 0x1316, &kaveri_device_info },	/* Kaveri */
2104a488a7aSOded Gabbay 	{ 0x1317, &kaveri_device_info },	/* Kaveri */
2114a488a7aSOded Gabbay 	{ 0x1318, &kaveri_device_info },	/* Kaveri */
2124a488a7aSOded Gabbay 	{ 0x131B, &kaveri_device_info },	/* Kaveri */
2134a488a7aSOded Gabbay 	{ 0x131C, &kaveri_device_info },	/* Kaveri */
214123576d1SBen Goz 	{ 0x131D, &kaveri_device_info },	/* Kaveri */
215123576d1SBen Goz 	{ 0x9870, &carrizo_device_info },	/* Carrizo */
216123576d1SBen Goz 	{ 0x9874, &carrizo_device_info },	/* Carrizo */
217123576d1SBen Goz 	{ 0x9875, &carrizo_device_info },	/* Carrizo */
218123576d1SBen Goz 	{ 0x9876, &carrizo_device_info },	/* Carrizo */
219a3084e6cSFelix Kuehling 	{ 0x9877, &carrizo_device_info },	/* Carrizo */
22064d1c3a4SFelix Kuehling #endif
221a3084e6cSFelix Kuehling 	{ 0x67A0, &hawaii_device_info },	/* Hawaii */
222a3084e6cSFelix Kuehling 	{ 0x67A1, &hawaii_device_info },	/* Hawaii */
223a3084e6cSFelix Kuehling 	{ 0x67A2, &hawaii_device_info },	/* Hawaii */
224a3084e6cSFelix Kuehling 	{ 0x67A8, &hawaii_device_info },	/* Hawaii */
225a3084e6cSFelix Kuehling 	{ 0x67A9, &hawaii_device_info },	/* Hawaii */
226a3084e6cSFelix Kuehling 	{ 0x67AA, &hawaii_device_info },	/* Hawaii */
227a3084e6cSFelix Kuehling 	{ 0x67B0, &hawaii_device_info },	/* Hawaii */
228a3084e6cSFelix Kuehling 	{ 0x67B1, &hawaii_device_info },	/* Hawaii */
229a3084e6cSFelix Kuehling 	{ 0x67B8, &hawaii_device_info },	/* Hawaii */
230a3084e6cSFelix Kuehling 	{ 0x67B9, &hawaii_device_info },	/* Hawaii */
231a3084e6cSFelix Kuehling 	{ 0x67BA, &hawaii_device_info },	/* Hawaii */
232a3084e6cSFelix Kuehling 	{ 0x67BE, &hawaii_device_info },	/* Hawaii */
233a3084e6cSFelix Kuehling 	{ 0x6920, &tonga_device_info },		/* Tonga */
234a3084e6cSFelix Kuehling 	{ 0x6921, &tonga_device_info },		/* Tonga */
235a3084e6cSFelix Kuehling 	{ 0x6928, &tonga_device_info },		/* Tonga */
236a3084e6cSFelix Kuehling 	{ 0x6929, &tonga_device_info },		/* Tonga */
237a3084e6cSFelix Kuehling 	{ 0x692B, &tonga_device_info },		/* Tonga */
238a3084e6cSFelix Kuehling 	{ 0x692F, &tonga_vf_device_info },	/* Tonga vf */
239a3084e6cSFelix Kuehling 	{ 0x6938, &tonga_device_info },		/* Tonga */
240a3084e6cSFelix Kuehling 	{ 0x6939, &tonga_device_info },		/* Tonga */
241a3084e6cSFelix Kuehling 	{ 0x7300, &fiji_device_info },		/* Fiji */
242a3084e6cSFelix Kuehling 	{ 0x730F, &fiji_vf_device_info },	/* Fiji vf*/
243a3084e6cSFelix Kuehling 	{ 0x67C0, &polaris10_device_info },	/* Polaris10 */
244a3084e6cSFelix Kuehling 	{ 0x67C1, &polaris10_device_info },	/* Polaris10 */
245a3084e6cSFelix Kuehling 	{ 0x67C2, &polaris10_device_info },	/* Polaris10 */
246a3084e6cSFelix Kuehling 	{ 0x67C4, &polaris10_device_info },	/* Polaris10 */
247a3084e6cSFelix Kuehling 	{ 0x67C7, &polaris10_device_info },	/* Polaris10 */
248a3084e6cSFelix Kuehling 	{ 0x67C8, &polaris10_device_info },	/* Polaris10 */
249a3084e6cSFelix Kuehling 	{ 0x67C9, &polaris10_device_info },	/* Polaris10 */
250a3084e6cSFelix Kuehling 	{ 0x67CA, &polaris10_device_info },	/* Polaris10 */
251a3084e6cSFelix Kuehling 	{ 0x67CC, &polaris10_device_info },	/* Polaris10 */
252a3084e6cSFelix Kuehling 	{ 0x67CF, &polaris10_device_info },	/* Polaris10 */
253a3084e6cSFelix Kuehling 	{ 0x67D0, &polaris10_vf_device_info },	/* Polaris10 vf*/
254a3084e6cSFelix Kuehling 	{ 0x67DF, &polaris10_device_info },	/* Polaris10 */
255a3084e6cSFelix Kuehling 	{ 0x67E0, &polaris11_device_info },	/* Polaris11 */
256a3084e6cSFelix Kuehling 	{ 0x67E1, &polaris11_device_info },	/* Polaris11 */
257a3084e6cSFelix Kuehling 	{ 0x67E3, &polaris11_device_info },	/* Polaris11 */
258a3084e6cSFelix Kuehling 	{ 0x67E7, &polaris11_device_info },	/* Polaris11 */
259a3084e6cSFelix Kuehling 	{ 0x67E8, &polaris11_device_info },	/* Polaris11 */
260a3084e6cSFelix Kuehling 	{ 0x67E9, &polaris11_device_info },	/* Polaris11 */
261a3084e6cSFelix Kuehling 	{ 0x67EB, &polaris11_device_info },	/* Polaris11 */
262a3084e6cSFelix Kuehling 	{ 0x67EF, &polaris11_device_info },	/* Polaris11 */
263a3084e6cSFelix Kuehling 	{ 0x67FF, &polaris11_device_info },	/* Polaris11 */
2644a488a7aSOded Gabbay };
2654a488a7aSOded Gabbay 
2666e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
2676e81090bSOded Gabbay 				unsigned int chunk_size);
2686e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
2696e81090bSOded Gabbay 
270b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
271b8935a7cSYong Zhao 
2724a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did)
2734a488a7aSOded Gabbay {
2744a488a7aSOded Gabbay 	size_t i;
2754a488a7aSOded Gabbay 
2764a488a7aSOded Gabbay 	for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
2774a488a7aSOded Gabbay 		if (supported_devices[i].did == did) {
27832fa8219SFelix Kuehling 			WARN_ON(!supported_devices[i].device_info);
2794a488a7aSOded Gabbay 			return supported_devices[i].device_info;
2804a488a7aSOded Gabbay 		}
2814a488a7aSOded Gabbay 	}
2824a488a7aSOded Gabbay 
2834ebc7182SYong Zhao 	dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
2844ebc7182SYong Zhao 		 did);
2854ebc7182SYong Zhao 
2864a488a7aSOded Gabbay 	return NULL;
2874a488a7aSOded Gabbay }
2884a488a7aSOded Gabbay 
289cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
290cea405b1SXihan Zhang 	struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
2914a488a7aSOded Gabbay {
2924a488a7aSOded Gabbay 	struct kfd_dev *kfd;
2934a488a7aSOded Gabbay 
2944a488a7aSOded Gabbay 	const struct kfd_device_info *device_info =
2954a488a7aSOded Gabbay 					lookup_device_info(pdev->device);
2964a488a7aSOded Gabbay 
2974ebc7182SYong Zhao 	if (!device_info) {
2984ebc7182SYong Zhao 		dev_err(kfd_device, "kgd2kfd_probe failed\n");
2994a488a7aSOded Gabbay 		return NULL;
3004ebc7182SYong Zhao 	}
3014a488a7aSOded Gabbay 
3023ee2d00cSFelix Kuehling 	if (device_info->needs_pci_atomics) {
3033ee2d00cSFelix Kuehling 		/* Allow BIF to recode atomics to PCIe 3.0
3043ee2d00cSFelix Kuehling 		 * AtomicOps. 32 and 64-bit requests are possible and
3053ee2d00cSFelix Kuehling 		 * must be supported.
3063ee2d00cSFelix Kuehling 		 */
3073ee2d00cSFelix Kuehling 		if (pci_enable_atomic_ops_to_root(pdev,
3083ee2d00cSFelix Kuehling 				PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3093ee2d00cSFelix Kuehling 				PCI_EXP_DEVCAP2_ATOMIC_COMP64) < 0) {
3103ee2d00cSFelix Kuehling 			dev_info(kfd_device,
3113ee2d00cSFelix Kuehling 				"skipped device %x:%x, PCI rejects atomics",
3123ee2d00cSFelix Kuehling 				 pdev->vendor, pdev->device);
3133ee2d00cSFelix Kuehling 			return NULL;
3143ee2d00cSFelix Kuehling 		}
3153ee2d00cSFelix Kuehling 	}
3163ee2d00cSFelix Kuehling 
3174a488a7aSOded Gabbay 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
3184a488a7aSOded Gabbay 	if (!kfd)
3194a488a7aSOded Gabbay 		return NULL;
3204a488a7aSOded Gabbay 
3214a488a7aSOded Gabbay 	kfd->kgd = kgd;
3224a488a7aSOded Gabbay 	kfd->device_info = device_info;
3234a488a7aSOded Gabbay 	kfd->pdev = pdev;
32419f6d2a6SOded Gabbay 	kfd->init_complete = false;
325cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
326cea405b1SXihan Zhang 
327cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
328cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
329cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
3304a488a7aSOded Gabbay 
3314a488a7aSOded Gabbay 	return kfd;
3324a488a7aSOded Gabbay }
3334a488a7aSOded Gabbay 
334373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
335373d7080SFelix Kuehling {
336373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
337*3e76c239SFelix Kuehling 		if (kfd->device_info->asic_family < CHIP_VEGA10) {
338373d7080SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
339373d7080SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx8_hex;
340373d7080SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
341*3e76c239SFelix Kuehling 		} else {
342*3e76c239SFelix Kuehling 			BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
343*3e76c239SFelix Kuehling 			kfd->cwsr_isa = cwsr_trap_gfx9_hex;
344*3e76c239SFelix Kuehling 			kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
345*3e76c239SFelix Kuehling 		}
346*3e76c239SFelix Kuehling 
347373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
348373d7080SFelix Kuehling 	}
349373d7080SFelix Kuehling }
350373d7080SFelix Kuehling 
3514a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
3524a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
3534a488a7aSOded Gabbay {
35419f6d2a6SOded Gabbay 	unsigned int size;
35519f6d2a6SOded Gabbay 
3564a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
3574a488a7aSOded Gabbay 
35844008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
35944008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
36044008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
36144008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
36244008d7aSYong Zhao 
363a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
364a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
365a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
366a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
367a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
368a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
369a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
370a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
371a99c6d4fSFelix Kuehling 	} else
372a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
373a99c6d4fSFelix Kuehling 
37419f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
375b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
37619f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
37719f6d2a6SOded Gabbay 
378e18e794eSOded Gabbay 	/*
379e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
380e18e794eSOded Gabbay 	 * There can be only 2 packets at once
381e18e794eSOded Gabbay 	 */
382507968ddSFelix Kuehling 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
383507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
384507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
385e18e794eSOded Gabbay 
386e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
387e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
388e18e794eSOded Gabbay 
389e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
39019f6d2a6SOded Gabbay 	size += 512 * 1024;
39119f6d2a6SOded Gabbay 
392cea405b1SXihan Zhang 	if (kfd->kfd2kgd->init_gtt_mem_allocation(
393cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
39473a1da0bSOded Gabbay 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
39579775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
39619f6d2a6SOded Gabbay 		goto out;
39719f6d2a6SOded Gabbay 	}
39819f6d2a6SOded Gabbay 
39979775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
400e18e794eSOded Gabbay 
40173a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
40273a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
40379775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
40473a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
40573a1da0bSOded Gabbay 	}
40673a1da0bSOded Gabbay 
407735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
408735df2baSFelix Kuehling 		dev_err(kfd_device,
409735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
410735df2baSFelix Kuehling 		goto kfd_doorbell_error;
411735df2baSFelix Kuehling 	}
41219f6d2a6SOded Gabbay 
4134eacc26bSKent Russell 	if (kfd_topology_add_device(kfd)) {
41479775b62SKent Russell 		dev_err(kfd_device, "Error adding device to topology\n");
41519f6d2a6SOded Gabbay 		goto kfd_topology_add_device_error;
41619f6d2a6SOded Gabbay 	}
41719f6d2a6SOded Gabbay 
4182249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
41979775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
4202249d558SAndrew Lewycky 		goto kfd_interrupt_error;
4212249d558SAndrew Lewycky 	}
4222249d558SAndrew Lewycky 
42364c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
42464c7f8cfSBen Goz 	if (!kfd->dqm) {
42579775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
42664c7f8cfSBen Goz 		goto device_queue_manager_error;
42764c7f8cfSBen Goz 	}
42864c7f8cfSBen Goz 
42964d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
43064d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
43164d1c3a4SFelix Kuehling 		goto device_iommu_error;
43264c7f8cfSBen Goz 	}
43364c7f8cfSBen Goz 
434373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
435373d7080SFelix Kuehling 
436b8935a7cSYong Zhao 	if (kfd_resume(kfd))
437b8935a7cSYong Zhao 		goto kfd_resume_error;
438b8935a7cSYong Zhao 
439fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
440fbeb661bSYair Shachar 
4414a488a7aSOded Gabbay 	kfd->init_complete = true;
44279775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
4434a488a7aSOded Gabbay 		 kfd->pdev->device);
4444a488a7aSOded Gabbay 
44579775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
446d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
44764c7f8cfSBen Goz 
44819f6d2a6SOded Gabbay 	goto out;
44919f6d2a6SOded Gabbay 
450b8935a7cSYong Zhao kfd_resume_error:
45164d1c3a4SFelix Kuehling device_iommu_error:
45264c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
45364c7f8cfSBen Goz device_queue_manager_error:
4542249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
4552249d558SAndrew Lewycky kfd_interrupt_error:
456b17f068aSOded Gabbay 	kfd_topology_remove_device(kfd);
45719f6d2a6SOded Gabbay kfd_topology_add_device_error:
458735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
459735df2baSFelix Kuehling kfd_doorbell_error:
46073a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
46173a1da0bSOded Gabbay kfd_gtt_sa_init_error:
462cea405b1SXihan Zhang 	kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
46319f6d2a6SOded Gabbay 	dev_err(kfd_device,
46479775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
46519f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
46619f6d2a6SOded Gabbay out:
46719f6d2a6SOded Gabbay 	return kfd->init_complete;
4684a488a7aSOded Gabbay }
4694a488a7aSOded Gabbay 
4704a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
4714a488a7aSOded Gabbay {
472b17f068aSOded Gabbay 	if (kfd->init_complete) {
473b8935a7cSYong Zhao 		kgd2kfd_suspend(kfd);
47464c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
4752249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
47619f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
477735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
47873a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
479cea405b1SXihan Zhang 		kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
480b17f068aSOded Gabbay 	}
4815b5c4e40SEvgeny Pinchuk 
4824a488a7aSOded Gabbay 	kfree(kfd);
4834a488a7aSOded Gabbay }
4844a488a7aSOded Gabbay 
4854a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd)
4864a488a7aSOded Gabbay {
487733fa1f7SYong Zhao 	if (!kfd->init_complete)
488733fa1f7SYong Zhao 		return;
489733fa1f7SYong Zhao 
49026103436SFelix Kuehling 	/* For first KFD device suspend all the KFD processes */
49126103436SFelix Kuehling 	if (atomic_inc_return(&kfd_device_suspended) == 1)
49226103436SFelix Kuehling 		kfd_suspend_all_processes();
49326103436SFelix Kuehling 
49445c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
495733fa1f7SYong Zhao 
49664d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
4974a488a7aSOded Gabbay }
4984a488a7aSOded Gabbay 
4994a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd)
5004a488a7aSOded Gabbay {
50126103436SFelix Kuehling 	int ret, count;
50226103436SFelix Kuehling 
503b8935a7cSYong Zhao 	if (!kfd->init_complete)
504b8935a7cSYong Zhao 		return 0;
505b17f068aSOded Gabbay 
50626103436SFelix Kuehling 	ret = kfd_resume(kfd);
50726103436SFelix Kuehling 	if (ret)
50826103436SFelix Kuehling 		return ret;
509b17f068aSOded Gabbay 
51026103436SFelix Kuehling 	count = atomic_dec_return(&kfd_device_suspended);
51126103436SFelix Kuehling 	WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
51226103436SFelix Kuehling 	if (count == 0)
51326103436SFelix Kuehling 		ret = kfd_resume_all_processes();
51426103436SFelix Kuehling 
51526103436SFelix Kuehling 	return ret;
5164ebc7182SYong Zhao }
5174ebc7182SYong Zhao 
518b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
519b8935a7cSYong Zhao {
520b8935a7cSYong Zhao 	int err = 0;
521b8935a7cSYong Zhao 
52264d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
52364d1c3a4SFelix Kuehling 	if (err) {
52464d1c3a4SFelix Kuehling 		dev_err(kfd_device,
52564d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
52664d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
52764d1c3a4SFelix Kuehling 		return err;
52864d1c3a4SFelix Kuehling 	}
529733fa1f7SYong Zhao 
530b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
531b8935a7cSYong Zhao 	if (err) {
532b8935a7cSYong Zhao 		dev_err(kfd_device,
533b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
534b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
535b8935a7cSYong Zhao 		goto dqm_start_error;
536b17f068aSOded Gabbay 	}
537b17f068aSOded Gabbay 
538b8935a7cSYong Zhao 	return err;
539b8935a7cSYong Zhao 
540b8935a7cSYong Zhao dqm_start_error:
54164d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
542b8935a7cSYong Zhao 	return err;
5434a488a7aSOded Gabbay }
5444a488a7aSOded Gabbay 
545b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
546b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
5474a488a7aSOded Gabbay {
5482249d558SAndrew Lewycky 	if (!kfd->init_complete)
5492249d558SAndrew Lewycky 		return;
5502249d558SAndrew Lewycky 
5512249d558SAndrew Lewycky 	spin_lock(&kfd->interrupt_lock);
5522249d558SAndrew Lewycky 
5532249d558SAndrew Lewycky 	if (kfd->interrupts_active
5542249d558SAndrew Lewycky 	    && interrupt_is_wanted(kfd, ih_ring_entry)
5552249d558SAndrew Lewycky 	    && enqueue_ih_ring_entry(kfd, ih_ring_entry))
55648e876a2SAndres Rodriguez 		queue_work(kfd->ih_wq, &kfd->interrupt_work);
5572249d558SAndrew Lewycky 
5582249d558SAndrew Lewycky 	spin_unlock(&kfd->interrupt_lock);
5594a488a7aSOded Gabbay }
5606e81090bSOded Gabbay 
5616b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
5626b95e797SFelix Kuehling {
5636b95e797SFelix Kuehling 	struct kfd_process *p;
5646b95e797SFelix Kuehling 	int r;
5656b95e797SFelix Kuehling 
5666b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
5676b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
5686b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
5696b95e797SFelix Kuehling 	 */
5706b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
5716b95e797SFelix Kuehling 	if (!p)
5726b95e797SFelix Kuehling 		return -ESRCH;
5736b95e797SFelix Kuehling 
5746b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
5756b95e797SFelix Kuehling 
5766b95e797SFelix Kuehling 	kfd_unref_process(p);
5776b95e797SFelix Kuehling 	return r;
5786b95e797SFelix Kuehling }
5796b95e797SFelix Kuehling 
5806b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
5816b95e797SFelix Kuehling {
5826b95e797SFelix Kuehling 	struct kfd_process *p;
5836b95e797SFelix Kuehling 	int r;
5846b95e797SFelix Kuehling 
5856b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
5866b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
5876b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
5886b95e797SFelix Kuehling 	 */
5896b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
5906b95e797SFelix Kuehling 	if (!p)
5916b95e797SFelix Kuehling 		return -ESRCH;
5926b95e797SFelix Kuehling 
5936b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
5946b95e797SFelix Kuehling 
5956b95e797SFelix Kuehling 	kfd_unref_process(p);
5966b95e797SFelix Kuehling 	return r;
5976b95e797SFelix Kuehling }
5986b95e797SFelix Kuehling 
59926103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
60026103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
60126103436SFelix Kuehling  *   process.
60226103436SFelix Kuehling  *
60326103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
60426103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
60526103436SFelix Kuehling  *
60626103436SFelix Kuehling  */
60726103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
60826103436SFelix Kuehling 					       struct dma_fence *fence)
60926103436SFelix Kuehling {
61026103436SFelix Kuehling 	struct kfd_process *p;
61126103436SFelix Kuehling 	unsigned long active_time;
61226103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
61326103436SFelix Kuehling 
61426103436SFelix Kuehling 	if (!fence)
61526103436SFelix Kuehling 		return -EINVAL;
61626103436SFelix Kuehling 
61726103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
61826103436SFelix Kuehling 		return 0;
61926103436SFelix Kuehling 
62026103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
62126103436SFelix Kuehling 	if (!p)
62226103436SFelix Kuehling 		return -ENODEV;
62326103436SFelix Kuehling 
62426103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
62526103436SFelix Kuehling 		goto out;
62626103436SFelix Kuehling 
62726103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
62826103436SFelix Kuehling 
62926103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
63026103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
63126103436SFelix Kuehling 	 */
63226103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
63326103436SFelix Kuehling 	if (delay_jiffies > active_time)
63426103436SFelix Kuehling 		delay_jiffies -= active_time;
63526103436SFelix Kuehling 	else
63626103436SFelix Kuehling 		delay_jiffies = 0;
63726103436SFelix Kuehling 
63826103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
63926103436SFelix Kuehling 	 * to kfd_evict_bo_worker
64026103436SFelix Kuehling 	 */
64126103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
64226103436SFelix Kuehling out:
64326103436SFelix Kuehling 	kfd_unref_process(p);
64426103436SFelix Kuehling 	return 0;
64526103436SFelix Kuehling }
64626103436SFelix Kuehling 
6476e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
6486e81090bSOded Gabbay 				unsigned int chunk_size)
6496e81090bSOded Gabbay {
6508625ff9cSFelix Kuehling 	unsigned int num_of_longs;
6516e81090bSOded Gabbay 
65232fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
65332fa8219SFelix Kuehling 		return -EINVAL;
65432fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
65532fa8219SFelix Kuehling 		return -EINVAL;
65632fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
65732fa8219SFelix Kuehling 		return -EINVAL;
6586e81090bSOded Gabbay 
6596e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
6606e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
6616e81090bSOded Gabbay 
6628625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
6638625ff9cSFelix Kuehling 		BITS_PER_LONG;
6646e81090bSOded Gabbay 
6658625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
6666e81090bSOded Gabbay 
6676e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
6686e81090bSOded Gabbay 		return -ENOMEM;
6696e81090bSOded Gabbay 
67079775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
6716e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
6726e81090bSOded Gabbay 
6736e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
6746e81090bSOded Gabbay 
6756e81090bSOded Gabbay 	return 0;
6766e81090bSOded Gabbay 
6776e81090bSOded Gabbay }
6786e81090bSOded Gabbay 
6796e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
6806e81090bSOded Gabbay {
6816e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
6826e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
6836e81090bSOded Gabbay }
6846e81090bSOded Gabbay 
6856e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
6866e81090bSOded Gabbay 						unsigned int bit_num,
6876e81090bSOded Gabbay 						unsigned int chunk_size)
6886e81090bSOded Gabbay {
6896e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
6906e81090bSOded Gabbay }
6916e81090bSOded Gabbay 
6926e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
6936e81090bSOded Gabbay 						unsigned int bit_num,
6946e81090bSOded Gabbay 						unsigned int chunk_size)
6956e81090bSOded Gabbay {
6966e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
6976e81090bSOded Gabbay }
6986e81090bSOded Gabbay 
6996e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
7006e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
7016e81090bSOded Gabbay {
7026e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
7036e81090bSOded Gabbay 
7046e81090bSOded Gabbay 	if (size == 0)
7056e81090bSOded Gabbay 		return -EINVAL;
7066e81090bSOded Gabbay 
7076e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
7086e81090bSOded Gabbay 		return -ENOMEM;
7096e81090bSOded Gabbay 
710b91d43ddSFelix Kuehling 	*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_NOIO);
7116e81090bSOded Gabbay 	if ((*mem_obj) == NULL)
7126e81090bSOded Gabbay 		return -ENOMEM;
7136e81090bSOded Gabbay 
71479775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
7156e81090bSOded Gabbay 
7166e81090bSOded Gabbay 	start_search = 0;
7176e81090bSOded Gabbay 
7186e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
7196e81090bSOded Gabbay 
7206e81090bSOded Gabbay kfd_gtt_restart_search:
7216e81090bSOded Gabbay 	/* Find the first chunk that is free */
7226e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
7236e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
7246e81090bSOded Gabbay 					start_search);
7256e81090bSOded Gabbay 
72679775b62SKent Russell 	pr_debug("Found = %d\n", found);
7276e81090bSOded Gabbay 
7286e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
7296e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
7306e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
7316e81090bSOded Gabbay 
7326e81090bSOded Gabbay 	/* Update fields of mem_obj */
7336e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
7346e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
7356e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
7366e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
7376e81090bSOded Gabbay 					found,
7386e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
7396e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
7406e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
7416e81090bSOded Gabbay 					found,
7426e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
7436e81090bSOded Gabbay 
74479775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
7456e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
7466e81090bSOded Gabbay 
7476e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
7486e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
74979775b62SKent Russell 		pr_debug("Single bit\n");
7506e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
7516e81090bSOded Gabbay 		goto kfd_gtt_out;
7526e81090bSOded Gabbay 	}
7536e81090bSOded Gabbay 
7546e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
7556e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
7566e81090bSOded Gabbay 	do {
7576e81090bSOded Gabbay 		(*mem_obj)->range_end =
7586e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
7596e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
7606e81090bSOded Gabbay 		/*
7616e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
7626e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
7636e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
7646e81090bSOded Gabbay 		 */
7656e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
7666e81090bSOded Gabbay 			start_search = found;
7676e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
7686e81090bSOded Gabbay 		}
7696e81090bSOded Gabbay 
7706e81090bSOded Gabbay 		/*
7716e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
7726e81090bSOded Gabbay 		 */
7736e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
7746e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
7756e81090bSOded Gabbay 
7766e81090bSOded Gabbay 		/* Check if we don't need another chunk */
7776e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
7786e81090bSOded Gabbay 			cur_size = 0;
7796e81090bSOded Gabbay 		else
7806e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
7816e81090bSOded Gabbay 
7826e81090bSOded Gabbay 	} while (cur_size > 0);
7836e81090bSOded Gabbay 
78479775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
7856e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
7866e81090bSOded Gabbay 
7876e81090bSOded Gabbay 	/* Mark the chunks as allocated */
7886e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
7896e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
7906e81090bSOded Gabbay 		found++)
7916e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
7926e81090bSOded Gabbay 
7936e81090bSOded Gabbay kfd_gtt_out:
7946e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
7956e81090bSOded Gabbay 	return 0;
7966e81090bSOded Gabbay 
7976e81090bSOded Gabbay kfd_gtt_no_free_chunk:
79879775b62SKent Russell 	pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
7996e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
8006e81090bSOded Gabbay 	kfree(mem_obj);
8016e81090bSOded Gabbay 	return -ENOMEM;
8026e81090bSOded Gabbay }
8036e81090bSOded Gabbay 
8046e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
8056e81090bSOded Gabbay {
8066e81090bSOded Gabbay 	unsigned int bit;
8076e81090bSOded Gabbay 
8089216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
8099216ed29SOded Gabbay 	if (!mem_obj)
8109216ed29SOded Gabbay 		return 0;
8116e81090bSOded Gabbay 
81279775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
8136e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
8146e81090bSOded Gabbay 
8156e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
8166e81090bSOded Gabbay 
8176e81090bSOded Gabbay 	/* Mark the chunks as free */
8186e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
8196e81090bSOded Gabbay 		bit <= mem_obj->range_end;
8206e81090bSOded Gabbay 		bit++)
8216e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
8226e81090bSOded Gabbay 
8236e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
8246e81090bSOded Gabbay 
8256e81090bSOded Gabbay 	kfree(mem_obj);
8266e81090bSOded Gabbay 	return 0;
8276e81090bSOded Gabbay }
828