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"
3364d1c3a4SFelix Kuehling #include "kfd_iommu.h"
344a488a7aSOded Gabbay 
3519f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768
3626103436SFelix Kuehling static atomic_t kfd_device_suspended = ATOMIC_INIT(0);
3719f6d2a6SOded Gabbay 
3864d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
394a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = {
400da7558cSBen Goz 	.asic_family = CHIP_KAVERI,
410da7558cSBen Goz 	.max_pasid_bits = 16,
42992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
43992839adSYair Shachar 	.max_no_of_hqd	= 24,
440da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
45f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
46fbeb661bSYair Shachar 	.num_of_watch_points = 4,
47373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
48373d7080SFelix Kuehling 	.supports_cwsr = false,
4964d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
503ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
510da7558cSBen Goz };
520da7558cSBen Goz 
530da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
540da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
554a488a7aSOded Gabbay 	.max_pasid_bits = 16,
56eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
57eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
58b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
59eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
60f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
61373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
62373d7080SFelix Kuehling 	.supports_cwsr = true,
6364d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
643ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
654a488a7aSOded Gabbay };
6664d1c3a4SFelix Kuehling #endif
674a488a7aSOded Gabbay 
68a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
69a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
70a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
71a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
72a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
73a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
74a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
75a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
76a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
77a3084e6cSFelix Kuehling 	.supports_cwsr = false,
7864d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
79a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
80a3084e6cSFelix Kuehling };
81a3084e6cSFelix Kuehling 
82a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
83a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
84a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
85a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
86a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
87a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
88a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
89a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
90a3084e6cSFelix Kuehling 	.supports_cwsr = false,
9164d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
92a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
93a3084e6cSFelix Kuehling };
94a3084e6cSFelix Kuehling 
95a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_vf_device_info = {
96a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
97a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
98a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
99a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
100a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
101a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
102a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
103a3084e6cSFelix Kuehling 	.supports_cwsr = false,
10464d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
105a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
106a3084e6cSFelix Kuehling };
107a3084e6cSFelix Kuehling 
108a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
109a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
110a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
111a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
112a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
113a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
114a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
115a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
116a3084e6cSFelix Kuehling 	.supports_cwsr = true,
11764d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
118a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
119a3084e6cSFelix Kuehling };
120a3084e6cSFelix Kuehling 
121a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
122a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
123a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
124a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
125a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
126a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
127a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
128a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
129a3084e6cSFelix Kuehling 	.supports_cwsr = true,
13064d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
131a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
132a3084e6cSFelix Kuehling };
133a3084e6cSFelix Kuehling 
134a3084e6cSFelix Kuehling 
135a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
136a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
137a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
138a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
139a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
140a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
141a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
142a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
143a3084e6cSFelix Kuehling 	.supports_cwsr = true,
14464d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
145a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
146a3084e6cSFelix Kuehling };
147a3084e6cSFelix Kuehling 
148a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = {
149a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
150a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
151a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
152a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
153a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
154a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
155a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
156a3084e6cSFelix Kuehling 	.supports_cwsr = true,
15764d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
158a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
159a3084e6cSFelix Kuehling };
160a3084e6cSFelix Kuehling 
161a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = {
162a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
163a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
164a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
165a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
166a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
167a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
168a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
169a3084e6cSFelix Kuehling 	.supports_cwsr = true,
17064d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
171a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
172a3084e6cSFelix Kuehling };
173a3084e6cSFelix Kuehling 
174a3084e6cSFelix Kuehling 
1754a488a7aSOded Gabbay struct kfd_deviceid {
1764a488a7aSOded Gabbay 	unsigned short did;
1774a488a7aSOded Gabbay 	const struct kfd_device_info *device_info;
1784a488a7aSOded Gabbay };
1794a488a7aSOded Gabbay 
1804a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = {
18164d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
1824a488a7aSOded Gabbay 	{ 0x1304, &kaveri_device_info },	/* Kaveri */
1834a488a7aSOded Gabbay 	{ 0x1305, &kaveri_device_info },	/* Kaveri */
1844a488a7aSOded Gabbay 	{ 0x1306, &kaveri_device_info },	/* Kaveri */
1854a488a7aSOded Gabbay 	{ 0x1307, &kaveri_device_info },	/* Kaveri */
1864a488a7aSOded Gabbay 	{ 0x1309, &kaveri_device_info },	/* Kaveri */
1874a488a7aSOded Gabbay 	{ 0x130A, &kaveri_device_info },	/* Kaveri */
1884a488a7aSOded Gabbay 	{ 0x130B, &kaveri_device_info },	/* Kaveri */
1894a488a7aSOded Gabbay 	{ 0x130C, &kaveri_device_info },	/* Kaveri */
1904a488a7aSOded Gabbay 	{ 0x130D, &kaveri_device_info },	/* Kaveri */
1914a488a7aSOded Gabbay 	{ 0x130E, &kaveri_device_info },	/* Kaveri */
1924a488a7aSOded Gabbay 	{ 0x130F, &kaveri_device_info },	/* Kaveri */
1934a488a7aSOded Gabbay 	{ 0x1310, &kaveri_device_info },	/* Kaveri */
1944a488a7aSOded Gabbay 	{ 0x1311, &kaveri_device_info },	/* Kaveri */
1954a488a7aSOded Gabbay 	{ 0x1312, &kaveri_device_info },	/* Kaveri */
1964a488a7aSOded Gabbay 	{ 0x1313, &kaveri_device_info },	/* Kaveri */
1974a488a7aSOded Gabbay 	{ 0x1315, &kaveri_device_info },	/* Kaveri */
1984a488a7aSOded Gabbay 	{ 0x1316, &kaveri_device_info },	/* Kaveri */
1994a488a7aSOded Gabbay 	{ 0x1317, &kaveri_device_info },	/* Kaveri */
2004a488a7aSOded Gabbay 	{ 0x1318, &kaveri_device_info },	/* Kaveri */
2014a488a7aSOded Gabbay 	{ 0x131B, &kaveri_device_info },	/* Kaveri */
2024a488a7aSOded Gabbay 	{ 0x131C, &kaveri_device_info },	/* Kaveri */
203123576d1SBen Goz 	{ 0x131D, &kaveri_device_info },	/* Kaveri */
204123576d1SBen Goz 	{ 0x9870, &carrizo_device_info },	/* Carrizo */
205123576d1SBen Goz 	{ 0x9874, &carrizo_device_info },	/* Carrizo */
206123576d1SBen Goz 	{ 0x9875, &carrizo_device_info },	/* Carrizo */
207123576d1SBen Goz 	{ 0x9876, &carrizo_device_info },	/* Carrizo */
208a3084e6cSFelix Kuehling 	{ 0x9877, &carrizo_device_info },	/* Carrizo */
20964d1c3a4SFelix Kuehling #endif
210a3084e6cSFelix Kuehling 	{ 0x67A0, &hawaii_device_info },	/* Hawaii */
211a3084e6cSFelix Kuehling 	{ 0x67A1, &hawaii_device_info },	/* Hawaii */
212a3084e6cSFelix Kuehling 	{ 0x67A2, &hawaii_device_info },	/* Hawaii */
213a3084e6cSFelix Kuehling 	{ 0x67A8, &hawaii_device_info },	/* Hawaii */
214a3084e6cSFelix Kuehling 	{ 0x67A9, &hawaii_device_info },	/* Hawaii */
215a3084e6cSFelix Kuehling 	{ 0x67AA, &hawaii_device_info },	/* Hawaii */
216a3084e6cSFelix Kuehling 	{ 0x67B0, &hawaii_device_info },	/* Hawaii */
217a3084e6cSFelix Kuehling 	{ 0x67B1, &hawaii_device_info },	/* Hawaii */
218a3084e6cSFelix Kuehling 	{ 0x67B8, &hawaii_device_info },	/* Hawaii */
219a3084e6cSFelix Kuehling 	{ 0x67B9, &hawaii_device_info },	/* Hawaii */
220a3084e6cSFelix Kuehling 	{ 0x67BA, &hawaii_device_info },	/* Hawaii */
221a3084e6cSFelix Kuehling 	{ 0x67BE, &hawaii_device_info },	/* Hawaii */
222a3084e6cSFelix Kuehling 	{ 0x6920, &tonga_device_info },		/* Tonga */
223a3084e6cSFelix Kuehling 	{ 0x6921, &tonga_device_info },		/* Tonga */
224a3084e6cSFelix Kuehling 	{ 0x6928, &tonga_device_info },		/* Tonga */
225a3084e6cSFelix Kuehling 	{ 0x6929, &tonga_device_info },		/* Tonga */
226a3084e6cSFelix Kuehling 	{ 0x692B, &tonga_device_info },		/* Tonga */
227a3084e6cSFelix Kuehling 	{ 0x692F, &tonga_vf_device_info },	/* Tonga vf */
228a3084e6cSFelix Kuehling 	{ 0x6938, &tonga_device_info },		/* Tonga */
229a3084e6cSFelix Kuehling 	{ 0x6939, &tonga_device_info },		/* Tonga */
230a3084e6cSFelix Kuehling 	{ 0x7300, &fiji_device_info },		/* Fiji */
231a3084e6cSFelix Kuehling 	{ 0x730F, &fiji_vf_device_info },	/* Fiji vf*/
232a3084e6cSFelix Kuehling 	{ 0x67C0, &polaris10_device_info },	/* Polaris10 */
233a3084e6cSFelix Kuehling 	{ 0x67C1, &polaris10_device_info },	/* Polaris10 */
234a3084e6cSFelix Kuehling 	{ 0x67C2, &polaris10_device_info },	/* Polaris10 */
235a3084e6cSFelix Kuehling 	{ 0x67C4, &polaris10_device_info },	/* Polaris10 */
236a3084e6cSFelix Kuehling 	{ 0x67C7, &polaris10_device_info },	/* Polaris10 */
237a3084e6cSFelix Kuehling 	{ 0x67C8, &polaris10_device_info },	/* Polaris10 */
238a3084e6cSFelix Kuehling 	{ 0x67C9, &polaris10_device_info },	/* Polaris10 */
239a3084e6cSFelix Kuehling 	{ 0x67CA, &polaris10_device_info },	/* Polaris10 */
240a3084e6cSFelix Kuehling 	{ 0x67CC, &polaris10_device_info },	/* Polaris10 */
241a3084e6cSFelix Kuehling 	{ 0x67CF, &polaris10_device_info },	/* Polaris10 */
242a3084e6cSFelix Kuehling 	{ 0x67D0, &polaris10_vf_device_info },	/* Polaris10 vf*/
243a3084e6cSFelix Kuehling 	{ 0x67DF, &polaris10_device_info },	/* Polaris10 */
244a3084e6cSFelix Kuehling 	{ 0x67E0, &polaris11_device_info },	/* Polaris11 */
245a3084e6cSFelix Kuehling 	{ 0x67E1, &polaris11_device_info },	/* Polaris11 */
246a3084e6cSFelix Kuehling 	{ 0x67E3, &polaris11_device_info },	/* Polaris11 */
247a3084e6cSFelix Kuehling 	{ 0x67E7, &polaris11_device_info },	/* Polaris11 */
248a3084e6cSFelix Kuehling 	{ 0x67E8, &polaris11_device_info },	/* Polaris11 */
249a3084e6cSFelix Kuehling 	{ 0x67E9, &polaris11_device_info },	/* Polaris11 */
250a3084e6cSFelix Kuehling 	{ 0x67EB, &polaris11_device_info },	/* Polaris11 */
251a3084e6cSFelix Kuehling 	{ 0x67EF, &polaris11_device_info },	/* Polaris11 */
252a3084e6cSFelix Kuehling 	{ 0x67FF, &polaris11_device_info },	/* Polaris11 */
2534a488a7aSOded Gabbay };
2544a488a7aSOded Gabbay 
2556e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
2566e81090bSOded Gabbay 				unsigned int chunk_size);
2576e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
2586e81090bSOded Gabbay 
259b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
260b8935a7cSYong Zhao 
2614a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did)
2624a488a7aSOded Gabbay {
2634a488a7aSOded Gabbay 	size_t i;
2644a488a7aSOded Gabbay 
2654a488a7aSOded Gabbay 	for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
2664a488a7aSOded Gabbay 		if (supported_devices[i].did == did) {
26732fa8219SFelix Kuehling 			WARN_ON(!supported_devices[i].device_info);
2684a488a7aSOded Gabbay 			return supported_devices[i].device_info;
2694a488a7aSOded Gabbay 		}
2704a488a7aSOded Gabbay 	}
2714a488a7aSOded Gabbay 
2724ebc7182SYong Zhao 	dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
2734ebc7182SYong Zhao 		 did);
2744ebc7182SYong Zhao 
2754a488a7aSOded Gabbay 	return NULL;
2764a488a7aSOded Gabbay }
2774a488a7aSOded Gabbay 
278cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
279cea405b1SXihan Zhang 	struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
2804a488a7aSOded Gabbay {
2814a488a7aSOded Gabbay 	struct kfd_dev *kfd;
2824a488a7aSOded Gabbay 
2834a488a7aSOded Gabbay 	const struct kfd_device_info *device_info =
2844a488a7aSOded Gabbay 					lookup_device_info(pdev->device);
2854a488a7aSOded Gabbay 
2864ebc7182SYong Zhao 	if (!device_info) {
2874ebc7182SYong Zhao 		dev_err(kfd_device, "kgd2kfd_probe failed\n");
2884a488a7aSOded Gabbay 		return NULL;
2894ebc7182SYong Zhao 	}
2904a488a7aSOded Gabbay 
2913ee2d00cSFelix Kuehling 	if (device_info->needs_pci_atomics) {
2923ee2d00cSFelix Kuehling 		/* Allow BIF to recode atomics to PCIe 3.0
2933ee2d00cSFelix Kuehling 		 * AtomicOps. 32 and 64-bit requests are possible and
2943ee2d00cSFelix Kuehling 		 * must be supported.
2953ee2d00cSFelix Kuehling 		 */
2963ee2d00cSFelix Kuehling 		if (pci_enable_atomic_ops_to_root(pdev,
2973ee2d00cSFelix Kuehling 				PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
2983ee2d00cSFelix Kuehling 				PCI_EXP_DEVCAP2_ATOMIC_COMP64) < 0) {
2993ee2d00cSFelix Kuehling 			dev_info(kfd_device,
3003ee2d00cSFelix Kuehling 				"skipped device %x:%x, PCI rejects atomics",
3013ee2d00cSFelix Kuehling 				 pdev->vendor, pdev->device);
3023ee2d00cSFelix Kuehling 			return NULL;
3033ee2d00cSFelix Kuehling 		}
3043ee2d00cSFelix Kuehling 	}
3053ee2d00cSFelix Kuehling 
3064a488a7aSOded Gabbay 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
3074a488a7aSOded Gabbay 	if (!kfd)
3084a488a7aSOded Gabbay 		return NULL;
3094a488a7aSOded Gabbay 
3104a488a7aSOded Gabbay 	kfd->kgd = kgd;
3114a488a7aSOded Gabbay 	kfd->device_info = device_info;
3124a488a7aSOded Gabbay 	kfd->pdev = pdev;
31319f6d2a6SOded Gabbay 	kfd->init_complete = false;
314cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
315cea405b1SXihan Zhang 
316cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
317cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
318cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
3194a488a7aSOded Gabbay 
3204a488a7aSOded Gabbay 	return kfd;
3214a488a7aSOded Gabbay }
3224a488a7aSOded Gabbay 
323373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
324373d7080SFelix Kuehling {
325373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
326373d7080SFelix Kuehling 		BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
327373d7080SFelix Kuehling 
328373d7080SFelix Kuehling 		kfd->cwsr_isa = cwsr_trap_gfx8_hex;
329373d7080SFelix Kuehling 		kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
330373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
331373d7080SFelix Kuehling 	}
332373d7080SFelix Kuehling }
333373d7080SFelix Kuehling 
3344a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
3354a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
3364a488a7aSOded Gabbay {
33719f6d2a6SOded Gabbay 	unsigned int size;
33819f6d2a6SOded Gabbay 
3394a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
3404a488a7aSOded Gabbay 
34144008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
34244008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
34344008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
34444008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
34544008d7aSYong Zhao 
346a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
347a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
348a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
349a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
350a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
351a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
352a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
353a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
354a99c6d4fSFelix Kuehling 	} else
355a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
356a99c6d4fSFelix Kuehling 
35719f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
358b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
35919f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
36019f6d2a6SOded Gabbay 
361e18e794eSOded Gabbay 	/*
362e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
363e18e794eSOded Gabbay 	 * There can be only 2 packets at once
364e18e794eSOded Gabbay 	 */
365507968ddSFelix Kuehling 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
366507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
367507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
368e18e794eSOded Gabbay 
369e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
370e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
371e18e794eSOded Gabbay 
372e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
37319f6d2a6SOded Gabbay 	size += 512 * 1024;
37419f6d2a6SOded Gabbay 
375cea405b1SXihan Zhang 	if (kfd->kfd2kgd->init_gtt_mem_allocation(
376cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
37773a1da0bSOded Gabbay 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
37879775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
37919f6d2a6SOded Gabbay 		goto out;
38019f6d2a6SOded Gabbay 	}
38119f6d2a6SOded Gabbay 
38279775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
383e18e794eSOded Gabbay 
38473a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
38573a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
38679775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
38773a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
38873a1da0bSOded Gabbay 	}
38973a1da0bSOded Gabbay 
390735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
391735df2baSFelix Kuehling 		dev_err(kfd_device,
392735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
393735df2baSFelix Kuehling 		goto kfd_doorbell_error;
394735df2baSFelix Kuehling 	}
39519f6d2a6SOded Gabbay 
3964eacc26bSKent Russell 	if (kfd_topology_add_device(kfd)) {
39779775b62SKent Russell 		dev_err(kfd_device, "Error adding device to topology\n");
39819f6d2a6SOded Gabbay 		goto kfd_topology_add_device_error;
39919f6d2a6SOded Gabbay 	}
40019f6d2a6SOded Gabbay 
4012249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
40279775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
4032249d558SAndrew Lewycky 		goto kfd_interrupt_error;
4042249d558SAndrew Lewycky 	}
4052249d558SAndrew Lewycky 
40664c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
40764c7f8cfSBen Goz 	if (!kfd->dqm) {
40879775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
40964c7f8cfSBen Goz 		goto device_queue_manager_error;
41064c7f8cfSBen Goz 	}
41164c7f8cfSBen Goz 
41264d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
41364d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
41464d1c3a4SFelix Kuehling 		goto device_iommu_error;
41564c7f8cfSBen Goz 	}
41664c7f8cfSBen Goz 
417373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
418373d7080SFelix Kuehling 
419b8935a7cSYong Zhao 	if (kfd_resume(kfd))
420b8935a7cSYong Zhao 		goto kfd_resume_error;
421b8935a7cSYong Zhao 
422fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
423fbeb661bSYair Shachar 
4244a488a7aSOded Gabbay 	kfd->init_complete = true;
42579775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
4264a488a7aSOded Gabbay 		 kfd->pdev->device);
4274a488a7aSOded Gabbay 
42879775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
429d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
43064c7f8cfSBen Goz 
43119f6d2a6SOded Gabbay 	goto out;
43219f6d2a6SOded Gabbay 
433b8935a7cSYong Zhao kfd_resume_error:
43464d1c3a4SFelix Kuehling device_iommu_error:
43564c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
43664c7f8cfSBen Goz device_queue_manager_error:
4372249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
4382249d558SAndrew Lewycky kfd_interrupt_error:
439b17f068aSOded Gabbay 	kfd_topology_remove_device(kfd);
44019f6d2a6SOded Gabbay kfd_topology_add_device_error:
441735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
442735df2baSFelix Kuehling kfd_doorbell_error:
44373a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
44473a1da0bSOded Gabbay kfd_gtt_sa_init_error:
445cea405b1SXihan Zhang 	kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
44619f6d2a6SOded Gabbay 	dev_err(kfd_device,
44779775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
44819f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
44919f6d2a6SOded Gabbay out:
45019f6d2a6SOded Gabbay 	return kfd->init_complete;
4514a488a7aSOded Gabbay }
4524a488a7aSOded Gabbay 
4534a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
4544a488a7aSOded Gabbay {
455b17f068aSOded Gabbay 	if (kfd->init_complete) {
456b8935a7cSYong Zhao 		kgd2kfd_suspend(kfd);
45764c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
4582249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
45919f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
460735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
46173a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
462cea405b1SXihan Zhang 		kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
463b17f068aSOded Gabbay 	}
4645b5c4e40SEvgeny Pinchuk 
4654a488a7aSOded Gabbay 	kfree(kfd);
4664a488a7aSOded Gabbay }
4674a488a7aSOded Gabbay 
4684a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd)
4694a488a7aSOded Gabbay {
470733fa1f7SYong Zhao 	if (!kfd->init_complete)
471733fa1f7SYong Zhao 		return;
472733fa1f7SYong Zhao 
47326103436SFelix Kuehling 	/* For first KFD device suspend all the KFD processes */
47426103436SFelix Kuehling 	if (atomic_inc_return(&kfd_device_suspended) == 1)
47526103436SFelix Kuehling 		kfd_suspend_all_processes();
47626103436SFelix Kuehling 
47745c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
478733fa1f7SYong Zhao 
47964d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
4804a488a7aSOded Gabbay }
4814a488a7aSOded Gabbay 
4824a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd)
4834a488a7aSOded Gabbay {
48426103436SFelix Kuehling 	int ret, count;
48526103436SFelix Kuehling 
486b8935a7cSYong Zhao 	if (!kfd->init_complete)
487b8935a7cSYong Zhao 		return 0;
488b17f068aSOded Gabbay 
48926103436SFelix Kuehling 	ret = kfd_resume(kfd);
49026103436SFelix Kuehling 	if (ret)
49126103436SFelix Kuehling 		return ret;
492b17f068aSOded Gabbay 
49326103436SFelix Kuehling 	count = atomic_dec_return(&kfd_device_suspended);
49426103436SFelix Kuehling 	WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
49526103436SFelix Kuehling 	if (count == 0)
49626103436SFelix Kuehling 		ret = kfd_resume_all_processes();
49726103436SFelix Kuehling 
49826103436SFelix Kuehling 	return ret;
4994ebc7182SYong Zhao }
5004ebc7182SYong Zhao 
501b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
502b8935a7cSYong Zhao {
503b8935a7cSYong Zhao 	int err = 0;
504b8935a7cSYong Zhao 
50564d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
50664d1c3a4SFelix Kuehling 	if (err) {
50764d1c3a4SFelix Kuehling 		dev_err(kfd_device,
50864d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
50964d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
51064d1c3a4SFelix Kuehling 		return err;
51164d1c3a4SFelix Kuehling 	}
512733fa1f7SYong Zhao 
513b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
514b8935a7cSYong Zhao 	if (err) {
515b8935a7cSYong Zhao 		dev_err(kfd_device,
516b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
517b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
518b8935a7cSYong Zhao 		goto dqm_start_error;
519b17f068aSOded Gabbay 	}
520b17f068aSOded Gabbay 
521b8935a7cSYong Zhao 	return err;
522b8935a7cSYong Zhao 
523b8935a7cSYong Zhao dqm_start_error:
52464d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
525b8935a7cSYong Zhao 	return err;
5264a488a7aSOded Gabbay }
5274a488a7aSOded Gabbay 
528b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
529b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
5304a488a7aSOded Gabbay {
5312249d558SAndrew Lewycky 	if (!kfd->init_complete)
5322249d558SAndrew Lewycky 		return;
5332249d558SAndrew Lewycky 
5342249d558SAndrew Lewycky 	spin_lock(&kfd->interrupt_lock);
5352249d558SAndrew Lewycky 
5362249d558SAndrew Lewycky 	if (kfd->interrupts_active
5372249d558SAndrew Lewycky 	    && interrupt_is_wanted(kfd, ih_ring_entry)
5382249d558SAndrew Lewycky 	    && enqueue_ih_ring_entry(kfd, ih_ring_entry))
53948e876a2SAndres Rodriguez 		queue_work(kfd->ih_wq, &kfd->interrupt_work);
5402249d558SAndrew Lewycky 
5412249d558SAndrew Lewycky 	spin_unlock(&kfd->interrupt_lock);
5424a488a7aSOded Gabbay }
5436e81090bSOded Gabbay 
544*6b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
545*6b95e797SFelix Kuehling {
546*6b95e797SFelix Kuehling 	struct kfd_process *p;
547*6b95e797SFelix Kuehling 	int r;
548*6b95e797SFelix Kuehling 
549*6b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
550*6b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
551*6b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
552*6b95e797SFelix Kuehling 	 */
553*6b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
554*6b95e797SFelix Kuehling 	if (!p)
555*6b95e797SFelix Kuehling 		return -ESRCH;
556*6b95e797SFelix Kuehling 
557*6b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
558*6b95e797SFelix Kuehling 
559*6b95e797SFelix Kuehling 	kfd_unref_process(p);
560*6b95e797SFelix Kuehling 	return r;
561*6b95e797SFelix Kuehling }
562*6b95e797SFelix Kuehling 
563*6b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
564*6b95e797SFelix Kuehling {
565*6b95e797SFelix Kuehling 	struct kfd_process *p;
566*6b95e797SFelix Kuehling 	int r;
567*6b95e797SFelix Kuehling 
568*6b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
569*6b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
570*6b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
571*6b95e797SFelix Kuehling 	 */
572*6b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
573*6b95e797SFelix Kuehling 	if (!p)
574*6b95e797SFelix Kuehling 		return -ESRCH;
575*6b95e797SFelix Kuehling 
576*6b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
577*6b95e797SFelix Kuehling 
578*6b95e797SFelix Kuehling 	kfd_unref_process(p);
579*6b95e797SFelix Kuehling 	return r;
580*6b95e797SFelix Kuehling }
581*6b95e797SFelix Kuehling 
58226103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
58326103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
58426103436SFelix Kuehling  *   process.
58526103436SFelix Kuehling  *
58626103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
58726103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
58826103436SFelix Kuehling  *
58926103436SFelix Kuehling  */
59026103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
59126103436SFelix Kuehling 					       struct dma_fence *fence)
59226103436SFelix Kuehling {
59326103436SFelix Kuehling 	struct kfd_process *p;
59426103436SFelix Kuehling 	unsigned long active_time;
59526103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
59626103436SFelix Kuehling 
59726103436SFelix Kuehling 	if (!fence)
59826103436SFelix Kuehling 		return -EINVAL;
59926103436SFelix Kuehling 
60026103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
60126103436SFelix Kuehling 		return 0;
60226103436SFelix Kuehling 
60326103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
60426103436SFelix Kuehling 	if (!p)
60526103436SFelix Kuehling 		return -ENODEV;
60626103436SFelix Kuehling 
60726103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
60826103436SFelix Kuehling 		goto out;
60926103436SFelix Kuehling 
61026103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
61126103436SFelix Kuehling 
61226103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
61326103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
61426103436SFelix Kuehling 	 */
61526103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
61626103436SFelix Kuehling 	if (delay_jiffies > active_time)
61726103436SFelix Kuehling 		delay_jiffies -= active_time;
61826103436SFelix Kuehling 	else
61926103436SFelix Kuehling 		delay_jiffies = 0;
62026103436SFelix Kuehling 
62126103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
62226103436SFelix Kuehling 	 * to kfd_evict_bo_worker
62326103436SFelix Kuehling 	 */
62426103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
62526103436SFelix Kuehling out:
62626103436SFelix Kuehling 	kfd_unref_process(p);
62726103436SFelix Kuehling 	return 0;
62826103436SFelix Kuehling }
62926103436SFelix Kuehling 
6306e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
6316e81090bSOded Gabbay 				unsigned int chunk_size)
6326e81090bSOded Gabbay {
6338625ff9cSFelix Kuehling 	unsigned int num_of_longs;
6346e81090bSOded Gabbay 
63532fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
63632fa8219SFelix Kuehling 		return -EINVAL;
63732fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
63832fa8219SFelix Kuehling 		return -EINVAL;
63932fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
64032fa8219SFelix Kuehling 		return -EINVAL;
6416e81090bSOded Gabbay 
6426e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
6436e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
6446e81090bSOded Gabbay 
6458625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
6468625ff9cSFelix Kuehling 		BITS_PER_LONG;
6476e81090bSOded Gabbay 
6488625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
6496e81090bSOded Gabbay 
6506e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
6516e81090bSOded Gabbay 		return -ENOMEM;
6526e81090bSOded Gabbay 
65379775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
6546e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
6556e81090bSOded Gabbay 
6566e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
6576e81090bSOded Gabbay 
6586e81090bSOded Gabbay 	return 0;
6596e81090bSOded Gabbay 
6606e81090bSOded Gabbay }
6616e81090bSOded Gabbay 
6626e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
6636e81090bSOded Gabbay {
6646e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
6656e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
6666e81090bSOded Gabbay }
6676e81090bSOded Gabbay 
6686e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
6696e81090bSOded Gabbay 						unsigned int bit_num,
6706e81090bSOded Gabbay 						unsigned int chunk_size)
6716e81090bSOded Gabbay {
6726e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
6736e81090bSOded Gabbay }
6746e81090bSOded Gabbay 
6756e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
6766e81090bSOded Gabbay 						unsigned int bit_num,
6776e81090bSOded Gabbay 						unsigned int chunk_size)
6786e81090bSOded Gabbay {
6796e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
6806e81090bSOded Gabbay }
6816e81090bSOded Gabbay 
6826e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
6836e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
6846e81090bSOded Gabbay {
6856e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
6866e81090bSOded Gabbay 
6876e81090bSOded Gabbay 	if (size == 0)
6886e81090bSOded Gabbay 		return -EINVAL;
6896e81090bSOded Gabbay 
6906e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
6916e81090bSOded Gabbay 		return -ENOMEM;
6926e81090bSOded Gabbay 
693d1853f42SFelix Kuehling 	*mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_NOIO);
6946e81090bSOded Gabbay 	if ((*mem_obj) == NULL)
6956e81090bSOded Gabbay 		return -ENOMEM;
6966e81090bSOded Gabbay 
69779775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
6986e81090bSOded Gabbay 
6996e81090bSOded Gabbay 	start_search = 0;
7006e81090bSOded Gabbay 
7016e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
7026e81090bSOded Gabbay 
7036e81090bSOded Gabbay kfd_gtt_restart_search:
7046e81090bSOded Gabbay 	/* Find the first chunk that is free */
7056e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
7066e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
7076e81090bSOded Gabbay 					start_search);
7086e81090bSOded Gabbay 
70979775b62SKent Russell 	pr_debug("Found = %d\n", found);
7106e81090bSOded Gabbay 
7116e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
7126e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
7136e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
7146e81090bSOded Gabbay 
7156e81090bSOded Gabbay 	/* Update fields of mem_obj */
7166e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
7176e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
7186e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
7196e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
7206e81090bSOded Gabbay 					found,
7216e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
7226e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
7236e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
7246e81090bSOded Gabbay 					found,
7256e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
7266e81090bSOded Gabbay 
72779775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
7286e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
7296e81090bSOded Gabbay 
7306e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
7316e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
73279775b62SKent Russell 		pr_debug("Single bit\n");
7336e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
7346e81090bSOded Gabbay 		goto kfd_gtt_out;
7356e81090bSOded Gabbay 	}
7366e81090bSOded Gabbay 
7376e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
7386e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
7396e81090bSOded Gabbay 	do {
7406e81090bSOded Gabbay 		(*mem_obj)->range_end =
7416e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
7426e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
7436e81090bSOded Gabbay 		/*
7446e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
7456e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
7466e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
7476e81090bSOded Gabbay 		 */
7486e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
7496e81090bSOded Gabbay 			start_search = found;
7506e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
7516e81090bSOded Gabbay 		}
7526e81090bSOded Gabbay 
7536e81090bSOded Gabbay 		/*
7546e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
7556e81090bSOded Gabbay 		 */
7566e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
7576e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
7586e81090bSOded Gabbay 
7596e81090bSOded Gabbay 		/* Check if we don't need another chunk */
7606e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
7616e81090bSOded Gabbay 			cur_size = 0;
7626e81090bSOded Gabbay 		else
7636e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
7646e81090bSOded Gabbay 
7656e81090bSOded Gabbay 	} while (cur_size > 0);
7666e81090bSOded Gabbay 
76779775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
7686e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
7696e81090bSOded Gabbay 
7706e81090bSOded Gabbay 	/* Mark the chunks as allocated */
7716e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
7726e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
7736e81090bSOded Gabbay 		found++)
7746e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
7756e81090bSOded Gabbay 
7766e81090bSOded Gabbay kfd_gtt_out:
7776e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
7786e81090bSOded Gabbay 	return 0;
7796e81090bSOded Gabbay 
7806e81090bSOded Gabbay kfd_gtt_no_free_chunk:
78179775b62SKent Russell 	pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
7826e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
7836e81090bSOded Gabbay 	kfree(mem_obj);
7846e81090bSOded Gabbay 	return -ENOMEM;
7856e81090bSOded Gabbay }
7866e81090bSOded Gabbay 
7876e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
7886e81090bSOded Gabbay {
7896e81090bSOded Gabbay 	unsigned int bit;
7906e81090bSOded Gabbay 
7919216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
7929216ed29SOded Gabbay 	if (!mem_obj)
7939216ed29SOded Gabbay 		return 0;
7946e81090bSOded Gabbay 
79579775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
7966e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
7976e81090bSOded Gabbay 
7986e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
7996e81090bSOded Gabbay 
8006e81090bSOded Gabbay 	/* Mark the chunks as free */
8016e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
8026e81090bSOded Gabbay 		bit <= mem_obj->range_end;
8036e81090bSOded Gabbay 		bit++)
8046e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
8056e81090bSOded Gabbay 
8066e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
8076e81090bSOded Gabbay 
8086e81090bSOded Gabbay 	kfree(mem_obj);
8096e81090bSOded Gabbay 	return 0;
8106e81090bSOded Gabbay }
811