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" 294a488a7aSOded Gabbay 3019f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768 3119f6d2a6SOded Gabbay 324a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = { 334a488a7aSOded Gabbay .max_pasid_bits = 16, 34b3f5e6b4SAndrew Lewycky .ih_ring_entry_size = 4 * sizeof(uint32_t), 3519f6d2a6SOded Gabbay .mqd_size_aligned = MQD_SIZE_ALIGNED 364a488a7aSOded Gabbay }; 374a488a7aSOded Gabbay 384a488a7aSOded Gabbay struct kfd_deviceid { 394a488a7aSOded Gabbay unsigned short did; 404a488a7aSOded Gabbay const struct kfd_device_info *device_info; 414a488a7aSOded Gabbay }; 424a488a7aSOded Gabbay 434a488a7aSOded Gabbay /* Please keep this sorted by increasing device id. */ 444a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = { 454a488a7aSOded Gabbay { 0x1304, &kaveri_device_info }, /* Kaveri */ 464a488a7aSOded Gabbay { 0x1305, &kaveri_device_info }, /* Kaveri */ 474a488a7aSOded Gabbay { 0x1306, &kaveri_device_info }, /* Kaveri */ 484a488a7aSOded Gabbay { 0x1307, &kaveri_device_info }, /* Kaveri */ 494a488a7aSOded Gabbay { 0x1309, &kaveri_device_info }, /* Kaveri */ 504a488a7aSOded Gabbay { 0x130A, &kaveri_device_info }, /* Kaveri */ 514a488a7aSOded Gabbay { 0x130B, &kaveri_device_info }, /* Kaveri */ 524a488a7aSOded Gabbay { 0x130C, &kaveri_device_info }, /* Kaveri */ 534a488a7aSOded Gabbay { 0x130D, &kaveri_device_info }, /* Kaveri */ 544a488a7aSOded Gabbay { 0x130E, &kaveri_device_info }, /* Kaveri */ 554a488a7aSOded Gabbay { 0x130F, &kaveri_device_info }, /* Kaveri */ 564a488a7aSOded Gabbay { 0x1310, &kaveri_device_info }, /* Kaveri */ 574a488a7aSOded Gabbay { 0x1311, &kaveri_device_info }, /* Kaveri */ 584a488a7aSOded Gabbay { 0x1312, &kaveri_device_info }, /* Kaveri */ 594a488a7aSOded Gabbay { 0x1313, &kaveri_device_info }, /* Kaveri */ 604a488a7aSOded Gabbay { 0x1315, &kaveri_device_info }, /* Kaveri */ 614a488a7aSOded Gabbay { 0x1316, &kaveri_device_info }, /* Kaveri */ 624a488a7aSOded Gabbay { 0x1317, &kaveri_device_info }, /* Kaveri */ 634a488a7aSOded Gabbay { 0x1318, &kaveri_device_info }, /* Kaveri */ 644a488a7aSOded Gabbay { 0x131B, &kaveri_device_info }, /* Kaveri */ 654a488a7aSOded Gabbay { 0x131C, &kaveri_device_info }, /* Kaveri */ 664a488a7aSOded Gabbay { 0x131D, &kaveri_device_info }, /* Kaveri */ 674a488a7aSOded Gabbay }; 684a488a7aSOded Gabbay 694a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did) 704a488a7aSOded Gabbay { 714a488a7aSOded Gabbay size_t i; 724a488a7aSOded Gabbay 734a488a7aSOded Gabbay for (i = 0; i < ARRAY_SIZE(supported_devices); i++) { 744a488a7aSOded Gabbay if (supported_devices[i].did == did) { 754a488a7aSOded Gabbay BUG_ON(supported_devices[i].device_info == NULL); 764a488a7aSOded Gabbay return supported_devices[i].device_info; 774a488a7aSOded Gabbay } 784a488a7aSOded Gabbay } 794a488a7aSOded Gabbay 804a488a7aSOded Gabbay return NULL; 814a488a7aSOded Gabbay } 824a488a7aSOded Gabbay 834a488a7aSOded Gabbay struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev) 844a488a7aSOded Gabbay { 854a488a7aSOded Gabbay struct kfd_dev *kfd; 864a488a7aSOded Gabbay 874a488a7aSOded Gabbay const struct kfd_device_info *device_info = 884a488a7aSOded Gabbay lookup_device_info(pdev->device); 894a488a7aSOded Gabbay 904a488a7aSOded Gabbay if (!device_info) 914a488a7aSOded Gabbay return NULL; 924a488a7aSOded Gabbay 934a488a7aSOded Gabbay kfd = kzalloc(sizeof(*kfd), GFP_KERNEL); 944a488a7aSOded Gabbay if (!kfd) 954a488a7aSOded Gabbay return NULL; 964a488a7aSOded Gabbay 974a488a7aSOded Gabbay kfd->kgd = kgd; 984a488a7aSOded Gabbay kfd->device_info = device_info; 994a488a7aSOded Gabbay kfd->pdev = pdev; 10019f6d2a6SOded Gabbay kfd->init_complete = false; 1014a488a7aSOded Gabbay 1024a488a7aSOded Gabbay return kfd; 1034a488a7aSOded Gabbay } 1044a488a7aSOded Gabbay 105b17f068aSOded Gabbay static bool device_iommu_pasid_init(struct kfd_dev *kfd) 106b17f068aSOded Gabbay { 107b17f068aSOded Gabbay const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP | 108b17f068aSOded Gabbay AMD_IOMMU_DEVICE_FLAG_PRI_SUP | 109b17f068aSOded Gabbay AMD_IOMMU_DEVICE_FLAG_PASID_SUP; 110b17f068aSOded Gabbay 111b17f068aSOded Gabbay struct amd_iommu_device_info iommu_info; 112b17f068aSOded Gabbay unsigned int pasid_limit; 113b17f068aSOded Gabbay int err; 114b17f068aSOded Gabbay 115b17f068aSOded Gabbay err = amd_iommu_device_info(kfd->pdev, &iommu_info); 116b17f068aSOded Gabbay if (err < 0) { 117b17f068aSOded Gabbay dev_err(kfd_device, 118b17f068aSOded Gabbay "error getting iommu info. is the iommu enabled?\n"); 119b17f068aSOded Gabbay return false; 120b17f068aSOded Gabbay } 121b17f068aSOded Gabbay 122b17f068aSOded Gabbay if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) { 123b17f068aSOded Gabbay dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n", 124b17f068aSOded Gabbay (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0, 125b17f068aSOded Gabbay (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0, 126b17f068aSOded Gabbay (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) != 0); 127b17f068aSOded Gabbay return false; 128b17f068aSOded Gabbay } 129b17f068aSOded Gabbay 130b17f068aSOded Gabbay pasid_limit = min_t(unsigned int, 131b17f068aSOded Gabbay (unsigned int)1 << kfd->device_info->max_pasid_bits, 132b17f068aSOded Gabbay iommu_info.max_pasids); 133b17f068aSOded Gabbay /* 134b17f068aSOded Gabbay * last pasid is used for kernel queues doorbells 135b17f068aSOded Gabbay * in the future the last pasid might be used for a kernel thread. 136b17f068aSOded Gabbay */ 137b17f068aSOded Gabbay pasid_limit = min_t(unsigned int, 138b17f068aSOded Gabbay pasid_limit, 139b17f068aSOded Gabbay kfd->doorbell_process_limit - 1); 140b17f068aSOded Gabbay 141b17f068aSOded Gabbay err = amd_iommu_init_device(kfd->pdev, pasid_limit); 142b17f068aSOded Gabbay if (err < 0) { 143b17f068aSOded Gabbay dev_err(kfd_device, "error initializing iommu device\n"); 144b17f068aSOded Gabbay return false; 145b17f068aSOded Gabbay } 146b17f068aSOded Gabbay 147b17f068aSOded Gabbay if (!kfd_set_pasid_limit(pasid_limit)) { 148b17f068aSOded Gabbay dev_err(kfd_device, "error setting pasid limit\n"); 149b17f068aSOded Gabbay amd_iommu_free_device(kfd->pdev); 150b17f068aSOded Gabbay return false; 151b17f068aSOded Gabbay } 152b17f068aSOded Gabbay 153b17f068aSOded Gabbay return true; 154b17f068aSOded Gabbay } 155b17f068aSOded Gabbay 156b17f068aSOded Gabbay static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid) 157b17f068aSOded Gabbay { 158b17f068aSOded Gabbay struct kfd_dev *dev = kfd_device_by_pci_dev(pdev); 159b17f068aSOded Gabbay 160b17f068aSOded Gabbay if (dev) 161b17f068aSOded Gabbay kfd_unbind_process_from_device(dev, pasid); 162b17f068aSOded Gabbay } 163b17f068aSOded Gabbay 1644a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd, 1654a488a7aSOded Gabbay const struct kgd2kfd_shared_resources *gpu_resources) 1664a488a7aSOded Gabbay { 16719f6d2a6SOded Gabbay unsigned int size; 16819f6d2a6SOded Gabbay 1694a488a7aSOded Gabbay kfd->shared_resources = *gpu_resources; 1704a488a7aSOded Gabbay 17119f6d2a6SOded Gabbay /* calculate max size of mqds needed for queues */ 17219f6d2a6SOded Gabbay size = max_num_of_processes * 17319f6d2a6SOded Gabbay max_num_of_queues_per_process * 17419f6d2a6SOded Gabbay kfd->device_info->mqd_size_aligned; 17519f6d2a6SOded Gabbay 17619f6d2a6SOded Gabbay /* add another 512KB for all other allocations on gart */ 17719f6d2a6SOded Gabbay size += 512 * 1024; 17819f6d2a6SOded Gabbay 17919f6d2a6SOded Gabbay if (kfd2kgd->init_sa_manager(kfd->kgd, size)) { 18019f6d2a6SOded Gabbay dev_err(kfd_device, 18119f6d2a6SOded Gabbay "Error initializing sa manager for device (%x:%x)\n", 18219f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 18319f6d2a6SOded Gabbay goto out; 18419f6d2a6SOded Gabbay } 18519f6d2a6SOded Gabbay 18619f6d2a6SOded Gabbay kfd_doorbell_init(kfd); 18719f6d2a6SOded Gabbay 18819f6d2a6SOded Gabbay if (kfd_topology_add_device(kfd) != 0) { 18919f6d2a6SOded Gabbay dev_err(kfd_device, 19019f6d2a6SOded Gabbay "Error adding device (%x:%x) to topology\n", 19119f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 19219f6d2a6SOded Gabbay goto kfd_topology_add_device_error; 19319f6d2a6SOded Gabbay } 19419f6d2a6SOded Gabbay 195b3f5e6b4SAndrew Lewycky if (kfd_interrupt_init(kfd)) { 196b3f5e6b4SAndrew Lewycky dev_err(kfd_device, 197b3f5e6b4SAndrew Lewycky "Error initializing interrupts for device (%x:%x)\n", 198b3f5e6b4SAndrew Lewycky kfd->pdev->vendor, kfd->pdev->device); 199b3f5e6b4SAndrew Lewycky goto kfd_interrupt_error; 200b3f5e6b4SAndrew Lewycky } 201b3f5e6b4SAndrew Lewycky 202b17f068aSOded Gabbay if (!device_iommu_pasid_init(kfd)) { 203b17f068aSOded Gabbay dev_err(kfd_device, 204b17f068aSOded Gabbay "Error initializing iommuv2 for device (%x:%x)\n", 205b17f068aSOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 206b17f068aSOded Gabbay goto device_iommu_pasid_error; 207b17f068aSOded Gabbay } 208b17f068aSOded Gabbay amd_iommu_set_invalidate_ctx_cb(kfd->pdev, 209b17f068aSOded Gabbay iommu_pasid_shutdown_callback); 2105b5c4e40SEvgeny Pinchuk 21164c7f8cfSBen Goz kfd->dqm = device_queue_manager_init(kfd); 21264c7f8cfSBen Goz if (!kfd->dqm) { 21364c7f8cfSBen Goz dev_err(kfd_device, 21464c7f8cfSBen Goz "Error initializing queue manager for device (%x:%x)\n", 21564c7f8cfSBen Goz kfd->pdev->vendor, kfd->pdev->device); 21664c7f8cfSBen Goz goto device_queue_manager_error; 21764c7f8cfSBen Goz } 21864c7f8cfSBen Goz 21964c7f8cfSBen Goz if (kfd->dqm->start(kfd->dqm) != 0) { 22064c7f8cfSBen Goz dev_err(kfd_device, 22164c7f8cfSBen Goz "Error starting queuen manager for device (%x:%x)\n", 22264c7f8cfSBen Goz kfd->pdev->vendor, kfd->pdev->device); 22364c7f8cfSBen Goz goto dqm_start_error; 22464c7f8cfSBen Goz } 22564c7f8cfSBen Goz 2264a488a7aSOded Gabbay kfd->init_complete = true; 2274a488a7aSOded Gabbay dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor, 2284a488a7aSOded Gabbay kfd->pdev->device); 2294a488a7aSOded Gabbay 23064c7f8cfSBen Goz pr_debug("kfd: Starting kfd with the following scheduling policy %d\n", 23164c7f8cfSBen Goz sched_policy); 23264c7f8cfSBen Goz 23319f6d2a6SOded Gabbay goto out; 23419f6d2a6SOded Gabbay 23564c7f8cfSBen Goz dqm_start_error: 23664c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 23764c7f8cfSBen Goz device_queue_manager_error: 23864c7f8cfSBen Goz amd_iommu_free_device(kfd->pdev); 239b17f068aSOded Gabbay device_iommu_pasid_error: 240b3f5e6b4SAndrew Lewycky kfd_interrupt_exit(kfd); 241b3f5e6b4SAndrew Lewycky kfd_interrupt_error: 242b17f068aSOded Gabbay kfd_topology_remove_device(kfd); 24319f6d2a6SOded Gabbay kfd_topology_add_device_error: 24419f6d2a6SOded Gabbay kfd2kgd->fini_sa_manager(kfd->kgd); 24519f6d2a6SOded Gabbay dev_err(kfd_device, 24619f6d2a6SOded Gabbay "device (%x:%x) NOT added due to errors\n", 24719f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 24819f6d2a6SOded Gabbay out: 24919f6d2a6SOded Gabbay return kfd->init_complete; 2504a488a7aSOded Gabbay } 2514a488a7aSOded Gabbay 2524a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd) 2534a488a7aSOded Gabbay { 254b17f068aSOded Gabbay if (kfd->init_complete) { 25564c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 256b17f068aSOded Gabbay amd_iommu_free_device(kfd->pdev); 257b3f5e6b4SAndrew Lewycky kfd_interrupt_exit(kfd); 25819f6d2a6SOded Gabbay kfd_topology_remove_device(kfd); 259b17f068aSOded Gabbay } 2605b5c4e40SEvgeny Pinchuk 2614a488a7aSOded Gabbay kfree(kfd); 2624a488a7aSOded Gabbay } 2634a488a7aSOded Gabbay 2644a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd) 2654a488a7aSOded Gabbay { 2664a488a7aSOded Gabbay BUG_ON(kfd == NULL); 267b17f068aSOded Gabbay 26864c7f8cfSBen Goz if (kfd->init_complete) { 26964c7f8cfSBen Goz kfd->dqm->stop(kfd->dqm); 270*abc9d3e3SOded Gabbay amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL); 271b17f068aSOded Gabbay amd_iommu_free_device(kfd->pdev); 2724a488a7aSOded Gabbay } 27364c7f8cfSBen Goz } 2744a488a7aSOded Gabbay 2754a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd) 2764a488a7aSOded Gabbay { 277b17f068aSOded Gabbay unsigned int pasid_limit; 278b17f068aSOded Gabbay int err; 279b17f068aSOded Gabbay 2804a488a7aSOded Gabbay BUG_ON(kfd == NULL); 2814a488a7aSOded Gabbay 282b17f068aSOded Gabbay pasid_limit = kfd_get_pasid_limit(); 283b17f068aSOded Gabbay 284b17f068aSOded Gabbay if (kfd->init_complete) { 285b17f068aSOded Gabbay err = amd_iommu_init_device(kfd->pdev, pasid_limit); 286b17f068aSOded Gabbay if (err < 0) 287b17f068aSOded Gabbay return -ENXIO; 288b17f068aSOded Gabbay amd_iommu_set_invalidate_ctx_cb(kfd->pdev, 289b17f068aSOded Gabbay iommu_pasid_shutdown_callback); 29064c7f8cfSBen Goz kfd->dqm->start(kfd->dqm); 291b17f068aSOded Gabbay } 292b17f068aSOded Gabbay 2934a488a7aSOded Gabbay return 0; 2944a488a7aSOded Gabbay } 2954a488a7aSOded Gabbay 296b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */ 297b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry) 2984a488a7aSOded Gabbay { 299b3f5e6b4SAndrew Lewycky if (kfd->init_complete) { 300b3f5e6b4SAndrew Lewycky spin_lock(&kfd->interrupt_lock); 301b3f5e6b4SAndrew Lewycky 302b3f5e6b4SAndrew Lewycky if (kfd->interrupts_active 303b3f5e6b4SAndrew Lewycky && enqueue_ih_ring_entry(kfd, ih_ring_entry)) 304b3f5e6b4SAndrew Lewycky schedule_work(&kfd->interrupt_work); 305b3f5e6b4SAndrew Lewycky 306b3f5e6b4SAndrew Lewycky spin_unlock(&kfd->interrupt_lock); 307b3f5e6b4SAndrew Lewycky } 3084a488a7aSOded Gabbay } 309