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" 29e18e794eSOded Gabbay #include "kfd_pm4_headers.h" 304a488a7aSOded Gabbay 3119f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768 3219f6d2a6SOded Gabbay 334a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = { 340da7558cSBen Goz .asic_family = CHIP_KAVERI, 350da7558cSBen Goz .max_pasid_bits = 16, 360da7558cSBen Goz .ih_ring_entry_size = 4 * sizeof(uint32_t), 37*f3a39818SAndrew Lewycky .event_interrupt_class = &event_interrupt_class_cik, 380da7558cSBen Goz .mqd_size_aligned = MQD_SIZE_ALIGNED 390da7558cSBen Goz }; 400da7558cSBen Goz 410da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = { 420da7558cSBen Goz .asic_family = CHIP_CARRIZO, 434a488a7aSOded Gabbay .max_pasid_bits = 16, 44b3f5e6b4SAndrew Lewycky .ih_ring_entry_size = 4 * sizeof(uint32_t), 45f7c826adSAlexey Skidanov .num_of_watch_points = 4, 4619f6d2a6SOded Gabbay .mqd_size_aligned = MQD_SIZE_ALIGNED 474a488a7aSOded Gabbay }; 484a488a7aSOded Gabbay 494a488a7aSOded Gabbay struct kfd_deviceid { 504a488a7aSOded Gabbay unsigned short did; 514a488a7aSOded Gabbay const struct kfd_device_info *device_info; 524a488a7aSOded Gabbay }; 534a488a7aSOded Gabbay 544a488a7aSOded Gabbay /* Please keep this sorted by increasing device id. */ 554a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = { 564a488a7aSOded Gabbay { 0x1304, &kaveri_device_info }, /* Kaveri */ 574a488a7aSOded Gabbay { 0x1305, &kaveri_device_info }, /* Kaveri */ 584a488a7aSOded Gabbay { 0x1306, &kaveri_device_info }, /* Kaveri */ 594a488a7aSOded Gabbay { 0x1307, &kaveri_device_info }, /* Kaveri */ 604a488a7aSOded Gabbay { 0x1309, &kaveri_device_info }, /* Kaveri */ 614a488a7aSOded Gabbay { 0x130A, &kaveri_device_info }, /* Kaveri */ 624a488a7aSOded Gabbay { 0x130B, &kaveri_device_info }, /* Kaveri */ 634a488a7aSOded Gabbay { 0x130C, &kaveri_device_info }, /* Kaveri */ 644a488a7aSOded Gabbay { 0x130D, &kaveri_device_info }, /* Kaveri */ 654a488a7aSOded Gabbay { 0x130E, &kaveri_device_info }, /* Kaveri */ 664a488a7aSOded Gabbay { 0x130F, &kaveri_device_info }, /* Kaveri */ 674a488a7aSOded Gabbay { 0x1310, &kaveri_device_info }, /* Kaveri */ 684a488a7aSOded Gabbay { 0x1311, &kaveri_device_info }, /* Kaveri */ 694a488a7aSOded Gabbay { 0x1312, &kaveri_device_info }, /* Kaveri */ 704a488a7aSOded Gabbay { 0x1313, &kaveri_device_info }, /* Kaveri */ 714a488a7aSOded Gabbay { 0x1315, &kaveri_device_info }, /* Kaveri */ 724a488a7aSOded Gabbay { 0x1316, &kaveri_device_info }, /* Kaveri */ 734a488a7aSOded Gabbay { 0x1317, &kaveri_device_info }, /* Kaveri */ 744a488a7aSOded Gabbay { 0x1318, &kaveri_device_info }, /* Kaveri */ 754a488a7aSOded Gabbay { 0x131B, &kaveri_device_info }, /* Kaveri */ 764a488a7aSOded Gabbay { 0x131C, &kaveri_device_info }, /* Kaveri */ 770da7558cSBen Goz { 0x131D, &kaveri_device_info } /* Kaveri */ 784a488a7aSOded Gabbay }; 794a488a7aSOded Gabbay 806e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 816e81090bSOded Gabbay unsigned int chunk_size); 826e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd); 836e81090bSOded Gabbay 844a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did) 854a488a7aSOded Gabbay { 864a488a7aSOded Gabbay size_t i; 874a488a7aSOded Gabbay 884a488a7aSOded Gabbay for (i = 0; i < ARRAY_SIZE(supported_devices); i++) { 894a488a7aSOded Gabbay if (supported_devices[i].did == did) { 904a488a7aSOded Gabbay BUG_ON(supported_devices[i].device_info == NULL); 914a488a7aSOded Gabbay return supported_devices[i].device_info; 924a488a7aSOded Gabbay } 934a488a7aSOded Gabbay } 944a488a7aSOded Gabbay 954a488a7aSOded Gabbay return NULL; 964a488a7aSOded Gabbay } 974a488a7aSOded Gabbay 98cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, 99cea405b1SXihan Zhang struct pci_dev *pdev, const struct kfd2kgd_calls *f2g) 1004a488a7aSOded Gabbay { 1014a488a7aSOded Gabbay struct kfd_dev *kfd; 1024a488a7aSOded Gabbay 1034a488a7aSOded Gabbay const struct kfd_device_info *device_info = 1044a488a7aSOded Gabbay lookup_device_info(pdev->device); 1054a488a7aSOded Gabbay 1064a488a7aSOded Gabbay if (!device_info) 1074a488a7aSOded Gabbay return NULL; 1084a488a7aSOded Gabbay 1094a488a7aSOded Gabbay kfd = kzalloc(sizeof(*kfd), GFP_KERNEL); 1104a488a7aSOded Gabbay if (!kfd) 1114a488a7aSOded Gabbay return NULL; 1124a488a7aSOded Gabbay 1134a488a7aSOded Gabbay kfd->kgd = kgd; 1144a488a7aSOded Gabbay kfd->device_info = device_info; 1154a488a7aSOded Gabbay kfd->pdev = pdev; 11619f6d2a6SOded Gabbay kfd->init_complete = false; 117cea405b1SXihan Zhang kfd->kfd2kgd = f2g; 118cea405b1SXihan Zhang 119cea405b1SXihan Zhang mutex_init(&kfd->doorbell_mutex); 120cea405b1SXihan Zhang memset(&kfd->doorbell_available_index, 0, 121cea405b1SXihan Zhang sizeof(kfd->doorbell_available_index)); 1224a488a7aSOded Gabbay 1234a488a7aSOded Gabbay return kfd; 1244a488a7aSOded Gabbay } 1254a488a7aSOded Gabbay 126b17f068aSOded Gabbay static bool device_iommu_pasid_init(struct kfd_dev *kfd) 127b17f068aSOded Gabbay { 128b17f068aSOded Gabbay const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP | 129b17f068aSOded Gabbay AMD_IOMMU_DEVICE_FLAG_PRI_SUP | 130b17f068aSOded Gabbay AMD_IOMMU_DEVICE_FLAG_PASID_SUP; 131b17f068aSOded Gabbay 132b17f068aSOded Gabbay struct amd_iommu_device_info iommu_info; 133b17f068aSOded Gabbay unsigned int pasid_limit; 134b17f068aSOded Gabbay int err; 135b17f068aSOded Gabbay 136b17f068aSOded Gabbay err = amd_iommu_device_info(kfd->pdev, &iommu_info); 137b17f068aSOded Gabbay if (err < 0) { 138b17f068aSOded Gabbay dev_err(kfd_device, 139b17f068aSOded Gabbay "error getting iommu info. is the iommu enabled?\n"); 140b17f068aSOded Gabbay return false; 141b17f068aSOded Gabbay } 142b17f068aSOded Gabbay 143b17f068aSOded Gabbay if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) { 144b17f068aSOded Gabbay dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n", 145b17f068aSOded Gabbay (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0, 146b17f068aSOded Gabbay (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0, 147b17f068aSOded Gabbay (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) != 0); 148b17f068aSOded Gabbay return false; 149b17f068aSOded Gabbay } 150b17f068aSOded Gabbay 151b17f068aSOded Gabbay pasid_limit = min_t(unsigned int, 152b17f068aSOded Gabbay (unsigned int)1 << kfd->device_info->max_pasid_bits, 153b17f068aSOded Gabbay iommu_info.max_pasids); 154b17f068aSOded Gabbay /* 155b17f068aSOded Gabbay * last pasid is used for kernel queues doorbells 156b17f068aSOded Gabbay * in the future the last pasid might be used for a kernel thread. 157b17f068aSOded Gabbay */ 158b17f068aSOded Gabbay pasid_limit = min_t(unsigned int, 159b17f068aSOded Gabbay pasid_limit, 160b17f068aSOded Gabbay kfd->doorbell_process_limit - 1); 161b17f068aSOded Gabbay 162b17f068aSOded Gabbay err = amd_iommu_init_device(kfd->pdev, pasid_limit); 163b17f068aSOded Gabbay if (err < 0) { 164b17f068aSOded Gabbay dev_err(kfd_device, "error initializing iommu device\n"); 165b17f068aSOded Gabbay return false; 166b17f068aSOded Gabbay } 167b17f068aSOded Gabbay 168b17f068aSOded Gabbay if (!kfd_set_pasid_limit(pasid_limit)) { 169b17f068aSOded Gabbay dev_err(kfd_device, "error setting pasid limit\n"); 170b17f068aSOded Gabbay amd_iommu_free_device(kfd->pdev); 171b17f068aSOded Gabbay return false; 172b17f068aSOded Gabbay } 173b17f068aSOded Gabbay 174b17f068aSOded Gabbay return true; 175b17f068aSOded Gabbay } 176b17f068aSOded Gabbay 177b17f068aSOded Gabbay static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid) 178b17f068aSOded Gabbay { 179b17f068aSOded Gabbay struct kfd_dev *dev = kfd_device_by_pci_dev(pdev); 180b17f068aSOded Gabbay 181b17f068aSOded Gabbay if (dev) 182b17f068aSOded Gabbay kfd_unbind_process_from_device(dev, pasid); 183b17f068aSOded Gabbay } 184b17f068aSOded Gabbay 1854a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd, 1864a488a7aSOded Gabbay const struct kgd2kfd_shared_resources *gpu_resources) 1874a488a7aSOded Gabbay { 18819f6d2a6SOded Gabbay unsigned int size; 18919f6d2a6SOded Gabbay 1904a488a7aSOded Gabbay kfd->shared_resources = *gpu_resources; 1914a488a7aSOded Gabbay 19219f6d2a6SOded Gabbay /* calculate max size of mqds needed for queues */ 193b8cbab04SOded Gabbay size = max_num_of_queues_per_device * 19419f6d2a6SOded Gabbay kfd->device_info->mqd_size_aligned; 19519f6d2a6SOded Gabbay 196e18e794eSOded Gabbay /* 197e18e794eSOded Gabbay * calculate max size of runlist packet. 198e18e794eSOded Gabbay * There can be only 2 packets at once 199e18e794eSOded Gabbay */ 200b3869b17SDave Airlie size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_map_process) + 201b3869b17SDave Airlie max_num_of_queues_per_device * 202e18e794eSOded Gabbay sizeof(struct pm4_map_queues) + sizeof(struct pm4_runlist)) * 2; 203e18e794eSOded Gabbay 204e18e794eSOded Gabbay /* Add size of HIQ & DIQ */ 205e18e794eSOded Gabbay size += KFD_KERNEL_QUEUE_SIZE * 2; 206e18e794eSOded Gabbay 207e18e794eSOded Gabbay /* add another 512KB for all other allocations on gart (HPD, fences) */ 20819f6d2a6SOded Gabbay size += 512 * 1024; 20919f6d2a6SOded Gabbay 210cea405b1SXihan Zhang if (kfd->kfd2kgd->init_gtt_mem_allocation( 211cea405b1SXihan Zhang kfd->kgd, size, &kfd->gtt_mem, 21273a1da0bSOded Gabbay &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){ 21319f6d2a6SOded Gabbay dev_err(kfd_device, 214e18e794eSOded Gabbay "Could not allocate %d bytes for device (%x:%x)\n", 215e18e794eSOded Gabbay size, kfd->pdev->vendor, kfd->pdev->device); 21619f6d2a6SOded Gabbay goto out; 21719f6d2a6SOded Gabbay } 21819f6d2a6SOded Gabbay 219e18e794eSOded Gabbay dev_info(kfd_device, 220e18e794eSOded Gabbay "Allocated %d bytes on gart for device(%x:%x)\n", 221e18e794eSOded Gabbay size, kfd->pdev->vendor, kfd->pdev->device); 222e18e794eSOded Gabbay 22373a1da0bSOded Gabbay /* Initialize GTT sa with 512 byte chunk size */ 22473a1da0bSOded Gabbay if (kfd_gtt_sa_init(kfd, size, 512) != 0) { 22573a1da0bSOded Gabbay dev_err(kfd_device, 22673a1da0bSOded Gabbay "Error initializing gtt sub-allocator\n"); 22773a1da0bSOded Gabbay goto kfd_gtt_sa_init_error; 22873a1da0bSOded Gabbay } 22973a1da0bSOded Gabbay 23019f6d2a6SOded Gabbay kfd_doorbell_init(kfd); 23119f6d2a6SOded Gabbay 23219f6d2a6SOded Gabbay if (kfd_topology_add_device(kfd) != 0) { 23319f6d2a6SOded Gabbay dev_err(kfd_device, 23419f6d2a6SOded Gabbay "Error adding device (%x:%x) to topology\n", 23519f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 23619f6d2a6SOded Gabbay goto kfd_topology_add_device_error; 23719f6d2a6SOded Gabbay } 23819f6d2a6SOded Gabbay 2392249d558SAndrew Lewycky if (kfd_interrupt_init(kfd)) { 2402249d558SAndrew Lewycky dev_err(kfd_device, 2412249d558SAndrew Lewycky "Error initializing interrupts for device (%x:%x)\n", 2422249d558SAndrew Lewycky kfd->pdev->vendor, kfd->pdev->device); 2432249d558SAndrew Lewycky goto kfd_interrupt_error; 2442249d558SAndrew Lewycky } 2452249d558SAndrew Lewycky 246b17f068aSOded Gabbay if (!device_iommu_pasid_init(kfd)) { 247b17f068aSOded Gabbay dev_err(kfd_device, 248b17f068aSOded Gabbay "Error initializing iommuv2 for device (%x:%x)\n", 249b17f068aSOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 250b17f068aSOded Gabbay goto device_iommu_pasid_error; 251b17f068aSOded Gabbay } 252b17f068aSOded Gabbay amd_iommu_set_invalidate_ctx_cb(kfd->pdev, 253b17f068aSOded Gabbay iommu_pasid_shutdown_callback); 2545b5c4e40SEvgeny Pinchuk 25564c7f8cfSBen Goz kfd->dqm = device_queue_manager_init(kfd); 25664c7f8cfSBen Goz if (!kfd->dqm) { 25764c7f8cfSBen Goz dev_err(kfd_device, 25864c7f8cfSBen Goz "Error initializing queue manager for device (%x:%x)\n", 25964c7f8cfSBen Goz kfd->pdev->vendor, kfd->pdev->device); 26064c7f8cfSBen Goz goto device_queue_manager_error; 26164c7f8cfSBen Goz } 26264c7f8cfSBen Goz 26345c9a5e4SOded Gabbay if (kfd->dqm->ops.start(kfd->dqm) != 0) { 26464c7f8cfSBen Goz dev_err(kfd_device, 26564c7f8cfSBen Goz "Error starting queuen manager for device (%x:%x)\n", 26664c7f8cfSBen Goz kfd->pdev->vendor, kfd->pdev->device); 26764c7f8cfSBen Goz goto dqm_start_error; 26864c7f8cfSBen Goz } 26964c7f8cfSBen Goz 2704a488a7aSOded Gabbay kfd->init_complete = true; 2714a488a7aSOded Gabbay dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor, 2724a488a7aSOded Gabbay kfd->pdev->device); 2734a488a7aSOded Gabbay 27464c7f8cfSBen Goz pr_debug("kfd: Starting kfd with the following scheduling policy %d\n", 27564c7f8cfSBen Goz sched_policy); 27664c7f8cfSBen Goz 27719f6d2a6SOded Gabbay goto out; 27819f6d2a6SOded Gabbay 27964c7f8cfSBen Goz dqm_start_error: 28064c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 28164c7f8cfSBen Goz device_queue_manager_error: 28264c7f8cfSBen Goz amd_iommu_free_device(kfd->pdev); 283b17f068aSOded Gabbay device_iommu_pasid_error: 2842249d558SAndrew Lewycky kfd_interrupt_exit(kfd); 2852249d558SAndrew Lewycky kfd_interrupt_error: 286b17f068aSOded Gabbay kfd_topology_remove_device(kfd); 28719f6d2a6SOded Gabbay kfd_topology_add_device_error: 28873a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd); 28973a1da0bSOded Gabbay kfd_gtt_sa_init_error: 290cea405b1SXihan Zhang kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem); 29119f6d2a6SOded Gabbay dev_err(kfd_device, 29219f6d2a6SOded Gabbay "device (%x:%x) NOT added due to errors\n", 29319f6d2a6SOded Gabbay kfd->pdev->vendor, kfd->pdev->device); 29419f6d2a6SOded Gabbay out: 29519f6d2a6SOded Gabbay return kfd->init_complete; 2964a488a7aSOded Gabbay } 2974a488a7aSOded Gabbay 2984a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd) 2994a488a7aSOded Gabbay { 300b17f068aSOded Gabbay if (kfd->init_complete) { 30164c7f8cfSBen Goz device_queue_manager_uninit(kfd->dqm); 302b17f068aSOded Gabbay amd_iommu_free_device(kfd->pdev); 3032249d558SAndrew Lewycky kfd_interrupt_exit(kfd); 30419f6d2a6SOded Gabbay kfd_topology_remove_device(kfd); 30573a1da0bSOded Gabbay kfd_gtt_sa_fini(kfd); 306cea405b1SXihan Zhang kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem); 307b17f068aSOded Gabbay } 3085b5c4e40SEvgeny Pinchuk 3094a488a7aSOded Gabbay kfree(kfd); 3104a488a7aSOded Gabbay } 3114a488a7aSOded Gabbay 3124a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd) 3134a488a7aSOded Gabbay { 3144a488a7aSOded Gabbay BUG_ON(kfd == NULL); 315b17f068aSOded Gabbay 31664c7f8cfSBen Goz if (kfd->init_complete) { 31745c9a5e4SOded Gabbay kfd->dqm->ops.stop(kfd->dqm); 318abc9d3e3SOded Gabbay amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL); 319b17f068aSOded Gabbay amd_iommu_free_device(kfd->pdev); 3204a488a7aSOded Gabbay } 32164c7f8cfSBen Goz } 3224a488a7aSOded Gabbay 3234a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd) 3244a488a7aSOded Gabbay { 325b17f068aSOded Gabbay unsigned int pasid_limit; 326b17f068aSOded Gabbay int err; 327b17f068aSOded Gabbay 3284a488a7aSOded Gabbay BUG_ON(kfd == NULL); 3294a488a7aSOded Gabbay 330b17f068aSOded Gabbay pasid_limit = kfd_get_pasid_limit(); 331b17f068aSOded Gabbay 332b17f068aSOded Gabbay if (kfd->init_complete) { 333b17f068aSOded Gabbay err = amd_iommu_init_device(kfd->pdev, pasid_limit); 334b17f068aSOded Gabbay if (err < 0) 335b17f068aSOded Gabbay return -ENXIO; 336b17f068aSOded Gabbay amd_iommu_set_invalidate_ctx_cb(kfd->pdev, 337b17f068aSOded Gabbay iommu_pasid_shutdown_callback); 33845c9a5e4SOded Gabbay kfd->dqm->ops.start(kfd->dqm); 339b17f068aSOded Gabbay } 340b17f068aSOded Gabbay 3414a488a7aSOded Gabbay return 0; 3424a488a7aSOded Gabbay } 3434a488a7aSOded Gabbay 344b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */ 345b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry) 3464a488a7aSOded Gabbay { 3472249d558SAndrew Lewycky if (!kfd->init_complete) 3482249d558SAndrew Lewycky return; 3492249d558SAndrew Lewycky 3502249d558SAndrew Lewycky spin_lock(&kfd->interrupt_lock); 3512249d558SAndrew Lewycky 3522249d558SAndrew Lewycky if (kfd->interrupts_active 3532249d558SAndrew Lewycky && interrupt_is_wanted(kfd, ih_ring_entry) 3542249d558SAndrew Lewycky && enqueue_ih_ring_entry(kfd, ih_ring_entry)) 3552249d558SAndrew Lewycky schedule_work(&kfd->interrupt_work); 3562249d558SAndrew Lewycky 3572249d558SAndrew Lewycky spin_unlock(&kfd->interrupt_lock); 3584a488a7aSOded Gabbay } 3596e81090bSOded Gabbay 3606e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 3616e81090bSOded Gabbay unsigned int chunk_size) 3626e81090bSOded Gabbay { 3636e81090bSOded Gabbay unsigned int num_of_bits; 3646e81090bSOded Gabbay 3656e81090bSOded Gabbay BUG_ON(!kfd); 3666e81090bSOded Gabbay BUG_ON(!kfd->gtt_mem); 3676e81090bSOded Gabbay BUG_ON(buf_size < chunk_size); 3686e81090bSOded Gabbay BUG_ON(buf_size == 0); 3696e81090bSOded Gabbay BUG_ON(chunk_size == 0); 3706e81090bSOded Gabbay 3716e81090bSOded Gabbay kfd->gtt_sa_chunk_size = chunk_size; 3726e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks = buf_size / chunk_size; 3736e81090bSOded Gabbay 3746e81090bSOded Gabbay num_of_bits = kfd->gtt_sa_num_of_chunks / BITS_PER_BYTE; 3756e81090bSOded Gabbay BUG_ON(num_of_bits == 0); 3766e81090bSOded Gabbay 3776e81090bSOded Gabbay kfd->gtt_sa_bitmap = kzalloc(num_of_bits, GFP_KERNEL); 3786e81090bSOded Gabbay 3796e81090bSOded Gabbay if (!kfd->gtt_sa_bitmap) 3806e81090bSOded Gabbay return -ENOMEM; 3816e81090bSOded Gabbay 3826e81090bSOded Gabbay pr_debug("kfd: gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n", 3836e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap); 3846e81090bSOded Gabbay 3856e81090bSOded Gabbay mutex_init(&kfd->gtt_sa_lock); 3866e81090bSOded Gabbay 3876e81090bSOded Gabbay return 0; 3886e81090bSOded Gabbay 3896e81090bSOded Gabbay } 3906e81090bSOded Gabbay 3916e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd) 3926e81090bSOded Gabbay { 3936e81090bSOded Gabbay mutex_destroy(&kfd->gtt_sa_lock); 3946e81090bSOded Gabbay kfree(kfd->gtt_sa_bitmap); 3956e81090bSOded Gabbay } 3966e81090bSOded Gabbay 3976e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr, 3986e81090bSOded Gabbay unsigned int bit_num, 3996e81090bSOded Gabbay unsigned int chunk_size) 4006e81090bSOded Gabbay { 4016e81090bSOded Gabbay return start_addr + bit_num * chunk_size; 4026e81090bSOded Gabbay } 4036e81090bSOded Gabbay 4046e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr, 4056e81090bSOded Gabbay unsigned int bit_num, 4066e81090bSOded Gabbay unsigned int chunk_size) 4076e81090bSOded Gabbay { 4086e81090bSOded Gabbay return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size); 4096e81090bSOded Gabbay } 4106e81090bSOded Gabbay 4116e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size, 4126e81090bSOded Gabbay struct kfd_mem_obj **mem_obj) 4136e81090bSOded Gabbay { 4146e81090bSOded Gabbay unsigned int found, start_search, cur_size; 4156e81090bSOded Gabbay 4166e81090bSOded Gabbay BUG_ON(!kfd); 4176e81090bSOded Gabbay 4186e81090bSOded Gabbay if (size == 0) 4196e81090bSOded Gabbay return -EINVAL; 4206e81090bSOded Gabbay 4216e81090bSOded Gabbay if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size) 4226e81090bSOded Gabbay return -ENOMEM; 4236e81090bSOded Gabbay 4246e81090bSOded Gabbay *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL); 4256e81090bSOded Gabbay if ((*mem_obj) == NULL) 4266e81090bSOded Gabbay return -ENOMEM; 4276e81090bSOded Gabbay 4286e81090bSOded Gabbay pr_debug("kfd: allocated mem_obj = %p for size = %d\n", *mem_obj, size); 4296e81090bSOded Gabbay 4306e81090bSOded Gabbay start_search = 0; 4316e81090bSOded Gabbay 4326e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock); 4336e81090bSOded Gabbay 4346e81090bSOded Gabbay kfd_gtt_restart_search: 4356e81090bSOded Gabbay /* Find the first chunk that is free */ 4366e81090bSOded Gabbay found = find_next_zero_bit(kfd->gtt_sa_bitmap, 4376e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, 4386e81090bSOded Gabbay start_search); 4396e81090bSOded Gabbay 4406e81090bSOded Gabbay pr_debug("kfd: found = %d\n", found); 4416e81090bSOded Gabbay 4426e81090bSOded Gabbay /* If there wasn't any free chunk, bail out */ 4436e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks) 4446e81090bSOded Gabbay goto kfd_gtt_no_free_chunk; 4456e81090bSOded Gabbay 4466e81090bSOded Gabbay /* Update fields of mem_obj */ 4476e81090bSOded Gabbay (*mem_obj)->range_start = found; 4486e81090bSOded Gabbay (*mem_obj)->range_end = found; 4496e81090bSOded Gabbay (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr( 4506e81090bSOded Gabbay kfd->gtt_start_gpu_addr, 4516e81090bSOded Gabbay found, 4526e81090bSOded Gabbay kfd->gtt_sa_chunk_size); 4536e81090bSOded Gabbay (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr( 4546e81090bSOded Gabbay kfd->gtt_start_cpu_ptr, 4556e81090bSOded Gabbay found, 4566e81090bSOded Gabbay kfd->gtt_sa_chunk_size); 4576e81090bSOded Gabbay 4586e81090bSOded Gabbay pr_debug("kfd: gpu_addr = %p, cpu_addr = %p\n", 4596e81090bSOded Gabbay (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr); 4606e81090bSOded Gabbay 4616e81090bSOded Gabbay /* If we need only one chunk, mark it as allocated and get out */ 4626e81090bSOded Gabbay if (size <= kfd->gtt_sa_chunk_size) { 4636e81090bSOded Gabbay pr_debug("kfd: single bit\n"); 4646e81090bSOded Gabbay set_bit(found, kfd->gtt_sa_bitmap); 4656e81090bSOded Gabbay goto kfd_gtt_out; 4666e81090bSOded Gabbay } 4676e81090bSOded Gabbay 4686e81090bSOded Gabbay /* Otherwise, try to see if we have enough contiguous chunks */ 4696e81090bSOded Gabbay cur_size = size - kfd->gtt_sa_chunk_size; 4706e81090bSOded Gabbay do { 4716e81090bSOded Gabbay (*mem_obj)->range_end = 4726e81090bSOded Gabbay find_next_zero_bit(kfd->gtt_sa_bitmap, 4736e81090bSOded Gabbay kfd->gtt_sa_num_of_chunks, ++found); 4746e81090bSOded Gabbay /* 4756e81090bSOded Gabbay * If next free chunk is not contiguous than we need to 4766e81090bSOded Gabbay * restart our search from the last free chunk we found (which 4776e81090bSOded Gabbay * wasn't contiguous to the previous ones 4786e81090bSOded Gabbay */ 4796e81090bSOded Gabbay if ((*mem_obj)->range_end != found) { 4806e81090bSOded Gabbay start_search = found; 4816e81090bSOded Gabbay goto kfd_gtt_restart_search; 4826e81090bSOded Gabbay } 4836e81090bSOded Gabbay 4846e81090bSOded Gabbay /* 4856e81090bSOded Gabbay * If we reached end of buffer, bail out with error 4866e81090bSOded Gabbay */ 4876e81090bSOded Gabbay if (found == kfd->gtt_sa_num_of_chunks) 4886e81090bSOded Gabbay goto kfd_gtt_no_free_chunk; 4896e81090bSOded Gabbay 4906e81090bSOded Gabbay /* Check if we don't need another chunk */ 4916e81090bSOded Gabbay if (cur_size <= kfd->gtt_sa_chunk_size) 4926e81090bSOded Gabbay cur_size = 0; 4936e81090bSOded Gabbay else 4946e81090bSOded Gabbay cur_size -= kfd->gtt_sa_chunk_size; 4956e81090bSOded Gabbay 4966e81090bSOded Gabbay } while (cur_size > 0); 4976e81090bSOded Gabbay 4986e81090bSOded Gabbay pr_debug("kfd: range_start = %d, range_end = %d\n", 4996e81090bSOded Gabbay (*mem_obj)->range_start, (*mem_obj)->range_end); 5006e81090bSOded Gabbay 5016e81090bSOded Gabbay /* Mark the chunks as allocated */ 5026e81090bSOded Gabbay for (found = (*mem_obj)->range_start; 5036e81090bSOded Gabbay found <= (*mem_obj)->range_end; 5046e81090bSOded Gabbay found++) 5056e81090bSOded Gabbay set_bit(found, kfd->gtt_sa_bitmap); 5066e81090bSOded Gabbay 5076e81090bSOded Gabbay kfd_gtt_out: 5086e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 5096e81090bSOded Gabbay return 0; 5106e81090bSOded Gabbay 5116e81090bSOded Gabbay kfd_gtt_no_free_chunk: 5126e81090bSOded Gabbay pr_debug("kfd: allocation failed with mem_obj = %p\n", mem_obj); 5136e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 5146e81090bSOded Gabbay kfree(mem_obj); 5156e81090bSOded Gabbay return -ENOMEM; 5166e81090bSOded Gabbay } 5176e81090bSOded Gabbay 5186e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj) 5196e81090bSOded Gabbay { 5206e81090bSOded Gabbay unsigned int bit; 5216e81090bSOded Gabbay 5226e81090bSOded Gabbay BUG_ON(!kfd); 5239216ed29SOded Gabbay 5249216ed29SOded Gabbay /* Act like kfree when trying to free a NULL object */ 5259216ed29SOded Gabbay if (!mem_obj) 5269216ed29SOded Gabbay return 0; 5276e81090bSOded Gabbay 5286e81090bSOded Gabbay pr_debug("kfd: free mem_obj = %p, range_start = %d, range_end = %d\n", 5296e81090bSOded Gabbay mem_obj, mem_obj->range_start, mem_obj->range_end); 5306e81090bSOded Gabbay 5316e81090bSOded Gabbay mutex_lock(&kfd->gtt_sa_lock); 5326e81090bSOded Gabbay 5336e81090bSOded Gabbay /* Mark the chunks as free */ 5346e81090bSOded Gabbay for (bit = mem_obj->range_start; 5356e81090bSOded Gabbay bit <= mem_obj->range_end; 5366e81090bSOded Gabbay bit++) 5376e81090bSOded Gabbay clear_bit(bit, kfd->gtt_sa_bitmap); 5386e81090bSOded Gabbay 5396e81090bSOded Gabbay mutex_unlock(&kfd->gtt_sa_lock); 5406e81090bSOded Gabbay 5416e81090bSOded Gabbay kfree(mem_obj); 5426e81090bSOded Gabbay return 0; 5436e81090bSOded Gabbay } 544