From nobody Sat Feb 7 06:45:14 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1715172004; cv=none; d=zohomail.com; s=zohoarc; b=A8mulxIQiJ8GWa/4qbzng54I8DsQ2BUPzhEJ3qnvQ8kj+Y3WHj+3ilEzbFWc4uJZyUUhkD8O4yYwzbCXnOxAq69+9lDY3oNaVqGlUVCKJWFlC+kFa1XPvy+MQLTcHpTbRFyd5h9han3Wv/rqS2zkqeIGjE6eMolTi4X69U25emw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1715172004; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Tjb9wDk03eu5cy1/rNtEigMH1xNhBsbk5Mon31Q8thQ=; b=LJXDRIzkuEFrpKh9CLLC6/q6YI1WdAMIQG+MwN70g2gz4LuLNh+zSZZQ4mkCnVCilisMMlh80LUqDQFTngUr9Nt3ffMUvwupTzkAlXA/NmeH/fHuj30om7zjLdYr7VqLXol+pVhOar30ZB9O9UJUe3QirpOXFWIR/YqP1a4/YrI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1715172004317663.0619080462637; Wed, 8 May 2024 05:40:04 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.718780.1121303 (Exim 4.92) (envelope-from ) id 1s4gaB-0003TH-VS; Wed, 08 May 2024 12:39:39 +0000 Received: by outflank-mailman (output) from mailman id 718780.1121303; Wed, 08 May 2024 12:39:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1s4gaB-0003SD-Ov; Wed, 08 May 2024 12:39:39 +0000 Received: by outflank-mailman (input) for mailman id 718780; Wed, 08 May 2024 12:39:38 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1s4gaA-0002b0-Jf for xen-devel@lists.xenproject.org; Wed, 08 May 2024 12:39:38 +0000 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [2a00:1450:4864:20::62d]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 0750ef9e-0d38-11ef-909c-e314d9c70b13; Wed, 08 May 2024 14:39:36 +0200 (CEST) Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-a59c04839caso977235566b.2 for ; Wed, 08 May 2024 05:39:36 -0700 (PDT) Received: from EMEAENGAAD19049.citrite.net (default-46-102-197-194.interdsl.co.uk. [46.102.197.194]) by smtp.gmail.com with ESMTPSA id uj4-20020a170907c98400b00a599f876c28sm5984439ejc.38.2024.05.08.05.39.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 05:39:35 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 0750ef9e-0d38-11ef-909c-e314d9c70b13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1715171976; x=1715776776; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Tjb9wDk03eu5cy1/rNtEigMH1xNhBsbk5Mon31Q8thQ=; b=E3lIERaoiBhvhymIlJVY5fSiUamqJXXkB5MeK37T9+Lx48NFtdYICzQ5oaGslclVBg 0V1hW36VSnVNJuY1Nl0q+gh6slUTMmURmdyMo9u6bEbMVyMJm6Ff56l/BTNhXf0sTJMe t7TUGtNZNupX5YFJF5kwurW9uUFVlv4ExCGI4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715171976; x=1715776776; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Tjb9wDk03eu5cy1/rNtEigMH1xNhBsbk5Mon31Q8thQ=; b=v34JS0yPekgUZVYkb2ZJaQ3JsRs6ErcrNXdTRwUlyrBzA19wlg569afnmNxhcMwdm5 WZ/ioguIFu8dLz/2/4CVaQGCntF6IyR7UrUG2znToYZVmAnG+90yjC69SO4qW+RqBCGJ KqN8yQ2Ds+IpPeswnhImumqFZe3qlffHkdBw/yz3ie6zrzTonD5xoqdAeob74k/9JkBp CiQRGVnfGe0WRPAr4n+3X3mCm2qHFrLZfjOVNHKifUzASV0zHkOzHp9G698Biy2nR3qO QlzVULXgKXJaHOd2pYQelfjGYn4hZYcoIq6vzxyvx6k857EKaSSm6ZUoMVINCoy2yPGi qjGA== X-Gm-Message-State: AOJu0YzsjjURn4l0VRqukhY+zkrG/asJAZFMxKNFE5MAaoqvCgm98vBK jHRkkFH1637MSEVzlQt10Amj9ZMlTUrNZ2jvZWxXev1LUTHLxTBrBkdyfBhlIG6SRx1kyGnZhCs b X-Google-Smtp-Source: AGHT+IF9b75aKMImWVHAuQOHLUdGMkpVk6WnMpMg8aFYo7LFMr0V61yRnpql50wsVtLQWkTMQRkUOQ== X-Received: by 2002:a17:907:3e13:b0:a59:ca33:6845 with SMTP id a640c23a62f3a-a59fb9d2c7emr178006266b.75.1715171975798; Wed, 08 May 2024 05:39:35 -0700 (PDT) From: Alejandro Vallejo To: Xen-devel Cc: Alejandro Vallejo , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Anthony PERARD Subject: [PATCH v2 5/8] tools/hvmloader: Retrieve (x2)APIC IDs from the APs themselves Date: Wed, 8 May 2024 13:39:24 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1715172005145100007 Content-Type: text/plain; charset="utf-8" Make it so the APs expose their own APIC IDs in a LUT. We can use that LUT = to populate the MADT, decoupling the algorithm that relates CPU IDs and APIC I= Ds from hvmloader. While at this also remove ap_callin, as writing the APIC ID may serve the s= ame purpose. Signed-off-by: Alejandro Vallejo --- v2: * New patch. Replaces adding cpu policy to hvmloader in v1. --- tools/firmware/hvmloader/config.h | 6 ++++- tools/firmware/hvmloader/hvmloader.c | 4 +-- tools/firmware/hvmloader/smp.c | 40 +++++++++++++++++++++++----- tools/firmware/hvmloader/util.h | 5 ++++ xen/arch/x86/include/asm/hvm/hvm.h | 1 + 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/tools/firmware/hvmloader/config.h b/tools/firmware/hvmloader/c= onfig.h index c82adf6dc508..edf6fa9c908c 100644 --- a/tools/firmware/hvmloader/config.h +++ b/tools/firmware/hvmloader/config.h @@ -4,6 +4,8 @@ #include #include =20 +#include + enum virtual_vga { VGA_none, VGA_std, VGA_cirrus, VGA_pt }; extern enum virtual_vga virtual_vga; =20 @@ -49,8 +51,10 @@ extern uint8_t ioapic_version; =20 #define IOAPIC_ID 0x01 =20 +extern uint32_t CPU_TO_X2APICID[HVM_MAX_VCPUS]; + #define LAPIC_BASE_ADDRESS 0xfee00000 -#define LAPIC_ID(vcpu_id) ((vcpu_id) * 2) +#define LAPIC_ID(vcpu_id) (CPU_TO_X2APICID[(vcpu_id)]) =20 #define PCI_ISA_DEVFN 0x08 /* dev 1, fn 0 */ #define PCI_ISA_IRQ_MASK 0x0c20U /* ISA IRQs 5,10,11 are PCI connected = */ diff --git a/tools/firmware/hvmloader/hvmloader.c b/tools/firmware/hvmloade= r/hvmloader.c index c58841e5b556..1eba92229925 100644 --- a/tools/firmware/hvmloader/hvmloader.c +++ b/tools/firmware/hvmloader/hvmloader.c @@ -342,11 +342,11 @@ int main(void) =20 printf("CPU speed is %u MHz\n", get_cpu_mhz()); =20 + smp_initialise(); + apic_setup(); pci_setup(); =20 - smp_initialise(); - perform_tests(); =20 if ( bios->bios_info_setup ) diff --git a/tools/firmware/hvmloader/smp.c b/tools/firmware/hvmloader/smp.c index a668f15d7e1f..4d75f239c2f5 100644 --- a/tools/firmware/hvmloader/smp.c +++ b/tools/firmware/hvmloader/smp.c @@ -29,7 +29,34 @@ =20 #include =20 -static int ap_callin, ap_cpuid; +static int ap_cpuid; + +/** + * Lookup table of x2APIC IDs. + * + * Each entry is populated its respective CPU as they come online. This is= required + * for generating the MADT with minimal assumptions about ID relationships. + */ +uint32_t CPU_TO_X2APICID[HVM_MAX_VCPUS]; + +static uint32_t read_apic_id(void) +{ + uint32_t apic_id; + + cpuid(1, NULL, &apic_id, NULL, NULL); + apic_id >>=3D 24; + + /* + * APIC IDs over 255 are represented by 255 in leaf 1 and are meant to= be + * read from topology leaves instead. Xen exposes x2APIC IDs in leaf 0= xb, + * but only if the x2APIC feature is present. If there are that many C= PUs + * it's guaranteed to be there so we can avoid checking for it specifi= cally + */ + if ( apic_id =3D=3D 255 ) + cpuid(0xb, NULL, NULL, NULL, &apic_id); + + return apic_id; +} =20 static void ap_start(void) { @@ -37,12 +64,12 @@ static void ap_start(void) cacheattr_init(); printf("done.\n"); =20 + wmb(); + ACCESS_ONCE(CPU_TO_X2APICID[ap_cpuid]) =3D read_apic_id(); + if ( !ap_cpuid ) return; =20 - wmb(); - ap_callin =3D 1; - while ( 1 ) asm volatile ( "hlt" ); } @@ -86,10 +113,11 @@ static void boot_cpu(unsigned int cpu) BUG(); =20 /* - * Wait for the secondary processor to complete initialisation. + * Wait for the secondary processor to complete initialisation, + * which is signaled by its x2APIC ID being writted to the LUT. * Do not touch shared resources meanwhile. */ - while ( !ap_callin ) + while ( !ACCESS_ONCE(CPU_TO_X2APICID[cpu]) ) cpu_relax(); =20 /* Take the secondary processor offline. */ diff --git a/tools/firmware/hvmloader/util.h b/tools/firmware/hvmloader/uti= l.h index 14078bde1e30..51e9003bc615 100644 --- a/tools/firmware/hvmloader/util.h +++ b/tools/firmware/hvmloader/util.h @@ -23,6 +23,11 @@ enum { #define __STR(...) #__VA_ARGS__ #define STR(...) __STR(__VA_ARGS__) =20 +#define __ACCESS_ONCE(x) ({ \ + (void)(typeof(x))0; /* Scalar typecheck. */ \ + (volatile typeof(x) *)&(x); }) +#define ACCESS_ONCE(x) (*__ACCESS_ONCE(x)) + /* GDT selector values. */ #define SEL_CODE16 0x0008 #define SEL_DATA16 0x0010 diff --git a/xen/arch/x86/include/asm/hvm/hvm.h b/xen/arch/x86/include/asm/= hvm/hvm.h index 84911f3ebcb4..6c005f0b0b38 100644 --- a/xen/arch/x86/include/asm/hvm/hvm.h +++ b/xen/arch/x86/include/asm/hvm/hvm.h @@ -16,6 +16,7 @@ #include #include #include +#include =20 struct pirq; /* needed by pi_update_irte */ =20 --=20 2.34.1