From nobody Wed Nov 27 04:38:14 2024 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 header.i=@intel.com; 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=none dis=none) header.from=linux.intel.com ARC-Seal: i=1; a=rsa-sha256; t=1701355228; cv=none; d=zohomail.com; s=zohoarc; b=CBU5arKl2ykQdGFGJVqAGwqprfJxvjCBP5SrlJCqDMK+HdOl8mKKe+npVUzYQak4262dVtyRAt9cEkB+24x3GbN0dJ9n/N6bcu2RbpjjJY7Rja/wtvP4UFHD3YN5oyOWSDmxDbPTtfixaFlxHYw9bt3c/PtpfC0b/tsrz7nqbW8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1701355228; 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=Z4L3nI7wR/SVhNHr/XVqJtZAzytK6Ic2h7XDN6tz6YM=; b=Wh4LRbx2+EAMpzZr8kxjn9/SGbr6IW0vEiHSDQaNEp5C5HaoFvHpONZxRaJycA7WTbV0YR1R/dLSiH6KL9LNL8XPpimUuCbtPQHeCWmlTcnI2YqyIqA6cnKr0hZw86Sj2xYGPdgbDY0lQ05YNtOIVyVe+1FZdsE/X9SzX3yP5Hc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; 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=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1701355228926458.01951154788867; Thu, 30 Nov 2023 06:40:28 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.644824.1006518 (Exim 4.92) (envelope-from ) id 1r8iCm-0004lm-LY; Thu, 30 Nov 2023 14:39:52 +0000 Received: by outflank-mailman (output) from mailman id 644824.1006518; Thu, 30 Nov 2023 14:39:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r8iCl-0004Wc-Ek; Thu, 30 Nov 2023 14:39:51 +0000 Received: by outflank-mailman (input) for mailman id 644824; Thu, 30 Nov 2023 14:36:48 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r8i9n-0000pO-Uk for xen-devel@lists.xenproject.org; Thu, 30 Nov 2023 14:36:47 +0000 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id e3151d4e-8f8d-11ee-9b0f-b553b5be7939; Thu, 30 Nov 2023 15:36:46 +0100 (CET) Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2023 06:36:42 -0800 Received: from liuzhao-optiplex-7080.sh.intel.com ([10.239.160.36]) by orsmga005.jf.intel.com with ESMTP; 30 Nov 2023 06:36:33 -0800 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: e3151d4e-8f8d-11ee-9b0f-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701355006; x=1732891006; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=q0lEBoFOr9b0x5amAD6sBLj9mxt45Se06XmWgm72SHY=; b=aCv8TiwjThHi9DnlHxz3vJ6b31n05Kofu3wuWgXE+6wb9hgy0Ph2xAPj u/w/zHbvjh5AAhDM3ImFz7XunalIXo/RoUc9F6tfGPSNV/+Eb3o/jLzU+ CjwlRnyPQoKXa1Fs1cZ6QVzhDPEI4ZQsANedF0CpRnMDqYNXjlRTVnJ2Y f7cmY4UtQlSBzNJBq/qN7C4Offqwpi24SZxaWOxP0Yhu4SErFyva6ODlH /DKXRQzAqALUzB2co7zqh/SpYuk0QY74Tt41+hwgpKHMdM1U1Yb0o+hxC zZgNIcn1pJkTQwO2s+6Ngdj9hlYeuUHKOp5EMFQcmxogs/EvWDQleartj w==; X-IronPort-AV: E=McAfee;i="6600,9927,10910"; a="479532773" X-IronPort-AV: E=Sophos;i="6.04,239,1695711600"; d="scan'208";a="479532773" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10910"; a="942730530" X-IronPort-AV: E=Sophos;i="6.04,239,1695711600"; d="scan'208";a="942730530" From: Zhao Liu To: Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Richard Henderson , "Michael S . Tsirkin" , Jason Wang , Nicholas Piggin , Daniel Henrique Barboza , Igor Mammedov , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Barrat?= , David Gibson , Harsh Prateek Bora , Stefano Stabellini , Anthony Perard , Paul Durrant , Gerd Hoffmann , Peter Maydell , Alistair Francis , "Edgar E . Iglesias" , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Bin Meng , Palmer Dabbelt , Weiwei Li , Liu Zhiwei , qemu-devel@nongnu.org, kvm@vger.kernel.org, qemu-ppc@nongnu.org, xen-devel@lists.xenproject.org, qemu-arm@nongnu.org, qemu-riscv@nongnu.org, qemu-s390x@nongnu.org Cc: Nina Schoetterl-Glausch , Thomas Huth , Zhiyuan Lv , Zhenyu Wang , Yongwei Ma , Zhao Liu Subject: [RFC 39/41] hw/i386: Add the interface to search parent for QOM topology Date: Thu, 30 Nov 2023 22:42:01 +0800 Message-Id: <20231130144203.2307629-40-zhao1.liu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231130144203.2307629-1-zhao1.liu@linux.intel.com> References: <20231130144203.2307629-1-zhao1.liu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1701355230054000002 Content-Type: text/plain; charset="utf-8" From: Zhao Liu QOM topology needs to search parent cpu-core for hotplugged CPU to create topology child<> property in qdev_set_id(). This process is before x86_cpu_pre_plug(), thus place 2 helpers x86_cpu_assign_apic_id() and x86_cpu_assign_topo_id() in x86_cpu_search_parent_pre_plug() to help get the correct topology sub indexes. Then x86_cpu_search_parent_pre_plug() searches the parent cpu-core with these sub indexes. Signed-off-by: Zhao Liu --- hw/i386/x86.c | 128 +++++++++++++++++++++++++++++++++++++++--- include/hw/i386/x86.h | 3 + 2 files changed, 122 insertions(+), 9 deletions(-) diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 04edd6de6aeb..595d4365fdd1 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -460,16 +460,18 @@ void x86_cpu_pre_plug(HotplugHandler *hotplug_dev, return; } =20 - /* if 'address' properties socket-id/core-id/thread-id are not set, se= t them - * so that machine_query_hotpluggable_cpus would show correct values + /* + * possible_cpus_qom_granu means the QOM topology support. + * + * TODO: Drop the "!mc->smp_props.possible_cpus_qom_granu" case when + * i386 completes QOM topology support. */ - /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizef= n() - * once -smp refactoring is complete and there will be CPU private - * CPUState::nr_cores and CPUState::nr_threads fields instead of globa= ls */ - x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids); - x86_cpu_assign_topo_id(cpu, &topo_ids, errp); - if (*errp) { - return; + if (!mc->smp_props.possible_cpus_qom_granu) { + x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids); + x86_cpu_assign_topo_id(cpu, &topo_ids, errp); + if (*errp) { + return; + } } =20 if (hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) && @@ -484,6 +486,114 @@ void x86_cpu_pre_plug(HotplugHandler *hotplug_dev, numa_cpu_pre_plug(cpu_slot, dev, errp); } =20 +static int x86_cpu_get_topo_id_by_level(X86CPU *cpu, + CPUTopoLevel level) +{ + switch (level) { + case CPU_TOPO_THREAD: + return cpu->thread_id; + case CPU_TOPO_CORE: + return cpu->core_id; + case CPU_TOPO_DIE: + return cpu->die_id; + case CPU_TOPO_SOCKET: + return cpu->socket_id; + default: + g_assert_not_reached(); + } + + return -1; +} + +typedef struct SearchCoreCb { + X86CPU *cpu; + CPUTopoState *parent; + int id; +} SearchCoreCb; + +static int x86_cpu_search_parent_core(CPUTopoState *topo, + void *opaque) +{ + SearchCoreCb *cb =3D opaque; + CPUTopoLevel level =3D CPU_TOPO_LEVEL(topo); + + cb->parent =3D topo; + cb->id =3D x86_cpu_get_topo_id_by_level(cb->cpu, level); + + if (cb->id =3D=3D topo->index) { + if (level =3D=3D CPU_TOPO_CORE) { + return TOPO_FOREACH_END; + } + return TOPO_FOREACH_CONTINUE; + } + return TOPO_FOREACH_SIBLING; +} + +Object *x86_cpu_search_parent_pre_plug(CPUTopoState *topo, + CPUTopoState *root, + Error **errp) +{ + int ret; + SearchCoreCb cb; + X86CPUTopoIDs topo_ids; + X86CPUTopoInfo topo_info; + X86CPU *cpu =3D X86_CPU(topo); + CPUSlot *slot =3D CPU_SLOT(root); + MachineState *ms =3D slot->ms; + DECLARE_BITMAP(foreach_bitmap, USER_AVAIL_LEVEL_NUM); + + topo_info.dies_per_pkg =3D ms->smp.dies; + topo_info.cores_per_die =3D ms->smp.cores; + topo_info.threads_per_core =3D ms->smp.threads; + + if (cpu->apic_id =3D=3D UNASSIGNED_APIC_ID) { + x86_cpu_assign_apic_id(ms, cpu, &topo_ids, &topo_info, errp); + if (*errp) { + return NULL; + } + } else { + /* + * if 'address' properties socket-id/core-id/thread-id are not set, + * set them so that machine_query_hotpluggable_cpus would show + * correct values. + * + * TODO: move socket_id/core_id/thread_id checks into + * x86_cpu_realizefn() once -smp refactoring is complete and there + * will be CPU private CPUState::nr_cores and CPUState::nr_threads + * fields instead of globals. + */ + x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids); + } + + x86_cpu_assign_topo_id(cpu, &topo_ids, errp); + if (*errp) { + return NULL; + } + + cb.cpu =3D cpu; + cb.parent =3D NULL; + cb.id =3D -1; + bitmap_fill(foreach_bitmap, USER_AVAIL_LEVEL_NUM); + clear_bit(CPU_TOPO_UNKNOWN, foreach_bitmap); + clear_bit(CPU_TOPO_THREAD, foreach_bitmap); + + ret =3D cpu_topo_child_foreach_recursive(root, foreach_bitmap, + x86_cpu_search_parent_core, &cb= ); + if (ret !=3D TOPO_FOREACH_END) { + g_autofree char *search_info =3D NULL; + + search_info =3D !cb.parent ? g_strdup("") : + g_strdup_printf(" for %s level with id: %d", + cpu_topo_level_to_string(CPU_TOPO_LEVEL(cb.parent)), cb.id); + error_setg(errp, "Can't find parent%s", search_info); + return NULL; + } + + /* Keep the index of CPU topology device the same as the thread_id. */ + topo->index =3D cpu->thread_id; + return OBJECT(cb.parent); +} + CpuInstanceProperties x86_cpu_index_to_props(MachineState *ms, unsigned cpu_index) { diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h index 19e9f93fe286..e8c9ddc36359 100644 --- a/include/hw/i386/x86.h +++ b/include/hw/i386/x86.h @@ -104,6 +104,9 @@ int64_t x86_get_default_cpu_node_id(const MachineState = *ms, int idx); const CPUArchIdList *x86_possible_cpu_arch_ids(MachineState *ms); CPUArchId *x86_find_cpu_slot(MachineState *ms, uint32_t id, int *idx); void x86_rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count); +Object *x86_cpu_search_parent_pre_plug(CPUTopoState *topo, + CPUTopoState *root, + Error **errp); void x86_cpu_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp); void x86_cpu_plug(HotplugHandler *hotplug_dev, --=20 2.34.1