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" 284a488a7aSOded Gabbay 2919f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768 3019f6d2a6SOded Gabbay 314a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = { 324a488a7aSOded Gabbay .max_pasid_bits = 16, 3319f6d2a6SOded Gabbay .mqd_size_aligned = MQD_SIZE_ALIGNED 344a488a7aSOded Gabbay }; 354a488a7aSOded Gabbay 364a488a7aSOded Gabbay struct kfd_deviceid { 374a488a7aSOded Gabbay unsigned short did; 384a488a7aSOded Gabbay const struct kfd_device_info *device_info; 394a488a7aSOded Gabbay }; 404a488a7aSOded Gabbay 414a488a7aSOded Gabbay /* Please keep this sorted by increasing device id. */ 424a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = { 434a488a7aSOded Gabbay { 0x1304, &kaveri_device_info }, /* Kaveri */ 444a488a7aSOded Gabbay { 0x1305, &kaveri_device_info }, /* Kaveri */ 454a488a7aSOded Gabbay { 0x1306, &kaveri_device_info }, /* Kaveri */ 464a488a7aSOded Gabbay { 0x1307, &kaveri_device_info }, /* Kaveri */ 474a488a7aSOded Gabbay { 0x1309, &kaveri_device_info }, /* Kaveri */ 484a488a7aSOded Gabbay { 0x130A, &kaveri_device_info }, /* Kaveri */ 494a488a7aSOded Gabbay { 0x130B, &kaveri_device_info }, /* Kaveri */ 504a488a7aSOded Gabbay { 0x130C, &kaveri_device_info }, /* Kaveri */ 514a488a7aSOded Gabbay { 0x130D, &kaveri_device_info }, /* Kaveri */ 524a488a7aSOded Gabbay { 0x130E, &kaveri_device_info }, /* Kaveri */ 534a488a7aSOded Gabbay { 0x130F, &kaveri_device_info }, /* Kaveri */ 544a488a7aSOded Gabbay { 0x1310, &kaveri_device_info }, /* Kaveri */ 554a488a7aSOded Gabbay { 0x1311, &kaveri_device_info }, /* Kaveri */ 564a488a7aSOded Gabbay { 0x1312, &kaveri_device_info }, /* Kaveri */ 574a488a7aSOded Gabbay { 0x1313, &kaveri_device_info }, /* Kaveri */ 584a488a7aSOded Gabbay { 0x1315, &kaveri_device_info }, /* Kaveri */ 594a488a7aSOded Gabbay { 0x1316, &kaveri_device_info }, /* Kaveri */ 604a488a7aSOded Gabbay { 0x1317, &kaveri_device_info }, /* Kaveri */ 614a488a7aSOded Gabbay { 0x1318, &kaveri_device_info }, /* Kaveri */ 624a488a7aSOded Gabbay { 0x131B, &kaveri_device_info }, /* Kaveri */ 634a488a7aSOded Gabbay { 0x131C, &kaveri_device_info }, /* Kaveri */ 644a488a7aSOded Gabbay { 0x131D, &kaveri_device_info }, /* Kaveri */ 654a488a7aSOded Gabbay }; 664a488a7aSOded Gabbay 674a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did) 684a488a7aSOded Gabbay { 694a488a7aSOded Gabbay size_t i; 704a488a7aSOded Gabbay 714a488a7aSOded Gabbay for (i = 0; i < ARRAY_SIZE(supported_devices); i++) { 724a488a7aSOded Gabbay if (supported_devices[i].did == did) { 734a488a7aSOded Gabbay BUG_ON(supported_devices[i].device_info == NULL); 744a488a7aSOded Gabbay return supported_devices[i].device_info; 754a488a7aSOded Gabbay } 764a488a7aSOded Gabbay } 774a488a7aSOded Gabbay 784a488a7aSOded Gabbay return NULL; 794a488a7aSOded Gabbay } 804a488a7aSOded Gabbay 814a488a7aSOded Gabbay struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev) 824a488a7aSOded Gabbay { 834a488a7aSOded Gabbay struct kfd_dev *kfd; 844a488a7aSOded Gabbay 854a488a7aSOded Gabbay const struct kfd_device_info *device_info = 864a488a7aSOded Gabbay lookup_device_info(pdev->device); 874a488a7aSOded Gabbay 884a488a7aSOded Gabbay if (!device_info) 894a488a7aSOded Gabbay return NULL; 904a488a7aSOded Gabbay 914a488a7aSOded Gabbay kfd = kzalloc(sizeof(*kfd), GFP_KERNEL); 924a488a7aSOded Gabbay if (!kfd) 934a488a7aSOded Gabbay return NULL; 944a488a7aSOded Gabbay 954a488a7aSOded Gabbay kfd->kgd = kgd; 964a488a7aSOded Gabbay kfd->device_info = device_info; 974a488a7aSOded Gabbay kfd->pdev = pdev; 9819f6d2a6SOded Gabbay kfd->init_complete = false; 994a488a7aSOded Gabbay 1004a488a7aSOded Gabbay return kfd; 1014a488a7aSOded Gabbay } 1024a488a7aSOded Gabbay 103*b17f068aSOded Gabbay static bool device_iommu_pasid_init(struct kfd_dev *kfd) 104*b17f068aSOded Gabbay { 105*b17f068aSOded Gabbay const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP | 106*b17f068aSOded Gabbay AMD_IOMMU_DEVICE_FLAG_PRI_SUP | 107*b17f068aSOded Gabbay AMD_IOMMU_DEVICE_FLAG_PASID_SUP; 108*b17f068aSOded Gabbay 109*b17f068aSOded Gabbay struct amd_iommu_device_info iommu_info; 110*b17f068aSOded Gabbay unsigned int pasid_limit; 111*b17f068aSOded Gabbay int err; 112*b17f068aSOded Gabbay 113*b17f068aSOded Gabbay err = amd_iommu_device_info(kfd->pdev, &iommu_info); 114*b17f068aSOded Gabbay if (err < 0) { 115*b17f068aSOded Gabbay dev_err(kfd_device, 116*b17f068aSOded Gabbay "error getting iommu info. is the iommu enabled?\n"); 117*b17f068aSOded Gabbay return false; 118*b17f068aSOded Gabbay } 119*b17f068aSOded Gabbay 120*b17f068aSOded Gabbay if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) { 121*b17f068aSOded Gabbay dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n", 122*b17f068aSOded Gabbay (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0, 123*b17f068aSOded Gabbay (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0, 124*b17f068aSOded Gabbay (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) != 0); 125*b17f068aSOded Gabbay return false; 126*b17f068aSOded Gabbay } 127*b17f068aSOded Gabbay 128*b17f068aSOded Gabbay pasid_limit = min_t(unsigned int, 129*b17f068aSOded Gabbay (unsigned int)1 << kfd->device_info->max_pasid_bits, 130*b17f068aSOded Gabbay iommu_info.max_pasids); 131*b17f068aSOded Gabbay /* 132*b17f068aSOded Gabbay * last pasid is used for kernel queues doorbells 133*b17f068aSOded Gabbay * in the future the last pasid might be used for a kernel thread. 134*b17f068aSOded Gabbay */ 135*b17f068aSOded Gabbay pasid_limit = min_t(unsigned int, 136*b17f068aSOded Gabbay pasid_limit, 137*b17f068aSOded Gabbay kfd->doorbell_process_limit - 1); 138*b17f068aSOded Gabbay 139*b17f068aSOded Gabbay err = amd_iommu_init_device(kfd->pdev, pasid_limit); 140*b17f068aSOded Gabbay if (err < 0) { 141*b17f068aSOded Gabbay dev_err(kfd_device, "error initializing iommu device\n"); 142*b17f068aSOded Gabbay return false; 143*b17f068aSOded Gabbay } 144*b17f068aSOded Gabbay 145*b17f068aSOded Gabbay if (!kfd_set_pasid_limit(pasid_limit)) { 146*b17f068aSOded Gabbay dev_err(kfd_device, "error setting pasid limit\n"); 147*b17f068aSOded Gabbay amd_iommu_free_device(kfd->pdev); 148*b17f068aSOded Gabbay return false; 149*b17f068aSOded Gabbay } 150*b17f068aSOded Gabbay 151*b17f068aSOded Gabbay return true; 152*b17f068aSOded Gabbay } 153*b17f068aSOded Gabbay 154*b17f068aSOded Gabbay static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid) 155*b17f068aSOded Gabbay { 156*b17f068aSOded Gabbay struct kfd_dev *dev = kfd_device_by_pci_dev(pdev); 157*b17f068aSOded Gabbay 158*b17f068aSOded Gabbay if (dev) 159*b17f068aSOded Gabbay kfd_unbind_process_from_device(dev, pasid); 160*b17f068aSOded Gabbay } 161*b17f068aSOded Gabbay 1624a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd, 1634a488a7aSOded Gabbay const struct kgd2kfd_shared_resources *gpu_resources) 1644a488a7aSOded Gabbay { 16519f6d2a6SOded Gabbay unsigned int size; 16619f6d2a6SOded Gabbay 1674a488a7aSOded Gabbay kfd->shared_resources = *gpu_resources; 1684a488a7aSOded Gabbay 16919f6d2a6SOded Gabbay /* calculate max size of mqds needed for queues */ 17019f6d2a6SOded Gabbay size = max_num_of_processes * 17119f6d2a6SOded Gabbay max_num_of_queues_per_process * 17219f6d2a6SOded Gabbay kfd->device_info->mqd_size_aligned; 17319f6d2a6SOded Gabbay 17419f6d2a6SOded Gabbay /* add another 512KB for all other allocations on gart */ 17519f6d2a6SOded Gabbay size += 512 * 1024; 17619f6d2a6SOded Gabbay 17719f6d2a6SOded Gabbay if (kfd2kgd->init_sa_manager(kfd->kgd, size)) { 17819f6d2a6SOded Gabbay dev_err(kfd_device, 17919f6d2a6SOded Gabbay "Error initializing sa manager for device (%x:%x)\n", 18019f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 18119f6d2a6SOded Gabbay goto out; 18219f6d2a6SOded Gabbay } 18319f6d2a6SOded Gabbay 18419f6d2a6SOded Gabbay kfd_doorbell_init(kfd); 18519f6d2a6SOded Gabbay 18619f6d2a6SOded Gabbay if (kfd_topology_add_device(kfd) != 0) { 18719f6d2a6SOded Gabbay dev_err(kfd_device, 18819f6d2a6SOded Gabbay "Error adding device (%x:%x) to topology\n", 18919f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 19019f6d2a6SOded Gabbay goto kfd_topology_add_device_error; 19119f6d2a6SOded Gabbay } 19219f6d2a6SOded Gabbay 193*b17f068aSOded Gabbay if (!device_iommu_pasid_init(kfd)) { 194*b17f068aSOded Gabbay dev_err(kfd_device, 195*b17f068aSOded Gabbay "Error initializing iommuv2 for device (%x:%x)\n", 196*b17f068aSOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 197*b17f068aSOded Gabbay goto device_iommu_pasid_error; 198*b17f068aSOded Gabbay } 199*b17f068aSOded Gabbay amd_iommu_set_invalidate_ctx_cb(kfd->pdev, 200*b17f068aSOded Gabbay iommu_pasid_shutdown_callback); 2015b5c4e40SEvgeny Pinchuk 2024a488a7aSOded Gabbay kfd->init_complete = true; 2034a488a7aSOded Gabbay dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor, 2044a488a7aSOded Gabbay kfd->pdev->device); 2054a488a7aSOded Gabbay 20619f6d2a6SOded Gabbay goto out; 20719f6d2a6SOded Gabbay 208*b17f068aSOded Gabbay device_iommu_pasid_error: 209*b17f068aSOded Gabbay kfd_topology_remove_device(kfd); 21019f6d2a6SOded Gabbay kfd_topology_add_device_error: 21119f6d2a6SOded Gabbay kfd2kgd->fini_sa_manager(kfd->kgd); 21219f6d2a6SOded Gabbay dev_err(kfd_device, 21319f6d2a6SOded Gabbay "device (%x:%x) NOT added due to errors\n", 21419f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 21519f6d2a6SOded Gabbay out: 21619f6d2a6SOded Gabbay return kfd->init_complete; 2174a488a7aSOded Gabbay } 2184a488a7aSOded Gabbay 2194a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd) 2204a488a7aSOded Gabbay { 221*b17f068aSOded Gabbay if (kfd->init_complete) { 222*b17f068aSOded Gabbay amd_iommu_free_device(kfd->pdev); 22319f6d2a6SOded Gabbay kfd_topology_remove_device(kfd); 224*b17f068aSOded Gabbay } 2255b5c4e40SEvgeny Pinchuk 2264a488a7aSOded Gabbay kfree(kfd); 2274a488a7aSOded Gabbay } 2284a488a7aSOded Gabbay 2294a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd) 2304a488a7aSOded Gabbay { 2314a488a7aSOded Gabbay BUG_ON(kfd == NULL); 232*b17f068aSOded Gabbay 233*b17f068aSOded Gabbay if (kfd->init_complete) 234*b17f068aSOded Gabbay amd_iommu_free_device(kfd->pdev); 2354a488a7aSOded Gabbay } 2364a488a7aSOded Gabbay 2374a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd) 2384a488a7aSOded Gabbay { 239*b17f068aSOded Gabbay unsigned int pasid_limit; 240*b17f068aSOded Gabbay int err; 241*b17f068aSOded Gabbay 2424a488a7aSOded Gabbay BUG_ON(kfd == NULL); 2434a488a7aSOded Gabbay 244*b17f068aSOded Gabbay pasid_limit = kfd_get_pasid_limit(); 245*b17f068aSOded Gabbay 246*b17f068aSOded Gabbay if (kfd->init_complete) { 247*b17f068aSOded Gabbay err = amd_iommu_init_device(kfd->pdev, pasid_limit); 248*b17f068aSOded Gabbay if (err < 0) 249*b17f068aSOded Gabbay return -ENXIO; 250*b17f068aSOded Gabbay amd_iommu_set_invalidate_ctx_cb(kfd->pdev, 251*b17f068aSOded Gabbay iommu_pasid_shutdown_callback); 252*b17f068aSOded Gabbay } 253*b17f068aSOded Gabbay 2544a488a7aSOded Gabbay return 0; 2554a488a7aSOded Gabbay } 2564a488a7aSOded Gabbay 2574a488a7aSOded Gabbay void kgd2kfd_interrupt(struct kfd_dev *dev, const void *ih_ring_entry) 2584a488a7aSOded Gabbay { 2594a488a7aSOded Gabbay } 260