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/amd-iommu.h>
244a488a7aSOded Gabbay #include <linux/bsearch.h>
254a488a7aSOded Gabbay #include <linux/pci.h>
264a488a7aSOded Gabbay #include <linux/slab.h>
274a488a7aSOded Gabbay #include "kfd_priv.h"
2864c7f8cfSBen Goz #include "kfd_device_queue_manager.h"
29507968ddSFelix Kuehling #include "kfd_pm4_headers_vi.h"
30373d7080SFelix Kuehling #include "cwsr_trap_handler_gfx8.asm"
314a488a7aSOded Gabbay 
3219f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768
3319f6d2a6SOded Gabbay 
344a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = {
350da7558cSBen Goz 	.asic_family = CHIP_KAVERI,
360da7558cSBen Goz 	.max_pasid_bits = 16,
37992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
38992839adSYair Shachar 	.max_no_of_hqd	= 24,
390da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
40f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
41fbeb661bSYair Shachar 	.num_of_watch_points = 4,
42373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
43373d7080SFelix Kuehling 	.supports_cwsr = false,
443ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
450da7558cSBen Goz };
460da7558cSBen Goz 
470da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
480da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
494a488a7aSOded Gabbay 	.max_pasid_bits = 16,
50eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
51eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
52b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
53eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
54f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
55373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
56373d7080SFelix Kuehling 	.supports_cwsr = true,
573ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
584a488a7aSOded Gabbay };
594a488a7aSOded Gabbay 
604a488a7aSOded Gabbay struct kfd_deviceid {
614a488a7aSOded Gabbay 	unsigned short did;
624a488a7aSOded Gabbay 	const struct kfd_device_info *device_info;
634a488a7aSOded Gabbay };
644a488a7aSOded Gabbay 
654a488a7aSOded Gabbay /* Please keep this sorted by increasing device id. */
664a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = {
674a488a7aSOded Gabbay 	{ 0x1304, &kaveri_device_info },	/* Kaveri */
684a488a7aSOded Gabbay 	{ 0x1305, &kaveri_device_info },	/* Kaveri */
694a488a7aSOded Gabbay 	{ 0x1306, &kaveri_device_info },	/* Kaveri */
704a488a7aSOded Gabbay 	{ 0x1307, &kaveri_device_info },	/* Kaveri */
714a488a7aSOded Gabbay 	{ 0x1309, &kaveri_device_info },	/* Kaveri */
724a488a7aSOded Gabbay 	{ 0x130A, &kaveri_device_info },	/* Kaveri */
734a488a7aSOded Gabbay 	{ 0x130B, &kaveri_device_info },	/* Kaveri */
744a488a7aSOded Gabbay 	{ 0x130C, &kaveri_device_info },	/* Kaveri */
754a488a7aSOded Gabbay 	{ 0x130D, &kaveri_device_info },	/* Kaveri */
764a488a7aSOded Gabbay 	{ 0x130E, &kaveri_device_info },	/* Kaveri */
774a488a7aSOded Gabbay 	{ 0x130F, &kaveri_device_info },	/* Kaveri */
784a488a7aSOded Gabbay 	{ 0x1310, &kaveri_device_info },	/* Kaveri */
794a488a7aSOded Gabbay 	{ 0x1311, &kaveri_device_info },	/* Kaveri */
804a488a7aSOded Gabbay 	{ 0x1312, &kaveri_device_info },	/* Kaveri */
814a488a7aSOded Gabbay 	{ 0x1313, &kaveri_device_info },	/* Kaveri */
824a488a7aSOded Gabbay 	{ 0x1315, &kaveri_device_info },	/* Kaveri */
834a488a7aSOded Gabbay 	{ 0x1316, &kaveri_device_info },	/* Kaveri */
844a488a7aSOded Gabbay 	{ 0x1317, &kaveri_device_info },	/* Kaveri */
854a488a7aSOded Gabbay 	{ 0x1318, &kaveri_device_info },	/* Kaveri */
864a488a7aSOded Gabbay 	{ 0x131B, &kaveri_device_info },	/* Kaveri */
874a488a7aSOded Gabbay 	{ 0x131C, &kaveri_device_info },	/* Kaveri */
88123576d1SBen Goz 	{ 0x131D, &kaveri_device_info },	/* Kaveri */
89123576d1SBen Goz 	{ 0x9870, &carrizo_device_info },	/* Carrizo */
90123576d1SBen Goz 	{ 0x9874, &carrizo_device_info },	/* Carrizo */
91123576d1SBen Goz 	{ 0x9875, &carrizo_device_info },	/* Carrizo */
92123576d1SBen Goz 	{ 0x9876, &carrizo_device_info },	/* Carrizo */
93123576d1SBen Goz 	{ 0x9877, &carrizo_device_info }	/* Carrizo */
944a488a7aSOded Gabbay };
954a488a7aSOded Gabbay 
966e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
976e81090bSOded Gabbay 				unsigned int chunk_size);
986e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
996e81090bSOded Gabbay 
100b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
101b8935a7cSYong Zhao 
1024a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did)
1034a488a7aSOded Gabbay {
1044a488a7aSOded Gabbay 	size_t i;
1054a488a7aSOded Gabbay 
1064a488a7aSOded Gabbay 	for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
1074a488a7aSOded Gabbay 		if (supported_devices[i].did == did) {
10832fa8219SFelix Kuehling 			WARN_ON(!supported_devices[i].device_info);
1094a488a7aSOded Gabbay 			return supported_devices[i].device_info;
1104a488a7aSOded Gabbay 		}
1114a488a7aSOded Gabbay 	}
1124a488a7aSOded Gabbay 
1134ebc7182SYong Zhao 	dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
1144ebc7182SYong Zhao 		 did);
1154ebc7182SYong Zhao 
1164a488a7aSOded Gabbay 	return NULL;
1174a488a7aSOded Gabbay }
1184a488a7aSOded Gabbay 
119cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
120cea405b1SXihan Zhang 	struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
1214a488a7aSOded Gabbay {
1224a488a7aSOded Gabbay 	struct kfd_dev *kfd;
1234a488a7aSOded Gabbay 
1244a488a7aSOded Gabbay 	const struct kfd_device_info *device_info =
1254a488a7aSOded Gabbay 					lookup_device_info(pdev->device);
1264a488a7aSOded Gabbay 
1274ebc7182SYong Zhao 	if (!device_info) {
1284ebc7182SYong Zhao 		dev_err(kfd_device, "kgd2kfd_probe failed\n");
1294a488a7aSOded Gabbay 		return NULL;
1304ebc7182SYong Zhao 	}
1314a488a7aSOded Gabbay 
1323ee2d00cSFelix Kuehling 	if (device_info->needs_pci_atomics) {
1333ee2d00cSFelix Kuehling 		/* Allow BIF to recode atomics to PCIe 3.0
1343ee2d00cSFelix Kuehling 		 * AtomicOps. 32 and 64-bit requests are possible and
1353ee2d00cSFelix Kuehling 		 * must be supported.
1363ee2d00cSFelix Kuehling 		 */
1373ee2d00cSFelix Kuehling 		if (pci_enable_atomic_ops_to_root(pdev,
1383ee2d00cSFelix Kuehling 				PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1393ee2d00cSFelix Kuehling 				PCI_EXP_DEVCAP2_ATOMIC_COMP64) < 0) {
1403ee2d00cSFelix Kuehling 			dev_info(kfd_device,
1413ee2d00cSFelix Kuehling 				"skipped device %x:%x, PCI rejects atomics",
1423ee2d00cSFelix Kuehling 				 pdev->vendor, pdev->device);
1433ee2d00cSFelix Kuehling 			return NULL;
1443ee2d00cSFelix Kuehling 		}
1453ee2d00cSFelix Kuehling 	}
1463ee2d00cSFelix Kuehling 
1474a488a7aSOded Gabbay 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
1484a488a7aSOded Gabbay 	if (!kfd)
1494a488a7aSOded Gabbay 		return NULL;
1504a488a7aSOded Gabbay 
1514a488a7aSOded Gabbay 	kfd->kgd = kgd;
1524a488a7aSOded Gabbay 	kfd->device_info = device_info;
1534a488a7aSOded Gabbay 	kfd->pdev = pdev;
15419f6d2a6SOded Gabbay 	kfd->init_complete = false;
155cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
156cea405b1SXihan Zhang 
157cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
158cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
159cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
1604a488a7aSOded Gabbay 
1614a488a7aSOded Gabbay 	return kfd;
1624a488a7aSOded Gabbay }
1634a488a7aSOded Gabbay 
164b17f068aSOded Gabbay static bool device_iommu_pasid_init(struct kfd_dev *kfd)
165b17f068aSOded Gabbay {
166b17f068aSOded Gabbay 	const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
167b17f068aSOded Gabbay 					AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
168b17f068aSOded Gabbay 					AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
169b17f068aSOded Gabbay 
170b17f068aSOded Gabbay 	struct amd_iommu_device_info iommu_info;
171b17f068aSOded Gabbay 	unsigned int pasid_limit;
172b17f068aSOded Gabbay 	int err;
173b17f068aSOded Gabbay 
174b17f068aSOded Gabbay 	err = amd_iommu_device_info(kfd->pdev, &iommu_info);
175b17f068aSOded Gabbay 	if (err < 0) {
176b17f068aSOded Gabbay 		dev_err(kfd_device,
177b17f068aSOded Gabbay 			"error getting iommu info. is the iommu enabled?\n");
178b17f068aSOded Gabbay 		return false;
179b17f068aSOded Gabbay 	}
180b17f068aSOded Gabbay 
181b17f068aSOded Gabbay 	if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
18279775b62SKent Russell 		dev_err(kfd_device, "error required iommu flags ats %i, pri %i, pasid %i\n",
183b17f068aSOded Gabbay 		       (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
184b17f068aSOded Gabbay 		       (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
1858eabaf54SKent Russell 		       (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP)
1868eabaf54SKent Russell 									!= 0);
187b17f068aSOded Gabbay 		return false;
188b17f068aSOded Gabbay 	}
189b17f068aSOded Gabbay 
190b17f068aSOded Gabbay 	pasid_limit = min_t(unsigned int,
1918eabaf54SKent Russell 			(unsigned int)(1 << kfd->device_info->max_pasid_bits),
192b17f068aSOded Gabbay 			iommu_info.max_pasids);
193b17f068aSOded Gabbay 
194b17f068aSOded Gabbay 	if (!kfd_set_pasid_limit(pasid_limit)) {
195b17f068aSOded Gabbay 		dev_err(kfd_device, "error setting pasid limit\n");
196b17f068aSOded Gabbay 		return false;
197b17f068aSOded Gabbay 	}
198b17f068aSOded Gabbay 
199b17f068aSOded Gabbay 	return true;
200b17f068aSOded Gabbay }
201b17f068aSOded Gabbay 
202b17f068aSOded Gabbay static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
203b17f068aSOded Gabbay {
204b17f068aSOded Gabbay 	struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
205b17f068aSOded Gabbay 
206b17f068aSOded Gabbay 	if (dev)
207733fa1f7SYong Zhao 		kfd_process_iommu_unbind_callback(dev, pasid);
208b17f068aSOded Gabbay }
209b17f068aSOded Gabbay 
21059d3e8beSAlexey Skidanov /*
21159d3e8beSAlexey Skidanov  * This function called by IOMMU driver on PPR failure
21259d3e8beSAlexey Skidanov  */
21359d3e8beSAlexey Skidanov static int iommu_invalid_ppr_cb(struct pci_dev *pdev, int pasid,
21459d3e8beSAlexey Skidanov 		unsigned long address, u16 flags)
21559d3e8beSAlexey Skidanov {
21659d3e8beSAlexey Skidanov 	struct kfd_dev *dev;
21759d3e8beSAlexey Skidanov 
21859d3e8beSAlexey Skidanov 	dev_warn(kfd_device,
21959d3e8beSAlexey Skidanov 			"Invalid PPR device %x:%x.%x pasid %d address 0x%lX flags 0x%X",
22059d3e8beSAlexey Skidanov 			PCI_BUS_NUM(pdev->devfn),
22159d3e8beSAlexey Skidanov 			PCI_SLOT(pdev->devfn),
22259d3e8beSAlexey Skidanov 			PCI_FUNC(pdev->devfn),
22359d3e8beSAlexey Skidanov 			pasid,
22459d3e8beSAlexey Skidanov 			address,
22559d3e8beSAlexey Skidanov 			flags);
22659d3e8beSAlexey Skidanov 
22759d3e8beSAlexey Skidanov 	dev = kfd_device_by_pci_dev(pdev);
22832fa8219SFelix Kuehling 	if (!WARN_ON(!dev))
22959d3e8beSAlexey Skidanov 		kfd_signal_iommu_event(dev, pasid, address,
23059d3e8beSAlexey Skidanov 			flags & PPR_FAULT_WRITE, flags & PPR_FAULT_EXEC);
23159d3e8beSAlexey Skidanov 
23259d3e8beSAlexey Skidanov 	return AMD_IOMMU_INV_PRI_RSP_INVALID;
23359d3e8beSAlexey Skidanov }
23459d3e8beSAlexey Skidanov 
235373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
236373d7080SFelix Kuehling {
237373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
238373d7080SFelix Kuehling 		BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
239373d7080SFelix Kuehling 
240373d7080SFelix Kuehling 		kfd->cwsr_isa = cwsr_trap_gfx8_hex;
241373d7080SFelix Kuehling 		kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
242373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
243373d7080SFelix Kuehling 	}
244373d7080SFelix Kuehling }
245373d7080SFelix Kuehling 
2464a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
2474a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
2484a488a7aSOded Gabbay {
24919f6d2a6SOded Gabbay 	unsigned int size;
25019f6d2a6SOded Gabbay 
2514a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
2524a488a7aSOded Gabbay 
25344008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
25444008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
25544008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
25644008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
25744008d7aSYong Zhao 
258a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
259a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
260a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
261a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
262a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
263a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
264a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
265a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
266a99c6d4fSFelix Kuehling 	} else
267a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
268a99c6d4fSFelix Kuehling 
26919f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
270b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
27119f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
27219f6d2a6SOded Gabbay 
273e18e794eSOded Gabbay 	/*
274e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
275e18e794eSOded Gabbay 	 * There can be only 2 packets at once
276e18e794eSOded Gabbay 	 */
277507968ddSFelix Kuehling 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
278507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
279507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
280e18e794eSOded Gabbay 
281e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
282e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
283e18e794eSOded Gabbay 
284e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
28519f6d2a6SOded Gabbay 	size += 512 * 1024;
28619f6d2a6SOded Gabbay 
287cea405b1SXihan Zhang 	if (kfd->kfd2kgd->init_gtt_mem_allocation(
288cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
28973a1da0bSOded Gabbay 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
29079775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
29119f6d2a6SOded Gabbay 		goto out;
29219f6d2a6SOded Gabbay 	}
29319f6d2a6SOded Gabbay 
29479775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
295e18e794eSOded Gabbay 
29673a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
29773a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
29879775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
29973a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
30073a1da0bSOded Gabbay 	}
30173a1da0bSOded Gabbay 
302735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
303735df2baSFelix Kuehling 		dev_err(kfd_device,
304735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
305735df2baSFelix Kuehling 		goto kfd_doorbell_error;
306735df2baSFelix Kuehling 	}
30719f6d2a6SOded Gabbay 
3084eacc26bSKent Russell 	if (kfd_topology_add_device(kfd)) {
30979775b62SKent Russell 		dev_err(kfd_device, "Error adding device to topology\n");
31019f6d2a6SOded Gabbay 		goto kfd_topology_add_device_error;
31119f6d2a6SOded Gabbay 	}
31219f6d2a6SOded Gabbay 
3132249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
31479775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
3152249d558SAndrew Lewycky 		goto kfd_interrupt_error;
3162249d558SAndrew Lewycky 	}
3172249d558SAndrew Lewycky 
31864c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
31964c7f8cfSBen Goz 	if (!kfd->dqm) {
32079775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
32164c7f8cfSBen Goz 		goto device_queue_manager_error;
32264c7f8cfSBen Goz 	}
32364c7f8cfSBen Goz 
324b8935a7cSYong Zhao 	if (!device_iommu_pasid_init(kfd)) {
32564c7f8cfSBen Goz 		dev_err(kfd_device,
326b8935a7cSYong Zhao 			"Error initializing iommuv2 for device %x:%x\n",
32764c7f8cfSBen Goz 			kfd->pdev->vendor, kfd->pdev->device);
328b8935a7cSYong Zhao 		goto device_iommu_pasid_error;
32964c7f8cfSBen Goz 	}
33064c7f8cfSBen Goz 
331373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
332373d7080SFelix Kuehling 
333b8935a7cSYong Zhao 	if (kfd_resume(kfd))
334b8935a7cSYong Zhao 		goto kfd_resume_error;
335b8935a7cSYong Zhao 
336fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
337fbeb661bSYair Shachar 
3384a488a7aSOded Gabbay 	kfd->init_complete = true;
33979775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
3404a488a7aSOded Gabbay 		 kfd->pdev->device);
3414a488a7aSOded Gabbay 
34279775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
343*d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
34464c7f8cfSBen Goz 
34519f6d2a6SOded Gabbay 	goto out;
34619f6d2a6SOded Gabbay 
347b8935a7cSYong Zhao kfd_resume_error:
348b8935a7cSYong Zhao device_iommu_pasid_error:
34964c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
35064c7f8cfSBen Goz device_queue_manager_error:
3512249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
3522249d558SAndrew Lewycky kfd_interrupt_error:
353b17f068aSOded Gabbay 	kfd_topology_remove_device(kfd);
35419f6d2a6SOded Gabbay kfd_topology_add_device_error:
355735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
356735df2baSFelix Kuehling kfd_doorbell_error:
35773a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
35873a1da0bSOded Gabbay kfd_gtt_sa_init_error:
359cea405b1SXihan Zhang 	kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
36019f6d2a6SOded Gabbay 	dev_err(kfd_device,
36179775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
36219f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
36319f6d2a6SOded Gabbay out:
36419f6d2a6SOded Gabbay 	return kfd->init_complete;
3654a488a7aSOded Gabbay }
3664a488a7aSOded Gabbay 
3674a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
3684a488a7aSOded Gabbay {
369b17f068aSOded Gabbay 	if (kfd->init_complete) {
370b8935a7cSYong Zhao 		kgd2kfd_suspend(kfd);
37164c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
3722249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
37319f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
374735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
37573a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
376cea405b1SXihan Zhang 		kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
377b17f068aSOded Gabbay 	}
3785b5c4e40SEvgeny Pinchuk 
3794a488a7aSOded Gabbay 	kfree(kfd);
3804a488a7aSOded Gabbay }
3814a488a7aSOded Gabbay 
3824a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd)
3834a488a7aSOded Gabbay {
384733fa1f7SYong Zhao 	if (!kfd->init_complete)
385733fa1f7SYong Zhao 		return;
386733fa1f7SYong Zhao 
38745c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
388733fa1f7SYong Zhao 
389733fa1f7SYong Zhao 	kfd_unbind_processes_from_device(kfd);
390733fa1f7SYong Zhao 
391abc9d3e3SOded Gabbay 	amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
39259d3e8beSAlexey Skidanov 	amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
393b17f068aSOded Gabbay 	amd_iommu_free_device(kfd->pdev);
3944a488a7aSOded Gabbay }
3954a488a7aSOded Gabbay 
3964a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd)
3974a488a7aSOded Gabbay {
398b8935a7cSYong Zhao 	if (!kfd->init_complete)
399b8935a7cSYong Zhao 		return 0;
400b17f068aSOded Gabbay 
401b8935a7cSYong Zhao 	return kfd_resume(kfd);
402b17f068aSOded Gabbay 
4034ebc7182SYong Zhao }
4044ebc7182SYong Zhao 
405b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
406b8935a7cSYong Zhao {
407b8935a7cSYong Zhao 	int err = 0;
408b8935a7cSYong Zhao 	unsigned int pasid_limit = kfd_get_pasid_limit();
409b8935a7cSYong Zhao 
410b8935a7cSYong Zhao 	err = amd_iommu_init_device(kfd->pdev, pasid_limit);
411b8935a7cSYong Zhao 	if (err)
412b8935a7cSYong Zhao 		return -ENXIO;
413b17f068aSOded Gabbay 	amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
414b17f068aSOded Gabbay 					iommu_pasid_shutdown_callback);
415b8935a7cSYong Zhao 	amd_iommu_set_invalid_ppr_cb(kfd->pdev,
416b8935a7cSYong Zhao 				     iommu_invalid_ppr_cb);
417b8935a7cSYong Zhao 
418733fa1f7SYong Zhao 	err = kfd_bind_processes_to_device(kfd);
419733fa1f7SYong Zhao 	if (err)
420733fa1f7SYong Zhao 		goto processes_bind_error;
421733fa1f7SYong Zhao 
422b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
423b8935a7cSYong Zhao 	if (err) {
424b8935a7cSYong Zhao 		dev_err(kfd_device,
425b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
426b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
427b8935a7cSYong Zhao 		goto dqm_start_error;
428b17f068aSOded Gabbay 	}
429b17f068aSOded Gabbay 
430b8935a7cSYong Zhao 	return err;
431b8935a7cSYong Zhao 
432b8935a7cSYong Zhao dqm_start_error:
433733fa1f7SYong Zhao processes_bind_error:
434b8935a7cSYong Zhao 	amd_iommu_free_device(kfd->pdev);
435b8935a7cSYong Zhao 
436b8935a7cSYong Zhao 	return err;
4374a488a7aSOded Gabbay }
4384a488a7aSOded Gabbay 
439b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
440b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
4414a488a7aSOded Gabbay {
4422249d558SAndrew Lewycky 	if (!kfd->init_complete)
4432249d558SAndrew Lewycky 		return;
4442249d558SAndrew Lewycky 
4452249d558SAndrew Lewycky 	spin_lock(&kfd->interrupt_lock);
4462249d558SAndrew Lewycky 
4472249d558SAndrew Lewycky 	if (kfd->interrupts_active
4482249d558SAndrew Lewycky 	    && interrupt_is_wanted(kfd, ih_ring_entry)
4492249d558SAndrew Lewycky 	    && enqueue_ih_ring_entry(kfd, ih_ring_entry))
45048e876a2SAndres Rodriguez 		queue_work(kfd->ih_wq, &kfd->interrupt_work);
4512249d558SAndrew Lewycky 
4522249d558SAndrew Lewycky 	spin_unlock(&kfd->interrupt_lock);
4534a488a7aSOded Gabbay }
4546e81090bSOded Gabbay 
4556e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
4566e81090bSOded Gabbay 				unsigned int chunk_size)
4576e81090bSOded Gabbay {
4588625ff9cSFelix Kuehling 	unsigned int num_of_longs;
4596e81090bSOded Gabbay 
46032fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
46132fa8219SFelix Kuehling 		return -EINVAL;
46232fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
46332fa8219SFelix Kuehling 		return -EINVAL;
46432fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
46532fa8219SFelix Kuehling 		return -EINVAL;
4666e81090bSOded Gabbay 
4676e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
4686e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
4696e81090bSOded Gabbay 
4708625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
4718625ff9cSFelix Kuehling 		BITS_PER_LONG;
4726e81090bSOded Gabbay 
4738625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
4746e81090bSOded Gabbay 
4756e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
4766e81090bSOded Gabbay 		return -ENOMEM;
4776e81090bSOded Gabbay 
47879775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
4796e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
4806e81090bSOded Gabbay 
4816e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
4826e81090bSOded Gabbay 
4836e81090bSOded Gabbay 	return 0;
4846e81090bSOded Gabbay 
4856e81090bSOded Gabbay }
4866e81090bSOded Gabbay 
4876e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
4886e81090bSOded Gabbay {
4896e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
4906e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
4916e81090bSOded Gabbay }
4926e81090bSOded Gabbay 
4936e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
4946e81090bSOded Gabbay 						unsigned int bit_num,
4956e81090bSOded Gabbay 						unsigned int chunk_size)
4966e81090bSOded Gabbay {
4976e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
4986e81090bSOded Gabbay }
4996e81090bSOded Gabbay 
5006e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
5016e81090bSOded Gabbay 						unsigned int bit_num,
5026e81090bSOded Gabbay 						unsigned int chunk_size)
5036e81090bSOded Gabbay {
5046e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
5056e81090bSOded Gabbay }
5066e81090bSOded Gabbay 
5076e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
5086e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
5096e81090bSOded Gabbay {
5106e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
5116e81090bSOded Gabbay 
5126e81090bSOded Gabbay 	if (size == 0)
5136e81090bSOded Gabbay 		return -EINVAL;
5146e81090bSOded Gabbay 
5156e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
5166e81090bSOded Gabbay 		return -ENOMEM;
5176e81090bSOded Gabbay 
5186e81090bSOded Gabbay 	*mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
5196e81090bSOded Gabbay 	if ((*mem_obj) == NULL)
5206e81090bSOded Gabbay 		return -ENOMEM;
5216e81090bSOded Gabbay 
52279775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
5236e81090bSOded Gabbay 
5246e81090bSOded Gabbay 	start_search = 0;
5256e81090bSOded Gabbay 
5266e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
5276e81090bSOded Gabbay 
5286e81090bSOded Gabbay kfd_gtt_restart_search:
5296e81090bSOded Gabbay 	/* Find the first chunk that is free */
5306e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
5316e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
5326e81090bSOded Gabbay 					start_search);
5336e81090bSOded Gabbay 
53479775b62SKent Russell 	pr_debug("Found = %d\n", found);
5356e81090bSOded Gabbay 
5366e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
5376e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
5386e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
5396e81090bSOded Gabbay 
5406e81090bSOded Gabbay 	/* Update fields of mem_obj */
5416e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
5426e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
5436e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
5446e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
5456e81090bSOded Gabbay 					found,
5466e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
5476e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
5486e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
5496e81090bSOded Gabbay 					found,
5506e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
5516e81090bSOded Gabbay 
55279775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
5536e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
5546e81090bSOded Gabbay 
5556e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
5566e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
55779775b62SKent Russell 		pr_debug("Single bit\n");
5586e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
5596e81090bSOded Gabbay 		goto kfd_gtt_out;
5606e81090bSOded Gabbay 	}
5616e81090bSOded Gabbay 
5626e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
5636e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
5646e81090bSOded Gabbay 	do {
5656e81090bSOded Gabbay 		(*mem_obj)->range_end =
5666e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
5676e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
5686e81090bSOded Gabbay 		/*
5696e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
5706e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
5716e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
5726e81090bSOded Gabbay 		 */
5736e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
5746e81090bSOded Gabbay 			start_search = found;
5756e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
5766e81090bSOded Gabbay 		}
5776e81090bSOded Gabbay 
5786e81090bSOded Gabbay 		/*
5796e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
5806e81090bSOded Gabbay 		 */
5816e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
5826e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
5836e81090bSOded Gabbay 
5846e81090bSOded Gabbay 		/* Check if we don't need another chunk */
5856e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
5866e81090bSOded Gabbay 			cur_size = 0;
5876e81090bSOded Gabbay 		else
5886e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
5896e81090bSOded Gabbay 
5906e81090bSOded Gabbay 	} while (cur_size > 0);
5916e81090bSOded Gabbay 
59279775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
5936e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
5946e81090bSOded Gabbay 
5956e81090bSOded Gabbay 	/* Mark the chunks as allocated */
5966e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
5976e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
5986e81090bSOded Gabbay 		found++)
5996e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
6006e81090bSOded Gabbay 
6016e81090bSOded Gabbay kfd_gtt_out:
6026e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
6036e81090bSOded Gabbay 	return 0;
6046e81090bSOded Gabbay 
6056e81090bSOded Gabbay kfd_gtt_no_free_chunk:
60679775b62SKent Russell 	pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
6076e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
6086e81090bSOded Gabbay 	kfree(mem_obj);
6096e81090bSOded Gabbay 	return -ENOMEM;
6106e81090bSOded Gabbay }
6116e81090bSOded Gabbay 
6126e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
6136e81090bSOded Gabbay {
6146e81090bSOded Gabbay 	unsigned int bit;
6156e81090bSOded Gabbay 
6169216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
6179216ed29SOded Gabbay 	if (!mem_obj)
6189216ed29SOded Gabbay 		return 0;
6196e81090bSOded Gabbay 
62079775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
6216e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
6226e81090bSOded Gabbay 
6236e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
6246e81090bSOded Gabbay 
6256e81090bSOded Gabbay 	/* Mark the chunks as free */
6266e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
6276e81090bSOded Gabbay 		bit <= mem_obj->range_end;
6286e81090bSOded Gabbay 		bit++)
6296e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
6306e81090bSOded Gabbay 
6316e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
6326e81090bSOded Gabbay 
6336e81090bSOded Gabbay 	kfree(mem_obj);
6346e81090bSOded Gabbay 	return 0;
6356e81090bSOded Gabbay }
636