Path: blob/master/drivers/gpu/drm/amd/amdkfd/kfd_device.c
26516 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);497498return kfd;499}500501static void kfd_cwsr_init(struct kfd_dev *kfd)502{503if (cwsr_enable && kfd->device_info.supports_cwsr) {504if (KFD_GC_VERSION(kfd) < IP_VERSION(9, 0, 1)) {505BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex)506> KFD_CWSR_TMA_OFFSET);507kfd->cwsr_isa = cwsr_trap_gfx8_hex;508kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);509} else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)) {510BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex)511> KFD_CWSR_TMA_OFFSET);512kfd->cwsr_isa = cwsr_trap_arcturus_hex;513kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);514} else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)) {515BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex)516> KFD_CWSR_TMA_OFFSET);517kfd->cwsr_isa = cwsr_trap_aldebaran_hex;518kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex);519} else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 3) ||520KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 4)) {521BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_4_3_hex)522> KFD_CWSR_TMA_OFFSET);523kfd->cwsr_isa = cwsr_trap_gfx9_4_3_hex;524kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_4_3_hex);525} else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 5, 0)) {526BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_5_0_hex) > PAGE_SIZE);527kfd->cwsr_isa = cwsr_trap_gfx9_5_0_hex;528kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_5_0_hex);529} else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 1, 1)) {530BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex)531> KFD_CWSR_TMA_OFFSET);532kfd->cwsr_isa = cwsr_trap_gfx9_hex;533kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);534} else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 3, 0)) {535BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex)536> KFD_CWSR_TMA_OFFSET);537kfd->cwsr_isa = cwsr_trap_nv1x_hex;538kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);539} else if (KFD_GC_VERSION(kfd) < IP_VERSION(11, 0, 0)) {540BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex)541> KFD_CWSR_TMA_OFFSET);542kfd->cwsr_isa = cwsr_trap_gfx10_hex;543kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);544} else if (KFD_GC_VERSION(kfd) < IP_VERSION(12, 0, 0)) {545/* The gfx11 cwsr trap handler must fit inside a single546page. */547BUILD_BUG_ON(sizeof(cwsr_trap_gfx11_hex) > PAGE_SIZE);548kfd->cwsr_isa = cwsr_trap_gfx11_hex;549kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx11_hex);550} else {551BUILD_BUG_ON(sizeof(cwsr_trap_gfx12_hex)552> KFD_CWSR_TMA_OFFSET);553kfd->cwsr_isa = cwsr_trap_gfx12_hex;554kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx12_hex);555}556557kfd->cwsr_enabled = true;558}559}560561static int kfd_gws_init(struct kfd_node *node)562{563int ret = 0;564struct kfd_dev *kfd = node->kfd;565uint32_t mes_rev = node->adev->mes.sched_version & AMDGPU_MES_VERSION_MASK;566567if (node->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)568return 0;569570if (hws_gws_support || (KFD_IS_SOC15(node) &&571((KFD_GC_VERSION(node) == IP_VERSION(9, 0, 1)572&& kfd->mec2_fw_version >= 0x81b3) ||573(KFD_GC_VERSION(node) <= IP_VERSION(9, 4, 0)574&& kfd->mec2_fw_version >= 0x1b3) ||575(KFD_GC_VERSION(node) == IP_VERSION(9, 4, 1)576&& kfd->mec2_fw_version >= 0x30) ||577(KFD_GC_VERSION(node) == IP_VERSION(9, 4, 2)578&& kfd->mec2_fw_version >= 0x28) ||579(KFD_GC_VERSION(node) == IP_VERSION(9, 4, 3) ||580KFD_GC_VERSION(node) == IP_VERSION(9, 4, 4)) ||581(KFD_GC_VERSION(node) == IP_VERSION(9, 5, 0)) ||582(KFD_GC_VERSION(node) >= IP_VERSION(10, 3, 0)583&& KFD_GC_VERSION(node) < IP_VERSION(11, 0, 0)584&& kfd->mec2_fw_version >= 0x6b) ||585(KFD_GC_VERSION(node) >= IP_VERSION(11, 0, 0)586&& KFD_GC_VERSION(node) < IP_VERSION(12, 0, 0)587&& mes_rev >= 68) ||588(KFD_GC_VERSION(node) >= IP_VERSION(12, 0, 0))))) {589if (KFD_GC_VERSION(node) >= IP_VERSION(12, 0, 0))590node->adev->gds.gws_size = 64;591ret = amdgpu_amdkfd_alloc_gws(node->adev,592node->adev->gds.gws_size, &node->gws);593}594595return ret;596}597598static void kfd_smi_init(struct kfd_node *dev)599{600INIT_LIST_HEAD(&dev->smi_clients);601spin_lock_init(&dev->smi_lock);602}603604static int kfd_init_node(struct kfd_node *node)605{606int err = -1;607608if (kfd_interrupt_init(node)) {609dev_err(kfd_device, "Error initializing interrupts\n");610goto kfd_interrupt_error;611}612613node->dqm = device_queue_manager_init(node);614if (!node->dqm) {615dev_err(kfd_device, "Error initializing queue manager\n");616goto device_queue_manager_error;617}618619if (kfd_gws_init(node)) {620dev_err(kfd_device, "Could not allocate %d gws\n",621node->adev->gds.gws_size);622goto gws_error;623}624625if (kfd_resume(node))626goto kfd_resume_error;627628if (kfd_topology_add_device(node)) {629dev_err(kfd_device, "Error adding device to topology\n");630goto kfd_topology_add_device_error;631}632633kfd_smi_init(node);634635return 0;636637kfd_topology_add_device_error:638kfd_resume_error:639gws_error:640device_queue_manager_uninit(node->dqm);641device_queue_manager_error:642kfd_interrupt_exit(node);643kfd_interrupt_error:644if (node->gws)645amdgpu_amdkfd_free_gws(node->adev, node->gws);646647/* Cleanup the node memory here */648kfree(node);649return err;650}651652static void kfd_cleanup_nodes(struct kfd_dev *kfd, unsigned int num_nodes)653{654struct kfd_node *knode;655unsigned int i;656657/*658* flush_work ensures that there are no outstanding659* work-queue items that will access interrupt_ring. New work items660* can't be created because we stopped interrupt handling above.661*/662flush_workqueue(kfd->ih_wq);663destroy_workqueue(kfd->ih_wq);664665for (i = 0; i < num_nodes; i++) {666knode = kfd->nodes[i];667device_queue_manager_uninit(knode->dqm);668kfd_interrupt_exit(knode);669kfd_topology_remove_device(knode);670if (knode->gws)671amdgpu_amdkfd_free_gws(knode->adev, knode->gws);672kfree(knode);673kfd->nodes[i] = NULL;674}675}676677static void kfd_setup_interrupt_bitmap(struct kfd_node *node,678unsigned int kfd_node_idx)679{680struct amdgpu_device *adev = node->adev;681uint32_t xcc_mask = node->xcc_mask;682uint32_t xcc, mapped_xcc;683/*684* Interrupt bitmap is setup for processing interrupts from685* different XCDs and AIDs.686* Interrupt bitmap is defined as follows:687* 1. Bits 0-15 - correspond to the NodeId field.688* Each bit corresponds to NodeId number. For example, if689* a KFD node has interrupt bitmap set to 0x7, then this690* KFD node will process interrupts with NodeId = 0, 1 and 2691* in the IH cookie.692* 2. Bits 16-31 - unused.693*694* Please note that the kfd_node_idx argument passed to this695* function is not related to NodeId field received in the696* IH cookie.697*698* In CPX mode, a KFD node will process an interrupt if:699* - the Node Id matches the corresponding bit set in700* Bits 0-15.701* - AND VMID reported in the interrupt lies within the702* VMID range of the node.703*/704for_each_inst(xcc, xcc_mask) {705mapped_xcc = GET_INST(GC, xcc);706node->interrupt_bitmap |= (mapped_xcc % 2 ? 5 : 3) << (4 * (mapped_xcc / 2));707}708dev_info(kfd_device, "Node: %d, interrupt_bitmap: %x\n", kfd_node_idx,709node->interrupt_bitmap);710}711712bool kgd2kfd_device_init(struct kfd_dev *kfd,713const struct kgd2kfd_shared_resources *gpu_resources)714{715unsigned int size, map_process_packet_size, i;716struct kfd_node *node;717uint32_t first_vmid_kfd, last_vmid_kfd, vmid_num_kfd;718unsigned int max_proc_per_quantum;719int partition_mode;720int xcp_idx;721722kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,723KGD_ENGINE_MEC1);724kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,725KGD_ENGINE_MEC2);726kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,727KGD_ENGINE_SDMA1);728kfd->shared_resources = *gpu_resources;729730kfd->num_nodes = amdgpu_xcp_get_num_xcp(kfd->adev->xcp_mgr);731732if (kfd->num_nodes == 0) {733dev_err(kfd_device,734"KFD num nodes cannot be 0, num_xcc_in_node: %d\n",735kfd->adev->gfx.num_xcc_per_xcp);736goto out;737}738739/* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.740* 32 and 64-bit requests are possible and must be741* supported.742*/743kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kfd->adev);744if (!kfd->pci_atomic_requested &&745kfd->device_info.needs_pci_atomics &&746(!kfd->device_info.no_atomic_fw_version ||747kfd->mec_fw_version < kfd->device_info.no_atomic_fw_version)) {748dev_info(kfd_device,749"skipped device %x:%x, PCI rejects atomics %d<%d\n",750kfd->adev->pdev->vendor, kfd->adev->pdev->device,751kfd->mec_fw_version,752kfd->device_info.no_atomic_fw_version);753return false;754}755756first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;757last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;758vmid_num_kfd = last_vmid_kfd - first_vmid_kfd + 1;759760/* For multi-partition capable GPUs, we need special handling for VMIDs761* depending on partition mode.762* In CPX mode, the VMID range needs to be shared between XCDs.763* Additionally, there are 13 VMIDs (3-15) available for KFD. To764* divide them equally, we change starting VMID to 4 and not use765* VMID 3.766* If the VMID range changes for multi-partition capable GPUs, then767* this code MUST be revisited.768*/769if (kfd->adev->xcp_mgr) {770partition_mode = amdgpu_xcp_query_partition_mode(kfd->adev->xcp_mgr,771AMDGPU_XCP_FL_LOCKED);772if (partition_mode == AMDGPU_CPX_PARTITION_MODE &&773kfd->num_nodes != 1) {774vmid_num_kfd /= 2;775first_vmid_kfd = last_vmid_kfd + 1 - vmid_num_kfd*2;776}777}778779/* Verify module parameters regarding mapped process number*/780if (hws_max_conc_proc >= 0)781max_proc_per_quantum = min((u32)hws_max_conc_proc, vmid_num_kfd);782else783max_proc_per_quantum = vmid_num_kfd;784785/* calculate max size of mqds needed for queues */786size = max_num_of_queues_per_device *787kfd->device_info.mqd_size_aligned;788789/*790* calculate max size of runlist packet.791* There can be only 2 packets at once792*/793map_process_packet_size = KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2) ?794sizeof(struct pm4_mes_map_process_aldebaran) :795sizeof(struct pm4_mes_map_process);796size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size +797max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)798+ sizeof(struct pm4_mes_runlist)) * 2;799800/* Add size of HIQ & DIQ */801size += KFD_KERNEL_QUEUE_SIZE * 2;802803/* add another 512KB for all other allocations on gart (HPD, fences) */804size += 512 * 1024;805806if (amdgpu_amdkfd_alloc_gtt_mem(807kfd->adev, size, &kfd->gtt_mem,808&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,809false)) {810dev_err(kfd_device, "Could not allocate %d bytes\n", size);811goto alloc_gtt_mem_failure;812}813814dev_info(kfd_device, "Allocated %d bytes on gart\n", size);815816/* Initialize GTT sa with 512 byte chunk size */817if (kfd_gtt_sa_init(kfd, size, 512) != 0) {818dev_err(kfd_device, "Error initializing gtt sub-allocator\n");819goto kfd_gtt_sa_init_error;820}821822if (kfd_doorbell_init(kfd)) {823dev_err(kfd_device,824"Error initializing doorbell aperture\n");825goto kfd_doorbell_error;826}827828if (amdgpu_use_xgmi_p2p)829kfd->hive_id = kfd->adev->gmc.xgmi.hive_id;830831/*832* For multi-partition capable GPUs, the KFD abstracts all partitions833* within a socket as xGMI connected in the topology so assign a unique834* hive id per device based on the pci device location if device is in835* PCIe mode.836*/837if (!kfd->hive_id && kfd->num_nodes > 1)838kfd->hive_id = pci_dev_id(kfd->adev->pdev);839840kfd->noretry = kfd->adev->gmc.noretry;841842kfd_cwsr_init(kfd);843844dev_info(kfd_device, "Total number of KFD nodes to be created: %d\n",845kfd->num_nodes);846847/* Allocate the KFD nodes */848for (i = 0, xcp_idx = 0; i < kfd->num_nodes; i++) {849node = kzalloc(sizeof(struct kfd_node), GFP_KERNEL);850if (!node)851goto node_alloc_error;852853node->node_id = i;854node->adev = kfd->adev;855node->kfd = kfd;856node->kfd2kgd = kfd->kfd2kgd;857node->vm_info.vmid_num_kfd = vmid_num_kfd;858node->xcp = amdgpu_get_next_xcp(kfd->adev->xcp_mgr, &xcp_idx);859/* TODO : Check if error handling is needed */860if (node->xcp) {861amdgpu_xcp_get_inst_details(node->xcp, AMDGPU_XCP_GFX,862&node->xcc_mask);863++xcp_idx;864} else {865node->xcc_mask =866(1U << NUM_XCC(kfd->adev->gfx.xcc_mask)) - 1;867}868869if (node->xcp) {870dev_info(kfd_device, "KFD node %d partition %d size %lldM\n",871node->node_id, node->xcp->mem_id,872KFD_XCP_MEMORY_SIZE(node->adev, node->node_id) >> 20);873}874875if (partition_mode == AMDGPU_CPX_PARTITION_MODE &&876kfd->num_nodes != 1) {877/* For multi-partition capable GPUs and CPX mode, first878* XCD gets VMID range 4-9 and second XCD gets VMID879* range 10-15.880*/881882node->vm_info.first_vmid_kfd = (i%2 == 0) ?883first_vmid_kfd :884first_vmid_kfd+vmid_num_kfd;885node->vm_info.last_vmid_kfd = (i%2 == 0) ?886last_vmid_kfd-vmid_num_kfd :887last_vmid_kfd;888node->compute_vmid_bitmap =889((0x1 << (node->vm_info.last_vmid_kfd + 1)) - 1) -890((0x1 << (node->vm_info.first_vmid_kfd)) - 1);891} else {892node->vm_info.first_vmid_kfd = first_vmid_kfd;893node->vm_info.last_vmid_kfd = last_vmid_kfd;894node->compute_vmid_bitmap =895gpu_resources->compute_vmid_bitmap;896}897node->max_proc_per_quantum = max_proc_per_quantum;898atomic_set(&node->sram_ecc_flag, 0);899900amdgpu_amdkfd_get_local_mem_info(kfd->adev,901&node->local_mem_info, node->xcp);902903if (kfd->adev->xcp_mgr)904kfd_setup_interrupt_bitmap(node, i);905906/* Initialize the KFD node */907if (kfd_init_node(node)) {908dev_err(kfd_device, "Error initializing KFD node\n");909goto node_init_error;910}911912spin_lock_init(&node->watch_points_lock);913914kfd->nodes[i] = node;915}916917svm_range_set_max_pages(kfd->adev);918919kfd->init_complete = true;920dev_info(kfd_device, "added device %x:%x\n", kfd->adev->pdev->vendor,921kfd->adev->pdev->device);922923pr_debug("Starting kfd with the following scheduling policy %d\n",924node->dqm->sched_policy);925926goto out;927928node_init_error:929node_alloc_error:930kfd_cleanup_nodes(kfd, i);931kfd_doorbell_fini(kfd);932kfd_doorbell_error:933kfd_gtt_sa_fini(kfd);934kfd_gtt_sa_init_error:935amdgpu_amdkfd_free_gtt_mem(kfd->adev, &kfd->gtt_mem);936alloc_gtt_mem_failure:937dev_err(kfd_device,938"device %x:%x NOT added due to errors\n",939kfd->adev->pdev->vendor, kfd->adev->pdev->device);940out:941return kfd->init_complete;942}943944void kgd2kfd_device_exit(struct kfd_dev *kfd)945{946if (kfd->init_complete) {947/* Cleanup KFD nodes */948kfd_cleanup_nodes(kfd, kfd->num_nodes);949/* Cleanup common/shared resources */950kfd_doorbell_fini(kfd);951ida_destroy(&kfd->doorbell_ida);952kfd_gtt_sa_fini(kfd);953amdgpu_amdkfd_free_gtt_mem(kfd->adev, &kfd->gtt_mem);954}955956kfree(kfd);957}958959int kgd2kfd_pre_reset(struct kfd_dev *kfd,960struct amdgpu_reset_context *reset_context)961{962struct kfd_node *node;963int i;964965if (!kfd->init_complete)966return 0;967968for (i = 0; i < kfd->num_nodes; i++) {969node = kfd->nodes[i];970kfd_smi_event_update_gpu_reset(node, false, reset_context);971}972973kgd2kfd_suspend(kfd, true);974975for (i = 0; i < kfd->num_nodes; i++)976kfd_signal_reset_event(kfd->nodes[i]);977978return 0;979}980981/*982* Fix me. KFD won't be able to resume existing process for now.983* We will keep all existing process in a evicted state and984* wait the process to be terminated.985*/986987int kgd2kfd_post_reset(struct kfd_dev *kfd)988{989int ret;990struct kfd_node *node;991int i;992993if (!kfd->init_complete)994return 0;995996for (i = 0; i < kfd->num_nodes; i++) {997ret = kfd_resume(kfd->nodes[i]);998if (ret)999return ret;1000}10011002mutex_lock(&kfd_processes_mutex);1003--kfd_locked;1004mutex_unlock(&kfd_processes_mutex);10051006for (i = 0; i < kfd->num_nodes; i++) {1007node = kfd->nodes[i];1008atomic_set(&node->sram_ecc_flag, 0);1009kfd_smi_event_update_gpu_reset(node, true, NULL);1010}10111012return 0;1013}10141015bool kfd_is_locked(struct kfd_dev *kfd)1016{1017uint8_t id = 0;1018struct kfd_node *dev;10191020lockdep_assert_held(&kfd_processes_mutex);10211022/* check reset/suspend lock */1023if (kfd_locked > 0)1024return true;10251026if (kfd)1027return kfd->kfd_dev_lock > 0;10281029/* check lock on all cgroup accessible devices */1030while (kfd_topology_enum_kfd_devices(id++, &dev) == 0) {1031if (!dev || kfd_devcgroup_check_permission(dev))1032continue;10331034if (dev->kfd->kfd_dev_lock > 0)1035return true;1036}10371038return false;1039}10401041void kgd2kfd_suspend(struct kfd_dev *kfd, bool suspend_proc)1042{1043struct kfd_node *node;1044int i;10451046if (!kfd->init_complete)1047return;10481049if (suspend_proc)1050kgd2kfd_suspend_process(kfd);10511052for (i = 0; i < kfd->num_nodes; i++) {1053node = kfd->nodes[i];1054node->dqm->ops.stop(node->dqm);1055}1056}10571058int kgd2kfd_resume(struct kfd_dev *kfd, bool resume_proc)1059{1060int ret, i;10611062if (!kfd->init_complete)1063return 0;10641065for (i = 0; i < kfd->num_nodes; i++) {1066ret = kfd_resume(kfd->nodes[i]);1067if (ret)1068return ret;1069}10701071if (resume_proc)1072ret = kgd2kfd_resume_process(kfd);10731074return ret;1075}10761077void kgd2kfd_suspend_process(struct kfd_dev *kfd)1078{1079if (!kfd->init_complete)1080return;10811082mutex_lock(&kfd_processes_mutex);1083/* For first KFD device suspend all the KFD processes */1084if (++kfd_locked == 1)1085kfd_suspend_all_processes();1086mutex_unlock(&kfd_processes_mutex);1087}10881089int kgd2kfd_resume_process(struct kfd_dev *kfd)1090{1091int ret = 0;10921093if (!kfd->init_complete)1094return 0;10951096mutex_lock(&kfd_processes_mutex);1097if (--kfd_locked == 0)1098ret = kfd_resume_all_processes();1099WARN_ONCE(kfd_locked < 0, "KFD suspend / resume ref. error");1100mutex_unlock(&kfd_processes_mutex);11011102return ret;1103}11041105static int kfd_resume(struct kfd_node *node)1106{1107int err = 0;11081109err = node->dqm->ops.start(node->dqm);1110if (err)1111dev_err(kfd_device,1112"Error starting queue manager for device %x:%x\n",1113node->adev->pdev->vendor, node->adev->pdev->device);11141115return err;1116}11171118/* This is called directly from KGD at ISR. */1119void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)1120{1121uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE], i;1122bool is_patched = false;1123unsigned long flags;1124struct kfd_node *node;11251126if (!kfd->init_complete)1127return;11281129if (kfd->device_info.ih_ring_entry_size > sizeof(patched_ihre)) {1130dev_err_once(kfd_device, "Ring entry too small\n");1131return;1132}11331134for (i = 0; i < kfd->num_nodes; i++) {1135node = kfd->nodes[i];1136spin_lock_irqsave(&node->interrupt_lock, flags);11371138if (node->interrupts_active1139&& interrupt_is_wanted(node, ih_ring_entry,1140patched_ihre, &is_patched)1141&& enqueue_ih_ring_entry(node,1142is_patched ? patched_ihre : ih_ring_entry)) {1143queue_work(node->kfd->ih_wq, &node->interrupt_work);1144spin_unlock_irqrestore(&node->interrupt_lock, flags);1145return;1146}1147spin_unlock_irqrestore(&node->interrupt_lock, flags);1148}11491150}11511152int kgd2kfd_quiesce_mm(struct mm_struct *mm, uint32_t trigger)1153{1154struct kfd_process *p;1155int r;11561157/* Because we are called from arbitrary context (workqueue) as opposed1158* to process context, kfd_process could attempt to exit while we are1159* running so the lookup function increments the process ref count.1160*/1161p = kfd_lookup_process_by_mm(mm);1162if (!p)1163return -ESRCH;11641165WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);1166r = kfd_process_evict_queues(p, trigger);11671168kfd_unref_process(p);1169return r;1170}11711172int kgd2kfd_resume_mm(struct mm_struct *mm)1173{1174struct kfd_process *p;1175int r;11761177/* Because we are called from arbitrary context (workqueue) as opposed1178* to process context, kfd_process could attempt to exit while we are1179* running so the lookup function increments the process ref count.1180*/1181p = kfd_lookup_process_by_mm(mm);1182if (!p)1183return -ESRCH;11841185r = kfd_process_restore_queues(p);11861187kfd_unref_process(p);1188return r;1189}11901191/** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will1192* prepare for safe eviction of KFD BOs that belong to the specified1193* process.1194*1195* @mm: mm_struct that identifies the specified KFD process1196* @fence: eviction fence attached to KFD process BOs1197*1198*/1199int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,1200struct dma_fence *fence)1201{1202struct kfd_process *p;1203unsigned long active_time;1204unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);12051206if (!fence)1207return -EINVAL;12081209if (dma_fence_is_signaled(fence))1210return 0;12111212p = kfd_lookup_process_by_mm(mm);1213if (!p)1214return -ENODEV;12151216if (fence->seqno == p->last_eviction_seqno)1217goto out;12181219p->last_eviction_seqno = fence->seqno;12201221/* Avoid KFD process starvation. Wait for at least1222* PROCESS_ACTIVE_TIME_MS before evicting the process again1223*/1224active_time = get_jiffies_64() - p->last_restore_timestamp;1225if (delay_jiffies > active_time)1226delay_jiffies -= active_time;1227else1228delay_jiffies = 0;12291230/* During process initialization eviction_work.dwork is initialized1231* to kfd_evict_bo_worker1232*/1233WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",1234p->lead_thread->pid, delay_jiffies);1235schedule_delayed_work(&p->eviction_work, delay_jiffies);1236out:1237kfd_unref_process(p);1238return 0;1239}12401241static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,1242unsigned int chunk_size)1243{1244if (WARN_ON(buf_size < chunk_size))1245return -EINVAL;1246if (WARN_ON(buf_size == 0))1247return -EINVAL;1248if (WARN_ON(chunk_size == 0))1249return -EINVAL;12501251kfd->gtt_sa_chunk_size = chunk_size;1252kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;12531254kfd->gtt_sa_bitmap = bitmap_zalloc(kfd->gtt_sa_num_of_chunks,1255GFP_KERNEL);1256if (!kfd->gtt_sa_bitmap)1257return -ENOMEM;12581259pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",1260kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);12611262mutex_init(&kfd->gtt_sa_lock);12631264return 0;1265}12661267static void kfd_gtt_sa_fini(struct kfd_dev *kfd)1268{1269mutex_destroy(&kfd->gtt_sa_lock);1270bitmap_free(kfd->gtt_sa_bitmap);1271}12721273static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,1274unsigned int bit_num,1275unsigned int chunk_size)1276{1277return start_addr + bit_num * chunk_size;1278}12791280static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,1281unsigned int bit_num,1282unsigned int chunk_size)1283{1284return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);1285}12861287int kfd_gtt_sa_allocate(struct kfd_node *node, unsigned int size,1288struct kfd_mem_obj **mem_obj)1289{1290unsigned int found, start_search, cur_size;1291struct kfd_dev *kfd = node->kfd;12921293if (size == 0)1294return -EINVAL;12951296if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)1297return -ENOMEM;12981299*mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);1300if (!(*mem_obj))1301return -ENOMEM;13021303pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);13041305start_search = 0;13061307mutex_lock(&kfd->gtt_sa_lock);13081309kfd_gtt_restart_search:1310/* Find the first chunk that is free */1311found = find_next_zero_bit(kfd->gtt_sa_bitmap,1312kfd->gtt_sa_num_of_chunks,1313start_search);13141315pr_debug("Found = %d\n", found);13161317/* If there wasn't any free chunk, bail out */1318if (found == kfd->gtt_sa_num_of_chunks)1319goto kfd_gtt_no_free_chunk;13201321/* Update fields of mem_obj */1322(*mem_obj)->range_start = found;1323(*mem_obj)->range_end = found;1324(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(1325kfd->gtt_start_gpu_addr,1326found,1327kfd->gtt_sa_chunk_size);1328(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(1329kfd->gtt_start_cpu_ptr,1330found,1331kfd->gtt_sa_chunk_size);13321333pr_debug("gpu_addr = %p, cpu_addr = %p\n",1334(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);13351336/* If we need only one chunk, mark it as allocated and get out */1337if (size <= kfd->gtt_sa_chunk_size) {1338pr_debug("Single bit\n");1339__set_bit(found, kfd->gtt_sa_bitmap);1340goto kfd_gtt_out;1341}13421343/* Otherwise, try to see if we have enough contiguous chunks */1344cur_size = size - kfd->gtt_sa_chunk_size;1345do {1346(*mem_obj)->range_end =1347find_next_zero_bit(kfd->gtt_sa_bitmap,1348kfd->gtt_sa_num_of_chunks, ++found);1349/*1350* If next free chunk is not contiguous than we need to1351* restart our search from the last free chunk we found (which1352* wasn't contiguous to the previous ones1353*/1354if ((*mem_obj)->range_end != found) {1355start_search = found;1356goto kfd_gtt_restart_search;1357}13581359/*1360* If we reached end of buffer, bail out with error1361*/1362if (found == kfd->gtt_sa_num_of_chunks)1363goto kfd_gtt_no_free_chunk;13641365/* Check if we don't need another chunk */1366if (cur_size <= kfd->gtt_sa_chunk_size)1367cur_size = 0;1368else1369cur_size -= kfd->gtt_sa_chunk_size;13701371} while (cur_size > 0);13721373pr_debug("range_start = %d, range_end = %d\n",1374(*mem_obj)->range_start, (*mem_obj)->range_end);13751376/* Mark the chunks as allocated */1377bitmap_set(kfd->gtt_sa_bitmap, (*mem_obj)->range_start,1378(*mem_obj)->range_end - (*mem_obj)->range_start + 1);13791380kfd_gtt_out:1381mutex_unlock(&kfd->gtt_sa_lock);1382return 0;13831384kfd_gtt_no_free_chunk:1385pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);1386mutex_unlock(&kfd->gtt_sa_lock);1387kfree(*mem_obj);1388return -ENOMEM;1389}13901391int kfd_gtt_sa_free(struct kfd_node *node, struct kfd_mem_obj *mem_obj)1392{1393struct kfd_dev *kfd = node->kfd;13941395/* Act like kfree when trying to free a NULL object */1396if (!mem_obj)1397return 0;13981399pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",1400mem_obj, mem_obj->range_start, mem_obj->range_end);14011402mutex_lock(&kfd->gtt_sa_lock);14031404/* Mark the chunks as free */1405bitmap_clear(kfd->gtt_sa_bitmap, mem_obj->range_start,1406mem_obj->range_end - mem_obj->range_start + 1);14071408mutex_unlock(&kfd->gtt_sa_lock);14091410kfree(mem_obj);1411return 0;1412}14131414void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)1415{1416/*1417* TODO: Currently update SRAM ECC flag for first node.1418* This needs to be updated later when we can1419* identify SRAM ECC error on other nodes also.1420*/1421if (kfd)1422atomic_inc(&kfd->nodes[0]->sram_ecc_flag);1423}14241425void kfd_inc_compute_active(struct kfd_node *node)1426{1427if (atomic_inc_return(&node->kfd->compute_profile) == 1)1428amdgpu_amdkfd_set_compute_idle(node->adev, false);1429}14301431void kfd_dec_compute_active(struct kfd_node *node)1432{1433int count = atomic_dec_return(&node->kfd->compute_profile);14341435if (count == 0)1436amdgpu_amdkfd_set_compute_idle(node->adev, true);1437WARN_ONCE(count < 0, "Compute profile ref. count error");1438}14391440static bool kfd_compute_active(struct kfd_node *node)1441{1442if (atomic_read(&node->kfd->compute_profile))1443return true;1444return false;1445}14461447void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask)1448{1449/*1450* TODO: For now, raise the throttling event only on first node.1451* This will need to change after we are able to determine1452* which node raised the throttling event.1453*/1454if (kfd && kfd->init_complete)1455kfd_smi_event_update_thermal_throttling(kfd->nodes[0],1456throttle_bitmask);1457}14581459/* kfd_get_num_sdma_engines returns the number of PCIe optimized SDMA and1460* kfd_get_num_xgmi_sdma_engines returns the number of XGMI SDMA.1461* When the device has more than two engines, we reserve two for PCIe to enable1462* full-duplex and the rest are used as XGMI.1463*/1464unsigned int kfd_get_num_sdma_engines(struct kfd_node *node)1465{1466/* If XGMI is not supported, all SDMA engines are PCIe */1467if (!node->adev->gmc.xgmi.supported)1468return node->adev->sdma.num_instances/(int)node->kfd->num_nodes;14691470return min(node->adev->sdma.num_instances/(int)node->kfd->num_nodes, 2);1471}14721473unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_node *node)1474{1475/* After reserved for PCIe, the rest of engines are XGMI */1476return node->adev->sdma.num_instances/(int)node->kfd->num_nodes -1477kfd_get_num_sdma_engines(node);1478}14791480int kgd2kfd_check_and_lock_kfd(struct kfd_dev *kfd)1481{1482struct kfd_process *p;1483int r = 0, temp, idx;14841485mutex_lock(&kfd_processes_mutex);14861487if (hash_empty(kfd_processes_table) && !kfd_is_locked(kfd))1488goto out;14891490/* fail under system reset/resume or kfd device is partition switching. */1491if (kfd_is_locked(kfd)) {1492r = -EBUSY;1493goto out;1494}14951496/*1497* ensure all running processes are cgroup excluded from device before mode switch.1498* i.e. no pdd was created on the process socket.1499*/1500idx = srcu_read_lock(&kfd_processes_srcu);1501hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {1502int i;15031504for (i = 0; i < p->n_pdds; i++) {1505if (p->pdds[i]->dev->kfd != kfd)1506continue;15071508r = -EBUSY;1509goto proc_check_unlock;1510}1511}15121513proc_check_unlock:1514srcu_read_unlock(&kfd_processes_srcu, idx);1515out:1516if (!r)1517++kfd->kfd_dev_lock;1518mutex_unlock(&kfd_processes_mutex);15191520return r;1521}15221523void kgd2kfd_unlock_kfd(struct kfd_dev *kfd)1524{1525mutex_lock(&kfd_processes_mutex);1526--kfd->kfd_dev_lock;1527mutex_unlock(&kfd_processes_mutex);1528}15291530int kgd2kfd_start_sched(struct kfd_dev *kfd, uint32_t node_id)1531{1532struct kfd_node *node;1533int ret;15341535if (!kfd->init_complete)1536return 0;15371538if (node_id >= kfd->num_nodes) {1539dev_warn(kfd->adev->dev, "Invalid node ID: %u exceeds %u\n",1540node_id, kfd->num_nodes - 1);1541return -EINVAL;1542}1543node = kfd->nodes[node_id];15441545ret = node->dqm->ops.unhalt(node->dqm);1546if (ret)1547dev_err(kfd_device, "Error in starting scheduler\n");15481549return ret;1550}15511552int kgd2kfd_stop_sched(struct kfd_dev *kfd, uint32_t node_id)1553{1554struct kfd_node *node;15551556if (!kfd->init_complete)1557return 0;15581559if (node_id >= kfd->num_nodes) {1560dev_warn(kfd->adev->dev, "Invalid node ID: %u exceeds %u\n",1561node_id, kfd->num_nodes - 1);1562return -EINVAL;1563}15641565node = kfd->nodes[node_id];1566return node->dqm->ops.halt(node->dqm);1567}15681569bool kgd2kfd_compute_active(struct kfd_dev *kfd, uint32_t node_id)1570{1571struct kfd_node *node;15721573if (!kfd->init_complete)1574return false;15751576if (node_id >= kfd->num_nodes) {1577dev_warn(kfd->adev->dev, "Invalid node ID: %u exceeds %u\n",1578node_id, kfd->num_nodes - 1);1579return false;1580}15811582node = kfd->nodes[node_id];15831584return kfd_compute_active(node);1585}15861587/**1588* kgd2kfd_vmfault_fast_path() - KFD vm page fault interrupt handling fast path for gmc v91589* @adev: amdgpu device1590* @entry: vm fault interrupt vector1591* @retry_fault: if this is retry fault1592*1593* retry fault -1594* with CAM enabled, adev primary ring1595* | gmc_v9_0_process_interrupt()1596* adev soft_ring1597* | gmc_v9_0_process_interrupt() worker failed to recover page fault1598* KFD node ih_fifo1599* | KFD interrupt_wq worker1600* kfd_signal_vm_fault_event1601*1602* without CAM, adev primary ring11603* | gmc_v9_0_process_interrupt worker failed to recvoer page fault1604* KFD node ih_fifo1605* | KFD interrupt_wq worker1606* kfd_signal_vm_fault_event1607*1608* no-retry fault -1609* adev primary ring1610* | gmc_v9_0_process_interrupt()1611* KFD node ih_fifo1612* | KFD interrupt_wq worker1613* kfd_signal_vm_fault_event1614*1615* fast path - After kfd_signal_vm_fault_event, gmc_v9_0_process_interrupt drop the page fault1616* of same process, don't copy interrupt to KFD node ih_fifo.1617* With gdb debugger enabled, need convert the retry fault to no-retry fault for1618* debugger, cannot use the fast path.1619*1620* Return:1621* true - use the fast path to handle this fault1622* false - use normal path to handle it1623*/1624bool kgd2kfd_vmfault_fast_path(struct amdgpu_device *adev, struct amdgpu_iv_entry *entry,1625bool retry_fault)1626{1627struct kfd_process *p;1628u32 cam_index;16291630if (entry->ih == &adev->irq.ih_soft || entry->ih == &adev->irq.ih1) {1631p = kfd_lookup_process_by_pasid(entry->pasid, NULL);1632if (!p)1633return true;16341635if (p->gpu_page_fault && !p->debug_trap_enabled) {1636if (retry_fault && adev->irq.retry_cam_enabled) {1637cam_index = entry->src_data[2] & 0x3ff;1638WDOORBELL32(adev->irq.retry_cam_doorbell_index, cam_index);1639}16401641kfd_unref_process(p);1642return true;1643}16441645/*1646* This is the first page fault, set flag and then signal user space1647*/1648p->gpu_page_fault = true;1649kfd_unref_process(p);1650}1651return false;1652}16531654#if defined(CONFIG_DEBUG_FS)16551656/* This function will send a package to HIQ to hang the HWS1657* which will trigger a GPU reset and bring the HWS back to normal state1658*/1659int kfd_debugfs_hang_hws(struct kfd_node *dev)1660{1661if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {1662pr_err("HWS is not enabled");1663return -EINVAL;1664}16651666if (dev->kfd->shared_resources.enable_mes) {1667dev_err(dev->adev->dev, "Inducing MES hang is not supported\n");1668return -EINVAL;1669}16701671return dqm_debugfs_hang_hws(dev->dqm);1672}16731674#endif167516761677