Path: blob/master/drivers/gpu/drm/amd/amdkfd/kfd_device.c
29285 views
// SPDX-License-Identifier: GPL-2.0 OR MIT1/*2* Copyright 2014-2022 Advanced Micro Devices, Inc.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be included in12* all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR18* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,19* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR20* OTHER DEALINGS IN THE SOFTWARE.21*/2223#include <linux/bsearch.h>24#include <linux/pci.h>25#include <linux/slab.h>26#include "kfd_priv.h"27#include "kfd_device_queue_manager.h"28#include "kfd_pm4_headers_vi.h"29#include "kfd_pm4_headers_aldebaran.h"30#include "cwsr_trap_handler.h"31#include "amdgpu_amdkfd.h"32#include "kfd_smi_events.h"33#include "kfd_svm.h"34#include "kfd_migrate.h"35#include "amdgpu.h"36#include "amdgpu_xcp.h"3738#define MQD_SIZE_ALIGNED 7683940/*41* kfd_locked is used to lock the kfd driver during suspend or reset42* once locked, kfd driver will stop any further GPU execution.43* create process (open) will return -EAGAIN.44*/45static int kfd_locked;4647#ifdef CONFIG_DRM_AMDGPU_CIK48extern const struct kfd2kgd_calls gfx_v7_kfd2kgd;49#endif50extern const struct kfd2kgd_calls gfx_v8_kfd2kgd;51extern const struct kfd2kgd_calls gfx_v9_kfd2kgd;52extern const struct kfd2kgd_calls arcturus_kfd2kgd;53extern const struct kfd2kgd_calls aldebaran_kfd2kgd;54extern const struct kfd2kgd_calls gc_9_4_3_kfd2kgd;55extern const struct kfd2kgd_calls gfx_v10_kfd2kgd;56extern const struct kfd2kgd_calls gfx_v10_3_kfd2kgd;57extern const struct kfd2kgd_calls gfx_v11_kfd2kgd;58extern const struct kfd2kgd_calls gfx_v12_kfd2kgd;5960static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,61unsigned int chunk_size);62static void kfd_gtt_sa_fini(struct kfd_dev *kfd);6364static int kfd_resume(struct kfd_node *kfd);6566static void kfd_device_info_set_sdma_info(struct kfd_dev *kfd)67{68uint32_t sdma_version = amdgpu_ip_version(kfd->adev, SDMA0_HWIP, 0);6970switch (sdma_version) {71case IP_VERSION(4, 0, 0):/* VEGA10 */72case IP_VERSION(4, 0, 1):/* VEGA12 */73case IP_VERSION(4, 1, 0):/* RAVEN */74case IP_VERSION(4, 1, 1):/* RAVEN */75case IP_VERSION(4, 1, 2):/* RENOIR */76case IP_VERSION(5, 2, 1):/* VANGOGH */77case IP_VERSION(5, 2, 3):/* YELLOW_CARP */78case IP_VERSION(5, 2, 6):/* GC 10.3.6 */79case IP_VERSION(5, 2, 7):/* GC 10.3.7 */80kfd->device_info.num_sdma_queues_per_engine = 2;81break;82case IP_VERSION(4, 2, 0):/* VEGA20 */83case IP_VERSION(4, 2, 2):/* ARCTURUS */84case IP_VERSION(4, 4, 0):/* ALDEBARAN */85case IP_VERSION(4, 4, 2):86case IP_VERSION(4, 4, 5):87case IP_VERSION(4, 4, 4):88case IP_VERSION(5, 0, 0):/* NAVI10 */89case IP_VERSION(5, 0, 1):/* CYAN_SKILLFISH */90case IP_VERSION(5, 0, 2):/* NAVI14 */91case IP_VERSION(5, 0, 5):/* NAVI12 */92case IP_VERSION(5, 2, 0):/* SIENNA_CICHLID */93case IP_VERSION(5, 2, 2):/* NAVY_FLOUNDER */94case IP_VERSION(5, 2, 4):/* DIMGREY_CAVEFISH */95case IP_VERSION(5, 2, 5):/* BEIGE_GOBY */96case IP_VERSION(6, 0, 0):97case IP_VERSION(6, 0, 1):98case IP_VERSION(6, 0, 2):99case IP_VERSION(6, 0, 3):100case IP_VERSION(6, 1, 0):101case IP_VERSION(6, 1, 1):102case IP_VERSION(6, 1, 2):103case IP_VERSION(6, 1, 3):104case IP_VERSION(7, 0, 0):105case IP_VERSION(7, 0, 1):106kfd->device_info.num_sdma_queues_per_engine = 8;107break;108default:109dev_warn(kfd_device,110"Default sdma queue per engine(8) is set due to mismatch of sdma ip block(SDMA_HWIP:0x%x).\n",111sdma_version);112kfd->device_info.num_sdma_queues_per_engine = 8;113}114115bitmap_zero(kfd->device_info.reserved_sdma_queues_bitmap, KFD_MAX_SDMA_QUEUES);116117switch (sdma_version) {118case IP_VERSION(6, 0, 0):119case IP_VERSION(6, 0, 1):120case IP_VERSION(6, 0, 2):121case IP_VERSION(6, 0, 3):122case IP_VERSION(6, 1, 0):123case IP_VERSION(6, 1, 1):124case IP_VERSION(6, 1, 2):125case IP_VERSION(6, 1, 3):126case IP_VERSION(7, 0, 0):127case IP_VERSION(7, 0, 1):128/* Reserve 1 for paging and 1 for gfx */129kfd->device_info.num_reserved_sdma_queues_per_engine = 2;130/* BIT(0)=engine-0 queue-0; BIT(1)=engine-1 queue-0; BIT(2)=engine-0 queue-1; ... */131bitmap_set(kfd->device_info.reserved_sdma_queues_bitmap, 0,132kfd->adev->sdma.num_instances *133kfd->device_info.num_reserved_sdma_queues_per_engine);134break;135default:136break;137}138}139140static void kfd_device_info_set_event_interrupt_class(struct kfd_dev *kfd)141{142uint32_t gc_version = KFD_GC_VERSION(kfd);143144switch (gc_version) {145case IP_VERSION(9, 0, 1): /* VEGA10 */146case IP_VERSION(9, 1, 0): /* RAVEN */147case IP_VERSION(9, 2, 1): /* VEGA12 */148case IP_VERSION(9, 2, 2): /* RAVEN */149case IP_VERSION(9, 3, 0): /* RENOIR */150case IP_VERSION(9, 4, 0): /* VEGA20 */151case IP_VERSION(9, 4, 1): /* ARCTURUS */152case IP_VERSION(9, 4, 2): /* ALDEBARAN */153kfd->device_info.event_interrupt_class = &event_interrupt_class_v9;154break;155case IP_VERSION(9, 4, 3): /* GC 9.4.3 */156case IP_VERSION(9, 4, 4): /* GC 9.4.4 */157case IP_VERSION(9, 5, 0): /* GC 9.5.0 */158kfd->device_info.event_interrupt_class =159&event_interrupt_class_v9_4_3;160break;161case IP_VERSION(10, 3, 1): /* VANGOGH */162case IP_VERSION(10, 3, 3): /* YELLOW_CARP */163case IP_VERSION(10, 3, 6): /* GC 10.3.6 */164case IP_VERSION(10, 3, 7): /* GC 10.3.7 */165case IP_VERSION(10, 1, 3): /* CYAN_SKILLFISH */166case IP_VERSION(10, 1, 4):167case IP_VERSION(10, 1, 10): /* NAVI10 */168case IP_VERSION(10, 1, 2): /* NAVI12 */169case IP_VERSION(10, 1, 1): /* NAVI14 */170case IP_VERSION(10, 3, 0): /* SIENNA_CICHLID */171case IP_VERSION(10, 3, 2): /* NAVY_FLOUNDER */172case IP_VERSION(10, 3, 4): /* DIMGREY_CAVEFISH */173case IP_VERSION(10, 3, 5): /* BEIGE_GOBY */174kfd->device_info.event_interrupt_class = &event_interrupt_class_v10;175break;176case IP_VERSION(11, 0, 0):177case IP_VERSION(11, 0, 1):178case IP_VERSION(11, 0, 2):179case IP_VERSION(11, 0, 3):180case IP_VERSION(11, 0, 4):181case IP_VERSION(11, 5, 0):182case IP_VERSION(11, 5, 1):183case IP_VERSION(11, 5, 2):184case IP_VERSION(11, 5, 3):185kfd->device_info.event_interrupt_class = &event_interrupt_class_v11;186break;187case IP_VERSION(12, 0, 0):188case IP_VERSION(12, 0, 1):189/* GFX12_TODO: Change to v12 version. */190kfd->device_info.event_interrupt_class = &event_interrupt_class_v11;191break;192default:193dev_warn(kfd_device, "v9 event interrupt handler is set due to "194"mismatch of gc ip block(GC_HWIP:0x%x).\n", gc_version);195kfd->device_info.event_interrupt_class = &event_interrupt_class_v9;196}197}198199static void kfd_device_info_init(struct kfd_dev *kfd,200bool vf, uint32_t gfx_target_version)201{202uint32_t gc_version = KFD_GC_VERSION(kfd);203uint32_t asic_type = kfd->adev->asic_type;204205kfd->device_info.max_pasid_bits = 16;206kfd->device_info.max_no_of_hqd = 24;207kfd->device_info.num_of_watch_points = 4;208kfd->device_info.mqd_size_aligned = MQD_SIZE_ALIGNED;209kfd->device_info.gfx_target_version = gfx_target_version;210211if (KFD_IS_SOC15(kfd)) {212kfd->device_info.doorbell_size = 8;213kfd->device_info.ih_ring_entry_size = 8 * sizeof(uint32_t);214kfd->device_info.supports_cwsr = true;215216kfd_device_info_set_sdma_info(kfd);217218kfd_device_info_set_event_interrupt_class(kfd);219220if (gc_version < IP_VERSION(11, 0, 0)) {221/* Navi2x+, Navi1x+ */222if (gc_version == IP_VERSION(10, 3, 6))223kfd->device_info.no_atomic_fw_version = 14;224else if (gc_version == IP_VERSION(10, 3, 7))225kfd->device_info.no_atomic_fw_version = 3;226else if (gc_version >= IP_VERSION(10, 3, 0))227kfd->device_info.no_atomic_fw_version = 92;228else if (gc_version >= IP_VERSION(10, 1, 1))229kfd->device_info.no_atomic_fw_version = 145;230231/* Navi1x+ */232if (gc_version >= IP_VERSION(10, 1, 1))233kfd->device_info.needs_pci_atomics = true;234} else if (gc_version < IP_VERSION(12, 0, 0)) {235/*236* PCIe atomics support acknowledgment in GFX11 RS64 CPFW requires237* MEC version >= 509. Prior RS64 CPFW versions (and all F32) require238* PCIe atomics support.239*/240kfd->device_info.needs_pci_atomics = true;241kfd->device_info.no_atomic_fw_version = kfd->adev->gfx.rs64_enable ? 509 : 0;242} else if (gc_version < IP_VERSION(13, 0, 0)) {243kfd->device_info.needs_pci_atomics = true;244kfd->device_info.no_atomic_fw_version = 2090;245} else {246kfd->device_info.needs_pci_atomics = true;247}248} else {249kfd->device_info.doorbell_size = 4;250kfd->device_info.ih_ring_entry_size = 4 * sizeof(uint32_t);251kfd->device_info.event_interrupt_class = &event_interrupt_class_cik;252kfd->device_info.num_sdma_queues_per_engine = 2;253254if (asic_type != CHIP_KAVERI &&255asic_type != CHIP_HAWAII &&256asic_type != CHIP_TONGA)257kfd->device_info.supports_cwsr = true;258259if (asic_type != CHIP_HAWAII && !vf)260kfd->device_info.needs_pci_atomics = true;261}262}263264struct kfd_dev *kgd2kfd_probe(struct amdgpu_device *adev, bool vf)265{266struct kfd_dev *kfd = NULL;267const struct kfd2kgd_calls *f2g = NULL;268uint32_t gfx_target_version = 0;269270switch (adev->asic_type) {271#ifdef CONFIG_DRM_AMDGPU_CIK272case CHIP_KAVERI:273gfx_target_version = 70000;274if (!vf)275f2g = &gfx_v7_kfd2kgd;276break;277#endif278case CHIP_CARRIZO:279gfx_target_version = 80001;280if (!vf)281f2g = &gfx_v8_kfd2kgd;282break;283#ifdef CONFIG_DRM_AMDGPU_CIK284case CHIP_HAWAII:285gfx_target_version = 70001;286if (!amdgpu_exp_hw_support)287pr_info(288"KFD support on Hawaii is experimental. See modparam exp_hw_support\n"289);290else if (!vf)291f2g = &gfx_v7_kfd2kgd;292break;293#endif294case CHIP_TONGA:295gfx_target_version = 80002;296if (!vf)297f2g = &gfx_v8_kfd2kgd;298break;299case CHIP_FIJI:300case CHIP_POLARIS10:301gfx_target_version = 80003;302f2g = &gfx_v8_kfd2kgd;303break;304case CHIP_POLARIS11:305case CHIP_POLARIS12:306case CHIP_VEGAM:307gfx_target_version = 80003;308if (!vf)309f2g = &gfx_v8_kfd2kgd;310break;311default:312switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {313/* Vega 10 */314case IP_VERSION(9, 0, 1):315gfx_target_version = 90000;316f2g = &gfx_v9_kfd2kgd;317break;318/* Raven */319case IP_VERSION(9, 1, 0):320case IP_VERSION(9, 2, 2):321gfx_target_version = 90002;322if (!vf)323f2g = &gfx_v9_kfd2kgd;324break;325/* Vega12 */326case IP_VERSION(9, 2, 1):327gfx_target_version = 90004;328if (!vf)329f2g = &gfx_v9_kfd2kgd;330break;331/* Renoir */332case IP_VERSION(9, 3, 0):333gfx_target_version = 90012;334if (!vf)335f2g = &gfx_v9_kfd2kgd;336break;337/* Vega20 */338case IP_VERSION(9, 4, 0):339gfx_target_version = 90006;340if (!vf)341f2g = &gfx_v9_kfd2kgd;342break;343/* Arcturus */344case IP_VERSION(9, 4, 1):345gfx_target_version = 90008;346f2g = &arcturus_kfd2kgd;347break;348/* Aldebaran */349case IP_VERSION(9, 4, 2):350gfx_target_version = 90010;351f2g = &aldebaran_kfd2kgd;352break;353case IP_VERSION(9, 4, 3):354case IP_VERSION(9, 4, 4):355gfx_target_version = 90402;356f2g = &gc_9_4_3_kfd2kgd;357break;358case IP_VERSION(9, 5, 0):359gfx_target_version = 90500;360f2g = &gc_9_4_3_kfd2kgd;361break;362/* Navi10 */363case IP_VERSION(10, 1, 10):364gfx_target_version = 100100;365if (!vf)366f2g = &gfx_v10_kfd2kgd;367break;368/* Navi12 */369case IP_VERSION(10, 1, 2):370gfx_target_version = 100101;371f2g = &gfx_v10_kfd2kgd;372break;373/* Navi14 */374case IP_VERSION(10, 1, 1):375gfx_target_version = 100102;376if (!vf)377f2g = &gfx_v10_kfd2kgd;378break;379/* Cyan Skillfish */380case IP_VERSION(10, 1, 3):381case IP_VERSION(10, 1, 4):382gfx_target_version = 100103;383if (!vf)384f2g = &gfx_v10_kfd2kgd;385break;386/* Sienna Cichlid */387case IP_VERSION(10, 3, 0):388gfx_target_version = 100300;389f2g = &gfx_v10_3_kfd2kgd;390break;391/* Navy Flounder */392case IP_VERSION(10, 3, 2):393gfx_target_version = 100301;394f2g = &gfx_v10_3_kfd2kgd;395break;396/* Van Gogh */397case IP_VERSION(10, 3, 1):398gfx_target_version = 100303;399if (!vf)400f2g = &gfx_v10_3_kfd2kgd;401break;402/* Dimgrey Cavefish */403case IP_VERSION(10, 3, 4):404gfx_target_version = 100302;405f2g = &gfx_v10_3_kfd2kgd;406break;407/* Beige Goby */408case IP_VERSION(10, 3, 5):409gfx_target_version = 100304;410f2g = &gfx_v10_3_kfd2kgd;411break;412/* Yellow Carp */413case IP_VERSION(10, 3, 3):414gfx_target_version = 100305;415if (!vf)416f2g = &gfx_v10_3_kfd2kgd;417break;418case IP_VERSION(10, 3, 6):419case IP_VERSION(10, 3, 7):420gfx_target_version = 100306;421if (!vf)422f2g = &gfx_v10_3_kfd2kgd;423break;424case IP_VERSION(11, 0, 0):425gfx_target_version = 110000;426f2g = &gfx_v11_kfd2kgd;427break;428case IP_VERSION(11, 0, 1):429case IP_VERSION(11, 0, 4):430gfx_target_version = 110003;431f2g = &gfx_v11_kfd2kgd;432break;433case IP_VERSION(11, 0, 2):434gfx_target_version = 110002;435f2g = &gfx_v11_kfd2kgd;436break;437case IP_VERSION(11, 0, 3):438/* Note: Compiler version is 11.0.1 while HW version is 11.0.3 */439gfx_target_version = 110001;440f2g = &gfx_v11_kfd2kgd;441break;442case IP_VERSION(11, 5, 0):443gfx_target_version = 110500;444f2g = &gfx_v11_kfd2kgd;445break;446case IP_VERSION(11, 5, 1):447gfx_target_version = 110501;448f2g = &gfx_v11_kfd2kgd;449break;450case IP_VERSION(11, 5, 2):451gfx_target_version = 110502;452f2g = &gfx_v11_kfd2kgd;453break;454case IP_VERSION(11, 5, 3):455gfx_target_version = 110503;456f2g = &gfx_v11_kfd2kgd;457break;458case IP_VERSION(12, 0, 0):459gfx_target_version = 120000;460f2g = &gfx_v12_kfd2kgd;461break;462case IP_VERSION(12, 0, 1):463gfx_target_version = 120001;464f2g = &gfx_v12_kfd2kgd;465break;466default:467break;468}469break;470}471472if (!f2g) {473if (amdgpu_ip_version(adev, GC_HWIP, 0))474dev_info(kfd_device,475"GC IP %06x %s not supported in kfd\n",476amdgpu_ip_version(adev, GC_HWIP, 0),477vf ? "VF" : "");478else479dev_info(kfd_device, "%s %s not supported in kfd\n",480amdgpu_asic_name[adev->asic_type], vf ? "VF" : "");481return NULL;482}483484kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);485if (!kfd)486return NULL;487488kfd->adev = adev;489kfd_device_info_init(kfd, vf, gfx_target_version);490kfd->init_complete = false;491kfd->kfd2kgd = f2g;492atomic_set(&kfd->compute_profile, 0);493494mutex_init(&kfd->doorbell_mutex);495496ida_init(&kfd->doorbell_ida);497atomic_set(&kfd->kfd_processes_count, 0);498499return kfd;500}501502static void kfd_cwsr_init(struct kfd_dev *kfd)503{504if (cwsr_enable && kfd->device_info.supports_cwsr) {505if (KFD_GC_VERSION(kfd) < IP_VERSION(9, 0, 1)) {506BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex)507> KFD_CWSR_TMA_OFFSET);508kfd->cwsr_isa = cwsr_trap_gfx8_hex;509kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);510} else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)) {511BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex)512> KFD_CWSR_TMA_OFFSET);513kfd->cwsr_isa = cwsr_trap_arcturus_hex;514kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);515} else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)) {516BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex)517> KFD_CWSR_TMA_OFFSET);518kfd->cwsr_isa = cwsr_trap_aldebaran_hex;519kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex);520} else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 3) ||521KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 4)) {522BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_4_3_hex)523> KFD_CWSR_TMA_OFFSET);524kfd->cwsr_isa = cwsr_trap_gfx9_4_3_hex;525kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_4_3_hex);526} else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 5, 0)) {527BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_5_0_hex) > PAGE_SIZE);528kfd->cwsr_isa = cwsr_trap_gfx9_5_0_hex;529kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_5_0_hex);530} else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 1, 1)) {531BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex)532> KFD_CWSR_TMA_OFFSET);533kfd->cwsr_isa = cwsr_trap_gfx9_hex;534kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);535} else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 3, 0)) {536BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex)537> KFD_CWSR_TMA_OFFSET);538kfd->cwsr_isa = cwsr_trap_nv1x_hex;539kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);540} else if (KFD_GC_VERSION(kfd) < IP_VERSION(11, 0, 0)) {541BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex)542> KFD_CWSR_TMA_OFFSET);543kfd->cwsr_isa = cwsr_trap_gfx10_hex;544kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);545} else if (KFD_GC_VERSION(kfd) < IP_VERSION(12, 0, 0)) {546/* The gfx11 cwsr trap handler must fit inside a single547page. */548BUILD_BUG_ON(sizeof(cwsr_trap_gfx11_hex) > PAGE_SIZE);549kfd->cwsr_isa = cwsr_trap_gfx11_hex;550kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx11_hex);551} else {552BUILD_BUG_ON(sizeof(cwsr_trap_gfx12_hex)553> KFD_CWSR_TMA_OFFSET);554kfd->cwsr_isa = cwsr_trap_gfx12_hex;555kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx12_hex);556}557558kfd->cwsr_enabled = true;559}560}561562static int kfd_gws_init(struct kfd_node *node)563{564int ret = 0;565struct kfd_dev *kfd = node->kfd;566uint32_t mes_rev = node->adev->mes.sched_version & AMDGPU_MES_VERSION_MASK;567568if (node->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)569return 0;570571if (hws_gws_support || (KFD_IS_SOC15(node) &&572((KFD_GC_VERSION(node) == IP_VERSION(9, 0, 1)573&& kfd->mec2_fw_version >= 0x81b3) ||574(KFD_GC_VERSION(node) <= IP_VERSION(9, 4, 0)575&& kfd->mec2_fw_version >= 0x1b3) ||576(KFD_GC_VERSION(node) == IP_VERSION(9, 4, 1)577&& kfd->mec2_fw_version >= 0x30) ||578(KFD_GC_VERSION(node) == IP_VERSION(9, 4, 2)579&& kfd->mec2_fw_version >= 0x28) ||580(KFD_GC_VERSION(node) == IP_VERSION(9, 4, 3) ||581KFD_GC_VERSION(node) == IP_VERSION(9, 4, 4)) ||582(KFD_GC_VERSION(node) == IP_VERSION(9, 5, 0)) ||583(KFD_GC_VERSION(node) >= IP_VERSION(10, 3, 0)584&& KFD_GC_VERSION(node) < IP_VERSION(11, 0, 0)585&& kfd->mec2_fw_version >= 0x6b) ||586(KFD_GC_VERSION(node) >= IP_VERSION(11, 0, 0)587&& KFD_GC_VERSION(node) < IP_VERSION(12, 0, 0)588&& mes_rev >= 68) ||589(KFD_GC_VERSION(node) >= IP_VERSION(12, 0, 0))))) {590if (KFD_GC_VERSION(node) >= IP_VERSION(12, 0, 0))591node->adev->gds.gws_size = 64;592ret = amdgpu_amdkfd_alloc_gws(node->adev,593node->adev->gds.gws_size, &node->gws);594}595596return ret;597}598599static void kfd_smi_init(struct kfd_node *dev)600{601INIT_LIST_HEAD(&dev->smi_clients);602spin_lock_init(&dev->smi_lock);603}604605static int kfd_init_node(struct kfd_node *node)606{607int err = -1;608609if (kfd_interrupt_init(node)) {610dev_err(kfd_device, "Error initializing interrupts\n");611goto kfd_interrupt_error;612}613614node->dqm = device_queue_manager_init(node);615if (!node->dqm) {616dev_err(kfd_device, "Error initializing queue manager\n");617goto device_queue_manager_error;618}619620if (kfd_gws_init(node)) {621dev_err(kfd_device, "Could not allocate %d gws\n",622node->adev->gds.gws_size);623goto gws_error;624}625626if (kfd_resume(node))627goto kfd_resume_error;628629if (kfd_topology_add_device(node)) {630dev_err(kfd_device, "Error adding device to topology\n");631goto kfd_topology_add_device_error;632}633634kfd_smi_init(node);635636return 0;637638kfd_topology_add_device_error:639kfd_resume_error:640gws_error:641device_queue_manager_uninit(node->dqm);642device_queue_manager_error:643kfd_interrupt_exit(node);644kfd_interrupt_error:645if (node->gws)646amdgpu_amdkfd_free_gws(node->adev, node->gws);647648/* Cleanup the node memory here */649kfree(node);650return err;651}652653static void kfd_cleanup_nodes(struct kfd_dev *kfd, unsigned int num_nodes)654{655struct kfd_node *knode;656unsigned int i;657658/*659* flush_work ensures that there are no outstanding660* work-queue items that will access interrupt_ring. New work items661* can't be created because we stopped interrupt handling above.662*/663flush_workqueue(kfd->ih_wq);664destroy_workqueue(kfd->ih_wq);665666for (i = 0; i < num_nodes; i++) {667knode = kfd->nodes[i];668device_queue_manager_uninit(knode->dqm);669kfd_interrupt_exit(knode);670kfd_topology_remove_device(knode);671if (knode->gws)672amdgpu_amdkfd_free_gws(knode->adev, knode->gws);673kfree(knode);674kfd->nodes[i] = NULL;675}676}677678static void kfd_setup_interrupt_bitmap(struct kfd_node *node,679unsigned int kfd_node_idx)680{681struct amdgpu_device *adev = node->adev;682uint32_t xcc_mask = node->xcc_mask;683uint32_t xcc, mapped_xcc;684/*685* Interrupt bitmap is setup for processing interrupts from686* different XCDs and AIDs.687* Interrupt bitmap is defined as follows:688* 1. Bits 0-15 - correspond to the NodeId field.689* Each bit corresponds to NodeId number. For example, if690* a KFD node has interrupt bitmap set to 0x7, then this691* KFD node will process interrupts with NodeId = 0, 1 and 2692* in the IH cookie.693* 2. Bits 16-31 - unused.694*695* Please note that the kfd_node_idx argument passed to this696* function is not related to NodeId field received in the697* IH cookie.698*699* In CPX mode, a KFD node will process an interrupt if:700* - the Node Id matches the corresponding bit set in701* Bits 0-15.702* - AND VMID reported in the interrupt lies within the703* VMID range of the node.704*/705for_each_inst(xcc, xcc_mask) {706mapped_xcc = GET_INST(GC, xcc);707node->interrupt_bitmap |= (mapped_xcc % 2 ? 5 : 3) << (4 * (mapped_xcc / 2));708}709dev_info(kfd_device, "Node: %d, interrupt_bitmap: %x\n", kfd_node_idx,710node->interrupt_bitmap);711}712713bool kgd2kfd_device_init(struct kfd_dev *kfd,714const struct kgd2kfd_shared_resources *gpu_resources)715{716unsigned int size, map_process_packet_size, i;717struct kfd_node *node;718uint32_t first_vmid_kfd, last_vmid_kfd, vmid_num_kfd;719unsigned int max_proc_per_quantum;720int partition_mode;721int xcp_idx;722723kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,724KGD_ENGINE_MEC1);725kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,726KGD_ENGINE_MEC2);727kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,728KGD_ENGINE_SDMA1);729kfd->shared_resources = *gpu_resources;730731kfd->num_nodes = amdgpu_xcp_get_num_xcp(kfd->adev->xcp_mgr);732733if (kfd->num_nodes == 0) {734dev_err(kfd_device,735"KFD num nodes cannot be 0, num_xcc_in_node: %d\n",736kfd->adev->gfx.num_xcc_per_xcp);737goto out;738}739740/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.741* 32 and 64-bit requests are possible and must be742* supported.743*/744kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kfd->adev);745if (!kfd->pci_atomic_requested &&746kfd->device_info.needs_pci_atomics &&747(!kfd->device_info.no_atomic_fw_version ||748kfd->mec_fw_version < kfd->device_info.no_atomic_fw_version)) {749dev_info(kfd_device,750"skipped device %x:%x, PCI rejects atomics %d<%d\n",751kfd->adev->pdev->vendor, kfd->adev->pdev->device,752kfd->mec_fw_version,753kfd->device_info.no_atomic_fw_version);754return false;755}756757first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;758last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;759vmid_num_kfd = last_vmid_kfd - first_vmid_kfd + 1;760761/* For multi-partition capable GPUs, we need special handling for VMIDs762* depending on partition mode.763* In CPX mode, the VMID range needs to be shared between XCDs.764* Additionally, there are 13 VMIDs (3-15) available for KFD. To765* divide them equally, we change starting VMID to 4 and not use766* VMID 3.767* If the VMID range changes for multi-partition capable GPUs, then768* this code MUST be revisited.769*/770if (kfd->adev->xcp_mgr) {771partition_mode = amdgpu_xcp_query_partition_mode(kfd->adev->xcp_mgr,772AMDGPU_XCP_FL_LOCKED);773if (partition_mode == AMDGPU_CPX_PARTITION_MODE &&774kfd->num_nodes != 1) {775vmid_num_kfd /= 2;776first_vmid_kfd = last_vmid_kfd + 1 - vmid_num_kfd*2;777}778}779780/* Verify module parameters regarding mapped process number*/781if (hws_max_conc_proc >= 0)782max_proc_per_quantum = min((u32)hws_max_conc_proc, vmid_num_kfd);783else784max_proc_per_quantum = vmid_num_kfd;785786/* calculate max size of mqds needed for queues */787size = max_num_of_queues_per_device *788kfd->device_info.mqd_size_aligned;789790/*791* calculate max size of runlist packet.792* There can be only 2 packets at once793*/794map_process_packet_size = KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2) ?795sizeof(struct pm4_mes_map_process_aldebaran) :796sizeof(struct pm4_mes_map_process);797size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size +798max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)799+ sizeof(struct pm4_mes_runlist)) * 2;800801/* Add size of HIQ & DIQ */802size += KFD_KERNEL_QUEUE_SIZE * 2;803804/* add another 512KB for all other allocations on gart (HPD, fences) */805size += 512 * 1024;806807if (amdgpu_amdkfd_alloc_gtt_mem(808kfd->adev, size, &kfd->gtt_mem,809&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,810false)) {811dev_err(kfd_device, "Could not allocate %d bytes\n", size);812goto alloc_gtt_mem_failure;813}814815dev_info(kfd_device, "Allocated %d bytes on gart\n", size);816817/* Initialize GTT sa with 512 byte chunk size */818if (kfd_gtt_sa_init(kfd, size, 512) != 0) {819dev_err(kfd_device, "Error initializing gtt sub-allocator\n");820goto kfd_gtt_sa_init_error;821}822823if (kfd_doorbell_init(kfd)) {824dev_err(kfd_device,825"Error initializing doorbell aperture\n");826goto kfd_doorbell_error;827}828829if (amdgpu_use_xgmi_p2p)830kfd->hive_id = kfd->adev->gmc.xgmi.hive_id;831832/*833* For multi-partition capable GPUs, the KFD abstracts all partitions834* within a socket as xGMI connected in the topology so assign a unique835* hive id per device based on the pci device location if device is in836* PCIe mode.837*/838if (!kfd->hive_id && kfd->num_nodes > 1)839kfd->hive_id = pci_dev_id(kfd->adev->pdev);840841kfd->noretry = kfd->adev->gmc.noretry;842843kfd_cwsr_init(kfd);844845dev_info(kfd_device, "Total number of KFD nodes to be created: %d\n",846kfd->num_nodes);847848/* Allocate the KFD nodes */849for (i = 0, xcp_idx = 0; i < kfd->num_nodes; i++) {850node = kzalloc(sizeof(struct kfd_node), GFP_KERNEL);851if (!node)852goto node_alloc_error;853854node->node_id = i;855node->adev = kfd->adev;856node->kfd = kfd;857node->kfd2kgd = kfd->kfd2kgd;858node->vm_info.vmid_num_kfd = vmid_num_kfd;859node->xcp = amdgpu_get_next_xcp(kfd->adev->xcp_mgr, &xcp_idx);860/* TODO : Check if error handling is needed */861if (node->xcp) {862amdgpu_xcp_get_inst_details(node->xcp, AMDGPU_XCP_GFX,863&node->xcc_mask);864++xcp_idx;865} else {866node->xcc_mask =867(1U << NUM_XCC(kfd->adev->gfx.xcc_mask)) - 1;868}869870if (node->xcp) {871dev_info(kfd_device, "KFD node %d partition %d size %lldM\n",872node->node_id, node->xcp->mem_id,873KFD_XCP_MEMORY_SIZE(node->adev, node->node_id) >> 20);874}875876if (partition_mode == AMDGPU_CPX_PARTITION_MODE &&877kfd->num_nodes != 1) {878/* For multi-partition capable GPUs and CPX mode, first879* XCD gets VMID range 4-9 and second XCD gets VMID880* range 10-15.881*/882883node->vm_info.first_vmid_kfd = (i%2 == 0) ?884first_vmid_kfd :885first_vmid_kfd+vmid_num_kfd;886node->vm_info.last_vmid_kfd = (i%2 == 0) ?887last_vmid_kfd-vmid_num_kfd :888last_vmid_kfd;889node->compute_vmid_bitmap =890((0x1 << (node->vm_info.last_vmid_kfd + 1)) - 1) -891((0x1 << (node->vm_info.first_vmid_kfd)) - 1);892} else {893node->vm_info.first_vmid_kfd = first_vmid_kfd;894node->vm_info.last_vmid_kfd = last_vmid_kfd;895node->compute_vmid_bitmap =896gpu_resources->compute_vmid_bitmap;897}898node->max_proc_per_quantum = max_proc_per_quantum;899atomic_set(&node->sram_ecc_flag, 0);900901amdgpu_amdkfd_get_local_mem_info(kfd->adev,902&node->local_mem_info, node->xcp);903904if (kfd->adev->xcp_mgr)905kfd_setup_interrupt_bitmap(node, i);906907/* Initialize the KFD node */908if (kfd_init_node(node)) {909dev_err(kfd_device, "Error initializing KFD node\n");910goto node_init_error;911}912913spin_lock_init(&node->watch_points_lock);914915kfd->nodes[i] = node;916}917918svm_range_set_max_pages(kfd->adev);919920kfd->init_complete = true;921dev_info(kfd_device, "added device %x:%x\n", kfd->adev->pdev->vendor,922kfd->adev->pdev->device);923924pr_debug("Starting kfd with the following scheduling policy %d\n",925node->dqm->sched_policy);926927goto out;928929node_init_error:930node_alloc_error:931kfd_cleanup_nodes(kfd, i);932kfd_doorbell_fini(kfd);933kfd_doorbell_error:934kfd_gtt_sa_fini(kfd);935kfd_gtt_sa_init_error:936amdgpu_amdkfd_free_gtt_mem(kfd->adev, &kfd->gtt_mem);937alloc_gtt_mem_failure:938dev_err(kfd_device,939"device %x:%x NOT added due to errors\n",940kfd->adev->pdev->vendor, kfd->adev->pdev->device);941out:942return kfd->init_complete;943}944945void kgd2kfd_device_exit(struct kfd_dev *kfd)946{947if (kfd->init_complete) {948/* Cleanup KFD nodes */949kfd_cleanup_nodes(kfd, kfd->num_nodes);950/* Cleanup common/shared resources */951kfd_doorbell_fini(kfd);952ida_destroy(&kfd->doorbell_ida);953kfd_gtt_sa_fini(kfd);954amdgpu_amdkfd_free_gtt_mem(kfd->adev, &kfd->gtt_mem);955}956957kfree(kfd);958}959960int kgd2kfd_pre_reset(struct kfd_dev *kfd,961struct amdgpu_reset_context *reset_context)962{963struct kfd_node *node;964int i;965966if (!kfd->init_complete)967return 0;968969for (i = 0; i < kfd->num_nodes; i++) {970node = kfd->nodes[i];971kfd_smi_event_update_gpu_reset(node, false, reset_context);972}973974kgd2kfd_suspend(kfd, true);975976for (i = 0; i < kfd->num_nodes; i++)977kfd_signal_reset_event(kfd->nodes[i]);978979return 0;980}981982/*983* Fix me. KFD won't be able to resume existing process for now.984* We will keep all existing process in a evicted state and985* wait the process to be terminated.986*/987988int kgd2kfd_post_reset(struct kfd_dev *kfd)989{990int ret;991struct kfd_node *node;992int i;993994if (!kfd->init_complete)995return 0;996997for (i = 0; i < kfd->num_nodes; i++) {998ret = kfd_resume(kfd->nodes[i]);999if (ret)1000return ret;1001}10021003mutex_lock(&kfd_processes_mutex);1004--kfd_locked;1005mutex_unlock(&kfd_processes_mutex);10061007for (i = 0; i < kfd->num_nodes; i++) {1008node = kfd->nodes[i];1009atomic_set(&node->sram_ecc_flag, 0);1010kfd_smi_event_update_gpu_reset(node, true, NULL);1011}10121013return 0;1014}10151016bool kfd_is_locked(struct kfd_dev *kfd)1017{1018uint8_t id = 0;1019struct kfd_node *dev;10201021lockdep_assert_held(&kfd_processes_mutex);10221023/* check reset/suspend lock */1024if (kfd_locked > 0)1025return true;10261027if (kfd)1028return kfd->kfd_dev_lock > 0;10291030/* check lock on all cgroup accessible devices */1031while (kfd_topology_enum_kfd_devices(id++, &dev) == 0) {1032if (!dev || kfd_devcgroup_check_permission(dev))1033continue;10341035if (dev->kfd->kfd_dev_lock > 0)1036return true;1037}10381039return false;1040}10411042void kgd2kfd_suspend(struct kfd_dev *kfd, bool suspend_proc)1043{1044struct kfd_node *node;1045int i;10461047if (!kfd->init_complete)1048return;10491050if (suspend_proc)1051kgd2kfd_suspend_process(kfd);10521053for (i = 0; i < kfd->num_nodes; i++) {1054node = kfd->nodes[i];1055node->dqm->ops.stop(node->dqm);1056}1057}10581059int kgd2kfd_resume(struct kfd_dev *kfd, bool resume_proc)1060{1061int ret, i;10621063if (!kfd->init_complete)1064return 0;10651066for (i = 0; i < kfd->num_nodes; i++) {1067ret = kfd_resume(kfd->nodes[i]);1068if (ret)1069return ret;1070}10711072if (resume_proc)1073ret = kgd2kfd_resume_process(kfd);10741075return ret;1076}10771078void kgd2kfd_suspend_process(struct kfd_dev *kfd)1079{1080if (!kfd->init_complete)1081return;10821083mutex_lock(&kfd_processes_mutex);1084/* For first KFD device suspend all the KFD processes */1085if (++kfd_locked == 1)1086kfd_suspend_all_processes();1087mutex_unlock(&kfd_processes_mutex);1088}10891090int kgd2kfd_resume_process(struct kfd_dev *kfd)1091{1092int ret = 0;10931094if (!kfd->init_complete)1095return 0;10961097mutex_lock(&kfd_processes_mutex);1098if (--kfd_locked == 0)1099ret = kfd_resume_all_processes();1100WARN_ONCE(kfd_locked < 0, "KFD suspend / resume ref. error");1101mutex_unlock(&kfd_processes_mutex);11021103return ret;1104}11051106static int kfd_resume(struct kfd_node *node)1107{1108int err = 0;11091110err = node->dqm->ops.start(node->dqm);1111if (err)1112dev_err(kfd_device,1113"Error starting queue manager for device %x:%x\n",1114node->adev->pdev->vendor, node->adev->pdev->device);11151116return err;1117}11181119/* This is called directly from KGD at ISR. */1120void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)1121{1122uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE], i;1123bool is_patched = false;1124unsigned long flags;1125struct kfd_node *node;11261127if (!kfd->init_complete)1128return;11291130if (kfd->device_info.ih_ring_entry_size > sizeof(patched_ihre)) {1131dev_err_once(kfd_device, "Ring entry too small\n");1132return;1133}11341135for (i = 0; i < kfd->num_nodes; i++) {1136/* Race if another thread in b/w1137* kfd_cleanup_nodes and kfree(kfd),1138* when kfd->nodes[i] = NULL1139*/1140if (kfd->nodes[i])1141node = kfd->nodes[i];1142else1143return;11441145spin_lock_irqsave(&node->interrupt_lock, flags);11461147if (node->interrupts_active1148&& interrupt_is_wanted(node, ih_ring_entry,1149patched_ihre, &is_patched)1150&& enqueue_ih_ring_entry(node,1151is_patched ? patched_ihre : ih_ring_entry)) {1152queue_work(node->kfd->ih_wq, &node->interrupt_work);1153spin_unlock_irqrestore(&node->interrupt_lock, flags);1154return;1155}1156spin_unlock_irqrestore(&node->interrupt_lock, flags);1157}11581159}11601161int kgd2kfd_quiesce_mm(struct mm_struct *mm, uint32_t trigger)1162{1163struct kfd_process *p;1164int r;11651166/* Because we are called from arbitrary context (workqueue) as opposed1167* to process context, kfd_process could attempt to exit while we are1168* running so the lookup function increments the process ref count.1169*/1170p = kfd_lookup_process_by_mm(mm);1171if (!p)1172return -ESRCH;11731174WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);1175r = kfd_process_evict_queues(p, trigger);11761177kfd_unref_process(p);1178return r;1179}11801181int kgd2kfd_resume_mm(struct mm_struct *mm)1182{1183struct kfd_process *p;1184int r;11851186/* Because we are called from arbitrary context (workqueue) as opposed1187* to process context, kfd_process could attempt to exit while we are1188* running so the lookup function increments the process ref count.1189*/1190p = kfd_lookup_process_by_mm(mm);1191if (!p)1192return -ESRCH;11931194r = kfd_process_restore_queues(p);11951196kfd_unref_process(p);1197return r;1198}11991200/** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will1201* prepare for safe eviction of KFD BOs that belong to the specified1202* process.1203*1204* @mm: mm_struct that identifies the specified KFD process1205* @fence: eviction fence attached to KFD process BOs1206*1207*/1208int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,1209struct dma_fence *fence)1210{1211struct kfd_process *p;1212unsigned long active_time;1213unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);12141215if (!fence)1216return -EINVAL;12171218if (dma_fence_is_signaled(fence))1219return 0;12201221p = kfd_lookup_process_by_mm(mm);1222if (!p)1223return -ENODEV;12241225if (fence->seqno == p->last_eviction_seqno)1226goto out;12271228p->last_eviction_seqno = fence->seqno;12291230/* Avoid KFD process starvation. Wait for at least1231* PROCESS_ACTIVE_TIME_MS before evicting the process again1232*/1233active_time = get_jiffies_64() - p->last_restore_timestamp;1234if (delay_jiffies > active_time)1235delay_jiffies -= active_time;1236else1237delay_jiffies = 0;12381239/* During process initialization eviction_work.dwork is initialized1240* to kfd_evict_bo_worker1241*/1242WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",1243p->lead_thread->pid, delay_jiffies);1244schedule_delayed_work(&p->eviction_work, delay_jiffies);1245out:1246kfd_unref_process(p);1247return 0;1248}12491250static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,1251unsigned int chunk_size)1252{1253if (WARN_ON(buf_size < chunk_size))1254return -EINVAL;1255if (WARN_ON(buf_size == 0))1256return -EINVAL;1257if (WARN_ON(chunk_size == 0))1258return -EINVAL;12591260kfd->gtt_sa_chunk_size = chunk_size;1261kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;12621263kfd->gtt_sa_bitmap = bitmap_zalloc(kfd->gtt_sa_num_of_chunks,1264GFP_KERNEL);1265if (!kfd->gtt_sa_bitmap)1266return -ENOMEM;12671268pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",1269kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);12701271mutex_init(&kfd->gtt_sa_lock);12721273return 0;1274}12751276static void kfd_gtt_sa_fini(struct kfd_dev *kfd)1277{1278mutex_destroy(&kfd->gtt_sa_lock);1279bitmap_free(kfd->gtt_sa_bitmap);1280}12811282static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,1283unsigned int bit_num,1284unsigned int chunk_size)1285{1286return start_addr + bit_num * chunk_size;1287}12881289static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,1290unsigned int bit_num,1291unsigned int chunk_size)1292{1293return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);1294}12951296int kfd_gtt_sa_allocate(struct kfd_node *node, unsigned int size,1297struct kfd_mem_obj **mem_obj)1298{1299unsigned int found, start_search, cur_size;1300struct kfd_dev *kfd = node->kfd;13011302if (size == 0)1303return -EINVAL;13041305if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)1306return -ENOMEM;13071308*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);1309if (!(*mem_obj))1310return -ENOMEM;13111312pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);13131314start_search = 0;13151316mutex_lock(&kfd->gtt_sa_lock);13171318kfd_gtt_restart_search:1319/* Find the first chunk that is free */1320found = find_next_zero_bit(kfd->gtt_sa_bitmap,1321kfd->gtt_sa_num_of_chunks,1322start_search);13231324pr_debug("Found = %d\n", found);13251326/* If there wasn't any free chunk, bail out */1327if (found == kfd->gtt_sa_num_of_chunks)1328goto kfd_gtt_no_free_chunk;13291330/* Update fields of mem_obj */1331(*mem_obj)->range_start = found;1332(*mem_obj)->range_end = found;1333(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(1334kfd->gtt_start_gpu_addr,1335found,1336kfd->gtt_sa_chunk_size);1337(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(1338kfd->gtt_start_cpu_ptr,1339found,1340kfd->gtt_sa_chunk_size);13411342pr_debug("gpu_addr = %p, cpu_addr = %p\n",1343(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);13441345/* If we need only one chunk, mark it as allocated and get out */1346if (size <= kfd->gtt_sa_chunk_size) {1347pr_debug("Single bit\n");1348__set_bit(found, kfd->gtt_sa_bitmap);1349goto kfd_gtt_out;1350}13511352/* Otherwise, try to see if we have enough contiguous chunks */1353cur_size = size - kfd->gtt_sa_chunk_size;1354do {1355(*mem_obj)->range_end =1356find_next_zero_bit(kfd->gtt_sa_bitmap,1357kfd->gtt_sa_num_of_chunks, ++found);1358/*1359* If next free chunk is not contiguous than we need to1360* restart our search from the last free chunk we found (which1361* wasn't contiguous to the previous ones1362*/1363if ((*mem_obj)->range_end != found) {1364start_search = found;1365goto kfd_gtt_restart_search;1366}13671368/*1369* If we reached end of buffer, bail out with error1370*/1371if (found == kfd->gtt_sa_num_of_chunks)1372goto kfd_gtt_no_free_chunk;13731374/* Check if we don't need another chunk */1375if (cur_size <= kfd->gtt_sa_chunk_size)1376cur_size = 0;1377else1378cur_size -= kfd->gtt_sa_chunk_size;13791380} while (cur_size > 0);13811382pr_debug("range_start = %d, range_end = %d\n",1383(*mem_obj)->range_start, (*mem_obj)->range_end);13841385/* Mark the chunks as allocated */1386bitmap_set(kfd->gtt_sa_bitmap, (*mem_obj)->range_start,1387(*mem_obj)->range_end - (*mem_obj)->range_start + 1);13881389kfd_gtt_out:1390mutex_unlock(&kfd->gtt_sa_lock);1391return 0;13921393kfd_gtt_no_free_chunk:1394pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);1395mutex_unlock(&kfd->gtt_sa_lock);1396kfree(*mem_obj);1397return -ENOMEM;1398}13991400int kfd_gtt_sa_free(struct kfd_node *node, struct kfd_mem_obj *mem_obj)1401{1402struct kfd_dev *kfd = node->kfd;14031404/* Act like kfree when trying to free a NULL object */1405if (!mem_obj)1406return 0;14071408pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",1409mem_obj, mem_obj->range_start, mem_obj->range_end);14101411mutex_lock(&kfd->gtt_sa_lock);14121413/* Mark the chunks as free */1414bitmap_clear(kfd->gtt_sa_bitmap, mem_obj->range_start,1415mem_obj->range_end - mem_obj->range_start + 1);14161417mutex_unlock(&kfd->gtt_sa_lock);14181419kfree(mem_obj);1420return 0;1421}14221423void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)1424{1425/*1426* TODO: Currently update SRAM ECC flag for first node.1427* This needs to be updated later when we can1428* identify SRAM ECC error on other nodes also.1429*/1430if (kfd)1431atomic_inc(&kfd->nodes[0]->sram_ecc_flag);1432}14331434void kfd_inc_compute_active(struct kfd_node *node)1435{1436if (atomic_inc_return(&node->kfd->compute_profile) == 1)1437amdgpu_amdkfd_set_compute_idle(node->adev, false);1438}14391440void kfd_dec_compute_active(struct kfd_node *node)1441{1442int count = atomic_dec_return(&node->kfd->compute_profile);14431444if (count == 0)1445amdgpu_amdkfd_set_compute_idle(node->adev, true);1446WARN_ONCE(count < 0, "Compute profile ref. count error");1447}14481449static bool kfd_compute_active(struct kfd_node *node)1450{1451if (atomic_read(&node->kfd->compute_profile))1452return true;1453return false;1454}14551456void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask)1457{1458/*1459* TODO: For now, raise the throttling event only on first node.1460* This will need to change after we are able to determine1461* which node raised the throttling event.1462*/1463if (kfd && kfd->init_complete)1464kfd_smi_event_update_thermal_throttling(kfd->nodes[0],1465throttle_bitmask);1466}14671468/* kfd_get_num_sdma_engines returns the number of PCIe optimized SDMA and1469* kfd_get_num_xgmi_sdma_engines returns the number of XGMI SDMA.1470* When the device has more than two engines, we reserve two for PCIe to enable1471* full-duplex and the rest are used as XGMI.1472*/1473unsigned int kfd_get_num_sdma_engines(struct kfd_node *node)1474{1475/* If XGMI is not supported, all SDMA engines are PCIe */1476if (!node->adev->gmc.xgmi.supported)1477return node->adev->sdma.num_instances/(int)node->kfd->num_nodes;14781479return min(node->adev->sdma.num_instances/(int)node->kfd->num_nodes, 2);1480}14811482unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_node *node)1483{1484/* After reserved for PCIe, the rest of engines are XGMI */1485return node->adev->sdma.num_instances/(int)node->kfd->num_nodes -1486kfd_get_num_sdma_engines(node);1487}14881489int kgd2kfd_check_and_lock_kfd(struct kfd_dev *kfd)1490{1491struct kfd_process *p;1492int r = 0, temp, idx;14931494mutex_lock(&kfd_processes_mutex);14951496/* kfd_processes_count is per kfd_dev, return -EBUSY without1497* further check1498*/1499if (!!atomic_read(&kfd->kfd_processes_count)) {1500pr_debug("process_wq_release not finished\n");1501r = -EBUSY;1502goto out;1503}15041505if (hash_empty(kfd_processes_table) && !kfd_is_locked(kfd))1506goto out;15071508/* fail under system reset/resume or kfd device is partition switching. */1509if (kfd_is_locked(kfd)) {1510r = -EBUSY;1511goto out;1512}15131514/*1515* ensure all running processes are cgroup excluded from device before mode switch.1516* i.e. no pdd was created on the process socket.1517*/1518idx = srcu_read_lock(&kfd_processes_srcu);1519hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {1520int i;15211522for (i = 0; i < p->n_pdds; i++) {1523if (p->pdds[i]->dev->kfd != kfd)1524continue;15251526r = -EBUSY;1527goto proc_check_unlock;1528}1529}15301531proc_check_unlock:1532srcu_read_unlock(&kfd_processes_srcu, idx);1533out:1534if (!r)1535++kfd->kfd_dev_lock;1536mutex_unlock(&kfd_processes_mutex);15371538return r;1539}15401541void kgd2kfd_unlock_kfd(struct kfd_dev *kfd)1542{1543mutex_lock(&kfd_processes_mutex);1544--kfd->kfd_dev_lock;1545mutex_unlock(&kfd_processes_mutex);1546}15471548int kgd2kfd_start_sched(struct kfd_dev *kfd, uint32_t node_id)1549{1550struct kfd_node *node;1551int ret;15521553if (!kfd->init_complete)1554return 0;15551556if (node_id >= kfd->num_nodes) {1557dev_warn(kfd->adev->dev, "Invalid node ID: %u exceeds %u\n",1558node_id, kfd->num_nodes - 1);1559return -EINVAL;1560}1561node = kfd->nodes[node_id];15621563ret = node->dqm->ops.unhalt(node->dqm);1564if (ret)1565dev_err(kfd_device, "Error in starting scheduler\n");15661567return ret;1568}15691570int kgd2kfd_start_sched_all_nodes(struct kfd_dev *kfd)1571{1572struct kfd_node *node;1573int i, r;15741575if (!kfd->init_complete)1576return 0;15771578for (i = 0; i < kfd->num_nodes; i++) {1579node = kfd->nodes[i];1580r = node->dqm->ops.unhalt(node->dqm);1581if (r) {1582dev_err(kfd_device, "Error in starting scheduler\n");1583return r;1584}1585}1586return 0;1587}15881589int kgd2kfd_stop_sched(struct kfd_dev *kfd, uint32_t node_id)1590{1591struct kfd_node *node;15921593if (!kfd->init_complete)1594return 0;15951596if (node_id >= kfd->num_nodes) {1597dev_warn(kfd->adev->dev, "Invalid node ID: %u exceeds %u\n",1598node_id, kfd->num_nodes - 1);1599return -EINVAL;1600}16011602node = kfd->nodes[node_id];1603return node->dqm->ops.halt(node->dqm);1604}16051606int kgd2kfd_stop_sched_all_nodes(struct kfd_dev *kfd)1607{1608struct kfd_node *node;1609int i, r;16101611if (!kfd->init_complete)1612return 0;16131614for (i = 0; i < kfd->num_nodes; i++) {1615node = kfd->nodes[i];1616r = node->dqm->ops.halt(node->dqm);1617if (r)1618return r;1619}1620return 0;1621}16221623bool kgd2kfd_compute_active(struct kfd_dev *kfd, uint32_t node_id)1624{1625struct kfd_node *node;16261627if (!kfd->init_complete)1628return false;16291630if (node_id >= kfd->num_nodes) {1631dev_warn(kfd->adev->dev, "Invalid node ID: %u exceeds %u\n",1632node_id, kfd->num_nodes - 1);1633return false;1634}16351636node = kfd->nodes[node_id];16371638return kfd_compute_active(node);1639}16401641/**1642* kgd2kfd_vmfault_fast_path() - KFD vm page fault interrupt handling fast path for gmc v91643* @adev: amdgpu device1644* @entry: vm fault interrupt vector1645* @retry_fault: if this is retry fault1646*1647* retry fault -1648* with CAM enabled, adev primary ring1649* | gmc_v9_0_process_interrupt()1650* adev soft_ring1651* | gmc_v9_0_process_interrupt() worker failed to recover page fault1652* KFD node ih_fifo1653* | KFD interrupt_wq worker1654* kfd_signal_vm_fault_event1655*1656* without CAM, adev primary ring11657* | gmc_v9_0_process_interrupt worker failed to recvoer page fault1658* KFD node ih_fifo1659* | KFD interrupt_wq worker1660* kfd_signal_vm_fault_event1661*1662* no-retry fault -1663* adev primary ring1664* | gmc_v9_0_process_interrupt()1665* KFD node ih_fifo1666* | KFD interrupt_wq worker1667* kfd_signal_vm_fault_event1668*1669* fast path - After kfd_signal_vm_fault_event, gmc_v9_0_process_interrupt drop the page fault1670* of same process, don't copy interrupt to KFD node ih_fifo.1671* With gdb debugger enabled, need convert the retry fault to no-retry fault for1672* debugger, cannot use the fast path.1673*1674* Return:1675* true - use the fast path to handle this fault1676* false - use normal path to handle it1677*/1678bool kgd2kfd_vmfault_fast_path(struct amdgpu_device *adev, struct amdgpu_iv_entry *entry,1679bool retry_fault)1680{1681struct kfd_process *p;1682u32 cam_index;16831684if (entry->ih == &adev->irq.ih_soft || entry->ih == &adev->irq.ih1) {1685p = kfd_lookup_process_by_pasid(entry->pasid, NULL);1686if (!p)1687return true;16881689if (p->gpu_page_fault && !p->debug_trap_enabled) {1690if (retry_fault && adev->irq.retry_cam_enabled) {1691cam_index = entry->src_data[2] & 0x3ff;1692WDOORBELL32(adev->irq.retry_cam_doorbell_index, cam_index);1693}16941695kfd_unref_process(p);1696return true;1697}16981699/*1700* This is the first page fault, set flag and then signal user space1701*/1702p->gpu_page_fault = true;1703kfd_unref_process(p);1704}1705return false;1706}17071708#if defined(CONFIG_DEBUG_FS)17091710/* This function will send a package to HIQ to hang the HWS1711* which will trigger a GPU reset and bring the HWS back to normal state1712*/1713int kfd_debugfs_hang_hws(struct kfd_node *dev)1714{1715if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {1716pr_err("HWS is not enabled");1717return -EINVAL;1718}17191720if (dev->kfd->shared_resources.enable_mes) {1721dev_err(dev->adev->dev, "Inducing MES hang is not supported\n");1722return -EINVAL;1723}17241725return dqm_debugfs_hang_hws(dev->dqm);1726}17271728#endif172917301731