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